blob: 2cfd2d4db6dba99cb1f4fa97ba9d0d1f054690d9 [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
Dan Gohman2f67df72009-09-03 17:18:51 +000060// Disable16Bit - 16-bit operations typically have a larger encoding than
61// corresponding 32-bit instructions, and 16-bit code is slow on some
62// processors. This is an experimental flag to disable 16-bit operations
63// (which forces them to be Legalized to 32-bit operations).
64static cl::opt<bool>
65Disable16Bit("disable-16bit", cl::Hidden,
66 cl::desc("Disable use of 16-bit instructions"));
67
Evan Cheng10e86422008-04-25 19:11:04 +000068// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000069static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000070 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000071
Chris Lattnerf0144122009-07-28 03:13:23 +000072static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
73 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
74 default: llvm_unreachable("unknown subtarget type");
75 case X86Subtarget::isDarwin:
Bill Wendling757e75b2010-03-15 19:04:37 +000076 if (TM.getSubtarget<X86Subtarget>().is64Bit())
77 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000078 return new TargetLoweringObjectFileMachO();
Chris Lattnerf0144122009-07-28 03:13:23 +000079 case X86Subtarget::isELF:
Anton Korobeynikov9184b252010-02-15 22:35:59 +000080 if (TM.getSubtarget<X86Subtarget>().is64Bit())
81 return new X8664_ELFTargetObjectFile(TM);
82 return new X8632_ELFTargetObjectFile(TM);
Chris Lattnerf0144122009-07-28 03:13:23 +000083 case X86Subtarget::isMingw:
84 case X86Subtarget::isCygwin:
85 case X86Subtarget::isWindows:
86 return new TargetLoweringObjectFileCOFF();
87 }
Chris Lattnerf0144122009-07-28 03:13:23 +000088}
89
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000090X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000091 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000092 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000093 X86ScalarSSEf64 = Subtarget->hasSSE2();
94 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000095 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000096
Anton Korobeynikov2365f512007-07-14 14:06:15 +000097 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000098 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000099
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000100 // Set up the TargetLowering object.
101
102 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000104 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +0000105 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000106 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000107
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000108 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000109 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000110 setUseUnderscoreSetJmp(false);
111 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000112 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000113 // MS runtime is weird: it exports _setjmp, but longjmp!
114 setUseUnderscoreSetJmp(true);
115 setUseUnderscoreLongJmp(false);
116 } else {
117 setUseUnderscoreSetJmp(true);
118 setUseUnderscoreLongJmp(true);
119 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000120
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000121 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman2f67df72009-09-03 17:18:51 +0000123 if (!Disable16Bit)
124 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000126 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000128
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000130
Scott Michelfdc40a02009-02-17 22:15:04 +0000131 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000133 if (!Disable16Bit)
134 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000136 if (!Disable16Bit)
137 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
139 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000140
141 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
143 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
144 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
145 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
146 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
147 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000148
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000149 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
150 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
152 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
153 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000154
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
157 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000158 } else if (!UseSoftFloat) {
159 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000160 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000162 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000163 // We have an algorithm for SSE2, and we turn this into a 64-bit
164 // FILD for other targets.
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000166 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167
168 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
169 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
171 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000172
Devang Patel6a784892009-06-05 18:48:29 +0000173 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000174 // SSE has no i16 to fp conversion, only i32
175 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000176 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000177 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000179 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
181 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000182 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000183 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
185 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000186 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000187
Dale Johannesen73328d12007-09-19 23:55:34 +0000188 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
189 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
191 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000192
Evan Cheng02568ff2006-01-30 22:13:22 +0000193 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
194 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
196 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000197
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000198 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000200 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000202 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
204 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000205 }
206
207 // Handle FP_TO_UINT by promoting the destination to a larger signed
208 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
210 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
211 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000212
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
215 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000216 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000217 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000218 // Expand FP_TO_UINT into a select.
219 // FIXME: We would like to use a Custom expander here eventually to do
220 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000222 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000223 // With SSE3 we can use fisttpll to convert to a signed i64; without
224 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000227
Chris Lattner399610a2006-12-05 18:22:22 +0000228 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000229 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
231 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000232 }
Chris Lattner21f66852005-12-23 05:15:23 +0000233
Dan Gohmanb00ee212008-02-18 19:34:53 +0000234 // Scalar integer divide and remainder are lowered to use operations that
235 // produce two results, to match the available instructions. This exposes
236 // the two-result form to trivial CSE, which is able to combine x/y and x%y
237 // into a single instruction.
238 //
239 // Scalar integer multiply-high is also lowered to use two-result
240 // operations, to match the available instructions. However, plain multiply
241 // (low) operations are left as Legal, as there are single-result
242 // instructions for this in x86. Using the two-result multiply instructions
243 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
245 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
246 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
247 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
248 setOperationAction(ISD::SREM , MVT::i8 , Expand);
249 setOperationAction(ISD::UREM , MVT::i8 , Expand);
250 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
251 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
252 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
253 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
254 setOperationAction(ISD::SREM , MVT::i16 , Expand);
255 setOperationAction(ISD::UREM , MVT::i16 , Expand);
256 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
257 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
258 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
259 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
260 setOperationAction(ISD::SREM , MVT::i32 , Expand);
261 setOperationAction(ISD::UREM , MVT::i32 , Expand);
262 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
263 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
264 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
265 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
266 setOperationAction(ISD::SREM , MVT::i64 , Expand);
267 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000268
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
270 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
271 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
272 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
275 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
276 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
277 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
278 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
279 setOperationAction(ISD::FREM , MVT::f32 , Expand);
280 setOperationAction(ISD::FREM , MVT::f64 , Expand);
281 setOperationAction(ISD::FREM , MVT::f80 , Expand);
282 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000283
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
285 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
286 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
287 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000288 if (Disable16Bit) {
289 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
290 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
291 } else {
292 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
293 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
294 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
296 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
297 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000298 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
300 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
301 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000302 }
303
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
305 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000306
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000307 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000308 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000309 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000310 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000311 if (Disable16Bit)
312 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
313 else
314 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
316 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
317 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
318 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
319 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000320 if (Disable16Bit)
321 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
322 else
323 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
325 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
326 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
327 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
330 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000331 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000333
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000334 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
336 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
337 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
338 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000339 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
341 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000342 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000343 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
345 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
346 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
347 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000348 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000349 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000350 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
352 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
353 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000354 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
356 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
357 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000358 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000359
Evan Chengd2cde682008-03-10 19:38:10 +0000360 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000362
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000363 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000365
Mon P Wang63307c32008-05-05 19:05:59 +0000366 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
368 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
369 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
370 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000371
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
374 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000376
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000377 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
379 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
380 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
381 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
382 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
383 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
384 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000385 }
386
Evan Cheng3c992d22006-03-07 02:02:57 +0000387 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000388 if (!Subtarget->isTargetDarwin() &&
389 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000390 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000392 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000393
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
395 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
396 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
397 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000398 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000399 setExceptionPointerRegister(X86::RAX);
400 setExceptionSelectorRegister(X86::RDX);
401 } else {
402 setExceptionPointerRegister(X86::EAX);
403 setExceptionSelectorRegister(X86::EDX);
404 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
406 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000407
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000409
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000411
Nate Begemanacc398c2006-01-25 18:21:52 +0000412 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::VASTART , MVT::Other, Custom);
414 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000415 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::VAARG , MVT::Other, Custom);
417 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000418 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::VAARG , MVT::Other, Expand);
420 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000421 }
Evan Chengae642192007-03-02 23:16:35 +0000422
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
424 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000425 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000427 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000429 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000431
Evan Chengc7ce29b2009-02-13 22:36:38 +0000432 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000433 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000434 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
436 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437
Evan Cheng223547a2006-01-31 22:28:30 +0000438 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FABS , MVT::f64, Custom);
440 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000441
442 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FNEG , MVT::f64, Custom);
444 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000445
Evan Cheng68c47cb2007-01-05 07:55:56 +0000446 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
448 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000449
Evan Chengd25e9e82006-02-02 00:28:23 +0000450 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::FSIN , MVT::f64, Expand);
452 setOperationAction(ISD::FCOS , MVT::f64, Expand);
453 setOperationAction(ISD::FSIN , MVT::f32, Expand);
454 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000455
Chris Lattnera54aa942006-01-29 06:26:08 +0000456 // Expand FP immediates into loads from the stack, except for the special
457 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000458 addLegalFPImmediate(APFloat(+0.0)); // xorpd
459 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000460 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461 // Use SSE for f32, x87 for f64.
462 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
464 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000465
466 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000468
469 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000473
474 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
476 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477
478 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::FSIN , MVT::f32, Expand);
480 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000481
Nate Begemane1795842008-02-14 08:57:00 +0000482 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000483 addLegalFPImmediate(APFloat(+0.0f)); // xorps
484 addLegalFPImmediate(APFloat(+0.0)); // FLD0
485 addLegalFPImmediate(APFloat(+1.0)); // FLD1
486 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
487 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
488
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000489 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
491 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000492 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000493 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000494 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000495 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
497 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000498
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
500 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
501 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
502 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000503
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000504 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
506 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000507 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000508 addLegalFPImmediate(APFloat(+0.0)); // FLD0
509 addLegalFPImmediate(APFloat(+1.0)); // FLD1
510 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
511 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000512 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
513 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
514 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
515 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000516 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000517
Dale Johannesen59a58732007-08-05 18:49:15 +0000518 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000519 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
521 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
522 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000523 {
524 bool ignored;
525 APFloat TmpFlt(+0.0);
526 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
527 &ignored);
528 addLegalFPImmediate(TmpFlt); // FLD0
529 TmpFlt.changeSign();
530 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
531 APFloat TmpFlt2(+1.0);
532 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
533 &ignored);
534 addLegalFPImmediate(TmpFlt2); // FLD1
535 TmpFlt2.changeSign();
536 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
537 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000538
Evan Chengc7ce29b2009-02-13 22:36:38 +0000539 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
541 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000542 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000543 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000544
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000545 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
547 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
548 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000549
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::FLOG, MVT::f80, Expand);
551 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
552 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
553 setOperationAction(ISD::FEXP, MVT::f80, Expand);
554 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000555
Mon P Wangf007a8b2008-11-06 05:31:54 +0000556 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000557 // (for widening) or expand (for scalarization). Then we will selectively
558 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
560 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
561 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
576 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
577 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
605 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
606 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000609 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000610 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
611 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
612 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
613 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
614 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
615 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
616 setTruncStoreAction((MVT::SimpleValueType)VT,
617 (MVT::SimpleValueType)InnerVT, Expand);
618 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
619 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
620 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000621 }
622
Evan Chengc7ce29b2009-02-13 22:36:38 +0000623 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
624 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000625 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
627 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
628 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
629 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
630 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000631
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
633 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
634 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
635 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000636
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
638 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
639 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
640 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000641
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
643 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000644
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 setOperationAction(ISD::AND, MVT::v8i8, Promote);
646 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
647 setOperationAction(ISD::AND, MVT::v4i16, Promote);
648 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
649 setOperationAction(ISD::AND, MVT::v2i32, Promote);
650 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
651 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000652
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::OR, MVT::v8i8, Promote);
654 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
655 setOperationAction(ISD::OR, MVT::v4i16, Promote);
656 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
657 setOperationAction(ISD::OR, MVT::v2i32, Promote);
658 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
659 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
662 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
663 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
664 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
665 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
666 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
667 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000668
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
670 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
671 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
672 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
673 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
674 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
675 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
676 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
677 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000678
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
680 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
681 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
682 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
683 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000684
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
686 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
687 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
688 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000689
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
691 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
692 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
693 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000694
Owen Anderson825b72b2009-08-11 20:47:22 +0000695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000696
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
698 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
699 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
700 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
701 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
702 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
703 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000704 }
705
Evan Cheng92722532009-03-26 23:06:32 +0000706 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000708
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
710 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
711 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
712 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
713 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
714 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
715 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
716 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
717 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
718 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
719 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
720 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000721 }
722
Evan Cheng92722532009-03-26 23:06:32 +0000723 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000725
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000726 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
727 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
729 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
730 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
731 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000732
Owen Anderson825b72b2009-08-11 20:47:22 +0000733 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
734 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
735 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
736 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
737 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
738 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
739 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
740 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
741 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
742 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
743 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
744 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
745 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
746 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
747 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
748 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000749
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
751 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
752 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
753 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000754
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
756 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
757 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
758 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
759 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000760
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000761 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
762 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
763 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
764 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
765 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
766
Evan Cheng2c3ae372006-04-12 21:21:57 +0000767 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
769 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000770 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000771 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000772 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000773 // Do not attempt to custom lower non-128-bit vectors
774 if (!VT.is128BitVector())
775 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 setOperationAction(ISD::BUILD_VECTOR,
777 VT.getSimpleVT().SimpleTy, Custom);
778 setOperationAction(ISD::VECTOR_SHUFFLE,
779 VT.getSimpleVT().SimpleTy, Custom);
780 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
781 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000782 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000783
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
785 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
786 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
787 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
788 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
789 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000790
Nate Begemancdd1eec2008-02-12 22:51:28 +0000791 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
793 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000794 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000795
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000796 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Eric Christopher4bd24c22010-03-30 01:04:59 +0000797 // FIXME: This produces lots of inefficiencies in isel since
798 // we then need notice that most of our operands have been implicitly
799 // converted to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
801 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000802 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000803
804 // Do not attempt to promote non-128-bit vectors
805 if (!VT.is128BitVector()) {
806 continue;
807 }
Eric Christopher4bd24c22010-03-30 01:04:59 +0000808
Owen Andersond6662ad2009-08-10 20:46:15 +0000809 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000811 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000812 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000813 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000815 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000817 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000819 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000820
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000822
Evan Cheng2c3ae372006-04-12 21:21:57 +0000823 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
825 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
826 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
827 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
830 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000831 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
833 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000834 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000835 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000836
Nate Begeman14d12ca2008-02-11 04:19:36 +0000837 if (Subtarget->hasSSE41()) {
838 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000840
841 // i8 and i16 vectors are custom , because the source register and source
842 // source memory operand types are not the same width. f32 vectors are
843 // custom since the immediate controlling the insert encodes additional
844 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
847 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
848 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000849
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
851 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
852 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
853 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000854
855 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
857 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000858 }
859 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000860
Nate Begeman30a0de92008-07-17 16:51:19 +0000861 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000863 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000864
David Greene9b9838d2009-06-29 16:47:10 +0000865 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
867 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
868 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
869 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
872 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
873 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
874 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
875 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
876 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
877 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
878 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
879 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
880 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
881 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
882 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
883 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
884 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
885 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000886
887 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
889 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
890 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
891 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
892 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
893 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
894 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
895 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
896 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
897 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
898 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
899 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
900 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
901 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000902
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
904 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
905 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
906 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
909 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
910 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
911 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
912 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000913
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
915 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
916 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
917 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
918 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
919 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000920
921#if 0
922 // Not sure we want to do this since there are no 256-bit integer
923 // operations in AVX
924
925 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
926 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
928 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000929
930 // Do not attempt to custom lower non-power-of-2 vectors
931 if (!isPowerOf2_32(VT.getVectorNumElements()))
932 continue;
933
934 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
935 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
937 }
938
939 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000942 }
David Greene9b9838d2009-06-29 16:47:10 +0000943#endif
944
945#if 0
946 // Not sure we want to do this since there are no 256-bit integer
947 // operations in AVX
948
949 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
950 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
952 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000953
954 if (!VT.is256BitVector()) {
955 continue;
956 }
957 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000958 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000959 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000961 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000963 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000965 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000967 }
968
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000970#endif
971 }
972
Evan Cheng6be2c582006-04-05 23:38:46 +0000973 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000975
Bill Wendling74c37652008-12-09 22:08:41 +0000976 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000977 setOperationAction(ISD::SADDO, MVT::i32, Custom);
978 setOperationAction(ISD::SADDO, MVT::i64, Custom);
979 setOperationAction(ISD::UADDO, MVT::i32, Custom);
980 setOperationAction(ISD::UADDO, MVT::i64, Custom);
981 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
982 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
983 setOperationAction(ISD::USUBO, MVT::i32, Custom);
984 setOperationAction(ISD::USUBO, MVT::i64, Custom);
985 setOperationAction(ISD::SMULO, MVT::i32, Custom);
986 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000987
Evan Chengd54f2d52009-03-31 19:38:51 +0000988 if (!Subtarget->is64Bit()) {
989 // These libcalls are not available in 32-bit.
990 setLibcallName(RTLIB::SHL_I128, 0);
991 setLibcallName(RTLIB::SRL_I128, 0);
992 setLibcallName(RTLIB::SRA_I128, 0);
993 }
994
Evan Cheng206ee9d2006-07-07 08:33:52 +0000995 // We have target-specific dag combine patterns for the following nodes:
996 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000997 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000998 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000999 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001000 setTargetDAGCombine(ISD::SHL);
1001 setTargetDAGCombine(ISD::SRA);
1002 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001003 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +00001004 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +00001005 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +00001006 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001007 if (Subtarget->is64Bit())
1008 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001009
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001010 computeRegisterProperties();
1011
Evan Cheng87ed7162006-02-14 08:25:08 +00001012 // FIXME: These should be based on subtarget info. Plus, the values should
1013 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001014 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1015 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
1016 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001017 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001018 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001019}
1020
Scott Michel5b8f82e2008-03-10 15:42:14 +00001021
Owen Anderson825b72b2009-08-11 20:47:22 +00001022MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1023 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001024}
1025
1026
Evan Cheng29286502008-01-23 23:17:41 +00001027/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1028/// the desired ByVal argument alignment.
1029static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1030 if (MaxAlign == 16)
1031 return;
1032 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1033 if (VTy->getBitWidth() == 128)
1034 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001035 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1036 unsigned EltAlign = 0;
1037 getMaxByValAlign(ATy->getElementType(), EltAlign);
1038 if (EltAlign > MaxAlign)
1039 MaxAlign = EltAlign;
1040 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1041 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1042 unsigned EltAlign = 0;
1043 getMaxByValAlign(STy->getElementType(i), EltAlign);
1044 if (EltAlign > MaxAlign)
1045 MaxAlign = EltAlign;
1046 if (MaxAlign == 16)
1047 break;
1048 }
1049 }
1050 return;
1051}
1052
1053/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1054/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001055/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1056/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001057unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001058 if (Subtarget->is64Bit()) {
1059 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001060 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001061 if (TyAlign > 8)
1062 return TyAlign;
1063 return 8;
1064 }
1065
Evan Cheng29286502008-01-23 23:17:41 +00001066 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001067 if (Subtarget->hasSSE1())
1068 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001069 return Align;
1070}
Chris Lattner2b02a442007-02-25 08:29:00 +00001071
Evan Chengf0df0312008-05-15 08:39:06 +00001072/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001073/// and store operations as a result of memset, memcpy, and memmove
Owen Anderson825b72b2009-08-11 20:47:22 +00001074/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001075/// determining it.
Owen Andersone50ed302009-08-10 22:56:29 +00001076EVT
Evan Chengf0df0312008-05-15 08:39:06 +00001077X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001078 bool isSrcConst, bool isSrcStr,
1079 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001080 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1081 // linux. This is because the stack realignment code can't handle certain
1082 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001083 const Function *F = DAG.getMachineFunction().getFunction();
1084 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1085 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001086 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001087 return MVT::v4i32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001088 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001089 return MVT::v4f32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001090 }
Evan Chengf0df0312008-05-15 08:39:06 +00001091 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001092 return MVT::i64;
1093 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001094}
1095
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001096/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1097/// current function. The returned value is a member of the
1098/// MachineJumpTableInfo::JTEntryKind enum.
1099unsigned X86TargetLowering::getJumpTableEncoding() const {
1100 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1101 // symbol.
1102 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1103 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001104 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001105
1106 // Otherwise, use the normal jump table encoding heuristics.
1107 return TargetLowering::getJumpTableEncoding();
1108}
1109
Chris Lattner589c6f62010-01-26 06:28:43 +00001110/// getPICBaseSymbol - Return the X86-32 PIC base.
1111MCSymbol *
1112X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1113 MCContext &Ctx) const {
1114 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001115 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1116 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001117}
1118
1119
Chris Lattnerc64daab2010-01-26 05:02:42 +00001120const MCExpr *
1121X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1122 const MachineBasicBlock *MBB,
1123 unsigned uid,MCContext &Ctx) const{
1124 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1125 Subtarget->isPICStyleGOT());
1126 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1127 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001128 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1129 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001130}
1131
Evan Chengcc415862007-11-09 01:32:10 +00001132/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1133/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001134SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001135 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001136 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001137 // This doesn't have DebugLoc associated with it, but is not really the
1138 // same as a Register.
1139 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1140 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001141 return Table;
1142}
1143
Chris Lattner589c6f62010-01-26 06:28:43 +00001144/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1145/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1146/// MCExpr.
1147const MCExpr *X86TargetLowering::
1148getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1149 MCContext &Ctx) const {
1150 // X86-64 uses RIP relative addressing based on the jump table label.
1151 if (Subtarget->isPICStyleRIPRel())
1152 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1153
1154 // Otherwise, the reference is relative to the PIC base.
1155 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1156}
1157
Bill Wendlingb4202b82009-07-01 18:50:55 +00001158/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001159unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001160 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001161}
1162
Chris Lattner2b02a442007-02-25 08:29:00 +00001163//===----------------------------------------------------------------------===//
1164// Return Value Calling Convention Implementation
1165//===----------------------------------------------------------------------===//
1166
Chris Lattner59ed56b2007-02-28 04:55:35 +00001167#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001168
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001169bool
1170X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1171 const SmallVectorImpl<EVT> &OutTys,
1172 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1173 SelectionDAG &DAG) {
1174 SmallVector<CCValAssign, 16> RVLocs;
1175 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1176 RVLocs, *DAG.getContext());
1177 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1178}
1179
Dan Gohman98ca4f22009-08-05 01:29:28 +00001180SDValue
1181X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001182 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001183 const SmallVectorImpl<ISD::OutputArg> &Outs,
1184 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001185
Chris Lattner9774c912007-02-27 05:28:59 +00001186 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001187 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1188 RVLocs, *DAG.getContext());
1189 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001190
Evan Chengdcea1632010-02-04 02:40:39 +00001191 // Add the regs to the liveout set for the function.
1192 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1193 for (unsigned i = 0; i != RVLocs.size(); ++i)
1194 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1195 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001196
Dan Gohman475871a2008-07-27 21:46:04 +00001197 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001198
Dan Gohman475871a2008-07-27 21:46:04 +00001199 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001200 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1201 // Operand #1 = Bytes To Pop
Dan Gohman2f67df72009-09-03 17:18:51 +00001202 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001203
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001204 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001205 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1206 CCValAssign &VA = RVLocs[i];
1207 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001209
Chris Lattner447ff682008-03-11 03:23:40 +00001210 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1211 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001212 if (VA.getLocReg() == X86::ST0 ||
1213 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001214 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1215 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001216 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001218 RetOps.push_back(ValToCopy);
1219 // Don't emit a copytoreg.
1220 continue;
1221 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001222
Evan Cheng242b38b2009-02-23 09:03:22 +00001223 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1224 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001225 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001226 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001227 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001228 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001229 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001231 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001232 }
1233
Dale Johannesendd64c412009-02-04 00:33:20 +00001234 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001235 Flag = Chain.getValue(1);
1236 }
Dan Gohman61a92132008-04-21 23:59:07 +00001237
1238 // The x86-64 ABI for returning structs by value requires that we copy
1239 // the sret argument into %rax for the return. We saved the argument into
1240 // a virtual register in the entry block, so now we copy the value out
1241 // and into %rax.
1242 if (Subtarget->is64Bit() &&
1243 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1244 MachineFunction &MF = DAG.getMachineFunction();
1245 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1246 unsigned Reg = FuncInfo->getSRetReturnReg();
1247 if (!Reg) {
Evan Chengdcea1632010-02-04 02:40:39 +00001248 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001249 FuncInfo->setSRetReturnReg(Reg);
1250 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001251 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001252
Dale Johannesendd64c412009-02-04 00:33:20 +00001253 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001254 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001255
1256 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001257 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001258 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001259
Chris Lattner447ff682008-03-11 03:23:40 +00001260 RetOps[0] = Chain; // Update chain.
1261
1262 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001263 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001264 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001265
1266 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001267 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001268}
1269
Dan Gohman98ca4f22009-08-05 01:29:28 +00001270/// LowerCallResult - Lower the result values of a call into the
1271/// appropriate copies out of appropriate physical registers.
1272///
1273SDValue
1274X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001275 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001276 const SmallVectorImpl<ISD::InputArg> &Ins,
1277 DebugLoc dl, SelectionDAG &DAG,
1278 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001279
Chris Lattnere32bbf62007-02-28 07:09:55 +00001280 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001281 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001282 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001283 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001284 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001285 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001286
Chris Lattner3085e152007-02-25 08:59:22 +00001287 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001288 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001289 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001290 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001291
Torok Edwin3f142c32009-02-01 18:15:56 +00001292 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001293 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001294 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001295 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001296 }
1297
Chris Lattner8e6da152008-03-10 21:08:41 +00001298 // If this is a call to a function that returns an fp value on the floating
1299 // point stack, but where we prefer to use the value in xmm registers, copy
1300 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman37eed792009-02-04 17:28:58 +00001301 if ((VA.getLocReg() == X86::ST0 ||
1302 VA.getLocReg() == X86::ST1) &&
1303 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001304 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001305 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001306
Evan Cheng79fb3b42009-02-20 20:43:02 +00001307 SDValue Val;
1308 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001309 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1310 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1311 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001313 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1315 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001316 } else {
1317 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001319 Val = Chain.getValue(0);
1320 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001321 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1322 } else {
1323 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1324 CopyVT, InFlag).getValue(1);
1325 Val = Chain.getValue(0);
1326 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001327 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001328
Dan Gohman37eed792009-02-04 17:28:58 +00001329 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001330 // Round the F80 the right size, which also moves to the appropriate xmm
1331 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001332 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001333 // This truncation won't change the value.
1334 DAG.getIntPtrConstant(1));
1335 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001336
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001338 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001339
Dan Gohman98ca4f22009-08-05 01:29:28 +00001340 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001341}
1342
1343
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001344//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001345// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001346//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001347// StdCall calling convention seems to be standard for many Windows' API
1348// routines and around. It differs from C calling convention just a little:
1349// callee should clean up the stack, not caller. Symbols should be also
1350// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001351// For info on fast calling convention see Fast Calling Convention (tail call)
1352// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001353
Dan Gohman98ca4f22009-08-05 01:29:28 +00001354/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001355/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1357 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001358 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001359
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001361}
1362
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001363/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001364/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001365static bool
1366ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1367 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001368 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001369
Dan Gohman98ca4f22009-08-05 01:29:28 +00001370 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001371}
1372
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001373/// IsCalleePop - Determines whether the callee is required to pop its
1374/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001375bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001376 if (IsVarArg)
1377 return false;
1378
Dan Gohman095cc292008-09-13 01:54:27 +00001379 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001380 default:
1381 return false;
1382 case CallingConv::X86_StdCall:
1383 return !Subtarget->is64Bit();
1384 case CallingConv::X86_FastCall:
1385 return !Subtarget->is64Bit();
1386 case CallingConv::Fast:
Dan Gohman1797ed52010-02-08 20:27:50 +00001387 return GuaranteedTailCallOpt;
Chris Lattner29689432010-03-11 00:22:57 +00001388 case CallingConv::GHC:
1389 return GuaranteedTailCallOpt;
Gordon Henriksen86737662008-01-05 16:56:59 +00001390 }
1391}
1392
Dan Gohman095cc292008-09-13 01:54:27 +00001393/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1394/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001395CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001396 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001397 if (CC == CallingConv::GHC)
1398 return CC_X86_64_GHC;
1399 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001400 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001401 else
1402 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001403 }
1404
Gordon Henriksen86737662008-01-05 16:56:59 +00001405 if (CC == CallingConv::X86_FastCall)
1406 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001407 else if (CC == CallingConv::Fast)
1408 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001409 else if (CC == CallingConv::GHC)
1410 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001411 else
1412 return CC_X86_32_C;
1413}
1414
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001415/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1416/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001417/// the specific parameter attribute. The copy will be passed as a byval
1418/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001419static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001420CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001421 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1422 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001423 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001424 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang808bab02010-03-30 20:55:56 +00001425 /*isVolatile*/false, /*AlwaysInline=*/true,
1426 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001427}
1428
Chris Lattner29689432010-03-11 00:22:57 +00001429/// IsTailCallConvention - Return true if the calling convention is one that
1430/// supports tail call optimization.
1431static bool IsTailCallConvention(CallingConv::ID CC) {
1432 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1433}
1434
Evan Cheng0c439eb2010-01-27 00:07:07 +00001435/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1436/// a tailcall target by changing its ABI.
1437static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001438 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001439}
1440
Dan Gohman98ca4f22009-08-05 01:29:28 +00001441SDValue
1442X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001443 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444 const SmallVectorImpl<ISD::InputArg> &Ins,
1445 DebugLoc dl, SelectionDAG &DAG,
1446 const CCValAssign &VA,
1447 MachineFrameInfo *MFI,
1448 unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001449 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001450 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001451 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001452 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001453 EVT ValVT;
1454
1455 // If value is passed by pointer we have address passed instead of the value
1456 // itself.
1457 if (VA.getLocInfo() == CCValAssign::Indirect)
1458 ValVT = VA.getLocVT();
1459 else
1460 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001461
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001462 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001463 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001464 // In case of tail call optimization mark all arguments mutable. Since they
1465 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001466 if (Flags.isByVal()) {
1467 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1468 VA.getLocMemOffset(), isImmutable, false);
1469 return DAG.getFrameIndex(FI, getPointerTy());
1470 } else {
1471 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1472 VA.getLocMemOffset(), isImmutable, false);
1473 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1474 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001475 PseudoSourceValue::getFixedStack(FI), 0,
1476 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001477 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001478}
1479
Dan Gohman475871a2008-07-27 21:46:04 +00001480SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001481X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001482 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001483 bool isVarArg,
1484 const SmallVectorImpl<ISD::InputArg> &Ins,
1485 DebugLoc dl,
1486 SelectionDAG &DAG,
1487 SmallVectorImpl<SDValue> &InVals) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001488 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001489 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
Gordon Henriksen86737662008-01-05 16:56:59 +00001491 const Function* Fn = MF.getFunction();
1492 if (Fn->hasExternalLinkage() &&
1493 Subtarget->isTargetCygMing() &&
1494 Fn->getName() == "main")
1495 FuncInfo->setForceFramePointer(true);
1496
Evan Cheng1bc78042006-04-26 01:20:17 +00001497 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001498 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001499 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001500
Chris Lattner29689432010-03-11 00:22:57 +00001501 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1502 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001503
Chris Lattner638402b2007-02-28 07:00:42 +00001504 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001505 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001506 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1507 ArgLocs, *DAG.getContext());
1508 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001509
Chris Lattnerf39f7712007-02-28 05:46:49 +00001510 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001511 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001512 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1513 CCValAssign &VA = ArgLocs[i];
1514 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1515 // places.
1516 assert(VA.getValNo() != LastVal &&
1517 "Don't support value assigned to multiple locs yet");
1518 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001519
Chris Lattnerf39f7712007-02-28 05:46:49 +00001520 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001521 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001522 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001524 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001525 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001526 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001527 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001529 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001530 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001531 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001532 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001533 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1534 RC = X86::VR64RegisterClass;
1535 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001536 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001537
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001538 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001539 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001540
Chris Lattnerf39f7712007-02-28 05:46:49 +00001541 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1542 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1543 // right size.
1544 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001545 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001546 DAG.getValueType(VA.getValVT()));
1547 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001548 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001549 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001550 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001551 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001552
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001553 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001554 // Handle MMX values passed in XMM regs.
1555 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001556 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1557 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001558 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1559 } else
1560 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001561 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001562 } else {
1563 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001565 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001566
1567 // If value is passed via pointer - do a load.
1568 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001569 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1570 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001571
Dan Gohman98ca4f22009-08-05 01:29:28 +00001572 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001573 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001574
Dan Gohman61a92132008-04-21 23:59:07 +00001575 // The x86-64 ABI for returning structs by value requires that we copy
1576 // the sret argument into %rax for the return. Save the argument into
1577 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001578 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001579 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1580 unsigned Reg = FuncInfo->getSRetReturnReg();
1581 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001582 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001583 FuncInfo->setSRetReturnReg(Reg);
1584 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001585 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001586 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001587 }
1588
Chris Lattnerf39f7712007-02-28 05:46:49 +00001589 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001590 // Align stack specially for tail calls.
1591 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001592 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001593
Evan Cheng1bc78042006-04-26 01:20:17 +00001594 // If the function takes variable number of arguments, make a frame index for
1595 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001596 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001598 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001599 }
1600 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001601 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1602
1603 // FIXME: We should really autogenerate these arrays
1604 static const unsigned GPR64ArgRegsWin64[] = {
1605 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001606 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001607 static const unsigned XMMArgRegsWin64[] = {
1608 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1609 };
1610 static const unsigned GPR64ArgRegs64Bit[] = {
1611 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1612 };
1613 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001614 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1615 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1616 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001617 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1618
1619 if (IsWin64) {
1620 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1621 GPR64ArgRegs = GPR64ArgRegsWin64;
1622 XMMArgRegs = XMMArgRegsWin64;
1623 } else {
1624 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1625 GPR64ArgRegs = GPR64ArgRegs64Bit;
1626 XMMArgRegs = XMMArgRegs64Bit;
1627 }
1628 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1629 TotalNumIntRegs);
1630 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1631 TotalNumXMMRegs);
1632
Devang Patel578efa92009-06-05 21:57:13 +00001633 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001634 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001635 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001636 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001637 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001638 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001639 // Kernel mode asks for SSE to be disabled, so don't push them
1640 // on the stack.
1641 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001642
Gordon Henriksen86737662008-01-05 16:56:59 +00001643 // For X86-64, if there are vararg parameters that are passed via
1644 // registers, then we must store them to their spots on the stack so they
1645 // may be loaded by deferencing the result of va_next.
1646 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001647 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1648 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001649 TotalNumXMMRegs * 16, 16,
1650 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001651
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001653 SmallVector<SDValue, 8> MemOps;
1654 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001655 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001656 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001657 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1658 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001659 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1660 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001661 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001662 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001663 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001664 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
David Greene67c9d422010-02-15 16:53:33 +00001665 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001666 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001667 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001668 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001669
Dan Gohmanface41a2009-08-16 21:24:25 +00001670 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1671 // Now store the XMM (fp + vector) parameter registers.
1672 SmallVector<SDValue, 11> SaveXMMOps;
1673 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001674
Dan Gohmanface41a2009-08-16 21:24:25 +00001675 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1676 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1677 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001678
Dan Gohmanface41a2009-08-16 21:24:25 +00001679 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1680 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001681
Dan Gohmanface41a2009-08-16 21:24:25 +00001682 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1683 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1684 X86::VR128RegisterClass);
1685 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1686 SaveXMMOps.push_back(Val);
1687 }
1688 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1689 MVT::Other,
1690 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001691 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001692
1693 if (!MemOps.empty())
1694 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1695 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001696 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001697 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001698
Gordon Henriksen86737662008-01-05 16:56:59 +00001699 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001700 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001701 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001702 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001703 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001704 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001705 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001706 BytesToPopOnReturn = 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001707 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001708
Gordon Henriksen86737662008-01-05 16:56:59 +00001709 if (!Is64Bit) {
1710 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1713 }
Evan Cheng25caf632006-05-23 21:06:34 +00001714
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001715 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001716
Dan Gohman98ca4f22009-08-05 01:29:28 +00001717 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001718}
1719
Dan Gohman475871a2008-07-27 21:46:04 +00001720SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001721X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1722 SDValue StackPtr, SDValue Arg,
1723 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001724 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001725 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001726 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001727 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001728 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001729 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001730 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001731 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001732 }
Dale Johannesenace16102009-02-03 19:33:06 +00001733 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001734 PseudoSourceValue::getStack(), LocMemOffset,
1735 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001736}
1737
Bill Wendling64e87322009-01-16 19:25:27 +00001738/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001739/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001740SDValue
1741X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001742 SDValue &OutRetAddr, SDValue Chain,
1743 bool IsTailCall, bool Is64Bit,
1744 int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001745 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001746 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001747 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001748
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001749 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001750 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001751 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001752}
1753
1754/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1755/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001756static SDValue
1757EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001759 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001760 // Store the return address to the appropriate stack slot.
1761 if (!FPDiff) return Chain;
1762 // Calculate the new stack slot for the return address.
1763 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001764 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001765 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001767 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001768 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001769 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1770 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001771 return Chain;
1772}
1773
Dan Gohman98ca4f22009-08-05 01:29:28 +00001774SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001775X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001776 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001777 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001778 const SmallVectorImpl<ISD::OutputArg> &Outs,
1779 const SmallVectorImpl<ISD::InputArg> &Ins,
1780 DebugLoc dl, SelectionDAG &DAG,
1781 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001782 MachineFunction &MF = DAG.getMachineFunction();
1783 bool Is64Bit = Subtarget->is64Bit();
1784 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001785 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001786
Evan Cheng5f941932010-02-05 02:21:12 +00001787 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001788 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001789 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1790 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001791 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001792
1793 // Sibcalls are automatically detected tailcalls which do not require
1794 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001795 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001796 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001797
1798 if (isTailCall)
1799 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001800 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001801
Chris Lattner29689432010-03-11 00:22:57 +00001802 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1803 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001804
Chris Lattner638402b2007-02-28 07:00:42 +00001805 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001806 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1808 ArgLocs, *DAG.getContext());
1809 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001810
Chris Lattner423c5f42007-02-28 05:31:48 +00001811 // Get a count of how many bytes are to be pushed on the stack.
1812 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001813 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001814 // This is a sibcall. The memory operands are available in caller's
1815 // own caller's stack.
1816 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001817 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001818 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001819
Gordon Henriksen86737662008-01-05 16:56:59 +00001820 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001821 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001822 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001823 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001824 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1825 FPDiff = NumBytesCallerPushed - NumBytes;
1826
1827 // Set the delta of movement of the returnaddr stackslot.
1828 // But only set if delta is greater than previous delta.
1829 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1830 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1831 }
1832
Evan Chengf22f9b32010-02-06 03:28:46 +00001833 if (!IsSibcall)
1834 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001835
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001837 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001838 if (isTailCall && FPDiff)
1839 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1840 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001841
Dan Gohman475871a2008-07-27 21:46:04 +00001842 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1843 SmallVector<SDValue, 8> MemOpChains;
1844 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001845
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001846 // Walk the register/memloc assignments, inserting copies/loads. In the case
1847 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001848 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1849 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001850 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001851 SDValue Arg = Outs[i].Val;
1852 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001853 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001854
Chris Lattner423c5f42007-02-28 05:31:48 +00001855 // Promote the value if needed.
1856 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001857 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001858 case CCValAssign::Full: break;
1859 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001860 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001861 break;
1862 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001863 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001864 break;
1865 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001866 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1867 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1869 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1870 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001871 } else
1872 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1873 break;
1874 case CCValAssign::BCvt:
1875 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001876 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001877 case CCValAssign::Indirect: {
1878 // Store the argument.
1879 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001880 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001881 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001882 PseudoSourceValue::getFixedStack(FI), 0,
1883 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001884 Arg = SpillSlot;
1885 break;
1886 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001887 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001888
Chris Lattner423c5f42007-02-28 05:31:48 +00001889 if (VA.isRegLoc()) {
1890 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001891 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001892 assert(VA.isMemLoc());
1893 if (StackPtr.getNode() == 0)
1894 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1895 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1896 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001897 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001898 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001899
Evan Cheng32fe1032006-05-25 00:59:30 +00001900 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001901 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001902 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001903
Evan Cheng347d5f72006-04-28 21:29:37 +00001904 // Build a sequence of copy-to-reg nodes chained together with token chain
1905 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001906 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001907 // Tail call byval lowering might overwrite argument registers so in case of
1908 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001910 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001911 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001912 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001913 InFlag = Chain.getValue(1);
1914 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001915
Chris Lattner88e1fd52009-07-09 04:24:46 +00001916 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001917 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1918 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001919 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001920 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1921 DAG.getNode(X86ISD::GlobalBaseReg,
1922 DebugLoc::getUnknownLoc(),
1923 getPointerTy()),
1924 InFlag);
1925 InFlag = Chain.getValue(1);
1926 } else {
1927 // If we are tail calling and generating PIC/GOT style code load the
1928 // address of the callee into ECX. The value in ecx is used as target of
1929 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1930 // for tail calls on PIC/GOT architectures. Normally we would just put the
1931 // address of GOT into ebx and then call target@PLT. But for tail calls
1932 // ebx would be restored (since ebx is callee saved) before jumping to the
1933 // target@PLT.
1934
1935 // Note: The actual moving to ECX is done further down.
1936 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1937 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1938 !G->getGlobal()->hasProtectedVisibility())
1939 Callee = LowerGlobalAddress(Callee, DAG);
1940 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001941 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001942 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001943 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001944
Gordon Henriksen86737662008-01-05 16:56:59 +00001945 if (Is64Bit && isVarArg) {
1946 // From AMD64 ABI document:
1947 // For calls that may call functions that use varargs or stdargs
1948 // (prototype-less calls or calls to functions containing ellipsis (...) in
1949 // the declaration) %al is used as hidden argument to specify the number
1950 // of SSE registers used. The contents of %al do not need to match exactly
1951 // the number of registers, but must be an ubound on the number of SSE
1952 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001953
1954 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 // Count the number of XMM registers allocated.
1956 static const unsigned XMMArgRegs[] = {
1957 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1958 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1959 };
1960 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001961 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001962 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001963
Dale Johannesendd64c412009-02-04 00:33:20 +00001964 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001965 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001966 InFlag = Chain.getValue(1);
1967 }
1968
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001969
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001970 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001971 if (isTailCall) {
1972 // Force all the incoming stack arguments to be loaded from the stack
1973 // before any new outgoing arguments are stored to the stack, because the
1974 // outgoing stack slots may alias the incoming argument stack slots, and
1975 // the alias isn't otherwise explicit. This is slightly more conservative
1976 // than necessary, because it means that each store effectively depends
1977 // on every argument instead of just those arguments it would clobber.
1978 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1979
Dan Gohman475871a2008-07-27 21:46:04 +00001980 SmallVector<SDValue, 8> MemOpChains2;
1981 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001982 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001983 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001984 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00001985 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00001986 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1987 CCValAssign &VA = ArgLocs[i];
1988 if (VA.isRegLoc())
1989 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001990 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001991 SDValue Arg = Outs[i].Val;
1992 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001993 // Create frame index.
1994 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001995 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001996 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001997 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001998
Duncan Sands276dcbd2008-03-21 09:14:45 +00001999 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002000 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002001 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002002 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002003 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002004 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002005 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002006
Dan Gohman98ca4f22009-08-05 01:29:28 +00002007 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2008 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002009 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002010 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002011 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002012 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002013 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002014 PseudoSourceValue::getFixedStack(FI), 0,
2015 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002016 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002017 }
2018 }
2019
2020 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002021 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002022 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002023
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002024 // Copy arguments to their registers.
2025 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002026 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002027 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002028 InFlag = Chain.getValue(1);
2029 }
Dan Gohman475871a2008-07-27 21:46:04 +00002030 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002031
Gordon Henriksen86737662008-01-05 16:56:59 +00002032 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002033 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002034 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002035 }
2036
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002037 bool WasGlobalOrExternal = false;
2038 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2039 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2040 // In the 64-bit large code model, we have to make all calls
2041 // through a register, since the call instruction's 32-bit
2042 // pc-relative offset may not be large enough to hold the whole
2043 // address.
2044 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2045 WasGlobalOrExternal = true;
2046 // If the callee is a GlobalAddress node (quite common, every direct call
2047 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2048 // it.
2049
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002050 // We should use extra load for direct calls to dllimported functions in
2051 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00002052 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002053 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002054 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002055
Chris Lattner48a7d022009-07-09 05:02:21 +00002056 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2057 // external symbols most go through the PLT in PIC mode. If the symbol
2058 // has hidden or protected visibility, or if it is static or local, then
2059 // we don't need to use the PLT - we can directly call it.
2060 if (Subtarget->isTargetELF() &&
2061 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002062 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002063 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002064 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002065 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2066 Subtarget->getDarwinVers() < 9) {
2067 // PC-relative references to external symbols should go through $stub,
2068 // unless we're building with the leopard linker or later, which
2069 // automatically synthesizes these stubs.
2070 OpFlags = X86II::MO_DARWIN_STUB;
2071 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002072
Chris Lattner74e726e2009-07-09 05:27:35 +00002073 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002074 G->getOffset(), OpFlags);
2075 }
Bill Wendling056292f2008-09-16 21:48:12 +00002076 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002077 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002078 unsigned char OpFlags = 0;
2079
2080 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2081 // symbols should go through the PLT.
2082 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002083 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002084 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002085 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002086 Subtarget->getDarwinVers() < 9) {
2087 // PC-relative references to external symbols should go through $stub,
2088 // unless we're building with the leopard linker or later, which
2089 // automatically synthesizes these stubs.
2090 OpFlags = X86II::MO_DARWIN_STUB;
2091 }
Eric Christopherfd179292009-08-27 18:07:15 +00002092
Chris Lattner48a7d022009-07-09 05:02:21 +00002093 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2094 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002095 }
2096
Chris Lattnerd96d0722007-02-25 06:40:16 +00002097 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002098 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002099 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002100
Evan Chengf22f9b32010-02-06 03:28:46 +00002101 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002102 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2103 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002104 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002105 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002106
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002107 Ops.push_back(Chain);
2108 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002109
Dan Gohman98ca4f22009-08-05 01:29:28 +00002110 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002111 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002112
Gordon Henriksen86737662008-01-05 16:56:59 +00002113 // Add argument registers to the end of the list so that they are known live
2114 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002115 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2116 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2117 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002118
Evan Cheng586ccac2008-03-18 23:36:35 +00002119 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002120 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002121 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2122
2123 // Add an implicit use of AL for x86 vararg functions.
2124 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002125 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002126
Gabor Greifba36cb52008-08-28 21:40:38 +00002127 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002128 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002129
Dan Gohman98ca4f22009-08-05 01:29:28 +00002130 if (isTailCall) {
2131 // If this is the first return lowered for this function, add the regs
2132 // to the liveout set for the function.
2133 if (MF.getRegInfo().liveout_empty()) {
2134 SmallVector<CCValAssign, 16> RVLocs;
2135 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2136 *DAG.getContext());
2137 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2138 for (unsigned i = 0; i != RVLocs.size(); ++i)
2139 if (RVLocs[i].isRegLoc())
2140 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2141 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002142 return DAG.getNode(X86ISD::TC_RETURN, dl,
2143 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002144 }
2145
Dale Johannesenace16102009-02-03 19:33:06 +00002146 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002147 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002148
Chris Lattner2d297092006-05-23 18:50:38 +00002149 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002150 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002151 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002152 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002153 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002154 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002155 // pops the hidden struct pointer, so we have to push it back.
2156 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002157 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002158 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002159 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002160
Gordon Henriksenae636f82008-01-03 16:47:34 +00002161 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002162 if (!IsSibcall) {
2163 Chain = DAG.getCALLSEQ_END(Chain,
2164 DAG.getIntPtrConstant(NumBytes, true),
2165 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2166 true),
2167 InFlag);
2168 InFlag = Chain.getValue(1);
2169 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002170
Chris Lattner3085e152007-02-25 08:59:22 +00002171 // Handle result values, copying them out of physregs into vregs that we
2172 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002173 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2174 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002175}
2176
Evan Cheng25ab6902006-09-08 06:48:29 +00002177
2178//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002179// Fast Calling Convention (tail call) implementation
2180//===----------------------------------------------------------------------===//
2181
2182// Like std call, callee cleans arguments, convention except that ECX is
2183// reserved for storing the tail called function address. Only 2 registers are
2184// free for argument passing (inreg). Tail call optimization is performed
2185// provided:
2186// * tailcallopt is enabled
2187// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002188// On X86_64 architecture with GOT-style position independent code only local
2189// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002190// To keep the stack aligned according to platform abi the function
2191// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2192// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002193// If a tail called function callee has more arguments than the caller the
2194// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002195// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002196// original REtADDR, but before the saved framepointer or the spilled registers
2197// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2198// stack layout:
2199// arg1
2200// arg2
2201// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002202// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002203// move area ]
2204// (possible EBP)
2205// ESI
2206// EDI
2207// local1 ..
2208
2209/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2210/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002211unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002212 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002213 MachineFunction &MF = DAG.getMachineFunction();
2214 const TargetMachine &TM = MF.getTarget();
2215 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2216 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002217 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002218 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002219 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002220 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2221 // Number smaller than 12 so just add the difference.
2222 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2223 } else {
2224 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002225 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002226 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002227 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002228 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002229}
2230
Evan Cheng5f941932010-02-05 02:21:12 +00002231/// MatchingStackOffset - Return true if the given stack call argument is
2232/// already available in the same position (relatively) of the caller's
2233/// incoming argument stack.
2234static
2235bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2236 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2237 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002238 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2239 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002240 if (Arg.getOpcode() == ISD::CopyFromReg) {
2241 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2242 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2243 return false;
2244 MachineInstr *Def = MRI->getVRegDef(VR);
2245 if (!Def)
2246 return false;
2247 if (!Flags.isByVal()) {
2248 if (!TII->isLoadFromStackSlot(Def, FI))
2249 return false;
2250 } else {
2251 unsigned Opcode = Def->getOpcode();
2252 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2253 Def->getOperand(1).isFI()) {
2254 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002255 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002256 } else
2257 return false;
2258 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002259 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2260 if (Flags.isByVal())
2261 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002262 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002263 // define @foo(%struct.X* %A) {
2264 // tail call @bar(%struct.X* byval %A)
2265 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002266 return false;
2267 SDValue Ptr = Ld->getBasePtr();
2268 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2269 if (!FINode)
2270 return false;
2271 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002272 } else
2273 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002274
Evan Cheng4cae1332010-03-05 08:38:04 +00002275 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002276 if (!MFI->isFixedObjectIndex(FI))
2277 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002278 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002279}
2280
Dan Gohman98ca4f22009-08-05 01:29:28 +00002281/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2282/// for tail call optimization. Targets which want to do tail call
2283/// optimization should implement this function.
2284bool
2285X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002286 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002287 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002288 bool isCalleeStructRet,
2289 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002290 const SmallVectorImpl<ISD::OutputArg> &Outs,
2291 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002292 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002293 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002294 CalleeCC != CallingConv::C)
2295 return false;
2296
Evan Cheng7096ae42010-01-29 06:45:59 +00002297 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002298 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002299 const Function *CallerF = DAG.getMachineFunction().getFunction();
Dan Gohman1797ed52010-02-08 20:27:50 +00002300 if (GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00002301 if (IsTailCallConvention(CalleeCC) &&
Evan Cheng843bd692010-01-31 06:44:49 +00002302 CallerF->getCallingConv() == CalleeCC)
2303 return true;
2304 return false;
2305 }
2306
Evan Chengb2c92902010-02-02 02:22:50 +00002307 // Look for obvious safe cases to perform tail call optimization that does not
2308 // requite ABI changes. This is what gcc calls sibcall.
2309
Evan Cheng2c12cb42010-03-26 16:26:03 +00002310 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2311 // emit a special epilogue.
2312 if (RegInfo->needsStackRealignment(MF))
2313 return false;
2314
Evan Cheng3c262ee2010-03-26 02:13:13 +00002315 // Do not sibcall optimize vararg calls unless the call site is not passing any
2316 // arguments.
2317 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002318 return false;
2319
Evan Chenga375d472010-03-15 18:54:48 +00002320 // Also avoid sibcall optimization if either caller or callee uses struct
2321 // return semantics.
2322 if (isCalleeStructRet || isCallerStructRet)
2323 return false;
2324
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002325 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2326 // Therefore if it's not used by the call it is not safe to optimize this into
2327 // a sibcall.
2328 bool Unused = false;
2329 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2330 if (!Ins[i].Used) {
2331 Unused = true;
2332 break;
2333 }
2334 }
2335 if (Unused) {
2336 SmallVector<CCValAssign, 16> RVLocs;
2337 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2338 RVLocs, *DAG.getContext());
2339 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2340 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2341 CCValAssign &VA = RVLocs[i];
2342 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2343 return false;
2344 }
2345 }
2346
Evan Chenga6bff982010-01-30 01:22:00 +00002347 // If the callee takes no arguments then go on to check the results of the
2348 // call.
2349 if (!Outs.empty()) {
2350 // Check if stack adjustment is needed. For now, do not do this if any
2351 // argument is passed on the stack.
2352 SmallVector<CCValAssign, 16> ArgLocs;
2353 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2354 ArgLocs, *DAG.getContext());
2355 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002356 if (CCInfo.getNextStackOffset()) {
2357 MachineFunction &MF = DAG.getMachineFunction();
2358 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2359 return false;
2360 if (Subtarget->isTargetWin64())
2361 // Win64 ABI has additional complications.
2362 return false;
2363
2364 // Check if the arguments are already laid out in the right way as
2365 // the caller's fixed stack objects.
2366 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002367 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2368 const X86InstrInfo *TII =
2369 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002370 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2371 CCValAssign &VA = ArgLocs[i];
2372 EVT RegVT = VA.getLocVT();
2373 SDValue Arg = Outs[i].Val;
2374 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002375 if (VA.getLocInfo() == CCValAssign::Indirect)
2376 return false;
2377 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002378 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2379 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002380 return false;
2381 }
2382 }
2383 }
Evan Chenga6bff982010-01-30 01:22:00 +00002384 }
Evan Chengb1712452010-01-27 06:25:16 +00002385
Evan Cheng86809cc2010-02-03 03:28:02 +00002386 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002387}
2388
Dan Gohman3df24e62008-09-03 23:12:08 +00002389FastISel *
Evan Chengddc419c2010-01-26 19:04:47 +00002390X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2391 DwarfWriter *dw,
2392 DenseMap<const Value *, unsigned> &vm,
2393 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2394 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002395#ifndef NDEBUG
Evan Chengddc419c2010-01-26 19:04:47 +00002396 , SmallSet<Instruction*, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002397#endif
2398 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002399 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002400#ifndef NDEBUG
2401 , cil
2402#endif
2403 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002404}
2405
2406
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002407//===----------------------------------------------------------------------===//
2408// Other Lowering Hooks
2409//===----------------------------------------------------------------------===//
2410
2411
Dan Gohman475871a2008-07-27 21:46:04 +00002412SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002413 MachineFunction &MF = DAG.getMachineFunction();
2414 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2415 int ReturnAddrIndex = FuncInfo->getRAIndex();
2416
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002417 if (ReturnAddrIndex == 0) {
2418 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002419 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002420 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002421 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002422 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002423 }
2424
Evan Cheng25ab6902006-09-08 06:48:29 +00002425 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002426}
2427
2428
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002429bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2430 bool hasSymbolicDisplacement) {
2431 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002432 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002433 return false;
2434
2435 // If we don't have a symbolic displacement - we don't have any extra
2436 // restrictions.
2437 if (!hasSymbolicDisplacement)
2438 return true;
2439
2440 // FIXME: Some tweaks might be needed for medium code model.
2441 if (M != CodeModel::Small && M != CodeModel::Kernel)
2442 return false;
2443
2444 // For small code model we assume that latest object is 16MB before end of 31
2445 // bits boundary. We may also accept pretty large negative constants knowing
2446 // that all objects are in the positive half of address space.
2447 if (M == CodeModel::Small && Offset < 16*1024*1024)
2448 return true;
2449
2450 // For kernel code model we know that all object resist in the negative half
2451 // of 32bits address space. We may not accept negative offsets, since they may
2452 // be just off and we may accept pretty large positive ones.
2453 if (M == CodeModel::Kernel && Offset > 0)
2454 return true;
2455
2456 return false;
2457}
2458
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002459/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2460/// specific condition code, returning the condition code and the LHS/RHS of the
2461/// comparison to make.
2462static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2463 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002464 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002465 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2466 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2467 // X > -1 -> X == 0, jump !sign.
2468 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002469 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002470 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2471 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002472 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002473 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002474 // X < 1 -> X <= 0
2475 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002476 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002477 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002478 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002479
Evan Chengd9558e02006-01-06 00:43:03 +00002480 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002481 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002482 case ISD::SETEQ: return X86::COND_E;
2483 case ISD::SETGT: return X86::COND_G;
2484 case ISD::SETGE: return X86::COND_GE;
2485 case ISD::SETLT: return X86::COND_L;
2486 case ISD::SETLE: return X86::COND_LE;
2487 case ISD::SETNE: return X86::COND_NE;
2488 case ISD::SETULT: return X86::COND_B;
2489 case ISD::SETUGT: return X86::COND_A;
2490 case ISD::SETULE: return X86::COND_BE;
2491 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002492 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002493 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002494
Chris Lattner4c78e022008-12-23 23:42:27 +00002495 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002496
Chris Lattner4c78e022008-12-23 23:42:27 +00002497 // If LHS is a foldable load, but RHS is not, flip the condition.
2498 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2499 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2500 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2501 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002502 }
2503
Chris Lattner4c78e022008-12-23 23:42:27 +00002504 switch (SetCCOpcode) {
2505 default: break;
2506 case ISD::SETOLT:
2507 case ISD::SETOLE:
2508 case ISD::SETUGT:
2509 case ISD::SETUGE:
2510 std::swap(LHS, RHS);
2511 break;
2512 }
2513
2514 // On a floating point condition, the flags are set as follows:
2515 // ZF PF CF op
2516 // 0 | 0 | 0 | X > Y
2517 // 0 | 0 | 1 | X < Y
2518 // 1 | 0 | 0 | X == Y
2519 // 1 | 1 | 1 | unordered
2520 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002521 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002522 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002523 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002524 case ISD::SETOLT: // flipped
2525 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002526 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002527 case ISD::SETOLE: // flipped
2528 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002529 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002530 case ISD::SETUGT: // flipped
2531 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002532 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002533 case ISD::SETUGE: // flipped
2534 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002535 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002536 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002537 case ISD::SETNE: return X86::COND_NE;
2538 case ISD::SETUO: return X86::COND_P;
2539 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002540 case ISD::SETOEQ:
2541 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002542 }
Evan Chengd9558e02006-01-06 00:43:03 +00002543}
2544
Evan Cheng4a460802006-01-11 00:33:36 +00002545/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2546/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002547/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002548static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002549 switch (X86CC) {
2550 default:
2551 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002552 case X86::COND_B:
2553 case X86::COND_BE:
2554 case X86::COND_E:
2555 case X86::COND_P:
2556 case X86::COND_A:
2557 case X86::COND_AE:
2558 case X86::COND_NE:
2559 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002560 return true;
2561 }
2562}
2563
Evan Chengeb2f9692009-10-27 19:56:55 +00002564/// isFPImmLegal - Returns true if the target can instruction select the
2565/// specified FP immediate natively. If false, the legalizer will
2566/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002567bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002568 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2569 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2570 return true;
2571 }
2572 return false;
2573}
2574
Nate Begeman9008ca62009-04-27 18:41:29 +00002575/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2576/// the specified range (L, H].
2577static bool isUndefOrInRange(int Val, int Low, int Hi) {
2578 return (Val < 0) || (Val >= Low && Val < Hi);
2579}
2580
2581/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2582/// specified value.
2583static bool isUndefOrEqual(int Val, int CmpVal) {
2584 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002585 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002586 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002587}
2588
Nate Begeman9008ca62009-04-27 18:41:29 +00002589/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2590/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2591/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002592static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002593 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002594 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002595 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002596 return (Mask[0] < 2 && Mask[1] < 2);
2597 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002598}
2599
Nate Begeman9008ca62009-04-27 18:41:29 +00002600bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002601 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002602 N->getMask(M);
2603 return ::isPSHUFDMask(M, N->getValueType(0));
2604}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002605
Nate Begeman9008ca62009-04-27 18:41:29 +00002606/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2607/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002608static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002609 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002610 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002611
Nate Begeman9008ca62009-04-27 18:41:29 +00002612 // Lower quadword copied in order or undef.
2613 for (int i = 0; i != 4; ++i)
2614 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002615 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002616
Evan Cheng506d3df2006-03-29 23:07:14 +00002617 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002618 for (int i = 4; i != 8; ++i)
2619 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002620 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002621
Evan Cheng506d3df2006-03-29 23:07:14 +00002622 return true;
2623}
2624
Nate Begeman9008ca62009-04-27 18:41:29 +00002625bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002626 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002627 N->getMask(M);
2628 return ::isPSHUFHWMask(M, N->getValueType(0));
2629}
Evan Cheng506d3df2006-03-29 23:07:14 +00002630
Nate Begeman9008ca62009-04-27 18:41:29 +00002631/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2632/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002633static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002634 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002635 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002636
Rafael Espindola15684b22009-04-24 12:40:33 +00002637 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002638 for (int i = 4; i != 8; ++i)
2639 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002640 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002641
Rafael Espindola15684b22009-04-24 12:40:33 +00002642 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002643 for (int i = 0; i != 4; ++i)
2644 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002645 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002646
Rafael Espindola15684b22009-04-24 12:40:33 +00002647 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002648}
2649
Nate Begeman9008ca62009-04-27 18:41:29 +00002650bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002651 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002652 N->getMask(M);
2653 return ::isPSHUFLWMask(M, N->getValueType(0));
2654}
2655
Nate Begemana09008b2009-10-19 02:17:23 +00002656/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2657/// is suitable for input to PALIGNR.
2658static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2659 bool hasSSSE3) {
2660 int i, e = VT.getVectorNumElements();
2661
2662 // Do not handle v2i64 / v2f64 shuffles with palignr.
2663 if (e < 4 || !hasSSSE3)
2664 return false;
2665
2666 for (i = 0; i != e; ++i)
2667 if (Mask[i] >= 0)
2668 break;
2669
2670 // All undef, not a palignr.
2671 if (i == e)
2672 return false;
2673
2674 // Determine if it's ok to perform a palignr with only the LHS, since we
2675 // don't have access to the actual shuffle elements to see if RHS is undef.
2676 bool Unary = Mask[i] < (int)e;
2677 bool NeedsUnary = false;
2678
2679 int s = Mask[i] - i;
2680
2681 // Check the rest of the elements to see if they are consecutive.
2682 for (++i; i != e; ++i) {
2683 int m = Mask[i];
2684 if (m < 0)
2685 continue;
2686
2687 Unary = Unary && (m < (int)e);
2688 NeedsUnary = NeedsUnary || (m < s);
2689
2690 if (NeedsUnary && !Unary)
2691 return false;
2692 if (Unary && m != ((s+i) & (e-1)))
2693 return false;
2694 if (!Unary && m != (s+i))
2695 return false;
2696 }
2697 return true;
2698}
2699
2700bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2701 SmallVector<int, 8> M;
2702 N->getMask(M);
2703 return ::isPALIGNRMask(M, N->getValueType(0), true);
2704}
2705
Evan Cheng14aed5e2006-03-24 01:18:28 +00002706/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2707/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002708static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002709 int NumElems = VT.getVectorNumElements();
2710 if (NumElems != 2 && NumElems != 4)
2711 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002712
Nate Begeman9008ca62009-04-27 18:41:29 +00002713 int Half = NumElems / 2;
2714 for (int i = 0; i < Half; ++i)
2715 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002716 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002717 for (int i = Half; i < NumElems; ++i)
2718 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002719 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002720
Evan Cheng14aed5e2006-03-24 01:18:28 +00002721 return true;
2722}
2723
Nate Begeman9008ca62009-04-27 18:41:29 +00002724bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2725 SmallVector<int, 8> M;
2726 N->getMask(M);
2727 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002728}
2729
Evan Cheng213d2cf2007-05-17 18:45:50 +00002730/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002731/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2732/// half elements to come from vector 1 (which would equal the dest.) and
2733/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002734static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002735 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002736
2737 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002738 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002739
Nate Begeman9008ca62009-04-27 18:41:29 +00002740 int Half = NumElems / 2;
2741 for (int i = 0; i < Half; ++i)
2742 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002743 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002744 for (int i = Half; i < NumElems; ++i)
2745 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002746 return false;
2747 return true;
2748}
2749
Nate Begeman9008ca62009-04-27 18:41:29 +00002750static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2751 SmallVector<int, 8> M;
2752 N->getMask(M);
2753 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002754}
2755
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002756/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2757/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002758bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2759 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002760 return false;
2761
Evan Cheng2064a2b2006-03-28 06:50:32 +00002762 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002763 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2764 isUndefOrEqual(N->getMaskElt(1), 7) &&
2765 isUndefOrEqual(N->getMaskElt(2), 2) &&
2766 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002767}
2768
Nate Begeman0b10b912009-11-07 23:17:15 +00002769/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2770/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2771/// <2, 3, 2, 3>
2772bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2773 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2774
2775 if (NumElems != 4)
2776 return false;
2777
2778 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2779 isUndefOrEqual(N->getMaskElt(1), 3) &&
2780 isUndefOrEqual(N->getMaskElt(2), 2) &&
2781 isUndefOrEqual(N->getMaskElt(3), 3);
2782}
2783
Evan Cheng5ced1d82006-04-06 23:23:56 +00002784/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2785/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002786bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2787 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002788
Evan Cheng5ced1d82006-04-06 23:23:56 +00002789 if (NumElems != 2 && NumElems != 4)
2790 return false;
2791
Evan Chengc5cdff22006-04-07 21:53:05 +00002792 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002793 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002794 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002795
Evan Chengc5cdff22006-04-07 21:53:05 +00002796 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002797 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002798 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002799
2800 return true;
2801}
2802
Nate Begeman0b10b912009-11-07 23:17:15 +00002803/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2804/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2805bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002806 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002807
Evan Cheng5ced1d82006-04-06 23:23:56 +00002808 if (NumElems != 2 && NumElems != 4)
2809 return false;
2810
Evan Chengc5cdff22006-04-07 21:53:05 +00002811 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002812 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002813 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002814
Nate Begeman9008ca62009-04-27 18:41:29 +00002815 for (unsigned i = 0; i < NumElems/2; ++i)
2816 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002817 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002818
2819 return true;
2820}
2821
Evan Cheng0038e592006-03-28 00:39:58 +00002822/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2823/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002824static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002825 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002826 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002827 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002828 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002829
Nate Begeman9008ca62009-04-27 18:41:29 +00002830 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2831 int BitI = Mask[i];
2832 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002833 if (!isUndefOrEqual(BitI, j))
2834 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002835 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002836 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002837 return false;
2838 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002839 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002840 return false;
2841 }
Evan Cheng0038e592006-03-28 00:39:58 +00002842 }
Evan Cheng0038e592006-03-28 00:39:58 +00002843 return true;
2844}
2845
Nate Begeman9008ca62009-04-27 18:41:29 +00002846bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2847 SmallVector<int, 8> M;
2848 N->getMask(M);
2849 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002850}
2851
Evan Cheng4fcb9222006-03-28 02:43:26 +00002852/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2853/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002854static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002855 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002856 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002857 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002858 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002859
Nate Begeman9008ca62009-04-27 18:41:29 +00002860 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2861 int BitI = Mask[i];
2862 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002863 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002864 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002865 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002866 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002867 return false;
2868 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002869 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002870 return false;
2871 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002872 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002873 return true;
2874}
2875
Nate Begeman9008ca62009-04-27 18:41:29 +00002876bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2877 SmallVector<int, 8> M;
2878 N->getMask(M);
2879 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002880}
2881
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002882/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2883/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2884/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002885static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002886 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002887 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002888 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002889
Nate Begeman9008ca62009-04-27 18:41:29 +00002890 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2891 int BitI = Mask[i];
2892 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002893 if (!isUndefOrEqual(BitI, j))
2894 return false;
2895 if (!isUndefOrEqual(BitI1, j))
2896 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002897 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002898 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002899}
2900
Nate Begeman9008ca62009-04-27 18:41:29 +00002901bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2902 SmallVector<int, 8> M;
2903 N->getMask(M);
2904 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2905}
2906
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002907/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2908/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2909/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002910static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002911 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002912 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2913 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002914
Nate Begeman9008ca62009-04-27 18:41:29 +00002915 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2916 int BitI = Mask[i];
2917 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002918 if (!isUndefOrEqual(BitI, j))
2919 return false;
2920 if (!isUndefOrEqual(BitI1, j))
2921 return false;
2922 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002923 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002924}
2925
Nate Begeman9008ca62009-04-27 18:41:29 +00002926bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2927 SmallVector<int, 8> M;
2928 N->getMask(M);
2929 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2930}
2931
Evan Cheng017dcc62006-04-21 01:05:10 +00002932/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2933/// specifies a shuffle of elements that is suitable for input to MOVSS,
2934/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002935static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002936 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002937 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002938
2939 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002940
Nate Begeman9008ca62009-04-27 18:41:29 +00002941 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002942 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002943
Nate Begeman9008ca62009-04-27 18:41:29 +00002944 for (int i = 1; i < NumElts; ++i)
2945 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002946 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002947
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002948 return true;
2949}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002950
Nate Begeman9008ca62009-04-27 18:41:29 +00002951bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2952 SmallVector<int, 8> M;
2953 N->getMask(M);
2954 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002955}
2956
Evan Cheng017dcc62006-04-21 01:05:10 +00002957/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2958/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002959/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002960static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 bool V2IsSplat = false, bool V2IsUndef = false) {
2962 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002963 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002964 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002965
Nate Begeman9008ca62009-04-27 18:41:29 +00002966 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002967 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002968
Nate Begeman9008ca62009-04-27 18:41:29 +00002969 for (int i = 1; i < NumOps; ++i)
2970 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2971 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2972 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002973 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002974
Evan Cheng39623da2006-04-20 08:58:49 +00002975 return true;
2976}
2977
Nate Begeman9008ca62009-04-27 18:41:29 +00002978static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002979 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002980 SmallVector<int, 8> M;
2981 N->getMask(M);
2982 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002983}
2984
Evan Chengd9539472006-04-14 21:59:03 +00002985/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2986/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002987bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2988 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002989 return false;
2990
2991 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002992 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002993 int Elt = N->getMaskElt(i);
2994 if (Elt >= 0 && Elt != 1)
2995 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002996 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002997
2998 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002999 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003000 int Elt = N->getMaskElt(i);
3001 if (Elt >= 0 && Elt != 3)
3002 return false;
3003 if (Elt == 3)
3004 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003005 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003006 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003008 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003009}
3010
3011/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3012/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003013bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3014 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003015 return false;
3016
3017 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003018 for (unsigned i = 0; i < 2; ++i)
3019 if (N->getMaskElt(i) > 0)
3020 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003021
3022 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003023 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003024 int Elt = N->getMaskElt(i);
3025 if (Elt >= 0 && Elt != 2)
3026 return false;
3027 if (Elt == 2)
3028 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003029 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003031 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003032}
3033
Evan Cheng0b457f02008-09-25 20:50:48 +00003034/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3035/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003036bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3037 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003038
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 for (int i = 0; i < e; ++i)
3040 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003041 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003042 for (int i = 0; i < e; ++i)
3043 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003044 return false;
3045 return true;
3046}
3047
Evan Cheng63d33002006-03-22 08:01:21 +00003048/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003049/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003050unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003051 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3052 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3053
Evan Chengb9df0ca2006-03-22 02:53:00 +00003054 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3055 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 for (int i = 0; i < NumOperands; ++i) {
3057 int Val = SVOp->getMaskElt(NumOperands-i-1);
3058 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003059 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003060 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003061 if (i != NumOperands - 1)
3062 Mask <<= Shift;
3063 }
Evan Cheng63d33002006-03-22 08:01:21 +00003064 return Mask;
3065}
3066
Evan Cheng506d3df2006-03-29 23:07:14 +00003067/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003068/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003069unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003070 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003071 unsigned Mask = 0;
3072 // 8 nodes, but we only care about the last 4.
3073 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003074 int Val = SVOp->getMaskElt(i);
3075 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003076 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003077 if (i != 4)
3078 Mask <<= 2;
3079 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003080 return Mask;
3081}
3082
3083/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003084/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003085unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003086 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003087 unsigned Mask = 0;
3088 // 8 nodes, but we only care about the first 4.
3089 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003090 int Val = SVOp->getMaskElt(i);
3091 if (Val >= 0)
3092 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003093 if (i != 0)
3094 Mask <<= 2;
3095 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003096 return Mask;
3097}
3098
Nate Begemana09008b2009-10-19 02:17:23 +00003099/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3100/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3101unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3102 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3103 EVT VVT = N->getValueType(0);
3104 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3105 int Val = 0;
3106
3107 unsigned i, e;
3108 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3109 Val = SVOp->getMaskElt(i);
3110 if (Val >= 0)
3111 break;
3112 }
3113 return (Val - i) * EltSize;
3114}
3115
Evan Cheng37b73872009-07-30 08:33:02 +00003116/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3117/// constant +0.0.
3118bool X86::isZeroNode(SDValue Elt) {
3119 return ((isa<ConstantSDNode>(Elt) &&
3120 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3121 (isa<ConstantFPSDNode>(Elt) &&
3122 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3123}
3124
Nate Begeman9008ca62009-04-27 18:41:29 +00003125/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3126/// their permute mask.
3127static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3128 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003129 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003130 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003131 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003132
Nate Begeman5a5ca152009-04-29 05:20:52 +00003133 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 int idx = SVOp->getMaskElt(i);
3135 if (idx < 0)
3136 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003137 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003138 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003139 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003140 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003141 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003142 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3143 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003144}
3145
Evan Cheng779ccea2007-12-07 21:30:01 +00003146/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3147/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003148static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003149 unsigned NumElems = VT.getVectorNumElements();
3150 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 int idx = Mask[i];
3152 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003153 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003154 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003155 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003156 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003157 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003158 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003159}
3160
Evan Cheng533a0aa2006-04-19 20:35:22 +00003161/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3162/// match movhlps. The lower half elements should come from upper half of
3163/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003164/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003165static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3166 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003167 return false;
3168 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003169 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003170 return false;
3171 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003172 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003173 return false;
3174 return true;
3175}
3176
Evan Cheng5ced1d82006-04-06 23:23:56 +00003177/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003178/// is promoted to a vector. It also returns the LoadSDNode by reference if
3179/// required.
3180static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003181 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3182 return false;
3183 N = N->getOperand(0).getNode();
3184 if (!ISD::isNON_EXTLoad(N))
3185 return false;
3186 if (LD)
3187 *LD = cast<LoadSDNode>(N);
3188 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003189}
3190
Evan Cheng533a0aa2006-04-19 20:35:22 +00003191/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3192/// match movlp{s|d}. The lower half elements should come from lower half of
3193/// V1 (and in order), and the upper half elements should come from the upper
3194/// half of V2 (and in order). And since V1 will become the source of the
3195/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003196static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3197 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003198 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003199 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003200 // Is V2 is a vector load, don't do this transformation. We will try to use
3201 // load folding shufps op.
3202 if (ISD::isNON_EXTLoad(V2))
3203 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003204
Nate Begeman5a5ca152009-04-29 05:20:52 +00003205 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003206
Evan Cheng533a0aa2006-04-19 20:35:22 +00003207 if (NumElems != 2 && NumElems != 4)
3208 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003209 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003211 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003212 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003213 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003214 return false;
3215 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003216}
3217
Evan Cheng39623da2006-04-20 08:58:49 +00003218/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3219/// all the same.
3220static bool isSplatVector(SDNode *N) {
3221 if (N->getOpcode() != ISD::BUILD_VECTOR)
3222 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003223
Dan Gohman475871a2008-07-27 21:46:04 +00003224 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003225 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3226 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003227 return false;
3228 return true;
3229}
3230
Evan Cheng213d2cf2007-05-17 18:45:50 +00003231/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003232/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003233/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003234static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003235 SDValue V1 = N->getOperand(0);
3236 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003237 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3238 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003239 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003240 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003242 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3243 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003244 if (Opc != ISD::BUILD_VECTOR ||
3245 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003246 return false;
3247 } else if (Idx >= 0) {
3248 unsigned Opc = V1.getOpcode();
3249 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3250 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003251 if (Opc != ISD::BUILD_VECTOR ||
3252 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003253 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003254 }
3255 }
3256 return true;
3257}
3258
3259/// getZeroVector - Returns a vector of specified type with all zero elements.
3260///
Owen Andersone50ed302009-08-10 22:56:29 +00003261static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003262 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003263 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003264
Chris Lattner8a594482007-11-25 00:24:49 +00003265 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3266 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003267 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003268 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003269 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3270 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003271 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003272 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3273 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003274 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3276 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003277 }
Dale Johannesenace16102009-02-03 19:33:06 +00003278 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003279}
3280
Chris Lattner8a594482007-11-25 00:24:49 +00003281/// getOnesVector - Returns a vector of specified type with all bits set.
3282///
Owen Andersone50ed302009-08-10 22:56:29 +00003283static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003284 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003285
Chris Lattner8a594482007-11-25 00:24:49 +00003286 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3287 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003288 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003289 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003290 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003292 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003294 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003295}
3296
3297
Evan Cheng39623da2006-04-20 08:58:49 +00003298/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3299/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003300static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003301 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003302 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003303
Evan Cheng39623da2006-04-20 08:58:49 +00003304 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003305 SmallVector<int, 8> MaskVec;
3306 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003307
Nate Begeman5a5ca152009-04-29 05:20:52 +00003308 for (unsigned i = 0; i != NumElems; ++i) {
3309 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003310 MaskVec[i] = NumElems;
3311 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003312 }
Evan Cheng39623da2006-04-20 08:58:49 +00003313 }
Evan Cheng39623da2006-04-20 08:58:49 +00003314 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003315 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3316 SVOp->getOperand(1), &MaskVec[0]);
3317 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003318}
3319
Evan Cheng017dcc62006-04-21 01:05:10 +00003320/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3321/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003322static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003323 SDValue V2) {
3324 unsigned NumElems = VT.getVectorNumElements();
3325 SmallVector<int, 8> Mask;
3326 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003327 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003328 Mask.push_back(i);
3329 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003330}
3331
Nate Begeman9008ca62009-04-27 18:41:29 +00003332/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003333static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003334 SDValue V2) {
3335 unsigned NumElems = VT.getVectorNumElements();
3336 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003337 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003338 Mask.push_back(i);
3339 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003340 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003341 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003342}
3343
Nate Begeman9008ca62009-04-27 18:41:29 +00003344/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003345static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003346 SDValue V2) {
3347 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003348 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003350 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 Mask.push_back(i + Half);
3352 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003353 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003354 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003355}
3356
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003357/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003358static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 bool HasSSE2) {
3360 if (SV->getValueType(0).getVectorNumElements() <= 4)
3361 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003362
Owen Anderson825b72b2009-08-11 20:47:22 +00003363 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003364 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003365 DebugLoc dl = SV->getDebugLoc();
3366 SDValue V1 = SV->getOperand(0);
3367 int NumElems = VT.getVectorNumElements();
3368 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003369
Nate Begeman9008ca62009-04-27 18:41:29 +00003370 // unpack elements to the correct location
3371 while (NumElems > 4) {
3372 if (EltNo < NumElems/2) {
3373 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3374 } else {
3375 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3376 EltNo -= NumElems/2;
3377 }
3378 NumElems >>= 1;
3379 }
Eric Christopherfd179292009-08-27 18:07:15 +00003380
Nate Begeman9008ca62009-04-27 18:41:29 +00003381 // Perform the splat.
3382 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003383 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3385 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003386}
3387
Evan Chengba05f722006-04-21 23:03:30 +00003388/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003389/// vector of zero or undef vector. This produces a shuffle where the low
3390/// element of V2 is swizzled into the zero/undef vector, landing at element
3391/// 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 +00003392static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003393 bool isZero, bool HasSSE2,
3394 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003395 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003396 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003397 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3398 unsigned NumElems = VT.getVectorNumElements();
3399 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003400 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003401 // If this is the insertion idx, put the low elt of V2 here.
3402 MaskVec.push_back(i == Idx ? NumElems : i);
3403 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003404}
3405
Evan Chengf26ffe92008-05-29 08:22:04 +00003406/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3407/// a shuffle that is zero.
3408static
Nate Begeman9008ca62009-04-27 18:41:29 +00003409unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3410 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003411 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003413 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003414 int Idx = SVOp->getMaskElt(Index);
3415 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003416 ++NumZeros;
3417 continue;
3418 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003419 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003420 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003421 ++NumZeros;
3422 else
3423 break;
3424 }
3425 return NumZeros;
3426}
3427
3428/// isVectorShift - Returns true if the shuffle can be implemented as a
3429/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003430/// FIXME: split into pslldqi, psrldqi, palignr variants.
3431static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003432 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003433 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003434
3435 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003436 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003437 if (!NumZeros) {
3438 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003439 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003440 if (!NumZeros)
3441 return false;
3442 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003443 bool SeenV1 = false;
3444 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 for (int i = NumZeros; i < NumElems; ++i) {
3446 int Val = isLeft ? (i - NumZeros) : i;
3447 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3448 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003449 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003450 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003451 SeenV1 = true;
3452 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003453 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003454 SeenV2 = true;
3455 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003456 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003457 return false;
3458 }
3459 if (SeenV1 && SeenV2)
3460 return false;
3461
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003463 ShAmt = NumZeros;
3464 return true;
3465}
3466
3467
Evan Chengc78d3b42006-04-24 18:01:45 +00003468/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3469///
Dan Gohman475871a2008-07-27 21:46:04 +00003470static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003471 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003472 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003473 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003474 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003475
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003476 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003477 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003478 bool First = true;
3479 for (unsigned i = 0; i < 16; ++i) {
3480 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3481 if (ThisIsNonZero && First) {
3482 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003483 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003484 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003485 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003486 First = false;
3487 }
3488
3489 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003490 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003491 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3492 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003493 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003494 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003495 }
3496 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3498 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3499 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003500 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003501 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003502 } else
3503 ThisElt = LastElt;
3504
Gabor Greifba36cb52008-08-28 21:40:38 +00003505 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003506 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003507 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003508 }
3509 }
3510
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003512}
3513
Bill Wendlinga348c562007-03-22 18:42:45 +00003514/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003515///
Dan Gohman475871a2008-07-27 21:46:04 +00003516static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003517 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003518 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003519 if (NumNonZero > 4)
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 < 8; ++i) {
3526 bool isNonZero = (NonZeros & (1 << i)) != 0;
3527 if (isNonZero) {
3528 if (First) {
3529 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003531 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003533 First = false;
3534 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003535 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003537 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003538 }
3539 }
3540
3541 return V;
3542}
3543
Evan Chengf26ffe92008-05-29 08:22:04 +00003544/// getVShift - Return a vector logical shift node.
3545///
Owen Andersone50ed302009-08-10 22:56:29 +00003546static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003547 unsigned NumBits, SelectionDAG &DAG,
3548 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003549 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003551 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003552 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3553 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3554 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003555 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003556}
3557
Dan Gohman475871a2008-07-27 21:46:04 +00003558SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003559X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3560 SelectionDAG &DAG) {
3561
3562 // Check if the scalar load can be widened into a vector load. And if
3563 // the address is "base + cst" see if the cst can be "absorbed" into
3564 // the shuffle mask.
3565 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3566 SDValue Ptr = LD->getBasePtr();
3567 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3568 return SDValue();
3569 EVT PVT = LD->getValueType(0);
3570 if (PVT != MVT::i32 && PVT != MVT::f32)
3571 return SDValue();
3572
3573 int FI = -1;
3574 int64_t Offset = 0;
3575 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3576 FI = FINode->getIndex();
3577 Offset = 0;
3578 } else if (Ptr.getOpcode() == ISD::ADD &&
3579 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3580 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3581 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3582 Offset = Ptr.getConstantOperandVal(1);
3583 Ptr = Ptr.getOperand(0);
3584 } else {
3585 return SDValue();
3586 }
3587
3588 SDValue Chain = LD->getChain();
3589 // Make sure the stack object alignment is at least 16.
3590 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3591 if (DAG.InferPtrAlignment(Ptr) < 16) {
3592 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003593 // Can't change the alignment. FIXME: It's possible to compute
3594 // the exact stack offset and reference FI + adjust offset instead.
3595 // If someone *really* cares about this. That's the way to implement it.
3596 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003597 } else {
3598 MFI->setObjectAlignment(FI, 16);
3599 }
3600 }
3601
3602 // (Offset % 16) must be multiple of 4. Then address is then
3603 // Ptr + (Offset & ~15).
3604 if (Offset < 0)
3605 return SDValue();
3606 if ((Offset % 16) & 3)
3607 return SDValue();
3608 int64_t StartOffset = Offset & ~15;
3609 if (StartOffset)
3610 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3611 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3612
3613 int EltNo = (Offset - StartOffset) >> 2;
3614 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3615 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003616 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3617 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003618 // Canonicalize it to a v4i32 shuffle.
3619 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3620 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3621 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3622 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3623 }
3624
3625 return SDValue();
3626}
3627
Nate Begeman1449f292010-03-24 22:19:06 +00003628/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3629/// vector of type 'VT', see if the elements can be replaced by a single large
3630/// load which has the same value as a build_vector whose operands are 'elts'.
3631///
3632/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3633///
3634/// FIXME: we'd also like to handle the case where the last elements are zero
3635/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3636/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003637static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3638 DebugLoc &dl, SelectionDAG &DAG) {
3639 EVT EltVT = VT.getVectorElementType();
3640 unsigned NumElems = Elts.size();
3641
Nate Begemanfdea31a2010-03-24 20:49:50 +00003642 LoadSDNode *LDBase = NULL;
3643 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003644
3645 // For each element in the initializer, see if we've found a load or an undef.
3646 // If we don't find an initial load element, or later load elements are
3647 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003648 for (unsigned i = 0; i < NumElems; ++i) {
3649 SDValue Elt = Elts[i];
3650
3651 if (!Elt.getNode() ||
3652 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3653 return SDValue();
3654 if (!LDBase) {
3655 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3656 return SDValue();
3657 LDBase = cast<LoadSDNode>(Elt.getNode());
3658 LastLoadedElt = i;
3659 continue;
3660 }
3661 if (Elt.getOpcode() == ISD::UNDEF)
3662 continue;
3663
3664 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3665 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3666 return SDValue();
3667 LastLoadedElt = i;
3668 }
Nate Begeman1449f292010-03-24 22:19:06 +00003669
3670 // If we have found an entire vector of loads and undefs, then return a large
3671 // load of the entire vector width starting at the base pointer. If we found
3672 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003673 if (LastLoadedElt == NumElems - 1) {
3674 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3675 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3676 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3677 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3678 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3679 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3680 LDBase->isVolatile(), LDBase->isNonTemporal(),
3681 LDBase->getAlignment());
3682 } else if (NumElems == 4 && LastLoadedElt == 1) {
3683 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3684 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3685 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3686 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3687 }
3688 return SDValue();
3689}
3690
Evan Chengc3630942009-12-09 21:00:30 +00003691SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003692X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003693 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003694 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003695 if (ISD::isBuildVectorAllZeros(Op.getNode())
3696 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003697 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3698 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3699 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003700 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003701 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003702
Gabor Greifba36cb52008-08-28 21:40:38 +00003703 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003704 return getOnesVector(Op.getValueType(), DAG, dl);
3705 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003706 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003707
Owen Andersone50ed302009-08-10 22:56:29 +00003708 EVT VT = Op.getValueType();
3709 EVT ExtVT = VT.getVectorElementType();
3710 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003711
3712 unsigned NumElems = Op.getNumOperands();
3713 unsigned NumZero = 0;
3714 unsigned NumNonZero = 0;
3715 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003716 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003717 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003718 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003719 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003720 if (Elt.getOpcode() == ISD::UNDEF)
3721 continue;
3722 Values.insert(Elt);
3723 if (Elt.getOpcode() != ISD::Constant &&
3724 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003725 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003726 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003727 NumZero++;
3728 else {
3729 NonZeros |= (1 << i);
3730 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003731 }
3732 }
3733
Dan Gohman7f321562007-06-25 16:23:39 +00003734 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003735 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003736 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003737 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003738
Chris Lattner67f453a2008-03-09 05:42:06 +00003739 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003740 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003741 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003742 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003743
Chris Lattner62098042008-03-09 01:05:04 +00003744 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3745 // the value are obviously zero, truncate the value to i32 and do the
3746 // insertion that way. Only do this if the value is non-constant or if the
3747 // value is a constant being inserted into element 0. It is cheaper to do
3748 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003749 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003750 (!IsAllConstants || Idx == 0)) {
3751 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3752 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003753 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3754 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003755
Chris Lattner62098042008-03-09 01:05:04 +00003756 // Truncate the value (which may itself be a constant) to i32, and
3757 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003758 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003759 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003760 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3761 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003762
Chris Lattner62098042008-03-09 01:05:04 +00003763 // Now we have our 32-bit value zero extended in the low element of
3764 // a vector. If Idx != 0, swizzle it into place.
3765 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003766 SmallVector<int, 4> Mask;
3767 Mask.push_back(Idx);
3768 for (unsigned i = 1; i != VecElts; ++i)
3769 Mask.push_back(i);
3770 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003771 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003772 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003773 }
Dale Johannesenace16102009-02-03 19:33:06 +00003774 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003775 }
3776 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003777
Chris Lattner19f79692008-03-08 22:59:52 +00003778 // If we have a constant or non-constant insertion into the low element of
3779 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3780 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003781 // depending on what the source datatype is.
3782 if (Idx == 0) {
3783 if (NumZero == 0) {
3784 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003785 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3786 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003787 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3788 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3789 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3790 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003791 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3792 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3793 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003794 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3795 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3796 Subtarget->hasSSE2(), DAG);
3797 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3798 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003799 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003800
3801 // Is it a vector logical left shift?
3802 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003803 X86::isZeroNode(Op.getOperand(0)) &&
3804 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003805 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003806 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003807 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003808 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003809 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003810 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003811
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003812 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003813 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003814
Chris Lattner19f79692008-03-08 22:59:52 +00003815 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3816 // is a non-constant being inserted into an element other than the low one,
3817 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3818 // movd/movss) to move this into the low element, then shuffle it into
3819 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003820 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003821 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003822
Evan Cheng0db9fe62006-04-25 20:13:52 +00003823 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003824 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3825 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003826 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003827 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003828 MaskVec.push_back(i == Idx ? 0 : 1);
3829 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003830 }
3831 }
3832
Chris Lattner67f453a2008-03-09 05:42:06 +00003833 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003834 if (Values.size() == 1) {
3835 if (EVTBits == 32) {
3836 // Instead of a shuffle like this:
3837 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3838 // Check if it's possible to issue this instead.
3839 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3840 unsigned Idx = CountTrailingZeros_32(NonZeros);
3841 SDValue Item = Op.getOperand(Idx);
3842 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3843 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3844 }
Dan Gohman475871a2008-07-27 21:46:04 +00003845 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003846 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003847
Dan Gohmana3941172007-07-24 22:55:08 +00003848 // A vector full of immediates; various special cases are already
3849 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003850 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003851 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003852
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003853 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003854 if (EVTBits == 64) {
3855 if (NumNonZero == 1) {
3856 // One half is zero or undef.
3857 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003858 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003859 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003860 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3861 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003862 }
Dan Gohman475871a2008-07-27 21:46:04 +00003863 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003864 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003865
3866 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003867 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003868 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003869 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003870 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003871 }
3872
Bill Wendling826f36f2007-03-28 00:57:11 +00003873 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003874 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003875 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003876 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003877 }
3878
3879 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003880 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003881 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003882 if (NumElems == 4 && NumZero > 0) {
3883 for (unsigned i = 0; i < 4; ++i) {
3884 bool isZero = !(NonZeros & (1 << i));
3885 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003886 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003887 else
Dale Johannesenace16102009-02-03 19:33:06 +00003888 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003889 }
3890
3891 for (unsigned i = 0; i < 2; ++i) {
3892 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3893 default: break;
3894 case 0:
3895 V[i] = V[i*2]; // Must be a zero vector.
3896 break;
3897 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003898 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003899 break;
3900 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003901 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003902 break;
3903 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003904 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003905 break;
3906 }
3907 }
3908
Nate Begeman9008ca62009-04-27 18:41:29 +00003909 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003910 bool Reverse = (NonZeros & 0x3) == 2;
3911 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003912 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003913 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3914 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003915 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3916 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003917 }
3918
Nate Begemanfdea31a2010-03-24 20:49:50 +00003919 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3920 // Check for a build vector of consecutive loads.
3921 for (unsigned i = 0; i < NumElems; ++i)
3922 V[i] = Op.getOperand(i);
3923
3924 // Check for elements which are consecutive loads.
3925 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3926 if (LD.getNode())
3927 return LD;
3928
3929 // For SSE 4.1, use inserts into undef.
3930 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003931 V[0] = DAG.getUNDEF(VT);
3932 for (unsigned i = 0; i < NumElems; ++i)
3933 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3934 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3935 Op.getOperand(i), DAG.getIntPtrConstant(i));
3936 return V[0];
3937 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00003938
3939 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00003940 // e.g. for v4f32
3941 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3942 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3943 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003944 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003945 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003946 NumElems >>= 1;
3947 while (NumElems != 0) {
3948 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003949 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003950 NumElems >>= 1;
3951 }
3952 return V[0];
3953 }
Dan Gohman475871a2008-07-27 21:46:04 +00003954 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003955}
3956
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003957SDValue
3958X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3959 // We support concatenate two MMX registers and place them in a MMX
3960 // register. This is better than doing a stack convert.
3961 DebugLoc dl = Op.getDebugLoc();
3962 EVT ResVT = Op.getValueType();
3963 assert(Op.getNumOperands() == 2);
3964 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3965 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3966 int Mask[2];
3967 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3968 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3969 InVec = Op.getOperand(1);
3970 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3971 unsigned NumElts = ResVT.getVectorNumElements();
3972 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3973 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3974 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3975 } else {
3976 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3977 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3978 Mask[0] = 0; Mask[1] = 2;
3979 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3980 }
3981 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3982}
3983
Nate Begemanb9a47b82009-02-23 08:49:38 +00003984// v8i16 shuffles - Prefer shuffles in the following order:
3985// 1. [all] pshuflw, pshufhw, optional move
3986// 2. [ssse3] 1 x pshufb
3987// 3. [ssse3] 2 x pshufb + 1 x por
3988// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003989static
Nate Begeman9008ca62009-04-27 18:41:29 +00003990SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3991 SelectionDAG &DAG, X86TargetLowering &TLI) {
3992 SDValue V1 = SVOp->getOperand(0);
3993 SDValue V2 = SVOp->getOperand(1);
3994 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003995 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003996
Nate Begemanb9a47b82009-02-23 08:49:38 +00003997 // Determine if more than 1 of the words in each of the low and high quadwords
3998 // of the result come from the same quadword of one of the two inputs. Undef
3999 // mask values count as coming from any quadword, for better codegen.
4000 SmallVector<unsigned, 4> LoQuad(4);
4001 SmallVector<unsigned, 4> HiQuad(4);
4002 BitVector InputQuads(4);
4003 for (unsigned i = 0; i < 8; ++i) {
4004 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004005 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004006 MaskVals.push_back(EltIdx);
4007 if (EltIdx < 0) {
4008 ++Quad[0];
4009 ++Quad[1];
4010 ++Quad[2];
4011 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004012 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004013 }
4014 ++Quad[EltIdx / 4];
4015 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004016 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004017
Nate Begemanb9a47b82009-02-23 08:49:38 +00004018 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004019 unsigned MaxQuad = 1;
4020 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004021 if (LoQuad[i] > MaxQuad) {
4022 BestLoQuad = i;
4023 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004024 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004025 }
4026
Nate Begemanb9a47b82009-02-23 08:49:38 +00004027 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004028 MaxQuad = 1;
4029 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004030 if (HiQuad[i] > MaxQuad) {
4031 BestHiQuad = i;
4032 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004033 }
4034 }
4035
Nate Begemanb9a47b82009-02-23 08:49:38 +00004036 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004037 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004038 // single pshufb instruction is necessary. If There are more than 2 input
4039 // quads, disable the next transformation since it does not help SSSE3.
4040 bool V1Used = InputQuads[0] || InputQuads[1];
4041 bool V2Used = InputQuads[2] || InputQuads[3];
4042 if (TLI.getSubtarget()->hasSSSE3()) {
4043 if (InputQuads.count() == 2 && V1Used && V2Used) {
4044 BestLoQuad = InputQuads.find_first();
4045 BestHiQuad = InputQuads.find_next(BestLoQuad);
4046 }
4047 if (InputQuads.count() > 2) {
4048 BestLoQuad = -1;
4049 BestHiQuad = -1;
4050 }
4051 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004052
Nate Begemanb9a47b82009-02-23 08:49:38 +00004053 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4054 // the shuffle mask. If a quad is scored as -1, that means that it contains
4055 // words from all 4 input quadwords.
4056 SDValue NewV;
4057 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004058 SmallVector<int, 8> MaskV;
4059 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4060 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004061 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004062 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4063 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4064 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004065
Nate Begemanb9a47b82009-02-23 08:49:38 +00004066 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4067 // source words for the shuffle, to aid later transformations.
4068 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004069 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004070 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004071 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004072 if (idx != (int)i)
4073 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004074 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004075 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004076 AllWordsInNewV = false;
4077 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004078 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004079
Nate Begemanb9a47b82009-02-23 08:49:38 +00004080 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4081 if (AllWordsInNewV) {
4082 for (int i = 0; i != 8; ++i) {
4083 int idx = MaskVals[i];
4084 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004085 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004086 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004087 if ((idx != i) && idx < 4)
4088 pshufhw = false;
4089 if ((idx != i) && idx > 3)
4090 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004091 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004092 V1 = NewV;
4093 V2Used = false;
4094 BestLoQuad = 0;
4095 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004096 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004097
Nate Begemanb9a47b82009-02-23 08:49:38 +00004098 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4099 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004100 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004101 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004102 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004103 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004104 }
Eric Christopherfd179292009-08-27 18:07:15 +00004105
Nate Begemanb9a47b82009-02-23 08:49:38 +00004106 // If we have SSSE3, and all words of the result are from 1 input vector,
4107 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4108 // is present, fall back to case 4.
4109 if (TLI.getSubtarget()->hasSSSE3()) {
4110 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004111
Nate Begemanb9a47b82009-02-23 08:49:38 +00004112 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004113 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004114 // mask, and elements that come from V1 in the V2 mask, so that the two
4115 // results can be OR'd together.
4116 bool TwoInputs = V1Used && V2Used;
4117 for (unsigned i = 0; i != 8; ++i) {
4118 int EltIdx = MaskVals[i] * 2;
4119 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004120 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4121 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004122 continue;
4123 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004124 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4125 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004126 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004127 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004128 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004129 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004130 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004131 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004132 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004133
Nate Begemanb9a47b82009-02-23 08:49:38 +00004134 // Calculate the shuffle mask for the second input, shuffle it, and
4135 // OR it with the first shuffled input.
4136 pshufbMask.clear();
4137 for (unsigned i = 0; i != 8; ++i) {
4138 int EltIdx = MaskVals[i] * 2;
4139 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004140 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4141 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004142 continue;
4143 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4145 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004146 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004148 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004149 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 MVT::v16i8, &pshufbMask[0], 16));
4151 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4152 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004153 }
4154
4155 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4156 // and update MaskVals with new element order.
4157 BitVector InOrder(8);
4158 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004159 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004160 for (int i = 0; i != 4; ++i) {
4161 int idx = MaskVals[i];
4162 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004163 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004164 InOrder.set(i);
4165 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004166 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004167 InOrder.set(i);
4168 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004169 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004170 }
4171 }
4172 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004173 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004175 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004176 }
Eric Christopherfd179292009-08-27 18:07:15 +00004177
Nate Begemanb9a47b82009-02-23 08:49:38 +00004178 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4179 // and update MaskVals with the new element order.
4180 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004181 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004182 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004183 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004184 for (unsigned i = 4; i != 8; ++i) {
4185 int idx = MaskVals[i];
4186 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004188 InOrder.set(i);
4189 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004190 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004191 InOrder.set(i);
4192 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004193 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004194 }
4195 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004196 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004197 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004198 }
Eric Christopherfd179292009-08-27 18:07:15 +00004199
Nate Begemanb9a47b82009-02-23 08:49:38 +00004200 // In case BestHi & BestLo were both -1, which means each quadword has a word
4201 // from each of the four input quadwords, calculate the InOrder bitvector now
4202 // before falling through to the insert/extract cleanup.
4203 if (BestLoQuad == -1 && BestHiQuad == -1) {
4204 NewV = V1;
4205 for (int i = 0; i != 8; ++i)
4206 if (MaskVals[i] < 0 || MaskVals[i] == i)
4207 InOrder.set(i);
4208 }
Eric Christopherfd179292009-08-27 18:07:15 +00004209
Nate Begemanb9a47b82009-02-23 08:49:38 +00004210 // The other elements are put in the right place using pextrw and pinsrw.
4211 for (unsigned i = 0; i != 8; ++i) {
4212 if (InOrder[i])
4213 continue;
4214 int EltIdx = MaskVals[i];
4215 if (EltIdx < 0)
4216 continue;
4217 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004219 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004221 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004222 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004223 DAG.getIntPtrConstant(i));
4224 }
4225 return NewV;
4226}
4227
4228// v16i8 shuffles - Prefer shuffles in the following order:
4229// 1. [ssse3] 1 x pshufb
4230// 2. [ssse3] 2 x pshufb + 1 x por
4231// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4232static
Nate Begeman9008ca62009-04-27 18:41:29 +00004233SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4234 SelectionDAG &DAG, X86TargetLowering &TLI) {
4235 SDValue V1 = SVOp->getOperand(0);
4236 SDValue V2 = SVOp->getOperand(1);
4237 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004238 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004239 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004240
Nate Begemanb9a47b82009-02-23 08:49:38 +00004241 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004242 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004243 // present, fall back to case 3.
4244 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4245 bool V1Only = true;
4246 bool V2Only = true;
4247 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004248 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004249 if (EltIdx < 0)
4250 continue;
4251 if (EltIdx < 16)
4252 V2Only = false;
4253 else
4254 V1Only = false;
4255 }
Eric Christopherfd179292009-08-27 18:07:15 +00004256
Nate Begemanb9a47b82009-02-23 08:49:38 +00004257 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4258 if (TLI.getSubtarget()->hasSSSE3()) {
4259 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004260
Nate Begemanb9a47b82009-02-23 08:49:38 +00004261 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004262 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004263 //
4264 // Otherwise, we have elements from both input vectors, and must zero out
4265 // elements that come from V2 in the first mask, and V1 in the second mask
4266 // so that we can OR them together.
4267 bool TwoInputs = !(V1Only || V2Only);
4268 for (unsigned i = 0; i != 16; ++i) {
4269 int EltIdx = MaskVals[i];
4270 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004271 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004272 continue;
4273 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004274 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004275 }
4276 // If all the elements are from V2, assign it to V1 and return after
4277 // building the first pshufb.
4278 if (V2Only)
4279 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004280 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004281 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004282 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004283 if (!TwoInputs)
4284 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004285
Nate Begemanb9a47b82009-02-23 08:49:38 +00004286 // Calculate the shuffle mask for the second input, shuffle it, and
4287 // OR it with the first shuffled input.
4288 pshufbMask.clear();
4289 for (unsigned i = 0; i != 16; ++i) {
4290 int EltIdx = MaskVals[i];
4291 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004292 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004293 continue;
4294 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004295 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004296 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004297 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004298 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004299 MVT::v16i8, &pshufbMask[0], 16));
4300 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004301 }
Eric Christopherfd179292009-08-27 18:07:15 +00004302
Nate Begemanb9a47b82009-02-23 08:49:38 +00004303 // No SSSE3 - Calculate in place words and then fix all out of place words
4304 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4305 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004306 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4307 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004308 SDValue NewV = V2Only ? V2 : V1;
4309 for (int i = 0; i != 8; ++i) {
4310 int Elt0 = MaskVals[i*2];
4311 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004312
Nate Begemanb9a47b82009-02-23 08:49:38 +00004313 // This word of the result is all undef, skip it.
4314 if (Elt0 < 0 && Elt1 < 0)
4315 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004316
Nate Begemanb9a47b82009-02-23 08:49:38 +00004317 // This word of the result is already in the correct place, skip it.
4318 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4319 continue;
4320 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4321 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004322
Nate Begemanb9a47b82009-02-23 08:49:38 +00004323 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4324 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4325 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004326
4327 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4328 // using a single extract together, load it and store it.
4329 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004330 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004331 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004332 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004333 DAG.getIntPtrConstant(i));
4334 continue;
4335 }
4336
Nate Begemanb9a47b82009-02-23 08:49:38 +00004337 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004338 // source byte is not also odd, shift the extracted word left 8 bits
4339 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004340 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004341 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004342 DAG.getIntPtrConstant(Elt1 / 2));
4343 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004344 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004345 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004346 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004347 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4348 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004349 }
4350 // If Elt0 is defined, extract it from the appropriate source. If the
4351 // source byte is not also even, shift the extracted word right 8 bits. If
4352 // Elt1 was also defined, OR the extracted values together before
4353 // inserting them in the result.
4354 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004355 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004356 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4357 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004358 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004359 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004360 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004361 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4362 DAG.getConstant(0x00FF, MVT::i16));
4363 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004364 : InsElt0;
4365 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004366 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004367 DAG.getIntPtrConstant(i));
4368 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004369 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004370}
4371
Evan Cheng7a831ce2007-12-15 03:00:47 +00004372/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4373/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4374/// done when every pair / quad of shuffle mask elements point to elements in
4375/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004376/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4377static
Nate Begeman9008ca62009-04-27 18:41:29 +00004378SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4379 SelectionDAG &DAG,
4380 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004381 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 SDValue V1 = SVOp->getOperand(0);
4383 SDValue V2 = SVOp->getOperand(1);
4384 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004385 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004386 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004387 EVT MaskEltVT = MaskVT.getVectorElementType();
4388 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004389 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004390 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004391 case MVT::v4f32: NewVT = MVT::v2f64; break;
4392 case MVT::v4i32: NewVT = MVT::v2i64; break;
4393 case MVT::v8i16: NewVT = MVT::v4i32; break;
4394 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004395 }
4396
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004397 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004398 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004399 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004400 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004401 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004402 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004403 int Scale = NumElems / NewWidth;
4404 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004405 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004406 int StartIdx = -1;
4407 for (int j = 0; j < Scale; ++j) {
4408 int EltIdx = SVOp->getMaskElt(i+j);
4409 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004410 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004411 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004412 StartIdx = EltIdx - (EltIdx % Scale);
4413 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004414 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004415 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004416 if (StartIdx == -1)
4417 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004418 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004419 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004420 }
4421
Dale Johannesenace16102009-02-03 19:33:06 +00004422 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4423 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004424 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004425}
4426
Evan Chengd880b972008-05-09 21:53:03 +00004427/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004428///
Owen Andersone50ed302009-08-10 22:56:29 +00004429static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004430 SDValue SrcOp, SelectionDAG &DAG,
4431 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004432 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004433 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004434 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004435 LD = dyn_cast<LoadSDNode>(SrcOp);
4436 if (!LD) {
4437 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4438 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004439 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4440 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004441 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4442 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004443 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004444 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004445 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004446 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4447 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4448 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4449 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004450 SrcOp.getOperand(0)
4451 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004452 }
4453 }
4454 }
4455
Dale Johannesenace16102009-02-03 19:33:06 +00004456 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4457 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004458 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004459 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004460}
4461
Evan Chengace3c172008-07-22 21:13:36 +00004462/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4463/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004464static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004465LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4466 SDValue V1 = SVOp->getOperand(0);
4467 SDValue V2 = SVOp->getOperand(1);
4468 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004469 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004470
Evan Chengace3c172008-07-22 21:13:36 +00004471 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004472 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004473 SmallVector<int, 8> Mask1(4U, -1);
4474 SmallVector<int, 8> PermMask;
4475 SVOp->getMask(PermMask);
4476
Evan Chengace3c172008-07-22 21:13:36 +00004477 unsigned NumHi = 0;
4478 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004479 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004480 int Idx = PermMask[i];
4481 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004482 Locs[i] = std::make_pair(-1, -1);
4483 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4485 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004486 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004487 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004488 NumLo++;
4489 } else {
4490 Locs[i] = std::make_pair(1, NumHi);
4491 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004492 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004493 NumHi++;
4494 }
4495 }
4496 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004497
Evan Chengace3c172008-07-22 21:13:36 +00004498 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004499 // If no more than two elements come from either vector. This can be
4500 // implemented with two shuffles. First shuffle gather the elements.
4501 // The second shuffle, which takes the first shuffle as both of its
4502 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004503 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004504
Nate Begeman9008ca62009-04-27 18:41:29 +00004505 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004506
Evan Chengace3c172008-07-22 21:13:36 +00004507 for (unsigned i = 0; i != 4; ++i) {
4508 if (Locs[i].first == -1)
4509 continue;
4510 else {
4511 unsigned Idx = (i < 2) ? 0 : 4;
4512 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004513 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004514 }
4515 }
4516
Nate Begeman9008ca62009-04-27 18:41:29 +00004517 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004518 } else if (NumLo == 3 || NumHi == 3) {
4519 // Otherwise, we must have three elements from one vector, call it X, and
4520 // one element from the other, call it Y. First, use a shufps to build an
4521 // intermediate vector with the one element from Y and the element from X
4522 // that will be in the same half in the final destination (the indexes don't
4523 // matter). Then, use a shufps to build the final vector, taking the half
4524 // containing the element from Y from the intermediate, and the other half
4525 // from X.
4526 if (NumHi == 3) {
4527 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004528 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004529 std::swap(V1, V2);
4530 }
4531
4532 // Find the element from V2.
4533 unsigned HiIndex;
4534 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004535 int Val = PermMask[HiIndex];
4536 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004537 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004538 if (Val >= 4)
4539 break;
4540 }
4541
Nate Begeman9008ca62009-04-27 18:41:29 +00004542 Mask1[0] = PermMask[HiIndex];
4543 Mask1[1] = -1;
4544 Mask1[2] = PermMask[HiIndex^1];
4545 Mask1[3] = -1;
4546 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004547
4548 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004549 Mask1[0] = PermMask[0];
4550 Mask1[1] = PermMask[1];
4551 Mask1[2] = HiIndex & 1 ? 6 : 4;
4552 Mask1[3] = HiIndex & 1 ? 4 : 6;
4553 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004554 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004555 Mask1[0] = HiIndex & 1 ? 2 : 0;
4556 Mask1[1] = HiIndex & 1 ? 0 : 2;
4557 Mask1[2] = PermMask[2];
4558 Mask1[3] = PermMask[3];
4559 if (Mask1[2] >= 0)
4560 Mask1[2] += 4;
4561 if (Mask1[3] >= 0)
4562 Mask1[3] += 4;
4563 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004564 }
Evan Chengace3c172008-07-22 21:13:36 +00004565 }
4566
4567 // Break it into (shuffle shuffle_hi, shuffle_lo).
4568 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004569 SmallVector<int,8> LoMask(4U, -1);
4570 SmallVector<int,8> HiMask(4U, -1);
4571
4572 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004573 unsigned MaskIdx = 0;
4574 unsigned LoIdx = 0;
4575 unsigned HiIdx = 2;
4576 for (unsigned i = 0; i != 4; ++i) {
4577 if (i == 2) {
4578 MaskPtr = &HiMask;
4579 MaskIdx = 1;
4580 LoIdx = 0;
4581 HiIdx = 2;
4582 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004583 int Idx = PermMask[i];
4584 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004585 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004586 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004587 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004588 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004589 LoIdx++;
4590 } else {
4591 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004592 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004593 HiIdx++;
4594 }
4595 }
4596
Nate Begeman9008ca62009-04-27 18:41:29 +00004597 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4598 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4599 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004600 for (unsigned i = 0; i != 4; ++i) {
4601 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004602 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004603 } else {
4604 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004605 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004606 }
4607 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004608 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004609}
4610
Dan Gohman475871a2008-07-27 21:46:04 +00004611SDValue
4612X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004613 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004614 SDValue V1 = Op.getOperand(0);
4615 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004616 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004617 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004618 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004619 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004620 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4621 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004622 bool V1IsSplat = false;
4623 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004624
Nate Begeman9008ca62009-04-27 18:41:29 +00004625 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004626 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004627
Nate Begeman9008ca62009-04-27 18:41:29 +00004628 // Promote splats to v4f32.
4629 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004630 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004631 return Op;
4632 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004633 }
4634
Evan Cheng7a831ce2007-12-15 03:00:47 +00004635 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4636 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004637 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004638 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004639 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004640 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004641 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004642 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004643 // FIXME: Figure out a cleaner way to do this.
4644 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004645 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004647 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004648 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4649 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4650 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004651 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004652 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004653 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4654 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004655 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004656 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004657 }
4658 }
Eric Christopherfd179292009-08-27 18:07:15 +00004659
Nate Begeman9008ca62009-04-27 18:41:29 +00004660 if (X86::isPSHUFDMask(SVOp))
4661 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004662
Evan Chengf26ffe92008-05-29 08:22:04 +00004663 // Check if this can be converted into a logical shift.
4664 bool isLeft = false;
4665 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004666 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004667 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004668 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004669 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004670 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004671 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004672 EVT EltVT = VT.getVectorElementType();
4673 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004674 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004675 }
Eric Christopherfd179292009-08-27 18:07:15 +00004676
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004678 if (V1IsUndef)
4679 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004680 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004681 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004682 if (!isMMX)
4683 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004684 }
Eric Christopherfd179292009-08-27 18:07:15 +00004685
Nate Begeman9008ca62009-04-27 18:41:29 +00004686 // FIXME: fold these into legal mask.
4687 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4688 X86::isMOVSLDUPMask(SVOp) ||
4689 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004690 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004692 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004693
Nate Begeman9008ca62009-04-27 18:41:29 +00004694 if (ShouldXformToMOVHLPS(SVOp) ||
4695 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4696 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004697
Evan Chengf26ffe92008-05-29 08:22:04 +00004698 if (isShift) {
4699 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004700 EVT EltVT = VT.getVectorElementType();
4701 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004702 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004703 }
Eric Christopherfd179292009-08-27 18:07:15 +00004704
Evan Cheng9eca5e82006-10-25 21:49:50 +00004705 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004706 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4707 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004708 V1IsSplat = isSplatVector(V1.getNode());
4709 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004710
Chris Lattner8a594482007-11-25 00:24:49 +00004711 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004712 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004713 Op = CommuteVectorShuffle(SVOp, DAG);
4714 SVOp = cast<ShuffleVectorSDNode>(Op);
4715 V1 = SVOp->getOperand(0);
4716 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004717 std::swap(V1IsSplat, V2IsSplat);
4718 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004719 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004720 }
4721
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4723 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004724 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004725 return V1;
4726 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4727 // the instruction selector will not match, so get a canonical MOVL with
4728 // swapped operands to undo the commute.
4729 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004730 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004731
Nate Begeman9008ca62009-04-27 18:41:29 +00004732 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4733 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4734 X86::isUNPCKLMask(SVOp) ||
4735 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004736 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004737
Evan Cheng9bbbb982006-10-25 20:48:19 +00004738 if (V2IsSplat) {
4739 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004740 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004741 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004742 SDValue NewMask = NormalizeMask(SVOp, DAG);
4743 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4744 if (NSVOp != SVOp) {
4745 if (X86::isUNPCKLMask(NSVOp, true)) {
4746 return NewMask;
4747 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4748 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004749 }
4750 }
4751 }
4752
Evan Cheng9eca5e82006-10-25 21:49:50 +00004753 if (Commuted) {
4754 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004755 // FIXME: this seems wrong.
4756 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4757 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4758 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4759 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4760 X86::isUNPCKLMask(NewSVOp) ||
4761 X86::isUNPCKHMask(NewSVOp))
4762 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004763 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004764
Nate Begemanb9a47b82009-02-23 08:49:38 +00004765 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004766
4767 // Normalize the node to match x86 shuffle ops if needed
4768 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4769 return CommuteVectorShuffle(SVOp, DAG);
4770
4771 // Check for legal shuffle and return?
4772 SmallVector<int, 16> PermMask;
4773 SVOp->getMask(PermMask);
4774 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004775 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004776
Evan Cheng14b32e12007-12-11 01:46:18 +00004777 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004778 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004779 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004780 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004781 return NewOp;
4782 }
4783
Owen Anderson825b72b2009-08-11 20:47:22 +00004784 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004785 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004786 if (NewOp.getNode())
4787 return NewOp;
4788 }
Eric Christopherfd179292009-08-27 18:07:15 +00004789
Evan Chengace3c172008-07-22 21:13:36 +00004790 // Handle all 4 wide cases with a number of shuffles except for MMX.
4791 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004792 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004793
Dan Gohman475871a2008-07-27 21:46:04 +00004794 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004795}
4796
Dan Gohman475871a2008-07-27 21:46:04 +00004797SDValue
4798X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004799 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004800 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004801 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004802 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004803 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004804 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004805 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004806 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004807 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004808 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004809 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4810 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4811 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4813 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004814 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004815 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004816 Op.getOperand(0)),
4817 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004818 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004819 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004821 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004822 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004823 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004824 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4825 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004826 // result has a single use which is a store or a bitcast to i32. And in
4827 // the case of a store, it's not worth it if the index is a constant 0,
4828 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004829 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004830 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004831 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004832 if ((User->getOpcode() != ISD::STORE ||
4833 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4834 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004835 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004836 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004837 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4839 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004840 Op.getOperand(0)),
4841 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004842 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4843 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004844 // ExtractPS works with constant index.
4845 if (isa<ConstantSDNode>(Op.getOperand(1)))
4846 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004847 }
Dan Gohman475871a2008-07-27 21:46:04 +00004848 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004849}
4850
4851
Dan Gohman475871a2008-07-27 21:46:04 +00004852SDValue
4853X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004854 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004855 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004856
Evan Cheng62a3f152008-03-24 21:52:23 +00004857 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004858 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004859 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004860 return Res;
4861 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004862
Owen Andersone50ed302009-08-10 22:56:29 +00004863 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004864 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004865 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004866 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004867 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004868 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004869 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004870 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4871 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004872 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004873 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004874 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004875 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004876 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004877 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004878 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004879 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004880 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004881 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004882 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004883 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004884 if (Idx == 0)
4885 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004886
Evan Cheng0db9fe62006-04-25 20:13:52 +00004887 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004888 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004889 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004890 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004891 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004892 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004893 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004894 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004895 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4896 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4897 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004898 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004899 if (Idx == 0)
4900 return Op;
4901
4902 // UNPCKHPD the element to the lowest double word, then movsd.
4903 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4904 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004905 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004906 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004907 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004908 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004909 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004910 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004911 }
4912
Dan Gohman475871a2008-07-27 21:46:04 +00004913 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004914}
4915
Dan Gohman475871a2008-07-27 21:46:04 +00004916SDValue
4917X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004918 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004919 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004920 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004921
Dan Gohman475871a2008-07-27 21:46:04 +00004922 SDValue N0 = Op.getOperand(0);
4923 SDValue N1 = Op.getOperand(1);
4924 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004925
Dan Gohman8a55ce42009-09-23 21:02:20 +00004926 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004927 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004928 unsigned Opc;
4929 if (VT == MVT::v8i16)
4930 Opc = X86ISD::PINSRW;
4931 else if (VT == MVT::v4i16)
4932 Opc = X86ISD::MMX_PINSRW;
4933 else if (VT == MVT::v16i8)
4934 Opc = X86ISD::PINSRB;
4935 else
4936 Opc = X86ISD::PINSRB;
4937
Nate Begeman14d12ca2008-02-11 04:19:36 +00004938 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4939 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 if (N1.getValueType() != MVT::i32)
4941 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4942 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004943 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004944 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004945 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004946 // Bits [7:6] of the constant are the source select. This will always be
4947 // zero here. The DAG Combiner may combine an extract_elt index into these
4948 // bits. For example (insert (extract, 3), 2) could be matched by putting
4949 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004950 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004951 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004952 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004953 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004954 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004955 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004956 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004957 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004958 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004959 // PINSR* works with constant index.
4960 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004961 }
Dan Gohman475871a2008-07-27 21:46:04 +00004962 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004963}
4964
Dan Gohman475871a2008-07-27 21:46:04 +00004965SDValue
4966X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004967 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004968 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004969
4970 if (Subtarget->hasSSE41())
4971 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4972
Dan Gohman8a55ce42009-09-23 21:02:20 +00004973 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004974 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004975
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004976 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004977 SDValue N0 = Op.getOperand(0);
4978 SDValue N1 = Op.getOperand(1);
4979 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004980
Dan Gohman8a55ce42009-09-23 21:02:20 +00004981 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004982 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4983 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004984 if (N1.getValueType() != MVT::i32)
4985 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4986 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004987 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004988 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
4989 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004990 }
Dan Gohman475871a2008-07-27 21:46:04 +00004991 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004992}
4993
Dan Gohman475871a2008-07-27 21:46:04 +00004994SDValue
4995X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004996 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004997 if (Op.getValueType() == MVT::v2f32)
4998 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4999 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5000 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00005001 Op.getOperand(0))));
5002
Owen Anderson825b72b2009-08-11 20:47:22 +00005003 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5004 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005005
Owen Anderson825b72b2009-08-11 20:47:22 +00005006 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5007 EVT VT = MVT::v2i32;
5008 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005009 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005010 case MVT::v16i8:
5011 case MVT::v8i16:
5012 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005013 break;
5014 }
Dale Johannesenace16102009-02-03 19:33:06 +00005015 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5016 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005017}
5018
Bill Wendling056292f2008-09-16 21:48:12 +00005019// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5020// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5021// one of the above mentioned nodes. It has to be wrapped because otherwise
5022// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5023// be used to form addressing mode. These wrapped nodes will be selected
5024// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005025SDValue
5026X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005027 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005028
Chris Lattner41621a22009-06-26 19:22:52 +00005029 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5030 // global base reg.
5031 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005032 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005033 CodeModel::Model M = getTargetMachine().getCodeModel();
5034
Chris Lattner4f066492009-07-11 20:29:19 +00005035 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005036 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005037 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005038 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005039 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005040 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005041 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005042
Evan Cheng1606e8e2009-03-13 07:51:59 +00005043 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005044 CP->getAlignment(),
5045 CP->getOffset(), OpFlag);
5046 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005047 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005048 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005049 if (OpFlag) {
5050 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005051 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00005052 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005053 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005054 }
5055
5056 return Result;
5057}
5058
Chris Lattner18c59872009-06-27 04:16:01 +00005059SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
5060 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005061
Chris Lattner18c59872009-06-27 04:16:01 +00005062 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5063 // global base reg.
5064 unsigned char OpFlag = 0;
5065 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005066 CodeModel::Model M = getTargetMachine().getCodeModel();
5067
Chris Lattner4f066492009-07-11 20:29:19 +00005068 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005069 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005070 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005071 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005072 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005073 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005074 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005075
Chris Lattner18c59872009-06-27 04:16:01 +00005076 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5077 OpFlag);
5078 DebugLoc DL = JT->getDebugLoc();
5079 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005080
Chris Lattner18c59872009-06-27 04:16:01 +00005081 // With PIC, the address is actually $g + Offset.
5082 if (OpFlag) {
5083 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5084 DAG.getNode(X86ISD::GlobalBaseReg,
5085 DebugLoc::getUnknownLoc(), getPointerTy()),
5086 Result);
5087 }
Eric Christopherfd179292009-08-27 18:07:15 +00005088
Chris Lattner18c59872009-06-27 04:16:01 +00005089 return Result;
5090}
5091
5092SDValue
5093X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
5094 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005095
Chris Lattner18c59872009-06-27 04:16:01 +00005096 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5097 // global base reg.
5098 unsigned char OpFlag = 0;
5099 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005100 CodeModel::Model M = getTargetMachine().getCodeModel();
5101
Chris Lattner4f066492009-07-11 20:29:19 +00005102 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005103 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005104 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005105 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005106 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005107 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005108 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005109
Chris Lattner18c59872009-06-27 04:16:01 +00005110 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005111
Chris Lattner18c59872009-06-27 04:16:01 +00005112 DebugLoc DL = Op.getDebugLoc();
5113 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005114
5115
Chris Lattner18c59872009-06-27 04:16:01 +00005116 // With PIC, the address is actually $g + Offset.
5117 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005118 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005119 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5120 DAG.getNode(X86ISD::GlobalBaseReg,
5121 DebugLoc::getUnknownLoc(),
5122 getPointerTy()),
5123 Result);
5124 }
Eric Christopherfd179292009-08-27 18:07:15 +00005125
Chris Lattner18c59872009-06-27 04:16:01 +00005126 return Result;
5127}
5128
Dan Gohman475871a2008-07-27 21:46:04 +00005129SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00005130X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00005131 // Create the TargetBlockAddressAddress node.
5132 unsigned char OpFlags =
5133 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005134 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00005135 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
5136 DebugLoc dl = Op.getDebugLoc();
5137 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5138 /*isTarget=*/true, OpFlags);
5139
Dan Gohmanf705adb2009-10-30 01:28:02 +00005140 if (Subtarget->isPICStyleRIPRel() &&
5141 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005142 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5143 else
5144 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005145
Dan Gohman29cbade2009-11-20 23:18:13 +00005146 // With PIC, the address is actually $g + Offset.
5147 if (isGlobalRelativeToPICBase(OpFlags)) {
5148 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5149 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5150 Result);
5151 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005152
5153 return Result;
5154}
5155
5156SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005157X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005158 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005159 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005160 // Create the TargetGlobalAddress node, folding in the constant
5161 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005162 unsigned char OpFlags =
5163 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005164 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005165 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005166 if (OpFlags == X86II::MO_NO_FLAG &&
5167 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005168 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005169 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005170 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005171 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005172 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005173 }
Eric Christopherfd179292009-08-27 18:07:15 +00005174
Chris Lattner4f066492009-07-11 20:29:19 +00005175 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005176 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005177 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5178 else
5179 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005180
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005181 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005182 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005183 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5184 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005185 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005186 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005187
Chris Lattner36c25012009-07-10 07:34:39 +00005188 // For globals that require a load from a stub to get the address, emit the
5189 // load.
5190 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005191 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005192 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005193
Dan Gohman6520e202008-10-18 02:06:02 +00005194 // If there was a non-zero offset that we didn't fold, create an explicit
5195 // addition for it.
5196 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005197 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005198 DAG.getConstant(Offset, getPointerTy()));
5199
Evan Cheng0db9fe62006-04-25 20:13:52 +00005200 return Result;
5201}
5202
Evan Chengda43bcf2008-09-24 00:05:32 +00005203SDValue
5204X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
5205 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005206 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005207 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005208}
5209
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005210static SDValue
5211GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005212 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005213 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005214 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005215 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005216 DebugLoc dl = GA->getDebugLoc();
5217 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5218 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005219 GA->getOffset(),
5220 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005221 if (InFlag) {
5222 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005223 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005224 } else {
5225 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005226 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005227 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005228
5229 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5230 MFI->setHasCalls(true);
5231
Rafael Espindola15f1b662009-04-24 12:59:40 +00005232 SDValue Flag = Chain.getValue(1);
5233 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005234}
5235
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005236// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005237static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005238LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005239 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005240 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005241 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5242 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005243 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005244 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005245 PtrVT), InFlag);
5246 InFlag = Chain.getValue(1);
5247
Chris Lattnerb903bed2009-06-26 21:20:29 +00005248 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005249}
5250
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005251// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005252static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005253LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005254 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005255 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5256 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005257}
5258
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005259// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5260// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005261static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005262 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005263 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005264 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005265 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005266 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5267 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005268 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005269 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005270
5271 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005272 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005273
Chris Lattnerb903bed2009-06-26 21:20:29 +00005274 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005275 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5276 // initialexec.
5277 unsigned WrapperKind = X86ISD::Wrapper;
5278 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005279 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005280 } else if (is64Bit) {
5281 assert(model == TLSModel::InitialExec);
5282 OperandFlags = X86II::MO_GOTTPOFF;
5283 WrapperKind = X86ISD::WrapperRIP;
5284 } else {
5285 assert(model == TLSModel::InitialExec);
5286 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005287 }
Eric Christopherfd179292009-08-27 18:07:15 +00005288
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005289 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5290 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005291 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005292 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005293 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005294
Rafael Espindola9a580232009-02-27 13:37:18 +00005295 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005296 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005297 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005298
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005299 // The address of the thread local variable is the add of the thread
5300 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005301 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005302}
5303
Dan Gohman475871a2008-07-27 21:46:04 +00005304SDValue
5305X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005306 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005307 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005308 assert(Subtarget->isTargetELF() &&
5309 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005310 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005311 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005312
Chris Lattnerb903bed2009-06-26 21:20:29 +00005313 // If GV is an alias then use the aliasee for determining
5314 // thread-localness.
5315 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5316 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005317
Chris Lattnerb903bed2009-06-26 21:20:29 +00005318 TLSModel::Model model = getTLSModel(GV,
5319 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005320
Chris Lattnerb903bed2009-06-26 21:20:29 +00005321 switch (model) {
5322 case TLSModel::GeneralDynamic:
5323 case TLSModel::LocalDynamic: // not implemented
5324 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005325 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005326 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005327
Chris Lattnerb903bed2009-06-26 21:20:29 +00005328 case TLSModel::InitialExec:
5329 case TLSModel::LocalExec:
5330 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5331 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005332 }
Eric Christopherfd179292009-08-27 18:07:15 +00005333
Torok Edwinc23197a2009-07-14 16:55:14 +00005334 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005335 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005336}
5337
Evan Cheng0db9fe62006-04-25 20:13:52 +00005338
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005339/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005340/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005341SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005342 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005343 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005344 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005345 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005346 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005347 SDValue ShOpLo = Op.getOperand(0);
5348 SDValue ShOpHi = Op.getOperand(1);
5349 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005350 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005351 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005352 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005353
Dan Gohman475871a2008-07-27 21:46:04 +00005354 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005355 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005356 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5357 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005358 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005359 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5360 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005361 }
Evan Chenge3413162006-01-09 18:33:28 +00005362
Owen Anderson825b72b2009-08-11 20:47:22 +00005363 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5364 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005365 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005366 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005367
Dan Gohman475871a2008-07-27 21:46:04 +00005368 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005369 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005370 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5371 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005372
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005373 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005374 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5375 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005376 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005377 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5378 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005379 }
5380
Dan Gohman475871a2008-07-27 21:46:04 +00005381 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005382 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005383}
Evan Chenga3195e82006-01-12 22:54:21 +00005384
Dan Gohman475871a2008-07-27 21:46:04 +00005385SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005386 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005387
5388 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005389 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005390 return Op;
5391 }
5392 return SDValue();
5393 }
5394
Owen Anderson825b72b2009-08-11 20:47:22 +00005395 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005396 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005397
Eli Friedman36df4992009-05-27 00:47:34 +00005398 // These are really Legal; return the operand so the caller accepts it as
5399 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005400 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005401 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005402 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005403 Subtarget->is64Bit()) {
5404 return Op;
5405 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005406
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005407 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005408 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005409 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005410 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005411 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005412 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005413 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005414 PseudoSourceValue::getFixedStack(SSFI), 0,
5415 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005416 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5417}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005418
Owen Andersone50ed302009-08-10 22:56:29 +00005419SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005420 SDValue StackSlot,
5421 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005422 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005423 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005424 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005425 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005426 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005427 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005428 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005429 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005430 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005431 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005432 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005434 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005435 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005436 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005437
5438 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5439 // shouldn't be necessary except that RFP cannot be live across
5440 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005441 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005442 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005443 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005444 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005445 SDValue Ops[] = {
5446 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5447 };
5448 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005449 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005450 PseudoSourceValue::getFixedStack(SSFI), 0,
5451 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005452 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005453
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454 return Result;
5455}
5456
Bill Wendling8b8a6362009-01-17 03:56:04 +00005457// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5458SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5459 // This algorithm is not obvious. Here it is in C code, more or less:
5460 /*
5461 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5462 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5463 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005464
Bill Wendling8b8a6362009-01-17 03:56:04 +00005465 // Copy ints to xmm registers.
5466 __m128i xh = _mm_cvtsi32_si128( hi );
5467 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005468
Bill Wendling8b8a6362009-01-17 03:56:04 +00005469 // Combine into low half of a single xmm register.
5470 __m128i x = _mm_unpacklo_epi32( xh, xl );
5471 __m128d d;
5472 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005473
Bill Wendling8b8a6362009-01-17 03:56:04 +00005474 // Merge in appropriate exponents to give the integer bits the right
5475 // magnitude.
5476 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005477
Bill Wendling8b8a6362009-01-17 03:56:04 +00005478 // Subtract away the biases to deal with the IEEE-754 double precision
5479 // implicit 1.
5480 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005481
Bill Wendling8b8a6362009-01-17 03:56:04 +00005482 // All conversions up to here are exact. The correctly rounded result is
5483 // calculated using the current rounding mode using the following
5484 // horizontal add.
5485 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5486 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5487 // store doesn't really need to be here (except
5488 // maybe to zero the other double)
5489 return sd;
5490 }
5491 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005492
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005493 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005494 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005495
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005496 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005497 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005498 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5499 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5500 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5501 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005502 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005503 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005504
Bill Wendling8b8a6362009-01-17 03:56:04 +00005505 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005506 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005507 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005508 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005509 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005510 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005511 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005512
Owen Anderson825b72b2009-08-11 20:47:22 +00005513 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5514 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005515 Op.getOperand(0),
5516 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005517 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5518 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005519 Op.getOperand(0),
5520 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005521 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5522 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005523 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005524 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005525 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5526 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5527 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005528 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005529 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005530 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005531
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005532 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005533 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005534 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5535 DAG.getUNDEF(MVT::v2f64), ShufMask);
5536 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5537 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005538 DAG.getIntPtrConstant(0));
5539}
5540
Bill Wendling8b8a6362009-01-17 03:56:04 +00005541// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5542SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005543 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005544 // FP constant to bias correct the final result.
5545 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005546 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005547
5548 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5550 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005551 Op.getOperand(0),
5552 DAG.getIntPtrConstant(0)));
5553
Owen Anderson825b72b2009-08-11 20:47:22 +00005554 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5555 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005556 DAG.getIntPtrConstant(0));
5557
5558 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005559 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5560 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005561 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 MVT::v2f64, Load)),
5563 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005564 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005565 MVT::v2f64, Bias)));
5566 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5567 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005568 DAG.getIntPtrConstant(0));
5569
5570 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005571 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005572
5573 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005574 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005575
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005577 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005578 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005579 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005580 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005581 }
5582
5583 // Handle final rounding.
5584 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005585}
5586
5587SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005588 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005589 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005590
Evan Chenga06ec9e2009-01-19 08:08:22 +00005591 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5592 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5593 // the optimization here.
5594 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005595 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005596
Owen Andersone50ed302009-08-10 22:56:29 +00005597 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005599 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005600 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005601 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005602
Bill Wendling8b8a6362009-01-17 03:56:04 +00005603 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005604 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005605 return LowerUINT_TO_FP_i32(Op, DAG);
5606 }
5607
Owen Anderson825b72b2009-08-11 20:47:22 +00005608 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005609
5610 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005611 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005612 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5613 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5614 getPointerTy(), StackSlot, WordOff);
5615 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005616 StackSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005617 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00005618 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005619 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005620}
5621
Dan Gohman475871a2008-07-27 21:46:04 +00005622std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005623FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005624 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005625
Owen Andersone50ed302009-08-10 22:56:29 +00005626 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005627
5628 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005629 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5630 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005631 }
5632
Owen Anderson825b72b2009-08-11 20:47:22 +00005633 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5634 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005635 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005636
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005637 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005638 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005639 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005640 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005641 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005642 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005643 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005644 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005645
Evan Cheng87c89352007-10-15 20:11:21 +00005646 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5647 // stack slot.
5648 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005649 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005650 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005651 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005652
Evan Cheng0db9fe62006-04-25 20:13:52 +00005653 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005654 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005655 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5657 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5658 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005659 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005660
Dan Gohman475871a2008-07-27 21:46:04 +00005661 SDValue Chain = DAG.getEntryNode();
5662 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005663 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005664 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005665 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005666 PseudoSourceValue::getFixedStack(SSFI), 0,
5667 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005668 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005669 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005670 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5671 };
Dale Johannesenace16102009-02-03 19:33:06 +00005672 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005673 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005674 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005675 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5676 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005677
Evan Cheng0db9fe62006-04-25 20:13:52 +00005678 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005679 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005680 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005681
Chris Lattner27a6c732007-11-24 07:07:01 +00005682 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005683}
5684
Dan Gohman475871a2008-07-27 21:46:04 +00005685SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005686 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005687 if (Op.getValueType() == MVT::v2i32 &&
5688 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005689 return Op;
5690 }
5691 return SDValue();
5692 }
5693
Eli Friedman948e95a2009-05-23 09:59:16 +00005694 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005695 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005696 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5697 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005698
Chris Lattner27a6c732007-11-24 07:07:01 +00005699 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005700 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005701 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005702}
5703
Eli Friedman948e95a2009-05-23 09:59:16 +00005704SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5705 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5706 SDValue FIST = Vals.first, StackSlot = Vals.second;
5707 assert(FIST.getNode() && "Unexpected failure");
5708
5709 // Load the result.
5710 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005711 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005712}
5713
Dan Gohman475871a2008-07-27 21:46:04 +00005714SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005715 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005716 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005717 EVT VT = Op.getValueType();
5718 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005719 if (VT.isVector())
5720 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005721 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005723 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005724 CV.push_back(C);
5725 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005727 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005728 CV.push_back(C);
5729 CV.push_back(C);
5730 CV.push_back(C);
5731 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005732 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005733 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005734 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005735 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005736 PseudoSourceValue::getConstantPool(), 0,
5737 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005738 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005739}
5740
Dan Gohman475871a2008-07-27 21:46:04 +00005741SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005742 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005743 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005744 EVT VT = Op.getValueType();
5745 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005746 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005747 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005748 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005749 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005750 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005751 CV.push_back(C);
5752 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005753 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005754 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005755 CV.push_back(C);
5756 CV.push_back(C);
5757 CV.push_back(C);
5758 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005759 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005760 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005761 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005762 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005763 PseudoSourceValue::getConstantPool(), 0,
5764 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005765 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005766 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005767 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5768 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005769 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005770 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005771 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005772 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005773 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005774}
5775
Dan Gohman475871a2008-07-27 21:46:04 +00005776SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005777 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005778 SDValue Op0 = Op.getOperand(0);
5779 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005780 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005781 EVT VT = Op.getValueType();
5782 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005783
5784 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005785 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005786 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005787 SrcVT = VT;
5788 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005789 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005790 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005791 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005792 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005793 }
5794
5795 // At this point the operands and the result should have the same
5796 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005797
Evan Cheng68c47cb2007-01-05 07:55:56 +00005798 // First get the sign bit of second operand.
5799 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005800 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005801 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5802 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005803 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005804 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5805 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5806 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5807 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005808 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005809 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005810 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005811 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005812 PseudoSourceValue::getConstantPool(), 0,
5813 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005814 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005815
5816 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005817 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005818 // Op0 is MVT::f32, Op1 is MVT::f64.
5819 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5820 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5821 DAG.getConstant(32, MVT::i32));
5822 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5823 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005824 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005825 }
5826
Evan Cheng73d6cf12007-01-05 21:37:56 +00005827 // Clear first operand sign bit.
5828 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005829 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005830 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5831 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005832 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005833 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5834 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5835 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5836 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005837 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005838 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005839 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005840 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005841 PseudoSourceValue::getConstantPool(), 0,
5842 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005843 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005844
5845 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005846 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005847}
5848
Dan Gohman076aee32009-03-04 19:44:21 +00005849/// Emit nodes that will be selected as "test Op0,Op0", or something
5850/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005851SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5852 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005853 DebugLoc dl = Op.getDebugLoc();
5854
Dan Gohman31125812009-03-07 01:58:32 +00005855 // CF and OF aren't always set the way we want. Determine which
5856 // of these we need.
5857 bool NeedCF = false;
5858 bool NeedOF = false;
5859 switch (X86CC) {
5860 case X86::COND_A: case X86::COND_AE:
5861 case X86::COND_B: case X86::COND_BE:
5862 NeedCF = true;
5863 break;
5864 case X86::COND_G: case X86::COND_GE:
5865 case X86::COND_L: case X86::COND_LE:
5866 case X86::COND_O: case X86::COND_NO:
5867 NeedOF = true;
5868 break;
5869 default: break;
5870 }
5871
Dan Gohman076aee32009-03-04 19:44:21 +00005872 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005873 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5874 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5875 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005876 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005877 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005878 switch (Op.getNode()->getOpcode()) {
5879 case ISD::ADD:
5880 // Due to an isel shortcoming, be conservative if this add is likely to
5881 // be selected as part of a load-modify-store instruction. When the root
5882 // node in a match is a store, isel doesn't know how to remap non-chain
5883 // non-flag uses of other nodes in the match, such as the ADD in this
5884 // case. This leads to the ADD being left around and reselected, with
5885 // the result being two adds in the output.
5886 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5887 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5888 if (UI->getOpcode() == ISD::STORE)
5889 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005890 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005891 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5892 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005893 if (C->getAPIntValue() == 1) {
5894 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005895 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005896 break;
5897 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005898 // An add of negative one (subtract of one) will be selected as a DEC.
5899 if (C->getAPIntValue().isAllOnesValue()) {
5900 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005901 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005902 break;
5903 }
5904 }
Dan Gohman076aee32009-03-04 19:44:21 +00005905 // Otherwise use a regular EFLAGS-setting add.
5906 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005907 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005908 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005909 case ISD::AND: {
5910 // If the primary and result isn't used, don't bother using X86ISD::AND,
5911 // because a TEST instruction will be better.
5912 bool NonFlagUse = false;
5913 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005914 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5915 SDNode *User = *UI;
5916 unsigned UOpNo = UI.getOperandNo();
5917 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5918 // Look pass truncate.
5919 UOpNo = User->use_begin().getOperandNo();
5920 User = *User->use_begin();
5921 }
5922 if (User->getOpcode() != ISD::BRCOND &&
5923 User->getOpcode() != ISD::SETCC &&
5924 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005925 NonFlagUse = true;
5926 break;
5927 }
Evan Cheng17751da2010-01-07 00:54:06 +00005928 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005929 if (!NonFlagUse)
5930 break;
5931 }
5932 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005933 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005934 case ISD::OR:
5935 case ISD::XOR:
5936 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005937 // likely to be selected as part of a load-modify-store instruction.
5938 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5939 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5940 if (UI->getOpcode() == ISD::STORE)
5941 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005942 // Otherwise use a regular EFLAGS-setting instruction.
5943 switch (Op.getNode()->getOpcode()) {
5944 case ISD::SUB: Opcode = X86ISD::SUB; break;
5945 case ISD::OR: Opcode = X86ISD::OR; break;
5946 case ISD::XOR: Opcode = X86ISD::XOR; break;
5947 case ISD::AND: Opcode = X86ISD::AND; break;
5948 default: llvm_unreachable("unexpected operator!");
5949 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005950 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005951 break;
5952 case X86ISD::ADD:
5953 case X86ISD::SUB:
5954 case X86ISD::INC:
5955 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005956 case X86ISD::OR:
5957 case X86ISD::XOR:
5958 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005959 return SDValue(Op.getNode(), 1);
5960 default:
5961 default_case:
5962 break;
5963 }
5964 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005965 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005966 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005967 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005968 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005969 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005970 DAG.ReplaceAllUsesWith(Op, New);
5971 return SDValue(New.getNode(), 1);
5972 }
5973 }
5974
5975 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005976 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005977 DAG.getConstant(0, Op.getValueType()));
5978}
5979
5980/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5981/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005982SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5983 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005984 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5985 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005986 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005987
5988 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005989 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005990}
5991
Evan Chengd40d03e2010-01-06 19:38:29 +00005992/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5993/// if it's possible.
Evan Cheng2c755ba2010-02-27 07:36:59 +00005994static SDValue LowerToBT(SDValue And, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005995 DebugLoc dl, SelectionDAG &DAG) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00005996 SDValue Op0 = And.getOperand(0);
5997 SDValue Op1 = And.getOperand(1);
5998 if (Op0.getOpcode() == ISD::TRUNCATE)
5999 Op0 = Op0.getOperand(0);
6000 if (Op1.getOpcode() == ISD::TRUNCATE)
6001 Op1 = Op1.getOperand(0);
6002
Evan Chengd40d03e2010-01-06 19:38:29 +00006003 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00006004 if (Op1.getOpcode() == ISD::SHL) {
6005 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6006 if (And10C->getZExtValue() == 1) {
6007 LHS = Op0;
6008 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006009 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006010 } else if (Op0.getOpcode() == ISD::SHL) {
6011 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6012 if (And00C->getZExtValue() == 1) {
6013 LHS = Op1;
6014 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006015 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006016 } else if (Op1.getOpcode() == ISD::Constant) {
6017 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6018 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006019 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6020 LHS = AndLHS.getOperand(0);
6021 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006022 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006023 }
Evan Cheng0488db92007-09-25 01:57:46 +00006024
Evan Chengd40d03e2010-01-06 19:38:29 +00006025 if (LHS.getNode()) {
6026 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
6027 // instruction. Since the shift amount is in-range-or-undefined, we know
6028 // that doing a bittest on the i16 value is ok. We extend to i32 because
6029 // the encoding for the i16 version is larger than the i32 version.
6030 if (LHS.getValueType() == MVT::i8)
6031 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006032
Evan Chengd40d03e2010-01-06 19:38:29 +00006033 // If the operand types disagree, extend the shift amount to match. Since
6034 // BT ignores high bits (like shifts) we can use anyextend.
6035 if (LHS.getValueType() != RHS.getValueType())
6036 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006037
Evan Chengd40d03e2010-01-06 19:38:29 +00006038 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6039 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6040 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6041 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006042 }
6043
Evan Cheng54de3ea2010-01-05 06:52:31 +00006044 return SDValue();
6045}
6046
6047SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
6048 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6049 SDValue Op0 = Op.getOperand(0);
6050 SDValue Op1 = Op.getOperand(1);
6051 DebugLoc dl = Op.getDebugLoc();
6052 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6053
6054 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006055 // Lower (X & (1 << N)) == 0 to BT(X, N).
6056 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6057 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6058 if (Op0.getOpcode() == ISD::AND &&
6059 Op0.hasOneUse() &&
6060 Op1.getOpcode() == ISD::Constant &&
6061 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6062 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6063 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6064 if (NewSetCC.getNode())
6065 return NewSetCC;
6066 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006067
Evan Cheng2c755ba2010-02-27 07:36:59 +00006068 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6069 if (Op0.getOpcode() == X86ISD::SETCC &&
6070 Op1.getOpcode() == ISD::Constant &&
6071 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6072 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6073 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6074 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6075 bool Invert = (CC == ISD::SETNE) ^
6076 cast<ConstantSDNode>(Op1)->isNullValue();
6077 if (Invert)
6078 CCode = X86::GetOppositeBranchCondition(CCode);
6079 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6080 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6081 }
6082
Chris Lattnere55484e2008-12-25 05:34:37 +00006083 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
6084 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006085 if (X86CC == X86::COND_INVALID)
6086 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006087
Dan Gohman31125812009-03-07 01:58:32 +00006088 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006089
6090 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006091 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006092 return DAG.getNode(ISD::AND, dl, MVT::i8,
6093 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6094 DAG.getConstant(X86CC, MVT::i8), Cond),
6095 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006096
Owen Anderson825b72b2009-08-11 20:47:22 +00006097 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6098 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006099}
6100
Dan Gohman475871a2008-07-27 21:46:04 +00006101SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
6102 SDValue Cond;
6103 SDValue Op0 = Op.getOperand(0);
6104 SDValue Op1 = Op.getOperand(1);
6105 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006106 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006107 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6108 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006109 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006110
6111 if (isFP) {
6112 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006113 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006114 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6115 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006116 bool Swap = false;
6117
6118 switch (SetCCOpcode) {
6119 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006120 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006121 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006122 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006123 case ISD::SETGT: Swap = true; // Fallthrough
6124 case ISD::SETLT:
6125 case ISD::SETOLT: SSECC = 1; break;
6126 case ISD::SETOGE:
6127 case ISD::SETGE: Swap = true; // Fallthrough
6128 case ISD::SETLE:
6129 case ISD::SETOLE: SSECC = 2; break;
6130 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006131 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006132 case ISD::SETNE: SSECC = 4; break;
6133 case ISD::SETULE: Swap = true;
6134 case ISD::SETUGE: SSECC = 5; break;
6135 case ISD::SETULT: Swap = true;
6136 case ISD::SETUGT: SSECC = 6; break;
6137 case ISD::SETO: SSECC = 7; break;
6138 }
6139 if (Swap)
6140 std::swap(Op0, Op1);
6141
Nate Begemanfb8ead02008-07-25 19:05:58 +00006142 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006143 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006144 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006145 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006146 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6147 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006148 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006149 }
6150 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006151 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006152 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6153 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006154 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006155 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006156 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006157 }
6158 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006159 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006160 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006161
Nate Begeman30a0de92008-07-17 16:51:19 +00006162 // We are handling one of the integer comparisons here. Since SSE only has
6163 // GT and EQ comparisons for integer, swapping operands and multiple
6164 // operations may be required for some comparisons.
6165 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6166 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006167
Owen Anderson825b72b2009-08-11 20:47:22 +00006168 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006169 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006170 case MVT::v8i8:
6171 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6172 case MVT::v4i16:
6173 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6174 case MVT::v2i32:
6175 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6176 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006177 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006178
Nate Begeman30a0de92008-07-17 16:51:19 +00006179 switch (SetCCOpcode) {
6180 default: break;
6181 case ISD::SETNE: Invert = true;
6182 case ISD::SETEQ: Opc = EQOpc; break;
6183 case ISD::SETLT: Swap = true;
6184 case ISD::SETGT: Opc = GTOpc; break;
6185 case ISD::SETGE: Swap = true;
6186 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6187 case ISD::SETULT: Swap = true;
6188 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6189 case ISD::SETUGE: Swap = true;
6190 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6191 }
6192 if (Swap)
6193 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006194
Nate Begeman30a0de92008-07-17 16:51:19 +00006195 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6196 // bits of the inputs before performing those operations.
6197 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006198 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006199 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6200 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006201 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006202 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6203 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006204 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6205 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006206 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006207
Dale Johannesenace16102009-02-03 19:33:06 +00006208 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006209
6210 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006211 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006212 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006213
Nate Begeman30a0de92008-07-17 16:51:19 +00006214 return Result;
6215}
Evan Cheng0488db92007-09-25 01:57:46 +00006216
Evan Cheng370e5342008-12-03 08:38:43 +00006217// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006218static bool isX86LogicalCmp(SDValue Op) {
6219 unsigned Opc = Op.getNode()->getOpcode();
6220 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6221 return true;
6222 if (Op.getResNo() == 1 &&
6223 (Opc == X86ISD::ADD ||
6224 Opc == X86ISD::SUB ||
6225 Opc == X86ISD::SMUL ||
6226 Opc == X86ISD::UMUL ||
6227 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006228 Opc == X86ISD::DEC ||
6229 Opc == X86ISD::OR ||
6230 Opc == X86ISD::XOR ||
6231 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006232 return true;
6233
6234 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006235}
6236
Dan Gohman475871a2008-07-27 21:46:04 +00006237SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006238 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006239 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006240 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006241 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006242
Dan Gohman1a492952009-10-20 16:22:37 +00006243 if (Cond.getOpcode() == ISD::SETCC) {
6244 SDValue NewCond = LowerSETCC(Cond, DAG);
6245 if (NewCond.getNode())
6246 Cond = NewCond;
6247 }
Evan Cheng734503b2006-09-11 02:19:56 +00006248
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006249 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6250 SDValue Op1 = Op.getOperand(1);
6251 SDValue Op2 = Op.getOperand(2);
6252 if (Cond.getOpcode() == X86ISD::SETCC &&
6253 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6254 SDValue Cmp = Cond.getOperand(1);
6255 if (Cmp.getOpcode() == X86ISD::CMP) {
6256 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6257 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6258 ConstantSDNode *RHSC =
6259 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6260 if (N1C && N1C->isAllOnesValue() &&
6261 N2C && N2C->isNullValue() &&
6262 RHSC && RHSC->isNullValue()) {
6263 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006264 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006265 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6266 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6267 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6268 }
6269 }
6270 }
6271
Evan Chengad9c0a32009-12-15 00:53:42 +00006272 // Look pass (and (setcc_carry (cmp ...)), 1).
6273 if (Cond.getOpcode() == ISD::AND &&
6274 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6275 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6276 if (C && C->getAPIntValue() == 1)
6277 Cond = Cond.getOperand(0);
6278 }
6279
Evan Cheng3f41d662007-10-08 22:16:29 +00006280 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6281 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006282 if (Cond.getOpcode() == X86ISD::SETCC ||
6283 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006284 CC = Cond.getOperand(0);
6285
Dan Gohman475871a2008-07-27 21:46:04 +00006286 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006287 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006288 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006289
Evan Cheng3f41d662007-10-08 22:16:29 +00006290 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006291 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006292 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006293 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006294
Chris Lattnerd1980a52009-03-12 06:52:53 +00006295 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6296 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006297 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006298 addTest = false;
6299 }
6300 }
6301
6302 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006303 // Look pass the truncate.
6304 if (Cond.getOpcode() == ISD::TRUNCATE)
6305 Cond = Cond.getOperand(0);
6306
6307 // We know the result of AND is compared against zero. Try to match
6308 // it to BT.
6309 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6310 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6311 if (NewSetCC.getNode()) {
6312 CC = NewSetCC.getOperand(0);
6313 Cond = NewSetCC.getOperand(1);
6314 addTest = false;
6315 }
6316 }
6317 }
6318
6319 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006320 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006321 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006322 }
6323
Evan Cheng0488db92007-09-25 01:57:46 +00006324 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6325 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006326 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6327 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006328 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006329}
6330
Evan Cheng370e5342008-12-03 08:38:43 +00006331// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6332// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6333// from the AND / OR.
6334static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6335 Opc = Op.getOpcode();
6336 if (Opc != ISD::OR && Opc != ISD::AND)
6337 return false;
6338 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6339 Op.getOperand(0).hasOneUse() &&
6340 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6341 Op.getOperand(1).hasOneUse());
6342}
6343
Evan Cheng961d6d42009-02-02 08:19:07 +00006344// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6345// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006346static bool isXor1OfSetCC(SDValue Op) {
6347 if (Op.getOpcode() != ISD::XOR)
6348 return false;
6349 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6350 if (N1C && N1C->getAPIntValue() == 1) {
6351 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6352 Op.getOperand(0).hasOneUse();
6353 }
6354 return false;
6355}
6356
Dan Gohman475871a2008-07-27 21:46:04 +00006357SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006358 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006359 SDValue Chain = Op.getOperand(0);
6360 SDValue Cond = Op.getOperand(1);
6361 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006362 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006363 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006364
Dan Gohman1a492952009-10-20 16:22:37 +00006365 if (Cond.getOpcode() == ISD::SETCC) {
6366 SDValue NewCond = LowerSETCC(Cond, DAG);
6367 if (NewCond.getNode())
6368 Cond = NewCond;
6369 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006370#if 0
6371 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006372 else if (Cond.getOpcode() == X86ISD::ADD ||
6373 Cond.getOpcode() == X86ISD::SUB ||
6374 Cond.getOpcode() == X86ISD::SMUL ||
6375 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006376 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006377#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006378
Evan Chengad9c0a32009-12-15 00:53:42 +00006379 // Look pass (and (setcc_carry (cmp ...)), 1).
6380 if (Cond.getOpcode() == ISD::AND &&
6381 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6382 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6383 if (C && C->getAPIntValue() == 1)
6384 Cond = Cond.getOperand(0);
6385 }
6386
Evan Cheng3f41d662007-10-08 22:16:29 +00006387 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6388 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006389 if (Cond.getOpcode() == X86ISD::SETCC ||
6390 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006391 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006392
Dan Gohman475871a2008-07-27 21:46:04 +00006393 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006394 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006395 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006396 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006397 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006398 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006399 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006400 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006401 default: break;
6402 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006403 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006404 // These can only come from an arithmetic instruction with overflow,
6405 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006406 Cond = Cond.getNode()->getOperand(1);
6407 addTest = false;
6408 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006409 }
Evan Cheng0488db92007-09-25 01:57:46 +00006410 }
Evan Cheng370e5342008-12-03 08:38:43 +00006411 } else {
6412 unsigned CondOpc;
6413 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6414 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006415 if (CondOpc == ISD::OR) {
6416 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6417 // two branches instead of an explicit OR instruction with a
6418 // separate test.
6419 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006420 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006421 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006422 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006423 Chain, Dest, CC, Cmp);
6424 CC = Cond.getOperand(1).getOperand(0);
6425 Cond = Cmp;
6426 addTest = false;
6427 }
6428 } else { // ISD::AND
6429 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6430 // two branches instead of an explicit AND instruction with a
6431 // separate test. However, we only do this if this block doesn't
6432 // have a fall-through edge, because this requires an explicit
6433 // jmp when the condition is false.
6434 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006435 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006436 Op.getNode()->hasOneUse()) {
6437 X86::CondCode CCode =
6438 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6439 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006440 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006441 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6442 // Look for an unconditional branch following this conditional branch.
6443 // We need this because we need to reverse the successors in order
6444 // to implement FCMP_OEQ.
6445 if (User.getOpcode() == ISD::BR) {
6446 SDValue FalseBB = User.getOperand(1);
6447 SDValue NewBR =
6448 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6449 assert(NewBR == User);
6450 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006451
Dale Johannesene4d209d2009-02-03 20:21:25 +00006452 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006453 Chain, Dest, CC, Cmp);
6454 X86::CondCode CCode =
6455 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6456 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006457 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006458 Cond = Cmp;
6459 addTest = false;
6460 }
6461 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006462 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006463 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6464 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6465 // It should be transformed during dag combiner except when the condition
6466 // is set by a arithmetics with overflow node.
6467 X86::CondCode CCode =
6468 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6469 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006470 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006471 Cond = Cond.getOperand(0).getOperand(1);
6472 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006473 }
Evan Cheng0488db92007-09-25 01:57:46 +00006474 }
6475
6476 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006477 // Look pass the truncate.
6478 if (Cond.getOpcode() == ISD::TRUNCATE)
6479 Cond = Cond.getOperand(0);
6480
6481 // We know the result of AND is compared against zero. Try to match
6482 // it to BT.
6483 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6484 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6485 if (NewSetCC.getNode()) {
6486 CC = NewSetCC.getOperand(0);
6487 Cond = NewSetCC.getOperand(1);
6488 addTest = false;
6489 }
6490 }
6491 }
6492
6493 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006494 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006495 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006496 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006497 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006498 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006499}
6500
Anton Korobeynikove060b532007-04-17 19:34:00 +00006501
6502// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6503// Calls to _alloca is needed to probe the stack when allocating more than 4k
6504// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6505// that the guard pages used by the OS virtual memory manager are allocated in
6506// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006507SDValue
6508X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006509 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006510 assert(Subtarget->isTargetCygMing() &&
6511 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006512 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006513
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006514 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006515 SDValue Chain = Op.getOperand(0);
6516 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006517 // FIXME: Ensure alignment here
6518
Dan Gohman475871a2008-07-27 21:46:04 +00006519 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006520
Owen Andersone50ed302009-08-10 22:56:29 +00006521 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006522 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006523
Dale Johannesendd64c412009-02-04 00:33:20 +00006524 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006525 Flag = Chain.getValue(1);
6526
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006527 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006528
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006529 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6530 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006531
Dale Johannesendd64c412009-02-04 00:33:20 +00006532 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006533
Dan Gohman475871a2008-07-27 21:46:04 +00006534 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006535 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006536}
6537
Dan Gohman475871a2008-07-27 21:46:04 +00006538SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006539X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006540 SDValue Chain,
6541 SDValue Dst, SDValue Src,
6542 SDValue Size, unsigned Align,
Mon P Wang808bab02010-03-30 20:55:56 +00006543 bool isVolatile,
Bill Wendling6f287b22008-09-30 21:22:07 +00006544 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006545 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006546 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006547
Bill Wendling6f287b22008-09-30 21:22:07 +00006548 // If not DWORD aligned or size is more than the threshold, call the library.
6549 // The libc version is likely to be faster for these cases. It can use the
6550 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006551 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006552 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006553 ConstantSize->getZExtValue() >
6554 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006555 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006556
6557 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006558 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006559
Bill Wendling6158d842008-10-01 00:59:58 +00006560 if (const char *bzeroEntry = V &&
6561 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006562 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006563 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006564 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006565 TargetLowering::ArgListEntry Entry;
6566 Entry.Node = Dst;
6567 Entry.Ty = IntPtrTy;
6568 Args.push_back(Entry);
6569 Entry.Node = Size;
6570 Args.push_back(Entry);
6571 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006572 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6573 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006574 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling46ada192010-03-02 01:55:18 +00006575 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00006576 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006577 }
6578
Dan Gohman707e0182008-04-12 04:36:06 +00006579 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006580 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006581 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006582
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006583 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006584 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006585 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006586 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006587 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006588 unsigned BytesLeft = 0;
6589 bool TwoRepStos = false;
6590 if (ValC) {
6591 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006592 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006593
Evan Cheng0db9fe62006-04-25 20:13:52 +00006594 // If the value is a constant, then we can potentially use larger sets.
6595 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006596 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006597 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006598 ValReg = X86::AX;
6599 Val = (Val << 8) | Val;
6600 break;
6601 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006602 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006603 ValReg = X86::EAX;
6604 Val = (Val << 8) | Val;
6605 Val = (Val << 16) | Val;
6606 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006607 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006608 ValReg = X86::RAX;
6609 Val = (Val << 32) | Val;
6610 }
6611 break;
6612 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006613 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006614 ValReg = X86::AL;
6615 Count = DAG.getIntPtrConstant(SizeVal);
6616 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006617 }
6618
Owen Anderson825b72b2009-08-11 20:47:22 +00006619 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006620 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006621 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6622 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006623 }
6624
Dale Johannesen0f502f62009-02-03 22:26:09 +00006625 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006626 InFlag);
6627 InFlag = Chain.getValue(1);
6628 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006629 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006630 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006631 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006632 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006633 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006634
Scott Michelfdc40a02009-02-17 22:15:04 +00006635 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006636 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006637 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006638 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006639 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006640 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006641 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006642 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006643
Owen Anderson825b72b2009-08-11 20:47:22 +00006644 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006645 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6646 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006647
Evan Cheng0db9fe62006-04-25 20:13:52 +00006648 if (TwoRepStos) {
6649 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006650 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006651 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006652 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006653 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6654 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006655 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006656 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006657 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006658 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006659 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6660 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006661 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006662 // Handle the last 1 - 7 bytes.
6663 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006664 EVT AddrVT = Dst.getValueType();
6665 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006666
Dale Johannesen0f502f62009-02-03 22:26:09 +00006667 Chain = DAG.getMemset(Chain, dl,
6668 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006669 DAG.getConstant(Offset, AddrVT)),
6670 Src,
6671 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang808bab02010-03-30 20:55:56 +00006672 Align, isVolatile, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006673 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006674
Dan Gohman707e0182008-04-12 04:36:06 +00006675 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006676 return Chain;
6677}
Evan Cheng11e15b32006-04-03 20:53:28 +00006678
Dan Gohman475871a2008-07-27 21:46:04 +00006679SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006680X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006681 SDValue Chain, SDValue Dst, SDValue Src,
6682 SDValue Size, unsigned Align,
Mon P Wang808bab02010-03-30 20:55:56 +00006683 bool isVolatile, bool AlwaysInline,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006684 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006685 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006686 // This requires the copy size to be a constant, preferrably
6687 // within a subtarget-specific limit.
6688 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6689 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006690 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006691 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006692 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006693 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006694
Evan Cheng1887c1c2008-08-21 21:00:15 +00006695 /// If not DWORD aligned, call the library.
6696 if ((Align & 3) != 0)
6697 return SDValue();
6698
6699 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006700 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006701 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006702 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006703
Duncan Sands83ec4b62008-06-06 12:08:01 +00006704 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006705 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006706 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006707 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006708
Dan Gohman475871a2008-07-27 21:46:04 +00006709 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006710 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006711 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006712 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006713 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006714 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006715 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006716 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006717 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006718 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006719 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006720 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006721 InFlag = Chain.getValue(1);
6722
Owen Anderson825b72b2009-08-11 20:47:22 +00006723 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006724 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6725 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6726 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006727
Dan Gohman475871a2008-07-27 21:46:04 +00006728 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006729 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006730 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006731 // Handle the last 1 - 7 bytes.
6732 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006733 EVT DstVT = Dst.getValueType();
6734 EVT SrcVT = Src.getValueType();
6735 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006736 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006737 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006738 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006739 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006740 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006741 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang808bab02010-03-30 20:55:56 +00006742 Align, isVolatile, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006743 DstSV, DstSVOff + Offset,
6744 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006745 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006746
Owen Anderson825b72b2009-08-11 20:47:22 +00006747 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006748 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006749}
6750
Dan Gohman475871a2008-07-27 21:46:04 +00006751SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006752 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006753 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006754
Evan Cheng25ab6902006-09-08 06:48:29 +00006755 if (!Subtarget->is64Bit()) {
6756 // vastart just stores the address of the VarArgsFrameIndex slot into the
6757 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006758 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006759 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6760 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006761 }
6762
6763 // __va_list_tag:
6764 // gp_offset (0 - 6 * 8)
6765 // fp_offset (48 - 48 + 8 * 16)
6766 // overflow_arg_area (point to parameters coming in memory).
6767 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006768 SmallVector<SDValue, 8> MemOps;
6769 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006770 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006771 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
David Greene67c9d422010-02-15 16:53:33 +00006772 DAG.getConstant(VarArgsGPOffset, MVT::i32),
6773 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006774 MemOps.push_back(Store);
6775
6776 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006777 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006778 FIN, DAG.getIntPtrConstant(4));
6779 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006780 DAG.getConstant(VarArgsFPOffset, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006781 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006782 MemOps.push_back(Store);
6783
6784 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006785 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006786 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006787 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006788 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6789 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006790 MemOps.push_back(Store);
6791
6792 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006793 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006794 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006795 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006796 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6797 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006798 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006799 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006800 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801}
6802
Dan Gohman475871a2008-07-27 21:46:04 +00006803SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006804 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6805 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006806 SDValue Chain = Op.getOperand(0);
6807 SDValue SrcPtr = Op.getOperand(1);
6808 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006809
Torok Edwindac237e2009-07-08 20:53:28 +00006810 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006811 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006812}
6813
Dan Gohman475871a2008-07-27 21:46:04 +00006814SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006815 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006816 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006817 SDValue Chain = Op.getOperand(0);
6818 SDValue DstPtr = Op.getOperand(1);
6819 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006820 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6821 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006822 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006823
Dale Johannesendd64c412009-02-04 00:33:20 +00006824 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang808bab02010-03-30 20:55:56 +00006825 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6826 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006827}
6828
Dan Gohman475871a2008-07-27 21:46:04 +00006829SDValue
6830X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006831 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006832 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006833 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006834 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006835 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006836 case Intrinsic::x86_sse_comieq_ss:
6837 case Intrinsic::x86_sse_comilt_ss:
6838 case Intrinsic::x86_sse_comile_ss:
6839 case Intrinsic::x86_sse_comigt_ss:
6840 case Intrinsic::x86_sse_comige_ss:
6841 case Intrinsic::x86_sse_comineq_ss:
6842 case Intrinsic::x86_sse_ucomieq_ss:
6843 case Intrinsic::x86_sse_ucomilt_ss:
6844 case Intrinsic::x86_sse_ucomile_ss:
6845 case Intrinsic::x86_sse_ucomigt_ss:
6846 case Intrinsic::x86_sse_ucomige_ss:
6847 case Intrinsic::x86_sse_ucomineq_ss:
6848 case Intrinsic::x86_sse2_comieq_sd:
6849 case Intrinsic::x86_sse2_comilt_sd:
6850 case Intrinsic::x86_sse2_comile_sd:
6851 case Intrinsic::x86_sse2_comigt_sd:
6852 case Intrinsic::x86_sse2_comige_sd:
6853 case Intrinsic::x86_sse2_comineq_sd:
6854 case Intrinsic::x86_sse2_ucomieq_sd:
6855 case Intrinsic::x86_sse2_ucomilt_sd:
6856 case Intrinsic::x86_sse2_ucomile_sd:
6857 case Intrinsic::x86_sse2_ucomigt_sd:
6858 case Intrinsic::x86_sse2_ucomige_sd:
6859 case Intrinsic::x86_sse2_ucomineq_sd: {
6860 unsigned Opc = 0;
6861 ISD::CondCode CC = ISD::SETCC_INVALID;
6862 switch (IntNo) {
6863 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006864 case Intrinsic::x86_sse_comieq_ss:
6865 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006866 Opc = X86ISD::COMI;
6867 CC = ISD::SETEQ;
6868 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006869 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006870 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006871 Opc = X86ISD::COMI;
6872 CC = ISD::SETLT;
6873 break;
6874 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006875 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006876 Opc = X86ISD::COMI;
6877 CC = ISD::SETLE;
6878 break;
6879 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006880 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006881 Opc = X86ISD::COMI;
6882 CC = ISD::SETGT;
6883 break;
6884 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006885 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006886 Opc = X86ISD::COMI;
6887 CC = ISD::SETGE;
6888 break;
6889 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006890 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006891 Opc = X86ISD::COMI;
6892 CC = ISD::SETNE;
6893 break;
6894 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006895 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006896 Opc = X86ISD::UCOMI;
6897 CC = ISD::SETEQ;
6898 break;
6899 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006900 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006901 Opc = X86ISD::UCOMI;
6902 CC = ISD::SETLT;
6903 break;
6904 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006905 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006906 Opc = X86ISD::UCOMI;
6907 CC = ISD::SETLE;
6908 break;
6909 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006910 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006911 Opc = X86ISD::UCOMI;
6912 CC = ISD::SETGT;
6913 break;
6914 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006915 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006916 Opc = X86ISD::UCOMI;
6917 CC = ISD::SETGE;
6918 break;
6919 case Intrinsic::x86_sse_ucomineq_ss:
6920 case Intrinsic::x86_sse2_ucomineq_sd:
6921 Opc = X86ISD::UCOMI;
6922 CC = ISD::SETNE;
6923 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006924 }
Evan Cheng734503b2006-09-11 02:19:56 +00006925
Dan Gohman475871a2008-07-27 21:46:04 +00006926 SDValue LHS = Op.getOperand(1);
6927 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006928 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006929 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006930 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6931 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6932 DAG.getConstant(X86CC, MVT::i8), Cond);
6933 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006934 }
Eric Christopher71c67532009-07-29 00:28:05 +00006935 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006936 // an integer value, not just an instruction so lower it to the ptest
6937 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006938 case Intrinsic::x86_sse41_ptestz:
6939 case Intrinsic::x86_sse41_ptestc:
6940 case Intrinsic::x86_sse41_ptestnzc:{
6941 unsigned X86CC = 0;
6942 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006943 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006944 case Intrinsic::x86_sse41_ptestz:
6945 // ZF = 1
6946 X86CC = X86::COND_E;
6947 break;
6948 case Intrinsic::x86_sse41_ptestc:
6949 // CF = 1
6950 X86CC = X86::COND_B;
6951 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006952 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006953 // ZF and CF = 0
6954 X86CC = X86::COND_A;
6955 break;
6956 }
Eric Christopherfd179292009-08-27 18:07:15 +00006957
Eric Christopher71c67532009-07-29 00:28:05 +00006958 SDValue LHS = Op.getOperand(1);
6959 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006960 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6961 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6962 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6963 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006964 }
Evan Cheng5759f972008-05-04 09:15:50 +00006965
6966 // Fix vector shift instructions where the last operand is a non-immediate
6967 // i32 value.
6968 case Intrinsic::x86_sse2_pslli_w:
6969 case Intrinsic::x86_sse2_pslli_d:
6970 case Intrinsic::x86_sse2_pslli_q:
6971 case Intrinsic::x86_sse2_psrli_w:
6972 case Intrinsic::x86_sse2_psrli_d:
6973 case Intrinsic::x86_sse2_psrli_q:
6974 case Intrinsic::x86_sse2_psrai_w:
6975 case Intrinsic::x86_sse2_psrai_d:
6976 case Intrinsic::x86_mmx_pslli_w:
6977 case Intrinsic::x86_mmx_pslli_d:
6978 case Intrinsic::x86_mmx_pslli_q:
6979 case Intrinsic::x86_mmx_psrli_w:
6980 case Intrinsic::x86_mmx_psrli_d:
6981 case Intrinsic::x86_mmx_psrli_q:
6982 case Intrinsic::x86_mmx_psrai_w:
6983 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006984 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006985 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006986 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006987
6988 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006989 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006990 switch (IntNo) {
6991 case Intrinsic::x86_sse2_pslli_w:
6992 NewIntNo = Intrinsic::x86_sse2_psll_w;
6993 break;
6994 case Intrinsic::x86_sse2_pslli_d:
6995 NewIntNo = Intrinsic::x86_sse2_psll_d;
6996 break;
6997 case Intrinsic::x86_sse2_pslli_q:
6998 NewIntNo = Intrinsic::x86_sse2_psll_q;
6999 break;
7000 case Intrinsic::x86_sse2_psrli_w:
7001 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7002 break;
7003 case Intrinsic::x86_sse2_psrli_d:
7004 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7005 break;
7006 case Intrinsic::x86_sse2_psrli_q:
7007 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7008 break;
7009 case Intrinsic::x86_sse2_psrai_w:
7010 NewIntNo = Intrinsic::x86_sse2_psra_w;
7011 break;
7012 case Intrinsic::x86_sse2_psrai_d:
7013 NewIntNo = Intrinsic::x86_sse2_psra_d;
7014 break;
7015 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007016 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007017 switch (IntNo) {
7018 case Intrinsic::x86_mmx_pslli_w:
7019 NewIntNo = Intrinsic::x86_mmx_psll_w;
7020 break;
7021 case Intrinsic::x86_mmx_pslli_d:
7022 NewIntNo = Intrinsic::x86_mmx_psll_d;
7023 break;
7024 case Intrinsic::x86_mmx_pslli_q:
7025 NewIntNo = Intrinsic::x86_mmx_psll_q;
7026 break;
7027 case Intrinsic::x86_mmx_psrli_w:
7028 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7029 break;
7030 case Intrinsic::x86_mmx_psrli_d:
7031 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7032 break;
7033 case Intrinsic::x86_mmx_psrli_q:
7034 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7035 break;
7036 case Intrinsic::x86_mmx_psrai_w:
7037 NewIntNo = Intrinsic::x86_mmx_psra_w;
7038 break;
7039 case Intrinsic::x86_mmx_psrai_d:
7040 NewIntNo = Intrinsic::x86_mmx_psra_d;
7041 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007042 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007043 }
7044 break;
7045 }
7046 }
Mon P Wangefa42202009-09-03 19:56:25 +00007047
7048 // The vector shift intrinsics with scalars uses 32b shift amounts but
7049 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7050 // to be zero.
7051 SDValue ShOps[4];
7052 ShOps[0] = ShAmt;
7053 ShOps[1] = DAG.getConstant(0, MVT::i32);
7054 if (ShAmtVT == MVT::v4i32) {
7055 ShOps[2] = DAG.getUNDEF(MVT::i32);
7056 ShOps[3] = DAG.getUNDEF(MVT::i32);
7057 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7058 } else {
7059 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7060 }
7061
Owen Andersone50ed302009-08-10 22:56:29 +00007062 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007063 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007064 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007065 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007066 Op.getOperand(1), ShAmt);
7067 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007068 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007069}
Evan Cheng72261582005-12-20 06:22:03 +00007070
Dan Gohman475871a2008-07-27 21:46:04 +00007071SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00007072 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007073 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007074
7075 if (Depth > 0) {
7076 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7077 SDValue Offset =
7078 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007080 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007081 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007082 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007083 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007084 }
7085
7086 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007087 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007088 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007089 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007090}
7091
Dan Gohman475871a2008-07-27 21:46:04 +00007092SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00007093 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7094 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00007095 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007096 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007097 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7098 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007099 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007100 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007101 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7102 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007103 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007104}
7105
Dan Gohman475871a2008-07-27 21:46:04 +00007106SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00007107 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007108 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007109}
7110
Dan Gohman475871a2008-07-27 21:46:04 +00007111SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007112{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007113 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007114 SDValue Chain = Op.getOperand(0);
7115 SDValue Offset = Op.getOperand(1);
7116 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007117 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007118
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007119 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7120 getPointerTy());
7121 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007122
Dale Johannesene4d209d2009-02-03 20:21:25 +00007123 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007124 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007125 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007126 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007127 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007128 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007129
Dale Johannesene4d209d2009-02-03 20:21:25 +00007130 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007131 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007132 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007133}
7134
Dan Gohman475871a2008-07-27 21:46:04 +00007135SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00007136 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00007137 SDValue Root = Op.getOperand(0);
7138 SDValue Trmp = Op.getOperand(1); // trampoline
7139 SDValue FPtr = Op.getOperand(2); // nested function
7140 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007141 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007142
Dan Gohman69de1932008-02-06 22:27:42 +00007143 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007144
7145 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007146 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007147
7148 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007149 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7150 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007151
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007152 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7153 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007154
7155 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7156
7157 // Load the pointer to the nested function into R11.
7158 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007159 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007160 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007161 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007162
Owen Anderson825b72b2009-08-11 20:47:22 +00007163 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7164 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007165 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7166 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007167
7168 // Load the 'nest' parameter value into R10.
7169 // R10 is specified in X86CallingConv.td
7170 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007171 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7172 DAG.getConstant(10, MVT::i64));
7173 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007174 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007175
Owen Anderson825b72b2009-08-11 20:47:22 +00007176 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7177 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007178 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7179 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007180
7181 // Jump to the nested function.
7182 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007183 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7184 DAG.getConstant(20, MVT::i64));
7185 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007186 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007187
7188 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007189 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7190 DAG.getConstant(22, MVT::i64));
7191 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007192 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007193
Dan Gohman475871a2008-07-27 21:46:04 +00007194 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007195 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007196 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007197 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007198 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007199 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007200 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007201 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007202
7203 switch (CC) {
7204 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007205 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007206 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007207 case CallingConv::X86_StdCall: {
7208 // Pass 'nest' parameter in ECX.
7209 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007210 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007211
7212 // Check that ECX wasn't needed by an 'inreg' parameter.
7213 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007214 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007215
Chris Lattner58d74912008-03-12 17:45:29 +00007216 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007217 unsigned InRegCount = 0;
7218 unsigned Idx = 1;
7219
7220 for (FunctionType::param_iterator I = FTy->param_begin(),
7221 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007222 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007223 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007224 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007225
7226 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00007227 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007228 }
7229 }
7230 break;
7231 }
7232 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007233 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007234 // Pass 'nest' parameter in EAX.
7235 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007236 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007237 break;
7238 }
7239
Dan Gohman475871a2008-07-27 21:46:04 +00007240 SDValue OutChains[4];
7241 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007242
Owen Anderson825b72b2009-08-11 20:47:22 +00007243 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7244 DAG.getConstant(10, MVT::i32));
7245 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007246
Chris Lattnera62fe662010-02-05 19:20:30 +00007247 // This is storing the opcode for MOV32ri.
7248 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007249 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007250 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007251 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007252 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007253
Owen Anderson825b72b2009-08-11 20:47:22 +00007254 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7255 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007256 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7257 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007258
Chris Lattnera62fe662010-02-05 19:20:30 +00007259 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007260 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7261 DAG.getConstant(5, MVT::i32));
7262 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007263 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007264
Owen Anderson825b72b2009-08-11 20:47:22 +00007265 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7266 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007267 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7268 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007269
Dan Gohman475871a2008-07-27 21:46:04 +00007270 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007271 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007272 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007273 }
7274}
7275
Dan Gohman475871a2008-07-27 21:46:04 +00007276SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007277 /*
7278 The rounding mode is in bits 11:10 of FPSR, and has the following
7279 settings:
7280 00 Round to nearest
7281 01 Round to -inf
7282 10 Round to +inf
7283 11 Round to 0
7284
7285 FLT_ROUNDS, on the other hand, expects the following:
7286 -1 Undefined
7287 0 Round to 0
7288 1 Round to nearest
7289 2 Round to +inf
7290 3 Round to -inf
7291
7292 To perform the conversion, we do:
7293 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7294 */
7295
7296 MachineFunction &MF = DAG.getMachineFunction();
7297 const TargetMachine &TM = MF.getTarget();
7298 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7299 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007300 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007301 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007302
7303 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007304 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007305 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007306
Owen Anderson825b72b2009-08-11 20:47:22 +00007307 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007308 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007309
7310 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007311 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7312 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007313
7314 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007315 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007316 DAG.getNode(ISD::SRL, dl, MVT::i16,
7317 DAG.getNode(ISD::AND, dl, MVT::i16,
7318 CWD, DAG.getConstant(0x800, MVT::i16)),
7319 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007320 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007321 DAG.getNode(ISD::SRL, dl, MVT::i16,
7322 DAG.getNode(ISD::AND, dl, MVT::i16,
7323 CWD, DAG.getConstant(0x400, MVT::i16)),
7324 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007325
Dan Gohman475871a2008-07-27 21:46:04 +00007326 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007327 DAG.getNode(ISD::AND, dl, MVT::i16,
7328 DAG.getNode(ISD::ADD, dl, MVT::i16,
7329 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7330 DAG.getConstant(1, MVT::i16)),
7331 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007332
7333
Duncan Sands83ec4b62008-06-06 12:08:01 +00007334 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007335 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007336}
7337
Dan Gohman475871a2008-07-27 21:46:04 +00007338SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007339 EVT VT = Op.getValueType();
7340 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007341 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007342 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007343
7344 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007345 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007346 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007347 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007348 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007349 }
Evan Cheng18efe262007-12-14 02:13:44 +00007350
Evan Cheng152804e2007-12-14 08:30:15 +00007351 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007352 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007353 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007354
7355 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007356 SDValue Ops[] = {
7357 Op,
7358 DAG.getConstant(NumBits+NumBits-1, OpVT),
7359 DAG.getConstant(X86::COND_E, MVT::i8),
7360 Op.getValue(1)
7361 };
7362 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007363
7364 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007365 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007366
Owen Anderson825b72b2009-08-11 20:47:22 +00007367 if (VT == MVT::i8)
7368 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007369 return Op;
7370}
7371
Dan Gohman475871a2008-07-27 21:46:04 +00007372SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007373 EVT VT = Op.getValueType();
7374 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007375 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007376 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007377
7378 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007379 if (VT == MVT::i8) {
7380 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007381 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007382 }
Evan Cheng152804e2007-12-14 08:30:15 +00007383
7384 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007385 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007386 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007387
7388 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007389 SDValue Ops[] = {
7390 Op,
7391 DAG.getConstant(NumBits, OpVT),
7392 DAG.getConstant(X86::COND_E, MVT::i8),
7393 Op.getValue(1)
7394 };
7395 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007396
Owen Anderson825b72b2009-08-11 20:47:22 +00007397 if (VT == MVT::i8)
7398 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007399 return Op;
7400}
7401
Mon P Wangaf9b9522008-12-18 21:42:19 +00007402SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007403 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007404 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007405 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007406
Mon P Wangaf9b9522008-12-18 21:42:19 +00007407 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7408 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7409 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7410 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7411 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7412 //
7413 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7414 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7415 // return AloBlo + AloBhi + AhiBlo;
7416
7417 SDValue A = Op.getOperand(0);
7418 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007419
Dale Johannesene4d209d2009-02-03 20:21:25 +00007420 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007421 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7422 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007423 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007424 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7425 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007426 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007427 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007428 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007429 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007430 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007431 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007432 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007433 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007434 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007435 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007436 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7437 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007438 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007439 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7440 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007441 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7442 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007443 return Res;
7444}
7445
7446
Bill Wendling74c37652008-12-09 22:08:41 +00007447SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7448 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7449 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007450 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7451 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007452 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007453 SDValue LHS = N->getOperand(0);
7454 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007455 unsigned BaseOp = 0;
7456 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007457 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007458
7459 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007460 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007461 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007462 // A subtract of one will be selected as a INC. Note that INC doesn't
7463 // set CF, so we can't do this for UADDO.
7464 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7465 if (C->getAPIntValue() == 1) {
7466 BaseOp = X86ISD::INC;
7467 Cond = X86::COND_O;
7468 break;
7469 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007470 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007471 Cond = X86::COND_O;
7472 break;
7473 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007474 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007475 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007476 break;
7477 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007478 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7479 // set CF, so we can't do this for USUBO.
7480 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7481 if (C->getAPIntValue() == 1) {
7482 BaseOp = X86ISD::DEC;
7483 Cond = X86::COND_O;
7484 break;
7485 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007486 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007487 Cond = X86::COND_O;
7488 break;
7489 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007490 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007491 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007492 break;
7493 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007494 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007495 Cond = X86::COND_O;
7496 break;
7497 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007498 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007499 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007500 break;
7501 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007502
Bill Wendling61edeb52008-12-02 01:06:39 +00007503 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007504 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007505 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007506
Bill Wendling61edeb52008-12-02 01:06:39 +00007507 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007508 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007509 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007510
Bill Wendling61edeb52008-12-02 01:06:39 +00007511 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7512 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007513}
7514
Dan Gohman475871a2008-07-27 21:46:04 +00007515SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007516 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007517 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007518 unsigned Reg = 0;
7519 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007520 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007521 default:
7522 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007523 case MVT::i8: Reg = X86::AL; size = 1; break;
7524 case MVT::i16: Reg = X86::AX; size = 2; break;
7525 case MVT::i32: Reg = X86::EAX; size = 4; break;
7526 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007527 assert(Subtarget->is64Bit() && "Node not type legal!");
7528 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007529 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007530 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007531 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007532 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007533 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007534 Op.getOperand(1),
7535 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007536 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007537 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007538 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007539 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007540 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007541 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007542 return cpOut;
7543}
7544
Duncan Sands1607f052008-12-01 11:39:25 +00007545SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007546 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007547 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007548 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007549 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007550 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007551 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007552 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7553 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007554 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007555 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7556 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007557 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007558 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007559 rdx.getValue(1)
7560 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007561 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007562}
7563
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007564SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7565 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007566 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007567 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007568 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007569 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007570 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007571 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007572 Node->getOperand(0),
7573 Node->getOperand(1), negOp,
7574 cast<AtomicSDNode>(Node)->getSrcValue(),
7575 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007576}
7577
Evan Cheng0db9fe62006-04-25 20:13:52 +00007578/// LowerOperation - Provide custom lowering hooks for some operations.
7579///
Dan Gohman475871a2008-07-27 21:46:04 +00007580SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007581 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007582 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007583 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7584 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007585 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007586 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007587 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7588 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7589 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7590 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7591 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7592 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007593 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007594 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007595 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007596 case ISD::SHL_PARTS:
7597 case ISD::SRA_PARTS:
7598 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7599 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007600 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007601 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007602 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007603 case ISD::FABS: return LowerFABS(Op, DAG);
7604 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007605 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007606 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007607 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007608 case ISD::SELECT: return LowerSELECT(Op, DAG);
7609 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007610 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007611 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007612 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007613 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007614 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007615 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7616 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007617 case ISD::FRAME_TO_ARGS_OFFSET:
7618 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007619 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007620 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007621 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007622 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007623 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7624 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007625 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007626 case ISD::SADDO:
7627 case ISD::UADDO:
7628 case ISD::SSUBO:
7629 case ISD::USUBO:
7630 case ISD::SMULO:
7631 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007632 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007633 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007634}
7635
Duncan Sands1607f052008-12-01 11:39:25 +00007636void X86TargetLowering::
7637ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7638 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007639 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007640 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007641 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007642
7643 SDValue Chain = Node->getOperand(0);
7644 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007645 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007646 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007647 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007648 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007649 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007650 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007651 SDValue Result =
7652 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7653 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007654 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007655 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007656 Results.push_back(Result.getValue(2));
7657}
7658
Duncan Sands126d9072008-07-04 11:47:58 +00007659/// ReplaceNodeResults - Replace a node with an illegal result type
7660/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007661void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7662 SmallVectorImpl<SDValue>&Results,
7663 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007664 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007665 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007666 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007667 assert(false && "Do not know how to custom type legalize this operation!");
7668 return;
7669 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007670 std::pair<SDValue,SDValue> Vals =
7671 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007672 SDValue FIST = Vals.first, StackSlot = Vals.second;
7673 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007674 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007675 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007676 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7677 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007678 }
7679 return;
7680 }
7681 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007682 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007683 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007684 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007685 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007686 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007687 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007688 eax.getValue(2));
7689 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7690 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007691 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007692 Results.push_back(edx.getValue(1));
7693 return;
7694 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007695 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007696 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007697 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007698 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007699 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7700 DAG.getConstant(0, MVT::i32));
7701 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7702 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007703 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7704 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007705 cpInL.getValue(1));
7706 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007707 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7708 DAG.getConstant(0, MVT::i32));
7709 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7710 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007711 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007712 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007713 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007714 swapInL.getValue(1));
7715 SDValue Ops[] = { swapInH.getValue(0),
7716 N->getOperand(1),
7717 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007718 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007719 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007720 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007721 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007722 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007723 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007724 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007725 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007726 Results.push_back(cpOutH.getValue(1));
7727 return;
7728 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007729 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007730 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7731 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007732 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007733 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7734 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007735 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007736 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7737 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007738 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007739 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7740 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007741 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007742 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7743 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007744 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007745 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7746 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007747 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007748 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7749 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007750 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007751}
7752
Evan Cheng72261582005-12-20 06:22:03 +00007753const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7754 switch (Opcode) {
7755 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007756 case X86ISD::BSF: return "X86ISD::BSF";
7757 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007758 case X86ISD::SHLD: return "X86ISD::SHLD";
7759 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007760 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007761 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007762 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007763 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007764 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007765 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007766 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7767 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7768 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007769 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007770 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007771 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007772 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007773 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007774 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007775 case X86ISD::COMI: return "X86ISD::COMI";
7776 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007777 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007778 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007779 case X86ISD::CMOV: return "X86ISD::CMOV";
7780 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007781 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007782 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7783 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007784 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007785 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007786 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007787 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007788 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007789 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7790 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007791 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007792 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007793 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007794 case X86ISD::FMAX: return "X86ISD::FMAX";
7795 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007796 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7797 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007798 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007799 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007800 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007801 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007802 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007803 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7804 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007805 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7806 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7807 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7808 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7809 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7810 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007811 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7812 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007813 case X86ISD::VSHL: return "X86ISD::VSHL";
7814 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007815 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7816 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7817 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7818 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7819 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7820 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7821 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7822 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7823 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7824 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007825 case X86ISD::ADD: return "X86ISD::ADD";
7826 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007827 case X86ISD::SMUL: return "X86ISD::SMUL";
7828 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007829 case X86ISD::INC: return "X86ISD::INC";
7830 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007831 case X86ISD::OR: return "X86ISD::OR";
7832 case X86ISD::XOR: return "X86ISD::XOR";
7833 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007834 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007835 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007836 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007837 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007838 }
7839}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007840
Chris Lattnerc9addb72007-03-30 23:15:24 +00007841// isLegalAddressingMode - Return true if the addressing mode represented
7842// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007843bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007844 const Type *Ty) const {
7845 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007846 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007847
Chris Lattnerc9addb72007-03-30 23:15:24 +00007848 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007849 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007850 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007851
Chris Lattnerc9addb72007-03-30 23:15:24 +00007852 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007853 unsigned GVFlags =
7854 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007855
Chris Lattnerdfed4132009-07-10 07:38:24 +00007856 // If a reference to this global requires an extra load, we can't fold it.
7857 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007858 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007859
Chris Lattnerdfed4132009-07-10 07:38:24 +00007860 // If BaseGV requires a register for the PIC base, we cannot also have a
7861 // BaseReg specified.
7862 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007863 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007864
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007865 // If lower 4G is not available, then we must use rip-relative addressing.
7866 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7867 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007868 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007869
Chris Lattnerc9addb72007-03-30 23:15:24 +00007870 switch (AM.Scale) {
7871 case 0:
7872 case 1:
7873 case 2:
7874 case 4:
7875 case 8:
7876 // These scales always work.
7877 break;
7878 case 3:
7879 case 5:
7880 case 9:
7881 // These scales are formed with basereg+scalereg. Only accept if there is
7882 // no basereg yet.
7883 if (AM.HasBaseReg)
7884 return false;
7885 break;
7886 default: // Other stuff never works.
7887 return false;
7888 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007889
Chris Lattnerc9addb72007-03-30 23:15:24 +00007890 return true;
7891}
7892
7893
Evan Cheng2bd122c2007-10-26 01:56:11 +00007894bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007895 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007896 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007897 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7898 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007899 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007900 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007901 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007902}
7903
Owen Andersone50ed302009-08-10 22:56:29 +00007904bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007905 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007906 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007907 unsigned NumBits1 = VT1.getSizeInBits();
7908 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007909 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007910 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007911 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007912}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007913
Dan Gohman97121ba2009-04-08 00:15:30 +00007914bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007915 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007916 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007917}
7918
Owen Andersone50ed302009-08-10 22:56:29 +00007919bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007920 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007921 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007922}
7923
Owen Andersone50ed302009-08-10 22:56:29 +00007924bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007925 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007926 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007927}
7928
Evan Cheng60c07e12006-07-05 22:17:51 +00007929/// isShuffleMaskLegal - Targets can use this to indicate that they only
7930/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7931/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7932/// are assumed to be legal.
7933bool
Eric Christopherfd179292009-08-27 18:07:15 +00007934X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007935 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007936 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007937 if (VT.getSizeInBits() == 64)
7938 return false;
7939
Nate Begemana09008b2009-10-19 02:17:23 +00007940 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007941 return (VT.getVectorNumElements() == 2 ||
7942 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7943 isMOVLMask(M, VT) ||
7944 isSHUFPMask(M, VT) ||
7945 isPSHUFDMask(M, VT) ||
7946 isPSHUFHWMask(M, VT) ||
7947 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007948 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007949 isUNPCKLMask(M, VT) ||
7950 isUNPCKHMask(M, VT) ||
7951 isUNPCKL_v_undef_Mask(M, VT) ||
7952 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007953}
7954
Dan Gohman7d8143f2008-04-09 20:09:42 +00007955bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007956X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007957 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007958 unsigned NumElts = VT.getVectorNumElements();
7959 // FIXME: This collection of masks seems suspect.
7960 if (NumElts == 2)
7961 return true;
7962 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7963 return (isMOVLMask(Mask, VT) ||
7964 isCommutedMOVLMask(Mask, VT, true) ||
7965 isSHUFPMask(Mask, VT) ||
7966 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007967 }
7968 return false;
7969}
7970
7971//===----------------------------------------------------------------------===//
7972// X86 Scheduler Hooks
7973//===----------------------------------------------------------------------===//
7974
Mon P Wang63307c32008-05-05 19:05:59 +00007975// private utility function
7976MachineBasicBlock *
7977X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7978 MachineBasicBlock *MBB,
7979 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007980 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007981 unsigned LoadOpc,
7982 unsigned CXchgOpc,
7983 unsigned copyOpc,
7984 unsigned notOpc,
7985 unsigned EAXreg,
7986 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007987 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007988 // For the atomic bitwise operator, we generate
7989 // thisMBB:
7990 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007991 // ld t1 = [bitinstr.addr]
7992 // op t2 = t1, [bitinstr.val]
7993 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007994 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7995 // bz newMBB
7996 // fallthrough -->nextMBB
7997 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7998 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007999 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008000 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008001
Mon P Wang63307c32008-05-05 19:05:59 +00008002 /// First build the CFG
8003 MachineFunction *F = MBB->getParent();
8004 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008005 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8006 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8007 F->insert(MBBIter, newMBB);
8008 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008009
Mon P Wang63307c32008-05-05 19:05:59 +00008010 // Move all successors to thisMBB to nextMBB
8011 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008012
Mon P Wang63307c32008-05-05 19:05:59 +00008013 // Update thisMBB to fall through to newMBB
8014 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008015
Mon P Wang63307c32008-05-05 19:05:59 +00008016 // newMBB jumps to itself and fall through to nextMBB
8017 newMBB->addSuccessor(nextMBB);
8018 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008019
Mon P Wang63307c32008-05-05 19:05:59 +00008020 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008021 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008022 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00008023 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008024 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008025 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008026 int numArgs = bInstr->getNumOperands() - 1;
8027 for (int i=0; i < numArgs; ++i)
8028 argOpers[i] = &bInstr->getOperand(i+1);
8029
8030 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008031 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8032 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008033
Dale Johannesen140be2d2008-08-19 18:47:28 +00008034 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008035 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008036 for (int i=0; i <= lastAddrIndx; ++i)
8037 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008038
Dale Johannesen140be2d2008-08-19 18:47:28 +00008039 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008040 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008041 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008042 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008043 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008044 tt = t1;
8045
Dale Johannesen140be2d2008-08-19 18:47:28 +00008046 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00008047 assert((argOpers[valArgIndx]->isReg() ||
8048 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008049 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00008050 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008051 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008052 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008053 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008054 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00008055 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008056
Dale Johannesene4d209d2009-02-03 20:21:25 +00008057 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008058 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008059
Dale Johannesene4d209d2009-02-03 20:21:25 +00008060 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008061 for (int i=0; i <= lastAddrIndx; ++i)
8062 (*MIB).addOperand(*argOpers[i]);
8063 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008064 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008065 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8066 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008067
Dale Johannesene4d209d2009-02-03 20:21:25 +00008068 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008069 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008070
Mon P Wang63307c32008-05-05 19:05:59 +00008071 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008072 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008073
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008074 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008075 return nextMBB;
8076}
8077
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008078// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008079MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008080X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8081 MachineBasicBlock *MBB,
8082 unsigned regOpcL,
8083 unsigned regOpcH,
8084 unsigned immOpcL,
8085 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008086 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008087 // For the atomic bitwise operator, we generate
8088 // thisMBB (instructions are in pairs, except cmpxchg8b)
8089 // ld t1,t2 = [bitinstr.addr]
8090 // newMBB:
8091 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8092 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008093 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008094 // mov ECX, EBX <- t5, t6
8095 // mov EAX, EDX <- t1, t2
8096 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8097 // mov t3, t4 <- EAX, EDX
8098 // bz newMBB
8099 // result in out1, out2
8100 // fallthrough -->nextMBB
8101
8102 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8103 const unsigned LoadOpc = X86::MOV32rm;
8104 const unsigned copyOpc = X86::MOV32rr;
8105 const unsigned NotOpc = X86::NOT32r;
8106 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8107 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8108 MachineFunction::iterator MBBIter = MBB;
8109 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008110
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008111 /// First build the CFG
8112 MachineFunction *F = MBB->getParent();
8113 MachineBasicBlock *thisMBB = MBB;
8114 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8115 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8116 F->insert(MBBIter, newMBB);
8117 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008118
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008119 // Move all successors to thisMBB to nextMBB
8120 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008121
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008122 // Update thisMBB to fall through to newMBB
8123 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008124
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008125 // newMBB jumps to itself and fall through to nextMBB
8126 newMBB->addSuccessor(nextMBB);
8127 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008128
Dale Johannesene4d209d2009-02-03 20:21:25 +00008129 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008130 // Insert instructions into newMBB based on incoming instruction
8131 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008132 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008133 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008134 MachineOperand& dest1Oper = bInstr->getOperand(0);
8135 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008136 MachineOperand* argOpers[2 + X86AddrNumOperands];
8137 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008138 argOpers[i] = &bInstr->getOperand(i+2);
8139
Evan Chengad5b52f2010-01-08 19:14:57 +00008140 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008141 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008142
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008143 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008144 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008145 for (int i=0; i <= lastAddrIndx; ++i)
8146 (*MIB).addOperand(*argOpers[i]);
8147 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008148 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008149 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008150 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008151 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008152 MachineOperand newOp3 = *(argOpers[3]);
8153 if (newOp3.isImm())
8154 newOp3.setImm(newOp3.getImm()+4);
8155 else
8156 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008157 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008158 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008159
8160 // t3/4 are defined later, at the bottom of the loop
8161 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8162 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008163 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008164 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008165 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008166 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8167
Evan Cheng306b4ca2010-01-08 23:41:50 +00008168 // The subsequent operations should be using the destination registers of
8169 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008170 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008171 t1 = F->getRegInfo().createVirtualRegister(RC);
8172 t2 = F->getRegInfo().createVirtualRegister(RC);
8173 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8174 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008175 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008176 t1 = dest1Oper.getReg();
8177 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008178 }
8179
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008180 int valArgIndx = lastAddrIndx + 1;
8181 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008182 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008183 "invalid operand");
8184 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8185 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008186 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008187 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008188 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008189 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008190 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008191 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008192 (*MIB).addOperand(*argOpers[valArgIndx]);
8193 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008194 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008195 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008196 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008197 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008198 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008199 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008200 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008201 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008202 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008203 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008204
Dale Johannesene4d209d2009-02-03 20:21:25 +00008205 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008206 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008207 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008208 MIB.addReg(t2);
8209
Dale Johannesene4d209d2009-02-03 20:21:25 +00008210 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008211 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008212 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008213 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008214
Dale Johannesene4d209d2009-02-03 20:21:25 +00008215 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008216 for (int i=0; i <= lastAddrIndx; ++i)
8217 (*MIB).addOperand(*argOpers[i]);
8218
8219 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008220 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8221 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008222
Dale Johannesene4d209d2009-02-03 20:21:25 +00008223 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008224 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008225 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008226 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008227
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008228 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008229 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008230
8231 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8232 return nextMBB;
8233}
8234
8235// private utility function
8236MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008237X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8238 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008239 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008240 // For the atomic min/max operator, we generate
8241 // thisMBB:
8242 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008243 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008244 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008245 // cmp t1, t2
8246 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008247 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008248 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8249 // bz newMBB
8250 // fallthrough -->nextMBB
8251 //
8252 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8253 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008254 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008255 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008256
Mon P Wang63307c32008-05-05 19:05:59 +00008257 /// First build the CFG
8258 MachineFunction *F = MBB->getParent();
8259 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008260 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8261 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8262 F->insert(MBBIter, newMBB);
8263 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008264
Dan Gohmand6708ea2009-08-15 01:38:56 +00008265 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008266 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008267
Mon P Wang63307c32008-05-05 19:05:59 +00008268 // Update thisMBB to fall through to newMBB
8269 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008270
Mon P Wang63307c32008-05-05 19:05:59 +00008271 // newMBB jumps to newMBB and fall through to nextMBB
8272 newMBB->addSuccessor(nextMBB);
8273 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008274
Dale Johannesene4d209d2009-02-03 20:21:25 +00008275 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008276 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008277 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008278 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008279 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008280 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008281 int numArgs = mInstr->getNumOperands() - 1;
8282 for (int i=0; i < numArgs; ++i)
8283 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008284
Mon P Wang63307c32008-05-05 19:05:59 +00008285 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008286 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8287 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008288
Mon P Wangab3e7472008-05-05 22:56:23 +00008289 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008290 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008291 for (int i=0; i <= lastAddrIndx; ++i)
8292 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008293
Mon P Wang63307c32008-05-05 19:05:59 +00008294 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008295 assert((argOpers[valArgIndx]->isReg() ||
8296 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008297 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008298
8299 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008300 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008301 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008302 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008303 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008304 (*MIB).addOperand(*argOpers[valArgIndx]);
8305
Dale Johannesene4d209d2009-02-03 20:21:25 +00008306 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008307 MIB.addReg(t1);
8308
Dale Johannesene4d209d2009-02-03 20:21:25 +00008309 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008310 MIB.addReg(t1);
8311 MIB.addReg(t2);
8312
8313 // Generate movc
8314 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008315 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008316 MIB.addReg(t2);
8317 MIB.addReg(t1);
8318
8319 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008320 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008321 for (int i=0; i <= lastAddrIndx; ++i)
8322 (*MIB).addOperand(*argOpers[i]);
8323 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008324 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008325 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8326 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008327
Dale Johannesene4d209d2009-02-03 20:21:25 +00008328 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008329 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008330
Mon P Wang63307c32008-05-05 19:05:59 +00008331 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008332 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008333
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008334 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008335 return nextMBB;
8336}
8337
Eric Christopherf83a5de2009-08-27 18:08:16 +00008338// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8339// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008340MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008341X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008342 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008343
8344 MachineFunction *F = BB->getParent();
8345 DebugLoc dl = MI->getDebugLoc();
8346 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8347
8348 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008349 if (memArg)
8350 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8351 else
8352 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008353
8354 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8355
8356 for (unsigned i = 0; i < numArgs; ++i) {
8357 MachineOperand &Op = MI->getOperand(i+1);
8358
8359 if (!(Op.isReg() && Op.isImplicit()))
8360 MIB.addOperand(Op);
8361 }
8362
8363 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8364 .addReg(X86::XMM0);
8365
8366 F->DeleteMachineInstr(MI);
8367
8368 return BB;
8369}
8370
8371MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008372X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8373 MachineInstr *MI,
8374 MachineBasicBlock *MBB) const {
8375 // Emit code to save XMM registers to the stack. The ABI says that the
8376 // number of registers to save is given in %al, so it's theoretically
8377 // possible to do an indirect jump trick to avoid saving all of them,
8378 // however this code takes a simpler approach and just executes all
8379 // of the stores if %al is non-zero. It's less code, and it's probably
8380 // easier on the hardware branch predictor, and stores aren't all that
8381 // expensive anyway.
8382
8383 // Create the new basic blocks. One block contains all the XMM stores,
8384 // and one block is the final destination regardless of whether any
8385 // stores were performed.
8386 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8387 MachineFunction *F = MBB->getParent();
8388 MachineFunction::iterator MBBIter = MBB;
8389 ++MBBIter;
8390 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8391 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8392 F->insert(MBBIter, XMMSaveMBB);
8393 F->insert(MBBIter, EndMBB);
8394
8395 // Set up the CFG.
8396 // Move any original successors of MBB to the end block.
8397 EndMBB->transferSuccessors(MBB);
8398 // The original block will now fall through to the XMM save block.
8399 MBB->addSuccessor(XMMSaveMBB);
8400 // The XMMSaveMBB will fall through to the end block.
8401 XMMSaveMBB->addSuccessor(EndMBB);
8402
8403 // Now add the instructions.
8404 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8405 DebugLoc DL = MI->getDebugLoc();
8406
8407 unsigned CountReg = MI->getOperand(0).getReg();
8408 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8409 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8410
8411 if (!Subtarget->isTargetWin64()) {
8412 // If %al is 0, branch around the XMM save block.
8413 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008414 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008415 MBB->addSuccessor(EndMBB);
8416 }
8417
8418 // In the XMM save block, save all the XMM argument registers.
8419 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8420 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008421 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008422 F->getMachineMemOperand(
8423 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8424 MachineMemOperand::MOStore, Offset,
8425 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008426 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8427 .addFrameIndex(RegSaveFrameIndex)
8428 .addImm(/*Scale=*/1)
8429 .addReg(/*IndexReg=*/0)
8430 .addImm(/*Disp=*/Offset)
8431 .addReg(/*Segment=*/0)
8432 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008433 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008434 }
8435
8436 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8437
8438 return EndMBB;
8439}
Mon P Wang63307c32008-05-05 19:05:59 +00008440
Evan Cheng60c07e12006-07-05 22:17:51 +00008441MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008442X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008443 MachineBasicBlock *BB,
8444 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008445 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8446 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008447
Chris Lattner52600972009-09-02 05:57:00 +00008448 // To "insert" a SELECT_CC instruction, we actually have to insert the
8449 // diamond control-flow pattern. The incoming instruction knows the
8450 // destination vreg to set, the condition code register to branch on, the
8451 // true/false values to select between, and a branch opcode to use.
8452 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8453 MachineFunction::iterator It = BB;
8454 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008455
Chris Lattner52600972009-09-02 05:57:00 +00008456 // thisMBB:
8457 // ...
8458 // TrueVal = ...
8459 // cmpTY ccX, r1, r2
8460 // bCC copy1MBB
8461 // fallthrough --> copy0MBB
8462 MachineBasicBlock *thisMBB = BB;
8463 MachineFunction *F = BB->getParent();
8464 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8465 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8466 unsigned Opc =
8467 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8468 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8469 F->insert(It, copy0MBB);
8470 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008471 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008472 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008473 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008474 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008475 E = BB->succ_end(); I != E; ++I) {
8476 EM->insert(std::make_pair(*I, sinkMBB));
8477 sinkMBB->addSuccessor(*I);
8478 }
8479 // Next, remove all successors of the current block, and add the true
8480 // and fallthrough blocks as its successors.
8481 while (!BB->succ_empty())
8482 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008483 // Add the true and fallthrough blocks as its successors.
8484 BB->addSuccessor(copy0MBB);
8485 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008486
Chris Lattner52600972009-09-02 05:57:00 +00008487 // copy0MBB:
8488 // %FalseValue = ...
8489 // # fallthrough to sinkMBB
8490 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008491
Chris Lattner52600972009-09-02 05:57:00 +00008492 // Update machine-CFG edges
8493 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008494
Chris Lattner52600972009-09-02 05:57:00 +00008495 // sinkMBB:
8496 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8497 // ...
8498 BB = sinkMBB;
8499 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8500 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8501 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8502
8503 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8504 return BB;
8505}
8506
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008507MachineBasicBlock *
8508X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8509 MachineBasicBlock *BB,
8510 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8511 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8512 DebugLoc DL = MI->getDebugLoc();
8513 MachineFunction *F = BB->getParent();
8514
8515 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8516 // non-trivial part is impdef of ESP.
8517 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8518 // mingw-w64.
8519
8520 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8521 .addExternalSymbol("_alloca")
8522 .addReg(X86::EAX, RegState::Implicit)
8523 .addReg(X86::ESP, RegState::Implicit)
8524 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8525 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8526
8527 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8528 return BB;
8529}
Chris Lattner52600972009-09-02 05:57:00 +00008530
8531MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008532X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008533 MachineBasicBlock *BB,
8534 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008535 switch (MI->getOpcode()) {
8536 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008537 case X86::MINGW_ALLOCA:
8538 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008539 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008540 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008541 case X86::CMOV_FR32:
8542 case X86::CMOV_FR64:
8543 case X86::CMOV_V4F32:
8544 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008545 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008546 case X86::CMOV_GR16:
8547 case X86::CMOV_GR32:
8548 case X86::CMOV_RFP32:
8549 case X86::CMOV_RFP64:
8550 case X86::CMOV_RFP80:
Evan Chengce319102009-09-19 09:51:03 +00008551 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008552
Dale Johannesen849f2142007-07-03 00:53:03 +00008553 case X86::FP32_TO_INT16_IN_MEM:
8554 case X86::FP32_TO_INT32_IN_MEM:
8555 case X86::FP32_TO_INT64_IN_MEM:
8556 case X86::FP64_TO_INT16_IN_MEM:
8557 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008558 case X86::FP64_TO_INT64_IN_MEM:
8559 case X86::FP80_TO_INT16_IN_MEM:
8560 case X86::FP80_TO_INT32_IN_MEM:
8561 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008562 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8563 DebugLoc DL = MI->getDebugLoc();
8564
Evan Cheng60c07e12006-07-05 22:17:51 +00008565 // Change the floating point control register to use "round towards zero"
8566 // mode when truncating to an integer value.
8567 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008568 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008569 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008570
8571 // Load the old value of the high byte of the control word...
8572 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008573 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008574 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008575 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008576
8577 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008578 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008579 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008580
8581 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008582 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008583
8584 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008585 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008586 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008587
8588 // Get the X86 opcode to use.
8589 unsigned Opc;
8590 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008591 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008592 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8593 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8594 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8595 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8596 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8597 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008598 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8599 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8600 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008601 }
8602
8603 X86AddressMode AM;
8604 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008605 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008606 AM.BaseType = X86AddressMode::RegBase;
8607 AM.Base.Reg = Op.getReg();
8608 } else {
8609 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008610 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008611 }
8612 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008613 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008614 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008615 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008616 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008617 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008618 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008619 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008620 AM.GV = Op.getGlobal();
8621 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008622 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008623 }
Chris Lattner52600972009-09-02 05:57:00 +00008624 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008625 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008626
8627 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008628 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008629
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008630 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008631 return BB;
8632 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00008633 // DBG_VALUE. Only the frame index case is done here.
8634 case X86::DBG_VALUE: {
8635 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8636 DebugLoc DL = MI->getDebugLoc();
8637 X86AddressMode AM;
8638 MachineFunction *F = BB->getParent();
8639 AM.BaseType = X86AddressMode::FrameIndexBase;
8640 AM.Base.FrameIndex = MI->getOperand(0).getImm();
8641 addFullAddress(BuildMI(BB, DL, TII->get(X86::DBG_VALUE)), AM).
8642 addImm(MI->getOperand(1).getImm()).
8643 addMetadata(MI->getOperand(2).getMetadata());
8644 F->DeleteMachineInstr(MI); // Remove pseudo.
8645 return BB;
8646 }
8647
Eric Christopherb120ab42009-08-18 22:50:32 +00008648 // String/text processing lowering.
8649 case X86::PCMPISTRM128REG:
8650 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8651 case X86::PCMPISTRM128MEM:
8652 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8653 case X86::PCMPESTRM128REG:
8654 return EmitPCMP(MI, BB, 5, false /* in mem */);
8655 case X86::PCMPESTRM128MEM:
8656 return EmitPCMP(MI, BB, 5, true /* in mem */);
8657
8658 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008659 case X86::ATOMAND32:
8660 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008661 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008662 X86::LCMPXCHG32, X86::MOV32rr,
8663 X86::NOT32r, X86::EAX,
8664 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008665 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008666 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8667 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008668 X86::LCMPXCHG32, X86::MOV32rr,
8669 X86::NOT32r, X86::EAX,
8670 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008671 case X86::ATOMXOR32:
8672 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008673 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008674 X86::LCMPXCHG32, X86::MOV32rr,
8675 X86::NOT32r, X86::EAX,
8676 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008677 case X86::ATOMNAND32:
8678 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008679 X86::AND32ri, X86::MOV32rm,
8680 X86::LCMPXCHG32, X86::MOV32rr,
8681 X86::NOT32r, X86::EAX,
8682 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008683 case X86::ATOMMIN32:
8684 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8685 case X86::ATOMMAX32:
8686 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8687 case X86::ATOMUMIN32:
8688 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8689 case X86::ATOMUMAX32:
8690 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008691
8692 case X86::ATOMAND16:
8693 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8694 X86::AND16ri, X86::MOV16rm,
8695 X86::LCMPXCHG16, X86::MOV16rr,
8696 X86::NOT16r, X86::AX,
8697 X86::GR16RegisterClass);
8698 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008699 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008700 X86::OR16ri, X86::MOV16rm,
8701 X86::LCMPXCHG16, X86::MOV16rr,
8702 X86::NOT16r, X86::AX,
8703 X86::GR16RegisterClass);
8704 case X86::ATOMXOR16:
8705 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8706 X86::XOR16ri, X86::MOV16rm,
8707 X86::LCMPXCHG16, X86::MOV16rr,
8708 X86::NOT16r, X86::AX,
8709 X86::GR16RegisterClass);
8710 case X86::ATOMNAND16:
8711 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8712 X86::AND16ri, X86::MOV16rm,
8713 X86::LCMPXCHG16, X86::MOV16rr,
8714 X86::NOT16r, X86::AX,
8715 X86::GR16RegisterClass, true);
8716 case X86::ATOMMIN16:
8717 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8718 case X86::ATOMMAX16:
8719 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8720 case X86::ATOMUMIN16:
8721 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8722 case X86::ATOMUMAX16:
8723 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8724
8725 case X86::ATOMAND8:
8726 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8727 X86::AND8ri, X86::MOV8rm,
8728 X86::LCMPXCHG8, X86::MOV8rr,
8729 X86::NOT8r, X86::AL,
8730 X86::GR8RegisterClass);
8731 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008732 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008733 X86::OR8ri, X86::MOV8rm,
8734 X86::LCMPXCHG8, X86::MOV8rr,
8735 X86::NOT8r, X86::AL,
8736 X86::GR8RegisterClass);
8737 case X86::ATOMXOR8:
8738 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8739 X86::XOR8ri, X86::MOV8rm,
8740 X86::LCMPXCHG8, X86::MOV8rr,
8741 X86::NOT8r, X86::AL,
8742 X86::GR8RegisterClass);
8743 case X86::ATOMNAND8:
8744 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8745 X86::AND8ri, X86::MOV8rm,
8746 X86::LCMPXCHG8, X86::MOV8rr,
8747 X86::NOT8r, X86::AL,
8748 X86::GR8RegisterClass, true);
8749 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008750 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008751 case X86::ATOMAND64:
8752 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008753 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008754 X86::LCMPXCHG64, X86::MOV64rr,
8755 X86::NOT64r, X86::RAX,
8756 X86::GR64RegisterClass);
8757 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008758 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8759 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008760 X86::LCMPXCHG64, X86::MOV64rr,
8761 X86::NOT64r, X86::RAX,
8762 X86::GR64RegisterClass);
8763 case X86::ATOMXOR64:
8764 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008765 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008766 X86::LCMPXCHG64, X86::MOV64rr,
8767 X86::NOT64r, X86::RAX,
8768 X86::GR64RegisterClass);
8769 case X86::ATOMNAND64:
8770 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8771 X86::AND64ri32, X86::MOV64rm,
8772 X86::LCMPXCHG64, X86::MOV64rr,
8773 X86::NOT64r, X86::RAX,
8774 X86::GR64RegisterClass, true);
8775 case X86::ATOMMIN64:
8776 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8777 case X86::ATOMMAX64:
8778 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8779 case X86::ATOMUMIN64:
8780 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8781 case X86::ATOMUMAX64:
8782 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008783
8784 // This group does 64-bit operations on a 32-bit host.
8785 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008786 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008787 X86::AND32rr, X86::AND32rr,
8788 X86::AND32ri, X86::AND32ri,
8789 false);
8790 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008791 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008792 X86::OR32rr, X86::OR32rr,
8793 X86::OR32ri, X86::OR32ri,
8794 false);
8795 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008796 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008797 X86::XOR32rr, X86::XOR32rr,
8798 X86::XOR32ri, X86::XOR32ri,
8799 false);
8800 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008801 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008802 X86::AND32rr, X86::AND32rr,
8803 X86::AND32ri, X86::AND32ri,
8804 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008805 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008806 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008807 X86::ADD32rr, X86::ADC32rr,
8808 X86::ADD32ri, X86::ADC32ri,
8809 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008810 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008811 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008812 X86::SUB32rr, X86::SBB32rr,
8813 X86::SUB32ri, X86::SBB32ri,
8814 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008815 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008816 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008817 X86::MOV32rr, X86::MOV32rr,
8818 X86::MOV32ri, X86::MOV32ri,
8819 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008820 case X86::VASTART_SAVE_XMM_REGS:
8821 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008822 }
8823}
8824
8825//===----------------------------------------------------------------------===//
8826// X86 Optimization Hooks
8827//===----------------------------------------------------------------------===//
8828
Dan Gohman475871a2008-07-27 21:46:04 +00008829void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008830 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008831 APInt &KnownZero,
8832 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008833 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008834 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008835 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008836 assert((Opc >= ISD::BUILTIN_OP_END ||
8837 Opc == ISD::INTRINSIC_WO_CHAIN ||
8838 Opc == ISD::INTRINSIC_W_CHAIN ||
8839 Opc == ISD::INTRINSIC_VOID) &&
8840 "Should use MaskedValueIsZero if you don't know whether Op"
8841 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008842
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008843 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008844 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008845 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008846 case X86ISD::ADD:
8847 case X86ISD::SUB:
8848 case X86ISD::SMUL:
8849 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008850 case X86ISD::INC:
8851 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008852 case X86ISD::OR:
8853 case X86ISD::XOR:
8854 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008855 // These nodes' second result is a boolean.
8856 if (Op.getResNo() == 0)
8857 break;
8858 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008859 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008860 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8861 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008862 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008863 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008864}
Chris Lattner259e97c2006-01-31 19:43:35 +00008865
Evan Cheng206ee9d2006-07-07 08:33:52 +00008866/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008867/// node is a GlobalAddress + offset.
8868bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8869 GlobalValue* &GA, int64_t &Offset) const{
8870 if (N->getOpcode() == X86ISD::Wrapper) {
8871 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008872 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008873 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008874 return true;
8875 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008876 }
Evan Chengad4196b2008-05-12 19:56:52 +00008877 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008878}
8879
Evan Cheng206ee9d2006-07-07 08:33:52 +00008880/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8881/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8882/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008883/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008884static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008885 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008886 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008887 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008888 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008889
Eli Friedman7a5e5552009-06-07 06:52:44 +00008890 if (VT.getSizeInBits() != 128)
8891 return SDValue();
8892
Nate Begemanfdea31a2010-03-24 20:49:50 +00008893 SmallVector<SDValue, 16> Elts;
8894 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8895 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8896
8897 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008898}
Evan Chengd880b972008-05-09 21:53:03 +00008899
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008900/// PerformShuffleCombine - Detect vector gather/scatter index generation
8901/// and convert it from being a bunch of shuffles and extracts to a simple
8902/// store and scalar loads to extract the elements.
8903static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8904 const TargetLowering &TLI) {
8905 SDValue InputVector = N->getOperand(0);
8906
8907 // Only operate on vectors of 4 elements, where the alternative shuffling
8908 // gets to be more expensive.
8909 if (InputVector.getValueType() != MVT::v4i32)
8910 return SDValue();
8911
8912 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8913 // single use which is a sign-extend or zero-extend, and all elements are
8914 // used.
8915 SmallVector<SDNode *, 4> Uses;
8916 unsigned ExtractedElements = 0;
8917 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8918 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8919 if (UI.getUse().getResNo() != InputVector.getResNo())
8920 return SDValue();
8921
8922 SDNode *Extract = *UI;
8923 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8924 return SDValue();
8925
8926 if (Extract->getValueType(0) != MVT::i32)
8927 return SDValue();
8928 if (!Extract->hasOneUse())
8929 return SDValue();
8930 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8931 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8932 return SDValue();
8933 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8934 return SDValue();
8935
8936 // Record which element was extracted.
8937 ExtractedElements |=
8938 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8939
8940 Uses.push_back(Extract);
8941 }
8942
8943 // If not all the elements were used, this may not be worthwhile.
8944 if (ExtractedElements != 15)
8945 return SDValue();
8946
8947 // Ok, we've now decided to do the transformation.
8948 DebugLoc dl = InputVector.getDebugLoc();
8949
8950 // Store the value to a temporary stack slot.
8951 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
8952 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
8953 false, false, 0);
8954
8955 // Replace each use (extract) with a load of the appropriate element.
8956 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
8957 UE = Uses.end(); UI != UE; ++UI) {
8958 SDNode *Extract = *UI;
8959
8960 // Compute the element's address.
8961 SDValue Idx = Extract->getOperand(1);
8962 unsigned EltSize =
8963 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
8964 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
8965 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
8966
8967 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
8968
8969 // Load the scalar.
8970 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
8971 NULL, 0, false, false, 0);
8972
8973 // Replace the exact with the load.
8974 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
8975 }
8976
8977 // The replacement was made in place; don't return anything.
8978 return SDValue();
8979}
8980
Chris Lattner83e6c992006-10-04 06:57:07 +00008981/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008982static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008983 const X86Subtarget *Subtarget) {
8984 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008985 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008986 // Get the LHS/RHS of the select.
8987 SDValue LHS = N->getOperand(1);
8988 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008989
Dan Gohman670e5392009-09-21 18:03:22 +00008990 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00008991 // instructions match the semantics of the common C idiom x<y?x:y but not
8992 // x<=y?x:y, because of how they handle negative zero (which can be
8993 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00008994 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008995 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008996 Cond.getOpcode() == ISD::SETCC) {
8997 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008998
Chris Lattner47b4ce82009-03-11 05:48:52 +00008999 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00009000 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00009001 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
9002 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009003 switch (CC) {
9004 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009005 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009006 // Converting this to a min would handle NaNs incorrectly, and swapping
9007 // the operands would cause it to handle comparisons between positive
9008 // and negative zero incorrectly.
9009 if (!FiniteOnlyFPMath() &&
9010 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9011 if (!UnsafeFPMath &&
9012 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9013 break;
9014 std::swap(LHS, RHS);
9015 }
Dan Gohman670e5392009-09-21 18:03:22 +00009016 Opcode = X86ISD::FMIN;
9017 break;
9018 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009019 // Converting this to a min would handle comparisons between positive
9020 // and negative zero incorrectly.
9021 if (!UnsafeFPMath &&
9022 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
9023 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009024 Opcode = X86ISD::FMIN;
9025 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009026 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009027 // Converting this to a min would handle both negative zeros and NaNs
9028 // incorrectly, but we can swap the operands to fix both.
9029 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009030 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009031 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009032 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009033 Opcode = X86ISD::FMIN;
9034 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009035
Dan Gohman670e5392009-09-21 18:03:22 +00009036 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009037 // Converting this to a max would handle comparisons between positive
9038 // and negative zero incorrectly.
9039 if (!UnsafeFPMath &&
9040 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
9041 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009042 Opcode = X86ISD::FMAX;
9043 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009044 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009045 // Converting this to a max would handle NaNs incorrectly, and swapping
9046 // the operands would cause it to handle comparisons between positive
9047 // and negative zero incorrectly.
9048 if (!FiniteOnlyFPMath() &&
9049 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9050 if (!UnsafeFPMath &&
9051 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9052 break;
9053 std::swap(LHS, RHS);
9054 }
Dan Gohman670e5392009-09-21 18:03:22 +00009055 Opcode = X86ISD::FMAX;
9056 break;
9057 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009058 // Converting this to a max would handle both negative zeros and NaNs
9059 // incorrectly, but we can swap the operands to fix both.
9060 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009061 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009062 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009063 case ISD::SETGE:
9064 Opcode = X86ISD::FMAX;
9065 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00009066 }
Dan Gohman670e5392009-09-21 18:03:22 +00009067 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00009068 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
9069 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009070 switch (CC) {
9071 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009072 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009073 // Converting this to a min would handle comparisons between positive
9074 // and negative zero incorrectly, and swapping the operands would
9075 // cause it to handle NaNs incorrectly.
9076 if (!UnsafeFPMath &&
9077 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
9078 if (!FiniteOnlyFPMath() &&
9079 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9080 break;
9081 std::swap(LHS, RHS);
9082 }
Dan Gohman670e5392009-09-21 18:03:22 +00009083 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009084 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009085 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009086 // Converting this to a min would handle NaNs incorrectly.
9087 if (!UnsafeFPMath &&
9088 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9089 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009090 Opcode = X86ISD::FMIN;
9091 break;
9092 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009093 // Converting this to a min would handle both negative zeros and NaNs
9094 // incorrectly, but we can swap the operands to fix both.
9095 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009096 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009097 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009098 case ISD::SETGE:
9099 Opcode = X86ISD::FMIN;
9100 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009101
Dan Gohman670e5392009-09-21 18:03:22 +00009102 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009103 // Converting this to a max would handle NaNs incorrectly.
9104 if (!FiniteOnlyFPMath() &&
9105 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9106 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009107 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009108 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009109 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009110 // Converting this to a max would handle comparisons between positive
9111 // and negative zero incorrectly, and swapping the operands would
9112 // cause it to handle NaNs incorrectly.
9113 if (!UnsafeFPMath &&
9114 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9115 if (!FiniteOnlyFPMath() &&
9116 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9117 break;
9118 std::swap(LHS, RHS);
9119 }
Dan Gohman670e5392009-09-21 18:03:22 +00009120 Opcode = X86ISD::FMAX;
9121 break;
9122 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009123 // Converting this to a max would handle both negative zeros and NaNs
9124 // incorrectly, but we can swap the operands to fix both.
9125 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009126 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009127 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009128 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009129 Opcode = X86ISD::FMAX;
9130 break;
9131 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009132 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009133
Chris Lattner47b4ce82009-03-11 05:48:52 +00009134 if (Opcode)
9135 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009136 }
Eric Christopherfd179292009-08-27 18:07:15 +00009137
Chris Lattnerd1980a52009-03-12 06:52:53 +00009138 // If this is a select between two integer constants, try to do some
9139 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009140 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9141 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009142 // Don't do this for crazy integer types.
9143 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9144 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009145 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009146 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009147
Chris Lattnercee56e72009-03-13 05:53:31 +00009148 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009149 // Efficiently invertible.
9150 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9151 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9152 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9153 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009154 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009155 }
Eric Christopherfd179292009-08-27 18:07:15 +00009156
Chris Lattnerd1980a52009-03-12 06:52:53 +00009157 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009158 if (FalseC->getAPIntValue() == 0 &&
9159 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009160 if (NeedsCondInvert) // Invert the condition if needed.
9161 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9162 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009163
Chris Lattnerd1980a52009-03-12 06:52:53 +00009164 // Zero extend the condition if needed.
9165 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009166
Chris Lattnercee56e72009-03-13 05:53:31 +00009167 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009168 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009169 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009170 }
Eric Christopherfd179292009-08-27 18:07:15 +00009171
Chris Lattner97a29a52009-03-13 05:22:11 +00009172 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009173 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009174 if (NeedsCondInvert) // Invert the condition if needed.
9175 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9176 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009177
Chris Lattner97a29a52009-03-13 05:22:11 +00009178 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009179 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9180 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009181 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009182 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009183 }
Eric Christopherfd179292009-08-27 18:07:15 +00009184
Chris Lattnercee56e72009-03-13 05:53:31 +00009185 // Optimize cases that will turn into an LEA instruction. This requires
9186 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009187 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009188 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009189 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009190
Chris Lattnercee56e72009-03-13 05:53:31 +00009191 bool isFastMultiplier = false;
9192 if (Diff < 10) {
9193 switch ((unsigned char)Diff) {
9194 default: break;
9195 case 1: // result = add base, cond
9196 case 2: // result = lea base( , cond*2)
9197 case 3: // result = lea base(cond, cond*2)
9198 case 4: // result = lea base( , cond*4)
9199 case 5: // result = lea base(cond, cond*4)
9200 case 8: // result = lea base( , cond*8)
9201 case 9: // result = lea base(cond, cond*8)
9202 isFastMultiplier = true;
9203 break;
9204 }
9205 }
Eric Christopherfd179292009-08-27 18:07:15 +00009206
Chris Lattnercee56e72009-03-13 05:53:31 +00009207 if (isFastMultiplier) {
9208 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9209 if (NeedsCondInvert) // Invert the condition if needed.
9210 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9211 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009212
Chris Lattnercee56e72009-03-13 05:53:31 +00009213 // Zero extend the condition if needed.
9214 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9215 Cond);
9216 // Scale the condition by the difference.
9217 if (Diff != 1)
9218 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9219 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009220
Chris Lattnercee56e72009-03-13 05:53:31 +00009221 // Add the base if non-zero.
9222 if (FalseC->getAPIntValue() != 0)
9223 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9224 SDValue(FalseC, 0));
9225 return Cond;
9226 }
Eric Christopherfd179292009-08-27 18:07:15 +00009227 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009228 }
9229 }
Eric Christopherfd179292009-08-27 18:07:15 +00009230
Dan Gohman475871a2008-07-27 21:46:04 +00009231 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009232}
9233
Chris Lattnerd1980a52009-03-12 06:52:53 +00009234/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9235static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9236 TargetLowering::DAGCombinerInfo &DCI) {
9237 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009238
Chris Lattnerd1980a52009-03-12 06:52:53 +00009239 // If the flag operand isn't dead, don't touch this CMOV.
9240 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9241 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009242
Chris Lattnerd1980a52009-03-12 06:52:53 +00009243 // If this is a select between two integer constants, try to do some
9244 // optimizations. Note that the operands are ordered the opposite of SELECT
9245 // operands.
9246 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9247 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9248 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9249 // larger than FalseC (the false value).
9250 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009251
Chris Lattnerd1980a52009-03-12 06:52:53 +00009252 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9253 CC = X86::GetOppositeBranchCondition(CC);
9254 std::swap(TrueC, FalseC);
9255 }
Eric Christopherfd179292009-08-27 18:07:15 +00009256
Chris Lattnerd1980a52009-03-12 06:52:53 +00009257 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009258 // This is efficient for any integer data type (including i8/i16) and
9259 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009260 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9261 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009262 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9263 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009264
Chris Lattnerd1980a52009-03-12 06:52:53 +00009265 // Zero extend the condition if needed.
9266 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009267
Chris Lattnerd1980a52009-03-12 06:52:53 +00009268 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9269 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009270 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009271 if (N->getNumValues() == 2) // Dead flag value?
9272 return DCI.CombineTo(N, Cond, SDValue());
9273 return Cond;
9274 }
Eric Christopherfd179292009-08-27 18:07:15 +00009275
Chris Lattnercee56e72009-03-13 05:53:31 +00009276 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9277 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009278 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9279 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009280 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9281 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009282
Chris Lattner97a29a52009-03-13 05:22:11 +00009283 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009284 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9285 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009286 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9287 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009288
Chris Lattner97a29a52009-03-13 05:22:11 +00009289 if (N->getNumValues() == 2) // Dead flag value?
9290 return DCI.CombineTo(N, Cond, SDValue());
9291 return Cond;
9292 }
Eric Christopherfd179292009-08-27 18:07:15 +00009293
Chris Lattnercee56e72009-03-13 05:53:31 +00009294 // Optimize cases that will turn into an LEA instruction. This requires
9295 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009296 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009297 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009298 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009299
Chris Lattnercee56e72009-03-13 05:53:31 +00009300 bool isFastMultiplier = false;
9301 if (Diff < 10) {
9302 switch ((unsigned char)Diff) {
9303 default: break;
9304 case 1: // result = add base, cond
9305 case 2: // result = lea base( , cond*2)
9306 case 3: // result = lea base(cond, cond*2)
9307 case 4: // result = lea base( , cond*4)
9308 case 5: // result = lea base(cond, cond*4)
9309 case 8: // result = lea base( , cond*8)
9310 case 9: // result = lea base(cond, cond*8)
9311 isFastMultiplier = true;
9312 break;
9313 }
9314 }
Eric Christopherfd179292009-08-27 18:07:15 +00009315
Chris Lattnercee56e72009-03-13 05:53:31 +00009316 if (isFastMultiplier) {
9317 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9318 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009319 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9320 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009321 // Zero extend the condition if needed.
9322 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9323 Cond);
9324 // Scale the condition by the difference.
9325 if (Diff != 1)
9326 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9327 DAG.getConstant(Diff, Cond.getValueType()));
9328
9329 // Add the base if non-zero.
9330 if (FalseC->getAPIntValue() != 0)
9331 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9332 SDValue(FalseC, 0));
9333 if (N->getNumValues() == 2) // Dead flag value?
9334 return DCI.CombineTo(N, Cond, SDValue());
9335 return Cond;
9336 }
Eric Christopherfd179292009-08-27 18:07:15 +00009337 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009338 }
9339 }
9340 return SDValue();
9341}
9342
9343
Evan Cheng0b0cd912009-03-28 05:57:29 +00009344/// PerformMulCombine - Optimize a single multiply with constant into two
9345/// in order to implement it with two cheaper instructions, e.g.
9346/// LEA + SHL, LEA + LEA.
9347static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9348 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009349 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9350 return SDValue();
9351
Owen Andersone50ed302009-08-10 22:56:29 +00009352 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009353 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009354 return SDValue();
9355
9356 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9357 if (!C)
9358 return SDValue();
9359 uint64_t MulAmt = C->getZExtValue();
9360 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9361 return SDValue();
9362
9363 uint64_t MulAmt1 = 0;
9364 uint64_t MulAmt2 = 0;
9365 if ((MulAmt % 9) == 0) {
9366 MulAmt1 = 9;
9367 MulAmt2 = MulAmt / 9;
9368 } else if ((MulAmt % 5) == 0) {
9369 MulAmt1 = 5;
9370 MulAmt2 = MulAmt / 5;
9371 } else if ((MulAmt % 3) == 0) {
9372 MulAmt1 = 3;
9373 MulAmt2 = MulAmt / 3;
9374 }
9375 if (MulAmt2 &&
9376 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9377 DebugLoc DL = N->getDebugLoc();
9378
9379 if (isPowerOf2_64(MulAmt2) &&
9380 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9381 // If second multiplifer is pow2, issue it first. We want the multiply by
9382 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9383 // is an add.
9384 std::swap(MulAmt1, MulAmt2);
9385
9386 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009387 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009388 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009389 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009390 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009391 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009392 DAG.getConstant(MulAmt1, VT));
9393
Eric Christopherfd179292009-08-27 18:07:15 +00009394 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009395 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009396 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009397 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009398 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009399 DAG.getConstant(MulAmt2, VT));
9400
9401 // Do not add new nodes to DAG combiner worklist.
9402 DCI.CombineTo(N, NewMul, false);
9403 }
9404 return SDValue();
9405}
9406
Evan Chengad9c0a32009-12-15 00:53:42 +00009407static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9408 SDValue N0 = N->getOperand(0);
9409 SDValue N1 = N->getOperand(1);
9410 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9411 EVT VT = N0.getValueType();
9412
9413 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9414 // since the result of setcc_c is all zero's or all ones.
9415 if (N1C && N0.getOpcode() == ISD::AND &&
9416 N0.getOperand(1).getOpcode() == ISD::Constant) {
9417 SDValue N00 = N0.getOperand(0);
9418 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9419 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9420 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9421 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9422 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9423 APInt ShAmt = N1C->getAPIntValue();
9424 Mask = Mask.shl(ShAmt);
9425 if (Mask != 0)
9426 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9427 N00, DAG.getConstant(Mask, VT));
9428 }
9429 }
9430
9431 return SDValue();
9432}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009433
Nate Begeman740ab032009-01-26 00:52:55 +00009434/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9435/// when possible.
9436static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9437 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009438 EVT VT = N->getValueType(0);
9439 if (!VT.isVector() && VT.isInteger() &&
9440 N->getOpcode() == ISD::SHL)
9441 return PerformSHLCombine(N, DAG);
9442
Nate Begeman740ab032009-01-26 00:52:55 +00009443 // On X86 with SSE2 support, we can transform this to a vector shift if
9444 // all elements are shifted by the same amount. We can't do this in legalize
9445 // because the a constant vector is typically transformed to a constant pool
9446 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009447 if (!Subtarget->hasSSE2())
9448 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009449
Owen Anderson825b72b2009-08-11 20:47:22 +00009450 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009451 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009452
Mon P Wang3becd092009-01-28 08:12:05 +00009453 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009454 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009455 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009456 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009457 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9458 unsigned NumElts = VT.getVectorNumElements();
9459 unsigned i = 0;
9460 for (; i != NumElts; ++i) {
9461 SDValue Arg = ShAmtOp.getOperand(i);
9462 if (Arg.getOpcode() == ISD::UNDEF) continue;
9463 BaseShAmt = Arg;
9464 break;
9465 }
9466 for (; i != NumElts; ++i) {
9467 SDValue Arg = ShAmtOp.getOperand(i);
9468 if (Arg.getOpcode() == ISD::UNDEF) continue;
9469 if (Arg != BaseShAmt) {
9470 return SDValue();
9471 }
9472 }
9473 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009474 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009475 SDValue InVec = ShAmtOp.getOperand(0);
9476 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9477 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9478 unsigned i = 0;
9479 for (; i != NumElts; ++i) {
9480 SDValue Arg = InVec.getOperand(i);
9481 if (Arg.getOpcode() == ISD::UNDEF) continue;
9482 BaseShAmt = Arg;
9483 break;
9484 }
9485 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9486 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009487 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009488 if (C->getZExtValue() == SplatIdx)
9489 BaseShAmt = InVec.getOperand(1);
9490 }
9491 }
9492 if (BaseShAmt.getNode() == 0)
9493 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9494 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009495 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009496 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009497
Mon P Wangefa42202009-09-03 19:56:25 +00009498 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009499 if (EltVT.bitsGT(MVT::i32))
9500 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9501 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009502 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009503
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009504 // The shift amount is identical so we can do a vector shift.
9505 SDValue ValOp = N->getOperand(0);
9506 switch (N->getOpcode()) {
9507 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009508 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009509 break;
9510 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009511 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009512 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009513 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009514 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009515 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009516 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009517 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009518 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009519 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009520 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009521 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009522 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009523 break;
9524 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009525 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009526 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009527 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009528 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009529 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009530 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009531 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009532 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009533 break;
9534 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009535 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009536 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009537 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009538 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009539 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009540 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009541 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009542 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009543 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009544 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009545 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009546 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009547 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009548 }
9549 return SDValue();
9550}
9551
Evan Cheng760d1942010-01-04 21:22:48 +00009552static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9553 const X86Subtarget *Subtarget) {
9554 EVT VT = N->getValueType(0);
9555 if (VT != MVT::i64 || !Subtarget->is64Bit())
9556 return SDValue();
9557
9558 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9559 SDValue N0 = N->getOperand(0);
9560 SDValue N1 = N->getOperand(1);
9561 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9562 std::swap(N0, N1);
9563 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9564 return SDValue();
9565
9566 SDValue ShAmt0 = N0.getOperand(1);
9567 if (ShAmt0.getValueType() != MVT::i8)
9568 return SDValue();
9569 SDValue ShAmt1 = N1.getOperand(1);
9570 if (ShAmt1.getValueType() != MVT::i8)
9571 return SDValue();
9572 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9573 ShAmt0 = ShAmt0.getOperand(0);
9574 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9575 ShAmt1 = ShAmt1.getOperand(0);
9576
9577 DebugLoc DL = N->getDebugLoc();
9578 unsigned Opc = X86ISD::SHLD;
9579 SDValue Op0 = N0.getOperand(0);
9580 SDValue Op1 = N1.getOperand(0);
9581 if (ShAmt0.getOpcode() == ISD::SUB) {
9582 Opc = X86ISD::SHRD;
9583 std::swap(Op0, Op1);
9584 std::swap(ShAmt0, ShAmt1);
9585 }
9586
9587 if (ShAmt1.getOpcode() == ISD::SUB) {
9588 SDValue Sum = ShAmt1.getOperand(0);
9589 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9590 if (SumC->getSExtValue() == 64 &&
9591 ShAmt1.getOperand(1) == ShAmt0)
9592 return DAG.getNode(Opc, DL, VT,
9593 Op0, Op1,
9594 DAG.getNode(ISD::TRUNCATE, DL,
9595 MVT::i8, ShAmt0));
9596 }
9597 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9598 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9599 if (ShAmt0C &&
9600 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9601 return DAG.getNode(Opc, DL, VT,
9602 N0.getOperand(0), N1.getOperand(0),
9603 DAG.getNode(ISD::TRUNCATE, DL,
9604 MVT::i8, ShAmt0));
9605 }
9606
9607 return SDValue();
9608}
9609
Chris Lattner149a4e52008-02-22 02:09:43 +00009610/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009611static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009612 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009613 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9614 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009615 // A preferable solution to the general problem is to figure out the right
9616 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009617
9618 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009619 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009620 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009621 if (VT.getSizeInBits() != 64)
9622 return SDValue();
9623
Devang Patel578efa92009-06-05 21:57:13 +00009624 const Function *F = DAG.getMachineFunction().getFunction();
9625 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009626 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009627 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009628 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009629 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009630 isa<LoadSDNode>(St->getValue()) &&
9631 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9632 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009633 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009634 LoadSDNode *Ld = 0;
9635 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009636 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009637 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009638 // Must be a store of a load. We currently handle two cases: the load
9639 // is a direct child, and it's under an intervening TokenFactor. It is
9640 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009641 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009642 Ld = cast<LoadSDNode>(St->getChain());
9643 else if (St->getValue().hasOneUse() &&
9644 ChainVal->getOpcode() == ISD::TokenFactor) {
9645 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009646 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009647 TokenFactorIndex = i;
9648 Ld = cast<LoadSDNode>(St->getValue());
9649 } else
9650 Ops.push_back(ChainVal->getOperand(i));
9651 }
9652 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009653
Evan Cheng536e6672009-03-12 05:59:15 +00009654 if (!Ld || !ISD::isNormalLoad(Ld))
9655 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009656
Evan Cheng536e6672009-03-12 05:59:15 +00009657 // If this is not the MMX case, i.e. we are just turning i64 load/store
9658 // into f64 load/store, avoid the transformation if there are multiple
9659 // uses of the loaded value.
9660 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9661 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009662
Evan Cheng536e6672009-03-12 05:59:15 +00009663 DebugLoc LdDL = Ld->getDebugLoc();
9664 DebugLoc StDL = N->getDebugLoc();
9665 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9666 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9667 // pair instead.
9668 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009669 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009670 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9671 Ld->getBasePtr(), Ld->getSrcValue(),
9672 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009673 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009674 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009675 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009676 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009677 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009678 Ops.size());
9679 }
Evan Cheng536e6672009-03-12 05:59:15 +00009680 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009681 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009682 St->isVolatile(), St->isNonTemporal(),
9683 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009684 }
Evan Cheng536e6672009-03-12 05:59:15 +00009685
9686 // Otherwise, lower to two pairs of 32-bit loads / stores.
9687 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009688 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9689 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009690
Owen Anderson825b72b2009-08-11 20:47:22 +00009691 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009692 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009693 Ld->isVolatile(), Ld->isNonTemporal(),
9694 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009695 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009696 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009697 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009698 MinAlign(Ld->getAlignment(), 4));
9699
9700 SDValue NewChain = LoLd.getValue(1);
9701 if (TokenFactorIndex != -1) {
9702 Ops.push_back(LoLd);
9703 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009704 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009705 Ops.size());
9706 }
9707
9708 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009709 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9710 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009711
9712 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9713 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009714 St->isVolatile(), St->isNonTemporal(),
9715 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009716 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9717 St->getSrcValue(),
9718 St->getSrcValueOffset() + 4,
9719 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009720 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009721 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009722 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009723 }
Dan Gohman475871a2008-07-27 21:46:04 +00009724 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009725}
9726
Chris Lattner6cf73262008-01-25 06:14:17 +00009727/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9728/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009729static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009730 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9731 // F[X]OR(0.0, x) -> x
9732 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009733 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9734 if (C->getValueAPF().isPosZero())
9735 return N->getOperand(1);
9736 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9737 if (C->getValueAPF().isPosZero())
9738 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009739 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009740}
9741
9742/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009743static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009744 // FAND(0.0, x) -> 0.0
9745 // FAND(x, 0.0) -> 0.0
9746 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9747 if (C->getValueAPF().isPosZero())
9748 return N->getOperand(0);
9749 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9750 if (C->getValueAPF().isPosZero())
9751 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009752 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009753}
9754
Dan Gohmane5af2d32009-01-29 01:59:02 +00009755static SDValue PerformBTCombine(SDNode *N,
9756 SelectionDAG &DAG,
9757 TargetLowering::DAGCombinerInfo &DCI) {
9758 // BT ignores high bits in the bit index operand.
9759 SDValue Op1 = N->getOperand(1);
9760 if (Op1.hasOneUse()) {
9761 unsigned BitWidth = Op1.getValueSizeInBits();
9762 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9763 APInt KnownZero, KnownOne;
9764 TargetLowering::TargetLoweringOpt TLO(DAG);
9765 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9766 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9767 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9768 DCI.CommitTargetLoweringOpt(TLO);
9769 }
9770 return SDValue();
9771}
Chris Lattner83e6c992006-10-04 06:57:07 +00009772
Eli Friedman7a5e5552009-06-07 06:52:44 +00009773static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9774 SDValue Op = N->getOperand(0);
9775 if (Op.getOpcode() == ISD::BIT_CONVERT)
9776 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009777 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009778 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009779 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009780 OpVT.getVectorElementType().getSizeInBits()) {
9781 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9782 }
9783 return SDValue();
9784}
9785
Owen Anderson99177002009-06-29 18:04:45 +00009786// On X86 and X86-64, atomic operations are lowered to locked instructions.
9787// Locked instructions, in turn, have implicit fence semantics (all memory
9788// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009789// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009790// fence-atomic-fence.
9791static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9792 SDValue atomic = N->getOperand(0);
9793 switch (atomic.getOpcode()) {
9794 case ISD::ATOMIC_CMP_SWAP:
9795 case ISD::ATOMIC_SWAP:
9796 case ISD::ATOMIC_LOAD_ADD:
9797 case ISD::ATOMIC_LOAD_SUB:
9798 case ISD::ATOMIC_LOAD_AND:
9799 case ISD::ATOMIC_LOAD_OR:
9800 case ISD::ATOMIC_LOAD_XOR:
9801 case ISD::ATOMIC_LOAD_NAND:
9802 case ISD::ATOMIC_LOAD_MIN:
9803 case ISD::ATOMIC_LOAD_MAX:
9804 case ISD::ATOMIC_LOAD_UMIN:
9805 case ISD::ATOMIC_LOAD_UMAX:
9806 break;
9807 default:
9808 return SDValue();
9809 }
Eric Christopherfd179292009-08-27 18:07:15 +00009810
Owen Anderson99177002009-06-29 18:04:45 +00009811 SDValue fence = atomic.getOperand(0);
9812 if (fence.getOpcode() != ISD::MEMBARRIER)
9813 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009814
Owen Anderson99177002009-06-29 18:04:45 +00009815 switch (atomic.getOpcode()) {
9816 case ISD::ATOMIC_CMP_SWAP:
9817 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9818 atomic.getOperand(1), atomic.getOperand(2),
9819 atomic.getOperand(3));
9820 case ISD::ATOMIC_SWAP:
9821 case ISD::ATOMIC_LOAD_ADD:
9822 case ISD::ATOMIC_LOAD_SUB:
9823 case ISD::ATOMIC_LOAD_AND:
9824 case ISD::ATOMIC_LOAD_OR:
9825 case ISD::ATOMIC_LOAD_XOR:
9826 case ISD::ATOMIC_LOAD_NAND:
9827 case ISD::ATOMIC_LOAD_MIN:
9828 case ISD::ATOMIC_LOAD_MAX:
9829 case ISD::ATOMIC_LOAD_UMIN:
9830 case ISD::ATOMIC_LOAD_UMAX:
9831 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9832 atomic.getOperand(1), atomic.getOperand(2));
9833 default:
9834 return SDValue();
9835 }
9836}
9837
Evan Cheng2e489c42009-12-16 00:53:11 +00009838static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9839 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9840 // (and (i32 x86isd::setcc_carry), 1)
9841 // This eliminates the zext. This transformation is necessary because
9842 // ISD::SETCC is always legalized to i8.
9843 DebugLoc dl = N->getDebugLoc();
9844 SDValue N0 = N->getOperand(0);
9845 EVT VT = N->getValueType(0);
9846 if (N0.getOpcode() == ISD::AND &&
9847 N0.hasOneUse() &&
9848 N0.getOperand(0).hasOneUse()) {
9849 SDValue N00 = N0.getOperand(0);
9850 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9851 return SDValue();
9852 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9853 if (!C || C->getZExtValue() != 1)
9854 return SDValue();
9855 return DAG.getNode(ISD::AND, dl, VT,
9856 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9857 N00.getOperand(0), N00.getOperand(1)),
9858 DAG.getConstant(1, VT));
9859 }
9860
9861 return SDValue();
9862}
9863
Dan Gohman475871a2008-07-27 21:46:04 +00009864SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009865 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009866 SelectionDAG &DAG = DCI.DAG;
9867 switch (N->getOpcode()) {
9868 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009869 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009870 case ISD::EXTRACT_VECTOR_ELT:
9871 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009872 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009873 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009874 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009875 case ISD::SHL:
9876 case ISD::SRA:
9877 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009878 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009879 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009880 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009881 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9882 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009883 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009884 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009885 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009886 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009887 }
9888
Dan Gohman475871a2008-07-27 21:46:04 +00009889 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009890}
9891
Evan Cheng60c07e12006-07-05 22:17:51 +00009892//===----------------------------------------------------------------------===//
9893// X86 Inline Assembly Support
9894//===----------------------------------------------------------------------===//
9895
Chris Lattnerb8105652009-07-20 17:51:36 +00009896static bool LowerToBSwap(CallInst *CI) {
9897 // FIXME: this should verify that we are targetting a 486 or better. If not,
9898 // we will turn this bswap into something that will be lowered to logical ops
9899 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9900 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009901
Chris Lattnerb8105652009-07-20 17:51:36 +00009902 // Verify this is a simple bswap.
9903 if (CI->getNumOperands() != 2 ||
9904 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009905 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +00009906 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009907
Chris Lattnerb8105652009-07-20 17:51:36 +00009908 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9909 if (!Ty || Ty->getBitWidth() % 16 != 0)
9910 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009911
Chris Lattnerb8105652009-07-20 17:51:36 +00009912 // Okay, we can do this xform, do so now.
9913 const Type *Tys[] = { Ty };
9914 Module *M = CI->getParent()->getParent()->getParent();
9915 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009916
Chris Lattnerb8105652009-07-20 17:51:36 +00009917 Value *Op = CI->getOperand(1);
9918 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009919
Chris Lattnerb8105652009-07-20 17:51:36 +00009920 CI->replaceAllUsesWith(Op);
9921 CI->eraseFromParent();
9922 return true;
9923}
9924
9925bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9926 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9927 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9928
9929 std::string AsmStr = IA->getAsmString();
9930
9931 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009932 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009933 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9934
9935 switch (AsmPieces.size()) {
9936 default: return false;
9937 case 1:
9938 AsmStr = AsmPieces[0];
9939 AsmPieces.clear();
9940 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9941
9942 // bswap $0
9943 if (AsmPieces.size() == 2 &&
9944 (AsmPieces[0] == "bswap" ||
9945 AsmPieces[0] == "bswapq" ||
9946 AsmPieces[0] == "bswapl") &&
9947 (AsmPieces[1] == "$0" ||
9948 AsmPieces[1] == "${0:q}")) {
9949 // No need to check constraints, nothing other than the equivalent of
9950 // "=r,0" would be valid here.
9951 return LowerToBSwap(CI);
9952 }
9953 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009954 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009955 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009956 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009957 AsmPieces[1] == "$$8," &&
9958 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009959 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9960 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +00009961 const std::string &Constraints = IA->getConstraintString();
9962 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +00009963 std::sort(AsmPieces.begin(), AsmPieces.end());
9964 if (AsmPieces.size() == 4 &&
9965 AsmPieces[0] == "~{cc}" &&
9966 AsmPieces[1] == "~{dirflag}" &&
9967 AsmPieces[2] == "~{flags}" &&
9968 AsmPieces[3] == "~{fpsr}") {
9969 return LowerToBSwap(CI);
9970 }
Chris Lattnerb8105652009-07-20 17:51:36 +00009971 }
9972 break;
9973 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009974 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009975 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009976 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9977 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9978 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009979 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009980 SplitString(AsmPieces[0], Words, " \t");
9981 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9982 Words.clear();
9983 SplitString(AsmPieces[1], Words, " \t");
9984 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9985 Words.clear();
9986 SplitString(AsmPieces[2], Words, " \t,");
9987 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9988 Words[2] == "%edx") {
9989 return LowerToBSwap(CI);
9990 }
9991 }
9992 }
9993 }
9994 break;
9995 }
9996 return false;
9997}
9998
9999
10000
Chris Lattnerf4dff842006-07-11 02:54:03 +000010001/// getConstraintType - Given a constraint letter, return the type of
10002/// constraint it is for this target.
10003X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010004X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10005 if (Constraint.size() == 1) {
10006 switch (Constraint[0]) {
10007 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010008 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010009 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010010 case 'r':
10011 case 'R':
10012 case 'l':
10013 case 'q':
10014 case 'Q':
10015 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010016 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010017 case 'Y':
10018 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010019 case 'e':
10020 case 'Z':
10021 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010022 default:
10023 break;
10024 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010025 }
Chris Lattner4234f572007-03-25 02:14:49 +000010026 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010027}
10028
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010029/// LowerXConstraint - try to replace an X constraint, which matches anything,
10030/// with another that has more specific requirements based on the type of the
10031/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010032const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010033LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010034 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10035 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010036 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010037 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010038 return "Y";
10039 if (Subtarget->hasSSE1())
10040 return "x";
10041 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010042
Chris Lattner5e764232008-04-26 23:02:14 +000010043 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010044}
10045
Chris Lattner48884cd2007-08-25 00:47:38 +000010046/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10047/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010048void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010049 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +000010050 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +000010051 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010052 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010053 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010054
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010055 switch (Constraint) {
10056 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010057 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010058 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010059 if (C->getZExtValue() <= 31) {
10060 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010061 break;
10062 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010063 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010064 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010065 case 'J':
10066 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010067 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010068 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10069 break;
10070 }
10071 }
10072 return;
10073 case 'K':
10074 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010075 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010076 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10077 break;
10078 }
10079 }
10080 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010081 case 'N':
10082 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010083 if (C->getZExtValue() <= 255) {
10084 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010085 break;
10086 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010087 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010088 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010089 case 'e': {
10090 // 32-bit signed value
10091 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10092 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010093 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10094 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010095 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010096 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010097 break;
10098 }
10099 // FIXME gcc accepts some relocatable values here too, but only in certain
10100 // memory models; it's complicated.
10101 }
10102 return;
10103 }
10104 case 'Z': {
10105 // 32-bit unsigned value
10106 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10107 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010108 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10109 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010110 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10111 break;
10112 }
10113 }
10114 // FIXME gcc accepts some relocatable values here too, but only in certain
10115 // memory models; it's complicated.
10116 return;
10117 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010118 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010119 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010120 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010121 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010122 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010123 break;
10124 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010125
Chris Lattnerdc43a882007-05-03 16:52:29 +000010126 // If we are in non-pic codegen mode, we allow the address of a global (with
10127 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010128 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010129 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010130
Chris Lattner49921962009-05-08 18:23:14 +000010131 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10132 while (1) {
10133 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10134 Offset += GA->getOffset();
10135 break;
10136 } else if (Op.getOpcode() == ISD::ADD) {
10137 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10138 Offset += C->getZExtValue();
10139 Op = Op.getOperand(0);
10140 continue;
10141 }
10142 } else if (Op.getOpcode() == ISD::SUB) {
10143 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10144 Offset += -C->getZExtValue();
10145 Op = Op.getOperand(0);
10146 continue;
10147 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010148 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010149
Chris Lattner49921962009-05-08 18:23:14 +000010150 // Otherwise, this isn't something we can handle, reject it.
10151 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010152 }
Eric Christopherfd179292009-08-27 18:07:15 +000010153
Chris Lattner36c25012009-07-10 07:34:39 +000010154 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010155 // If we require an extra load to get this address, as in PIC mode, we
10156 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010157 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10158 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010159 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010160
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010161 if (hasMemory)
10162 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10163 else
10164 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010165 Result = Op;
10166 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010167 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010168 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010169
Gabor Greifba36cb52008-08-28 21:40:38 +000010170 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010171 Ops.push_back(Result);
10172 return;
10173 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010174 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10175 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010176}
10177
Chris Lattner259e97c2006-01-31 19:43:35 +000010178std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010179getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010180 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010181 if (Constraint.size() == 1) {
10182 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010183 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010184 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010185 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10186 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010187 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010188 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10189 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10190 X86::R10D,X86::R11D,X86::R12D,
10191 X86::R13D,X86::R14D,X86::R15D,
10192 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010193 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010194 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10195 X86::SI, X86::DI, X86::R8W,X86::R9W,
10196 X86::R10W,X86::R11W,X86::R12W,
10197 X86::R13W,X86::R14W,X86::R15W,
10198 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010199 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010200 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10201 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10202 X86::R10B,X86::R11B,X86::R12B,
10203 X86::R13B,X86::R14B,X86::R15B,
10204 X86::BPL, X86::SPL, 0);
10205
Owen Anderson825b72b2009-08-11 20:47:22 +000010206 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010207 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10208 X86::RSI, X86::RDI, X86::R8, X86::R9,
10209 X86::R10, X86::R11, X86::R12,
10210 X86::R13, X86::R14, X86::R15,
10211 X86::RBP, X86::RSP, 0);
10212
10213 break;
10214 }
Eric Christopherfd179292009-08-27 18:07:15 +000010215 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010216 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010217 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010218 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010219 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010220 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010221 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010222 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010223 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010224 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10225 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010226 }
10227 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010228
Chris Lattner1efa40f2006-02-22 00:56:39 +000010229 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010230}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010231
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010232std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010233X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010234 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010235 // First, see if this is a constraint that directly corresponds to an LLVM
10236 // register class.
10237 if (Constraint.size() == 1) {
10238 // GCC Constraint Letters
10239 switch (Constraint[0]) {
10240 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010241 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010242 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010243 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010244 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010245 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010246 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010247 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010248 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010249 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010250 case 'R': // LEGACY_REGS
10251 if (VT == MVT::i8)
10252 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10253 if (VT == MVT::i16)
10254 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10255 if (VT == MVT::i32 || !Subtarget->is64Bit())
10256 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10257 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010258 case 'f': // FP Stack registers.
10259 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10260 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010261 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010262 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010263 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010264 return std::make_pair(0U, X86::RFP64RegisterClass);
10265 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010266 case 'y': // MMX_REGS if MMX allowed.
10267 if (!Subtarget->hasMMX()) break;
10268 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010269 case 'Y': // SSE_REGS if SSE2 allowed
10270 if (!Subtarget->hasSSE2()) break;
10271 // FALL THROUGH.
10272 case 'x': // SSE_REGS if SSE1 allowed
10273 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010274
Owen Anderson825b72b2009-08-11 20:47:22 +000010275 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010276 default: break;
10277 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010278 case MVT::f32:
10279 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010280 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010281 case MVT::f64:
10282 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010283 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010284 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010285 case MVT::v16i8:
10286 case MVT::v8i16:
10287 case MVT::v4i32:
10288 case MVT::v2i64:
10289 case MVT::v4f32:
10290 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010291 return std::make_pair(0U, X86::VR128RegisterClass);
10292 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010293 break;
10294 }
10295 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010296
Chris Lattnerf76d1802006-07-31 23:26:50 +000010297 // Use the default implementation in TargetLowering to convert the register
10298 // constraint into a member of a register class.
10299 std::pair<unsigned, const TargetRegisterClass*> Res;
10300 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010301
10302 // Not found as a standard register?
10303 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010304 // Map st(0) -> st(7) -> ST0
10305 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10306 tolower(Constraint[1]) == 's' &&
10307 tolower(Constraint[2]) == 't' &&
10308 Constraint[3] == '(' &&
10309 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10310 Constraint[5] == ')' &&
10311 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010312
Chris Lattner56d77c72009-09-13 22:41:48 +000010313 Res.first = X86::ST0+Constraint[4]-'0';
10314 Res.second = X86::RFP80RegisterClass;
10315 return Res;
10316 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010317
Chris Lattner56d77c72009-09-13 22:41:48 +000010318 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010319 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010320 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010321 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010322 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010323 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010324
10325 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010326 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010327 Res.first = X86::EFLAGS;
10328 Res.second = X86::CCRRegisterClass;
10329 return Res;
10330 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010331
Dale Johannesen330169f2008-11-13 21:52:36 +000010332 // 'A' means EAX + EDX.
10333 if (Constraint == "A") {
10334 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010335 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010336 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010337 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010338 return Res;
10339 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010340
Chris Lattnerf76d1802006-07-31 23:26:50 +000010341 // Otherwise, check to see if this is a register class of the wrong value
10342 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10343 // turn into {ax},{dx}.
10344 if (Res.second->hasType(VT))
10345 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010346
Chris Lattnerf76d1802006-07-31 23:26:50 +000010347 // All of the single-register GCC register classes map their values onto
10348 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10349 // really want an 8-bit or 32-bit register, map to the appropriate register
10350 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010351 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010352 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010353 unsigned DestReg = 0;
10354 switch (Res.first) {
10355 default: break;
10356 case X86::AX: DestReg = X86::AL; break;
10357 case X86::DX: DestReg = X86::DL; break;
10358 case X86::CX: DestReg = X86::CL; break;
10359 case X86::BX: DestReg = X86::BL; break;
10360 }
10361 if (DestReg) {
10362 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010363 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010364 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010365 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010366 unsigned DestReg = 0;
10367 switch (Res.first) {
10368 default: break;
10369 case X86::AX: DestReg = X86::EAX; break;
10370 case X86::DX: DestReg = X86::EDX; break;
10371 case X86::CX: DestReg = X86::ECX; break;
10372 case X86::BX: DestReg = X86::EBX; break;
10373 case X86::SI: DestReg = X86::ESI; break;
10374 case X86::DI: DestReg = X86::EDI; break;
10375 case X86::BP: DestReg = X86::EBP; break;
10376 case X86::SP: DestReg = X86::ESP; break;
10377 }
10378 if (DestReg) {
10379 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010380 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010381 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010382 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010383 unsigned DestReg = 0;
10384 switch (Res.first) {
10385 default: break;
10386 case X86::AX: DestReg = X86::RAX; break;
10387 case X86::DX: DestReg = X86::RDX; break;
10388 case X86::CX: DestReg = X86::RCX; break;
10389 case X86::BX: DestReg = X86::RBX; break;
10390 case X86::SI: DestReg = X86::RSI; break;
10391 case X86::DI: DestReg = X86::RDI; break;
10392 case X86::BP: DestReg = X86::RBP; break;
10393 case X86::SP: DestReg = X86::RSP; break;
10394 }
10395 if (DestReg) {
10396 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010397 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010398 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010399 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010400 } else if (Res.second == X86::FR32RegisterClass ||
10401 Res.second == X86::FR64RegisterClass ||
10402 Res.second == X86::VR128RegisterClass) {
10403 // Handle references to XMM physical registers that got mapped into the
10404 // wrong class. This can happen with constraints like {xmm0} where the
10405 // target independent register mapper will just pick the first match it can
10406 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010407 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010408 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010409 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010410 Res.second = X86::FR64RegisterClass;
10411 else if (X86::VR128RegisterClass->hasType(VT))
10412 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010413 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010414
Chris Lattnerf76d1802006-07-31 23:26:50 +000010415 return Res;
10416}