blob: a47f4ca33bd2114abd382a966f772f6cf9890970 [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()) {
Dale Johannesen76090172010-04-20 22:34:09 +0000626 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass, false);
627 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass, false);
628 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass, false);
629 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass, false);
630 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass, false);
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.
Owen Anderson825b72b2009-08-11 20:47:22 +0000797 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
798 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000799 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000800
801 // Do not attempt to promote non-128-bit vectors
802 if (!VT.is128BitVector()) {
803 continue;
804 }
Eric Christopher4bd24c22010-03-30 01:04:59 +0000805
Owen Andersond6662ad2009-08-10 20:46:15 +0000806 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000808 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000810 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000812 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000814 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000816 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000817
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000819
Evan Cheng2c3ae372006-04-12 21:21:57 +0000820 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
822 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
823 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
824 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000825
Owen Anderson825b72b2009-08-11 20:47:22 +0000826 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
827 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000828 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
830 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000831 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000832 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000833
Nate Begeman14d12ca2008-02-11 04:19:36 +0000834 if (Subtarget->hasSSE41()) {
835 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000836 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000837
838 // i8 and i16 vectors are custom , because the source register and source
839 // source memory operand types are not the same width. f32 vectors are
840 // custom since the immediate controlling the insert encodes additional
841 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
843 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000846
Owen Anderson825b72b2009-08-11 20:47:22 +0000847 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
848 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
849 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
850 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000851
852 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
854 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000855 }
856 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000857
Nate Begeman30a0de92008-07-17 16:51:19 +0000858 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000860 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000861
David Greene9b9838d2009-06-29 16:47:10 +0000862 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
864 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
865 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
866 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000867
Owen Anderson825b72b2009-08-11 20:47:22 +0000868 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
869 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
870 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
871 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
872 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
873 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
874 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
875 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
876 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
877 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
878 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
879 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
880 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
881 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
882 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000883
884 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
886 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
887 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
888 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
889 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
890 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
891 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
892 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
893 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
894 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
895 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
896 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
897 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
898 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000899
Owen Anderson825b72b2009-08-11 20:47:22 +0000900 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
901 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
902 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
903 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000904
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
906 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
907 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
908 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
909 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000910
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
912 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
913 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
914 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
915 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
916 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000917
918#if 0
919 // Not sure we want to do this since there are no 256-bit integer
920 // operations in AVX
921
922 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
923 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
925 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000926
927 // Do not attempt to custom lower non-power-of-2 vectors
928 if (!isPowerOf2_32(VT.getVectorNumElements()))
929 continue;
930
931 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
932 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
933 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
934 }
935
936 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000937 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
938 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000939 }
David Greene9b9838d2009-06-29 16:47:10 +0000940#endif
941
942#if 0
943 // Not sure we want to do this since there are no 256-bit integer
944 // operations in AVX
945
946 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
947 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000948 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
949 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000950
951 if (!VT.is256BitVector()) {
952 continue;
953 }
954 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000955 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000956 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000958 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000960 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000962 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000964 }
965
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000967#endif
968 }
969
Evan Cheng6be2c582006-04-05 23:38:46 +0000970 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000971 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000972
Bill Wendling74c37652008-12-09 22:08:41 +0000973 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::SADDO, MVT::i32, Custom);
975 setOperationAction(ISD::SADDO, MVT::i64, Custom);
976 setOperationAction(ISD::UADDO, MVT::i32, Custom);
977 setOperationAction(ISD::UADDO, MVT::i64, Custom);
978 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
979 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
980 setOperationAction(ISD::USUBO, MVT::i32, Custom);
981 setOperationAction(ISD::USUBO, MVT::i64, Custom);
982 setOperationAction(ISD::SMULO, MVT::i32, Custom);
983 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000984
Evan Chengd54f2d52009-03-31 19:38:51 +0000985 if (!Subtarget->is64Bit()) {
986 // These libcalls are not available in 32-bit.
987 setLibcallName(RTLIB::SHL_I128, 0);
988 setLibcallName(RTLIB::SRL_I128, 0);
989 setLibcallName(RTLIB::SRA_I128, 0);
990 }
991
Evan Cheng206ee9d2006-07-07 08:33:52 +0000992 // We have target-specific dag combine patterns for the following nodes:
993 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000994 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000995 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000996 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000997 setTargetDAGCombine(ISD::SHL);
998 setTargetDAGCombine(ISD::SRA);
999 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001000 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +00001001 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +00001002 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +00001003 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001004 if (Subtarget->is64Bit())
1005 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001006
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001007 computeRegisterProperties();
1008
Evan Cheng87ed7162006-02-14 08:25:08 +00001009 // FIXME: These should be based on subtarget info. Plus, the values should
1010 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001011 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +00001012 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +00001013 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001014 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001015 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001016}
1017
Scott Michel5b8f82e2008-03-10 15:42:14 +00001018
Owen Anderson825b72b2009-08-11 20:47:22 +00001019MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1020 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001021}
1022
1023
Evan Cheng29286502008-01-23 23:17:41 +00001024/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1025/// the desired ByVal argument alignment.
1026static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1027 if (MaxAlign == 16)
1028 return;
1029 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1030 if (VTy->getBitWidth() == 128)
1031 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001032 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1033 unsigned EltAlign = 0;
1034 getMaxByValAlign(ATy->getElementType(), EltAlign);
1035 if (EltAlign > MaxAlign)
1036 MaxAlign = EltAlign;
1037 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1038 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1039 unsigned EltAlign = 0;
1040 getMaxByValAlign(STy->getElementType(i), EltAlign);
1041 if (EltAlign > MaxAlign)
1042 MaxAlign = EltAlign;
1043 if (MaxAlign == 16)
1044 break;
1045 }
1046 }
1047 return;
1048}
1049
1050/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1051/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001052/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1053/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001054unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001055 if (Subtarget->is64Bit()) {
1056 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001057 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001058 if (TyAlign > 8)
1059 return TyAlign;
1060 return 8;
1061 }
1062
Evan Cheng29286502008-01-23 23:17:41 +00001063 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001064 if (Subtarget->hasSSE1())
1065 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001066 return Align;
1067}
Chris Lattner2b02a442007-02-25 08:29:00 +00001068
Evan Chengf0df0312008-05-15 08:39:06 +00001069/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001070/// and store operations as a result of memset, memcpy, and memmove
1071/// lowering. If DstAlign is zero that means it's safe to destination
1072/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1073/// means there isn't a need to check it against alignment requirement,
1074/// probably because the source does not need to be loaded. If
1075/// 'NonScalarIntSafe' is true, that means it's safe to return a
1076/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1077/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1078/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001079/// It returns EVT::Other if the type should be determined using generic
1080/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001081EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001082X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1083 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001084 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001085 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001086 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001087 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1088 // linux. This is because the stack realignment code can't handle certain
1089 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001090 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001091 if (NonScalarIntSafe &&
1092 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001093 if (Size >= 16 &&
1094 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001095 ((DstAlign == 0 || DstAlign >= 16) &&
1096 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001097 Subtarget->getStackAlignment() >= 16) {
1098 if (Subtarget->hasSSE2())
1099 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001100 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001101 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001102 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001103 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001104 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001105 Subtarget->hasSSE2()) {
1106 // Do not use f64 to lower memcpy if source is string constant. It's
1107 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001108 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001109 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001110 }
Evan Chengf0df0312008-05-15 08:39:06 +00001111 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001112 return MVT::i64;
1113 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001114}
1115
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001116/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1117/// current function. The returned value is a member of the
1118/// MachineJumpTableInfo::JTEntryKind enum.
1119unsigned X86TargetLowering::getJumpTableEncoding() const {
1120 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1121 // symbol.
1122 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1123 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001124 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001125
1126 // Otherwise, use the normal jump table encoding heuristics.
1127 return TargetLowering::getJumpTableEncoding();
1128}
1129
Chris Lattner589c6f62010-01-26 06:28:43 +00001130/// getPICBaseSymbol - Return the X86-32 PIC base.
1131MCSymbol *
1132X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1133 MCContext &Ctx) const {
1134 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001135 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1136 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001137}
1138
1139
Chris Lattnerc64daab2010-01-26 05:02:42 +00001140const MCExpr *
1141X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1142 const MachineBasicBlock *MBB,
1143 unsigned uid,MCContext &Ctx) const{
1144 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1145 Subtarget->isPICStyleGOT());
1146 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1147 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001148 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1149 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001150}
1151
Evan Chengcc415862007-11-09 01:32:10 +00001152/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1153/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001154SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001155 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001156 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001157 // This doesn't have DebugLoc associated with it, but is not really the
1158 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001159 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001160 return Table;
1161}
1162
Chris Lattner589c6f62010-01-26 06:28:43 +00001163/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1164/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1165/// MCExpr.
1166const MCExpr *X86TargetLowering::
1167getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1168 MCContext &Ctx) const {
1169 // X86-64 uses RIP relative addressing based on the jump table label.
1170 if (Subtarget->isPICStyleRIPRel())
1171 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1172
1173 // Otherwise, the reference is relative to the PIC base.
1174 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1175}
1176
Bill Wendlingb4202b82009-07-01 18:50:55 +00001177/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001178unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001179 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001180}
1181
Chris Lattner2b02a442007-02-25 08:29:00 +00001182//===----------------------------------------------------------------------===//
1183// Return Value Calling Convention Implementation
1184//===----------------------------------------------------------------------===//
1185
Chris Lattner59ed56b2007-02-28 04:55:35 +00001186#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001187
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001188bool
1189X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1190 const SmallVectorImpl<EVT> &OutTys,
1191 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
Dan Gohmand858e902010-04-17 15:26:15 +00001192 SelectionDAG &DAG) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001193 SmallVector<CCValAssign, 16> RVLocs;
1194 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1195 RVLocs, *DAG.getContext());
1196 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1197}
1198
Dan Gohman98ca4f22009-08-05 01:29:28 +00001199SDValue
1200X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001201 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001203 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001204 MachineFunction &MF = DAG.getMachineFunction();
1205 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001206
Chris Lattner9774c912007-02-27 05:28:59 +00001207 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001208 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1209 RVLocs, *DAG.getContext());
1210 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001211
Evan Chengdcea1632010-02-04 02:40:39 +00001212 // Add the regs to the liveout set for the function.
1213 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1214 for (unsigned i = 0; i != RVLocs.size(); ++i)
1215 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1216 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001217
Dan Gohman475871a2008-07-27 21:46:04 +00001218 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001219
Dan Gohman475871a2008-07-27 21:46:04 +00001220 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001221 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1222 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001223 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1224 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001225
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001226 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001227 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1228 CCValAssign &VA = RVLocs[i];
1229 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001230 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001231
Chris Lattner447ff682008-03-11 03:23:40 +00001232 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1233 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001234 if (VA.getLocReg() == X86::ST0 ||
1235 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001236 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1237 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001238 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001240 RetOps.push_back(ValToCopy);
1241 // Don't emit a copytoreg.
1242 continue;
1243 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001244
Evan Cheng242b38b2009-02-23 09:03:22 +00001245 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1246 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001247 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001248 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001249 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001250 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001251 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001252 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001253 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001254 }
1255
Dale Johannesendd64c412009-02-04 00:33:20 +00001256 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001257 Flag = Chain.getValue(1);
1258 }
Dan Gohman61a92132008-04-21 23:59:07 +00001259
1260 // The x86-64 ABI for returning structs by value requires that we copy
1261 // the sret argument into %rax for the return. We saved the argument into
1262 // a virtual register in the entry block, so now we copy the value out
1263 // and into %rax.
1264 if (Subtarget->is64Bit() &&
1265 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1266 MachineFunction &MF = DAG.getMachineFunction();
1267 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1268 unsigned Reg = FuncInfo->getSRetReturnReg();
1269 if (!Reg) {
Evan Chengdcea1632010-02-04 02:40:39 +00001270 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001271 FuncInfo->setSRetReturnReg(Reg);
1272 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001273 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001274
Dale Johannesendd64c412009-02-04 00:33:20 +00001275 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001276 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001277
1278 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001279 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001280 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Chris Lattner447ff682008-03-11 03:23:40 +00001282 RetOps[0] = Chain; // Update chain.
1283
1284 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001285 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001286 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001287
1288 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001289 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001290}
1291
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292/// LowerCallResult - Lower the result values of a call into the
1293/// appropriate copies out of appropriate physical registers.
1294///
1295SDValue
1296X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001297 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001298 const SmallVectorImpl<ISD::InputArg> &Ins,
1299 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001300 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001301
Chris Lattnere32bbf62007-02-28 07:09:55 +00001302 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001303 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001304 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001305 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001306 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001307 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001308
Chris Lattner3085e152007-02-25 08:59:22 +00001309 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001310 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001311 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001312 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001313
Torok Edwin3f142c32009-02-01 18:15:56 +00001314 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001315 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001316 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001317 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001318 }
1319
Chris Lattner8e6da152008-03-10 21:08:41 +00001320 // If this is a call to a function that returns an fp value on the floating
1321 // point stack, but where we prefer to use the value in xmm registers, copy
1322 // 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 +00001323 if ((VA.getLocReg() == X86::ST0 ||
1324 VA.getLocReg() == X86::ST1) &&
1325 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001326 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001327 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001328
Evan Cheng79fb3b42009-02-20 20:43:02 +00001329 SDValue Val;
1330 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001331 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1332 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1333 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001334 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001335 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001336 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1337 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001338 } else {
1339 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001340 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001341 Val = Chain.getValue(0);
1342 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001343 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1344 } else {
1345 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1346 CopyVT, InFlag).getValue(1);
1347 Val = Chain.getValue(0);
1348 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001349 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001350
Dan Gohman37eed792009-02-04 17:28:58 +00001351 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001352 // Round the F80 the right size, which also moves to the appropriate xmm
1353 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001354 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001355 // This truncation won't change the value.
1356 DAG.getIntPtrConstant(1));
1357 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001358
Dan Gohman98ca4f22009-08-05 01:29:28 +00001359 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001360 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001361
Dan Gohman98ca4f22009-08-05 01:29:28 +00001362 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001363}
1364
1365
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001366//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001367// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001368//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001369// StdCall calling convention seems to be standard for many Windows' API
1370// routines and around. It differs from C calling convention just a little:
1371// callee should clean up the stack, not caller. Symbols should be also
1372// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001373// For info on fast calling convention see Fast Calling Convention (tail call)
1374// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001375
Dan Gohman98ca4f22009-08-05 01:29:28 +00001376/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001377/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001378static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1379 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001380 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001381
Dan Gohman98ca4f22009-08-05 01:29:28 +00001382 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001383}
1384
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001385/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001386/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001387static bool
1388ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1389 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001390 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001391
Dan Gohman98ca4f22009-08-05 01:29:28 +00001392 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001393}
1394
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001395/// IsCalleePop - Determines whether the callee is required to pop its
1396/// own arguments. Callee pop is necessary to support tail calls.
Dan Gohmand858e902010-04-17 15:26:15 +00001397bool X86TargetLowering::IsCalleePop(bool IsVarArg,
1398 CallingConv::ID CallingConv) const {
Gordon Henriksen86737662008-01-05 16:56:59 +00001399 if (IsVarArg)
1400 return false;
1401
Dan Gohman095cc292008-09-13 01:54:27 +00001402 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001403 default:
1404 return false;
1405 case CallingConv::X86_StdCall:
1406 return !Subtarget->is64Bit();
1407 case CallingConv::X86_FastCall:
1408 return !Subtarget->is64Bit();
1409 case CallingConv::Fast:
Dan Gohman1797ed52010-02-08 20:27:50 +00001410 return GuaranteedTailCallOpt;
Chris Lattner29689432010-03-11 00:22:57 +00001411 case CallingConv::GHC:
1412 return GuaranteedTailCallOpt;
Gordon Henriksen86737662008-01-05 16:56:59 +00001413 }
1414}
1415
Dan Gohman095cc292008-09-13 01:54:27 +00001416/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1417/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001418CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001419 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001420 if (CC == CallingConv::GHC)
1421 return CC_X86_64_GHC;
1422 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001423 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001424 else
1425 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001426 }
1427
Gordon Henriksen86737662008-01-05 16:56:59 +00001428 if (CC == CallingConv::X86_FastCall)
1429 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001430 else if (CC == CallingConv::Fast)
1431 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001432 else if (CC == CallingConv::GHC)
1433 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001434 else
1435 return CC_X86_32_C;
1436}
1437
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001438/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1439/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001440/// the specific parameter attribute. The copy will be passed as a byval
1441/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001442static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001443CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001444 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1445 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001446 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001447 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001448 /*isVolatile*/false, /*AlwaysInline=*/true,
1449 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001450}
1451
Chris Lattner29689432010-03-11 00:22:57 +00001452/// IsTailCallConvention - Return true if the calling convention is one that
1453/// supports tail call optimization.
1454static bool IsTailCallConvention(CallingConv::ID CC) {
1455 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1456}
1457
Evan Cheng0c439eb2010-01-27 00:07:07 +00001458/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1459/// a tailcall target by changing its ABI.
1460static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001461 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001462}
1463
Dan Gohman98ca4f22009-08-05 01:29:28 +00001464SDValue
1465X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001466 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001467 const SmallVectorImpl<ISD::InputArg> &Ins,
1468 DebugLoc dl, SelectionDAG &DAG,
1469 const CCValAssign &VA,
1470 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001471 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001472 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001473 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001474 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001475 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001476 EVT ValVT;
1477
1478 // If value is passed by pointer we have address passed instead of the value
1479 // itself.
1480 if (VA.getLocInfo() == CCValAssign::Indirect)
1481 ValVT = VA.getLocVT();
1482 else
1483 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001484
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001485 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001486 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001487 // In case of tail call optimization mark all arguments mutable. Since they
1488 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001489 if (Flags.isByVal()) {
1490 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1491 VA.getLocMemOffset(), isImmutable, false);
1492 return DAG.getFrameIndex(FI, getPointerTy());
1493 } else {
1494 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1495 VA.getLocMemOffset(), isImmutable, false);
1496 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1497 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001498 PseudoSourceValue::getFixedStack(FI), 0,
1499 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001500 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001501}
1502
Dan Gohman475871a2008-07-27 21:46:04 +00001503SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001504X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001505 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001506 bool isVarArg,
1507 const SmallVectorImpl<ISD::InputArg> &Ins,
1508 DebugLoc dl,
1509 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001510 SmallVectorImpl<SDValue> &InVals)
1511 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001512 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001513 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001514
Gordon Henriksen86737662008-01-05 16:56:59 +00001515 const Function* Fn = MF.getFunction();
1516 if (Fn->hasExternalLinkage() &&
1517 Subtarget->isTargetCygMing() &&
1518 Fn->getName() == "main")
1519 FuncInfo->setForceFramePointer(true);
1520
Evan Cheng1bc78042006-04-26 01:20:17 +00001521 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001522 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001523 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001524
Chris Lattner29689432010-03-11 00:22:57 +00001525 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1526 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001527
Chris Lattner638402b2007-02-28 07:00:42 +00001528 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001529 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001530 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1531 ArgLocs, *DAG.getContext());
1532 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001533
Chris Lattnerf39f7712007-02-28 05:46:49 +00001534 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001535 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001536 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1537 CCValAssign &VA = ArgLocs[i];
1538 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1539 // places.
1540 assert(VA.getValNo() != LastVal &&
1541 "Don't support value assigned to multiple locs yet");
1542 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001543
Chris Lattnerf39f7712007-02-28 05:46:49 +00001544 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001545 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001546 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001547 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001548 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001549 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001550 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001551 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001552 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001553 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001554 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001555 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001556 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001557 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1558 RC = X86::VR64RegisterClass;
1559 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001560 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001561
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001562 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001563 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001564
Chris Lattnerf39f7712007-02-28 05:46:49 +00001565 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1566 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1567 // right size.
1568 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001569 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001570 DAG.getValueType(VA.getValVT()));
1571 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001572 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001573 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001574 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001575 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001576
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001577 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001578 // Handle MMX values passed in XMM regs.
1579 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001580 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1581 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001582 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1583 } else
1584 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001585 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001586 } else {
1587 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001588 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001589 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001590
1591 // If value is passed via pointer - do a load.
1592 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001593 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1594 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001595
Dan Gohman98ca4f22009-08-05 01:29:28 +00001596 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001597 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001598
Dan Gohman61a92132008-04-21 23:59:07 +00001599 // The x86-64 ABI for returning structs by value requires that we copy
1600 // the sret argument into %rax for the return. Save the argument into
1601 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001602 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001603 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1604 unsigned Reg = FuncInfo->getSRetReturnReg();
1605 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001606 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001607 FuncInfo->setSRetReturnReg(Reg);
1608 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001609 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001610 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001611 }
1612
Chris Lattnerf39f7712007-02-28 05:46:49 +00001613 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001614 // Align stack specially for tail calls.
1615 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001616 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001617
Evan Cheng1bc78042006-04-26 01:20:17 +00001618 // If the function takes variable number of arguments, make a frame index for
1619 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001620 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001621 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001622 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,
1623 true, false));
Gordon Henriksen86737662008-01-05 16:56:59 +00001624 }
1625 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001626 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1627
1628 // FIXME: We should really autogenerate these arrays
1629 static const unsigned GPR64ArgRegsWin64[] = {
1630 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001631 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001632 static const unsigned XMMArgRegsWin64[] = {
1633 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1634 };
1635 static const unsigned GPR64ArgRegs64Bit[] = {
1636 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1637 };
1638 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1640 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1641 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001642 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1643
1644 if (IsWin64) {
1645 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1646 GPR64ArgRegs = GPR64ArgRegsWin64;
1647 XMMArgRegs = XMMArgRegsWin64;
1648 } else {
1649 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1650 GPR64ArgRegs = GPR64ArgRegs64Bit;
1651 XMMArgRegs = XMMArgRegs64Bit;
1652 }
1653 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1654 TotalNumIntRegs);
1655 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1656 TotalNumXMMRegs);
1657
Devang Patel578efa92009-06-05 21:57:13 +00001658 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001659 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001660 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001661 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001662 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001663 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001664 // Kernel mode asks for SSE to be disabled, so don't push them
1665 // on the stack.
1666 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001667
Gordon Henriksen86737662008-01-05 16:56:59 +00001668 // For X86-64, if there are vararg parameters that are passed via
1669 // registers, then we must store them to their spots on the stack so they
1670 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001671 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1672 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1673 FuncInfo->setRegSaveFrameIndex(
1674 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1675 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001676
Gordon Henriksen86737662008-01-05 16:56:59 +00001677 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001678 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001679 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1680 getPointerTy());
1681 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001682 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001683 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1684 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001685 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1686 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001687 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001688 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001689 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001690 PseudoSourceValue::getFixedStack(
1691 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001692 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001693 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001694 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001695 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001696
Dan Gohmanface41a2009-08-16 21:24:25 +00001697 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1698 // Now store the XMM (fp + vector) parameter registers.
1699 SmallVector<SDValue, 11> SaveXMMOps;
1700 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001701
Dan Gohmanface41a2009-08-16 21:24:25 +00001702 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1703 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1704 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001705
Dan Gohman1e93df62010-04-17 14:41:14 +00001706 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1707 FuncInfo->getRegSaveFrameIndex()));
1708 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1709 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001710
Dan Gohmanface41a2009-08-16 21:24:25 +00001711 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1712 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1713 X86::VR128RegisterClass);
1714 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1715 SaveXMMOps.push_back(Val);
1716 }
1717 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1718 MVT::Other,
1719 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001721
1722 if (!MemOps.empty())
1723 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1724 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001725 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001726 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001727
Gordon Henriksen86737662008-01-05 16:56:59 +00001728 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001729 if (IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001730 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001731 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001732 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001733 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001734 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001735 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001736 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001737
Gordon Henriksen86737662008-01-05 16:56:59 +00001738 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001739 // RegSaveFrameIndex is X86-64 only.
1740 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001741 if (CallConv == CallingConv::X86_FastCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001742 // fastcc functions can't have varargs.
1743 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001744 }
Evan Cheng25caf632006-05-23 21:06:34 +00001745
Dan Gohman98ca4f22009-08-05 01:29:28 +00001746 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001747}
1748
Dan Gohman475871a2008-07-27 21:46:04 +00001749SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1751 SDValue StackPtr, SDValue Arg,
1752 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001753 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001754 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001755 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001756 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001757 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001758 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001759 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001760 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001761 }
Dale Johannesenace16102009-02-03 19:33:06 +00001762 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001763 PseudoSourceValue::getStack(), LocMemOffset,
1764 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001765}
1766
Bill Wendling64e87322009-01-16 19:25:27 +00001767/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001768/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001769SDValue
1770X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001771 SDValue &OutRetAddr, SDValue Chain,
1772 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001773 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001774 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001775 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001776 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001777
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001778 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001779 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001780 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001781}
1782
1783/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1784/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001785static SDValue
1786EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001787 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001788 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001789 // Store the return address to the appropriate stack slot.
1790 if (!FPDiff) return Chain;
1791 // Calculate the new stack slot for the return address.
1792 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001793 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001794 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001795 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001796 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001797 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001798 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1799 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001800 return Chain;
1801}
1802
Dan Gohman98ca4f22009-08-05 01:29:28 +00001803SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001804X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001805 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001806 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001807 const SmallVectorImpl<ISD::OutputArg> &Outs,
1808 const SmallVectorImpl<ISD::InputArg> &Ins,
1809 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001810 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001811 MachineFunction &MF = DAG.getMachineFunction();
1812 bool Is64Bit = Subtarget->is64Bit();
1813 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001814 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001815
Evan Cheng5f941932010-02-05 02:21:12 +00001816 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001817 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001818 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1819 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001820 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001821
1822 // Sibcalls are automatically detected tailcalls which do not require
1823 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001824 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001825 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001826
1827 if (isTailCall)
1828 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001829 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001830
Chris Lattner29689432010-03-11 00:22:57 +00001831 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1832 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001833
Chris Lattner638402b2007-02-28 07:00:42 +00001834 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001835 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001836 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1837 ArgLocs, *DAG.getContext());
1838 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001839
Chris Lattner423c5f42007-02-28 05:31:48 +00001840 // Get a count of how many bytes are to be pushed on the stack.
1841 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001842 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001843 // This is a sibcall. The memory operands are available in caller's
1844 // own caller's stack.
1845 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001846 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001847 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001848
Gordon Henriksen86737662008-01-05 16:56:59 +00001849 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001850 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001851 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001852 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001853 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1854 FPDiff = NumBytesCallerPushed - NumBytes;
1855
1856 // Set the delta of movement of the returnaddr stackslot.
1857 // But only set if delta is greater than previous delta.
1858 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1859 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1860 }
1861
Evan Chengf22f9b32010-02-06 03:28:46 +00001862 if (!IsSibcall)
1863 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001864
Dan Gohman475871a2008-07-27 21:46:04 +00001865 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001866 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001867 if (isTailCall && FPDiff)
1868 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1869 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001870
Dan Gohman475871a2008-07-27 21:46:04 +00001871 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1872 SmallVector<SDValue, 8> MemOpChains;
1873 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001874
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001875 // Walk the register/memloc assignments, inserting copies/loads. In the case
1876 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001877 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1878 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001879 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001880 SDValue Arg = Outs[i].Val;
1881 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001882 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001883
Chris Lattner423c5f42007-02-28 05:31:48 +00001884 // Promote the value if needed.
1885 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001886 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001887 case CCValAssign::Full: break;
1888 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001889 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001890 break;
1891 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001892 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001893 break;
1894 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001895 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1896 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001897 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1898 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1899 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001900 } else
1901 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1902 break;
1903 case CCValAssign::BCvt:
1904 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001905 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001906 case CCValAssign::Indirect: {
1907 // Store the argument.
1908 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001909 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001910 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001911 PseudoSourceValue::getFixedStack(FI), 0,
1912 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001913 Arg = SpillSlot;
1914 break;
1915 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001916 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001917
Chris Lattner423c5f42007-02-28 05:31:48 +00001918 if (VA.isRegLoc()) {
1919 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001920 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001921 assert(VA.isMemLoc());
1922 if (StackPtr.getNode() == 0)
1923 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1924 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1925 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001926 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001927 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001928
Evan Cheng32fe1032006-05-25 00:59:30 +00001929 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001930 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001931 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001932
Evan Cheng347d5f72006-04-28 21:29:37 +00001933 // Build a sequence of copy-to-reg nodes chained together with token chain
1934 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001935 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001936 // Tail call byval lowering might overwrite argument registers so in case of
1937 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001939 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001940 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001941 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001942 InFlag = Chain.getValue(1);
1943 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001944
Chris Lattner88e1fd52009-07-09 04:24:46 +00001945 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001946 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1947 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001948 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001949 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1950 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001951 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001952 InFlag);
1953 InFlag = Chain.getValue(1);
1954 } else {
1955 // If we are tail calling and generating PIC/GOT style code load the
1956 // address of the callee into ECX. The value in ecx is used as target of
1957 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1958 // for tail calls on PIC/GOT architectures. Normally we would just put the
1959 // address of GOT into ebx and then call target@PLT. But for tail calls
1960 // ebx would be restored (since ebx is callee saved) before jumping to the
1961 // target@PLT.
1962
1963 // Note: The actual moving to ECX is done further down.
1964 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1965 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1966 !G->getGlobal()->hasProtectedVisibility())
1967 Callee = LowerGlobalAddress(Callee, DAG);
1968 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001969 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001970 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001971 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001972
Gordon Henriksen86737662008-01-05 16:56:59 +00001973 if (Is64Bit && isVarArg) {
1974 // From AMD64 ABI document:
1975 // For calls that may call functions that use varargs or stdargs
1976 // (prototype-less calls or calls to functions containing ellipsis (...) in
1977 // the declaration) %al is used as hidden argument to specify the number
1978 // of SSE registers used. The contents of %al do not need to match exactly
1979 // the number of registers, but must be an ubound on the number of SSE
1980 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001981
1982 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001983 // Count the number of XMM registers allocated.
1984 static const unsigned XMMArgRegs[] = {
1985 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1986 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1987 };
1988 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001989 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001990 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001991
Dale Johannesendd64c412009-02-04 00:33:20 +00001992 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001993 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001994 InFlag = Chain.getValue(1);
1995 }
1996
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001997
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001998 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001999 if (isTailCall) {
2000 // Force all the incoming stack arguments to be loaded from the stack
2001 // before any new outgoing arguments are stored to the stack, because the
2002 // outgoing stack slots may alias the incoming argument stack slots, and
2003 // the alias isn't otherwise explicit. This is slightly more conservative
2004 // than necessary, because it means that each store effectively depends
2005 // on every argument instead of just those arguments it would clobber.
2006 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2007
Dan Gohman475871a2008-07-27 21:46:04 +00002008 SmallVector<SDValue, 8> MemOpChains2;
2009 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002010 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002011 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002012 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002013 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002014 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2015 CCValAssign &VA = ArgLocs[i];
2016 if (VA.isRegLoc())
2017 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002018 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002019 SDValue Arg = Outs[i].Val;
2020 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002021 // Create frame index.
2022 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002023 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00002024 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002025 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002026
Duncan Sands276dcbd2008-03-21 09:14:45 +00002027 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002028 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002029 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002030 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002031 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002032 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002033 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002034
Dan Gohman98ca4f22009-08-05 01:29:28 +00002035 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2036 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002037 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002038 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002039 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002040 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002041 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002042 PseudoSourceValue::getFixedStack(FI), 0,
2043 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002044 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 }
2046 }
2047
2048 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002049 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002050 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002051
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002052 // Copy arguments to their registers.
2053 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002054 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002055 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002056 InFlag = Chain.getValue(1);
2057 }
Dan Gohman475871a2008-07-27 21:46:04 +00002058 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002059
Gordon Henriksen86737662008-01-05 16:56:59 +00002060 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002061 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002062 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002063 }
2064
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002065 bool WasGlobalOrExternal = false;
2066 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2067 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2068 // In the 64-bit large code model, we have to make all calls
2069 // through a register, since the call instruction's 32-bit
2070 // pc-relative offset may not be large enough to hold the whole
2071 // address.
2072 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2073 WasGlobalOrExternal = true;
2074 // If the callee is a GlobalAddress node (quite common, every direct call
2075 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2076 // it.
2077
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002078 // We should use extra load for direct calls to dllimported functions in
2079 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002080 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002081 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002082 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002083
Chris Lattner48a7d022009-07-09 05:02:21 +00002084 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2085 // external symbols most go through the PLT in PIC mode. If the symbol
2086 // has hidden or protected visibility, or if it is static or local, then
2087 // we don't need to use the PLT - we can directly call it.
2088 if (Subtarget->isTargetELF() &&
2089 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002090 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002091 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002092 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002093 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2094 Subtarget->getDarwinVers() < 9) {
2095 // PC-relative references to external symbols should go through $stub,
2096 // unless we're building with the leopard linker or later, which
2097 // automatically synthesizes these stubs.
2098 OpFlags = X86II::MO_DARWIN_STUB;
2099 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002100
Chris Lattner74e726e2009-07-09 05:27:35 +00002101 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002102 G->getOffset(), OpFlags);
2103 }
Bill Wendling056292f2008-09-16 21:48:12 +00002104 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002105 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002106 unsigned char OpFlags = 0;
2107
2108 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2109 // symbols should go through the PLT.
2110 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002111 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002112 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002113 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002114 Subtarget->getDarwinVers() < 9) {
2115 // PC-relative references to external symbols should go through $stub,
2116 // unless we're building with the leopard linker or later, which
2117 // automatically synthesizes these stubs.
2118 OpFlags = X86II::MO_DARWIN_STUB;
2119 }
Eric Christopherfd179292009-08-27 18:07:15 +00002120
Chris Lattner48a7d022009-07-09 05:02:21 +00002121 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2122 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002123 }
2124
Chris Lattnerd96d0722007-02-25 06:40:16 +00002125 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002126 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002127 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002128
Evan Chengf22f9b32010-02-06 03:28:46 +00002129 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002130 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2131 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002132 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002134
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002135 Ops.push_back(Chain);
2136 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002137
Dan Gohman98ca4f22009-08-05 01:29:28 +00002138 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002139 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002140
Gordon Henriksen86737662008-01-05 16:56:59 +00002141 // Add argument registers to the end of the list so that they are known live
2142 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002143 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2144 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2145 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002146
Evan Cheng586ccac2008-03-18 23:36:35 +00002147 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002148 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002149 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2150
2151 // Add an implicit use of AL for x86 vararg functions.
2152 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002153 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002154
Gabor Greifba36cb52008-08-28 21:40:38 +00002155 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002156 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002157
Dan Gohman98ca4f22009-08-05 01:29:28 +00002158 if (isTailCall) {
2159 // If this is the first return lowered for this function, add the regs
2160 // to the liveout set for the function.
2161 if (MF.getRegInfo().liveout_empty()) {
2162 SmallVector<CCValAssign, 16> RVLocs;
2163 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2164 *DAG.getContext());
2165 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2166 for (unsigned i = 0; i != RVLocs.size(); ++i)
2167 if (RVLocs[i].isRegLoc())
2168 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2169 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002170 return DAG.getNode(X86ISD::TC_RETURN, dl,
2171 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002172 }
2173
Dale Johannesenace16102009-02-03 19:33:06 +00002174 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002175 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002176
Chris Lattner2d297092006-05-23 18:50:38 +00002177 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002178 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002179 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002180 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002181 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002182 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002183 // pops the hidden struct pointer, so we have to push it back.
2184 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002185 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002186 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002187 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002188
Gordon Henriksenae636f82008-01-03 16:47:34 +00002189 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002190 if (!IsSibcall) {
2191 Chain = DAG.getCALLSEQ_END(Chain,
2192 DAG.getIntPtrConstant(NumBytes, true),
2193 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2194 true),
2195 InFlag);
2196 InFlag = Chain.getValue(1);
2197 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002198
Chris Lattner3085e152007-02-25 08:59:22 +00002199 // Handle result values, copying them out of physregs into vregs that we
2200 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002201 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2202 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002203}
2204
Evan Cheng25ab6902006-09-08 06:48:29 +00002205
2206//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002207// Fast Calling Convention (tail call) implementation
2208//===----------------------------------------------------------------------===//
2209
2210// Like std call, callee cleans arguments, convention except that ECX is
2211// reserved for storing the tail called function address. Only 2 registers are
2212// free for argument passing (inreg). Tail call optimization is performed
2213// provided:
2214// * tailcallopt is enabled
2215// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002216// On X86_64 architecture with GOT-style position independent code only local
2217// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002218// To keep the stack aligned according to platform abi the function
2219// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2220// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002221// If a tail called function callee has more arguments than the caller the
2222// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002223// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002224// original REtADDR, but before the saved framepointer or the spilled registers
2225// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2226// stack layout:
2227// arg1
2228// arg2
2229// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002230// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002231// move area ]
2232// (possible EBP)
2233// ESI
2234// EDI
2235// local1 ..
2236
2237/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2238/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002239unsigned
2240X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2241 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002242 MachineFunction &MF = DAG.getMachineFunction();
2243 const TargetMachine &TM = MF.getTarget();
2244 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2245 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002246 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002247 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002248 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002249 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2250 // Number smaller than 12 so just add the difference.
2251 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2252 } else {
2253 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002254 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002255 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002256 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002257 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002258}
2259
Evan Cheng5f941932010-02-05 02:21:12 +00002260/// MatchingStackOffset - Return true if the given stack call argument is
2261/// already available in the same position (relatively) of the caller's
2262/// incoming argument stack.
2263static
2264bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2265 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2266 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002267 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2268 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002269 if (Arg.getOpcode() == ISD::CopyFromReg) {
2270 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2271 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2272 return false;
2273 MachineInstr *Def = MRI->getVRegDef(VR);
2274 if (!Def)
2275 return false;
2276 if (!Flags.isByVal()) {
2277 if (!TII->isLoadFromStackSlot(Def, FI))
2278 return false;
2279 } else {
2280 unsigned Opcode = Def->getOpcode();
2281 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2282 Def->getOperand(1).isFI()) {
2283 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002284 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002285 } else
2286 return false;
2287 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002288 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2289 if (Flags.isByVal())
2290 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002291 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002292 // define @foo(%struct.X* %A) {
2293 // tail call @bar(%struct.X* byval %A)
2294 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002295 return false;
2296 SDValue Ptr = Ld->getBasePtr();
2297 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2298 if (!FINode)
2299 return false;
2300 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002301 } else
2302 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002303
Evan Cheng4cae1332010-03-05 08:38:04 +00002304 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002305 if (!MFI->isFixedObjectIndex(FI))
2306 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002307 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002308}
2309
Dan Gohman98ca4f22009-08-05 01:29:28 +00002310/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2311/// for tail call optimization. Targets which want to do tail call
2312/// optimization should implement this function.
2313bool
2314X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002315 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002316 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002317 bool isCalleeStructRet,
2318 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002319 const SmallVectorImpl<ISD::OutputArg> &Outs,
2320 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002321 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002322 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002323 CalleeCC != CallingConv::C)
2324 return false;
2325
Evan Cheng7096ae42010-01-29 06:45:59 +00002326 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002327 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002328 const Function *CallerF = DAG.getMachineFunction().getFunction();
Dan Gohman1797ed52010-02-08 20:27:50 +00002329 if (GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00002330 if (IsTailCallConvention(CalleeCC) &&
Evan Cheng843bd692010-01-31 06:44:49 +00002331 CallerF->getCallingConv() == CalleeCC)
2332 return true;
2333 return false;
2334 }
2335
Evan Chengb2c92902010-02-02 02:22:50 +00002336 // Look for obvious safe cases to perform tail call optimization that does not
2337 // requite ABI changes. This is what gcc calls sibcall.
2338
Evan Cheng2c12cb42010-03-26 16:26:03 +00002339 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2340 // emit a special epilogue.
2341 if (RegInfo->needsStackRealignment(MF))
2342 return false;
2343
Evan Cheng3c262ee2010-03-26 02:13:13 +00002344 // Do not sibcall optimize vararg calls unless the call site is not passing any
2345 // arguments.
2346 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002347 return false;
2348
Evan Chenga375d472010-03-15 18:54:48 +00002349 // Also avoid sibcall optimization if either caller or callee uses struct
2350 // return semantics.
2351 if (isCalleeStructRet || isCallerStructRet)
2352 return false;
2353
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002354 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2355 // Therefore if it's not used by the call it is not safe to optimize this into
2356 // a sibcall.
2357 bool Unused = false;
2358 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2359 if (!Ins[i].Used) {
2360 Unused = true;
2361 break;
2362 }
2363 }
2364 if (Unused) {
2365 SmallVector<CCValAssign, 16> RVLocs;
2366 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2367 RVLocs, *DAG.getContext());
2368 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2369 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2370 CCValAssign &VA = RVLocs[i];
2371 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2372 return false;
2373 }
2374 }
2375
Evan Chenga6bff982010-01-30 01:22:00 +00002376 // If the callee takes no arguments then go on to check the results of the
2377 // call.
2378 if (!Outs.empty()) {
2379 // Check if stack adjustment is needed. For now, do not do this if any
2380 // argument is passed on the stack.
2381 SmallVector<CCValAssign, 16> ArgLocs;
2382 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2383 ArgLocs, *DAG.getContext());
2384 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002385 if (CCInfo.getNextStackOffset()) {
2386 MachineFunction &MF = DAG.getMachineFunction();
2387 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2388 return false;
2389 if (Subtarget->isTargetWin64())
2390 // Win64 ABI has additional complications.
2391 return false;
2392
2393 // Check if the arguments are already laid out in the right way as
2394 // the caller's fixed stack objects.
2395 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002396 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2397 const X86InstrInfo *TII =
2398 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002399 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2400 CCValAssign &VA = ArgLocs[i];
2401 EVT RegVT = VA.getLocVT();
2402 SDValue Arg = Outs[i].Val;
2403 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002404 if (VA.getLocInfo() == CCValAssign::Indirect)
2405 return false;
2406 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002407 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2408 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002409 return false;
2410 }
2411 }
2412 }
Evan Chenga6bff982010-01-30 01:22:00 +00002413 }
Evan Chengb1712452010-01-27 06:25:16 +00002414
Evan Cheng86809cc2010-02-03 03:28:02 +00002415 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002416}
2417
Dan Gohman3df24e62008-09-03 23:12:08 +00002418FastISel *
Chris Lattnered3a8062010-04-05 06:05:26 +00002419X86TargetLowering::createFastISel(MachineFunction &mf,
Evan Chengddc419c2010-01-26 19:04:47 +00002420 DenseMap<const Value *, unsigned> &vm,
2421 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2422 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002423#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00002424 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002425#endif
Dan Gohmand858e902010-04-17 15:26:15 +00002426 ) const {
Chris Lattnered3a8062010-04-05 06:05:26 +00002427 return X86::createFastISel(mf, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002428#ifndef NDEBUG
2429 , cil
2430#endif
2431 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002432}
2433
2434
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002435//===----------------------------------------------------------------------===//
2436// Other Lowering Hooks
2437//===----------------------------------------------------------------------===//
2438
2439
Dan Gohmand858e902010-04-17 15:26:15 +00002440SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002441 MachineFunction &MF = DAG.getMachineFunction();
2442 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2443 int ReturnAddrIndex = FuncInfo->getRAIndex();
2444
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002445 if (ReturnAddrIndex == 0) {
2446 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002447 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002448 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002449 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002450 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002451 }
2452
Evan Cheng25ab6902006-09-08 06:48:29 +00002453 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002454}
2455
2456
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002457bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2458 bool hasSymbolicDisplacement) {
2459 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002460 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002461 return false;
2462
2463 // If we don't have a symbolic displacement - we don't have any extra
2464 // restrictions.
2465 if (!hasSymbolicDisplacement)
2466 return true;
2467
2468 // FIXME: Some tweaks might be needed for medium code model.
2469 if (M != CodeModel::Small && M != CodeModel::Kernel)
2470 return false;
2471
2472 // For small code model we assume that latest object is 16MB before end of 31
2473 // bits boundary. We may also accept pretty large negative constants knowing
2474 // that all objects are in the positive half of address space.
2475 if (M == CodeModel::Small && Offset < 16*1024*1024)
2476 return true;
2477
2478 // For kernel code model we know that all object resist in the negative half
2479 // of 32bits address space. We may not accept negative offsets, since they may
2480 // be just off and we may accept pretty large positive ones.
2481 if (M == CodeModel::Kernel && Offset > 0)
2482 return true;
2483
2484 return false;
2485}
2486
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002487/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2488/// specific condition code, returning the condition code and the LHS/RHS of the
2489/// comparison to make.
2490static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2491 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002492 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002493 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2494 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2495 // X > -1 -> X == 0, jump !sign.
2496 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002497 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002498 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2499 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002500 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002501 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002502 // X < 1 -> X <= 0
2503 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002504 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002505 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002506 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002507
Evan Chengd9558e02006-01-06 00:43:03 +00002508 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002509 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002510 case ISD::SETEQ: return X86::COND_E;
2511 case ISD::SETGT: return X86::COND_G;
2512 case ISD::SETGE: return X86::COND_GE;
2513 case ISD::SETLT: return X86::COND_L;
2514 case ISD::SETLE: return X86::COND_LE;
2515 case ISD::SETNE: return X86::COND_NE;
2516 case ISD::SETULT: return X86::COND_B;
2517 case ISD::SETUGT: return X86::COND_A;
2518 case ISD::SETULE: return X86::COND_BE;
2519 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002520 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002521 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002522
Chris Lattner4c78e022008-12-23 23:42:27 +00002523 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002524
Chris Lattner4c78e022008-12-23 23:42:27 +00002525 // If LHS is a foldable load, but RHS is not, flip the condition.
2526 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2527 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2528 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2529 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002530 }
2531
Chris Lattner4c78e022008-12-23 23:42:27 +00002532 switch (SetCCOpcode) {
2533 default: break;
2534 case ISD::SETOLT:
2535 case ISD::SETOLE:
2536 case ISD::SETUGT:
2537 case ISD::SETUGE:
2538 std::swap(LHS, RHS);
2539 break;
2540 }
2541
2542 // On a floating point condition, the flags are set as follows:
2543 // ZF PF CF op
2544 // 0 | 0 | 0 | X > Y
2545 // 0 | 0 | 1 | X < Y
2546 // 1 | 0 | 0 | X == Y
2547 // 1 | 1 | 1 | unordered
2548 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002549 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002550 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002551 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002552 case ISD::SETOLT: // flipped
2553 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002554 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002555 case ISD::SETOLE: // flipped
2556 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002557 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002558 case ISD::SETUGT: // flipped
2559 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002560 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002561 case ISD::SETUGE: // flipped
2562 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002563 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002564 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002565 case ISD::SETNE: return X86::COND_NE;
2566 case ISD::SETUO: return X86::COND_P;
2567 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002568 case ISD::SETOEQ:
2569 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002570 }
Evan Chengd9558e02006-01-06 00:43:03 +00002571}
2572
Evan Cheng4a460802006-01-11 00:33:36 +00002573/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2574/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002575/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002576static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002577 switch (X86CC) {
2578 default:
2579 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002580 case X86::COND_B:
2581 case X86::COND_BE:
2582 case X86::COND_E:
2583 case X86::COND_P:
2584 case X86::COND_A:
2585 case X86::COND_AE:
2586 case X86::COND_NE:
2587 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002588 return true;
2589 }
2590}
2591
Evan Chengeb2f9692009-10-27 19:56:55 +00002592/// isFPImmLegal - Returns true if the target can instruction select the
2593/// specified FP immediate natively. If false, the legalizer will
2594/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002595bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002596 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2597 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2598 return true;
2599 }
2600 return false;
2601}
2602
Nate Begeman9008ca62009-04-27 18:41:29 +00002603/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2604/// the specified range (L, H].
2605static bool isUndefOrInRange(int Val, int Low, int Hi) {
2606 return (Val < 0) || (Val >= Low && Val < Hi);
2607}
2608
2609/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2610/// specified value.
2611static bool isUndefOrEqual(int Val, int CmpVal) {
2612 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002613 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002614 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002615}
2616
Nate Begeman9008ca62009-04-27 18:41:29 +00002617/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2618/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2619/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002620static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002621 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002622 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002623 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002624 return (Mask[0] < 2 && Mask[1] < 2);
2625 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002626}
2627
Nate Begeman9008ca62009-04-27 18:41:29 +00002628bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002629 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 N->getMask(M);
2631 return ::isPSHUFDMask(M, N->getValueType(0));
2632}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002633
Nate Begeman9008ca62009-04-27 18:41:29 +00002634/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2635/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002636static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002637 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002638 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002639
Nate Begeman9008ca62009-04-27 18:41:29 +00002640 // Lower quadword copied in order or undef.
2641 for (int i = 0; i != 4; ++i)
2642 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002643 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002644
Evan Cheng506d3df2006-03-29 23:07:14 +00002645 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002646 for (int i = 4; i != 8; ++i)
2647 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002648 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002649
Evan Cheng506d3df2006-03-29 23:07:14 +00002650 return true;
2651}
2652
Nate Begeman9008ca62009-04-27 18:41:29 +00002653bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002654 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002655 N->getMask(M);
2656 return ::isPSHUFHWMask(M, N->getValueType(0));
2657}
Evan Cheng506d3df2006-03-29 23:07:14 +00002658
Nate Begeman9008ca62009-04-27 18:41:29 +00002659/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2660/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002661static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002662 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002663 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002664
Rafael Espindola15684b22009-04-24 12:40:33 +00002665 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002666 for (int i = 4; i != 8; ++i)
2667 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002668 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002669
Rafael Espindola15684b22009-04-24 12:40:33 +00002670 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002671 for (int i = 0; i != 4; ++i)
2672 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002673 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002674
Rafael Espindola15684b22009-04-24 12:40:33 +00002675 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002676}
2677
Nate Begeman9008ca62009-04-27 18:41:29 +00002678bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002679 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002680 N->getMask(M);
2681 return ::isPSHUFLWMask(M, N->getValueType(0));
2682}
2683
Nate Begemana09008b2009-10-19 02:17:23 +00002684/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2685/// is suitable for input to PALIGNR.
2686static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2687 bool hasSSSE3) {
2688 int i, e = VT.getVectorNumElements();
2689
2690 // Do not handle v2i64 / v2f64 shuffles with palignr.
2691 if (e < 4 || !hasSSSE3)
2692 return false;
2693
2694 for (i = 0; i != e; ++i)
2695 if (Mask[i] >= 0)
2696 break;
2697
2698 // All undef, not a palignr.
2699 if (i == e)
2700 return false;
2701
2702 // Determine if it's ok to perform a palignr with only the LHS, since we
2703 // don't have access to the actual shuffle elements to see if RHS is undef.
2704 bool Unary = Mask[i] < (int)e;
2705 bool NeedsUnary = false;
2706
2707 int s = Mask[i] - i;
2708
2709 // Check the rest of the elements to see if they are consecutive.
2710 for (++i; i != e; ++i) {
2711 int m = Mask[i];
2712 if (m < 0)
2713 continue;
2714
2715 Unary = Unary && (m < (int)e);
2716 NeedsUnary = NeedsUnary || (m < s);
2717
2718 if (NeedsUnary && !Unary)
2719 return false;
2720 if (Unary && m != ((s+i) & (e-1)))
2721 return false;
2722 if (!Unary && m != (s+i))
2723 return false;
2724 }
2725 return true;
2726}
2727
2728bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2729 SmallVector<int, 8> M;
2730 N->getMask(M);
2731 return ::isPALIGNRMask(M, N->getValueType(0), true);
2732}
2733
Evan Cheng14aed5e2006-03-24 01:18:28 +00002734/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2735/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002736static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002737 int NumElems = VT.getVectorNumElements();
2738 if (NumElems != 2 && NumElems != 4)
2739 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002740
Nate Begeman9008ca62009-04-27 18:41:29 +00002741 int Half = NumElems / 2;
2742 for (int i = 0; i < Half; ++i)
2743 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002744 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002745 for (int i = Half; i < NumElems; ++i)
2746 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002747 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002748
Evan Cheng14aed5e2006-03-24 01:18:28 +00002749 return true;
2750}
2751
Nate Begeman9008ca62009-04-27 18:41:29 +00002752bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2753 SmallVector<int, 8> M;
2754 N->getMask(M);
2755 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002756}
2757
Evan Cheng213d2cf2007-05-17 18:45:50 +00002758/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002759/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2760/// half elements to come from vector 1 (which would equal the dest.) and
2761/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002762static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002763 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002764
2765 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002766 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002767
Nate Begeman9008ca62009-04-27 18:41:29 +00002768 int Half = NumElems / 2;
2769 for (int i = 0; i < Half; ++i)
2770 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002771 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002772 for (int i = Half; i < NumElems; ++i)
2773 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002774 return false;
2775 return true;
2776}
2777
Nate Begeman9008ca62009-04-27 18:41:29 +00002778static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2779 SmallVector<int, 8> M;
2780 N->getMask(M);
2781 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002782}
2783
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002784/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2785/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002786bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2787 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002788 return false;
2789
Evan Cheng2064a2b2006-03-28 06:50:32 +00002790 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002791 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2792 isUndefOrEqual(N->getMaskElt(1), 7) &&
2793 isUndefOrEqual(N->getMaskElt(2), 2) &&
2794 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002795}
2796
Nate Begeman0b10b912009-11-07 23:17:15 +00002797/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2798/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2799/// <2, 3, 2, 3>
2800bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2801 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2802
2803 if (NumElems != 4)
2804 return false;
2805
2806 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2807 isUndefOrEqual(N->getMaskElt(1), 3) &&
2808 isUndefOrEqual(N->getMaskElt(2), 2) &&
2809 isUndefOrEqual(N->getMaskElt(3), 3);
2810}
2811
Evan Cheng5ced1d82006-04-06 23:23:56 +00002812/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2813/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002814bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2815 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002816
Evan Cheng5ced1d82006-04-06 23:23:56 +00002817 if (NumElems != 2 && NumElems != 4)
2818 return false;
2819
Evan Chengc5cdff22006-04-07 21:53:05 +00002820 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002821 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002822 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002823
Evan Chengc5cdff22006-04-07 21:53:05 +00002824 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002825 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002826 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002827
2828 return true;
2829}
2830
Nate Begeman0b10b912009-11-07 23:17:15 +00002831/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2832/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2833bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002834 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002835
Evan Cheng5ced1d82006-04-06 23:23:56 +00002836 if (NumElems != 2 && NumElems != 4)
2837 return false;
2838
Evan Chengc5cdff22006-04-07 21:53:05 +00002839 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002840 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002841 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002842
Nate Begeman9008ca62009-04-27 18:41:29 +00002843 for (unsigned i = 0; i < NumElems/2; ++i)
2844 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002845 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002846
2847 return true;
2848}
2849
Evan Cheng0038e592006-03-28 00:39:58 +00002850/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2851/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002852static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002853 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002854 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002855 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002856 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002857
Nate Begeman9008ca62009-04-27 18:41:29 +00002858 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2859 int BitI = Mask[i];
2860 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002861 if (!isUndefOrEqual(BitI, j))
2862 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002863 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002864 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002865 return false;
2866 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002867 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002868 return false;
2869 }
Evan Cheng0038e592006-03-28 00:39:58 +00002870 }
Evan Cheng0038e592006-03-28 00:39:58 +00002871 return true;
2872}
2873
Nate Begeman9008ca62009-04-27 18:41:29 +00002874bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2875 SmallVector<int, 8> M;
2876 N->getMask(M);
2877 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002878}
2879
Evan Cheng4fcb9222006-03-28 02:43:26 +00002880/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2881/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002882static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002883 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002884 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002885 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002886 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002887
Nate Begeman9008ca62009-04-27 18:41:29 +00002888 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2889 int BitI = Mask[i];
2890 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002891 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002892 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002893 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002894 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002895 return false;
2896 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002897 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002898 return false;
2899 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002900 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002901 return true;
2902}
2903
Nate Begeman9008ca62009-04-27 18:41:29 +00002904bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2905 SmallVector<int, 8> M;
2906 N->getMask(M);
2907 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002908}
2909
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002910/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2911/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2912/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002913static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002914 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002915 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002916 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002917
Nate Begeman9008ca62009-04-27 18:41:29 +00002918 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2919 int BitI = Mask[i];
2920 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002921 if (!isUndefOrEqual(BitI, j))
2922 return false;
2923 if (!isUndefOrEqual(BitI1, j))
2924 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002925 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002926 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002927}
2928
Nate Begeman9008ca62009-04-27 18:41:29 +00002929bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2930 SmallVector<int, 8> M;
2931 N->getMask(M);
2932 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2933}
2934
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002935/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2936/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2937/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002938static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002939 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002940 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2941 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002942
Nate Begeman9008ca62009-04-27 18:41:29 +00002943 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2944 int BitI = Mask[i];
2945 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002946 if (!isUndefOrEqual(BitI, j))
2947 return false;
2948 if (!isUndefOrEqual(BitI1, j))
2949 return false;
2950 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002951 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002952}
2953
Nate Begeman9008ca62009-04-27 18:41:29 +00002954bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2955 SmallVector<int, 8> M;
2956 N->getMask(M);
2957 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2958}
2959
Evan Cheng017dcc62006-04-21 01:05:10 +00002960/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2961/// specifies a shuffle of elements that is suitable for input to MOVSS,
2962/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002963static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002964 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002965 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002966
2967 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002968
Nate Begeman9008ca62009-04-27 18:41:29 +00002969 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002970 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002971
Nate Begeman9008ca62009-04-27 18:41:29 +00002972 for (int i = 1; i < NumElts; ++i)
2973 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002974 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002975
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002976 return true;
2977}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002978
Nate Begeman9008ca62009-04-27 18:41:29 +00002979bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2980 SmallVector<int, 8> M;
2981 N->getMask(M);
2982 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002983}
2984
Evan Cheng017dcc62006-04-21 01:05:10 +00002985/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2986/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002987/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002988static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002989 bool V2IsSplat = false, bool V2IsUndef = false) {
2990 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002991 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002992 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002993
Nate Begeman9008ca62009-04-27 18:41:29 +00002994 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002995 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002996
Nate Begeman9008ca62009-04-27 18:41:29 +00002997 for (int i = 1; i < NumOps; ++i)
2998 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2999 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3000 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003001 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003002
Evan Cheng39623da2006-04-20 08:58:49 +00003003 return true;
3004}
3005
Nate Begeman9008ca62009-04-27 18:41:29 +00003006static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003007 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003008 SmallVector<int, 8> M;
3009 N->getMask(M);
3010 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003011}
3012
Evan Chengd9539472006-04-14 21:59:03 +00003013/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3014/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003015bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3016 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003017 return false;
3018
3019 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003020 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003021 int Elt = N->getMaskElt(i);
3022 if (Elt >= 0 && Elt != 1)
3023 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003024 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003025
3026 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003027 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 int Elt = N->getMaskElt(i);
3029 if (Elt >= 0 && Elt != 3)
3030 return false;
3031 if (Elt == 3)
3032 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003033 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003034 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003035 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003036 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003037}
3038
3039/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3040/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003041bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3042 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003043 return false;
3044
3045 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003046 for (unsigned i = 0; i < 2; ++i)
3047 if (N->getMaskElt(i) > 0)
3048 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003049
3050 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003051 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 int Elt = N->getMaskElt(i);
3053 if (Elt >= 0 && Elt != 2)
3054 return false;
3055 if (Elt == 2)
3056 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003057 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003059 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003060}
3061
Evan Cheng0b457f02008-09-25 20:50:48 +00003062/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3063/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003064bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3065 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003066
Nate Begeman9008ca62009-04-27 18:41:29 +00003067 for (int i = 0; i < e; ++i)
3068 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003069 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003070 for (int i = 0; i < e; ++i)
3071 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003072 return false;
3073 return true;
3074}
3075
Evan Cheng63d33002006-03-22 08:01:21 +00003076/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003077/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003078unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003079 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3080 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3081
Evan Chengb9df0ca2006-03-22 02:53:00 +00003082 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3083 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003084 for (int i = 0; i < NumOperands; ++i) {
3085 int Val = SVOp->getMaskElt(NumOperands-i-1);
3086 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003087 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003088 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003089 if (i != NumOperands - 1)
3090 Mask <<= Shift;
3091 }
Evan Cheng63d33002006-03-22 08:01:21 +00003092 return Mask;
3093}
3094
Evan Cheng506d3df2006-03-29 23:07:14 +00003095/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003096/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003097unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003098 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003099 unsigned Mask = 0;
3100 // 8 nodes, but we only care about the last 4.
3101 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003102 int Val = SVOp->getMaskElt(i);
3103 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003104 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003105 if (i != 4)
3106 Mask <<= 2;
3107 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003108 return Mask;
3109}
3110
3111/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003112/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003113unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003115 unsigned Mask = 0;
3116 // 8 nodes, but we only care about the first 4.
3117 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003118 int Val = SVOp->getMaskElt(i);
3119 if (Val >= 0)
3120 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003121 if (i != 0)
3122 Mask <<= 2;
3123 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003124 return Mask;
3125}
3126
Nate Begemana09008b2009-10-19 02:17:23 +00003127/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3128/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3129unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3130 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3131 EVT VVT = N->getValueType(0);
3132 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3133 int Val = 0;
3134
3135 unsigned i, e;
3136 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3137 Val = SVOp->getMaskElt(i);
3138 if (Val >= 0)
3139 break;
3140 }
3141 return (Val - i) * EltSize;
3142}
3143
Evan Cheng37b73872009-07-30 08:33:02 +00003144/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3145/// constant +0.0.
3146bool X86::isZeroNode(SDValue Elt) {
3147 return ((isa<ConstantSDNode>(Elt) &&
3148 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3149 (isa<ConstantFPSDNode>(Elt) &&
3150 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3151}
3152
Nate Begeman9008ca62009-04-27 18:41:29 +00003153/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3154/// their permute mask.
3155static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3156 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003157 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003158 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003159 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Nate Begeman5a5ca152009-04-29 05:20:52 +00003161 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 int idx = SVOp->getMaskElt(i);
3163 if (idx < 0)
3164 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003165 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003166 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003167 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003168 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003169 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003170 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3171 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003172}
3173
Evan Cheng779ccea2007-12-07 21:30:01 +00003174/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3175/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003176static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003177 unsigned NumElems = VT.getVectorNumElements();
3178 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 int idx = Mask[i];
3180 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003181 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003182 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003184 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003186 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003187}
3188
Evan Cheng533a0aa2006-04-19 20:35:22 +00003189/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3190/// match movhlps. The lower half elements should come from upper half of
3191/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003192/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003193static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3194 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003195 return false;
3196 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003198 return false;
3199 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003201 return false;
3202 return true;
3203}
3204
Evan Cheng5ced1d82006-04-06 23:23:56 +00003205/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003206/// is promoted to a vector. It also returns the LoadSDNode by reference if
3207/// required.
3208static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003209 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3210 return false;
3211 N = N->getOperand(0).getNode();
3212 if (!ISD::isNON_EXTLoad(N))
3213 return false;
3214 if (LD)
3215 *LD = cast<LoadSDNode>(N);
3216 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003217}
3218
Evan Cheng533a0aa2006-04-19 20:35:22 +00003219/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3220/// match movlp{s|d}. The lower half elements should come from lower half of
3221/// V1 (and in order), and the upper half elements should come from the upper
3222/// half of V2 (and in order). And since V1 will become the source of the
3223/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003224static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3225 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003226 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003227 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003228 // Is V2 is a vector load, don't do this transformation. We will try to use
3229 // load folding shufps op.
3230 if (ISD::isNON_EXTLoad(V2))
3231 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003232
Nate Begeman5a5ca152009-04-29 05:20:52 +00003233 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003234
Evan Cheng533a0aa2006-04-19 20:35:22 +00003235 if (NumElems != 2 && NumElems != 4)
3236 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003237 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003238 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003239 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003240 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003242 return false;
3243 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003244}
3245
Evan Cheng39623da2006-04-20 08:58:49 +00003246/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3247/// all the same.
3248static bool isSplatVector(SDNode *N) {
3249 if (N->getOpcode() != ISD::BUILD_VECTOR)
3250 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003251
Dan Gohman475871a2008-07-27 21:46:04 +00003252 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003253 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3254 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003255 return false;
3256 return true;
3257}
3258
Evan Cheng213d2cf2007-05-17 18:45:50 +00003259/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003260/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003261/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003262static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003263 SDValue V1 = N->getOperand(0);
3264 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003265 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3266 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003268 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003269 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003270 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3271 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003272 if (Opc != ISD::BUILD_VECTOR ||
3273 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003274 return false;
3275 } else if (Idx >= 0) {
3276 unsigned Opc = V1.getOpcode();
3277 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3278 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003279 if (Opc != ISD::BUILD_VECTOR ||
3280 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003281 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003282 }
3283 }
3284 return true;
3285}
3286
3287/// getZeroVector - Returns a vector of specified type with all zero elements.
3288///
Owen Andersone50ed302009-08-10 22:56:29 +00003289static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003290 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003291 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003292
Chris Lattner8a594482007-11-25 00:24:49 +00003293 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3294 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003295 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003296 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3298 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003299 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3301 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003302 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003303 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3304 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003305 }
Dale Johannesenace16102009-02-03 19:33:06 +00003306 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003307}
3308
Chris Lattner8a594482007-11-25 00:24:49 +00003309/// getOnesVector - Returns a vector of specified type with all bits set.
3310///
Owen Andersone50ed302009-08-10 22:56:29 +00003311static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003312 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003313
Chris Lattner8a594482007-11-25 00:24:49 +00003314 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3315 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003317 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003318 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003320 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003321 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003322 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003323}
3324
3325
Evan Cheng39623da2006-04-20 08:58:49 +00003326/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3327/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003328static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003329 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003330 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003331
Evan Cheng39623da2006-04-20 08:58:49 +00003332 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003333 SmallVector<int, 8> MaskVec;
3334 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003335
Nate Begeman5a5ca152009-04-29 05:20:52 +00003336 for (unsigned i = 0; i != NumElems; ++i) {
3337 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003338 MaskVec[i] = NumElems;
3339 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003340 }
Evan Cheng39623da2006-04-20 08:58:49 +00003341 }
Evan Cheng39623da2006-04-20 08:58:49 +00003342 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003343 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3344 SVOp->getOperand(1), &MaskVec[0]);
3345 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003346}
3347
Evan Cheng017dcc62006-04-21 01:05:10 +00003348/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3349/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003350static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 SDValue V2) {
3352 unsigned NumElems = VT.getVectorNumElements();
3353 SmallVector<int, 8> Mask;
3354 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003355 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003356 Mask.push_back(i);
3357 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003358}
3359
Nate Begeman9008ca62009-04-27 18:41:29 +00003360/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003361static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 SDValue V2) {
3363 unsigned NumElems = VT.getVectorNumElements();
3364 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003365 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 Mask.push_back(i);
3367 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003368 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003370}
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003373static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 SDValue V2) {
3375 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003376 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003378 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 Mask.push_back(i + Half);
3380 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003381 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003383}
3384
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003385/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003386static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 bool HasSSE2) {
3388 if (SV->getValueType(0).getVectorNumElements() <= 4)
3389 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003390
Owen Anderson825b72b2009-08-11 20:47:22 +00003391 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003392 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 DebugLoc dl = SV->getDebugLoc();
3394 SDValue V1 = SV->getOperand(0);
3395 int NumElems = VT.getVectorNumElements();
3396 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003397
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 // unpack elements to the correct location
3399 while (NumElems > 4) {
3400 if (EltNo < NumElems/2) {
3401 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3402 } else {
3403 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3404 EltNo -= NumElems/2;
3405 }
3406 NumElems >>= 1;
3407 }
Eric Christopherfd179292009-08-27 18:07:15 +00003408
Nate Begeman9008ca62009-04-27 18:41:29 +00003409 // Perform the splat.
3410 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003411 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3413 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003414}
3415
Evan Chengba05f722006-04-21 23:03:30 +00003416/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003417/// vector of zero or undef vector. This produces a shuffle where the low
3418/// element of V2 is swizzled into the zero/undef vector, landing at element
3419/// 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 +00003420static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003421 bool isZero, bool HasSSE2,
3422 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003423 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003424 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003425 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3426 unsigned NumElems = VT.getVectorNumElements();
3427 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003428 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 // If this is the insertion idx, put the low elt of V2 here.
3430 MaskVec.push_back(i == Idx ? NumElems : i);
3431 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003432}
3433
Evan Chengf26ffe92008-05-29 08:22:04 +00003434/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3435/// a shuffle that is zero.
3436static
Nate Begeman9008ca62009-04-27 18:41:29 +00003437unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3438 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003439 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003440 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003441 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 int Idx = SVOp->getMaskElt(Index);
3443 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003444 ++NumZeros;
3445 continue;
3446 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003447 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003448 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003449 ++NumZeros;
3450 else
3451 break;
3452 }
3453 return NumZeros;
3454}
3455
3456/// isVectorShift - Returns true if the shuffle can be implemented as a
3457/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003458/// FIXME: split into pslldqi, psrldqi, palignr variants.
3459static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003460 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003461 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003462
3463 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003465 if (!NumZeros) {
3466 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003467 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003468 if (!NumZeros)
3469 return false;
3470 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003471 bool SeenV1 = false;
3472 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003473 for (unsigned i = NumZeros; i < NumElems; ++i) {
3474 unsigned Val = isLeft ? (i - NumZeros) : i;
3475 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3476 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003477 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003478 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003479 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003480 SeenV1 = true;
3481 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003482 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003483 SeenV2 = true;
3484 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003486 return false;
3487 }
3488 if (SeenV1 && SeenV2)
3489 return false;
3490
Nate Begeman9008ca62009-04-27 18:41:29 +00003491 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003492 ShAmt = NumZeros;
3493 return true;
3494}
3495
3496
Evan Chengc78d3b42006-04-24 18:01:45 +00003497/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3498///
Dan Gohman475871a2008-07-27 21:46:04 +00003499static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003500 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003501 SelectionDAG &DAG,
3502 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003503 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003504 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003505
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003506 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003507 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003508 bool First = true;
3509 for (unsigned i = 0; i < 16; ++i) {
3510 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3511 if (ThisIsNonZero && First) {
3512 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003513 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003514 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003515 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003516 First = false;
3517 }
3518
3519 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003520 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003521 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3522 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003523 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003525 }
3526 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3528 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3529 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003530 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003532 } else
3533 ThisElt = LastElt;
3534
Gabor Greifba36cb52008-08-28 21:40:38 +00003535 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003537 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003538 }
3539 }
3540
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003542}
3543
Bill Wendlinga348c562007-03-22 18:42:45 +00003544/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003545///
Dan Gohman475871a2008-07-27 21:46:04 +00003546static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003547 unsigned NumNonZero, unsigned NumZero,
3548 SelectionDAG &DAG,
3549 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003550 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003551 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003552
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003553 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003554 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003555 bool First = true;
3556 for (unsigned i = 0; i < 8; ++i) {
3557 bool isNonZero = (NonZeros & (1 << i)) != 0;
3558 if (isNonZero) {
3559 if (First) {
3560 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003562 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003563 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003564 First = false;
3565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003566 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003568 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003569 }
3570 }
3571
3572 return V;
3573}
3574
Evan Chengf26ffe92008-05-29 08:22:04 +00003575/// getVShift - Return a vector logical shift node.
3576///
Owen Andersone50ed302009-08-10 22:56:29 +00003577static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003578 unsigned NumBits, SelectionDAG &DAG,
3579 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003580 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003581 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003582 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003583 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3584 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3585 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003586 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003587}
3588
Dan Gohman475871a2008-07-27 21:46:04 +00003589SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003590X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003591 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003592
3593 // Check if the scalar load can be widened into a vector load. And if
3594 // the address is "base + cst" see if the cst can be "absorbed" into
3595 // the shuffle mask.
3596 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3597 SDValue Ptr = LD->getBasePtr();
3598 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3599 return SDValue();
3600 EVT PVT = LD->getValueType(0);
3601 if (PVT != MVT::i32 && PVT != MVT::f32)
3602 return SDValue();
3603
3604 int FI = -1;
3605 int64_t Offset = 0;
3606 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3607 FI = FINode->getIndex();
3608 Offset = 0;
3609 } else if (Ptr.getOpcode() == ISD::ADD &&
3610 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3611 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3612 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3613 Offset = Ptr.getConstantOperandVal(1);
3614 Ptr = Ptr.getOperand(0);
3615 } else {
3616 return SDValue();
3617 }
3618
3619 SDValue Chain = LD->getChain();
3620 // Make sure the stack object alignment is at least 16.
3621 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3622 if (DAG.InferPtrAlignment(Ptr) < 16) {
3623 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003624 // Can't change the alignment. FIXME: It's possible to compute
3625 // the exact stack offset and reference FI + adjust offset instead.
3626 // If someone *really* cares about this. That's the way to implement it.
3627 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003628 } else {
3629 MFI->setObjectAlignment(FI, 16);
3630 }
3631 }
3632
3633 // (Offset % 16) must be multiple of 4. Then address is then
3634 // Ptr + (Offset & ~15).
3635 if (Offset < 0)
3636 return SDValue();
3637 if ((Offset % 16) & 3)
3638 return SDValue();
3639 int64_t StartOffset = Offset & ~15;
3640 if (StartOffset)
3641 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3642 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3643
3644 int EltNo = (Offset - StartOffset) >> 2;
3645 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3646 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003647 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3648 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003649 // Canonicalize it to a v4i32 shuffle.
3650 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3651 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3652 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3653 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3654 }
3655
3656 return SDValue();
3657}
3658
Nate Begeman1449f292010-03-24 22:19:06 +00003659/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3660/// vector of type 'VT', see if the elements can be replaced by a single large
3661/// load which has the same value as a build_vector whose operands are 'elts'.
3662///
3663/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3664///
3665/// FIXME: we'd also like to handle the case where the last elements are zero
3666/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3667/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003668static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3669 DebugLoc &dl, SelectionDAG &DAG) {
3670 EVT EltVT = VT.getVectorElementType();
3671 unsigned NumElems = Elts.size();
3672
Nate Begemanfdea31a2010-03-24 20:49:50 +00003673 LoadSDNode *LDBase = NULL;
3674 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003675
3676 // For each element in the initializer, see if we've found a load or an undef.
3677 // If we don't find an initial load element, or later load elements are
3678 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003679 for (unsigned i = 0; i < NumElems; ++i) {
3680 SDValue Elt = Elts[i];
3681
3682 if (!Elt.getNode() ||
3683 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3684 return SDValue();
3685 if (!LDBase) {
3686 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3687 return SDValue();
3688 LDBase = cast<LoadSDNode>(Elt.getNode());
3689 LastLoadedElt = i;
3690 continue;
3691 }
3692 if (Elt.getOpcode() == ISD::UNDEF)
3693 continue;
3694
3695 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3696 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3697 return SDValue();
3698 LastLoadedElt = i;
3699 }
Nate Begeman1449f292010-03-24 22:19:06 +00003700
3701 // If we have found an entire vector of loads and undefs, then return a large
3702 // load of the entire vector width starting at the base pointer. If we found
3703 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003704 if (LastLoadedElt == NumElems - 1) {
3705 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3706 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3707 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3708 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3709 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3710 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3711 LDBase->isVolatile(), LDBase->isNonTemporal(),
3712 LDBase->getAlignment());
3713 } else if (NumElems == 4 && LastLoadedElt == 1) {
3714 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3715 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3716 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3717 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3718 }
3719 return SDValue();
3720}
3721
Evan Chengc3630942009-12-09 21:00:30 +00003722SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003723X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003724 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003725 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003726 if (ISD::isBuildVectorAllZeros(Op.getNode())
3727 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003728 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3729 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3730 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003731 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003732 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003733
Gabor Greifba36cb52008-08-28 21:40:38 +00003734 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003735 return getOnesVector(Op.getValueType(), DAG, dl);
3736 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003737 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003738
Owen Andersone50ed302009-08-10 22:56:29 +00003739 EVT VT = Op.getValueType();
3740 EVT ExtVT = VT.getVectorElementType();
3741 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003742
3743 unsigned NumElems = Op.getNumOperands();
3744 unsigned NumZero = 0;
3745 unsigned NumNonZero = 0;
3746 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003747 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003748 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003749 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003750 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003751 if (Elt.getOpcode() == ISD::UNDEF)
3752 continue;
3753 Values.insert(Elt);
3754 if (Elt.getOpcode() != ISD::Constant &&
3755 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003756 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003757 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003758 NumZero++;
3759 else {
3760 NonZeros |= (1 << i);
3761 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003762 }
3763 }
3764
Dan Gohman7f321562007-06-25 16:23:39 +00003765 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003766 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003767 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003768 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003769
Chris Lattner67f453a2008-03-09 05:42:06 +00003770 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003771 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003772 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003773 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003774
Chris Lattner62098042008-03-09 01:05:04 +00003775 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3776 // the value are obviously zero, truncate the value to i32 and do the
3777 // insertion that way. Only do this if the value is non-constant or if the
3778 // value is a constant being inserted into element 0. It is cheaper to do
3779 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003780 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003781 (!IsAllConstants || Idx == 0)) {
3782 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3783 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003784 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3785 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003786
Chris Lattner62098042008-03-09 01:05:04 +00003787 // Truncate the value (which may itself be a constant) to i32, and
3788 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003789 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003790 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003791 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3792 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003793
Chris Lattner62098042008-03-09 01:05:04 +00003794 // Now we have our 32-bit value zero extended in the low element of
3795 // a vector. If Idx != 0, swizzle it into place.
3796 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003797 SmallVector<int, 4> Mask;
3798 Mask.push_back(Idx);
3799 for (unsigned i = 1; i != VecElts; ++i)
3800 Mask.push_back(i);
3801 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003802 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003803 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003804 }
Dale Johannesenace16102009-02-03 19:33:06 +00003805 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003806 }
3807 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003808
Chris Lattner19f79692008-03-08 22:59:52 +00003809 // If we have a constant or non-constant insertion into the low element of
3810 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3811 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003812 // depending on what the source datatype is.
3813 if (Idx == 0) {
3814 if (NumZero == 0) {
3815 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003816 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3817 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003818 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3819 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3820 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3821 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003822 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3823 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3824 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003825 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3826 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3827 Subtarget->hasSSE2(), DAG);
3828 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3829 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003830 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003831
3832 // Is it a vector logical left shift?
3833 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003834 X86::isZeroNode(Op.getOperand(0)) &&
3835 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003836 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003837 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003838 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003839 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003840 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003841 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003842
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003843 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003844 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003845
Chris Lattner19f79692008-03-08 22:59:52 +00003846 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3847 // is a non-constant being inserted into an element other than the low one,
3848 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3849 // movd/movss) to move this into the low element, then shuffle it into
3850 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003851 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003852 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003853
Evan Cheng0db9fe62006-04-25 20:13:52 +00003854 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003855 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3856 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003857 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003858 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003859 MaskVec.push_back(i == Idx ? 0 : 1);
3860 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003861 }
3862 }
3863
Chris Lattner67f453a2008-03-09 05:42:06 +00003864 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003865 if (Values.size() == 1) {
3866 if (EVTBits == 32) {
3867 // Instead of a shuffle like this:
3868 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3869 // Check if it's possible to issue this instead.
3870 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3871 unsigned Idx = CountTrailingZeros_32(NonZeros);
3872 SDValue Item = Op.getOperand(Idx);
3873 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3874 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3875 }
Dan Gohman475871a2008-07-27 21:46:04 +00003876 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003877 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003878
Dan Gohmana3941172007-07-24 22:55:08 +00003879 // A vector full of immediates; various special cases are already
3880 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003881 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003882 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003883
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003884 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003885 if (EVTBits == 64) {
3886 if (NumNonZero == 1) {
3887 // One half is zero or undef.
3888 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003889 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003890 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003891 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3892 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003893 }
Dan Gohman475871a2008-07-27 21:46:04 +00003894 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003895 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003896
3897 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003898 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003899 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003900 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003901 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003902 }
3903
Bill Wendling826f36f2007-03-28 00:57:11 +00003904 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003905 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003906 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003907 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003908 }
3909
3910 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003911 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003912 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003913 if (NumElems == 4 && NumZero > 0) {
3914 for (unsigned i = 0; i < 4; ++i) {
3915 bool isZero = !(NonZeros & (1 << i));
3916 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003917 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003918 else
Dale Johannesenace16102009-02-03 19:33:06 +00003919 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003920 }
3921
3922 for (unsigned i = 0; i < 2; ++i) {
3923 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3924 default: break;
3925 case 0:
3926 V[i] = V[i*2]; // Must be a zero vector.
3927 break;
3928 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003929 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003930 break;
3931 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003932 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003933 break;
3934 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003935 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003936 break;
3937 }
3938 }
3939
Nate Begeman9008ca62009-04-27 18:41:29 +00003940 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003941 bool Reverse = (NonZeros & 0x3) == 2;
3942 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003943 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003944 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3945 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003946 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3947 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003948 }
3949
Nate Begemanfdea31a2010-03-24 20:49:50 +00003950 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3951 // Check for a build vector of consecutive loads.
3952 for (unsigned i = 0; i < NumElems; ++i)
3953 V[i] = Op.getOperand(i);
3954
3955 // Check for elements which are consecutive loads.
3956 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3957 if (LD.getNode())
3958 return LD;
3959
3960 // For SSE 4.1, use inserts into undef.
3961 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 V[0] = DAG.getUNDEF(VT);
3963 for (unsigned i = 0; i < NumElems; ++i)
3964 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3965 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3966 Op.getOperand(i), DAG.getIntPtrConstant(i));
3967 return V[0];
3968 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00003969
3970 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00003971 // e.g. for v4f32
3972 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3973 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3974 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003975 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003976 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003977 NumElems >>= 1;
3978 while (NumElems != 0) {
3979 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003980 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003981 NumElems >>= 1;
3982 }
3983 return V[0];
3984 }
Dan Gohman475871a2008-07-27 21:46:04 +00003985 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003986}
3987
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003988SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003989X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003990 // We support concatenate two MMX registers and place them in a MMX
3991 // register. This is better than doing a stack convert.
3992 DebugLoc dl = Op.getDebugLoc();
3993 EVT ResVT = Op.getValueType();
3994 assert(Op.getNumOperands() == 2);
3995 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3996 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3997 int Mask[2];
3998 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3999 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4000 InVec = Op.getOperand(1);
4001 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4002 unsigned NumElts = ResVT.getVectorNumElements();
4003 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4004 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4005 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4006 } else {
4007 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4008 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4009 Mask[0] = 0; Mask[1] = 2;
4010 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4011 }
4012 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4013}
4014
Nate Begemanb9a47b82009-02-23 08:49:38 +00004015// v8i16 shuffles - Prefer shuffles in the following order:
4016// 1. [all] pshuflw, pshufhw, optional move
4017// 2. [ssse3] 1 x pshufb
4018// 3. [ssse3] 2 x pshufb + 1 x por
4019// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004020static
Nate Begeman9008ca62009-04-27 18:41:29 +00004021SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004022 SelectionDAG &DAG,
4023 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004024 SDValue V1 = SVOp->getOperand(0);
4025 SDValue V2 = SVOp->getOperand(1);
4026 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004027 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004028
Nate Begemanb9a47b82009-02-23 08:49:38 +00004029 // Determine if more than 1 of the words in each of the low and high quadwords
4030 // of the result come from the same quadword of one of the two inputs. Undef
4031 // mask values count as coming from any quadword, for better codegen.
4032 SmallVector<unsigned, 4> LoQuad(4);
4033 SmallVector<unsigned, 4> HiQuad(4);
4034 BitVector InputQuads(4);
4035 for (unsigned i = 0; i < 8; ++i) {
4036 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004037 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004038 MaskVals.push_back(EltIdx);
4039 if (EltIdx < 0) {
4040 ++Quad[0];
4041 ++Quad[1];
4042 ++Quad[2];
4043 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004044 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004045 }
4046 ++Quad[EltIdx / 4];
4047 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004048 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004049
Nate Begemanb9a47b82009-02-23 08:49:38 +00004050 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004051 unsigned MaxQuad = 1;
4052 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004053 if (LoQuad[i] > MaxQuad) {
4054 BestLoQuad = i;
4055 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004056 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004057 }
4058
Nate Begemanb9a47b82009-02-23 08:49:38 +00004059 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004060 MaxQuad = 1;
4061 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004062 if (HiQuad[i] > MaxQuad) {
4063 BestHiQuad = i;
4064 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004065 }
4066 }
4067
Nate Begemanb9a47b82009-02-23 08:49:38 +00004068 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004069 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004070 // single pshufb instruction is necessary. If There are more than 2 input
4071 // quads, disable the next transformation since it does not help SSSE3.
4072 bool V1Used = InputQuads[0] || InputQuads[1];
4073 bool V2Used = InputQuads[2] || InputQuads[3];
4074 if (TLI.getSubtarget()->hasSSSE3()) {
4075 if (InputQuads.count() == 2 && V1Used && V2Used) {
4076 BestLoQuad = InputQuads.find_first();
4077 BestHiQuad = InputQuads.find_next(BestLoQuad);
4078 }
4079 if (InputQuads.count() > 2) {
4080 BestLoQuad = -1;
4081 BestHiQuad = -1;
4082 }
4083 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004084
Nate Begemanb9a47b82009-02-23 08:49:38 +00004085 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4086 // the shuffle mask. If a quad is scored as -1, that means that it contains
4087 // words from all 4 input quadwords.
4088 SDValue NewV;
4089 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004090 SmallVector<int, 8> MaskV;
4091 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4092 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004093 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004094 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4095 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4096 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004097
Nate Begemanb9a47b82009-02-23 08:49:38 +00004098 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4099 // source words for the shuffle, to aid later transformations.
4100 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004101 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004102 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004103 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004104 if (idx != (int)i)
4105 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004106 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004107 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004108 AllWordsInNewV = false;
4109 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004110 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004111
Nate Begemanb9a47b82009-02-23 08:49:38 +00004112 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4113 if (AllWordsInNewV) {
4114 for (int i = 0; i != 8; ++i) {
4115 int idx = MaskVals[i];
4116 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004117 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004118 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004119 if ((idx != i) && idx < 4)
4120 pshufhw = false;
4121 if ((idx != i) && idx > 3)
4122 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004123 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004124 V1 = NewV;
4125 V2Used = false;
4126 BestLoQuad = 0;
4127 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004128 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004129
Nate Begemanb9a47b82009-02-23 08:49:38 +00004130 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4131 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004132 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004133 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004134 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004135 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004136 }
Eric Christopherfd179292009-08-27 18:07:15 +00004137
Nate Begemanb9a47b82009-02-23 08:49:38 +00004138 // If we have SSSE3, and all words of the result are from 1 input vector,
4139 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4140 // is present, fall back to case 4.
4141 if (TLI.getSubtarget()->hasSSSE3()) {
4142 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004143
Nate Begemanb9a47b82009-02-23 08:49:38 +00004144 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004145 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004146 // mask, and elements that come from V1 in the V2 mask, so that the two
4147 // results can be OR'd together.
4148 bool TwoInputs = V1Used && V2Used;
4149 for (unsigned i = 0; i != 8; ++i) {
4150 int EltIdx = MaskVals[i] * 2;
4151 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004152 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4153 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004154 continue;
4155 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4157 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004158 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004160 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004161 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004162 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004163 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004164 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004165
Nate Begemanb9a47b82009-02-23 08:49:38 +00004166 // Calculate the shuffle mask for the second input, shuffle it, and
4167 // OR it with the first shuffled input.
4168 pshufbMask.clear();
4169 for (unsigned i = 0; i != 8; ++i) {
4170 int EltIdx = MaskVals[i] * 2;
4171 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4173 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004174 continue;
4175 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004176 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4177 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004178 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004180 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004181 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 MVT::v16i8, &pshufbMask[0], 16));
4183 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4184 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004185 }
4186
4187 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4188 // and update MaskVals with new element order.
4189 BitVector InOrder(8);
4190 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004191 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004192 for (int i = 0; i != 4; ++i) {
4193 int idx = MaskVals[i];
4194 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004195 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004196 InOrder.set(i);
4197 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004198 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004199 InOrder.set(i);
4200 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004201 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004202 }
4203 }
4204 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004205 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004206 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004207 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004208 }
Eric Christopherfd179292009-08-27 18:07:15 +00004209
Nate Begemanb9a47b82009-02-23 08:49:38 +00004210 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4211 // and update MaskVals with the new element order.
4212 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004213 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004214 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004215 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004216 for (unsigned i = 4; i != 8; ++i) {
4217 int idx = MaskVals[i];
4218 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004219 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004220 InOrder.set(i);
4221 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004222 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004223 InOrder.set(i);
4224 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004225 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004226 }
4227 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004229 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004230 }
Eric Christopherfd179292009-08-27 18:07:15 +00004231
Nate Begemanb9a47b82009-02-23 08:49:38 +00004232 // In case BestHi & BestLo were both -1, which means each quadword has a word
4233 // from each of the four input quadwords, calculate the InOrder bitvector now
4234 // before falling through to the insert/extract cleanup.
4235 if (BestLoQuad == -1 && BestHiQuad == -1) {
4236 NewV = V1;
4237 for (int i = 0; i != 8; ++i)
4238 if (MaskVals[i] < 0 || MaskVals[i] == i)
4239 InOrder.set(i);
4240 }
Eric Christopherfd179292009-08-27 18:07:15 +00004241
Nate Begemanb9a47b82009-02-23 08:49:38 +00004242 // The other elements are put in the right place using pextrw and pinsrw.
4243 for (unsigned i = 0; i != 8; ++i) {
4244 if (InOrder[i])
4245 continue;
4246 int EltIdx = MaskVals[i];
4247 if (EltIdx < 0)
4248 continue;
4249 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004250 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004251 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004252 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004253 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004254 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004255 DAG.getIntPtrConstant(i));
4256 }
4257 return NewV;
4258}
4259
4260// v16i8 shuffles - Prefer shuffles in the following order:
4261// 1. [ssse3] 1 x pshufb
4262// 2. [ssse3] 2 x pshufb + 1 x por
4263// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4264static
Nate Begeman9008ca62009-04-27 18:41:29 +00004265SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004266 SelectionDAG &DAG,
4267 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004268 SDValue V1 = SVOp->getOperand(0);
4269 SDValue V2 = SVOp->getOperand(1);
4270 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004271 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004272 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004273
Nate Begemanb9a47b82009-02-23 08:49:38 +00004274 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004275 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004276 // present, fall back to case 3.
4277 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4278 bool V1Only = true;
4279 bool V2Only = true;
4280 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004281 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004282 if (EltIdx < 0)
4283 continue;
4284 if (EltIdx < 16)
4285 V2Only = false;
4286 else
4287 V1Only = false;
4288 }
Eric Christopherfd179292009-08-27 18:07:15 +00004289
Nate Begemanb9a47b82009-02-23 08:49:38 +00004290 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4291 if (TLI.getSubtarget()->hasSSSE3()) {
4292 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004293
Nate Begemanb9a47b82009-02-23 08:49:38 +00004294 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004295 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004296 //
4297 // Otherwise, we have elements from both input vectors, and must zero out
4298 // elements that come from V2 in the first mask, and V1 in the second mask
4299 // so that we can OR them together.
4300 bool TwoInputs = !(V1Only || V2Only);
4301 for (unsigned i = 0; i != 16; ++i) {
4302 int EltIdx = MaskVals[i];
4303 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004304 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004305 continue;
4306 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004307 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004308 }
4309 // If all the elements are from V2, assign it to V1 and return after
4310 // building the first pshufb.
4311 if (V2Only)
4312 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004313 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004314 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004315 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004316 if (!TwoInputs)
4317 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004318
Nate Begemanb9a47b82009-02-23 08:49:38 +00004319 // Calculate the shuffle mask for the second input, shuffle it, and
4320 // OR it with the first shuffled input.
4321 pshufbMask.clear();
4322 for (unsigned i = 0; i != 16; ++i) {
4323 int EltIdx = MaskVals[i];
4324 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004325 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004326 continue;
4327 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004328 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004329 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004330 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004331 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004332 MVT::v16i8, &pshufbMask[0], 16));
4333 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004334 }
Eric Christopherfd179292009-08-27 18:07:15 +00004335
Nate Begemanb9a47b82009-02-23 08:49:38 +00004336 // No SSSE3 - Calculate in place words and then fix all out of place words
4337 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4338 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004339 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4340 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004341 SDValue NewV = V2Only ? V2 : V1;
4342 for (int i = 0; i != 8; ++i) {
4343 int Elt0 = MaskVals[i*2];
4344 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004345
Nate Begemanb9a47b82009-02-23 08:49:38 +00004346 // This word of the result is all undef, skip it.
4347 if (Elt0 < 0 && Elt1 < 0)
4348 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004349
Nate Begemanb9a47b82009-02-23 08:49:38 +00004350 // This word of the result is already in the correct place, skip it.
4351 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4352 continue;
4353 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4354 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004355
Nate Begemanb9a47b82009-02-23 08:49:38 +00004356 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4357 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4358 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004359
4360 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4361 // using a single extract together, load it and store it.
4362 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004363 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004364 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004365 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004366 DAG.getIntPtrConstant(i));
4367 continue;
4368 }
4369
Nate Begemanb9a47b82009-02-23 08:49:38 +00004370 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004371 // source byte is not also odd, shift the extracted word left 8 bits
4372 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004373 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004374 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004375 DAG.getIntPtrConstant(Elt1 / 2));
4376 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004377 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004378 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004379 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004380 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4381 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004382 }
4383 // If Elt0 is defined, extract it from the appropriate source. If the
4384 // source byte is not also even, shift the extracted word right 8 bits. If
4385 // Elt1 was also defined, OR the extracted values together before
4386 // inserting them in the result.
4387 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004388 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004389 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4390 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004391 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004392 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004393 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004394 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4395 DAG.getConstant(0x00FF, MVT::i16));
4396 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004397 : InsElt0;
4398 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004399 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004400 DAG.getIntPtrConstant(i));
4401 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004402 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004403}
4404
Evan Cheng7a831ce2007-12-15 03:00:47 +00004405/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4406/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4407/// done when every pair / quad of shuffle mask elements point to elements in
4408/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004409/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4410static
Nate Begeman9008ca62009-04-27 18:41:29 +00004411SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4412 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004413 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004414 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 SDValue V1 = SVOp->getOperand(0);
4416 SDValue V2 = SVOp->getOperand(1);
4417 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004418 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004419 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004420 EVT MaskEltVT = MaskVT.getVectorElementType();
4421 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004422 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004423 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 case MVT::v4f32: NewVT = MVT::v2f64; break;
4425 case MVT::v4i32: NewVT = MVT::v2i64; break;
4426 case MVT::v8i16: NewVT = MVT::v4i32; break;
4427 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004428 }
4429
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004430 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004431 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004432 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004433 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004434 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004435 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004436 int Scale = NumElems / NewWidth;
4437 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004438 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004439 int StartIdx = -1;
4440 for (int j = 0; j < Scale; ++j) {
4441 int EltIdx = SVOp->getMaskElt(i+j);
4442 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004443 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004444 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004445 StartIdx = EltIdx - (EltIdx % Scale);
4446 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004447 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004448 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004449 if (StartIdx == -1)
4450 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004451 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004452 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004453 }
4454
Dale Johannesenace16102009-02-03 19:33:06 +00004455 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4456 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004457 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004458}
4459
Evan Chengd880b972008-05-09 21:53:03 +00004460/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004461///
Owen Andersone50ed302009-08-10 22:56:29 +00004462static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004463 SDValue SrcOp, SelectionDAG &DAG,
4464 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004465 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004466 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004467 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004468 LD = dyn_cast<LoadSDNode>(SrcOp);
4469 if (!LD) {
4470 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4471 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004472 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4473 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004474 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4475 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004476 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004477 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004478 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004479 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4480 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4481 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4482 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004483 SrcOp.getOperand(0)
4484 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004485 }
4486 }
4487 }
4488
Dale Johannesenace16102009-02-03 19:33:06 +00004489 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4490 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004491 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004492 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004493}
4494
Evan Chengace3c172008-07-22 21:13:36 +00004495/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4496/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004497static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004498LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4499 SDValue V1 = SVOp->getOperand(0);
4500 SDValue V2 = SVOp->getOperand(1);
4501 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004502 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004503
Evan Chengace3c172008-07-22 21:13:36 +00004504 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004505 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004506 SmallVector<int, 8> Mask1(4U, -1);
4507 SmallVector<int, 8> PermMask;
4508 SVOp->getMask(PermMask);
4509
Evan Chengace3c172008-07-22 21:13:36 +00004510 unsigned NumHi = 0;
4511 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004512 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004513 int Idx = PermMask[i];
4514 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004515 Locs[i] = std::make_pair(-1, -1);
4516 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004517 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4518 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004519 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004520 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004521 NumLo++;
4522 } else {
4523 Locs[i] = std::make_pair(1, NumHi);
4524 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004525 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004526 NumHi++;
4527 }
4528 }
4529 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004530
Evan Chengace3c172008-07-22 21:13:36 +00004531 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004532 // If no more than two elements come from either vector. This can be
4533 // implemented with two shuffles. First shuffle gather the elements.
4534 // The second shuffle, which takes the first shuffle as both of its
4535 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004536 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004537
Nate Begeman9008ca62009-04-27 18:41:29 +00004538 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004539
Evan Chengace3c172008-07-22 21:13:36 +00004540 for (unsigned i = 0; i != 4; ++i) {
4541 if (Locs[i].first == -1)
4542 continue;
4543 else {
4544 unsigned Idx = (i < 2) ? 0 : 4;
4545 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004547 }
4548 }
4549
Nate Begeman9008ca62009-04-27 18:41:29 +00004550 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004551 } else if (NumLo == 3 || NumHi == 3) {
4552 // Otherwise, we must have three elements from one vector, call it X, and
4553 // one element from the other, call it Y. First, use a shufps to build an
4554 // intermediate vector with the one element from Y and the element from X
4555 // that will be in the same half in the final destination (the indexes don't
4556 // matter). Then, use a shufps to build the final vector, taking the half
4557 // containing the element from Y from the intermediate, and the other half
4558 // from X.
4559 if (NumHi == 3) {
4560 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004561 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004562 std::swap(V1, V2);
4563 }
4564
4565 // Find the element from V2.
4566 unsigned HiIndex;
4567 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004568 int Val = PermMask[HiIndex];
4569 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004570 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004571 if (Val >= 4)
4572 break;
4573 }
4574
Nate Begeman9008ca62009-04-27 18:41:29 +00004575 Mask1[0] = PermMask[HiIndex];
4576 Mask1[1] = -1;
4577 Mask1[2] = PermMask[HiIndex^1];
4578 Mask1[3] = -1;
4579 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004580
4581 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004582 Mask1[0] = PermMask[0];
4583 Mask1[1] = PermMask[1];
4584 Mask1[2] = HiIndex & 1 ? 6 : 4;
4585 Mask1[3] = HiIndex & 1 ? 4 : 6;
4586 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004587 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004588 Mask1[0] = HiIndex & 1 ? 2 : 0;
4589 Mask1[1] = HiIndex & 1 ? 0 : 2;
4590 Mask1[2] = PermMask[2];
4591 Mask1[3] = PermMask[3];
4592 if (Mask1[2] >= 0)
4593 Mask1[2] += 4;
4594 if (Mask1[3] >= 0)
4595 Mask1[3] += 4;
4596 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004597 }
Evan Chengace3c172008-07-22 21:13:36 +00004598 }
4599
4600 // Break it into (shuffle shuffle_hi, shuffle_lo).
4601 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004602 SmallVector<int,8> LoMask(4U, -1);
4603 SmallVector<int,8> HiMask(4U, -1);
4604
4605 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004606 unsigned MaskIdx = 0;
4607 unsigned LoIdx = 0;
4608 unsigned HiIdx = 2;
4609 for (unsigned i = 0; i != 4; ++i) {
4610 if (i == 2) {
4611 MaskPtr = &HiMask;
4612 MaskIdx = 1;
4613 LoIdx = 0;
4614 HiIdx = 2;
4615 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004616 int Idx = PermMask[i];
4617 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004618 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004619 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004620 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004621 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004622 LoIdx++;
4623 } else {
4624 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004625 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004626 HiIdx++;
4627 }
4628 }
4629
Nate Begeman9008ca62009-04-27 18:41:29 +00004630 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4631 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4632 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004633 for (unsigned i = 0; i != 4; ++i) {
4634 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004635 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004636 } else {
4637 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004638 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004639 }
4640 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004641 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004642}
4643
Dan Gohman475871a2008-07-27 21:46:04 +00004644SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004645X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004646 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004647 SDValue V1 = Op.getOperand(0);
4648 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004649 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004650 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004651 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004652 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004653 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4654 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004655 bool V1IsSplat = false;
4656 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004657
Nate Begeman9008ca62009-04-27 18:41:29 +00004658 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004659 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004660
Nate Begeman9008ca62009-04-27 18:41:29 +00004661 // Promote splats to v4f32.
4662 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004663 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004664 return Op;
4665 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004666 }
4667
Evan Cheng7a831ce2007-12-15 03:00:47 +00004668 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4669 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004670 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004671 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004672 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004673 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004674 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004675 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004676 // FIXME: Figure out a cleaner way to do this.
4677 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004678 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004680 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004681 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4682 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4683 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004684 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004685 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004686 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4687 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004688 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004689 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004690 }
4691 }
Eric Christopherfd179292009-08-27 18:07:15 +00004692
Nate Begeman9008ca62009-04-27 18:41:29 +00004693 if (X86::isPSHUFDMask(SVOp))
4694 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004695
Evan Chengf26ffe92008-05-29 08:22:04 +00004696 // Check if this can be converted into a logical shift.
4697 bool isLeft = false;
4698 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004699 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004700 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004701 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004702 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004703 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004704 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004705 EVT EltVT = VT.getVectorElementType();
4706 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004707 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004708 }
Eric Christopherfd179292009-08-27 18:07:15 +00004709
Nate Begeman9008ca62009-04-27 18:41:29 +00004710 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004711 if (V1IsUndef)
4712 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004713 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004714 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004715 if (!isMMX)
4716 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004717 }
Eric Christopherfd179292009-08-27 18:07:15 +00004718
Nate Begeman9008ca62009-04-27 18:41:29 +00004719 // FIXME: fold these into legal mask.
4720 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4721 X86::isMOVSLDUPMask(SVOp) ||
4722 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004723 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004724 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004725 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004726
Nate Begeman9008ca62009-04-27 18:41:29 +00004727 if (ShouldXformToMOVHLPS(SVOp) ||
4728 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4729 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004730
Evan Chengf26ffe92008-05-29 08:22:04 +00004731 if (isShift) {
4732 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004733 EVT EltVT = VT.getVectorElementType();
4734 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004735 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004736 }
Eric Christopherfd179292009-08-27 18:07:15 +00004737
Evan Cheng9eca5e82006-10-25 21:49:50 +00004738 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004739 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4740 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004741 V1IsSplat = isSplatVector(V1.getNode());
4742 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004743
Chris Lattner8a594482007-11-25 00:24:49 +00004744 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004745 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004746 Op = CommuteVectorShuffle(SVOp, DAG);
4747 SVOp = cast<ShuffleVectorSDNode>(Op);
4748 V1 = SVOp->getOperand(0);
4749 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004750 std::swap(V1IsSplat, V2IsSplat);
4751 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004752 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004753 }
4754
Nate Begeman9008ca62009-04-27 18:41:29 +00004755 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4756 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004757 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004758 return V1;
4759 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4760 // the instruction selector will not match, so get a canonical MOVL with
4761 // swapped operands to undo the commute.
4762 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004763 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004764
Nate Begeman9008ca62009-04-27 18:41:29 +00004765 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4766 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4767 X86::isUNPCKLMask(SVOp) ||
4768 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004769 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004770
Evan Cheng9bbbb982006-10-25 20:48:19 +00004771 if (V2IsSplat) {
4772 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004773 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004774 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004775 SDValue NewMask = NormalizeMask(SVOp, DAG);
4776 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4777 if (NSVOp != SVOp) {
4778 if (X86::isUNPCKLMask(NSVOp, true)) {
4779 return NewMask;
4780 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4781 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004782 }
4783 }
4784 }
4785
Evan Cheng9eca5e82006-10-25 21:49:50 +00004786 if (Commuted) {
4787 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004788 // FIXME: this seems wrong.
4789 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4790 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4791 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4792 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4793 X86::isUNPCKLMask(NewSVOp) ||
4794 X86::isUNPCKHMask(NewSVOp))
4795 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004796 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004797
Nate Begemanb9a47b82009-02-23 08:49:38 +00004798 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004799
4800 // Normalize the node to match x86 shuffle ops if needed
4801 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4802 return CommuteVectorShuffle(SVOp, DAG);
4803
4804 // Check for legal shuffle and return?
4805 SmallVector<int, 16> PermMask;
4806 SVOp->getMask(PermMask);
4807 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004808 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004809
Evan Cheng14b32e12007-12-11 01:46:18 +00004810 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004811 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004812 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004813 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004814 return NewOp;
4815 }
4816
Owen Anderson825b72b2009-08-11 20:47:22 +00004817 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004818 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004819 if (NewOp.getNode())
4820 return NewOp;
4821 }
Eric Christopherfd179292009-08-27 18:07:15 +00004822
Evan Chengace3c172008-07-22 21:13:36 +00004823 // Handle all 4 wide cases with a number of shuffles except for MMX.
4824 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004825 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004826
Dan Gohman475871a2008-07-27 21:46:04 +00004827 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004828}
4829
Dan Gohman475871a2008-07-27 21:46:04 +00004830SDValue
4831X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004832 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004833 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004834 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004835 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004836 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004837 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004839 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004840 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004841 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004842 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4843 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4844 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004845 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4846 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004847 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004848 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004849 Op.getOperand(0)),
4850 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004851 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004852 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004853 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004854 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004855 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004856 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004857 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4858 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004859 // result has a single use which is a store or a bitcast to i32. And in
4860 // the case of a store, it's not worth it if the index is a constant 0,
4861 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004862 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004863 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004864 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004865 if ((User->getOpcode() != ISD::STORE ||
4866 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4867 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004868 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004870 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004871 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4872 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004873 Op.getOperand(0)),
4874 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004875 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4876 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004877 // ExtractPS works with constant index.
4878 if (isa<ConstantSDNode>(Op.getOperand(1)))
4879 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004880 }
Dan Gohman475871a2008-07-27 21:46:04 +00004881 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004882}
4883
4884
Dan Gohman475871a2008-07-27 21:46:04 +00004885SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004886X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
4887 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004888 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004889 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004890
Evan Cheng62a3f152008-03-24 21:52:23 +00004891 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004892 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004893 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004894 return Res;
4895 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004896
Owen Andersone50ed302009-08-10 22:56:29 +00004897 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004898 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004899 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004900 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004901 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004902 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004903 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4905 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004906 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004907 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004908 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004909 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004910 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004911 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004912 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004913 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004914 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004915 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004916 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004917 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004918 if (Idx == 0)
4919 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004920
Evan Cheng0db9fe62006-04-25 20:13:52 +00004921 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004922 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004923 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004924 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004925 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004926 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004927 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004928 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004929 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4930 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4931 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004932 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004933 if (Idx == 0)
4934 return Op;
4935
4936 // UNPCKHPD the element to the lowest double word, then movsd.
4937 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4938 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004939 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004940 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004941 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004942 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004943 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004944 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004945 }
4946
Dan Gohman475871a2008-07-27 21:46:04 +00004947 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004948}
4949
Dan Gohman475871a2008-07-27 21:46:04 +00004950SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004951X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
4952 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004953 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004954 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004955 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004956
Dan Gohman475871a2008-07-27 21:46:04 +00004957 SDValue N0 = Op.getOperand(0);
4958 SDValue N1 = Op.getOperand(1);
4959 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004960
Dan Gohman8a55ce42009-09-23 21:02:20 +00004961 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004962 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004963 unsigned Opc;
4964 if (VT == MVT::v8i16)
4965 Opc = X86ISD::PINSRW;
4966 else if (VT == MVT::v4i16)
4967 Opc = X86ISD::MMX_PINSRW;
4968 else if (VT == MVT::v16i8)
4969 Opc = X86ISD::PINSRB;
4970 else
4971 Opc = X86ISD::PINSRB;
4972
Nate Begeman14d12ca2008-02-11 04:19:36 +00004973 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4974 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004975 if (N1.getValueType() != MVT::i32)
4976 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4977 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004978 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004979 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004980 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004981 // Bits [7:6] of the constant are the source select. This will always be
4982 // zero here. The DAG Combiner may combine an extract_elt index into these
4983 // bits. For example (insert (extract, 3), 2) could be matched by putting
4984 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004985 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004986 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004987 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004988 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004989 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004990 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004991 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004992 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004993 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004994 // PINSR* works with constant index.
4995 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004996 }
Dan Gohman475871a2008-07-27 21:46:04 +00004997 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004998}
4999
Dan Gohman475871a2008-07-27 21:46:04 +00005000SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005001X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005002 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005003 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005004
5005 if (Subtarget->hasSSE41())
5006 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5007
Dan Gohman8a55ce42009-09-23 21:02:20 +00005008 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005009 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005010
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005011 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005012 SDValue N0 = Op.getOperand(0);
5013 SDValue N1 = Op.getOperand(1);
5014 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005015
Dan Gohman8a55ce42009-09-23 21:02:20 +00005016 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005017 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5018 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005019 if (N1.getValueType() != MVT::i32)
5020 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5021 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005022 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005023 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5024 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005025 }
Dan Gohman475871a2008-07-27 21:46:04 +00005026 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005027}
5028
Dan Gohman475871a2008-07-27 21:46:04 +00005029SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005030X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005031 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005032 if (Op.getValueType() == MVT::v2f32)
5033 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
5034 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5035 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00005036 Op.getOperand(0))));
5037
Owen Anderson825b72b2009-08-11 20:47:22 +00005038 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5039 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005040
Owen Anderson825b72b2009-08-11 20:47:22 +00005041 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5042 EVT VT = MVT::v2i32;
5043 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005044 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005045 case MVT::v16i8:
5046 case MVT::v8i16:
5047 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005048 break;
5049 }
Dale Johannesenace16102009-02-03 19:33:06 +00005050 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5051 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005052}
5053
Bill Wendling056292f2008-09-16 21:48:12 +00005054// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5055// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5056// one of the above mentioned nodes. It has to be wrapped because otherwise
5057// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5058// be used to form addressing mode. These wrapped nodes will be selected
5059// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005060SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005061X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005062 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005063
Chris Lattner41621a22009-06-26 19:22:52 +00005064 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5065 // global base reg.
5066 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005067 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005068 CodeModel::Model M = getTargetMachine().getCodeModel();
5069
Chris Lattner4f066492009-07-11 20:29:19 +00005070 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005071 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005072 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005073 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005074 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005075 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005076 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005077
Evan Cheng1606e8e2009-03-13 07:51:59 +00005078 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005079 CP->getAlignment(),
5080 CP->getOffset(), OpFlag);
5081 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005082 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005083 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005084 if (OpFlag) {
5085 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005086 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005087 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005088 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005089 }
5090
5091 return Result;
5092}
5093
Dan Gohmand858e902010-04-17 15:26:15 +00005094SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005095 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005096
Chris Lattner18c59872009-06-27 04:16:01 +00005097 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5098 // global base reg.
5099 unsigned char OpFlag = 0;
5100 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005101 CodeModel::Model M = getTargetMachine().getCodeModel();
5102
Chris Lattner4f066492009-07-11 20:29:19 +00005103 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005104 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005105 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005106 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005107 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005108 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005109 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005110
Chris Lattner18c59872009-06-27 04:16:01 +00005111 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5112 OpFlag);
5113 DebugLoc DL = JT->getDebugLoc();
5114 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005115
Chris Lattner18c59872009-06-27 04:16:01 +00005116 // With PIC, the address is actually $g + Offset.
5117 if (OpFlag) {
5118 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5119 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005120 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005121 Result);
5122 }
Eric Christopherfd179292009-08-27 18:07:15 +00005123
Chris Lattner18c59872009-06-27 04:16:01 +00005124 return Result;
5125}
5126
5127SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005128X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005129 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005130
Chris Lattner18c59872009-06-27 04:16:01 +00005131 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5132 // global base reg.
5133 unsigned char OpFlag = 0;
5134 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005135 CodeModel::Model M = getTargetMachine().getCodeModel();
5136
Chris Lattner4f066492009-07-11 20:29:19 +00005137 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005138 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005139 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005140 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005141 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005142 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005143 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005144
Chris Lattner18c59872009-06-27 04:16:01 +00005145 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005146
Chris Lattner18c59872009-06-27 04:16:01 +00005147 DebugLoc DL = Op.getDebugLoc();
5148 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005149
5150
Chris Lattner18c59872009-06-27 04:16:01 +00005151 // With PIC, the address is actually $g + Offset.
5152 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005153 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005154 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5155 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005156 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005157 Result);
5158 }
Eric Christopherfd179292009-08-27 18:07:15 +00005159
Chris Lattner18c59872009-06-27 04:16:01 +00005160 return Result;
5161}
5162
Dan Gohman475871a2008-07-27 21:46:04 +00005163SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005164X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005165 // Create the TargetBlockAddressAddress node.
5166 unsigned char OpFlags =
5167 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005168 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005169 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005170 DebugLoc dl = Op.getDebugLoc();
5171 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5172 /*isTarget=*/true, OpFlags);
5173
Dan Gohmanf705adb2009-10-30 01:28:02 +00005174 if (Subtarget->isPICStyleRIPRel() &&
5175 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005176 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5177 else
5178 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005179
Dan Gohman29cbade2009-11-20 23:18:13 +00005180 // With PIC, the address is actually $g + Offset.
5181 if (isGlobalRelativeToPICBase(OpFlags)) {
5182 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5183 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5184 Result);
5185 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005186
5187 return Result;
5188}
5189
5190SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005191X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005192 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005193 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005194 // Create the TargetGlobalAddress node, folding in the constant
5195 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005196 unsigned char OpFlags =
5197 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005198 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005199 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005200 if (OpFlags == X86II::MO_NO_FLAG &&
5201 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005202 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005203 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005204 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005205 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005206 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005207 }
Eric Christopherfd179292009-08-27 18:07:15 +00005208
Chris Lattner4f066492009-07-11 20:29:19 +00005209 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005210 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005211 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5212 else
5213 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005214
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005215 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005216 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005217 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5218 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005219 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005220 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005221
Chris Lattner36c25012009-07-10 07:34:39 +00005222 // For globals that require a load from a stub to get the address, emit the
5223 // load.
5224 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005225 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005226 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005227
Dan Gohman6520e202008-10-18 02:06:02 +00005228 // If there was a non-zero offset that we didn't fold, create an explicit
5229 // addition for it.
5230 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005231 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005232 DAG.getConstant(Offset, getPointerTy()));
5233
Evan Cheng0db9fe62006-04-25 20:13:52 +00005234 return Result;
5235}
5236
Evan Chengda43bcf2008-09-24 00:05:32 +00005237SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005238X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00005239 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005240 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005241 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005242}
5243
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005244static SDValue
5245GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005246 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005247 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005248 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005249 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005250 DebugLoc dl = GA->getDebugLoc();
5251 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5252 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005253 GA->getOffset(),
5254 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005255 if (InFlag) {
5256 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005257 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005258 } else {
5259 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005260 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005261 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005262
5263 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5264 MFI->setHasCalls(true);
5265
Rafael Espindola15f1b662009-04-24 12:59:40 +00005266 SDValue Flag = Chain.getValue(1);
5267 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005268}
5269
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005270// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005271static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005272LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005273 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005274 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005275 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5276 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005277 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005278 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005279 InFlag = Chain.getValue(1);
5280
Chris Lattnerb903bed2009-06-26 21:20:29 +00005281 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005282}
5283
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005284// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005285static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005286LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005287 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005288 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5289 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005290}
5291
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005292// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5293// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005294static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005295 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005296 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005297 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005298 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005299 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005300 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005301 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005302 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005303
5304 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005305 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005306
Chris Lattnerb903bed2009-06-26 21:20:29 +00005307 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005308 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5309 // initialexec.
5310 unsigned WrapperKind = X86ISD::Wrapper;
5311 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005312 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005313 } else if (is64Bit) {
5314 assert(model == TLSModel::InitialExec);
5315 OperandFlags = X86II::MO_GOTTPOFF;
5316 WrapperKind = X86ISD::WrapperRIP;
5317 } else {
5318 assert(model == TLSModel::InitialExec);
5319 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005320 }
Eric Christopherfd179292009-08-27 18:07:15 +00005321
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005322 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5323 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005324 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005325 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005326 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005327
Rafael Espindola9a580232009-02-27 13:37:18 +00005328 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005329 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005330 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005331
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005332 // The address of the thread local variable is the add of the thread
5333 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005334 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005335}
5336
Dan Gohman475871a2008-07-27 21:46:04 +00005337SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005338X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005339 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005340 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005341 assert(Subtarget->isTargetELF() &&
5342 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005343 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005344 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005345
Chris Lattnerb903bed2009-06-26 21:20:29 +00005346 // If GV is an alias then use the aliasee for determining
5347 // thread-localness.
5348 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5349 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005350
Chris Lattnerb903bed2009-06-26 21:20:29 +00005351 TLSModel::Model model = getTLSModel(GV,
5352 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005353
Chris Lattnerb903bed2009-06-26 21:20:29 +00005354 switch (model) {
5355 case TLSModel::GeneralDynamic:
5356 case TLSModel::LocalDynamic: // not implemented
5357 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005358 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005359 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005360
Chris Lattnerb903bed2009-06-26 21:20:29 +00005361 case TLSModel::InitialExec:
5362 case TLSModel::LocalExec:
5363 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5364 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005365 }
Eric Christopherfd179292009-08-27 18:07:15 +00005366
Torok Edwinc23197a2009-07-14 16:55:14 +00005367 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005368 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005369}
5370
Evan Cheng0db9fe62006-04-25 20:13:52 +00005371
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005372/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005373/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005374SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005375 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005376 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005377 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005378 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005379 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005380 SDValue ShOpLo = Op.getOperand(0);
5381 SDValue ShOpHi = Op.getOperand(1);
5382 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005383 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005384 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005385 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005386
Dan Gohman475871a2008-07-27 21:46:04 +00005387 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005388 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005389 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5390 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005391 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005392 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5393 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005394 }
Evan Chenge3413162006-01-09 18:33:28 +00005395
Owen Anderson825b72b2009-08-11 20:47:22 +00005396 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5397 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005398 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005399 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005400
Dan Gohman475871a2008-07-27 21:46:04 +00005401 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005402 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005403 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5404 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005405
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005406 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005407 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5408 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005409 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005410 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5411 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005412 }
5413
Dan Gohman475871a2008-07-27 21:46:04 +00005414 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005415 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005416}
Evan Chenga3195e82006-01-12 22:54:21 +00005417
Dan Gohmand858e902010-04-17 15:26:15 +00005418SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5419 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005420 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005421
5422 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005423 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005424 return Op;
5425 }
5426 return SDValue();
5427 }
5428
Owen Anderson825b72b2009-08-11 20:47:22 +00005429 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005430 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005431
Eli Friedman36df4992009-05-27 00:47:34 +00005432 // These are really Legal; return the operand so the caller accepts it as
5433 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005434 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005435 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005436 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005437 Subtarget->is64Bit()) {
5438 return Op;
5439 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005440
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005441 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005442 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005443 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005444 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005445 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005446 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005447 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005448 PseudoSourceValue::getFixedStack(SSFI), 0,
5449 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005450 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5451}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005452
Owen Andersone50ed302009-08-10 22:56:29 +00005453SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005454 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005455 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005456 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005457 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005458 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005459 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005460 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005461 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005462 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005463 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005464 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005465 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005466 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005467
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005468 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005469 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005470 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005471
5472 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5473 // shouldn't be necessary except that RFP cannot be live across
5474 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005475 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005476 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005477 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005478 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005479 SDValue Ops[] = {
5480 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5481 };
5482 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005483 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005484 PseudoSourceValue::getFixedStack(SSFI), 0,
5485 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005486 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005487
Evan Cheng0db9fe62006-04-25 20:13:52 +00005488 return Result;
5489}
5490
Bill Wendling8b8a6362009-01-17 03:56:04 +00005491// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005492SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5493 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005494 // This algorithm is not obvious. Here it is in C code, more or less:
5495 /*
5496 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5497 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5498 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005499
Bill Wendling8b8a6362009-01-17 03:56:04 +00005500 // Copy ints to xmm registers.
5501 __m128i xh = _mm_cvtsi32_si128( hi );
5502 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005503
Bill Wendling8b8a6362009-01-17 03:56:04 +00005504 // Combine into low half of a single xmm register.
5505 __m128i x = _mm_unpacklo_epi32( xh, xl );
5506 __m128d d;
5507 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005508
Bill Wendling8b8a6362009-01-17 03:56:04 +00005509 // Merge in appropriate exponents to give the integer bits the right
5510 // magnitude.
5511 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005512
Bill Wendling8b8a6362009-01-17 03:56:04 +00005513 // Subtract away the biases to deal with the IEEE-754 double precision
5514 // implicit 1.
5515 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005516
Bill Wendling8b8a6362009-01-17 03:56:04 +00005517 // All conversions up to here are exact. The correctly rounded result is
5518 // calculated using the current rounding mode using the following
5519 // horizontal add.
5520 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5521 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5522 // store doesn't really need to be here (except
5523 // maybe to zero the other double)
5524 return sd;
5525 }
5526 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005527
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005528 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005529 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005530
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005531 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005532 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005533 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5534 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5535 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5536 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005537 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005538 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005539
Bill Wendling8b8a6362009-01-17 03:56:04 +00005540 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005541 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005542 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005543 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005544 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005545 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005546 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005547
Owen Anderson825b72b2009-08-11 20:47:22 +00005548 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5549 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005550 Op.getOperand(0),
5551 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005552 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5553 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005554 Op.getOperand(0),
5555 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005556 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5557 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005558 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005559 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005560 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5561 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5562 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005563 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005564 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005565 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005566
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005567 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005568 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005569 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5570 DAG.getUNDEF(MVT::v2f64), ShufMask);
5571 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5572 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005573 DAG.getIntPtrConstant(0));
5574}
5575
Bill Wendling8b8a6362009-01-17 03:56:04 +00005576// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005577SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5578 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005579 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005580 // FP constant to bias correct the final result.
5581 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005582 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005583
5584 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5586 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005587 Op.getOperand(0),
5588 DAG.getIntPtrConstant(0)));
5589
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5591 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005592 DAG.getIntPtrConstant(0));
5593
5594 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005595 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5596 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005597 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 MVT::v2f64, Load)),
5599 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005600 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005601 MVT::v2f64, Bias)));
5602 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5603 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005604 DAG.getIntPtrConstant(0));
5605
5606 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005607 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005608
5609 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005610 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005611
Owen Anderson825b72b2009-08-11 20:47:22 +00005612 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005613 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005614 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005615 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005616 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005617 }
5618
5619 // Handle final rounding.
5620 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005621}
5622
Dan Gohmand858e902010-04-17 15:26:15 +00005623SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5624 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005625 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005626 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005627
Evan Chenga06ec9e2009-01-19 08:08:22 +00005628 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5629 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5630 // the optimization here.
5631 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005632 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005633
Owen Andersone50ed302009-08-10 22:56:29 +00005634 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005635 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005636 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005637 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005638 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005639
Bill Wendling8b8a6362009-01-17 03:56:04 +00005640 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005641 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005642 return LowerUINT_TO_FP_i32(Op, DAG);
5643 }
5644
Owen Anderson825b72b2009-08-11 20:47:22 +00005645 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005646
5647 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005648 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005649 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5650 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5651 getPointerTy(), StackSlot, WordOff);
5652 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005653 StackSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005654 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00005655 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005657}
5658
Dan Gohman475871a2008-07-27 21:46:04 +00005659std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005660FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005661 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005662
Owen Andersone50ed302009-08-10 22:56:29 +00005663 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005664
5665 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5667 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005668 }
5669
Owen Anderson825b72b2009-08-11 20:47:22 +00005670 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5671 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005672 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005673
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005674 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005675 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005676 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005677 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005678 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005679 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005680 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005681 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005682
Evan Cheng87c89352007-10-15 20:11:21 +00005683 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5684 // stack slot.
5685 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005686 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005687 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005688 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005689
Evan Cheng0db9fe62006-04-25 20:13:52 +00005690 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005691 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005692 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005693 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5694 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5695 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005696 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005697
Dan Gohman475871a2008-07-27 21:46:04 +00005698 SDValue Chain = DAG.getEntryNode();
5699 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005700 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005701 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005702 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005703 PseudoSourceValue::getFixedStack(SSFI), 0,
5704 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005705 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005706 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005707 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5708 };
Dale Johannesenace16102009-02-03 19:33:06 +00005709 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005710 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005711 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005712 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5713 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005714
Evan Cheng0db9fe62006-04-25 20:13:52 +00005715 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005716 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005717 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005718
Chris Lattner27a6c732007-11-24 07:07:01 +00005719 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005720}
5721
Dan Gohmand858e902010-04-17 15:26:15 +00005722SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5723 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005724 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005725 if (Op.getValueType() == MVT::v2i32 &&
5726 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005727 return Op;
5728 }
5729 return SDValue();
5730 }
5731
Eli Friedman948e95a2009-05-23 09:59:16 +00005732 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005733 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005734 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5735 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005736
Chris Lattner27a6c732007-11-24 07:07:01 +00005737 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005738 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005739 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005740}
5741
Dan Gohmand858e902010-04-17 15:26:15 +00005742SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5743 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005744 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5745 SDValue FIST = Vals.first, StackSlot = Vals.second;
5746 assert(FIST.getNode() && "Unexpected failure");
5747
5748 // Load the result.
5749 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005750 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005751}
5752
Dan Gohmand858e902010-04-17 15:26:15 +00005753SDValue X86TargetLowering::LowerFABS(SDValue Op,
5754 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005755 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005756 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005757 EVT VT = Op.getValueType();
5758 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005759 if (VT.isVector())
5760 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005761 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005762 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005763 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005764 CV.push_back(C);
5765 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005766 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005767 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005768 CV.push_back(C);
5769 CV.push_back(C);
5770 CV.push_back(C);
5771 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005772 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005773 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005774 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005775 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005776 PseudoSourceValue::getConstantPool(), 0,
5777 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005778 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005779}
5780
Dan Gohmand858e902010-04-17 15:26:15 +00005781SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005782 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005783 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005784 EVT VT = Op.getValueType();
5785 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005786 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005787 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005788 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005789 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005790 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005791 CV.push_back(C);
5792 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005793 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005794 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005795 CV.push_back(C);
5796 CV.push_back(C);
5797 CV.push_back(C);
5798 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005799 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005800 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005801 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005802 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005803 PseudoSourceValue::getConstantPool(), 0,
5804 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005805 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005806 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005807 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5808 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005809 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005810 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005811 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005812 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005813 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005814}
5815
Dan Gohmand858e902010-04-17 15:26:15 +00005816SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005817 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005818 SDValue Op0 = Op.getOperand(0);
5819 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005820 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005821 EVT VT = Op.getValueType();
5822 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005823
5824 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005825 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005826 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005827 SrcVT = VT;
5828 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005829 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005830 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005831 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005832 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005833 }
5834
5835 // At this point the operands and the result should have the same
5836 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005837
Evan Cheng68c47cb2007-01-05 07:55:56 +00005838 // First get the sign bit of second operand.
5839 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005840 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005841 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5842 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005843 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005844 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5845 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5846 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5847 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005848 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005849 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005850 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005851 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005852 PseudoSourceValue::getConstantPool(), 0,
5853 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005854 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005855
5856 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005857 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005858 // Op0 is MVT::f32, Op1 is MVT::f64.
5859 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5860 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5861 DAG.getConstant(32, MVT::i32));
5862 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5863 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005864 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005865 }
5866
Evan Cheng73d6cf12007-01-05 21:37:56 +00005867 // Clear first operand sign bit.
5868 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005869 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005870 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5871 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005872 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005873 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5874 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5875 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5876 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005877 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005878 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005879 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005880 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005881 PseudoSourceValue::getConstantPool(), 0,
5882 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005883 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005884
5885 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005886 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005887}
5888
Dan Gohman076aee32009-03-04 19:44:21 +00005889/// Emit nodes that will be selected as "test Op0,Op0", or something
5890/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005891SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Dan Gohmand858e902010-04-17 15:26:15 +00005892 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00005893 DebugLoc dl = Op.getDebugLoc();
5894
Dan Gohman31125812009-03-07 01:58:32 +00005895 // CF and OF aren't always set the way we want. Determine which
5896 // of these we need.
5897 bool NeedCF = false;
5898 bool NeedOF = false;
5899 switch (X86CC) {
5900 case X86::COND_A: case X86::COND_AE:
5901 case X86::COND_B: case X86::COND_BE:
5902 NeedCF = true;
5903 break;
5904 case X86::COND_G: case X86::COND_GE:
5905 case X86::COND_L: case X86::COND_LE:
5906 case X86::COND_O: case X86::COND_NO:
5907 NeedOF = true;
5908 break;
5909 default: break;
5910 }
5911
Dan Gohman076aee32009-03-04 19:44:21 +00005912 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005913 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5914 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5915 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005916 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005917 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005918 switch (Op.getNode()->getOpcode()) {
5919 case ISD::ADD:
5920 // Due to an isel shortcoming, be conservative if this add is likely to
5921 // be selected as part of a load-modify-store instruction. When the root
5922 // node in a match is a store, isel doesn't know how to remap non-chain
5923 // non-flag uses of other nodes in the match, such as the ADD in this
5924 // case. This leads to the ADD being left around and reselected, with
5925 // the result being two adds in the output.
5926 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5927 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5928 if (UI->getOpcode() == ISD::STORE)
5929 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005930 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005931 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5932 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005933 if (C->getAPIntValue() == 1) {
5934 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005935 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005936 break;
5937 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005938 // An add of negative one (subtract of one) will be selected as a DEC.
5939 if (C->getAPIntValue().isAllOnesValue()) {
5940 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005941 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005942 break;
5943 }
5944 }
Dan Gohman076aee32009-03-04 19:44:21 +00005945 // Otherwise use a regular EFLAGS-setting add.
5946 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005947 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005948 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005949 case ISD::AND: {
5950 // If the primary and result isn't used, don't bother using X86ISD::AND,
5951 // because a TEST instruction will be better.
5952 bool NonFlagUse = false;
5953 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005954 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5955 SDNode *User = *UI;
5956 unsigned UOpNo = UI.getOperandNo();
5957 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5958 // Look pass truncate.
5959 UOpNo = User->use_begin().getOperandNo();
5960 User = *User->use_begin();
5961 }
5962 if (User->getOpcode() != ISD::BRCOND &&
5963 User->getOpcode() != ISD::SETCC &&
5964 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005965 NonFlagUse = true;
5966 break;
5967 }
Evan Cheng17751da2010-01-07 00:54:06 +00005968 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005969 if (!NonFlagUse)
5970 break;
5971 }
5972 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005973 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005974 case ISD::OR:
5975 case ISD::XOR:
5976 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005977 // likely to be selected as part of a load-modify-store instruction.
5978 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5979 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5980 if (UI->getOpcode() == ISD::STORE)
5981 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005982 // Otherwise use a regular EFLAGS-setting instruction.
5983 switch (Op.getNode()->getOpcode()) {
5984 case ISD::SUB: Opcode = X86ISD::SUB; break;
5985 case ISD::OR: Opcode = X86ISD::OR; break;
5986 case ISD::XOR: Opcode = X86ISD::XOR; break;
5987 case ISD::AND: Opcode = X86ISD::AND; break;
5988 default: llvm_unreachable("unexpected operator!");
5989 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005990 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005991 break;
5992 case X86ISD::ADD:
5993 case X86ISD::SUB:
5994 case X86ISD::INC:
5995 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005996 case X86ISD::OR:
5997 case X86ISD::XOR:
5998 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005999 return SDValue(Op.getNode(), 1);
6000 default:
6001 default_case:
6002 break;
6003 }
6004 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006005 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00006006 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00006007 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00006008 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00006009 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00006010 DAG.ReplaceAllUsesWith(Op, New);
6011 return SDValue(New.getNode(), 1);
6012 }
6013 }
6014
6015 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006016 if (Subtarget->shouldPromote16Bit() && Op.getValueType() == MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +00006017 Op = DAG.getNode(ISD::ANY_EXTEND, Op.getDebugLoc(), MVT::i32, Op);
Owen Anderson825b72b2009-08-11 20:47:22 +00006018 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00006019 DAG.getConstant(0, Op.getValueType()));
6020}
6021
6022/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6023/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006024SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Dan Gohmand858e902010-04-17 15:26:15 +00006025 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006026 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6027 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00006028 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006029
6030 DebugLoc dl = Op0.getDebugLoc();
Evan Cheng5528e7b2010-04-21 01:47:12 +00006031 if (Subtarget->shouldPromote16Bit() && Op0.getValueType() == MVT::i16) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006032 Op0 = DAG.getNode(ISD::ANY_EXTEND, Op0.getDebugLoc(), MVT::i32, Op0);
6033 Op1 = DAG.getNode(ISD::ANY_EXTEND, Op1.getDebugLoc(), MVT::i32, Op1);
6034 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006035 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006036}
6037
Evan Chengd40d03e2010-01-06 19:38:29 +00006038/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6039/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006040SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6041 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006042 SDValue Op0 = And.getOperand(0);
6043 SDValue Op1 = And.getOperand(1);
6044 if (Op0.getOpcode() == ISD::TRUNCATE)
6045 Op0 = Op0.getOperand(0);
6046 if (Op1.getOpcode() == ISD::TRUNCATE)
6047 Op1 = Op1.getOperand(0);
6048
Evan Chengd40d03e2010-01-06 19:38:29 +00006049 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00006050 if (Op1.getOpcode() == ISD::SHL) {
6051 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6052 if (And10C->getZExtValue() == 1) {
6053 LHS = Op0;
6054 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006055 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006056 } else if (Op0.getOpcode() == ISD::SHL) {
6057 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6058 if (And00C->getZExtValue() == 1) {
6059 LHS = Op1;
6060 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006061 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006062 } else if (Op1.getOpcode() == ISD::Constant) {
6063 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6064 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006065 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6066 LHS = AndLHS.getOperand(0);
6067 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006068 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006069 }
Evan Cheng0488db92007-09-25 01:57:46 +00006070
Evan Chengd40d03e2010-01-06 19:38:29 +00006071 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006072 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006073 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006074 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006075 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006076 // Also promote i16 to i32 for performance / code size reason.
6077 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng5528e7b2010-04-21 01:47:12 +00006078 (Subtarget->shouldPromote16Bit() && LHS.getValueType() == MVT::i16))
Evan Chengd40d03e2010-01-06 19:38:29 +00006079 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006080
Evan Chengd40d03e2010-01-06 19:38:29 +00006081 // If the operand types disagree, extend the shift amount to match. Since
6082 // BT ignores high bits (like shifts) we can use anyextend.
6083 if (LHS.getValueType() != RHS.getValueType())
6084 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006085
Evan Chengd40d03e2010-01-06 19:38:29 +00006086 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6087 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6088 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6089 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006090 }
6091
Evan Cheng54de3ea2010-01-05 06:52:31 +00006092 return SDValue();
6093}
6094
Dan Gohmand858e902010-04-17 15:26:15 +00006095SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006096 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6097 SDValue Op0 = Op.getOperand(0);
6098 SDValue Op1 = Op.getOperand(1);
6099 DebugLoc dl = Op.getDebugLoc();
6100 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6101
6102 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006103 // Lower (X & (1 << N)) == 0 to BT(X, N).
6104 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6105 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6106 if (Op0.getOpcode() == ISD::AND &&
6107 Op0.hasOneUse() &&
6108 Op1.getOpcode() == ISD::Constant &&
6109 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6110 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6111 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6112 if (NewSetCC.getNode())
6113 return NewSetCC;
6114 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006115
Evan Cheng2c755ba2010-02-27 07:36:59 +00006116 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6117 if (Op0.getOpcode() == X86ISD::SETCC &&
6118 Op1.getOpcode() == ISD::Constant &&
6119 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6120 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6121 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6122 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6123 bool Invert = (CC == ISD::SETNE) ^
6124 cast<ConstantSDNode>(Op1)->isNullValue();
6125 if (Invert)
6126 CCode = X86::GetOppositeBranchCondition(CCode);
6127 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6128 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6129 }
6130
Evan Chenge5b51ac2010-04-17 06:13:15 +00006131 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006132 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006133 if (X86CC == X86::COND_INVALID)
6134 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006135
Dan Gohman31125812009-03-07 01:58:32 +00006136 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006137
6138 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006139 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006140 return DAG.getNode(ISD::AND, dl, MVT::i8,
6141 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6142 DAG.getConstant(X86CC, MVT::i8), Cond),
6143 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006144
Owen Anderson825b72b2009-08-11 20:47:22 +00006145 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6146 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006147}
6148
Dan Gohmand858e902010-04-17 15:26:15 +00006149SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006150 SDValue Cond;
6151 SDValue Op0 = Op.getOperand(0);
6152 SDValue Op1 = Op.getOperand(1);
6153 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006154 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006155 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6156 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006157 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006158
6159 if (isFP) {
6160 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006161 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006162 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6163 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006164 bool Swap = false;
6165
6166 switch (SetCCOpcode) {
6167 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006168 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006169 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006170 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006171 case ISD::SETGT: Swap = true; // Fallthrough
6172 case ISD::SETLT:
6173 case ISD::SETOLT: SSECC = 1; break;
6174 case ISD::SETOGE:
6175 case ISD::SETGE: Swap = true; // Fallthrough
6176 case ISD::SETLE:
6177 case ISD::SETOLE: SSECC = 2; break;
6178 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006179 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006180 case ISD::SETNE: SSECC = 4; break;
6181 case ISD::SETULE: Swap = true;
6182 case ISD::SETUGE: SSECC = 5; break;
6183 case ISD::SETULT: Swap = true;
6184 case ISD::SETUGT: SSECC = 6; break;
6185 case ISD::SETO: SSECC = 7; break;
6186 }
6187 if (Swap)
6188 std::swap(Op0, Op1);
6189
Nate Begemanfb8ead02008-07-25 19:05:58 +00006190 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006191 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006192 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006193 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006194 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6195 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006196 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006197 }
6198 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006199 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006200 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6201 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006202 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006203 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006204 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006205 }
6206 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006207 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006208 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006209
Nate Begeman30a0de92008-07-17 16:51:19 +00006210 // We are handling one of the integer comparisons here. Since SSE only has
6211 // GT and EQ comparisons for integer, swapping operands and multiple
6212 // operations may be required for some comparisons.
6213 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6214 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006215
Owen Anderson825b72b2009-08-11 20:47:22 +00006216 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006217 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006218 case MVT::v8i8:
6219 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6220 case MVT::v4i16:
6221 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6222 case MVT::v2i32:
6223 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6224 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006225 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006226
Nate Begeman30a0de92008-07-17 16:51:19 +00006227 switch (SetCCOpcode) {
6228 default: break;
6229 case ISD::SETNE: Invert = true;
6230 case ISD::SETEQ: Opc = EQOpc; break;
6231 case ISD::SETLT: Swap = true;
6232 case ISD::SETGT: Opc = GTOpc; break;
6233 case ISD::SETGE: Swap = true;
6234 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6235 case ISD::SETULT: Swap = true;
6236 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6237 case ISD::SETUGE: Swap = true;
6238 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6239 }
6240 if (Swap)
6241 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006242
Nate Begeman30a0de92008-07-17 16:51:19 +00006243 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6244 // bits of the inputs before performing those operations.
6245 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006246 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006247 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6248 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006249 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006250 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6251 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006252 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6253 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006254 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006255
Dale Johannesenace16102009-02-03 19:33:06 +00006256 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006257
6258 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006259 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006260 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006261
Nate Begeman30a0de92008-07-17 16:51:19 +00006262 return Result;
6263}
Evan Cheng0488db92007-09-25 01:57:46 +00006264
Evan Cheng370e5342008-12-03 08:38:43 +00006265// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006266static bool isX86LogicalCmp(SDValue Op) {
6267 unsigned Opc = Op.getNode()->getOpcode();
6268 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6269 return true;
6270 if (Op.getResNo() == 1 &&
6271 (Opc == X86ISD::ADD ||
6272 Opc == X86ISD::SUB ||
6273 Opc == X86ISD::SMUL ||
6274 Opc == X86ISD::UMUL ||
6275 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006276 Opc == X86ISD::DEC ||
6277 Opc == X86ISD::OR ||
6278 Opc == X86ISD::XOR ||
6279 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006280 return true;
6281
6282 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006283}
6284
Dan Gohmand858e902010-04-17 15:26:15 +00006285SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006286 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006287 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006288 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006289 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006290
Dan Gohman1a492952009-10-20 16:22:37 +00006291 if (Cond.getOpcode() == ISD::SETCC) {
6292 SDValue NewCond = LowerSETCC(Cond, DAG);
6293 if (NewCond.getNode())
6294 Cond = NewCond;
6295 }
Evan Cheng734503b2006-09-11 02:19:56 +00006296
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006297 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6298 SDValue Op1 = Op.getOperand(1);
6299 SDValue Op2 = Op.getOperand(2);
6300 if (Cond.getOpcode() == X86ISD::SETCC &&
6301 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6302 SDValue Cmp = Cond.getOperand(1);
6303 if (Cmp.getOpcode() == X86ISD::CMP) {
6304 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6305 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6306 ConstantSDNode *RHSC =
6307 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6308 if (N1C && N1C->isAllOnesValue() &&
6309 N2C && N2C->isNullValue() &&
6310 RHSC && RHSC->isNullValue()) {
6311 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006312 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006313 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6314 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6315 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6316 }
6317 }
6318 }
6319
Evan Chengad9c0a32009-12-15 00:53:42 +00006320 // Look pass (and (setcc_carry (cmp ...)), 1).
6321 if (Cond.getOpcode() == ISD::AND &&
6322 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6323 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6324 if (C && C->getAPIntValue() == 1)
6325 Cond = Cond.getOperand(0);
6326 }
6327
Evan Cheng3f41d662007-10-08 22:16:29 +00006328 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6329 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006330 if (Cond.getOpcode() == X86ISD::SETCC ||
6331 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006332 CC = Cond.getOperand(0);
6333
Dan Gohman475871a2008-07-27 21:46:04 +00006334 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006335 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006336 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006337
Evan Cheng3f41d662007-10-08 22:16:29 +00006338 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006339 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006340 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006341 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006342
Chris Lattnerd1980a52009-03-12 06:52:53 +00006343 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6344 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006345 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006346 addTest = false;
6347 }
6348 }
6349
6350 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006351 // Look pass the truncate.
6352 if (Cond.getOpcode() == ISD::TRUNCATE)
6353 Cond = Cond.getOperand(0);
6354
6355 // We know the result of AND is compared against zero. Try to match
6356 // it to BT.
6357 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6358 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6359 if (NewSetCC.getNode()) {
6360 CC = NewSetCC.getOperand(0);
6361 Cond = NewSetCC.getOperand(1);
6362 addTest = false;
6363 }
6364 }
6365 }
6366
6367 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006368 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006369 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006370 }
6371
Evan Cheng0488db92007-09-25 01:57:46 +00006372 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6373 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006374 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6375 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006376 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006377}
6378
Evan Cheng370e5342008-12-03 08:38:43 +00006379// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6380// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6381// from the AND / OR.
6382static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6383 Opc = Op.getOpcode();
6384 if (Opc != ISD::OR && Opc != ISD::AND)
6385 return false;
6386 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6387 Op.getOperand(0).hasOneUse() &&
6388 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6389 Op.getOperand(1).hasOneUse());
6390}
6391
Evan Cheng961d6d42009-02-02 08:19:07 +00006392// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6393// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006394static bool isXor1OfSetCC(SDValue Op) {
6395 if (Op.getOpcode() != ISD::XOR)
6396 return false;
6397 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6398 if (N1C && N1C->getAPIntValue() == 1) {
6399 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6400 Op.getOperand(0).hasOneUse();
6401 }
6402 return false;
6403}
6404
Dan Gohmand858e902010-04-17 15:26:15 +00006405SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006406 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006407 SDValue Chain = Op.getOperand(0);
6408 SDValue Cond = Op.getOperand(1);
6409 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006410 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006411 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006412
Dan Gohman1a492952009-10-20 16:22:37 +00006413 if (Cond.getOpcode() == ISD::SETCC) {
6414 SDValue NewCond = LowerSETCC(Cond, DAG);
6415 if (NewCond.getNode())
6416 Cond = NewCond;
6417 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006418#if 0
6419 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006420 else if (Cond.getOpcode() == X86ISD::ADD ||
6421 Cond.getOpcode() == X86ISD::SUB ||
6422 Cond.getOpcode() == X86ISD::SMUL ||
6423 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006424 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006425#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006426
Evan Chengad9c0a32009-12-15 00:53:42 +00006427 // Look pass (and (setcc_carry (cmp ...)), 1).
6428 if (Cond.getOpcode() == ISD::AND &&
6429 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6430 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6431 if (C && C->getAPIntValue() == 1)
6432 Cond = Cond.getOperand(0);
6433 }
6434
Evan Cheng3f41d662007-10-08 22:16:29 +00006435 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6436 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006437 if (Cond.getOpcode() == X86ISD::SETCC ||
6438 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006439 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006440
Dan Gohman475871a2008-07-27 21:46:04 +00006441 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006442 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006443 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006444 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006445 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006446 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006447 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006448 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006449 default: break;
6450 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006451 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006452 // These can only come from an arithmetic instruction with overflow,
6453 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006454 Cond = Cond.getNode()->getOperand(1);
6455 addTest = false;
6456 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006457 }
Evan Cheng0488db92007-09-25 01:57:46 +00006458 }
Evan Cheng370e5342008-12-03 08:38:43 +00006459 } else {
6460 unsigned CondOpc;
6461 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6462 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006463 if (CondOpc == ISD::OR) {
6464 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6465 // two branches instead of an explicit OR instruction with a
6466 // separate test.
6467 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006468 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006469 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006470 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006471 Chain, Dest, CC, Cmp);
6472 CC = Cond.getOperand(1).getOperand(0);
6473 Cond = Cmp;
6474 addTest = false;
6475 }
6476 } else { // ISD::AND
6477 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6478 // two branches instead of an explicit AND instruction with a
6479 // separate test. However, we only do this if this block doesn't
6480 // have a fall-through edge, because this requires an explicit
6481 // jmp when the condition is false.
6482 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006483 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006484 Op.getNode()->hasOneUse()) {
6485 X86::CondCode CCode =
6486 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6487 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006488 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006489 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6490 // Look for an unconditional branch following this conditional branch.
6491 // We need this because we need to reverse the successors in order
6492 // to implement FCMP_OEQ.
6493 if (User.getOpcode() == ISD::BR) {
6494 SDValue FalseBB = User.getOperand(1);
6495 SDValue NewBR =
6496 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6497 assert(NewBR == User);
6498 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006499
Dale Johannesene4d209d2009-02-03 20:21:25 +00006500 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006501 Chain, Dest, CC, Cmp);
6502 X86::CondCode CCode =
6503 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6504 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006505 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006506 Cond = Cmp;
6507 addTest = false;
6508 }
6509 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006510 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006511 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6512 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6513 // It should be transformed during dag combiner except when the condition
6514 // is set by a arithmetics with overflow node.
6515 X86::CondCode CCode =
6516 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6517 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006518 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006519 Cond = Cond.getOperand(0).getOperand(1);
6520 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006521 }
Evan Cheng0488db92007-09-25 01:57:46 +00006522 }
6523
6524 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006525 // Look pass the truncate.
6526 if (Cond.getOpcode() == ISD::TRUNCATE)
6527 Cond = Cond.getOperand(0);
6528
6529 // We know the result of AND is compared against zero. Try to match
6530 // it to BT.
6531 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6532 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6533 if (NewSetCC.getNode()) {
6534 CC = NewSetCC.getOperand(0);
6535 Cond = NewSetCC.getOperand(1);
6536 addTest = false;
6537 }
6538 }
6539 }
6540
6541 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006542 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006543 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006544 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006545 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006546 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006547}
6548
Anton Korobeynikove060b532007-04-17 19:34:00 +00006549
6550// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6551// Calls to _alloca is needed to probe the stack when allocating more than 4k
6552// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6553// that the guard pages used by the OS virtual memory manager are allocated in
6554// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006555SDValue
6556X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006557 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006558 assert(Subtarget->isTargetCygMing() &&
6559 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006560 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006561
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006562 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006563 SDValue Chain = Op.getOperand(0);
6564 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006565 // FIXME: Ensure alignment here
6566
Dan Gohman475871a2008-07-27 21:46:04 +00006567 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006568
Owen Andersone50ed302009-08-10 22:56:29 +00006569 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006570 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006571
Dale Johannesendd64c412009-02-04 00:33:20 +00006572 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006573 Flag = Chain.getValue(1);
6574
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006575 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006576
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006577 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6578 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006579
Dale Johannesendd64c412009-02-04 00:33:20 +00006580 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006581
Dan Gohman475871a2008-07-27 21:46:04 +00006582 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006583 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006584}
6585
Dan Gohman475871a2008-07-27 21:46:04 +00006586SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006587X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006588 SDValue Chain,
6589 SDValue Dst, SDValue Src,
6590 SDValue Size, unsigned Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006591 bool isVolatile,
Bill Wendling6f287b22008-09-30 21:22:07 +00006592 const Value *DstSV,
Dan Gohmand858e902010-04-17 15:26:15 +00006593 uint64_t DstSVOff) const {
Dan Gohman707e0182008-04-12 04:36:06 +00006594 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006595
Bill Wendling6f287b22008-09-30 21:22:07 +00006596 // If not DWORD aligned or size is more than the threshold, call the library.
6597 // The libc version is likely to be faster for these cases. It can use the
6598 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006599 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006600 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006601 ConstantSize->getZExtValue() >
6602 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006603 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006604
6605 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006606 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006607
Bill Wendling6158d842008-10-01 00:59:58 +00006608 if (const char *bzeroEntry = V &&
6609 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006610 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006611 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006612 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006613 TargetLowering::ArgListEntry Entry;
6614 Entry.Node = Dst;
6615 Entry.Ty = IntPtrTy;
6616 Args.push_back(Entry);
6617 Entry.Node = Size;
6618 Args.push_back(Entry);
6619 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006620 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6621 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006622 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling46ada192010-03-02 01:55:18 +00006623 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00006624 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006625 }
6626
Dan Gohman707e0182008-04-12 04:36:06 +00006627 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006628 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006629 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006630
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006631 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006632 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006633 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006634 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006635 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006636 unsigned BytesLeft = 0;
6637 bool TwoRepStos = false;
6638 if (ValC) {
6639 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006640 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006641
Evan Cheng0db9fe62006-04-25 20:13:52 +00006642 // If the value is a constant, then we can potentially use larger sets.
6643 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006644 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006645 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006646 ValReg = X86::AX;
6647 Val = (Val << 8) | Val;
6648 break;
6649 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006650 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006651 ValReg = X86::EAX;
6652 Val = (Val << 8) | Val;
6653 Val = (Val << 16) | Val;
6654 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006655 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006656 ValReg = X86::RAX;
6657 Val = (Val << 32) | Val;
6658 }
6659 break;
6660 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006661 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006662 ValReg = X86::AL;
6663 Count = DAG.getIntPtrConstant(SizeVal);
6664 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006665 }
6666
Owen Anderson825b72b2009-08-11 20:47:22 +00006667 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006668 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006669 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6670 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006671 }
6672
Dale Johannesen0f502f62009-02-03 22:26:09 +00006673 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006674 InFlag);
6675 InFlag = Chain.getValue(1);
6676 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006677 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006678 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006679 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006680 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006681 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006682
Scott Michelfdc40a02009-02-17 22:15:04 +00006683 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006684 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006685 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006686 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006687 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006688 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006689 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006690 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006691
Owen Anderson825b72b2009-08-11 20:47:22 +00006692 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006693 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6694 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006695
Evan Cheng0db9fe62006-04-25 20:13:52 +00006696 if (TwoRepStos) {
6697 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006698 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006699 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006700 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006701 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6702 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006703 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006704 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006705 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006706 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006707 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6708 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006709 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006710 // Handle the last 1 - 7 bytes.
6711 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006712 EVT AddrVT = Dst.getValueType();
6713 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006714
Dale Johannesen0f502f62009-02-03 22:26:09 +00006715 Chain = DAG.getMemset(Chain, dl,
6716 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006717 DAG.getConstant(Offset, AddrVT)),
6718 Src,
6719 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang20adc9d2010-04-04 03:10:48 +00006720 Align, isVolatile, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006721 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006722
Dan Gohman707e0182008-04-12 04:36:06 +00006723 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006724 return Chain;
6725}
Evan Cheng11e15b32006-04-03 20:53:28 +00006726
Dan Gohman475871a2008-07-27 21:46:04 +00006727SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006728X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006729 SDValue Chain, SDValue Dst, SDValue Src,
6730 SDValue Size, unsigned Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006731 bool isVolatile, bool AlwaysInline,
Dan Gohmand858e902010-04-17 15:26:15 +00006732 const Value *DstSV,
6733 uint64_t DstSVOff,
6734 const Value *SrcSV,
6735 uint64_t SrcSVOff) const {
Dan Gohman707e0182008-04-12 04:36:06 +00006736 // This requires the copy size to be a constant, preferrably
6737 // within a subtarget-specific limit.
6738 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6739 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006740 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006741 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006742 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006743 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006744
Evan Cheng1887c1c2008-08-21 21:00:15 +00006745 /// If not DWORD aligned, call the library.
6746 if ((Align & 3) != 0)
6747 return SDValue();
6748
6749 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006750 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006751 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006752 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006753
Duncan Sands83ec4b62008-06-06 12:08:01 +00006754 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006755 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006756 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006757 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006758
Dan Gohman475871a2008-07-27 21:46:04 +00006759 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006760 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006761 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006762 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006763 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006764 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Evan Chengc3b0c342010-04-08 07:37:57 +00006765 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006766 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006767 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006768 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006769 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006770 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006771 InFlag = Chain.getValue(1);
6772
Owen Anderson825b72b2009-08-11 20:47:22 +00006773 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006774 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6775 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6776 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006777
Dan Gohman475871a2008-07-27 21:46:04 +00006778 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006779 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006780 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006781 // Handle the last 1 - 7 bytes.
6782 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006783 EVT DstVT = Dst.getValueType();
6784 EVT SrcVT = Src.getValueType();
6785 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006786 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006787 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006788 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006789 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006790 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006791 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang20adc9d2010-04-04 03:10:48 +00006792 Align, isVolatile, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006793 DstSV, DstSVOff + Offset,
6794 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006795 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006796
Owen Anderson825b72b2009-08-11 20:47:22 +00006797 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006798 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006799}
6800
Dan Gohmand858e902010-04-17 15:26:15 +00006801SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006802 MachineFunction &MF = DAG.getMachineFunction();
6803 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6804
Dan Gohman69de1932008-02-06 22:27:42 +00006805 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006806 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006807
Evan Cheng25ab6902006-09-08 06:48:29 +00006808 if (!Subtarget->is64Bit()) {
6809 // vastart just stores the address of the VarArgsFrameIndex slot into the
6810 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006811 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6812 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006813 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6814 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006815 }
6816
6817 // __va_list_tag:
6818 // gp_offset (0 - 6 * 8)
6819 // fp_offset (48 - 48 + 8 * 16)
6820 // overflow_arg_area (point to parameters coming in memory).
6821 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006822 SmallVector<SDValue, 8> MemOps;
6823 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006824 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006825 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006826 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6827 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006828 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006829 MemOps.push_back(Store);
6830
6831 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006832 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006833 FIN, DAG.getIntPtrConstant(4));
6834 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006835 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6836 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006837 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006838 MemOps.push_back(Store);
6839
6840 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006841 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006842 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006843 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6844 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006845 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6846 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006847 MemOps.push_back(Store);
6848
6849 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006850 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006851 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006852 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6853 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006854 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6855 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006856 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006857 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006858 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006859}
6860
Dan Gohmand858e902010-04-17 15:26:15 +00006861SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006862 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6863 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006864 SDValue Chain = Op.getOperand(0);
6865 SDValue SrcPtr = Op.getOperand(1);
6866 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006867
Chris Lattner75361b62010-04-07 22:58:41 +00006868 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006869 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006870}
6871
Dan Gohmand858e902010-04-17 15:26:15 +00006872SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006873 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006874 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006875 SDValue Chain = Op.getOperand(0);
6876 SDValue DstPtr = Op.getOperand(1);
6877 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006878 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6879 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006880 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006881
Dale Johannesendd64c412009-02-04 00:33:20 +00006882 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006883 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6884 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006885}
6886
Dan Gohman475871a2008-07-27 21:46:04 +00006887SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006888X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006889 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006890 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006891 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006892 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006893 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006894 case Intrinsic::x86_sse_comieq_ss:
6895 case Intrinsic::x86_sse_comilt_ss:
6896 case Intrinsic::x86_sse_comile_ss:
6897 case Intrinsic::x86_sse_comigt_ss:
6898 case Intrinsic::x86_sse_comige_ss:
6899 case Intrinsic::x86_sse_comineq_ss:
6900 case Intrinsic::x86_sse_ucomieq_ss:
6901 case Intrinsic::x86_sse_ucomilt_ss:
6902 case Intrinsic::x86_sse_ucomile_ss:
6903 case Intrinsic::x86_sse_ucomigt_ss:
6904 case Intrinsic::x86_sse_ucomige_ss:
6905 case Intrinsic::x86_sse_ucomineq_ss:
6906 case Intrinsic::x86_sse2_comieq_sd:
6907 case Intrinsic::x86_sse2_comilt_sd:
6908 case Intrinsic::x86_sse2_comile_sd:
6909 case Intrinsic::x86_sse2_comigt_sd:
6910 case Intrinsic::x86_sse2_comige_sd:
6911 case Intrinsic::x86_sse2_comineq_sd:
6912 case Intrinsic::x86_sse2_ucomieq_sd:
6913 case Intrinsic::x86_sse2_ucomilt_sd:
6914 case Intrinsic::x86_sse2_ucomile_sd:
6915 case Intrinsic::x86_sse2_ucomigt_sd:
6916 case Intrinsic::x86_sse2_ucomige_sd:
6917 case Intrinsic::x86_sse2_ucomineq_sd: {
6918 unsigned Opc = 0;
6919 ISD::CondCode CC = ISD::SETCC_INVALID;
6920 switch (IntNo) {
6921 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006922 case Intrinsic::x86_sse_comieq_ss:
6923 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006924 Opc = X86ISD::COMI;
6925 CC = ISD::SETEQ;
6926 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006927 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006928 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006929 Opc = X86ISD::COMI;
6930 CC = ISD::SETLT;
6931 break;
6932 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006933 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006934 Opc = X86ISD::COMI;
6935 CC = ISD::SETLE;
6936 break;
6937 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006938 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006939 Opc = X86ISD::COMI;
6940 CC = ISD::SETGT;
6941 break;
6942 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006943 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006944 Opc = X86ISD::COMI;
6945 CC = ISD::SETGE;
6946 break;
6947 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006948 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006949 Opc = X86ISD::COMI;
6950 CC = ISD::SETNE;
6951 break;
6952 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006953 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006954 Opc = X86ISD::UCOMI;
6955 CC = ISD::SETEQ;
6956 break;
6957 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006958 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959 Opc = X86ISD::UCOMI;
6960 CC = ISD::SETLT;
6961 break;
6962 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006963 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964 Opc = X86ISD::UCOMI;
6965 CC = ISD::SETLE;
6966 break;
6967 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006968 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006969 Opc = X86ISD::UCOMI;
6970 CC = ISD::SETGT;
6971 break;
6972 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006973 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006974 Opc = X86ISD::UCOMI;
6975 CC = ISD::SETGE;
6976 break;
6977 case Intrinsic::x86_sse_ucomineq_ss:
6978 case Intrinsic::x86_sse2_ucomineq_sd:
6979 Opc = X86ISD::UCOMI;
6980 CC = ISD::SETNE;
6981 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006982 }
Evan Cheng734503b2006-09-11 02:19:56 +00006983
Dan Gohman475871a2008-07-27 21:46:04 +00006984 SDValue LHS = Op.getOperand(1);
6985 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006986 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006987 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006988 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6989 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6990 DAG.getConstant(X86CC, MVT::i8), Cond);
6991 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006992 }
Eric Christopher71c67532009-07-29 00:28:05 +00006993 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006994 // an integer value, not just an instruction so lower it to the ptest
6995 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006996 case Intrinsic::x86_sse41_ptestz:
6997 case Intrinsic::x86_sse41_ptestc:
6998 case Intrinsic::x86_sse41_ptestnzc:{
6999 unsigned X86CC = 0;
7000 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007001 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00007002 case Intrinsic::x86_sse41_ptestz:
7003 // ZF = 1
7004 X86CC = X86::COND_E;
7005 break;
7006 case Intrinsic::x86_sse41_ptestc:
7007 // CF = 1
7008 X86CC = X86::COND_B;
7009 break;
Eric Christopherfd179292009-08-27 18:07:15 +00007010 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00007011 // ZF and CF = 0
7012 X86CC = X86::COND_A;
7013 break;
7014 }
Eric Christopherfd179292009-08-27 18:07:15 +00007015
Eric Christopher71c67532009-07-29 00:28:05 +00007016 SDValue LHS = Op.getOperand(1);
7017 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007018 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
7019 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7020 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7021 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007022 }
Evan Cheng5759f972008-05-04 09:15:50 +00007023
7024 // Fix vector shift instructions where the last operand is a non-immediate
7025 // i32 value.
7026 case Intrinsic::x86_sse2_pslli_w:
7027 case Intrinsic::x86_sse2_pslli_d:
7028 case Intrinsic::x86_sse2_pslli_q:
7029 case Intrinsic::x86_sse2_psrli_w:
7030 case Intrinsic::x86_sse2_psrli_d:
7031 case Intrinsic::x86_sse2_psrli_q:
7032 case Intrinsic::x86_sse2_psrai_w:
7033 case Intrinsic::x86_sse2_psrai_d:
7034 case Intrinsic::x86_mmx_pslli_w:
7035 case Intrinsic::x86_mmx_pslli_d:
7036 case Intrinsic::x86_mmx_pslli_q:
7037 case Intrinsic::x86_mmx_psrli_w:
7038 case Intrinsic::x86_mmx_psrli_d:
7039 case Intrinsic::x86_mmx_psrli_q:
7040 case Intrinsic::x86_mmx_psrai_w:
7041 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007042 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007043 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007044 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007045
7046 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007047 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007048 switch (IntNo) {
7049 case Intrinsic::x86_sse2_pslli_w:
7050 NewIntNo = Intrinsic::x86_sse2_psll_w;
7051 break;
7052 case Intrinsic::x86_sse2_pslli_d:
7053 NewIntNo = Intrinsic::x86_sse2_psll_d;
7054 break;
7055 case Intrinsic::x86_sse2_pslli_q:
7056 NewIntNo = Intrinsic::x86_sse2_psll_q;
7057 break;
7058 case Intrinsic::x86_sse2_psrli_w:
7059 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7060 break;
7061 case Intrinsic::x86_sse2_psrli_d:
7062 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7063 break;
7064 case Intrinsic::x86_sse2_psrli_q:
7065 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7066 break;
7067 case Intrinsic::x86_sse2_psrai_w:
7068 NewIntNo = Intrinsic::x86_sse2_psra_w;
7069 break;
7070 case Intrinsic::x86_sse2_psrai_d:
7071 NewIntNo = Intrinsic::x86_sse2_psra_d;
7072 break;
7073 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007074 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007075 switch (IntNo) {
7076 case Intrinsic::x86_mmx_pslli_w:
7077 NewIntNo = Intrinsic::x86_mmx_psll_w;
7078 break;
7079 case Intrinsic::x86_mmx_pslli_d:
7080 NewIntNo = Intrinsic::x86_mmx_psll_d;
7081 break;
7082 case Intrinsic::x86_mmx_pslli_q:
7083 NewIntNo = Intrinsic::x86_mmx_psll_q;
7084 break;
7085 case Intrinsic::x86_mmx_psrli_w:
7086 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7087 break;
7088 case Intrinsic::x86_mmx_psrli_d:
7089 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7090 break;
7091 case Intrinsic::x86_mmx_psrli_q:
7092 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7093 break;
7094 case Intrinsic::x86_mmx_psrai_w:
7095 NewIntNo = Intrinsic::x86_mmx_psra_w;
7096 break;
7097 case Intrinsic::x86_mmx_psrai_d:
7098 NewIntNo = Intrinsic::x86_mmx_psra_d;
7099 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007100 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007101 }
7102 break;
7103 }
7104 }
Mon P Wangefa42202009-09-03 19:56:25 +00007105
7106 // The vector shift intrinsics with scalars uses 32b shift amounts but
7107 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7108 // to be zero.
7109 SDValue ShOps[4];
7110 ShOps[0] = ShAmt;
7111 ShOps[1] = DAG.getConstant(0, MVT::i32);
7112 if (ShAmtVT == MVT::v4i32) {
7113 ShOps[2] = DAG.getUNDEF(MVT::i32);
7114 ShOps[3] = DAG.getUNDEF(MVT::i32);
7115 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7116 } else {
7117 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7118 }
7119
Owen Andersone50ed302009-08-10 22:56:29 +00007120 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007121 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007122 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007123 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007124 Op.getOperand(1), ShAmt);
7125 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007126 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007127}
Evan Cheng72261582005-12-20 06:22:03 +00007128
Dan Gohmand858e902010-04-17 15:26:15 +00007129SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7130 SelectionDAG &DAG) const {
Bill Wendling64e87322009-01-16 19:25:27 +00007131 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007132 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007133
7134 if (Depth > 0) {
7135 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7136 SDValue Offset =
7137 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007138 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007139 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007140 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007141 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007142 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007143 }
7144
7145 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007146 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007147 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007148 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007149}
7150
Dan Gohmand858e902010-04-17 15:26:15 +00007151SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007152 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7153 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00007154 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007155 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007156 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7157 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007158 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007159 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007160 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7161 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007162 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007163}
7164
Dan Gohman475871a2008-07-27 21:46:04 +00007165SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007166 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007167 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007168}
7169
Dan Gohmand858e902010-04-17 15:26:15 +00007170SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007171 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007172 SDValue Chain = Op.getOperand(0);
7173 SDValue Offset = Op.getOperand(1);
7174 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007175 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007176
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007177 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7178 getPointerTy());
7179 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007180
Dale Johannesene4d209d2009-02-03 20:21:25 +00007181 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007182 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007183 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007184 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007185 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007186 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007187
Dale Johannesene4d209d2009-02-03 20:21:25 +00007188 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007189 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007190 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007191}
7192
Dan Gohman475871a2008-07-27 21:46:04 +00007193SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007194 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007195 SDValue Root = Op.getOperand(0);
7196 SDValue Trmp = Op.getOperand(1); // trampoline
7197 SDValue FPtr = Op.getOperand(2); // nested function
7198 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007199 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007200
Dan Gohman69de1932008-02-06 22:27:42 +00007201 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007202
7203 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007204 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007205
7206 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007207 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7208 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007209
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007210 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7211 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007212
7213 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7214
7215 // Load the pointer to the nested function into R11.
7216 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007217 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007218 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007219 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007220
Owen Anderson825b72b2009-08-11 20:47:22 +00007221 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7222 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007223 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7224 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007225
7226 // Load the 'nest' parameter value into R10.
7227 // R10 is specified in X86CallingConv.td
7228 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007229 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7230 DAG.getConstant(10, MVT::i64));
7231 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007232 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007233
Owen Anderson825b72b2009-08-11 20:47:22 +00007234 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7235 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007236 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7237 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007238
7239 // Jump to the nested function.
7240 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007241 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7242 DAG.getConstant(20, MVT::i64));
7243 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007244 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007245
7246 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007247 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7248 DAG.getConstant(22, MVT::i64));
7249 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007250 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007251
Dan Gohman475871a2008-07-27 21:46:04 +00007252 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007253 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007254 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007255 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007256 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007257 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007258 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007259 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007260
7261 switch (CC) {
7262 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007263 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007264 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007265 case CallingConv::X86_StdCall: {
7266 // Pass 'nest' parameter in ECX.
7267 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007268 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007269
7270 // Check that ECX wasn't needed by an 'inreg' parameter.
7271 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007272 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007273
Chris Lattner58d74912008-03-12 17:45:29 +00007274 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007275 unsigned InRegCount = 0;
7276 unsigned Idx = 1;
7277
7278 for (FunctionType::param_iterator I = FTy->param_begin(),
7279 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007280 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007281 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007282 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007283
7284 if (InRegCount > 2) {
Chris Lattner75361b62010-04-07 22:58:41 +00007285 report_fatal_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007286 }
7287 }
7288 break;
7289 }
7290 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007291 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007292 // Pass 'nest' parameter in EAX.
7293 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007294 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007295 break;
7296 }
7297
Dan Gohman475871a2008-07-27 21:46:04 +00007298 SDValue OutChains[4];
7299 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007300
Owen Anderson825b72b2009-08-11 20:47:22 +00007301 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7302 DAG.getConstant(10, MVT::i32));
7303 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007304
Chris Lattnera62fe662010-02-05 19:20:30 +00007305 // This is storing the opcode for MOV32ri.
7306 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007307 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007308 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007309 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007310 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007311
Owen Anderson825b72b2009-08-11 20:47:22 +00007312 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7313 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007314 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7315 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007316
Chris Lattnera62fe662010-02-05 19:20:30 +00007317 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007318 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7319 DAG.getConstant(5, MVT::i32));
7320 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007321 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007322
Owen Anderson825b72b2009-08-11 20:47:22 +00007323 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7324 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007325 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7326 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007327
Dan Gohman475871a2008-07-27 21:46:04 +00007328 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007329 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007330 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007331 }
7332}
7333
Dan Gohmand858e902010-04-17 15:26:15 +00007334SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7335 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007336 /*
7337 The rounding mode is in bits 11:10 of FPSR, and has the following
7338 settings:
7339 00 Round to nearest
7340 01 Round to -inf
7341 10 Round to +inf
7342 11 Round to 0
7343
7344 FLT_ROUNDS, on the other hand, expects the following:
7345 -1 Undefined
7346 0 Round to 0
7347 1 Round to nearest
7348 2 Round to +inf
7349 3 Round to -inf
7350
7351 To perform the conversion, we do:
7352 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7353 */
7354
7355 MachineFunction &MF = DAG.getMachineFunction();
7356 const TargetMachine &TM = MF.getTarget();
7357 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7358 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007359 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007360 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007361
7362 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007363 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007364 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007365
Owen Anderson825b72b2009-08-11 20:47:22 +00007366 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007367 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007368
7369 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007370 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7371 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007372
7373 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007374 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007375 DAG.getNode(ISD::SRL, dl, MVT::i16,
7376 DAG.getNode(ISD::AND, dl, MVT::i16,
7377 CWD, DAG.getConstant(0x800, MVT::i16)),
7378 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007379 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007380 DAG.getNode(ISD::SRL, dl, MVT::i16,
7381 DAG.getNode(ISD::AND, dl, MVT::i16,
7382 CWD, DAG.getConstant(0x400, MVT::i16)),
7383 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007384
Dan Gohman475871a2008-07-27 21:46:04 +00007385 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007386 DAG.getNode(ISD::AND, dl, MVT::i16,
7387 DAG.getNode(ISD::ADD, dl, MVT::i16,
7388 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7389 DAG.getConstant(1, MVT::i16)),
7390 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007391
7392
Duncan Sands83ec4b62008-06-06 12:08:01 +00007393 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007394 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007395}
7396
Dan Gohmand858e902010-04-17 15:26:15 +00007397SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007398 EVT VT = Op.getValueType();
7399 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007400 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007401 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007402
7403 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007404 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007405 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007406 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007407 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007408 }
Evan Cheng18efe262007-12-14 02:13:44 +00007409
Evan Cheng152804e2007-12-14 08:30:15 +00007410 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007411 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007412 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007413
7414 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007415 SDValue Ops[] = {
7416 Op,
7417 DAG.getConstant(NumBits+NumBits-1, OpVT),
7418 DAG.getConstant(X86::COND_E, MVT::i8),
7419 Op.getValue(1)
7420 };
7421 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007422
7423 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007424 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007425
Owen Anderson825b72b2009-08-11 20:47:22 +00007426 if (VT == MVT::i8)
7427 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007428 return Op;
7429}
7430
Dan Gohmand858e902010-04-17 15:26:15 +00007431SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007432 EVT VT = Op.getValueType();
7433 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007434 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007435 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007436
7437 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007438 if (VT == MVT::i8) {
7439 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007440 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007441 }
Evan Cheng152804e2007-12-14 08:30:15 +00007442
7443 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007444 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007445 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007446
7447 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007448 SDValue Ops[] = {
7449 Op,
7450 DAG.getConstant(NumBits, OpVT),
7451 DAG.getConstant(X86::COND_E, MVT::i8),
7452 Op.getValue(1)
7453 };
7454 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007455
Owen Anderson825b72b2009-08-11 20:47:22 +00007456 if (VT == MVT::i8)
7457 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007458 return Op;
7459}
7460
Dan Gohmand858e902010-04-17 15:26:15 +00007461SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007462 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007463 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007464 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007465
Mon P Wangaf9b9522008-12-18 21:42:19 +00007466 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7467 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7468 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7469 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7470 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7471 //
7472 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7473 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7474 // return AloBlo + AloBhi + AhiBlo;
7475
7476 SDValue A = Op.getOperand(0);
7477 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007478
Dale Johannesene4d209d2009-02-03 20:21:25 +00007479 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007480 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7481 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007482 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007483 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7484 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007485 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007486 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007487 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007488 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007489 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007490 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007491 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007492 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007493 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007494 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007495 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7496 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007497 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007498 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7499 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007500 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7501 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007502 return Res;
7503}
7504
7505
Dan Gohmand858e902010-04-17 15:26:15 +00007506SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007507 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7508 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007509 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7510 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007511 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007512 SDValue LHS = N->getOperand(0);
7513 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007514 unsigned BaseOp = 0;
7515 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007516 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007517
7518 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007519 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007520 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007521 // A subtract of one will be selected as a INC. Note that INC doesn't
7522 // set CF, so we can't do this for UADDO.
7523 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7524 if (C->getAPIntValue() == 1) {
7525 BaseOp = X86ISD::INC;
7526 Cond = X86::COND_O;
7527 break;
7528 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007529 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007530 Cond = X86::COND_O;
7531 break;
7532 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007533 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007534 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007535 break;
7536 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007537 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7538 // set CF, so we can't do this for USUBO.
7539 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7540 if (C->getAPIntValue() == 1) {
7541 BaseOp = X86ISD::DEC;
7542 Cond = X86::COND_O;
7543 break;
7544 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007545 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007546 Cond = X86::COND_O;
7547 break;
7548 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007549 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007550 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007551 break;
7552 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007553 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007554 Cond = X86::COND_O;
7555 break;
7556 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007557 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007558 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007559 break;
7560 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007561
Bill Wendling61edeb52008-12-02 01:06:39 +00007562 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007563 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007564 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007565
Bill Wendling61edeb52008-12-02 01:06:39 +00007566 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007567 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007568 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007569
Bill Wendling61edeb52008-12-02 01:06:39 +00007570 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7571 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007572}
7573
Dan Gohmand858e902010-04-17 15:26:15 +00007574SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007575 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007576 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007577 unsigned Reg = 0;
7578 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007579 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007580 default:
7581 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007582 case MVT::i8: Reg = X86::AL; size = 1; break;
7583 case MVT::i16: Reg = X86::AX; size = 2; break;
7584 case MVT::i32: Reg = X86::EAX; size = 4; break;
7585 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007586 assert(Subtarget->is64Bit() && "Node not type legal!");
7587 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007588 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007589 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007590 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007591 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007592 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007593 Op.getOperand(1),
7594 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007595 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007596 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007597 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007598 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007599 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007600 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007601 return cpOut;
7602}
7603
Duncan Sands1607f052008-12-01 11:39:25 +00007604SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007605 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007606 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007607 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007608 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007609 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007610 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007611 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7612 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007613 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007614 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7615 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007616 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007617 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007618 rdx.getValue(1)
7619 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007620 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007621}
7622
Dan Gohmand858e902010-04-17 15:26:15 +00007623SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007624 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007625 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007626 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007627 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007628 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007629 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007630 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007631 Node->getOperand(0),
7632 Node->getOperand(1), negOp,
7633 cast<AtomicSDNode>(Node)->getSrcValue(),
7634 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007635}
7636
Evan Cheng0db9fe62006-04-25 20:13:52 +00007637/// LowerOperation - Provide custom lowering hooks for some operations.
7638///
Dan Gohmand858e902010-04-17 15:26:15 +00007639SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007640 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007641 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007642 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7643 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007644 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007645 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007646 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7647 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7648 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7649 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7650 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7651 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007652 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007653 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007654 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007655 case ISD::SHL_PARTS:
7656 case ISD::SRA_PARTS:
7657 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7658 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007659 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007660 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007661 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007662 case ISD::FABS: return LowerFABS(Op, DAG);
7663 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007664 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007665 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007666 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007667 case ISD::SELECT: return LowerSELECT(Op, DAG);
7668 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007669 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007670 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007671 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007672 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007673 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007674 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7675 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007676 case ISD::FRAME_TO_ARGS_OFFSET:
7677 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007678 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007679 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007680 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007681 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007682 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7683 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007684 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007685 case ISD::SADDO:
7686 case ISD::UADDO:
7687 case ISD::SSUBO:
7688 case ISD::USUBO:
7689 case ISD::SMULO:
7690 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007691 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007692 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007693}
7694
Duncan Sands1607f052008-12-01 11:39:25 +00007695void X86TargetLowering::
7696ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007697 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007698 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007699 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007700 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007701
7702 SDValue Chain = Node->getOperand(0);
7703 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007704 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007705 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007706 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007707 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007708 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007709 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007710 SDValue Result =
7711 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7712 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007713 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007714 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007715 Results.push_back(Result.getValue(2));
7716}
7717
Duncan Sands126d9072008-07-04 11:47:58 +00007718/// ReplaceNodeResults - Replace a node with an illegal result type
7719/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007720void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7721 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007722 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007723 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007724 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007725 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007726 assert(false && "Do not know how to custom type legalize this operation!");
7727 return;
7728 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007729 std::pair<SDValue,SDValue> Vals =
7730 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007731 SDValue FIST = Vals.first, StackSlot = Vals.second;
7732 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007733 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007734 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007735 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7736 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007737 }
7738 return;
7739 }
7740 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007741 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007742 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007743 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007744 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007745 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007746 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007747 eax.getValue(2));
7748 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7749 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007750 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007751 Results.push_back(edx.getValue(1));
7752 return;
7753 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007754 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007755 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007756 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007757 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007758 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7759 DAG.getConstant(0, MVT::i32));
7760 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7761 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007762 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7763 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007764 cpInL.getValue(1));
7765 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007766 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7767 DAG.getConstant(0, MVT::i32));
7768 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7769 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007770 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007771 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007772 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007773 swapInL.getValue(1));
7774 SDValue Ops[] = { swapInH.getValue(0),
7775 N->getOperand(1),
7776 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007777 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007778 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007779 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007780 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007781 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007782 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007783 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007784 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007785 Results.push_back(cpOutH.getValue(1));
7786 return;
7787 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007788 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007789 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7790 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007791 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007792 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7793 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007794 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007795 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7796 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007797 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007798 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7799 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007800 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007801 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7802 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007803 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007804 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7805 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007806 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007807 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7808 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007809 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007810}
7811
Evan Cheng72261582005-12-20 06:22:03 +00007812const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7813 switch (Opcode) {
7814 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007815 case X86ISD::BSF: return "X86ISD::BSF";
7816 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007817 case X86ISD::SHLD: return "X86ISD::SHLD";
7818 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007819 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007820 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007821 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007822 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007823 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007824 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007825 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7826 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7827 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007828 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007829 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007830 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007831 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007832 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007833 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007834 case X86ISD::COMI: return "X86ISD::COMI";
7835 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007836 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007837 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007838 case X86ISD::CMOV: return "X86ISD::CMOV";
7839 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007840 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007841 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7842 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007843 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007844 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007845 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007846 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007847 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007848 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7849 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007850 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007851 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007852 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007853 case X86ISD::FMAX: return "X86ISD::FMAX";
7854 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007855 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7856 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007857 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007858 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007859 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007860 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007861 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007862 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7863 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007864 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7865 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7866 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7867 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7868 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7869 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007870 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7871 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007872 case X86ISD::VSHL: return "X86ISD::VSHL";
7873 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007874 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7875 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7876 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7877 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7878 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7879 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7880 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7881 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7882 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7883 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007884 case X86ISD::ADD: return "X86ISD::ADD";
7885 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007886 case X86ISD::SMUL: return "X86ISD::SMUL";
7887 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007888 case X86ISD::INC: return "X86ISD::INC";
7889 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007890 case X86ISD::OR: return "X86ISD::OR";
7891 case X86ISD::XOR: return "X86ISD::XOR";
7892 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007893 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007894 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007895 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007896 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007897 }
7898}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007899
Chris Lattnerc9addb72007-03-30 23:15:24 +00007900// isLegalAddressingMode - Return true if the addressing mode represented
7901// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007902bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007903 const Type *Ty) const {
7904 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007905 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007906
Chris Lattnerc9addb72007-03-30 23:15:24 +00007907 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007908 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007909 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007910
Chris Lattnerc9addb72007-03-30 23:15:24 +00007911 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007912 unsigned GVFlags =
7913 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007914
Chris Lattnerdfed4132009-07-10 07:38:24 +00007915 // If a reference to this global requires an extra load, we can't fold it.
7916 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007917 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007918
Chris Lattnerdfed4132009-07-10 07:38:24 +00007919 // If BaseGV requires a register for the PIC base, we cannot also have a
7920 // BaseReg specified.
7921 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007922 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007923
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007924 // If lower 4G is not available, then we must use rip-relative addressing.
7925 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7926 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007927 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007928
Chris Lattnerc9addb72007-03-30 23:15:24 +00007929 switch (AM.Scale) {
7930 case 0:
7931 case 1:
7932 case 2:
7933 case 4:
7934 case 8:
7935 // These scales always work.
7936 break;
7937 case 3:
7938 case 5:
7939 case 9:
7940 // These scales are formed with basereg+scalereg. Only accept if there is
7941 // no basereg yet.
7942 if (AM.HasBaseReg)
7943 return false;
7944 break;
7945 default: // Other stuff never works.
7946 return false;
7947 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007948
Chris Lattnerc9addb72007-03-30 23:15:24 +00007949 return true;
7950}
7951
7952
Evan Cheng2bd122c2007-10-26 01:56:11 +00007953bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007954 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007955 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007956 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7957 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007958 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007959 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007960 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007961}
7962
Owen Andersone50ed302009-08-10 22:56:29 +00007963bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007964 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007965 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007966 unsigned NumBits1 = VT1.getSizeInBits();
7967 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007968 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007969 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007970 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007971}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007972
Dan Gohman97121ba2009-04-08 00:15:30 +00007973bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007974 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007975 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007976}
7977
Owen Andersone50ed302009-08-10 22:56:29 +00007978bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007979 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007980 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007981}
7982
Owen Andersone50ed302009-08-10 22:56:29 +00007983bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007984 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007985 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007986}
7987
Evan Cheng60c07e12006-07-05 22:17:51 +00007988/// isShuffleMaskLegal - Targets can use this to indicate that they only
7989/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7990/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7991/// are assumed to be legal.
7992bool
Eric Christopherfd179292009-08-27 18:07:15 +00007993X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007994 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00007995 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007996 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00007997 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00007998
Nate Begemana09008b2009-10-19 02:17:23 +00007999 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00008000 return (VT.getVectorNumElements() == 2 ||
8001 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
8002 isMOVLMask(M, VT) ||
8003 isSHUFPMask(M, VT) ||
8004 isPSHUFDMask(M, VT) ||
8005 isPSHUFHWMask(M, VT) ||
8006 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00008007 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00008008 isUNPCKLMask(M, VT) ||
8009 isUNPCKHMask(M, VT) ||
8010 isUNPCKL_v_undef_Mask(M, VT) ||
8011 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008012}
8013
Dan Gohman7d8143f2008-04-09 20:09:42 +00008014bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00008015X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00008016 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00008017 unsigned NumElts = VT.getVectorNumElements();
8018 // FIXME: This collection of masks seems suspect.
8019 if (NumElts == 2)
8020 return true;
8021 if (NumElts == 4 && VT.getSizeInBits() == 128) {
8022 return (isMOVLMask(Mask, VT) ||
8023 isCommutedMOVLMask(Mask, VT, true) ||
8024 isSHUFPMask(Mask, VT) ||
8025 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008026 }
8027 return false;
8028}
8029
8030//===----------------------------------------------------------------------===//
8031// X86 Scheduler Hooks
8032//===----------------------------------------------------------------------===//
8033
Mon P Wang63307c32008-05-05 19:05:59 +00008034// private utility function
8035MachineBasicBlock *
8036X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
8037 MachineBasicBlock *MBB,
8038 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008039 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008040 unsigned LoadOpc,
8041 unsigned CXchgOpc,
8042 unsigned copyOpc,
8043 unsigned notOpc,
8044 unsigned EAXreg,
8045 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008046 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008047 // For the atomic bitwise operator, we generate
8048 // thisMBB:
8049 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008050 // ld t1 = [bitinstr.addr]
8051 // op t2 = t1, [bitinstr.val]
8052 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008053 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8054 // bz newMBB
8055 // fallthrough -->nextMBB
8056 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8057 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008058 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008059 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008060
Mon P Wang63307c32008-05-05 19:05:59 +00008061 /// First build the CFG
8062 MachineFunction *F = MBB->getParent();
8063 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008064 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8065 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8066 F->insert(MBBIter, newMBB);
8067 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008068
Mon P Wang63307c32008-05-05 19:05:59 +00008069 // Move all successors to thisMBB to nextMBB
8070 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008071
Mon P Wang63307c32008-05-05 19:05:59 +00008072 // Update thisMBB to fall through to newMBB
8073 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008074
Mon P Wang63307c32008-05-05 19:05:59 +00008075 // newMBB jumps to itself and fall through to nextMBB
8076 newMBB->addSuccessor(nextMBB);
8077 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008078
Mon P Wang63307c32008-05-05 19:05:59 +00008079 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008080 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008081 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00008082 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008083 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008084 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008085 int numArgs = bInstr->getNumOperands() - 1;
8086 for (int i=0; i < numArgs; ++i)
8087 argOpers[i] = &bInstr->getOperand(i+1);
8088
8089 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008090 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8091 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008092
Dale Johannesen140be2d2008-08-19 18:47:28 +00008093 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008094 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008095 for (int i=0; i <= lastAddrIndx; ++i)
8096 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008097
Dale Johannesen140be2d2008-08-19 18:47:28 +00008098 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008099 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008100 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008101 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008102 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008103 tt = t1;
8104
Dale Johannesen140be2d2008-08-19 18:47:28 +00008105 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00008106 assert((argOpers[valArgIndx]->isReg() ||
8107 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008108 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00008109 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008110 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008111 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008112 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008113 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00008114 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008115
Dale Johannesene4d209d2009-02-03 20:21:25 +00008116 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008117 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008118
Dale Johannesene4d209d2009-02-03 20:21:25 +00008119 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008120 for (int i=0; i <= lastAddrIndx; ++i)
8121 (*MIB).addOperand(*argOpers[i]);
8122 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008123 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008124 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8125 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008126
Dale Johannesene4d209d2009-02-03 20:21:25 +00008127 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008128 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008129
Mon P Wang63307c32008-05-05 19:05:59 +00008130 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008131 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008132
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008133 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008134 return nextMBB;
8135}
8136
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008137// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008138MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008139X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8140 MachineBasicBlock *MBB,
8141 unsigned regOpcL,
8142 unsigned regOpcH,
8143 unsigned immOpcL,
8144 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008145 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008146 // For the atomic bitwise operator, we generate
8147 // thisMBB (instructions are in pairs, except cmpxchg8b)
8148 // ld t1,t2 = [bitinstr.addr]
8149 // newMBB:
8150 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8151 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008152 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008153 // mov ECX, EBX <- t5, t6
8154 // mov EAX, EDX <- t1, t2
8155 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8156 // mov t3, t4 <- EAX, EDX
8157 // bz newMBB
8158 // result in out1, out2
8159 // fallthrough -->nextMBB
8160
8161 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8162 const unsigned LoadOpc = X86::MOV32rm;
8163 const unsigned copyOpc = X86::MOV32rr;
8164 const unsigned NotOpc = X86::NOT32r;
8165 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8166 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8167 MachineFunction::iterator MBBIter = MBB;
8168 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008169
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008170 /// First build the CFG
8171 MachineFunction *F = MBB->getParent();
8172 MachineBasicBlock *thisMBB = MBB;
8173 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8174 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8175 F->insert(MBBIter, newMBB);
8176 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008177
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008178 // Move all successors to thisMBB to nextMBB
8179 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008180
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008181 // Update thisMBB to fall through to newMBB
8182 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008183
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008184 // newMBB jumps to itself and fall through to nextMBB
8185 newMBB->addSuccessor(nextMBB);
8186 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008187
Dale Johannesene4d209d2009-02-03 20:21:25 +00008188 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008189 // Insert instructions into newMBB based on incoming instruction
8190 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008191 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008192 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008193 MachineOperand& dest1Oper = bInstr->getOperand(0);
8194 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008195 MachineOperand* argOpers[2 + X86AddrNumOperands];
8196 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008197 argOpers[i] = &bInstr->getOperand(i+2);
8198
Evan Chengad5b52f2010-01-08 19:14:57 +00008199 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008200 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008201
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008202 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008203 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008204 for (int i=0; i <= lastAddrIndx; ++i)
8205 (*MIB).addOperand(*argOpers[i]);
8206 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008207 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008208 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008209 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008210 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008211 MachineOperand newOp3 = *(argOpers[3]);
8212 if (newOp3.isImm())
8213 newOp3.setImm(newOp3.getImm()+4);
8214 else
8215 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008216 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008217 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008218
8219 // t3/4 are defined later, at the bottom of the loop
8220 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8221 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008222 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008223 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008224 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008225 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8226
Evan Cheng306b4ca2010-01-08 23:41:50 +00008227 // The subsequent operations should be using the destination registers of
8228 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008229 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008230 t1 = F->getRegInfo().createVirtualRegister(RC);
8231 t2 = F->getRegInfo().createVirtualRegister(RC);
8232 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8233 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008234 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008235 t1 = dest1Oper.getReg();
8236 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008237 }
8238
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008239 int valArgIndx = lastAddrIndx + 1;
8240 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008241 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008242 "invalid operand");
8243 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8244 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008245 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008246 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008247 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008248 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008249 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008250 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008251 (*MIB).addOperand(*argOpers[valArgIndx]);
8252 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008253 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008254 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008255 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008256 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008257 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008258 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008259 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008260 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008261 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008262 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008263
Dale Johannesene4d209d2009-02-03 20:21:25 +00008264 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008265 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008266 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008267 MIB.addReg(t2);
8268
Dale Johannesene4d209d2009-02-03 20:21:25 +00008269 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008270 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008271 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008272 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008273
Dale Johannesene4d209d2009-02-03 20:21:25 +00008274 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008275 for (int i=0; i <= lastAddrIndx; ++i)
8276 (*MIB).addOperand(*argOpers[i]);
8277
8278 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008279 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8280 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008281
Dale Johannesene4d209d2009-02-03 20:21:25 +00008282 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008283 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008284 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008285 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008286
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008287 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008288 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008289
8290 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8291 return nextMBB;
8292}
8293
8294// private utility function
8295MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008296X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8297 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008298 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008299 // For the atomic min/max operator, we generate
8300 // thisMBB:
8301 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008302 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008303 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008304 // cmp t1, t2
8305 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008306 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008307 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8308 // bz newMBB
8309 // fallthrough -->nextMBB
8310 //
8311 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8312 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008313 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008314 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008315
Mon P Wang63307c32008-05-05 19:05:59 +00008316 /// First build the CFG
8317 MachineFunction *F = MBB->getParent();
8318 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008319 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8320 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8321 F->insert(MBBIter, newMBB);
8322 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008323
Dan Gohmand6708ea2009-08-15 01:38:56 +00008324 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008325 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008326
Mon P Wang63307c32008-05-05 19:05:59 +00008327 // Update thisMBB to fall through to newMBB
8328 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008329
Mon P Wang63307c32008-05-05 19:05:59 +00008330 // newMBB jumps to newMBB and fall through to nextMBB
8331 newMBB->addSuccessor(nextMBB);
8332 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008333
Dale Johannesene4d209d2009-02-03 20:21:25 +00008334 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008335 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008336 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008337 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008338 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008339 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008340 int numArgs = mInstr->getNumOperands() - 1;
8341 for (int i=0; i < numArgs; ++i)
8342 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008343
Mon P Wang63307c32008-05-05 19:05:59 +00008344 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008345 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8346 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008347
Mon P Wangab3e7472008-05-05 22:56:23 +00008348 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008349 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008350 for (int i=0; i <= lastAddrIndx; ++i)
8351 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008352
Mon P Wang63307c32008-05-05 19:05:59 +00008353 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008354 assert((argOpers[valArgIndx]->isReg() ||
8355 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008356 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008357
8358 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008359 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008360 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008361 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008362 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008363 (*MIB).addOperand(*argOpers[valArgIndx]);
8364
Dale Johannesene4d209d2009-02-03 20:21:25 +00008365 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008366 MIB.addReg(t1);
8367
Dale Johannesene4d209d2009-02-03 20:21:25 +00008368 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008369 MIB.addReg(t1);
8370 MIB.addReg(t2);
8371
8372 // Generate movc
8373 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008374 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008375 MIB.addReg(t2);
8376 MIB.addReg(t1);
8377
8378 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008379 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008380 for (int i=0; i <= lastAddrIndx; ++i)
8381 (*MIB).addOperand(*argOpers[i]);
8382 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008383 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008384 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8385 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008386
Dale Johannesene4d209d2009-02-03 20:21:25 +00008387 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008388 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008389
Mon P Wang63307c32008-05-05 19:05:59 +00008390 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008391 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008392
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008393 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008394 return nextMBB;
8395}
8396
Eric Christopherf83a5de2009-08-27 18:08:16 +00008397// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8398// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008399MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008400X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008401 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008402
8403 MachineFunction *F = BB->getParent();
8404 DebugLoc dl = MI->getDebugLoc();
8405 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8406
8407 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008408 if (memArg)
8409 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8410 else
8411 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008412
8413 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8414
8415 for (unsigned i = 0; i < numArgs; ++i) {
8416 MachineOperand &Op = MI->getOperand(i+1);
8417
8418 if (!(Op.isReg() && Op.isImplicit()))
8419 MIB.addOperand(Op);
8420 }
8421
8422 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8423 .addReg(X86::XMM0);
8424
8425 F->DeleteMachineInstr(MI);
8426
8427 return BB;
8428}
8429
8430MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008431X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8432 MachineInstr *MI,
8433 MachineBasicBlock *MBB) const {
8434 // Emit code to save XMM registers to the stack. The ABI says that the
8435 // number of registers to save is given in %al, so it's theoretically
8436 // possible to do an indirect jump trick to avoid saving all of them,
8437 // however this code takes a simpler approach and just executes all
8438 // of the stores if %al is non-zero. It's less code, and it's probably
8439 // easier on the hardware branch predictor, and stores aren't all that
8440 // expensive anyway.
8441
8442 // Create the new basic blocks. One block contains all the XMM stores,
8443 // and one block is the final destination regardless of whether any
8444 // stores were performed.
8445 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8446 MachineFunction *F = MBB->getParent();
8447 MachineFunction::iterator MBBIter = MBB;
8448 ++MBBIter;
8449 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8450 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8451 F->insert(MBBIter, XMMSaveMBB);
8452 F->insert(MBBIter, EndMBB);
8453
8454 // Set up the CFG.
8455 // Move any original successors of MBB to the end block.
8456 EndMBB->transferSuccessors(MBB);
8457 // The original block will now fall through to the XMM save block.
8458 MBB->addSuccessor(XMMSaveMBB);
8459 // The XMMSaveMBB will fall through to the end block.
8460 XMMSaveMBB->addSuccessor(EndMBB);
8461
8462 // Now add the instructions.
8463 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8464 DebugLoc DL = MI->getDebugLoc();
8465
8466 unsigned CountReg = MI->getOperand(0).getReg();
8467 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8468 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8469
8470 if (!Subtarget->isTargetWin64()) {
8471 // If %al is 0, branch around the XMM save block.
8472 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008473 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008474 MBB->addSuccessor(EndMBB);
8475 }
8476
8477 // In the XMM save block, save all the XMM argument registers.
8478 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8479 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008480 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008481 F->getMachineMemOperand(
8482 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8483 MachineMemOperand::MOStore, Offset,
8484 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008485 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8486 .addFrameIndex(RegSaveFrameIndex)
8487 .addImm(/*Scale=*/1)
8488 .addReg(/*IndexReg=*/0)
8489 .addImm(/*Disp=*/Offset)
8490 .addReg(/*Segment=*/0)
8491 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008492 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008493 }
8494
8495 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8496
8497 return EndMBB;
8498}
Mon P Wang63307c32008-05-05 19:05:59 +00008499
Evan Cheng60c07e12006-07-05 22:17:51 +00008500MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008501X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008502 MachineBasicBlock *BB,
8503 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008504 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8505 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008506
Chris Lattner52600972009-09-02 05:57:00 +00008507 // To "insert" a SELECT_CC instruction, we actually have to insert the
8508 // diamond control-flow pattern. The incoming instruction knows the
8509 // destination vreg to set, the condition code register to branch on, the
8510 // true/false values to select between, and a branch opcode to use.
8511 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8512 MachineFunction::iterator It = BB;
8513 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008514
Chris Lattner52600972009-09-02 05:57:00 +00008515 // thisMBB:
8516 // ...
8517 // TrueVal = ...
8518 // cmpTY ccX, r1, r2
8519 // bCC copy1MBB
8520 // fallthrough --> copy0MBB
8521 MachineBasicBlock *thisMBB = BB;
8522 MachineFunction *F = BB->getParent();
8523 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8524 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8525 unsigned Opc =
8526 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8527 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8528 F->insert(It, copy0MBB);
8529 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008530 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008531 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008532 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008533 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008534 E = BB->succ_end(); I != E; ++I) {
8535 EM->insert(std::make_pair(*I, sinkMBB));
8536 sinkMBB->addSuccessor(*I);
8537 }
8538 // Next, remove all successors of the current block, and add the true
8539 // and fallthrough blocks as its successors.
8540 while (!BB->succ_empty())
8541 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008542 // Add the true and fallthrough blocks as its successors.
8543 BB->addSuccessor(copy0MBB);
8544 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008545
Chris Lattner52600972009-09-02 05:57:00 +00008546 // copy0MBB:
8547 // %FalseValue = ...
8548 // # fallthrough to sinkMBB
8549 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008550
Chris Lattner52600972009-09-02 05:57:00 +00008551 // Update machine-CFG edges
8552 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008553
Chris Lattner52600972009-09-02 05:57:00 +00008554 // sinkMBB:
8555 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8556 // ...
8557 BB = sinkMBB;
8558 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8559 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8560 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8561
8562 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8563 return BB;
8564}
8565
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008566MachineBasicBlock *
8567X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8568 MachineBasicBlock *BB,
8569 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8570 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8571 DebugLoc DL = MI->getDebugLoc();
8572 MachineFunction *F = BB->getParent();
8573
8574 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8575 // non-trivial part is impdef of ESP.
8576 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8577 // mingw-w64.
8578
8579 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8580 .addExternalSymbol("_alloca")
8581 .addReg(X86::EAX, RegState::Implicit)
8582 .addReg(X86::ESP, RegState::Implicit)
8583 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8584 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8585
8586 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8587 return BB;
8588}
Chris Lattner52600972009-09-02 05:57:00 +00008589
8590MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008591X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008592 MachineBasicBlock *BB,
8593 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008594 switch (MI->getOpcode()) {
8595 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008596 case X86::MINGW_ALLOCA:
8597 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008598 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008599 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008600 case X86::CMOV_FR32:
8601 case X86::CMOV_FR64:
8602 case X86::CMOV_V4F32:
8603 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008604 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008605 case X86::CMOV_GR16:
8606 case X86::CMOV_GR32:
8607 case X86::CMOV_RFP32:
8608 case X86::CMOV_RFP64:
8609 case X86::CMOV_RFP80:
Evan Chengce319102009-09-19 09:51:03 +00008610 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008611
Dale Johannesen849f2142007-07-03 00:53:03 +00008612 case X86::FP32_TO_INT16_IN_MEM:
8613 case X86::FP32_TO_INT32_IN_MEM:
8614 case X86::FP32_TO_INT64_IN_MEM:
8615 case X86::FP64_TO_INT16_IN_MEM:
8616 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008617 case X86::FP64_TO_INT64_IN_MEM:
8618 case X86::FP80_TO_INT16_IN_MEM:
8619 case X86::FP80_TO_INT32_IN_MEM:
8620 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008621 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8622 DebugLoc DL = MI->getDebugLoc();
8623
Evan Cheng60c07e12006-07-05 22:17:51 +00008624 // Change the floating point control register to use "round towards zero"
8625 // mode when truncating to an integer value.
8626 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008627 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008628 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008629
8630 // Load the old value of the high byte of the control word...
8631 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008632 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008633 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008634 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008635
8636 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008637 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008638 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008639
8640 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008641 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008642
8643 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008644 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008645 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008646
8647 // Get the X86 opcode to use.
8648 unsigned Opc;
8649 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008650 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008651 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8652 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8653 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8654 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8655 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8656 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008657 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8658 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8659 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008660 }
8661
8662 X86AddressMode AM;
8663 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008664 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008665 AM.BaseType = X86AddressMode::RegBase;
8666 AM.Base.Reg = Op.getReg();
8667 } else {
8668 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008669 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008670 }
8671 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008672 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008673 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008674 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008675 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008676 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008677 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008678 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008679 AM.GV = Op.getGlobal();
8680 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008681 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008682 }
Chris Lattner52600972009-09-02 05:57:00 +00008683 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008684 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008685
8686 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008687 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008688
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008689 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008690 return BB;
8691 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00008692 // DBG_VALUE. Only the frame index case is done here.
8693 case X86::DBG_VALUE: {
8694 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8695 DebugLoc DL = MI->getDebugLoc();
8696 X86AddressMode AM;
8697 MachineFunction *F = BB->getParent();
8698 AM.BaseType = X86AddressMode::FrameIndexBase;
8699 AM.Base.FrameIndex = MI->getOperand(0).getImm();
8700 addFullAddress(BuildMI(BB, DL, TII->get(X86::DBG_VALUE)), AM).
8701 addImm(MI->getOperand(1).getImm()).
8702 addMetadata(MI->getOperand(2).getMetadata());
8703 F->DeleteMachineInstr(MI); // Remove pseudo.
8704 return BB;
8705 }
8706
Eric Christopherb120ab42009-08-18 22:50:32 +00008707 // String/text processing lowering.
8708 case X86::PCMPISTRM128REG:
8709 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8710 case X86::PCMPISTRM128MEM:
8711 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8712 case X86::PCMPESTRM128REG:
8713 return EmitPCMP(MI, BB, 5, false /* in mem */);
8714 case X86::PCMPESTRM128MEM:
8715 return EmitPCMP(MI, BB, 5, true /* in mem */);
8716
8717 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008718 case X86::ATOMAND32:
8719 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008720 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008721 X86::LCMPXCHG32, X86::MOV32rr,
8722 X86::NOT32r, X86::EAX,
8723 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008724 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008725 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8726 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008727 X86::LCMPXCHG32, X86::MOV32rr,
8728 X86::NOT32r, X86::EAX,
8729 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008730 case X86::ATOMXOR32:
8731 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008732 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008733 X86::LCMPXCHG32, X86::MOV32rr,
8734 X86::NOT32r, X86::EAX,
8735 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008736 case X86::ATOMNAND32:
8737 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008738 X86::AND32ri, X86::MOV32rm,
8739 X86::LCMPXCHG32, X86::MOV32rr,
8740 X86::NOT32r, X86::EAX,
8741 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008742 case X86::ATOMMIN32:
8743 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8744 case X86::ATOMMAX32:
8745 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8746 case X86::ATOMUMIN32:
8747 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8748 case X86::ATOMUMAX32:
8749 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008750
8751 case X86::ATOMAND16:
8752 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8753 X86::AND16ri, X86::MOV16rm,
8754 X86::LCMPXCHG16, X86::MOV16rr,
8755 X86::NOT16r, X86::AX,
8756 X86::GR16RegisterClass);
8757 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008758 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008759 X86::OR16ri, X86::MOV16rm,
8760 X86::LCMPXCHG16, X86::MOV16rr,
8761 X86::NOT16r, X86::AX,
8762 X86::GR16RegisterClass);
8763 case X86::ATOMXOR16:
8764 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8765 X86::XOR16ri, X86::MOV16rm,
8766 X86::LCMPXCHG16, X86::MOV16rr,
8767 X86::NOT16r, X86::AX,
8768 X86::GR16RegisterClass);
8769 case X86::ATOMNAND16:
8770 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8771 X86::AND16ri, X86::MOV16rm,
8772 X86::LCMPXCHG16, X86::MOV16rr,
8773 X86::NOT16r, X86::AX,
8774 X86::GR16RegisterClass, true);
8775 case X86::ATOMMIN16:
8776 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8777 case X86::ATOMMAX16:
8778 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8779 case X86::ATOMUMIN16:
8780 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8781 case X86::ATOMUMAX16:
8782 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8783
8784 case X86::ATOMAND8:
8785 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8786 X86::AND8ri, X86::MOV8rm,
8787 X86::LCMPXCHG8, X86::MOV8rr,
8788 X86::NOT8r, X86::AL,
8789 X86::GR8RegisterClass);
8790 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008791 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008792 X86::OR8ri, X86::MOV8rm,
8793 X86::LCMPXCHG8, X86::MOV8rr,
8794 X86::NOT8r, X86::AL,
8795 X86::GR8RegisterClass);
8796 case X86::ATOMXOR8:
8797 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8798 X86::XOR8ri, X86::MOV8rm,
8799 X86::LCMPXCHG8, X86::MOV8rr,
8800 X86::NOT8r, X86::AL,
8801 X86::GR8RegisterClass);
8802 case X86::ATOMNAND8:
8803 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8804 X86::AND8ri, X86::MOV8rm,
8805 X86::LCMPXCHG8, X86::MOV8rr,
8806 X86::NOT8r, X86::AL,
8807 X86::GR8RegisterClass, true);
8808 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008809 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008810 case X86::ATOMAND64:
8811 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008812 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008813 X86::LCMPXCHG64, X86::MOV64rr,
8814 X86::NOT64r, X86::RAX,
8815 X86::GR64RegisterClass);
8816 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008817 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8818 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008819 X86::LCMPXCHG64, X86::MOV64rr,
8820 X86::NOT64r, X86::RAX,
8821 X86::GR64RegisterClass);
8822 case X86::ATOMXOR64:
8823 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008824 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008825 X86::LCMPXCHG64, X86::MOV64rr,
8826 X86::NOT64r, X86::RAX,
8827 X86::GR64RegisterClass);
8828 case X86::ATOMNAND64:
8829 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8830 X86::AND64ri32, X86::MOV64rm,
8831 X86::LCMPXCHG64, X86::MOV64rr,
8832 X86::NOT64r, X86::RAX,
8833 X86::GR64RegisterClass, true);
8834 case X86::ATOMMIN64:
8835 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8836 case X86::ATOMMAX64:
8837 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8838 case X86::ATOMUMIN64:
8839 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8840 case X86::ATOMUMAX64:
8841 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008842
8843 // This group does 64-bit operations on a 32-bit host.
8844 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008845 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008846 X86::AND32rr, X86::AND32rr,
8847 X86::AND32ri, X86::AND32ri,
8848 false);
8849 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008850 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008851 X86::OR32rr, X86::OR32rr,
8852 X86::OR32ri, X86::OR32ri,
8853 false);
8854 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008855 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008856 X86::XOR32rr, X86::XOR32rr,
8857 X86::XOR32ri, X86::XOR32ri,
8858 false);
8859 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008860 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008861 X86::AND32rr, X86::AND32rr,
8862 X86::AND32ri, X86::AND32ri,
8863 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008864 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008865 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008866 X86::ADD32rr, X86::ADC32rr,
8867 X86::ADD32ri, X86::ADC32ri,
8868 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008869 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008870 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008871 X86::SUB32rr, X86::SBB32rr,
8872 X86::SUB32ri, X86::SBB32ri,
8873 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008874 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008875 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008876 X86::MOV32rr, X86::MOV32rr,
8877 X86::MOV32ri, X86::MOV32ri,
8878 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008879 case X86::VASTART_SAVE_XMM_REGS:
8880 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008881 }
8882}
8883
8884//===----------------------------------------------------------------------===//
8885// X86 Optimization Hooks
8886//===----------------------------------------------------------------------===//
8887
Dan Gohman475871a2008-07-27 21:46:04 +00008888void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008889 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008890 APInt &KnownZero,
8891 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008892 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008893 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008894 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008895 assert((Opc >= ISD::BUILTIN_OP_END ||
8896 Opc == ISD::INTRINSIC_WO_CHAIN ||
8897 Opc == ISD::INTRINSIC_W_CHAIN ||
8898 Opc == ISD::INTRINSIC_VOID) &&
8899 "Should use MaskedValueIsZero if you don't know whether Op"
8900 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008901
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008902 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008903 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008904 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008905 case X86ISD::ADD:
8906 case X86ISD::SUB:
8907 case X86ISD::SMUL:
8908 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008909 case X86ISD::INC:
8910 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008911 case X86ISD::OR:
8912 case X86ISD::XOR:
8913 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008914 // These nodes' second result is a boolean.
8915 if (Op.getResNo() == 0)
8916 break;
8917 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008918 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008919 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8920 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008921 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008922 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008923}
Chris Lattner259e97c2006-01-31 19:43:35 +00008924
Evan Cheng206ee9d2006-07-07 08:33:52 +00008925/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008926/// node is a GlobalAddress + offset.
8927bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00008928 const GlobalValue* &GA,
8929 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00008930 if (N->getOpcode() == X86ISD::Wrapper) {
8931 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008932 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008933 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008934 return true;
8935 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008936 }
Evan Chengad4196b2008-05-12 19:56:52 +00008937 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008938}
8939
Evan Cheng206ee9d2006-07-07 08:33:52 +00008940/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8941/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8942/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008943/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008944static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008945 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008946 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008947 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008948 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008949
Eli Friedman7a5e5552009-06-07 06:52:44 +00008950 if (VT.getSizeInBits() != 128)
8951 return SDValue();
8952
Nate Begemanfdea31a2010-03-24 20:49:50 +00008953 SmallVector<SDValue, 16> Elts;
8954 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8955 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8956
8957 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008958}
Evan Chengd880b972008-05-09 21:53:03 +00008959
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008960/// PerformShuffleCombine - Detect vector gather/scatter index generation
8961/// and convert it from being a bunch of shuffles and extracts to a simple
8962/// store and scalar loads to extract the elements.
8963static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8964 const TargetLowering &TLI) {
8965 SDValue InputVector = N->getOperand(0);
8966
8967 // Only operate on vectors of 4 elements, where the alternative shuffling
8968 // gets to be more expensive.
8969 if (InputVector.getValueType() != MVT::v4i32)
8970 return SDValue();
8971
8972 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8973 // single use which is a sign-extend or zero-extend, and all elements are
8974 // used.
8975 SmallVector<SDNode *, 4> Uses;
8976 unsigned ExtractedElements = 0;
8977 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8978 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8979 if (UI.getUse().getResNo() != InputVector.getResNo())
8980 return SDValue();
8981
8982 SDNode *Extract = *UI;
8983 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8984 return SDValue();
8985
8986 if (Extract->getValueType(0) != MVT::i32)
8987 return SDValue();
8988 if (!Extract->hasOneUse())
8989 return SDValue();
8990 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8991 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8992 return SDValue();
8993 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8994 return SDValue();
8995
8996 // Record which element was extracted.
8997 ExtractedElements |=
8998 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8999
9000 Uses.push_back(Extract);
9001 }
9002
9003 // If not all the elements were used, this may not be worthwhile.
9004 if (ExtractedElements != 15)
9005 return SDValue();
9006
9007 // Ok, we've now decided to do the transformation.
9008 DebugLoc dl = InputVector.getDebugLoc();
9009
9010 // Store the value to a temporary stack slot.
9011 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
9012 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
9013 false, false, 0);
9014
9015 // Replace each use (extract) with a load of the appropriate element.
9016 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
9017 UE = Uses.end(); UI != UE; ++UI) {
9018 SDNode *Extract = *UI;
9019
9020 // Compute the element's address.
9021 SDValue Idx = Extract->getOperand(1);
9022 unsigned EltSize =
9023 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
9024 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
9025 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
9026
9027 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
9028
9029 // Load the scalar.
9030 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
9031 NULL, 0, false, false, 0);
9032
9033 // Replace the exact with the load.
9034 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
9035 }
9036
9037 // The replacement was made in place; don't return anything.
9038 return SDValue();
9039}
9040
Chris Lattner83e6c992006-10-04 06:57:07 +00009041/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009042static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00009043 const X86Subtarget *Subtarget) {
9044 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009045 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00009046 // Get the LHS/RHS of the select.
9047 SDValue LHS = N->getOperand(1);
9048 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009049
Dan Gohman670e5392009-09-21 18:03:22 +00009050 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00009051 // instructions match the semantics of the common C idiom x<y?x:y but not
9052 // x<=y?x:y, because of how they handle negative zero (which can be
9053 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00009054 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00009055 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00009056 Cond.getOpcode() == ISD::SETCC) {
9057 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009058
Chris Lattner47b4ce82009-03-11 05:48:52 +00009059 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00009060 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00009061 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
9062 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009063 switch (CC) {
9064 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009065 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009066 // Converting this to a min would handle NaNs incorrectly, and swapping
9067 // the operands would cause it to handle comparisons between positive
9068 // and negative zero incorrectly.
9069 if (!FiniteOnlyFPMath() &&
9070 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9071 if (!UnsafeFPMath &&
9072 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9073 break;
9074 std::swap(LHS, RHS);
9075 }
Dan Gohman670e5392009-09-21 18:03:22 +00009076 Opcode = X86ISD::FMIN;
9077 break;
9078 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009079 // Converting this to a min would handle comparisons between positive
9080 // and negative zero incorrectly.
9081 if (!UnsafeFPMath &&
9082 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
9083 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009084 Opcode = X86ISD::FMIN;
9085 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009086 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009087 // Converting this to a min would handle both negative zeros and NaNs
9088 // incorrectly, but we can swap the operands to fix both.
9089 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009090 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009091 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009092 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009093 Opcode = X86ISD::FMIN;
9094 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009095
Dan Gohman670e5392009-09-21 18:03:22 +00009096 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009097 // Converting this to a max would handle comparisons between positive
9098 // and negative zero incorrectly.
9099 if (!UnsafeFPMath &&
9100 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
9101 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009102 Opcode = X86ISD::FMAX;
9103 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009104 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009105 // Converting this to a max would handle NaNs incorrectly, and swapping
9106 // the operands would cause it to handle comparisons between positive
9107 // and negative zero incorrectly.
9108 if (!FiniteOnlyFPMath() &&
9109 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9110 if (!UnsafeFPMath &&
9111 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9112 break;
9113 std::swap(LHS, RHS);
9114 }
Dan Gohman670e5392009-09-21 18:03:22 +00009115 Opcode = X86ISD::FMAX;
9116 break;
9117 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009118 // Converting this to a max would handle both negative zeros and NaNs
9119 // incorrectly, but we can swap the operands to fix both.
9120 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009121 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009122 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009123 case ISD::SETGE:
9124 Opcode = X86ISD::FMAX;
9125 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00009126 }
Dan Gohman670e5392009-09-21 18:03:22 +00009127 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00009128 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
9129 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009130 switch (CC) {
9131 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009132 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009133 // Converting this to a min would handle comparisons between positive
9134 // and negative zero incorrectly, and swapping the operands would
9135 // cause it to handle NaNs incorrectly.
9136 if (!UnsafeFPMath &&
9137 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
9138 if (!FiniteOnlyFPMath() &&
9139 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9140 break;
9141 std::swap(LHS, RHS);
9142 }
Dan Gohman670e5392009-09-21 18:03:22 +00009143 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009144 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009145 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009146 // Converting this to a min would handle NaNs incorrectly.
9147 if (!UnsafeFPMath &&
9148 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9149 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009150 Opcode = X86ISD::FMIN;
9151 break;
9152 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009153 // Converting this to a min would handle both negative zeros and NaNs
9154 // incorrectly, but we can swap the operands to fix both.
9155 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009156 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009157 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009158 case ISD::SETGE:
9159 Opcode = X86ISD::FMIN;
9160 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009161
Dan Gohman670e5392009-09-21 18:03:22 +00009162 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009163 // Converting this to a max would handle NaNs incorrectly.
9164 if (!FiniteOnlyFPMath() &&
9165 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9166 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009167 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009168 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009169 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009170 // Converting this to a max would handle comparisons between positive
9171 // and negative zero incorrectly, and swapping the operands would
9172 // cause it to handle NaNs incorrectly.
9173 if (!UnsafeFPMath &&
9174 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9175 if (!FiniteOnlyFPMath() &&
9176 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9177 break;
9178 std::swap(LHS, RHS);
9179 }
Dan Gohman670e5392009-09-21 18:03:22 +00009180 Opcode = X86ISD::FMAX;
9181 break;
9182 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009183 // Converting this to a max would handle both negative zeros and NaNs
9184 // incorrectly, but we can swap the operands to fix both.
9185 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009186 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009187 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009188 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009189 Opcode = X86ISD::FMAX;
9190 break;
9191 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009192 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009193
Chris Lattner47b4ce82009-03-11 05:48:52 +00009194 if (Opcode)
9195 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009196 }
Eric Christopherfd179292009-08-27 18:07:15 +00009197
Chris Lattnerd1980a52009-03-12 06:52:53 +00009198 // If this is a select between two integer constants, try to do some
9199 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009200 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9201 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009202 // Don't do this for crazy integer types.
9203 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9204 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009205 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009206 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009207
Chris Lattnercee56e72009-03-13 05:53:31 +00009208 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009209 // Efficiently invertible.
9210 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9211 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9212 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9213 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009214 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009215 }
Eric Christopherfd179292009-08-27 18:07:15 +00009216
Chris Lattnerd1980a52009-03-12 06:52:53 +00009217 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009218 if (FalseC->getAPIntValue() == 0 &&
9219 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009220 if (NeedsCondInvert) // Invert the condition if needed.
9221 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9222 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009223
Chris Lattnerd1980a52009-03-12 06:52:53 +00009224 // Zero extend the condition if needed.
9225 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009226
Chris Lattnercee56e72009-03-13 05:53:31 +00009227 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009228 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009229 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009230 }
Eric Christopherfd179292009-08-27 18:07:15 +00009231
Chris Lattner97a29a52009-03-13 05:22:11 +00009232 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009233 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009234 if (NeedsCondInvert) // Invert the condition if needed.
9235 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9236 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009237
Chris Lattner97a29a52009-03-13 05:22:11 +00009238 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009239 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9240 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009241 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009242 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009243 }
Eric Christopherfd179292009-08-27 18:07:15 +00009244
Chris Lattnercee56e72009-03-13 05:53:31 +00009245 // Optimize cases that will turn into an LEA instruction. This requires
9246 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009247 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009248 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009249 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009250
Chris Lattnercee56e72009-03-13 05:53:31 +00009251 bool isFastMultiplier = false;
9252 if (Diff < 10) {
9253 switch ((unsigned char)Diff) {
9254 default: break;
9255 case 1: // result = add base, cond
9256 case 2: // result = lea base( , cond*2)
9257 case 3: // result = lea base(cond, cond*2)
9258 case 4: // result = lea base( , cond*4)
9259 case 5: // result = lea base(cond, cond*4)
9260 case 8: // result = lea base( , cond*8)
9261 case 9: // result = lea base(cond, cond*8)
9262 isFastMultiplier = true;
9263 break;
9264 }
9265 }
Eric Christopherfd179292009-08-27 18:07:15 +00009266
Chris Lattnercee56e72009-03-13 05:53:31 +00009267 if (isFastMultiplier) {
9268 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9269 if (NeedsCondInvert) // Invert the condition if needed.
9270 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9271 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009272
Chris Lattnercee56e72009-03-13 05:53:31 +00009273 // Zero extend the condition if needed.
9274 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9275 Cond);
9276 // Scale the condition by the difference.
9277 if (Diff != 1)
9278 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9279 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009280
Chris Lattnercee56e72009-03-13 05:53:31 +00009281 // Add the base if non-zero.
9282 if (FalseC->getAPIntValue() != 0)
9283 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9284 SDValue(FalseC, 0));
9285 return Cond;
9286 }
Eric Christopherfd179292009-08-27 18:07:15 +00009287 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009288 }
9289 }
Eric Christopherfd179292009-08-27 18:07:15 +00009290
Dan Gohman475871a2008-07-27 21:46:04 +00009291 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009292}
9293
Chris Lattnerd1980a52009-03-12 06:52:53 +00009294/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9295static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9296 TargetLowering::DAGCombinerInfo &DCI) {
9297 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009298
Chris Lattnerd1980a52009-03-12 06:52:53 +00009299 // If the flag operand isn't dead, don't touch this CMOV.
9300 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9301 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009302
Chris Lattnerd1980a52009-03-12 06:52:53 +00009303 // If this is a select between two integer constants, try to do some
9304 // optimizations. Note that the operands are ordered the opposite of SELECT
9305 // operands.
9306 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9307 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9308 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9309 // larger than FalseC (the false value).
9310 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009311
Chris Lattnerd1980a52009-03-12 06:52:53 +00009312 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9313 CC = X86::GetOppositeBranchCondition(CC);
9314 std::swap(TrueC, FalseC);
9315 }
Eric Christopherfd179292009-08-27 18:07:15 +00009316
Chris Lattnerd1980a52009-03-12 06:52:53 +00009317 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009318 // This is efficient for any integer data type (including i8/i16) and
9319 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009320 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9321 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009322 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9323 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009324
Chris Lattnerd1980a52009-03-12 06:52:53 +00009325 // Zero extend the condition if needed.
9326 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009327
Chris Lattnerd1980a52009-03-12 06:52:53 +00009328 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9329 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009330 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009331 if (N->getNumValues() == 2) // Dead flag value?
9332 return DCI.CombineTo(N, Cond, SDValue());
9333 return Cond;
9334 }
Eric Christopherfd179292009-08-27 18:07:15 +00009335
Chris Lattnercee56e72009-03-13 05:53:31 +00009336 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9337 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009338 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9339 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009340 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9341 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009342
Chris Lattner97a29a52009-03-13 05:22:11 +00009343 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009344 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9345 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009346 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9347 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009348
Chris Lattner97a29a52009-03-13 05:22:11 +00009349 if (N->getNumValues() == 2) // Dead flag value?
9350 return DCI.CombineTo(N, Cond, SDValue());
9351 return Cond;
9352 }
Eric Christopherfd179292009-08-27 18:07:15 +00009353
Chris Lattnercee56e72009-03-13 05:53:31 +00009354 // Optimize cases that will turn into an LEA instruction. This requires
9355 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009356 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009357 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009358 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009359
Chris Lattnercee56e72009-03-13 05:53:31 +00009360 bool isFastMultiplier = false;
9361 if (Diff < 10) {
9362 switch ((unsigned char)Diff) {
9363 default: break;
9364 case 1: // result = add base, cond
9365 case 2: // result = lea base( , cond*2)
9366 case 3: // result = lea base(cond, cond*2)
9367 case 4: // result = lea base( , cond*4)
9368 case 5: // result = lea base(cond, cond*4)
9369 case 8: // result = lea base( , cond*8)
9370 case 9: // result = lea base(cond, cond*8)
9371 isFastMultiplier = true;
9372 break;
9373 }
9374 }
Eric Christopherfd179292009-08-27 18:07:15 +00009375
Chris Lattnercee56e72009-03-13 05:53:31 +00009376 if (isFastMultiplier) {
9377 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9378 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009379 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9380 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009381 // Zero extend the condition if needed.
9382 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9383 Cond);
9384 // Scale the condition by the difference.
9385 if (Diff != 1)
9386 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9387 DAG.getConstant(Diff, Cond.getValueType()));
9388
9389 // Add the base if non-zero.
9390 if (FalseC->getAPIntValue() != 0)
9391 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9392 SDValue(FalseC, 0));
9393 if (N->getNumValues() == 2) // Dead flag value?
9394 return DCI.CombineTo(N, Cond, SDValue());
9395 return Cond;
9396 }
Eric Christopherfd179292009-08-27 18:07:15 +00009397 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009398 }
9399 }
9400 return SDValue();
9401}
9402
9403
Evan Cheng0b0cd912009-03-28 05:57:29 +00009404/// PerformMulCombine - Optimize a single multiply with constant into two
9405/// in order to implement it with two cheaper instructions, e.g.
9406/// LEA + SHL, LEA + LEA.
9407static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9408 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009409 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9410 return SDValue();
9411
Owen Andersone50ed302009-08-10 22:56:29 +00009412 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009413 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009414 return SDValue();
9415
9416 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9417 if (!C)
9418 return SDValue();
9419 uint64_t MulAmt = C->getZExtValue();
9420 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9421 return SDValue();
9422
9423 uint64_t MulAmt1 = 0;
9424 uint64_t MulAmt2 = 0;
9425 if ((MulAmt % 9) == 0) {
9426 MulAmt1 = 9;
9427 MulAmt2 = MulAmt / 9;
9428 } else if ((MulAmt % 5) == 0) {
9429 MulAmt1 = 5;
9430 MulAmt2 = MulAmt / 5;
9431 } else if ((MulAmt % 3) == 0) {
9432 MulAmt1 = 3;
9433 MulAmt2 = MulAmt / 3;
9434 }
9435 if (MulAmt2 &&
9436 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9437 DebugLoc DL = N->getDebugLoc();
9438
9439 if (isPowerOf2_64(MulAmt2) &&
9440 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9441 // If second multiplifer is pow2, issue it first. We want the multiply by
9442 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9443 // is an add.
9444 std::swap(MulAmt1, MulAmt2);
9445
9446 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009447 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009448 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009449 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009450 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009451 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009452 DAG.getConstant(MulAmt1, VT));
9453
Eric Christopherfd179292009-08-27 18:07:15 +00009454 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009455 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009456 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009457 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009458 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009459 DAG.getConstant(MulAmt2, VT));
9460
9461 // Do not add new nodes to DAG combiner worklist.
9462 DCI.CombineTo(N, NewMul, false);
9463 }
9464 return SDValue();
9465}
9466
Evan Chengad9c0a32009-12-15 00:53:42 +00009467static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9468 SDValue N0 = N->getOperand(0);
9469 SDValue N1 = N->getOperand(1);
9470 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9471 EVT VT = N0.getValueType();
9472
9473 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9474 // since the result of setcc_c is all zero's or all ones.
9475 if (N1C && N0.getOpcode() == ISD::AND &&
9476 N0.getOperand(1).getOpcode() == ISD::Constant) {
9477 SDValue N00 = N0.getOperand(0);
9478 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9479 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9480 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9481 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9482 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9483 APInt ShAmt = N1C->getAPIntValue();
9484 Mask = Mask.shl(ShAmt);
9485 if (Mask != 0)
9486 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9487 N00, DAG.getConstant(Mask, VT));
9488 }
9489 }
9490
9491 return SDValue();
9492}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009493
Nate Begeman740ab032009-01-26 00:52:55 +00009494/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9495/// when possible.
9496static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9497 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009498 EVT VT = N->getValueType(0);
9499 if (!VT.isVector() && VT.isInteger() &&
9500 N->getOpcode() == ISD::SHL)
9501 return PerformSHLCombine(N, DAG);
9502
Nate Begeman740ab032009-01-26 00:52:55 +00009503 // On X86 with SSE2 support, we can transform this to a vector shift if
9504 // all elements are shifted by the same amount. We can't do this in legalize
9505 // because the a constant vector is typically transformed to a constant pool
9506 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009507 if (!Subtarget->hasSSE2())
9508 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009509
Owen Anderson825b72b2009-08-11 20:47:22 +00009510 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009511 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009512
Mon P Wang3becd092009-01-28 08:12:05 +00009513 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009514 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009515 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009516 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009517 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9518 unsigned NumElts = VT.getVectorNumElements();
9519 unsigned i = 0;
9520 for (; i != NumElts; ++i) {
9521 SDValue Arg = ShAmtOp.getOperand(i);
9522 if (Arg.getOpcode() == ISD::UNDEF) continue;
9523 BaseShAmt = Arg;
9524 break;
9525 }
9526 for (; i != NumElts; ++i) {
9527 SDValue Arg = ShAmtOp.getOperand(i);
9528 if (Arg.getOpcode() == ISD::UNDEF) continue;
9529 if (Arg != BaseShAmt) {
9530 return SDValue();
9531 }
9532 }
9533 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009534 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009535 SDValue InVec = ShAmtOp.getOperand(0);
9536 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9537 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9538 unsigned i = 0;
9539 for (; i != NumElts; ++i) {
9540 SDValue Arg = InVec.getOperand(i);
9541 if (Arg.getOpcode() == ISD::UNDEF) continue;
9542 BaseShAmt = Arg;
9543 break;
9544 }
9545 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9546 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009547 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009548 if (C->getZExtValue() == SplatIdx)
9549 BaseShAmt = InVec.getOperand(1);
9550 }
9551 }
9552 if (BaseShAmt.getNode() == 0)
9553 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9554 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009555 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009556 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009557
Mon P Wangefa42202009-09-03 19:56:25 +00009558 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009559 if (EltVT.bitsGT(MVT::i32))
9560 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9561 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009562 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009563
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009564 // The shift amount is identical so we can do a vector shift.
9565 SDValue ValOp = N->getOperand(0);
9566 switch (N->getOpcode()) {
9567 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009568 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009569 break;
9570 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009571 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009572 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009573 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009574 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009575 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009576 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009577 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009578 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009579 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009580 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009581 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009582 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009583 break;
9584 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009585 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009586 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009587 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009588 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009589 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009590 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009591 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009592 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009593 break;
9594 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009595 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009596 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009597 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009598 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009599 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009600 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009601 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009602 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009603 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009604 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009605 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009606 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009607 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009608 }
9609 return SDValue();
9610}
9611
Evan Cheng760d1942010-01-04 21:22:48 +00009612static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9613 const X86Subtarget *Subtarget) {
9614 EVT VT = N->getValueType(0);
9615 if (VT != MVT::i64 || !Subtarget->is64Bit())
9616 return SDValue();
9617
9618 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9619 SDValue N0 = N->getOperand(0);
9620 SDValue N1 = N->getOperand(1);
9621 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9622 std::swap(N0, N1);
9623 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9624 return SDValue();
9625
9626 SDValue ShAmt0 = N0.getOperand(1);
9627 if (ShAmt0.getValueType() != MVT::i8)
9628 return SDValue();
9629 SDValue ShAmt1 = N1.getOperand(1);
9630 if (ShAmt1.getValueType() != MVT::i8)
9631 return SDValue();
9632 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9633 ShAmt0 = ShAmt0.getOperand(0);
9634 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9635 ShAmt1 = ShAmt1.getOperand(0);
9636
9637 DebugLoc DL = N->getDebugLoc();
9638 unsigned Opc = X86ISD::SHLD;
9639 SDValue Op0 = N0.getOperand(0);
9640 SDValue Op1 = N1.getOperand(0);
9641 if (ShAmt0.getOpcode() == ISD::SUB) {
9642 Opc = X86ISD::SHRD;
9643 std::swap(Op0, Op1);
9644 std::swap(ShAmt0, ShAmt1);
9645 }
9646
9647 if (ShAmt1.getOpcode() == ISD::SUB) {
9648 SDValue Sum = ShAmt1.getOperand(0);
9649 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9650 if (SumC->getSExtValue() == 64 &&
9651 ShAmt1.getOperand(1) == ShAmt0)
9652 return DAG.getNode(Opc, DL, VT,
9653 Op0, Op1,
9654 DAG.getNode(ISD::TRUNCATE, DL,
9655 MVT::i8, ShAmt0));
9656 }
9657 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9658 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9659 if (ShAmt0C &&
9660 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9661 return DAG.getNode(Opc, DL, VT,
9662 N0.getOperand(0), N1.getOperand(0),
9663 DAG.getNode(ISD::TRUNCATE, DL,
9664 MVT::i8, ShAmt0));
9665 }
9666
9667 return SDValue();
9668}
9669
Chris Lattner149a4e52008-02-22 02:09:43 +00009670/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009671static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009672 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009673 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9674 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009675 // A preferable solution to the general problem is to figure out the right
9676 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009677
9678 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009679 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009680 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009681 if (VT.getSizeInBits() != 64)
9682 return SDValue();
9683
Devang Patel578efa92009-06-05 21:57:13 +00009684 const Function *F = DAG.getMachineFunction().getFunction();
9685 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009686 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009687 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009688 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009689 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009690 isa<LoadSDNode>(St->getValue()) &&
9691 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9692 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009693 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009694 LoadSDNode *Ld = 0;
9695 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009696 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009697 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009698 // Must be a store of a load. We currently handle two cases: the load
9699 // is a direct child, and it's under an intervening TokenFactor. It is
9700 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009701 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009702 Ld = cast<LoadSDNode>(St->getChain());
9703 else if (St->getValue().hasOneUse() &&
9704 ChainVal->getOpcode() == ISD::TokenFactor) {
9705 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009706 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009707 TokenFactorIndex = i;
9708 Ld = cast<LoadSDNode>(St->getValue());
9709 } else
9710 Ops.push_back(ChainVal->getOperand(i));
9711 }
9712 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009713
Evan Cheng536e6672009-03-12 05:59:15 +00009714 if (!Ld || !ISD::isNormalLoad(Ld))
9715 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009716
Evan Cheng536e6672009-03-12 05:59:15 +00009717 // If this is not the MMX case, i.e. we are just turning i64 load/store
9718 // into f64 load/store, avoid the transformation if there are multiple
9719 // uses of the loaded value.
9720 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9721 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009722
Evan Cheng536e6672009-03-12 05:59:15 +00009723 DebugLoc LdDL = Ld->getDebugLoc();
9724 DebugLoc StDL = N->getDebugLoc();
9725 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9726 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9727 // pair instead.
9728 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009729 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009730 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9731 Ld->getBasePtr(), Ld->getSrcValue(),
9732 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009733 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009734 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009735 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009736 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009737 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009738 Ops.size());
9739 }
Evan Cheng536e6672009-03-12 05:59:15 +00009740 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009741 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009742 St->isVolatile(), St->isNonTemporal(),
9743 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009744 }
Evan Cheng536e6672009-03-12 05:59:15 +00009745
9746 // Otherwise, lower to two pairs of 32-bit loads / stores.
9747 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009748 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9749 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009750
Owen Anderson825b72b2009-08-11 20:47:22 +00009751 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009752 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009753 Ld->isVolatile(), Ld->isNonTemporal(),
9754 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009755 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009756 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009757 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009758 MinAlign(Ld->getAlignment(), 4));
9759
9760 SDValue NewChain = LoLd.getValue(1);
9761 if (TokenFactorIndex != -1) {
9762 Ops.push_back(LoLd);
9763 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009764 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009765 Ops.size());
9766 }
9767
9768 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009769 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9770 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009771
9772 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9773 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009774 St->isVolatile(), St->isNonTemporal(),
9775 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009776 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9777 St->getSrcValue(),
9778 St->getSrcValueOffset() + 4,
9779 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009780 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009781 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009782 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009783 }
Dan Gohman475871a2008-07-27 21:46:04 +00009784 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009785}
9786
Chris Lattner6cf73262008-01-25 06:14:17 +00009787/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9788/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009789static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009790 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9791 // F[X]OR(0.0, x) -> x
9792 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009793 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9794 if (C->getValueAPF().isPosZero())
9795 return N->getOperand(1);
9796 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9797 if (C->getValueAPF().isPosZero())
9798 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009799 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009800}
9801
9802/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009803static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009804 // FAND(0.0, x) -> 0.0
9805 // FAND(x, 0.0) -> 0.0
9806 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9807 if (C->getValueAPF().isPosZero())
9808 return N->getOperand(0);
9809 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9810 if (C->getValueAPF().isPosZero())
9811 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009812 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009813}
9814
Dan Gohmane5af2d32009-01-29 01:59:02 +00009815static SDValue PerformBTCombine(SDNode *N,
9816 SelectionDAG &DAG,
9817 TargetLowering::DAGCombinerInfo &DCI) {
9818 // BT ignores high bits in the bit index operand.
9819 SDValue Op1 = N->getOperand(1);
9820 if (Op1.hasOneUse()) {
9821 unsigned BitWidth = Op1.getValueSizeInBits();
9822 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9823 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009824 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
9825 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +00009826 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +00009827 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9828 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9829 DCI.CommitTargetLoweringOpt(TLO);
9830 }
9831 return SDValue();
9832}
Chris Lattner83e6c992006-10-04 06:57:07 +00009833
Eli Friedman7a5e5552009-06-07 06:52:44 +00009834static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9835 SDValue Op = N->getOperand(0);
9836 if (Op.getOpcode() == ISD::BIT_CONVERT)
9837 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009838 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009839 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009840 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009841 OpVT.getVectorElementType().getSizeInBits()) {
9842 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9843 }
9844 return SDValue();
9845}
9846
Owen Anderson99177002009-06-29 18:04:45 +00009847// On X86 and X86-64, atomic operations are lowered to locked instructions.
9848// Locked instructions, in turn, have implicit fence semantics (all memory
9849// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009850// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009851// fence-atomic-fence.
9852static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9853 SDValue atomic = N->getOperand(0);
9854 switch (atomic.getOpcode()) {
9855 case ISD::ATOMIC_CMP_SWAP:
9856 case ISD::ATOMIC_SWAP:
9857 case ISD::ATOMIC_LOAD_ADD:
9858 case ISD::ATOMIC_LOAD_SUB:
9859 case ISD::ATOMIC_LOAD_AND:
9860 case ISD::ATOMIC_LOAD_OR:
9861 case ISD::ATOMIC_LOAD_XOR:
9862 case ISD::ATOMIC_LOAD_NAND:
9863 case ISD::ATOMIC_LOAD_MIN:
9864 case ISD::ATOMIC_LOAD_MAX:
9865 case ISD::ATOMIC_LOAD_UMIN:
9866 case ISD::ATOMIC_LOAD_UMAX:
9867 break;
9868 default:
9869 return SDValue();
9870 }
Eric Christopherfd179292009-08-27 18:07:15 +00009871
Owen Anderson99177002009-06-29 18:04:45 +00009872 SDValue fence = atomic.getOperand(0);
9873 if (fence.getOpcode() != ISD::MEMBARRIER)
9874 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009875
Owen Anderson99177002009-06-29 18:04:45 +00009876 switch (atomic.getOpcode()) {
9877 case ISD::ATOMIC_CMP_SWAP:
9878 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9879 atomic.getOperand(1), atomic.getOperand(2),
9880 atomic.getOperand(3));
9881 case ISD::ATOMIC_SWAP:
9882 case ISD::ATOMIC_LOAD_ADD:
9883 case ISD::ATOMIC_LOAD_SUB:
9884 case ISD::ATOMIC_LOAD_AND:
9885 case ISD::ATOMIC_LOAD_OR:
9886 case ISD::ATOMIC_LOAD_XOR:
9887 case ISD::ATOMIC_LOAD_NAND:
9888 case ISD::ATOMIC_LOAD_MIN:
9889 case ISD::ATOMIC_LOAD_MAX:
9890 case ISD::ATOMIC_LOAD_UMIN:
9891 case ISD::ATOMIC_LOAD_UMAX:
9892 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9893 atomic.getOperand(1), atomic.getOperand(2));
9894 default:
9895 return SDValue();
9896 }
9897}
9898
Evan Cheng2e489c42009-12-16 00:53:11 +00009899static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9900 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9901 // (and (i32 x86isd::setcc_carry), 1)
9902 // This eliminates the zext. This transformation is necessary because
9903 // ISD::SETCC is always legalized to i8.
9904 DebugLoc dl = N->getDebugLoc();
9905 SDValue N0 = N->getOperand(0);
9906 EVT VT = N->getValueType(0);
9907 if (N0.getOpcode() == ISD::AND &&
9908 N0.hasOneUse() &&
9909 N0.getOperand(0).hasOneUse()) {
9910 SDValue N00 = N0.getOperand(0);
9911 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9912 return SDValue();
9913 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9914 if (!C || C->getZExtValue() != 1)
9915 return SDValue();
9916 return DAG.getNode(ISD::AND, dl, VT,
9917 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9918 N00.getOperand(0), N00.getOperand(1)),
9919 DAG.getConstant(1, VT));
9920 }
9921
9922 return SDValue();
9923}
9924
Dan Gohman475871a2008-07-27 21:46:04 +00009925SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009926 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009927 SelectionDAG &DAG = DCI.DAG;
9928 switch (N->getOpcode()) {
9929 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009930 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009931 case ISD::EXTRACT_VECTOR_ELT:
9932 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009933 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009934 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009935 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009936 case ISD::SHL:
9937 case ISD::SRA:
9938 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009939 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009940 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009941 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009942 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9943 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009944 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009945 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009946 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009947 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009948 }
9949
Dan Gohman475871a2008-07-27 21:46:04 +00009950 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009951}
9952
Evan Chenge5b51ac2010-04-17 06:13:15 +00009953/// isTypeDesirableForOp - Return true if the target has native support for
9954/// the specified value type and it is 'desirable' to use the type for the
9955/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
9956/// instruction encodings are longer and some i16 instructions are slow.
9957bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
9958 if (!isTypeLegal(VT))
9959 return false;
Evan Cheng5528e7b2010-04-21 01:47:12 +00009960 if (!Subtarget->shouldPromote16Bit() || VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +00009961 return true;
9962
9963 switch (Opc) {
9964 default:
9965 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +00009966 case ISD::LOAD:
9967 case ISD::SIGN_EXTEND:
9968 case ISD::ZERO_EXTEND:
9969 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009970 case ISD::SHL:
9971 case ISD::SRA:
9972 case ISD::SRL:
9973 case ISD::SUB:
9974 case ISD::ADD:
9975 case ISD::MUL:
9976 case ISD::AND:
9977 case ISD::OR:
9978 case ISD::XOR:
9979 return false;
9980 }
9981}
9982
9983/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +00009984/// beneficial for dag combiner to promote the specified node. If true, it
9985/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009986bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng5528e7b2010-04-21 01:47:12 +00009987 if (!Subtarget->shouldPromote16Bit())
Evan Cheng64b7bf72010-04-16 06:14:10 +00009988 return false;
9989
9990 EVT VT = Op.getValueType();
9991 if (VT != MVT::i16)
9992 return false;
9993
Evan Cheng4c26e932010-04-19 19:29:22 +00009994 bool Promote = false;
9995 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009996 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +00009997 default: break;
9998 case ISD::LOAD: {
9999 LoadSDNode *LD = cast<LoadSDNode>(Op);
10000 // If the non-extending load has a single use and it's not live out, then it
10001 // might be folded.
10002 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
10003 Op.hasOneUse() &&
10004 Op.getNode()->use_begin()->getOpcode() != ISD::CopyToReg)
10005 return false;
10006 Promote = true;
10007 break;
10008 }
10009 case ISD::SIGN_EXTEND:
10010 case ISD::ZERO_EXTEND:
10011 case ISD::ANY_EXTEND:
10012 Promote = true;
10013 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010014 case ISD::SHL:
10015 case ISD::SRA:
Evan Cheng07c4e102010-04-22 20:19:46 +000010016 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000010017 SDValue N0 = Op.getOperand(0);
10018 // Look out for (store (shl (load), x)).
10019 if (isa<LoadSDNode>(N0) && N0.hasOneUse() &&
10020 Op.hasOneUse() && Op.getNode()->use_begin()->getOpcode() == ISD::STORE)
10021 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010022 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010023 break;
10024 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000010025 case ISD::ADD:
10026 case ISD::MUL:
10027 case ISD::AND:
10028 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000010029 case ISD::XOR:
10030 Commute = true;
10031 // fallthrough
10032 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010033 SDValue N0 = Op.getOperand(0);
10034 SDValue N1 = Op.getOperand(1);
10035 if (!Commute && isa<LoadSDNode>(N1))
10036 return false;
10037 // Avoid disabling potential load folding opportunities.
10038 if ((isa<LoadSDNode>(N0) && N0.hasOneUse()) && !isa<ConstantSDNode>(N1))
10039 return false;
10040 if ((isa<LoadSDNode>(N1) && N1.hasOneUse()) && !isa<ConstantSDNode>(N0))
10041 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010042 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010043 }
10044 }
10045
10046 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000010047 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010048}
10049
Evan Cheng60c07e12006-07-05 22:17:51 +000010050//===----------------------------------------------------------------------===//
10051// X86 Inline Assembly Support
10052//===----------------------------------------------------------------------===//
10053
Chris Lattnerb8105652009-07-20 17:51:36 +000010054static bool LowerToBSwap(CallInst *CI) {
10055 // FIXME: this should verify that we are targetting a 486 or better. If not,
10056 // we will turn this bswap into something that will be lowered to logical ops
10057 // instead of emitting the bswap asm. For now, we don't support 486 or lower
10058 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000010059
Chris Lattnerb8105652009-07-20 17:51:36 +000010060 // Verify this is a simple bswap.
10061 if (CI->getNumOperands() != 2 ||
Eric Christopher551754c2010-04-16 23:37:20 +000010062 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010063 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000010064 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010065
Chris Lattnerb8105652009-07-20 17:51:36 +000010066 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
10067 if (!Ty || Ty->getBitWidth() % 16 != 0)
10068 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010069
Chris Lattnerb8105652009-07-20 17:51:36 +000010070 // Okay, we can do this xform, do so now.
10071 const Type *Tys[] = { Ty };
10072 Module *M = CI->getParent()->getParent()->getParent();
10073 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000010074
Eric Christopher551754c2010-04-16 23:37:20 +000010075 Value *Op = CI->getOperand(1);
Chris Lattnerb8105652009-07-20 17:51:36 +000010076 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000010077
Chris Lattnerb8105652009-07-20 17:51:36 +000010078 CI->replaceAllUsesWith(Op);
10079 CI->eraseFromParent();
10080 return true;
10081}
10082
10083bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
10084 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10085 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
10086
10087 std::string AsmStr = IA->getAsmString();
10088
10089 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010090 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000010091 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
10092
10093 switch (AsmPieces.size()) {
10094 default: return false;
10095 case 1:
10096 AsmStr = AsmPieces[0];
10097 AsmPieces.clear();
10098 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
10099
10100 // bswap $0
10101 if (AsmPieces.size() == 2 &&
10102 (AsmPieces[0] == "bswap" ||
10103 AsmPieces[0] == "bswapq" ||
10104 AsmPieces[0] == "bswapl") &&
10105 (AsmPieces[1] == "$0" ||
10106 AsmPieces[1] == "${0:q}")) {
10107 // No need to check constraints, nothing other than the equivalent of
10108 // "=r,0" would be valid here.
10109 return LowerToBSwap(CI);
10110 }
10111 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010112 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010113 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010114 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010115 AsmPieces[1] == "$$8," &&
10116 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010117 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
10118 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000010119 const std::string &Constraints = IA->getConstraintString();
10120 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000010121 std::sort(AsmPieces.begin(), AsmPieces.end());
10122 if (AsmPieces.size() == 4 &&
10123 AsmPieces[0] == "~{cc}" &&
10124 AsmPieces[1] == "~{dirflag}" &&
10125 AsmPieces[2] == "~{flags}" &&
10126 AsmPieces[3] == "~{fpsr}") {
10127 return LowerToBSwap(CI);
10128 }
Chris Lattnerb8105652009-07-20 17:51:36 +000010129 }
10130 break;
10131 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010132 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000010133 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010134 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
10135 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
10136 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010137 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000010138 SplitString(AsmPieces[0], Words, " \t");
10139 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
10140 Words.clear();
10141 SplitString(AsmPieces[1], Words, " \t");
10142 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
10143 Words.clear();
10144 SplitString(AsmPieces[2], Words, " \t,");
10145 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
10146 Words[2] == "%edx") {
10147 return LowerToBSwap(CI);
10148 }
10149 }
10150 }
10151 }
10152 break;
10153 }
10154 return false;
10155}
10156
10157
10158
Chris Lattnerf4dff842006-07-11 02:54:03 +000010159/// getConstraintType - Given a constraint letter, return the type of
10160/// constraint it is for this target.
10161X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010162X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10163 if (Constraint.size() == 1) {
10164 switch (Constraint[0]) {
10165 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010166 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010167 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010168 case 'r':
10169 case 'R':
10170 case 'l':
10171 case 'q':
10172 case 'Q':
10173 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010174 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010175 case 'Y':
10176 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010177 case 'e':
10178 case 'Z':
10179 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010180 default:
10181 break;
10182 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010183 }
Chris Lattner4234f572007-03-25 02:14:49 +000010184 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010185}
10186
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010187/// LowerXConstraint - try to replace an X constraint, which matches anything,
10188/// with another that has more specific requirements based on the type of the
10189/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010190const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010191LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010192 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10193 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010194 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010195 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010196 return "Y";
10197 if (Subtarget->hasSSE1())
10198 return "x";
10199 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010200
Chris Lattner5e764232008-04-26 23:02:14 +000010201 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010202}
10203
Chris Lattner48884cd2007-08-25 00:47:38 +000010204/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10205/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010206void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010207 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +000010208 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +000010209 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010210 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010211 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010212
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010213 switch (Constraint) {
10214 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010215 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010216 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010217 if (C->getZExtValue() <= 31) {
10218 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010219 break;
10220 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010221 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010222 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010223 case 'J':
10224 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010225 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010226 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10227 break;
10228 }
10229 }
10230 return;
10231 case 'K':
10232 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010233 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010234 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10235 break;
10236 }
10237 }
10238 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010239 case 'N':
10240 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010241 if (C->getZExtValue() <= 255) {
10242 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010243 break;
10244 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010245 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010246 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010247 case 'e': {
10248 // 32-bit signed value
10249 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10250 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010251 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10252 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010253 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010254 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010255 break;
10256 }
10257 // FIXME gcc accepts some relocatable values here too, but only in certain
10258 // memory models; it's complicated.
10259 }
10260 return;
10261 }
10262 case 'Z': {
10263 // 32-bit unsigned value
10264 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10265 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010266 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10267 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010268 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10269 break;
10270 }
10271 }
10272 // FIXME gcc accepts some relocatable values here too, but only in certain
10273 // memory models; it's complicated.
10274 return;
10275 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010276 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010277 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010278 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010279 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010280 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010281 break;
10282 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010283
Chris Lattnerdc43a882007-05-03 16:52:29 +000010284 // If we are in non-pic codegen mode, we allow the address of a global (with
10285 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010286 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010287 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010288
Chris Lattner49921962009-05-08 18:23:14 +000010289 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10290 while (1) {
10291 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10292 Offset += GA->getOffset();
10293 break;
10294 } else if (Op.getOpcode() == ISD::ADD) {
10295 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10296 Offset += C->getZExtValue();
10297 Op = Op.getOperand(0);
10298 continue;
10299 }
10300 } else if (Op.getOpcode() == ISD::SUB) {
10301 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10302 Offset += -C->getZExtValue();
10303 Op = Op.getOperand(0);
10304 continue;
10305 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010306 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010307
Chris Lattner49921962009-05-08 18:23:14 +000010308 // Otherwise, this isn't something we can handle, reject it.
10309 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010310 }
Eric Christopherfd179292009-08-27 18:07:15 +000010311
Dan Gohman46510a72010-04-15 01:51:59 +000010312 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010313 // If we require an extra load to get this address, as in PIC mode, we
10314 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010315 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10316 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010317 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010318
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010319 if (hasMemory)
10320 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10321 else
10322 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010323 Result = Op;
10324 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010325 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010326 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010327
Gabor Greifba36cb52008-08-28 21:40:38 +000010328 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010329 Ops.push_back(Result);
10330 return;
10331 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010332 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10333 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010334}
10335
Chris Lattner259e97c2006-01-31 19:43:35 +000010336std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010337getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010338 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010339 if (Constraint.size() == 1) {
10340 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010341 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010342 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010343 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10344 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010345 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010346 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10347 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10348 X86::R10D,X86::R11D,X86::R12D,
10349 X86::R13D,X86::R14D,X86::R15D,
10350 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010351 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010352 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10353 X86::SI, X86::DI, X86::R8W,X86::R9W,
10354 X86::R10W,X86::R11W,X86::R12W,
10355 X86::R13W,X86::R14W,X86::R15W,
10356 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010357 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010358 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10359 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10360 X86::R10B,X86::R11B,X86::R12B,
10361 X86::R13B,X86::R14B,X86::R15B,
10362 X86::BPL, X86::SPL, 0);
10363
Owen Anderson825b72b2009-08-11 20:47:22 +000010364 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010365 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10366 X86::RSI, X86::RDI, X86::R8, X86::R9,
10367 X86::R10, X86::R11, X86::R12,
10368 X86::R13, X86::R14, X86::R15,
10369 X86::RBP, X86::RSP, 0);
10370
10371 break;
10372 }
Eric Christopherfd179292009-08-27 18:07:15 +000010373 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010374 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010375 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010376 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010377 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010378 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010379 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010380 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010381 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010382 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10383 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010384 }
10385 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010386
Chris Lattner1efa40f2006-02-22 00:56:39 +000010387 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010388}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010389
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010390std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010391X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010392 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010393 // First, see if this is a constraint that directly corresponds to an LLVM
10394 // register class.
10395 if (Constraint.size() == 1) {
10396 // GCC Constraint Letters
10397 switch (Constraint[0]) {
10398 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010399 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010400 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010401 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010402 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010403 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010404 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010405 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010406 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010407 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010408 case 'R': // LEGACY_REGS
10409 if (VT == MVT::i8)
10410 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10411 if (VT == MVT::i16)
10412 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10413 if (VT == MVT::i32 || !Subtarget->is64Bit())
10414 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10415 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010416 case 'f': // FP Stack registers.
10417 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10418 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010419 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010420 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010421 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010422 return std::make_pair(0U, X86::RFP64RegisterClass);
10423 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010424 case 'y': // MMX_REGS if MMX allowed.
10425 if (!Subtarget->hasMMX()) break;
10426 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010427 case 'Y': // SSE_REGS if SSE2 allowed
10428 if (!Subtarget->hasSSE2()) break;
10429 // FALL THROUGH.
10430 case 'x': // SSE_REGS if SSE1 allowed
10431 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010432
Owen Anderson825b72b2009-08-11 20:47:22 +000010433 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010434 default: break;
10435 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010436 case MVT::f32:
10437 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010438 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010439 case MVT::f64:
10440 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010441 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010442 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010443 case MVT::v16i8:
10444 case MVT::v8i16:
10445 case MVT::v4i32:
10446 case MVT::v2i64:
10447 case MVT::v4f32:
10448 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010449 return std::make_pair(0U, X86::VR128RegisterClass);
10450 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010451 break;
10452 }
10453 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010454
Chris Lattnerf76d1802006-07-31 23:26:50 +000010455 // Use the default implementation in TargetLowering to convert the register
10456 // constraint into a member of a register class.
10457 std::pair<unsigned, const TargetRegisterClass*> Res;
10458 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010459
10460 // Not found as a standard register?
10461 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010462 // Map st(0) -> st(7) -> ST0
10463 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10464 tolower(Constraint[1]) == 's' &&
10465 tolower(Constraint[2]) == 't' &&
10466 Constraint[3] == '(' &&
10467 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10468 Constraint[5] == ')' &&
10469 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010470
Chris Lattner56d77c72009-09-13 22:41:48 +000010471 Res.first = X86::ST0+Constraint[4]-'0';
10472 Res.second = X86::RFP80RegisterClass;
10473 return Res;
10474 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010475
Chris Lattner56d77c72009-09-13 22:41:48 +000010476 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010477 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010478 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010479 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010480 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010481 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010482
10483 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010484 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010485 Res.first = X86::EFLAGS;
10486 Res.second = X86::CCRRegisterClass;
10487 return Res;
10488 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010489
Dale Johannesen330169f2008-11-13 21:52:36 +000010490 // 'A' means EAX + EDX.
10491 if (Constraint == "A") {
10492 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010493 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010494 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010495 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010496 return Res;
10497 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010498
Chris Lattnerf76d1802006-07-31 23:26:50 +000010499 // Otherwise, check to see if this is a register class of the wrong value
10500 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10501 // turn into {ax},{dx}.
10502 if (Res.second->hasType(VT))
10503 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010504
Chris Lattnerf76d1802006-07-31 23:26:50 +000010505 // All of the single-register GCC register classes map their values onto
10506 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10507 // really want an 8-bit or 32-bit register, map to the appropriate register
10508 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010509 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010510 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010511 unsigned DestReg = 0;
10512 switch (Res.first) {
10513 default: break;
10514 case X86::AX: DestReg = X86::AL; break;
10515 case X86::DX: DestReg = X86::DL; break;
10516 case X86::CX: DestReg = X86::CL; break;
10517 case X86::BX: DestReg = X86::BL; break;
10518 }
10519 if (DestReg) {
10520 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010521 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010522 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010523 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010524 unsigned DestReg = 0;
10525 switch (Res.first) {
10526 default: break;
10527 case X86::AX: DestReg = X86::EAX; break;
10528 case X86::DX: DestReg = X86::EDX; break;
10529 case X86::CX: DestReg = X86::ECX; break;
10530 case X86::BX: DestReg = X86::EBX; break;
10531 case X86::SI: DestReg = X86::ESI; break;
10532 case X86::DI: DestReg = X86::EDI; break;
10533 case X86::BP: DestReg = X86::EBP; break;
10534 case X86::SP: DestReg = X86::ESP; break;
10535 }
10536 if (DestReg) {
10537 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010538 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010539 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010540 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010541 unsigned DestReg = 0;
10542 switch (Res.first) {
10543 default: break;
10544 case X86::AX: DestReg = X86::RAX; break;
10545 case X86::DX: DestReg = X86::RDX; break;
10546 case X86::CX: DestReg = X86::RCX; break;
10547 case X86::BX: DestReg = X86::RBX; break;
10548 case X86::SI: DestReg = X86::RSI; break;
10549 case X86::DI: DestReg = X86::RDI; break;
10550 case X86::BP: DestReg = X86::RBP; break;
10551 case X86::SP: DestReg = X86::RSP; break;
10552 }
10553 if (DestReg) {
10554 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010555 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010556 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010557 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010558 } else if (Res.second == X86::FR32RegisterClass ||
10559 Res.second == X86::FR64RegisterClass ||
10560 Res.second == X86::VR128RegisterClass) {
10561 // Handle references to XMM physical registers that got mapped into the
10562 // wrong class. This can happen with constraints like {xmm0} where the
10563 // target independent register mapper will just pick the first match it can
10564 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010565 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010566 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010567 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010568 Res.second = X86::FR64RegisterClass;
10569 else if (X86::VR128RegisterClass->hasType(VT))
10570 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010571 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010572
Chris Lattnerf76d1802006-07-31 23:26:50 +000010573 return Res;
10574}