blob: cdbe1ad9f2e2b787943fe6122ec0c2fb6bb3868c [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();
Evan Cheng13617962010-04-30 01:12:32 +00002329 CallingConv::ID CallerCC = CallerF->getCallingConv();
2330 bool CCMatch = CallerCC == CalleeCC;
2331
Dan Gohman1797ed52010-02-08 20:27:50 +00002332 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002333 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002334 return true;
2335 return false;
2336 }
2337
Evan Chengb2c92902010-02-02 02:22:50 +00002338 // Look for obvious safe cases to perform tail call optimization that does not
2339 // requite ABI changes. This is what gcc calls sibcall.
2340
Evan Cheng2c12cb42010-03-26 16:26:03 +00002341 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2342 // emit a special epilogue.
2343 if (RegInfo->needsStackRealignment(MF))
2344 return false;
2345
Evan Cheng3c262ee2010-03-26 02:13:13 +00002346 // Do not sibcall optimize vararg calls unless the call site is not passing any
2347 // arguments.
2348 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002349 return false;
2350
Evan Chenga375d472010-03-15 18:54:48 +00002351 // Also avoid sibcall optimization if either caller or callee uses struct
2352 // return semantics.
2353 if (isCalleeStructRet || isCallerStructRet)
2354 return false;
2355
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002356 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2357 // Therefore if it's not used by the call it is not safe to optimize this into
2358 // a sibcall.
2359 bool Unused = false;
2360 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2361 if (!Ins[i].Used) {
2362 Unused = true;
2363 break;
2364 }
2365 }
2366 if (Unused) {
2367 SmallVector<CCValAssign, 16> RVLocs;
2368 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2369 RVLocs, *DAG.getContext());
2370 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002371 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002372 CCValAssign &VA = RVLocs[i];
2373 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2374 return false;
2375 }
2376 }
2377
Evan Cheng13617962010-04-30 01:12:32 +00002378 // If the calling conventions do not match, then we'd better make sure the
2379 // results are returned in the same way as what the caller expects.
2380 if (!CCMatch) {
2381 SmallVector<CCValAssign, 16> RVLocs1;
2382 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2383 RVLocs1, *DAG.getContext());
2384 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2385
2386 SmallVector<CCValAssign, 16> RVLocs2;
2387 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2388 RVLocs2, *DAG.getContext());
2389 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2390
2391 if (RVLocs1.size() != RVLocs2.size())
2392 return false;
2393 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2394 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2395 return false;
2396 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2397 return false;
2398 if (RVLocs1[i].isRegLoc()) {
2399 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2400 return false;
2401 } else {
2402 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2403 return false;
2404 }
2405 }
2406 }
2407
Evan Chenga6bff982010-01-30 01:22:00 +00002408 // If the callee takes no arguments then go on to check the results of the
2409 // call.
2410 if (!Outs.empty()) {
2411 // Check if stack adjustment is needed. For now, do not do this if any
2412 // argument is passed on the stack.
2413 SmallVector<CCValAssign, 16> ArgLocs;
2414 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2415 ArgLocs, *DAG.getContext());
2416 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002417 if (CCInfo.getNextStackOffset()) {
2418 MachineFunction &MF = DAG.getMachineFunction();
2419 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2420 return false;
2421 if (Subtarget->isTargetWin64())
2422 // Win64 ABI has additional complications.
2423 return false;
2424
2425 // Check if the arguments are already laid out in the right way as
2426 // the caller's fixed stack objects.
2427 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002428 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2429 const X86InstrInfo *TII =
2430 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002431 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2432 CCValAssign &VA = ArgLocs[i];
2433 EVT RegVT = VA.getLocVT();
2434 SDValue Arg = Outs[i].Val;
2435 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002436 if (VA.getLocInfo() == CCValAssign::Indirect)
2437 return false;
2438 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002439 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2440 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002441 return false;
2442 }
2443 }
2444 }
Evan Chenga6bff982010-01-30 01:22:00 +00002445 }
Evan Chengb1712452010-01-27 06:25:16 +00002446
Evan Cheng86809cc2010-02-03 03:28:02 +00002447 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002448}
2449
Dan Gohman3df24e62008-09-03 23:12:08 +00002450FastISel *
Chris Lattnered3a8062010-04-05 06:05:26 +00002451X86TargetLowering::createFastISel(MachineFunction &mf,
Evan Chengddc419c2010-01-26 19:04:47 +00002452 DenseMap<const Value *, unsigned> &vm,
2453 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00002454 DenseMap<const AllocaInst *, int> &am,
2455 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002456#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00002457 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002458#endif
Dan Gohmand858e902010-04-17 15:26:15 +00002459 ) const {
Dan Gohmanf81eca02010-04-22 20:46:50 +00002460 return X86::createFastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002461#ifndef NDEBUG
2462 , cil
2463#endif
2464 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002465}
2466
2467
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002468//===----------------------------------------------------------------------===//
2469// Other Lowering Hooks
2470//===----------------------------------------------------------------------===//
2471
2472
Dan Gohmand858e902010-04-17 15:26:15 +00002473SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002474 MachineFunction &MF = DAG.getMachineFunction();
2475 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2476 int ReturnAddrIndex = FuncInfo->getRAIndex();
2477
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002478 if (ReturnAddrIndex == 0) {
2479 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002480 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002481 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002482 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002483 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002484 }
2485
Evan Cheng25ab6902006-09-08 06:48:29 +00002486 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002487}
2488
2489
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002490bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2491 bool hasSymbolicDisplacement) {
2492 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002493 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002494 return false;
2495
2496 // If we don't have a symbolic displacement - we don't have any extra
2497 // restrictions.
2498 if (!hasSymbolicDisplacement)
2499 return true;
2500
2501 // FIXME: Some tweaks might be needed for medium code model.
2502 if (M != CodeModel::Small && M != CodeModel::Kernel)
2503 return false;
2504
2505 // For small code model we assume that latest object is 16MB before end of 31
2506 // bits boundary. We may also accept pretty large negative constants knowing
2507 // that all objects are in the positive half of address space.
2508 if (M == CodeModel::Small && Offset < 16*1024*1024)
2509 return true;
2510
2511 // For kernel code model we know that all object resist in the negative half
2512 // of 32bits address space. We may not accept negative offsets, since they may
2513 // be just off and we may accept pretty large positive ones.
2514 if (M == CodeModel::Kernel && Offset > 0)
2515 return true;
2516
2517 return false;
2518}
2519
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002520/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2521/// specific condition code, returning the condition code and the LHS/RHS of the
2522/// comparison to make.
2523static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2524 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002525 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002526 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2527 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2528 // X > -1 -> X == 0, jump !sign.
2529 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002530 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002531 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2532 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002533 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002534 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002535 // X < 1 -> X <= 0
2536 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002537 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002538 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002539 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002540
Evan Chengd9558e02006-01-06 00:43:03 +00002541 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002542 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002543 case ISD::SETEQ: return X86::COND_E;
2544 case ISD::SETGT: return X86::COND_G;
2545 case ISD::SETGE: return X86::COND_GE;
2546 case ISD::SETLT: return X86::COND_L;
2547 case ISD::SETLE: return X86::COND_LE;
2548 case ISD::SETNE: return X86::COND_NE;
2549 case ISD::SETULT: return X86::COND_B;
2550 case ISD::SETUGT: return X86::COND_A;
2551 case ISD::SETULE: return X86::COND_BE;
2552 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002553 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002554 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002555
Chris Lattner4c78e022008-12-23 23:42:27 +00002556 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002557
Chris Lattner4c78e022008-12-23 23:42:27 +00002558 // If LHS is a foldable load, but RHS is not, flip the condition.
2559 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2560 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2561 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2562 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002563 }
2564
Chris Lattner4c78e022008-12-23 23:42:27 +00002565 switch (SetCCOpcode) {
2566 default: break;
2567 case ISD::SETOLT:
2568 case ISD::SETOLE:
2569 case ISD::SETUGT:
2570 case ISD::SETUGE:
2571 std::swap(LHS, RHS);
2572 break;
2573 }
2574
2575 // On a floating point condition, the flags are set as follows:
2576 // ZF PF CF op
2577 // 0 | 0 | 0 | X > Y
2578 // 0 | 0 | 1 | X < Y
2579 // 1 | 0 | 0 | X == Y
2580 // 1 | 1 | 1 | unordered
2581 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002582 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002583 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002584 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002585 case ISD::SETOLT: // flipped
2586 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002587 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002588 case ISD::SETOLE: // flipped
2589 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002590 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002591 case ISD::SETUGT: // flipped
2592 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002593 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002594 case ISD::SETUGE: // flipped
2595 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002596 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002597 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002598 case ISD::SETNE: return X86::COND_NE;
2599 case ISD::SETUO: return X86::COND_P;
2600 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002601 case ISD::SETOEQ:
2602 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002603 }
Evan Chengd9558e02006-01-06 00:43:03 +00002604}
2605
Evan Cheng4a460802006-01-11 00:33:36 +00002606/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2607/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002608/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002609static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002610 switch (X86CC) {
2611 default:
2612 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002613 case X86::COND_B:
2614 case X86::COND_BE:
2615 case X86::COND_E:
2616 case X86::COND_P:
2617 case X86::COND_A:
2618 case X86::COND_AE:
2619 case X86::COND_NE:
2620 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002621 return true;
2622 }
2623}
2624
Evan Chengeb2f9692009-10-27 19:56:55 +00002625/// isFPImmLegal - Returns true if the target can instruction select the
2626/// specified FP immediate natively. If false, the legalizer will
2627/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002628bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002629 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2630 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2631 return true;
2632 }
2633 return false;
2634}
2635
Nate Begeman9008ca62009-04-27 18:41:29 +00002636/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2637/// the specified range (L, H].
2638static bool isUndefOrInRange(int Val, int Low, int Hi) {
2639 return (Val < 0) || (Val >= Low && Val < Hi);
2640}
2641
2642/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2643/// specified value.
2644static bool isUndefOrEqual(int Val, int CmpVal) {
2645 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002646 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002647 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002648}
2649
Nate Begeman9008ca62009-04-27 18:41:29 +00002650/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2651/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2652/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002653static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002654 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002655 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002656 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002657 return (Mask[0] < 2 && Mask[1] < 2);
2658 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002659}
2660
Nate Begeman9008ca62009-04-27 18:41:29 +00002661bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002662 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002663 N->getMask(M);
2664 return ::isPSHUFDMask(M, N->getValueType(0));
2665}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002666
Nate Begeman9008ca62009-04-27 18:41:29 +00002667/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2668/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002669static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002671 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002672
Nate Begeman9008ca62009-04-27 18:41:29 +00002673 // Lower quadword copied in order or undef.
2674 for (int i = 0; i != 4; ++i)
2675 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002676 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002677
Evan Cheng506d3df2006-03-29 23:07:14 +00002678 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002679 for (int i = 4; i != 8; ++i)
2680 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002681 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002682
Evan Cheng506d3df2006-03-29 23:07:14 +00002683 return true;
2684}
2685
Nate Begeman9008ca62009-04-27 18:41:29 +00002686bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002687 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002688 N->getMask(M);
2689 return ::isPSHUFHWMask(M, N->getValueType(0));
2690}
Evan Cheng506d3df2006-03-29 23:07:14 +00002691
Nate Begeman9008ca62009-04-27 18:41:29 +00002692/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2693/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002694static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002695 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002696 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002697
Rafael Espindola15684b22009-04-24 12:40:33 +00002698 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002699 for (int i = 4; i != 8; ++i)
2700 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002701 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002702
Rafael Espindola15684b22009-04-24 12:40:33 +00002703 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002704 for (int i = 0; i != 4; ++i)
2705 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002706 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002707
Rafael Espindola15684b22009-04-24 12:40:33 +00002708 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002709}
2710
Nate Begeman9008ca62009-04-27 18:41:29 +00002711bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002712 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002713 N->getMask(M);
2714 return ::isPSHUFLWMask(M, N->getValueType(0));
2715}
2716
Nate Begemana09008b2009-10-19 02:17:23 +00002717/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2718/// is suitable for input to PALIGNR.
2719static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2720 bool hasSSSE3) {
2721 int i, e = VT.getVectorNumElements();
2722
2723 // Do not handle v2i64 / v2f64 shuffles with palignr.
2724 if (e < 4 || !hasSSSE3)
2725 return false;
2726
2727 for (i = 0; i != e; ++i)
2728 if (Mask[i] >= 0)
2729 break;
2730
2731 // All undef, not a palignr.
2732 if (i == e)
2733 return false;
2734
2735 // Determine if it's ok to perform a palignr with only the LHS, since we
2736 // don't have access to the actual shuffle elements to see if RHS is undef.
2737 bool Unary = Mask[i] < (int)e;
2738 bool NeedsUnary = false;
2739
2740 int s = Mask[i] - i;
2741
2742 // Check the rest of the elements to see if they are consecutive.
2743 for (++i; i != e; ++i) {
2744 int m = Mask[i];
2745 if (m < 0)
2746 continue;
2747
2748 Unary = Unary && (m < (int)e);
2749 NeedsUnary = NeedsUnary || (m < s);
2750
2751 if (NeedsUnary && !Unary)
2752 return false;
2753 if (Unary && m != ((s+i) & (e-1)))
2754 return false;
2755 if (!Unary && m != (s+i))
2756 return false;
2757 }
2758 return true;
2759}
2760
2761bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2762 SmallVector<int, 8> M;
2763 N->getMask(M);
2764 return ::isPALIGNRMask(M, N->getValueType(0), true);
2765}
2766
Evan Cheng14aed5e2006-03-24 01:18:28 +00002767/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2768/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002769static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002770 int NumElems = VT.getVectorNumElements();
2771 if (NumElems != 2 && NumElems != 4)
2772 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002773
Nate Begeman9008ca62009-04-27 18:41:29 +00002774 int Half = NumElems / 2;
2775 for (int i = 0; i < Half; ++i)
2776 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002777 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002778 for (int i = Half; i < NumElems; ++i)
2779 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002780 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002781
Evan Cheng14aed5e2006-03-24 01:18:28 +00002782 return true;
2783}
2784
Nate Begeman9008ca62009-04-27 18:41:29 +00002785bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2786 SmallVector<int, 8> M;
2787 N->getMask(M);
2788 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002789}
2790
Evan Cheng213d2cf2007-05-17 18:45:50 +00002791/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002792/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2793/// half elements to come from vector 1 (which would equal the dest.) and
2794/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002795static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002796 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002797
2798 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002799 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002800
Nate Begeman9008ca62009-04-27 18:41:29 +00002801 int Half = NumElems / 2;
2802 for (int i = 0; i < Half; ++i)
2803 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002804 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002805 for (int i = Half; i < NumElems; ++i)
2806 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002807 return false;
2808 return true;
2809}
2810
Nate Begeman9008ca62009-04-27 18:41:29 +00002811static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2812 SmallVector<int, 8> M;
2813 N->getMask(M);
2814 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002815}
2816
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002817/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2818/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002819bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2820 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002821 return false;
2822
Evan Cheng2064a2b2006-03-28 06:50:32 +00002823 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002824 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2825 isUndefOrEqual(N->getMaskElt(1), 7) &&
2826 isUndefOrEqual(N->getMaskElt(2), 2) &&
2827 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002828}
2829
Nate Begeman0b10b912009-11-07 23:17:15 +00002830/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2831/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2832/// <2, 3, 2, 3>
2833bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2834 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2835
2836 if (NumElems != 4)
2837 return false;
2838
2839 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2840 isUndefOrEqual(N->getMaskElt(1), 3) &&
2841 isUndefOrEqual(N->getMaskElt(2), 2) &&
2842 isUndefOrEqual(N->getMaskElt(3), 3);
2843}
2844
Evan Cheng5ced1d82006-04-06 23:23:56 +00002845/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2846/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002847bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2848 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002849
Evan Cheng5ced1d82006-04-06 23:23:56 +00002850 if (NumElems != 2 && NumElems != 4)
2851 return false;
2852
Evan Chengc5cdff22006-04-07 21:53:05 +00002853 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002854 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002855 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002856
Evan Chengc5cdff22006-04-07 21:53:05 +00002857 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002858 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002859 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002860
2861 return true;
2862}
2863
Nate Begeman0b10b912009-11-07 23:17:15 +00002864/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2865/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2866bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002867 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002868
Evan Cheng5ced1d82006-04-06 23:23:56 +00002869 if (NumElems != 2 && NumElems != 4)
2870 return false;
2871
Evan Chengc5cdff22006-04-07 21:53:05 +00002872 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002873 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002874 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002875
Nate Begeman9008ca62009-04-27 18:41:29 +00002876 for (unsigned i = 0; i < NumElems/2; ++i)
2877 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002878 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002879
2880 return true;
2881}
2882
Evan Cheng0038e592006-03-28 00:39:58 +00002883/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2884/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002885static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002886 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002887 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002888 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002889 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002890
Nate Begeman9008ca62009-04-27 18:41:29 +00002891 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2892 int BitI = Mask[i];
2893 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002894 if (!isUndefOrEqual(BitI, j))
2895 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002896 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002897 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002898 return false;
2899 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002900 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002901 return false;
2902 }
Evan Cheng0038e592006-03-28 00:39:58 +00002903 }
Evan Cheng0038e592006-03-28 00:39:58 +00002904 return true;
2905}
2906
Nate Begeman9008ca62009-04-27 18:41:29 +00002907bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2908 SmallVector<int, 8> M;
2909 N->getMask(M);
2910 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002911}
2912
Evan Cheng4fcb9222006-03-28 02:43:26 +00002913/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2914/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002915static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002916 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002917 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002918 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002919 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002920
Nate Begeman9008ca62009-04-27 18:41:29 +00002921 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2922 int BitI = Mask[i];
2923 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002924 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002925 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002926 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002927 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002928 return false;
2929 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002930 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002931 return false;
2932 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002933 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002934 return true;
2935}
2936
Nate Begeman9008ca62009-04-27 18:41:29 +00002937bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2938 SmallVector<int, 8> M;
2939 N->getMask(M);
2940 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002941}
2942
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002943/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2944/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2945/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002946static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002947 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002948 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002949 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002950
Nate Begeman9008ca62009-04-27 18:41:29 +00002951 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2952 int BitI = Mask[i];
2953 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002954 if (!isUndefOrEqual(BitI, j))
2955 return false;
2956 if (!isUndefOrEqual(BitI1, j))
2957 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002958 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002959 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002960}
2961
Nate Begeman9008ca62009-04-27 18:41:29 +00002962bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2963 SmallVector<int, 8> M;
2964 N->getMask(M);
2965 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2966}
2967
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002968/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2969/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2970/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002971static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002972 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002973 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2974 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002975
Nate Begeman9008ca62009-04-27 18:41:29 +00002976 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2977 int BitI = Mask[i];
2978 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002979 if (!isUndefOrEqual(BitI, j))
2980 return false;
2981 if (!isUndefOrEqual(BitI1, j))
2982 return false;
2983 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002984 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002985}
2986
Nate Begeman9008ca62009-04-27 18:41:29 +00002987bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2988 SmallVector<int, 8> M;
2989 N->getMask(M);
2990 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2991}
2992
Evan Cheng017dcc62006-04-21 01:05:10 +00002993/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2994/// specifies a shuffle of elements that is suitable for input to MOVSS,
2995/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002996static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002997 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002998 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002999
3000 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003001
Nate Begeman9008ca62009-04-27 18:41:29 +00003002 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003003 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003004
Nate Begeman9008ca62009-04-27 18:41:29 +00003005 for (int i = 1; i < NumElts; ++i)
3006 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003007 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003008
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003009 return true;
3010}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003011
Nate Begeman9008ca62009-04-27 18:41:29 +00003012bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3013 SmallVector<int, 8> M;
3014 N->getMask(M);
3015 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003016}
3017
Evan Cheng017dcc62006-04-21 01:05:10 +00003018/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3019/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003020/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003021static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003022 bool V2IsSplat = false, bool V2IsUndef = false) {
3023 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003024 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003025 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003026
Nate Begeman9008ca62009-04-27 18:41:29 +00003027 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003028 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003029
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 for (int i = 1; i < NumOps; ++i)
3031 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3032 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3033 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003034 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003035
Evan Cheng39623da2006-04-20 08:58:49 +00003036 return true;
3037}
3038
Nate Begeman9008ca62009-04-27 18:41:29 +00003039static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003040 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003041 SmallVector<int, 8> M;
3042 N->getMask(M);
3043 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003044}
3045
Evan Chengd9539472006-04-14 21:59:03 +00003046/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3047/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003048bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3049 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003050 return false;
3051
3052 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003053 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003054 int Elt = N->getMaskElt(i);
3055 if (Elt >= 0 && Elt != 1)
3056 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003057 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003058
3059 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003060 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003061 int Elt = N->getMaskElt(i);
3062 if (Elt >= 0 && Elt != 3)
3063 return false;
3064 if (Elt == 3)
3065 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003066 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003067 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003068 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003069 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003070}
3071
3072/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3073/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003074bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3075 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003076 return false;
3077
3078 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003079 for (unsigned i = 0; i < 2; ++i)
3080 if (N->getMaskElt(i) > 0)
3081 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003082
3083 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003084 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003085 int Elt = N->getMaskElt(i);
3086 if (Elt >= 0 && Elt != 2)
3087 return false;
3088 if (Elt == 2)
3089 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003090 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003091 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003092 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003093}
3094
Evan Cheng0b457f02008-09-25 20:50:48 +00003095/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3096/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003097bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3098 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003099
Nate Begeman9008ca62009-04-27 18:41:29 +00003100 for (int i = 0; i < e; ++i)
3101 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003102 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003103 for (int i = 0; i < e; ++i)
3104 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003105 return false;
3106 return true;
3107}
3108
Evan Cheng63d33002006-03-22 08:01:21 +00003109/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003110/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003111unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003112 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3113 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3114
Evan Chengb9df0ca2006-03-22 02:53:00 +00003115 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3116 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003117 for (int i = 0; i < NumOperands; ++i) {
3118 int Val = SVOp->getMaskElt(NumOperands-i-1);
3119 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003120 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003121 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003122 if (i != NumOperands - 1)
3123 Mask <<= Shift;
3124 }
Evan Cheng63d33002006-03-22 08:01:21 +00003125 return Mask;
3126}
3127
Evan Cheng506d3df2006-03-29 23:07:14 +00003128/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003129/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003130unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003131 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003132 unsigned Mask = 0;
3133 // 8 nodes, but we only care about the last 4.
3134 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003135 int Val = SVOp->getMaskElt(i);
3136 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003137 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003138 if (i != 4)
3139 Mask <<= 2;
3140 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003141 return Mask;
3142}
3143
3144/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003145/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003146unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003147 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003148 unsigned Mask = 0;
3149 // 8 nodes, but we only care about the first 4.
3150 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003151 int Val = SVOp->getMaskElt(i);
3152 if (Val >= 0)
3153 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003154 if (i != 0)
3155 Mask <<= 2;
3156 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003157 return Mask;
3158}
3159
Nate Begemana09008b2009-10-19 02:17:23 +00003160/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3161/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3162unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3163 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3164 EVT VVT = N->getValueType(0);
3165 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3166 int Val = 0;
3167
3168 unsigned i, e;
3169 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3170 Val = SVOp->getMaskElt(i);
3171 if (Val >= 0)
3172 break;
3173 }
3174 return (Val - i) * EltSize;
3175}
3176
Evan Cheng37b73872009-07-30 08:33:02 +00003177/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3178/// constant +0.0.
3179bool X86::isZeroNode(SDValue Elt) {
3180 return ((isa<ConstantSDNode>(Elt) &&
3181 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3182 (isa<ConstantFPSDNode>(Elt) &&
3183 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3184}
3185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3187/// their permute mask.
3188static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3189 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003190 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003191 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003192 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003193
Nate Begeman5a5ca152009-04-29 05:20:52 +00003194 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 int idx = SVOp->getMaskElt(i);
3196 if (idx < 0)
3197 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003198 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003199 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003200 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003201 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003202 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003203 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3204 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003205}
3206
Evan Cheng779ccea2007-12-07 21:30:01 +00003207/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3208/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003209static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003210 unsigned NumElems = VT.getVectorNumElements();
3211 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003212 int idx = Mask[i];
3213 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003214 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003215 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003216 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003217 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003218 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003219 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003220}
3221
Evan Cheng533a0aa2006-04-19 20:35:22 +00003222/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3223/// match movhlps. The lower half elements should come from upper half of
3224/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003225/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003226static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3227 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003228 return false;
3229 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003230 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003231 return false;
3232 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003234 return false;
3235 return true;
3236}
3237
Evan Cheng5ced1d82006-04-06 23:23:56 +00003238/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003239/// is promoted to a vector. It also returns the LoadSDNode by reference if
3240/// required.
3241static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003242 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3243 return false;
3244 N = N->getOperand(0).getNode();
3245 if (!ISD::isNON_EXTLoad(N))
3246 return false;
3247 if (LD)
3248 *LD = cast<LoadSDNode>(N);
3249 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003250}
3251
Evan Cheng533a0aa2006-04-19 20:35:22 +00003252/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3253/// match movlp{s|d}. The lower half elements should come from lower half of
3254/// V1 (and in order), and the upper half elements should come from the upper
3255/// half of V2 (and in order). And since V1 will become the source of the
3256/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003257static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3258 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003259 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003260 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003261 // Is V2 is a vector load, don't do this transformation. We will try to use
3262 // load folding shufps op.
3263 if (ISD::isNON_EXTLoad(V2))
3264 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003265
Nate Begeman5a5ca152009-04-29 05:20:52 +00003266 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003267
Evan Cheng533a0aa2006-04-19 20:35:22 +00003268 if (NumElems != 2 && NumElems != 4)
3269 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003270 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003271 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003272 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003273 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003274 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003275 return false;
3276 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003277}
3278
Evan Cheng39623da2006-04-20 08:58:49 +00003279/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3280/// all the same.
3281static bool isSplatVector(SDNode *N) {
3282 if (N->getOpcode() != ISD::BUILD_VECTOR)
3283 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003284
Dan Gohman475871a2008-07-27 21:46:04 +00003285 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003286 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3287 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003288 return false;
3289 return true;
3290}
3291
Evan Cheng213d2cf2007-05-17 18:45:50 +00003292/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003293/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003294/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003295static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003296 SDValue V1 = N->getOperand(0);
3297 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003298 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3299 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003300 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003301 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003302 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003303 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3304 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003305 if (Opc != ISD::BUILD_VECTOR ||
3306 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003307 return false;
3308 } else if (Idx >= 0) {
3309 unsigned Opc = V1.getOpcode();
3310 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3311 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003312 if (Opc != ISD::BUILD_VECTOR ||
3313 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003314 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003315 }
3316 }
3317 return true;
3318}
3319
3320/// getZeroVector - Returns a vector of specified type with all zero elements.
3321///
Owen Andersone50ed302009-08-10 22:56:29 +00003322static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003323 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003324 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003325
Chris Lattner8a594482007-11-25 00:24:49 +00003326 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3327 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003328 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003329 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3331 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003332 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003333 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3334 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003335 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3337 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003338 }
Dale Johannesenace16102009-02-03 19:33:06 +00003339 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003340}
3341
Chris Lattner8a594482007-11-25 00:24:49 +00003342/// getOnesVector - Returns a vector of specified type with all bits set.
3343///
Owen Andersone50ed302009-08-10 22:56:29 +00003344static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003345 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003346
Chris Lattner8a594482007-11-25 00:24:49 +00003347 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3348 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003349 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003350 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003351 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003353 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003354 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003355 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003356}
3357
3358
Evan Cheng39623da2006-04-20 08:58:49 +00003359/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3360/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003361static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003362 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003363 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003364
Evan Cheng39623da2006-04-20 08:58:49 +00003365 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 SmallVector<int, 8> MaskVec;
3367 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003368
Nate Begeman5a5ca152009-04-29 05:20:52 +00003369 for (unsigned i = 0; i != NumElems; ++i) {
3370 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003371 MaskVec[i] = NumElems;
3372 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003373 }
Evan Cheng39623da2006-04-20 08:58:49 +00003374 }
Evan Cheng39623da2006-04-20 08:58:49 +00003375 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003376 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3377 SVOp->getOperand(1), &MaskVec[0]);
3378 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003379}
3380
Evan Cheng017dcc62006-04-21 01:05:10 +00003381/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3382/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003383static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 SDValue V2) {
3385 unsigned NumElems = VT.getVectorNumElements();
3386 SmallVector<int, 8> Mask;
3387 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003388 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003389 Mask.push_back(i);
3390 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003391}
3392
Nate Begeman9008ca62009-04-27 18:41:29 +00003393/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003394static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003395 SDValue V2) {
3396 unsigned NumElems = VT.getVectorNumElements();
3397 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003398 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003399 Mask.push_back(i);
3400 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003401 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003402 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003403}
3404
Nate Begeman9008ca62009-04-27 18:41:29 +00003405/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003406static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003407 SDValue V2) {
3408 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003409 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003410 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003411 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 Mask.push_back(i + Half);
3413 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003414 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003416}
3417
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003418/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003419static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003420 bool HasSSE2) {
3421 if (SV->getValueType(0).getVectorNumElements() <= 4)
3422 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003423
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003425 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003426 DebugLoc dl = SV->getDebugLoc();
3427 SDValue V1 = SV->getOperand(0);
3428 int NumElems = VT.getVectorNumElements();
3429 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003430
Nate Begeman9008ca62009-04-27 18:41:29 +00003431 // unpack elements to the correct location
3432 while (NumElems > 4) {
3433 if (EltNo < NumElems/2) {
3434 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3435 } else {
3436 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3437 EltNo -= NumElems/2;
3438 }
3439 NumElems >>= 1;
3440 }
Eric Christopherfd179292009-08-27 18:07:15 +00003441
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 // Perform the splat.
3443 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003444 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3446 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003447}
3448
Evan Chengba05f722006-04-21 23:03:30 +00003449/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003450/// vector of zero or undef vector. This produces a shuffle where the low
3451/// element of V2 is swizzled into the zero/undef vector, landing at element
3452/// 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 +00003453static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003454 bool isZero, bool HasSSE2,
3455 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003456 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003457 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003458 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3459 unsigned NumElems = VT.getVectorNumElements();
3460 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003461 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003462 // If this is the insertion idx, put the low elt of V2 here.
3463 MaskVec.push_back(i == Idx ? NumElems : i);
3464 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003465}
3466
Evan Chengf26ffe92008-05-29 08:22:04 +00003467/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3468/// a shuffle that is zero.
3469static
Nate Begeman9008ca62009-04-27 18:41:29 +00003470unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3471 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003472 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003473 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003474 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 int Idx = SVOp->getMaskElt(Index);
3476 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003477 ++NumZeros;
3478 continue;
3479 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003480 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003481 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003482 ++NumZeros;
3483 else
3484 break;
3485 }
3486 return NumZeros;
3487}
3488
3489/// isVectorShift - Returns true if the shuffle can be implemented as a
3490/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003491/// FIXME: split into pslldqi, psrldqi, palignr variants.
3492static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003493 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003494 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003495
3496 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003497 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003498 if (!NumZeros) {
3499 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003500 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003501 if (!NumZeros)
3502 return false;
3503 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003504 bool SeenV1 = false;
3505 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003506 for (unsigned i = NumZeros; i < NumElems; ++i) {
3507 unsigned Val = isLeft ? (i - NumZeros) : i;
3508 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3509 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003510 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003511 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003512 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003513 SeenV1 = true;
3514 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003515 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003516 SeenV2 = true;
3517 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003518 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003519 return false;
3520 }
3521 if (SeenV1 && SeenV2)
3522 return false;
3523
Nate Begeman9008ca62009-04-27 18:41:29 +00003524 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003525 ShAmt = NumZeros;
3526 return true;
3527}
3528
3529
Evan Chengc78d3b42006-04-24 18:01:45 +00003530/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3531///
Dan Gohman475871a2008-07-27 21:46:04 +00003532static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003533 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003534 SelectionDAG &DAG,
3535 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003536 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003537 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003538
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003539 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003540 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003541 bool First = true;
3542 for (unsigned i = 0; i < 16; ++i) {
3543 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3544 if (ThisIsNonZero && First) {
3545 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003547 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003549 First = false;
3550 }
3551
3552 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003553 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003554 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3555 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003556 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003558 }
3559 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3561 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3562 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003563 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003565 } else
3566 ThisElt = LastElt;
3567
Gabor Greifba36cb52008-08-28 21:40:38 +00003568 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003570 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003571 }
3572 }
3573
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003575}
3576
Bill Wendlinga348c562007-03-22 18:42:45 +00003577/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003578///
Dan Gohman475871a2008-07-27 21:46:04 +00003579static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003580 unsigned NumNonZero, unsigned NumZero,
3581 SelectionDAG &DAG,
3582 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003583 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003584 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003585
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003586 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003587 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003588 bool First = true;
3589 for (unsigned i = 0; i < 8; ++i) {
3590 bool isNonZero = (NonZeros & (1 << i)) != 0;
3591 if (isNonZero) {
3592 if (First) {
3593 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003595 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003596 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003597 First = false;
3598 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003599 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003601 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003602 }
3603 }
3604
3605 return V;
3606}
3607
Evan Chengf26ffe92008-05-29 08:22:04 +00003608/// getVShift - Return a vector logical shift node.
3609///
Owen Andersone50ed302009-08-10 22:56:29 +00003610static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 unsigned NumBits, SelectionDAG &DAG,
3612 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003613 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003615 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003616 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3617 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3618 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003619 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003620}
3621
Dan Gohman475871a2008-07-27 21:46:04 +00003622SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003623X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003624 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003625
3626 // Check if the scalar load can be widened into a vector load. And if
3627 // the address is "base + cst" see if the cst can be "absorbed" into
3628 // the shuffle mask.
3629 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3630 SDValue Ptr = LD->getBasePtr();
3631 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3632 return SDValue();
3633 EVT PVT = LD->getValueType(0);
3634 if (PVT != MVT::i32 && PVT != MVT::f32)
3635 return SDValue();
3636
3637 int FI = -1;
3638 int64_t Offset = 0;
3639 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3640 FI = FINode->getIndex();
3641 Offset = 0;
3642 } else if (Ptr.getOpcode() == ISD::ADD &&
3643 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3644 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3645 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3646 Offset = Ptr.getConstantOperandVal(1);
3647 Ptr = Ptr.getOperand(0);
3648 } else {
3649 return SDValue();
3650 }
3651
3652 SDValue Chain = LD->getChain();
3653 // Make sure the stack object alignment is at least 16.
3654 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3655 if (DAG.InferPtrAlignment(Ptr) < 16) {
3656 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003657 // Can't change the alignment. FIXME: It's possible to compute
3658 // the exact stack offset and reference FI + adjust offset instead.
3659 // If someone *really* cares about this. That's the way to implement it.
3660 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003661 } else {
3662 MFI->setObjectAlignment(FI, 16);
3663 }
3664 }
3665
3666 // (Offset % 16) must be multiple of 4. Then address is then
3667 // Ptr + (Offset & ~15).
3668 if (Offset < 0)
3669 return SDValue();
3670 if ((Offset % 16) & 3)
3671 return SDValue();
3672 int64_t StartOffset = Offset & ~15;
3673 if (StartOffset)
3674 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3675 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3676
3677 int EltNo = (Offset - StartOffset) >> 2;
3678 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3679 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003680 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3681 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003682 // Canonicalize it to a v4i32 shuffle.
3683 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3684 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3685 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3686 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3687 }
3688
3689 return SDValue();
3690}
3691
Nate Begeman1449f292010-03-24 22:19:06 +00003692/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3693/// vector of type 'VT', see if the elements can be replaced by a single large
3694/// load which has the same value as a build_vector whose operands are 'elts'.
3695///
3696/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3697///
3698/// FIXME: we'd also like to handle the case where the last elements are zero
3699/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3700/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003701static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3702 DebugLoc &dl, SelectionDAG &DAG) {
3703 EVT EltVT = VT.getVectorElementType();
3704 unsigned NumElems = Elts.size();
3705
Nate Begemanfdea31a2010-03-24 20:49:50 +00003706 LoadSDNode *LDBase = NULL;
3707 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003708
3709 // For each element in the initializer, see if we've found a load or an undef.
3710 // If we don't find an initial load element, or later load elements are
3711 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003712 for (unsigned i = 0; i < NumElems; ++i) {
3713 SDValue Elt = Elts[i];
3714
3715 if (!Elt.getNode() ||
3716 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3717 return SDValue();
3718 if (!LDBase) {
3719 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3720 return SDValue();
3721 LDBase = cast<LoadSDNode>(Elt.getNode());
3722 LastLoadedElt = i;
3723 continue;
3724 }
3725 if (Elt.getOpcode() == ISD::UNDEF)
3726 continue;
3727
3728 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3729 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3730 return SDValue();
3731 LastLoadedElt = i;
3732 }
Nate Begeman1449f292010-03-24 22:19:06 +00003733
3734 // If we have found an entire vector of loads and undefs, then return a large
3735 // load of the entire vector width starting at the base pointer. If we found
3736 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003737 if (LastLoadedElt == NumElems - 1) {
3738 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3739 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3740 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3741 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3742 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3743 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3744 LDBase->isVolatile(), LDBase->isNonTemporal(),
3745 LDBase->getAlignment());
3746 } else if (NumElems == 4 && LastLoadedElt == 1) {
3747 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3748 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3749 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3750 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3751 }
3752 return SDValue();
3753}
3754
Evan Chengc3630942009-12-09 21:00:30 +00003755SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003756X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003757 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003758 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003759 if (ISD::isBuildVectorAllZeros(Op.getNode())
3760 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003761 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3762 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3763 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003764 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003765 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003766
Gabor Greifba36cb52008-08-28 21:40:38 +00003767 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003768 return getOnesVector(Op.getValueType(), DAG, dl);
3769 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003770 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003771
Owen Andersone50ed302009-08-10 22:56:29 +00003772 EVT VT = Op.getValueType();
3773 EVT ExtVT = VT.getVectorElementType();
3774 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003775
3776 unsigned NumElems = Op.getNumOperands();
3777 unsigned NumZero = 0;
3778 unsigned NumNonZero = 0;
3779 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003780 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003781 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003782 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003783 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003784 if (Elt.getOpcode() == ISD::UNDEF)
3785 continue;
3786 Values.insert(Elt);
3787 if (Elt.getOpcode() != ISD::Constant &&
3788 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003789 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003790 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003791 NumZero++;
3792 else {
3793 NonZeros |= (1 << i);
3794 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003795 }
3796 }
3797
Dan Gohman7f321562007-06-25 16:23:39 +00003798 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003799 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003800 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003801 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003802
Chris Lattner67f453a2008-03-09 05:42:06 +00003803 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003804 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003805 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003806 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003807
Chris Lattner62098042008-03-09 01:05:04 +00003808 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3809 // the value are obviously zero, truncate the value to i32 and do the
3810 // insertion that way. Only do this if the value is non-constant or if the
3811 // value is a constant being inserted into element 0. It is cheaper to do
3812 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003814 (!IsAllConstants || Idx == 0)) {
3815 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3816 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003817 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3818 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003819
Chris Lattner62098042008-03-09 01:05:04 +00003820 // Truncate the value (which may itself be a constant) to i32, and
3821 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003822 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003823 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003824 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3825 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003826
Chris Lattner62098042008-03-09 01:05:04 +00003827 // Now we have our 32-bit value zero extended in the low element of
3828 // a vector. If Idx != 0, swizzle it into place.
3829 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003830 SmallVector<int, 4> Mask;
3831 Mask.push_back(Idx);
3832 for (unsigned i = 1; i != VecElts; ++i)
3833 Mask.push_back(i);
3834 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003835 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003836 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003837 }
Dale Johannesenace16102009-02-03 19:33:06 +00003838 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003839 }
3840 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003841
Chris Lattner19f79692008-03-08 22:59:52 +00003842 // If we have a constant or non-constant insertion into the low element of
3843 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3844 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003845 // depending on what the source datatype is.
3846 if (Idx == 0) {
3847 if (NumZero == 0) {
3848 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003849 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3850 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003851 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3852 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3853 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3854 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003855 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3856 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3857 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003858 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3859 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3860 Subtarget->hasSSE2(), DAG);
3861 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3862 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003863 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003864
3865 // Is it a vector logical left shift?
3866 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003867 X86::isZeroNode(Op.getOperand(0)) &&
3868 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003869 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003870 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003871 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003872 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003873 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003874 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003875
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003876 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003877 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003878
Chris Lattner19f79692008-03-08 22:59:52 +00003879 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3880 // is a non-constant being inserted into an element other than the low one,
3881 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3882 // movd/movss) to move this into the low element, then shuffle it into
3883 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003884 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003885 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003886
Evan Cheng0db9fe62006-04-25 20:13:52 +00003887 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003888 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3889 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003890 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003891 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 MaskVec.push_back(i == Idx ? 0 : 1);
3893 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003894 }
3895 }
3896
Chris Lattner67f453a2008-03-09 05:42:06 +00003897 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003898 if (Values.size() == 1) {
3899 if (EVTBits == 32) {
3900 // Instead of a shuffle like this:
3901 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3902 // Check if it's possible to issue this instead.
3903 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3904 unsigned Idx = CountTrailingZeros_32(NonZeros);
3905 SDValue Item = Op.getOperand(Idx);
3906 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3907 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3908 }
Dan Gohman475871a2008-07-27 21:46:04 +00003909 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003910 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003911
Dan Gohmana3941172007-07-24 22:55:08 +00003912 // A vector full of immediates; various special cases are already
3913 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003914 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003915 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003916
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003917 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003918 if (EVTBits == 64) {
3919 if (NumNonZero == 1) {
3920 // One half is zero or undef.
3921 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003922 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003923 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003924 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3925 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003926 }
Dan Gohman475871a2008-07-27 21:46:04 +00003927 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003928 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003929
3930 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003931 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003932 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003933 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003934 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003935 }
3936
Bill Wendling826f36f2007-03-28 00:57:11 +00003937 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003938 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003939 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003940 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003941 }
3942
3943 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003944 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003945 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003946 if (NumElems == 4 && NumZero > 0) {
3947 for (unsigned i = 0; i < 4; ++i) {
3948 bool isZero = !(NonZeros & (1 << i));
3949 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003950 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003951 else
Dale Johannesenace16102009-02-03 19:33:06 +00003952 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003953 }
3954
3955 for (unsigned i = 0; i < 2; ++i) {
3956 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3957 default: break;
3958 case 0:
3959 V[i] = V[i*2]; // Must be a zero vector.
3960 break;
3961 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003962 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003963 break;
3964 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003965 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003966 break;
3967 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003969 break;
3970 }
3971 }
3972
Nate Begeman9008ca62009-04-27 18:41:29 +00003973 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003974 bool Reverse = (NonZeros & 0x3) == 2;
3975 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003976 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003977 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3978 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003979 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3980 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003981 }
3982
Nate Begemanfdea31a2010-03-24 20:49:50 +00003983 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3984 // Check for a build vector of consecutive loads.
3985 for (unsigned i = 0; i < NumElems; ++i)
3986 V[i] = Op.getOperand(i);
3987
3988 // Check for elements which are consecutive loads.
3989 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3990 if (LD.getNode())
3991 return LD;
3992
3993 // For SSE 4.1, use inserts into undef.
3994 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003995 V[0] = DAG.getUNDEF(VT);
3996 for (unsigned i = 0; i < NumElems; ++i)
3997 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3998 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3999 Op.getOperand(i), DAG.getIntPtrConstant(i));
4000 return V[0];
4001 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00004002
4003 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00004004 // e.g. for v4f32
4005 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4006 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4007 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00004008 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00004009 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004010 NumElems >>= 1;
4011 while (NumElems != 0) {
4012 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004013 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004014 NumElems >>= 1;
4015 }
4016 return V[0];
4017 }
Dan Gohman475871a2008-07-27 21:46:04 +00004018 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004019}
4020
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004021SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004022X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004023 // We support concatenate two MMX registers and place them in a MMX
4024 // register. This is better than doing a stack convert.
4025 DebugLoc dl = Op.getDebugLoc();
4026 EVT ResVT = Op.getValueType();
4027 assert(Op.getNumOperands() == 2);
4028 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4029 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4030 int Mask[2];
4031 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4032 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4033 InVec = Op.getOperand(1);
4034 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4035 unsigned NumElts = ResVT.getVectorNumElements();
4036 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4037 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4038 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4039 } else {
4040 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4041 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4042 Mask[0] = 0; Mask[1] = 2;
4043 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4044 }
4045 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4046}
4047
Nate Begemanb9a47b82009-02-23 08:49:38 +00004048// v8i16 shuffles - Prefer shuffles in the following order:
4049// 1. [all] pshuflw, pshufhw, optional move
4050// 2. [ssse3] 1 x pshufb
4051// 3. [ssse3] 2 x pshufb + 1 x por
4052// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004053static
Nate Begeman9008ca62009-04-27 18:41:29 +00004054SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004055 SelectionDAG &DAG,
4056 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004057 SDValue V1 = SVOp->getOperand(0);
4058 SDValue V2 = SVOp->getOperand(1);
4059 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004060 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004061
Nate Begemanb9a47b82009-02-23 08:49:38 +00004062 // Determine if more than 1 of the words in each of the low and high quadwords
4063 // of the result come from the same quadword of one of the two inputs. Undef
4064 // mask values count as coming from any quadword, for better codegen.
4065 SmallVector<unsigned, 4> LoQuad(4);
4066 SmallVector<unsigned, 4> HiQuad(4);
4067 BitVector InputQuads(4);
4068 for (unsigned i = 0; i < 8; ++i) {
4069 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004070 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004071 MaskVals.push_back(EltIdx);
4072 if (EltIdx < 0) {
4073 ++Quad[0];
4074 ++Quad[1];
4075 ++Quad[2];
4076 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004077 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004078 }
4079 ++Quad[EltIdx / 4];
4080 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004081 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004082
Nate Begemanb9a47b82009-02-23 08:49:38 +00004083 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004084 unsigned MaxQuad = 1;
4085 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004086 if (LoQuad[i] > MaxQuad) {
4087 BestLoQuad = i;
4088 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004089 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004090 }
4091
Nate Begemanb9a47b82009-02-23 08:49:38 +00004092 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004093 MaxQuad = 1;
4094 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004095 if (HiQuad[i] > MaxQuad) {
4096 BestHiQuad = i;
4097 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004098 }
4099 }
4100
Nate Begemanb9a47b82009-02-23 08:49:38 +00004101 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004102 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004103 // single pshufb instruction is necessary. If There are more than 2 input
4104 // quads, disable the next transformation since it does not help SSSE3.
4105 bool V1Used = InputQuads[0] || InputQuads[1];
4106 bool V2Used = InputQuads[2] || InputQuads[3];
4107 if (TLI.getSubtarget()->hasSSSE3()) {
4108 if (InputQuads.count() == 2 && V1Used && V2Used) {
4109 BestLoQuad = InputQuads.find_first();
4110 BestHiQuad = InputQuads.find_next(BestLoQuad);
4111 }
4112 if (InputQuads.count() > 2) {
4113 BestLoQuad = -1;
4114 BestHiQuad = -1;
4115 }
4116 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004117
Nate Begemanb9a47b82009-02-23 08:49:38 +00004118 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4119 // the shuffle mask. If a quad is scored as -1, that means that it contains
4120 // words from all 4 input quadwords.
4121 SDValue NewV;
4122 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004123 SmallVector<int, 8> MaskV;
4124 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4125 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004126 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004127 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4128 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4129 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004130
Nate Begemanb9a47b82009-02-23 08:49:38 +00004131 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4132 // source words for the shuffle, to aid later transformations.
4133 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004134 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004135 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004136 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004137 if (idx != (int)i)
4138 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004139 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004140 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004141 AllWordsInNewV = false;
4142 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004143 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004144
Nate Begemanb9a47b82009-02-23 08:49:38 +00004145 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4146 if (AllWordsInNewV) {
4147 for (int i = 0; i != 8; ++i) {
4148 int idx = MaskVals[i];
4149 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004150 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004151 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004152 if ((idx != i) && idx < 4)
4153 pshufhw = false;
4154 if ((idx != i) && idx > 3)
4155 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004156 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004157 V1 = NewV;
4158 V2Used = false;
4159 BestLoQuad = 0;
4160 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004161 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004162
Nate Begemanb9a47b82009-02-23 08:49:38 +00004163 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4164 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004165 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004166 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004167 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004168 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004169 }
Eric Christopherfd179292009-08-27 18:07:15 +00004170
Nate Begemanb9a47b82009-02-23 08:49:38 +00004171 // If we have SSSE3, and all words of the result are from 1 input vector,
4172 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4173 // is present, fall back to case 4.
4174 if (TLI.getSubtarget()->hasSSSE3()) {
4175 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004176
Nate Begemanb9a47b82009-02-23 08:49:38 +00004177 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004178 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004179 // mask, and elements that come from V1 in the V2 mask, so that the two
4180 // results can be OR'd together.
4181 bool TwoInputs = V1Used && V2Used;
4182 for (unsigned i = 0; i != 8; ++i) {
4183 int EltIdx = MaskVals[i] * 2;
4184 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004185 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4186 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004187 continue;
4188 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004189 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4190 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004191 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004192 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004193 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004194 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004196 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004197 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004198
Nate Begemanb9a47b82009-02-23 08:49:38 +00004199 // Calculate the shuffle mask for the second input, shuffle it, and
4200 // OR it with the first shuffled input.
4201 pshufbMask.clear();
4202 for (unsigned i = 0; i != 8; ++i) {
4203 int EltIdx = MaskVals[i] * 2;
4204 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004205 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4206 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004207 continue;
4208 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004209 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4210 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004211 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004212 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004213 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004214 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004215 MVT::v16i8, &pshufbMask[0], 16));
4216 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4217 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004218 }
4219
4220 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4221 // and update MaskVals with new element order.
4222 BitVector InOrder(8);
4223 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004224 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004225 for (int i = 0; i != 4; ++i) {
4226 int idx = MaskVals[i];
4227 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004228 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004229 InOrder.set(i);
4230 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004231 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004232 InOrder.set(i);
4233 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004234 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004235 }
4236 }
4237 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004238 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004240 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004241 }
Eric Christopherfd179292009-08-27 18:07:15 +00004242
Nate Begemanb9a47b82009-02-23 08:49:38 +00004243 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4244 // and update MaskVals with the new element order.
4245 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004246 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004247 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004248 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004249 for (unsigned i = 4; i != 8; ++i) {
4250 int idx = MaskVals[i];
4251 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004252 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004253 InOrder.set(i);
4254 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004255 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004256 InOrder.set(i);
4257 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004258 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004259 }
4260 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004261 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004262 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004263 }
Eric Christopherfd179292009-08-27 18:07:15 +00004264
Nate Begemanb9a47b82009-02-23 08:49:38 +00004265 // In case BestHi & BestLo were both -1, which means each quadword has a word
4266 // from each of the four input quadwords, calculate the InOrder bitvector now
4267 // before falling through to the insert/extract cleanup.
4268 if (BestLoQuad == -1 && BestHiQuad == -1) {
4269 NewV = V1;
4270 for (int i = 0; i != 8; ++i)
4271 if (MaskVals[i] < 0 || MaskVals[i] == i)
4272 InOrder.set(i);
4273 }
Eric Christopherfd179292009-08-27 18:07:15 +00004274
Nate Begemanb9a47b82009-02-23 08:49:38 +00004275 // The other elements are put in the right place using pextrw and pinsrw.
4276 for (unsigned i = 0; i != 8; ++i) {
4277 if (InOrder[i])
4278 continue;
4279 int EltIdx = MaskVals[i];
4280 if (EltIdx < 0)
4281 continue;
4282 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004283 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004284 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004285 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004286 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004287 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004288 DAG.getIntPtrConstant(i));
4289 }
4290 return NewV;
4291}
4292
4293// v16i8 shuffles - Prefer shuffles in the following order:
4294// 1. [ssse3] 1 x pshufb
4295// 2. [ssse3] 2 x pshufb + 1 x por
4296// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4297static
Nate Begeman9008ca62009-04-27 18:41:29 +00004298SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004299 SelectionDAG &DAG,
4300 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004301 SDValue V1 = SVOp->getOperand(0);
4302 SDValue V2 = SVOp->getOperand(1);
4303 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004304 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004305 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004306
Nate Begemanb9a47b82009-02-23 08:49:38 +00004307 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004308 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004309 // present, fall back to case 3.
4310 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4311 bool V1Only = true;
4312 bool V2Only = true;
4313 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004314 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004315 if (EltIdx < 0)
4316 continue;
4317 if (EltIdx < 16)
4318 V2Only = false;
4319 else
4320 V1Only = false;
4321 }
Eric Christopherfd179292009-08-27 18:07:15 +00004322
Nate Begemanb9a47b82009-02-23 08:49:38 +00004323 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4324 if (TLI.getSubtarget()->hasSSSE3()) {
4325 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004326
Nate Begemanb9a47b82009-02-23 08:49:38 +00004327 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004328 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004329 //
4330 // Otherwise, we have elements from both input vectors, and must zero out
4331 // elements that come from V2 in the first mask, and V1 in the second mask
4332 // so that we can OR them together.
4333 bool TwoInputs = !(V1Only || V2Only);
4334 for (unsigned i = 0; i != 16; ++i) {
4335 int EltIdx = MaskVals[i];
4336 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004337 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004338 continue;
4339 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004340 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004341 }
4342 // If all the elements are from V2, assign it to V1 and return after
4343 // building the first pshufb.
4344 if (V2Only)
4345 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004346 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004347 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004348 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004349 if (!TwoInputs)
4350 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004351
Nate Begemanb9a47b82009-02-23 08:49:38 +00004352 // Calculate the shuffle mask for the second input, shuffle it, and
4353 // OR it with the first shuffled input.
4354 pshufbMask.clear();
4355 for (unsigned i = 0; i != 16; ++i) {
4356 int EltIdx = MaskVals[i];
4357 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004358 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004359 continue;
4360 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004361 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004362 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004363 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004364 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004365 MVT::v16i8, &pshufbMask[0], 16));
4366 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004367 }
Eric Christopherfd179292009-08-27 18:07:15 +00004368
Nate Begemanb9a47b82009-02-23 08:49:38 +00004369 // No SSSE3 - Calculate in place words and then fix all out of place words
4370 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4371 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004372 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4373 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004374 SDValue NewV = V2Only ? V2 : V1;
4375 for (int i = 0; i != 8; ++i) {
4376 int Elt0 = MaskVals[i*2];
4377 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004378
Nate Begemanb9a47b82009-02-23 08:49:38 +00004379 // This word of the result is all undef, skip it.
4380 if (Elt0 < 0 && Elt1 < 0)
4381 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004382
Nate Begemanb9a47b82009-02-23 08:49:38 +00004383 // This word of the result is already in the correct place, skip it.
4384 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4385 continue;
4386 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4387 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004388
Nate Begemanb9a47b82009-02-23 08:49:38 +00004389 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4390 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4391 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004392
4393 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4394 // using a single extract together, load it and store it.
4395 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004396 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004397 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004398 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004399 DAG.getIntPtrConstant(i));
4400 continue;
4401 }
4402
Nate Begemanb9a47b82009-02-23 08:49:38 +00004403 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004404 // source byte is not also odd, shift the extracted word left 8 bits
4405 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004406 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004407 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004408 DAG.getIntPtrConstant(Elt1 / 2));
4409 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004410 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004411 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004412 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004413 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4414 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004415 }
4416 // If Elt0 is defined, extract it from the appropriate source. If the
4417 // source byte is not also even, shift the extracted word right 8 bits. If
4418 // Elt1 was also defined, OR the extracted values together before
4419 // inserting them in the result.
4420 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004421 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004422 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4423 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004425 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004426 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004427 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4428 DAG.getConstant(0x00FF, MVT::i16));
4429 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004430 : InsElt0;
4431 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004432 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004433 DAG.getIntPtrConstant(i));
4434 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004435 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004436}
4437
Evan Cheng7a831ce2007-12-15 03:00:47 +00004438/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4439/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4440/// done when every pair / quad of shuffle mask elements point to elements in
4441/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004442/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4443static
Nate Begeman9008ca62009-04-27 18:41:29 +00004444SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4445 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004446 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004447 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004448 SDValue V1 = SVOp->getOperand(0);
4449 SDValue V2 = SVOp->getOperand(1);
4450 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004451 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004452 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004453 EVT MaskEltVT = MaskVT.getVectorElementType();
4454 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004455 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004456 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004457 case MVT::v4f32: NewVT = MVT::v2f64; break;
4458 case MVT::v4i32: NewVT = MVT::v2i64; break;
4459 case MVT::v8i16: NewVT = MVT::v4i32; break;
4460 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004461 }
4462
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004463 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004464 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004465 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004466 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004467 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004468 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004469 int Scale = NumElems / NewWidth;
4470 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004471 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004472 int StartIdx = -1;
4473 for (int j = 0; j < Scale; ++j) {
4474 int EltIdx = SVOp->getMaskElt(i+j);
4475 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004476 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004478 StartIdx = EltIdx - (EltIdx % Scale);
4479 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004480 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004481 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004482 if (StartIdx == -1)
4483 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004484 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004486 }
4487
Dale Johannesenace16102009-02-03 19:33:06 +00004488 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4489 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004490 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004491}
4492
Evan Chengd880b972008-05-09 21:53:03 +00004493/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004494///
Owen Andersone50ed302009-08-10 22:56:29 +00004495static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004496 SDValue SrcOp, SelectionDAG &DAG,
4497 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004498 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004499 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004500 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004501 LD = dyn_cast<LoadSDNode>(SrcOp);
4502 if (!LD) {
4503 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4504 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004505 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4506 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004507 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4508 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004509 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004510 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004511 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004512 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4513 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4514 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4515 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004516 SrcOp.getOperand(0)
4517 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004518 }
4519 }
4520 }
4521
Dale Johannesenace16102009-02-03 19:33:06 +00004522 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4523 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004524 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004525 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004526}
4527
Evan Chengace3c172008-07-22 21:13:36 +00004528/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4529/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004530static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004531LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4532 SDValue V1 = SVOp->getOperand(0);
4533 SDValue V2 = SVOp->getOperand(1);
4534 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004535 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004536
Evan Chengace3c172008-07-22 21:13:36 +00004537 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004538 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004539 SmallVector<int, 8> Mask1(4U, -1);
4540 SmallVector<int, 8> PermMask;
4541 SVOp->getMask(PermMask);
4542
Evan Chengace3c172008-07-22 21:13:36 +00004543 unsigned NumHi = 0;
4544 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004545 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 int Idx = PermMask[i];
4547 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004548 Locs[i] = std::make_pair(-1, -1);
4549 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004550 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4551 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004552 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004553 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004554 NumLo++;
4555 } else {
4556 Locs[i] = std::make_pair(1, NumHi);
4557 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004558 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004559 NumHi++;
4560 }
4561 }
4562 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004563
Evan Chengace3c172008-07-22 21:13:36 +00004564 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004565 // If no more than two elements come from either vector. This can be
4566 // implemented with two shuffles. First shuffle gather the elements.
4567 // The second shuffle, which takes the first shuffle as both of its
4568 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004569 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004570
Nate Begeman9008ca62009-04-27 18:41:29 +00004571 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004572
Evan Chengace3c172008-07-22 21:13:36 +00004573 for (unsigned i = 0; i != 4; ++i) {
4574 if (Locs[i].first == -1)
4575 continue;
4576 else {
4577 unsigned Idx = (i < 2) ? 0 : 4;
4578 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004579 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004580 }
4581 }
4582
Nate Begeman9008ca62009-04-27 18:41:29 +00004583 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004584 } else if (NumLo == 3 || NumHi == 3) {
4585 // Otherwise, we must have three elements from one vector, call it X, and
4586 // one element from the other, call it Y. First, use a shufps to build an
4587 // intermediate vector with the one element from Y and the element from X
4588 // that will be in the same half in the final destination (the indexes don't
4589 // matter). Then, use a shufps to build the final vector, taking the half
4590 // containing the element from Y from the intermediate, and the other half
4591 // from X.
4592 if (NumHi == 3) {
4593 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004594 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004595 std::swap(V1, V2);
4596 }
4597
4598 // Find the element from V2.
4599 unsigned HiIndex;
4600 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004601 int Val = PermMask[HiIndex];
4602 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004603 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004604 if (Val >= 4)
4605 break;
4606 }
4607
Nate Begeman9008ca62009-04-27 18:41:29 +00004608 Mask1[0] = PermMask[HiIndex];
4609 Mask1[1] = -1;
4610 Mask1[2] = PermMask[HiIndex^1];
4611 Mask1[3] = -1;
4612 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004613
4614 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004615 Mask1[0] = PermMask[0];
4616 Mask1[1] = PermMask[1];
4617 Mask1[2] = HiIndex & 1 ? 6 : 4;
4618 Mask1[3] = HiIndex & 1 ? 4 : 6;
4619 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004620 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004621 Mask1[0] = HiIndex & 1 ? 2 : 0;
4622 Mask1[1] = HiIndex & 1 ? 0 : 2;
4623 Mask1[2] = PermMask[2];
4624 Mask1[3] = PermMask[3];
4625 if (Mask1[2] >= 0)
4626 Mask1[2] += 4;
4627 if (Mask1[3] >= 0)
4628 Mask1[3] += 4;
4629 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004630 }
Evan Chengace3c172008-07-22 21:13:36 +00004631 }
4632
4633 // Break it into (shuffle shuffle_hi, shuffle_lo).
4634 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004635 SmallVector<int,8> LoMask(4U, -1);
4636 SmallVector<int,8> HiMask(4U, -1);
4637
4638 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004639 unsigned MaskIdx = 0;
4640 unsigned LoIdx = 0;
4641 unsigned HiIdx = 2;
4642 for (unsigned i = 0; i != 4; ++i) {
4643 if (i == 2) {
4644 MaskPtr = &HiMask;
4645 MaskIdx = 1;
4646 LoIdx = 0;
4647 HiIdx = 2;
4648 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004649 int Idx = PermMask[i];
4650 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004651 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004652 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004653 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004655 LoIdx++;
4656 } else {
4657 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004658 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004659 HiIdx++;
4660 }
4661 }
4662
Nate Begeman9008ca62009-04-27 18:41:29 +00004663 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4664 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4665 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004666 for (unsigned i = 0; i != 4; ++i) {
4667 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004668 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004669 } else {
4670 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004671 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004672 }
4673 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004675}
4676
Dan Gohman475871a2008-07-27 21:46:04 +00004677SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004678X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004679 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004680 SDValue V1 = Op.getOperand(0);
4681 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004682 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004683 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004684 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004685 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004686 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4687 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004688 bool V1IsSplat = false;
4689 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004690
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004692 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004693
Nate Begeman9008ca62009-04-27 18:41:29 +00004694 // Promote splats to v4f32.
4695 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004696 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004697 return Op;
4698 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004699 }
4700
Evan Cheng7a831ce2007-12-15 03:00:47 +00004701 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4702 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004703 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004704 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004705 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004706 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004707 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004709 // FIXME: Figure out a cleaner way to do this.
4710 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004711 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004712 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004713 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004714 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4715 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4716 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004717 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004718 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004719 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4720 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004721 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004723 }
4724 }
Eric Christopherfd179292009-08-27 18:07:15 +00004725
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 if (X86::isPSHUFDMask(SVOp))
4727 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004728
Evan Chengf26ffe92008-05-29 08:22:04 +00004729 // Check if this can be converted into a logical shift.
4730 bool isLeft = false;
4731 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004732 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004733 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004734 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004735 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004736 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004737 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004738 EVT EltVT = VT.getVectorElementType();
4739 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004740 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004741 }
Eric Christopherfd179292009-08-27 18:07:15 +00004742
Nate Begeman9008ca62009-04-27 18:41:29 +00004743 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004744 if (V1IsUndef)
4745 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004746 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004747 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004748 if (!isMMX)
4749 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004750 }
Eric Christopherfd179292009-08-27 18:07:15 +00004751
Nate Begeman9008ca62009-04-27 18:41:29 +00004752 // FIXME: fold these into legal mask.
4753 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4754 X86::isMOVSLDUPMask(SVOp) ||
4755 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004756 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004757 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004758 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004759
Nate Begeman9008ca62009-04-27 18:41:29 +00004760 if (ShouldXformToMOVHLPS(SVOp) ||
4761 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4762 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004763
Evan Chengf26ffe92008-05-29 08:22:04 +00004764 if (isShift) {
4765 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004766 EVT EltVT = VT.getVectorElementType();
4767 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004768 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004769 }
Eric Christopherfd179292009-08-27 18:07:15 +00004770
Evan Cheng9eca5e82006-10-25 21:49:50 +00004771 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004772 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4773 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004774 V1IsSplat = isSplatVector(V1.getNode());
4775 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004776
Chris Lattner8a594482007-11-25 00:24:49 +00004777 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004778 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004779 Op = CommuteVectorShuffle(SVOp, DAG);
4780 SVOp = cast<ShuffleVectorSDNode>(Op);
4781 V1 = SVOp->getOperand(0);
4782 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004783 std::swap(V1IsSplat, V2IsSplat);
4784 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004785 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004786 }
4787
Nate Begeman9008ca62009-04-27 18:41:29 +00004788 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4789 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004790 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004791 return V1;
4792 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4793 // the instruction selector will not match, so get a canonical MOVL with
4794 // swapped operands to undo the commute.
4795 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004796 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004797
Nate Begeman9008ca62009-04-27 18:41:29 +00004798 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4799 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4800 X86::isUNPCKLMask(SVOp) ||
4801 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004802 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004803
Evan Cheng9bbbb982006-10-25 20:48:19 +00004804 if (V2IsSplat) {
4805 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004806 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004807 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004808 SDValue NewMask = NormalizeMask(SVOp, DAG);
4809 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4810 if (NSVOp != SVOp) {
4811 if (X86::isUNPCKLMask(NSVOp, true)) {
4812 return NewMask;
4813 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4814 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004815 }
4816 }
4817 }
4818
Evan Cheng9eca5e82006-10-25 21:49:50 +00004819 if (Commuted) {
4820 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004821 // FIXME: this seems wrong.
4822 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4823 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4824 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4825 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4826 X86::isUNPCKLMask(NewSVOp) ||
4827 X86::isUNPCKHMask(NewSVOp))
4828 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004829 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004830
Nate Begemanb9a47b82009-02-23 08:49:38 +00004831 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004832
4833 // Normalize the node to match x86 shuffle ops if needed
4834 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4835 return CommuteVectorShuffle(SVOp, DAG);
4836
4837 // Check for legal shuffle and return?
4838 SmallVector<int, 16> PermMask;
4839 SVOp->getMask(PermMask);
4840 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004841 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004842
Evan Cheng14b32e12007-12-11 01:46:18 +00004843 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004844 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004845 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004846 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004847 return NewOp;
4848 }
4849
Owen Anderson825b72b2009-08-11 20:47:22 +00004850 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004851 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004852 if (NewOp.getNode())
4853 return NewOp;
4854 }
Eric Christopherfd179292009-08-27 18:07:15 +00004855
Evan Chengace3c172008-07-22 21:13:36 +00004856 // Handle all 4 wide cases with a number of shuffles except for MMX.
4857 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004858 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004859
Dan Gohman475871a2008-07-27 21:46:04 +00004860 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004861}
4862
Dan Gohman475871a2008-07-27 21:46:04 +00004863SDValue
4864X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004865 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004866 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004867 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004868 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004870 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004871 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004872 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004873 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004874 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004875 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4876 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4877 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004878 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4879 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004880 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004881 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004882 Op.getOperand(0)),
4883 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004884 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004885 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004886 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004887 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004888 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004889 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004890 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4891 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004892 // result has a single use which is a store or a bitcast to i32. And in
4893 // the case of a store, it's not worth it if the index is a constant 0,
4894 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004895 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004896 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004897 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004898 if ((User->getOpcode() != ISD::STORE ||
4899 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4900 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004901 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004902 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004903 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4905 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004906 Op.getOperand(0)),
4907 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004908 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4909 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004910 // ExtractPS works with constant index.
4911 if (isa<ConstantSDNode>(Op.getOperand(1)))
4912 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004913 }
Dan Gohman475871a2008-07-27 21:46:04 +00004914 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004915}
4916
4917
Dan Gohman475871a2008-07-27 21:46:04 +00004918SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004919X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
4920 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004921 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004922 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004923
Evan Cheng62a3f152008-03-24 21:52:23 +00004924 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004925 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004926 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004927 return Res;
4928 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004929
Owen Andersone50ed302009-08-10 22:56:29 +00004930 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004931 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004932 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004933 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004934 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004935 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004936 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004937 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4938 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004939 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004941 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004942 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004943 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004944 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004945 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004946 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004947 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004948 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004949 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004950 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004951 if (Idx == 0)
4952 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004953
Evan Cheng0db9fe62006-04-25 20:13:52 +00004954 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004955 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004956 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004957 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004958 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004959 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004960 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004961 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004962 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4963 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4964 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004965 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004966 if (Idx == 0)
4967 return Op;
4968
4969 // UNPCKHPD the element to the lowest double word, then movsd.
4970 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4971 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004972 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004973 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004974 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004975 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004976 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004977 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004978 }
4979
Dan Gohman475871a2008-07-27 21:46:04 +00004980 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004981}
4982
Dan Gohman475871a2008-07-27 21:46:04 +00004983SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004984X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
4985 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004986 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004987 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004988 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004989
Dan Gohman475871a2008-07-27 21:46:04 +00004990 SDValue N0 = Op.getOperand(0);
4991 SDValue N1 = Op.getOperand(1);
4992 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004993
Dan Gohman8a55ce42009-09-23 21:02:20 +00004994 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004995 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004996 unsigned Opc;
4997 if (VT == MVT::v8i16)
4998 Opc = X86ISD::PINSRW;
4999 else if (VT == MVT::v4i16)
5000 Opc = X86ISD::MMX_PINSRW;
5001 else if (VT == MVT::v16i8)
5002 Opc = X86ISD::PINSRB;
5003 else
5004 Opc = X86ISD::PINSRB;
5005
Nate Begeman14d12ca2008-02-11 04:19:36 +00005006 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5007 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005008 if (N1.getValueType() != MVT::i32)
5009 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5010 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005011 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005012 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005013 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005014 // Bits [7:6] of the constant are the source select. This will always be
5015 // zero here. The DAG Combiner may combine an extract_elt index into these
5016 // bits. For example (insert (extract, 3), 2) could be matched by putting
5017 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005018 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005019 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005020 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005021 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005022 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005023 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005024 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005025 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005026 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005027 // PINSR* works with constant index.
5028 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005029 }
Dan Gohman475871a2008-07-27 21:46:04 +00005030 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005031}
5032
Dan Gohman475871a2008-07-27 21:46:04 +00005033SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005034X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005035 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005036 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005037
5038 if (Subtarget->hasSSE41())
5039 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5040
Dan Gohman8a55ce42009-09-23 21:02:20 +00005041 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005042 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005043
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005044 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005045 SDValue N0 = Op.getOperand(0);
5046 SDValue N1 = Op.getOperand(1);
5047 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005048
Dan Gohman8a55ce42009-09-23 21:02:20 +00005049 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005050 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5051 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005052 if (N1.getValueType() != MVT::i32)
5053 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5054 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005055 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005056 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5057 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005058 }
Dan Gohman475871a2008-07-27 21:46:04 +00005059 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005060}
5061
Dan Gohman475871a2008-07-27 21:46:04 +00005062SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005063X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005064 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005065 if (Op.getValueType() == MVT::v2f32)
5066 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
5067 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5068 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00005069 Op.getOperand(0))));
5070
Owen Anderson825b72b2009-08-11 20:47:22 +00005071 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5072 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005073
Owen Anderson825b72b2009-08-11 20:47:22 +00005074 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5075 EVT VT = MVT::v2i32;
5076 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005077 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005078 case MVT::v16i8:
5079 case MVT::v8i16:
5080 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005081 break;
5082 }
Dale Johannesenace16102009-02-03 19:33:06 +00005083 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5084 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005085}
5086
Bill Wendling056292f2008-09-16 21:48:12 +00005087// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5088// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5089// one of the above mentioned nodes. It has to be wrapped because otherwise
5090// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5091// be used to form addressing mode. These wrapped nodes will be selected
5092// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005093SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005094X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005095 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005096
Chris Lattner41621a22009-06-26 19:22:52 +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;
Chris Lattner18c59872009-06-27 04:16:01 +00005100 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
Evan Cheng1606e8e2009-03-13 07:51:59 +00005111 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005112 CP->getAlignment(),
5113 CP->getOffset(), OpFlag);
5114 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005115 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005116 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005117 if (OpFlag) {
5118 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005119 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005120 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005121 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005122 }
5123
5124 return Result;
5125}
5126
Dan Gohmand858e902010-04-17 15:26:15 +00005127SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005128 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005129
Chris Lattner18c59872009-06-27 04:16:01 +00005130 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5131 // global base reg.
5132 unsigned char OpFlag = 0;
5133 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005134 CodeModel::Model M = getTargetMachine().getCodeModel();
5135
Chris Lattner4f066492009-07-11 20:29:19 +00005136 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005137 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005138 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005139 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005140 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005141 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005142 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005143
Chris Lattner18c59872009-06-27 04:16:01 +00005144 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5145 OpFlag);
5146 DebugLoc DL = JT->getDebugLoc();
5147 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005148
Chris Lattner18c59872009-06-27 04:16:01 +00005149 // With PIC, the address is actually $g + Offset.
5150 if (OpFlag) {
5151 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5152 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005153 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005154 Result);
5155 }
Eric Christopherfd179292009-08-27 18:07:15 +00005156
Chris Lattner18c59872009-06-27 04:16:01 +00005157 return Result;
5158}
5159
5160SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005161X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005162 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005163
Chris Lattner18c59872009-06-27 04:16:01 +00005164 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5165 // global base reg.
5166 unsigned char OpFlag = 0;
5167 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005168 CodeModel::Model M = getTargetMachine().getCodeModel();
5169
Chris Lattner4f066492009-07-11 20:29:19 +00005170 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005171 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005172 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005173 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005174 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005175 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005176 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005177
Chris Lattner18c59872009-06-27 04:16:01 +00005178 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005179
Chris Lattner18c59872009-06-27 04:16:01 +00005180 DebugLoc DL = Op.getDebugLoc();
5181 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005182
5183
Chris Lattner18c59872009-06-27 04:16:01 +00005184 // With PIC, the address is actually $g + Offset.
5185 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005186 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005187 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5188 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005189 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005190 Result);
5191 }
Eric Christopherfd179292009-08-27 18:07:15 +00005192
Chris Lattner18c59872009-06-27 04:16:01 +00005193 return Result;
5194}
5195
Dan Gohman475871a2008-07-27 21:46:04 +00005196SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005197X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005198 // Create the TargetBlockAddressAddress node.
5199 unsigned char OpFlags =
5200 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005201 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005202 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005203 DebugLoc dl = Op.getDebugLoc();
5204 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5205 /*isTarget=*/true, OpFlags);
5206
Dan Gohmanf705adb2009-10-30 01:28:02 +00005207 if (Subtarget->isPICStyleRIPRel() &&
5208 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005209 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5210 else
5211 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005212
Dan Gohman29cbade2009-11-20 23:18:13 +00005213 // With PIC, the address is actually $g + Offset.
5214 if (isGlobalRelativeToPICBase(OpFlags)) {
5215 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5216 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5217 Result);
5218 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005219
5220 return Result;
5221}
5222
5223SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005224X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005225 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005226 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005227 // Create the TargetGlobalAddress node, folding in the constant
5228 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005229 unsigned char OpFlags =
5230 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005231 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005232 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005233 if (OpFlags == X86II::MO_NO_FLAG &&
5234 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005235 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005236 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005237 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005238 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005239 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005240 }
Eric Christopherfd179292009-08-27 18:07:15 +00005241
Chris Lattner4f066492009-07-11 20:29:19 +00005242 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005243 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005244 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5245 else
5246 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005247
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005248 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005249 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005250 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5251 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005252 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005253 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005254
Chris Lattner36c25012009-07-10 07:34:39 +00005255 // For globals that require a load from a stub to get the address, emit the
5256 // load.
5257 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005258 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005259 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005260
Dan Gohman6520e202008-10-18 02:06:02 +00005261 // If there was a non-zero offset that we didn't fold, create an explicit
5262 // addition for it.
5263 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005264 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005265 DAG.getConstant(Offset, getPointerTy()));
5266
Evan Cheng0db9fe62006-04-25 20:13:52 +00005267 return Result;
5268}
5269
Evan Chengda43bcf2008-09-24 00:05:32 +00005270SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005271X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00005272 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005273 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005274 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005275}
5276
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005277static SDValue
5278GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005279 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005280 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005281 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005282 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005283 DebugLoc dl = GA->getDebugLoc();
5284 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5285 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005286 GA->getOffset(),
5287 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005288 if (InFlag) {
5289 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005290 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005291 } else {
5292 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005293 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005294 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005295
5296 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5297 MFI->setHasCalls(true);
5298
Rafael Espindola15f1b662009-04-24 12:59:40 +00005299 SDValue Flag = Chain.getValue(1);
5300 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005301}
5302
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005303// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005304static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005305LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005306 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005307 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005308 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5309 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005310 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005311 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005312 InFlag = Chain.getValue(1);
5313
Chris Lattnerb903bed2009-06-26 21:20:29 +00005314 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005315}
5316
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005317// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005318static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005319LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005320 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005321 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5322 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005323}
5324
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005325// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5326// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005327static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005328 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005329 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005330 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005331 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005332 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005333 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005334 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005335 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005336
5337 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005338 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005339
Chris Lattnerb903bed2009-06-26 21:20:29 +00005340 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005341 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5342 // initialexec.
5343 unsigned WrapperKind = X86ISD::Wrapper;
5344 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005345 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005346 } else if (is64Bit) {
5347 assert(model == TLSModel::InitialExec);
5348 OperandFlags = X86II::MO_GOTTPOFF;
5349 WrapperKind = X86ISD::WrapperRIP;
5350 } else {
5351 assert(model == TLSModel::InitialExec);
5352 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005353 }
Eric Christopherfd179292009-08-27 18:07:15 +00005354
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005355 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5356 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005357 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005358 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005359 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005360
Rafael Espindola9a580232009-02-27 13:37:18 +00005361 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005362 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005363 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005364
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005365 // The address of the thread local variable is the add of the thread
5366 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005367 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005368}
5369
Dan Gohman475871a2008-07-27 21:46:04 +00005370SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005371X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005372 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005373 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005374 assert(Subtarget->isTargetELF() &&
5375 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005376 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005377 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005378
Chris Lattnerb903bed2009-06-26 21:20:29 +00005379 // If GV is an alias then use the aliasee for determining
5380 // thread-localness.
5381 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5382 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005383
Chris Lattnerb903bed2009-06-26 21:20:29 +00005384 TLSModel::Model model = getTLSModel(GV,
5385 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005386
Chris Lattnerb903bed2009-06-26 21:20:29 +00005387 switch (model) {
5388 case TLSModel::GeneralDynamic:
5389 case TLSModel::LocalDynamic: // not implemented
5390 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005391 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005392 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005393
Chris Lattnerb903bed2009-06-26 21:20:29 +00005394 case TLSModel::InitialExec:
5395 case TLSModel::LocalExec:
5396 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5397 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005398 }
Eric Christopherfd179292009-08-27 18:07:15 +00005399
Torok Edwinc23197a2009-07-14 16:55:14 +00005400 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005401 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005402}
5403
Evan Cheng0db9fe62006-04-25 20:13:52 +00005404
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005405/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005406/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005407SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005408 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005409 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005410 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005411 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005412 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005413 SDValue ShOpLo = Op.getOperand(0);
5414 SDValue ShOpHi = Op.getOperand(1);
5415 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005416 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005417 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005418 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005419
Dan Gohman475871a2008-07-27 21:46:04 +00005420 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005421 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005422 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5423 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005424 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005425 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5426 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005427 }
Evan Chenge3413162006-01-09 18:33:28 +00005428
Owen Anderson825b72b2009-08-11 20:47:22 +00005429 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5430 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005431 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005432 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005433
Dan Gohman475871a2008-07-27 21:46:04 +00005434 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005435 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005436 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5437 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005438
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005439 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005440 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5441 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005442 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005443 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5444 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005445 }
5446
Dan Gohman475871a2008-07-27 21:46:04 +00005447 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005448 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005449}
Evan Chenga3195e82006-01-12 22:54:21 +00005450
Dan Gohmand858e902010-04-17 15:26:15 +00005451SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5452 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005453 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005454
5455 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005456 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005457 return Op;
5458 }
5459 return SDValue();
5460 }
5461
Owen Anderson825b72b2009-08-11 20:47:22 +00005462 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005463 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005464
Eli Friedman36df4992009-05-27 00:47:34 +00005465 // These are really Legal; return the operand so the caller accepts it as
5466 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005467 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005468 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005469 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005470 Subtarget->is64Bit()) {
5471 return Op;
5472 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005473
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005474 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005475 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005476 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005477 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005478 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005479 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005480 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005481 PseudoSourceValue::getFixedStack(SSFI), 0,
5482 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005483 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5484}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005485
Owen Andersone50ed302009-08-10 22:56:29 +00005486SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005487 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005488 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005489 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005490 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005491 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005492 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005493 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005494 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005495 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005496 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005497 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005498 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005499 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005500
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005501 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005502 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005503 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005504
5505 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5506 // shouldn't be necessary except that RFP cannot be live across
5507 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005508 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005509 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005510 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005511 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005512 SDValue Ops[] = {
5513 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5514 };
5515 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005516 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005517 PseudoSourceValue::getFixedStack(SSFI), 0,
5518 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005519 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005520
Evan Cheng0db9fe62006-04-25 20:13:52 +00005521 return Result;
5522}
5523
Bill Wendling8b8a6362009-01-17 03:56:04 +00005524// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005525SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5526 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005527 // This algorithm is not obvious. Here it is in C code, more or less:
5528 /*
5529 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5530 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5531 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005532
Bill Wendling8b8a6362009-01-17 03:56:04 +00005533 // Copy ints to xmm registers.
5534 __m128i xh = _mm_cvtsi32_si128( hi );
5535 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005536
Bill Wendling8b8a6362009-01-17 03:56:04 +00005537 // Combine into low half of a single xmm register.
5538 __m128i x = _mm_unpacklo_epi32( xh, xl );
5539 __m128d d;
5540 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005541
Bill Wendling8b8a6362009-01-17 03:56:04 +00005542 // Merge in appropriate exponents to give the integer bits the right
5543 // magnitude.
5544 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005545
Bill Wendling8b8a6362009-01-17 03:56:04 +00005546 // Subtract away the biases to deal with the IEEE-754 double precision
5547 // implicit 1.
5548 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005549
Bill Wendling8b8a6362009-01-17 03:56:04 +00005550 // All conversions up to here are exact. The correctly rounded result is
5551 // calculated using the current rounding mode using the following
5552 // horizontal add.
5553 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5554 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5555 // store doesn't really need to be here (except
5556 // maybe to zero the other double)
5557 return sd;
5558 }
5559 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005560
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005561 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005562 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005563
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005564 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005565 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005566 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5567 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5568 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5569 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005570 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005571 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005572
Bill Wendling8b8a6362009-01-17 03:56:04 +00005573 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005574 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005575 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005576 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005577 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005578 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005579 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005580
Owen Anderson825b72b2009-08-11 20:47:22 +00005581 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5582 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005583 Op.getOperand(0),
5584 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5586 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005587 Op.getOperand(0),
5588 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005589 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5590 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005591 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005592 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005593 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5594 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5595 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005596 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005597 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005599
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005600 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005601 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005602 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5603 DAG.getUNDEF(MVT::v2f64), ShufMask);
5604 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5605 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005606 DAG.getIntPtrConstant(0));
5607}
5608
Bill Wendling8b8a6362009-01-17 03:56:04 +00005609// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005610SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5611 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005612 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005613 // FP constant to bias correct the final result.
5614 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005615 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005616
5617 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005618 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5619 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005620 Op.getOperand(0),
5621 DAG.getIntPtrConstant(0)));
5622
Owen Anderson825b72b2009-08-11 20:47:22 +00005623 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5624 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005625 DAG.getIntPtrConstant(0));
5626
5627 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005628 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5629 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005630 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005631 MVT::v2f64, Load)),
5632 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005633 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005634 MVT::v2f64, Bias)));
5635 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5636 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005637 DAG.getIntPtrConstant(0));
5638
5639 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005640 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005641
5642 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005643 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005644
Owen Anderson825b72b2009-08-11 20:47:22 +00005645 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005646 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005647 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005648 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005649 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005650 }
5651
5652 // Handle final rounding.
5653 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005654}
5655
Dan Gohmand858e902010-04-17 15:26:15 +00005656SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5657 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005658 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005659 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005660
Evan Chenga06ec9e2009-01-19 08:08:22 +00005661 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5662 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5663 // the optimization here.
5664 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005665 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005666
Owen Andersone50ed302009-08-10 22:56:29 +00005667 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005668 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005669 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005670 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005671 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005672
Bill Wendling8b8a6362009-01-17 03:56:04 +00005673 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005674 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005675 return LowerUINT_TO_FP_i32(Op, DAG);
5676 }
5677
Owen Anderson825b72b2009-08-11 20:47:22 +00005678 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005679
5680 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005681 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005682 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5683 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5684 getPointerTy(), StackSlot, WordOff);
5685 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005686 StackSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005687 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00005688 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005689 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005690}
5691
Dan Gohman475871a2008-07-27 21:46:04 +00005692std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005693FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005694 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005695
Owen Andersone50ed302009-08-10 22:56:29 +00005696 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005697
5698 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005699 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5700 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005701 }
5702
Owen Anderson825b72b2009-08-11 20:47:22 +00005703 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5704 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005705 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005706
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005707 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005708 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005709 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005710 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005711 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005713 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005714 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005715
Evan Cheng87c89352007-10-15 20:11:21 +00005716 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5717 // stack slot.
5718 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005719 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005720 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005721 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005722
Evan Cheng0db9fe62006-04-25 20:13:52 +00005723 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005724 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005725 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005726 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5727 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5728 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005729 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005730
Dan Gohman475871a2008-07-27 21:46:04 +00005731 SDValue Chain = DAG.getEntryNode();
5732 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005733 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005734 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005735 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005736 PseudoSourceValue::getFixedStack(SSFI), 0,
5737 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005738 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005739 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005740 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5741 };
Dale Johannesenace16102009-02-03 19:33:06 +00005742 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005743 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005744 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005745 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5746 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005747
Evan Cheng0db9fe62006-04-25 20:13:52 +00005748 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005749 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005750 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005751
Chris Lattner27a6c732007-11-24 07:07:01 +00005752 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005753}
5754
Dan Gohmand858e902010-04-17 15:26:15 +00005755SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5756 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005757 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005758 if (Op.getValueType() == MVT::v2i32 &&
5759 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005760 return Op;
5761 }
5762 return SDValue();
5763 }
5764
Eli Friedman948e95a2009-05-23 09:59:16 +00005765 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005766 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005767 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5768 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005769
Chris Lattner27a6c732007-11-24 07:07:01 +00005770 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005771 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005772 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005773}
5774
Dan Gohmand858e902010-04-17 15:26:15 +00005775SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5776 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005777 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5778 SDValue FIST = Vals.first, StackSlot = Vals.second;
5779 assert(FIST.getNode() && "Unexpected failure");
5780
5781 // Load the result.
5782 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005783 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005784}
5785
Dan Gohmand858e902010-04-17 15:26:15 +00005786SDValue X86TargetLowering::LowerFABS(SDValue Op,
5787 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005788 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005789 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005790 EVT VT = Op.getValueType();
5791 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005792 if (VT.isVector())
5793 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005794 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005795 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005796 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005797 CV.push_back(C);
5798 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005799 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005800 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005801 CV.push_back(C);
5802 CV.push_back(C);
5803 CV.push_back(C);
5804 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005805 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005806 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005807 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005808 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005809 PseudoSourceValue::getConstantPool(), 0,
5810 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005811 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005812}
5813
Dan Gohmand858e902010-04-17 15:26:15 +00005814SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005815 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005816 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005817 EVT VT = Op.getValueType();
5818 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005819 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005820 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005821 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005822 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005823 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005824 CV.push_back(C);
5825 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005826 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005827 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005828 CV.push_back(C);
5829 CV.push_back(C);
5830 CV.push_back(C);
5831 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005832 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005833 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005834 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005835 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005836 PseudoSourceValue::getConstantPool(), 0,
5837 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005838 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005839 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005840 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5841 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005842 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005843 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005844 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005845 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005846 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005847}
5848
Dan Gohmand858e902010-04-17 15:26:15 +00005849SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005850 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005851 SDValue Op0 = Op.getOperand(0);
5852 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005853 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005854 EVT VT = Op.getValueType();
5855 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005856
5857 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005858 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005859 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005860 SrcVT = VT;
5861 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005862 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005863 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005864 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005865 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005866 }
5867
5868 // At this point the operands and the result should have the same
5869 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005870
Evan Cheng68c47cb2007-01-05 07:55:56 +00005871 // First get the sign bit of second operand.
5872 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005873 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005874 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5875 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005876 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005877 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5878 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5879 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5880 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005881 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005882 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005883 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005884 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005885 PseudoSourceValue::getConstantPool(), 0,
5886 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005887 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005888
5889 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005890 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005891 // Op0 is MVT::f32, Op1 is MVT::f64.
5892 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5893 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5894 DAG.getConstant(32, MVT::i32));
5895 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5896 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005897 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005898 }
5899
Evan Cheng73d6cf12007-01-05 21:37:56 +00005900 // Clear first operand sign bit.
5901 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005902 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005903 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5904 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005905 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005906 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5907 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5908 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5909 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005910 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005911 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005912 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005913 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005914 PseudoSourceValue::getConstantPool(), 0,
5915 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005916 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005917
5918 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005919 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005920}
5921
Dan Gohman076aee32009-03-04 19:44:21 +00005922/// Emit nodes that will be selected as "test Op0,Op0", or something
5923/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005924SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00005925 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00005926 DebugLoc dl = Op.getDebugLoc();
5927
Dan Gohman31125812009-03-07 01:58:32 +00005928 // CF and OF aren't always set the way we want. Determine which
5929 // of these we need.
5930 bool NeedCF = false;
5931 bool NeedOF = false;
5932 switch (X86CC) {
5933 case X86::COND_A: case X86::COND_AE:
5934 case X86::COND_B: case X86::COND_BE:
5935 NeedCF = true;
5936 break;
5937 case X86::COND_G: case X86::COND_GE:
5938 case X86::COND_L: case X86::COND_LE:
5939 case X86::COND_O: case X86::COND_NO:
5940 NeedOF = true;
5941 break;
5942 default: break;
5943 }
5944
Dan Gohman076aee32009-03-04 19:44:21 +00005945 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005946 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5947 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5948 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005949 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005950 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005951 switch (Op.getNode()->getOpcode()) {
5952 case ISD::ADD:
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005953 // Due to an isel shortcoming, be conservative if this add is
5954 // likely to be selected as part of a load-modify-store
5955 // instruction. When the root node in a match is a store, isel
5956 // doesn't know how to remap non-chain non-flag uses of other
5957 // nodes in the match, such as the ADD in this case. This leads
5958 // to the ADD being left around and reselected, with the result
5959 // being two adds in the output. Alas, even if none our users
5960 // are stores, that doesn't prove we're O.K. Ergo, if we have
5961 // any parents that aren't CopyToReg or SETCC, eschew INC/DEC.
5962 // A better fix seems to require climbing the DAG back to the
5963 // root, and it doesn't seem to be worth the effort.
Dan Gohman076aee32009-03-04 19:44:21 +00005964 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005965 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5966 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
Dan Gohman076aee32009-03-04 19:44:21 +00005967 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005968 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005969 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5970 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005971 if (C->getAPIntValue() == 1) {
5972 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005973 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005974 break;
5975 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005976 // An add of negative one (subtract of one) will be selected as a DEC.
5977 if (C->getAPIntValue().isAllOnesValue()) {
5978 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005979 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005980 break;
5981 }
5982 }
Dan Gohman076aee32009-03-04 19:44:21 +00005983 // Otherwise use a regular EFLAGS-setting add.
5984 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005985 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005986 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005987 case ISD::AND: {
5988 // If the primary and result isn't used, don't bother using X86ISD::AND,
5989 // because a TEST instruction will be better.
5990 bool NonFlagUse = false;
5991 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005992 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5993 SDNode *User = *UI;
5994 unsigned UOpNo = UI.getOperandNo();
5995 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5996 // Look pass truncate.
5997 UOpNo = User->use_begin().getOperandNo();
5998 User = *User->use_begin();
5999 }
6000 if (User->getOpcode() != ISD::BRCOND &&
6001 User->getOpcode() != ISD::SETCC &&
6002 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00006003 NonFlagUse = true;
6004 break;
6005 }
Evan Cheng17751da2010-01-07 00:54:06 +00006006 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00006007 if (!NonFlagUse)
6008 break;
6009 }
6010 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00006011 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006012 case ISD::OR:
6013 case ISD::XOR:
6014 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00006015 // likely to be selected as part of a load-modify-store instruction.
6016 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6017 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6018 if (UI->getOpcode() == ISD::STORE)
6019 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006020 // Otherwise use a regular EFLAGS-setting instruction.
6021 switch (Op.getNode()->getOpcode()) {
6022 case ISD::SUB: Opcode = X86ISD::SUB; break;
6023 case ISD::OR: Opcode = X86ISD::OR; break;
6024 case ISD::XOR: Opcode = X86ISD::XOR; break;
6025 case ISD::AND: Opcode = X86ISD::AND; break;
6026 default: llvm_unreachable("unexpected operator!");
6027 }
Dan Gohman51bb4742009-03-05 21:29:28 +00006028 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006029 break;
6030 case X86ISD::ADD:
6031 case X86ISD::SUB:
6032 case X86ISD::INC:
6033 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006034 case X86ISD::OR:
6035 case X86ISD::XOR:
6036 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00006037 return SDValue(Op.getNode(), 1);
6038 default:
6039 default_case:
6040 break;
6041 }
6042 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006043 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00006044 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00006045 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00006046 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00006047 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00006048 DAG.ReplaceAllUsesWith(Op, New);
6049 return SDValue(New.getNode(), 1);
6050 }
6051 }
6052
6053 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00006054 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00006055 DAG.getConstant(0, Op.getValueType()));
6056}
6057
6058/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6059/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006060SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006061 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006062 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6063 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006064 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006065
6066 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006067 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006068}
6069
Evan Chengd40d03e2010-01-06 19:38:29 +00006070/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6071/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006072SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6073 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006074 SDValue Op0 = And.getOperand(0);
6075 SDValue Op1 = And.getOperand(1);
6076 if (Op0.getOpcode() == ISD::TRUNCATE)
6077 Op0 = Op0.getOperand(0);
6078 if (Op1.getOpcode() == ISD::TRUNCATE)
6079 Op1 = Op1.getOperand(0);
6080
Evan Chengd40d03e2010-01-06 19:38:29 +00006081 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00006082 if (Op1.getOpcode() == ISD::SHL) {
6083 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6084 if (And10C->getZExtValue() == 1) {
6085 LHS = Op0;
6086 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006087 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006088 } else if (Op0.getOpcode() == ISD::SHL) {
6089 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6090 if (And00C->getZExtValue() == 1) {
6091 LHS = Op1;
6092 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006093 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006094 } else if (Op1.getOpcode() == ISD::Constant) {
6095 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6096 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006097 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6098 LHS = AndLHS.getOperand(0);
6099 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006100 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006101 }
Evan Cheng0488db92007-09-25 01:57:46 +00006102
Evan Chengd40d03e2010-01-06 19:38:29 +00006103 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006104 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006105 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006106 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006107 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006108 // Also promote i16 to i32 for performance / code size reason.
6109 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006110 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006111 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006112
Evan Chengd40d03e2010-01-06 19:38:29 +00006113 // If the operand types disagree, extend the shift amount to match. Since
6114 // BT ignores high bits (like shifts) we can use anyextend.
6115 if (LHS.getValueType() != RHS.getValueType())
6116 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006117
Evan Chengd40d03e2010-01-06 19:38:29 +00006118 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6119 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6120 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6121 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006122 }
6123
Evan Cheng54de3ea2010-01-05 06:52:31 +00006124 return SDValue();
6125}
6126
Dan Gohmand858e902010-04-17 15:26:15 +00006127SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006128 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6129 SDValue Op0 = Op.getOperand(0);
6130 SDValue Op1 = Op.getOperand(1);
6131 DebugLoc dl = Op.getDebugLoc();
6132 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6133
6134 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006135 // Lower (X & (1 << N)) == 0 to BT(X, N).
6136 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6137 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6138 if (Op0.getOpcode() == ISD::AND &&
6139 Op0.hasOneUse() &&
6140 Op1.getOpcode() == ISD::Constant &&
6141 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6142 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6143 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6144 if (NewSetCC.getNode())
6145 return NewSetCC;
6146 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006147
Evan Cheng2c755ba2010-02-27 07:36:59 +00006148 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6149 if (Op0.getOpcode() == X86ISD::SETCC &&
6150 Op1.getOpcode() == ISD::Constant &&
6151 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6152 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6153 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6154 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6155 bool Invert = (CC == ISD::SETNE) ^
6156 cast<ConstantSDNode>(Op1)->isNullValue();
6157 if (Invert)
6158 CCode = X86::GetOppositeBranchCondition(CCode);
6159 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6160 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6161 }
6162
Evan Chenge5b51ac2010-04-17 06:13:15 +00006163 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006164 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006165 if (X86CC == X86::COND_INVALID)
6166 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006167
Evan Cheng552f09a2010-04-26 19:06:11 +00006168 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006169
6170 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006171 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006172 return DAG.getNode(ISD::AND, dl, MVT::i8,
6173 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6174 DAG.getConstant(X86CC, MVT::i8), Cond),
6175 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006176
Owen Anderson825b72b2009-08-11 20:47:22 +00006177 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6178 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006179}
6180
Dan Gohmand858e902010-04-17 15:26:15 +00006181SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006182 SDValue Cond;
6183 SDValue Op0 = Op.getOperand(0);
6184 SDValue Op1 = Op.getOperand(1);
6185 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006186 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006187 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6188 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006189 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006190
6191 if (isFP) {
6192 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006193 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006194 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6195 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006196 bool Swap = false;
6197
6198 switch (SetCCOpcode) {
6199 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006200 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006201 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006202 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006203 case ISD::SETGT: Swap = true; // Fallthrough
6204 case ISD::SETLT:
6205 case ISD::SETOLT: SSECC = 1; break;
6206 case ISD::SETOGE:
6207 case ISD::SETGE: Swap = true; // Fallthrough
6208 case ISD::SETLE:
6209 case ISD::SETOLE: SSECC = 2; break;
6210 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006211 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006212 case ISD::SETNE: SSECC = 4; break;
6213 case ISD::SETULE: Swap = true;
6214 case ISD::SETUGE: SSECC = 5; break;
6215 case ISD::SETULT: Swap = true;
6216 case ISD::SETUGT: SSECC = 6; break;
6217 case ISD::SETO: SSECC = 7; break;
6218 }
6219 if (Swap)
6220 std::swap(Op0, Op1);
6221
Nate Begemanfb8ead02008-07-25 19:05:58 +00006222 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006223 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006224 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006225 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006226 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6227 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006228 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006229 }
6230 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006231 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006232 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6233 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006234 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006235 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006236 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006237 }
6238 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006239 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006240 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006241
Nate Begeman30a0de92008-07-17 16:51:19 +00006242 // We are handling one of the integer comparisons here. Since SSE only has
6243 // GT and EQ comparisons for integer, swapping operands and multiple
6244 // operations may be required for some comparisons.
6245 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6246 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006247
Owen Anderson825b72b2009-08-11 20:47:22 +00006248 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006249 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006250 case MVT::v8i8:
6251 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6252 case MVT::v4i16:
6253 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6254 case MVT::v2i32:
6255 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6256 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006257 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006258
Nate Begeman30a0de92008-07-17 16:51:19 +00006259 switch (SetCCOpcode) {
6260 default: break;
6261 case ISD::SETNE: Invert = true;
6262 case ISD::SETEQ: Opc = EQOpc; break;
6263 case ISD::SETLT: Swap = true;
6264 case ISD::SETGT: Opc = GTOpc; break;
6265 case ISD::SETGE: Swap = true;
6266 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6267 case ISD::SETULT: Swap = true;
6268 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6269 case ISD::SETUGE: Swap = true;
6270 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6271 }
6272 if (Swap)
6273 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006274
Nate Begeman30a0de92008-07-17 16:51:19 +00006275 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6276 // bits of the inputs before performing those operations.
6277 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006278 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006279 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6280 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006281 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006282 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6283 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006284 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6285 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006286 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006287
Dale Johannesenace16102009-02-03 19:33:06 +00006288 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006289
6290 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006291 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006292 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006293
Nate Begeman30a0de92008-07-17 16:51:19 +00006294 return Result;
6295}
Evan Cheng0488db92007-09-25 01:57:46 +00006296
Evan Cheng370e5342008-12-03 08:38:43 +00006297// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006298static bool isX86LogicalCmp(SDValue Op) {
6299 unsigned Opc = Op.getNode()->getOpcode();
6300 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6301 return true;
6302 if (Op.getResNo() == 1 &&
6303 (Opc == X86ISD::ADD ||
6304 Opc == X86ISD::SUB ||
6305 Opc == X86ISD::SMUL ||
6306 Opc == X86ISD::UMUL ||
6307 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006308 Opc == X86ISD::DEC ||
6309 Opc == X86ISD::OR ||
6310 Opc == X86ISD::XOR ||
6311 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006312 return true;
6313
6314 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006315}
6316
Dan Gohmand858e902010-04-17 15:26:15 +00006317SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006318 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006319 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006320 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006321 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006322
Dan Gohman1a492952009-10-20 16:22:37 +00006323 if (Cond.getOpcode() == ISD::SETCC) {
6324 SDValue NewCond = LowerSETCC(Cond, DAG);
6325 if (NewCond.getNode())
6326 Cond = NewCond;
6327 }
Evan Cheng734503b2006-09-11 02:19:56 +00006328
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006329 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6330 SDValue Op1 = Op.getOperand(1);
6331 SDValue Op2 = Op.getOperand(2);
6332 if (Cond.getOpcode() == X86ISD::SETCC &&
6333 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6334 SDValue Cmp = Cond.getOperand(1);
6335 if (Cmp.getOpcode() == X86ISD::CMP) {
6336 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6337 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6338 ConstantSDNode *RHSC =
6339 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6340 if (N1C && N1C->isAllOnesValue() &&
6341 N2C && N2C->isNullValue() &&
6342 RHSC && RHSC->isNullValue()) {
6343 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006344 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006345 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6346 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6347 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6348 }
6349 }
6350 }
6351
Evan Chengad9c0a32009-12-15 00:53:42 +00006352 // Look pass (and (setcc_carry (cmp ...)), 1).
6353 if (Cond.getOpcode() == ISD::AND &&
6354 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6355 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6356 if (C && C->getAPIntValue() == 1)
6357 Cond = Cond.getOperand(0);
6358 }
6359
Evan Cheng3f41d662007-10-08 22:16:29 +00006360 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6361 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006362 if (Cond.getOpcode() == X86ISD::SETCC ||
6363 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006364 CC = Cond.getOperand(0);
6365
Dan Gohman475871a2008-07-27 21:46:04 +00006366 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006367 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006368 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006369
Evan Cheng3f41d662007-10-08 22:16:29 +00006370 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006371 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006372 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006373 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006374
Chris Lattnerd1980a52009-03-12 06:52:53 +00006375 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6376 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006377 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006378 addTest = false;
6379 }
6380 }
6381
6382 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006383 // Look pass the truncate.
6384 if (Cond.getOpcode() == ISD::TRUNCATE)
6385 Cond = Cond.getOperand(0);
6386
6387 // We know the result of AND is compared against zero. Try to match
6388 // it to BT.
6389 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6390 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6391 if (NewSetCC.getNode()) {
6392 CC = NewSetCC.getOperand(0);
6393 Cond = NewSetCC.getOperand(1);
6394 addTest = false;
6395 }
6396 }
6397 }
6398
6399 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006400 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006401 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006402 }
6403
Evan Cheng0488db92007-09-25 01:57:46 +00006404 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6405 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006406 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6407 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006408 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006409}
6410
Evan Cheng370e5342008-12-03 08:38:43 +00006411// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6412// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6413// from the AND / OR.
6414static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6415 Opc = Op.getOpcode();
6416 if (Opc != ISD::OR && Opc != ISD::AND)
6417 return false;
6418 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6419 Op.getOperand(0).hasOneUse() &&
6420 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6421 Op.getOperand(1).hasOneUse());
6422}
6423
Evan Cheng961d6d42009-02-02 08:19:07 +00006424// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6425// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006426static bool isXor1OfSetCC(SDValue Op) {
6427 if (Op.getOpcode() != ISD::XOR)
6428 return false;
6429 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6430 if (N1C && N1C->getAPIntValue() == 1) {
6431 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6432 Op.getOperand(0).hasOneUse();
6433 }
6434 return false;
6435}
6436
Dan Gohmand858e902010-04-17 15:26:15 +00006437SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006438 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006439 SDValue Chain = Op.getOperand(0);
6440 SDValue Cond = Op.getOperand(1);
6441 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006442 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006443 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006444
Dan Gohman1a492952009-10-20 16:22:37 +00006445 if (Cond.getOpcode() == ISD::SETCC) {
6446 SDValue NewCond = LowerSETCC(Cond, DAG);
6447 if (NewCond.getNode())
6448 Cond = NewCond;
6449 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006450#if 0
6451 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006452 else if (Cond.getOpcode() == X86ISD::ADD ||
6453 Cond.getOpcode() == X86ISD::SUB ||
6454 Cond.getOpcode() == X86ISD::SMUL ||
6455 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006456 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006457#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006458
Evan Chengad9c0a32009-12-15 00:53:42 +00006459 // Look pass (and (setcc_carry (cmp ...)), 1).
6460 if (Cond.getOpcode() == ISD::AND &&
6461 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6462 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6463 if (C && C->getAPIntValue() == 1)
6464 Cond = Cond.getOperand(0);
6465 }
6466
Evan Cheng3f41d662007-10-08 22:16:29 +00006467 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6468 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006469 if (Cond.getOpcode() == X86ISD::SETCC ||
6470 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006471 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006472
Dan Gohman475871a2008-07-27 21:46:04 +00006473 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006474 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006475 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006476 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006477 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006478 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006479 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006480 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006481 default: break;
6482 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006483 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006484 // These can only come from an arithmetic instruction with overflow,
6485 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006486 Cond = Cond.getNode()->getOperand(1);
6487 addTest = false;
6488 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006489 }
Evan Cheng0488db92007-09-25 01:57:46 +00006490 }
Evan Cheng370e5342008-12-03 08:38:43 +00006491 } else {
6492 unsigned CondOpc;
6493 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6494 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006495 if (CondOpc == ISD::OR) {
6496 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6497 // two branches instead of an explicit OR instruction with a
6498 // separate test.
6499 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006500 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006501 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006502 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006503 Chain, Dest, CC, Cmp);
6504 CC = Cond.getOperand(1).getOperand(0);
6505 Cond = Cmp;
6506 addTest = false;
6507 }
6508 } else { // ISD::AND
6509 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6510 // two branches instead of an explicit AND instruction with a
6511 // separate test. However, we only do this if this block doesn't
6512 // have a fall-through edge, because this requires an explicit
6513 // jmp when the condition is false.
6514 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006515 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006516 Op.getNode()->hasOneUse()) {
6517 X86::CondCode CCode =
6518 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6519 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006520 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006521 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6522 // Look for an unconditional branch following this conditional branch.
6523 // We need this because we need to reverse the successors in order
6524 // to implement FCMP_OEQ.
6525 if (User.getOpcode() == ISD::BR) {
6526 SDValue FalseBB = User.getOperand(1);
6527 SDValue NewBR =
6528 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6529 assert(NewBR == User);
6530 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006531
Dale Johannesene4d209d2009-02-03 20:21:25 +00006532 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006533 Chain, Dest, CC, Cmp);
6534 X86::CondCode CCode =
6535 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6536 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006537 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006538 Cond = Cmp;
6539 addTest = false;
6540 }
6541 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006542 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006543 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6544 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6545 // It should be transformed during dag combiner except when the condition
6546 // is set by a arithmetics with overflow node.
6547 X86::CondCode CCode =
6548 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6549 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006550 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006551 Cond = Cond.getOperand(0).getOperand(1);
6552 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006553 }
Evan Cheng0488db92007-09-25 01:57:46 +00006554 }
6555
6556 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006557 // Look pass the truncate.
6558 if (Cond.getOpcode() == ISD::TRUNCATE)
6559 Cond = Cond.getOperand(0);
6560
6561 // We know the result of AND is compared against zero. Try to match
6562 // it to BT.
6563 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6564 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6565 if (NewSetCC.getNode()) {
6566 CC = NewSetCC.getOperand(0);
6567 Cond = NewSetCC.getOperand(1);
6568 addTest = false;
6569 }
6570 }
6571 }
6572
6573 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006574 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006575 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006576 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006577 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006578 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006579}
6580
Anton Korobeynikove060b532007-04-17 19:34:00 +00006581
6582// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6583// Calls to _alloca is needed to probe the stack when allocating more than 4k
6584// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6585// that the guard pages used by the OS virtual memory manager are allocated in
6586// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006587SDValue
6588X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006589 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006590 assert(Subtarget->isTargetCygMing() &&
6591 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006592 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006593
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006594 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006595 SDValue Chain = Op.getOperand(0);
6596 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006597 // FIXME: Ensure alignment here
6598
Dan Gohman475871a2008-07-27 21:46:04 +00006599 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006600
Owen Andersone50ed302009-08-10 22:56:29 +00006601 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006602 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006603
Dale Johannesendd64c412009-02-04 00:33:20 +00006604 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006605 Flag = Chain.getValue(1);
6606
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006607 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006608
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006609 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6610 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006611
Dale Johannesendd64c412009-02-04 00:33:20 +00006612 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006613
Dan Gohman475871a2008-07-27 21:46:04 +00006614 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006615 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006616}
6617
Dan Gohman475871a2008-07-27 21:46:04 +00006618SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006619X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006620 SDValue Chain,
6621 SDValue Dst, SDValue Src,
6622 SDValue Size, unsigned Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006623 bool isVolatile,
Bill Wendling6f287b22008-09-30 21:22:07 +00006624 const Value *DstSV,
Dan Gohmand858e902010-04-17 15:26:15 +00006625 uint64_t DstSVOff) const {
Dan Gohman707e0182008-04-12 04:36:06 +00006626 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006627
Bill Wendling6f287b22008-09-30 21:22:07 +00006628 // If not DWORD aligned or size is more than the threshold, call the library.
6629 // The libc version is likely to be faster for these cases. It can use the
6630 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006631 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006632 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006633 ConstantSize->getZExtValue() >
6634 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006635 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006636
6637 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006638 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006639
Bill Wendling6158d842008-10-01 00:59:58 +00006640 if (const char *bzeroEntry = V &&
6641 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006642 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006643 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006644 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006645 TargetLowering::ArgListEntry Entry;
6646 Entry.Node = Dst;
6647 Entry.Ty = IntPtrTy;
6648 Args.push_back(Entry);
6649 Entry.Node = Size;
6650 Args.push_back(Entry);
6651 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006652 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6653 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006654 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling46ada192010-03-02 01:55:18 +00006655 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00006656 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006657 }
6658
Dan Gohman707e0182008-04-12 04:36:06 +00006659 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006660 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006661 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006662
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006663 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006664 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006665 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006666 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006667 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006668 unsigned BytesLeft = 0;
6669 bool TwoRepStos = false;
6670 if (ValC) {
6671 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006672 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006673
Evan Cheng0db9fe62006-04-25 20:13:52 +00006674 // If the value is a constant, then we can potentially use larger sets.
6675 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006676 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006677 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006678 ValReg = X86::AX;
6679 Val = (Val << 8) | Val;
6680 break;
6681 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006682 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006683 ValReg = X86::EAX;
6684 Val = (Val << 8) | Val;
6685 Val = (Val << 16) | Val;
6686 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006687 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006688 ValReg = X86::RAX;
6689 Val = (Val << 32) | Val;
6690 }
6691 break;
6692 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006693 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006694 ValReg = X86::AL;
6695 Count = DAG.getIntPtrConstant(SizeVal);
6696 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006697 }
6698
Owen Anderson825b72b2009-08-11 20:47:22 +00006699 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006700 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006701 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6702 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006703 }
6704
Dale Johannesen0f502f62009-02-03 22:26:09 +00006705 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006706 InFlag);
6707 InFlag = Chain.getValue(1);
6708 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006709 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006710 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006711 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006712 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006713 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006714
Scott Michelfdc40a02009-02-17 22:15:04 +00006715 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006716 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006717 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006718 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006719 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006720 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006721 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006722 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006723
Owen Anderson825b72b2009-08-11 20:47:22 +00006724 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006725 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6726 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006727
Evan Cheng0db9fe62006-04-25 20:13:52 +00006728 if (TwoRepStos) {
6729 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006730 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006731 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006732 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006733 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6734 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006735 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006736 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006737 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006738 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006739 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6740 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006741 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006742 // Handle the last 1 - 7 bytes.
6743 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006744 EVT AddrVT = Dst.getValueType();
6745 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006746
Dale Johannesen0f502f62009-02-03 22:26:09 +00006747 Chain = DAG.getMemset(Chain, dl,
6748 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006749 DAG.getConstant(Offset, AddrVT)),
6750 Src,
6751 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang20adc9d2010-04-04 03:10:48 +00006752 Align, isVolatile, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006753 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006754
Dan Gohman707e0182008-04-12 04:36:06 +00006755 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006756 return Chain;
6757}
Evan Cheng11e15b32006-04-03 20:53:28 +00006758
Dan Gohman475871a2008-07-27 21:46:04 +00006759SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006760X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006761 SDValue Chain, SDValue Dst, SDValue Src,
6762 SDValue Size, unsigned Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006763 bool isVolatile, bool AlwaysInline,
Dan Gohmand858e902010-04-17 15:26:15 +00006764 const Value *DstSV,
6765 uint64_t DstSVOff,
6766 const Value *SrcSV,
6767 uint64_t SrcSVOff) const {
Dan Gohman707e0182008-04-12 04:36:06 +00006768 // This requires the copy size to be a constant, preferrably
6769 // within a subtarget-specific limit.
6770 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6771 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006772 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006773 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006774 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006775 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006776
Evan Cheng1887c1c2008-08-21 21:00:15 +00006777 /// If not DWORD aligned, call the library.
6778 if ((Align & 3) != 0)
6779 return SDValue();
6780
6781 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006782 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006783 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006784 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006785
Duncan Sands83ec4b62008-06-06 12:08:01 +00006786 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006787 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006788 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006789 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006790
Dan Gohman475871a2008-07-27 21:46:04 +00006791 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006792 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006793 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006794 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006795 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006796 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Evan Chengc3b0c342010-04-08 07:37:57 +00006797 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006798 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006799 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006800 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006801 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006802 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006803 InFlag = Chain.getValue(1);
6804
Owen Anderson825b72b2009-08-11 20:47:22 +00006805 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006806 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6807 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6808 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006809
Dan Gohman475871a2008-07-27 21:46:04 +00006810 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006811 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006812 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006813 // Handle the last 1 - 7 bytes.
6814 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006815 EVT DstVT = Dst.getValueType();
6816 EVT SrcVT = Src.getValueType();
6817 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006818 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006819 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006820 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006821 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006822 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006823 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang20adc9d2010-04-04 03:10:48 +00006824 Align, isVolatile, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006825 DstSV, DstSVOff + Offset,
6826 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006827 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006828
Owen Anderson825b72b2009-08-11 20:47:22 +00006829 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006830 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006831}
6832
Dan Gohmand858e902010-04-17 15:26:15 +00006833SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006834 MachineFunction &MF = DAG.getMachineFunction();
6835 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6836
Dan Gohman69de1932008-02-06 22:27:42 +00006837 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006838 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006839
Evan Cheng25ab6902006-09-08 06:48:29 +00006840 if (!Subtarget->is64Bit()) {
6841 // vastart just stores the address of the VarArgsFrameIndex slot into the
6842 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006843 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6844 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006845 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6846 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006847 }
6848
6849 // __va_list_tag:
6850 // gp_offset (0 - 6 * 8)
6851 // fp_offset (48 - 48 + 8 * 16)
6852 // overflow_arg_area (point to parameters coming in memory).
6853 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006854 SmallVector<SDValue, 8> MemOps;
6855 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006856 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006857 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006858 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6859 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006860 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006861 MemOps.push_back(Store);
6862
6863 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006864 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006865 FIN, DAG.getIntPtrConstant(4));
6866 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006867 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6868 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006869 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006870 MemOps.push_back(Store);
6871
6872 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006873 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006874 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006875 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6876 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006877 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6878 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006879 MemOps.push_back(Store);
6880
6881 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006882 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006883 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006884 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6885 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006886 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6887 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006888 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006889 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006890 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006891}
6892
Dan Gohmand858e902010-04-17 15:26:15 +00006893SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006894 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6895 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006896 SDValue Chain = Op.getOperand(0);
6897 SDValue SrcPtr = Op.getOperand(1);
6898 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006899
Chris Lattner75361b62010-04-07 22:58:41 +00006900 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006901 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006902}
6903
Dan Gohmand858e902010-04-17 15:26:15 +00006904SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006905 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006906 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006907 SDValue Chain = Op.getOperand(0);
6908 SDValue DstPtr = Op.getOperand(1);
6909 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006910 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6911 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006912 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006913
Dale Johannesendd64c412009-02-04 00:33:20 +00006914 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006915 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6916 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006917}
6918
Dan Gohman475871a2008-07-27 21:46:04 +00006919SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006920X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006921 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006922 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006923 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006924 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006925 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006926 case Intrinsic::x86_sse_comieq_ss:
6927 case Intrinsic::x86_sse_comilt_ss:
6928 case Intrinsic::x86_sse_comile_ss:
6929 case Intrinsic::x86_sse_comigt_ss:
6930 case Intrinsic::x86_sse_comige_ss:
6931 case Intrinsic::x86_sse_comineq_ss:
6932 case Intrinsic::x86_sse_ucomieq_ss:
6933 case Intrinsic::x86_sse_ucomilt_ss:
6934 case Intrinsic::x86_sse_ucomile_ss:
6935 case Intrinsic::x86_sse_ucomigt_ss:
6936 case Intrinsic::x86_sse_ucomige_ss:
6937 case Intrinsic::x86_sse_ucomineq_ss:
6938 case Intrinsic::x86_sse2_comieq_sd:
6939 case Intrinsic::x86_sse2_comilt_sd:
6940 case Intrinsic::x86_sse2_comile_sd:
6941 case Intrinsic::x86_sse2_comigt_sd:
6942 case Intrinsic::x86_sse2_comige_sd:
6943 case Intrinsic::x86_sse2_comineq_sd:
6944 case Intrinsic::x86_sse2_ucomieq_sd:
6945 case Intrinsic::x86_sse2_ucomilt_sd:
6946 case Intrinsic::x86_sse2_ucomile_sd:
6947 case Intrinsic::x86_sse2_ucomigt_sd:
6948 case Intrinsic::x86_sse2_ucomige_sd:
6949 case Intrinsic::x86_sse2_ucomineq_sd: {
6950 unsigned Opc = 0;
6951 ISD::CondCode CC = ISD::SETCC_INVALID;
6952 switch (IntNo) {
6953 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006954 case Intrinsic::x86_sse_comieq_ss:
6955 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006956 Opc = X86ISD::COMI;
6957 CC = ISD::SETEQ;
6958 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006959 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006960 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006961 Opc = X86ISD::COMI;
6962 CC = ISD::SETLT;
6963 break;
6964 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006965 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006966 Opc = X86ISD::COMI;
6967 CC = ISD::SETLE;
6968 break;
6969 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006970 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006971 Opc = X86ISD::COMI;
6972 CC = ISD::SETGT;
6973 break;
6974 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006975 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006976 Opc = X86ISD::COMI;
6977 CC = ISD::SETGE;
6978 break;
6979 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006980 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006981 Opc = X86ISD::COMI;
6982 CC = ISD::SETNE;
6983 break;
6984 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006985 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006986 Opc = X86ISD::UCOMI;
6987 CC = ISD::SETEQ;
6988 break;
6989 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006990 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006991 Opc = X86ISD::UCOMI;
6992 CC = ISD::SETLT;
6993 break;
6994 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006995 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006996 Opc = X86ISD::UCOMI;
6997 CC = ISD::SETLE;
6998 break;
6999 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007000 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007001 Opc = X86ISD::UCOMI;
7002 CC = ISD::SETGT;
7003 break;
7004 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007005 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007006 Opc = X86ISD::UCOMI;
7007 CC = ISD::SETGE;
7008 break;
7009 case Intrinsic::x86_sse_ucomineq_ss:
7010 case Intrinsic::x86_sse2_ucomineq_sd:
7011 Opc = X86ISD::UCOMI;
7012 CC = ISD::SETNE;
7013 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007014 }
Evan Cheng734503b2006-09-11 02:19:56 +00007015
Dan Gohman475871a2008-07-27 21:46:04 +00007016 SDValue LHS = Op.getOperand(1);
7017 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007018 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007019 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007020 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7021 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7022 DAG.getConstant(X86CC, MVT::i8), Cond);
7023 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007024 }
Eric Christopher71c67532009-07-29 00:28:05 +00007025 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00007026 // an integer value, not just an instruction so lower it to the ptest
7027 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007028 case Intrinsic::x86_sse41_ptestz:
7029 case Intrinsic::x86_sse41_ptestc:
7030 case Intrinsic::x86_sse41_ptestnzc:{
7031 unsigned X86CC = 0;
7032 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007033 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00007034 case Intrinsic::x86_sse41_ptestz:
7035 // ZF = 1
7036 X86CC = X86::COND_E;
7037 break;
7038 case Intrinsic::x86_sse41_ptestc:
7039 // CF = 1
7040 X86CC = X86::COND_B;
7041 break;
Eric Christopherfd179292009-08-27 18:07:15 +00007042 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00007043 // ZF and CF = 0
7044 X86CC = X86::COND_A;
7045 break;
7046 }
Eric Christopherfd179292009-08-27 18:07:15 +00007047
Eric Christopher71c67532009-07-29 00:28:05 +00007048 SDValue LHS = Op.getOperand(1);
7049 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
7051 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7052 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7053 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007054 }
Evan Cheng5759f972008-05-04 09:15:50 +00007055
7056 // Fix vector shift instructions where the last operand is a non-immediate
7057 // i32 value.
7058 case Intrinsic::x86_sse2_pslli_w:
7059 case Intrinsic::x86_sse2_pslli_d:
7060 case Intrinsic::x86_sse2_pslli_q:
7061 case Intrinsic::x86_sse2_psrli_w:
7062 case Intrinsic::x86_sse2_psrli_d:
7063 case Intrinsic::x86_sse2_psrli_q:
7064 case Intrinsic::x86_sse2_psrai_w:
7065 case Intrinsic::x86_sse2_psrai_d:
7066 case Intrinsic::x86_mmx_pslli_w:
7067 case Intrinsic::x86_mmx_pslli_d:
7068 case Intrinsic::x86_mmx_pslli_q:
7069 case Intrinsic::x86_mmx_psrli_w:
7070 case Intrinsic::x86_mmx_psrli_d:
7071 case Intrinsic::x86_mmx_psrli_q:
7072 case Intrinsic::x86_mmx_psrai_w:
7073 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007074 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007075 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007076 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007077
7078 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007080 switch (IntNo) {
7081 case Intrinsic::x86_sse2_pslli_w:
7082 NewIntNo = Intrinsic::x86_sse2_psll_w;
7083 break;
7084 case Intrinsic::x86_sse2_pslli_d:
7085 NewIntNo = Intrinsic::x86_sse2_psll_d;
7086 break;
7087 case Intrinsic::x86_sse2_pslli_q:
7088 NewIntNo = Intrinsic::x86_sse2_psll_q;
7089 break;
7090 case Intrinsic::x86_sse2_psrli_w:
7091 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7092 break;
7093 case Intrinsic::x86_sse2_psrli_d:
7094 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7095 break;
7096 case Intrinsic::x86_sse2_psrli_q:
7097 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7098 break;
7099 case Intrinsic::x86_sse2_psrai_w:
7100 NewIntNo = Intrinsic::x86_sse2_psra_w;
7101 break;
7102 case Intrinsic::x86_sse2_psrai_d:
7103 NewIntNo = Intrinsic::x86_sse2_psra_d;
7104 break;
7105 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007106 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007107 switch (IntNo) {
7108 case Intrinsic::x86_mmx_pslli_w:
7109 NewIntNo = Intrinsic::x86_mmx_psll_w;
7110 break;
7111 case Intrinsic::x86_mmx_pslli_d:
7112 NewIntNo = Intrinsic::x86_mmx_psll_d;
7113 break;
7114 case Intrinsic::x86_mmx_pslli_q:
7115 NewIntNo = Intrinsic::x86_mmx_psll_q;
7116 break;
7117 case Intrinsic::x86_mmx_psrli_w:
7118 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7119 break;
7120 case Intrinsic::x86_mmx_psrli_d:
7121 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7122 break;
7123 case Intrinsic::x86_mmx_psrli_q:
7124 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7125 break;
7126 case Intrinsic::x86_mmx_psrai_w:
7127 NewIntNo = Intrinsic::x86_mmx_psra_w;
7128 break;
7129 case Intrinsic::x86_mmx_psrai_d:
7130 NewIntNo = Intrinsic::x86_mmx_psra_d;
7131 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007132 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007133 }
7134 break;
7135 }
7136 }
Mon P Wangefa42202009-09-03 19:56:25 +00007137
7138 // The vector shift intrinsics with scalars uses 32b shift amounts but
7139 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7140 // to be zero.
7141 SDValue ShOps[4];
7142 ShOps[0] = ShAmt;
7143 ShOps[1] = DAG.getConstant(0, MVT::i32);
7144 if (ShAmtVT == MVT::v4i32) {
7145 ShOps[2] = DAG.getUNDEF(MVT::i32);
7146 ShOps[3] = DAG.getUNDEF(MVT::i32);
7147 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7148 } else {
7149 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7150 }
7151
Owen Andersone50ed302009-08-10 22:56:29 +00007152 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007153 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007154 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007155 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007156 Op.getOperand(1), ShAmt);
7157 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007158 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007159}
Evan Cheng72261582005-12-20 06:22:03 +00007160
Dan Gohmand858e902010-04-17 15:26:15 +00007161SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
7162 SelectionDAG &DAG) const {
Bill Wendling64e87322009-01-16 19:25:27 +00007163 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007164 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007165
7166 if (Depth > 0) {
7167 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7168 SDValue Offset =
7169 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007170 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007171 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007172 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007173 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007174 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007175 }
7176
7177 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007178 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007179 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007180 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007181}
7182
Dan Gohmand858e902010-04-17 15:26:15 +00007183SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007184 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7185 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00007186 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007187 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007188 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7189 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007190 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007191 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007192 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7193 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007194 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007195}
7196
Dan Gohman475871a2008-07-27 21:46:04 +00007197SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007198 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007199 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007200}
7201
Dan Gohmand858e902010-04-17 15:26:15 +00007202SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007203 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007204 SDValue Chain = Op.getOperand(0);
7205 SDValue Offset = Op.getOperand(1);
7206 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007207 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007208
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007209 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7210 getPointerTy());
7211 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007212
Dale Johannesene4d209d2009-02-03 20:21:25 +00007213 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007214 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007215 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007216 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007217 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007218 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007219
Dale Johannesene4d209d2009-02-03 20:21:25 +00007220 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007221 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007222 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007223}
7224
Dan Gohman475871a2008-07-27 21:46:04 +00007225SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007226 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007227 SDValue Root = Op.getOperand(0);
7228 SDValue Trmp = Op.getOperand(1); // trampoline
7229 SDValue FPtr = Op.getOperand(2); // nested function
7230 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007231 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007232
Dan Gohman69de1932008-02-06 22:27:42 +00007233 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007234
7235 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007236 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007237
7238 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007239 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7240 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007241
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007242 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7243 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007244
7245 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7246
7247 // Load the pointer to the nested function into R11.
7248 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007249 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007250 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007251 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007252
Owen Anderson825b72b2009-08-11 20:47:22 +00007253 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7254 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007255 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7256 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007257
7258 // Load the 'nest' parameter value into R10.
7259 // R10 is specified in X86CallingConv.td
7260 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007261 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7262 DAG.getConstant(10, MVT::i64));
7263 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007264 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007265
Owen Anderson825b72b2009-08-11 20:47:22 +00007266 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7267 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007268 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7269 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007270
7271 // Jump to the nested function.
7272 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007273 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7274 DAG.getConstant(20, MVT::i64));
7275 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007276 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007277
7278 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007279 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7280 DAG.getConstant(22, MVT::i64));
7281 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007282 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007283
Dan Gohman475871a2008-07-27 21:46:04 +00007284 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007285 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007286 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007287 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007288 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007289 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007290 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007291 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007292
7293 switch (CC) {
7294 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007295 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007296 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007297 case CallingConv::X86_StdCall: {
7298 // Pass 'nest' parameter in ECX.
7299 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007300 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007301
7302 // Check that ECX wasn't needed by an 'inreg' parameter.
7303 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007304 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007305
Chris Lattner58d74912008-03-12 17:45:29 +00007306 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007307 unsigned InRegCount = 0;
7308 unsigned Idx = 1;
7309
7310 for (FunctionType::param_iterator I = FTy->param_begin(),
7311 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007312 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007313 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007314 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007315
7316 if (InRegCount > 2) {
Chris Lattner75361b62010-04-07 22:58:41 +00007317 report_fatal_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007318 }
7319 }
7320 break;
7321 }
7322 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007323 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007324 // Pass 'nest' parameter in EAX.
7325 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007326 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007327 break;
7328 }
7329
Dan Gohman475871a2008-07-27 21:46:04 +00007330 SDValue OutChains[4];
7331 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007332
Owen Anderson825b72b2009-08-11 20:47:22 +00007333 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7334 DAG.getConstant(10, MVT::i32));
7335 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007336
Chris Lattnera62fe662010-02-05 19:20:30 +00007337 // This is storing the opcode for MOV32ri.
7338 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007339 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007340 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007341 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007342 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007343
Owen Anderson825b72b2009-08-11 20:47:22 +00007344 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7345 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007346 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7347 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007348
Chris Lattnera62fe662010-02-05 19:20:30 +00007349 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007350 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7351 DAG.getConstant(5, MVT::i32));
7352 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007353 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007354
Owen Anderson825b72b2009-08-11 20:47:22 +00007355 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7356 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007357 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7358 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007359
Dan Gohman475871a2008-07-27 21:46:04 +00007360 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007361 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007362 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007363 }
7364}
7365
Dan Gohmand858e902010-04-17 15:26:15 +00007366SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7367 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007368 /*
7369 The rounding mode is in bits 11:10 of FPSR, and has the following
7370 settings:
7371 00 Round to nearest
7372 01 Round to -inf
7373 10 Round to +inf
7374 11 Round to 0
7375
7376 FLT_ROUNDS, on the other hand, expects the following:
7377 -1 Undefined
7378 0 Round to 0
7379 1 Round to nearest
7380 2 Round to +inf
7381 3 Round to -inf
7382
7383 To perform the conversion, we do:
7384 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7385 */
7386
7387 MachineFunction &MF = DAG.getMachineFunction();
7388 const TargetMachine &TM = MF.getTarget();
7389 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7390 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007391 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007392 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007393
7394 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007395 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007396 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007397
Owen Anderson825b72b2009-08-11 20:47:22 +00007398 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007399 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007400
7401 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007402 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7403 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007404
7405 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007406 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007407 DAG.getNode(ISD::SRL, dl, MVT::i16,
7408 DAG.getNode(ISD::AND, dl, MVT::i16,
7409 CWD, DAG.getConstant(0x800, MVT::i16)),
7410 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007411 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007412 DAG.getNode(ISD::SRL, dl, MVT::i16,
7413 DAG.getNode(ISD::AND, dl, MVT::i16,
7414 CWD, DAG.getConstant(0x400, MVT::i16)),
7415 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007416
Dan Gohman475871a2008-07-27 21:46:04 +00007417 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007418 DAG.getNode(ISD::AND, dl, MVT::i16,
7419 DAG.getNode(ISD::ADD, dl, MVT::i16,
7420 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7421 DAG.getConstant(1, MVT::i16)),
7422 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007423
7424
Duncan Sands83ec4b62008-06-06 12:08:01 +00007425 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007426 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007427}
7428
Dan Gohmand858e902010-04-17 15:26:15 +00007429SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007430 EVT VT = Op.getValueType();
7431 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007432 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007433 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007434
7435 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007436 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007437 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007438 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007439 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007440 }
Evan Cheng18efe262007-12-14 02:13:44 +00007441
Evan Cheng152804e2007-12-14 08:30:15 +00007442 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007443 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007444 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007445
7446 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007447 SDValue Ops[] = {
7448 Op,
7449 DAG.getConstant(NumBits+NumBits-1, OpVT),
7450 DAG.getConstant(X86::COND_E, MVT::i8),
7451 Op.getValue(1)
7452 };
7453 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007454
7455 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007456 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007457
Owen Anderson825b72b2009-08-11 20:47:22 +00007458 if (VT == MVT::i8)
7459 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007460 return Op;
7461}
7462
Dan Gohmand858e902010-04-17 15:26:15 +00007463SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007464 EVT VT = Op.getValueType();
7465 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007466 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007467 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007468
7469 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007470 if (VT == MVT::i8) {
7471 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007472 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007473 }
Evan Cheng152804e2007-12-14 08:30:15 +00007474
7475 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007476 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007477 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007478
7479 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007480 SDValue Ops[] = {
7481 Op,
7482 DAG.getConstant(NumBits, OpVT),
7483 DAG.getConstant(X86::COND_E, MVT::i8),
7484 Op.getValue(1)
7485 };
7486 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007487
Owen Anderson825b72b2009-08-11 20:47:22 +00007488 if (VT == MVT::i8)
7489 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007490 return Op;
7491}
7492
Dan Gohmand858e902010-04-17 15:26:15 +00007493SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007494 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007495 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007496 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007497
Mon P Wangaf9b9522008-12-18 21:42:19 +00007498 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7499 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7500 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7501 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7502 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7503 //
7504 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7505 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7506 // return AloBlo + AloBhi + AhiBlo;
7507
7508 SDValue A = Op.getOperand(0);
7509 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007510
Dale Johannesene4d209d2009-02-03 20:21:25 +00007511 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007512 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7513 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007514 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007515 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7516 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007517 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007518 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007519 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007520 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007521 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007522 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007523 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007524 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007525 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007526 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007527 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7528 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007529 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007530 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7531 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007532 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7533 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007534 return Res;
7535}
7536
7537
Dan Gohmand858e902010-04-17 15:26:15 +00007538SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007539 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7540 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007541 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7542 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007543 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007544 SDValue LHS = N->getOperand(0);
7545 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007546 unsigned BaseOp = 0;
7547 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007548 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007549
7550 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007551 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007552 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007553 // A subtract of one will be selected as a INC. Note that INC doesn't
7554 // set CF, so we can't do this for UADDO.
7555 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7556 if (C->getAPIntValue() == 1) {
7557 BaseOp = X86ISD::INC;
7558 Cond = X86::COND_O;
7559 break;
7560 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007561 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007562 Cond = X86::COND_O;
7563 break;
7564 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007565 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007566 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007567 break;
7568 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007569 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7570 // set CF, so we can't do this for USUBO.
7571 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7572 if (C->getAPIntValue() == 1) {
7573 BaseOp = X86ISD::DEC;
7574 Cond = X86::COND_O;
7575 break;
7576 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007577 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007578 Cond = X86::COND_O;
7579 break;
7580 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007581 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007582 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007583 break;
7584 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007585 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007586 Cond = X86::COND_O;
7587 break;
7588 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007589 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007590 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007591 break;
7592 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007593
Bill Wendling61edeb52008-12-02 01:06:39 +00007594 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007595 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007596 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007597
Bill Wendling61edeb52008-12-02 01:06:39 +00007598 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007599 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007600 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007601
Bill Wendling61edeb52008-12-02 01:06:39 +00007602 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7603 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007604}
7605
Dan Gohmand858e902010-04-17 15:26:15 +00007606SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007607 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007608 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007609 unsigned Reg = 0;
7610 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007611 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007612 default:
7613 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007614 case MVT::i8: Reg = X86::AL; size = 1; break;
7615 case MVT::i16: Reg = X86::AX; size = 2; break;
7616 case MVT::i32: Reg = X86::EAX; size = 4; break;
7617 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007618 assert(Subtarget->is64Bit() && "Node not type legal!");
7619 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007620 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007621 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007622 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007623 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007624 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007625 Op.getOperand(1),
7626 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007627 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007628 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007629 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007630 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007631 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007632 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007633 return cpOut;
7634}
7635
Duncan Sands1607f052008-12-01 11:39:25 +00007636SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007637 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007638 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007639 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007640 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007641 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007642 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007643 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7644 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007645 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007646 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7647 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007648 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007649 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007650 rdx.getValue(1)
7651 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007652 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007653}
7654
Dan Gohmand858e902010-04-17 15:26:15 +00007655SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007656 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007657 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007658 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007659 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007660 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007661 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007662 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007663 Node->getOperand(0),
7664 Node->getOperand(1), negOp,
7665 cast<AtomicSDNode>(Node)->getSrcValue(),
7666 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007667}
7668
Evan Cheng0db9fe62006-04-25 20:13:52 +00007669/// LowerOperation - Provide custom lowering hooks for some operations.
7670///
Dan Gohmand858e902010-04-17 15:26:15 +00007671SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007672 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007673 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007674 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7675 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007676 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007677 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007678 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7679 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7680 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7681 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7682 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7683 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007684 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007685 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007686 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007687 case ISD::SHL_PARTS:
7688 case ISD::SRA_PARTS:
7689 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7690 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007691 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007692 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007693 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007694 case ISD::FABS: return LowerFABS(Op, DAG);
7695 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007696 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007697 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007698 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007699 case ISD::SELECT: return LowerSELECT(Op, DAG);
7700 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007701 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007702 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007703 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007704 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007705 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007706 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7707 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007708 case ISD::FRAME_TO_ARGS_OFFSET:
7709 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007710 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007711 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007712 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007713 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007714 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7715 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007716 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007717 case ISD::SADDO:
7718 case ISD::UADDO:
7719 case ISD::SSUBO:
7720 case ISD::USUBO:
7721 case ISD::SMULO:
7722 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007723 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007724 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007725}
7726
Duncan Sands1607f052008-12-01 11:39:25 +00007727void X86TargetLowering::
7728ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007729 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007730 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007731 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007732 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007733
7734 SDValue Chain = Node->getOperand(0);
7735 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007736 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007737 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007738 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007739 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007740 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007741 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007742 SDValue Result =
7743 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7744 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007745 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007746 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007747 Results.push_back(Result.getValue(2));
7748}
7749
Duncan Sands126d9072008-07-04 11:47:58 +00007750/// ReplaceNodeResults - Replace a node with an illegal result type
7751/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007752void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7753 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007754 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007755 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007756 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007757 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007758 assert(false && "Do not know how to custom type legalize this operation!");
7759 return;
7760 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007761 std::pair<SDValue,SDValue> Vals =
7762 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007763 SDValue FIST = Vals.first, StackSlot = Vals.second;
7764 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007765 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007766 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007767 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7768 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007769 }
7770 return;
7771 }
7772 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007773 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007774 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007775 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007776 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007777 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007778 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007779 eax.getValue(2));
7780 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7781 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007782 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007783 Results.push_back(edx.getValue(1));
7784 return;
7785 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007786 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007787 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007788 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007789 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007790 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7791 DAG.getConstant(0, MVT::i32));
7792 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7793 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007794 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7795 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007796 cpInL.getValue(1));
7797 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007798 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7799 DAG.getConstant(0, MVT::i32));
7800 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7801 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007802 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007803 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007804 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007805 swapInL.getValue(1));
7806 SDValue Ops[] = { swapInH.getValue(0),
7807 N->getOperand(1),
7808 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007809 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007810 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007811 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007812 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007813 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007814 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007815 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007816 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007817 Results.push_back(cpOutH.getValue(1));
7818 return;
7819 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007820 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007821 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7822 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007823 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007824 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7825 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007826 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007827 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7828 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007829 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007830 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7831 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007832 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007833 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7834 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007835 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007836 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7837 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007838 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007839 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7840 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007841 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007842}
7843
Evan Cheng72261582005-12-20 06:22:03 +00007844const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7845 switch (Opcode) {
7846 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007847 case X86ISD::BSF: return "X86ISD::BSF";
7848 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007849 case X86ISD::SHLD: return "X86ISD::SHLD";
7850 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007851 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007852 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007853 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007854 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007855 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007856 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007857 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7858 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7859 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007860 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007861 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007862 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007863 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007864 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007865 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007866 case X86ISD::COMI: return "X86ISD::COMI";
7867 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007868 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007869 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007870 case X86ISD::CMOV: return "X86ISD::CMOV";
7871 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007872 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007873 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7874 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007875 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007876 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007877 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007878 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007879 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007880 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7881 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007882 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007883 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007884 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007885 case X86ISD::FMAX: return "X86ISD::FMAX";
7886 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007887 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7888 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007889 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007890 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007891 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007892 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007893 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007894 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7895 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007896 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7897 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7898 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7899 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7900 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7901 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007902 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7903 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007904 case X86ISD::VSHL: return "X86ISD::VSHL";
7905 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007906 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7907 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7908 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7909 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7910 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7911 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7912 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7913 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7914 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7915 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007916 case X86ISD::ADD: return "X86ISD::ADD";
7917 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007918 case X86ISD::SMUL: return "X86ISD::SMUL";
7919 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007920 case X86ISD::INC: return "X86ISD::INC";
7921 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007922 case X86ISD::OR: return "X86ISD::OR";
7923 case X86ISD::XOR: return "X86ISD::XOR";
7924 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007925 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007926 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007927 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007928 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007929 }
7930}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007931
Chris Lattnerc9addb72007-03-30 23:15:24 +00007932// isLegalAddressingMode - Return true if the addressing mode represented
7933// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007934bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007935 const Type *Ty) const {
7936 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007937 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007938
Chris Lattnerc9addb72007-03-30 23:15:24 +00007939 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007940 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007941 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007942
Chris Lattnerc9addb72007-03-30 23:15:24 +00007943 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007944 unsigned GVFlags =
7945 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007946
Chris Lattnerdfed4132009-07-10 07:38:24 +00007947 // If a reference to this global requires an extra load, we can't fold it.
7948 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007949 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007950
Chris Lattnerdfed4132009-07-10 07:38:24 +00007951 // If BaseGV requires a register for the PIC base, we cannot also have a
7952 // BaseReg specified.
7953 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007954 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007955
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007956 // If lower 4G is not available, then we must use rip-relative addressing.
7957 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7958 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007959 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007960
Chris Lattnerc9addb72007-03-30 23:15:24 +00007961 switch (AM.Scale) {
7962 case 0:
7963 case 1:
7964 case 2:
7965 case 4:
7966 case 8:
7967 // These scales always work.
7968 break;
7969 case 3:
7970 case 5:
7971 case 9:
7972 // These scales are formed with basereg+scalereg. Only accept if there is
7973 // no basereg yet.
7974 if (AM.HasBaseReg)
7975 return false;
7976 break;
7977 default: // Other stuff never works.
7978 return false;
7979 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007980
Chris Lattnerc9addb72007-03-30 23:15:24 +00007981 return true;
7982}
7983
7984
Evan Cheng2bd122c2007-10-26 01:56:11 +00007985bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007986 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007987 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007988 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7989 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007990 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007991 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007992 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007993}
7994
Owen Andersone50ed302009-08-10 22:56:29 +00007995bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007996 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007997 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007998 unsigned NumBits1 = VT1.getSizeInBits();
7999 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00008000 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008001 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00008002 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00008003}
Evan Cheng2bd122c2007-10-26 01:56:11 +00008004
Dan Gohman97121ba2009-04-08 00:15:30 +00008005bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008006 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00008007 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008008}
8009
Owen Andersone50ed302009-08-10 22:56:29 +00008010bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00008011 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00008012 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00008013}
8014
Owen Andersone50ed302009-08-10 22:56:29 +00008015bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00008016 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00008017 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00008018}
8019
Evan Cheng60c07e12006-07-05 22:17:51 +00008020/// isShuffleMaskLegal - Targets can use this to indicate that they only
8021/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
8022/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
8023/// are assumed to be legal.
8024bool
Eric Christopherfd179292009-08-27 18:07:15 +00008025X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00008026 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00008027 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00008028 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00008029 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00008030
Nate Begemana09008b2009-10-19 02:17:23 +00008031 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00008032 return (VT.getVectorNumElements() == 2 ||
8033 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
8034 isMOVLMask(M, VT) ||
8035 isSHUFPMask(M, VT) ||
8036 isPSHUFDMask(M, VT) ||
8037 isPSHUFHWMask(M, VT) ||
8038 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00008039 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00008040 isUNPCKLMask(M, VT) ||
8041 isUNPCKHMask(M, VT) ||
8042 isUNPCKL_v_undef_Mask(M, VT) ||
8043 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008044}
8045
Dan Gohman7d8143f2008-04-09 20:09:42 +00008046bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00008047X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00008048 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00008049 unsigned NumElts = VT.getVectorNumElements();
8050 // FIXME: This collection of masks seems suspect.
8051 if (NumElts == 2)
8052 return true;
8053 if (NumElts == 4 && VT.getSizeInBits() == 128) {
8054 return (isMOVLMask(Mask, VT) ||
8055 isCommutedMOVLMask(Mask, VT, true) ||
8056 isSHUFPMask(Mask, VT) ||
8057 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008058 }
8059 return false;
8060}
8061
8062//===----------------------------------------------------------------------===//
8063// X86 Scheduler Hooks
8064//===----------------------------------------------------------------------===//
8065
Mon P Wang63307c32008-05-05 19:05:59 +00008066// private utility function
8067MachineBasicBlock *
8068X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
8069 MachineBasicBlock *MBB,
8070 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008071 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008072 unsigned LoadOpc,
8073 unsigned CXchgOpc,
8074 unsigned copyOpc,
8075 unsigned notOpc,
8076 unsigned EAXreg,
8077 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008078 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008079 // For the atomic bitwise operator, we generate
8080 // thisMBB:
8081 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008082 // ld t1 = [bitinstr.addr]
8083 // op t2 = t1, [bitinstr.val]
8084 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008085 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8086 // bz newMBB
8087 // fallthrough -->nextMBB
8088 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8089 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008090 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008091 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008092
Mon P Wang63307c32008-05-05 19:05:59 +00008093 /// First build the CFG
8094 MachineFunction *F = MBB->getParent();
8095 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008096 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8097 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8098 F->insert(MBBIter, newMBB);
8099 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008100
Mon P Wang63307c32008-05-05 19:05:59 +00008101 // Move all successors to thisMBB to nextMBB
8102 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008103
Mon P Wang63307c32008-05-05 19:05:59 +00008104 // Update thisMBB to fall through to newMBB
8105 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008106
Mon P Wang63307c32008-05-05 19:05:59 +00008107 // newMBB jumps to itself and fall through to nextMBB
8108 newMBB->addSuccessor(nextMBB);
8109 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008110
Mon P Wang63307c32008-05-05 19:05:59 +00008111 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008112 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008113 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00008114 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008115 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008116 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008117 int numArgs = bInstr->getNumOperands() - 1;
8118 for (int i=0; i < numArgs; ++i)
8119 argOpers[i] = &bInstr->getOperand(i+1);
8120
8121 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008122 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8123 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008124
Dale Johannesen140be2d2008-08-19 18:47:28 +00008125 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008126 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008127 for (int i=0; i <= lastAddrIndx; ++i)
8128 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008129
Dale Johannesen140be2d2008-08-19 18:47:28 +00008130 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008131 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008132 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008133 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008134 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008135 tt = t1;
8136
Dale Johannesen140be2d2008-08-19 18:47:28 +00008137 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00008138 assert((argOpers[valArgIndx]->isReg() ||
8139 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008140 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00008141 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008142 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008143 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008144 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008145 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00008146 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008147
Dale Johannesene4d209d2009-02-03 20:21:25 +00008148 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008149 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008150
Dale Johannesene4d209d2009-02-03 20:21:25 +00008151 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008152 for (int i=0; i <= lastAddrIndx; ++i)
8153 (*MIB).addOperand(*argOpers[i]);
8154 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008155 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008156 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8157 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008158
Dale Johannesene4d209d2009-02-03 20:21:25 +00008159 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008160 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008161
Mon P Wang63307c32008-05-05 19:05:59 +00008162 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008163 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008164
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008165 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008166 return nextMBB;
8167}
8168
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008169// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008170MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008171X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8172 MachineBasicBlock *MBB,
8173 unsigned regOpcL,
8174 unsigned regOpcH,
8175 unsigned immOpcL,
8176 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008177 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008178 // For the atomic bitwise operator, we generate
8179 // thisMBB (instructions are in pairs, except cmpxchg8b)
8180 // ld t1,t2 = [bitinstr.addr]
8181 // newMBB:
8182 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8183 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008184 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008185 // mov ECX, EBX <- t5, t6
8186 // mov EAX, EDX <- t1, t2
8187 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8188 // mov t3, t4 <- EAX, EDX
8189 // bz newMBB
8190 // result in out1, out2
8191 // fallthrough -->nextMBB
8192
8193 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8194 const unsigned LoadOpc = X86::MOV32rm;
8195 const unsigned copyOpc = X86::MOV32rr;
8196 const unsigned NotOpc = X86::NOT32r;
8197 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8198 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8199 MachineFunction::iterator MBBIter = MBB;
8200 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008201
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008202 /// First build the CFG
8203 MachineFunction *F = MBB->getParent();
8204 MachineBasicBlock *thisMBB = MBB;
8205 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8206 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8207 F->insert(MBBIter, newMBB);
8208 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008209
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008210 // Move all successors to thisMBB to nextMBB
8211 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008212
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008213 // Update thisMBB to fall through to newMBB
8214 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008215
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008216 // newMBB jumps to itself and fall through to nextMBB
8217 newMBB->addSuccessor(nextMBB);
8218 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008219
Dale Johannesene4d209d2009-02-03 20:21:25 +00008220 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008221 // Insert instructions into newMBB based on incoming instruction
8222 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008223 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008224 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008225 MachineOperand& dest1Oper = bInstr->getOperand(0);
8226 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008227 MachineOperand* argOpers[2 + X86AddrNumOperands];
8228 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008229 argOpers[i] = &bInstr->getOperand(i+2);
8230
Evan Chengad5b52f2010-01-08 19:14:57 +00008231 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008232 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008233
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008234 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008235 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008236 for (int i=0; i <= lastAddrIndx; ++i)
8237 (*MIB).addOperand(*argOpers[i]);
8238 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008239 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008240 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008241 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008242 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008243 MachineOperand newOp3 = *(argOpers[3]);
8244 if (newOp3.isImm())
8245 newOp3.setImm(newOp3.getImm()+4);
8246 else
8247 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008248 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008249 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008250
8251 // t3/4 are defined later, at the bottom of the loop
8252 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8253 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008254 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008255 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008256 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008257 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8258
Evan Cheng306b4ca2010-01-08 23:41:50 +00008259 // The subsequent operations should be using the destination registers of
8260 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008261 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008262 t1 = F->getRegInfo().createVirtualRegister(RC);
8263 t2 = F->getRegInfo().createVirtualRegister(RC);
8264 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8265 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008266 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008267 t1 = dest1Oper.getReg();
8268 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008269 }
8270
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008271 int valArgIndx = lastAddrIndx + 1;
8272 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008273 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008274 "invalid operand");
8275 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8276 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008277 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008278 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008279 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008280 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008281 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008282 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008283 (*MIB).addOperand(*argOpers[valArgIndx]);
8284 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008285 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008286 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008287 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008288 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008289 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008290 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008291 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008292 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008293 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008294 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008295
Dale Johannesene4d209d2009-02-03 20:21:25 +00008296 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008297 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008298 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008299 MIB.addReg(t2);
8300
Dale Johannesene4d209d2009-02-03 20:21:25 +00008301 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008302 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008303 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008304 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008305
Dale Johannesene4d209d2009-02-03 20:21:25 +00008306 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008307 for (int i=0; i <= lastAddrIndx; ++i)
8308 (*MIB).addOperand(*argOpers[i]);
8309
8310 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008311 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8312 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008313
Dale Johannesene4d209d2009-02-03 20:21:25 +00008314 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008315 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008316 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008317 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008318
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008319 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008320 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008321
8322 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8323 return nextMBB;
8324}
8325
8326// private utility function
8327MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008328X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8329 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008330 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008331 // For the atomic min/max operator, we generate
8332 // thisMBB:
8333 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008334 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008335 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008336 // cmp t1, t2
8337 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008338 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008339 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8340 // bz newMBB
8341 // fallthrough -->nextMBB
8342 //
8343 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8344 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008345 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008346 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008347
Mon P Wang63307c32008-05-05 19:05:59 +00008348 /// First build the CFG
8349 MachineFunction *F = MBB->getParent();
8350 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008351 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8352 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8353 F->insert(MBBIter, newMBB);
8354 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008355
Dan Gohmand6708ea2009-08-15 01:38:56 +00008356 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008357 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008358
Mon P Wang63307c32008-05-05 19:05:59 +00008359 // Update thisMBB to fall through to newMBB
8360 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008361
Mon P Wang63307c32008-05-05 19:05:59 +00008362 // newMBB jumps to newMBB and fall through to nextMBB
8363 newMBB->addSuccessor(nextMBB);
8364 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008365
Dale Johannesene4d209d2009-02-03 20:21:25 +00008366 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008367 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008368 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008369 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008370 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008371 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008372 int numArgs = mInstr->getNumOperands() - 1;
8373 for (int i=0; i < numArgs; ++i)
8374 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008375
Mon P Wang63307c32008-05-05 19:05:59 +00008376 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008377 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8378 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008379
Mon P Wangab3e7472008-05-05 22:56:23 +00008380 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008381 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008382 for (int i=0; i <= lastAddrIndx; ++i)
8383 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008384
Mon P Wang63307c32008-05-05 19:05:59 +00008385 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008386 assert((argOpers[valArgIndx]->isReg() ||
8387 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008388 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008389
8390 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008391 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008392 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008393 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008394 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008395 (*MIB).addOperand(*argOpers[valArgIndx]);
8396
Dale Johannesene4d209d2009-02-03 20:21:25 +00008397 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008398 MIB.addReg(t1);
8399
Dale Johannesene4d209d2009-02-03 20:21:25 +00008400 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008401 MIB.addReg(t1);
8402 MIB.addReg(t2);
8403
8404 // Generate movc
8405 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008406 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008407 MIB.addReg(t2);
8408 MIB.addReg(t1);
8409
8410 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008411 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008412 for (int i=0; i <= lastAddrIndx; ++i)
8413 (*MIB).addOperand(*argOpers[i]);
8414 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008415 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008416 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8417 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008418
Dale Johannesene4d209d2009-02-03 20:21:25 +00008419 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008420 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008421
Mon P Wang63307c32008-05-05 19:05:59 +00008422 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008423 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008424
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008425 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008426 return nextMBB;
8427}
8428
Eric Christopherf83a5de2009-08-27 18:08:16 +00008429// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8430// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008431MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008432X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008433 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008434
8435 MachineFunction *F = BB->getParent();
8436 DebugLoc dl = MI->getDebugLoc();
8437 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8438
8439 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008440 if (memArg)
8441 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8442 else
8443 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008444
8445 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8446
8447 for (unsigned i = 0; i < numArgs; ++i) {
8448 MachineOperand &Op = MI->getOperand(i+1);
8449
8450 if (!(Op.isReg() && Op.isImplicit()))
8451 MIB.addOperand(Op);
8452 }
8453
8454 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8455 .addReg(X86::XMM0);
8456
8457 F->DeleteMachineInstr(MI);
8458
8459 return BB;
8460}
8461
8462MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008463X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8464 MachineInstr *MI,
8465 MachineBasicBlock *MBB) const {
8466 // Emit code to save XMM registers to the stack. The ABI says that the
8467 // number of registers to save is given in %al, so it's theoretically
8468 // possible to do an indirect jump trick to avoid saving all of them,
8469 // however this code takes a simpler approach and just executes all
8470 // of the stores if %al is non-zero. It's less code, and it's probably
8471 // easier on the hardware branch predictor, and stores aren't all that
8472 // expensive anyway.
8473
8474 // Create the new basic blocks. One block contains all the XMM stores,
8475 // and one block is the final destination regardless of whether any
8476 // stores were performed.
8477 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8478 MachineFunction *F = MBB->getParent();
8479 MachineFunction::iterator MBBIter = MBB;
8480 ++MBBIter;
8481 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8482 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8483 F->insert(MBBIter, XMMSaveMBB);
8484 F->insert(MBBIter, EndMBB);
8485
8486 // Set up the CFG.
8487 // Move any original successors of MBB to the end block.
8488 EndMBB->transferSuccessors(MBB);
8489 // The original block will now fall through to the XMM save block.
8490 MBB->addSuccessor(XMMSaveMBB);
8491 // The XMMSaveMBB will fall through to the end block.
8492 XMMSaveMBB->addSuccessor(EndMBB);
8493
8494 // Now add the instructions.
8495 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8496 DebugLoc DL = MI->getDebugLoc();
8497
8498 unsigned CountReg = MI->getOperand(0).getReg();
8499 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8500 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8501
8502 if (!Subtarget->isTargetWin64()) {
8503 // If %al is 0, branch around the XMM save block.
8504 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008505 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008506 MBB->addSuccessor(EndMBB);
8507 }
8508
8509 // In the XMM save block, save all the XMM argument registers.
8510 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8511 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008512 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008513 F->getMachineMemOperand(
8514 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8515 MachineMemOperand::MOStore, Offset,
8516 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008517 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8518 .addFrameIndex(RegSaveFrameIndex)
8519 .addImm(/*Scale=*/1)
8520 .addReg(/*IndexReg=*/0)
8521 .addImm(/*Disp=*/Offset)
8522 .addReg(/*Segment=*/0)
8523 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008524 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008525 }
8526
8527 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8528
8529 return EndMBB;
8530}
Mon P Wang63307c32008-05-05 19:05:59 +00008531
Evan Cheng60c07e12006-07-05 22:17:51 +00008532MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008533X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008534 MachineBasicBlock *BB,
8535 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008536 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8537 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008538
Chris Lattner52600972009-09-02 05:57:00 +00008539 // To "insert" a SELECT_CC instruction, we actually have to insert the
8540 // diamond control-flow pattern. The incoming instruction knows the
8541 // destination vreg to set, the condition code register to branch on, the
8542 // true/false values to select between, and a branch opcode to use.
8543 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8544 MachineFunction::iterator It = BB;
8545 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008546
Chris Lattner52600972009-09-02 05:57:00 +00008547 // thisMBB:
8548 // ...
8549 // TrueVal = ...
8550 // cmpTY ccX, r1, r2
8551 // bCC copy1MBB
8552 // fallthrough --> copy0MBB
8553 MachineBasicBlock *thisMBB = BB;
8554 MachineFunction *F = BB->getParent();
8555 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8556 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8557 unsigned Opc =
8558 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8559 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8560 F->insert(It, copy0MBB);
8561 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008562 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008563 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008564 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008565 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008566 E = BB->succ_end(); I != E; ++I) {
8567 EM->insert(std::make_pair(*I, sinkMBB));
8568 sinkMBB->addSuccessor(*I);
8569 }
8570 // Next, remove all successors of the current block, and add the true
8571 // and fallthrough blocks as its successors.
8572 while (!BB->succ_empty())
8573 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008574 // Add the true and fallthrough blocks as its successors.
8575 BB->addSuccessor(copy0MBB);
8576 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008577
Chris Lattner52600972009-09-02 05:57:00 +00008578 // copy0MBB:
8579 // %FalseValue = ...
8580 // # fallthrough to sinkMBB
8581 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008582
Chris Lattner52600972009-09-02 05:57:00 +00008583 // Update machine-CFG edges
8584 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008585
Chris Lattner52600972009-09-02 05:57:00 +00008586 // sinkMBB:
8587 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8588 // ...
8589 BB = sinkMBB;
8590 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8591 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8592 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8593
8594 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8595 return BB;
8596}
8597
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008598MachineBasicBlock *
8599X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8600 MachineBasicBlock *BB,
8601 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8602 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8603 DebugLoc DL = MI->getDebugLoc();
8604 MachineFunction *F = BB->getParent();
8605
8606 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8607 // non-trivial part is impdef of ESP.
8608 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8609 // mingw-w64.
8610
8611 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8612 .addExternalSymbol("_alloca")
8613 .addReg(X86::EAX, RegState::Implicit)
8614 .addReg(X86::ESP, RegState::Implicit)
8615 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8616 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8617
8618 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8619 return BB;
8620}
Chris Lattner52600972009-09-02 05:57:00 +00008621
8622MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008623X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008624 MachineBasicBlock *BB,
8625 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008626 switch (MI->getOpcode()) {
8627 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008628 case X86::MINGW_ALLOCA:
8629 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008630 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008631 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008632 case X86::CMOV_FR32:
8633 case X86::CMOV_FR64:
8634 case X86::CMOV_V4F32:
8635 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008636 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008637 case X86::CMOV_GR16:
8638 case X86::CMOV_GR32:
8639 case X86::CMOV_RFP32:
8640 case X86::CMOV_RFP64:
8641 case X86::CMOV_RFP80:
Evan Chengce319102009-09-19 09:51:03 +00008642 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008643
Dale Johannesen849f2142007-07-03 00:53:03 +00008644 case X86::FP32_TO_INT16_IN_MEM:
8645 case X86::FP32_TO_INT32_IN_MEM:
8646 case X86::FP32_TO_INT64_IN_MEM:
8647 case X86::FP64_TO_INT16_IN_MEM:
8648 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008649 case X86::FP64_TO_INT64_IN_MEM:
8650 case X86::FP80_TO_INT16_IN_MEM:
8651 case X86::FP80_TO_INT32_IN_MEM:
8652 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008653 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8654 DebugLoc DL = MI->getDebugLoc();
8655
Evan Cheng60c07e12006-07-05 22:17:51 +00008656 // Change the floating point control register to use "round towards zero"
8657 // mode when truncating to an integer value.
8658 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008659 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008660 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008661
8662 // Load the old value of the high byte of the control word...
8663 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008664 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008665 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008666 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008667
8668 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008669 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008670 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008671
8672 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008673 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008674
8675 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008676 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008677 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008678
8679 // Get the X86 opcode to use.
8680 unsigned Opc;
8681 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008682 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008683 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8684 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8685 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8686 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8687 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8688 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008689 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8690 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8691 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008692 }
8693
8694 X86AddressMode AM;
8695 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008696 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008697 AM.BaseType = X86AddressMode::RegBase;
8698 AM.Base.Reg = Op.getReg();
8699 } else {
8700 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008701 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008702 }
8703 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008704 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008705 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008706 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008707 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008708 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008709 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008710 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008711 AM.GV = Op.getGlobal();
8712 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008713 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008714 }
Chris Lattner52600972009-09-02 05:57:00 +00008715 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008716 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008717
8718 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008719 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008720
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008721 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008722 return BB;
8723 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008724 // String/text processing lowering.
8725 case X86::PCMPISTRM128REG:
8726 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8727 case X86::PCMPISTRM128MEM:
8728 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8729 case X86::PCMPESTRM128REG:
8730 return EmitPCMP(MI, BB, 5, false /* in mem */);
8731 case X86::PCMPESTRM128MEM:
8732 return EmitPCMP(MI, BB, 5, true /* in mem */);
8733
8734 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008735 case X86::ATOMAND32:
8736 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008737 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008738 X86::LCMPXCHG32, X86::MOV32rr,
8739 X86::NOT32r, X86::EAX,
8740 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008741 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008742 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8743 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008744 X86::LCMPXCHG32, X86::MOV32rr,
8745 X86::NOT32r, X86::EAX,
8746 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008747 case X86::ATOMXOR32:
8748 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008749 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008750 X86::LCMPXCHG32, X86::MOV32rr,
8751 X86::NOT32r, X86::EAX,
8752 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008753 case X86::ATOMNAND32:
8754 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008755 X86::AND32ri, X86::MOV32rm,
8756 X86::LCMPXCHG32, X86::MOV32rr,
8757 X86::NOT32r, X86::EAX,
8758 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008759 case X86::ATOMMIN32:
8760 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8761 case X86::ATOMMAX32:
8762 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8763 case X86::ATOMUMIN32:
8764 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8765 case X86::ATOMUMAX32:
8766 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008767
8768 case X86::ATOMAND16:
8769 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8770 X86::AND16ri, X86::MOV16rm,
8771 X86::LCMPXCHG16, X86::MOV16rr,
8772 X86::NOT16r, X86::AX,
8773 X86::GR16RegisterClass);
8774 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008775 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008776 X86::OR16ri, X86::MOV16rm,
8777 X86::LCMPXCHG16, X86::MOV16rr,
8778 X86::NOT16r, X86::AX,
8779 X86::GR16RegisterClass);
8780 case X86::ATOMXOR16:
8781 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8782 X86::XOR16ri, X86::MOV16rm,
8783 X86::LCMPXCHG16, X86::MOV16rr,
8784 X86::NOT16r, X86::AX,
8785 X86::GR16RegisterClass);
8786 case X86::ATOMNAND16:
8787 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8788 X86::AND16ri, X86::MOV16rm,
8789 X86::LCMPXCHG16, X86::MOV16rr,
8790 X86::NOT16r, X86::AX,
8791 X86::GR16RegisterClass, true);
8792 case X86::ATOMMIN16:
8793 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8794 case X86::ATOMMAX16:
8795 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8796 case X86::ATOMUMIN16:
8797 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8798 case X86::ATOMUMAX16:
8799 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8800
8801 case X86::ATOMAND8:
8802 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8803 X86::AND8ri, X86::MOV8rm,
8804 X86::LCMPXCHG8, X86::MOV8rr,
8805 X86::NOT8r, X86::AL,
8806 X86::GR8RegisterClass);
8807 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008808 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008809 X86::OR8ri, X86::MOV8rm,
8810 X86::LCMPXCHG8, X86::MOV8rr,
8811 X86::NOT8r, X86::AL,
8812 X86::GR8RegisterClass);
8813 case X86::ATOMXOR8:
8814 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8815 X86::XOR8ri, X86::MOV8rm,
8816 X86::LCMPXCHG8, X86::MOV8rr,
8817 X86::NOT8r, X86::AL,
8818 X86::GR8RegisterClass);
8819 case X86::ATOMNAND8:
8820 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8821 X86::AND8ri, X86::MOV8rm,
8822 X86::LCMPXCHG8, X86::MOV8rr,
8823 X86::NOT8r, X86::AL,
8824 X86::GR8RegisterClass, true);
8825 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008826 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008827 case X86::ATOMAND64:
8828 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008829 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008830 X86::LCMPXCHG64, X86::MOV64rr,
8831 X86::NOT64r, X86::RAX,
8832 X86::GR64RegisterClass);
8833 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008834 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8835 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008836 X86::LCMPXCHG64, X86::MOV64rr,
8837 X86::NOT64r, X86::RAX,
8838 X86::GR64RegisterClass);
8839 case X86::ATOMXOR64:
8840 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008841 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008842 X86::LCMPXCHG64, X86::MOV64rr,
8843 X86::NOT64r, X86::RAX,
8844 X86::GR64RegisterClass);
8845 case X86::ATOMNAND64:
8846 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8847 X86::AND64ri32, X86::MOV64rm,
8848 X86::LCMPXCHG64, X86::MOV64rr,
8849 X86::NOT64r, X86::RAX,
8850 X86::GR64RegisterClass, true);
8851 case X86::ATOMMIN64:
8852 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8853 case X86::ATOMMAX64:
8854 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8855 case X86::ATOMUMIN64:
8856 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8857 case X86::ATOMUMAX64:
8858 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008859
8860 // This group does 64-bit operations on a 32-bit host.
8861 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008862 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008863 X86::AND32rr, X86::AND32rr,
8864 X86::AND32ri, X86::AND32ri,
8865 false);
8866 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008867 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008868 X86::OR32rr, X86::OR32rr,
8869 X86::OR32ri, X86::OR32ri,
8870 false);
8871 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008872 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008873 X86::XOR32rr, X86::XOR32rr,
8874 X86::XOR32ri, X86::XOR32ri,
8875 false);
8876 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008877 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008878 X86::AND32rr, X86::AND32rr,
8879 X86::AND32ri, X86::AND32ri,
8880 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008881 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008882 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008883 X86::ADD32rr, X86::ADC32rr,
8884 X86::ADD32ri, X86::ADC32ri,
8885 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008886 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008887 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008888 X86::SUB32rr, X86::SBB32rr,
8889 X86::SUB32ri, X86::SBB32ri,
8890 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008891 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008892 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008893 X86::MOV32rr, X86::MOV32rr,
8894 X86::MOV32ri, X86::MOV32ri,
8895 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008896 case X86::VASTART_SAVE_XMM_REGS:
8897 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008898 }
8899}
8900
8901//===----------------------------------------------------------------------===//
8902// X86 Optimization Hooks
8903//===----------------------------------------------------------------------===//
8904
Dan Gohman475871a2008-07-27 21:46:04 +00008905void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008906 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008907 APInt &KnownZero,
8908 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008909 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008910 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008911 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008912 assert((Opc >= ISD::BUILTIN_OP_END ||
8913 Opc == ISD::INTRINSIC_WO_CHAIN ||
8914 Opc == ISD::INTRINSIC_W_CHAIN ||
8915 Opc == ISD::INTRINSIC_VOID) &&
8916 "Should use MaskedValueIsZero if you don't know whether Op"
8917 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008918
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008919 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008920 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008921 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008922 case X86ISD::ADD:
8923 case X86ISD::SUB:
8924 case X86ISD::SMUL:
8925 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008926 case X86ISD::INC:
8927 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008928 case X86ISD::OR:
8929 case X86ISD::XOR:
8930 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008931 // These nodes' second result is a boolean.
8932 if (Op.getResNo() == 0)
8933 break;
8934 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008935 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008936 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8937 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008938 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008939 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008940}
Chris Lattner259e97c2006-01-31 19:43:35 +00008941
Evan Cheng206ee9d2006-07-07 08:33:52 +00008942/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008943/// node is a GlobalAddress + offset.
8944bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00008945 const GlobalValue* &GA,
8946 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00008947 if (N->getOpcode() == X86ISD::Wrapper) {
8948 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008949 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008950 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008951 return true;
8952 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008953 }
Evan Chengad4196b2008-05-12 19:56:52 +00008954 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008955}
8956
Evan Cheng206ee9d2006-07-07 08:33:52 +00008957/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8958/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8959/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008960/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008961static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008962 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008963 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008964 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008965 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008966
Eli Friedman7a5e5552009-06-07 06:52:44 +00008967 if (VT.getSizeInBits() != 128)
8968 return SDValue();
8969
Nate Begemanfdea31a2010-03-24 20:49:50 +00008970 SmallVector<SDValue, 16> Elts;
8971 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8972 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8973
8974 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008975}
Evan Chengd880b972008-05-09 21:53:03 +00008976
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008977/// PerformShuffleCombine - Detect vector gather/scatter index generation
8978/// and convert it from being a bunch of shuffles and extracts to a simple
8979/// store and scalar loads to extract the elements.
8980static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8981 const TargetLowering &TLI) {
8982 SDValue InputVector = N->getOperand(0);
8983
8984 // Only operate on vectors of 4 elements, where the alternative shuffling
8985 // gets to be more expensive.
8986 if (InputVector.getValueType() != MVT::v4i32)
8987 return SDValue();
8988
8989 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8990 // single use which is a sign-extend or zero-extend, and all elements are
8991 // used.
8992 SmallVector<SDNode *, 4> Uses;
8993 unsigned ExtractedElements = 0;
8994 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8995 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8996 if (UI.getUse().getResNo() != InputVector.getResNo())
8997 return SDValue();
8998
8999 SDNode *Extract = *UI;
9000 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
9001 return SDValue();
9002
9003 if (Extract->getValueType(0) != MVT::i32)
9004 return SDValue();
9005 if (!Extract->hasOneUse())
9006 return SDValue();
9007 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
9008 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
9009 return SDValue();
9010 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
9011 return SDValue();
9012
9013 // Record which element was extracted.
9014 ExtractedElements |=
9015 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
9016
9017 Uses.push_back(Extract);
9018 }
9019
9020 // If not all the elements were used, this may not be worthwhile.
9021 if (ExtractedElements != 15)
9022 return SDValue();
9023
9024 // Ok, we've now decided to do the transformation.
9025 DebugLoc dl = InputVector.getDebugLoc();
9026
9027 // Store the value to a temporary stack slot.
9028 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
9029 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
9030 false, false, 0);
9031
9032 // Replace each use (extract) with a load of the appropriate element.
9033 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
9034 UE = Uses.end(); UI != UE; ++UI) {
9035 SDNode *Extract = *UI;
9036
9037 // Compute the element's address.
9038 SDValue Idx = Extract->getOperand(1);
9039 unsigned EltSize =
9040 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
9041 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
9042 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
9043
9044 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
9045
9046 // Load the scalar.
9047 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
9048 NULL, 0, false, false, 0);
9049
9050 // Replace the exact with the load.
9051 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
9052 }
9053
9054 // The replacement was made in place; don't return anything.
9055 return SDValue();
9056}
9057
Chris Lattner83e6c992006-10-04 06:57:07 +00009058/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009059static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00009060 const X86Subtarget *Subtarget) {
9061 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009062 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00009063 // Get the LHS/RHS of the select.
9064 SDValue LHS = N->getOperand(1);
9065 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009066
Dan Gohman670e5392009-09-21 18:03:22 +00009067 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00009068 // instructions match the semantics of the common C idiom x<y?x:y but not
9069 // x<=y?x:y, because of how they handle negative zero (which can be
9070 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00009071 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00009072 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00009073 Cond.getOpcode() == ISD::SETCC) {
9074 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009075
Chris Lattner47b4ce82009-03-11 05:48:52 +00009076 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00009077 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00009078 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
9079 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009080 switch (CC) {
9081 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009082 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009083 // Converting this to a min would handle NaNs incorrectly, and swapping
9084 // the operands would cause it to handle comparisons between positive
9085 // and negative zero incorrectly.
9086 if (!FiniteOnlyFPMath() &&
9087 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9088 if (!UnsafeFPMath &&
9089 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9090 break;
9091 std::swap(LHS, RHS);
9092 }
Dan Gohman670e5392009-09-21 18:03:22 +00009093 Opcode = X86ISD::FMIN;
9094 break;
9095 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009096 // Converting this to a min would handle comparisons between positive
9097 // and negative zero incorrectly.
9098 if (!UnsafeFPMath &&
9099 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
9100 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009101 Opcode = X86ISD::FMIN;
9102 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009103 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009104 // Converting this to a min would handle both negative zeros and NaNs
9105 // incorrectly, but we can swap the operands to fix both.
9106 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009107 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009108 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009109 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009110 Opcode = X86ISD::FMIN;
9111 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009112
Dan Gohman670e5392009-09-21 18:03:22 +00009113 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009114 // Converting this to a max would handle comparisons between positive
9115 // and negative zero incorrectly.
9116 if (!UnsafeFPMath &&
9117 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
9118 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009119 Opcode = X86ISD::FMAX;
9120 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009121 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009122 // Converting this to a max would handle NaNs incorrectly, and swapping
9123 // the operands would cause it to handle comparisons between positive
9124 // and negative zero incorrectly.
9125 if (!FiniteOnlyFPMath() &&
9126 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9127 if (!UnsafeFPMath &&
9128 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9129 break;
9130 std::swap(LHS, RHS);
9131 }
Dan Gohman670e5392009-09-21 18:03:22 +00009132 Opcode = X86ISD::FMAX;
9133 break;
9134 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009135 // Converting this to a max would handle both negative zeros and NaNs
9136 // incorrectly, but we can swap the operands to fix both.
9137 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009138 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009139 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009140 case ISD::SETGE:
9141 Opcode = X86ISD::FMAX;
9142 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00009143 }
Dan Gohman670e5392009-09-21 18:03:22 +00009144 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00009145 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
9146 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009147 switch (CC) {
9148 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009149 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009150 // Converting this to a min would handle comparisons between positive
9151 // and negative zero incorrectly, and swapping the operands would
9152 // cause it to handle NaNs incorrectly.
9153 if (!UnsafeFPMath &&
9154 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
9155 if (!FiniteOnlyFPMath() &&
9156 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9157 break;
9158 std::swap(LHS, RHS);
9159 }
Dan Gohman670e5392009-09-21 18:03:22 +00009160 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009161 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009162 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009163 // Converting this to a min would handle NaNs incorrectly.
9164 if (!UnsafeFPMath &&
9165 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9166 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009167 Opcode = X86ISD::FMIN;
9168 break;
9169 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009170 // Converting this to a min would handle both negative zeros and NaNs
9171 // incorrectly, but we can swap the operands to fix both.
9172 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009173 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009174 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009175 case ISD::SETGE:
9176 Opcode = X86ISD::FMIN;
9177 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009178
Dan Gohman670e5392009-09-21 18:03:22 +00009179 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009180 // Converting this to a max would handle NaNs incorrectly.
9181 if (!FiniteOnlyFPMath() &&
9182 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9183 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009184 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009185 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009186 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009187 // Converting this to a max would handle comparisons between positive
9188 // and negative zero incorrectly, and swapping the operands would
9189 // cause it to handle NaNs incorrectly.
9190 if (!UnsafeFPMath &&
9191 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9192 if (!FiniteOnlyFPMath() &&
9193 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9194 break;
9195 std::swap(LHS, RHS);
9196 }
Dan Gohman670e5392009-09-21 18:03:22 +00009197 Opcode = X86ISD::FMAX;
9198 break;
9199 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009200 // Converting this to a max would handle both negative zeros and NaNs
9201 // incorrectly, but we can swap the operands to fix both.
9202 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009203 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009204 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009205 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009206 Opcode = X86ISD::FMAX;
9207 break;
9208 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009209 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009210
Chris Lattner47b4ce82009-03-11 05:48:52 +00009211 if (Opcode)
9212 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009213 }
Eric Christopherfd179292009-08-27 18:07:15 +00009214
Chris Lattnerd1980a52009-03-12 06:52:53 +00009215 // If this is a select between two integer constants, try to do some
9216 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009217 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9218 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009219 // Don't do this for crazy integer types.
9220 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9221 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009222 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009223 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009224
Chris Lattnercee56e72009-03-13 05:53:31 +00009225 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009226 // Efficiently invertible.
9227 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9228 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9229 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9230 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009231 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009232 }
Eric Christopherfd179292009-08-27 18:07:15 +00009233
Chris Lattnerd1980a52009-03-12 06:52:53 +00009234 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009235 if (FalseC->getAPIntValue() == 0 &&
9236 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009237 if (NeedsCondInvert) // Invert the condition if needed.
9238 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9239 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009240
Chris Lattnerd1980a52009-03-12 06:52:53 +00009241 // Zero extend the condition if needed.
9242 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009243
Chris Lattnercee56e72009-03-13 05:53:31 +00009244 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009245 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009246 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009247 }
Eric Christopherfd179292009-08-27 18:07:15 +00009248
Chris Lattner97a29a52009-03-13 05:22:11 +00009249 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009250 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009251 if (NeedsCondInvert) // Invert the condition if needed.
9252 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9253 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009254
Chris Lattner97a29a52009-03-13 05:22:11 +00009255 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009256 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9257 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009258 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009259 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009260 }
Eric Christopherfd179292009-08-27 18:07:15 +00009261
Chris Lattnercee56e72009-03-13 05:53:31 +00009262 // Optimize cases that will turn into an LEA instruction. This requires
9263 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009264 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009265 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009266 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009267
Chris Lattnercee56e72009-03-13 05:53:31 +00009268 bool isFastMultiplier = false;
9269 if (Diff < 10) {
9270 switch ((unsigned char)Diff) {
9271 default: break;
9272 case 1: // result = add base, cond
9273 case 2: // result = lea base( , cond*2)
9274 case 3: // result = lea base(cond, cond*2)
9275 case 4: // result = lea base( , cond*4)
9276 case 5: // result = lea base(cond, cond*4)
9277 case 8: // result = lea base( , cond*8)
9278 case 9: // result = lea base(cond, cond*8)
9279 isFastMultiplier = true;
9280 break;
9281 }
9282 }
Eric Christopherfd179292009-08-27 18:07:15 +00009283
Chris Lattnercee56e72009-03-13 05:53:31 +00009284 if (isFastMultiplier) {
9285 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9286 if (NeedsCondInvert) // Invert the condition if needed.
9287 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9288 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009289
Chris Lattnercee56e72009-03-13 05:53:31 +00009290 // Zero extend the condition if needed.
9291 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9292 Cond);
9293 // Scale the condition by the difference.
9294 if (Diff != 1)
9295 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9296 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009297
Chris Lattnercee56e72009-03-13 05:53:31 +00009298 // Add the base if non-zero.
9299 if (FalseC->getAPIntValue() != 0)
9300 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9301 SDValue(FalseC, 0));
9302 return Cond;
9303 }
Eric Christopherfd179292009-08-27 18:07:15 +00009304 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009305 }
9306 }
Eric Christopherfd179292009-08-27 18:07:15 +00009307
Dan Gohman475871a2008-07-27 21:46:04 +00009308 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009309}
9310
Chris Lattnerd1980a52009-03-12 06:52:53 +00009311/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9312static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9313 TargetLowering::DAGCombinerInfo &DCI) {
9314 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009315
Chris Lattnerd1980a52009-03-12 06:52:53 +00009316 // If the flag operand isn't dead, don't touch this CMOV.
9317 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9318 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009319
Chris Lattnerd1980a52009-03-12 06:52:53 +00009320 // If this is a select between two integer constants, try to do some
9321 // optimizations. Note that the operands are ordered the opposite of SELECT
9322 // operands.
9323 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9324 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9325 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9326 // larger than FalseC (the false value).
9327 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009328
Chris Lattnerd1980a52009-03-12 06:52:53 +00009329 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9330 CC = X86::GetOppositeBranchCondition(CC);
9331 std::swap(TrueC, FalseC);
9332 }
Eric Christopherfd179292009-08-27 18:07:15 +00009333
Chris Lattnerd1980a52009-03-12 06:52:53 +00009334 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009335 // This is efficient for any integer data type (including i8/i16) and
9336 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009337 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9338 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009339 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9340 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009341
Chris Lattnerd1980a52009-03-12 06:52:53 +00009342 // Zero extend the condition if needed.
9343 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009344
Chris Lattnerd1980a52009-03-12 06:52:53 +00009345 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9346 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009347 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009348 if (N->getNumValues() == 2) // Dead flag value?
9349 return DCI.CombineTo(N, Cond, SDValue());
9350 return Cond;
9351 }
Eric Christopherfd179292009-08-27 18:07:15 +00009352
Chris Lattnercee56e72009-03-13 05:53:31 +00009353 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9354 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009355 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9356 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009357 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9358 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009359
Chris Lattner97a29a52009-03-13 05:22:11 +00009360 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009361 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9362 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009363 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9364 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009365
Chris Lattner97a29a52009-03-13 05:22:11 +00009366 if (N->getNumValues() == 2) // Dead flag value?
9367 return DCI.CombineTo(N, Cond, SDValue());
9368 return Cond;
9369 }
Eric Christopherfd179292009-08-27 18:07:15 +00009370
Chris Lattnercee56e72009-03-13 05:53:31 +00009371 // Optimize cases that will turn into an LEA instruction. This requires
9372 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009373 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009374 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009375 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009376
Chris Lattnercee56e72009-03-13 05:53:31 +00009377 bool isFastMultiplier = false;
9378 if (Diff < 10) {
9379 switch ((unsigned char)Diff) {
9380 default: break;
9381 case 1: // result = add base, cond
9382 case 2: // result = lea base( , cond*2)
9383 case 3: // result = lea base(cond, cond*2)
9384 case 4: // result = lea base( , cond*4)
9385 case 5: // result = lea base(cond, cond*4)
9386 case 8: // result = lea base( , cond*8)
9387 case 9: // result = lea base(cond, cond*8)
9388 isFastMultiplier = true;
9389 break;
9390 }
9391 }
Eric Christopherfd179292009-08-27 18:07:15 +00009392
Chris Lattnercee56e72009-03-13 05:53:31 +00009393 if (isFastMultiplier) {
9394 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9395 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009396 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9397 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009398 // Zero extend the condition if needed.
9399 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9400 Cond);
9401 // Scale the condition by the difference.
9402 if (Diff != 1)
9403 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9404 DAG.getConstant(Diff, Cond.getValueType()));
9405
9406 // Add the base if non-zero.
9407 if (FalseC->getAPIntValue() != 0)
9408 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9409 SDValue(FalseC, 0));
9410 if (N->getNumValues() == 2) // Dead flag value?
9411 return DCI.CombineTo(N, Cond, SDValue());
9412 return Cond;
9413 }
Eric Christopherfd179292009-08-27 18:07:15 +00009414 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009415 }
9416 }
9417 return SDValue();
9418}
9419
9420
Evan Cheng0b0cd912009-03-28 05:57:29 +00009421/// PerformMulCombine - Optimize a single multiply with constant into two
9422/// in order to implement it with two cheaper instructions, e.g.
9423/// LEA + SHL, LEA + LEA.
9424static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9425 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009426 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9427 return SDValue();
9428
Owen Andersone50ed302009-08-10 22:56:29 +00009429 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009430 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009431 return SDValue();
9432
9433 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9434 if (!C)
9435 return SDValue();
9436 uint64_t MulAmt = C->getZExtValue();
9437 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9438 return SDValue();
9439
9440 uint64_t MulAmt1 = 0;
9441 uint64_t MulAmt2 = 0;
9442 if ((MulAmt % 9) == 0) {
9443 MulAmt1 = 9;
9444 MulAmt2 = MulAmt / 9;
9445 } else if ((MulAmt % 5) == 0) {
9446 MulAmt1 = 5;
9447 MulAmt2 = MulAmt / 5;
9448 } else if ((MulAmt % 3) == 0) {
9449 MulAmt1 = 3;
9450 MulAmt2 = MulAmt / 3;
9451 }
9452 if (MulAmt2 &&
9453 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9454 DebugLoc DL = N->getDebugLoc();
9455
9456 if (isPowerOf2_64(MulAmt2) &&
9457 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9458 // If second multiplifer is pow2, issue it first. We want the multiply by
9459 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9460 // is an add.
9461 std::swap(MulAmt1, MulAmt2);
9462
9463 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009464 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009465 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009466 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009467 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009468 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009469 DAG.getConstant(MulAmt1, VT));
9470
Eric Christopherfd179292009-08-27 18:07:15 +00009471 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009472 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009473 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009474 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009475 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009476 DAG.getConstant(MulAmt2, VT));
9477
9478 // Do not add new nodes to DAG combiner worklist.
9479 DCI.CombineTo(N, NewMul, false);
9480 }
9481 return SDValue();
9482}
9483
Evan Chengad9c0a32009-12-15 00:53:42 +00009484static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9485 SDValue N0 = N->getOperand(0);
9486 SDValue N1 = N->getOperand(1);
9487 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9488 EVT VT = N0.getValueType();
9489
9490 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9491 // since the result of setcc_c is all zero's or all ones.
9492 if (N1C && N0.getOpcode() == ISD::AND &&
9493 N0.getOperand(1).getOpcode() == ISD::Constant) {
9494 SDValue N00 = N0.getOperand(0);
9495 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9496 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9497 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9498 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9499 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9500 APInt ShAmt = N1C->getAPIntValue();
9501 Mask = Mask.shl(ShAmt);
9502 if (Mask != 0)
9503 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9504 N00, DAG.getConstant(Mask, VT));
9505 }
9506 }
9507
9508 return SDValue();
9509}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009510
Nate Begeman740ab032009-01-26 00:52:55 +00009511/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9512/// when possible.
9513static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9514 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009515 EVT VT = N->getValueType(0);
9516 if (!VT.isVector() && VT.isInteger() &&
9517 N->getOpcode() == ISD::SHL)
9518 return PerformSHLCombine(N, DAG);
9519
Nate Begeman740ab032009-01-26 00:52:55 +00009520 // On X86 with SSE2 support, we can transform this to a vector shift if
9521 // all elements are shifted by the same amount. We can't do this in legalize
9522 // because the a constant vector is typically transformed to a constant pool
9523 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009524 if (!Subtarget->hasSSE2())
9525 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009526
Owen Anderson825b72b2009-08-11 20:47:22 +00009527 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009528 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009529
Mon P Wang3becd092009-01-28 08:12:05 +00009530 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009531 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009532 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009533 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009534 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9535 unsigned NumElts = VT.getVectorNumElements();
9536 unsigned i = 0;
9537 for (; i != NumElts; ++i) {
9538 SDValue Arg = ShAmtOp.getOperand(i);
9539 if (Arg.getOpcode() == ISD::UNDEF) continue;
9540 BaseShAmt = Arg;
9541 break;
9542 }
9543 for (; i != NumElts; ++i) {
9544 SDValue Arg = ShAmtOp.getOperand(i);
9545 if (Arg.getOpcode() == ISD::UNDEF) continue;
9546 if (Arg != BaseShAmt) {
9547 return SDValue();
9548 }
9549 }
9550 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009551 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009552 SDValue InVec = ShAmtOp.getOperand(0);
9553 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9554 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9555 unsigned i = 0;
9556 for (; i != NumElts; ++i) {
9557 SDValue Arg = InVec.getOperand(i);
9558 if (Arg.getOpcode() == ISD::UNDEF) continue;
9559 BaseShAmt = Arg;
9560 break;
9561 }
9562 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9563 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009564 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009565 if (C->getZExtValue() == SplatIdx)
9566 BaseShAmt = InVec.getOperand(1);
9567 }
9568 }
9569 if (BaseShAmt.getNode() == 0)
9570 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9571 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009572 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009573 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009574
Mon P Wangefa42202009-09-03 19:56:25 +00009575 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009576 if (EltVT.bitsGT(MVT::i32))
9577 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9578 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009579 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009580
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009581 // The shift amount is identical so we can do a vector shift.
9582 SDValue ValOp = N->getOperand(0);
9583 switch (N->getOpcode()) {
9584 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009585 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009586 break;
9587 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009588 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009589 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009590 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009591 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009592 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009593 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009594 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009595 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009596 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009597 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009598 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009599 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009600 break;
9601 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009602 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009603 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009604 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009605 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009606 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009607 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009608 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009609 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009610 break;
9611 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009612 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009613 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009614 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009615 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009616 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009617 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009618 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009619 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009620 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009621 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009622 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009623 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009624 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009625 }
9626 return SDValue();
9627}
9628
Evan Cheng760d1942010-01-04 21:22:48 +00009629static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +00009630 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +00009631 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +00009632 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +00009633 return SDValue();
9634
Evan Cheng760d1942010-01-04 21:22:48 +00009635 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009636 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +00009637 return SDValue();
9638
9639 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9640 SDValue N0 = N->getOperand(0);
9641 SDValue N1 = N->getOperand(1);
9642 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9643 std::swap(N0, N1);
9644 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9645 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +00009646 if (!N0.hasOneUse() || !N1.hasOneUse())
9647 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +00009648
9649 SDValue ShAmt0 = N0.getOperand(1);
9650 if (ShAmt0.getValueType() != MVT::i8)
9651 return SDValue();
9652 SDValue ShAmt1 = N1.getOperand(1);
9653 if (ShAmt1.getValueType() != MVT::i8)
9654 return SDValue();
9655 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9656 ShAmt0 = ShAmt0.getOperand(0);
9657 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9658 ShAmt1 = ShAmt1.getOperand(0);
9659
9660 DebugLoc DL = N->getDebugLoc();
9661 unsigned Opc = X86ISD::SHLD;
9662 SDValue Op0 = N0.getOperand(0);
9663 SDValue Op1 = N1.getOperand(0);
9664 if (ShAmt0.getOpcode() == ISD::SUB) {
9665 Opc = X86ISD::SHRD;
9666 std::swap(Op0, Op1);
9667 std::swap(ShAmt0, ShAmt1);
9668 }
9669
Evan Cheng8b1190a2010-04-28 01:18:01 +00009670 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +00009671 if (ShAmt1.getOpcode() == ISD::SUB) {
9672 SDValue Sum = ShAmt1.getOperand(0);
9673 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Evan Cheng8b1190a2010-04-28 01:18:01 +00009674 if (SumC->getSExtValue() == Bits &&
Evan Cheng760d1942010-01-04 21:22:48 +00009675 ShAmt1.getOperand(1) == ShAmt0)
9676 return DAG.getNode(Opc, DL, VT,
9677 Op0, Op1,
9678 DAG.getNode(ISD::TRUNCATE, DL,
9679 MVT::i8, ShAmt0));
9680 }
9681 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9682 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9683 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +00009684 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +00009685 return DAG.getNode(Opc, DL, VT,
9686 N0.getOperand(0), N1.getOperand(0),
9687 DAG.getNode(ISD::TRUNCATE, DL,
9688 MVT::i8, ShAmt0));
9689 }
9690
9691 return SDValue();
9692}
9693
Chris Lattner149a4e52008-02-22 02:09:43 +00009694/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009695static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009696 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009697 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9698 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009699 // A preferable solution to the general problem is to figure out the right
9700 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009701
9702 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009703 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009704 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009705 if (VT.getSizeInBits() != 64)
9706 return SDValue();
9707
Devang Patel578efa92009-06-05 21:57:13 +00009708 const Function *F = DAG.getMachineFunction().getFunction();
9709 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009710 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009711 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009712 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009713 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009714 isa<LoadSDNode>(St->getValue()) &&
9715 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9716 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009717 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009718 LoadSDNode *Ld = 0;
9719 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009720 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009721 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009722 // Must be a store of a load. We currently handle two cases: the load
9723 // is a direct child, and it's under an intervening TokenFactor. It is
9724 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009725 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009726 Ld = cast<LoadSDNode>(St->getChain());
9727 else if (St->getValue().hasOneUse() &&
9728 ChainVal->getOpcode() == ISD::TokenFactor) {
9729 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009730 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009731 TokenFactorIndex = i;
9732 Ld = cast<LoadSDNode>(St->getValue());
9733 } else
9734 Ops.push_back(ChainVal->getOperand(i));
9735 }
9736 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009737
Evan Cheng536e6672009-03-12 05:59:15 +00009738 if (!Ld || !ISD::isNormalLoad(Ld))
9739 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009740
Evan Cheng536e6672009-03-12 05:59:15 +00009741 // If this is not the MMX case, i.e. we are just turning i64 load/store
9742 // into f64 load/store, avoid the transformation if there are multiple
9743 // uses of the loaded value.
9744 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9745 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009746
Evan Cheng536e6672009-03-12 05:59:15 +00009747 DebugLoc LdDL = Ld->getDebugLoc();
9748 DebugLoc StDL = N->getDebugLoc();
9749 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9750 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9751 // pair instead.
9752 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009753 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009754 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9755 Ld->getBasePtr(), Ld->getSrcValue(),
9756 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009757 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009758 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009759 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009760 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009761 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009762 Ops.size());
9763 }
Evan Cheng536e6672009-03-12 05:59:15 +00009764 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009765 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009766 St->isVolatile(), St->isNonTemporal(),
9767 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009768 }
Evan Cheng536e6672009-03-12 05:59:15 +00009769
9770 // Otherwise, lower to two pairs of 32-bit loads / stores.
9771 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009772 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9773 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009774
Owen Anderson825b72b2009-08-11 20:47:22 +00009775 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009776 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009777 Ld->isVolatile(), Ld->isNonTemporal(),
9778 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009779 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009780 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009781 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009782 MinAlign(Ld->getAlignment(), 4));
9783
9784 SDValue NewChain = LoLd.getValue(1);
9785 if (TokenFactorIndex != -1) {
9786 Ops.push_back(LoLd);
9787 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009788 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009789 Ops.size());
9790 }
9791
9792 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009793 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9794 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009795
9796 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9797 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009798 St->isVolatile(), St->isNonTemporal(),
9799 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009800 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9801 St->getSrcValue(),
9802 St->getSrcValueOffset() + 4,
9803 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009804 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009805 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009806 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009807 }
Dan Gohman475871a2008-07-27 21:46:04 +00009808 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009809}
9810
Chris Lattner6cf73262008-01-25 06:14:17 +00009811/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9812/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009813static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009814 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9815 // F[X]OR(0.0, x) -> x
9816 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009817 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9818 if (C->getValueAPF().isPosZero())
9819 return N->getOperand(1);
9820 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9821 if (C->getValueAPF().isPosZero())
9822 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009823 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009824}
9825
9826/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009827static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009828 // FAND(0.0, x) -> 0.0
9829 // FAND(x, 0.0) -> 0.0
9830 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9831 if (C->getValueAPF().isPosZero())
9832 return N->getOperand(0);
9833 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9834 if (C->getValueAPF().isPosZero())
9835 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009836 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009837}
9838
Dan Gohmane5af2d32009-01-29 01:59:02 +00009839static SDValue PerformBTCombine(SDNode *N,
9840 SelectionDAG &DAG,
9841 TargetLowering::DAGCombinerInfo &DCI) {
9842 // BT ignores high bits in the bit index operand.
9843 SDValue Op1 = N->getOperand(1);
9844 if (Op1.hasOneUse()) {
9845 unsigned BitWidth = Op1.getValueSizeInBits();
9846 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9847 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009848 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
9849 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +00009850 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +00009851 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9852 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9853 DCI.CommitTargetLoweringOpt(TLO);
9854 }
9855 return SDValue();
9856}
Chris Lattner83e6c992006-10-04 06:57:07 +00009857
Eli Friedman7a5e5552009-06-07 06:52:44 +00009858static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9859 SDValue Op = N->getOperand(0);
9860 if (Op.getOpcode() == ISD::BIT_CONVERT)
9861 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009862 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009863 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009864 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009865 OpVT.getVectorElementType().getSizeInBits()) {
9866 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9867 }
9868 return SDValue();
9869}
9870
Owen Anderson99177002009-06-29 18:04:45 +00009871// On X86 and X86-64, atomic operations are lowered to locked instructions.
9872// Locked instructions, in turn, have implicit fence semantics (all memory
9873// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009874// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009875// fence-atomic-fence.
9876static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9877 SDValue atomic = N->getOperand(0);
9878 switch (atomic.getOpcode()) {
9879 case ISD::ATOMIC_CMP_SWAP:
9880 case ISD::ATOMIC_SWAP:
9881 case ISD::ATOMIC_LOAD_ADD:
9882 case ISD::ATOMIC_LOAD_SUB:
9883 case ISD::ATOMIC_LOAD_AND:
9884 case ISD::ATOMIC_LOAD_OR:
9885 case ISD::ATOMIC_LOAD_XOR:
9886 case ISD::ATOMIC_LOAD_NAND:
9887 case ISD::ATOMIC_LOAD_MIN:
9888 case ISD::ATOMIC_LOAD_MAX:
9889 case ISD::ATOMIC_LOAD_UMIN:
9890 case ISD::ATOMIC_LOAD_UMAX:
9891 break;
9892 default:
9893 return SDValue();
9894 }
Eric Christopherfd179292009-08-27 18:07:15 +00009895
Owen Anderson99177002009-06-29 18:04:45 +00009896 SDValue fence = atomic.getOperand(0);
9897 if (fence.getOpcode() != ISD::MEMBARRIER)
9898 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009899
Owen Anderson99177002009-06-29 18:04:45 +00009900 switch (atomic.getOpcode()) {
9901 case ISD::ATOMIC_CMP_SWAP:
9902 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9903 atomic.getOperand(1), atomic.getOperand(2),
9904 atomic.getOperand(3));
9905 case ISD::ATOMIC_SWAP:
9906 case ISD::ATOMIC_LOAD_ADD:
9907 case ISD::ATOMIC_LOAD_SUB:
9908 case ISD::ATOMIC_LOAD_AND:
9909 case ISD::ATOMIC_LOAD_OR:
9910 case ISD::ATOMIC_LOAD_XOR:
9911 case ISD::ATOMIC_LOAD_NAND:
9912 case ISD::ATOMIC_LOAD_MIN:
9913 case ISD::ATOMIC_LOAD_MAX:
9914 case ISD::ATOMIC_LOAD_UMIN:
9915 case ISD::ATOMIC_LOAD_UMAX:
9916 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9917 atomic.getOperand(1), atomic.getOperand(2));
9918 default:
9919 return SDValue();
9920 }
9921}
9922
Evan Cheng2e489c42009-12-16 00:53:11 +00009923static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9924 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9925 // (and (i32 x86isd::setcc_carry), 1)
9926 // This eliminates the zext. This transformation is necessary because
9927 // ISD::SETCC is always legalized to i8.
9928 DebugLoc dl = N->getDebugLoc();
9929 SDValue N0 = N->getOperand(0);
9930 EVT VT = N->getValueType(0);
9931 if (N0.getOpcode() == ISD::AND &&
9932 N0.hasOneUse() &&
9933 N0.getOperand(0).hasOneUse()) {
9934 SDValue N00 = N0.getOperand(0);
9935 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9936 return SDValue();
9937 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9938 if (!C || C->getZExtValue() != 1)
9939 return SDValue();
9940 return DAG.getNode(ISD::AND, dl, VT,
9941 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9942 N00.getOperand(0), N00.getOperand(1)),
9943 DAG.getConstant(1, VT));
9944 }
9945
9946 return SDValue();
9947}
9948
Dan Gohman475871a2008-07-27 21:46:04 +00009949SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009950 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009951 SelectionDAG &DAG = DCI.DAG;
9952 switch (N->getOpcode()) {
9953 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009954 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009955 case ISD::EXTRACT_VECTOR_ELT:
9956 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009957 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009958 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009959 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009960 case ISD::SHL:
9961 case ISD::SRA:
9962 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009963 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009964 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009965 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009966 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9967 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009968 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009969 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009970 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009971 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009972 }
9973
Dan Gohman475871a2008-07-27 21:46:04 +00009974 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009975}
9976
Evan Chenge5b51ac2010-04-17 06:13:15 +00009977/// isTypeDesirableForOp - Return true if the target has native support for
9978/// the specified value type and it is 'desirable' to use the type for the
9979/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
9980/// instruction encodings are longer and some i16 instructions are slow.
9981bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
9982 if (!isTypeLegal(VT))
9983 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009984 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +00009985 return true;
9986
9987 switch (Opc) {
9988 default:
9989 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +00009990 case ISD::LOAD:
9991 case ISD::SIGN_EXTEND:
9992 case ISD::ZERO_EXTEND:
9993 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009994 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009995 case ISD::SRL:
9996 case ISD::SUB:
9997 case ISD::ADD:
9998 case ISD::MUL:
9999 case ISD::AND:
10000 case ISD::OR:
10001 case ISD::XOR:
10002 return false;
10003 }
10004}
10005
Evan Chengc82c20b2010-04-24 04:44:57 +000010006static bool MayFoldLoad(SDValue Op) {
10007 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
10008}
10009
10010static bool MayFoldIntoStore(SDValue Op) {
10011 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
10012}
10013
Evan Chenge5b51ac2010-04-17 06:13:15 +000010014/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000010015/// beneficial for dag combiner to promote the specified node. If true, it
10016/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000010017bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010018 EVT VT = Op.getValueType();
10019 if (VT != MVT::i16)
10020 return false;
10021
Evan Cheng4c26e932010-04-19 19:29:22 +000010022 bool Promote = false;
10023 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010024 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000010025 default: break;
10026 case ISD::LOAD: {
10027 LoadSDNode *LD = cast<LoadSDNode>(Op);
10028 // If the non-extending load has a single use and it's not live out, then it
10029 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010030 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
10031 Op.hasOneUse()*/) {
10032 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
10033 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
10034 // The only case where we'd want to promote LOAD (rather then it being
10035 // promoted as an operand is when it's only use is liveout.
10036 if (UI->getOpcode() != ISD::CopyToReg)
10037 return false;
10038 }
10039 }
Evan Cheng4c26e932010-04-19 19:29:22 +000010040 Promote = true;
10041 break;
10042 }
10043 case ISD::SIGN_EXTEND:
10044 case ISD::ZERO_EXTEND:
10045 case ISD::ANY_EXTEND:
10046 Promote = true;
10047 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010048 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000010049 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000010050 SDValue N0 = Op.getOperand(0);
10051 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000010052 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000010053 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010054 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000010055 break;
10056 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000010057 case ISD::ADD:
10058 case ISD::MUL:
10059 case ISD::AND:
10060 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000010061 case ISD::XOR:
10062 Commute = true;
10063 // fallthrough
10064 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000010065 SDValue N0 = Op.getOperand(0);
10066 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000010067 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010068 return false;
10069 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000010070 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010071 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000010072 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000010073 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000010074 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010075 }
10076 }
10077
10078 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000010079 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000010080}
10081
Evan Cheng60c07e12006-07-05 22:17:51 +000010082//===----------------------------------------------------------------------===//
10083// X86 Inline Assembly Support
10084//===----------------------------------------------------------------------===//
10085
Chris Lattnerb8105652009-07-20 17:51:36 +000010086static bool LowerToBSwap(CallInst *CI) {
10087 // FIXME: this should verify that we are targetting a 486 or better. If not,
10088 // we will turn this bswap into something that will be lowered to logical ops
10089 // instead of emitting the bswap asm. For now, we don't support 486 or lower
10090 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000010091
Chris Lattnerb8105652009-07-20 17:51:36 +000010092 // Verify this is a simple bswap.
10093 if (CI->getNumOperands() != 2 ||
Eric Christopher551754c2010-04-16 23:37:20 +000010094 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010095 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000010096 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010097
Chris Lattnerb8105652009-07-20 17:51:36 +000010098 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
10099 if (!Ty || Ty->getBitWidth() % 16 != 0)
10100 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010101
Chris Lattnerb8105652009-07-20 17:51:36 +000010102 // Okay, we can do this xform, do so now.
10103 const Type *Tys[] = { Ty };
10104 Module *M = CI->getParent()->getParent()->getParent();
10105 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000010106
Eric Christopher551754c2010-04-16 23:37:20 +000010107 Value *Op = CI->getOperand(1);
Chris Lattnerb8105652009-07-20 17:51:36 +000010108 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000010109
Chris Lattnerb8105652009-07-20 17:51:36 +000010110 CI->replaceAllUsesWith(Op);
10111 CI->eraseFromParent();
10112 return true;
10113}
10114
10115bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
10116 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10117 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
10118
10119 std::string AsmStr = IA->getAsmString();
10120
10121 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010122 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000010123 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
10124
10125 switch (AsmPieces.size()) {
10126 default: return false;
10127 case 1:
10128 AsmStr = AsmPieces[0];
10129 AsmPieces.clear();
10130 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
10131
10132 // bswap $0
10133 if (AsmPieces.size() == 2 &&
10134 (AsmPieces[0] == "bswap" ||
10135 AsmPieces[0] == "bswapq" ||
10136 AsmPieces[0] == "bswapl") &&
10137 (AsmPieces[1] == "$0" ||
10138 AsmPieces[1] == "${0:q}")) {
10139 // No need to check constraints, nothing other than the equivalent of
10140 // "=r,0" would be valid here.
10141 return LowerToBSwap(CI);
10142 }
10143 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010144 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010145 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010146 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010147 AsmPieces[1] == "$$8," &&
10148 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010149 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
10150 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000010151 const std::string &Constraints = IA->getConstraintString();
10152 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000010153 std::sort(AsmPieces.begin(), AsmPieces.end());
10154 if (AsmPieces.size() == 4 &&
10155 AsmPieces[0] == "~{cc}" &&
10156 AsmPieces[1] == "~{dirflag}" &&
10157 AsmPieces[2] == "~{flags}" &&
10158 AsmPieces[3] == "~{fpsr}") {
10159 return LowerToBSwap(CI);
10160 }
Chris Lattnerb8105652009-07-20 17:51:36 +000010161 }
10162 break;
10163 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010164 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000010165 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010166 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
10167 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
10168 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010169 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000010170 SplitString(AsmPieces[0], Words, " \t");
10171 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
10172 Words.clear();
10173 SplitString(AsmPieces[1], Words, " \t");
10174 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
10175 Words.clear();
10176 SplitString(AsmPieces[2], Words, " \t,");
10177 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
10178 Words[2] == "%edx") {
10179 return LowerToBSwap(CI);
10180 }
10181 }
10182 }
10183 }
10184 break;
10185 }
10186 return false;
10187}
10188
10189
10190
Chris Lattnerf4dff842006-07-11 02:54:03 +000010191/// getConstraintType - Given a constraint letter, return the type of
10192/// constraint it is for this target.
10193X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010194X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10195 if (Constraint.size() == 1) {
10196 switch (Constraint[0]) {
10197 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010198 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010199 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010200 case 'r':
10201 case 'R':
10202 case 'l':
10203 case 'q':
10204 case 'Q':
10205 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010206 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010207 case 'Y':
10208 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010209 case 'e':
10210 case 'Z':
10211 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010212 default:
10213 break;
10214 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010215 }
Chris Lattner4234f572007-03-25 02:14:49 +000010216 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010217}
10218
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010219/// LowerXConstraint - try to replace an X constraint, which matches anything,
10220/// with another that has more specific requirements based on the type of the
10221/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010222const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010223LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010224 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10225 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010226 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010227 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010228 return "Y";
10229 if (Subtarget->hasSSE1())
10230 return "x";
10231 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010232
Chris Lattner5e764232008-04-26 23:02:14 +000010233 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010234}
10235
Chris Lattner48884cd2007-08-25 00:47:38 +000010236/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10237/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010238void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010239 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +000010240 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +000010241 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010242 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010243 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010244
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010245 switch (Constraint) {
10246 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010247 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010248 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010249 if (C->getZExtValue() <= 31) {
10250 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010251 break;
10252 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010253 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010254 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010255 case 'J':
10256 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010257 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010258 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10259 break;
10260 }
10261 }
10262 return;
10263 case 'K':
10264 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010265 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010266 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10267 break;
10268 }
10269 }
10270 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010271 case 'N':
10272 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010273 if (C->getZExtValue() <= 255) {
10274 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010275 break;
10276 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010277 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010278 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010279 case 'e': {
10280 // 32-bit signed value
10281 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10282 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010283 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10284 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010285 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010286 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010287 break;
10288 }
10289 // FIXME gcc accepts some relocatable values here too, but only in certain
10290 // memory models; it's complicated.
10291 }
10292 return;
10293 }
10294 case 'Z': {
10295 // 32-bit unsigned value
10296 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10297 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010298 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10299 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010300 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10301 break;
10302 }
10303 }
10304 // FIXME gcc accepts some relocatable values here too, but only in certain
10305 // memory models; it's complicated.
10306 return;
10307 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010308 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010309 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010310 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010311 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010312 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010313 break;
10314 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010315
Chris Lattnerdc43a882007-05-03 16:52:29 +000010316 // If we are in non-pic codegen mode, we allow the address of a global (with
10317 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010318 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010319 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010320
Chris Lattner49921962009-05-08 18:23:14 +000010321 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10322 while (1) {
10323 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10324 Offset += GA->getOffset();
10325 break;
10326 } else if (Op.getOpcode() == ISD::ADD) {
10327 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10328 Offset += C->getZExtValue();
10329 Op = Op.getOperand(0);
10330 continue;
10331 }
10332 } else if (Op.getOpcode() == ISD::SUB) {
10333 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10334 Offset += -C->getZExtValue();
10335 Op = Op.getOperand(0);
10336 continue;
10337 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010338 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010339
Chris Lattner49921962009-05-08 18:23:14 +000010340 // Otherwise, this isn't something we can handle, reject it.
10341 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010342 }
Eric Christopherfd179292009-08-27 18:07:15 +000010343
Dan Gohman46510a72010-04-15 01:51:59 +000010344 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010345 // If we require an extra load to get this address, as in PIC mode, we
10346 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010347 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10348 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010349 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010350
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010351 if (hasMemory)
10352 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10353 else
10354 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010355 Result = Op;
10356 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010357 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010358 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010359
Gabor Greifba36cb52008-08-28 21:40:38 +000010360 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010361 Ops.push_back(Result);
10362 return;
10363 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010364 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10365 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010366}
10367
Chris Lattner259e97c2006-01-31 19:43:35 +000010368std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010369getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010370 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010371 if (Constraint.size() == 1) {
10372 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010373 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010374 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010375 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10376 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010377 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010378 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10379 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10380 X86::R10D,X86::R11D,X86::R12D,
10381 X86::R13D,X86::R14D,X86::R15D,
10382 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010383 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010384 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10385 X86::SI, X86::DI, X86::R8W,X86::R9W,
10386 X86::R10W,X86::R11W,X86::R12W,
10387 X86::R13W,X86::R14W,X86::R15W,
10388 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010389 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010390 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10391 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10392 X86::R10B,X86::R11B,X86::R12B,
10393 X86::R13B,X86::R14B,X86::R15B,
10394 X86::BPL, X86::SPL, 0);
10395
Owen Anderson825b72b2009-08-11 20:47:22 +000010396 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010397 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10398 X86::RSI, X86::RDI, X86::R8, X86::R9,
10399 X86::R10, X86::R11, X86::R12,
10400 X86::R13, X86::R14, X86::R15,
10401 X86::RBP, X86::RSP, 0);
10402
10403 break;
10404 }
Eric Christopherfd179292009-08-27 18:07:15 +000010405 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010406 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010407 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010408 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010409 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010410 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010411 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010412 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010413 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010414 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10415 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010416 }
10417 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010418
Chris Lattner1efa40f2006-02-22 00:56:39 +000010419 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010420}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010421
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010422std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010423X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010424 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010425 // First, see if this is a constraint that directly corresponds to an LLVM
10426 // register class.
10427 if (Constraint.size() == 1) {
10428 // GCC Constraint Letters
10429 switch (Constraint[0]) {
10430 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010431 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010432 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010433 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010434 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010435 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010436 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010437 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010438 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010439 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010440 case 'R': // LEGACY_REGS
10441 if (VT == MVT::i8)
10442 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10443 if (VT == MVT::i16)
10444 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10445 if (VT == MVT::i32 || !Subtarget->is64Bit())
10446 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10447 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010448 case 'f': // FP Stack registers.
10449 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10450 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010451 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010452 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010453 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010454 return std::make_pair(0U, X86::RFP64RegisterClass);
10455 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010456 case 'y': // MMX_REGS if MMX allowed.
10457 if (!Subtarget->hasMMX()) break;
10458 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010459 case 'Y': // SSE_REGS if SSE2 allowed
10460 if (!Subtarget->hasSSE2()) break;
10461 // FALL THROUGH.
10462 case 'x': // SSE_REGS if SSE1 allowed
10463 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010464
Owen Anderson825b72b2009-08-11 20:47:22 +000010465 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010466 default: break;
10467 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010468 case MVT::f32:
10469 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010470 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010471 case MVT::f64:
10472 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010473 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010474 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010475 case MVT::v16i8:
10476 case MVT::v8i16:
10477 case MVT::v4i32:
10478 case MVT::v2i64:
10479 case MVT::v4f32:
10480 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010481 return std::make_pair(0U, X86::VR128RegisterClass);
10482 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010483 break;
10484 }
10485 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010486
Chris Lattnerf76d1802006-07-31 23:26:50 +000010487 // Use the default implementation in TargetLowering to convert the register
10488 // constraint into a member of a register class.
10489 std::pair<unsigned, const TargetRegisterClass*> Res;
10490 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010491
10492 // Not found as a standard register?
10493 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010494 // Map st(0) -> st(7) -> ST0
10495 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10496 tolower(Constraint[1]) == 's' &&
10497 tolower(Constraint[2]) == 't' &&
10498 Constraint[3] == '(' &&
10499 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10500 Constraint[5] == ')' &&
10501 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010502
Chris Lattner56d77c72009-09-13 22:41:48 +000010503 Res.first = X86::ST0+Constraint[4]-'0';
10504 Res.second = X86::RFP80RegisterClass;
10505 return Res;
10506 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010507
Chris Lattner56d77c72009-09-13 22:41:48 +000010508 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010509 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010510 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010511 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010512 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010513 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010514
10515 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010516 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010517 Res.first = X86::EFLAGS;
10518 Res.second = X86::CCRRegisterClass;
10519 return Res;
10520 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010521
Dale Johannesen330169f2008-11-13 21:52:36 +000010522 // 'A' means EAX + EDX.
10523 if (Constraint == "A") {
10524 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010525 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010526 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010527 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010528 return Res;
10529 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010530
Chris Lattnerf76d1802006-07-31 23:26:50 +000010531 // Otherwise, check to see if this is a register class of the wrong value
10532 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10533 // turn into {ax},{dx}.
10534 if (Res.second->hasType(VT))
10535 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010536
Chris Lattnerf76d1802006-07-31 23:26:50 +000010537 // All of the single-register GCC register classes map their values onto
10538 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10539 // really want an 8-bit or 32-bit register, map to the appropriate register
10540 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010541 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010542 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010543 unsigned DestReg = 0;
10544 switch (Res.first) {
10545 default: break;
10546 case X86::AX: DestReg = X86::AL; break;
10547 case X86::DX: DestReg = X86::DL; break;
10548 case X86::CX: DestReg = X86::CL; break;
10549 case X86::BX: DestReg = X86::BL; break;
10550 }
10551 if (DestReg) {
10552 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010553 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010554 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010555 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010556 unsigned DestReg = 0;
10557 switch (Res.first) {
10558 default: break;
10559 case X86::AX: DestReg = X86::EAX; break;
10560 case X86::DX: DestReg = X86::EDX; break;
10561 case X86::CX: DestReg = X86::ECX; break;
10562 case X86::BX: DestReg = X86::EBX; break;
10563 case X86::SI: DestReg = X86::ESI; break;
10564 case X86::DI: DestReg = X86::EDI; break;
10565 case X86::BP: DestReg = X86::EBP; break;
10566 case X86::SP: DestReg = X86::ESP; break;
10567 }
10568 if (DestReg) {
10569 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010570 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010571 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010572 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010573 unsigned DestReg = 0;
10574 switch (Res.first) {
10575 default: break;
10576 case X86::AX: DestReg = X86::RAX; break;
10577 case X86::DX: DestReg = X86::RDX; break;
10578 case X86::CX: DestReg = X86::RCX; break;
10579 case X86::BX: DestReg = X86::RBX; break;
10580 case X86::SI: DestReg = X86::RSI; break;
10581 case X86::DI: DestReg = X86::RDI; break;
10582 case X86::BP: DestReg = X86::RBP; break;
10583 case X86::SP: DestReg = X86::RSP; break;
10584 }
10585 if (DestReg) {
10586 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010587 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010588 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010589 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010590 } else if (Res.second == X86::FR32RegisterClass ||
10591 Res.second == X86::FR64RegisterClass ||
10592 Res.second == X86::VR128RegisterClass) {
10593 // Handle references to XMM physical registers that got mapped into the
10594 // wrong class. This can happen with constraints like {xmm0} where the
10595 // target independent register mapper will just pick the first match it can
10596 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010597 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010598 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010599 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010600 Res.second = X86::FR64RegisterClass;
10601 else if (X86::VR128RegisterClass->hasType(VT))
10602 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010603 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010604
Chris Lattnerf76d1802006-07-31 23:26:50 +000010605 return Res;
10606}