blob: 491c118fe3b06dae66f1c6125ddceed0eca9e366 [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"
Chris Lattner017ec352010-02-08 22:33:55 +000019#include "X86MCTargetExpr.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000032#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000034#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000035#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000036#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000037#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000038#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000039#include "llvm/MC/MCContext.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000046#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000048#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000051#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000053using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Mon P Wang3c81d352008-11-23 04:37:22 +000057static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000058DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000059
Dan Gohman2f67df72009-09-03 17:18:51 +000060// Disable16Bit - 16-bit operations typically have a larger encoding than
61// corresponding 32-bit instructions, and 16-bit code is slow on some
62// processors. This is an experimental flag to disable 16-bit operations
63// (which forces them to be Legalized to 32-bit operations).
64static cl::opt<bool>
65Disable16Bit("disable-16bit", cl::Hidden,
66 cl::desc("Disable use of 16-bit instructions"));
67
Evan Cheng10e86422008-04-25 19:11:04 +000068// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000069static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000070 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000071
Chris Lattnerf0144122009-07-28 03:13:23 +000072static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
73 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
74 default: llvm_unreachable("unknown subtarget type");
75 case X86Subtarget::isDarwin:
Bill Wendling757e75b2010-03-15 19:04:37 +000076 if (TM.getSubtarget<X86Subtarget>().is64Bit())
77 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000078 return new TargetLoweringObjectFileMachO();
Chris Lattnerf0144122009-07-28 03:13:23 +000079 case X86Subtarget::isELF:
Anton Korobeynikov9184b252010-02-15 22:35:59 +000080 if (TM.getSubtarget<X86Subtarget>().is64Bit())
81 return new X8664_ELFTargetObjectFile(TM);
82 return new X8632_ELFTargetObjectFile(TM);
Chris Lattnerf0144122009-07-28 03:13:23 +000083 case X86Subtarget::isMingw:
84 case X86Subtarget::isCygwin:
85 case X86Subtarget::isWindows:
86 return new TargetLoweringObjectFileCOFF();
87 }
Chris Lattnerf0144122009-07-28 03:13:23 +000088}
89
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000090X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000091 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000092 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000093 X86ScalarSSEf64 = Subtarget->hasSSE2();
94 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000095 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000096
Anton Korobeynikov2365f512007-07-14 14:06:15 +000097 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000098 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000099
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000100 // Set up the TargetLowering object.
101
102 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +0000103 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000104 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +0000105 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000106 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000107
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000108 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000109 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000110 setUseUnderscoreSetJmp(false);
111 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000112 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000113 // MS runtime is weird: it exports _setjmp, but longjmp!
114 setUseUnderscoreSetJmp(true);
115 setUseUnderscoreLongJmp(false);
116 } else {
117 setUseUnderscoreSetJmp(true);
118 setUseUnderscoreLongJmp(true);
119 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000120
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000121 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000122 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman2f67df72009-09-03 17:18:51 +0000123 if (!Disable16Bit)
124 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000126 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000128
Owen Anderson825b72b2009-08-11 20:47:22 +0000129 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000130
Scott Michelfdc40a02009-02-17 22:15:04 +0000131 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000133 if (!Disable16Bit)
134 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000136 if (!Disable16Bit)
137 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
139 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000140
141 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000142 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
143 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
144 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
145 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
146 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
147 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000148
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000149 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
150 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000151 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
152 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
153 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000154
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000156 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
157 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000158 } else if (!UseSoftFloat) {
159 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000160 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000162 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000163 // We have an algorithm for SSE2, and we turn this into a 64-bit
164 // FILD for other targets.
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000166 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167
168 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
169 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000170 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
171 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000172
Devang Patel6a784892009-06-05 18:48:29 +0000173 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000174 // SSE has no i16 to fp conversion, only i32
175 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000176 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000177 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000179 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
181 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000182 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000183 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
185 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000186 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000187
Dale Johannesen73328d12007-09-19 23:55:34 +0000188 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
189 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
191 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000192
Evan Cheng02568ff2006-01-30 22:13:22 +0000193 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
194 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
196 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000197
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000198 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000200 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000202 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
204 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000205 }
206
207 // Handle FP_TO_UINT by promoting the destination to a larger signed
208 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
210 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
211 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000212
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
215 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000216 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000217 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000218 // Expand FP_TO_UINT into a select.
219 // FIXME: We would like to use a Custom expander here eventually to do
220 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000222 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000223 // With SSE3 we can use fisttpll to convert to a signed i64; without
224 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000226 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000227
Chris Lattner399610a2006-12-05 18:22:22 +0000228 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000229 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
231 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000232 }
Chris Lattner21f66852005-12-23 05:15:23 +0000233
Dan Gohmanb00ee212008-02-18 19:34:53 +0000234 // Scalar integer divide and remainder are lowered to use operations that
235 // produce two results, to match the available instructions. This exposes
236 // the two-result form to trivial CSE, which is able to combine x/y and x%y
237 // into a single instruction.
238 //
239 // Scalar integer multiply-high is also lowered to use two-result
240 // operations, to match the available instructions. However, plain multiply
241 // (low) operations are left as Legal, as there are single-result
242 // instructions for this in x86. Using the two-result multiply instructions
243 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
245 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
246 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
247 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
248 setOperationAction(ISD::SREM , MVT::i8 , Expand);
249 setOperationAction(ISD::UREM , MVT::i8 , Expand);
250 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
251 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
252 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
253 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
254 setOperationAction(ISD::SREM , MVT::i16 , Expand);
255 setOperationAction(ISD::UREM , MVT::i16 , Expand);
256 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
257 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
258 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
259 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
260 setOperationAction(ISD::SREM , MVT::i32 , Expand);
261 setOperationAction(ISD::UREM , MVT::i32 , Expand);
262 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
263 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
264 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
265 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
266 setOperationAction(ISD::SREM , MVT::i64 , Expand);
267 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000268
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
270 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
271 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
272 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000274 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
275 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
276 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
277 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
278 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
279 setOperationAction(ISD::FREM , MVT::f32 , Expand);
280 setOperationAction(ISD::FREM , MVT::f64 , Expand);
281 setOperationAction(ISD::FREM , MVT::f80 , Expand);
282 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000283
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
285 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
286 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
287 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000288 if (Disable16Bit) {
289 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
290 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
291 } else {
292 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
293 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
294 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
296 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
297 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000298 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
300 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
301 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000302 }
303
Owen Anderson825b72b2009-08-11 20:47:22 +0000304 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
305 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000306
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000307 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000308 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000309 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000310 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000311 if (Disable16Bit)
312 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
313 else
314 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000315 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
316 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
317 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
318 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
319 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000320 if (Disable16Bit)
321 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
322 else
323 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000324 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
325 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
326 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
327 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
330 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000331 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000333
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000334 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
336 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
337 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
338 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000339 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
341 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000342 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000343 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
345 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
346 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
347 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000348 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000349 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000350 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
352 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
353 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000354 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000355 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
356 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
357 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000358 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000359
Evan Chengd2cde682008-03-10 19:38:10 +0000360 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000361 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000362
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000363 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000365
Mon P Wang63307c32008-05-05 19:05:59 +0000366 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
368 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
369 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
370 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000371
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
373 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
374 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000376
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000377 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000378 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
379 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
380 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
381 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
382 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
383 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
384 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000385 }
386
Evan Cheng3c992d22006-03-07 02:02:57 +0000387 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000388 if (!Subtarget->isTargetDarwin() &&
389 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000390 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000391 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000392 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000393
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
395 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
396 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
397 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000398 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000399 setExceptionPointerRegister(X86::RAX);
400 setExceptionSelectorRegister(X86::RDX);
401 } else {
402 setExceptionPointerRegister(X86::EAX);
403 setExceptionSelectorRegister(X86::EDX);
404 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000405 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
406 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000407
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000409
Owen Anderson825b72b2009-08-11 20:47:22 +0000410 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000411
Nate Begemanacc398c2006-01-25 18:21:52 +0000412 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::VASTART , MVT::Other, Custom);
414 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000415 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::VAARG , MVT::Other, Custom);
417 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000418 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::VAARG , MVT::Other, Expand);
420 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000421 }
Evan Chengae642192007-03-02 23:16:35 +0000422
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
424 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000425 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000427 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000429 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000430 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000431
Evan Chengc7ce29b2009-02-13 22:36:38 +0000432 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000433 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000434 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
436 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437
Evan Cheng223547a2006-01-31 22:28:30 +0000438 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FABS , MVT::f64, Custom);
440 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000441
442 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FNEG , MVT::f64, Custom);
444 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000445
Evan Cheng68c47cb2007-01-05 07:55:56 +0000446 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
448 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000449
Evan Chengd25e9e82006-02-02 00:28:23 +0000450 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000451 setOperationAction(ISD::FSIN , MVT::f64, Expand);
452 setOperationAction(ISD::FCOS , MVT::f64, Expand);
453 setOperationAction(ISD::FSIN , MVT::f32, Expand);
454 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000455
Chris Lattnera54aa942006-01-29 06:26:08 +0000456 // Expand FP immediates into loads from the stack, except for the special
457 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000458 addLegalFPImmediate(APFloat(+0.0)); // xorpd
459 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000460 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461 // Use SSE for f32, x87 for f64.
462 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
464 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000465
466 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000468
469 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000473
474 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
476 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477
478 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000479 setOperationAction(ISD::FSIN , MVT::f32, Expand);
480 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000481
Nate Begemane1795842008-02-14 08:57:00 +0000482 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000483 addLegalFPImmediate(APFloat(+0.0f)); // xorps
484 addLegalFPImmediate(APFloat(+0.0)); // FLD0
485 addLegalFPImmediate(APFloat(+1.0)); // FLD1
486 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
487 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
488
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000489 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
491 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000492 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000493 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000494 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000495 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000496 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
497 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000498
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
500 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
501 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
502 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000503
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000504 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
506 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000507 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000508 addLegalFPImmediate(APFloat(+0.0)); // FLD0
509 addLegalFPImmediate(APFloat(+1.0)); // FLD1
510 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
511 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000512 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
513 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
514 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
515 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000516 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000517
Dale Johannesen59a58732007-08-05 18:49:15 +0000518 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000519 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000520 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
521 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
522 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000523 {
524 bool ignored;
525 APFloat TmpFlt(+0.0);
526 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
527 &ignored);
528 addLegalFPImmediate(TmpFlt); // FLD0
529 TmpFlt.changeSign();
530 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
531 APFloat TmpFlt2(+1.0);
532 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
533 &ignored);
534 addLegalFPImmediate(TmpFlt2); // FLD1
535 TmpFlt2.changeSign();
536 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
537 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000538
Evan Chengc7ce29b2009-02-13 22:36:38 +0000539 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
541 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000542 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000543 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000544
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000545 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000546 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
547 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
548 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000549
Owen Anderson825b72b2009-08-11 20:47:22 +0000550 setOperationAction(ISD::FLOG, MVT::f80, Expand);
551 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
552 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
553 setOperationAction(ISD::FEXP, MVT::f80, Expand);
554 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000555
Mon P Wangf007a8b2008-11-06 05:31:54 +0000556 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000557 // (for widening) or expand (for scalarization). Then we will selectively
558 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000559 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
560 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
561 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
576 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
577 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
605 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
606 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000609 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000610 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
611 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
612 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
613 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
614 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
615 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
616 setTruncStoreAction((MVT::SimpleValueType)VT,
617 (MVT::SimpleValueType)InnerVT, Expand);
618 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
619 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
620 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000621 }
622
Evan Chengc7ce29b2009-02-13 22:36:38 +0000623 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
624 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000625 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000626 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
627 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
628 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
629 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
630 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000631
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
633 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
634 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
635 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000636
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
638 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
639 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
640 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000641
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
643 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000644
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 setOperationAction(ISD::AND, MVT::v8i8, Promote);
646 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
647 setOperationAction(ISD::AND, MVT::v4i16, Promote);
648 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
649 setOperationAction(ISD::AND, MVT::v2i32, Promote);
650 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
651 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000652
Owen Anderson825b72b2009-08-11 20:47:22 +0000653 setOperationAction(ISD::OR, MVT::v8i8, Promote);
654 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
655 setOperationAction(ISD::OR, MVT::v4i16, Promote);
656 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
657 setOperationAction(ISD::OR, MVT::v2i32, Promote);
658 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
659 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
662 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
663 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
664 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
665 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
666 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
667 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000668
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
670 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
671 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
672 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
673 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
674 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
675 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
676 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
677 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000678
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
680 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
681 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
682 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
683 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000684
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
686 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
687 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
688 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000689
Owen Anderson825b72b2009-08-11 20:47:22 +0000690 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
691 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
692 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
693 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000694
Owen Anderson825b72b2009-08-11 20:47:22 +0000695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000696
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
698 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
699 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
700 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
701 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
702 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
703 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000704 }
705
Evan Cheng92722532009-03-26 23:06:32 +0000706 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000708
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
710 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
711 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
712 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
713 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
714 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
715 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
716 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
717 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
718 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
719 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
720 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000721 }
722
Evan Cheng92722532009-03-26 23:06:32 +0000723 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000725
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000726 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
727 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
729 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
730 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
731 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000732
Owen Anderson825b72b2009-08-11 20:47:22 +0000733 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
734 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
735 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
736 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
737 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
738 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
739 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
740 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
741 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
742 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
743 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
744 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
745 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
746 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
747 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
748 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000749
Owen Anderson825b72b2009-08-11 20:47:22 +0000750 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
751 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
752 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
753 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000754
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
756 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
757 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
758 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
759 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000760
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000761 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
762 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
763 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
764 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
765 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
766
Evan Cheng2c3ae372006-04-12 21:21:57 +0000767 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
769 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000770 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000771 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000772 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000773 // Do not attempt to custom lower non-128-bit vectors
774 if (!VT.is128BitVector())
775 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 setOperationAction(ISD::BUILD_VECTOR,
777 VT.getSimpleVT().SimpleTy, Custom);
778 setOperationAction(ISD::VECTOR_SHUFFLE,
779 VT.getSimpleVT().SimpleTy, Custom);
780 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
781 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000782 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000783
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
785 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
786 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
787 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
788 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
789 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000790
Nate Begemancdd1eec2008-02-12 22:51:28 +0000791 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
793 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000794 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000795
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000796 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
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 }
Owen Andersond6662ad2009-08-10 20:46:15 +0000805 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000807 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000809 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000811 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000812 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000813 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000815 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000816
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000818
Evan Cheng2c3ae372006-04-12 21:21:57 +0000819 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000820 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
821 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
822 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
823 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000824
Owen Anderson825b72b2009-08-11 20:47:22 +0000825 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
826 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000827 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
829 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000830 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000831 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000832
Nate Begeman14d12ca2008-02-11 04:19:36 +0000833 if (Subtarget->hasSSE41()) {
834 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000836
837 // i8 and i16 vectors are custom , because the source register and source
838 // source memory operand types are not the same width. f32 vectors are
839 // custom since the immediate controlling the insert encodes additional
840 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
842 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
843 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
844 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000845
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
847 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
848 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
849 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000850
851 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
853 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000854 }
855 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000856
Nate Begeman30a0de92008-07-17 16:51:19 +0000857 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000858 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000859 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000860
David Greene9b9838d2009-06-29 16:47:10 +0000861 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
863 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
864 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
865 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000866
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
868 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
869 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
870 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
871 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
872 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
873 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
874 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
875 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
876 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
877 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
878 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
879 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
880 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
881 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000882
883 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
885 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
886 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
887 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
888 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
889 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
890 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
891 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
892 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
893 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
894 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
895 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
896 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
897 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000898
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
900 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
901 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
902 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
905 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
906 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
907 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
908 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000909
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
911 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
912 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
913 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
914 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
915 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000916
917#if 0
918 // Not sure we want to do this since there are no 256-bit integer
919 // operations in AVX
920
921 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
922 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000923 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
924 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000925
926 // Do not attempt to custom lower non-power-of-2 vectors
927 if (!isPowerOf2_32(VT.getVectorNumElements()))
928 continue;
929
930 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
931 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
932 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
933 }
934
935 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000938 }
David Greene9b9838d2009-06-29 16:47:10 +0000939#endif
940
941#if 0
942 // Not sure we want to do this since there are no 256-bit integer
943 // operations in AVX
944
945 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
946 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000947 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
948 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000949
950 if (!VT.is256BitVector()) {
951 continue;
952 }
953 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000954 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000955 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000956 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000957 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000958 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000959 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000961 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000963 }
964
Owen Anderson825b72b2009-08-11 20:47:22 +0000965 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000966#endif
967 }
968
Evan Cheng6be2c582006-04-05 23:38:46 +0000969 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000970 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000971
Bill Wendling74c37652008-12-09 22:08:41 +0000972 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000973 setOperationAction(ISD::SADDO, MVT::i32, Custom);
974 setOperationAction(ISD::SADDO, MVT::i64, Custom);
975 setOperationAction(ISD::UADDO, MVT::i32, Custom);
976 setOperationAction(ISD::UADDO, MVT::i64, Custom);
977 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
978 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
979 setOperationAction(ISD::USUBO, MVT::i32, Custom);
980 setOperationAction(ISD::USUBO, MVT::i64, Custom);
981 setOperationAction(ISD::SMULO, MVT::i32, Custom);
982 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000983
Evan Chengd54f2d52009-03-31 19:38:51 +0000984 if (!Subtarget->is64Bit()) {
985 // These libcalls are not available in 32-bit.
986 setLibcallName(RTLIB::SHL_I128, 0);
987 setLibcallName(RTLIB::SRL_I128, 0);
988 setLibcallName(RTLIB::SRA_I128, 0);
989 }
990
Evan Cheng206ee9d2006-07-07 08:33:52 +0000991 // We have target-specific dag combine patterns for the following nodes:
992 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000993 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000994 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000995 setTargetDAGCombine(ISD::SHL);
996 setTargetDAGCombine(ISD::SRA);
997 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000998 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +0000999 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +00001000 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +00001001 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001002 if (Subtarget->is64Bit())
1003 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001004
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001005 computeRegisterProperties();
1006
Evan Cheng87ed7162006-02-14 08:25:08 +00001007 // FIXME: These should be based on subtarget info. Plus, the values should
1008 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001009 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1010 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
1011 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001012 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001013 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001014}
1015
Scott Michel5b8f82e2008-03-10 15:42:14 +00001016
Owen Anderson825b72b2009-08-11 20:47:22 +00001017MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1018 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001019}
1020
1021
Evan Cheng29286502008-01-23 23:17:41 +00001022/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1023/// the desired ByVal argument alignment.
1024static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1025 if (MaxAlign == 16)
1026 return;
1027 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1028 if (VTy->getBitWidth() == 128)
1029 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001030 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1031 unsigned EltAlign = 0;
1032 getMaxByValAlign(ATy->getElementType(), EltAlign);
1033 if (EltAlign > MaxAlign)
1034 MaxAlign = EltAlign;
1035 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1036 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1037 unsigned EltAlign = 0;
1038 getMaxByValAlign(STy->getElementType(i), EltAlign);
1039 if (EltAlign > MaxAlign)
1040 MaxAlign = EltAlign;
1041 if (MaxAlign == 16)
1042 break;
1043 }
1044 }
1045 return;
1046}
1047
1048/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1049/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001050/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1051/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001052unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001053 if (Subtarget->is64Bit()) {
1054 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001055 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001056 if (TyAlign > 8)
1057 return TyAlign;
1058 return 8;
1059 }
1060
Evan Cheng29286502008-01-23 23:17:41 +00001061 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001062 if (Subtarget->hasSSE1())
1063 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001064 return Align;
1065}
Chris Lattner2b02a442007-02-25 08:29:00 +00001066
Evan Chengf0df0312008-05-15 08:39:06 +00001067/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001068/// and store operations as a result of memset, memcpy, and memmove
Owen Anderson825b72b2009-08-11 20:47:22 +00001069/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001070/// determining it.
Owen Andersone50ed302009-08-10 22:56:29 +00001071EVT
Evan Chengf0df0312008-05-15 08:39:06 +00001072X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001073 bool isSrcConst, bool isSrcStr,
1074 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001075 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1076 // linux. This is because the stack realignment code can't handle certain
1077 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001078 const Function *F = DAG.getMachineFunction().getFunction();
1079 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1080 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001081 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001082 return MVT::v4i32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001083 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
Owen Anderson825b72b2009-08-11 20:47:22 +00001084 return MVT::v4f32;
Chris Lattner4002a1b2008-10-28 05:49:35 +00001085 }
Evan Chengf0df0312008-05-15 08:39:06 +00001086 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001087 return MVT::i64;
1088 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001089}
1090
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001091/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1092/// current function. The returned value is a member of the
1093/// MachineJumpTableInfo::JTEntryKind enum.
1094unsigned X86TargetLowering::getJumpTableEncoding() const {
1095 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1096 // symbol.
1097 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1098 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001099 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001100
1101 // Otherwise, use the normal jump table encoding heuristics.
1102 return TargetLowering::getJumpTableEncoding();
1103}
1104
Chris Lattner589c6f62010-01-26 06:28:43 +00001105/// getPICBaseSymbol - Return the X86-32 PIC base.
1106MCSymbol *
1107X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1108 MCContext &Ctx) const {
1109 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner98cdab52010-03-10 02:25:11 +00001110 return Ctx.GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix())+
1111 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001112}
1113
1114
Chris Lattnerc64daab2010-01-26 05:02:42 +00001115const MCExpr *
1116X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1117 const MachineBasicBlock *MBB,
1118 unsigned uid,MCContext &Ctx) const{
1119 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1120 Subtarget->isPICStyleGOT());
1121 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1122 // entries.
Chris Lattner1b2eb0e2010-03-13 21:04:28 +00001123 return X86MCTargetExpr::Create(MBB->getSymbol(),
Chris Lattner017ec352010-02-08 22:33:55 +00001124 X86MCTargetExpr::GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001125}
1126
Evan Chengcc415862007-11-09 01:32:10 +00001127/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1128/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001129SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001130 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001131 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001132 // This doesn't have DebugLoc associated with it, but is not really the
1133 // same as a Register.
1134 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1135 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001136 return Table;
1137}
1138
Chris Lattner589c6f62010-01-26 06:28:43 +00001139/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1140/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1141/// MCExpr.
1142const MCExpr *X86TargetLowering::
1143getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1144 MCContext &Ctx) const {
1145 // X86-64 uses RIP relative addressing based on the jump table label.
1146 if (Subtarget->isPICStyleRIPRel())
1147 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1148
1149 // Otherwise, the reference is relative to the PIC base.
1150 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1151}
1152
Bill Wendlingb4202b82009-07-01 18:50:55 +00001153/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001154unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001155 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001156}
1157
Chris Lattner2b02a442007-02-25 08:29:00 +00001158//===----------------------------------------------------------------------===//
1159// Return Value Calling Convention Implementation
1160//===----------------------------------------------------------------------===//
1161
Chris Lattner59ed56b2007-02-28 04:55:35 +00001162#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001163
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001164bool
1165X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1166 const SmallVectorImpl<EVT> &OutTys,
1167 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1168 SelectionDAG &DAG) {
1169 SmallVector<CCValAssign, 16> RVLocs;
1170 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1171 RVLocs, *DAG.getContext());
1172 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1173}
1174
Dan Gohman98ca4f22009-08-05 01:29:28 +00001175SDValue
1176X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001177 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001178 const SmallVectorImpl<ISD::OutputArg> &Outs,
1179 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001180
Chris Lattner9774c912007-02-27 05:28:59 +00001181 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001182 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1183 RVLocs, *DAG.getContext());
1184 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001185
Evan Chengdcea1632010-02-04 02:40:39 +00001186 // Add the regs to the liveout set for the function.
1187 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1188 for (unsigned i = 0; i != RVLocs.size(); ++i)
1189 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1190 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001191
Dan Gohman475871a2008-07-27 21:46:04 +00001192 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001193
Dan Gohman475871a2008-07-27 21:46:04 +00001194 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001195 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1196 // Operand #1 = Bytes To Pop
Dan Gohman2f67df72009-09-03 17:18:51 +00001197 RetOps.push_back(DAG.getTargetConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001198
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001199 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001200 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1201 CCValAssign &VA = RVLocs[i];
1202 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001203 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001204
Chris Lattner447ff682008-03-11 03:23:40 +00001205 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1206 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001207 if (VA.getLocReg() == X86::ST0 ||
1208 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001209 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1210 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001211 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001212 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001213 RetOps.push_back(ValToCopy);
1214 // Don't emit a copytoreg.
1215 continue;
1216 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001217
Evan Cheng242b38b2009-02-23 09:03:22 +00001218 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1219 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001220 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001221 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001222 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001223 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001224 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001225 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001226 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001227 }
1228
Dale Johannesendd64c412009-02-04 00:33:20 +00001229 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001230 Flag = Chain.getValue(1);
1231 }
Dan Gohman61a92132008-04-21 23:59:07 +00001232
1233 // The x86-64 ABI for returning structs by value requires that we copy
1234 // the sret argument into %rax for the return. We saved the argument into
1235 // a virtual register in the entry block, so now we copy the value out
1236 // and into %rax.
1237 if (Subtarget->is64Bit() &&
1238 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1239 MachineFunction &MF = DAG.getMachineFunction();
1240 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1241 unsigned Reg = FuncInfo->getSRetReturnReg();
1242 if (!Reg) {
Evan Chengdcea1632010-02-04 02:40:39 +00001243 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001244 FuncInfo->setSRetReturnReg(Reg);
1245 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001246 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001247
Dale Johannesendd64c412009-02-04 00:33:20 +00001248 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001249 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001250
1251 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001252 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001253 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001254
Chris Lattner447ff682008-03-11 03:23:40 +00001255 RetOps[0] = Chain; // Update chain.
1256
1257 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001258 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001259 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001260
1261 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001262 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001263}
1264
Dan Gohman98ca4f22009-08-05 01:29:28 +00001265/// LowerCallResult - Lower the result values of a call into the
1266/// appropriate copies out of appropriate physical registers.
1267///
1268SDValue
1269X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001270 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001271 const SmallVectorImpl<ISD::InputArg> &Ins,
1272 DebugLoc dl, SelectionDAG &DAG,
1273 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001274
Chris Lattnere32bbf62007-02-28 07:09:55 +00001275 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001276 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001277 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001278 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001279 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001280 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Chris Lattner3085e152007-02-25 08:59:22 +00001282 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001283 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001284 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001285 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001286
Torok Edwin3f142c32009-02-01 18:15:56 +00001287 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001288 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001290 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001291 }
1292
Chris Lattner8e6da152008-03-10 21:08:41 +00001293 // If this is a call to a function that returns an fp value on the floating
1294 // point stack, but where we prefer to use the value in xmm registers, copy
1295 // 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 +00001296 if ((VA.getLocReg() == X86::ST0 ||
1297 VA.getLocReg() == X86::ST1) &&
1298 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001299 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001300 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001301
Evan Cheng79fb3b42009-02-20 20:43:02 +00001302 SDValue Val;
1303 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001304 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1305 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1306 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001307 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001308 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1310 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001311 } else {
1312 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001313 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001314 Val = Chain.getValue(0);
1315 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001316 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1317 } else {
1318 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1319 CopyVT, InFlag).getValue(1);
1320 Val = Chain.getValue(0);
1321 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001322 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001323
Dan Gohman37eed792009-02-04 17:28:58 +00001324 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001325 // Round the F80 the right size, which also moves to the appropriate xmm
1326 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001327 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001328 // This truncation won't change the value.
1329 DAG.getIntPtrConstant(1));
1330 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001331
Dan Gohman98ca4f22009-08-05 01:29:28 +00001332 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001333 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001334
Dan Gohman98ca4f22009-08-05 01:29:28 +00001335 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001336}
1337
1338
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001339//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001340// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001341//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001342// StdCall calling convention seems to be standard for many Windows' API
1343// routines and around. It differs from C calling convention just a little:
1344// callee should clean up the stack, not caller. Symbols should be also
1345// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001346// For info on fast calling convention see Fast Calling Convention (tail call)
1347// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001348
Dan Gohman98ca4f22009-08-05 01:29:28 +00001349/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001350/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001351static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1352 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001353 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001354
Dan Gohman98ca4f22009-08-05 01:29:28 +00001355 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001356}
1357
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001358/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001359/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360static bool
1361ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1362 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001363 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001364
Dan Gohman98ca4f22009-08-05 01:29:28 +00001365 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001366}
1367
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001368/// IsCalleePop - Determines whether the callee is required to pop its
1369/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001370bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001371 if (IsVarArg)
1372 return false;
1373
Dan Gohman095cc292008-09-13 01:54:27 +00001374 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001375 default:
1376 return false;
1377 case CallingConv::X86_StdCall:
1378 return !Subtarget->is64Bit();
1379 case CallingConv::X86_FastCall:
1380 return !Subtarget->is64Bit();
1381 case CallingConv::Fast:
Dan Gohman1797ed52010-02-08 20:27:50 +00001382 return GuaranteedTailCallOpt;
Chris Lattner29689432010-03-11 00:22:57 +00001383 case CallingConv::GHC:
1384 return GuaranteedTailCallOpt;
Gordon Henriksen86737662008-01-05 16:56:59 +00001385 }
1386}
1387
Dan Gohman095cc292008-09-13 01:54:27 +00001388/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1389/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001390CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001391 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001392 if (CC == CallingConv::GHC)
1393 return CC_X86_64_GHC;
1394 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001395 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001396 else
1397 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001398 }
1399
Gordon Henriksen86737662008-01-05 16:56:59 +00001400 if (CC == CallingConv::X86_FastCall)
1401 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001402 else if (CC == CallingConv::Fast)
1403 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001404 else if (CC == CallingConv::GHC)
1405 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001406 else
1407 return CC_X86_32_C;
1408}
1409
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001410/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1411/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001412/// the specific parameter attribute. The copy will be passed as a byval
1413/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001414static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001415CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001416 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1417 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001418 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001419 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001420 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001421}
1422
Chris Lattner29689432010-03-11 00:22:57 +00001423/// IsTailCallConvention - Return true if the calling convention is one that
1424/// supports tail call optimization.
1425static bool IsTailCallConvention(CallingConv::ID CC) {
1426 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1427}
1428
Evan Cheng0c439eb2010-01-27 00:07:07 +00001429/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1430/// a tailcall target by changing its ABI.
1431static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001432 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001433}
1434
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435SDValue
1436X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001437 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001438 const SmallVectorImpl<ISD::InputArg> &Ins,
1439 DebugLoc dl, SelectionDAG &DAG,
1440 const CCValAssign &VA,
1441 MachineFrameInfo *MFI,
1442 unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001443 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001445 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001446 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001447 EVT ValVT;
1448
1449 // If value is passed by pointer we have address passed instead of the value
1450 // itself.
1451 if (VA.getLocInfo() == CCValAssign::Indirect)
1452 ValVT = VA.getLocVT();
1453 else
1454 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001455
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001456 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001457 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001458 // In case of tail call optimization mark all arguments mutable. Since they
1459 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001460 if (Flags.isByVal()) {
1461 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1462 VA.getLocMemOffset(), isImmutable, false);
1463 return DAG.getFrameIndex(FI, getPointerTy());
1464 } else {
1465 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1466 VA.getLocMemOffset(), isImmutable, false);
1467 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1468 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001469 PseudoSourceValue::getFixedStack(FI), 0,
1470 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001471 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001472}
1473
Dan Gohman475871a2008-07-27 21:46:04 +00001474SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001475X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001476 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001477 bool isVarArg,
1478 const SmallVectorImpl<ISD::InputArg> &Ins,
1479 DebugLoc dl,
1480 SelectionDAG &DAG,
1481 SmallVectorImpl<SDValue> &InVals) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001482 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001483 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001484
Gordon Henriksen86737662008-01-05 16:56:59 +00001485 const Function* Fn = MF.getFunction();
1486 if (Fn->hasExternalLinkage() &&
1487 Subtarget->isTargetCygMing() &&
1488 Fn->getName() == "main")
1489 FuncInfo->setForceFramePointer(true);
1490
Evan Cheng1bc78042006-04-26 01:20:17 +00001491 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001492 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001493 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001494
Chris Lattner29689432010-03-11 00:22:57 +00001495 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1496 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001497
Chris Lattner638402b2007-02-28 07:00:42 +00001498 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001499 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001500 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1501 ArgLocs, *DAG.getContext());
1502 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001503
Chris Lattnerf39f7712007-02-28 05:46:49 +00001504 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001505 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001506 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1507 CCValAssign &VA = ArgLocs[i];
1508 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1509 // places.
1510 assert(VA.getValNo() != LastVal &&
1511 "Don't support value assigned to multiple locs yet");
1512 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001513
Chris Lattnerf39f7712007-02-28 05:46:49 +00001514 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001515 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001516 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001517 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001518 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001519 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001520 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001521 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001522 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001524 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001525 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001526 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001527 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1528 RC = X86::VR64RegisterClass;
1529 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001530 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001531
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001532 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001533 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001534
Chris Lattnerf39f7712007-02-28 05:46:49 +00001535 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1536 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1537 // right size.
1538 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001539 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001540 DAG.getValueType(VA.getValVT()));
1541 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001542 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001543 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001544 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001545 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001546
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001547 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001548 // Handle MMX values passed in XMM regs.
1549 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001550 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1551 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001552 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1553 } else
1554 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001555 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001556 } else {
1557 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001558 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001559 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001560
1561 // If value is passed via pointer - do a load.
1562 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001563 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1564 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001565
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001567 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001568
Dan Gohman61a92132008-04-21 23:59:07 +00001569 // The x86-64 ABI for returning structs by value requires that we copy
1570 // the sret argument into %rax for the return. Save the argument into
1571 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001572 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001573 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1574 unsigned Reg = FuncInfo->getSRetReturnReg();
1575 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001576 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001577 FuncInfo->setSRetReturnReg(Reg);
1578 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001580 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001581 }
1582
Chris Lattnerf39f7712007-02-28 05:46:49 +00001583 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001584 // Align stack specially for tail calls.
1585 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001586 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001587
Evan Cheng1bc78042006-04-26 01:20:17 +00001588 // If the function takes variable number of arguments, make a frame index for
1589 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001590 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001591 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
David Greene3f2bf852009-11-12 20:49:22 +00001592 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize, true, false);
Gordon Henriksen86737662008-01-05 16:56:59 +00001593 }
1594 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001595 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1596
1597 // FIXME: We should really autogenerate these arrays
1598 static const unsigned GPR64ArgRegsWin64[] = {
1599 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001600 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001601 static const unsigned XMMArgRegsWin64[] = {
1602 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1603 };
1604 static const unsigned GPR64ArgRegs64Bit[] = {
1605 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1606 };
1607 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001608 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1609 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1610 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001611 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1612
1613 if (IsWin64) {
1614 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1615 GPR64ArgRegs = GPR64ArgRegsWin64;
1616 XMMArgRegs = XMMArgRegsWin64;
1617 } else {
1618 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1619 GPR64ArgRegs = GPR64ArgRegs64Bit;
1620 XMMArgRegs = XMMArgRegs64Bit;
1621 }
1622 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1623 TotalNumIntRegs);
1624 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1625 TotalNumXMMRegs);
1626
Devang Patel578efa92009-06-05 21:57:13 +00001627 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001628 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001629 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001630 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001631 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001632 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001633 // Kernel mode asks for SSE to be disabled, so don't push them
1634 // on the stack.
1635 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001636
Gordon Henriksen86737662008-01-05 16:56:59 +00001637 // For X86-64, if there are vararg parameters that are passed via
1638 // registers, then we must store them to their spots on the stack so they
1639 // may be loaded by deferencing the result of va_next.
1640 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001641 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1642 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
David Greene3f2bf852009-11-12 20:49:22 +00001643 TotalNumXMMRegs * 16, 16,
1644 false);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001645
Gordon Henriksen86737662008-01-05 16:56:59 +00001646 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001647 SmallVector<SDValue, 8> MemOps;
1648 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohmand6708ea2009-08-15 01:38:56 +00001649 unsigned Offset = VarArgsGPOffset;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001650 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001651 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1652 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001653 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1654 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001655 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001656 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001657 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00001658 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
David Greene67c9d422010-02-15 16:53:33 +00001659 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001660 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001661 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001663
Dan Gohmanface41a2009-08-16 21:24:25 +00001664 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1665 // Now store the XMM (fp + vector) parameter registers.
1666 SmallVector<SDValue, 11> SaveXMMOps;
1667 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001668
Dan Gohmanface41a2009-08-16 21:24:25 +00001669 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1670 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1671 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001672
Dan Gohmanface41a2009-08-16 21:24:25 +00001673 SaveXMMOps.push_back(DAG.getIntPtrConstant(RegSaveFrameIndex));
1674 SaveXMMOps.push_back(DAG.getIntPtrConstant(VarArgsFPOffset));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001675
Dan Gohmanface41a2009-08-16 21:24:25 +00001676 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1677 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1678 X86::VR128RegisterClass);
1679 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1680 SaveXMMOps.push_back(Val);
1681 }
1682 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1683 MVT::Other,
1684 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001685 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001686
1687 if (!MemOps.empty())
1688 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1689 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001690 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001691 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001692
Gordon Henriksen86737662008-01-05 16:56:59 +00001693 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001694 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001695 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001696 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001697 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001698 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001699 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001700 BytesToPopOnReturn = 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001701 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001702
Gordon Henriksen86737662008-01-05 16:56:59 +00001703 if (!Is64Bit) {
1704 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001705 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1707 }
Evan Cheng25caf632006-05-23 21:06:34 +00001708
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001709 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001710
Dan Gohman98ca4f22009-08-05 01:29:28 +00001711 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001712}
1713
Dan Gohman475871a2008-07-27 21:46:04 +00001714SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001715X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1716 SDValue StackPtr, SDValue Arg,
1717 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001718 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001719 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001720 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001721 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001722 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001723 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001724 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001725 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001726 }
Dale Johannesenace16102009-02-03 19:33:06 +00001727 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001728 PseudoSourceValue::getStack(), LocMemOffset,
1729 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001730}
1731
Bill Wendling64e87322009-01-16 19:25:27 +00001732/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001733/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001734SDValue
1735X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001736 SDValue &OutRetAddr, SDValue Chain,
1737 bool IsTailCall, bool Is64Bit,
1738 int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001739 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001740 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001741 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001742
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001743 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001744 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001745 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001746}
1747
1748/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1749/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001750static SDValue
1751EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001752 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001753 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001754 // Store the return address to the appropriate stack slot.
1755 if (!FPDiff) return Chain;
1756 // Calculate the new stack slot for the return address.
1757 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001758 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001759 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001760 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001761 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001762 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001763 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1764 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001765 return Chain;
1766}
1767
Dan Gohman98ca4f22009-08-05 01:29:28 +00001768SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001769X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001770 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001771 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772 const SmallVectorImpl<ISD::OutputArg> &Outs,
1773 const SmallVectorImpl<ISD::InputArg> &Ins,
1774 DebugLoc dl, SelectionDAG &DAG,
1775 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776 MachineFunction &MF = DAG.getMachineFunction();
1777 bool Is64Bit = Subtarget->is64Bit();
1778 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001779 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001780
Evan Cheng5f941932010-02-05 02:21:12 +00001781 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001782 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001783 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1784 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001785 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001786
1787 // Sibcalls are automatically detected tailcalls which do not require
1788 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001789 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001790 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001791
1792 if (isTailCall)
1793 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001794 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001795
Chris Lattner29689432010-03-11 00:22:57 +00001796 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1797 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001798
Chris Lattner638402b2007-02-28 07:00:42 +00001799 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001800 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001801 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1802 ArgLocs, *DAG.getContext());
1803 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001804
Chris Lattner423c5f42007-02-28 05:31:48 +00001805 // Get a count of how many bytes are to be pushed on the stack.
1806 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001807 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001808 // This is a sibcall. The memory operands are available in caller's
1809 // own caller's stack.
1810 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001811 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001812 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001813
Gordon Henriksen86737662008-01-05 16:56:59 +00001814 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001815 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001816 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001817 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001818 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1819 FPDiff = NumBytesCallerPushed - NumBytes;
1820
1821 // Set the delta of movement of the returnaddr stackslot.
1822 // But only set if delta is greater than previous delta.
1823 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1824 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1825 }
1826
Evan Chengf22f9b32010-02-06 03:28:46 +00001827 if (!IsSibcall)
1828 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001829
Dan Gohman475871a2008-07-27 21:46:04 +00001830 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001831 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001832 if (isTailCall && FPDiff)
1833 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1834 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001835
Dan Gohman475871a2008-07-27 21:46:04 +00001836 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1837 SmallVector<SDValue, 8> MemOpChains;
1838 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001839
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001840 // Walk the register/memloc assignments, inserting copies/loads. In the case
1841 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001842 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1843 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001844 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001845 SDValue Arg = Outs[i].Val;
1846 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001847 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001848
Chris Lattner423c5f42007-02-28 05:31:48 +00001849 // Promote the value if needed.
1850 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001851 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001852 case CCValAssign::Full: break;
1853 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001854 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001855 break;
1856 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001857 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001858 break;
1859 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001860 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1861 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001862 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1863 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1864 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001865 } else
1866 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1867 break;
1868 case CCValAssign::BCvt:
1869 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001870 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001871 case CCValAssign::Indirect: {
1872 // Store the argument.
1873 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001874 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001875 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001876 PseudoSourceValue::getFixedStack(FI), 0,
1877 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001878 Arg = SpillSlot;
1879 break;
1880 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001881 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001882
Chris Lattner423c5f42007-02-28 05:31:48 +00001883 if (VA.isRegLoc()) {
1884 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001885 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001886 assert(VA.isMemLoc());
1887 if (StackPtr.getNode() == 0)
1888 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1889 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1890 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001891 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001892 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001893
Evan Cheng32fe1032006-05-25 00:59:30 +00001894 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001895 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001896 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001897
Evan Cheng347d5f72006-04-28 21:29:37 +00001898 // Build a sequence of copy-to-reg nodes chained together with token chain
1899 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001900 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001901 // Tail call byval lowering might overwrite argument registers so in case of
1902 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001903 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001904 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001905 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001906 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001907 InFlag = Chain.getValue(1);
1908 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001909
Chris Lattner88e1fd52009-07-09 04:24:46 +00001910 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001911 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1912 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001913 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001914 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1915 DAG.getNode(X86ISD::GlobalBaseReg,
1916 DebugLoc::getUnknownLoc(),
1917 getPointerTy()),
1918 InFlag);
1919 InFlag = Chain.getValue(1);
1920 } else {
1921 // If we are tail calling and generating PIC/GOT style code load the
1922 // address of the callee into ECX. The value in ecx is used as target of
1923 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1924 // for tail calls on PIC/GOT architectures. Normally we would just put the
1925 // address of GOT into ebx and then call target@PLT. But for tail calls
1926 // ebx would be restored (since ebx is callee saved) before jumping to the
1927 // target@PLT.
1928
1929 // Note: The actual moving to ECX is done further down.
1930 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1931 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1932 !G->getGlobal()->hasProtectedVisibility())
1933 Callee = LowerGlobalAddress(Callee, DAG);
1934 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001935 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001936 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001937 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001938
Gordon Henriksen86737662008-01-05 16:56:59 +00001939 if (Is64Bit && isVarArg) {
1940 // From AMD64 ABI document:
1941 // For calls that may call functions that use varargs or stdargs
1942 // (prototype-less calls or calls to functions containing ellipsis (...) in
1943 // the declaration) %al is used as hidden argument to specify the number
1944 // of SSE registers used. The contents of %al do not need to match exactly
1945 // the number of registers, but must be an ubound on the number of SSE
1946 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001947
1948 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001949 // Count the number of XMM registers allocated.
1950 static const unsigned XMMArgRegs[] = {
1951 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1952 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1953 };
1954 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001955 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001956 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001957
Dale Johannesendd64c412009-02-04 00:33:20 +00001958 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001959 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001960 InFlag = Chain.getValue(1);
1961 }
1962
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001963
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001964 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001965 if (isTailCall) {
1966 // Force all the incoming stack arguments to be loaded from the stack
1967 // before any new outgoing arguments are stored to the stack, because the
1968 // outgoing stack slots may alias the incoming argument stack slots, and
1969 // the alias isn't otherwise explicit. This is slightly more conservative
1970 // than necessary, because it means that each store effectively depends
1971 // on every argument instead of just those arguments it would clobber.
1972 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1973
Dan Gohman475871a2008-07-27 21:46:04 +00001974 SmallVector<SDValue, 8> MemOpChains2;
1975 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001976 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001977 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001978 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00001979 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00001980 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1981 CCValAssign &VA = ArgLocs[i];
1982 if (VA.isRegLoc())
1983 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001984 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001985 SDValue Arg = Outs[i].Val;
1986 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001987 // Create frame index.
1988 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001989 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00001990 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001991 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001992
Duncan Sands276dcbd2008-03-21 09:14:45 +00001993 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001994 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001995 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001996 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001997 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001998 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001999 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002000
Dan Gohman98ca4f22009-08-05 01:29:28 +00002001 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2002 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002003 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002004 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002005 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002006 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002007 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002008 PseudoSourceValue::getFixedStack(FI), 0,
2009 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002010 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002011 }
2012 }
2013
2014 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002015 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002016 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002017
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002018 // Copy arguments to their registers.
2019 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002020 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002021 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002022 InFlag = Chain.getValue(1);
2023 }
Dan Gohman475871a2008-07-27 21:46:04 +00002024 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002025
Gordon Henriksen86737662008-01-05 16:56:59 +00002026 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002027 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002028 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002029 }
2030
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002031 bool WasGlobalOrExternal = false;
2032 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2033 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2034 // In the 64-bit large code model, we have to make all calls
2035 // through a register, since the call instruction's 32-bit
2036 // pc-relative offset may not be large enough to hold the whole
2037 // address.
2038 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2039 WasGlobalOrExternal = true;
2040 // If the callee is a GlobalAddress node (quite common, every direct call
2041 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2042 // it.
2043
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002044 // We should use extra load for direct calls to dllimported functions in
2045 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00002046 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002047 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002048 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002049
Chris Lattner48a7d022009-07-09 05:02:21 +00002050 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2051 // external symbols most go through the PLT in PIC mode. If the symbol
2052 // has hidden or protected visibility, or if it is static or local, then
2053 // we don't need to use the PLT - we can directly call it.
2054 if (Subtarget->isTargetELF() &&
2055 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002056 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002057 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002058 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002059 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2060 Subtarget->getDarwinVers() < 9) {
2061 // PC-relative references to external symbols should go through $stub,
2062 // unless we're building with the leopard linker or later, which
2063 // automatically synthesizes these stubs.
2064 OpFlags = X86II::MO_DARWIN_STUB;
2065 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002066
Chris Lattner74e726e2009-07-09 05:27:35 +00002067 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002068 G->getOffset(), OpFlags);
2069 }
Bill Wendling056292f2008-09-16 21:48:12 +00002070 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002071 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002072 unsigned char OpFlags = 0;
2073
2074 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2075 // symbols should go through the PLT.
2076 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002077 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002078 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002079 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002080 Subtarget->getDarwinVers() < 9) {
2081 // PC-relative references to external symbols should go through $stub,
2082 // unless we're building with the leopard linker or later, which
2083 // automatically synthesizes these stubs.
2084 OpFlags = X86II::MO_DARWIN_STUB;
2085 }
Eric Christopherfd179292009-08-27 18:07:15 +00002086
Chris Lattner48a7d022009-07-09 05:02:21 +00002087 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2088 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002089 }
2090
Chris Lattnerd96d0722007-02-25 06:40:16 +00002091 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002092 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002093 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002094
Evan Chengf22f9b32010-02-06 03:28:46 +00002095 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002096 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2097 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002098 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002099 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002100
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002101 Ops.push_back(Chain);
2102 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002103
Dan Gohman98ca4f22009-08-05 01:29:28 +00002104 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002105 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002106
Gordon Henriksen86737662008-01-05 16:56:59 +00002107 // Add argument registers to the end of the list so that they are known live
2108 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002109 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2110 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2111 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002112
Evan Cheng586ccac2008-03-18 23:36:35 +00002113 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002114 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002115 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2116
2117 // Add an implicit use of AL for x86 vararg functions.
2118 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002119 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002120
Gabor Greifba36cb52008-08-28 21:40:38 +00002121 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002122 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002123
Dan Gohman98ca4f22009-08-05 01:29:28 +00002124 if (isTailCall) {
2125 // If this is the first return lowered for this function, add the regs
2126 // to the liveout set for the function.
2127 if (MF.getRegInfo().liveout_empty()) {
2128 SmallVector<CCValAssign, 16> RVLocs;
2129 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2130 *DAG.getContext());
2131 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2132 for (unsigned i = 0; i != RVLocs.size(); ++i)
2133 if (RVLocs[i].isRegLoc())
2134 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2135 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002136 return DAG.getNode(X86ISD::TC_RETURN, dl,
2137 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002138 }
2139
Dale Johannesenace16102009-02-03 19:33:06 +00002140 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002141 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002142
Chris Lattner2d297092006-05-23 18:50:38 +00002143 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002144 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002145 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002146 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002147 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002148 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002149 // pops the hidden struct pointer, so we have to push it back.
2150 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002151 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002152 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002153 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002154
Gordon Henriksenae636f82008-01-03 16:47:34 +00002155 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002156 if (!IsSibcall) {
2157 Chain = DAG.getCALLSEQ_END(Chain,
2158 DAG.getIntPtrConstant(NumBytes, true),
2159 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2160 true),
2161 InFlag);
2162 InFlag = Chain.getValue(1);
2163 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002164
Chris Lattner3085e152007-02-25 08:59:22 +00002165 // Handle result values, copying them out of physregs into vregs that we
2166 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002167 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2168 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002169}
2170
Evan Cheng25ab6902006-09-08 06:48:29 +00002171
2172//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002173// Fast Calling Convention (tail call) implementation
2174//===----------------------------------------------------------------------===//
2175
2176// Like std call, callee cleans arguments, convention except that ECX is
2177// reserved for storing the tail called function address. Only 2 registers are
2178// free for argument passing (inreg). Tail call optimization is performed
2179// provided:
2180// * tailcallopt is enabled
2181// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002182// On X86_64 architecture with GOT-style position independent code only local
2183// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002184// To keep the stack aligned according to platform abi the function
2185// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2186// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002187// If a tail called function callee has more arguments than the caller the
2188// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002189// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002190// original REtADDR, but before the saved framepointer or the spilled registers
2191// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2192// stack layout:
2193// arg1
2194// arg2
2195// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002196// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002197// move area ]
2198// (possible EBP)
2199// ESI
2200// EDI
2201// local1 ..
2202
2203/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2204/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002205unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002206 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002207 MachineFunction &MF = DAG.getMachineFunction();
2208 const TargetMachine &TM = MF.getTarget();
2209 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2210 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002211 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002212 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002213 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002214 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2215 // Number smaller than 12 so just add the difference.
2216 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2217 } else {
2218 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002219 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002220 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002221 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002222 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002223}
2224
Evan Cheng5f941932010-02-05 02:21:12 +00002225/// MatchingStackOffset - Return true if the given stack call argument is
2226/// already available in the same position (relatively) of the caller's
2227/// incoming argument stack.
2228static
2229bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2230 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2231 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002232 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2233 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002234 if (Arg.getOpcode() == ISD::CopyFromReg) {
2235 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2236 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2237 return false;
2238 MachineInstr *Def = MRI->getVRegDef(VR);
2239 if (!Def)
2240 return false;
2241 if (!Flags.isByVal()) {
2242 if (!TII->isLoadFromStackSlot(Def, FI))
2243 return false;
2244 } else {
2245 unsigned Opcode = Def->getOpcode();
2246 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2247 Def->getOperand(1).isFI()) {
2248 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002249 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002250 } else
2251 return false;
2252 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002253 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2254 if (Flags.isByVal())
2255 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002256 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002257 // define @foo(%struct.X* %A) {
2258 // tail call @bar(%struct.X* byval %A)
2259 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002260 return false;
2261 SDValue Ptr = Ld->getBasePtr();
2262 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2263 if (!FINode)
2264 return false;
2265 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002266 } else
2267 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002268
Evan Cheng4cae1332010-03-05 08:38:04 +00002269 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002270 if (!MFI->isFixedObjectIndex(FI))
2271 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002272 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002273}
2274
Dan Gohman98ca4f22009-08-05 01:29:28 +00002275/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2276/// for tail call optimization. Targets which want to do tail call
2277/// optimization should implement this function.
2278bool
2279X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002280 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002281 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002282 bool isCalleeStructRet,
2283 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002284 const SmallVectorImpl<ISD::OutputArg> &Outs,
2285 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002286 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002287 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002288 CalleeCC != CallingConv::C)
2289 return false;
2290
Evan Cheng7096ae42010-01-29 06:45:59 +00002291 // If -tailcallopt is specified, make fastcc functions tail-callable.
2292 const Function *CallerF = DAG.getMachineFunction().getFunction();
Dan Gohman1797ed52010-02-08 20:27:50 +00002293 if (GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00002294 if (IsTailCallConvention(CalleeCC) &&
Evan Cheng843bd692010-01-31 06:44:49 +00002295 CallerF->getCallingConv() == CalleeCC)
2296 return true;
2297 return false;
2298 }
2299
Evan Chengb2c92902010-02-02 02:22:50 +00002300 // Look for obvious safe cases to perform tail call optimization that does not
2301 // requite ABI changes. This is what gcc calls sibcall.
2302
Evan Chenga375d472010-03-15 18:54:48 +00002303 // Do not sibcall optimize vararg calls for now.
Evan Cheng843bd692010-01-31 06:44:49 +00002304 if (isVarArg)
2305 return false;
2306
Evan Chenga375d472010-03-15 18:54:48 +00002307 // Also avoid sibcall optimization if either caller or callee uses struct
2308 // return semantics.
2309 if (isCalleeStructRet || isCallerStructRet)
2310 return false;
2311
Evan Chenga6bff982010-01-30 01:22:00 +00002312 // If the callee takes no arguments then go on to check the results of the
2313 // call.
2314 if (!Outs.empty()) {
2315 // Check if stack adjustment is needed. For now, do not do this if any
2316 // argument is passed on the stack.
2317 SmallVector<CCValAssign, 16> ArgLocs;
2318 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2319 ArgLocs, *DAG.getContext());
2320 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002321 if (CCInfo.getNextStackOffset()) {
2322 MachineFunction &MF = DAG.getMachineFunction();
2323 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2324 return false;
2325 if (Subtarget->isTargetWin64())
2326 // Win64 ABI has additional complications.
2327 return false;
2328
2329 // Check if the arguments are already laid out in the right way as
2330 // the caller's fixed stack objects.
2331 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002332 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2333 const X86InstrInfo *TII =
2334 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002335 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2336 CCValAssign &VA = ArgLocs[i];
2337 EVT RegVT = VA.getLocVT();
2338 SDValue Arg = Outs[i].Val;
2339 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002340 if (VA.getLocInfo() == CCValAssign::Indirect)
2341 return false;
2342 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002343 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2344 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002345 return false;
2346 }
2347 }
2348 }
Evan Chenga6bff982010-01-30 01:22:00 +00002349 }
Evan Chengb1712452010-01-27 06:25:16 +00002350
Evan Cheng86809cc2010-02-03 03:28:02 +00002351 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002352}
2353
Dan Gohman3df24e62008-09-03 23:12:08 +00002354FastISel *
Evan Chengddc419c2010-01-26 19:04:47 +00002355X86TargetLowering::createFastISel(MachineFunction &mf, MachineModuleInfo *mmo,
2356 DwarfWriter *dw,
2357 DenseMap<const Value *, unsigned> &vm,
2358 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2359 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002360#ifndef NDEBUG
Evan Chengddc419c2010-01-26 19:04:47 +00002361 , SmallSet<Instruction*, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002362#endif
2363 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002364 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002365#ifndef NDEBUG
2366 , cil
2367#endif
2368 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002369}
2370
2371
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002372//===----------------------------------------------------------------------===//
2373// Other Lowering Hooks
2374//===----------------------------------------------------------------------===//
2375
2376
Dan Gohman475871a2008-07-27 21:46:04 +00002377SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002378 MachineFunction &MF = DAG.getMachineFunction();
2379 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2380 int ReturnAddrIndex = FuncInfo->getRAIndex();
2381
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002382 if (ReturnAddrIndex == 0) {
2383 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002384 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002385 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002386 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002387 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002388 }
2389
Evan Cheng25ab6902006-09-08 06:48:29 +00002390 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002391}
2392
2393
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002394bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2395 bool hasSymbolicDisplacement) {
2396 // Offset should fit into 32 bit immediate field.
2397 if (!isInt32(Offset))
2398 return false;
2399
2400 // If we don't have a symbolic displacement - we don't have any extra
2401 // restrictions.
2402 if (!hasSymbolicDisplacement)
2403 return true;
2404
2405 // FIXME: Some tweaks might be needed for medium code model.
2406 if (M != CodeModel::Small && M != CodeModel::Kernel)
2407 return false;
2408
2409 // For small code model we assume that latest object is 16MB before end of 31
2410 // bits boundary. We may also accept pretty large negative constants knowing
2411 // that all objects are in the positive half of address space.
2412 if (M == CodeModel::Small && Offset < 16*1024*1024)
2413 return true;
2414
2415 // For kernel code model we know that all object resist in the negative half
2416 // of 32bits address space. We may not accept negative offsets, since they may
2417 // be just off and we may accept pretty large positive ones.
2418 if (M == CodeModel::Kernel && Offset > 0)
2419 return true;
2420
2421 return false;
2422}
2423
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002424/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2425/// specific condition code, returning the condition code and the LHS/RHS of the
2426/// comparison to make.
2427static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2428 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002429 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002430 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2431 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2432 // X > -1 -> X == 0, jump !sign.
2433 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002434 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002435 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2436 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002437 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002438 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002439 // X < 1 -> X <= 0
2440 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002441 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002442 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002443 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002444
Evan Chengd9558e02006-01-06 00:43:03 +00002445 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002446 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002447 case ISD::SETEQ: return X86::COND_E;
2448 case ISD::SETGT: return X86::COND_G;
2449 case ISD::SETGE: return X86::COND_GE;
2450 case ISD::SETLT: return X86::COND_L;
2451 case ISD::SETLE: return X86::COND_LE;
2452 case ISD::SETNE: return X86::COND_NE;
2453 case ISD::SETULT: return X86::COND_B;
2454 case ISD::SETUGT: return X86::COND_A;
2455 case ISD::SETULE: return X86::COND_BE;
2456 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002457 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002458 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002459
Chris Lattner4c78e022008-12-23 23:42:27 +00002460 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002461
Chris Lattner4c78e022008-12-23 23:42:27 +00002462 // If LHS is a foldable load, but RHS is not, flip the condition.
2463 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2464 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2465 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2466 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002467 }
2468
Chris Lattner4c78e022008-12-23 23:42:27 +00002469 switch (SetCCOpcode) {
2470 default: break;
2471 case ISD::SETOLT:
2472 case ISD::SETOLE:
2473 case ISD::SETUGT:
2474 case ISD::SETUGE:
2475 std::swap(LHS, RHS);
2476 break;
2477 }
2478
2479 // On a floating point condition, the flags are set as follows:
2480 // ZF PF CF op
2481 // 0 | 0 | 0 | X > Y
2482 // 0 | 0 | 1 | X < Y
2483 // 1 | 0 | 0 | X == Y
2484 // 1 | 1 | 1 | unordered
2485 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002486 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002487 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002488 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002489 case ISD::SETOLT: // flipped
2490 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002491 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002492 case ISD::SETOLE: // flipped
2493 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002494 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002495 case ISD::SETUGT: // flipped
2496 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002497 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002498 case ISD::SETUGE: // flipped
2499 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002500 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002501 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002502 case ISD::SETNE: return X86::COND_NE;
2503 case ISD::SETUO: return X86::COND_P;
2504 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002505 case ISD::SETOEQ:
2506 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002507 }
Evan Chengd9558e02006-01-06 00:43:03 +00002508}
2509
Evan Cheng4a460802006-01-11 00:33:36 +00002510/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2511/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002512/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002513static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002514 switch (X86CC) {
2515 default:
2516 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002517 case X86::COND_B:
2518 case X86::COND_BE:
2519 case X86::COND_E:
2520 case X86::COND_P:
2521 case X86::COND_A:
2522 case X86::COND_AE:
2523 case X86::COND_NE:
2524 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002525 return true;
2526 }
2527}
2528
Evan Chengeb2f9692009-10-27 19:56:55 +00002529/// isFPImmLegal - Returns true if the target can instruction select the
2530/// specified FP immediate natively. If false, the legalizer will
2531/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002532bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002533 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2534 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2535 return true;
2536 }
2537 return false;
2538}
2539
Nate Begeman9008ca62009-04-27 18:41:29 +00002540/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2541/// the specified range (L, H].
2542static bool isUndefOrInRange(int Val, int Low, int Hi) {
2543 return (Val < 0) || (Val >= Low && Val < Hi);
2544}
2545
2546/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2547/// specified value.
2548static bool isUndefOrEqual(int Val, int CmpVal) {
2549 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002550 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002551 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002552}
2553
Nate Begeman9008ca62009-04-27 18:41:29 +00002554/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2555/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2556/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002557static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002558 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002559 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002560 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002561 return (Mask[0] < 2 && Mask[1] < 2);
2562 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002563}
2564
Nate Begeman9008ca62009-04-27 18:41:29 +00002565bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002566 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002567 N->getMask(M);
2568 return ::isPSHUFDMask(M, N->getValueType(0));
2569}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002570
Nate Begeman9008ca62009-04-27 18:41:29 +00002571/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2572/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002573static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002574 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002575 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002576
Nate Begeman9008ca62009-04-27 18:41:29 +00002577 // Lower quadword copied in order or undef.
2578 for (int i = 0; i != 4; ++i)
2579 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002580 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002581
Evan Cheng506d3df2006-03-29 23:07:14 +00002582 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002583 for (int i = 4; i != 8; ++i)
2584 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002585 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002586
Evan Cheng506d3df2006-03-29 23:07:14 +00002587 return true;
2588}
2589
Nate Begeman9008ca62009-04-27 18:41:29 +00002590bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002591 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002592 N->getMask(M);
2593 return ::isPSHUFHWMask(M, N->getValueType(0));
2594}
Evan Cheng506d3df2006-03-29 23:07:14 +00002595
Nate Begeman9008ca62009-04-27 18:41:29 +00002596/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2597/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002598static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002599 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002600 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002601
Rafael Espindola15684b22009-04-24 12:40:33 +00002602 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002603 for (int i = 4; i != 8; ++i)
2604 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002605 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002606
Rafael Espindola15684b22009-04-24 12:40:33 +00002607 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002608 for (int i = 0; i != 4; ++i)
2609 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002610 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002611
Rafael Espindola15684b22009-04-24 12:40:33 +00002612 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002613}
2614
Nate Begeman9008ca62009-04-27 18:41:29 +00002615bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002616 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002617 N->getMask(M);
2618 return ::isPSHUFLWMask(M, N->getValueType(0));
2619}
2620
Nate Begemana09008b2009-10-19 02:17:23 +00002621/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2622/// is suitable for input to PALIGNR.
2623static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2624 bool hasSSSE3) {
2625 int i, e = VT.getVectorNumElements();
2626
2627 // Do not handle v2i64 / v2f64 shuffles with palignr.
2628 if (e < 4 || !hasSSSE3)
2629 return false;
2630
2631 for (i = 0; i != e; ++i)
2632 if (Mask[i] >= 0)
2633 break;
2634
2635 // All undef, not a palignr.
2636 if (i == e)
2637 return false;
2638
2639 // Determine if it's ok to perform a palignr with only the LHS, since we
2640 // don't have access to the actual shuffle elements to see if RHS is undef.
2641 bool Unary = Mask[i] < (int)e;
2642 bool NeedsUnary = false;
2643
2644 int s = Mask[i] - i;
2645
2646 // Check the rest of the elements to see if they are consecutive.
2647 for (++i; i != e; ++i) {
2648 int m = Mask[i];
2649 if (m < 0)
2650 continue;
2651
2652 Unary = Unary && (m < (int)e);
2653 NeedsUnary = NeedsUnary || (m < s);
2654
2655 if (NeedsUnary && !Unary)
2656 return false;
2657 if (Unary && m != ((s+i) & (e-1)))
2658 return false;
2659 if (!Unary && m != (s+i))
2660 return false;
2661 }
2662 return true;
2663}
2664
2665bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2666 SmallVector<int, 8> M;
2667 N->getMask(M);
2668 return ::isPALIGNRMask(M, N->getValueType(0), true);
2669}
2670
Evan Cheng14aed5e2006-03-24 01:18:28 +00002671/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2672/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002673static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002674 int NumElems = VT.getVectorNumElements();
2675 if (NumElems != 2 && NumElems != 4)
2676 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002677
Nate Begeman9008ca62009-04-27 18:41:29 +00002678 int Half = NumElems / 2;
2679 for (int i = 0; i < Half; ++i)
2680 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002681 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002682 for (int i = Half; i < NumElems; ++i)
2683 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002684 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002685
Evan Cheng14aed5e2006-03-24 01:18:28 +00002686 return true;
2687}
2688
Nate Begeman9008ca62009-04-27 18:41:29 +00002689bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2690 SmallVector<int, 8> M;
2691 N->getMask(M);
2692 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002693}
2694
Evan Cheng213d2cf2007-05-17 18:45:50 +00002695/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002696/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2697/// half elements to come from vector 1 (which would equal the dest.) and
2698/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002699static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002700 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002701
2702 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002703 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002704
Nate Begeman9008ca62009-04-27 18:41:29 +00002705 int Half = NumElems / 2;
2706 for (int i = 0; i < Half; ++i)
2707 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002708 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002709 for (int i = Half; i < NumElems; ++i)
2710 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002711 return false;
2712 return true;
2713}
2714
Nate Begeman9008ca62009-04-27 18:41:29 +00002715static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2716 SmallVector<int, 8> M;
2717 N->getMask(M);
2718 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002719}
2720
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002721/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2722/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002723bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2724 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002725 return false;
2726
Evan Cheng2064a2b2006-03-28 06:50:32 +00002727 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002728 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2729 isUndefOrEqual(N->getMaskElt(1), 7) &&
2730 isUndefOrEqual(N->getMaskElt(2), 2) &&
2731 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002732}
2733
Nate Begeman0b10b912009-11-07 23:17:15 +00002734/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2735/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2736/// <2, 3, 2, 3>
2737bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2738 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2739
2740 if (NumElems != 4)
2741 return false;
2742
2743 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2744 isUndefOrEqual(N->getMaskElt(1), 3) &&
2745 isUndefOrEqual(N->getMaskElt(2), 2) &&
2746 isUndefOrEqual(N->getMaskElt(3), 3);
2747}
2748
Evan Cheng5ced1d82006-04-06 23:23:56 +00002749/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2750/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002751bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2752 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002753
Evan Cheng5ced1d82006-04-06 23:23:56 +00002754 if (NumElems != 2 && NumElems != 4)
2755 return false;
2756
Evan Chengc5cdff22006-04-07 21:53:05 +00002757 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002758 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002759 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002760
Evan Chengc5cdff22006-04-07 21:53:05 +00002761 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002762 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002763 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002764
2765 return true;
2766}
2767
Nate Begeman0b10b912009-11-07 23:17:15 +00002768/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2769/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2770bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002771 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002772
Evan Cheng5ced1d82006-04-06 23:23:56 +00002773 if (NumElems != 2 && NumElems != 4)
2774 return false;
2775
Evan Chengc5cdff22006-04-07 21:53:05 +00002776 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002777 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002778 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002779
Nate Begeman9008ca62009-04-27 18:41:29 +00002780 for (unsigned i = 0; i < NumElems/2; ++i)
2781 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002782 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002783
2784 return true;
2785}
2786
Evan Cheng0038e592006-03-28 00:39:58 +00002787/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2788/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002789static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002790 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002791 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002792 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002793 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002794
Nate Begeman9008ca62009-04-27 18:41:29 +00002795 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2796 int BitI = Mask[i];
2797 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002798 if (!isUndefOrEqual(BitI, j))
2799 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002800 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002801 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002802 return false;
2803 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002804 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002805 return false;
2806 }
Evan Cheng0038e592006-03-28 00:39:58 +00002807 }
Evan Cheng0038e592006-03-28 00:39:58 +00002808 return true;
2809}
2810
Nate Begeman9008ca62009-04-27 18:41:29 +00002811bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2812 SmallVector<int, 8> M;
2813 N->getMask(M);
2814 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002815}
2816
Evan Cheng4fcb9222006-03-28 02:43:26 +00002817/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2818/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002819static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002820 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002821 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002822 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002823 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002824
Nate Begeman9008ca62009-04-27 18:41:29 +00002825 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2826 int BitI = Mask[i];
2827 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002828 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002829 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002830 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002831 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002832 return false;
2833 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002834 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002835 return false;
2836 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002837 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002838 return true;
2839}
2840
Nate Begeman9008ca62009-04-27 18:41:29 +00002841bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2842 SmallVector<int, 8> M;
2843 N->getMask(M);
2844 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002845}
2846
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002847/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2848/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2849/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002850static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002851 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002852 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002853 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002854
Nate Begeman9008ca62009-04-27 18:41:29 +00002855 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2856 int BitI = Mask[i];
2857 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002858 if (!isUndefOrEqual(BitI, j))
2859 return false;
2860 if (!isUndefOrEqual(BitI1, j))
2861 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002862 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002863 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002864}
2865
Nate Begeman9008ca62009-04-27 18:41:29 +00002866bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2867 SmallVector<int, 8> M;
2868 N->getMask(M);
2869 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2870}
2871
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002872/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2873/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2874/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002875static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002876 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002877 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2878 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002879
Nate Begeman9008ca62009-04-27 18:41:29 +00002880 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2881 int BitI = Mask[i];
2882 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002883 if (!isUndefOrEqual(BitI, j))
2884 return false;
2885 if (!isUndefOrEqual(BitI1, j))
2886 return false;
2887 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002888 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002889}
2890
Nate Begeman9008ca62009-04-27 18:41:29 +00002891bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2892 SmallVector<int, 8> M;
2893 N->getMask(M);
2894 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2895}
2896
Evan Cheng017dcc62006-04-21 01:05:10 +00002897/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2898/// specifies a shuffle of elements that is suitable for input to MOVSS,
2899/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002900static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002901 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002902 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002903
2904 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002905
Nate Begeman9008ca62009-04-27 18:41:29 +00002906 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002907 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002908
Nate Begeman9008ca62009-04-27 18:41:29 +00002909 for (int i = 1; i < NumElts; ++i)
2910 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002911 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002912
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002913 return true;
2914}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002915
Nate Begeman9008ca62009-04-27 18:41:29 +00002916bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2917 SmallVector<int, 8> M;
2918 N->getMask(M);
2919 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002920}
2921
Evan Cheng017dcc62006-04-21 01:05:10 +00002922/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2923/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002924/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002925static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002926 bool V2IsSplat = false, bool V2IsUndef = false) {
2927 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002928 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002929 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002930
Nate Begeman9008ca62009-04-27 18:41:29 +00002931 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002932 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002933
Nate Begeman9008ca62009-04-27 18:41:29 +00002934 for (int i = 1; i < NumOps; ++i)
2935 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2936 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2937 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002938 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002939
Evan Cheng39623da2006-04-20 08:58:49 +00002940 return true;
2941}
2942
Nate Begeman9008ca62009-04-27 18:41:29 +00002943static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002944 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002945 SmallVector<int, 8> M;
2946 N->getMask(M);
2947 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002948}
2949
Evan Chengd9539472006-04-14 21:59:03 +00002950/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2951/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002952bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2953 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002954 return false;
2955
2956 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002957 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002958 int Elt = N->getMaskElt(i);
2959 if (Elt >= 0 && Elt != 1)
2960 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002961 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002962
2963 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002964 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002965 int Elt = N->getMaskElt(i);
2966 if (Elt >= 0 && Elt != 3)
2967 return false;
2968 if (Elt == 3)
2969 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002970 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002971 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002972 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002973 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002974}
2975
2976/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2977/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002978bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2979 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002980 return false;
2981
2982 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002983 for (unsigned i = 0; i < 2; ++i)
2984 if (N->getMaskElt(i) > 0)
2985 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002986
2987 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002988 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002989 int Elt = N->getMaskElt(i);
2990 if (Elt >= 0 && Elt != 2)
2991 return false;
2992 if (Elt == 2)
2993 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002994 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002995 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002996 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002997}
2998
Evan Cheng0b457f02008-09-25 20:50:48 +00002999/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3000/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003001bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3002 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003003
Nate Begeman9008ca62009-04-27 18:41:29 +00003004 for (int i = 0; i < e; ++i)
3005 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003006 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003007 for (int i = 0; i < e; ++i)
3008 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003009 return false;
3010 return true;
3011}
3012
Evan Cheng63d33002006-03-22 08:01:21 +00003013/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003014/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003015unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003016 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3017 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3018
Evan Chengb9df0ca2006-03-22 02:53:00 +00003019 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3020 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003021 for (int i = 0; i < NumOperands; ++i) {
3022 int Val = SVOp->getMaskElt(NumOperands-i-1);
3023 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003024 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003025 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003026 if (i != NumOperands - 1)
3027 Mask <<= Shift;
3028 }
Evan Cheng63d33002006-03-22 08:01:21 +00003029 return Mask;
3030}
3031
Evan Cheng506d3df2006-03-29 23:07:14 +00003032/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003033/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003034unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003035 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003036 unsigned Mask = 0;
3037 // 8 nodes, but we only care about the last 4.
3038 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 int Val = SVOp->getMaskElt(i);
3040 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003041 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003042 if (i != 4)
3043 Mask <<= 2;
3044 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003045 return Mask;
3046}
3047
3048/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003049/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003050unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003051 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003052 unsigned Mask = 0;
3053 // 8 nodes, but we only care about the first 4.
3054 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003055 int Val = SVOp->getMaskElt(i);
3056 if (Val >= 0)
3057 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003058 if (i != 0)
3059 Mask <<= 2;
3060 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003061 return Mask;
3062}
3063
Nate Begemana09008b2009-10-19 02:17:23 +00003064/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3065/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3066unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3067 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3068 EVT VVT = N->getValueType(0);
3069 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3070 int Val = 0;
3071
3072 unsigned i, e;
3073 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3074 Val = SVOp->getMaskElt(i);
3075 if (Val >= 0)
3076 break;
3077 }
3078 return (Val - i) * EltSize;
3079}
3080
Evan Cheng37b73872009-07-30 08:33:02 +00003081/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3082/// constant +0.0.
3083bool X86::isZeroNode(SDValue Elt) {
3084 return ((isa<ConstantSDNode>(Elt) &&
3085 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3086 (isa<ConstantFPSDNode>(Elt) &&
3087 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3088}
3089
Nate Begeman9008ca62009-04-27 18:41:29 +00003090/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3091/// their permute mask.
3092static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3093 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003094 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003095 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003096 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003097
Nate Begeman5a5ca152009-04-29 05:20:52 +00003098 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003099 int idx = SVOp->getMaskElt(i);
3100 if (idx < 0)
3101 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003102 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003103 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003104 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003105 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003106 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003107 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3108 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003109}
3110
Evan Cheng779ccea2007-12-07 21:30:01 +00003111/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3112/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003113static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003114 unsigned NumElems = VT.getVectorNumElements();
3115 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003116 int idx = Mask[i];
3117 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003118 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003119 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003120 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003121 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003122 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003123 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003124}
3125
Evan Cheng533a0aa2006-04-19 20:35:22 +00003126/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3127/// match movhlps. The lower half elements should come from upper half of
3128/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003129/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003130static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3131 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003132 return false;
3133 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003135 return false;
3136 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003137 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003138 return false;
3139 return true;
3140}
3141
Evan Cheng5ced1d82006-04-06 23:23:56 +00003142/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003143/// is promoted to a vector. It also returns the LoadSDNode by reference if
3144/// required.
3145static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003146 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3147 return false;
3148 N = N->getOperand(0).getNode();
3149 if (!ISD::isNON_EXTLoad(N))
3150 return false;
3151 if (LD)
3152 *LD = cast<LoadSDNode>(N);
3153 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003154}
3155
Evan Cheng533a0aa2006-04-19 20:35:22 +00003156/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3157/// match movlp{s|d}. The lower half elements should come from lower half of
3158/// V1 (and in order), and the upper half elements should come from the upper
3159/// half of V2 (and in order). And since V1 will become the source of the
3160/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003161static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3162 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003163 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003164 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003165 // Is V2 is a vector load, don't do this transformation. We will try to use
3166 // load folding shufps op.
3167 if (ISD::isNON_EXTLoad(V2))
3168 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003169
Nate Begeman5a5ca152009-04-29 05:20:52 +00003170 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003171
Evan Cheng533a0aa2006-04-19 20:35:22 +00003172 if (NumElems != 2 && NumElems != 4)
3173 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003174 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003175 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003176 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003177 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003179 return false;
3180 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003181}
3182
Evan Cheng39623da2006-04-20 08:58:49 +00003183/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3184/// all the same.
3185static bool isSplatVector(SDNode *N) {
3186 if (N->getOpcode() != ISD::BUILD_VECTOR)
3187 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003188
Dan Gohman475871a2008-07-27 21:46:04 +00003189 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003190 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3191 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003192 return false;
3193 return true;
3194}
3195
Evan Cheng213d2cf2007-05-17 18:45:50 +00003196/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003197/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003198/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003199static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003200 SDValue V1 = N->getOperand(0);
3201 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003202 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3203 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003204 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003205 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003206 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003207 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3208 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003209 if (Opc != ISD::BUILD_VECTOR ||
3210 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 return false;
3212 } else if (Idx >= 0) {
3213 unsigned Opc = V1.getOpcode();
3214 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3215 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003216 if (Opc != ISD::BUILD_VECTOR ||
3217 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003218 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003219 }
3220 }
3221 return true;
3222}
3223
3224/// getZeroVector - Returns a vector of specified type with all zero elements.
3225///
Owen Andersone50ed302009-08-10 22:56:29 +00003226static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003227 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003228 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003229
Chris Lattner8a594482007-11-25 00:24:49 +00003230 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3231 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003232 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003233 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003234 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3235 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003236 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003237 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3238 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003239 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003240 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3241 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003242 }
Dale Johannesenace16102009-02-03 19:33:06 +00003243 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003244}
3245
Chris Lattner8a594482007-11-25 00:24:49 +00003246/// getOnesVector - Returns a vector of specified type with all bits set.
3247///
Owen Andersone50ed302009-08-10 22:56:29 +00003248static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003249 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003250
Chris Lattner8a594482007-11-25 00:24:49 +00003251 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3252 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003253 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003254 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003255 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003256 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003257 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003258 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003259 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003260}
3261
3262
Evan Cheng39623da2006-04-20 08:58:49 +00003263/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3264/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003265static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003266 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003267 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003268
Evan Cheng39623da2006-04-20 08:58:49 +00003269 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003270 SmallVector<int, 8> MaskVec;
3271 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003272
Nate Begeman5a5ca152009-04-29 05:20:52 +00003273 for (unsigned i = 0; i != NumElems; ++i) {
3274 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003275 MaskVec[i] = NumElems;
3276 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003277 }
Evan Cheng39623da2006-04-20 08:58:49 +00003278 }
Evan Cheng39623da2006-04-20 08:58:49 +00003279 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003280 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3281 SVOp->getOperand(1), &MaskVec[0]);
3282 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003283}
3284
Evan Cheng017dcc62006-04-21 01:05:10 +00003285/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3286/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003287static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003288 SDValue V2) {
3289 unsigned NumElems = VT.getVectorNumElements();
3290 SmallVector<int, 8> Mask;
3291 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003292 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003293 Mask.push_back(i);
3294 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003295}
3296
Nate Begeman9008ca62009-04-27 18:41:29 +00003297/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003298static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003299 SDValue V2) {
3300 unsigned NumElems = VT.getVectorNumElements();
3301 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003302 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003303 Mask.push_back(i);
3304 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003305 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003307}
3308
Nate Begeman9008ca62009-04-27 18:41:29 +00003309/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003310static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003311 SDValue V2) {
3312 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003313 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003314 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003315 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003316 Mask.push_back(i + Half);
3317 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003318 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003319 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003320}
3321
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003322/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003323static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003324 bool HasSSE2) {
3325 if (SV->getValueType(0).getVectorNumElements() <= 4)
3326 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003327
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003329 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003330 DebugLoc dl = SV->getDebugLoc();
3331 SDValue V1 = SV->getOperand(0);
3332 int NumElems = VT.getVectorNumElements();
3333 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003334
Nate Begeman9008ca62009-04-27 18:41:29 +00003335 // unpack elements to the correct location
3336 while (NumElems > 4) {
3337 if (EltNo < NumElems/2) {
3338 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3339 } else {
3340 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3341 EltNo -= NumElems/2;
3342 }
3343 NumElems >>= 1;
3344 }
Eric Christopherfd179292009-08-27 18:07:15 +00003345
Nate Begeman9008ca62009-04-27 18:41:29 +00003346 // Perform the splat.
3347 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003348 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003349 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3350 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003351}
3352
Evan Chengba05f722006-04-21 23:03:30 +00003353/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003354/// vector of zero or undef vector. This produces a shuffle where the low
3355/// element of V2 is swizzled into the zero/undef vector, landing at element
3356/// 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 +00003357static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003358 bool isZero, bool HasSSE2,
3359 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003360 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003361 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3363 unsigned NumElems = VT.getVectorNumElements();
3364 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003365 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 // If this is the insertion idx, put the low elt of V2 here.
3367 MaskVec.push_back(i == Idx ? NumElems : i);
3368 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003369}
3370
Evan Chengf26ffe92008-05-29 08:22:04 +00003371/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3372/// a shuffle that is zero.
3373static
Nate Begeman9008ca62009-04-27 18:41:29 +00003374unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3375 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003376 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003378 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 int Idx = SVOp->getMaskElt(Index);
3380 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003381 ++NumZeros;
3382 continue;
3383 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003384 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003385 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003386 ++NumZeros;
3387 else
3388 break;
3389 }
3390 return NumZeros;
3391}
3392
3393/// isVectorShift - Returns true if the shuffle can be implemented as a
3394/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003395/// FIXME: split into pslldqi, psrldqi, palignr variants.
3396static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003397 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003399
3400 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003401 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003402 if (!NumZeros) {
3403 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003404 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003405 if (!NumZeros)
3406 return false;
3407 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003408 bool SeenV1 = false;
3409 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003410 for (int i = NumZeros; i < NumElems; ++i) {
3411 int Val = isLeft ? (i - NumZeros) : i;
3412 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3413 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003414 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003416 SeenV1 = true;
3417 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003418 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003419 SeenV2 = true;
3420 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003421 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003422 return false;
3423 }
3424 if (SeenV1 && SeenV2)
3425 return false;
3426
Nate Begeman9008ca62009-04-27 18:41:29 +00003427 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003428 ShAmt = NumZeros;
3429 return true;
3430}
3431
3432
Evan Chengc78d3b42006-04-24 18:01:45 +00003433/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3434///
Dan Gohman475871a2008-07-27 21:46:04 +00003435static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003436 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003437 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003438 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003439 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003440
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003441 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003442 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003443 bool First = true;
3444 for (unsigned i = 0; i < 16; ++i) {
3445 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3446 if (ThisIsNonZero && First) {
3447 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003448 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003449 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003450 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003451 First = false;
3452 }
3453
3454 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003455 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003456 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3457 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003458 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003460 }
3461 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3463 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3464 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003465 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003467 } else
3468 ThisElt = LastElt;
3469
Gabor Greifba36cb52008-08-28 21:40:38 +00003470 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003472 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003473 }
3474 }
3475
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003477}
3478
Bill Wendlinga348c562007-03-22 18:42:45 +00003479/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003480///
Dan Gohman475871a2008-07-27 21:46:04 +00003481static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003482 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003483 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003484 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003485 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003486
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003487 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003488 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003489 bool First = true;
3490 for (unsigned i = 0; i < 8; ++i) {
3491 bool isNonZero = (NonZeros & (1 << i)) != 0;
3492 if (isNonZero) {
3493 if (First) {
3494 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003496 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003498 First = false;
3499 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003500 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003501 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003502 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003503 }
3504 }
3505
3506 return V;
3507}
3508
Evan Chengf26ffe92008-05-29 08:22:04 +00003509/// getVShift - Return a vector logical shift node.
3510///
Owen Andersone50ed302009-08-10 22:56:29 +00003511static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003512 unsigned NumBits, SelectionDAG &DAG,
3513 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003514 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003515 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003516 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003517 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3518 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3519 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003520 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003521}
3522
Dan Gohman475871a2008-07-27 21:46:04 +00003523SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003524X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3525 SelectionDAG &DAG) {
3526
3527 // Check if the scalar load can be widened into a vector load. And if
3528 // the address is "base + cst" see if the cst can be "absorbed" into
3529 // the shuffle mask.
3530 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3531 SDValue Ptr = LD->getBasePtr();
3532 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3533 return SDValue();
3534 EVT PVT = LD->getValueType(0);
3535 if (PVT != MVT::i32 && PVT != MVT::f32)
3536 return SDValue();
3537
3538 int FI = -1;
3539 int64_t Offset = 0;
3540 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3541 FI = FINode->getIndex();
3542 Offset = 0;
3543 } else if (Ptr.getOpcode() == ISD::ADD &&
3544 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3545 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3546 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3547 Offset = Ptr.getConstantOperandVal(1);
3548 Ptr = Ptr.getOperand(0);
3549 } else {
3550 return SDValue();
3551 }
3552
3553 SDValue Chain = LD->getChain();
3554 // Make sure the stack object alignment is at least 16.
3555 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3556 if (DAG.InferPtrAlignment(Ptr) < 16) {
3557 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003558 // Can't change the alignment. FIXME: It's possible to compute
3559 // the exact stack offset and reference FI + adjust offset instead.
3560 // If someone *really* cares about this. That's the way to implement it.
3561 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003562 } else {
3563 MFI->setObjectAlignment(FI, 16);
3564 }
3565 }
3566
3567 // (Offset % 16) must be multiple of 4. Then address is then
3568 // Ptr + (Offset & ~15).
3569 if (Offset < 0)
3570 return SDValue();
3571 if ((Offset % 16) & 3)
3572 return SDValue();
3573 int64_t StartOffset = Offset & ~15;
3574 if (StartOffset)
3575 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3576 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3577
3578 int EltNo = (Offset - StartOffset) >> 2;
3579 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3580 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003581 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3582 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003583 // Canonicalize it to a v4i32 shuffle.
3584 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3585 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3586 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3587 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3588 }
3589
3590 return SDValue();
3591}
3592
3593SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003594X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003595 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003596 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003597 if (ISD::isBuildVectorAllZeros(Op.getNode())
3598 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003599 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3600 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3601 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003603 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003604
Gabor Greifba36cb52008-08-28 21:40:38 +00003605 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003606 return getOnesVector(Op.getValueType(), DAG, dl);
3607 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003608 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003609
Owen Andersone50ed302009-08-10 22:56:29 +00003610 EVT VT = Op.getValueType();
3611 EVT ExtVT = VT.getVectorElementType();
3612 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003613
3614 unsigned NumElems = Op.getNumOperands();
3615 unsigned NumZero = 0;
3616 unsigned NumNonZero = 0;
3617 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003618 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003619 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003620 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003621 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003622 if (Elt.getOpcode() == ISD::UNDEF)
3623 continue;
3624 Values.insert(Elt);
3625 if (Elt.getOpcode() != ISD::Constant &&
3626 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003627 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003628 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003629 NumZero++;
3630 else {
3631 NonZeros |= (1 << i);
3632 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003633 }
3634 }
3635
Dan Gohman7f321562007-06-25 16:23:39 +00003636 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003637 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003638 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003639 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003640
Chris Lattner67f453a2008-03-09 05:42:06 +00003641 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003642 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003643 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003644 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003645
Chris Lattner62098042008-03-09 01:05:04 +00003646 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3647 // the value are obviously zero, truncate the value to i32 and do the
3648 // insertion that way. Only do this if the value is non-constant or if the
3649 // value is a constant being inserted into element 0. It is cheaper to do
3650 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003651 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003652 (!IsAllConstants || Idx == 0)) {
3653 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3654 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3656 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003657
Chris Lattner62098042008-03-09 01:05:04 +00003658 // Truncate the value (which may itself be a constant) to i32, and
3659 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003660 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003661 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003662 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3663 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003664
Chris Lattner62098042008-03-09 01:05:04 +00003665 // Now we have our 32-bit value zero extended in the low element of
3666 // a vector. If Idx != 0, swizzle it into place.
3667 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003668 SmallVector<int, 4> Mask;
3669 Mask.push_back(Idx);
3670 for (unsigned i = 1; i != VecElts; ++i)
3671 Mask.push_back(i);
3672 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003673 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003674 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003675 }
Dale Johannesenace16102009-02-03 19:33:06 +00003676 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003677 }
3678 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003679
Chris Lattner19f79692008-03-08 22:59:52 +00003680 // If we have a constant or non-constant insertion into the low element of
3681 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3682 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003683 // depending on what the source datatype is.
3684 if (Idx == 0) {
3685 if (NumZero == 0) {
3686 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003687 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3688 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003689 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3690 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3691 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3692 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003693 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3694 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3695 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003696 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3697 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3698 Subtarget->hasSSE2(), DAG);
3699 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3700 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003701 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003702
3703 // Is it a vector logical left shift?
3704 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003705 X86::isZeroNode(Op.getOperand(0)) &&
3706 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003707 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003708 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003709 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003710 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003711 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003712 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003713
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003714 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003715 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003716
Chris Lattner19f79692008-03-08 22:59:52 +00003717 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3718 // is a non-constant being inserted into an element other than the low one,
3719 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3720 // movd/movss) to move this into the low element, then shuffle it into
3721 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003722 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003723 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003724
Evan Cheng0db9fe62006-04-25 20:13:52 +00003725 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003726 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3727 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003728 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003729 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003730 MaskVec.push_back(i == Idx ? 0 : 1);
3731 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003732 }
3733 }
3734
Chris Lattner67f453a2008-03-09 05:42:06 +00003735 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003736 if (Values.size() == 1) {
3737 if (EVTBits == 32) {
3738 // Instead of a shuffle like this:
3739 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3740 // Check if it's possible to issue this instead.
3741 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3742 unsigned Idx = CountTrailingZeros_32(NonZeros);
3743 SDValue Item = Op.getOperand(Idx);
3744 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3745 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3746 }
Dan Gohman475871a2008-07-27 21:46:04 +00003747 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003748 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003749
Dan Gohmana3941172007-07-24 22:55:08 +00003750 // A vector full of immediates; various special cases are already
3751 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003752 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003753 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003754
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003755 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003756 if (EVTBits == 64) {
3757 if (NumNonZero == 1) {
3758 // One half is zero or undef.
3759 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003760 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003761 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003762 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3763 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003764 }
Dan Gohman475871a2008-07-27 21:46:04 +00003765 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003766 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003767
3768 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003769 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003770 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003771 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003772 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003773 }
3774
Bill Wendling826f36f2007-03-28 00:57:11 +00003775 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003776 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003777 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003778 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003779 }
3780
3781 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003782 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003783 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003784 if (NumElems == 4 && NumZero > 0) {
3785 for (unsigned i = 0; i < 4; ++i) {
3786 bool isZero = !(NonZeros & (1 << i));
3787 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003788 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003789 else
Dale Johannesenace16102009-02-03 19:33:06 +00003790 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003791 }
3792
3793 for (unsigned i = 0; i < 2; ++i) {
3794 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3795 default: break;
3796 case 0:
3797 V[i] = V[i*2]; // Must be a zero vector.
3798 break;
3799 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003800 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003801 break;
3802 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003803 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003804 break;
3805 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003806 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003807 break;
3808 }
3809 }
3810
Nate Begeman9008ca62009-04-27 18:41:29 +00003811 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003812 bool Reverse = (NonZeros & 0x3) == 2;
3813 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003814 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003815 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3816 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3818 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003819 }
3820
3821 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003822 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3823 // values to be inserted is equal to the number of elements, in which case
3824 // use the unpack code below in the hopes of matching the consecutive elts
Eric Christopherfd179292009-08-27 18:07:15 +00003825 // load merge pattern for shuffles.
Nate Begeman9008ca62009-04-27 18:41:29 +00003826 // FIXME: We could probably just check that here directly.
Eric Christopherfd179292009-08-27 18:07:15 +00003827 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
Nate Begeman9008ca62009-04-27 18:41:29 +00003828 getSubtarget()->hasSSE41()) {
3829 V[0] = DAG.getUNDEF(VT);
3830 for (unsigned i = 0; i < NumElems; ++i)
3831 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3832 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3833 Op.getOperand(i), DAG.getIntPtrConstant(i));
3834 return V[0];
3835 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003836 // Expand into a number of unpckl*.
3837 // e.g. for v4f32
3838 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3839 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3840 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003841 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003842 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003843 NumElems >>= 1;
3844 while (NumElems != 0) {
3845 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003846 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003847 NumElems >>= 1;
3848 }
3849 return V[0];
3850 }
3851
Dan Gohman475871a2008-07-27 21:46:04 +00003852 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003853}
3854
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003855SDValue
3856X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3857 // We support concatenate two MMX registers and place them in a MMX
3858 // register. This is better than doing a stack convert.
3859 DebugLoc dl = Op.getDebugLoc();
3860 EVT ResVT = Op.getValueType();
3861 assert(Op.getNumOperands() == 2);
3862 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3863 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3864 int Mask[2];
3865 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3866 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3867 InVec = Op.getOperand(1);
3868 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
3869 unsigned NumElts = ResVT.getVectorNumElements();
3870 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3871 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
3872 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
3873 } else {
3874 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
3875 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3876 Mask[0] = 0; Mask[1] = 2;
3877 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
3878 }
3879 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
3880}
3881
Nate Begemanb9a47b82009-02-23 08:49:38 +00003882// v8i16 shuffles - Prefer shuffles in the following order:
3883// 1. [all] pshuflw, pshufhw, optional move
3884// 2. [ssse3] 1 x pshufb
3885// 3. [ssse3] 2 x pshufb + 1 x por
3886// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003887static
Nate Begeman9008ca62009-04-27 18:41:29 +00003888SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3889 SelectionDAG &DAG, X86TargetLowering &TLI) {
3890 SDValue V1 = SVOp->getOperand(0);
3891 SDValue V2 = SVOp->getOperand(1);
3892 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003893 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003894
Nate Begemanb9a47b82009-02-23 08:49:38 +00003895 // Determine if more than 1 of the words in each of the low and high quadwords
3896 // of the result come from the same quadword of one of the two inputs. Undef
3897 // mask values count as coming from any quadword, for better codegen.
3898 SmallVector<unsigned, 4> LoQuad(4);
3899 SmallVector<unsigned, 4> HiQuad(4);
3900 BitVector InputQuads(4);
3901 for (unsigned i = 0; i < 8; ++i) {
3902 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003903 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003904 MaskVals.push_back(EltIdx);
3905 if (EltIdx < 0) {
3906 ++Quad[0];
3907 ++Quad[1];
3908 ++Quad[2];
3909 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003910 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003911 }
3912 ++Quad[EltIdx / 4];
3913 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003914 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003915
Nate Begemanb9a47b82009-02-23 08:49:38 +00003916 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003917 unsigned MaxQuad = 1;
3918 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003919 if (LoQuad[i] > MaxQuad) {
3920 BestLoQuad = i;
3921 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003922 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003923 }
3924
Nate Begemanb9a47b82009-02-23 08:49:38 +00003925 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003926 MaxQuad = 1;
3927 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003928 if (HiQuad[i] > MaxQuad) {
3929 BestHiQuad = i;
3930 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003931 }
3932 }
3933
Nate Begemanb9a47b82009-02-23 08:49:38 +00003934 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00003935 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00003936 // single pshufb instruction is necessary. If There are more than 2 input
3937 // quads, disable the next transformation since it does not help SSSE3.
3938 bool V1Used = InputQuads[0] || InputQuads[1];
3939 bool V2Used = InputQuads[2] || InputQuads[3];
3940 if (TLI.getSubtarget()->hasSSSE3()) {
3941 if (InputQuads.count() == 2 && V1Used && V2Used) {
3942 BestLoQuad = InputQuads.find_first();
3943 BestHiQuad = InputQuads.find_next(BestLoQuad);
3944 }
3945 if (InputQuads.count() > 2) {
3946 BestLoQuad = -1;
3947 BestHiQuad = -1;
3948 }
3949 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003950
Nate Begemanb9a47b82009-02-23 08:49:38 +00003951 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3952 // the shuffle mask. If a quad is scored as -1, that means that it contains
3953 // words from all 4 input quadwords.
3954 SDValue NewV;
3955 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003956 SmallVector<int, 8> MaskV;
3957 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3958 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00003959 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003960 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3961 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
3962 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003963
Nate Begemanb9a47b82009-02-23 08:49:38 +00003964 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3965 // source words for the shuffle, to aid later transformations.
3966 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003967 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003968 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003969 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003970 if (idx != (int)i)
3971 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003972 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003973 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003974 AllWordsInNewV = false;
3975 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003976 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003977
Nate Begemanb9a47b82009-02-23 08:49:38 +00003978 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3979 if (AllWordsInNewV) {
3980 for (int i = 0; i != 8; ++i) {
3981 int idx = MaskVals[i];
3982 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003983 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00003984 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003985 if ((idx != i) && idx < 4)
3986 pshufhw = false;
3987 if ((idx != i) && idx > 3)
3988 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003989 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003990 V1 = NewV;
3991 V2Used = false;
3992 BestLoQuad = 0;
3993 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003994 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003995
Nate Begemanb9a47b82009-02-23 08:49:38 +00003996 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3997 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003998 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00003999 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004000 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004001 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004002 }
Eric Christopherfd179292009-08-27 18:07:15 +00004003
Nate Begemanb9a47b82009-02-23 08:49:38 +00004004 // If we have SSSE3, and all words of the result are from 1 input vector,
4005 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4006 // is present, fall back to case 4.
4007 if (TLI.getSubtarget()->hasSSSE3()) {
4008 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004009
Nate Begemanb9a47b82009-02-23 08:49:38 +00004010 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004011 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004012 // mask, and elements that come from V1 in the V2 mask, so that the two
4013 // results can be OR'd together.
4014 bool TwoInputs = V1Used && V2Used;
4015 for (unsigned i = 0; i != 8; ++i) {
4016 int EltIdx = MaskVals[i] * 2;
4017 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004018 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4019 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004020 continue;
4021 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004022 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4023 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004024 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004025 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004026 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004027 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004028 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004029 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004030 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004031
Nate Begemanb9a47b82009-02-23 08:49:38 +00004032 // Calculate the shuffle mask for the second input, shuffle it, and
4033 // OR it with the first shuffled input.
4034 pshufbMask.clear();
4035 for (unsigned i = 0; i != 8; ++i) {
4036 int EltIdx = MaskVals[i] * 2;
4037 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004038 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4039 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004040 continue;
4041 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004042 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4043 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004044 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004045 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004046 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004047 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004048 MVT::v16i8, &pshufbMask[0], 16));
4049 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4050 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004051 }
4052
4053 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4054 // and update MaskVals with new element order.
4055 BitVector InOrder(8);
4056 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004057 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004058 for (int i = 0; i != 4; ++i) {
4059 int idx = MaskVals[i];
4060 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004061 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004062 InOrder.set(i);
4063 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004064 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004065 InOrder.set(i);
4066 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004067 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004068 }
4069 }
4070 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004071 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004072 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004073 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004074 }
Eric Christopherfd179292009-08-27 18:07:15 +00004075
Nate Begemanb9a47b82009-02-23 08:49:38 +00004076 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4077 // and update MaskVals with the new element order.
4078 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004079 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004080 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004081 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004082 for (unsigned i = 4; i != 8; ++i) {
4083 int idx = MaskVals[i];
4084 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004085 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004086 InOrder.set(i);
4087 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004088 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004089 InOrder.set(i);
4090 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004091 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004092 }
4093 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004094 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004095 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004096 }
Eric Christopherfd179292009-08-27 18:07:15 +00004097
Nate Begemanb9a47b82009-02-23 08:49:38 +00004098 // In case BestHi & BestLo were both -1, which means each quadword has a word
4099 // from each of the four input quadwords, calculate the InOrder bitvector now
4100 // before falling through to the insert/extract cleanup.
4101 if (BestLoQuad == -1 && BestHiQuad == -1) {
4102 NewV = V1;
4103 for (int i = 0; i != 8; ++i)
4104 if (MaskVals[i] < 0 || MaskVals[i] == i)
4105 InOrder.set(i);
4106 }
Eric Christopherfd179292009-08-27 18:07:15 +00004107
Nate Begemanb9a47b82009-02-23 08:49:38 +00004108 // The other elements are put in the right place using pextrw and pinsrw.
4109 for (unsigned i = 0; i != 8; ++i) {
4110 if (InOrder[i])
4111 continue;
4112 int EltIdx = MaskVals[i];
4113 if (EltIdx < 0)
4114 continue;
4115 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004116 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004117 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004118 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004119 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004120 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004121 DAG.getIntPtrConstant(i));
4122 }
4123 return NewV;
4124}
4125
4126// v16i8 shuffles - Prefer shuffles in the following order:
4127// 1. [ssse3] 1 x pshufb
4128// 2. [ssse3] 2 x pshufb + 1 x por
4129// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4130static
Nate Begeman9008ca62009-04-27 18:41:29 +00004131SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4132 SelectionDAG &DAG, X86TargetLowering &TLI) {
4133 SDValue V1 = SVOp->getOperand(0);
4134 SDValue V2 = SVOp->getOperand(1);
4135 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004136 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004137 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004138
Nate Begemanb9a47b82009-02-23 08:49:38 +00004139 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004140 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004141 // present, fall back to case 3.
4142 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4143 bool V1Only = true;
4144 bool V2Only = true;
4145 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004146 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004147 if (EltIdx < 0)
4148 continue;
4149 if (EltIdx < 16)
4150 V2Only = false;
4151 else
4152 V1Only = false;
4153 }
Eric Christopherfd179292009-08-27 18:07:15 +00004154
Nate Begemanb9a47b82009-02-23 08:49:38 +00004155 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4156 if (TLI.getSubtarget()->hasSSSE3()) {
4157 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004158
Nate Begemanb9a47b82009-02-23 08:49:38 +00004159 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004160 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004161 //
4162 // Otherwise, we have elements from both input vectors, and must zero out
4163 // elements that come from V2 in the first mask, and V1 in the second mask
4164 // so that we can OR them together.
4165 bool TwoInputs = !(V1Only || V2Only);
4166 for (unsigned i = 0; i != 16; ++i) {
4167 int EltIdx = MaskVals[i];
4168 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004170 continue;
4171 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004173 }
4174 // If all the elements are from V2, assign it to V1 and return after
4175 // building the first pshufb.
4176 if (V2Only)
4177 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004178 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004179 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004180 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004181 if (!TwoInputs)
4182 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004183
Nate Begemanb9a47b82009-02-23 08:49:38 +00004184 // Calculate the shuffle mask for the second input, shuffle it, and
4185 // OR it with the first shuffled input.
4186 pshufbMask.clear();
4187 for (unsigned i = 0; i != 16; ++i) {
4188 int EltIdx = MaskVals[i];
4189 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004190 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004191 continue;
4192 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004193 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004194 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004196 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004197 MVT::v16i8, &pshufbMask[0], 16));
4198 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004199 }
Eric Christopherfd179292009-08-27 18:07:15 +00004200
Nate Begemanb9a47b82009-02-23 08:49:38 +00004201 // No SSSE3 - Calculate in place words and then fix all out of place words
4202 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4203 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004204 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4205 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004206 SDValue NewV = V2Only ? V2 : V1;
4207 for (int i = 0; i != 8; ++i) {
4208 int Elt0 = MaskVals[i*2];
4209 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004210
Nate Begemanb9a47b82009-02-23 08:49:38 +00004211 // This word of the result is all undef, skip it.
4212 if (Elt0 < 0 && Elt1 < 0)
4213 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004214
Nate Begemanb9a47b82009-02-23 08:49:38 +00004215 // This word of the result is already in the correct place, skip it.
4216 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4217 continue;
4218 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4219 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004220
Nate Begemanb9a47b82009-02-23 08:49:38 +00004221 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4222 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4223 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004224
4225 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4226 // using a single extract together, load it and store it.
4227 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004229 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004231 DAG.getIntPtrConstant(i));
4232 continue;
4233 }
4234
Nate Begemanb9a47b82009-02-23 08:49:38 +00004235 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004236 // source byte is not also odd, shift the extracted word left 8 bits
4237 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004238 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004240 DAG.getIntPtrConstant(Elt1 / 2));
4241 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004243 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004244 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004245 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4246 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004247 }
4248 // If Elt0 is defined, extract it from the appropriate source. If the
4249 // source byte is not also even, shift the extracted word right 8 bits. If
4250 // Elt1 was also defined, OR the extracted values together before
4251 // inserting them in the result.
4252 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004253 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004254 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4255 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004256 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004257 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004258 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004259 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4260 DAG.getConstant(0x00FF, MVT::i16));
4261 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004262 : InsElt0;
4263 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004264 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004265 DAG.getIntPtrConstant(i));
4266 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004267 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004268}
4269
Evan Cheng7a831ce2007-12-15 03:00:47 +00004270/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4271/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4272/// done when every pair / quad of shuffle mask elements point to elements in
4273/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004274/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4275static
Nate Begeman9008ca62009-04-27 18:41:29 +00004276SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4277 SelectionDAG &DAG,
4278 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004279 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004280 SDValue V1 = SVOp->getOperand(0);
4281 SDValue V2 = SVOp->getOperand(1);
4282 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004283 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004284 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004285 EVT MaskEltVT = MaskVT.getVectorElementType();
4286 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004287 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004288 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004289 case MVT::v4f32: NewVT = MVT::v2f64; break;
4290 case MVT::v4i32: NewVT = MVT::v2i64; break;
4291 case MVT::v8i16: NewVT = MVT::v4i32; break;
4292 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004293 }
4294
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004295 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004296 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004297 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004298 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004299 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004300 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004301 int Scale = NumElems / NewWidth;
4302 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004303 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004304 int StartIdx = -1;
4305 for (int j = 0; j < Scale; ++j) {
4306 int EltIdx = SVOp->getMaskElt(i+j);
4307 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004308 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004309 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004310 StartIdx = EltIdx - (EltIdx % Scale);
4311 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004312 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004313 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004314 if (StartIdx == -1)
4315 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004316 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004317 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004318 }
4319
Dale Johannesenace16102009-02-03 19:33:06 +00004320 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4321 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004322 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004323}
4324
Evan Chengd880b972008-05-09 21:53:03 +00004325/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004326///
Owen Andersone50ed302009-08-10 22:56:29 +00004327static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004328 SDValue SrcOp, SelectionDAG &DAG,
4329 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004330 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004331 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004332 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004333 LD = dyn_cast<LoadSDNode>(SrcOp);
4334 if (!LD) {
4335 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4336 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004337 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4338 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004339 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4340 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004341 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004342 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004343 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004344 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4345 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4346 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4347 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004348 SrcOp.getOperand(0)
4349 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004350 }
4351 }
4352 }
4353
Dale Johannesenace16102009-02-03 19:33:06 +00004354 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4355 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004356 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004357 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004358}
4359
Evan Chengace3c172008-07-22 21:13:36 +00004360/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4361/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004362static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004363LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4364 SDValue V1 = SVOp->getOperand(0);
4365 SDValue V2 = SVOp->getOperand(1);
4366 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004367 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004368
Evan Chengace3c172008-07-22 21:13:36 +00004369 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004370 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004371 SmallVector<int, 8> Mask1(4U, -1);
4372 SmallVector<int, 8> PermMask;
4373 SVOp->getMask(PermMask);
4374
Evan Chengace3c172008-07-22 21:13:36 +00004375 unsigned NumHi = 0;
4376 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004377 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004378 int Idx = PermMask[i];
4379 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004380 Locs[i] = std::make_pair(-1, -1);
4381 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004382 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4383 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004384 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004385 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004386 NumLo++;
4387 } else {
4388 Locs[i] = std::make_pair(1, NumHi);
4389 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004390 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004391 NumHi++;
4392 }
4393 }
4394 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004395
Evan Chengace3c172008-07-22 21:13:36 +00004396 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004397 // If no more than two elements come from either vector. This can be
4398 // implemented with two shuffles. First shuffle gather the elements.
4399 // The second shuffle, which takes the first shuffle as both of its
4400 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004401 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004402
Nate Begeman9008ca62009-04-27 18:41:29 +00004403 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004404
Evan Chengace3c172008-07-22 21:13:36 +00004405 for (unsigned i = 0; i != 4; ++i) {
4406 if (Locs[i].first == -1)
4407 continue;
4408 else {
4409 unsigned Idx = (i < 2) ? 0 : 4;
4410 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004411 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004412 }
4413 }
4414
Nate Begeman9008ca62009-04-27 18:41:29 +00004415 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004416 } else if (NumLo == 3 || NumHi == 3) {
4417 // Otherwise, we must have three elements from one vector, call it X, and
4418 // one element from the other, call it Y. First, use a shufps to build an
4419 // intermediate vector with the one element from Y and the element from X
4420 // that will be in the same half in the final destination (the indexes don't
4421 // matter). Then, use a shufps to build the final vector, taking the half
4422 // containing the element from Y from the intermediate, and the other half
4423 // from X.
4424 if (NumHi == 3) {
4425 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004426 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004427 std::swap(V1, V2);
4428 }
4429
4430 // Find the element from V2.
4431 unsigned HiIndex;
4432 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004433 int Val = PermMask[HiIndex];
4434 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004435 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004436 if (Val >= 4)
4437 break;
4438 }
4439
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 Mask1[0] = PermMask[HiIndex];
4441 Mask1[1] = -1;
4442 Mask1[2] = PermMask[HiIndex^1];
4443 Mask1[3] = -1;
4444 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004445
4446 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004447 Mask1[0] = PermMask[0];
4448 Mask1[1] = PermMask[1];
4449 Mask1[2] = HiIndex & 1 ? 6 : 4;
4450 Mask1[3] = HiIndex & 1 ? 4 : 6;
4451 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004452 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004453 Mask1[0] = HiIndex & 1 ? 2 : 0;
4454 Mask1[1] = HiIndex & 1 ? 0 : 2;
4455 Mask1[2] = PermMask[2];
4456 Mask1[3] = PermMask[3];
4457 if (Mask1[2] >= 0)
4458 Mask1[2] += 4;
4459 if (Mask1[3] >= 0)
4460 Mask1[3] += 4;
4461 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004462 }
Evan Chengace3c172008-07-22 21:13:36 +00004463 }
4464
4465 // Break it into (shuffle shuffle_hi, shuffle_lo).
4466 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004467 SmallVector<int,8> LoMask(4U, -1);
4468 SmallVector<int,8> HiMask(4U, -1);
4469
4470 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004471 unsigned MaskIdx = 0;
4472 unsigned LoIdx = 0;
4473 unsigned HiIdx = 2;
4474 for (unsigned i = 0; i != 4; ++i) {
4475 if (i == 2) {
4476 MaskPtr = &HiMask;
4477 MaskIdx = 1;
4478 LoIdx = 0;
4479 HiIdx = 2;
4480 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004481 int Idx = PermMask[i];
4482 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004483 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004484 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004485 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004486 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004487 LoIdx++;
4488 } else {
4489 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004490 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004491 HiIdx++;
4492 }
4493 }
4494
Nate Begeman9008ca62009-04-27 18:41:29 +00004495 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4496 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4497 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004498 for (unsigned i = 0; i != 4; ++i) {
4499 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004500 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004501 } else {
4502 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004503 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004504 }
4505 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004506 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004507}
4508
Dan Gohman475871a2008-07-27 21:46:04 +00004509SDValue
4510X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004511 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004512 SDValue V1 = Op.getOperand(0);
4513 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004514 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004515 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004516 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004517 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004518 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4519 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004520 bool V1IsSplat = false;
4521 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004522
Nate Begeman9008ca62009-04-27 18:41:29 +00004523 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004524 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004525
Nate Begeman9008ca62009-04-27 18:41:29 +00004526 // Promote splats to v4f32.
4527 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004528 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004529 return Op;
4530 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004531 }
4532
Evan Cheng7a831ce2007-12-15 03:00:47 +00004533 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4534 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004535 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004536 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004537 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004538 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004539 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004540 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004541 // FIXME: Figure out a cleaner way to do this.
4542 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004543 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004544 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004545 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4547 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4548 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004549 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004550 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004551 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4552 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004553 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004554 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004555 }
4556 }
Eric Christopherfd179292009-08-27 18:07:15 +00004557
Nate Begeman9008ca62009-04-27 18:41:29 +00004558 if (X86::isPSHUFDMask(SVOp))
4559 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004560
Evan Chengf26ffe92008-05-29 08:22:04 +00004561 // Check if this can be converted into a logical shift.
4562 bool isLeft = false;
4563 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004564 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004565 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004566 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004567 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004568 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004569 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004570 EVT EltVT = VT.getVectorElementType();
4571 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004572 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004573 }
Eric Christopherfd179292009-08-27 18:07:15 +00004574
Nate Begeman9008ca62009-04-27 18:41:29 +00004575 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004576 if (V1IsUndef)
4577 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004578 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004579 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004580 if (!isMMX)
4581 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004582 }
Eric Christopherfd179292009-08-27 18:07:15 +00004583
Nate Begeman9008ca62009-04-27 18:41:29 +00004584 // FIXME: fold these into legal mask.
4585 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4586 X86::isMOVSLDUPMask(SVOp) ||
4587 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004588 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004589 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004590 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004591
Nate Begeman9008ca62009-04-27 18:41:29 +00004592 if (ShouldXformToMOVHLPS(SVOp) ||
4593 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4594 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004595
Evan Chengf26ffe92008-05-29 08:22:04 +00004596 if (isShift) {
4597 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004598 EVT EltVT = VT.getVectorElementType();
4599 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004600 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004601 }
Eric Christopherfd179292009-08-27 18:07:15 +00004602
Evan Cheng9eca5e82006-10-25 21:49:50 +00004603 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004604 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4605 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004606 V1IsSplat = isSplatVector(V1.getNode());
4607 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004608
Chris Lattner8a594482007-11-25 00:24:49 +00004609 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004610 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004611 Op = CommuteVectorShuffle(SVOp, DAG);
4612 SVOp = cast<ShuffleVectorSDNode>(Op);
4613 V1 = SVOp->getOperand(0);
4614 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004615 std::swap(V1IsSplat, V2IsSplat);
4616 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004617 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004618 }
4619
Nate Begeman9008ca62009-04-27 18:41:29 +00004620 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4621 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004622 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004623 return V1;
4624 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4625 // the instruction selector will not match, so get a canonical MOVL with
4626 // swapped operands to undo the commute.
4627 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004628 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004629
Nate Begeman9008ca62009-04-27 18:41:29 +00004630 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4631 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4632 X86::isUNPCKLMask(SVOp) ||
4633 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004634 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004635
Evan Cheng9bbbb982006-10-25 20:48:19 +00004636 if (V2IsSplat) {
4637 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004638 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004639 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004640 SDValue NewMask = NormalizeMask(SVOp, DAG);
4641 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4642 if (NSVOp != SVOp) {
4643 if (X86::isUNPCKLMask(NSVOp, true)) {
4644 return NewMask;
4645 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4646 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004647 }
4648 }
4649 }
4650
Evan Cheng9eca5e82006-10-25 21:49:50 +00004651 if (Commuted) {
4652 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004653 // FIXME: this seems wrong.
4654 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4655 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4656 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4657 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4658 X86::isUNPCKLMask(NewSVOp) ||
4659 X86::isUNPCKHMask(NewSVOp))
4660 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004661 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004662
Nate Begemanb9a47b82009-02-23 08:49:38 +00004663 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004664
4665 // Normalize the node to match x86 shuffle ops if needed
4666 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4667 return CommuteVectorShuffle(SVOp, DAG);
4668
4669 // Check for legal shuffle and return?
4670 SmallVector<int, 16> PermMask;
4671 SVOp->getMask(PermMask);
4672 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004673 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004674
Evan Cheng14b32e12007-12-11 01:46:18 +00004675 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004676 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004678 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004679 return NewOp;
4680 }
4681
Owen Anderson825b72b2009-08-11 20:47:22 +00004682 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004683 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004684 if (NewOp.getNode())
4685 return NewOp;
4686 }
Eric Christopherfd179292009-08-27 18:07:15 +00004687
Evan Chengace3c172008-07-22 21:13:36 +00004688 // Handle all 4 wide cases with a number of shuffles except for MMX.
4689 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004690 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004691
Dan Gohman475871a2008-07-27 21:46:04 +00004692 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004693}
4694
Dan Gohman475871a2008-07-27 21:46:04 +00004695SDValue
4696X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004697 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004698 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004699 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004700 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004701 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004702 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004703 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004704 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004705 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004706 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004707 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4708 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4709 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004710 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4711 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004712 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004713 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004714 Op.getOperand(0)),
4715 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004716 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004717 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004718 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004719 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004720 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004721 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004722 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4723 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004724 // result has a single use which is a store or a bitcast to i32. And in
4725 // the case of a store, it's not worth it if the index is a constant 0,
4726 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004727 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004728 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004729 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004730 if ((User->getOpcode() != ISD::STORE ||
4731 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4732 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004733 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004734 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004735 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004736 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4737 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004738 Op.getOperand(0)),
4739 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004740 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4741 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004742 // ExtractPS works with constant index.
4743 if (isa<ConstantSDNode>(Op.getOperand(1)))
4744 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004745 }
Dan Gohman475871a2008-07-27 21:46:04 +00004746 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004747}
4748
4749
Dan Gohman475871a2008-07-27 21:46:04 +00004750SDValue
4751X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004752 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004753 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004754
Evan Cheng62a3f152008-03-24 21:52:23 +00004755 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004756 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004757 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004758 return Res;
4759 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004760
Owen Andersone50ed302009-08-10 22:56:29 +00004761 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004762 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004763 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004764 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004765 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004766 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004767 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004768 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4769 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004770 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004771 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004772 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004773 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004774 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004775 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004776 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004777 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004778 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004779 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004780 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004781 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004782 if (Idx == 0)
4783 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004784
Evan Cheng0db9fe62006-04-25 20:13:52 +00004785 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004786 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004787 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004788 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004789 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004790 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004791 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004792 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004793 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4794 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4795 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004796 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004797 if (Idx == 0)
4798 return Op;
4799
4800 // UNPCKHPD the element to the lowest double word, then movsd.
4801 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4802 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004803 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004804 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004805 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004806 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004807 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004808 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004809 }
4810
Dan Gohman475871a2008-07-27 21:46:04 +00004811 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004812}
4813
Dan Gohman475871a2008-07-27 21:46:04 +00004814SDValue
4815X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004816 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004817 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004818 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004819
Dan Gohman475871a2008-07-27 21:46:04 +00004820 SDValue N0 = Op.getOperand(0);
4821 SDValue N1 = Op.getOperand(1);
4822 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004823
Dan Gohman8a55ce42009-09-23 21:02:20 +00004824 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004825 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004826 unsigned Opc;
4827 if (VT == MVT::v8i16)
4828 Opc = X86ISD::PINSRW;
4829 else if (VT == MVT::v4i16)
4830 Opc = X86ISD::MMX_PINSRW;
4831 else if (VT == MVT::v16i8)
4832 Opc = X86ISD::PINSRB;
4833 else
4834 Opc = X86ISD::PINSRB;
4835
Nate Begeman14d12ca2008-02-11 04:19:36 +00004836 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4837 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 if (N1.getValueType() != MVT::i32)
4839 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4840 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004841 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004842 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004843 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004844 // Bits [7:6] of the constant are the source select. This will always be
4845 // zero here. The DAG Combiner may combine an extract_elt index into these
4846 // bits. For example (insert (extract, 3), 2) could be matched by putting
4847 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004848 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004849 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004850 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004851 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004852 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004853 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004854 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004855 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004856 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004857 // PINSR* works with constant index.
4858 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004859 }
Dan Gohman475871a2008-07-27 21:46:04 +00004860 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004861}
4862
Dan Gohman475871a2008-07-27 21:46:04 +00004863SDValue
4864X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004865 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004866 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004867
4868 if (Subtarget->hasSSE41())
4869 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4870
Dan Gohman8a55ce42009-09-23 21:02:20 +00004871 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004872 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004873
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004874 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004875 SDValue N0 = Op.getOperand(0);
4876 SDValue N1 = Op.getOperand(1);
4877 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004878
Dan Gohman8a55ce42009-09-23 21:02:20 +00004879 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004880 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4881 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004882 if (N1.getValueType() != MVT::i32)
4883 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4884 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004885 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004886 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
4887 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004888 }
Dan Gohman475871a2008-07-27 21:46:04 +00004889 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004890}
4891
Dan Gohman475871a2008-07-27 21:46:04 +00004892SDValue
4893X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004894 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00004895 if (Op.getValueType() == MVT::v2f32)
4896 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4897 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4898 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004899 Op.getOperand(0))));
4900
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
4902 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004903
Owen Anderson825b72b2009-08-11 20:47:22 +00004904 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
4905 EVT VT = MVT::v2i32;
4906 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00004907 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00004908 case MVT::v16i8:
4909 case MVT::v8i16:
4910 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00004911 break;
4912 }
Dale Johannesenace16102009-02-03 19:33:06 +00004913 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4914 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004915}
4916
Bill Wendling056292f2008-09-16 21:48:12 +00004917// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4918// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4919// one of the above mentioned nodes. It has to be wrapped because otherwise
4920// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4921// be used to form addressing mode. These wrapped nodes will be selected
4922// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004923SDValue
4924X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004925 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004926
Chris Lattner41621a22009-06-26 19:22:52 +00004927 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4928 // global base reg.
4929 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004930 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004931 CodeModel::Model M = getTargetMachine().getCodeModel();
4932
Chris Lattner4f066492009-07-11 20:29:19 +00004933 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004934 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004935 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004936 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004937 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004938 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004939 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004940
Evan Cheng1606e8e2009-03-13 07:51:59 +00004941 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004942 CP->getAlignment(),
4943 CP->getOffset(), OpFlag);
4944 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004945 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004946 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004947 if (OpFlag) {
4948 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004949 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004950 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004951 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004952 }
4953
4954 return Result;
4955}
4956
Chris Lattner18c59872009-06-27 04:16:01 +00004957SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4958 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00004959
Chris Lattner18c59872009-06-27 04:16:01 +00004960 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4961 // global base reg.
4962 unsigned char OpFlag = 0;
4963 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004964 CodeModel::Model M = getTargetMachine().getCodeModel();
4965
Chris Lattner4f066492009-07-11 20:29:19 +00004966 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004967 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00004968 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004969 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004970 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004971 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004972 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00004973
Chris Lattner18c59872009-06-27 04:16:01 +00004974 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4975 OpFlag);
4976 DebugLoc DL = JT->getDebugLoc();
4977 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00004978
Chris Lattner18c59872009-06-27 04:16:01 +00004979 // With PIC, the address is actually $g + Offset.
4980 if (OpFlag) {
4981 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4982 DAG.getNode(X86ISD::GlobalBaseReg,
4983 DebugLoc::getUnknownLoc(), getPointerTy()),
4984 Result);
4985 }
Eric Christopherfd179292009-08-27 18:07:15 +00004986
Chris Lattner18c59872009-06-27 04:16:01 +00004987 return Result;
4988}
4989
4990SDValue
4991X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4992 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00004993
Chris Lattner18c59872009-06-27 04:16:01 +00004994 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4995 // global base reg.
4996 unsigned char OpFlag = 0;
4997 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00004998 CodeModel::Model M = getTargetMachine().getCodeModel();
4999
Chris Lattner4f066492009-07-11 20:29:19 +00005000 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005001 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005002 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005003 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005004 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005005 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005006 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005007
Chris Lattner18c59872009-06-27 04:16:01 +00005008 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005009
Chris Lattner18c59872009-06-27 04:16:01 +00005010 DebugLoc DL = Op.getDebugLoc();
5011 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005012
5013
Chris Lattner18c59872009-06-27 04:16:01 +00005014 // With PIC, the address is actually $g + Offset.
5015 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005016 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005017 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5018 DAG.getNode(X86ISD::GlobalBaseReg,
5019 DebugLoc::getUnknownLoc(),
5020 getPointerTy()),
5021 Result);
5022 }
Eric Christopherfd179292009-08-27 18:07:15 +00005023
Chris Lattner18c59872009-06-27 04:16:01 +00005024 return Result;
5025}
5026
Dan Gohman475871a2008-07-27 21:46:04 +00005027SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00005028X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00005029 // Create the TargetBlockAddressAddress node.
5030 unsigned char OpFlags =
5031 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005032 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman29cbade2009-11-20 23:18:13 +00005033 BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
5034 DebugLoc dl = Op.getDebugLoc();
5035 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5036 /*isTarget=*/true, OpFlags);
5037
Dan Gohmanf705adb2009-10-30 01:28:02 +00005038 if (Subtarget->isPICStyleRIPRel() &&
5039 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005040 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5041 else
5042 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005043
Dan Gohman29cbade2009-11-20 23:18:13 +00005044 // With PIC, the address is actually $g + Offset.
5045 if (isGlobalRelativeToPICBase(OpFlags)) {
5046 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5047 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5048 Result);
5049 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005050
5051 return Result;
5052}
5053
5054SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005055X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005056 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005057 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005058 // Create the TargetGlobalAddress node, folding in the constant
5059 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005060 unsigned char OpFlags =
5061 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005062 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005063 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005064 if (OpFlags == X86II::MO_NO_FLAG &&
5065 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005066 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005067 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005068 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005069 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005070 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005071 }
Eric Christopherfd179292009-08-27 18:07:15 +00005072
Chris Lattner4f066492009-07-11 20:29:19 +00005073 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005074 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005075 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5076 else
5077 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005078
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005079 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005080 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005081 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5082 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005083 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005084 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005085
Chris Lattner36c25012009-07-10 07:34:39 +00005086 // For globals that require a load from a stub to get the address, emit the
5087 // load.
5088 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005089 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005090 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005091
Dan Gohman6520e202008-10-18 02:06:02 +00005092 // If there was a non-zero offset that we didn't fold, create an explicit
5093 // addition for it.
5094 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005095 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005096 DAG.getConstant(Offset, getPointerTy()));
5097
Evan Cheng0db9fe62006-04-25 20:13:52 +00005098 return Result;
5099}
5100
Evan Chengda43bcf2008-09-24 00:05:32 +00005101SDValue
5102X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
5103 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005104 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005105 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005106}
5107
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005108static SDValue
5109GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005110 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005111 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005112 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005113 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005114 DebugLoc dl = GA->getDebugLoc();
5115 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5116 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005117 GA->getOffset(),
5118 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005119 if (InFlag) {
5120 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005121 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005122 } else {
5123 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005124 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005125 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005126
5127 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5128 MFI->setHasCalls(true);
5129
Rafael Espindola15f1b662009-04-24 12:59:40 +00005130 SDValue Flag = Chain.getValue(1);
5131 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005132}
5133
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005134// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005135static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005136LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005137 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005138 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005139 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5140 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005141 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005142 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005143 PtrVT), InFlag);
5144 InFlag = Chain.getValue(1);
5145
Chris Lattnerb903bed2009-06-26 21:20:29 +00005146 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005147}
5148
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005149// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005150static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005151LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005152 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005153 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5154 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005155}
5156
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005157// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5158// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005159static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005160 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005161 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005162 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005163 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005164 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
5165 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005166 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005167 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005168
5169 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005170 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005171
Chris Lattnerb903bed2009-06-26 21:20:29 +00005172 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005173 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5174 // initialexec.
5175 unsigned WrapperKind = X86ISD::Wrapper;
5176 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005177 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005178 } else if (is64Bit) {
5179 assert(model == TLSModel::InitialExec);
5180 OperandFlags = X86II::MO_GOTTPOFF;
5181 WrapperKind = X86ISD::WrapperRIP;
5182 } else {
5183 assert(model == TLSModel::InitialExec);
5184 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005185 }
Eric Christopherfd179292009-08-27 18:07:15 +00005186
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005187 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5188 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005189 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005190 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005191 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005192
Rafael Espindola9a580232009-02-27 13:37:18 +00005193 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005194 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005195 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005196
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005197 // The address of the thread local variable is the add of the thread
5198 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005199 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005200}
5201
Dan Gohman475871a2008-07-27 21:46:04 +00005202SDValue
5203X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005204 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005205 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005206 assert(Subtarget->isTargetELF() &&
5207 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005208 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005209 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005210
Chris Lattnerb903bed2009-06-26 21:20:29 +00005211 // If GV is an alias then use the aliasee for determining
5212 // thread-localness.
5213 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5214 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005215
Chris Lattnerb903bed2009-06-26 21:20:29 +00005216 TLSModel::Model model = getTLSModel(GV,
5217 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005218
Chris Lattnerb903bed2009-06-26 21:20:29 +00005219 switch (model) {
5220 case TLSModel::GeneralDynamic:
5221 case TLSModel::LocalDynamic: // not implemented
5222 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005223 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005224 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005225
Chris Lattnerb903bed2009-06-26 21:20:29 +00005226 case TLSModel::InitialExec:
5227 case TLSModel::LocalExec:
5228 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5229 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005230 }
Eric Christopherfd179292009-08-27 18:07:15 +00005231
Torok Edwinc23197a2009-07-14 16:55:14 +00005232 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005233 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005234}
5235
Evan Cheng0db9fe62006-04-25 20:13:52 +00005236
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005237/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005238/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005239SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005240 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005241 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005242 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005243 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005244 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005245 SDValue ShOpLo = Op.getOperand(0);
5246 SDValue ShOpHi = Op.getOperand(1);
5247 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005248 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005249 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005250 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005251
Dan Gohman475871a2008-07-27 21:46:04 +00005252 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005253 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005254 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5255 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005256 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005257 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5258 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005259 }
Evan Chenge3413162006-01-09 18:33:28 +00005260
Owen Anderson825b72b2009-08-11 20:47:22 +00005261 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5262 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005263 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005264 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005265
Dan Gohman475871a2008-07-27 21:46:04 +00005266 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005267 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005268 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5269 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005270
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005271 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005272 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5273 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005274 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005275 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5276 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005277 }
5278
Dan Gohman475871a2008-07-27 21:46:04 +00005279 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005280 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005281}
Evan Chenga3195e82006-01-12 22:54:21 +00005282
Dan Gohman475871a2008-07-27 21:46:04 +00005283SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005284 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005285
5286 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005287 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005288 return Op;
5289 }
5290 return SDValue();
5291 }
5292
Owen Anderson825b72b2009-08-11 20:47:22 +00005293 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005294 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005295
Eli Friedman36df4992009-05-27 00:47:34 +00005296 // These are really Legal; return the operand so the caller accepts it as
5297 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005298 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005299 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005300 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005301 Subtarget->is64Bit()) {
5302 return Op;
5303 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005304
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005305 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005306 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005307 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005308 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005309 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005310 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005311 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005312 PseudoSourceValue::getFixedStack(SSFI), 0,
5313 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005314 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5315}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005316
Owen Andersone50ed302009-08-10 22:56:29 +00005317SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005318 SDValue StackSlot,
5319 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005320 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005321 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005322 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005323 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005324 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005325 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005326 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005327 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005328 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005329 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005330 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005331
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005332 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005333 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005334 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005335
5336 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5337 // shouldn't be necessary except that RFP cannot be live across
5338 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005339 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005340 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005341 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005342 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005343 SDValue Ops[] = {
5344 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5345 };
5346 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005347 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005348 PseudoSourceValue::getFixedStack(SSFI), 0,
5349 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005350 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005351
Evan Cheng0db9fe62006-04-25 20:13:52 +00005352 return Result;
5353}
5354
Bill Wendling8b8a6362009-01-17 03:56:04 +00005355// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5356SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5357 // This algorithm is not obvious. Here it is in C code, more or less:
5358 /*
5359 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5360 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5361 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005362
Bill Wendling8b8a6362009-01-17 03:56:04 +00005363 // Copy ints to xmm registers.
5364 __m128i xh = _mm_cvtsi32_si128( hi );
5365 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005366
Bill Wendling8b8a6362009-01-17 03:56:04 +00005367 // Combine into low half of a single xmm register.
5368 __m128i x = _mm_unpacklo_epi32( xh, xl );
5369 __m128d d;
5370 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005371
Bill Wendling8b8a6362009-01-17 03:56:04 +00005372 // Merge in appropriate exponents to give the integer bits the right
5373 // magnitude.
5374 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005375
Bill Wendling8b8a6362009-01-17 03:56:04 +00005376 // Subtract away the biases to deal with the IEEE-754 double precision
5377 // implicit 1.
5378 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005379
Bill Wendling8b8a6362009-01-17 03:56:04 +00005380 // All conversions up to here are exact. The correctly rounded result is
5381 // calculated using the current rounding mode using the following
5382 // horizontal add.
5383 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5384 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5385 // store doesn't really need to be here (except
5386 // maybe to zero the other double)
5387 return sd;
5388 }
5389 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005390
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005391 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005392 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005393
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005394 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005395 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005396 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5397 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5398 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5399 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005400 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005401 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005402
Bill Wendling8b8a6362009-01-17 03:56:04 +00005403 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005404 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005405 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005406 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005407 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005408 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005409 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005410
Owen Anderson825b72b2009-08-11 20:47:22 +00005411 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5412 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005413 Op.getOperand(0),
5414 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5416 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005417 Op.getOperand(0),
5418 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005419 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5420 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005421 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005422 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005423 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5424 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5425 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005426 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005427 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005428 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005429
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005430 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005431 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005432 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5433 DAG.getUNDEF(MVT::v2f64), ShufMask);
5434 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5435 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005436 DAG.getIntPtrConstant(0));
5437}
5438
Bill Wendling8b8a6362009-01-17 03:56:04 +00005439// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5440SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005441 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005442 // FP constant to bias correct the final result.
5443 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005444 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005445
5446 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005447 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5448 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005449 Op.getOperand(0),
5450 DAG.getIntPtrConstant(0)));
5451
Owen Anderson825b72b2009-08-11 20:47:22 +00005452 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5453 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005454 DAG.getIntPtrConstant(0));
5455
5456 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005457 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5458 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005459 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005460 MVT::v2f64, Load)),
5461 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005462 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005463 MVT::v2f64, Bias)));
5464 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5465 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005466 DAG.getIntPtrConstant(0));
5467
5468 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005469 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005470
5471 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005472 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005473
Owen Anderson825b72b2009-08-11 20:47:22 +00005474 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005475 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005476 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005477 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005478 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005479 }
5480
5481 // Handle final rounding.
5482 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005483}
5484
5485SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005486 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005487 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005488
Evan Chenga06ec9e2009-01-19 08:08:22 +00005489 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5490 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5491 // the optimization here.
5492 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005493 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005494
Owen Andersone50ed302009-08-10 22:56:29 +00005495 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005496 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005497 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005498 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005499 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005500
Bill Wendling8b8a6362009-01-17 03:56:04 +00005501 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005502 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005503 return LowerUINT_TO_FP_i32(Op, DAG);
5504 }
5505
Owen Anderson825b72b2009-08-11 20:47:22 +00005506 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005507
5508 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005509 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005510 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5511 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5512 getPointerTy(), StackSlot, WordOff);
5513 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005514 StackSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005515 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00005516 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005517 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005518}
5519
Dan Gohman475871a2008-07-27 21:46:04 +00005520std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005521FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005522 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005523
Owen Andersone50ed302009-08-10 22:56:29 +00005524 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005525
5526 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005527 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5528 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005529 }
5530
Owen Anderson825b72b2009-08-11 20:47:22 +00005531 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5532 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005533 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005534
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005535 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005536 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005537 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005538 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005539 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005540 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005541 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005542 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005543
Evan Cheng87c89352007-10-15 20:11:21 +00005544 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5545 // stack slot.
5546 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005547 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005548 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005549 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005550
Evan Cheng0db9fe62006-04-25 20:13:52 +00005551 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005552 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005553 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005554 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5555 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5556 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005557 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005558
Dan Gohman475871a2008-07-27 21:46:04 +00005559 SDValue Chain = DAG.getEntryNode();
5560 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005561 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005563 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005564 PseudoSourceValue::getFixedStack(SSFI), 0,
5565 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005566 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005567 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005568 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5569 };
Dale Johannesenace16102009-02-03 19:33:06 +00005570 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005571 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005572 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005573 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5574 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005575
Evan Cheng0db9fe62006-04-25 20:13:52 +00005576 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005577 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005578 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005579
Chris Lattner27a6c732007-11-24 07:07:01 +00005580 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005581}
5582
Dan Gohman475871a2008-07-27 21:46:04 +00005583SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005584 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005585 if (Op.getValueType() == MVT::v2i32 &&
5586 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005587 return Op;
5588 }
5589 return SDValue();
5590 }
5591
Eli Friedman948e95a2009-05-23 09:59:16 +00005592 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005593 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005594 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5595 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005596
Chris Lattner27a6c732007-11-24 07:07:01 +00005597 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005598 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005599 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005600}
5601
Eli Friedman948e95a2009-05-23 09:59:16 +00005602SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5603 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5604 SDValue FIST = Vals.first, StackSlot = Vals.second;
5605 assert(FIST.getNode() && "Unexpected failure");
5606
5607 // Load the result.
5608 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005609 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005610}
5611
Dan Gohman475871a2008-07-27 21:46:04 +00005612SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005613 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005614 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005615 EVT VT = Op.getValueType();
5616 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005617 if (VT.isVector())
5618 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005619 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005620 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005621 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005622 CV.push_back(C);
5623 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005624 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005625 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005626 CV.push_back(C);
5627 CV.push_back(C);
5628 CV.push_back(C);
5629 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005630 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005631 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005632 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005633 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005634 PseudoSourceValue::getConstantPool(), 0,
5635 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005636 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005637}
5638
Dan Gohman475871a2008-07-27 21:46:04 +00005639SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005640 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005641 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005642 EVT VT = Op.getValueType();
5643 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005644 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005645 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005646 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005647 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005648 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005649 CV.push_back(C);
5650 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005651 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005652 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005653 CV.push_back(C);
5654 CV.push_back(C);
5655 CV.push_back(C);
5656 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005657 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005658 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005659 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005660 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005661 PseudoSourceValue::getConstantPool(), 0,
5662 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005663 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005664 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005665 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5666 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005667 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005668 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005669 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005670 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005671 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005672}
5673
Dan Gohman475871a2008-07-27 21:46:04 +00005674SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005675 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005676 SDValue Op0 = Op.getOperand(0);
5677 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005678 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005679 EVT VT = Op.getValueType();
5680 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005681
5682 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005683 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005684 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005685 SrcVT = VT;
5686 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005687 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005688 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005689 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005690 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005691 }
5692
5693 // At this point the operands and the result should have the same
5694 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005695
Evan Cheng68c47cb2007-01-05 07:55:56 +00005696 // First get the sign bit of second operand.
5697 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005698 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005699 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5700 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005701 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005702 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5703 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5704 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5705 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005706 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005707 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005708 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005709 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005710 PseudoSourceValue::getConstantPool(), 0,
5711 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005712 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005713
5714 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005715 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005716 // Op0 is MVT::f32, Op1 is MVT::f64.
5717 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5718 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5719 DAG.getConstant(32, MVT::i32));
5720 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5721 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005722 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005723 }
5724
Evan Cheng73d6cf12007-01-05 21:37:56 +00005725 // Clear first operand sign bit.
5726 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005727 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005728 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5729 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005730 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005731 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5732 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5733 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5734 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005735 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005736 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005737 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005738 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005739 PseudoSourceValue::getConstantPool(), 0,
5740 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005741 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005742
5743 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005744 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005745}
5746
Dan Gohman076aee32009-03-04 19:44:21 +00005747/// Emit nodes that will be selected as "test Op0,Op0", or something
5748/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005749SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5750 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005751 DebugLoc dl = Op.getDebugLoc();
5752
Dan Gohman31125812009-03-07 01:58:32 +00005753 // CF and OF aren't always set the way we want. Determine which
5754 // of these we need.
5755 bool NeedCF = false;
5756 bool NeedOF = false;
5757 switch (X86CC) {
5758 case X86::COND_A: case X86::COND_AE:
5759 case X86::COND_B: case X86::COND_BE:
5760 NeedCF = true;
5761 break;
5762 case X86::COND_G: case X86::COND_GE:
5763 case X86::COND_L: case X86::COND_LE:
5764 case X86::COND_O: case X86::COND_NO:
5765 NeedOF = true;
5766 break;
5767 default: break;
5768 }
5769
Dan Gohman076aee32009-03-04 19:44:21 +00005770 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005771 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5772 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5773 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005774 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005775 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005776 switch (Op.getNode()->getOpcode()) {
5777 case ISD::ADD:
5778 // Due to an isel shortcoming, be conservative if this add is likely to
5779 // be selected as part of a load-modify-store instruction. When the root
5780 // node in a match is a store, isel doesn't know how to remap non-chain
5781 // non-flag uses of other nodes in the match, such as the ADD in this
5782 // case. This leads to the ADD being left around and reselected, with
5783 // the result being two adds in the output.
5784 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5785 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5786 if (UI->getOpcode() == ISD::STORE)
5787 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005788 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005789 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5790 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005791 if (C->getAPIntValue() == 1) {
5792 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005793 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005794 break;
5795 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005796 // An add of negative one (subtract of one) will be selected as a DEC.
5797 if (C->getAPIntValue().isAllOnesValue()) {
5798 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005799 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005800 break;
5801 }
5802 }
Dan Gohman076aee32009-03-04 19:44:21 +00005803 // Otherwise use a regular EFLAGS-setting add.
5804 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005805 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005806 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005807 case ISD::AND: {
5808 // If the primary and result isn't used, don't bother using X86ISD::AND,
5809 // because a TEST instruction will be better.
5810 bool NonFlagUse = false;
5811 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005812 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5813 SDNode *User = *UI;
5814 unsigned UOpNo = UI.getOperandNo();
5815 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5816 // Look pass truncate.
5817 UOpNo = User->use_begin().getOperandNo();
5818 User = *User->use_begin();
5819 }
5820 if (User->getOpcode() != ISD::BRCOND &&
5821 User->getOpcode() != ISD::SETCC &&
5822 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005823 NonFlagUse = true;
5824 break;
5825 }
Evan Cheng17751da2010-01-07 00:54:06 +00005826 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005827 if (!NonFlagUse)
5828 break;
5829 }
5830 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005831 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005832 case ISD::OR:
5833 case ISD::XOR:
5834 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005835 // likely to be selected as part of a load-modify-store instruction.
5836 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5837 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5838 if (UI->getOpcode() == ISD::STORE)
5839 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005840 // Otherwise use a regular EFLAGS-setting instruction.
5841 switch (Op.getNode()->getOpcode()) {
5842 case ISD::SUB: Opcode = X86ISD::SUB; break;
5843 case ISD::OR: Opcode = X86ISD::OR; break;
5844 case ISD::XOR: Opcode = X86ISD::XOR; break;
5845 case ISD::AND: Opcode = X86ISD::AND; break;
5846 default: llvm_unreachable("unexpected operator!");
5847 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005848 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005849 break;
5850 case X86ISD::ADD:
5851 case X86ISD::SUB:
5852 case X86ISD::INC:
5853 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005854 case X86ISD::OR:
5855 case X86ISD::XOR:
5856 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005857 return SDValue(Op.getNode(), 1);
5858 default:
5859 default_case:
5860 break;
5861 }
5862 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005863 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005864 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005865 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005866 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005867 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005868 DAG.ReplaceAllUsesWith(Op, New);
5869 return SDValue(New.getNode(), 1);
5870 }
5871 }
5872
5873 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00005874 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00005875 DAG.getConstant(0, Op.getValueType()));
5876}
5877
5878/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5879/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005880SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5881 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005882 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5883 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005884 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005885
5886 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005887 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00005888}
5889
Evan Chengd40d03e2010-01-06 19:38:29 +00005890/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
5891/// if it's possible.
Evan Cheng2c755ba2010-02-27 07:36:59 +00005892static SDValue LowerToBT(SDValue And, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00005893 DebugLoc dl, SelectionDAG &DAG) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00005894 SDValue Op0 = And.getOperand(0);
5895 SDValue Op1 = And.getOperand(1);
5896 if (Op0.getOpcode() == ISD::TRUNCATE)
5897 Op0 = Op0.getOperand(0);
5898 if (Op1.getOpcode() == ISD::TRUNCATE)
5899 Op1 = Op1.getOperand(0);
5900
Evan Chengd40d03e2010-01-06 19:38:29 +00005901 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00005902 if (Op1.getOpcode() == ISD::SHL) {
5903 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
5904 if (And10C->getZExtValue() == 1) {
5905 LHS = Op0;
5906 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005907 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00005908 } else if (Op0.getOpcode() == ISD::SHL) {
5909 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
5910 if (And00C->getZExtValue() == 1) {
5911 LHS = Op1;
5912 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00005913 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00005914 } else if (Op1.getOpcode() == ISD::Constant) {
5915 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
5916 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00005917 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5918 LHS = AndLHS.getOperand(0);
5919 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005920 }
Evan Chengd40d03e2010-01-06 19:38:29 +00005921 }
Evan Cheng0488db92007-09-25 01:57:46 +00005922
Evan Chengd40d03e2010-01-06 19:38:29 +00005923 if (LHS.getNode()) {
5924 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5925 // instruction. Since the shift amount is in-range-or-undefined, we know
5926 // that doing a bittest on the i16 value is ok. We extend to i32 because
5927 // the encoding for the i16 version is larger than the i32 version.
5928 if (LHS.getValueType() == MVT::i8)
5929 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005930
Evan Chengd40d03e2010-01-06 19:38:29 +00005931 // If the operand types disagree, extend the shift amount to match. Since
5932 // BT ignores high bits (like shifts) we can use anyextend.
5933 if (LHS.getValueType() != RHS.getValueType())
5934 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005935
Evan Chengd40d03e2010-01-06 19:38:29 +00005936 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
5937 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
5938 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5939 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00005940 }
5941
Evan Cheng54de3ea2010-01-05 06:52:31 +00005942 return SDValue();
5943}
5944
5945SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
5946 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
5947 SDValue Op0 = Op.getOperand(0);
5948 SDValue Op1 = Op.getOperand(1);
5949 DebugLoc dl = Op.getDebugLoc();
5950 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
5951
5952 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00005953 // Lower (X & (1 << N)) == 0 to BT(X, N).
5954 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5955 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
5956 if (Op0.getOpcode() == ISD::AND &&
5957 Op0.hasOneUse() &&
5958 Op1.getOpcode() == ISD::Constant &&
5959 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
5960 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5961 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
5962 if (NewSetCC.getNode())
5963 return NewSetCC;
5964 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00005965
Evan Cheng2c755ba2010-02-27 07:36:59 +00005966 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
5967 if (Op0.getOpcode() == X86ISD::SETCC &&
5968 Op1.getOpcode() == ISD::Constant &&
5969 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
5970 cast<ConstantSDNode>(Op1)->isNullValue()) &&
5971 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
5972 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
5973 bool Invert = (CC == ISD::SETNE) ^
5974 cast<ConstantSDNode>(Op1)->isNullValue();
5975 if (Invert)
5976 CCode = X86::GetOppositeBranchCondition(CCode);
5977 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5978 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
5979 }
5980
Chris Lattnere55484e2008-12-25 05:34:37 +00005981 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5982 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00005983 if (X86CC == X86::COND_INVALID)
5984 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00005985
Dan Gohman31125812009-03-07 01:58:32 +00005986 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00005987
5988 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00005989 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00005990 return DAG.getNode(ISD::AND, dl, MVT::i8,
5991 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
5992 DAG.getConstant(X86CC, MVT::i8), Cond),
5993 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00005994
Owen Anderson825b72b2009-08-11 20:47:22 +00005995 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
5996 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005997}
5998
Dan Gohman475871a2008-07-27 21:46:04 +00005999SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
6000 SDValue Cond;
6001 SDValue Op0 = Op.getOperand(0);
6002 SDValue Op1 = Op.getOperand(1);
6003 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006004 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006005 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6006 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006007 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006008
6009 if (isFP) {
6010 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006011 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006012 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6013 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006014 bool Swap = false;
6015
6016 switch (SetCCOpcode) {
6017 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006018 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006019 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006020 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006021 case ISD::SETGT: Swap = true; // Fallthrough
6022 case ISD::SETLT:
6023 case ISD::SETOLT: SSECC = 1; break;
6024 case ISD::SETOGE:
6025 case ISD::SETGE: Swap = true; // Fallthrough
6026 case ISD::SETLE:
6027 case ISD::SETOLE: SSECC = 2; break;
6028 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006029 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006030 case ISD::SETNE: SSECC = 4; break;
6031 case ISD::SETULE: Swap = true;
6032 case ISD::SETUGE: SSECC = 5; break;
6033 case ISD::SETULT: Swap = true;
6034 case ISD::SETUGT: SSECC = 6; break;
6035 case ISD::SETO: SSECC = 7; break;
6036 }
6037 if (Swap)
6038 std::swap(Op0, Op1);
6039
Nate Begemanfb8ead02008-07-25 19:05:58 +00006040 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006041 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006042 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006043 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006044 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6045 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006046 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006047 }
6048 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006049 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006050 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6051 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006052 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006053 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006054 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006055 }
6056 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006057 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006058 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006059
Nate Begeman30a0de92008-07-17 16:51:19 +00006060 // We are handling one of the integer comparisons here. Since SSE only has
6061 // GT and EQ comparisons for integer, swapping operands and multiple
6062 // operations may be required for some comparisons.
6063 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6064 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006065
Owen Anderson825b72b2009-08-11 20:47:22 +00006066 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006067 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006068 case MVT::v8i8:
6069 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6070 case MVT::v4i16:
6071 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6072 case MVT::v2i32:
6073 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6074 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006075 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006076
Nate Begeman30a0de92008-07-17 16:51:19 +00006077 switch (SetCCOpcode) {
6078 default: break;
6079 case ISD::SETNE: Invert = true;
6080 case ISD::SETEQ: Opc = EQOpc; break;
6081 case ISD::SETLT: Swap = true;
6082 case ISD::SETGT: Opc = GTOpc; break;
6083 case ISD::SETGE: Swap = true;
6084 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6085 case ISD::SETULT: Swap = true;
6086 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6087 case ISD::SETUGE: Swap = true;
6088 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6089 }
6090 if (Swap)
6091 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006092
Nate Begeman30a0de92008-07-17 16:51:19 +00006093 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6094 // bits of the inputs before performing those operations.
6095 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006096 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006097 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6098 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006099 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006100 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6101 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006102 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6103 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006104 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006105
Dale Johannesenace16102009-02-03 19:33:06 +00006106 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006107
6108 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006109 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006110 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006111
Nate Begeman30a0de92008-07-17 16:51:19 +00006112 return Result;
6113}
Evan Cheng0488db92007-09-25 01:57:46 +00006114
Evan Cheng370e5342008-12-03 08:38:43 +00006115// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006116static bool isX86LogicalCmp(SDValue Op) {
6117 unsigned Opc = Op.getNode()->getOpcode();
6118 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6119 return true;
6120 if (Op.getResNo() == 1 &&
6121 (Opc == X86ISD::ADD ||
6122 Opc == X86ISD::SUB ||
6123 Opc == X86ISD::SMUL ||
6124 Opc == X86ISD::UMUL ||
6125 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006126 Opc == X86ISD::DEC ||
6127 Opc == X86ISD::OR ||
6128 Opc == X86ISD::XOR ||
6129 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006130 return true;
6131
6132 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006133}
6134
Dan Gohman475871a2008-07-27 21:46:04 +00006135SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006136 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006137 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006138 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006139 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006140
Dan Gohman1a492952009-10-20 16:22:37 +00006141 if (Cond.getOpcode() == ISD::SETCC) {
6142 SDValue NewCond = LowerSETCC(Cond, DAG);
6143 if (NewCond.getNode())
6144 Cond = NewCond;
6145 }
Evan Cheng734503b2006-09-11 02:19:56 +00006146
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006147 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6148 SDValue Op1 = Op.getOperand(1);
6149 SDValue Op2 = Op.getOperand(2);
6150 if (Cond.getOpcode() == X86ISD::SETCC &&
6151 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6152 SDValue Cmp = Cond.getOperand(1);
6153 if (Cmp.getOpcode() == X86ISD::CMP) {
6154 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6155 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6156 ConstantSDNode *RHSC =
6157 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6158 if (N1C && N1C->isAllOnesValue() &&
6159 N2C && N2C->isNullValue() &&
6160 RHSC && RHSC->isNullValue()) {
6161 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006162 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006163 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6164 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6165 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6166 }
6167 }
6168 }
6169
Evan Chengad9c0a32009-12-15 00:53:42 +00006170 // Look pass (and (setcc_carry (cmp ...)), 1).
6171 if (Cond.getOpcode() == ISD::AND &&
6172 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6173 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6174 if (C && C->getAPIntValue() == 1)
6175 Cond = Cond.getOperand(0);
6176 }
6177
Evan Cheng3f41d662007-10-08 22:16:29 +00006178 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6179 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006180 if (Cond.getOpcode() == X86ISD::SETCC ||
6181 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006182 CC = Cond.getOperand(0);
6183
Dan Gohman475871a2008-07-27 21:46:04 +00006184 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006185 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006186 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006187
Evan Cheng3f41d662007-10-08 22:16:29 +00006188 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006189 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006190 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006191 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006192
Chris Lattnerd1980a52009-03-12 06:52:53 +00006193 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6194 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006195 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006196 addTest = false;
6197 }
6198 }
6199
6200 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006201 // Look pass the truncate.
6202 if (Cond.getOpcode() == ISD::TRUNCATE)
6203 Cond = Cond.getOperand(0);
6204
6205 // We know the result of AND is compared against zero. Try to match
6206 // it to BT.
6207 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6208 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6209 if (NewSetCC.getNode()) {
6210 CC = NewSetCC.getOperand(0);
6211 Cond = NewSetCC.getOperand(1);
6212 addTest = false;
6213 }
6214 }
6215 }
6216
6217 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006218 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006219 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006220 }
6221
Evan Cheng0488db92007-09-25 01:57:46 +00006222 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6223 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006224 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6225 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006226 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006227}
6228
Evan Cheng370e5342008-12-03 08:38:43 +00006229// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6230// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6231// from the AND / OR.
6232static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6233 Opc = Op.getOpcode();
6234 if (Opc != ISD::OR && Opc != ISD::AND)
6235 return false;
6236 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6237 Op.getOperand(0).hasOneUse() &&
6238 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6239 Op.getOperand(1).hasOneUse());
6240}
6241
Evan Cheng961d6d42009-02-02 08:19:07 +00006242// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6243// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006244static bool isXor1OfSetCC(SDValue Op) {
6245 if (Op.getOpcode() != ISD::XOR)
6246 return false;
6247 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6248 if (N1C && N1C->getAPIntValue() == 1) {
6249 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6250 Op.getOperand(0).hasOneUse();
6251 }
6252 return false;
6253}
6254
Dan Gohman475871a2008-07-27 21:46:04 +00006255SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006256 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006257 SDValue Chain = Op.getOperand(0);
6258 SDValue Cond = Op.getOperand(1);
6259 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006260 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006261 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006262
Dan Gohman1a492952009-10-20 16:22:37 +00006263 if (Cond.getOpcode() == ISD::SETCC) {
6264 SDValue NewCond = LowerSETCC(Cond, DAG);
6265 if (NewCond.getNode())
6266 Cond = NewCond;
6267 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006268#if 0
6269 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006270 else if (Cond.getOpcode() == X86ISD::ADD ||
6271 Cond.getOpcode() == X86ISD::SUB ||
6272 Cond.getOpcode() == X86ISD::SMUL ||
6273 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006274 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006275#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006276
Evan Chengad9c0a32009-12-15 00:53:42 +00006277 // Look pass (and (setcc_carry (cmp ...)), 1).
6278 if (Cond.getOpcode() == ISD::AND &&
6279 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6280 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6281 if (C && C->getAPIntValue() == 1)
6282 Cond = Cond.getOperand(0);
6283 }
6284
Evan Cheng3f41d662007-10-08 22:16:29 +00006285 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6286 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006287 if (Cond.getOpcode() == X86ISD::SETCC ||
6288 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006289 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006290
Dan Gohman475871a2008-07-27 21:46:04 +00006291 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006292 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006293 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006294 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006295 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006296 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006297 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006298 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006299 default: break;
6300 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006301 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006302 // These can only come from an arithmetic instruction with overflow,
6303 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006304 Cond = Cond.getNode()->getOperand(1);
6305 addTest = false;
6306 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006307 }
Evan Cheng0488db92007-09-25 01:57:46 +00006308 }
Evan Cheng370e5342008-12-03 08:38:43 +00006309 } else {
6310 unsigned CondOpc;
6311 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6312 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006313 if (CondOpc == ISD::OR) {
6314 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6315 // two branches instead of an explicit OR instruction with a
6316 // separate test.
6317 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006318 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006319 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006320 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006321 Chain, Dest, CC, Cmp);
6322 CC = Cond.getOperand(1).getOperand(0);
6323 Cond = Cmp;
6324 addTest = false;
6325 }
6326 } else { // ISD::AND
6327 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6328 // two branches instead of an explicit AND instruction with a
6329 // separate test. However, we only do this if this block doesn't
6330 // have a fall-through edge, because this requires an explicit
6331 // jmp when the condition is false.
6332 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006333 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006334 Op.getNode()->hasOneUse()) {
6335 X86::CondCode CCode =
6336 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6337 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006338 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006339 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6340 // Look for an unconditional branch following this conditional branch.
6341 // We need this because we need to reverse the successors in order
6342 // to implement FCMP_OEQ.
6343 if (User.getOpcode() == ISD::BR) {
6344 SDValue FalseBB = User.getOperand(1);
6345 SDValue NewBR =
6346 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6347 assert(NewBR == User);
6348 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006349
Dale Johannesene4d209d2009-02-03 20:21:25 +00006350 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006351 Chain, Dest, CC, Cmp);
6352 X86::CondCode CCode =
6353 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6354 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006355 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006356 Cond = Cmp;
6357 addTest = false;
6358 }
6359 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006360 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006361 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6362 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6363 // It should be transformed during dag combiner except when the condition
6364 // is set by a arithmetics with overflow node.
6365 X86::CondCode CCode =
6366 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6367 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006368 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006369 Cond = Cond.getOperand(0).getOperand(1);
6370 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006371 }
Evan Cheng0488db92007-09-25 01:57:46 +00006372 }
6373
6374 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006375 // Look pass the truncate.
6376 if (Cond.getOpcode() == ISD::TRUNCATE)
6377 Cond = Cond.getOperand(0);
6378
6379 // We know the result of AND is compared against zero. Try to match
6380 // it to BT.
6381 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6382 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6383 if (NewSetCC.getNode()) {
6384 CC = NewSetCC.getOperand(0);
6385 Cond = NewSetCC.getOperand(1);
6386 addTest = false;
6387 }
6388 }
6389 }
6390
6391 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006392 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006393 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006394 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006395 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006396 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006397}
6398
Anton Korobeynikove060b532007-04-17 19:34:00 +00006399
6400// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6401// Calls to _alloca is needed to probe the stack when allocating more than 4k
6402// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6403// that the guard pages used by the OS virtual memory manager are allocated in
6404// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006405SDValue
6406X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006407 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006408 assert(Subtarget->isTargetCygMing() &&
6409 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006410 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006411
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006412 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006413 SDValue Chain = Op.getOperand(0);
6414 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006415 // FIXME: Ensure alignment here
6416
Dan Gohman475871a2008-07-27 21:46:04 +00006417 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006418
Owen Andersone50ed302009-08-10 22:56:29 +00006419 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006420 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006421
Dale Johannesendd64c412009-02-04 00:33:20 +00006422 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006423 Flag = Chain.getValue(1);
6424
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006425 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006426
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006427 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6428 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006429
Dale Johannesendd64c412009-02-04 00:33:20 +00006430 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006431
Dan Gohman475871a2008-07-27 21:46:04 +00006432 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006433 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006434}
6435
Dan Gohman475871a2008-07-27 21:46:04 +00006436SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006437X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006438 SDValue Chain,
6439 SDValue Dst, SDValue Src,
6440 SDValue Size, unsigned Align,
6441 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006442 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006443 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006444
Bill Wendling6f287b22008-09-30 21:22:07 +00006445 // If not DWORD aligned or size is more than the threshold, call the library.
6446 // The libc version is likely to be faster for these cases. It can use the
6447 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006448 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006449 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006450 ConstantSize->getZExtValue() >
6451 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006452 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006453
6454 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006455 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006456
Bill Wendling6158d842008-10-01 00:59:58 +00006457 if (const char *bzeroEntry = V &&
6458 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006459 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006460 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006461 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006462 TargetLowering::ArgListEntry Entry;
6463 Entry.Node = Dst;
6464 Entry.Ty = IntPtrTy;
6465 Args.push_back(Entry);
6466 Entry.Node = Size;
6467 Args.push_back(Entry);
6468 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006469 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6470 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006471 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling46ada192010-03-02 01:55:18 +00006472 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00006473 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006474 }
6475
Dan Gohman707e0182008-04-12 04:36:06 +00006476 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006477 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006478 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006479
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006480 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006481 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006482 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006483 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006484 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006485 unsigned BytesLeft = 0;
6486 bool TwoRepStos = false;
6487 if (ValC) {
6488 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006489 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006490
Evan Cheng0db9fe62006-04-25 20:13:52 +00006491 // If the value is a constant, then we can potentially use larger sets.
6492 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006493 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006494 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006495 ValReg = X86::AX;
6496 Val = (Val << 8) | Val;
6497 break;
6498 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006499 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006500 ValReg = X86::EAX;
6501 Val = (Val << 8) | Val;
6502 Val = (Val << 16) | Val;
6503 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006504 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006505 ValReg = X86::RAX;
6506 Val = (Val << 32) | Val;
6507 }
6508 break;
6509 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006510 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006511 ValReg = X86::AL;
6512 Count = DAG.getIntPtrConstant(SizeVal);
6513 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006514 }
6515
Owen Anderson825b72b2009-08-11 20:47:22 +00006516 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006517 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006518 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6519 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006520 }
6521
Dale Johannesen0f502f62009-02-03 22:26:09 +00006522 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006523 InFlag);
6524 InFlag = Chain.getValue(1);
6525 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006526 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006527 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006528 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006529 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006530 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006531
Scott Michelfdc40a02009-02-17 22:15:04 +00006532 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006533 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006534 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006535 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006536 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006537 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006538 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006539 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006540
Owen Anderson825b72b2009-08-11 20:47:22 +00006541 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006542 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6543 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006544
Evan Cheng0db9fe62006-04-25 20:13:52 +00006545 if (TwoRepStos) {
6546 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006547 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006548 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006549 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006550 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6551 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006552 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006553 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006554 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006555 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006556 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6557 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006558 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006559 // Handle the last 1 - 7 bytes.
6560 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006561 EVT AddrVT = Dst.getValueType();
6562 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006563
Dale Johannesen0f502f62009-02-03 22:26:09 +00006564 Chain = DAG.getMemset(Chain, dl,
6565 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006566 DAG.getConstant(Offset, AddrVT)),
6567 Src,
6568 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00006569 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006570 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006571
Dan Gohman707e0182008-04-12 04:36:06 +00006572 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006573 return Chain;
6574}
Evan Cheng11e15b32006-04-03 20:53:28 +00006575
Dan Gohman475871a2008-07-27 21:46:04 +00006576SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006577X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006578 SDValue Chain, SDValue Dst, SDValue Src,
6579 SDValue Size, unsigned Align,
6580 bool AlwaysInline,
6581 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006582 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006583 // This requires the copy size to be a constant, preferrably
6584 // within a subtarget-specific limit.
6585 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6586 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006587 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006588 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006589 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006590 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006591
Evan Cheng1887c1c2008-08-21 21:00:15 +00006592 /// If not DWORD aligned, call the library.
6593 if ((Align & 3) != 0)
6594 return SDValue();
6595
6596 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006597 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006598 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006599 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006600
Duncan Sands83ec4b62008-06-06 12:08:01 +00006601 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006602 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006603 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006604 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006605
Dan Gohman475871a2008-07-27 21:46:04 +00006606 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006607 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006608 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006609 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006610 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006611 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006612 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006613 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006614 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006615 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006616 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006617 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006618 InFlag = Chain.getValue(1);
6619
Owen Anderson825b72b2009-08-11 20:47:22 +00006620 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006621 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6622 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6623 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006624
Dan Gohman475871a2008-07-27 21:46:04 +00006625 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006626 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006627 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006628 // Handle the last 1 - 7 bytes.
6629 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006630 EVT DstVT = Dst.getValueType();
6631 EVT SrcVT = Src.getValueType();
6632 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006633 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006634 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006635 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006636 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006637 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006638 DAG.getConstant(BytesLeft, SizeVT),
6639 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006640 DstSV, DstSVOff + Offset,
6641 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006642 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006643
Owen Anderson825b72b2009-08-11 20:47:22 +00006644 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006645 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006646}
6647
Dan Gohman475871a2008-07-27 21:46:04 +00006648SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00006649 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006650 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006651
Evan Cheng25ab6902006-09-08 06:48:29 +00006652 if (!Subtarget->is64Bit()) {
6653 // vastart just stores the address of the VarArgsFrameIndex slot into the
6654 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00006655 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006656 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6657 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006658 }
6659
6660 // __va_list_tag:
6661 // gp_offset (0 - 6 * 8)
6662 // fp_offset (48 - 48 + 8 * 16)
6663 // overflow_arg_area (point to parameters coming in memory).
6664 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006665 SmallVector<SDValue, 8> MemOps;
6666 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006667 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006668 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
David Greene67c9d422010-02-15 16:53:33 +00006669 DAG.getConstant(VarArgsGPOffset, MVT::i32),
6670 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006671 MemOps.push_back(Store);
6672
6673 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006674 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006675 FIN, DAG.getIntPtrConstant(4));
6676 Store = DAG.getStore(Op.getOperand(0), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006677 DAG.getConstant(VarArgsFPOffset, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006678 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006679 MemOps.push_back(Store);
6680
6681 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006682 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006683 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006684 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006685 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6686 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006687 MemOps.push_back(Store);
6688
6689 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006690 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006691 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006692 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006693 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6694 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006695 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006696 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006697 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006698}
6699
Dan Gohman475871a2008-07-27 21:46:04 +00006700SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006701 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6702 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006703 SDValue Chain = Op.getOperand(0);
6704 SDValue SrcPtr = Op.getOperand(1);
6705 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006706
Torok Edwindac237e2009-07-08 20:53:28 +00006707 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006708 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006709}
6710
Dan Gohman475871a2008-07-27 21:46:04 +00006711SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006712 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006713 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006714 SDValue Chain = Op.getOperand(0);
6715 SDValue DstPtr = Op.getOperand(1);
6716 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006717 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6718 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006719 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006720
Dale Johannesendd64c412009-02-04 00:33:20 +00006721 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006722 DAG.getIntPtrConstant(24), 8, false,
6723 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006724}
6725
Dan Gohman475871a2008-07-27 21:46:04 +00006726SDValue
6727X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006728 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006729 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006730 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006731 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006732 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006733 case Intrinsic::x86_sse_comieq_ss:
6734 case Intrinsic::x86_sse_comilt_ss:
6735 case Intrinsic::x86_sse_comile_ss:
6736 case Intrinsic::x86_sse_comigt_ss:
6737 case Intrinsic::x86_sse_comige_ss:
6738 case Intrinsic::x86_sse_comineq_ss:
6739 case Intrinsic::x86_sse_ucomieq_ss:
6740 case Intrinsic::x86_sse_ucomilt_ss:
6741 case Intrinsic::x86_sse_ucomile_ss:
6742 case Intrinsic::x86_sse_ucomigt_ss:
6743 case Intrinsic::x86_sse_ucomige_ss:
6744 case Intrinsic::x86_sse_ucomineq_ss:
6745 case Intrinsic::x86_sse2_comieq_sd:
6746 case Intrinsic::x86_sse2_comilt_sd:
6747 case Intrinsic::x86_sse2_comile_sd:
6748 case Intrinsic::x86_sse2_comigt_sd:
6749 case Intrinsic::x86_sse2_comige_sd:
6750 case Intrinsic::x86_sse2_comineq_sd:
6751 case Intrinsic::x86_sse2_ucomieq_sd:
6752 case Intrinsic::x86_sse2_ucomilt_sd:
6753 case Intrinsic::x86_sse2_ucomile_sd:
6754 case Intrinsic::x86_sse2_ucomigt_sd:
6755 case Intrinsic::x86_sse2_ucomige_sd:
6756 case Intrinsic::x86_sse2_ucomineq_sd: {
6757 unsigned Opc = 0;
6758 ISD::CondCode CC = ISD::SETCC_INVALID;
6759 switch (IntNo) {
6760 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006761 case Intrinsic::x86_sse_comieq_ss:
6762 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006763 Opc = X86ISD::COMI;
6764 CC = ISD::SETEQ;
6765 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006766 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006767 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006768 Opc = X86ISD::COMI;
6769 CC = ISD::SETLT;
6770 break;
6771 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006772 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006773 Opc = X86ISD::COMI;
6774 CC = ISD::SETLE;
6775 break;
6776 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006777 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006778 Opc = X86ISD::COMI;
6779 CC = ISD::SETGT;
6780 break;
6781 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006782 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006783 Opc = X86ISD::COMI;
6784 CC = ISD::SETGE;
6785 break;
6786 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006787 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006788 Opc = X86ISD::COMI;
6789 CC = ISD::SETNE;
6790 break;
6791 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006792 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006793 Opc = X86ISD::UCOMI;
6794 CC = ISD::SETEQ;
6795 break;
6796 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006797 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006798 Opc = X86ISD::UCOMI;
6799 CC = ISD::SETLT;
6800 break;
6801 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006802 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006803 Opc = X86ISD::UCOMI;
6804 CC = ISD::SETLE;
6805 break;
6806 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006807 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006808 Opc = X86ISD::UCOMI;
6809 CC = ISD::SETGT;
6810 break;
6811 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006812 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006813 Opc = X86ISD::UCOMI;
6814 CC = ISD::SETGE;
6815 break;
6816 case Intrinsic::x86_sse_ucomineq_ss:
6817 case Intrinsic::x86_sse2_ucomineq_sd:
6818 Opc = X86ISD::UCOMI;
6819 CC = ISD::SETNE;
6820 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006821 }
Evan Cheng734503b2006-09-11 02:19:56 +00006822
Dan Gohman475871a2008-07-27 21:46:04 +00006823 SDValue LHS = Op.getOperand(1);
6824 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006825 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006826 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006827 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6828 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6829 DAG.getConstant(X86CC, MVT::i8), Cond);
6830 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006831 }
Eric Christopher71c67532009-07-29 00:28:05 +00006832 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006833 // an integer value, not just an instruction so lower it to the ptest
6834 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006835 case Intrinsic::x86_sse41_ptestz:
6836 case Intrinsic::x86_sse41_ptestc:
6837 case Intrinsic::x86_sse41_ptestnzc:{
6838 unsigned X86CC = 0;
6839 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006840 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006841 case Intrinsic::x86_sse41_ptestz:
6842 // ZF = 1
6843 X86CC = X86::COND_E;
6844 break;
6845 case Intrinsic::x86_sse41_ptestc:
6846 // CF = 1
6847 X86CC = X86::COND_B;
6848 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006849 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006850 // ZF and CF = 0
6851 X86CC = X86::COND_A;
6852 break;
6853 }
Eric Christopherfd179292009-08-27 18:07:15 +00006854
Eric Christopher71c67532009-07-29 00:28:05 +00006855 SDValue LHS = Op.getOperand(1);
6856 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006857 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6858 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6859 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6860 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006861 }
Evan Cheng5759f972008-05-04 09:15:50 +00006862
6863 // Fix vector shift instructions where the last operand is a non-immediate
6864 // i32 value.
6865 case Intrinsic::x86_sse2_pslli_w:
6866 case Intrinsic::x86_sse2_pslli_d:
6867 case Intrinsic::x86_sse2_pslli_q:
6868 case Intrinsic::x86_sse2_psrli_w:
6869 case Intrinsic::x86_sse2_psrli_d:
6870 case Intrinsic::x86_sse2_psrli_q:
6871 case Intrinsic::x86_sse2_psrai_w:
6872 case Intrinsic::x86_sse2_psrai_d:
6873 case Intrinsic::x86_mmx_pslli_w:
6874 case Intrinsic::x86_mmx_pslli_d:
6875 case Intrinsic::x86_mmx_pslli_q:
6876 case Intrinsic::x86_mmx_psrli_w:
6877 case Intrinsic::x86_mmx_psrli_d:
6878 case Intrinsic::x86_mmx_psrli_q:
6879 case Intrinsic::x86_mmx_psrai_w:
6880 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006881 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006882 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006883 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006884
6885 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006886 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006887 switch (IntNo) {
6888 case Intrinsic::x86_sse2_pslli_w:
6889 NewIntNo = Intrinsic::x86_sse2_psll_w;
6890 break;
6891 case Intrinsic::x86_sse2_pslli_d:
6892 NewIntNo = Intrinsic::x86_sse2_psll_d;
6893 break;
6894 case Intrinsic::x86_sse2_pslli_q:
6895 NewIntNo = Intrinsic::x86_sse2_psll_q;
6896 break;
6897 case Intrinsic::x86_sse2_psrli_w:
6898 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6899 break;
6900 case Intrinsic::x86_sse2_psrli_d:
6901 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6902 break;
6903 case Intrinsic::x86_sse2_psrli_q:
6904 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6905 break;
6906 case Intrinsic::x86_sse2_psrai_w:
6907 NewIntNo = Intrinsic::x86_sse2_psra_w;
6908 break;
6909 case Intrinsic::x86_sse2_psrai_d:
6910 NewIntNo = Intrinsic::x86_sse2_psra_d;
6911 break;
6912 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006913 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006914 switch (IntNo) {
6915 case Intrinsic::x86_mmx_pslli_w:
6916 NewIntNo = Intrinsic::x86_mmx_psll_w;
6917 break;
6918 case Intrinsic::x86_mmx_pslli_d:
6919 NewIntNo = Intrinsic::x86_mmx_psll_d;
6920 break;
6921 case Intrinsic::x86_mmx_pslli_q:
6922 NewIntNo = Intrinsic::x86_mmx_psll_q;
6923 break;
6924 case Intrinsic::x86_mmx_psrli_w:
6925 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6926 break;
6927 case Intrinsic::x86_mmx_psrli_d:
6928 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6929 break;
6930 case Intrinsic::x86_mmx_psrli_q:
6931 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6932 break;
6933 case Intrinsic::x86_mmx_psrai_w:
6934 NewIntNo = Intrinsic::x86_mmx_psra_w;
6935 break;
6936 case Intrinsic::x86_mmx_psrai_d:
6937 NewIntNo = Intrinsic::x86_mmx_psra_d;
6938 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006939 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006940 }
6941 break;
6942 }
6943 }
Mon P Wangefa42202009-09-03 19:56:25 +00006944
6945 // The vector shift intrinsics with scalars uses 32b shift amounts but
6946 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6947 // to be zero.
6948 SDValue ShOps[4];
6949 ShOps[0] = ShAmt;
6950 ShOps[1] = DAG.getConstant(0, MVT::i32);
6951 if (ShAmtVT == MVT::v4i32) {
6952 ShOps[2] = DAG.getUNDEF(MVT::i32);
6953 ShOps[3] = DAG.getUNDEF(MVT::i32);
6954 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6955 } else {
6956 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6957 }
6958
Owen Andersone50ed302009-08-10 22:56:29 +00006959 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006960 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006961 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006962 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006963 Op.getOperand(1), ShAmt);
6964 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006965 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006966}
Evan Cheng72261582005-12-20 06:22:03 +00006967
Dan Gohman475871a2008-07-27 21:46:04 +00006968SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006969 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006970 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006971
6972 if (Depth > 0) {
6973 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6974 SDValue Offset =
6975 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006976 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006977 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006978 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006979 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00006980 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00006981 }
6982
6983 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006984 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006985 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00006986 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006987}
6988
Dan Gohman475871a2008-07-27 21:46:04 +00006989SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006990 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6991 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00006992 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006993 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006994 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6995 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006996 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006997 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00006998 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
6999 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007000 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007001}
7002
Dan Gohman475871a2008-07-27 21:46:04 +00007003SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00007004 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007005 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007006}
7007
Dan Gohman475871a2008-07-27 21:46:04 +00007008SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007009{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007010 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007011 SDValue Chain = Op.getOperand(0);
7012 SDValue Offset = Op.getOperand(1);
7013 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007014 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007015
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007016 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7017 getPointerTy());
7018 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007019
Dale Johannesene4d209d2009-02-03 20:21:25 +00007020 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007021 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007022 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007023 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007024 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007025 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007026
Dale Johannesene4d209d2009-02-03 20:21:25 +00007027 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007028 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007029 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007030}
7031
Dan Gohman475871a2008-07-27 21:46:04 +00007032SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00007033 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00007034 SDValue Root = Op.getOperand(0);
7035 SDValue Trmp = Op.getOperand(1); // trampoline
7036 SDValue FPtr = Op.getOperand(2); // nested function
7037 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007038 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007039
Dan Gohman69de1932008-02-06 22:27:42 +00007040 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007041
7042 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007043 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007044
7045 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007046 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7047 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007048
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007049 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7050 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007051
7052 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7053
7054 // Load the pointer to the nested function into R11.
7055 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007056 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007057 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007058 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007059
Owen Anderson825b72b2009-08-11 20:47:22 +00007060 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7061 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007062 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7063 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007064
7065 // Load the 'nest' parameter value into R10.
7066 // R10 is specified in X86CallingConv.td
7067 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007068 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7069 DAG.getConstant(10, MVT::i64));
7070 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007071 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007072
Owen Anderson825b72b2009-08-11 20:47:22 +00007073 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7074 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007075 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7076 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007077
7078 // Jump to the nested function.
7079 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007080 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7081 DAG.getConstant(20, MVT::i64));
7082 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007083 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007084
7085 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007086 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7087 DAG.getConstant(22, MVT::i64));
7088 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007089 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007090
Dan Gohman475871a2008-07-27 21:46:04 +00007091 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007092 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007093 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007094 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007095 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007096 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007097 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007098 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007099
7100 switch (CC) {
7101 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007102 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007103 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007104 case CallingConv::X86_StdCall: {
7105 // Pass 'nest' parameter in ECX.
7106 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007107 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007108
7109 // Check that ECX wasn't needed by an 'inreg' parameter.
7110 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007111 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007112
Chris Lattner58d74912008-03-12 17:45:29 +00007113 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007114 unsigned InRegCount = 0;
7115 unsigned Idx = 1;
7116
7117 for (FunctionType::param_iterator I = FTy->param_begin(),
7118 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007119 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007120 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007121 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007122
7123 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00007124 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007125 }
7126 }
7127 break;
7128 }
7129 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007130 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007131 // Pass 'nest' parameter in EAX.
7132 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007133 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007134 break;
7135 }
7136
Dan Gohman475871a2008-07-27 21:46:04 +00007137 SDValue OutChains[4];
7138 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007139
Owen Anderson825b72b2009-08-11 20:47:22 +00007140 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7141 DAG.getConstant(10, MVT::i32));
7142 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007143
Chris Lattnera62fe662010-02-05 19:20:30 +00007144 // This is storing the opcode for MOV32ri.
7145 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007146 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007147 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007148 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007149 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007150
Owen Anderson825b72b2009-08-11 20:47:22 +00007151 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7152 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007153 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7154 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007155
Chris Lattnera62fe662010-02-05 19:20:30 +00007156 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007157 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7158 DAG.getConstant(5, MVT::i32));
7159 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007160 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007161
Owen Anderson825b72b2009-08-11 20:47:22 +00007162 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7163 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007164 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7165 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007166
Dan Gohman475871a2008-07-27 21:46:04 +00007167 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007168 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007169 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007170 }
7171}
7172
Dan Gohman475871a2008-07-27 21:46:04 +00007173SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007174 /*
7175 The rounding mode is in bits 11:10 of FPSR, and has the following
7176 settings:
7177 00 Round to nearest
7178 01 Round to -inf
7179 10 Round to +inf
7180 11 Round to 0
7181
7182 FLT_ROUNDS, on the other hand, expects the following:
7183 -1 Undefined
7184 0 Round to 0
7185 1 Round to nearest
7186 2 Round to +inf
7187 3 Round to -inf
7188
7189 To perform the conversion, we do:
7190 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7191 */
7192
7193 MachineFunction &MF = DAG.getMachineFunction();
7194 const TargetMachine &TM = MF.getTarget();
7195 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7196 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007197 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007198 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007199
7200 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007201 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007202 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007203
Owen Anderson825b72b2009-08-11 20:47:22 +00007204 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007205 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007206
7207 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007208 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7209 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007210
7211 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007212 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007213 DAG.getNode(ISD::SRL, dl, MVT::i16,
7214 DAG.getNode(ISD::AND, dl, MVT::i16,
7215 CWD, DAG.getConstant(0x800, MVT::i16)),
7216 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007217 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007218 DAG.getNode(ISD::SRL, dl, MVT::i16,
7219 DAG.getNode(ISD::AND, dl, MVT::i16,
7220 CWD, DAG.getConstant(0x400, MVT::i16)),
7221 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007222
Dan Gohman475871a2008-07-27 21:46:04 +00007223 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007224 DAG.getNode(ISD::AND, dl, MVT::i16,
7225 DAG.getNode(ISD::ADD, dl, MVT::i16,
7226 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7227 DAG.getConstant(1, MVT::i16)),
7228 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007229
7230
Duncan Sands83ec4b62008-06-06 12:08:01 +00007231 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007232 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007233}
7234
Dan Gohman475871a2008-07-27 21:46:04 +00007235SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007236 EVT VT = Op.getValueType();
7237 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007238 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007239 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007240
7241 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007242 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007243 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007244 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007245 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007246 }
Evan Cheng18efe262007-12-14 02:13:44 +00007247
Evan Cheng152804e2007-12-14 08:30:15 +00007248 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007249 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007250 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007251
7252 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007253 SDValue Ops[] = {
7254 Op,
7255 DAG.getConstant(NumBits+NumBits-1, OpVT),
7256 DAG.getConstant(X86::COND_E, MVT::i8),
7257 Op.getValue(1)
7258 };
7259 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007260
7261 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007262 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007263
Owen Anderson825b72b2009-08-11 20:47:22 +00007264 if (VT == MVT::i8)
7265 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007266 return Op;
7267}
7268
Dan Gohman475871a2008-07-27 21:46:04 +00007269SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007270 EVT VT = Op.getValueType();
7271 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007272 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007273 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007274
7275 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007276 if (VT == MVT::i8) {
7277 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007278 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007279 }
Evan Cheng152804e2007-12-14 08:30:15 +00007280
7281 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007282 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007283 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007284
7285 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007286 SDValue Ops[] = {
7287 Op,
7288 DAG.getConstant(NumBits, OpVT),
7289 DAG.getConstant(X86::COND_E, MVT::i8),
7290 Op.getValue(1)
7291 };
7292 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007293
Owen Anderson825b72b2009-08-11 20:47:22 +00007294 if (VT == MVT::i8)
7295 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007296 return Op;
7297}
7298
Mon P Wangaf9b9522008-12-18 21:42:19 +00007299SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007300 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007301 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007302 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007303
Mon P Wangaf9b9522008-12-18 21:42:19 +00007304 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7305 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7306 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7307 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7308 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7309 //
7310 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7311 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7312 // return AloBlo + AloBhi + AhiBlo;
7313
7314 SDValue A = Op.getOperand(0);
7315 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007316
Dale Johannesene4d209d2009-02-03 20:21:25 +00007317 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007318 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7319 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007320 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007321 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7322 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007323 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007324 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007325 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007326 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007327 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007328 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007329 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007330 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007331 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007332 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007333 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7334 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007335 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007336 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7337 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007338 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7339 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007340 return Res;
7341}
7342
7343
Bill Wendling74c37652008-12-09 22:08:41 +00007344SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7345 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7346 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007347 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7348 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007349 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007350 SDValue LHS = N->getOperand(0);
7351 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007352 unsigned BaseOp = 0;
7353 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007354 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007355
7356 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007357 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007358 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007359 // A subtract of one will be selected as a INC. Note that INC doesn't
7360 // set CF, so we can't do this for UADDO.
7361 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7362 if (C->getAPIntValue() == 1) {
7363 BaseOp = X86ISD::INC;
7364 Cond = X86::COND_O;
7365 break;
7366 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007367 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007368 Cond = X86::COND_O;
7369 break;
7370 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007371 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007372 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007373 break;
7374 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007375 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7376 // set CF, so we can't do this for USUBO.
7377 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7378 if (C->getAPIntValue() == 1) {
7379 BaseOp = X86ISD::DEC;
7380 Cond = X86::COND_O;
7381 break;
7382 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007383 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007384 Cond = X86::COND_O;
7385 break;
7386 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007387 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007388 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007389 break;
7390 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007391 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007392 Cond = X86::COND_O;
7393 break;
7394 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007395 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007396 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007397 break;
7398 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007399
Bill Wendling61edeb52008-12-02 01:06:39 +00007400 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007401 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007402 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007403
Bill Wendling61edeb52008-12-02 01:06:39 +00007404 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007405 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007406 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007407
Bill Wendling61edeb52008-12-02 01:06:39 +00007408 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7409 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007410}
7411
Dan Gohman475871a2008-07-27 21:46:04 +00007412SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007413 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007414 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007415 unsigned Reg = 0;
7416 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007417 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007418 default:
7419 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007420 case MVT::i8: Reg = X86::AL; size = 1; break;
7421 case MVT::i16: Reg = X86::AX; size = 2; break;
7422 case MVT::i32: Reg = X86::EAX; size = 4; break;
7423 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007424 assert(Subtarget->is64Bit() && "Node not type legal!");
7425 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007426 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007427 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007428 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007429 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007430 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007431 Op.getOperand(1),
7432 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007433 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007434 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007435 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007436 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007437 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007438 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007439 return cpOut;
7440}
7441
Duncan Sands1607f052008-12-01 11:39:25 +00007442SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007443 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007444 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007445 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007446 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007447 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007448 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007449 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7450 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007451 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007452 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7453 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007454 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007455 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007456 rdx.getValue(1)
7457 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007458 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007459}
7460
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007461SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7462 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007463 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007464 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007465 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007466 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007467 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007468 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007469 Node->getOperand(0),
7470 Node->getOperand(1), negOp,
7471 cast<AtomicSDNode>(Node)->getSrcValue(),
7472 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007473}
7474
Evan Cheng0db9fe62006-04-25 20:13:52 +00007475/// LowerOperation - Provide custom lowering hooks for some operations.
7476///
Dan Gohman475871a2008-07-27 21:46:04 +00007477SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007478 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007479 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007480 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7481 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007482 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007483 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007484 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7485 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7486 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7487 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7488 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7489 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007490 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007491 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007492 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007493 case ISD::SHL_PARTS:
7494 case ISD::SRA_PARTS:
7495 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7496 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007497 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007498 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007499 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007500 case ISD::FABS: return LowerFABS(Op, DAG);
7501 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007502 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007503 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007504 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007505 case ISD::SELECT: return LowerSELECT(Op, DAG);
7506 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007507 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007508 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007509 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007510 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007511 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007512 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7513 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007514 case ISD::FRAME_TO_ARGS_OFFSET:
7515 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007516 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007517 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007518 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007519 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007520 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7521 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007522 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007523 case ISD::SADDO:
7524 case ISD::UADDO:
7525 case ISD::SSUBO:
7526 case ISD::USUBO:
7527 case ISD::SMULO:
7528 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007529 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007530 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007531}
7532
Duncan Sands1607f052008-12-01 11:39:25 +00007533void X86TargetLowering::
7534ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7535 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007536 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007537 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007538 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007539
7540 SDValue Chain = Node->getOperand(0);
7541 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007542 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007543 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007544 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007545 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007546 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007547 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007548 SDValue Result =
7549 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7550 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007551 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007552 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007553 Results.push_back(Result.getValue(2));
7554}
7555
Duncan Sands126d9072008-07-04 11:47:58 +00007556/// ReplaceNodeResults - Replace a node with an illegal result type
7557/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007558void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7559 SmallVectorImpl<SDValue>&Results,
7560 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007561 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007562 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007563 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007564 assert(false && "Do not know how to custom type legalize this operation!");
7565 return;
7566 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007567 std::pair<SDValue,SDValue> Vals =
7568 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007569 SDValue FIST = Vals.first, StackSlot = Vals.second;
7570 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007571 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007572 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007573 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7574 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007575 }
7576 return;
7577 }
7578 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007579 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007580 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007581 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007582 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007583 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007584 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007585 eax.getValue(2));
7586 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7587 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007588 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007589 Results.push_back(edx.getValue(1));
7590 return;
7591 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007592 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007593 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007594 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007595 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007596 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7597 DAG.getConstant(0, MVT::i32));
7598 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7599 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007600 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7601 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007602 cpInL.getValue(1));
7603 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007604 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7605 DAG.getConstant(0, MVT::i32));
7606 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7607 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007608 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007609 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007610 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007611 swapInL.getValue(1));
7612 SDValue Ops[] = { swapInH.getValue(0),
7613 N->getOperand(1),
7614 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007615 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007616 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007617 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007618 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007619 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007620 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007621 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007622 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007623 Results.push_back(cpOutH.getValue(1));
7624 return;
7625 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007626 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007627 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7628 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007629 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007630 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7631 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007632 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007633 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7634 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007635 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007636 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7637 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007638 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007639 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7640 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007641 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007642 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7643 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007644 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007645 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7646 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007647 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007648}
7649
Evan Cheng72261582005-12-20 06:22:03 +00007650const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7651 switch (Opcode) {
7652 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007653 case X86ISD::BSF: return "X86ISD::BSF";
7654 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007655 case X86ISD::SHLD: return "X86ISD::SHLD";
7656 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007657 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007658 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007659 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007660 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007661 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007662 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007663 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7664 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7665 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007666 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007667 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007668 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007669 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007670 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007671 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007672 case X86ISD::COMI: return "X86ISD::COMI";
7673 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007674 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007675 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007676 case X86ISD::CMOV: return "X86ISD::CMOV";
7677 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007678 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007679 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7680 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007681 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007682 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007683 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007684 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007685 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007686 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7687 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007688 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007689 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007690 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007691 case X86ISD::FMAX: return "X86ISD::FMAX";
7692 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007693 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7694 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007695 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007696 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007697 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007698 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007699 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007700 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7701 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007702 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7703 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7704 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7705 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7706 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7707 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007708 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7709 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007710 case X86ISD::VSHL: return "X86ISD::VSHL";
7711 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007712 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7713 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7714 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7715 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7716 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7717 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7718 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7719 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7720 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7721 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007722 case X86ISD::ADD: return "X86ISD::ADD";
7723 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007724 case X86ISD::SMUL: return "X86ISD::SMUL";
7725 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007726 case X86ISD::INC: return "X86ISD::INC";
7727 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007728 case X86ISD::OR: return "X86ISD::OR";
7729 case X86ISD::XOR: return "X86ISD::XOR";
7730 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007731 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007732 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007733 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007734 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007735 }
7736}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007737
Chris Lattnerc9addb72007-03-30 23:15:24 +00007738// isLegalAddressingMode - Return true if the addressing mode represented
7739// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007740bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007741 const Type *Ty) const {
7742 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007743 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007744
Chris Lattnerc9addb72007-03-30 23:15:24 +00007745 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007746 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007747 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007748
Chris Lattnerc9addb72007-03-30 23:15:24 +00007749 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007750 unsigned GVFlags =
7751 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007752
Chris Lattnerdfed4132009-07-10 07:38:24 +00007753 // If a reference to this global requires an extra load, we can't fold it.
7754 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007755 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007756
Chris Lattnerdfed4132009-07-10 07:38:24 +00007757 // If BaseGV requires a register for the PIC base, we cannot also have a
7758 // BaseReg specified.
7759 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007760 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007761
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007762 // If lower 4G is not available, then we must use rip-relative addressing.
7763 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7764 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007765 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007766
Chris Lattnerc9addb72007-03-30 23:15:24 +00007767 switch (AM.Scale) {
7768 case 0:
7769 case 1:
7770 case 2:
7771 case 4:
7772 case 8:
7773 // These scales always work.
7774 break;
7775 case 3:
7776 case 5:
7777 case 9:
7778 // These scales are formed with basereg+scalereg. Only accept if there is
7779 // no basereg yet.
7780 if (AM.HasBaseReg)
7781 return false;
7782 break;
7783 default: // Other stuff never works.
7784 return false;
7785 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007786
Chris Lattnerc9addb72007-03-30 23:15:24 +00007787 return true;
7788}
7789
7790
Evan Cheng2bd122c2007-10-26 01:56:11 +00007791bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007792 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007793 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007794 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7795 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007796 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007797 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007798 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007799}
7800
Owen Andersone50ed302009-08-10 22:56:29 +00007801bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007802 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007803 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007804 unsigned NumBits1 = VT1.getSizeInBits();
7805 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007806 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007807 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007808 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007809}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007810
Dan Gohman97121ba2009-04-08 00:15:30 +00007811bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007812 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007813 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007814}
7815
Owen Andersone50ed302009-08-10 22:56:29 +00007816bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007817 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007818 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007819}
7820
Owen Andersone50ed302009-08-10 22:56:29 +00007821bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007822 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007823 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007824}
7825
Evan Cheng60c07e12006-07-05 22:17:51 +00007826/// isShuffleMaskLegal - Targets can use this to indicate that they only
7827/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7828/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7829/// are assumed to be legal.
7830bool
Eric Christopherfd179292009-08-27 18:07:15 +00007831X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007832 EVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007833 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007834 if (VT.getSizeInBits() == 64)
7835 return false;
7836
Nate Begemana09008b2009-10-19 02:17:23 +00007837 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007838 return (VT.getVectorNumElements() == 2 ||
7839 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7840 isMOVLMask(M, VT) ||
7841 isSHUFPMask(M, VT) ||
7842 isPSHUFDMask(M, VT) ||
7843 isPSHUFHWMask(M, VT) ||
7844 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007845 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007846 isUNPCKLMask(M, VT) ||
7847 isUNPCKHMask(M, VT) ||
7848 isUNPCKL_v_undef_Mask(M, VT) ||
7849 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007850}
7851
Dan Gohman7d8143f2008-04-09 20:09:42 +00007852bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007853X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007854 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007855 unsigned NumElts = VT.getVectorNumElements();
7856 // FIXME: This collection of masks seems suspect.
7857 if (NumElts == 2)
7858 return true;
7859 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7860 return (isMOVLMask(Mask, VT) ||
7861 isCommutedMOVLMask(Mask, VT, true) ||
7862 isSHUFPMask(Mask, VT) ||
7863 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007864 }
7865 return false;
7866}
7867
7868//===----------------------------------------------------------------------===//
7869// X86 Scheduler Hooks
7870//===----------------------------------------------------------------------===//
7871
Mon P Wang63307c32008-05-05 19:05:59 +00007872// private utility function
7873MachineBasicBlock *
7874X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7875 MachineBasicBlock *MBB,
7876 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007877 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007878 unsigned LoadOpc,
7879 unsigned CXchgOpc,
7880 unsigned copyOpc,
7881 unsigned notOpc,
7882 unsigned EAXreg,
7883 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007884 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007885 // For the atomic bitwise operator, we generate
7886 // thisMBB:
7887 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007888 // ld t1 = [bitinstr.addr]
7889 // op t2 = t1, [bitinstr.val]
7890 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007891 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7892 // bz newMBB
7893 // fallthrough -->nextMBB
7894 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7895 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007896 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007897 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007898
Mon P Wang63307c32008-05-05 19:05:59 +00007899 /// First build the CFG
7900 MachineFunction *F = MBB->getParent();
7901 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007902 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7903 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7904 F->insert(MBBIter, newMBB);
7905 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007906
Mon P Wang63307c32008-05-05 19:05:59 +00007907 // Move all successors to thisMBB to nextMBB
7908 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007909
Mon P Wang63307c32008-05-05 19:05:59 +00007910 // Update thisMBB to fall through to newMBB
7911 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007912
Mon P Wang63307c32008-05-05 19:05:59 +00007913 // newMBB jumps to itself and fall through to nextMBB
7914 newMBB->addSuccessor(nextMBB);
7915 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007916
Mon P Wang63307c32008-05-05 19:05:59 +00007917 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007918 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007919 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007920 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007921 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007922 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007923 int numArgs = bInstr->getNumOperands() - 1;
7924 for (int i=0; i < numArgs; ++i)
7925 argOpers[i] = &bInstr->getOperand(i+1);
7926
7927 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007928 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7929 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007930
Dale Johannesen140be2d2008-08-19 18:47:28 +00007931 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007932 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007933 for (int i=0; i <= lastAddrIndx; ++i)
7934 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007935
Dale Johannesen140be2d2008-08-19 18:47:28 +00007936 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007937 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007938 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007939 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007940 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007941 tt = t1;
7942
Dale Johannesen140be2d2008-08-19 18:47:28 +00007943 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007944 assert((argOpers[valArgIndx]->isReg() ||
7945 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007946 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007947 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007948 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007949 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007950 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007951 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007952 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007953
Dale Johannesene4d209d2009-02-03 20:21:25 +00007954 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007955 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007956
Dale Johannesene4d209d2009-02-03 20:21:25 +00007957 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007958 for (int i=0; i <= lastAddrIndx; ++i)
7959 (*MIB).addOperand(*argOpers[i]);
7960 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007961 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00007962 (*MIB).setMemRefs(bInstr->memoperands_begin(),
7963 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00007964
Dale Johannesene4d209d2009-02-03 20:21:25 +00007965 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007966 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007967
Mon P Wang63307c32008-05-05 19:05:59 +00007968 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00007969 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007970
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007971 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007972 return nextMBB;
7973}
7974
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007975// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007976MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007977X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7978 MachineBasicBlock *MBB,
7979 unsigned regOpcL,
7980 unsigned regOpcH,
7981 unsigned immOpcL,
7982 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007983 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007984 // For the atomic bitwise operator, we generate
7985 // thisMBB (instructions are in pairs, except cmpxchg8b)
7986 // ld t1,t2 = [bitinstr.addr]
7987 // newMBB:
7988 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7989 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007990 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007991 // mov ECX, EBX <- t5, t6
7992 // mov EAX, EDX <- t1, t2
7993 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7994 // mov t3, t4 <- EAX, EDX
7995 // bz newMBB
7996 // result in out1, out2
7997 // fallthrough -->nextMBB
7998
7999 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8000 const unsigned LoadOpc = X86::MOV32rm;
8001 const unsigned copyOpc = X86::MOV32rr;
8002 const unsigned NotOpc = X86::NOT32r;
8003 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8004 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8005 MachineFunction::iterator MBBIter = MBB;
8006 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008007
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008008 /// First build the CFG
8009 MachineFunction *F = MBB->getParent();
8010 MachineBasicBlock *thisMBB = MBB;
8011 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8012 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8013 F->insert(MBBIter, newMBB);
8014 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008015
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008016 // Move all successors to thisMBB to nextMBB
8017 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008018
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008019 // Update thisMBB to fall through to newMBB
8020 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008021
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008022 // newMBB jumps to itself and fall through to nextMBB
8023 newMBB->addSuccessor(nextMBB);
8024 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008025
Dale Johannesene4d209d2009-02-03 20:21:25 +00008026 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008027 // Insert instructions into newMBB based on incoming instruction
8028 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008029 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008030 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008031 MachineOperand& dest1Oper = bInstr->getOperand(0);
8032 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008033 MachineOperand* argOpers[2 + X86AddrNumOperands];
8034 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008035 argOpers[i] = &bInstr->getOperand(i+2);
8036
Evan Chengad5b52f2010-01-08 19:14:57 +00008037 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008038 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008039
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008040 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008041 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008042 for (int i=0; i <= lastAddrIndx; ++i)
8043 (*MIB).addOperand(*argOpers[i]);
8044 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008045 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008046 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008047 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008048 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008049 MachineOperand newOp3 = *(argOpers[3]);
8050 if (newOp3.isImm())
8051 newOp3.setImm(newOp3.getImm()+4);
8052 else
8053 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008054 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008055 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008056
8057 // t3/4 are defined later, at the bottom of the loop
8058 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8059 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008060 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008061 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008062 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008063 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8064
Evan Cheng306b4ca2010-01-08 23:41:50 +00008065 // The subsequent operations should be using the destination registers of
8066 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008067 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008068 t1 = F->getRegInfo().createVirtualRegister(RC);
8069 t2 = F->getRegInfo().createVirtualRegister(RC);
8070 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8071 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008072 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008073 t1 = dest1Oper.getReg();
8074 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008075 }
8076
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008077 int valArgIndx = lastAddrIndx + 1;
8078 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008079 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008080 "invalid operand");
8081 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8082 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008083 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008084 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008085 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008086 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008087 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008088 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008089 (*MIB).addOperand(*argOpers[valArgIndx]);
8090 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008091 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008092 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008093 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008094 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008095 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008096 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008097 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008098 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008099 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008100 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008101
Dale Johannesene4d209d2009-02-03 20:21:25 +00008102 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008103 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008104 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008105 MIB.addReg(t2);
8106
Dale Johannesene4d209d2009-02-03 20:21:25 +00008107 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008108 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008109 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008110 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008111
Dale Johannesene4d209d2009-02-03 20:21:25 +00008112 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008113 for (int i=0; i <= lastAddrIndx; ++i)
8114 (*MIB).addOperand(*argOpers[i]);
8115
8116 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008117 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8118 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008119
Dale Johannesene4d209d2009-02-03 20:21:25 +00008120 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008121 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008122 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008123 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008124
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008125 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008126 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008127
8128 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8129 return nextMBB;
8130}
8131
8132// private utility function
8133MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008134X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8135 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008136 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008137 // For the atomic min/max operator, we generate
8138 // thisMBB:
8139 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008140 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008141 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008142 // cmp t1, t2
8143 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008144 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008145 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8146 // bz newMBB
8147 // fallthrough -->nextMBB
8148 //
8149 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8150 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008151 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008152 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008153
Mon P Wang63307c32008-05-05 19:05:59 +00008154 /// First build the CFG
8155 MachineFunction *F = MBB->getParent();
8156 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008157 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8158 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8159 F->insert(MBBIter, newMBB);
8160 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008161
Dan Gohmand6708ea2009-08-15 01:38:56 +00008162 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008163 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008164
Mon P Wang63307c32008-05-05 19:05:59 +00008165 // Update thisMBB to fall through to newMBB
8166 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008167
Mon P Wang63307c32008-05-05 19:05:59 +00008168 // newMBB jumps to newMBB and fall through to nextMBB
8169 newMBB->addSuccessor(nextMBB);
8170 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008171
Dale Johannesene4d209d2009-02-03 20:21:25 +00008172 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008173 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008174 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008175 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008176 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008177 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008178 int numArgs = mInstr->getNumOperands() - 1;
8179 for (int i=0; i < numArgs; ++i)
8180 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008181
Mon P Wang63307c32008-05-05 19:05:59 +00008182 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008183 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8184 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008185
Mon P Wangab3e7472008-05-05 22:56:23 +00008186 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008187 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008188 for (int i=0; i <= lastAddrIndx; ++i)
8189 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008190
Mon P Wang63307c32008-05-05 19:05:59 +00008191 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008192 assert((argOpers[valArgIndx]->isReg() ||
8193 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008194 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008195
8196 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008197 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008198 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008199 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008200 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008201 (*MIB).addOperand(*argOpers[valArgIndx]);
8202
Dale Johannesene4d209d2009-02-03 20:21:25 +00008203 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008204 MIB.addReg(t1);
8205
Dale Johannesene4d209d2009-02-03 20:21:25 +00008206 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008207 MIB.addReg(t1);
8208 MIB.addReg(t2);
8209
8210 // Generate movc
8211 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008212 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008213 MIB.addReg(t2);
8214 MIB.addReg(t1);
8215
8216 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008217 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008218 for (int i=0; i <= lastAddrIndx; ++i)
8219 (*MIB).addOperand(*argOpers[i]);
8220 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008221 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008222 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8223 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008224
Dale Johannesene4d209d2009-02-03 20:21:25 +00008225 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008226 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008227
Mon P Wang63307c32008-05-05 19:05:59 +00008228 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008229 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008230
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008231 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008232 return nextMBB;
8233}
8234
Eric Christopherf83a5de2009-08-27 18:08:16 +00008235// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8236// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008237MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008238X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008239 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008240
8241 MachineFunction *F = BB->getParent();
8242 DebugLoc dl = MI->getDebugLoc();
8243 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8244
8245 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008246 if (memArg)
8247 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8248 else
8249 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008250
8251 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8252
8253 for (unsigned i = 0; i < numArgs; ++i) {
8254 MachineOperand &Op = MI->getOperand(i+1);
8255
8256 if (!(Op.isReg() && Op.isImplicit()))
8257 MIB.addOperand(Op);
8258 }
8259
8260 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8261 .addReg(X86::XMM0);
8262
8263 F->DeleteMachineInstr(MI);
8264
8265 return BB;
8266}
8267
8268MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008269X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8270 MachineInstr *MI,
8271 MachineBasicBlock *MBB) const {
8272 // Emit code to save XMM registers to the stack. The ABI says that the
8273 // number of registers to save is given in %al, so it's theoretically
8274 // possible to do an indirect jump trick to avoid saving all of them,
8275 // however this code takes a simpler approach and just executes all
8276 // of the stores if %al is non-zero. It's less code, and it's probably
8277 // easier on the hardware branch predictor, and stores aren't all that
8278 // expensive anyway.
8279
8280 // Create the new basic blocks. One block contains all the XMM stores,
8281 // and one block is the final destination regardless of whether any
8282 // stores were performed.
8283 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8284 MachineFunction *F = MBB->getParent();
8285 MachineFunction::iterator MBBIter = MBB;
8286 ++MBBIter;
8287 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8288 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8289 F->insert(MBBIter, XMMSaveMBB);
8290 F->insert(MBBIter, EndMBB);
8291
8292 // Set up the CFG.
8293 // Move any original successors of MBB to the end block.
8294 EndMBB->transferSuccessors(MBB);
8295 // The original block will now fall through to the XMM save block.
8296 MBB->addSuccessor(XMMSaveMBB);
8297 // The XMMSaveMBB will fall through to the end block.
8298 XMMSaveMBB->addSuccessor(EndMBB);
8299
8300 // Now add the instructions.
8301 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8302 DebugLoc DL = MI->getDebugLoc();
8303
8304 unsigned CountReg = MI->getOperand(0).getReg();
8305 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8306 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8307
8308 if (!Subtarget->isTargetWin64()) {
8309 // If %al is 0, branch around the XMM save block.
8310 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008311 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008312 MBB->addSuccessor(EndMBB);
8313 }
8314
8315 // In the XMM save block, save all the XMM argument registers.
8316 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8317 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008318 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008319 F->getMachineMemOperand(
8320 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8321 MachineMemOperand::MOStore, Offset,
8322 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008323 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8324 .addFrameIndex(RegSaveFrameIndex)
8325 .addImm(/*Scale=*/1)
8326 .addReg(/*IndexReg=*/0)
8327 .addImm(/*Disp=*/Offset)
8328 .addReg(/*Segment=*/0)
8329 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008330 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008331 }
8332
8333 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8334
8335 return EndMBB;
8336}
Mon P Wang63307c32008-05-05 19:05:59 +00008337
Evan Cheng60c07e12006-07-05 22:17:51 +00008338MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008339X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008340 MachineBasicBlock *BB,
8341 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008342 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8343 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008344
Chris Lattner52600972009-09-02 05:57:00 +00008345 // To "insert" a SELECT_CC instruction, we actually have to insert the
8346 // diamond control-flow pattern. The incoming instruction knows the
8347 // destination vreg to set, the condition code register to branch on, the
8348 // true/false values to select between, and a branch opcode to use.
8349 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8350 MachineFunction::iterator It = BB;
8351 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008352
Chris Lattner52600972009-09-02 05:57:00 +00008353 // thisMBB:
8354 // ...
8355 // TrueVal = ...
8356 // cmpTY ccX, r1, r2
8357 // bCC copy1MBB
8358 // fallthrough --> copy0MBB
8359 MachineBasicBlock *thisMBB = BB;
8360 MachineFunction *F = BB->getParent();
8361 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8362 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8363 unsigned Opc =
8364 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8365 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8366 F->insert(It, copy0MBB);
8367 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008368 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008369 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008370 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008371 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008372 E = BB->succ_end(); I != E; ++I) {
8373 EM->insert(std::make_pair(*I, sinkMBB));
8374 sinkMBB->addSuccessor(*I);
8375 }
8376 // Next, remove all successors of the current block, and add the true
8377 // and fallthrough blocks as its successors.
8378 while (!BB->succ_empty())
8379 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008380 // Add the true and fallthrough blocks as its successors.
8381 BB->addSuccessor(copy0MBB);
8382 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008383
Chris Lattner52600972009-09-02 05:57:00 +00008384 // copy0MBB:
8385 // %FalseValue = ...
8386 // # fallthrough to sinkMBB
8387 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008388
Chris Lattner52600972009-09-02 05:57:00 +00008389 // Update machine-CFG edges
8390 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008391
Chris Lattner52600972009-09-02 05:57:00 +00008392 // sinkMBB:
8393 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8394 // ...
8395 BB = sinkMBB;
8396 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8397 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8398 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8399
8400 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8401 return BB;
8402}
8403
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008404MachineBasicBlock *
8405X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8406 MachineBasicBlock *BB,
8407 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8408 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8409 DebugLoc DL = MI->getDebugLoc();
8410 MachineFunction *F = BB->getParent();
8411
8412 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8413 // non-trivial part is impdef of ESP.
8414 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8415 // mingw-w64.
8416
8417 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8418 .addExternalSymbol("_alloca")
8419 .addReg(X86::EAX, RegState::Implicit)
8420 .addReg(X86::ESP, RegState::Implicit)
8421 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8422 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8423
8424 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8425 return BB;
8426}
Chris Lattner52600972009-09-02 05:57:00 +00008427
8428MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008429X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008430 MachineBasicBlock *BB,
8431 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008432 switch (MI->getOpcode()) {
8433 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008434 case X86::MINGW_ALLOCA:
8435 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008436 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008437 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008438 case X86::CMOV_FR32:
8439 case X86::CMOV_FR64:
8440 case X86::CMOV_V4F32:
8441 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008442 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008443 case X86::CMOV_GR16:
8444 case X86::CMOV_GR32:
8445 case X86::CMOV_RFP32:
8446 case X86::CMOV_RFP64:
8447 case X86::CMOV_RFP80:
Evan Chengce319102009-09-19 09:51:03 +00008448 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008449
Dale Johannesen849f2142007-07-03 00:53:03 +00008450 case X86::FP32_TO_INT16_IN_MEM:
8451 case X86::FP32_TO_INT32_IN_MEM:
8452 case X86::FP32_TO_INT64_IN_MEM:
8453 case X86::FP64_TO_INT16_IN_MEM:
8454 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008455 case X86::FP64_TO_INT64_IN_MEM:
8456 case X86::FP80_TO_INT16_IN_MEM:
8457 case X86::FP80_TO_INT32_IN_MEM:
8458 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008459 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8460 DebugLoc DL = MI->getDebugLoc();
8461
Evan Cheng60c07e12006-07-05 22:17:51 +00008462 // Change the floating point control register to use "round towards zero"
8463 // mode when truncating to an integer value.
8464 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008465 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008466 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008467
8468 // Load the old value of the high byte of the control word...
8469 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008470 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008471 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008472 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008473
8474 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008475 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008476 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008477
8478 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008479 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008480
8481 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008482 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008483 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008484
8485 // Get the X86 opcode to use.
8486 unsigned Opc;
8487 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008488 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008489 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8490 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8491 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8492 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8493 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8494 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008495 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8496 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8497 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008498 }
8499
8500 X86AddressMode AM;
8501 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008502 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008503 AM.BaseType = X86AddressMode::RegBase;
8504 AM.Base.Reg = Op.getReg();
8505 } else {
8506 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008507 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008508 }
8509 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008510 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008511 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008512 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008513 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008514 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008515 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008516 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008517 AM.GV = Op.getGlobal();
8518 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008519 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008520 }
Chris Lattner52600972009-09-02 05:57:00 +00008521 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008522 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008523
8524 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008525 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008526
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008527 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008528 return BB;
8529 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00008530 // DBG_VALUE. Only the frame index case is done here.
8531 case X86::DBG_VALUE: {
8532 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8533 DebugLoc DL = MI->getDebugLoc();
8534 X86AddressMode AM;
8535 MachineFunction *F = BB->getParent();
8536 AM.BaseType = X86AddressMode::FrameIndexBase;
8537 AM.Base.FrameIndex = MI->getOperand(0).getImm();
8538 addFullAddress(BuildMI(BB, DL, TII->get(X86::DBG_VALUE)), AM).
8539 addImm(MI->getOperand(1).getImm()).
8540 addMetadata(MI->getOperand(2).getMetadata());
8541 F->DeleteMachineInstr(MI); // Remove pseudo.
8542 return BB;
8543 }
8544
Eric Christopherb120ab42009-08-18 22:50:32 +00008545 // String/text processing lowering.
8546 case X86::PCMPISTRM128REG:
8547 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8548 case X86::PCMPISTRM128MEM:
8549 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8550 case X86::PCMPESTRM128REG:
8551 return EmitPCMP(MI, BB, 5, false /* in mem */);
8552 case X86::PCMPESTRM128MEM:
8553 return EmitPCMP(MI, BB, 5, true /* in mem */);
8554
8555 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008556 case X86::ATOMAND32:
8557 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008558 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008559 X86::LCMPXCHG32, X86::MOV32rr,
8560 X86::NOT32r, X86::EAX,
8561 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008562 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008563 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8564 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008565 X86::LCMPXCHG32, X86::MOV32rr,
8566 X86::NOT32r, X86::EAX,
8567 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008568 case X86::ATOMXOR32:
8569 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008570 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008571 X86::LCMPXCHG32, X86::MOV32rr,
8572 X86::NOT32r, X86::EAX,
8573 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008574 case X86::ATOMNAND32:
8575 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008576 X86::AND32ri, X86::MOV32rm,
8577 X86::LCMPXCHG32, X86::MOV32rr,
8578 X86::NOT32r, X86::EAX,
8579 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008580 case X86::ATOMMIN32:
8581 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8582 case X86::ATOMMAX32:
8583 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8584 case X86::ATOMUMIN32:
8585 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8586 case X86::ATOMUMAX32:
8587 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008588
8589 case X86::ATOMAND16:
8590 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8591 X86::AND16ri, X86::MOV16rm,
8592 X86::LCMPXCHG16, X86::MOV16rr,
8593 X86::NOT16r, X86::AX,
8594 X86::GR16RegisterClass);
8595 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008596 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008597 X86::OR16ri, X86::MOV16rm,
8598 X86::LCMPXCHG16, X86::MOV16rr,
8599 X86::NOT16r, X86::AX,
8600 X86::GR16RegisterClass);
8601 case X86::ATOMXOR16:
8602 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8603 X86::XOR16ri, X86::MOV16rm,
8604 X86::LCMPXCHG16, X86::MOV16rr,
8605 X86::NOT16r, X86::AX,
8606 X86::GR16RegisterClass);
8607 case X86::ATOMNAND16:
8608 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8609 X86::AND16ri, X86::MOV16rm,
8610 X86::LCMPXCHG16, X86::MOV16rr,
8611 X86::NOT16r, X86::AX,
8612 X86::GR16RegisterClass, true);
8613 case X86::ATOMMIN16:
8614 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8615 case X86::ATOMMAX16:
8616 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8617 case X86::ATOMUMIN16:
8618 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8619 case X86::ATOMUMAX16:
8620 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8621
8622 case X86::ATOMAND8:
8623 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8624 X86::AND8ri, X86::MOV8rm,
8625 X86::LCMPXCHG8, X86::MOV8rr,
8626 X86::NOT8r, X86::AL,
8627 X86::GR8RegisterClass);
8628 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008629 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008630 X86::OR8ri, X86::MOV8rm,
8631 X86::LCMPXCHG8, X86::MOV8rr,
8632 X86::NOT8r, X86::AL,
8633 X86::GR8RegisterClass);
8634 case X86::ATOMXOR8:
8635 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8636 X86::XOR8ri, X86::MOV8rm,
8637 X86::LCMPXCHG8, X86::MOV8rr,
8638 X86::NOT8r, X86::AL,
8639 X86::GR8RegisterClass);
8640 case X86::ATOMNAND8:
8641 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8642 X86::AND8ri, X86::MOV8rm,
8643 X86::LCMPXCHG8, X86::MOV8rr,
8644 X86::NOT8r, X86::AL,
8645 X86::GR8RegisterClass, true);
8646 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008647 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008648 case X86::ATOMAND64:
8649 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008650 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008651 X86::LCMPXCHG64, X86::MOV64rr,
8652 X86::NOT64r, X86::RAX,
8653 X86::GR64RegisterClass);
8654 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008655 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8656 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008657 X86::LCMPXCHG64, X86::MOV64rr,
8658 X86::NOT64r, X86::RAX,
8659 X86::GR64RegisterClass);
8660 case X86::ATOMXOR64:
8661 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008662 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008663 X86::LCMPXCHG64, X86::MOV64rr,
8664 X86::NOT64r, X86::RAX,
8665 X86::GR64RegisterClass);
8666 case X86::ATOMNAND64:
8667 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8668 X86::AND64ri32, X86::MOV64rm,
8669 X86::LCMPXCHG64, X86::MOV64rr,
8670 X86::NOT64r, X86::RAX,
8671 X86::GR64RegisterClass, true);
8672 case X86::ATOMMIN64:
8673 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8674 case X86::ATOMMAX64:
8675 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8676 case X86::ATOMUMIN64:
8677 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8678 case X86::ATOMUMAX64:
8679 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008680
8681 // This group does 64-bit operations on a 32-bit host.
8682 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008683 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008684 X86::AND32rr, X86::AND32rr,
8685 X86::AND32ri, X86::AND32ri,
8686 false);
8687 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008688 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008689 X86::OR32rr, X86::OR32rr,
8690 X86::OR32ri, X86::OR32ri,
8691 false);
8692 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008693 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008694 X86::XOR32rr, X86::XOR32rr,
8695 X86::XOR32ri, X86::XOR32ri,
8696 false);
8697 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008698 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008699 X86::AND32rr, X86::AND32rr,
8700 X86::AND32ri, X86::AND32ri,
8701 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008702 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008703 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008704 X86::ADD32rr, X86::ADC32rr,
8705 X86::ADD32ri, X86::ADC32ri,
8706 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008707 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008708 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008709 X86::SUB32rr, X86::SBB32rr,
8710 X86::SUB32ri, X86::SBB32ri,
8711 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008712 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008713 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008714 X86::MOV32rr, X86::MOV32rr,
8715 X86::MOV32ri, X86::MOV32ri,
8716 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008717 case X86::VASTART_SAVE_XMM_REGS:
8718 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008719 }
8720}
8721
8722//===----------------------------------------------------------------------===//
8723// X86 Optimization Hooks
8724//===----------------------------------------------------------------------===//
8725
Dan Gohman475871a2008-07-27 21:46:04 +00008726void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008727 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008728 APInt &KnownZero,
8729 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008730 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008731 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008732 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008733 assert((Opc >= ISD::BUILTIN_OP_END ||
8734 Opc == ISD::INTRINSIC_WO_CHAIN ||
8735 Opc == ISD::INTRINSIC_W_CHAIN ||
8736 Opc == ISD::INTRINSIC_VOID) &&
8737 "Should use MaskedValueIsZero if you don't know whether Op"
8738 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008739
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008740 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008741 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008742 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008743 case X86ISD::ADD:
8744 case X86ISD::SUB:
8745 case X86ISD::SMUL:
8746 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008747 case X86ISD::INC:
8748 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008749 case X86ISD::OR:
8750 case X86ISD::XOR:
8751 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008752 // These nodes' second result is a boolean.
8753 if (Op.getResNo() == 0)
8754 break;
8755 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008756 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008757 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8758 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008759 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008760 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008761}
Chris Lattner259e97c2006-01-31 19:43:35 +00008762
Evan Cheng206ee9d2006-07-07 08:33:52 +00008763/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008764/// node is a GlobalAddress + offset.
8765bool X86TargetLowering::isGAPlusOffset(SDNode *N,
8766 GlobalValue* &GA, int64_t &Offset) const{
8767 if (N->getOpcode() == X86ISD::Wrapper) {
8768 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008769 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008770 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008771 return true;
8772 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008773 }
Evan Chengad4196b2008-05-12 19:56:52 +00008774 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008775}
8776
Nate Begeman9008ca62009-04-27 18:41:29 +00008777static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Dan Gohman8a55ce42009-09-23 21:02:20 +00008778 EVT EltVT, LoadSDNode *&LDBase,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008779 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00008780 SelectionDAG &DAG, MachineFrameInfo *MFI,
8781 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008782 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00008783 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008784 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008785 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00008786 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00008787 return false;
8788 continue;
8789 }
8790
Dan Gohman475871a2008-07-27 21:46:04 +00008791 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00008792 if (!Elt.getNode() ||
8793 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008794 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008795 if (!LDBase) {
8796 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00008797 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008798 LDBase = cast<LoadSDNode>(Elt.getNode());
8799 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008800 continue;
8801 }
8802 if (Elt.getOpcode() == ISD::UNDEF)
8803 continue;
8804
Nate Begemanabc01992009-06-05 21:37:30 +00008805 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Evan Cheng64fa4a92009-12-09 01:36:00 +00008806 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
Evan Cheng7e2ff772008-05-08 00:57:18 +00008807 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00008808 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00008809 }
8810 return true;
8811}
Evan Cheng206ee9d2006-07-07 08:33:52 +00008812
8813/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8814/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8815/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00008816/// order. In the case of v2i64, it will see if it can rewrite the
8817/// shuffle to be an appropriate build vector so it can take advantage of
8818// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00008819static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008820 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008821 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008822 EVT VT = N->getValueType(0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00008823 EVT EltVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008824 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8825 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00008826
Eli Friedman7a5e5552009-06-07 06:52:44 +00008827 if (VT.getSizeInBits() != 128)
8828 return SDValue();
8829
Mon P Wang1e955802009-04-03 02:43:30 +00008830 // Try to combine a vector_shuffle into a 128-bit load.
8831 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00008832 LoadSDNode *LD = NULL;
8833 unsigned LastLoadedElt;
Dan Gohman8a55ce42009-09-23 21:02:20 +00008834 if (!EltsFromConsecutiveLoads(SVN, NumElems, EltVT, LD, LastLoadedElt, DAG,
Eli Friedman7a5e5552009-06-07 06:52:44 +00008835 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00008836 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008837
Eli Friedman7a5e5552009-06-07 06:52:44 +00008838 if (LastLoadedElt == NumElems - 1) {
Evan Cheng7bd64782009-12-09 01:53:58 +00008839 if (DAG.InferPtrAlignment(LD->getBasePtr()) >= 16)
Eli Friedman7a5e5552009-06-07 06:52:44 +00008840 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
8841 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00008842 LD->isVolatile(), LD->isNonTemporal(), 0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008843 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008844 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00008845 LD->isVolatile(), LD->isNonTemporal(),
8846 LD->getAlignment());
Eli Friedman7a5e5552009-06-07 06:52:44 +00008847 } else if (NumElems == 4 && LastLoadedElt == 1) {
Owen Anderson825b72b2009-08-11 20:47:22 +00008848 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00008849 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
8850 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00008851 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
8852 }
8853 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008854}
Evan Chengd880b972008-05-09 21:53:03 +00008855
Chris Lattner83e6c992006-10-04 06:57:07 +00008856/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008857static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008858 const X86Subtarget *Subtarget) {
8859 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008860 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008861 // Get the LHS/RHS of the select.
8862 SDValue LHS = N->getOperand(1);
8863 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008864
Dan Gohman670e5392009-09-21 18:03:22 +00008865 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00008866 // instructions match the semantics of the common C idiom x<y?x:y but not
8867 // x<=y?x:y, because of how they handle negative zero (which can be
8868 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00008869 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008870 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008871 Cond.getOpcode() == ISD::SETCC) {
8872 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008873
Chris Lattner47b4ce82009-03-11 05:48:52 +00008874 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008875 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00008876 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
8877 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008878 switch (CC) {
8879 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008880 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008881 // Converting this to a min would handle NaNs incorrectly, and swapping
8882 // the operands would cause it to handle comparisons between positive
8883 // and negative zero incorrectly.
8884 if (!FiniteOnlyFPMath() &&
8885 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8886 if (!UnsafeFPMath &&
8887 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8888 break;
8889 std::swap(LHS, RHS);
8890 }
Dan Gohman670e5392009-09-21 18:03:22 +00008891 Opcode = X86ISD::FMIN;
8892 break;
8893 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008894 // Converting this to a min would handle comparisons between positive
8895 // and negative zero incorrectly.
8896 if (!UnsafeFPMath &&
8897 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
8898 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008899 Opcode = X86ISD::FMIN;
8900 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008901 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008902 // Converting this to a min would handle both negative zeros and NaNs
8903 // incorrectly, but we can swap the operands to fix both.
8904 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008905 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008906 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008907 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008908 Opcode = X86ISD::FMIN;
8909 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008910
Dan Gohman670e5392009-09-21 18:03:22 +00008911 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008912 // Converting this to a max would handle comparisons between positive
8913 // and negative zero incorrectly.
8914 if (!UnsafeFPMath &&
8915 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
8916 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008917 Opcode = X86ISD::FMAX;
8918 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008919 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008920 // Converting this to a max would handle NaNs incorrectly, and swapping
8921 // the operands would cause it to handle comparisons between positive
8922 // and negative zero incorrectly.
8923 if (!FiniteOnlyFPMath() &&
8924 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8925 if (!UnsafeFPMath &&
8926 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8927 break;
8928 std::swap(LHS, RHS);
8929 }
Dan Gohman670e5392009-09-21 18:03:22 +00008930 Opcode = X86ISD::FMAX;
8931 break;
8932 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008933 // Converting this to a max would handle both negative zeros and NaNs
8934 // incorrectly, but we can swap the operands to fix both.
8935 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008936 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008937 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008938 case ISD::SETGE:
8939 Opcode = X86ISD::FMAX;
8940 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008941 }
Dan Gohman670e5392009-09-21 18:03:22 +00008942 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00008943 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
8944 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008945 switch (CC) {
8946 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008947 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008948 // Converting this to a min would handle comparisons between positive
8949 // and negative zero incorrectly, and swapping the operands would
8950 // cause it to handle NaNs incorrectly.
8951 if (!UnsafeFPMath &&
8952 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
8953 if (!FiniteOnlyFPMath() &&
8954 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8955 break;
8956 std::swap(LHS, RHS);
8957 }
Dan Gohman670e5392009-09-21 18:03:22 +00008958 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00008959 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008960 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008961 // Converting this to a min would handle NaNs incorrectly.
8962 if (!UnsafeFPMath &&
8963 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8964 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008965 Opcode = X86ISD::FMIN;
8966 break;
8967 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008968 // Converting this to a min would handle both negative zeros and NaNs
8969 // incorrectly, but we can swap the operands to fix both.
8970 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008971 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008972 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008973 case ISD::SETGE:
8974 Opcode = X86ISD::FMIN;
8975 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008976
Dan Gohman670e5392009-09-21 18:03:22 +00008977 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008978 // Converting this to a max would handle NaNs incorrectly.
8979 if (!FiniteOnlyFPMath() &&
8980 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8981 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008982 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00008983 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008984 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008985 // Converting this to a max would handle comparisons between positive
8986 // and negative zero incorrectly, and swapping the operands would
8987 // cause it to handle NaNs incorrectly.
8988 if (!UnsafeFPMath &&
8989 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
8990 if (!FiniteOnlyFPMath() &&
8991 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
8992 break;
8993 std::swap(LHS, RHS);
8994 }
Dan Gohman670e5392009-09-21 18:03:22 +00008995 Opcode = X86ISD::FMAX;
8996 break;
8997 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008998 // Converting this to a max would handle both negative zeros and NaNs
8999 // incorrectly, but we can swap the operands to fix both.
9000 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009001 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009002 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009003 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009004 Opcode = X86ISD::FMAX;
9005 break;
9006 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009007 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009008
Chris Lattner47b4ce82009-03-11 05:48:52 +00009009 if (Opcode)
9010 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009011 }
Eric Christopherfd179292009-08-27 18:07:15 +00009012
Chris Lattnerd1980a52009-03-12 06:52:53 +00009013 // If this is a select between two integer constants, try to do some
9014 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009015 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9016 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009017 // Don't do this for crazy integer types.
9018 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9019 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009020 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009021 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009022
Chris Lattnercee56e72009-03-13 05:53:31 +00009023 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009024 // Efficiently invertible.
9025 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9026 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9027 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9028 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009029 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009030 }
Eric Christopherfd179292009-08-27 18:07:15 +00009031
Chris Lattnerd1980a52009-03-12 06:52:53 +00009032 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009033 if (FalseC->getAPIntValue() == 0 &&
9034 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009035 if (NeedsCondInvert) // Invert the condition if needed.
9036 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9037 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009038
Chris Lattnerd1980a52009-03-12 06:52:53 +00009039 // Zero extend the condition if needed.
9040 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009041
Chris Lattnercee56e72009-03-13 05:53:31 +00009042 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009043 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009044 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009045 }
Eric Christopherfd179292009-08-27 18:07:15 +00009046
Chris Lattner97a29a52009-03-13 05:22:11 +00009047 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009048 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009049 if (NeedsCondInvert) // Invert the condition if needed.
9050 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9051 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009052
Chris Lattner97a29a52009-03-13 05:22:11 +00009053 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009054 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9055 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009056 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009057 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009058 }
Eric Christopherfd179292009-08-27 18:07:15 +00009059
Chris Lattnercee56e72009-03-13 05:53:31 +00009060 // Optimize cases that will turn into an LEA instruction. This requires
9061 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009062 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009063 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009064 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009065
Chris Lattnercee56e72009-03-13 05:53:31 +00009066 bool isFastMultiplier = false;
9067 if (Diff < 10) {
9068 switch ((unsigned char)Diff) {
9069 default: break;
9070 case 1: // result = add base, cond
9071 case 2: // result = lea base( , cond*2)
9072 case 3: // result = lea base(cond, cond*2)
9073 case 4: // result = lea base( , cond*4)
9074 case 5: // result = lea base(cond, cond*4)
9075 case 8: // result = lea base( , cond*8)
9076 case 9: // result = lea base(cond, cond*8)
9077 isFastMultiplier = true;
9078 break;
9079 }
9080 }
Eric Christopherfd179292009-08-27 18:07:15 +00009081
Chris Lattnercee56e72009-03-13 05:53:31 +00009082 if (isFastMultiplier) {
9083 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9084 if (NeedsCondInvert) // Invert the condition if needed.
9085 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9086 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009087
Chris Lattnercee56e72009-03-13 05:53:31 +00009088 // Zero extend the condition if needed.
9089 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9090 Cond);
9091 // Scale the condition by the difference.
9092 if (Diff != 1)
9093 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9094 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009095
Chris Lattnercee56e72009-03-13 05:53:31 +00009096 // Add the base if non-zero.
9097 if (FalseC->getAPIntValue() != 0)
9098 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9099 SDValue(FalseC, 0));
9100 return Cond;
9101 }
Eric Christopherfd179292009-08-27 18:07:15 +00009102 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009103 }
9104 }
Eric Christopherfd179292009-08-27 18:07:15 +00009105
Dan Gohman475871a2008-07-27 21:46:04 +00009106 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009107}
9108
Chris Lattnerd1980a52009-03-12 06:52:53 +00009109/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9110static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9111 TargetLowering::DAGCombinerInfo &DCI) {
9112 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009113
Chris Lattnerd1980a52009-03-12 06:52:53 +00009114 // If the flag operand isn't dead, don't touch this CMOV.
9115 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9116 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009117
Chris Lattnerd1980a52009-03-12 06:52:53 +00009118 // If this is a select between two integer constants, try to do some
9119 // optimizations. Note that the operands are ordered the opposite of SELECT
9120 // operands.
9121 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9122 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9123 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9124 // larger than FalseC (the false value).
9125 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009126
Chris Lattnerd1980a52009-03-12 06:52:53 +00009127 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9128 CC = X86::GetOppositeBranchCondition(CC);
9129 std::swap(TrueC, FalseC);
9130 }
Eric Christopherfd179292009-08-27 18:07:15 +00009131
Chris Lattnerd1980a52009-03-12 06:52:53 +00009132 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009133 // This is efficient for any integer data type (including i8/i16) and
9134 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009135 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9136 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009137 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9138 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009139
Chris Lattnerd1980a52009-03-12 06:52:53 +00009140 // Zero extend the condition if needed.
9141 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009142
Chris Lattnerd1980a52009-03-12 06:52:53 +00009143 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9144 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009145 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009146 if (N->getNumValues() == 2) // Dead flag value?
9147 return DCI.CombineTo(N, Cond, SDValue());
9148 return Cond;
9149 }
Eric Christopherfd179292009-08-27 18:07:15 +00009150
Chris Lattnercee56e72009-03-13 05:53:31 +00009151 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9152 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009153 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9154 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009155 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9156 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009157
Chris Lattner97a29a52009-03-13 05:22:11 +00009158 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009159 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9160 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009161 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9162 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009163
Chris Lattner97a29a52009-03-13 05:22:11 +00009164 if (N->getNumValues() == 2) // Dead flag value?
9165 return DCI.CombineTo(N, Cond, SDValue());
9166 return Cond;
9167 }
Eric Christopherfd179292009-08-27 18:07:15 +00009168
Chris Lattnercee56e72009-03-13 05:53:31 +00009169 // Optimize cases that will turn into an LEA instruction. This requires
9170 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009171 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009172 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009173 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009174
Chris Lattnercee56e72009-03-13 05:53:31 +00009175 bool isFastMultiplier = false;
9176 if (Diff < 10) {
9177 switch ((unsigned char)Diff) {
9178 default: break;
9179 case 1: // result = add base, cond
9180 case 2: // result = lea base( , cond*2)
9181 case 3: // result = lea base(cond, cond*2)
9182 case 4: // result = lea base( , cond*4)
9183 case 5: // result = lea base(cond, cond*4)
9184 case 8: // result = lea base( , cond*8)
9185 case 9: // result = lea base(cond, cond*8)
9186 isFastMultiplier = true;
9187 break;
9188 }
9189 }
Eric Christopherfd179292009-08-27 18:07:15 +00009190
Chris Lattnercee56e72009-03-13 05:53:31 +00009191 if (isFastMultiplier) {
9192 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9193 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009194 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9195 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009196 // Zero extend the condition if needed.
9197 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9198 Cond);
9199 // Scale the condition by the difference.
9200 if (Diff != 1)
9201 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9202 DAG.getConstant(Diff, Cond.getValueType()));
9203
9204 // Add the base if non-zero.
9205 if (FalseC->getAPIntValue() != 0)
9206 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9207 SDValue(FalseC, 0));
9208 if (N->getNumValues() == 2) // Dead flag value?
9209 return DCI.CombineTo(N, Cond, SDValue());
9210 return Cond;
9211 }
Eric Christopherfd179292009-08-27 18:07:15 +00009212 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009213 }
9214 }
9215 return SDValue();
9216}
9217
9218
Evan Cheng0b0cd912009-03-28 05:57:29 +00009219/// PerformMulCombine - Optimize a single multiply with constant into two
9220/// in order to implement it with two cheaper instructions, e.g.
9221/// LEA + SHL, LEA + LEA.
9222static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9223 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009224 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9225 return SDValue();
9226
Owen Andersone50ed302009-08-10 22:56:29 +00009227 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009228 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009229 return SDValue();
9230
9231 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9232 if (!C)
9233 return SDValue();
9234 uint64_t MulAmt = C->getZExtValue();
9235 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9236 return SDValue();
9237
9238 uint64_t MulAmt1 = 0;
9239 uint64_t MulAmt2 = 0;
9240 if ((MulAmt % 9) == 0) {
9241 MulAmt1 = 9;
9242 MulAmt2 = MulAmt / 9;
9243 } else if ((MulAmt % 5) == 0) {
9244 MulAmt1 = 5;
9245 MulAmt2 = MulAmt / 5;
9246 } else if ((MulAmt % 3) == 0) {
9247 MulAmt1 = 3;
9248 MulAmt2 = MulAmt / 3;
9249 }
9250 if (MulAmt2 &&
9251 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9252 DebugLoc DL = N->getDebugLoc();
9253
9254 if (isPowerOf2_64(MulAmt2) &&
9255 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9256 // If second multiplifer is pow2, issue it first. We want the multiply by
9257 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9258 // is an add.
9259 std::swap(MulAmt1, MulAmt2);
9260
9261 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009262 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009263 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009264 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009265 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009266 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009267 DAG.getConstant(MulAmt1, VT));
9268
Eric Christopherfd179292009-08-27 18:07:15 +00009269 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009270 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009271 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009272 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009273 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009274 DAG.getConstant(MulAmt2, VT));
9275
9276 // Do not add new nodes to DAG combiner worklist.
9277 DCI.CombineTo(N, NewMul, false);
9278 }
9279 return SDValue();
9280}
9281
Evan Chengad9c0a32009-12-15 00:53:42 +00009282static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9283 SDValue N0 = N->getOperand(0);
9284 SDValue N1 = N->getOperand(1);
9285 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9286 EVT VT = N0.getValueType();
9287
9288 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9289 // since the result of setcc_c is all zero's or all ones.
9290 if (N1C && N0.getOpcode() == ISD::AND &&
9291 N0.getOperand(1).getOpcode() == ISD::Constant) {
9292 SDValue N00 = N0.getOperand(0);
9293 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9294 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9295 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9296 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9297 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9298 APInt ShAmt = N1C->getAPIntValue();
9299 Mask = Mask.shl(ShAmt);
9300 if (Mask != 0)
9301 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9302 N00, DAG.getConstant(Mask, VT));
9303 }
9304 }
9305
9306 return SDValue();
9307}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009308
Nate Begeman740ab032009-01-26 00:52:55 +00009309/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9310/// when possible.
9311static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9312 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009313 EVT VT = N->getValueType(0);
9314 if (!VT.isVector() && VT.isInteger() &&
9315 N->getOpcode() == ISD::SHL)
9316 return PerformSHLCombine(N, DAG);
9317
Nate Begeman740ab032009-01-26 00:52:55 +00009318 // On X86 with SSE2 support, we can transform this to a vector shift if
9319 // all elements are shifted by the same amount. We can't do this in legalize
9320 // because the a constant vector is typically transformed to a constant pool
9321 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009322 if (!Subtarget->hasSSE2())
9323 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009324
Owen Anderson825b72b2009-08-11 20:47:22 +00009325 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009326 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009327
Mon P Wang3becd092009-01-28 08:12:05 +00009328 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009329 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009330 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009331 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009332 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9333 unsigned NumElts = VT.getVectorNumElements();
9334 unsigned i = 0;
9335 for (; i != NumElts; ++i) {
9336 SDValue Arg = ShAmtOp.getOperand(i);
9337 if (Arg.getOpcode() == ISD::UNDEF) continue;
9338 BaseShAmt = Arg;
9339 break;
9340 }
9341 for (; i != NumElts; ++i) {
9342 SDValue Arg = ShAmtOp.getOperand(i);
9343 if (Arg.getOpcode() == ISD::UNDEF) continue;
9344 if (Arg != BaseShAmt) {
9345 return SDValue();
9346 }
9347 }
9348 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009349 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009350 SDValue InVec = ShAmtOp.getOperand(0);
9351 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9352 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9353 unsigned i = 0;
9354 for (; i != NumElts; ++i) {
9355 SDValue Arg = InVec.getOperand(i);
9356 if (Arg.getOpcode() == ISD::UNDEF) continue;
9357 BaseShAmt = Arg;
9358 break;
9359 }
9360 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9361 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009362 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009363 if (C->getZExtValue() == SplatIdx)
9364 BaseShAmt = InVec.getOperand(1);
9365 }
9366 }
9367 if (BaseShAmt.getNode() == 0)
9368 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9369 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009370 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009371 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009372
Mon P Wangefa42202009-09-03 19:56:25 +00009373 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009374 if (EltVT.bitsGT(MVT::i32))
9375 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9376 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009377 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009378
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009379 // The shift amount is identical so we can do a vector shift.
9380 SDValue ValOp = N->getOperand(0);
9381 switch (N->getOpcode()) {
9382 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009383 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009384 break;
9385 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009386 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009387 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009388 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009389 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009390 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009391 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009392 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009393 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009394 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009395 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009396 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009397 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009398 break;
9399 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009400 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009401 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009402 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009403 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009404 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009405 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009406 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009407 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009408 break;
9409 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009410 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009411 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009412 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009413 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009414 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009415 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009416 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009417 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009418 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009419 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009420 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009421 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009422 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009423 }
9424 return SDValue();
9425}
9426
Evan Cheng760d1942010-01-04 21:22:48 +00009427static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9428 const X86Subtarget *Subtarget) {
9429 EVT VT = N->getValueType(0);
9430 if (VT != MVT::i64 || !Subtarget->is64Bit())
9431 return SDValue();
9432
9433 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9434 SDValue N0 = N->getOperand(0);
9435 SDValue N1 = N->getOperand(1);
9436 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9437 std::swap(N0, N1);
9438 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9439 return SDValue();
9440
9441 SDValue ShAmt0 = N0.getOperand(1);
9442 if (ShAmt0.getValueType() != MVT::i8)
9443 return SDValue();
9444 SDValue ShAmt1 = N1.getOperand(1);
9445 if (ShAmt1.getValueType() != MVT::i8)
9446 return SDValue();
9447 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9448 ShAmt0 = ShAmt0.getOperand(0);
9449 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9450 ShAmt1 = ShAmt1.getOperand(0);
9451
9452 DebugLoc DL = N->getDebugLoc();
9453 unsigned Opc = X86ISD::SHLD;
9454 SDValue Op0 = N0.getOperand(0);
9455 SDValue Op1 = N1.getOperand(0);
9456 if (ShAmt0.getOpcode() == ISD::SUB) {
9457 Opc = X86ISD::SHRD;
9458 std::swap(Op0, Op1);
9459 std::swap(ShAmt0, ShAmt1);
9460 }
9461
9462 if (ShAmt1.getOpcode() == ISD::SUB) {
9463 SDValue Sum = ShAmt1.getOperand(0);
9464 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9465 if (SumC->getSExtValue() == 64 &&
9466 ShAmt1.getOperand(1) == ShAmt0)
9467 return DAG.getNode(Opc, DL, VT,
9468 Op0, Op1,
9469 DAG.getNode(ISD::TRUNCATE, DL,
9470 MVT::i8, ShAmt0));
9471 }
9472 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9473 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9474 if (ShAmt0C &&
9475 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9476 return DAG.getNode(Opc, DL, VT,
9477 N0.getOperand(0), N1.getOperand(0),
9478 DAG.getNode(ISD::TRUNCATE, DL,
9479 MVT::i8, ShAmt0));
9480 }
9481
9482 return SDValue();
9483}
9484
Chris Lattner149a4e52008-02-22 02:09:43 +00009485/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009486static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009487 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009488 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9489 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009490 // A preferable solution to the general problem is to figure out the right
9491 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009492
9493 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009494 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009495 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009496 if (VT.getSizeInBits() != 64)
9497 return SDValue();
9498
Devang Patel578efa92009-06-05 21:57:13 +00009499 const Function *F = DAG.getMachineFunction().getFunction();
9500 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009501 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009502 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009503 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009504 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009505 isa<LoadSDNode>(St->getValue()) &&
9506 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9507 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009508 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009509 LoadSDNode *Ld = 0;
9510 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009511 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009512 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009513 // Must be a store of a load. We currently handle two cases: the load
9514 // is a direct child, and it's under an intervening TokenFactor. It is
9515 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009516 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009517 Ld = cast<LoadSDNode>(St->getChain());
9518 else if (St->getValue().hasOneUse() &&
9519 ChainVal->getOpcode() == ISD::TokenFactor) {
9520 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009521 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009522 TokenFactorIndex = i;
9523 Ld = cast<LoadSDNode>(St->getValue());
9524 } else
9525 Ops.push_back(ChainVal->getOperand(i));
9526 }
9527 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009528
Evan Cheng536e6672009-03-12 05:59:15 +00009529 if (!Ld || !ISD::isNormalLoad(Ld))
9530 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009531
Evan Cheng536e6672009-03-12 05:59:15 +00009532 // If this is not the MMX case, i.e. we are just turning i64 load/store
9533 // into f64 load/store, avoid the transformation if there are multiple
9534 // uses of the loaded value.
9535 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9536 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009537
Evan Cheng536e6672009-03-12 05:59:15 +00009538 DebugLoc LdDL = Ld->getDebugLoc();
9539 DebugLoc StDL = N->getDebugLoc();
9540 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9541 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9542 // pair instead.
9543 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009544 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009545 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9546 Ld->getBasePtr(), Ld->getSrcValue(),
9547 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009548 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009549 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009550 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009551 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009552 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009553 Ops.size());
9554 }
Evan Cheng536e6672009-03-12 05:59:15 +00009555 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009556 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009557 St->isVolatile(), St->isNonTemporal(),
9558 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009559 }
Evan Cheng536e6672009-03-12 05:59:15 +00009560
9561 // Otherwise, lower to two pairs of 32-bit loads / stores.
9562 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009563 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9564 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009565
Owen Anderson825b72b2009-08-11 20:47:22 +00009566 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009567 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009568 Ld->isVolatile(), Ld->isNonTemporal(),
9569 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009570 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009571 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009572 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009573 MinAlign(Ld->getAlignment(), 4));
9574
9575 SDValue NewChain = LoLd.getValue(1);
9576 if (TokenFactorIndex != -1) {
9577 Ops.push_back(LoLd);
9578 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009579 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009580 Ops.size());
9581 }
9582
9583 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009584 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9585 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009586
9587 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9588 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009589 St->isVolatile(), St->isNonTemporal(),
9590 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009591 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9592 St->getSrcValue(),
9593 St->getSrcValueOffset() + 4,
9594 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009595 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009596 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009597 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009598 }
Dan Gohman475871a2008-07-27 21:46:04 +00009599 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009600}
9601
Chris Lattner6cf73262008-01-25 06:14:17 +00009602/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9603/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009604static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009605 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9606 // F[X]OR(0.0, x) -> x
9607 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009608 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9609 if (C->getValueAPF().isPosZero())
9610 return N->getOperand(1);
9611 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9612 if (C->getValueAPF().isPosZero())
9613 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009614 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009615}
9616
9617/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009618static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009619 // FAND(0.0, x) -> 0.0
9620 // FAND(x, 0.0) -> 0.0
9621 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9622 if (C->getValueAPF().isPosZero())
9623 return N->getOperand(0);
9624 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9625 if (C->getValueAPF().isPosZero())
9626 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009627 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009628}
9629
Dan Gohmane5af2d32009-01-29 01:59:02 +00009630static SDValue PerformBTCombine(SDNode *N,
9631 SelectionDAG &DAG,
9632 TargetLowering::DAGCombinerInfo &DCI) {
9633 // BT ignores high bits in the bit index operand.
9634 SDValue Op1 = N->getOperand(1);
9635 if (Op1.hasOneUse()) {
9636 unsigned BitWidth = Op1.getValueSizeInBits();
9637 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9638 APInt KnownZero, KnownOne;
9639 TargetLowering::TargetLoweringOpt TLO(DAG);
9640 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9641 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9642 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9643 DCI.CommitTargetLoweringOpt(TLO);
9644 }
9645 return SDValue();
9646}
Chris Lattner83e6c992006-10-04 06:57:07 +00009647
Eli Friedman7a5e5552009-06-07 06:52:44 +00009648static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9649 SDValue Op = N->getOperand(0);
9650 if (Op.getOpcode() == ISD::BIT_CONVERT)
9651 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009652 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009653 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009654 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009655 OpVT.getVectorElementType().getSizeInBits()) {
9656 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9657 }
9658 return SDValue();
9659}
9660
Owen Anderson99177002009-06-29 18:04:45 +00009661// On X86 and X86-64, atomic operations are lowered to locked instructions.
9662// Locked instructions, in turn, have implicit fence semantics (all memory
9663// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009664// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009665// fence-atomic-fence.
9666static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9667 SDValue atomic = N->getOperand(0);
9668 switch (atomic.getOpcode()) {
9669 case ISD::ATOMIC_CMP_SWAP:
9670 case ISD::ATOMIC_SWAP:
9671 case ISD::ATOMIC_LOAD_ADD:
9672 case ISD::ATOMIC_LOAD_SUB:
9673 case ISD::ATOMIC_LOAD_AND:
9674 case ISD::ATOMIC_LOAD_OR:
9675 case ISD::ATOMIC_LOAD_XOR:
9676 case ISD::ATOMIC_LOAD_NAND:
9677 case ISD::ATOMIC_LOAD_MIN:
9678 case ISD::ATOMIC_LOAD_MAX:
9679 case ISD::ATOMIC_LOAD_UMIN:
9680 case ISD::ATOMIC_LOAD_UMAX:
9681 break;
9682 default:
9683 return SDValue();
9684 }
Eric Christopherfd179292009-08-27 18:07:15 +00009685
Owen Anderson99177002009-06-29 18:04:45 +00009686 SDValue fence = atomic.getOperand(0);
9687 if (fence.getOpcode() != ISD::MEMBARRIER)
9688 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009689
Owen Anderson99177002009-06-29 18:04:45 +00009690 switch (atomic.getOpcode()) {
9691 case ISD::ATOMIC_CMP_SWAP:
9692 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9693 atomic.getOperand(1), atomic.getOperand(2),
9694 atomic.getOperand(3));
9695 case ISD::ATOMIC_SWAP:
9696 case ISD::ATOMIC_LOAD_ADD:
9697 case ISD::ATOMIC_LOAD_SUB:
9698 case ISD::ATOMIC_LOAD_AND:
9699 case ISD::ATOMIC_LOAD_OR:
9700 case ISD::ATOMIC_LOAD_XOR:
9701 case ISD::ATOMIC_LOAD_NAND:
9702 case ISD::ATOMIC_LOAD_MIN:
9703 case ISD::ATOMIC_LOAD_MAX:
9704 case ISD::ATOMIC_LOAD_UMIN:
9705 case ISD::ATOMIC_LOAD_UMAX:
9706 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9707 atomic.getOperand(1), atomic.getOperand(2));
9708 default:
9709 return SDValue();
9710 }
9711}
9712
Evan Cheng2e489c42009-12-16 00:53:11 +00009713static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9714 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9715 // (and (i32 x86isd::setcc_carry), 1)
9716 // This eliminates the zext. This transformation is necessary because
9717 // ISD::SETCC is always legalized to i8.
9718 DebugLoc dl = N->getDebugLoc();
9719 SDValue N0 = N->getOperand(0);
9720 EVT VT = N->getValueType(0);
9721 if (N0.getOpcode() == ISD::AND &&
9722 N0.hasOneUse() &&
9723 N0.getOperand(0).hasOneUse()) {
9724 SDValue N00 = N0.getOperand(0);
9725 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9726 return SDValue();
9727 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9728 if (!C || C->getZExtValue() != 1)
9729 return SDValue();
9730 return DAG.getNode(ISD::AND, dl, VT,
9731 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9732 N00.getOperand(0), N00.getOperand(1)),
9733 DAG.getConstant(1, VT));
9734 }
9735
9736 return SDValue();
9737}
9738
Dan Gohman475871a2008-07-27 21:46:04 +00009739SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009740 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009741 SelectionDAG &DAG = DCI.DAG;
9742 switch (N->getOpcode()) {
9743 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009744 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009745 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009746 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009747 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009748 case ISD::SHL:
9749 case ISD::SRA:
9750 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009751 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009752 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009753 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009754 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9755 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009756 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009757 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009758 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009759 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009760 }
9761
Dan Gohman475871a2008-07-27 21:46:04 +00009762 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009763}
9764
Evan Cheng60c07e12006-07-05 22:17:51 +00009765//===----------------------------------------------------------------------===//
9766// X86 Inline Assembly Support
9767//===----------------------------------------------------------------------===//
9768
Chris Lattnerb8105652009-07-20 17:51:36 +00009769static bool LowerToBSwap(CallInst *CI) {
9770 // FIXME: this should verify that we are targetting a 486 or better. If not,
9771 // we will turn this bswap into something that will be lowered to logical ops
9772 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9773 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009774
Chris Lattnerb8105652009-07-20 17:51:36 +00009775 // Verify this is a simple bswap.
9776 if (CI->getNumOperands() != 2 ||
9777 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009778 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +00009779 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009780
Chris Lattnerb8105652009-07-20 17:51:36 +00009781 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9782 if (!Ty || Ty->getBitWidth() % 16 != 0)
9783 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009784
Chris Lattnerb8105652009-07-20 17:51:36 +00009785 // Okay, we can do this xform, do so now.
9786 const Type *Tys[] = { Ty };
9787 Module *M = CI->getParent()->getParent()->getParent();
9788 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009789
Chris Lattnerb8105652009-07-20 17:51:36 +00009790 Value *Op = CI->getOperand(1);
9791 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009792
Chris Lattnerb8105652009-07-20 17:51:36 +00009793 CI->replaceAllUsesWith(Op);
9794 CI->eraseFromParent();
9795 return true;
9796}
9797
9798bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9799 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9800 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9801
9802 std::string AsmStr = IA->getAsmString();
9803
9804 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009805 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009806 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9807
9808 switch (AsmPieces.size()) {
9809 default: return false;
9810 case 1:
9811 AsmStr = AsmPieces[0];
9812 AsmPieces.clear();
9813 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9814
9815 // bswap $0
9816 if (AsmPieces.size() == 2 &&
9817 (AsmPieces[0] == "bswap" ||
9818 AsmPieces[0] == "bswapq" ||
9819 AsmPieces[0] == "bswapl") &&
9820 (AsmPieces[1] == "$0" ||
9821 AsmPieces[1] == "${0:q}")) {
9822 // No need to check constraints, nothing other than the equivalent of
9823 // "=r,0" would be valid here.
9824 return LowerToBSwap(CI);
9825 }
9826 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009827 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009828 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009829 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009830 AsmPieces[1] == "$$8," &&
9831 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009832 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9833 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +00009834 const std::string &Constraints = IA->getConstraintString();
9835 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +00009836 std::sort(AsmPieces.begin(), AsmPieces.end());
9837 if (AsmPieces.size() == 4 &&
9838 AsmPieces[0] == "~{cc}" &&
9839 AsmPieces[1] == "~{dirflag}" &&
9840 AsmPieces[2] == "~{flags}" &&
9841 AsmPieces[3] == "~{fpsr}") {
9842 return LowerToBSwap(CI);
9843 }
Chris Lattnerb8105652009-07-20 17:51:36 +00009844 }
9845 break;
9846 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009847 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +00009848 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009849 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
9850 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
9851 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009852 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +00009853 SplitString(AsmPieces[0], Words, " \t");
9854 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
9855 Words.clear();
9856 SplitString(AsmPieces[1], Words, " \t");
9857 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
9858 Words.clear();
9859 SplitString(AsmPieces[2], Words, " \t,");
9860 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
9861 Words[2] == "%edx") {
9862 return LowerToBSwap(CI);
9863 }
9864 }
9865 }
9866 }
9867 break;
9868 }
9869 return false;
9870}
9871
9872
9873
Chris Lattnerf4dff842006-07-11 02:54:03 +00009874/// getConstraintType - Given a constraint letter, return the type of
9875/// constraint it is for this target.
9876X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00009877X86TargetLowering::getConstraintType(const std::string &Constraint) const {
9878 if (Constraint.size() == 1) {
9879 switch (Constraint[0]) {
9880 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00009881 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009882 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00009883 case 'r':
9884 case 'R':
9885 case 'l':
9886 case 'q':
9887 case 'Q':
9888 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00009889 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00009890 case 'Y':
9891 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009892 case 'e':
9893 case 'Z':
9894 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00009895 default:
9896 break;
9897 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00009898 }
Chris Lattner4234f572007-03-25 02:14:49 +00009899 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00009900}
9901
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009902/// LowerXConstraint - try to replace an X constraint, which matches anything,
9903/// with another that has more specific requirements based on the type of the
9904/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00009905const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +00009906LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00009907 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
9908 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00009909 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009910 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00009911 return "Y";
9912 if (Subtarget->hasSSE1())
9913 return "x";
9914 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009915
Chris Lattner5e764232008-04-26 23:02:14 +00009916 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00009917}
9918
Chris Lattner48884cd2007-08-25 00:47:38 +00009919/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
9920/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00009921void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00009922 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00009923 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00009924 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00009925 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00009926 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009927
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009928 switch (Constraint) {
9929 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00009930 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00009931 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009932 if (C->getZExtValue() <= 31) {
9933 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009934 break;
9935 }
Devang Patel84f7fd22007-03-17 00:13:28 +00009936 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009937 return;
Evan Cheng364091e2008-09-22 23:57:37 +00009938 case 'J':
9939 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009940 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00009941 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9942 break;
9943 }
9944 }
9945 return;
9946 case 'K':
9947 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00009948 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00009949 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9950 break;
9951 }
9952 }
9953 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00009954 case 'N':
9955 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00009956 if (C->getZExtValue() <= 255) {
9957 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00009958 break;
9959 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00009960 }
Chris Lattner48884cd2007-08-25 00:47:38 +00009961 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00009962 case 'e': {
9963 // 32-bit signed value
9964 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9965 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009966 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9967 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009968 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009969 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +00009970 break;
9971 }
9972 // FIXME gcc accepts some relocatable values here too, but only in certain
9973 // memory models; it's complicated.
9974 }
9975 return;
9976 }
9977 case 'Z': {
9978 // 32-bit unsigned value
9979 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
9980 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +00009981 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
9982 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009983 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
9984 break;
9985 }
9986 }
9987 // FIXME gcc accepts some relocatable values here too, but only in certain
9988 // memory models; it's complicated.
9989 return;
9990 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00009991 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00009992 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00009993 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00009994 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +00009995 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00009996 break;
9997 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009998
Chris Lattnerdc43a882007-05-03 16:52:29 +00009999 // If we are in non-pic codegen mode, we allow the address of a global (with
10000 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010001 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010002 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010003
Chris Lattner49921962009-05-08 18:23:14 +000010004 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10005 while (1) {
10006 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10007 Offset += GA->getOffset();
10008 break;
10009 } else if (Op.getOpcode() == ISD::ADD) {
10010 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10011 Offset += C->getZExtValue();
10012 Op = Op.getOperand(0);
10013 continue;
10014 }
10015 } else if (Op.getOpcode() == ISD::SUB) {
10016 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10017 Offset += -C->getZExtValue();
10018 Op = Op.getOperand(0);
10019 continue;
10020 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010021 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010022
Chris Lattner49921962009-05-08 18:23:14 +000010023 // Otherwise, this isn't something we can handle, reject it.
10024 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010025 }
Eric Christopherfd179292009-08-27 18:07:15 +000010026
Chris Lattner36c25012009-07-10 07:34:39 +000010027 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010028 // If we require an extra load to get this address, as in PIC mode, we
10029 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010030 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10031 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010032 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010033
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010034 if (hasMemory)
10035 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10036 else
10037 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010038 Result = Op;
10039 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010040 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010041 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010042
Gabor Greifba36cb52008-08-28 21:40:38 +000010043 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010044 Ops.push_back(Result);
10045 return;
10046 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010047 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10048 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010049}
10050
Chris Lattner259e97c2006-01-31 19:43:35 +000010051std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010052getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010053 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010054 if (Constraint.size() == 1) {
10055 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010056 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010057 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010058 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10059 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010060 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010061 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10062 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10063 X86::R10D,X86::R11D,X86::R12D,
10064 X86::R13D,X86::R14D,X86::R15D,
10065 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010066 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010067 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10068 X86::SI, X86::DI, X86::R8W,X86::R9W,
10069 X86::R10W,X86::R11W,X86::R12W,
10070 X86::R13W,X86::R14W,X86::R15W,
10071 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010072 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010073 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10074 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10075 X86::R10B,X86::R11B,X86::R12B,
10076 X86::R13B,X86::R14B,X86::R15B,
10077 X86::BPL, X86::SPL, 0);
10078
Owen Anderson825b72b2009-08-11 20:47:22 +000010079 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010080 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10081 X86::RSI, X86::RDI, X86::R8, X86::R9,
10082 X86::R10, X86::R11, X86::R12,
10083 X86::R13, X86::R14, X86::R15,
10084 X86::RBP, X86::RSP, 0);
10085
10086 break;
10087 }
Eric Christopherfd179292009-08-27 18:07:15 +000010088 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010089 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010090 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010091 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010092 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010093 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010094 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010095 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010096 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010097 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10098 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010099 }
10100 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010101
Chris Lattner1efa40f2006-02-22 00:56:39 +000010102 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010103}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010104
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010105std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010106X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010107 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010108 // First, see if this is a constraint that directly corresponds to an LLVM
10109 // register class.
10110 if (Constraint.size() == 1) {
10111 // GCC Constraint Letters
10112 switch (Constraint[0]) {
10113 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010114 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010115 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010116 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010117 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010118 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010119 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010120 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010121 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010122 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010123 case 'R': // LEGACY_REGS
10124 if (VT == MVT::i8)
10125 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10126 if (VT == MVT::i16)
10127 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10128 if (VT == MVT::i32 || !Subtarget->is64Bit())
10129 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10130 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010131 case 'f': // FP Stack registers.
10132 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10133 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010134 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010135 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010136 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010137 return std::make_pair(0U, X86::RFP64RegisterClass);
10138 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010139 case 'y': // MMX_REGS if MMX allowed.
10140 if (!Subtarget->hasMMX()) break;
10141 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010142 case 'Y': // SSE_REGS if SSE2 allowed
10143 if (!Subtarget->hasSSE2()) break;
10144 // FALL THROUGH.
10145 case 'x': // SSE_REGS if SSE1 allowed
10146 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010147
Owen Anderson825b72b2009-08-11 20:47:22 +000010148 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010149 default: break;
10150 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010151 case MVT::f32:
10152 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010153 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010154 case MVT::f64:
10155 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010156 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010157 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010158 case MVT::v16i8:
10159 case MVT::v8i16:
10160 case MVT::v4i32:
10161 case MVT::v2i64:
10162 case MVT::v4f32:
10163 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010164 return std::make_pair(0U, X86::VR128RegisterClass);
10165 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010166 break;
10167 }
10168 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010169
Chris Lattnerf76d1802006-07-31 23:26:50 +000010170 // Use the default implementation in TargetLowering to convert the register
10171 // constraint into a member of a register class.
10172 std::pair<unsigned, const TargetRegisterClass*> Res;
10173 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010174
10175 // Not found as a standard register?
10176 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010177 // Map st(0) -> st(7) -> ST0
10178 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10179 tolower(Constraint[1]) == 's' &&
10180 tolower(Constraint[2]) == 't' &&
10181 Constraint[3] == '(' &&
10182 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10183 Constraint[5] == ')' &&
10184 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010185
Chris Lattner56d77c72009-09-13 22:41:48 +000010186 Res.first = X86::ST0+Constraint[4]-'0';
10187 Res.second = X86::RFP80RegisterClass;
10188 return Res;
10189 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010190
Chris Lattner56d77c72009-09-13 22:41:48 +000010191 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010192 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010193 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010194 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010195 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010196 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010197
10198 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010199 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010200 Res.first = X86::EFLAGS;
10201 Res.second = X86::CCRRegisterClass;
10202 return Res;
10203 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010204
Dale Johannesen330169f2008-11-13 21:52:36 +000010205 // 'A' means EAX + EDX.
10206 if (Constraint == "A") {
10207 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010208 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010209 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010210 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010211 return Res;
10212 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010213
Chris Lattnerf76d1802006-07-31 23:26:50 +000010214 // Otherwise, check to see if this is a register class of the wrong value
10215 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10216 // turn into {ax},{dx}.
10217 if (Res.second->hasType(VT))
10218 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010219
Chris Lattnerf76d1802006-07-31 23:26:50 +000010220 // All of the single-register GCC register classes map their values onto
10221 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10222 // really want an 8-bit or 32-bit register, map to the appropriate register
10223 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010224 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010225 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010226 unsigned DestReg = 0;
10227 switch (Res.first) {
10228 default: break;
10229 case X86::AX: DestReg = X86::AL; break;
10230 case X86::DX: DestReg = X86::DL; break;
10231 case X86::CX: DestReg = X86::CL; break;
10232 case X86::BX: DestReg = X86::BL; break;
10233 }
10234 if (DestReg) {
10235 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010236 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010237 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010238 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010239 unsigned DestReg = 0;
10240 switch (Res.first) {
10241 default: break;
10242 case X86::AX: DestReg = X86::EAX; break;
10243 case X86::DX: DestReg = X86::EDX; break;
10244 case X86::CX: DestReg = X86::ECX; break;
10245 case X86::BX: DestReg = X86::EBX; break;
10246 case X86::SI: DestReg = X86::ESI; break;
10247 case X86::DI: DestReg = X86::EDI; break;
10248 case X86::BP: DestReg = X86::EBP; break;
10249 case X86::SP: DestReg = X86::ESP; break;
10250 }
10251 if (DestReg) {
10252 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010253 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010254 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010255 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010256 unsigned DestReg = 0;
10257 switch (Res.first) {
10258 default: break;
10259 case X86::AX: DestReg = X86::RAX; break;
10260 case X86::DX: DestReg = X86::RDX; break;
10261 case X86::CX: DestReg = X86::RCX; break;
10262 case X86::BX: DestReg = X86::RBX; break;
10263 case X86::SI: DestReg = X86::RSI; break;
10264 case X86::DI: DestReg = X86::RDI; break;
10265 case X86::BP: DestReg = X86::RBP; break;
10266 case X86::SP: DestReg = X86::RSP; break;
10267 }
10268 if (DestReg) {
10269 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010270 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010271 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010272 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010273 } else if (Res.second == X86::FR32RegisterClass ||
10274 Res.second == X86::FR64RegisterClass ||
10275 Res.second == X86::VR128RegisterClass) {
10276 // Handle references to XMM physical registers that got mapped into the
10277 // wrong class. This can happen with constraints like {xmm0} where the
10278 // target independent register mapper will just pick the first match it can
10279 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010280 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010281 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010282 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010283 Res.second = X86::FR64RegisterClass;
10284 else if (X86::VR128RegisterClass->hasType(VT))
10285 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010286 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010287
Chris Lattnerf76d1802006-07-31 23:26:50 +000010288 return Res;
10289}