blob: 5f29ba60c1976c3a712ba65f7454dc01193f9953 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000022#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000024#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000025#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000026#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000027#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000028#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000029#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000038#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000039#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000046#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000048#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000051#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000053using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Mon P Wang3c81d352008-11-23 04:37:22 +000057static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000058DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000059
Dan Gohman2f67df72009-09-03 17:18:51 +000060// Disable16Bit - 16-bit operations typically have a larger encoding than
61// corresponding 32-bit instructions, and 16-bit code is slow on some
62// processors. This is an experimental flag to disable 16-bit operations
63// (which forces them to be Legalized to 32-bit operations).
64static cl::opt<bool>
65Disable16Bit("disable-16bit", cl::Hidden,
66 cl::desc("Disable use of 16-bit instructions"));
Evan Cheng64b7bf72010-04-16 06:14:10 +000067static cl::opt<bool>
68Promote16Bit("promote-16bit", cl::Hidden,
69 cl::desc("Promote 16-bit instructions"));
Dan Gohman2f67df72009-09-03 17:18:51 +000070
Evan Cheng10e86422008-04-25 19:11:04 +000071// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000072static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000073 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000074
Chris Lattnerf0144122009-07-28 03:13:23 +000075static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
76 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
77 default: llvm_unreachable("unknown subtarget type");
78 case X86Subtarget::isDarwin:
Bill Wendling757e75b2010-03-15 19:04:37 +000079 if (TM.getSubtarget<X86Subtarget>().is64Bit())
80 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000081 return new TargetLoweringObjectFileMachO();
Chris Lattnerf0144122009-07-28 03:13:23 +000082 case X86Subtarget::isELF:
Anton Korobeynikov9184b252010-02-15 22:35:59 +000083 if (TM.getSubtarget<X86Subtarget>().is64Bit())
84 return new X8664_ELFTargetObjectFile(TM);
85 return new X8632_ELFTargetObjectFile(TM);
Chris Lattnerf0144122009-07-28 03:13:23 +000086 case X86Subtarget::isMingw:
87 case X86Subtarget::isCygwin:
88 case X86Subtarget::isWindows:
89 return new TargetLoweringObjectFileCOFF();
90 }
Chris Lattnerf0144122009-07-28 03:13:23 +000091}
92
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000093X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000094 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000095 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000096 X86ScalarSSEf64 = Subtarget->hasSSE2();
97 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000098 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000099
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000100 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000101 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000102
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000103 // Set up the TargetLowering object.
104
105 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +0000107 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +0000108 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000109 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000110
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000111 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000112 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000113 setUseUnderscoreSetJmp(false);
114 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000115 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000116 // MS runtime is weird: it exports _setjmp, but longjmp!
117 setUseUnderscoreSetJmp(true);
118 setUseUnderscoreLongJmp(false);
119 } else {
120 setUseUnderscoreSetJmp(true);
121 setUseUnderscoreLongJmp(true);
122 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000123
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000124 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman2f67df72009-09-03 17:18:51 +0000126 if (!Disable16Bit)
127 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000129 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000131
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000133
Scott Michelfdc40a02009-02-17 22:15:04 +0000134 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000136 if (!Disable16Bit)
137 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000139 if (!Disable16Bit)
140 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
142 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000143
144 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
146 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
147 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
148 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
149 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
150 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000151
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000152 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
153 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
155 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
156 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000157
Evan Cheng25ab6902006-09-08 06:48:29 +0000158 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
160 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000161 } else if (!UseSoftFloat) {
162 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000163 // We have an impenetrably clever algorithm for ui64->double only.
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000165 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000166 // We have an algorithm for SSE2, and we turn this into a 64-bit
167 // FILD for other targets.
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000169 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000170
171 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
172 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
174 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000175
Devang Patel6a784892009-06-05 18:48:29 +0000176 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000177 // SSE has no i16 to fp conversion, only i32
178 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000179 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000180 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000181 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000182 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
184 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000185 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000186 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
188 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000189 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000190
Dale Johannesen73328d12007-09-19 23:55:34 +0000191 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
192 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
194 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000195
Evan Cheng02568ff2006-01-30 22:13:22 +0000196 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
197 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
199 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000200
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000201 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000203 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000205 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
207 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000208 }
209
210 // Handle FP_TO_UINT by promoting the destination to a larger signed
211 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
213 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
214 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000215
Evan Cheng25ab6902006-09-08 06:48:29 +0000216 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000217 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
218 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000219 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000220 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000221 // Expand FP_TO_UINT into a select.
222 // FIXME: We would like to use a Custom expander here eventually to do
223 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000224 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000225 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000226 // With SSE3 we can use fisttpll to convert to a signed i64; without
227 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000228 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000230
Chris Lattner399610a2006-12-05 18:22:22 +0000231 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000232 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000233 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
234 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattnerf3597a12006-12-05 18:45:06 +0000235 }
Chris Lattner21f66852005-12-23 05:15:23 +0000236
Dan Gohmanb00ee212008-02-18 19:34:53 +0000237 // Scalar integer divide and remainder are lowered to use operations that
238 // produce two results, to match the available instructions. This exposes
239 // the two-result form to trivial CSE, which is able to combine x/y and x%y
240 // into a single instruction.
241 //
242 // Scalar integer multiply-high is also lowered to use two-result
243 // operations, to match the available instructions. However, plain multiply
244 // (low) operations are left as Legal, as there are single-result
245 // instructions for this in x86. Using the two-result multiply instructions
246 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000247 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
248 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
249 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
250 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
251 setOperationAction(ISD::SREM , MVT::i8 , Expand);
252 setOperationAction(ISD::UREM , MVT::i8 , Expand);
253 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
254 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
255 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
256 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
257 setOperationAction(ISD::SREM , MVT::i16 , Expand);
258 setOperationAction(ISD::UREM , MVT::i16 , Expand);
259 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
260 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
261 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
262 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
263 setOperationAction(ISD::SREM , MVT::i32 , Expand);
264 setOperationAction(ISD::UREM , MVT::i32 , Expand);
265 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
266 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
267 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
268 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
269 setOperationAction(ISD::SREM , MVT::i64 , Expand);
270 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000271
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
273 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
274 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
275 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000276 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000277 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
278 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
279 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
280 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
281 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
282 setOperationAction(ISD::FREM , MVT::f32 , Expand);
283 setOperationAction(ISD::FREM , MVT::f64 , Expand);
284 setOperationAction(ISD::FREM , MVT::f80 , Expand);
285 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000286
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
288 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
289 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
290 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Dan Gohman2f67df72009-09-03 17:18:51 +0000291 if (Disable16Bit) {
292 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
293 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
294 } else {
295 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
296 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
297 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000298 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
299 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
300 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000301 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000302 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
303 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
304 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000305 }
306
Owen Anderson825b72b2009-08-11 20:47:22 +0000307 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
308 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000309
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000310 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000311 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000312 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000313 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000314 if (Disable16Bit)
315 setOperationAction(ISD::SELECT , MVT::i16 , Expand);
316 else
317 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000318 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
319 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
320 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
321 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
322 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman2f67df72009-09-03 17:18:51 +0000323 if (Disable16Bit)
324 setOperationAction(ISD::SETCC , MVT::i16 , Expand);
325 else
326 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000327 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
328 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
329 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
330 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000331 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000332 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
333 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000334 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000335 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000336
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000337 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
339 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
340 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
341 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000342 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000343 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
344 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000345 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000346 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000347 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
348 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
349 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
350 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000351 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000352 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000353 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
355 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
356 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000357 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
359 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
360 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000361 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000362
Evan Chengd2cde682008-03-10 19:38:10 +0000363 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000364 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000365
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000366 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000368
Mon P Wang63307c32008-05-05 19:05:59 +0000369 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
371 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
372 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
373 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000374
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
376 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
377 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
378 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000379
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000380 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
382 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
383 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
384 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
385 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
386 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
387 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000388 }
389
Evan Cheng3c992d22006-03-07 02:02:57 +0000390 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000391 if (!Subtarget->isTargetDarwin() &&
392 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000393 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000395 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000396
Owen Anderson825b72b2009-08-11 20:47:22 +0000397 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
398 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
399 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
400 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000401 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000402 setExceptionPointerRegister(X86::RAX);
403 setExceptionSelectorRegister(X86::RDX);
404 } else {
405 setExceptionPointerRegister(X86::EAX);
406 setExceptionSelectorRegister(X86::EDX);
407 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
409 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000410
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000412
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000414
Nate Begemanacc398c2006-01-25 18:21:52 +0000415 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 setOperationAction(ISD::VASTART , MVT::Other, Custom);
417 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000418 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::VAARG , MVT::Other, Custom);
420 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000421 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::VAARG , MVT::Other, Expand);
423 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000424 }
Evan Chengae642192007-03-02 23:16:35 +0000425
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
427 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000428 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000429 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000430 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000432 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000433 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000434
Evan Chengc7ce29b2009-02-13 22:36:38 +0000435 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000436 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000438 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
439 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000440
Evan Cheng223547a2006-01-31 22:28:30 +0000441 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000442 setOperationAction(ISD::FABS , MVT::f64, Custom);
443 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000444
445 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 setOperationAction(ISD::FNEG , MVT::f64, Custom);
447 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000448
Evan Cheng68c47cb2007-01-05 07:55:56 +0000449 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
451 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000452
Evan Chengd25e9e82006-02-02 00:28:23 +0000453 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000454 setOperationAction(ISD::FSIN , MVT::f64, Expand);
455 setOperationAction(ISD::FCOS , MVT::f64, Expand);
456 setOperationAction(ISD::FSIN , MVT::f32, Expand);
457 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000458
Chris Lattnera54aa942006-01-29 06:26:08 +0000459 // Expand FP immediates into loads from the stack, except for the special
460 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461 addLegalFPImmediate(APFloat(+0.0)); // xorpd
462 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000463 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464 // Use SSE for f32, x87 for f64.
465 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
467 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000468
469 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000471
472 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000474
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000476
477 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000478 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
479 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000480
481 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000482 setOperationAction(ISD::FSIN , MVT::f32, Expand);
483 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000484
Nate Begemane1795842008-02-14 08:57:00 +0000485 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000486 addLegalFPImmediate(APFloat(+0.0f)); // xorps
487 addLegalFPImmediate(APFloat(+0.0)); // FLD0
488 addLegalFPImmediate(APFloat(+1.0)); // FLD1
489 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
490 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
491
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000492 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000493 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
494 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000495 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000496 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000497 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000498 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000499 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
500 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000501
Owen Anderson825b72b2009-08-11 20:47:22 +0000502 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
503 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
504 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
505 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000506
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000507 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
509 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000510 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000511 addLegalFPImmediate(APFloat(+0.0)); // FLD0
512 addLegalFPImmediate(APFloat(+1.0)); // FLD1
513 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
514 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000515 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
516 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
517 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
518 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000519 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000520
Dale Johannesen59a58732007-08-05 18:49:15 +0000521 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000522 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000523 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
524 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
525 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000526 {
527 bool ignored;
528 APFloat TmpFlt(+0.0);
529 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
530 &ignored);
531 addLegalFPImmediate(TmpFlt); // FLD0
532 TmpFlt.changeSign();
533 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
534 APFloat TmpFlt2(+1.0);
535 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
536 &ignored);
537 addLegalFPImmediate(TmpFlt2); // FLD1
538 TmpFlt2.changeSign();
539 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
540 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000541
Evan Chengc7ce29b2009-02-13 22:36:38 +0000542 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
544 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000545 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000546 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000547
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000548 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000549 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
550 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
551 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000552
Owen Anderson825b72b2009-08-11 20:47:22 +0000553 setOperationAction(ISD::FLOG, MVT::f80, Expand);
554 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
555 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
556 setOperationAction(ISD::FEXP, MVT::f80, Expand);
557 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000558
Mon P Wangf007a8b2008-11-06 05:31:54 +0000559 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000560 // (for widening) or expand (for scalarization). Then we will selectively
561 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000562 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
563 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
564 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
579 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
580 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
605 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
606 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
609 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
610 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
611 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000612 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000613 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
614 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
615 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
616 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
617 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
618 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
619 setTruncStoreAction((MVT::SimpleValueType)VT,
620 (MVT::SimpleValueType)InnerVT, Expand);
621 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
622 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
623 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000624 }
625
Evan Chengc7ce29b2009-02-13 22:36:38 +0000626 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
627 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000628 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
630 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
631 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
632 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
633 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000634
Owen Anderson825b72b2009-08-11 20:47:22 +0000635 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
636 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
637 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
638 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000639
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
641 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
642 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
643 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000644
Owen Anderson825b72b2009-08-11 20:47:22 +0000645 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
646 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000647
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::AND, MVT::v8i8, Promote);
649 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
650 setOperationAction(ISD::AND, MVT::v4i16, Promote);
651 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
652 setOperationAction(ISD::AND, MVT::v2i32, Promote);
653 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
654 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000655
Owen Anderson825b72b2009-08-11 20:47:22 +0000656 setOperationAction(ISD::OR, MVT::v8i8, Promote);
657 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
658 setOperationAction(ISD::OR, MVT::v4i16, Promote);
659 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
660 setOperationAction(ISD::OR, MVT::v2i32, Promote);
661 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
662 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000663
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
665 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
666 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
667 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
668 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
669 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
670 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000671
Owen Anderson825b72b2009-08-11 20:47:22 +0000672 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
673 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
674 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
675 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
676 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
677 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
678 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
679 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
680 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000681
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
683 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
684 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
685 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
686 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000687
Owen Anderson825b72b2009-08-11 20:47:22 +0000688 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
689 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
690 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
691 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000692
Owen Anderson825b72b2009-08-11 20:47:22 +0000693 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
694 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
695 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
696 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000697
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000699
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
701 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
702 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
703 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
704 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
706 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000707 }
708
Evan Cheng92722532009-03-26 23:06:32 +0000709 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000711
Owen Anderson825b72b2009-08-11 20:47:22 +0000712 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
713 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
714 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
715 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
716 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
717 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
718 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
719 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
720 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
722 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
723 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000724 }
725
Evan Cheng92722532009-03-26 23:06:32 +0000726 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000727 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000728
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000729 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
730 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
732 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
733 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
734 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000735
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
737 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
738 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
739 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
740 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
741 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
742 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
743 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
744 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
745 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
746 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
747 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
748 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
749 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
750 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
751 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000752
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
754 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
755 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
756 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000757
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
759 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
760 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
761 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
762 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000763
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000764 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
765 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
766 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
767 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
768 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
769
Evan Cheng2c3ae372006-04-12 21:21:57 +0000770 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
772 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000773 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000774 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000775 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000776 // Do not attempt to custom lower non-128-bit vectors
777 if (!VT.is128BitVector())
778 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000779 setOperationAction(ISD::BUILD_VECTOR,
780 VT.getSimpleVT().SimpleTy, Custom);
781 setOperationAction(ISD::VECTOR_SHUFFLE,
782 VT.getSimpleVT().SimpleTy, Custom);
783 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
784 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000785 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000786
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
788 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
789 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
790 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
791 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
792 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000793
Nate Begemancdd1eec2008-02-12 22:51:28 +0000794 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
796 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000797 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000798
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000799 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
801 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000802 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000803
804 // Do not attempt to promote non-128-bit vectors
805 if (!VT.is128BitVector()) {
806 continue;
807 }
Eric Christopher4bd24c22010-03-30 01:04:59 +0000808
Owen Andersond6662ad2009-08-10 20:46:15 +0000809 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000811 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000812 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000813 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000815 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000817 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000818 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000819 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000820
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000822
Evan Cheng2c3ae372006-04-12 21:21:57 +0000823 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
825 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
826 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
827 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000828
Owen Anderson825b72b2009-08-11 20:47:22 +0000829 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
830 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000831 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000832 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
833 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000834 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000835 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000836
Nate Begeman14d12ca2008-02-11 04:19:36 +0000837 if (Subtarget->hasSSE41()) {
838 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000839 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000840
841 // i8 and i16 vectors are custom , because the source register and source
842 // source memory operand types are not the same width. f32 vectors are
843 // custom since the immediate controlling the insert encodes additional
844 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000845 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
847 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
848 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000849
Owen Anderson825b72b2009-08-11 20:47:22 +0000850 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
851 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
852 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
853 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000854
855 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000856 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
857 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000858 }
859 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000860
Nate Begeman30a0de92008-07-17 16:51:19 +0000861 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000863 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000864
David Greene9b9838d2009-06-29 16:47:10 +0000865 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
867 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
868 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
869 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
872 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
873 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
874 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
875 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
876 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
877 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
878 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
879 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
880 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
881 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
882 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
883 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
884 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
885 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000886
887 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000888 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
889 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
890 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
891 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
892 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
893 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
894 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
895 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
896 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
897 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
898 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
899 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
900 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
901 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000902
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
904 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
905 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
906 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000907
Owen Anderson825b72b2009-08-11 20:47:22 +0000908 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
909 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
910 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
911 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
912 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000913
Owen Anderson825b72b2009-08-11 20:47:22 +0000914 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
915 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
916 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
917 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
918 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
919 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000920
921#if 0
922 // Not sure we want to do this since there are no 256-bit integer
923 // operations in AVX
924
925 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
926 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000927 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
928 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000929
930 // Do not attempt to custom lower non-power-of-2 vectors
931 if (!isPowerOf2_32(VT.getVectorNumElements()))
932 continue;
933
934 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
935 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
936 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
937 }
938
939 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
941 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000942 }
David Greene9b9838d2009-06-29 16:47:10 +0000943#endif
944
945#if 0
946 // Not sure we want to do this since there are no 256-bit integer
947 // operations in AVX
948
949 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
950 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000951 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
952 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000953
954 if (!VT.is256BitVector()) {
955 continue;
956 }
957 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000958 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000959 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000961 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000962 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000963 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000964 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000965 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000966 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000967 }
968
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000970#endif
971 }
972
Evan Cheng6be2c582006-04-05 23:38:46 +0000973 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000975
Bill Wendling74c37652008-12-09 22:08:41 +0000976 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000977 setOperationAction(ISD::SADDO, MVT::i32, Custom);
978 setOperationAction(ISD::SADDO, MVT::i64, Custom);
979 setOperationAction(ISD::UADDO, MVT::i32, Custom);
980 setOperationAction(ISD::UADDO, MVT::i64, Custom);
981 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
982 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
983 setOperationAction(ISD::USUBO, MVT::i32, Custom);
984 setOperationAction(ISD::USUBO, MVT::i64, Custom);
985 setOperationAction(ISD::SMULO, MVT::i32, Custom);
986 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000987
Evan Chengd54f2d52009-03-31 19:38:51 +0000988 if (!Subtarget->is64Bit()) {
989 // These libcalls are not available in 32-bit.
990 setLibcallName(RTLIB::SHL_I128, 0);
991 setLibcallName(RTLIB::SRL_I128, 0);
992 setLibcallName(RTLIB::SRA_I128, 0);
993 }
994
Evan Cheng206ee9d2006-07-07 08:33:52 +0000995 // We have target-specific dag combine patterns for the following nodes:
996 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000997 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000998 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000999 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001000 setTargetDAGCombine(ISD::SHL);
1001 setTargetDAGCombine(ISD::SRA);
1002 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001003 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +00001004 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +00001005 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +00001006 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001007 if (Subtarget->is64Bit())
1008 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001009
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001010 computeRegisterProperties();
1011
Evan Cheng87ed7162006-02-14 08:25:08 +00001012 // FIXME: These should be based on subtarget info. Plus, the values should
1013 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001014 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +00001015 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +00001016 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001017 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001018 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001019}
1020
Scott Michel5b8f82e2008-03-10 15:42:14 +00001021
Owen Anderson825b72b2009-08-11 20:47:22 +00001022MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1023 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001024}
1025
1026
Evan Cheng29286502008-01-23 23:17:41 +00001027/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1028/// the desired ByVal argument alignment.
1029static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1030 if (MaxAlign == 16)
1031 return;
1032 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1033 if (VTy->getBitWidth() == 128)
1034 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001035 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1036 unsigned EltAlign = 0;
1037 getMaxByValAlign(ATy->getElementType(), EltAlign);
1038 if (EltAlign > MaxAlign)
1039 MaxAlign = EltAlign;
1040 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1041 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1042 unsigned EltAlign = 0;
1043 getMaxByValAlign(STy->getElementType(i), EltAlign);
1044 if (EltAlign > MaxAlign)
1045 MaxAlign = EltAlign;
1046 if (MaxAlign == 16)
1047 break;
1048 }
1049 }
1050 return;
1051}
1052
1053/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1054/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001055/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1056/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001057unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001058 if (Subtarget->is64Bit()) {
1059 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001060 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001061 if (TyAlign > 8)
1062 return TyAlign;
1063 return 8;
1064 }
1065
Evan Cheng29286502008-01-23 23:17:41 +00001066 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001067 if (Subtarget->hasSSE1())
1068 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001069 return Align;
1070}
Chris Lattner2b02a442007-02-25 08:29:00 +00001071
Evan Chengf0df0312008-05-15 08:39:06 +00001072/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001073/// and store operations as a result of memset, memcpy, and memmove
1074/// lowering. If DstAlign is zero that means it's safe to destination
1075/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1076/// means there isn't a need to check it against alignment requirement,
1077/// probably because the source does not need to be loaded. If
1078/// 'NonScalarIntSafe' is true, that means it's safe to return a
1079/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1080/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1081/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001082/// It returns EVT::Other if the type should be determined using generic
1083/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001084EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001085X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1086 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001087 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001088 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001089 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001090 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1091 // linux. This is because the stack realignment code can't handle certain
1092 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001093 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001094 if (NonScalarIntSafe &&
1095 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001096 if (Size >= 16 &&
1097 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001098 ((DstAlign == 0 || DstAlign >= 16) &&
1099 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001100 Subtarget->getStackAlignment() >= 16) {
1101 if (Subtarget->hasSSE2())
1102 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001103 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001104 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001105 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001106 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001107 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001108 Subtarget->hasSSE2()) {
1109 // Do not use f64 to lower memcpy if source is string constant. It's
1110 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001111 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001112 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001113 }
Evan Chengf0df0312008-05-15 08:39:06 +00001114 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001115 return MVT::i64;
1116 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001117}
1118
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001119/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1120/// current function. The returned value is a member of the
1121/// MachineJumpTableInfo::JTEntryKind enum.
1122unsigned X86TargetLowering::getJumpTableEncoding() const {
1123 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1124 // symbol.
1125 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1126 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001127 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001128
1129 // Otherwise, use the normal jump table encoding heuristics.
1130 return TargetLowering::getJumpTableEncoding();
1131}
1132
Chris Lattner589c6f62010-01-26 06:28:43 +00001133/// getPICBaseSymbol - Return the X86-32 PIC base.
1134MCSymbol *
1135X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1136 MCContext &Ctx) const {
1137 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001138 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1139 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001140}
1141
1142
Chris Lattnerc64daab2010-01-26 05:02:42 +00001143const MCExpr *
1144X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1145 const MachineBasicBlock *MBB,
1146 unsigned uid,MCContext &Ctx) const{
1147 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1148 Subtarget->isPICStyleGOT());
1149 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1150 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001151 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1152 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001153}
1154
Evan Chengcc415862007-11-09 01:32:10 +00001155/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1156/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001157SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001158 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001159 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001160 // This doesn't have DebugLoc associated with it, but is not really the
1161 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001162 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001163 return Table;
1164}
1165
Chris Lattner589c6f62010-01-26 06:28:43 +00001166/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1167/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1168/// MCExpr.
1169const MCExpr *X86TargetLowering::
1170getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1171 MCContext &Ctx) const {
1172 // X86-64 uses RIP relative addressing based on the jump table label.
1173 if (Subtarget->isPICStyleRIPRel())
1174 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1175
1176 // Otherwise, the reference is relative to the PIC base.
1177 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1178}
1179
Bill Wendlingb4202b82009-07-01 18:50:55 +00001180/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001181unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001182 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001183}
1184
Chris Lattner2b02a442007-02-25 08:29:00 +00001185//===----------------------------------------------------------------------===//
1186// Return Value Calling Convention Implementation
1187//===----------------------------------------------------------------------===//
1188
Chris Lattner59ed56b2007-02-28 04:55:35 +00001189#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001190
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001191bool
1192X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1193 const SmallVectorImpl<EVT> &OutTys,
1194 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
1195 SelectionDAG &DAG) {
1196 SmallVector<CCValAssign, 16> RVLocs;
1197 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1198 RVLocs, *DAG.getContext());
1199 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1200}
1201
Dan Gohman98ca4f22009-08-05 01:29:28 +00001202SDValue
1203X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001204 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001205 const SmallVectorImpl<ISD::OutputArg> &Outs,
1206 DebugLoc dl, SelectionDAG &DAG) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001207 MachineFunction &MF = DAG.getMachineFunction();
1208 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001209
Chris Lattner9774c912007-02-27 05:28:59 +00001210 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001211 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1212 RVLocs, *DAG.getContext());
1213 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001214
Evan Chengdcea1632010-02-04 02:40:39 +00001215 // Add the regs to the liveout set for the function.
1216 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1217 for (unsigned i = 0; i != RVLocs.size(); ++i)
1218 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1219 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001220
Dan Gohman475871a2008-07-27 21:46:04 +00001221 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001222
Dan Gohman475871a2008-07-27 21:46:04 +00001223 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001224 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1225 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001226 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1227 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001228
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001229 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001230 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1231 CCValAssign &VA = RVLocs[i];
1232 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001233 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001234
Chris Lattner447ff682008-03-11 03:23:40 +00001235 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1236 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001237 if (VA.getLocReg() == X86::ST0 ||
1238 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001239 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1240 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001241 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001243 RetOps.push_back(ValToCopy);
1244 // Don't emit a copytoreg.
1245 continue;
1246 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001247
Evan Cheng242b38b2009-02-23 09:03:22 +00001248 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1249 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001250 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001251 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001252 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001253 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001254 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001255 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001256 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001257 }
1258
Dale Johannesendd64c412009-02-04 00:33:20 +00001259 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001260 Flag = Chain.getValue(1);
1261 }
Dan Gohman61a92132008-04-21 23:59:07 +00001262
1263 // The x86-64 ABI for returning structs by value requires that we copy
1264 // the sret argument into %rax for the return. We saved the argument into
1265 // a virtual register in the entry block, so now we copy the value out
1266 // and into %rax.
1267 if (Subtarget->is64Bit() &&
1268 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1269 MachineFunction &MF = DAG.getMachineFunction();
1270 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1271 unsigned Reg = FuncInfo->getSRetReturnReg();
1272 if (!Reg) {
Evan Chengdcea1632010-02-04 02:40:39 +00001273 Reg = MRI.createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001274 FuncInfo->setSRetReturnReg(Reg);
1275 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001276 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001277
Dale Johannesendd64c412009-02-04 00:33:20 +00001278 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001279 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001280
1281 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001282 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001283 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001284
Chris Lattner447ff682008-03-11 03:23:40 +00001285 RetOps[0] = Chain; // Update chain.
1286
1287 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001289 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001290
1291 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001292 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001293}
1294
Dan Gohman98ca4f22009-08-05 01:29:28 +00001295/// LowerCallResult - Lower the result values of a call into the
1296/// appropriate copies out of appropriate physical registers.
1297///
1298SDValue
1299X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001300 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001301 const SmallVectorImpl<ISD::InputArg> &Ins,
1302 DebugLoc dl, SelectionDAG &DAG,
1303 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001304
Chris Lattnere32bbf62007-02-28 07:09:55 +00001305 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001306 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001307 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001308 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001309 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001310 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001311
Chris Lattner3085e152007-02-25 08:59:22 +00001312 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001313 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001314 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001315 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001316
Torok Edwin3f142c32009-02-01 18:15:56 +00001317 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001319 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001320 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001321 }
1322
Chris Lattner8e6da152008-03-10 21:08:41 +00001323 // If this is a call to a function that returns an fp value on the floating
1324 // point stack, but where we prefer to use the value in xmm registers, copy
1325 // 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 +00001326 if ((VA.getLocReg() == X86::ST0 ||
1327 VA.getLocReg() == X86::ST1) &&
1328 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001329 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001330 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001331
Evan Cheng79fb3b42009-02-20 20:43:02 +00001332 SDValue Val;
1333 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001334 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1335 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1336 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001337 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001338 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001339 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1340 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001341 } else {
1342 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001343 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001344 Val = Chain.getValue(0);
1345 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001346 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1347 } else {
1348 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1349 CopyVT, InFlag).getValue(1);
1350 Val = Chain.getValue(0);
1351 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001352 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001353
Dan Gohman37eed792009-02-04 17:28:58 +00001354 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001355 // Round the F80 the right size, which also moves to the appropriate xmm
1356 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001357 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001358 // This truncation won't change the value.
1359 DAG.getIntPtrConstant(1));
1360 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001361
Dan Gohman98ca4f22009-08-05 01:29:28 +00001362 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001363 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001364
Dan Gohman98ca4f22009-08-05 01:29:28 +00001365 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001366}
1367
1368
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001369//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001370// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001371//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001372// StdCall calling convention seems to be standard for many Windows' API
1373// routines and around. It differs from C calling convention just a little:
1374// callee should clean up the stack, not caller. Symbols should be also
1375// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001376// For info on fast calling convention see Fast Calling Convention (tail call)
1377// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001378
Dan Gohman98ca4f22009-08-05 01:29:28 +00001379/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001380/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001381static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1382 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001383 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001384
Dan Gohman98ca4f22009-08-05 01:29:28 +00001385 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001386}
1387
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001388/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001389/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001390static bool
1391ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1392 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001393 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001394
Dan Gohman98ca4f22009-08-05 01:29:28 +00001395 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001396}
1397
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001398/// IsCalleePop - Determines whether the callee is required to pop its
1399/// own arguments. Callee pop is necessary to support tail calls.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001400bool X86TargetLowering::IsCalleePop(bool IsVarArg, CallingConv::ID CallingConv){
Gordon Henriksen86737662008-01-05 16:56:59 +00001401 if (IsVarArg)
1402 return false;
1403
Dan Gohman095cc292008-09-13 01:54:27 +00001404 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001405 default:
1406 return false;
1407 case CallingConv::X86_StdCall:
1408 return !Subtarget->is64Bit();
1409 case CallingConv::X86_FastCall:
1410 return !Subtarget->is64Bit();
1411 case CallingConv::Fast:
Dan Gohman1797ed52010-02-08 20:27:50 +00001412 return GuaranteedTailCallOpt;
Chris Lattner29689432010-03-11 00:22:57 +00001413 case CallingConv::GHC:
1414 return GuaranteedTailCallOpt;
Gordon Henriksen86737662008-01-05 16:56:59 +00001415 }
1416}
1417
Dan Gohman095cc292008-09-13 01:54:27 +00001418/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1419/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001420CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001421 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001422 if (CC == CallingConv::GHC)
1423 return CC_X86_64_GHC;
1424 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001425 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001426 else
1427 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001428 }
1429
Gordon Henriksen86737662008-01-05 16:56:59 +00001430 if (CC == CallingConv::X86_FastCall)
1431 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001432 else if (CC == CallingConv::Fast)
1433 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001434 else if (CC == CallingConv::GHC)
1435 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001436 else
1437 return CC_X86_32_C;
1438}
1439
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001440/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1441/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001442/// the specific parameter attribute. The copy will be passed as a byval
1443/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001444static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001445CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001446 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1447 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001448 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001449 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001450 /*isVolatile*/false, /*AlwaysInline=*/true,
1451 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001452}
1453
Chris Lattner29689432010-03-11 00:22:57 +00001454/// IsTailCallConvention - Return true if the calling convention is one that
1455/// supports tail call optimization.
1456static bool IsTailCallConvention(CallingConv::ID CC) {
1457 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1458}
1459
Evan Cheng0c439eb2010-01-27 00:07:07 +00001460/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1461/// a tailcall target by changing its ABI.
1462static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001463 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001464}
1465
Dan Gohman98ca4f22009-08-05 01:29:28 +00001466SDValue
1467X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001468 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001469 const SmallVectorImpl<ISD::InputArg> &Ins,
1470 DebugLoc dl, SelectionDAG &DAG,
1471 const CCValAssign &VA,
1472 MachineFrameInfo *MFI,
1473 unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001474 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001475 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001476 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001477 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001478 EVT ValVT;
1479
1480 // If value is passed by pointer we have address passed instead of the value
1481 // itself.
1482 if (VA.getLocInfo() == CCValAssign::Indirect)
1483 ValVT = VA.getLocVT();
1484 else
1485 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001486
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001487 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001488 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001489 // In case of tail call optimization mark all arguments mutable. Since they
1490 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001491 if (Flags.isByVal()) {
1492 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1493 VA.getLocMemOffset(), isImmutable, false);
1494 return DAG.getFrameIndex(FI, getPointerTy());
1495 } else {
1496 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1497 VA.getLocMemOffset(), isImmutable, false);
1498 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1499 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001500 PseudoSourceValue::getFixedStack(FI), 0,
1501 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001502 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001503}
1504
Dan Gohman475871a2008-07-27 21:46:04 +00001505SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001506X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001507 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001508 bool isVarArg,
1509 const SmallVectorImpl<ISD::InputArg> &Ins,
1510 DebugLoc dl,
1511 SelectionDAG &DAG,
1512 SmallVectorImpl<SDValue> &InVals) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001513 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001514 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001515
Gordon Henriksen86737662008-01-05 16:56:59 +00001516 const Function* Fn = MF.getFunction();
1517 if (Fn->hasExternalLinkage() &&
1518 Subtarget->isTargetCygMing() &&
1519 Fn->getName() == "main")
1520 FuncInfo->setForceFramePointer(true);
1521
Evan Cheng1bc78042006-04-26 01:20:17 +00001522 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001523 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001524 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001525
Chris Lattner29689432010-03-11 00:22:57 +00001526 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1527 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001528
Chris Lattner638402b2007-02-28 07:00:42 +00001529 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001530 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001531 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1532 ArgLocs, *DAG.getContext());
1533 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001534
Chris Lattnerf39f7712007-02-28 05:46:49 +00001535 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001536 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001537 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1538 CCValAssign &VA = ArgLocs[i];
1539 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1540 // places.
1541 assert(VA.getValNo() != LastVal &&
1542 "Don't support value assigned to multiple locs yet");
1543 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001544
Chris Lattnerf39f7712007-02-28 05:46:49 +00001545 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001546 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001547 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001548 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001549 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001550 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001551 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001552 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001553 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001555 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001556 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001557 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001558 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1559 RC = X86::VR64RegisterClass;
1560 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001561 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001562
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001563 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001564 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Chris Lattnerf39f7712007-02-28 05:46:49 +00001566 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1567 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1568 // right size.
1569 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001570 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001571 DAG.getValueType(VA.getValVT()));
1572 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001573 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001574 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001575 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001576 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001577
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001578 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001579 // Handle MMX values passed in XMM regs.
1580 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001581 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1582 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001583 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1584 } else
1585 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001586 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001587 } else {
1588 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001589 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001590 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001591
1592 // If value is passed via pointer - do a load.
1593 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001594 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1595 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001596
Dan Gohman98ca4f22009-08-05 01:29:28 +00001597 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001598 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001599
Dan Gohman61a92132008-04-21 23:59:07 +00001600 // The x86-64 ABI for returning structs by value requires that we copy
1601 // the sret argument into %rax for the return. Save the argument into
1602 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001603 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001604 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1605 unsigned Reg = FuncInfo->getSRetReturnReg();
1606 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001607 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001608 FuncInfo->setSRetReturnReg(Reg);
1609 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001610 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001611 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001612 }
1613
Chris Lattnerf39f7712007-02-28 05:46:49 +00001614 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001615 // Align stack specially for tail calls.
1616 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001617 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001618
Evan Cheng1bc78042006-04-26 01:20:17 +00001619 // If the function takes variable number of arguments, make a frame index for
1620 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001621 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001622 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001623 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,
1624 true, false));
Gordon Henriksen86737662008-01-05 16:56:59 +00001625 }
1626 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001627 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1628
1629 // FIXME: We should really autogenerate these arrays
1630 static const unsigned GPR64ArgRegsWin64[] = {
1631 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001632 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001633 static const unsigned XMMArgRegsWin64[] = {
1634 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1635 };
1636 static const unsigned GPR64ArgRegs64Bit[] = {
1637 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1638 };
1639 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001640 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1641 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1642 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001643 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1644
1645 if (IsWin64) {
1646 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1647 GPR64ArgRegs = GPR64ArgRegsWin64;
1648 XMMArgRegs = XMMArgRegsWin64;
1649 } else {
1650 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1651 GPR64ArgRegs = GPR64ArgRegs64Bit;
1652 XMMArgRegs = XMMArgRegs64Bit;
1653 }
1654 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1655 TotalNumIntRegs);
1656 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1657 TotalNumXMMRegs);
1658
Devang Patel578efa92009-06-05 21:57:13 +00001659 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001660 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001661 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001662 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001663 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001664 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001665 // Kernel mode asks for SSE to be disabled, so don't push them
1666 // on the stack.
1667 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001668
Gordon Henriksen86737662008-01-05 16:56:59 +00001669 // For X86-64, if there are vararg parameters that are passed via
1670 // registers, then we must store them to their spots on the stack so they
1671 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001672 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1673 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1674 FuncInfo->setRegSaveFrameIndex(
1675 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1676 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001677
Gordon Henriksen86737662008-01-05 16:56:59 +00001678 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001679 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001680 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1681 getPointerTy());
1682 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001683 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001684 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1685 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001686 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1687 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001688 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001689 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001690 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001691 PseudoSourceValue::getFixedStack(
1692 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001693 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001694 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001695 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001696 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001697
Dan Gohmanface41a2009-08-16 21:24:25 +00001698 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1699 // Now store the XMM (fp + vector) parameter registers.
1700 SmallVector<SDValue, 11> SaveXMMOps;
1701 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001702
Dan Gohmanface41a2009-08-16 21:24:25 +00001703 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1704 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1705 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001706
Dan Gohman1e93df62010-04-17 14:41:14 +00001707 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1708 FuncInfo->getRegSaveFrameIndex()));
1709 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1710 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001711
Dan Gohmanface41a2009-08-16 21:24:25 +00001712 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1713 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1714 X86::VR128RegisterClass);
1715 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1716 SaveXMMOps.push_back(Val);
1717 }
1718 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1719 MVT::Other,
1720 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001721 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001722
1723 if (!MemOps.empty())
1724 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1725 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001726 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001727 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001728
Gordon Henriksen86737662008-01-05 16:56:59 +00001729 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 if (IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001731 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001732 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001733 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001734 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001735 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001736 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001737 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001738
Gordon Henriksen86737662008-01-05 16:56:59 +00001739 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001740 // RegSaveFrameIndex is X86-64 only.
1741 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001742 if (CallConv == CallingConv::X86_FastCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001743 // fastcc functions can't have varargs.
1744 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001745 }
Evan Cheng25caf632006-05-23 21:06:34 +00001746
Dan Gohman98ca4f22009-08-05 01:29:28 +00001747 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001748}
1749
Dan Gohman475871a2008-07-27 21:46:04 +00001750SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001751X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1752 SDValue StackPtr, SDValue Arg,
1753 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001754 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001755 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001756 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001757 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001758 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001759 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001760 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001761 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001762 }
Dale Johannesenace16102009-02-03 19:33:06 +00001763 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001764 PseudoSourceValue::getStack(), LocMemOffset,
1765 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001766}
1767
Bill Wendling64e87322009-01-16 19:25:27 +00001768/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001769/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001770SDValue
1771X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001772 SDValue &OutRetAddr, SDValue Chain,
1773 bool IsTailCall, bool Is64Bit,
1774 int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001775 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001776 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001777 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001778
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001779 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001780 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001781 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001782}
1783
1784/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1785/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001786static SDValue
1787EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001788 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001789 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001790 // Store the return address to the appropriate stack slot.
1791 if (!FPDiff) return Chain;
1792 // Calculate the new stack slot for the return address.
1793 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001794 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001795 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001796 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001797 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001798 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001799 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1800 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001801 return Chain;
1802}
1803
Dan Gohman98ca4f22009-08-05 01:29:28 +00001804SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001805X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001806 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001807 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001808 const SmallVectorImpl<ISD::OutputArg> &Outs,
1809 const SmallVectorImpl<ISD::InputArg> &Ins,
1810 DebugLoc dl, SelectionDAG &DAG,
1811 SmallVectorImpl<SDValue> &InVals) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001812 MachineFunction &MF = DAG.getMachineFunction();
1813 bool Is64Bit = Subtarget->is64Bit();
1814 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001815 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001816
Evan Cheng5f941932010-02-05 02:21:12 +00001817 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001818 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001819 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1820 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001821 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001822
1823 // Sibcalls are automatically detected tailcalls which do not require
1824 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001825 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001826 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001827
1828 if (isTailCall)
1829 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001830 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001831
Chris Lattner29689432010-03-11 00:22:57 +00001832 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1833 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001834
Chris Lattner638402b2007-02-28 07:00:42 +00001835 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001836 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001837 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1838 ArgLocs, *DAG.getContext());
1839 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001840
Chris Lattner423c5f42007-02-28 05:31:48 +00001841 // Get a count of how many bytes are to be pushed on the stack.
1842 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001843 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001844 // This is a sibcall. The memory operands are available in caller's
1845 // own caller's stack.
1846 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001847 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001848 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001849
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001851 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001852 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001853 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001854 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1855 FPDiff = NumBytesCallerPushed - NumBytes;
1856
1857 // Set the delta of movement of the returnaddr stackslot.
1858 // But only set if delta is greater than previous delta.
1859 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1860 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1861 }
1862
Evan Chengf22f9b32010-02-06 03:28:46 +00001863 if (!IsSibcall)
1864 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001865
Dan Gohman475871a2008-07-27 21:46:04 +00001866 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001867 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001868 if (isTailCall && FPDiff)
1869 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1870 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001871
Dan Gohman475871a2008-07-27 21:46:04 +00001872 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1873 SmallVector<SDValue, 8> MemOpChains;
1874 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001875
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001876 // Walk the register/memloc assignments, inserting copies/loads. In the case
1877 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001878 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1879 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001880 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001881 SDValue Arg = Outs[i].Val;
1882 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001883 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001884
Chris Lattner423c5f42007-02-28 05:31:48 +00001885 // Promote the value if needed.
1886 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001887 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001888 case CCValAssign::Full: break;
1889 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001890 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001891 break;
1892 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001893 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001894 break;
1895 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001896 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1897 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001898 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1899 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1900 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001901 } else
1902 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1903 break;
1904 case CCValAssign::BCvt:
1905 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001906 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001907 case CCValAssign::Indirect: {
1908 // Store the argument.
1909 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001910 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001911 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001912 PseudoSourceValue::getFixedStack(FI), 0,
1913 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001914 Arg = SpillSlot;
1915 break;
1916 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001917 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001918
Chris Lattner423c5f42007-02-28 05:31:48 +00001919 if (VA.isRegLoc()) {
1920 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001921 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001922 assert(VA.isMemLoc());
1923 if (StackPtr.getNode() == 0)
1924 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1925 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1926 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001927 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001929
Evan Cheng32fe1032006-05-25 00:59:30 +00001930 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001931 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001932 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001933
Evan Cheng347d5f72006-04-28 21:29:37 +00001934 // Build a sequence of copy-to-reg nodes chained together with token chain
1935 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001936 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001937 // Tail call byval lowering might overwrite argument registers so in case of
1938 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001939 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001940 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001941 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001942 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001943 InFlag = Chain.getValue(1);
1944 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001945
Chris Lattner88e1fd52009-07-09 04:24:46 +00001946 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001947 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1948 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001949 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001950 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1951 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001952 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001953 InFlag);
1954 InFlag = Chain.getValue(1);
1955 } else {
1956 // If we are tail calling and generating PIC/GOT style code load the
1957 // address of the callee into ECX. The value in ecx is used as target of
1958 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1959 // for tail calls on PIC/GOT architectures. Normally we would just put the
1960 // address of GOT into ebx and then call target@PLT. But for tail calls
1961 // ebx would be restored (since ebx is callee saved) before jumping to the
1962 // target@PLT.
1963
1964 // Note: The actual moving to ECX is done further down.
1965 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1966 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1967 !G->getGlobal()->hasProtectedVisibility())
1968 Callee = LowerGlobalAddress(Callee, DAG);
1969 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001970 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001971 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001972 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001973
Gordon Henriksen86737662008-01-05 16:56:59 +00001974 if (Is64Bit && isVarArg) {
1975 // From AMD64 ABI document:
1976 // For calls that may call functions that use varargs or stdargs
1977 // (prototype-less calls or calls to functions containing ellipsis (...) in
1978 // the declaration) %al is used as hidden argument to specify the number
1979 // of SSE registers used. The contents of %al do not need to match exactly
1980 // the number of registers, but must be an ubound on the number of SSE
1981 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001982
1983 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001984 // Count the number of XMM registers allocated.
1985 static const unsigned XMMArgRegs[] = {
1986 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1987 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1988 };
1989 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001990 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001991 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001992
Dale Johannesendd64c412009-02-04 00:33:20 +00001993 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001994 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001995 InFlag = Chain.getValue(1);
1996 }
1997
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001998
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001999 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002000 if (isTailCall) {
2001 // Force all the incoming stack arguments to be loaded from the stack
2002 // before any new outgoing arguments are stored to the stack, because the
2003 // outgoing stack slots may alias the incoming argument stack slots, and
2004 // the alias isn't otherwise explicit. This is slightly more conservative
2005 // than necessary, because it means that each store effectively depends
2006 // on every argument instead of just those arguments it would clobber.
2007 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2008
Dan Gohman475871a2008-07-27 21:46:04 +00002009 SmallVector<SDValue, 8> MemOpChains2;
2010 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002011 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002012 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002013 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002014 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002015 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2016 CCValAssign &VA = ArgLocs[i];
2017 if (VA.isRegLoc())
2018 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002019 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002020 SDValue Arg = Outs[i].Val;
2021 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002022 // Create frame index.
2023 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002024 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00002025 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002026 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002027
Duncan Sands276dcbd2008-03-21 09:14:45 +00002028 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002029 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002030 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002031 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002032 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002033 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002034 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002035
Dan Gohman98ca4f22009-08-05 01:29:28 +00002036 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2037 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002038 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002039 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002040 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002041 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002042 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002043 PseudoSourceValue::getFixedStack(FI), 0,
2044 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002045 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002046 }
2047 }
2048
2049 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002050 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002051 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002052
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002053 // Copy arguments to their registers.
2054 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002055 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002056 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002057 InFlag = Chain.getValue(1);
2058 }
Dan Gohman475871a2008-07-27 21:46:04 +00002059 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002060
Gordon Henriksen86737662008-01-05 16:56:59 +00002061 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002062 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002063 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002064 }
2065
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002066 bool WasGlobalOrExternal = false;
2067 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2068 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2069 // In the 64-bit large code model, we have to make all calls
2070 // through a register, since the call instruction's 32-bit
2071 // pc-relative offset may not be large enough to hold the whole
2072 // address.
2073 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2074 WasGlobalOrExternal = true;
2075 // If the callee is a GlobalAddress node (quite common, every direct call
2076 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2077 // it.
2078
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002079 // We should use extra load for direct calls to dllimported functions in
2080 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002081 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002082 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002083 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002084
Chris Lattner48a7d022009-07-09 05:02:21 +00002085 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2086 // external symbols most go through the PLT in PIC mode. If the symbol
2087 // has hidden or protected visibility, or if it is static or local, then
2088 // we don't need to use the PLT - we can directly call it.
2089 if (Subtarget->isTargetELF() &&
2090 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002091 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002092 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002093 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002094 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2095 Subtarget->getDarwinVers() < 9) {
2096 // PC-relative references to external symbols should go through $stub,
2097 // unless we're building with the leopard linker or later, which
2098 // automatically synthesizes these stubs.
2099 OpFlags = X86II::MO_DARWIN_STUB;
2100 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002101
Chris Lattner74e726e2009-07-09 05:27:35 +00002102 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002103 G->getOffset(), OpFlags);
2104 }
Bill Wendling056292f2008-09-16 21:48:12 +00002105 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002106 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002107 unsigned char OpFlags = 0;
2108
2109 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2110 // symbols should go through the PLT.
2111 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002112 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002113 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002114 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002115 Subtarget->getDarwinVers() < 9) {
2116 // PC-relative references to external symbols should go through $stub,
2117 // unless we're building with the leopard linker or later, which
2118 // automatically synthesizes these stubs.
2119 OpFlags = X86II::MO_DARWIN_STUB;
2120 }
Eric Christopherfd179292009-08-27 18:07:15 +00002121
Chris Lattner48a7d022009-07-09 05:02:21 +00002122 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2123 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002124 }
2125
Chris Lattnerd96d0722007-02-25 06:40:16 +00002126 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002127 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002128 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002129
Evan Chengf22f9b32010-02-06 03:28:46 +00002130 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002131 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2132 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002133 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002134 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002135
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002136 Ops.push_back(Chain);
2137 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002138
Dan Gohman98ca4f22009-08-05 01:29:28 +00002139 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002140 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002141
Gordon Henriksen86737662008-01-05 16:56:59 +00002142 // Add argument registers to the end of the list so that they are known live
2143 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002144 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2145 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2146 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002147
Evan Cheng586ccac2008-03-18 23:36:35 +00002148 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002149 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002150 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2151
2152 // Add an implicit use of AL for x86 vararg functions.
2153 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002154 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002155
Gabor Greifba36cb52008-08-28 21:40:38 +00002156 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002157 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002158
Dan Gohman98ca4f22009-08-05 01:29:28 +00002159 if (isTailCall) {
2160 // If this is the first return lowered for this function, add the regs
2161 // to the liveout set for the function.
2162 if (MF.getRegInfo().liveout_empty()) {
2163 SmallVector<CCValAssign, 16> RVLocs;
2164 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2165 *DAG.getContext());
2166 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2167 for (unsigned i = 0; i != RVLocs.size(); ++i)
2168 if (RVLocs[i].isRegLoc())
2169 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2170 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002171 return DAG.getNode(X86ISD::TC_RETURN, dl,
2172 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002173 }
2174
Dale Johannesenace16102009-02-03 19:33:06 +00002175 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002176 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002177
Chris Lattner2d297092006-05-23 18:50:38 +00002178 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002179 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00002180 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002181 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002182 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002183 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002184 // pops the hidden struct pointer, so we have to push it back.
2185 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002186 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002187 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002188 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002189
Gordon Henriksenae636f82008-01-03 16:47:34 +00002190 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002191 if (!IsSibcall) {
2192 Chain = DAG.getCALLSEQ_END(Chain,
2193 DAG.getIntPtrConstant(NumBytes, true),
2194 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2195 true),
2196 InFlag);
2197 InFlag = Chain.getValue(1);
2198 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002199
Chris Lattner3085e152007-02-25 08:59:22 +00002200 // Handle result values, copying them out of physregs into vregs that we
2201 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2203 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002204}
2205
Evan Cheng25ab6902006-09-08 06:48:29 +00002206
2207//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002208// Fast Calling Convention (tail call) implementation
2209//===----------------------------------------------------------------------===//
2210
2211// Like std call, callee cleans arguments, convention except that ECX is
2212// reserved for storing the tail called function address. Only 2 registers are
2213// free for argument passing (inreg). Tail call optimization is performed
2214// provided:
2215// * tailcallopt is enabled
2216// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002217// On X86_64 architecture with GOT-style position independent code only local
2218// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002219// To keep the stack aligned according to platform abi the function
2220// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2221// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002222// If a tail called function callee has more arguments than the caller the
2223// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002224// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002225// original REtADDR, but before the saved framepointer or the spilled registers
2226// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2227// stack layout:
2228// arg1
2229// arg2
2230// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002231// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002232// move area ]
2233// (possible EBP)
2234// ESI
2235// EDI
2236// local1 ..
2237
2238/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2239/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002240unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002241 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002242 MachineFunction &MF = DAG.getMachineFunction();
2243 const TargetMachine &TM = MF.getTarget();
2244 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2245 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002246 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002247 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002248 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002249 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2250 // Number smaller than 12 so just add the difference.
2251 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2252 } else {
2253 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002254 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002255 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002256 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002257 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002258}
2259
Evan Cheng5f941932010-02-05 02:21:12 +00002260/// MatchingStackOffset - Return true if the given stack call argument is
2261/// already available in the same position (relatively) of the caller's
2262/// incoming argument stack.
2263static
2264bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2265 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2266 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002267 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2268 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002269 if (Arg.getOpcode() == ISD::CopyFromReg) {
2270 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2271 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2272 return false;
2273 MachineInstr *Def = MRI->getVRegDef(VR);
2274 if (!Def)
2275 return false;
2276 if (!Flags.isByVal()) {
2277 if (!TII->isLoadFromStackSlot(Def, FI))
2278 return false;
2279 } else {
2280 unsigned Opcode = Def->getOpcode();
2281 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2282 Def->getOperand(1).isFI()) {
2283 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002284 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002285 } else
2286 return false;
2287 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002288 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2289 if (Flags.isByVal())
2290 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002291 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002292 // define @foo(%struct.X* %A) {
2293 // tail call @bar(%struct.X* byval %A)
2294 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002295 return false;
2296 SDValue Ptr = Ld->getBasePtr();
2297 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2298 if (!FINode)
2299 return false;
2300 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002301 } else
2302 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002303
Evan Cheng4cae1332010-03-05 08:38:04 +00002304 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002305 if (!MFI->isFixedObjectIndex(FI))
2306 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002307 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002308}
2309
Dan Gohman98ca4f22009-08-05 01:29:28 +00002310/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2311/// for tail call optimization. Targets which want to do tail call
2312/// optimization should implement this function.
2313bool
2314X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002315 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002316 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002317 bool isCalleeStructRet,
2318 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002319 const SmallVectorImpl<ISD::OutputArg> &Outs,
2320 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002321 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002322 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002323 CalleeCC != CallingConv::C)
2324 return false;
2325
Evan Cheng7096ae42010-01-29 06:45:59 +00002326 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002327 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002328 const Function *CallerF = DAG.getMachineFunction().getFunction();
Dan Gohman1797ed52010-02-08 20:27:50 +00002329 if (GuaranteedTailCallOpt) {
Chris Lattner29689432010-03-11 00:22:57 +00002330 if (IsTailCallConvention(CalleeCC) &&
Evan Cheng843bd692010-01-31 06:44:49 +00002331 CallerF->getCallingConv() == CalleeCC)
2332 return true;
2333 return false;
2334 }
2335
Evan Chengb2c92902010-02-02 02:22:50 +00002336 // Look for obvious safe cases to perform tail call optimization that does not
2337 // requite ABI changes. This is what gcc calls sibcall.
2338
Evan Cheng2c12cb42010-03-26 16:26:03 +00002339 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2340 // emit a special epilogue.
2341 if (RegInfo->needsStackRealignment(MF))
2342 return false;
2343
Evan Cheng3c262ee2010-03-26 02:13:13 +00002344 // Do not sibcall optimize vararg calls unless the call site is not passing any
2345 // arguments.
2346 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002347 return false;
2348
Evan Chenga375d472010-03-15 18:54:48 +00002349 // Also avoid sibcall optimization if either caller or callee uses struct
2350 // return semantics.
2351 if (isCalleeStructRet || isCallerStructRet)
2352 return false;
2353
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002354 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2355 // Therefore if it's not used by the call it is not safe to optimize this into
2356 // a sibcall.
2357 bool Unused = false;
2358 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2359 if (!Ins[i].Used) {
2360 Unused = true;
2361 break;
2362 }
2363 }
2364 if (Unused) {
2365 SmallVector<CCValAssign, 16> RVLocs;
2366 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2367 RVLocs, *DAG.getContext());
2368 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2369 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2370 CCValAssign &VA = RVLocs[i];
2371 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2372 return false;
2373 }
2374 }
2375
Evan Chenga6bff982010-01-30 01:22:00 +00002376 // If the callee takes no arguments then go on to check the results of the
2377 // call.
2378 if (!Outs.empty()) {
2379 // Check if stack adjustment is needed. For now, do not do this if any
2380 // argument is passed on the stack.
2381 SmallVector<CCValAssign, 16> ArgLocs;
2382 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2383 ArgLocs, *DAG.getContext());
2384 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002385 if (CCInfo.getNextStackOffset()) {
2386 MachineFunction &MF = DAG.getMachineFunction();
2387 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2388 return false;
2389 if (Subtarget->isTargetWin64())
2390 // Win64 ABI has additional complications.
2391 return false;
2392
2393 // Check if the arguments are already laid out in the right way as
2394 // the caller's fixed stack objects.
2395 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002396 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2397 const X86InstrInfo *TII =
2398 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002399 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2400 CCValAssign &VA = ArgLocs[i];
2401 EVT RegVT = VA.getLocVT();
2402 SDValue Arg = Outs[i].Val;
2403 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002404 if (VA.getLocInfo() == CCValAssign::Indirect)
2405 return false;
2406 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002407 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2408 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002409 return false;
2410 }
2411 }
2412 }
Evan Chenga6bff982010-01-30 01:22:00 +00002413 }
Evan Chengb1712452010-01-27 06:25:16 +00002414
Evan Cheng86809cc2010-02-03 03:28:02 +00002415 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002416}
2417
Dan Gohman3df24e62008-09-03 23:12:08 +00002418FastISel *
Chris Lattnered3a8062010-04-05 06:05:26 +00002419X86TargetLowering::createFastISel(MachineFunction &mf,
Evan Chengddc419c2010-01-26 19:04:47 +00002420 DenseMap<const Value *, unsigned> &vm,
2421 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
2422 DenseMap<const AllocaInst *, int> &am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002423#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00002424 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002425#endif
2426 ) {
Chris Lattnered3a8062010-04-05 06:05:26 +00002427 return X86::createFastISel(mf, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002428#ifndef NDEBUG
2429 , cil
2430#endif
2431 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002432}
2433
2434
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002435//===----------------------------------------------------------------------===//
2436// Other Lowering Hooks
2437//===----------------------------------------------------------------------===//
2438
2439
Dan Gohman475871a2008-07-27 21:46:04 +00002440SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002441 MachineFunction &MF = DAG.getMachineFunction();
2442 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2443 int ReturnAddrIndex = FuncInfo->getRAIndex();
2444
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002445 if (ReturnAddrIndex == 0) {
2446 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002447 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002448 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002449 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002450 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002451 }
2452
Evan Cheng25ab6902006-09-08 06:48:29 +00002453 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002454}
2455
2456
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002457bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2458 bool hasSymbolicDisplacement) {
2459 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002460 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002461 return false;
2462
2463 // If we don't have a symbolic displacement - we don't have any extra
2464 // restrictions.
2465 if (!hasSymbolicDisplacement)
2466 return true;
2467
2468 // FIXME: Some tweaks might be needed for medium code model.
2469 if (M != CodeModel::Small && M != CodeModel::Kernel)
2470 return false;
2471
2472 // For small code model we assume that latest object is 16MB before end of 31
2473 // bits boundary. We may also accept pretty large negative constants knowing
2474 // that all objects are in the positive half of address space.
2475 if (M == CodeModel::Small && Offset < 16*1024*1024)
2476 return true;
2477
2478 // For kernel code model we know that all object resist in the negative half
2479 // of 32bits address space. We may not accept negative offsets, since they may
2480 // be just off and we may accept pretty large positive ones.
2481 if (M == CodeModel::Kernel && Offset > 0)
2482 return true;
2483
2484 return false;
2485}
2486
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002487/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2488/// specific condition code, returning the condition code and the LHS/RHS of the
2489/// comparison to make.
2490static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2491 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002492 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002493 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2494 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2495 // X > -1 -> X == 0, jump !sign.
2496 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002497 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002498 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2499 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002500 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002501 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002502 // X < 1 -> X <= 0
2503 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002504 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002505 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002506 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002507
Evan Chengd9558e02006-01-06 00:43:03 +00002508 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002509 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002510 case ISD::SETEQ: return X86::COND_E;
2511 case ISD::SETGT: return X86::COND_G;
2512 case ISD::SETGE: return X86::COND_GE;
2513 case ISD::SETLT: return X86::COND_L;
2514 case ISD::SETLE: return X86::COND_LE;
2515 case ISD::SETNE: return X86::COND_NE;
2516 case ISD::SETULT: return X86::COND_B;
2517 case ISD::SETUGT: return X86::COND_A;
2518 case ISD::SETULE: return X86::COND_BE;
2519 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002520 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002521 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002522
Chris Lattner4c78e022008-12-23 23:42:27 +00002523 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002524
Chris Lattner4c78e022008-12-23 23:42:27 +00002525 // If LHS is a foldable load, but RHS is not, flip the condition.
2526 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2527 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2528 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2529 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002530 }
2531
Chris Lattner4c78e022008-12-23 23:42:27 +00002532 switch (SetCCOpcode) {
2533 default: break;
2534 case ISD::SETOLT:
2535 case ISD::SETOLE:
2536 case ISD::SETUGT:
2537 case ISD::SETUGE:
2538 std::swap(LHS, RHS);
2539 break;
2540 }
2541
2542 // On a floating point condition, the flags are set as follows:
2543 // ZF PF CF op
2544 // 0 | 0 | 0 | X > Y
2545 // 0 | 0 | 1 | X < Y
2546 // 1 | 0 | 0 | X == Y
2547 // 1 | 1 | 1 | unordered
2548 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002549 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002550 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002551 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002552 case ISD::SETOLT: // flipped
2553 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002554 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002555 case ISD::SETOLE: // flipped
2556 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002557 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002558 case ISD::SETUGT: // flipped
2559 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002560 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002561 case ISD::SETUGE: // flipped
2562 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002563 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002564 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002565 case ISD::SETNE: return X86::COND_NE;
2566 case ISD::SETUO: return X86::COND_P;
2567 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002568 case ISD::SETOEQ:
2569 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002570 }
Evan Chengd9558e02006-01-06 00:43:03 +00002571}
2572
Evan Cheng4a460802006-01-11 00:33:36 +00002573/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2574/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002575/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002576static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002577 switch (X86CC) {
2578 default:
2579 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002580 case X86::COND_B:
2581 case X86::COND_BE:
2582 case X86::COND_E:
2583 case X86::COND_P:
2584 case X86::COND_A:
2585 case X86::COND_AE:
2586 case X86::COND_NE:
2587 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002588 return true;
2589 }
2590}
2591
Evan Chengeb2f9692009-10-27 19:56:55 +00002592/// isFPImmLegal - Returns true if the target can instruction select the
2593/// specified FP immediate natively. If false, the legalizer will
2594/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002595bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002596 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2597 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2598 return true;
2599 }
2600 return false;
2601}
2602
Nate Begeman9008ca62009-04-27 18:41:29 +00002603/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2604/// the specified range (L, H].
2605static bool isUndefOrInRange(int Val, int Low, int Hi) {
2606 return (Val < 0) || (Val >= Low && Val < Hi);
2607}
2608
2609/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2610/// specified value.
2611static bool isUndefOrEqual(int Val, int CmpVal) {
2612 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002613 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002614 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002615}
2616
Nate Begeman9008ca62009-04-27 18:41:29 +00002617/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2618/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2619/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002620static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002621 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002622 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002623 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002624 return (Mask[0] < 2 && Mask[1] < 2);
2625 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002626}
2627
Nate Begeman9008ca62009-04-27 18:41:29 +00002628bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002629 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 N->getMask(M);
2631 return ::isPSHUFDMask(M, N->getValueType(0));
2632}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002633
Nate Begeman9008ca62009-04-27 18:41:29 +00002634/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2635/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002636static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002637 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002638 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002639
Nate Begeman9008ca62009-04-27 18:41:29 +00002640 // Lower quadword copied in order or undef.
2641 for (int i = 0; i != 4; ++i)
2642 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002643 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002644
Evan Cheng506d3df2006-03-29 23:07:14 +00002645 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002646 for (int i = 4; i != 8; ++i)
2647 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002648 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002649
Evan Cheng506d3df2006-03-29 23:07:14 +00002650 return true;
2651}
2652
Nate Begeman9008ca62009-04-27 18:41:29 +00002653bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002654 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002655 N->getMask(M);
2656 return ::isPSHUFHWMask(M, N->getValueType(0));
2657}
Evan Cheng506d3df2006-03-29 23:07:14 +00002658
Nate Begeman9008ca62009-04-27 18:41:29 +00002659/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2660/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002661static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002662 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002663 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002664
Rafael Espindola15684b22009-04-24 12:40:33 +00002665 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002666 for (int i = 4; i != 8; ++i)
2667 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002668 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002669
Rafael Espindola15684b22009-04-24 12:40:33 +00002670 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002671 for (int i = 0; i != 4; ++i)
2672 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002673 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002674
Rafael Espindola15684b22009-04-24 12:40:33 +00002675 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002676}
2677
Nate Begeman9008ca62009-04-27 18:41:29 +00002678bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002679 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002680 N->getMask(M);
2681 return ::isPSHUFLWMask(M, N->getValueType(0));
2682}
2683
Nate Begemana09008b2009-10-19 02:17:23 +00002684/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2685/// is suitable for input to PALIGNR.
2686static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2687 bool hasSSSE3) {
2688 int i, e = VT.getVectorNumElements();
2689
2690 // Do not handle v2i64 / v2f64 shuffles with palignr.
2691 if (e < 4 || !hasSSSE3)
2692 return false;
2693
2694 for (i = 0; i != e; ++i)
2695 if (Mask[i] >= 0)
2696 break;
2697
2698 // All undef, not a palignr.
2699 if (i == e)
2700 return false;
2701
2702 // Determine if it's ok to perform a palignr with only the LHS, since we
2703 // don't have access to the actual shuffle elements to see if RHS is undef.
2704 bool Unary = Mask[i] < (int)e;
2705 bool NeedsUnary = false;
2706
2707 int s = Mask[i] - i;
2708
2709 // Check the rest of the elements to see if they are consecutive.
2710 for (++i; i != e; ++i) {
2711 int m = Mask[i];
2712 if (m < 0)
2713 continue;
2714
2715 Unary = Unary && (m < (int)e);
2716 NeedsUnary = NeedsUnary || (m < s);
2717
2718 if (NeedsUnary && !Unary)
2719 return false;
2720 if (Unary && m != ((s+i) & (e-1)))
2721 return false;
2722 if (!Unary && m != (s+i))
2723 return false;
2724 }
2725 return true;
2726}
2727
2728bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2729 SmallVector<int, 8> M;
2730 N->getMask(M);
2731 return ::isPALIGNRMask(M, N->getValueType(0), true);
2732}
2733
Evan Cheng14aed5e2006-03-24 01:18:28 +00002734/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2735/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002736static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002737 int NumElems = VT.getVectorNumElements();
2738 if (NumElems != 2 && NumElems != 4)
2739 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002740
Nate Begeman9008ca62009-04-27 18:41:29 +00002741 int Half = NumElems / 2;
2742 for (int i = 0; i < Half; ++i)
2743 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002744 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002745 for (int i = Half; i < NumElems; ++i)
2746 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002747 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002748
Evan Cheng14aed5e2006-03-24 01:18:28 +00002749 return true;
2750}
2751
Nate Begeman9008ca62009-04-27 18:41:29 +00002752bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2753 SmallVector<int, 8> M;
2754 N->getMask(M);
2755 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002756}
2757
Evan Cheng213d2cf2007-05-17 18:45:50 +00002758/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002759/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2760/// half elements to come from vector 1 (which would equal the dest.) and
2761/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002762static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002763 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002764
2765 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002766 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002767
Nate Begeman9008ca62009-04-27 18:41:29 +00002768 int Half = NumElems / 2;
2769 for (int i = 0; i < Half; ++i)
2770 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002771 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002772 for (int i = Half; i < NumElems; ++i)
2773 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002774 return false;
2775 return true;
2776}
2777
Nate Begeman9008ca62009-04-27 18:41:29 +00002778static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2779 SmallVector<int, 8> M;
2780 N->getMask(M);
2781 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002782}
2783
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002784/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2785/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002786bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2787 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002788 return false;
2789
Evan Cheng2064a2b2006-03-28 06:50:32 +00002790 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002791 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2792 isUndefOrEqual(N->getMaskElt(1), 7) &&
2793 isUndefOrEqual(N->getMaskElt(2), 2) &&
2794 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002795}
2796
Nate Begeman0b10b912009-11-07 23:17:15 +00002797/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2798/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2799/// <2, 3, 2, 3>
2800bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2801 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2802
2803 if (NumElems != 4)
2804 return false;
2805
2806 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2807 isUndefOrEqual(N->getMaskElt(1), 3) &&
2808 isUndefOrEqual(N->getMaskElt(2), 2) &&
2809 isUndefOrEqual(N->getMaskElt(3), 3);
2810}
2811
Evan Cheng5ced1d82006-04-06 23:23:56 +00002812/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2813/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002814bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2815 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002816
Evan Cheng5ced1d82006-04-06 23:23:56 +00002817 if (NumElems != 2 && NumElems != 4)
2818 return false;
2819
Evan Chengc5cdff22006-04-07 21:53:05 +00002820 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002821 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002822 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002823
Evan Chengc5cdff22006-04-07 21:53:05 +00002824 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002825 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002826 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002827
2828 return true;
2829}
2830
Nate Begeman0b10b912009-11-07 23:17:15 +00002831/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2832/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2833bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002834 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002835
Evan Cheng5ced1d82006-04-06 23:23:56 +00002836 if (NumElems != 2 && NumElems != 4)
2837 return false;
2838
Evan Chengc5cdff22006-04-07 21:53:05 +00002839 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002840 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002841 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002842
Nate Begeman9008ca62009-04-27 18:41:29 +00002843 for (unsigned i = 0; i < NumElems/2; ++i)
2844 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002845 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002846
2847 return true;
2848}
2849
Evan Cheng0038e592006-03-28 00:39:58 +00002850/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2851/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002852static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002853 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002854 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002855 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002856 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002857
Nate Begeman9008ca62009-04-27 18:41:29 +00002858 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2859 int BitI = Mask[i];
2860 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002861 if (!isUndefOrEqual(BitI, j))
2862 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002863 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002864 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002865 return false;
2866 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002867 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002868 return false;
2869 }
Evan Cheng0038e592006-03-28 00:39:58 +00002870 }
Evan Cheng0038e592006-03-28 00:39:58 +00002871 return true;
2872}
2873
Nate Begeman9008ca62009-04-27 18:41:29 +00002874bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2875 SmallVector<int, 8> M;
2876 N->getMask(M);
2877 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002878}
2879
Evan Cheng4fcb9222006-03-28 02:43:26 +00002880/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2881/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002882static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002883 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002884 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002885 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002886 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002887
Nate Begeman9008ca62009-04-27 18:41:29 +00002888 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2889 int BitI = Mask[i];
2890 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002891 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002892 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002893 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002894 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002895 return false;
2896 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002897 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002898 return false;
2899 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002900 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002901 return true;
2902}
2903
Nate Begeman9008ca62009-04-27 18:41:29 +00002904bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2905 SmallVector<int, 8> M;
2906 N->getMask(M);
2907 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002908}
2909
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002910/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2911/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2912/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002913static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002914 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002915 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002916 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002917
Nate Begeman9008ca62009-04-27 18:41:29 +00002918 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2919 int BitI = Mask[i];
2920 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002921 if (!isUndefOrEqual(BitI, j))
2922 return false;
2923 if (!isUndefOrEqual(BitI1, j))
2924 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002925 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002926 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002927}
2928
Nate Begeman9008ca62009-04-27 18:41:29 +00002929bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2930 SmallVector<int, 8> M;
2931 N->getMask(M);
2932 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2933}
2934
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002935/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2936/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2937/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002938static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002939 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002940 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2941 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002942
Nate Begeman9008ca62009-04-27 18:41:29 +00002943 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2944 int BitI = Mask[i];
2945 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002946 if (!isUndefOrEqual(BitI, j))
2947 return false;
2948 if (!isUndefOrEqual(BitI1, j))
2949 return false;
2950 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002951 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002952}
2953
Nate Begeman9008ca62009-04-27 18:41:29 +00002954bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2955 SmallVector<int, 8> M;
2956 N->getMask(M);
2957 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2958}
2959
Evan Cheng017dcc62006-04-21 01:05:10 +00002960/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2961/// specifies a shuffle of elements that is suitable for input to MOVSS,
2962/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002963static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002964 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002965 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002966
2967 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002968
Nate Begeman9008ca62009-04-27 18:41:29 +00002969 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002970 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002971
Nate Begeman9008ca62009-04-27 18:41:29 +00002972 for (int i = 1; i < NumElts; ++i)
2973 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002974 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002975
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002976 return true;
2977}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002978
Nate Begeman9008ca62009-04-27 18:41:29 +00002979bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2980 SmallVector<int, 8> M;
2981 N->getMask(M);
2982 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002983}
2984
Evan Cheng017dcc62006-04-21 01:05:10 +00002985/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2986/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002987/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00002988static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002989 bool V2IsSplat = false, bool V2IsUndef = false) {
2990 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002991 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002992 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002993
Nate Begeman9008ca62009-04-27 18:41:29 +00002994 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002995 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002996
Nate Begeman9008ca62009-04-27 18:41:29 +00002997 for (int i = 1; i < NumOps; ++i)
2998 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2999 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3000 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003001 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003002
Evan Cheng39623da2006-04-20 08:58:49 +00003003 return true;
3004}
3005
Nate Begeman9008ca62009-04-27 18:41:29 +00003006static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003007 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003008 SmallVector<int, 8> M;
3009 N->getMask(M);
3010 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003011}
3012
Evan Chengd9539472006-04-14 21:59:03 +00003013/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3014/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003015bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3016 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003017 return false;
3018
3019 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003020 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003021 int Elt = N->getMaskElt(i);
3022 if (Elt >= 0 && Elt != 1)
3023 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003024 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003025
3026 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003027 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003028 int Elt = N->getMaskElt(i);
3029 if (Elt >= 0 && Elt != 3)
3030 return false;
3031 if (Elt == 3)
3032 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003033 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003034 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003035 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003036 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003037}
3038
3039/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3040/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003041bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3042 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003043 return false;
3044
3045 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003046 for (unsigned i = 0; i < 2; ++i)
3047 if (N->getMaskElt(i) > 0)
3048 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003049
3050 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003051 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003052 int Elt = N->getMaskElt(i);
3053 if (Elt >= 0 && Elt != 2)
3054 return false;
3055 if (Elt == 2)
3056 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003057 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003058 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003059 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003060}
3061
Evan Cheng0b457f02008-09-25 20:50:48 +00003062/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3063/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003064bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3065 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003066
Nate Begeman9008ca62009-04-27 18:41:29 +00003067 for (int i = 0; i < e; ++i)
3068 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003069 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003070 for (int i = 0; i < e; ++i)
3071 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003072 return false;
3073 return true;
3074}
3075
Evan Cheng63d33002006-03-22 08:01:21 +00003076/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003077/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003078unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003079 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3080 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3081
Evan Chengb9df0ca2006-03-22 02:53:00 +00003082 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3083 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003084 for (int i = 0; i < NumOperands; ++i) {
3085 int Val = SVOp->getMaskElt(NumOperands-i-1);
3086 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003087 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003088 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003089 if (i != NumOperands - 1)
3090 Mask <<= Shift;
3091 }
Evan Cheng63d33002006-03-22 08:01:21 +00003092 return Mask;
3093}
3094
Evan Cheng506d3df2006-03-29 23:07:14 +00003095/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003096/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003097unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003098 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003099 unsigned Mask = 0;
3100 // 8 nodes, but we only care about the last 4.
3101 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003102 int Val = SVOp->getMaskElt(i);
3103 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003104 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003105 if (i != 4)
3106 Mask <<= 2;
3107 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003108 return Mask;
3109}
3110
3111/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003112/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003113unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003114 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003115 unsigned Mask = 0;
3116 // 8 nodes, but we only care about the first 4.
3117 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003118 int Val = SVOp->getMaskElt(i);
3119 if (Val >= 0)
3120 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003121 if (i != 0)
3122 Mask <<= 2;
3123 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003124 return Mask;
3125}
3126
Nate Begemana09008b2009-10-19 02:17:23 +00003127/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3128/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3129unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3130 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3131 EVT VVT = N->getValueType(0);
3132 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3133 int Val = 0;
3134
3135 unsigned i, e;
3136 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3137 Val = SVOp->getMaskElt(i);
3138 if (Val >= 0)
3139 break;
3140 }
3141 return (Val - i) * EltSize;
3142}
3143
Evan Cheng37b73872009-07-30 08:33:02 +00003144/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3145/// constant +0.0.
3146bool X86::isZeroNode(SDValue Elt) {
3147 return ((isa<ConstantSDNode>(Elt) &&
3148 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3149 (isa<ConstantFPSDNode>(Elt) &&
3150 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3151}
3152
Nate Begeman9008ca62009-04-27 18:41:29 +00003153/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3154/// their permute mask.
3155static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3156 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003157 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003158 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003159 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Nate Begeman5a5ca152009-04-29 05:20:52 +00003161 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003162 int idx = SVOp->getMaskElt(i);
3163 if (idx < 0)
3164 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003165 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003166 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003167 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003168 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003169 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003170 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3171 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003172}
3173
Evan Cheng779ccea2007-12-07 21:30:01 +00003174/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3175/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003176static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003177 unsigned NumElems = VT.getVectorNumElements();
3178 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003179 int idx = Mask[i];
3180 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003181 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003182 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003184 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003185 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003186 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003187}
3188
Evan Cheng533a0aa2006-04-19 20:35:22 +00003189/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3190/// match movhlps. The lower half elements should come from upper half of
3191/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003192/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003193static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3194 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003195 return false;
3196 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003198 return false;
3199 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003200 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003201 return false;
3202 return true;
3203}
3204
Evan Cheng5ced1d82006-04-06 23:23:56 +00003205/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003206/// is promoted to a vector. It also returns the LoadSDNode by reference if
3207/// required.
3208static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003209 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3210 return false;
3211 N = N->getOperand(0).getNode();
3212 if (!ISD::isNON_EXTLoad(N))
3213 return false;
3214 if (LD)
3215 *LD = cast<LoadSDNode>(N);
3216 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003217}
3218
Evan Cheng533a0aa2006-04-19 20:35:22 +00003219/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3220/// match movlp{s|d}. The lower half elements should come from lower half of
3221/// V1 (and in order), and the upper half elements should come from the upper
3222/// half of V2 (and in order). And since V1 will become the source of the
3223/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003224static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3225 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003226 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003227 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003228 // Is V2 is a vector load, don't do this transformation. We will try to use
3229 // load folding shufps op.
3230 if (ISD::isNON_EXTLoad(V2))
3231 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003232
Nate Begeman5a5ca152009-04-29 05:20:52 +00003233 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003234
Evan Cheng533a0aa2006-04-19 20:35:22 +00003235 if (NumElems != 2 && NumElems != 4)
3236 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003237 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003238 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003239 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003240 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003242 return false;
3243 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003244}
3245
Evan Cheng39623da2006-04-20 08:58:49 +00003246/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3247/// all the same.
3248static bool isSplatVector(SDNode *N) {
3249 if (N->getOpcode() != ISD::BUILD_VECTOR)
3250 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003251
Dan Gohman475871a2008-07-27 21:46:04 +00003252 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003253 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3254 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003255 return false;
3256 return true;
3257}
3258
Evan Cheng213d2cf2007-05-17 18:45:50 +00003259/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003260/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003261/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003262static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003263 SDValue V1 = N->getOperand(0);
3264 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003265 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3266 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003267 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003268 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003269 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003270 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3271 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003272 if (Opc != ISD::BUILD_VECTOR ||
3273 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003274 return false;
3275 } else if (Idx >= 0) {
3276 unsigned Opc = V1.getOpcode();
3277 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3278 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003279 if (Opc != ISD::BUILD_VECTOR ||
3280 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003281 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003282 }
3283 }
3284 return true;
3285}
3286
3287/// getZeroVector - Returns a vector of specified type with all zero elements.
3288///
Owen Andersone50ed302009-08-10 22:56:29 +00003289static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003290 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003291 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003292
Chris Lattner8a594482007-11-25 00:24:49 +00003293 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3294 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003295 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003296 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3298 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003299 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3301 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003302 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003303 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3304 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003305 }
Dale Johannesenace16102009-02-03 19:33:06 +00003306 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003307}
3308
Chris Lattner8a594482007-11-25 00:24:49 +00003309/// getOnesVector - Returns a vector of specified type with all bits set.
3310///
Owen Andersone50ed302009-08-10 22:56:29 +00003311static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003312 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003313
Chris Lattner8a594482007-11-25 00:24:49 +00003314 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3315 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003317 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003318 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003320 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003321 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003322 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003323}
3324
3325
Evan Cheng39623da2006-04-20 08:58:49 +00003326/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3327/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003328static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003329 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003330 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003331
Evan Cheng39623da2006-04-20 08:58:49 +00003332 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003333 SmallVector<int, 8> MaskVec;
3334 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003335
Nate Begeman5a5ca152009-04-29 05:20:52 +00003336 for (unsigned i = 0; i != NumElems; ++i) {
3337 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003338 MaskVec[i] = NumElems;
3339 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003340 }
Evan Cheng39623da2006-04-20 08:58:49 +00003341 }
Evan Cheng39623da2006-04-20 08:58:49 +00003342 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003343 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3344 SVOp->getOperand(1), &MaskVec[0]);
3345 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003346}
3347
Evan Cheng017dcc62006-04-21 01:05:10 +00003348/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3349/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003350static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 SDValue V2) {
3352 unsigned NumElems = VT.getVectorNumElements();
3353 SmallVector<int, 8> Mask;
3354 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003355 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003356 Mask.push_back(i);
3357 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003358}
3359
Nate Begeman9008ca62009-04-27 18:41:29 +00003360/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003361static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 SDValue V2) {
3363 unsigned NumElems = VT.getVectorNumElements();
3364 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003365 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003366 Mask.push_back(i);
3367 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003368 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003370}
3371
Nate Begeman9008ca62009-04-27 18:41:29 +00003372/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003373static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 SDValue V2) {
3375 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003376 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003377 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003378 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 Mask.push_back(i + Half);
3380 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003381 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003382 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003383}
3384
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003385/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003386static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 bool HasSSE2) {
3388 if (SV->getValueType(0).getVectorNumElements() <= 4)
3389 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003390
Owen Anderson825b72b2009-08-11 20:47:22 +00003391 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003392 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003393 DebugLoc dl = SV->getDebugLoc();
3394 SDValue V1 = SV->getOperand(0);
3395 int NumElems = VT.getVectorNumElements();
3396 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003397
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 // unpack elements to the correct location
3399 while (NumElems > 4) {
3400 if (EltNo < NumElems/2) {
3401 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3402 } else {
3403 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3404 EltNo -= NumElems/2;
3405 }
3406 NumElems >>= 1;
3407 }
Eric Christopherfd179292009-08-27 18:07:15 +00003408
Nate Begeman9008ca62009-04-27 18:41:29 +00003409 // Perform the splat.
3410 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003411 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003412 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3413 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003414}
3415
Evan Chengba05f722006-04-21 23:03:30 +00003416/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003417/// vector of zero or undef vector. This produces a shuffle where the low
3418/// element of V2 is swizzled into the zero/undef vector, landing at element
3419/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00003420static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003421 bool isZero, bool HasSSE2,
3422 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003423 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003424 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003425 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3426 unsigned NumElems = VT.getVectorNumElements();
3427 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003428 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 // If this is the insertion idx, put the low elt of V2 here.
3430 MaskVec.push_back(i == Idx ? NumElems : i);
3431 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003432}
3433
Evan Chengf26ffe92008-05-29 08:22:04 +00003434/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3435/// a shuffle that is zero.
3436static
Nate Begeman9008ca62009-04-27 18:41:29 +00003437unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3438 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003439 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003440 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003441 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003442 int Idx = SVOp->getMaskElt(Index);
3443 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003444 ++NumZeros;
3445 continue;
3446 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003447 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003448 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003449 ++NumZeros;
3450 else
3451 break;
3452 }
3453 return NumZeros;
3454}
3455
3456/// isVectorShift - Returns true if the shuffle can be implemented as a
3457/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003458/// FIXME: split into pslldqi, psrldqi, palignr variants.
3459static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003460 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003461 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003462
3463 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003465 if (!NumZeros) {
3466 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003467 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003468 if (!NumZeros)
3469 return false;
3470 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003471 bool SeenV1 = false;
3472 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003473 for (unsigned i = NumZeros; i < NumElems; ++i) {
3474 unsigned Val = isLeft ? (i - NumZeros) : i;
3475 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3476 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003477 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003478 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003479 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003480 SeenV1 = true;
3481 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003482 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003483 SeenV2 = true;
3484 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003485 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003486 return false;
3487 }
3488 if (SeenV1 && SeenV2)
3489 return false;
3490
Nate Begeman9008ca62009-04-27 18:41:29 +00003491 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003492 ShAmt = NumZeros;
3493 return true;
3494}
3495
3496
Evan Chengc78d3b42006-04-24 18:01:45 +00003497/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3498///
Dan Gohman475871a2008-07-27 21:46:04 +00003499static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003500 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003501 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003502 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003503 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003504
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003505 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003506 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003507 bool First = true;
3508 for (unsigned i = 0; i < 16; ++i) {
3509 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3510 if (ThisIsNonZero && First) {
3511 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003512 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003513 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003514 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003515 First = false;
3516 }
3517
3518 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003519 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003520 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3521 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003522 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003524 }
3525 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3527 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3528 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003529 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003531 } else
3532 ThisElt = LastElt;
3533
Gabor Greifba36cb52008-08-28 21:40:38 +00003534 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003536 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003537 }
3538 }
3539
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003541}
3542
Bill Wendlinga348c562007-03-22 18:42:45 +00003543/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003544///
Dan Gohman475871a2008-07-27 21:46:04 +00003545static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003546 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003547 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003548 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003549 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003550
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003551 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003552 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003553 bool First = true;
3554 for (unsigned i = 0; i < 8; ++i) {
3555 bool isNonZero = (NonZeros & (1 << i)) != 0;
3556 if (isNonZero) {
3557 if (First) {
3558 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003559 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003560 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003562 First = false;
3563 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003564 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003566 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003567 }
3568 }
3569
3570 return V;
3571}
3572
Evan Chengf26ffe92008-05-29 08:22:04 +00003573/// getVShift - Return a vector logical shift node.
3574///
Owen Andersone50ed302009-08-10 22:56:29 +00003575static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003576 unsigned NumBits, SelectionDAG &DAG,
3577 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003578 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003580 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003581 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3582 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3583 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003584 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003585}
3586
Dan Gohman475871a2008-07-27 21:46:04 +00003587SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003588X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
3589 SelectionDAG &DAG) {
3590
3591 // Check if the scalar load can be widened into a vector load. And if
3592 // the address is "base + cst" see if the cst can be "absorbed" into
3593 // the shuffle mask.
3594 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3595 SDValue Ptr = LD->getBasePtr();
3596 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3597 return SDValue();
3598 EVT PVT = LD->getValueType(0);
3599 if (PVT != MVT::i32 && PVT != MVT::f32)
3600 return SDValue();
3601
3602 int FI = -1;
3603 int64_t Offset = 0;
3604 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3605 FI = FINode->getIndex();
3606 Offset = 0;
3607 } else if (Ptr.getOpcode() == ISD::ADD &&
3608 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3609 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3610 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3611 Offset = Ptr.getConstantOperandVal(1);
3612 Ptr = Ptr.getOperand(0);
3613 } else {
3614 return SDValue();
3615 }
3616
3617 SDValue Chain = LD->getChain();
3618 // Make sure the stack object alignment is at least 16.
3619 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3620 if (DAG.InferPtrAlignment(Ptr) < 16) {
3621 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003622 // Can't change the alignment. FIXME: It's possible to compute
3623 // the exact stack offset and reference FI + adjust offset instead.
3624 // If someone *really* cares about this. That's the way to implement it.
3625 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003626 } else {
3627 MFI->setObjectAlignment(FI, 16);
3628 }
3629 }
3630
3631 // (Offset % 16) must be multiple of 4. Then address is then
3632 // Ptr + (Offset & ~15).
3633 if (Offset < 0)
3634 return SDValue();
3635 if ((Offset % 16) & 3)
3636 return SDValue();
3637 int64_t StartOffset = Offset & ~15;
3638 if (StartOffset)
3639 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3640 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3641
3642 int EltNo = (Offset - StartOffset) >> 2;
3643 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3644 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003645 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3646 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003647 // Canonicalize it to a v4i32 shuffle.
3648 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3649 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3650 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3651 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3652 }
3653
3654 return SDValue();
3655}
3656
Nate Begeman1449f292010-03-24 22:19:06 +00003657/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3658/// vector of type 'VT', see if the elements can be replaced by a single large
3659/// load which has the same value as a build_vector whose operands are 'elts'.
3660///
3661/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3662///
3663/// FIXME: we'd also like to handle the case where the last elements are zero
3664/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3665/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003666static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3667 DebugLoc &dl, SelectionDAG &DAG) {
3668 EVT EltVT = VT.getVectorElementType();
3669 unsigned NumElems = Elts.size();
3670
Nate Begemanfdea31a2010-03-24 20:49:50 +00003671 LoadSDNode *LDBase = NULL;
3672 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003673
3674 // For each element in the initializer, see if we've found a load or an undef.
3675 // If we don't find an initial load element, or later load elements are
3676 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003677 for (unsigned i = 0; i < NumElems; ++i) {
3678 SDValue Elt = Elts[i];
3679
3680 if (!Elt.getNode() ||
3681 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3682 return SDValue();
3683 if (!LDBase) {
3684 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3685 return SDValue();
3686 LDBase = cast<LoadSDNode>(Elt.getNode());
3687 LastLoadedElt = i;
3688 continue;
3689 }
3690 if (Elt.getOpcode() == ISD::UNDEF)
3691 continue;
3692
3693 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3694 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3695 return SDValue();
3696 LastLoadedElt = i;
3697 }
Nate Begeman1449f292010-03-24 22:19:06 +00003698
3699 // If we have found an entire vector of loads and undefs, then return a large
3700 // load of the entire vector width starting at the base pointer. If we found
3701 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003702 if (LastLoadedElt == NumElems - 1) {
3703 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3704 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3705 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3706 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3707 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3708 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3709 LDBase->isVolatile(), LDBase->isNonTemporal(),
3710 LDBase->getAlignment());
3711 } else if (NumElems == 4 && LastLoadedElt == 1) {
3712 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3713 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3714 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3715 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3716 }
3717 return SDValue();
3718}
3719
Evan Chengc3630942009-12-09 21:00:30 +00003720SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00003721X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003722 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003723 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003724 if (ISD::isBuildVectorAllZeros(Op.getNode())
3725 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003726 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3727 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3728 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003729 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003730 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003731
Gabor Greifba36cb52008-08-28 21:40:38 +00003732 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003733 return getOnesVector(Op.getValueType(), DAG, dl);
3734 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003735 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003736
Owen Andersone50ed302009-08-10 22:56:29 +00003737 EVT VT = Op.getValueType();
3738 EVT ExtVT = VT.getVectorElementType();
3739 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003740
3741 unsigned NumElems = Op.getNumOperands();
3742 unsigned NumZero = 0;
3743 unsigned NumNonZero = 0;
3744 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003745 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003746 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003747 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003748 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003749 if (Elt.getOpcode() == ISD::UNDEF)
3750 continue;
3751 Values.insert(Elt);
3752 if (Elt.getOpcode() != ISD::Constant &&
3753 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003754 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003755 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003756 NumZero++;
3757 else {
3758 NonZeros |= (1 << i);
3759 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003760 }
3761 }
3762
Dan Gohman7f321562007-06-25 16:23:39 +00003763 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003764 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003765 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003766 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003767
Chris Lattner67f453a2008-03-09 05:42:06 +00003768 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003769 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003770 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003771 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003772
Chris Lattner62098042008-03-09 01:05:04 +00003773 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3774 // the value are obviously zero, truncate the value to i32 and do the
3775 // insertion that way. Only do this if the value is non-constant or if the
3776 // value is a constant being inserted into element 0. It is cheaper to do
3777 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003778 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003779 (!IsAllConstants || Idx == 0)) {
3780 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3781 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003782 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3783 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003784
Chris Lattner62098042008-03-09 01:05:04 +00003785 // Truncate the value (which may itself be a constant) to i32, and
3786 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003787 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003788 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003789 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3790 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003791
Chris Lattner62098042008-03-09 01:05:04 +00003792 // Now we have our 32-bit value zero extended in the low element of
3793 // a vector. If Idx != 0, swizzle it into place.
3794 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 SmallVector<int, 4> Mask;
3796 Mask.push_back(Idx);
3797 for (unsigned i = 1; i != VecElts; ++i)
3798 Mask.push_back(i);
3799 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003800 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003801 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003802 }
Dale Johannesenace16102009-02-03 19:33:06 +00003803 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003804 }
3805 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003806
Chris Lattner19f79692008-03-08 22:59:52 +00003807 // If we have a constant or non-constant insertion into the low element of
3808 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3809 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003810 // depending on what the source datatype is.
3811 if (Idx == 0) {
3812 if (NumZero == 0) {
3813 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003814 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3815 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003816 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3817 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3818 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3819 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003820 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3821 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3822 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003823 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3824 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3825 Subtarget->hasSSE2(), DAG);
3826 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3827 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003828 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003829
3830 // Is it a vector logical left shift?
3831 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003832 X86::isZeroNode(Op.getOperand(0)) &&
3833 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003834 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003835 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003836 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003837 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003838 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003839 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003840
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003841 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003842 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003843
Chris Lattner19f79692008-03-08 22:59:52 +00003844 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3845 // is a non-constant being inserted into an element other than the low one,
3846 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3847 // movd/movss) to move this into the low element, then shuffle it into
3848 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003849 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003850 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003851
Evan Cheng0db9fe62006-04-25 20:13:52 +00003852 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003853 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3854 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003855 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003856 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003857 MaskVec.push_back(i == Idx ? 0 : 1);
3858 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003859 }
3860 }
3861
Chris Lattner67f453a2008-03-09 05:42:06 +00003862 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003863 if (Values.size() == 1) {
3864 if (EVTBits == 32) {
3865 // Instead of a shuffle like this:
3866 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3867 // Check if it's possible to issue this instead.
3868 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3869 unsigned Idx = CountTrailingZeros_32(NonZeros);
3870 SDValue Item = Op.getOperand(Idx);
3871 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3872 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3873 }
Dan Gohman475871a2008-07-27 21:46:04 +00003874 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003875 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003876
Dan Gohmana3941172007-07-24 22:55:08 +00003877 // A vector full of immediates; various special cases are already
3878 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003879 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003880 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003881
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003882 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003883 if (EVTBits == 64) {
3884 if (NumNonZero == 1) {
3885 // One half is zero or undef.
3886 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003887 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003888 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003889 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3890 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003891 }
Dan Gohman475871a2008-07-27 21:46:04 +00003892 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003893 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003894
3895 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003896 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003897 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003898 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003899 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003900 }
3901
Bill Wendling826f36f2007-03-28 00:57:11 +00003902 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003903 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003904 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003905 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003906 }
3907
3908 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003909 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003910 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003911 if (NumElems == 4 && NumZero > 0) {
3912 for (unsigned i = 0; i < 4; ++i) {
3913 bool isZero = !(NonZeros & (1 << i));
3914 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003915 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003916 else
Dale Johannesenace16102009-02-03 19:33:06 +00003917 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003918 }
3919
3920 for (unsigned i = 0; i < 2; ++i) {
3921 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3922 default: break;
3923 case 0:
3924 V[i] = V[i*2]; // Must be a zero vector.
3925 break;
3926 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003927 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003928 break;
3929 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003931 break;
3932 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003933 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003934 break;
3935 }
3936 }
3937
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003939 bool Reverse = (NonZeros & 0x3) == 2;
3940 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003941 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003942 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3943 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003944 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3945 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003946 }
3947
Nate Begemanfdea31a2010-03-24 20:49:50 +00003948 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3949 // Check for a build vector of consecutive loads.
3950 for (unsigned i = 0; i < NumElems; ++i)
3951 V[i] = Op.getOperand(i);
3952
3953 // Check for elements which are consecutive loads.
3954 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3955 if (LD.getNode())
3956 return LD;
3957
3958 // For SSE 4.1, use inserts into undef.
3959 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003960 V[0] = DAG.getUNDEF(VT);
3961 for (unsigned i = 0; i < NumElems; ++i)
3962 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3963 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3964 Op.getOperand(i), DAG.getIntPtrConstant(i));
3965 return V[0];
3966 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00003967
3968 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00003969 // e.g. for v4f32
3970 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3971 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3972 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003973 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003974 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003975 NumElems >>= 1;
3976 while (NumElems != 0) {
3977 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003978 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003979 NumElems >>= 1;
3980 }
3981 return V[0];
3982 }
Dan Gohman475871a2008-07-27 21:46:04 +00003983 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003984}
3985
Mon P Wangeb38ebf2010-01-24 00:05:03 +00003986SDValue
3987X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
3988 // We support concatenate two MMX registers and place them in a MMX
3989 // register. This is better than doing a stack convert.
3990 DebugLoc dl = Op.getDebugLoc();
3991 EVT ResVT = Op.getValueType();
3992 assert(Op.getNumOperands() == 2);
3993 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
3994 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
3995 int Mask[2];
3996 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
3997 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
3998 InVec = Op.getOperand(1);
3999 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4000 unsigned NumElts = ResVT.getVectorNumElements();
4001 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4002 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4003 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4004 } else {
4005 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4006 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4007 Mask[0] = 0; Mask[1] = 2;
4008 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4009 }
4010 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4011}
4012
Nate Begemanb9a47b82009-02-23 08:49:38 +00004013// v8i16 shuffles - Prefer shuffles in the following order:
4014// 1. [all] pshuflw, pshufhw, optional move
4015// 2. [ssse3] 1 x pshufb
4016// 3. [ssse3] 2 x pshufb + 1 x por
4017// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004018static
Nate Begeman9008ca62009-04-27 18:41:29 +00004019SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
4020 SelectionDAG &DAG, X86TargetLowering &TLI) {
4021 SDValue V1 = SVOp->getOperand(0);
4022 SDValue V2 = SVOp->getOperand(1);
4023 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004024 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004025
Nate Begemanb9a47b82009-02-23 08:49:38 +00004026 // Determine if more than 1 of the words in each of the low and high quadwords
4027 // of the result come from the same quadword of one of the two inputs. Undef
4028 // mask values count as coming from any quadword, for better codegen.
4029 SmallVector<unsigned, 4> LoQuad(4);
4030 SmallVector<unsigned, 4> HiQuad(4);
4031 BitVector InputQuads(4);
4032 for (unsigned i = 0; i < 8; ++i) {
4033 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004034 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004035 MaskVals.push_back(EltIdx);
4036 if (EltIdx < 0) {
4037 ++Quad[0];
4038 ++Quad[1];
4039 ++Quad[2];
4040 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004041 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004042 }
4043 ++Quad[EltIdx / 4];
4044 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004045 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004046
Nate Begemanb9a47b82009-02-23 08:49:38 +00004047 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004048 unsigned MaxQuad = 1;
4049 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004050 if (LoQuad[i] > MaxQuad) {
4051 BestLoQuad = i;
4052 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004053 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004054 }
4055
Nate Begemanb9a47b82009-02-23 08:49:38 +00004056 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004057 MaxQuad = 1;
4058 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004059 if (HiQuad[i] > MaxQuad) {
4060 BestHiQuad = i;
4061 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004062 }
4063 }
4064
Nate Begemanb9a47b82009-02-23 08:49:38 +00004065 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004066 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004067 // single pshufb instruction is necessary. If There are more than 2 input
4068 // quads, disable the next transformation since it does not help SSSE3.
4069 bool V1Used = InputQuads[0] || InputQuads[1];
4070 bool V2Used = InputQuads[2] || InputQuads[3];
4071 if (TLI.getSubtarget()->hasSSSE3()) {
4072 if (InputQuads.count() == 2 && V1Used && V2Used) {
4073 BestLoQuad = InputQuads.find_first();
4074 BestHiQuad = InputQuads.find_next(BestLoQuad);
4075 }
4076 if (InputQuads.count() > 2) {
4077 BestLoQuad = -1;
4078 BestHiQuad = -1;
4079 }
4080 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004081
Nate Begemanb9a47b82009-02-23 08:49:38 +00004082 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4083 // the shuffle mask. If a quad is scored as -1, that means that it contains
4084 // words from all 4 input quadwords.
4085 SDValue NewV;
4086 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004087 SmallVector<int, 8> MaskV;
4088 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4089 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004090 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004091 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4092 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4093 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004094
Nate Begemanb9a47b82009-02-23 08:49:38 +00004095 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4096 // source words for the shuffle, to aid later transformations.
4097 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004098 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004099 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004100 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004101 if (idx != (int)i)
4102 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004103 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004104 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004105 AllWordsInNewV = false;
4106 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004107 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004108
Nate Begemanb9a47b82009-02-23 08:49:38 +00004109 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4110 if (AllWordsInNewV) {
4111 for (int i = 0; i != 8; ++i) {
4112 int idx = MaskVals[i];
4113 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004114 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004115 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004116 if ((idx != i) && idx < 4)
4117 pshufhw = false;
4118 if ((idx != i) && idx > 3)
4119 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004120 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004121 V1 = NewV;
4122 V2Used = false;
4123 BestLoQuad = 0;
4124 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004125 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004126
Nate Begemanb9a47b82009-02-23 08:49:38 +00004127 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4128 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004129 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004130 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004131 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004132 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004133 }
Eric Christopherfd179292009-08-27 18:07:15 +00004134
Nate Begemanb9a47b82009-02-23 08:49:38 +00004135 // If we have SSSE3, and all words of the result are from 1 input vector,
4136 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4137 // is present, fall back to case 4.
4138 if (TLI.getSubtarget()->hasSSSE3()) {
4139 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004140
Nate Begemanb9a47b82009-02-23 08:49:38 +00004141 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004142 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004143 // mask, and elements that come from V1 in the V2 mask, so that the two
4144 // results can be OR'd together.
4145 bool TwoInputs = V1Used && V2Used;
4146 for (unsigned i = 0; i != 8; ++i) {
4147 int EltIdx = MaskVals[i] * 2;
4148 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004149 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4150 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004151 continue;
4152 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004153 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4154 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004155 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004157 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004158 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004160 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004162
Nate Begemanb9a47b82009-02-23 08:49:38 +00004163 // Calculate the shuffle mask for the second input, shuffle it, and
4164 // OR it with the first shuffled input.
4165 pshufbMask.clear();
4166 for (unsigned i = 0; i != 8; ++i) {
4167 int EltIdx = MaskVals[i] * 2;
4168 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4170 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004171 continue;
4172 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004173 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4174 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004175 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004176 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004177 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004178 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 MVT::v16i8, &pshufbMask[0], 16));
4180 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4181 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004182 }
4183
4184 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4185 // and update MaskVals with new element order.
4186 BitVector InOrder(8);
4187 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004188 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004189 for (int i = 0; i != 4; ++i) {
4190 int idx = MaskVals[i];
4191 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004192 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004193 InOrder.set(i);
4194 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004195 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004196 InOrder.set(i);
4197 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004198 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004199 }
4200 }
4201 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004202 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004203 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004204 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004205 }
Eric Christopherfd179292009-08-27 18:07:15 +00004206
Nate Begemanb9a47b82009-02-23 08:49:38 +00004207 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4208 // and update MaskVals with the new element order.
4209 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004210 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004211 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004212 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004213 for (unsigned i = 4; i != 8; ++i) {
4214 int idx = MaskVals[i];
4215 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004216 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004217 InOrder.set(i);
4218 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004219 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004220 InOrder.set(i);
4221 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004222 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004223 }
4224 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004226 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004227 }
Eric Christopherfd179292009-08-27 18:07:15 +00004228
Nate Begemanb9a47b82009-02-23 08:49:38 +00004229 // In case BestHi & BestLo were both -1, which means each quadword has a word
4230 // from each of the four input quadwords, calculate the InOrder bitvector now
4231 // before falling through to the insert/extract cleanup.
4232 if (BestLoQuad == -1 && BestHiQuad == -1) {
4233 NewV = V1;
4234 for (int i = 0; i != 8; ++i)
4235 if (MaskVals[i] < 0 || MaskVals[i] == i)
4236 InOrder.set(i);
4237 }
Eric Christopherfd179292009-08-27 18:07:15 +00004238
Nate Begemanb9a47b82009-02-23 08:49:38 +00004239 // The other elements are put in the right place using pextrw and pinsrw.
4240 for (unsigned i = 0; i != 8; ++i) {
4241 if (InOrder[i])
4242 continue;
4243 int EltIdx = MaskVals[i];
4244 if (EltIdx < 0)
4245 continue;
4246 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004247 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004248 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004250 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004251 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004252 DAG.getIntPtrConstant(i));
4253 }
4254 return NewV;
4255}
4256
4257// v16i8 shuffles - Prefer shuffles in the following order:
4258// 1. [ssse3] 1 x pshufb
4259// 2. [ssse3] 2 x pshufb + 1 x por
4260// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4261static
Nate Begeman9008ca62009-04-27 18:41:29 +00004262SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
4263 SelectionDAG &DAG, X86TargetLowering &TLI) {
4264 SDValue V1 = SVOp->getOperand(0);
4265 SDValue V2 = SVOp->getOperand(1);
4266 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004267 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004268 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004269
Nate Begemanb9a47b82009-02-23 08:49:38 +00004270 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004271 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004272 // present, fall back to case 3.
4273 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4274 bool V1Only = true;
4275 bool V2Only = true;
4276 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004277 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004278 if (EltIdx < 0)
4279 continue;
4280 if (EltIdx < 16)
4281 V2Only = false;
4282 else
4283 V1Only = false;
4284 }
Eric Christopherfd179292009-08-27 18:07:15 +00004285
Nate Begemanb9a47b82009-02-23 08:49:38 +00004286 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4287 if (TLI.getSubtarget()->hasSSSE3()) {
4288 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004289
Nate Begemanb9a47b82009-02-23 08:49:38 +00004290 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004291 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004292 //
4293 // Otherwise, we have elements from both input vectors, and must zero out
4294 // elements that come from V2 in the first mask, and V1 in the second mask
4295 // so that we can OR them together.
4296 bool TwoInputs = !(V1Only || V2Only);
4297 for (unsigned i = 0; i != 16; ++i) {
4298 int EltIdx = MaskVals[i];
4299 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004300 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004301 continue;
4302 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004303 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004304 }
4305 // If all the elements are from V2, assign it to V1 and return after
4306 // building the first pshufb.
4307 if (V2Only)
4308 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004309 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004310 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004311 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004312 if (!TwoInputs)
4313 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004314
Nate Begemanb9a47b82009-02-23 08:49:38 +00004315 // Calculate the shuffle mask for the second input, shuffle it, and
4316 // OR it with the first shuffled input.
4317 pshufbMask.clear();
4318 for (unsigned i = 0; i != 16; ++i) {
4319 int EltIdx = MaskVals[i];
4320 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004321 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004322 continue;
4323 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004324 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004325 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004326 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004327 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004328 MVT::v16i8, &pshufbMask[0], 16));
4329 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004330 }
Eric Christopherfd179292009-08-27 18:07:15 +00004331
Nate Begemanb9a47b82009-02-23 08:49:38 +00004332 // No SSSE3 - Calculate in place words and then fix all out of place words
4333 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4334 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004335 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4336 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004337 SDValue NewV = V2Only ? V2 : V1;
4338 for (int i = 0; i != 8; ++i) {
4339 int Elt0 = MaskVals[i*2];
4340 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004341
Nate Begemanb9a47b82009-02-23 08:49:38 +00004342 // This word of the result is all undef, skip it.
4343 if (Elt0 < 0 && Elt1 < 0)
4344 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004345
Nate Begemanb9a47b82009-02-23 08:49:38 +00004346 // This word of the result is already in the correct place, skip it.
4347 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4348 continue;
4349 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4350 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004351
Nate Begemanb9a47b82009-02-23 08:49:38 +00004352 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4353 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4354 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004355
4356 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4357 // using a single extract together, load it and store it.
4358 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004359 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004360 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004361 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004362 DAG.getIntPtrConstant(i));
4363 continue;
4364 }
4365
Nate Begemanb9a47b82009-02-23 08:49:38 +00004366 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004367 // source byte is not also odd, shift the extracted word left 8 bits
4368 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004369 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004370 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004371 DAG.getIntPtrConstant(Elt1 / 2));
4372 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004373 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004374 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004375 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004376 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4377 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004378 }
4379 // If Elt0 is defined, extract it from the appropriate source. If the
4380 // source byte is not also even, shift the extracted word right 8 bits. If
4381 // Elt1 was also defined, OR the extracted values together before
4382 // inserting them in the result.
4383 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004384 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004385 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4386 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004387 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004388 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004389 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004390 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4391 DAG.getConstant(0x00FF, MVT::i16));
4392 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004393 : InsElt0;
4394 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004395 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004396 DAG.getIntPtrConstant(i));
4397 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004398 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004399}
4400
Evan Cheng7a831ce2007-12-15 03:00:47 +00004401/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4402/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4403/// done when every pair / quad of shuffle mask elements point to elements in
4404/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004405/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4406static
Nate Begeman9008ca62009-04-27 18:41:29 +00004407SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4408 SelectionDAG &DAG,
4409 TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004410 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004411 SDValue V1 = SVOp->getOperand(0);
4412 SDValue V2 = SVOp->getOperand(1);
4413 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004414 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004415 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004416 EVT MaskEltVT = MaskVT.getVectorElementType();
4417 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004418 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004419 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004420 case MVT::v4f32: NewVT = MVT::v2f64; break;
4421 case MVT::v4i32: NewVT = MVT::v2i64; break;
4422 case MVT::v8i16: NewVT = MVT::v4i32; break;
4423 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004424 }
4425
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004426 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004427 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004428 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004429 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004430 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004431 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004432 int Scale = NumElems / NewWidth;
4433 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004434 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004435 int StartIdx = -1;
4436 for (int j = 0; j < Scale; ++j) {
4437 int EltIdx = SVOp->getMaskElt(i+j);
4438 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004439 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004440 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004441 StartIdx = EltIdx - (EltIdx % Scale);
4442 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004443 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004444 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004445 if (StartIdx == -1)
4446 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004447 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004448 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004449 }
4450
Dale Johannesenace16102009-02-03 19:33:06 +00004451 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4452 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004453 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004454}
4455
Evan Chengd880b972008-05-09 21:53:03 +00004456/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004457///
Owen Andersone50ed302009-08-10 22:56:29 +00004458static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004459 SDValue SrcOp, SelectionDAG &DAG,
4460 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004461 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004462 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004463 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004464 LD = dyn_cast<LoadSDNode>(SrcOp);
4465 if (!LD) {
4466 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4467 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004468 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4469 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004470 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4471 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004472 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004473 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004474 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004475 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4476 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4477 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4478 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004479 SrcOp.getOperand(0)
4480 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004481 }
4482 }
4483 }
4484
Dale Johannesenace16102009-02-03 19:33:06 +00004485 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4486 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004487 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004488 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004489}
4490
Evan Chengace3c172008-07-22 21:13:36 +00004491/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4492/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004493static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004494LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4495 SDValue V1 = SVOp->getOperand(0);
4496 SDValue V2 = SVOp->getOperand(1);
4497 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004498 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004499
Evan Chengace3c172008-07-22 21:13:36 +00004500 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004501 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004502 SmallVector<int, 8> Mask1(4U, -1);
4503 SmallVector<int, 8> PermMask;
4504 SVOp->getMask(PermMask);
4505
Evan Chengace3c172008-07-22 21:13:36 +00004506 unsigned NumHi = 0;
4507 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004508 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004509 int Idx = PermMask[i];
4510 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004511 Locs[i] = std::make_pair(-1, -1);
4512 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004513 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4514 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004515 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004516 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004517 NumLo++;
4518 } else {
4519 Locs[i] = std::make_pair(1, NumHi);
4520 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004521 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004522 NumHi++;
4523 }
4524 }
4525 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004526
Evan Chengace3c172008-07-22 21:13:36 +00004527 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004528 // If no more than two elements come from either vector. This can be
4529 // implemented with two shuffles. First shuffle gather the elements.
4530 // The second shuffle, which takes the first shuffle as both of its
4531 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004532 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004533
Nate Begeman9008ca62009-04-27 18:41:29 +00004534 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004535
Evan Chengace3c172008-07-22 21:13:36 +00004536 for (unsigned i = 0; i != 4; ++i) {
4537 if (Locs[i].first == -1)
4538 continue;
4539 else {
4540 unsigned Idx = (i < 2) ? 0 : 4;
4541 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004542 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004543 }
4544 }
4545
Nate Begeman9008ca62009-04-27 18:41:29 +00004546 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004547 } else if (NumLo == 3 || NumHi == 3) {
4548 // Otherwise, we must have three elements from one vector, call it X, and
4549 // one element from the other, call it Y. First, use a shufps to build an
4550 // intermediate vector with the one element from Y and the element from X
4551 // that will be in the same half in the final destination (the indexes don't
4552 // matter). Then, use a shufps to build the final vector, taking the half
4553 // containing the element from Y from the intermediate, and the other half
4554 // from X.
4555 if (NumHi == 3) {
4556 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004557 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004558 std::swap(V1, V2);
4559 }
4560
4561 // Find the element from V2.
4562 unsigned HiIndex;
4563 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004564 int Val = PermMask[HiIndex];
4565 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004566 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004567 if (Val >= 4)
4568 break;
4569 }
4570
Nate Begeman9008ca62009-04-27 18:41:29 +00004571 Mask1[0] = PermMask[HiIndex];
4572 Mask1[1] = -1;
4573 Mask1[2] = PermMask[HiIndex^1];
4574 Mask1[3] = -1;
4575 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004576
4577 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004578 Mask1[0] = PermMask[0];
4579 Mask1[1] = PermMask[1];
4580 Mask1[2] = HiIndex & 1 ? 6 : 4;
4581 Mask1[3] = HiIndex & 1 ? 4 : 6;
4582 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004583 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004584 Mask1[0] = HiIndex & 1 ? 2 : 0;
4585 Mask1[1] = HiIndex & 1 ? 0 : 2;
4586 Mask1[2] = PermMask[2];
4587 Mask1[3] = PermMask[3];
4588 if (Mask1[2] >= 0)
4589 Mask1[2] += 4;
4590 if (Mask1[3] >= 0)
4591 Mask1[3] += 4;
4592 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004593 }
Evan Chengace3c172008-07-22 21:13:36 +00004594 }
4595
4596 // Break it into (shuffle shuffle_hi, shuffle_lo).
4597 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004598 SmallVector<int,8> LoMask(4U, -1);
4599 SmallVector<int,8> HiMask(4U, -1);
4600
4601 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004602 unsigned MaskIdx = 0;
4603 unsigned LoIdx = 0;
4604 unsigned HiIdx = 2;
4605 for (unsigned i = 0; i != 4; ++i) {
4606 if (i == 2) {
4607 MaskPtr = &HiMask;
4608 MaskIdx = 1;
4609 LoIdx = 0;
4610 HiIdx = 2;
4611 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004612 int Idx = PermMask[i];
4613 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004614 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004615 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004616 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004617 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004618 LoIdx++;
4619 } else {
4620 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004621 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004622 HiIdx++;
4623 }
4624 }
4625
Nate Begeman9008ca62009-04-27 18:41:29 +00004626 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4627 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4628 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004629 for (unsigned i = 0; i != 4; ++i) {
4630 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004631 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004632 } else {
4633 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004634 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004635 }
4636 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004637 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004638}
4639
Dan Gohman475871a2008-07-27 21:46:04 +00004640SDValue
4641X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004642 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004643 SDValue V1 = Op.getOperand(0);
4644 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004645 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004646 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004647 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004648 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004649 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4650 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004651 bool V1IsSplat = false;
4652 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004653
Nate Begeman9008ca62009-04-27 18:41:29 +00004654 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004655 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004656
Nate Begeman9008ca62009-04-27 18:41:29 +00004657 // Promote splats to v4f32.
4658 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004659 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004660 return Op;
4661 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004662 }
4663
Evan Cheng7a831ce2007-12-15 03:00:47 +00004664 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4665 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004666 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004667 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004668 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004669 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004670 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004671 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004672 // FIXME: Figure out a cleaner way to do this.
4673 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004674 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004675 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004676 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4678 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4679 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004680 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004681 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004682 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4683 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004684 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004685 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004686 }
4687 }
Eric Christopherfd179292009-08-27 18:07:15 +00004688
Nate Begeman9008ca62009-04-27 18:41:29 +00004689 if (X86::isPSHUFDMask(SVOp))
4690 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004691
Evan Chengf26ffe92008-05-29 08:22:04 +00004692 // Check if this can be converted into a logical shift.
4693 bool isLeft = false;
4694 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004695 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004696 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004697 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004698 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004699 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004700 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004701 EVT EltVT = VT.getVectorElementType();
4702 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004703 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004704 }
Eric Christopherfd179292009-08-27 18:07:15 +00004705
Nate Begeman9008ca62009-04-27 18:41:29 +00004706 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004707 if (V1IsUndef)
4708 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004709 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004710 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004711 if (!isMMX)
4712 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004713 }
Eric Christopherfd179292009-08-27 18:07:15 +00004714
Nate Begeman9008ca62009-04-27 18:41:29 +00004715 // FIXME: fold these into legal mask.
4716 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4717 X86::isMOVSLDUPMask(SVOp) ||
4718 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004719 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004720 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004721 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004722
Nate Begeman9008ca62009-04-27 18:41:29 +00004723 if (ShouldXformToMOVHLPS(SVOp) ||
4724 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4725 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004726
Evan Chengf26ffe92008-05-29 08:22:04 +00004727 if (isShift) {
4728 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004729 EVT EltVT = VT.getVectorElementType();
4730 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004731 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004732 }
Eric Christopherfd179292009-08-27 18:07:15 +00004733
Evan Cheng9eca5e82006-10-25 21:49:50 +00004734 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004735 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4736 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004737 V1IsSplat = isSplatVector(V1.getNode());
4738 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004739
Chris Lattner8a594482007-11-25 00:24:49 +00004740 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004741 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004742 Op = CommuteVectorShuffle(SVOp, DAG);
4743 SVOp = cast<ShuffleVectorSDNode>(Op);
4744 V1 = SVOp->getOperand(0);
4745 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004746 std::swap(V1IsSplat, V2IsSplat);
4747 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004748 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004749 }
4750
Nate Begeman9008ca62009-04-27 18:41:29 +00004751 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4752 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004753 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004754 return V1;
4755 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4756 // the instruction selector will not match, so get a canonical MOVL with
4757 // swapped operands to undo the commute.
4758 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004759 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004760
Nate Begeman9008ca62009-04-27 18:41:29 +00004761 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4762 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4763 X86::isUNPCKLMask(SVOp) ||
4764 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004765 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004766
Evan Cheng9bbbb982006-10-25 20:48:19 +00004767 if (V2IsSplat) {
4768 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004769 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004770 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004771 SDValue NewMask = NormalizeMask(SVOp, DAG);
4772 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4773 if (NSVOp != SVOp) {
4774 if (X86::isUNPCKLMask(NSVOp, true)) {
4775 return NewMask;
4776 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4777 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004778 }
4779 }
4780 }
4781
Evan Cheng9eca5e82006-10-25 21:49:50 +00004782 if (Commuted) {
4783 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004784 // FIXME: this seems wrong.
4785 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4786 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4787 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4788 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4789 X86::isUNPCKLMask(NewSVOp) ||
4790 X86::isUNPCKHMask(NewSVOp))
4791 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004792 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004793
Nate Begemanb9a47b82009-02-23 08:49:38 +00004794 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004795
4796 // Normalize the node to match x86 shuffle ops if needed
4797 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4798 return CommuteVectorShuffle(SVOp, DAG);
4799
4800 // Check for legal shuffle and return?
4801 SmallVector<int, 16> PermMask;
4802 SVOp->getMask(PermMask);
4803 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004804 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004805
Evan Cheng14b32e12007-12-11 01:46:18 +00004806 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004807 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004808 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004809 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004810 return NewOp;
4811 }
4812
Owen Anderson825b72b2009-08-11 20:47:22 +00004813 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004814 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004815 if (NewOp.getNode())
4816 return NewOp;
4817 }
Eric Christopherfd179292009-08-27 18:07:15 +00004818
Evan Chengace3c172008-07-22 21:13:36 +00004819 // Handle all 4 wide cases with a number of shuffles except for MMX.
4820 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004821 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004822
Dan Gohman475871a2008-07-27 21:46:04 +00004823 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004824}
4825
Dan Gohman475871a2008-07-27 21:46:04 +00004826SDValue
4827X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004828 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004829 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004830 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004831 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004833 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004835 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004836 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004837 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004838 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4839 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4840 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004841 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4842 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004843 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004844 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004845 Op.getOperand(0)),
4846 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004848 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004849 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004850 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004851 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004852 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004853 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4854 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004855 // result has a single use which is a store or a bitcast to i32. And in
4856 // the case of a store, it's not worth it if the index is a constant 0,
4857 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004858 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004859 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004860 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004861 if ((User->getOpcode() != ISD::STORE ||
4862 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4863 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004864 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004865 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004866 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004867 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4868 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004869 Op.getOperand(0)),
4870 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004871 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4872 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004873 // ExtractPS works with constant index.
4874 if (isa<ConstantSDNode>(Op.getOperand(1)))
4875 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004876 }
Dan Gohman475871a2008-07-27 21:46:04 +00004877 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004878}
4879
4880
Dan Gohman475871a2008-07-27 21:46:04 +00004881SDValue
4882X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004883 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004884 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004885
Evan Cheng62a3f152008-03-24 21:52:23 +00004886 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004887 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004888 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004889 return Res;
4890 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004891
Owen Andersone50ed302009-08-10 22:56:29 +00004892 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004893 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004894 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004895 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004896 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004897 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004898 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004899 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4900 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004901 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004902 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004903 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004904 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004905 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004906 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004907 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004908 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004909 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004910 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004911 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004912 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004913 if (Idx == 0)
4914 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004915
Evan Cheng0db9fe62006-04-25 20:13:52 +00004916 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004917 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004918 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004919 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004920 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004921 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004922 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004923 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004924 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4925 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4926 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004927 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004928 if (Idx == 0)
4929 return Op;
4930
4931 // UNPCKHPD the element to the lowest double word, then movsd.
4932 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4933 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004934 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004935 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004936 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004937 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004938 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004939 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004940 }
4941
Dan Gohman475871a2008-07-27 21:46:04 +00004942 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004943}
4944
Dan Gohman475871a2008-07-27 21:46:04 +00004945SDValue
4946X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Owen Andersone50ed302009-08-10 22:56:29 +00004947 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004948 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004949 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004950
Dan Gohman475871a2008-07-27 21:46:04 +00004951 SDValue N0 = Op.getOperand(0);
4952 SDValue N1 = Op.getOperand(1);
4953 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004954
Dan Gohman8a55ce42009-09-23 21:02:20 +00004955 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004956 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004957 unsigned Opc;
4958 if (VT == MVT::v8i16)
4959 Opc = X86ISD::PINSRW;
4960 else if (VT == MVT::v4i16)
4961 Opc = X86ISD::MMX_PINSRW;
4962 else if (VT == MVT::v16i8)
4963 Opc = X86ISD::PINSRB;
4964 else
4965 Opc = X86ISD::PINSRB;
4966
Nate Begeman14d12ca2008-02-11 04:19:36 +00004967 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4968 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00004969 if (N1.getValueType() != MVT::i32)
4970 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
4971 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004972 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004973 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004974 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004975 // Bits [7:6] of the constant are the source select. This will always be
4976 // zero here. The DAG Combiner may combine an extract_elt index into these
4977 // bits. For example (insert (extract, 3), 2) could be matched by putting
4978 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004979 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004980 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004981 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004982 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004983 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004984 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00004985 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004986 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004987 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00004988 // PINSR* works with constant index.
4989 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004990 }
Dan Gohman475871a2008-07-27 21:46:04 +00004991 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004992}
4993
Dan Gohman475871a2008-07-27 21:46:04 +00004994SDValue
4995X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00004996 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004997 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004998
4999 if (Subtarget->hasSSE41())
5000 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5001
Dan Gohman8a55ce42009-09-23 21:02:20 +00005002 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005003 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005004
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005005 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005006 SDValue N0 = Op.getOperand(0);
5007 SDValue N1 = Op.getOperand(1);
5008 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005009
Dan Gohman8a55ce42009-09-23 21:02:20 +00005010 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005011 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5012 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005013 if (N1.getValueType() != MVT::i32)
5014 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5015 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005016 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005017 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5018 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005019 }
Dan Gohman475871a2008-07-27 21:46:04 +00005020 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005021}
5022
Dan Gohman475871a2008-07-27 21:46:04 +00005023SDValue
5024X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005025 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005026 if (Op.getValueType() == MVT::v2f32)
5027 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
5028 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5029 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00005030 Op.getOperand(0))));
5031
Owen Anderson825b72b2009-08-11 20:47:22 +00005032 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5033 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005034
Owen Anderson825b72b2009-08-11 20:47:22 +00005035 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5036 EVT VT = MVT::v2i32;
5037 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005038 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005039 case MVT::v16i8:
5040 case MVT::v8i16:
5041 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005042 break;
5043 }
Dale Johannesenace16102009-02-03 19:33:06 +00005044 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5045 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005046}
5047
Bill Wendling056292f2008-09-16 21:48:12 +00005048// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5049// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5050// one of the above mentioned nodes. It has to be wrapped because otherwise
5051// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5052// be used to form addressing mode. These wrapped nodes will be selected
5053// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005054SDValue
5055X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005056 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005057
Chris Lattner41621a22009-06-26 19:22:52 +00005058 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5059 // global base reg.
5060 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005061 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005062 CodeModel::Model M = getTargetMachine().getCodeModel();
5063
Chris Lattner4f066492009-07-11 20:29:19 +00005064 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005065 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005066 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005067 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005068 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005069 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005070 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005071
Evan Cheng1606e8e2009-03-13 07:51:59 +00005072 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005073 CP->getAlignment(),
5074 CP->getOffset(), OpFlag);
5075 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005076 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005077 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005078 if (OpFlag) {
5079 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005080 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005081 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005082 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005083 }
5084
5085 return Result;
5086}
5087
Chris Lattner18c59872009-06-27 04:16:01 +00005088SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
5089 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005090
Chris Lattner18c59872009-06-27 04:16:01 +00005091 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5092 // global base reg.
5093 unsigned char OpFlag = 0;
5094 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005095 CodeModel::Model M = getTargetMachine().getCodeModel();
5096
Chris Lattner4f066492009-07-11 20:29:19 +00005097 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005098 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005099 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005100 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005101 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005102 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005103 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005104
Chris Lattner18c59872009-06-27 04:16:01 +00005105 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5106 OpFlag);
5107 DebugLoc DL = JT->getDebugLoc();
5108 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005109
Chris Lattner18c59872009-06-27 04:16:01 +00005110 // With PIC, the address is actually $g + Offset.
5111 if (OpFlag) {
5112 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5113 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005114 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005115 Result);
5116 }
Eric Christopherfd179292009-08-27 18:07:15 +00005117
Chris Lattner18c59872009-06-27 04:16:01 +00005118 return Result;
5119}
5120
5121SDValue
5122X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
5123 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005124
Chris Lattner18c59872009-06-27 04:16:01 +00005125 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5126 // global base reg.
5127 unsigned char OpFlag = 0;
5128 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005129 CodeModel::Model M = getTargetMachine().getCodeModel();
5130
Chris Lattner4f066492009-07-11 20:29:19 +00005131 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005132 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005133 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005134 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005135 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005136 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005137 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005138
Chris Lattner18c59872009-06-27 04:16:01 +00005139 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005140
Chris Lattner18c59872009-06-27 04:16:01 +00005141 DebugLoc DL = Op.getDebugLoc();
5142 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005143
5144
Chris Lattner18c59872009-06-27 04:16:01 +00005145 // With PIC, the address is actually $g + Offset.
5146 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005147 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005148 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5149 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005150 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005151 Result);
5152 }
Eric Christopherfd179292009-08-27 18:07:15 +00005153
Chris Lattner18c59872009-06-27 04:16:01 +00005154 return Result;
5155}
5156
Dan Gohman475871a2008-07-27 21:46:04 +00005157SDValue
Dan Gohmanf705adb2009-10-30 01:28:02 +00005158X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) {
Dan Gohman29cbade2009-11-20 23:18:13 +00005159 // Create the TargetBlockAddressAddress node.
5160 unsigned char OpFlags =
5161 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005162 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005163 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005164 DebugLoc dl = Op.getDebugLoc();
5165 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5166 /*isTarget=*/true, OpFlags);
5167
Dan Gohmanf705adb2009-10-30 01:28:02 +00005168 if (Subtarget->isPICStyleRIPRel() &&
5169 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005170 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5171 else
5172 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005173
Dan Gohman29cbade2009-11-20 23:18:13 +00005174 // With PIC, the address is actually $g + Offset.
5175 if (isGlobalRelativeToPICBase(OpFlags)) {
5176 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5177 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5178 Result);
5179 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005180
5181 return Result;
5182}
5183
5184SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005185X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005186 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005187 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005188 // Create the TargetGlobalAddress node, folding in the constant
5189 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005190 unsigned char OpFlags =
5191 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005192 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005193 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005194 if (OpFlags == X86II::MO_NO_FLAG &&
5195 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005196 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005197 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005198 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005199 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005200 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005201 }
Eric Christopherfd179292009-08-27 18:07:15 +00005202
Chris Lattner4f066492009-07-11 20:29:19 +00005203 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005204 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005205 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5206 else
5207 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005208
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005209 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005210 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005211 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5212 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005213 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005214 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005215
Chris Lattner36c25012009-07-10 07:34:39 +00005216 // For globals that require a load from a stub to get the address, emit the
5217 // load.
5218 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005219 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005220 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005221
Dan Gohman6520e202008-10-18 02:06:02 +00005222 // If there was a non-zero offset that we didn't fold, create an explicit
5223 // addition for it.
5224 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005225 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005226 DAG.getConstant(Offset, getPointerTy()));
5227
Evan Cheng0db9fe62006-04-25 20:13:52 +00005228 return Result;
5229}
5230
Evan Chengda43bcf2008-09-24 00:05:32 +00005231SDValue
5232X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
5233 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005234 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005235 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005236}
5237
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005238static SDValue
5239GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005240 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005241 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005242 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005243 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005244 DebugLoc dl = GA->getDebugLoc();
5245 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5246 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005247 GA->getOffset(),
5248 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005249 if (InFlag) {
5250 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005251 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005252 } else {
5253 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005254 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005255 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005256
5257 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
5258 MFI->setHasCalls(true);
5259
Rafael Espindola15f1b662009-04-24 12:59:40 +00005260 SDValue Flag = Chain.getValue(1);
5261 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005262}
5263
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005264// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005265static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005266LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005267 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005268 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005269 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5270 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005271 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005272 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005273 InFlag = Chain.getValue(1);
5274
Chris Lattnerb903bed2009-06-26 21:20:29 +00005275 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005276}
5277
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005278// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005279static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005280LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005281 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005282 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5283 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005284}
5285
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005286// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5287// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005288static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005289 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005290 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005291 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005292 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005293 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005294 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005295 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005296 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005297
5298 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005299 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005300
Chris Lattnerb903bed2009-06-26 21:20:29 +00005301 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005302 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5303 // initialexec.
5304 unsigned WrapperKind = X86ISD::Wrapper;
5305 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005306 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005307 } else if (is64Bit) {
5308 assert(model == TLSModel::InitialExec);
5309 OperandFlags = X86II::MO_GOTTPOFF;
5310 WrapperKind = X86ISD::WrapperRIP;
5311 } else {
5312 assert(model == TLSModel::InitialExec);
5313 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005314 }
Eric Christopherfd179292009-08-27 18:07:15 +00005315
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005316 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5317 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005318 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005319 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005320 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005321
Rafael Espindola9a580232009-02-27 13:37:18 +00005322 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005323 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005324 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005325
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005326 // The address of the thread local variable is the add of the thread
5327 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005328 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005329}
5330
Dan Gohman475871a2008-07-27 21:46:04 +00005331SDValue
5332X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005333 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005334 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005335 assert(Subtarget->isTargetELF() &&
5336 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005337 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005338 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005339
Chris Lattnerb903bed2009-06-26 21:20:29 +00005340 // If GV is an alias then use the aliasee for determining
5341 // thread-localness.
5342 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5343 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005344
Chris Lattnerb903bed2009-06-26 21:20:29 +00005345 TLSModel::Model model = getTLSModel(GV,
5346 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005347
Chris Lattnerb903bed2009-06-26 21:20:29 +00005348 switch (model) {
5349 case TLSModel::GeneralDynamic:
5350 case TLSModel::LocalDynamic: // not implemented
5351 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005352 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005353 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005354
Chris Lattnerb903bed2009-06-26 21:20:29 +00005355 case TLSModel::InitialExec:
5356 case TLSModel::LocalExec:
5357 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5358 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005359 }
Eric Christopherfd179292009-08-27 18:07:15 +00005360
Torok Edwinc23197a2009-07-14 16:55:14 +00005361 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005362 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005363}
5364
Evan Cheng0db9fe62006-04-25 20:13:52 +00005365
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005366/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005367/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00005368SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005369 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005370 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005371 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005372 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005373 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005374 SDValue ShOpLo = Op.getOperand(0);
5375 SDValue ShOpHi = Op.getOperand(1);
5376 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005377 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005378 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005379 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005380
Dan Gohman475871a2008-07-27 21:46:04 +00005381 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005382 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005383 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5384 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005385 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005386 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5387 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005388 }
Evan Chenge3413162006-01-09 18:33:28 +00005389
Owen Anderson825b72b2009-08-11 20:47:22 +00005390 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5391 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005392 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005393 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005394
Dan Gohman475871a2008-07-27 21:46:04 +00005395 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005396 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005397 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5398 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005399
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005400 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005401 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5402 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005403 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005404 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5405 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005406 }
5407
Dan Gohman475871a2008-07-27 21:46:04 +00005408 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005409 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005410}
Evan Chenga3195e82006-01-12 22:54:21 +00005411
Dan Gohman475871a2008-07-27 21:46:04 +00005412SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00005413 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005414
5415 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005416 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005417 return Op;
5418 }
5419 return SDValue();
5420 }
5421
Owen Anderson825b72b2009-08-11 20:47:22 +00005422 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005423 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005424
Eli Friedman36df4992009-05-27 00:47:34 +00005425 // These are really Legal; return the operand so the caller accepts it as
5426 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005427 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005428 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005429 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005430 Subtarget->is64Bit()) {
5431 return Op;
5432 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005433
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005434 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005435 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005436 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005437 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005438 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005439 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005440 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005441 PseudoSourceValue::getFixedStack(SSFI), 0,
5442 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005443 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5444}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005445
Owen Andersone50ed302009-08-10 22:56:29 +00005446SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Eli Friedman948e95a2009-05-23 09:59:16 +00005447 SDValue StackSlot,
5448 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005449 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005450 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005451 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005452 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005453 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005454 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005455 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005456 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005457 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005458 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005459 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005460
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005461 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005462 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005463 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005464
5465 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5466 // shouldn't be necessary except that RFP cannot be live across
5467 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005468 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005469 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005470 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005471 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005472 SDValue Ops[] = {
5473 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5474 };
5475 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005476 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005477 PseudoSourceValue::getFixedStack(SSFI), 0,
5478 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005479 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005480
Evan Cheng0db9fe62006-04-25 20:13:52 +00005481 return Result;
5482}
5483
Bill Wendling8b8a6362009-01-17 03:56:04 +00005484// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
5485SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
5486 // This algorithm is not obvious. Here it is in C code, more or less:
5487 /*
5488 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5489 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5490 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005491
Bill Wendling8b8a6362009-01-17 03:56:04 +00005492 // Copy ints to xmm registers.
5493 __m128i xh = _mm_cvtsi32_si128( hi );
5494 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005495
Bill Wendling8b8a6362009-01-17 03:56:04 +00005496 // Combine into low half of a single xmm register.
5497 __m128i x = _mm_unpacklo_epi32( xh, xl );
5498 __m128d d;
5499 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005500
Bill Wendling8b8a6362009-01-17 03:56:04 +00005501 // Merge in appropriate exponents to give the integer bits the right
5502 // magnitude.
5503 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005504
Bill Wendling8b8a6362009-01-17 03:56:04 +00005505 // Subtract away the biases to deal with the IEEE-754 double precision
5506 // implicit 1.
5507 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005508
Bill Wendling8b8a6362009-01-17 03:56:04 +00005509 // All conversions up to here are exact. The correctly rounded result is
5510 // calculated using the current rounding mode using the following
5511 // horizontal add.
5512 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5513 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5514 // store doesn't really need to be here (except
5515 // maybe to zero the other double)
5516 return sd;
5517 }
5518 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005519
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005520 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005521 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005522
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005523 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005524 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005525 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5526 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5527 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5528 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005529 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005530 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005531
Bill Wendling8b8a6362009-01-17 03:56:04 +00005532 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005533 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005534 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005535 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005536 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005537 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005538 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005539
Owen Anderson825b72b2009-08-11 20:47:22 +00005540 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5541 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005542 Op.getOperand(0),
5543 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005544 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5545 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005546 Op.getOperand(0),
5547 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005548 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5549 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005550 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005551 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005552 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5553 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5554 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005555 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005556 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005557 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005558
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005559 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005560 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005561 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5562 DAG.getUNDEF(MVT::v2f64), ShufMask);
5563 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5564 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005565 DAG.getIntPtrConstant(0));
5566}
5567
Bill Wendling8b8a6362009-01-17 03:56:04 +00005568// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
5569SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005570 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005571 // FP constant to bias correct the final result.
5572 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005574
5575 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5577 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005578 Op.getOperand(0),
5579 DAG.getIntPtrConstant(0)));
5580
Owen Anderson825b72b2009-08-11 20:47:22 +00005581 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5582 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005583 DAG.getIntPtrConstant(0));
5584
5585 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005586 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5587 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005588 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005589 MVT::v2f64, Load)),
5590 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005591 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005592 MVT::v2f64, Bias)));
5593 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5594 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005595 DAG.getIntPtrConstant(0));
5596
5597 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005598 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005599
5600 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005601 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005602
Owen Anderson825b72b2009-08-11 20:47:22 +00005603 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005604 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005605 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005606 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005607 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005608 }
5609
5610 // Handle final rounding.
5611 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005612}
5613
5614SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005615 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005616 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005617
Evan Chenga06ec9e2009-01-19 08:08:22 +00005618 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
5619 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5620 // the optimization here.
5621 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005622 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005623
Owen Andersone50ed302009-08-10 22:56:29 +00005624 EVT SrcVT = N0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00005625 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00005626 // We only handle SSE2 f64 target here; caller can expand the rest.
Owen Anderson825b72b2009-08-11 20:47:22 +00005627 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00005628 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00005629
Bill Wendling8b8a6362009-01-17 03:56:04 +00005630 return LowerUINT_TO_FP_i64(Op, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00005631 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005632 return LowerUINT_TO_FP_i32(Op, DAG);
5633 }
5634
Owen Anderson825b72b2009-08-11 20:47:22 +00005635 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
Eli Friedman948e95a2009-05-23 09:59:16 +00005636
5637 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005638 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Eli Friedman948e95a2009-05-23 09:59:16 +00005639 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5640 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5641 getPointerTy(), StackSlot, WordOff);
5642 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005643 StackSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005644 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00005645 OffsetSlot, NULL, 0, false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005646 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005647}
5648
Dan Gohman475871a2008-07-27 21:46:04 +00005649std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00005650FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005651 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005652
Owen Andersone50ed302009-08-10 22:56:29 +00005653 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005654
5655 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005656 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5657 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005658 }
5659
Owen Anderson825b72b2009-08-11 20:47:22 +00005660 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5661 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005662 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005663
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005664 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005665 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005666 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005667 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005668 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005669 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005670 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005671 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005672
Evan Cheng87c89352007-10-15 20:11:21 +00005673 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5674 // stack slot.
5675 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005676 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005677 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005678 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005679
Evan Cheng0db9fe62006-04-25 20:13:52 +00005680 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005681 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005682 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005683 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5684 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5685 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005686 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005687
Dan Gohman475871a2008-07-27 21:46:04 +00005688 SDValue Chain = DAG.getEntryNode();
5689 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005690 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005691 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005692 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005693 PseudoSourceValue::getFixedStack(SSFI), 0,
5694 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005695 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005696 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005697 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5698 };
Dale Johannesenace16102009-02-03 19:33:06 +00005699 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005700 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005701 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005702 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5703 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005704
Evan Cheng0db9fe62006-04-25 20:13:52 +00005705 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005706 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005707 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005708
Chris Lattner27a6c732007-11-24 07:07:01 +00005709 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005710}
5711
Dan Gohman475871a2008-07-27 21:46:04 +00005712SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005713 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005714 if (Op.getValueType() == MVT::v2i32 &&
5715 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005716 return Op;
5717 }
5718 return SDValue();
5719 }
5720
Eli Friedman948e95a2009-05-23 09:59:16 +00005721 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005722 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005723 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5724 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005725
Chris Lattner27a6c732007-11-24 07:07:01 +00005726 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005727 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005728 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005729}
5730
Eli Friedman948e95a2009-05-23 09:59:16 +00005731SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5732 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5733 SDValue FIST = Vals.first, StackSlot = Vals.second;
5734 assert(FIST.getNode() && "Unexpected failure");
5735
5736 // Load the result.
5737 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005738 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005739}
5740
Dan Gohman475871a2008-07-27 21:46:04 +00005741SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005742 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005743 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005744 EVT VT = Op.getValueType();
5745 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005746 if (VT.isVector())
5747 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005748 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005749 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005750 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005751 CV.push_back(C);
5752 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005753 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005754 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005755 CV.push_back(C);
5756 CV.push_back(C);
5757 CV.push_back(C);
5758 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005759 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005760 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005761 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005762 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005763 PseudoSourceValue::getConstantPool(), 0,
5764 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005765 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005766}
5767
Dan Gohman475871a2008-07-27 21:46:04 +00005768SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005769 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005770 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005771 EVT VT = Op.getValueType();
5772 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005773 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005774 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005775 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005776 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005777 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005778 CV.push_back(C);
5779 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005780 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005781 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005782 CV.push_back(C);
5783 CV.push_back(C);
5784 CV.push_back(C);
5785 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005786 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005787 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005788 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005789 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005790 PseudoSourceValue::getConstantPool(), 0,
5791 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005792 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005793 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005794 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5795 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005796 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005797 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005798 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005799 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005800 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005801}
5802
Dan Gohman475871a2008-07-27 21:46:04 +00005803SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005804 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005805 SDValue Op0 = Op.getOperand(0);
5806 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005807 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005808 EVT VT = Op.getValueType();
5809 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005810
5811 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005812 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005813 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005814 SrcVT = VT;
5815 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005816 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005817 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005818 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005819 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005820 }
5821
5822 // At this point the operands and the result should have the same
5823 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005824
Evan Cheng68c47cb2007-01-05 07:55:56 +00005825 // First get the sign bit of second operand.
5826 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005827 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005828 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5829 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005830 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005831 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5832 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5833 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5834 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005835 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005836 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005837 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005838 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005839 PseudoSourceValue::getConstantPool(), 0,
5840 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005841 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005842
5843 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005844 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005845 // Op0 is MVT::f32, Op1 is MVT::f64.
5846 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5847 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5848 DAG.getConstant(32, MVT::i32));
5849 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5850 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005851 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005852 }
5853
Evan Cheng73d6cf12007-01-05 21:37:56 +00005854 // Clear first operand sign bit.
5855 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005856 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005857 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5858 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005859 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005860 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5861 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5862 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5863 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005864 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005865 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005866 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005867 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005868 PseudoSourceValue::getConstantPool(), 0,
5869 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005870 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005871
5872 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005873 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005874}
5875
Dan Gohman076aee32009-03-04 19:44:21 +00005876/// Emit nodes that will be selected as "test Op0,Op0", or something
5877/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005878SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5879 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005880 DebugLoc dl = Op.getDebugLoc();
5881
Dan Gohman31125812009-03-07 01:58:32 +00005882 // CF and OF aren't always set the way we want. Determine which
5883 // of these we need.
5884 bool NeedCF = false;
5885 bool NeedOF = false;
5886 switch (X86CC) {
5887 case X86::COND_A: case X86::COND_AE:
5888 case X86::COND_B: case X86::COND_BE:
5889 NeedCF = true;
5890 break;
5891 case X86::COND_G: case X86::COND_GE:
5892 case X86::COND_L: case X86::COND_LE:
5893 case X86::COND_O: case X86::COND_NO:
5894 NeedOF = true;
5895 break;
5896 default: break;
5897 }
5898
Dan Gohman076aee32009-03-04 19:44:21 +00005899 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005900 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5901 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5902 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005903 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005904 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005905 switch (Op.getNode()->getOpcode()) {
5906 case ISD::ADD:
5907 // Due to an isel shortcoming, be conservative if this add is likely to
5908 // be selected as part of a load-modify-store instruction. When the root
5909 // node in a match is a store, isel doesn't know how to remap non-chain
5910 // non-flag uses of other nodes in the match, such as the ADD in this
5911 // case. This leads to the ADD being left around and reselected, with
5912 // the result being two adds in the output.
5913 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5914 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5915 if (UI->getOpcode() == ISD::STORE)
5916 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005917 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005918 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5919 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005920 if (C->getAPIntValue() == 1) {
5921 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005922 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005923 break;
5924 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005925 // An add of negative one (subtract of one) will be selected as a DEC.
5926 if (C->getAPIntValue().isAllOnesValue()) {
5927 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005928 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005929 break;
5930 }
5931 }
Dan Gohman076aee32009-03-04 19:44:21 +00005932 // Otherwise use a regular EFLAGS-setting add.
5933 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005934 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005935 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005936 case ISD::AND: {
5937 // If the primary and result isn't used, don't bother using X86ISD::AND,
5938 // because a TEST instruction will be better.
5939 bool NonFlagUse = false;
5940 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00005941 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
5942 SDNode *User = *UI;
5943 unsigned UOpNo = UI.getOperandNo();
5944 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
5945 // Look pass truncate.
5946 UOpNo = User->use_begin().getOperandNo();
5947 User = *User->use_begin();
5948 }
5949 if (User->getOpcode() != ISD::BRCOND &&
5950 User->getOpcode() != ISD::SETCC &&
5951 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00005952 NonFlagUse = true;
5953 break;
5954 }
Evan Cheng17751da2010-01-07 00:54:06 +00005955 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00005956 if (!NonFlagUse)
5957 break;
5958 }
5959 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00005960 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005961 case ISD::OR:
5962 case ISD::XOR:
5963 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00005964 // likely to be selected as part of a load-modify-store instruction.
5965 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5966 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5967 if (UI->getOpcode() == ISD::STORE)
5968 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00005969 // Otherwise use a regular EFLAGS-setting instruction.
5970 switch (Op.getNode()->getOpcode()) {
5971 case ISD::SUB: Opcode = X86ISD::SUB; break;
5972 case ISD::OR: Opcode = X86ISD::OR; break;
5973 case ISD::XOR: Opcode = X86ISD::XOR; break;
5974 case ISD::AND: Opcode = X86ISD::AND; break;
5975 default: llvm_unreachable("unexpected operator!");
5976 }
Dan Gohman51bb4742009-03-05 21:29:28 +00005977 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005978 break;
5979 case X86ISD::ADD:
5980 case X86ISD::SUB:
5981 case X86ISD::INC:
5982 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00005983 case X86ISD::OR:
5984 case X86ISD::XOR:
5985 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00005986 return SDValue(Op.getNode(), 1);
5987 default:
5988 default_case:
5989 break;
5990 }
5991 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005992 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005993 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005994 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005995 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005996 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005997 DAG.ReplaceAllUsesWith(Op, New);
5998 return SDValue(New.getNode(), 1);
5999 }
6000 }
6001
6002 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006003 if (Promote16Bit && Op.getValueType() == MVT::i16)
6004 Op = DAG.getNode(ISD::ANY_EXTEND, Op.getDebugLoc(), MVT::i32, Op);
Owen Anderson825b72b2009-08-11 20:47:22 +00006005 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00006006 DAG.getConstant(0, Op.getValueType()));
6007}
6008
6009/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6010/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006011SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
6012 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00006013 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6014 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00006015 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006016
6017 DebugLoc dl = Op0.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +00006018 if (Promote16Bit && Op0.getValueType() == MVT::i16) {
6019 Op0 = DAG.getNode(ISD::ANY_EXTEND, Op0.getDebugLoc(), MVT::i32, Op0);
6020 Op1 = DAG.getNode(ISD::ANY_EXTEND, Op1.getDebugLoc(), MVT::i32, Op1);
6021 }
Owen Anderson825b72b2009-08-11 20:47:22 +00006022 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006023}
6024
Evan Chengd40d03e2010-01-06 19:38:29 +00006025/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6026/// if it's possible.
Evan Cheng2c755ba2010-02-27 07:36:59 +00006027static SDValue LowerToBT(SDValue And, ISD::CondCode CC,
Evan Cheng54de3ea2010-01-05 06:52:31 +00006028 DebugLoc dl, SelectionDAG &DAG) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006029 SDValue Op0 = And.getOperand(0);
6030 SDValue Op1 = And.getOperand(1);
6031 if (Op0.getOpcode() == ISD::TRUNCATE)
6032 Op0 = Op0.getOperand(0);
6033 if (Op1.getOpcode() == ISD::TRUNCATE)
6034 Op1 = Op1.getOperand(0);
6035
Evan Chengd40d03e2010-01-06 19:38:29 +00006036 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00006037 if (Op1.getOpcode() == ISD::SHL) {
6038 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6039 if (And10C->getZExtValue() == 1) {
6040 LHS = Op0;
6041 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006042 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006043 } else if (Op0.getOpcode() == ISD::SHL) {
6044 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6045 if (And00C->getZExtValue() == 1) {
6046 LHS = Op1;
6047 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006048 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006049 } else if (Op1.getOpcode() == ISD::Constant) {
6050 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6051 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006052 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6053 LHS = AndLHS.getOperand(0);
6054 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006055 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006056 }
Evan Cheng0488db92007-09-25 01:57:46 +00006057
Evan Chengd40d03e2010-01-06 19:38:29 +00006058 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006059 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006060 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006061 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006062 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006063 // Also promote i16 to i32 for performance / code size reason.
6064 if (LHS.getValueType() == MVT::i8 ||
6065 (Promote16Bit && LHS.getValueType() == MVT::i16))
Evan Chengd40d03e2010-01-06 19:38:29 +00006066 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006067
Evan Chengd40d03e2010-01-06 19:38:29 +00006068 // If the operand types disagree, extend the shift amount to match. Since
6069 // BT ignores high bits (like shifts) we can use anyextend.
6070 if (LHS.getValueType() != RHS.getValueType())
6071 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006072
Evan Chengd40d03e2010-01-06 19:38:29 +00006073 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6074 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6075 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6076 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006077 }
6078
Evan Cheng54de3ea2010-01-05 06:52:31 +00006079 return SDValue();
6080}
6081
6082SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
6083 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6084 SDValue Op0 = Op.getOperand(0);
6085 SDValue Op1 = Op.getOperand(1);
6086 DebugLoc dl = Op.getDebugLoc();
6087 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6088
6089 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006090 // Lower (X & (1 << N)) == 0 to BT(X, N).
6091 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6092 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6093 if (Op0.getOpcode() == ISD::AND &&
6094 Op0.hasOneUse() &&
6095 Op1.getOpcode() == ISD::Constant &&
6096 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6097 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6098 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6099 if (NewSetCC.getNode())
6100 return NewSetCC;
6101 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006102
Evan Cheng2c755ba2010-02-27 07:36:59 +00006103 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6104 if (Op0.getOpcode() == X86ISD::SETCC &&
6105 Op1.getOpcode() == ISD::Constant &&
6106 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6107 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6108 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6109 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6110 bool Invert = (CC == ISD::SETNE) ^
6111 cast<ConstantSDNode>(Op1)->isNullValue();
6112 if (Invert)
6113 CCode = X86::GetOppositeBranchCondition(CCode);
6114 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6115 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6116 }
6117
Evan Chenge5b51ac2010-04-17 06:13:15 +00006118 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006119 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006120 if (X86CC == X86::COND_INVALID)
6121 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006122
Dan Gohman31125812009-03-07 01:58:32 +00006123 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006124
6125 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006126 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006127 return DAG.getNode(ISD::AND, dl, MVT::i8,
6128 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6129 DAG.getConstant(X86CC, MVT::i8), Cond),
6130 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006131
Owen Anderson825b72b2009-08-11 20:47:22 +00006132 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6133 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006134}
6135
Dan Gohman475871a2008-07-27 21:46:04 +00006136SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
6137 SDValue Cond;
6138 SDValue Op0 = Op.getOperand(0);
6139 SDValue Op1 = Op.getOperand(1);
6140 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006141 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006142 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6143 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006144 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006145
6146 if (isFP) {
6147 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006148 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006149 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6150 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006151 bool Swap = false;
6152
6153 switch (SetCCOpcode) {
6154 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006155 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006156 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006157 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006158 case ISD::SETGT: Swap = true; // Fallthrough
6159 case ISD::SETLT:
6160 case ISD::SETOLT: SSECC = 1; break;
6161 case ISD::SETOGE:
6162 case ISD::SETGE: Swap = true; // Fallthrough
6163 case ISD::SETLE:
6164 case ISD::SETOLE: SSECC = 2; break;
6165 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006166 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006167 case ISD::SETNE: SSECC = 4; break;
6168 case ISD::SETULE: Swap = true;
6169 case ISD::SETUGE: SSECC = 5; break;
6170 case ISD::SETULT: Swap = true;
6171 case ISD::SETUGT: SSECC = 6; break;
6172 case ISD::SETO: SSECC = 7; break;
6173 }
6174 if (Swap)
6175 std::swap(Op0, Op1);
6176
Nate Begemanfb8ead02008-07-25 19:05:58 +00006177 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006178 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006179 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006180 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006181 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6182 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006183 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006184 }
6185 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006186 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006187 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6188 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006189 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006190 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006191 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006192 }
6193 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006194 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006195 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006196
Nate Begeman30a0de92008-07-17 16:51:19 +00006197 // We are handling one of the integer comparisons here. Since SSE only has
6198 // GT and EQ comparisons for integer, swapping operands and multiple
6199 // operations may be required for some comparisons.
6200 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6201 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006202
Owen Anderson825b72b2009-08-11 20:47:22 +00006203 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006204 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006205 case MVT::v8i8:
6206 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6207 case MVT::v4i16:
6208 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6209 case MVT::v2i32:
6210 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6211 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006212 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006213
Nate Begeman30a0de92008-07-17 16:51:19 +00006214 switch (SetCCOpcode) {
6215 default: break;
6216 case ISD::SETNE: Invert = true;
6217 case ISD::SETEQ: Opc = EQOpc; break;
6218 case ISD::SETLT: Swap = true;
6219 case ISD::SETGT: Opc = GTOpc; break;
6220 case ISD::SETGE: Swap = true;
6221 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6222 case ISD::SETULT: Swap = true;
6223 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6224 case ISD::SETUGE: Swap = true;
6225 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6226 }
6227 if (Swap)
6228 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006229
Nate Begeman30a0de92008-07-17 16:51:19 +00006230 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6231 // bits of the inputs before performing those operations.
6232 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006233 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006234 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6235 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006236 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006237 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6238 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006239 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6240 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006241 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006242
Dale Johannesenace16102009-02-03 19:33:06 +00006243 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006244
6245 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006246 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006247 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006248
Nate Begeman30a0de92008-07-17 16:51:19 +00006249 return Result;
6250}
Evan Cheng0488db92007-09-25 01:57:46 +00006251
Evan Cheng370e5342008-12-03 08:38:43 +00006252// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006253static bool isX86LogicalCmp(SDValue Op) {
6254 unsigned Opc = Op.getNode()->getOpcode();
6255 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6256 return true;
6257 if (Op.getResNo() == 1 &&
6258 (Opc == X86ISD::ADD ||
6259 Opc == X86ISD::SUB ||
6260 Opc == X86ISD::SMUL ||
6261 Opc == X86ISD::UMUL ||
6262 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006263 Opc == X86ISD::DEC ||
6264 Opc == X86ISD::OR ||
6265 Opc == X86ISD::XOR ||
6266 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006267 return true;
6268
6269 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006270}
6271
Dan Gohman475871a2008-07-27 21:46:04 +00006272SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006273 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006274 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006275 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006276 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006277
Dan Gohman1a492952009-10-20 16:22:37 +00006278 if (Cond.getOpcode() == ISD::SETCC) {
6279 SDValue NewCond = LowerSETCC(Cond, DAG);
6280 if (NewCond.getNode())
6281 Cond = NewCond;
6282 }
Evan Cheng734503b2006-09-11 02:19:56 +00006283
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006284 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6285 SDValue Op1 = Op.getOperand(1);
6286 SDValue Op2 = Op.getOperand(2);
6287 if (Cond.getOpcode() == X86ISD::SETCC &&
6288 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6289 SDValue Cmp = Cond.getOperand(1);
6290 if (Cmp.getOpcode() == X86ISD::CMP) {
6291 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6292 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6293 ConstantSDNode *RHSC =
6294 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6295 if (N1C && N1C->isAllOnesValue() &&
6296 N2C && N2C->isNullValue() &&
6297 RHSC && RHSC->isNullValue()) {
6298 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006299 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006300 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6301 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6302 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6303 }
6304 }
6305 }
6306
Evan Chengad9c0a32009-12-15 00:53:42 +00006307 // Look pass (and (setcc_carry (cmp ...)), 1).
6308 if (Cond.getOpcode() == ISD::AND &&
6309 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6310 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6311 if (C && C->getAPIntValue() == 1)
6312 Cond = Cond.getOperand(0);
6313 }
6314
Evan Cheng3f41d662007-10-08 22:16:29 +00006315 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6316 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006317 if (Cond.getOpcode() == X86ISD::SETCC ||
6318 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006319 CC = Cond.getOperand(0);
6320
Dan Gohman475871a2008-07-27 21:46:04 +00006321 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006322 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006323 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006324
Evan Cheng3f41d662007-10-08 22:16:29 +00006325 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006326 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006327 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006328 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006329
Chris Lattnerd1980a52009-03-12 06:52:53 +00006330 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6331 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006332 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006333 addTest = false;
6334 }
6335 }
6336
6337 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006338 // Look pass the truncate.
6339 if (Cond.getOpcode() == ISD::TRUNCATE)
6340 Cond = Cond.getOperand(0);
6341
6342 // We know the result of AND is compared against zero. Try to match
6343 // it to BT.
6344 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6345 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6346 if (NewSetCC.getNode()) {
6347 CC = NewSetCC.getOperand(0);
6348 Cond = NewSetCC.getOperand(1);
6349 addTest = false;
6350 }
6351 }
6352 }
6353
6354 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006355 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006356 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006357 }
6358
Evan Cheng0488db92007-09-25 01:57:46 +00006359 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6360 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006361 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6362 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006363 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006364}
6365
Evan Cheng370e5342008-12-03 08:38:43 +00006366// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6367// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6368// from the AND / OR.
6369static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6370 Opc = Op.getOpcode();
6371 if (Opc != ISD::OR && Opc != ISD::AND)
6372 return false;
6373 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6374 Op.getOperand(0).hasOneUse() &&
6375 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6376 Op.getOperand(1).hasOneUse());
6377}
6378
Evan Cheng961d6d42009-02-02 08:19:07 +00006379// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6380// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006381static bool isXor1OfSetCC(SDValue Op) {
6382 if (Op.getOpcode() != ISD::XOR)
6383 return false;
6384 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6385 if (N1C && N1C->getAPIntValue() == 1) {
6386 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6387 Op.getOperand(0).hasOneUse();
6388 }
6389 return false;
6390}
6391
Dan Gohman475871a2008-07-27 21:46:04 +00006392SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00006393 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006394 SDValue Chain = Op.getOperand(0);
6395 SDValue Cond = Op.getOperand(1);
6396 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006397 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006398 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006399
Dan Gohman1a492952009-10-20 16:22:37 +00006400 if (Cond.getOpcode() == ISD::SETCC) {
6401 SDValue NewCond = LowerSETCC(Cond, DAG);
6402 if (NewCond.getNode())
6403 Cond = NewCond;
6404 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006405#if 0
6406 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006407 else if (Cond.getOpcode() == X86ISD::ADD ||
6408 Cond.getOpcode() == X86ISD::SUB ||
6409 Cond.getOpcode() == X86ISD::SMUL ||
6410 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006411 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006412#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006413
Evan Chengad9c0a32009-12-15 00:53:42 +00006414 // Look pass (and (setcc_carry (cmp ...)), 1).
6415 if (Cond.getOpcode() == ISD::AND &&
6416 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6417 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6418 if (C && C->getAPIntValue() == 1)
6419 Cond = Cond.getOperand(0);
6420 }
6421
Evan Cheng3f41d662007-10-08 22:16:29 +00006422 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6423 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006424 if (Cond.getOpcode() == X86ISD::SETCC ||
6425 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006426 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006427
Dan Gohman475871a2008-07-27 21:46:04 +00006428 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006429 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006430 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006431 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006432 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006433 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006434 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006435 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006436 default: break;
6437 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006438 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006439 // These can only come from an arithmetic instruction with overflow,
6440 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006441 Cond = Cond.getNode()->getOperand(1);
6442 addTest = false;
6443 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006444 }
Evan Cheng0488db92007-09-25 01:57:46 +00006445 }
Evan Cheng370e5342008-12-03 08:38:43 +00006446 } else {
6447 unsigned CondOpc;
6448 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6449 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006450 if (CondOpc == ISD::OR) {
6451 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6452 // two branches instead of an explicit OR instruction with a
6453 // separate test.
6454 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006455 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006456 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006457 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006458 Chain, Dest, CC, Cmp);
6459 CC = Cond.getOperand(1).getOperand(0);
6460 Cond = Cmp;
6461 addTest = false;
6462 }
6463 } else { // ISD::AND
6464 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6465 // two branches instead of an explicit AND instruction with a
6466 // separate test. However, we only do this if this block doesn't
6467 // have a fall-through edge, because this requires an explicit
6468 // jmp when the condition is false.
6469 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006470 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006471 Op.getNode()->hasOneUse()) {
6472 X86::CondCode CCode =
6473 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6474 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006475 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006476 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6477 // Look for an unconditional branch following this conditional branch.
6478 // We need this because we need to reverse the successors in order
6479 // to implement FCMP_OEQ.
6480 if (User.getOpcode() == ISD::BR) {
6481 SDValue FalseBB = User.getOperand(1);
6482 SDValue NewBR =
6483 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6484 assert(NewBR == User);
6485 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006486
Dale Johannesene4d209d2009-02-03 20:21:25 +00006487 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006488 Chain, Dest, CC, Cmp);
6489 X86::CondCode CCode =
6490 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6491 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006492 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006493 Cond = Cmp;
6494 addTest = false;
6495 }
6496 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006497 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006498 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6499 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6500 // It should be transformed during dag combiner except when the condition
6501 // is set by a arithmetics with overflow node.
6502 X86::CondCode CCode =
6503 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6504 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006505 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006506 Cond = Cond.getOperand(0).getOperand(1);
6507 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006508 }
Evan Cheng0488db92007-09-25 01:57:46 +00006509 }
6510
6511 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006512 // Look pass the truncate.
6513 if (Cond.getOpcode() == ISD::TRUNCATE)
6514 Cond = Cond.getOperand(0);
6515
6516 // We know the result of AND is compared against zero. Try to match
6517 // it to BT.
6518 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6519 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6520 if (NewSetCC.getNode()) {
6521 CC = NewSetCC.getOperand(0);
6522 Cond = NewSetCC.getOperand(1);
6523 addTest = false;
6524 }
6525 }
6526 }
6527
6528 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006529 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00006530 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006531 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006532 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006533 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006534}
6535
Anton Korobeynikove060b532007-04-17 19:34:00 +00006536
6537// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6538// Calls to _alloca is needed to probe the stack when allocating more than 4k
6539// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6540// that the guard pages used by the OS virtual memory manager are allocated in
6541// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006542SDValue
6543X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006544 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006545 assert(Subtarget->isTargetCygMing() &&
6546 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006547 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006548
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006549 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006550 SDValue Chain = Op.getOperand(0);
6551 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006552 // FIXME: Ensure alignment here
6553
Dan Gohman475871a2008-07-27 21:46:04 +00006554 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006555
Owen Andersone50ed302009-08-10 22:56:29 +00006556 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006557 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006558
Dale Johannesendd64c412009-02-04 00:33:20 +00006559 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006560 Flag = Chain.getValue(1);
6561
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006562 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006563
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006564 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6565 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006566
Dale Johannesendd64c412009-02-04 00:33:20 +00006567 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006568
Dan Gohman475871a2008-07-27 21:46:04 +00006569 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006570 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006571}
6572
Dan Gohman475871a2008-07-27 21:46:04 +00006573SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006574X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00006575 SDValue Chain,
6576 SDValue Dst, SDValue Src,
6577 SDValue Size, unsigned Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006578 bool isVolatile,
Bill Wendling6f287b22008-09-30 21:22:07 +00006579 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00006580 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006581 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006582
Bill Wendling6f287b22008-09-30 21:22:07 +00006583 // If not DWORD aligned or size is more than the threshold, call the library.
6584 // The libc version is likely to be faster for these cases. It can use the
6585 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00006586 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00006587 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006588 ConstantSize->getZExtValue() >
6589 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006590 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00006591
6592 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00006593 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00006594
Bill Wendling6158d842008-10-01 00:59:58 +00006595 if (const char *bzeroEntry = V &&
6596 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00006597 EVT IntPtr = getPointerTy();
Owen Anderson1d0be152009-08-13 21:58:54 +00006598 const Type *IntPtrTy = TD->getIntPtrType(*DAG.getContext());
Scott Michelfdc40a02009-02-17 22:15:04 +00006599 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00006600 TargetLowering::ArgListEntry Entry;
6601 Entry.Node = Dst;
6602 Entry.Ty = IntPtrTy;
6603 Args.push_back(Entry);
6604 Entry.Node = Size;
6605 Args.push_back(Entry);
6606 std::pair<SDValue,SDValue> CallResult =
Owen Anderson1d0be152009-08-13 21:58:54 +00006607 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
6608 false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006609 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Bill Wendling46ada192010-03-02 01:55:18 +00006610 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00006611 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00006612 }
6613
Dan Gohman707e0182008-04-12 04:36:06 +00006614 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00006615 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00006616 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00006617
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006618 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00006619 SDValue InFlag(0, 0);
Owen Andersone50ed302009-08-10 22:56:29 +00006620 EVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00006621 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00006622 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006623 unsigned BytesLeft = 0;
6624 bool TwoRepStos = false;
6625 if (ValC) {
6626 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006627 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00006628
Evan Cheng0db9fe62006-04-25 20:13:52 +00006629 // If the value is a constant, then we can potentially use larger sets.
6630 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00006631 case 2: // WORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006632 AVT = MVT::i16;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006633 ValReg = X86::AX;
6634 Val = (Val << 8) | Val;
6635 break;
6636 case 0: // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006637 AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006638 ValReg = X86::EAX;
6639 Val = (Val << 8) | Val;
6640 Val = (Val << 16) | Val;
6641 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006642 AVT = MVT::i64;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006643 ValReg = X86::RAX;
6644 Val = (Val << 32) | Val;
6645 }
6646 break;
6647 default: // Byte aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006648 AVT = MVT::i8;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006649 ValReg = X86::AL;
6650 Count = DAG.getIntPtrConstant(SizeVal);
6651 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00006652 }
6653
Owen Anderson825b72b2009-08-11 20:47:22 +00006654 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006655 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006656 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
6657 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006658 }
6659
Dale Johannesen0f502f62009-02-03 22:26:09 +00006660 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00006661 InFlag);
6662 InFlag = Chain.getValue(1);
6663 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00006664 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00006665 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00006666 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006667 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00006668 }
Evan Chengc78d3b42006-04-24 18:01:45 +00006669
Scott Michelfdc40a02009-02-17 22:15:04 +00006670 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006671 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006672 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006673 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006674 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006675 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006676 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006677 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00006678
Owen Anderson825b72b2009-08-11 20:47:22 +00006679 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006680 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6681 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Chengc78d3b42006-04-24 18:01:45 +00006682
Evan Cheng0db9fe62006-04-25 20:13:52 +00006683 if (TwoRepStos) {
6684 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00006685 Count = Size;
Owen Andersone50ed302009-08-10 22:56:29 +00006686 EVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00006687 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Owen Anderson825b72b2009-08-11 20:47:22 +00006688 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
6689 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006690 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006691 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006692 InFlag = Chain.getValue(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00006693 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006694 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
6695 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006696 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006697 // Handle the last 1 - 7 bytes.
6698 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006699 EVT AddrVT = Dst.getValueType();
6700 EVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00006701
Dale Johannesen0f502f62009-02-03 22:26:09 +00006702 Chain = DAG.getMemset(Chain, dl,
6703 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00006704 DAG.getConstant(Offset, AddrVT)),
6705 Src,
6706 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang20adc9d2010-04-04 03:10:48 +00006707 Align, isVolatile, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00006708 }
Evan Cheng11e15b32006-04-03 20:53:28 +00006709
Dan Gohman707e0182008-04-12 04:36:06 +00006710 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006711 return Chain;
6712}
Evan Cheng11e15b32006-04-03 20:53:28 +00006713
Dan Gohman475871a2008-07-27 21:46:04 +00006714SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00006715X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006716 SDValue Chain, SDValue Dst, SDValue Src,
6717 SDValue Size, unsigned Align,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006718 bool isVolatile, bool AlwaysInline,
Evan Cheng1887c1c2008-08-21 21:00:15 +00006719 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00006720 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00006721 // This requires the copy size to be a constant, preferrably
6722 // within a subtarget-specific limit.
6723 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
6724 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00006725 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006726 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006727 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00006728 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00006729
Evan Cheng1887c1c2008-08-21 21:00:15 +00006730 /// If not DWORD aligned, call the library.
6731 if ((Align & 3) != 0)
6732 return SDValue();
6733
6734 // DWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006735 EVT AVT = MVT::i32;
Evan Cheng1887c1c2008-08-21 21:00:15 +00006736 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Owen Anderson825b72b2009-08-11 20:47:22 +00006737 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006738
Duncan Sands83ec4b62008-06-06 12:08:01 +00006739 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00006740 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00006741 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00006742 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00006743
Dan Gohman475871a2008-07-27 21:46:04 +00006744 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006745 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006746 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00006747 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006748 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006749 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Evan Chengc3b0c342010-04-08 07:37:57 +00006750 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00006751 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006752 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006753 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00006754 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00006755 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006756 InFlag = Chain.getValue(1);
6757
Owen Anderson825b72b2009-08-11 20:47:22 +00006758 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006759 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
6760 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
6761 array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00006762
Dan Gohman475871a2008-07-27 21:46:04 +00006763 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00006764 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00006765 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00006766 // Handle the last 1 - 7 bytes.
6767 unsigned Offset = SizeVal - BytesLeft;
Owen Andersone50ed302009-08-10 22:56:29 +00006768 EVT DstVT = Dst.getValueType();
6769 EVT SrcVT = Src.getValueType();
6770 EVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006771 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006772 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00006773 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00006774 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00006775 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00006776 DAG.getConstant(BytesLeft, SizeVT),
Mon P Wang20adc9d2010-04-04 03:10:48 +00006777 Align, isVolatile, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00006778 DstSV, DstSVOff + Offset,
6779 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00006780 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006781
Owen Anderson825b72b2009-08-11 20:47:22 +00006782 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00006783 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006784}
6785
Dan Gohman475871a2008-07-27 21:46:04 +00006786SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman1e93df62010-04-17 14:41:14 +00006787 MachineFunction &MF = DAG.getMachineFunction();
6788 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6789
Dan Gohman69de1932008-02-06 22:27:42 +00006790 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006791 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006792
Evan Cheng25ab6902006-09-08 06:48:29 +00006793 if (!Subtarget->is64Bit()) {
6794 // vastart just stores the address of the VarArgsFrameIndex slot into the
6795 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006796 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6797 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006798 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6799 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006800 }
6801
6802 // __va_list_tag:
6803 // gp_offset (0 - 6 * 8)
6804 // fp_offset (48 - 48 + 8 * 16)
6805 // overflow_arg_area (point to parameters coming in memory).
6806 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006807 SmallVector<SDValue, 8> MemOps;
6808 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006809 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006810 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006811 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6812 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006813 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006814 MemOps.push_back(Store);
6815
6816 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006817 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006818 FIN, DAG.getIntPtrConstant(4));
6819 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006820 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6821 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006822 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006823 MemOps.push_back(Store);
6824
6825 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006826 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006827 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006828 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6829 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006830 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6831 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006832 MemOps.push_back(Store);
6833
6834 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006835 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006836 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006837 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6838 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006839 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6840 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006841 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006842 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006843 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006844}
6845
Dan Gohman475871a2008-07-27 21:46:04 +00006846SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006847 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6848 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006849 SDValue Chain = Op.getOperand(0);
6850 SDValue SrcPtr = Op.getOperand(1);
6851 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006852
Chris Lattner75361b62010-04-07 22:58:41 +00006853 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006854 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006855}
6856
Dan Gohman475871a2008-07-27 21:46:04 +00006857SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006858 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006859 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006860 SDValue Chain = Op.getOperand(0);
6861 SDValue DstPtr = Op.getOperand(1);
6862 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006863 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6864 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006865 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006866
Dale Johannesendd64c412009-02-04 00:33:20 +00006867 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006868 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6869 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006870}
6871
Dan Gohman475871a2008-07-27 21:46:04 +00006872SDValue
6873X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006874 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006875 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006876 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006877 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006878 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006879 case Intrinsic::x86_sse_comieq_ss:
6880 case Intrinsic::x86_sse_comilt_ss:
6881 case Intrinsic::x86_sse_comile_ss:
6882 case Intrinsic::x86_sse_comigt_ss:
6883 case Intrinsic::x86_sse_comige_ss:
6884 case Intrinsic::x86_sse_comineq_ss:
6885 case Intrinsic::x86_sse_ucomieq_ss:
6886 case Intrinsic::x86_sse_ucomilt_ss:
6887 case Intrinsic::x86_sse_ucomile_ss:
6888 case Intrinsic::x86_sse_ucomigt_ss:
6889 case Intrinsic::x86_sse_ucomige_ss:
6890 case Intrinsic::x86_sse_ucomineq_ss:
6891 case Intrinsic::x86_sse2_comieq_sd:
6892 case Intrinsic::x86_sse2_comilt_sd:
6893 case Intrinsic::x86_sse2_comile_sd:
6894 case Intrinsic::x86_sse2_comigt_sd:
6895 case Intrinsic::x86_sse2_comige_sd:
6896 case Intrinsic::x86_sse2_comineq_sd:
6897 case Intrinsic::x86_sse2_ucomieq_sd:
6898 case Intrinsic::x86_sse2_ucomilt_sd:
6899 case Intrinsic::x86_sse2_ucomile_sd:
6900 case Intrinsic::x86_sse2_ucomigt_sd:
6901 case Intrinsic::x86_sse2_ucomige_sd:
6902 case Intrinsic::x86_sse2_ucomineq_sd: {
6903 unsigned Opc = 0;
6904 ISD::CondCode CC = ISD::SETCC_INVALID;
6905 switch (IntNo) {
6906 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006907 case Intrinsic::x86_sse_comieq_ss:
6908 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006909 Opc = X86ISD::COMI;
6910 CC = ISD::SETEQ;
6911 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006912 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006913 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006914 Opc = X86ISD::COMI;
6915 CC = ISD::SETLT;
6916 break;
6917 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006918 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006919 Opc = X86ISD::COMI;
6920 CC = ISD::SETLE;
6921 break;
6922 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006923 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006924 Opc = X86ISD::COMI;
6925 CC = ISD::SETGT;
6926 break;
6927 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006928 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006929 Opc = X86ISD::COMI;
6930 CC = ISD::SETGE;
6931 break;
6932 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006933 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006934 Opc = X86ISD::COMI;
6935 CC = ISD::SETNE;
6936 break;
6937 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006938 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006939 Opc = X86ISD::UCOMI;
6940 CC = ISD::SETEQ;
6941 break;
6942 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006943 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006944 Opc = X86ISD::UCOMI;
6945 CC = ISD::SETLT;
6946 break;
6947 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006948 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006949 Opc = X86ISD::UCOMI;
6950 CC = ISD::SETLE;
6951 break;
6952 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006953 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006954 Opc = X86ISD::UCOMI;
6955 CC = ISD::SETGT;
6956 break;
6957 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006958 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006959 Opc = X86ISD::UCOMI;
6960 CC = ISD::SETGE;
6961 break;
6962 case Intrinsic::x86_sse_ucomineq_ss:
6963 case Intrinsic::x86_sse2_ucomineq_sd:
6964 Opc = X86ISD::UCOMI;
6965 CC = ISD::SETNE;
6966 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006967 }
Evan Cheng734503b2006-09-11 02:19:56 +00006968
Dan Gohman475871a2008-07-27 21:46:04 +00006969 SDValue LHS = Op.getOperand(1);
6970 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006971 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006972 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006973 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6974 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6975 DAG.getConstant(X86CC, MVT::i8), Cond);
6976 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006977 }
Eric Christopher71c67532009-07-29 00:28:05 +00006978 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006979 // an integer value, not just an instruction so lower it to the ptest
6980 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006981 case Intrinsic::x86_sse41_ptestz:
6982 case Intrinsic::x86_sse41_ptestc:
6983 case Intrinsic::x86_sse41_ptestnzc:{
6984 unsigned X86CC = 0;
6985 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006986 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006987 case Intrinsic::x86_sse41_ptestz:
6988 // ZF = 1
6989 X86CC = X86::COND_E;
6990 break;
6991 case Intrinsic::x86_sse41_ptestc:
6992 // CF = 1
6993 X86CC = X86::COND_B;
6994 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006995 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006996 // ZF and CF = 0
6997 X86CC = X86::COND_A;
6998 break;
6999 }
Eric Christopherfd179292009-08-27 18:07:15 +00007000
Eric Christopher71c67532009-07-29 00:28:05 +00007001 SDValue LHS = Op.getOperand(1);
7002 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00007003 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
7004 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7005 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7006 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007007 }
Evan Cheng5759f972008-05-04 09:15:50 +00007008
7009 // Fix vector shift instructions where the last operand is a non-immediate
7010 // i32 value.
7011 case Intrinsic::x86_sse2_pslli_w:
7012 case Intrinsic::x86_sse2_pslli_d:
7013 case Intrinsic::x86_sse2_pslli_q:
7014 case Intrinsic::x86_sse2_psrli_w:
7015 case Intrinsic::x86_sse2_psrli_d:
7016 case Intrinsic::x86_sse2_psrli_q:
7017 case Intrinsic::x86_sse2_psrai_w:
7018 case Intrinsic::x86_sse2_psrai_d:
7019 case Intrinsic::x86_mmx_pslli_w:
7020 case Intrinsic::x86_mmx_pslli_d:
7021 case Intrinsic::x86_mmx_pslli_q:
7022 case Intrinsic::x86_mmx_psrli_w:
7023 case Intrinsic::x86_mmx_psrli_d:
7024 case Intrinsic::x86_mmx_psrli_q:
7025 case Intrinsic::x86_mmx_psrai_w:
7026 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007027 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007028 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007029 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007030
7031 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007032 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007033 switch (IntNo) {
7034 case Intrinsic::x86_sse2_pslli_w:
7035 NewIntNo = Intrinsic::x86_sse2_psll_w;
7036 break;
7037 case Intrinsic::x86_sse2_pslli_d:
7038 NewIntNo = Intrinsic::x86_sse2_psll_d;
7039 break;
7040 case Intrinsic::x86_sse2_pslli_q:
7041 NewIntNo = Intrinsic::x86_sse2_psll_q;
7042 break;
7043 case Intrinsic::x86_sse2_psrli_w:
7044 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7045 break;
7046 case Intrinsic::x86_sse2_psrli_d:
7047 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7048 break;
7049 case Intrinsic::x86_sse2_psrli_q:
7050 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7051 break;
7052 case Intrinsic::x86_sse2_psrai_w:
7053 NewIntNo = Intrinsic::x86_sse2_psra_w;
7054 break;
7055 case Intrinsic::x86_sse2_psrai_d:
7056 NewIntNo = Intrinsic::x86_sse2_psra_d;
7057 break;
7058 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007059 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007060 switch (IntNo) {
7061 case Intrinsic::x86_mmx_pslli_w:
7062 NewIntNo = Intrinsic::x86_mmx_psll_w;
7063 break;
7064 case Intrinsic::x86_mmx_pslli_d:
7065 NewIntNo = Intrinsic::x86_mmx_psll_d;
7066 break;
7067 case Intrinsic::x86_mmx_pslli_q:
7068 NewIntNo = Intrinsic::x86_mmx_psll_q;
7069 break;
7070 case Intrinsic::x86_mmx_psrli_w:
7071 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7072 break;
7073 case Intrinsic::x86_mmx_psrli_d:
7074 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7075 break;
7076 case Intrinsic::x86_mmx_psrli_q:
7077 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7078 break;
7079 case Intrinsic::x86_mmx_psrai_w:
7080 NewIntNo = Intrinsic::x86_mmx_psra_w;
7081 break;
7082 case Intrinsic::x86_mmx_psrai_d:
7083 NewIntNo = Intrinsic::x86_mmx_psra_d;
7084 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007085 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007086 }
7087 break;
7088 }
7089 }
Mon P Wangefa42202009-09-03 19:56:25 +00007090
7091 // The vector shift intrinsics with scalars uses 32b shift amounts but
7092 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7093 // to be zero.
7094 SDValue ShOps[4];
7095 ShOps[0] = ShAmt;
7096 ShOps[1] = DAG.getConstant(0, MVT::i32);
7097 if (ShAmtVT == MVT::v4i32) {
7098 ShOps[2] = DAG.getUNDEF(MVT::i32);
7099 ShOps[3] = DAG.getUNDEF(MVT::i32);
7100 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7101 } else {
7102 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
7103 }
7104
Owen Andersone50ed302009-08-10 22:56:29 +00007105 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00007106 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007107 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007108 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00007109 Op.getOperand(1), ShAmt);
7110 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00007111 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007112}
Evan Cheng72261582005-12-20 06:22:03 +00007113
Dan Gohman475871a2008-07-27 21:46:04 +00007114SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00007115 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007116 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00007117
7118 if (Depth > 0) {
7119 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
7120 SDValue Offset =
7121 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007122 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007123 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007124 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007125 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007126 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007127 }
7128
7129 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007130 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007131 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007132 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007133}
7134
Dan Gohman475871a2008-07-27 21:46:04 +00007135SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00007136 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7137 MFI->setFrameAddressIsTaken(true);
Owen Andersone50ed302009-08-10 22:56:29 +00007138 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007139 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007140 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7141 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007142 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007143 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007144 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7145 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007146 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007147}
7148
Dan Gohman475871a2008-07-27 21:46:04 +00007149SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00007150 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007151 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007152}
7153
Dan Gohman475871a2008-07-27 21:46:04 +00007154SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007155{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007156 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007157 SDValue Chain = Op.getOperand(0);
7158 SDValue Offset = Op.getOperand(1);
7159 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007160 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007161
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007162 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7163 getPointerTy());
7164 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007165
Dale Johannesene4d209d2009-02-03 20:21:25 +00007166 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007167 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007168 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007169 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007170 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007171 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007172
Dale Johannesene4d209d2009-02-03 20:21:25 +00007173 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007174 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007175 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007176}
7177
Dan Gohman475871a2008-07-27 21:46:04 +00007178SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00007179 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00007180 SDValue Root = Op.getOperand(0);
7181 SDValue Trmp = Op.getOperand(1); // trampoline
7182 SDValue FPtr = Op.getOperand(2); // nested function
7183 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007184 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007185
Dan Gohman69de1932008-02-06 22:27:42 +00007186 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007187
7188 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007189 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007190
7191 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007192 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7193 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007194
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007195 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7196 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007197
7198 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7199
7200 // Load the pointer to the nested function into R11.
7201 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007202 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007203 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007204 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007205
Owen Anderson825b72b2009-08-11 20:47:22 +00007206 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7207 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007208 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7209 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007210
7211 // Load the 'nest' parameter value into R10.
7212 // R10 is specified in X86CallingConv.td
7213 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007214 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7215 DAG.getConstant(10, MVT::i64));
7216 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007217 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007218
Owen Anderson825b72b2009-08-11 20:47:22 +00007219 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7220 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007221 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7222 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007223
7224 // Jump to the nested function.
7225 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007226 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7227 DAG.getConstant(20, MVT::i64));
7228 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007229 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007230
7231 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007232 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7233 DAG.getConstant(22, MVT::i64));
7234 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007235 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007236
Dan Gohman475871a2008-07-27 21:46:04 +00007237 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007238 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007239 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007240 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007241 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007242 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007243 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007244 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007245
7246 switch (CC) {
7247 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007248 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007249 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007250 case CallingConv::X86_StdCall: {
7251 // Pass 'nest' parameter in ECX.
7252 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007253 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007254
7255 // Check that ECX wasn't needed by an 'inreg' parameter.
7256 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007257 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007258
Chris Lattner58d74912008-03-12 17:45:29 +00007259 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007260 unsigned InRegCount = 0;
7261 unsigned Idx = 1;
7262
7263 for (FunctionType::param_iterator I = FTy->param_begin(),
7264 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007265 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007266 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007267 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007268
7269 if (InRegCount > 2) {
Chris Lattner75361b62010-04-07 22:58:41 +00007270 report_fatal_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007271 }
7272 }
7273 break;
7274 }
7275 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007276 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007277 // Pass 'nest' parameter in EAX.
7278 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007279 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007280 break;
7281 }
7282
Dan Gohman475871a2008-07-27 21:46:04 +00007283 SDValue OutChains[4];
7284 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007285
Owen Anderson825b72b2009-08-11 20:47:22 +00007286 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7287 DAG.getConstant(10, MVT::i32));
7288 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007289
Chris Lattnera62fe662010-02-05 19:20:30 +00007290 // This is storing the opcode for MOV32ri.
7291 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007292 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007293 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007294 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007295 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007296
Owen Anderson825b72b2009-08-11 20:47:22 +00007297 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7298 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007299 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7300 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007301
Chris Lattnera62fe662010-02-05 19:20:30 +00007302 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007303 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7304 DAG.getConstant(5, MVT::i32));
7305 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007306 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007307
Owen Anderson825b72b2009-08-11 20:47:22 +00007308 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7309 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007310 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7311 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007312
Dan Gohman475871a2008-07-27 21:46:04 +00007313 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007314 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007315 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007316 }
7317}
7318
Dan Gohman475871a2008-07-27 21:46:04 +00007319SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007320 /*
7321 The rounding mode is in bits 11:10 of FPSR, and has the following
7322 settings:
7323 00 Round to nearest
7324 01 Round to -inf
7325 10 Round to +inf
7326 11 Round to 0
7327
7328 FLT_ROUNDS, on the other hand, expects the following:
7329 -1 Undefined
7330 0 Round to 0
7331 1 Round to nearest
7332 2 Round to +inf
7333 3 Round to -inf
7334
7335 To perform the conversion, we do:
7336 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7337 */
7338
7339 MachineFunction &MF = DAG.getMachineFunction();
7340 const TargetMachine &TM = MF.getTarget();
7341 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7342 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007343 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007344 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007345
7346 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007347 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007348 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007349
Owen Anderson825b72b2009-08-11 20:47:22 +00007350 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007351 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007352
7353 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007354 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7355 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007356
7357 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007358 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007359 DAG.getNode(ISD::SRL, dl, MVT::i16,
7360 DAG.getNode(ISD::AND, dl, MVT::i16,
7361 CWD, DAG.getConstant(0x800, MVT::i16)),
7362 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007363 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007364 DAG.getNode(ISD::SRL, dl, MVT::i16,
7365 DAG.getNode(ISD::AND, dl, MVT::i16,
7366 CWD, DAG.getConstant(0x400, MVT::i16)),
7367 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007368
Dan Gohman475871a2008-07-27 21:46:04 +00007369 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007370 DAG.getNode(ISD::AND, dl, MVT::i16,
7371 DAG.getNode(ISD::ADD, dl, MVT::i16,
7372 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7373 DAG.getConstant(1, MVT::i16)),
7374 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007375
7376
Duncan Sands83ec4b62008-06-06 12:08:01 +00007377 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007378 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007379}
7380
Dan Gohman475871a2008-07-27 21:46:04 +00007381SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007382 EVT VT = Op.getValueType();
7383 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007384 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007385 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007386
7387 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007388 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007389 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007390 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007391 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007392 }
Evan Cheng18efe262007-12-14 02:13:44 +00007393
Evan Cheng152804e2007-12-14 08:30:15 +00007394 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007395 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007396 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007397
7398 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007399 SDValue Ops[] = {
7400 Op,
7401 DAG.getConstant(NumBits+NumBits-1, OpVT),
7402 DAG.getConstant(X86::COND_E, MVT::i8),
7403 Op.getValue(1)
7404 };
7405 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007406
7407 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007408 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007409
Owen Anderson825b72b2009-08-11 20:47:22 +00007410 if (VT == MVT::i8)
7411 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007412 return Op;
7413}
7414
Dan Gohman475871a2008-07-27 21:46:04 +00007415SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007416 EVT VT = Op.getValueType();
7417 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007418 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007419 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007420
7421 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007422 if (VT == MVT::i8) {
7423 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007424 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007425 }
Evan Cheng152804e2007-12-14 08:30:15 +00007426
7427 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007428 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007429 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007430
7431 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007432 SDValue Ops[] = {
7433 Op,
7434 DAG.getConstant(NumBits, OpVT),
7435 DAG.getConstant(X86::COND_E, MVT::i8),
7436 Op.getValue(1)
7437 };
7438 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007439
Owen Anderson825b72b2009-08-11 20:47:22 +00007440 if (VT == MVT::i8)
7441 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007442 return Op;
7443}
7444
Mon P Wangaf9b9522008-12-18 21:42:19 +00007445SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007446 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007447 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007448 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007449
Mon P Wangaf9b9522008-12-18 21:42:19 +00007450 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7451 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7452 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7453 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7454 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7455 //
7456 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7457 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7458 // return AloBlo + AloBhi + AhiBlo;
7459
7460 SDValue A = Op.getOperand(0);
7461 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007462
Dale Johannesene4d209d2009-02-03 20:21:25 +00007463 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007464 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7465 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007466 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007467 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7468 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007469 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007470 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007471 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007472 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007473 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007474 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007475 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007476 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007477 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007478 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007479 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7480 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007481 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007482 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7483 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007484 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7485 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007486 return Res;
7487}
7488
7489
Bill Wendling74c37652008-12-09 22:08:41 +00007490SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
7491 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7492 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007493 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7494 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007495 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007496 SDValue LHS = N->getOperand(0);
7497 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007498 unsigned BaseOp = 0;
7499 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007500 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007501
7502 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007503 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007504 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007505 // A subtract of one will be selected as a INC. Note that INC doesn't
7506 // set CF, so we can't do this for UADDO.
7507 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7508 if (C->getAPIntValue() == 1) {
7509 BaseOp = X86ISD::INC;
7510 Cond = X86::COND_O;
7511 break;
7512 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007513 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007514 Cond = X86::COND_O;
7515 break;
7516 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007517 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007518 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007519 break;
7520 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007521 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7522 // set CF, so we can't do this for USUBO.
7523 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7524 if (C->getAPIntValue() == 1) {
7525 BaseOp = X86ISD::DEC;
7526 Cond = X86::COND_O;
7527 break;
7528 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007529 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007530 Cond = X86::COND_O;
7531 break;
7532 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007533 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007534 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007535 break;
7536 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007537 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007538 Cond = X86::COND_O;
7539 break;
7540 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007541 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007542 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007543 break;
7544 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007545
Bill Wendling61edeb52008-12-02 01:06:39 +00007546 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007547 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007548 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007549
Bill Wendling61edeb52008-12-02 01:06:39 +00007550 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007551 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007552 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007553
Bill Wendling61edeb52008-12-02 01:06:39 +00007554 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7555 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007556}
7557
Dan Gohman475871a2008-07-27 21:46:04 +00007558SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00007559 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007560 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007561 unsigned Reg = 0;
7562 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007563 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007564 default:
7565 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007566 case MVT::i8: Reg = X86::AL; size = 1; break;
7567 case MVT::i16: Reg = X86::AX; size = 2; break;
7568 case MVT::i32: Reg = X86::EAX; size = 4; break;
7569 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007570 assert(Subtarget->is64Bit() && "Node not type legal!");
7571 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007572 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007573 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007574 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007575 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007576 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007577 Op.getOperand(1),
7578 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007579 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007580 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007581 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007582 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007583 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007584 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007585 return cpOut;
7586}
7587
Duncan Sands1607f052008-12-01 11:39:25 +00007588SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00007589 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00007590 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007591 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007592 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007593 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007594 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007595 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7596 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007597 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007598 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7599 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007600 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007601 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007602 rdx.getValue(1)
7603 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007604 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007605}
7606
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007607SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
7608 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007609 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007610 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007611 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007612 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007613 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007614 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007615 Node->getOperand(0),
7616 Node->getOperand(1), negOp,
7617 cast<AtomicSDNode>(Node)->getSrcValue(),
7618 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007619}
7620
Evan Cheng0db9fe62006-04-25 20:13:52 +00007621/// LowerOperation - Provide custom lowering hooks for some operations.
7622///
Dan Gohman475871a2008-07-27 21:46:04 +00007623SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007624 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007625 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007626 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7627 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007628 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007629 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007630 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7631 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7632 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7633 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7634 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7635 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007636 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007637 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007638 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007639 case ISD::SHL_PARTS:
7640 case ISD::SRA_PARTS:
7641 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7642 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007643 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007644 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007645 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007646 case ISD::FABS: return LowerFABS(Op, DAG);
7647 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007648 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007649 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007650 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007651 case ISD::SELECT: return LowerSELECT(Op, DAG);
7652 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007653 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007654 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007655 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007656 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007657 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007658 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7659 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007660 case ISD::FRAME_TO_ARGS_OFFSET:
7661 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007662 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007663 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007664 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007665 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007666 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7667 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007668 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007669 case ISD::SADDO:
7670 case ISD::UADDO:
7671 case ISD::SSUBO:
7672 case ISD::USUBO:
7673 case ISD::SMULO:
7674 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007675 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007676 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007677}
7678
Duncan Sands1607f052008-12-01 11:39:25 +00007679void X86TargetLowering::
7680ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
7681 SelectionDAG &DAG, unsigned NewOp) {
Owen Andersone50ed302009-08-10 22:56:29 +00007682 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007683 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007684 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007685
7686 SDValue Chain = Node->getOperand(0);
7687 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007688 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007689 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007690 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007691 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007692 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007693 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007694 SDValue Result =
7695 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7696 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007697 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007698 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007699 Results.push_back(Result.getValue(2));
7700}
7701
Duncan Sands126d9072008-07-04 11:47:58 +00007702/// ReplaceNodeResults - Replace a node with an illegal result type
7703/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007704void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7705 SmallVectorImpl<SDValue>&Results,
7706 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007707 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007708 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007709 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007710 assert(false && "Do not know how to custom type legalize this operation!");
7711 return;
7712 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007713 std::pair<SDValue,SDValue> Vals =
7714 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007715 SDValue FIST = Vals.first, StackSlot = Vals.second;
7716 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007717 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007718 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007719 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7720 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007721 }
7722 return;
7723 }
7724 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007725 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007726 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007727 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007728 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007729 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007730 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007731 eax.getValue(2));
7732 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7733 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007734 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007735 Results.push_back(edx.getValue(1));
7736 return;
7737 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007738 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007739 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007740 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007741 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007742 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7743 DAG.getConstant(0, MVT::i32));
7744 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7745 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007746 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7747 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007748 cpInL.getValue(1));
7749 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007750 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7751 DAG.getConstant(0, MVT::i32));
7752 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7753 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007754 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007755 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007756 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007757 swapInL.getValue(1));
7758 SDValue Ops[] = { swapInH.getValue(0),
7759 N->getOperand(1),
7760 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007761 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007762 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007763 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007764 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007765 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007766 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007767 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007768 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007769 Results.push_back(cpOutH.getValue(1));
7770 return;
7771 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007772 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007773 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7774 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007775 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007776 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7777 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007778 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007779 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7780 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007781 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007782 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7783 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007784 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007785 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7786 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007787 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007788 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7789 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007790 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007791 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7792 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007793 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007794}
7795
Evan Cheng72261582005-12-20 06:22:03 +00007796const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7797 switch (Opcode) {
7798 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007799 case X86ISD::BSF: return "X86ISD::BSF";
7800 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007801 case X86ISD::SHLD: return "X86ISD::SHLD";
7802 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007803 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007804 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007805 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007806 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007807 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007808 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007809 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7810 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7811 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007812 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007813 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007814 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007815 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007816 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007817 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007818 case X86ISD::COMI: return "X86ISD::COMI";
7819 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007820 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007821 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007822 case X86ISD::CMOV: return "X86ISD::CMOV";
7823 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007824 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007825 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7826 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007827 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007828 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007829 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007830 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007831 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007832 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7833 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007834 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007835 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007836 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007837 case X86ISD::FMAX: return "X86ISD::FMAX";
7838 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007839 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7840 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007841 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007842 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007843 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007844 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007845 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007846 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7847 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007848 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7849 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7850 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7851 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7852 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7853 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007854 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7855 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007856 case X86ISD::VSHL: return "X86ISD::VSHL";
7857 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007858 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7859 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7860 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7861 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7862 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7863 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7864 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7865 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7866 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7867 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007868 case X86ISD::ADD: return "X86ISD::ADD";
7869 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007870 case X86ISD::SMUL: return "X86ISD::SMUL";
7871 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007872 case X86ISD::INC: return "X86ISD::INC";
7873 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007874 case X86ISD::OR: return "X86ISD::OR";
7875 case X86ISD::XOR: return "X86ISD::XOR";
7876 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007877 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007878 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007879 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007880 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007881 }
7882}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007883
Chris Lattnerc9addb72007-03-30 23:15:24 +00007884// isLegalAddressingMode - Return true if the addressing mode represented
7885// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007886bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007887 const Type *Ty) const {
7888 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007889 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007890
Chris Lattnerc9addb72007-03-30 23:15:24 +00007891 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007892 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007893 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007894
Chris Lattnerc9addb72007-03-30 23:15:24 +00007895 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007896 unsigned GVFlags =
7897 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007898
Chris Lattnerdfed4132009-07-10 07:38:24 +00007899 // If a reference to this global requires an extra load, we can't fold it.
7900 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007901 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007902
Chris Lattnerdfed4132009-07-10 07:38:24 +00007903 // If BaseGV requires a register for the PIC base, we cannot also have a
7904 // BaseReg specified.
7905 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007906 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007907
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007908 // If lower 4G is not available, then we must use rip-relative addressing.
7909 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7910 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007911 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007912
Chris Lattnerc9addb72007-03-30 23:15:24 +00007913 switch (AM.Scale) {
7914 case 0:
7915 case 1:
7916 case 2:
7917 case 4:
7918 case 8:
7919 // These scales always work.
7920 break;
7921 case 3:
7922 case 5:
7923 case 9:
7924 // These scales are formed with basereg+scalereg. Only accept if there is
7925 // no basereg yet.
7926 if (AM.HasBaseReg)
7927 return false;
7928 break;
7929 default: // Other stuff never works.
7930 return false;
7931 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007932
Chris Lattnerc9addb72007-03-30 23:15:24 +00007933 return true;
7934}
7935
7936
Evan Cheng2bd122c2007-10-26 01:56:11 +00007937bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007938 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007939 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007940 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7941 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007942 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007943 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007944 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007945}
7946
Owen Andersone50ed302009-08-10 22:56:29 +00007947bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007948 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007949 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007950 unsigned NumBits1 = VT1.getSizeInBits();
7951 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007952 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007953 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007954 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007955}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007956
Dan Gohman97121ba2009-04-08 00:15:30 +00007957bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007958 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007959 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007960}
7961
Owen Andersone50ed302009-08-10 22:56:29 +00007962bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007963 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007964 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007965}
7966
Owen Andersone50ed302009-08-10 22:56:29 +00007967bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007968 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007969 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007970}
7971
Evan Cheng60c07e12006-07-05 22:17:51 +00007972/// isShuffleMaskLegal - Targets can use this to indicate that they only
7973/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7974/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7975/// are assumed to be legal.
7976bool
Eric Christopherfd179292009-08-27 18:07:15 +00007977X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007978 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00007979 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007980 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00007981 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00007982
Nate Begemana09008b2009-10-19 02:17:23 +00007983 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007984 return (VT.getVectorNumElements() == 2 ||
7985 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7986 isMOVLMask(M, VT) ||
7987 isSHUFPMask(M, VT) ||
7988 isPSHUFDMask(M, VT) ||
7989 isPSHUFHWMask(M, VT) ||
7990 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007991 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007992 isUNPCKLMask(M, VT) ||
7993 isUNPCKHMask(M, VT) ||
7994 isUNPCKL_v_undef_Mask(M, VT) ||
7995 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007996}
7997
Dan Gohman7d8143f2008-04-09 20:09:42 +00007998bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007999X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00008000 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00008001 unsigned NumElts = VT.getVectorNumElements();
8002 // FIXME: This collection of masks seems suspect.
8003 if (NumElts == 2)
8004 return true;
8005 if (NumElts == 4 && VT.getSizeInBits() == 128) {
8006 return (isMOVLMask(Mask, VT) ||
8007 isCommutedMOVLMask(Mask, VT, true) ||
8008 isSHUFPMask(Mask, VT) ||
8009 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00008010 }
8011 return false;
8012}
8013
8014//===----------------------------------------------------------------------===//
8015// X86 Scheduler Hooks
8016//===----------------------------------------------------------------------===//
8017
Mon P Wang63307c32008-05-05 19:05:59 +00008018// private utility function
8019MachineBasicBlock *
8020X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
8021 MachineBasicBlock *MBB,
8022 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008023 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008024 unsigned LoadOpc,
8025 unsigned CXchgOpc,
8026 unsigned copyOpc,
8027 unsigned notOpc,
8028 unsigned EAXreg,
8029 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008030 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008031 // For the atomic bitwise operator, we generate
8032 // thisMBB:
8033 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008034 // ld t1 = [bitinstr.addr]
8035 // op t2 = t1, [bitinstr.val]
8036 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008037 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8038 // bz newMBB
8039 // fallthrough -->nextMBB
8040 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8041 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008042 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008043 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008044
Mon P Wang63307c32008-05-05 19:05:59 +00008045 /// First build the CFG
8046 MachineFunction *F = MBB->getParent();
8047 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008048 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8049 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8050 F->insert(MBBIter, newMBB);
8051 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008052
Mon P Wang63307c32008-05-05 19:05:59 +00008053 // Move all successors to thisMBB to nextMBB
8054 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008055
Mon P Wang63307c32008-05-05 19:05:59 +00008056 // Update thisMBB to fall through to newMBB
8057 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008058
Mon P Wang63307c32008-05-05 19:05:59 +00008059 // newMBB jumps to itself and fall through to nextMBB
8060 newMBB->addSuccessor(nextMBB);
8061 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008062
Mon P Wang63307c32008-05-05 19:05:59 +00008063 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008064 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008065 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00008066 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008067 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008068 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008069 int numArgs = bInstr->getNumOperands() - 1;
8070 for (int i=0; i < numArgs; ++i)
8071 argOpers[i] = &bInstr->getOperand(i+1);
8072
8073 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008074 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8075 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008076
Dale Johannesen140be2d2008-08-19 18:47:28 +00008077 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008078 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008079 for (int i=0; i <= lastAddrIndx; ++i)
8080 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008081
Dale Johannesen140be2d2008-08-19 18:47:28 +00008082 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008083 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008084 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008085 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008086 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008087 tt = t1;
8088
Dale Johannesen140be2d2008-08-19 18:47:28 +00008089 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00008090 assert((argOpers[valArgIndx]->isReg() ||
8091 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008092 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00008093 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008094 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008095 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008096 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008097 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00008098 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008099
Dale Johannesene4d209d2009-02-03 20:21:25 +00008100 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008101 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008102
Dale Johannesene4d209d2009-02-03 20:21:25 +00008103 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008104 for (int i=0; i <= lastAddrIndx; ++i)
8105 (*MIB).addOperand(*argOpers[i]);
8106 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008107 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008108 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8109 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008110
Dale Johannesene4d209d2009-02-03 20:21:25 +00008111 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008112 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008113
Mon P Wang63307c32008-05-05 19:05:59 +00008114 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008115 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008116
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008117 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008118 return nextMBB;
8119}
8120
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008121// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008122MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008123X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8124 MachineBasicBlock *MBB,
8125 unsigned regOpcL,
8126 unsigned regOpcH,
8127 unsigned immOpcL,
8128 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008129 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008130 // For the atomic bitwise operator, we generate
8131 // thisMBB (instructions are in pairs, except cmpxchg8b)
8132 // ld t1,t2 = [bitinstr.addr]
8133 // newMBB:
8134 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8135 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008136 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008137 // mov ECX, EBX <- t5, t6
8138 // mov EAX, EDX <- t1, t2
8139 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8140 // mov t3, t4 <- EAX, EDX
8141 // bz newMBB
8142 // result in out1, out2
8143 // fallthrough -->nextMBB
8144
8145 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8146 const unsigned LoadOpc = X86::MOV32rm;
8147 const unsigned copyOpc = X86::MOV32rr;
8148 const unsigned NotOpc = X86::NOT32r;
8149 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8150 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8151 MachineFunction::iterator MBBIter = MBB;
8152 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008153
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008154 /// First build the CFG
8155 MachineFunction *F = MBB->getParent();
8156 MachineBasicBlock *thisMBB = MBB;
8157 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
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008162 // Move all successors to thisMBB to nextMBB
8163 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008164
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008165 // Update thisMBB to fall through to newMBB
8166 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008167
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008168 // newMBB jumps to itself 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 = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008173 // Insert instructions into newMBB based on incoming instruction
8174 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008175 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008176 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008177 MachineOperand& dest1Oper = bInstr->getOperand(0);
8178 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008179 MachineOperand* argOpers[2 + X86AddrNumOperands];
8180 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008181 argOpers[i] = &bInstr->getOperand(i+2);
8182
Evan Chengad5b52f2010-01-08 19:14:57 +00008183 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008184 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008185
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008186 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008187 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008188 for (int i=0; i <= lastAddrIndx; ++i)
8189 (*MIB).addOperand(*argOpers[i]);
8190 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008191 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008192 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008193 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008194 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008195 MachineOperand newOp3 = *(argOpers[3]);
8196 if (newOp3.isImm())
8197 newOp3.setImm(newOp3.getImm()+4);
8198 else
8199 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008200 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008201 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008202
8203 // t3/4 are defined later, at the bottom of the loop
8204 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8205 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008206 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008207 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008208 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008209 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8210
Evan Cheng306b4ca2010-01-08 23:41:50 +00008211 // The subsequent operations should be using the destination registers of
8212 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008213 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008214 t1 = F->getRegInfo().createVirtualRegister(RC);
8215 t2 = F->getRegInfo().createVirtualRegister(RC);
8216 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8217 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008218 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008219 t1 = dest1Oper.getReg();
8220 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008221 }
8222
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008223 int valArgIndx = lastAddrIndx + 1;
8224 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008225 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008226 "invalid operand");
8227 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8228 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008229 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008230 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008231 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008232 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008233 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008234 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008235 (*MIB).addOperand(*argOpers[valArgIndx]);
8236 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008237 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008238 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008239 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008240 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008241 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008242 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008243 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008244 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008245 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008246 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008247
Dale Johannesene4d209d2009-02-03 20:21:25 +00008248 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008249 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008250 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008251 MIB.addReg(t2);
8252
Dale Johannesene4d209d2009-02-03 20:21:25 +00008253 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008254 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008255 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008256 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008257
Dale Johannesene4d209d2009-02-03 20:21:25 +00008258 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008259 for (int i=0; i <= lastAddrIndx; ++i)
8260 (*MIB).addOperand(*argOpers[i]);
8261
8262 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008263 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8264 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008265
Dale Johannesene4d209d2009-02-03 20:21:25 +00008266 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008267 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008268 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008269 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008270
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008271 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008272 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008273
8274 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8275 return nextMBB;
8276}
8277
8278// private utility function
8279MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008280X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8281 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008282 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008283 // For the atomic min/max operator, we generate
8284 // thisMBB:
8285 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008286 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008287 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008288 // cmp t1, t2
8289 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008290 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008291 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8292 // bz newMBB
8293 // fallthrough -->nextMBB
8294 //
8295 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8296 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008297 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008298 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008299
Mon P Wang63307c32008-05-05 19:05:59 +00008300 /// First build the CFG
8301 MachineFunction *F = MBB->getParent();
8302 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008303 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8304 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8305 F->insert(MBBIter, newMBB);
8306 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008307
Dan Gohmand6708ea2009-08-15 01:38:56 +00008308 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008309 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008310
Mon P Wang63307c32008-05-05 19:05:59 +00008311 // Update thisMBB to fall through to newMBB
8312 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008313
Mon P Wang63307c32008-05-05 19:05:59 +00008314 // newMBB jumps to newMBB and fall through to nextMBB
8315 newMBB->addSuccessor(nextMBB);
8316 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008317
Dale Johannesene4d209d2009-02-03 20:21:25 +00008318 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008319 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008320 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008321 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008322 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008323 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008324 int numArgs = mInstr->getNumOperands() - 1;
8325 for (int i=0; i < numArgs; ++i)
8326 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008327
Mon P Wang63307c32008-05-05 19:05:59 +00008328 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008329 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8330 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008331
Mon P Wangab3e7472008-05-05 22:56:23 +00008332 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008333 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008334 for (int i=0; i <= lastAddrIndx; ++i)
8335 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008336
Mon P Wang63307c32008-05-05 19:05:59 +00008337 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008338 assert((argOpers[valArgIndx]->isReg() ||
8339 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008340 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008341
8342 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008343 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008344 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008345 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008346 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008347 (*MIB).addOperand(*argOpers[valArgIndx]);
8348
Dale Johannesene4d209d2009-02-03 20:21:25 +00008349 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008350 MIB.addReg(t1);
8351
Dale Johannesene4d209d2009-02-03 20:21:25 +00008352 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008353 MIB.addReg(t1);
8354 MIB.addReg(t2);
8355
8356 // Generate movc
8357 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008358 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008359 MIB.addReg(t2);
8360 MIB.addReg(t1);
8361
8362 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008363 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008364 for (int i=0; i <= lastAddrIndx; ++i)
8365 (*MIB).addOperand(*argOpers[i]);
8366 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008367 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008368 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8369 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008370
Dale Johannesene4d209d2009-02-03 20:21:25 +00008371 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008372 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008373
Mon P Wang63307c32008-05-05 19:05:59 +00008374 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008375 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008376
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008377 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008378 return nextMBB;
8379}
8380
Eric Christopherf83a5de2009-08-27 18:08:16 +00008381// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8382// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008383MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008384X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008385 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008386
8387 MachineFunction *F = BB->getParent();
8388 DebugLoc dl = MI->getDebugLoc();
8389 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8390
8391 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008392 if (memArg)
8393 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8394 else
8395 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008396
8397 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8398
8399 for (unsigned i = 0; i < numArgs; ++i) {
8400 MachineOperand &Op = MI->getOperand(i+1);
8401
8402 if (!(Op.isReg() && Op.isImplicit()))
8403 MIB.addOperand(Op);
8404 }
8405
8406 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8407 .addReg(X86::XMM0);
8408
8409 F->DeleteMachineInstr(MI);
8410
8411 return BB;
8412}
8413
8414MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008415X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8416 MachineInstr *MI,
8417 MachineBasicBlock *MBB) const {
8418 // Emit code to save XMM registers to the stack. The ABI says that the
8419 // number of registers to save is given in %al, so it's theoretically
8420 // possible to do an indirect jump trick to avoid saving all of them,
8421 // however this code takes a simpler approach and just executes all
8422 // of the stores if %al is non-zero. It's less code, and it's probably
8423 // easier on the hardware branch predictor, and stores aren't all that
8424 // expensive anyway.
8425
8426 // Create the new basic blocks. One block contains all the XMM stores,
8427 // and one block is the final destination regardless of whether any
8428 // stores were performed.
8429 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8430 MachineFunction *F = MBB->getParent();
8431 MachineFunction::iterator MBBIter = MBB;
8432 ++MBBIter;
8433 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8434 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8435 F->insert(MBBIter, XMMSaveMBB);
8436 F->insert(MBBIter, EndMBB);
8437
8438 // Set up the CFG.
8439 // Move any original successors of MBB to the end block.
8440 EndMBB->transferSuccessors(MBB);
8441 // The original block will now fall through to the XMM save block.
8442 MBB->addSuccessor(XMMSaveMBB);
8443 // The XMMSaveMBB will fall through to the end block.
8444 XMMSaveMBB->addSuccessor(EndMBB);
8445
8446 // Now add the instructions.
8447 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8448 DebugLoc DL = MI->getDebugLoc();
8449
8450 unsigned CountReg = MI->getOperand(0).getReg();
8451 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8452 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8453
8454 if (!Subtarget->isTargetWin64()) {
8455 // If %al is 0, branch around the XMM save block.
8456 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008457 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008458 MBB->addSuccessor(EndMBB);
8459 }
8460
8461 // In the XMM save block, save all the XMM argument registers.
8462 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8463 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008464 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008465 F->getMachineMemOperand(
8466 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8467 MachineMemOperand::MOStore, Offset,
8468 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008469 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8470 .addFrameIndex(RegSaveFrameIndex)
8471 .addImm(/*Scale=*/1)
8472 .addReg(/*IndexReg=*/0)
8473 .addImm(/*Disp=*/Offset)
8474 .addReg(/*Segment=*/0)
8475 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008476 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008477 }
8478
8479 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8480
8481 return EndMBB;
8482}
Mon P Wang63307c32008-05-05 19:05:59 +00008483
Evan Cheng60c07e12006-07-05 22:17:51 +00008484MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008485X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Evan Chengce319102009-09-19 09:51:03 +00008486 MachineBasicBlock *BB,
8487 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattner52600972009-09-02 05:57:00 +00008488 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8489 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008490
Chris Lattner52600972009-09-02 05:57:00 +00008491 // To "insert" a SELECT_CC instruction, we actually have to insert the
8492 // diamond control-flow pattern. The incoming instruction knows the
8493 // destination vreg to set, the condition code register to branch on, the
8494 // true/false values to select between, and a branch opcode to use.
8495 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8496 MachineFunction::iterator It = BB;
8497 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008498
Chris Lattner52600972009-09-02 05:57:00 +00008499 // thisMBB:
8500 // ...
8501 // TrueVal = ...
8502 // cmpTY ccX, r1, r2
8503 // bCC copy1MBB
8504 // fallthrough --> copy0MBB
8505 MachineBasicBlock *thisMBB = BB;
8506 MachineFunction *F = BB->getParent();
8507 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8508 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8509 unsigned Opc =
8510 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8511 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8512 F->insert(It, copy0MBB);
8513 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008514 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008515 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +00008516 // Also inform sdisel of the edge changes.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008517 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Evan Chengce319102009-09-19 09:51:03 +00008518 E = BB->succ_end(); I != E; ++I) {
8519 EM->insert(std::make_pair(*I, sinkMBB));
8520 sinkMBB->addSuccessor(*I);
8521 }
8522 // Next, remove all successors of the current block, and add the true
8523 // and fallthrough blocks as its successors.
8524 while (!BB->succ_empty())
8525 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008526 // Add the true and fallthrough blocks as its successors.
8527 BB->addSuccessor(copy0MBB);
8528 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008529
Chris Lattner52600972009-09-02 05:57:00 +00008530 // copy0MBB:
8531 // %FalseValue = ...
8532 // # fallthrough to sinkMBB
8533 BB = copy0MBB;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008534
Chris Lattner52600972009-09-02 05:57:00 +00008535 // Update machine-CFG edges
8536 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008537
Chris Lattner52600972009-09-02 05:57:00 +00008538 // sinkMBB:
8539 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8540 // ...
8541 BB = sinkMBB;
8542 BuildMI(BB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
8543 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8544 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8545
8546 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8547 return BB;
8548}
8549
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008550MachineBasicBlock *
8551X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
8552 MachineBasicBlock *BB,
8553 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
8554 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8555 DebugLoc DL = MI->getDebugLoc();
8556 MachineFunction *F = BB->getParent();
8557
8558 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8559 // non-trivial part is impdef of ESP.
8560 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8561 // mingw-w64.
8562
8563 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8564 .addExternalSymbol("_alloca")
8565 .addReg(X86::EAX, RegState::Implicit)
8566 .addReg(X86::ESP, RegState::Implicit)
8567 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8568 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8569
8570 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8571 return BB;
8572}
Chris Lattner52600972009-09-02 05:57:00 +00008573
8574MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008575X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +00008576 MachineBasicBlock *BB,
8577 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008578 switch (MI->getOpcode()) {
8579 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008580 case X86::MINGW_ALLOCA:
8581 return EmitLoweredMingwAlloca(MI, BB, EM);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008582 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008583 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008584 case X86::CMOV_FR32:
8585 case X86::CMOV_FR64:
8586 case X86::CMOV_V4F32:
8587 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008588 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008589 case X86::CMOV_GR16:
8590 case X86::CMOV_GR32:
8591 case X86::CMOV_RFP32:
8592 case X86::CMOV_RFP64:
8593 case X86::CMOV_RFP80:
Evan Chengce319102009-09-19 09:51:03 +00008594 return EmitLoweredSelect(MI, BB, EM);
Evan Cheng60c07e12006-07-05 22:17:51 +00008595
Dale Johannesen849f2142007-07-03 00:53:03 +00008596 case X86::FP32_TO_INT16_IN_MEM:
8597 case X86::FP32_TO_INT32_IN_MEM:
8598 case X86::FP32_TO_INT64_IN_MEM:
8599 case X86::FP64_TO_INT16_IN_MEM:
8600 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008601 case X86::FP64_TO_INT64_IN_MEM:
8602 case X86::FP80_TO_INT16_IN_MEM:
8603 case X86::FP80_TO_INT32_IN_MEM:
8604 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008605 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8606 DebugLoc DL = MI->getDebugLoc();
8607
Evan Cheng60c07e12006-07-05 22:17:51 +00008608 // Change the floating point control register to use "round towards zero"
8609 // mode when truncating to an integer value.
8610 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008611 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008612 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008613
8614 // Load the old value of the high byte of the control word...
8615 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008616 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008617 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008618 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008619
8620 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008621 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008622 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008623
8624 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008625 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008626
8627 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008628 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008629 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008630
8631 // Get the X86 opcode to use.
8632 unsigned Opc;
8633 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008634 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008635 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8636 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8637 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8638 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8639 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8640 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008641 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8642 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8643 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008644 }
8645
8646 X86AddressMode AM;
8647 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008648 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008649 AM.BaseType = X86AddressMode::RegBase;
8650 AM.Base.Reg = Op.getReg();
8651 } else {
8652 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008653 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008654 }
8655 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008656 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008657 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008658 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008659 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008660 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008661 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008662 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008663 AM.GV = Op.getGlobal();
8664 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008665 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008666 }
Chris Lattner52600972009-09-02 05:57:00 +00008667 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008668 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008669
8670 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008671 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008672
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008673 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008674 return BB;
8675 }
Dale Johannesenbfdf7f32010-03-10 22:13:47 +00008676 // DBG_VALUE. Only the frame index case is done here.
8677 case X86::DBG_VALUE: {
8678 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8679 DebugLoc DL = MI->getDebugLoc();
8680 X86AddressMode AM;
8681 MachineFunction *F = BB->getParent();
8682 AM.BaseType = X86AddressMode::FrameIndexBase;
8683 AM.Base.FrameIndex = MI->getOperand(0).getImm();
8684 addFullAddress(BuildMI(BB, DL, TII->get(X86::DBG_VALUE)), AM).
8685 addImm(MI->getOperand(1).getImm()).
8686 addMetadata(MI->getOperand(2).getMetadata());
8687 F->DeleteMachineInstr(MI); // Remove pseudo.
8688 return BB;
8689 }
8690
Eric Christopherb120ab42009-08-18 22:50:32 +00008691 // String/text processing lowering.
8692 case X86::PCMPISTRM128REG:
8693 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8694 case X86::PCMPISTRM128MEM:
8695 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8696 case X86::PCMPESTRM128REG:
8697 return EmitPCMP(MI, BB, 5, false /* in mem */);
8698 case X86::PCMPESTRM128MEM:
8699 return EmitPCMP(MI, BB, 5, true /* in mem */);
8700
8701 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008702 case X86::ATOMAND32:
8703 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008704 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008705 X86::LCMPXCHG32, X86::MOV32rr,
8706 X86::NOT32r, X86::EAX,
8707 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008708 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008709 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8710 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008711 X86::LCMPXCHG32, X86::MOV32rr,
8712 X86::NOT32r, X86::EAX,
8713 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008714 case X86::ATOMXOR32:
8715 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008716 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008717 X86::LCMPXCHG32, X86::MOV32rr,
8718 X86::NOT32r, X86::EAX,
8719 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008720 case X86::ATOMNAND32:
8721 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008722 X86::AND32ri, X86::MOV32rm,
8723 X86::LCMPXCHG32, X86::MOV32rr,
8724 X86::NOT32r, X86::EAX,
8725 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008726 case X86::ATOMMIN32:
8727 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8728 case X86::ATOMMAX32:
8729 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8730 case X86::ATOMUMIN32:
8731 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8732 case X86::ATOMUMAX32:
8733 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008734
8735 case X86::ATOMAND16:
8736 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8737 X86::AND16ri, X86::MOV16rm,
8738 X86::LCMPXCHG16, X86::MOV16rr,
8739 X86::NOT16r, X86::AX,
8740 X86::GR16RegisterClass);
8741 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008742 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008743 X86::OR16ri, X86::MOV16rm,
8744 X86::LCMPXCHG16, X86::MOV16rr,
8745 X86::NOT16r, X86::AX,
8746 X86::GR16RegisterClass);
8747 case X86::ATOMXOR16:
8748 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8749 X86::XOR16ri, X86::MOV16rm,
8750 X86::LCMPXCHG16, X86::MOV16rr,
8751 X86::NOT16r, X86::AX,
8752 X86::GR16RegisterClass);
8753 case X86::ATOMNAND16:
8754 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8755 X86::AND16ri, X86::MOV16rm,
8756 X86::LCMPXCHG16, X86::MOV16rr,
8757 X86::NOT16r, X86::AX,
8758 X86::GR16RegisterClass, true);
8759 case X86::ATOMMIN16:
8760 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8761 case X86::ATOMMAX16:
8762 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8763 case X86::ATOMUMIN16:
8764 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8765 case X86::ATOMUMAX16:
8766 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8767
8768 case X86::ATOMAND8:
8769 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8770 X86::AND8ri, X86::MOV8rm,
8771 X86::LCMPXCHG8, X86::MOV8rr,
8772 X86::NOT8r, X86::AL,
8773 X86::GR8RegisterClass);
8774 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008775 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008776 X86::OR8ri, X86::MOV8rm,
8777 X86::LCMPXCHG8, X86::MOV8rr,
8778 X86::NOT8r, X86::AL,
8779 X86::GR8RegisterClass);
8780 case X86::ATOMXOR8:
8781 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8782 X86::XOR8ri, X86::MOV8rm,
8783 X86::LCMPXCHG8, X86::MOV8rr,
8784 X86::NOT8r, X86::AL,
8785 X86::GR8RegisterClass);
8786 case X86::ATOMNAND8:
8787 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8788 X86::AND8ri, X86::MOV8rm,
8789 X86::LCMPXCHG8, X86::MOV8rr,
8790 X86::NOT8r, X86::AL,
8791 X86::GR8RegisterClass, true);
8792 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008793 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008794 case X86::ATOMAND64:
8795 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008796 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008797 X86::LCMPXCHG64, X86::MOV64rr,
8798 X86::NOT64r, X86::RAX,
8799 X86::GR64RegisterClass);
8800 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008801 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8802 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008803 X86::LCMPXCHG64, X86::MOV64rr,
8804 X86::NOT64r, X86::RAX,
8805 X86::GR64RegisterClass);
8806 case X86::ATOMXOR64:
8807 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008808 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008809 X86::LCMPXCHG64, X86::MOV64rr,
8810 X86::NOT64r, X86::RAX,
8811 X86::GR64RegisterClass);
8812 case X86::ATOMNAND64:
8813 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8814 X86::AND64ri32, X86::MOV64rm,
8815 X86::LCMPXCHG64, X86::MOV64rr,
8816 X86::NOT64r, X86::RAX,
8817 X86::GR64RegisterClass, true);
8818 case X86::ATOMMIN64:
8819 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8820 case X86::ATOMMAX64:
8821 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8822 case X86::ATOMUMIN64:
8823 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8824 case X86::ATOMUMAX64:
8825 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008826
8827 // This group does 64-bit operations on a 32-bit host.
8828 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008829 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008830 X86::AND32rr, X86::AND32rr,
8831 X86::AND32ri, X86::AND32ri,
8832 false);
8833 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008834 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008835 X86::OR32rr, X86::OR32rr,
8836 X86::OR32ri, X86::OR32ri,
8837 false);
8838 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008839 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008840 X86::XOR32rr, X86::XOR32rr,
8841 X86::XOR32ri, X86::XOR32ri,
8842 false);
8843 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008844 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008845 X86::AND32rr, X86::AND32rr,
8846 X86::AND32ri, X86::AND32ri,
8847 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008848 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008849 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008850 X86::ADD32rr, X86::ADC32rr,
8851 X86::ADD32ri, X86::ADC32ri,
8852 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008853 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008854 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008855 X86::SUB32rr, X86::SBB32rr,
8856 X86::SUB32ri, X86::SBB32ri,
8857 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008858 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008859 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008860 X86::MOV32rr, X86::MOV32rr,
8861 X86::MOV32ri, X86::MOV32ri,
8862 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008863 case X86::VASTART_SAVE_XMM_REGS:
8864 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008865 }
8866}
8867
8868//===----------------------------------------------------------------------===//
8869// X86 Optimization Hooks
8870//===----------------------------------------------------------------------===//
8871
Dan Gohman475871a2008-07-27 21:46:04 +00008872void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008873 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008874 APInt &KnownZero,
8875 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008876 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008877 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008878 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008879 assert((Opc >= ISD::BUILTIN_OP_END ||
8880 Opc == ISD::INTRINSIC_WO_CHAIN ||
8881 Opc == ISD::INTRINSIC_W_CHAIN ||
8882 Opc == ISD::INTRINSIC_VOID) &&
8883 "Should use MaskedValueIsZero if you don't know whether Op"
8884 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008885
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008886 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008887 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008888 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008889 case X86ISD::ADD:
8890 case X86ISD::SUB:
8891 case X86ISD::SMUL:
8892 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008893 case X86ISD::INC:
8894 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008895 case X86ISD::OR:
8896 case X86ISD::XOR:
8897 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008898 // These nodes' second result is a boolean.
8899 if (Op.getResNo() == 0)
8900 break;
8901 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008902 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008903 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8904 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008905 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008906 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008907}
Chris Lattner259e97c2006-01-31 19:43:35 +00008908
Evan Cheng206ee9d2006-07-07 08:33:52 +00008909/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008910/// node is a GlobalAddress + offset.
8911bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00008912 const GlobalValue* &GA,
8913 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00008914 if (N->getOpcode() == X86ISD::Wrapper) {
8915 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008916 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008917 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008918 return true;
8919 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008920 }
Evan Chengad4196b2008-05-12 19:56:52 +00008921 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008922}
8923
Evan Cheng206ee9d2006-07-07 08:33:52 +00008924/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8925/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8926/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008927/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008928static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008929 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008930 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008931 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008932 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008933
Eli Friedman7a5e5552009-06-07 06:52:44 +00008934 if (VT.getSizeInBits() != 128)
8935 return SDValue();
8936
Nate Begemanfdea31a2010-03-24 20:49:50 +00008937 SmallVector<SDValue, 16> Elts;
8938 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8939 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8940
8941 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008942}
Evan Chengd880b972008-05-09 21:53:03 +00008943
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008944/// PerformShuffleCombine - Detect vector gather/scatter index generation
8945/// and convert it from being a bunch of shuffles and extracts to a simple
8946/// store and scalar loads to extract the elements.
8947static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8948 const TargetLowering &TLI) {
8949 SDValue InputVector = N->getOperand(0);
8950
8951 // Only operate on vectors of 4 elements, where the alternative shuffling
8952 // gets to be more expensive.
8953 if (InputVector.getValueType() != MVT::v4i32)
8954 return SDValue();
8955
8956 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8957 // single use which is a sign-extend or zero-extend, and all elements are
8958 // used.
8959 SmallVector<SDNode *, 4> Uses;
8960 unsigned ExtractedElements = 0;
8961 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8962 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8963 if (UI.getUse().getResNo() != InputVector.getResNo())
8964 return SDValue();
8965
8966 SDNode *Extract = *UI;
8967 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8968 return SDValue();
8969
8970 if (Extract->getValueType(0) != MVT::i32)
8971 return SDValue();
8972 if (!Extract->hasOneUse())
8973 return SDValue();
8974 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8975 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8976 return SDValue();
8977 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8978 return SDValue();
8979
8980 // Record which element was extracted.
8981 ExtractedElements |=
8982 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8983
8984 Uses.push_back(Extract);
8985 }
8986
8987 // If not all the elements were used, this may not be worthwhile.
8988 if (ExtractedElements != 15)
8989 return SDValue();
8990
8991 // Ok, we've now decided to do the transformation.
8992 DebugLoc dl = InputVector.getDebugLoc();
8993
8994 // Store the value to a temporary stack slot.
8995 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
8996 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
8997 false, false, 0);
8998
8999 // Replace each use (extract) with a load of the appropriate element.
9000 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
9001 UE = Uses.end(); UI != UE; ++UI) {
9002 SDNode *Extract = *UI;
9003
9004 // Compute the element's address.
9005 SDValue Idx = Extract->getOperand(1);
9006 unsigned EltSize =
9007 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
9008 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
9009 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
9010
9011 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
9012
9013 // Load the scalar.
9014 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
9015 NULL, 0, false, false, 0);
9016
9017 // Replace the exact with the load.
9018 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
9019 }
9020
9021 // The replacement was made in place; don't return anything.
9022 return SDValue();
9023}
9024
Chris Lattner83e6c992006-10-04 06:57:07 +00009025/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009026static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00009027 const X86Subtarget *Subtarget) {
9028 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009029 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00009030 // Get the LHS/RHS of the select.
9031 SDValue LHS = N->getOperand(1);
9032 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009033
Dan Gohman670e5392009-09-21 18:03:22 +00009034 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00009035 // instructions match the semantics of the common C idiom x<y?x:y but not
9036 // x<=y?x:y, because of how they handle negative zero (which can be
9037 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00009038 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00009039 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00009040 Cond.getOpcode() == ISD::SETCC) {
9041 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009042
Chris Lattner47b4ce82009-03-11 05:48:52 +00009043 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00009044 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00009045 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
9046 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009047 switch (CC) {
9048 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009049 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009050 // Converting this to a min would handle NaNs incorrectly, and swapping
9051 // the operands would cause it to handle comparisons between positive
9052 // and negative zero incorrectly.
9053 if (!FiniteOnlyFPMath() &&
9054 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9055 if (!UnsafeFPMath &&
9056 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9057 break;
9058 std::swap(LHS, RHS);
9059 }
Dan Gohman670e5392009-09-21 18:03:22 +00009060 Opcode = X86ISD::FMIN;
9061 break;
9062 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009063 // Converting this to a min would handle comparisons between positive
9064 // and negative zero incorrectly.
9065 if (!UnsafeFPMath &&
9066 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
9067 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009068 Opcode = X86ISD::FMIN;
9069 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009070 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009071 // Converting this to a min would handle both negative zeros and NaNs
9072 // incorrectly, but we can swap the operands to fix both.
9073 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009074 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009075 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009076 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009077 Opcode = X86ISD::FMIN;
9078 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009079
Dan Gohman670e5392009-09-21 18:03:22 +00009080 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009081 // Converting this to a max would handle comparisons between positive
9082 // and negative zero incorrectly.
9083 if (!UnsafeFPMath &&
9084 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
9085 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009086 Opcode = X86ISD::FMAX;
9087 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00009088 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009089 // Converting this to a max would handle NaNs incorrectly, and swapping
9090 // the operands would cause it to handle comparisons between positive
9091 // and negative zero incorrectly.
9092 if (!FiniteOnlyFPMath() &&
9093 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
9094 if (!UnsafeFPMath &&
9095 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
9096 break;
9097 std::swap(LHS, RHS);
9098 }
Dan Gohman670e5392009-09-21 18:03:22 +00009099 Opcode = X86ISD::FMAX;
9100 break;
9101 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009102 // Converting this to a max would handle both negative zeros and NaNs
9103 // incorrectly, but we can swap the operands to fix both.
9104 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009105 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009106 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009107 case ISD::SETGE:
9108 Opcode = X86ISD::FMAX;
9109 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00009110 }
Dan Gohman670e5392009-09-21 18:03:22 +00009111 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00009112 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
9113 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00009114 switch (CC) {
9115 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00009116 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009117 // Converting this to a min would handle comparisons between positive
9118 // and negative zero incorrectly, and swapping the operands would
9119 // cause it to handle NaNs incorrectly.
9120 if (!UnsafeFPMath &&
9121 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
9122 if (!FiniteOnlyFPMath() &&
9123 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9124 break;
9125 std::swap(LHS, RHS);
9126 }
Dan Gohman670e5392009-09-21 18:03:22 +00009127 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009128 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009129 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009130 // Converting this to a min would handle NaNs incorrectly.
9131 if (!UnsafeFPMath &&
9132 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9133 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009134 Opcode = X86ISD::FMIN;
9135 break;
9136 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009137 // Converting this to a min would handle both negative zeros and NaNs
9138 // incorrectly, but we can swap the operands to fix both.
9139 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009140 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009141 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009142 case ISD::SETGE:
9143 Opcode = X86ISD::FMIN;
9144 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009145
Dan Gohman670e5392009-09-21 18:03:22 +00009146 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009147 // Converting this to a max would handle NaNs incorrectly.
9148 if (!FiniteOnlyFPMath() &&
9149 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9150 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009151 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009152 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009153 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009154 // Converting this to a max would handle comparisons between positive
9155 // and negative zero incorrectly, and swapping the operands would
9156 // cause it to handle NaNs incorrectly.
9157 if (!UnsafeFPMath &&
9158 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9159 if (!FiniteOnlyFPMath() &&
9160 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9161 break;
9162 std::swap(LHS, RHS);
9163 }
Dan Gohman670e5392009-09-21 18:03:22 +00009164 Opcode = X86ISD::FMAX;
9165 break;
9166 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009167 // Converting this to a max would handle both negative zeros and NaNs
9168 // incorrectly, but we can swap the operands to fix both.
9169 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009170 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009171 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009172 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009173 Opcode = X86ISD::FMAX;
9174 break;
9175 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009176 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009177
Chris Lattner47b4ce82009-03-11 05:48:52 +00009178 if (Opcode)
9179 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009180 }
Eric Christopherfd179292009-08-27 18:07:15 +00009181
Chris Lattnerd1980a52009-03-12 06:52:53 +00009182 // If this is a select between two integer constants, try to do some
9183 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009184 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9185 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009186 // Don't do this for crazy integer types.
9187 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9188 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009189 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009190 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009191
Chris Lattnercee56e72009-03-13 05:53:31 +00009192 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009193 // Efficiently invertible.
9194 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9195 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9196 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9197 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009198 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009199 }
Eric Christopherfd179292009-08-27 18:07:15 +00009200
Chris Lattnerd1980a52009-03-12 06:52:53 +00009201 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009202 if (FalseC->getAPIntValue() == 0 &&
9203 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009204 if (NeedsCondInvert) // Invert the condition if needed.
9205 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9206 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009207
Chris Lattnerd1980a52009-03-12 06:52:53 +00009208 // Zero extend the condition if needed.
9209 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009210
Chris Lattnercee56e72009-03-13 05:53:31 +00009211 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009212 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009213 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009214 }
Eric Christopherfd179292009-08-27 18:07:15 +00009215
Chris Lattner97a29a52009-03-13 05:22:11 +00009216 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009217 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009218 if (NeedsCondInvert) // Invert the condition if needed.
9219 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9220 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009221
Chris Lattner97a29a52009-03-13 05:22:11 +00009222 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009223 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9224 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009225 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009226 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009227 }
Eric Christopherfd179292009-08-27 18:07:15 +00009228
Chris Lattnercee56e72009-03-13 05:53:31 +00009229 // Optimize cases that will turn into an LEA instruction. This requires
9230 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009231 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009232 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009233 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009234
Chris Lattnercee56e72009-03-13 05:53:31 +00009235 bool isFastMultiplier = false;
9236 if (Diff < 10) {
9237 switch ((unsigned char)Diff) {
9238 default: break;
9239 case 1: // result = add base, cond
9240 case 2: // result = lea base( , cond*2)
9241 case 3: // result = lea base(cond, cond*2)
9242 case 4: // result = lea base( , cond*4)
9243 case 5: // result = lea base(cond, cond*4)
9244 case 8: // result = lea base( , cond*8)
9245 case 9: // result = lea base(cond, cond*8)
9246 isFastMultiplier = true;
9247 break;
9248 }
9249 }
Eric Christopherfd179292009-08-27 18:07:15 +00009250
Chris Lattnercee56e72009-03-13 05:53:31 +00009251 if (isFastMultiplier) {
9252 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9253 if (NeedsCondInvert) // Invert the condition if needed.
9254 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9255 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009256
Chris Lattnercee56e72009-03-13 05:53:31 +00009257 // Zero extend the condition if needed.
9258 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9259 Cond);
9260 // Scale the condition by the difference.
9261 if (Diff != 1)
9262 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9263 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009264
Chris Lattnercee56e72009-03-13 05:53:31 +00009265 // Add the base if non-zero.
9266 if (FalseC->getAPIntValue() != 0)
9267 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9268 SDValue(FalseC, 0));
9269 return Cond;
9270 }
Eric Christopherfd179292009-08-27 18:07:15 +00009271 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009272 }
9273 }
Eric Christopherfd179292009-08-27 18:07:15 +00009274
Dan Gohman475871a2008-07-27 21:46:04 +00009275 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009276}
9277
Chris Lattnerd1980a52009-03-12 06:52:53 +00009278/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9279static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9280 TargetLowering::DAGCombinerInfo &DCI) {
9281 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009282
Chris Lattnerd1980a52009-03-12 06:52:53 +00009283 // If the flag operand isn't dead, don't touch this CMOV.
9284 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9285 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009286
Chris Lattnerd1980a52009-03-12 06:52:53 +00009287 // If this is a select between two integer constants, try to do some
9288 // optimizations. Note that the operands are ordered the opposite of SELECT
9289 // operands.
9290 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9291 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9292 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9293 // larger than FalseC (the false value).
9294 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009295
Chris Lattnerd1980a52009-03-12 06:52:53 +00009296 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9297 CC = X86::GetOppositeBranchCondition(CC);
9298 std::swap(TrueC, FalseC);
9299 }
Eric Christopherfd179292009-08-27 18:07:15 +00009300
Chris Lattnerd1980a52009-03-12 06:52:53 +00009301 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009302 // This is efficient for any integer data type (including i8/i16) and
9303 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009304 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9305 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009306 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9307 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009308
Chris Lattnerd1980a52009-03-12 06:52:53 +00009309 // Zero extend the condition if needed.
9310 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009311
Chris Lattnerd1980a52009-03-12 06:52:53 +00009312 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9313 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009314 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009315 if (N->getNumValues() == 2) // Dead flag value?
9316 return DCI.CombineTo(N, Cond, SDValue());
9317 return Cond;
9318 }
Eric Christopherfd179292009-08-27 18:07:15 +00009319
Chris Lattnercee56e72009-03-13 05:53:31 +00009320 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9321 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009322 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9323 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009324 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9325 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009326
Chris Lattner97a29a52009-03-13 05:22:11 +00009327 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009328 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9329 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009330 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9331 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009332
Chris Lattner97a29a52009-03-13 05:22:11 +00009333 if (N->getNumValues() == 2) // Dead flag value?
9334 return DCI.CombineTo(N, Cond, SDValue());
9335 return Cond;
9336 }
Eric Christopherfd179292009-08-27 18:07:15 +00009337
Chris Lattnercee56e72009-03-13 05:53:31 +00009338 // Optimize cases that will turn into an LEA instruction. This requires
9339 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009340 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009341 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009342 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009343
Chris Lattnercee56e72009-03-13 05:53:31 +00009344 bool isFastMultiplier = false;
9345 if (Diff < 10) {
9346 switch ((unsigned char)Diff) {
9347 default: break;
9348 case 1: // result = add base, cond
9349 case 2: // result = lea base( , cond*2)
9350 case 3: // result = lea base(cond, cond*2)
9351 case 4: // result = lea base( , cond*4)
9352 case 5: // result = lea base(cond, cond*4)
9353 case 8: // result = lea base( , cond*8)
9354 case 9: // result = lea base(cond, cond*8)
9355 isFastMultiplier = true;
9356 break;
9357 }
9358 }
Eric Christopherfd179292009-08-27 18:07:15 +00009359
Chris Lattnercee56e72009-03-13 05:53:31 +00009360 if (isFastMultiplier) {
9361 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9362 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009363 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9364 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009365 // Zero extend the condition if needed.
9366 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9367 Cond);
9368 // Scale the condition by the difference.
9369 if (Diff != 1)
9370 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9371 DAG.getConstant(Diff, Cond.getValueType()));
9372
9373 // Add the base if non-zero.
9374 if (FalseC->getAPIntValue() != 0)
9375 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9376 SDValue(FalseC, 0));
9377 if (N->getNumValues() == 2) // Dead flag value?
9378 return DCI.CombineTo(N, Cond, SDValue());
9379 return Cond;
9380 }
Eric Christopherfd179292009-08-27 18:07:15 +00009381 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009382 }
9383 }
9384 return SDValue();
9385}
9386
9387
Evan Cheng0b0cd912009-03-28 05:57:29 +00009388/// PerformMulCombine - Optimize a single multiply with constant into two
9389/// in order to implement it with two cheaper instructions, e.g.
9390/// LEA + SHL, LEA + LEA.
9391static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9392 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009393 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9394 return SDValue();
9395
Owen Andersone50ed302009-08-10 22:56:29 +00009396 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009397 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009398 return SDValue();
9399
9400 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9401 if (!C)
9402 return SDValue();
9403 uint64_t MulAmt = C->getZExtValue();
9404 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9405 return SDValue();
9406
9407 uint64_t MulAmt1 = 0;
9408 uint64_t MulAmt2 = 0;
9409 if ((MulAmt % 9) == 0) {
9410 MulAmt1 = 9;
9411 MulAmt2 = MulAmt / 9;
9412 } else if ((MulAmt % 5) == 0) {
9413 MulAmt1 = 5;
9414 MulAmt2 = MulAmt / 5;
9415 } else if ((MulAmt % 3) == 0) {
9416 MulAmt1 = 3;
9417 MulAmt2 = MulAmt / 3;
9418 }
9419 if (MulAmt2 &&
9420 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9421 DebugLoc DL = N->getDebugLoc();
9422
9423 if (isPowerOf2_64(MulAmt2) &&
9424 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9425 // If second multiplifer is pow2, issue it first. We want the multiply by
9426 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9427 // is an add.
9428 std::swap(MulAmt1, MulAmt2);
9429
9430 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009431 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009432 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009433 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009434 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009435 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009436 DAG.getConstant(MulAmt1, VT));
9437
Eric Christopherfd179292009-08-27 18:07:15 +00009438 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009439 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009440 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009441 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009442 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009443 DAG.getConstant(MulAmt2, VT));
9444
9445 // Do not add new nodes to DAG combiner worklist.
9446 DCI.CombineTo(N, NewMul, false);
9447 }
9448 return SDValue();
9449}
9450
Evan Chengad9c0a32009-12-15 00:53:42 +00009451static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9452 SDValue N0 = N->getOperand(0);
9453 SDValue N1 = N->getOperand(1);
9454 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9455 EVT VT = N0.getValueType();
9456
9457 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9458 // since the result of setcc_c is all zero's or all ones.
9459 if (N1C && N0.getOpcode() == ISD::AND &&
9460 N0.getOperand(1).getOpcode() == ISD::Constant) {
9461 SDValue N00 = N0.getOperand(0);
9462 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9463 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9464 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9465 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9466 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9467 APInt ShAmt = N1C->getAPIntValue();
9468 Mask = Mask.shl(ShAmt);
9469 if (Mask != 0)
9470 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9471 N00, DAG.getConstant(Mask, VT));
9472 }
9473 }
9474
9475 return SDValue();
9476}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009477
Nate Begeman740ab032009-01-26 00:52:55 +00009478/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9479/// when possible.
9480static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9481 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009482 EVT VT = N->getValueType(0);
9483 if (!VT.isVector() && VT.isInteger() &&
9484 N->getOpcode() == ISD::SHL)
9485 return PerformSHLCombine(N, DAG);
9486
Nate Begeman740ab032009-01-26 00:52:55 +00009487 // On X86 with SSE2 support, we can transform this to a vector shift if
9488 // all elements are shifted by the same amount. We can't do this in legalize
9489 // because the a constant vector is typically transformed to a constant pool
9490 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009491 if (!Subtarget->hasSSE2())
9492 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009493
Owen Anderson825b72b2009-08-11 20:47:22 +00009494 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009495 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009496
Mon P Wang3becd092009-01-28 08:12:05 +00009497 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009498 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009499 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009500 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009501 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9502 unsigned NumElts = VT.getVectorNumElements();
9503 unsigned i = 0;
9504 for (; i != NumElts; ++i) {
9505 SDValue Arg = ShAmtOp.getOperand(i);
9506 if (Arg.getOpcode() == ISD::UNDEF) continue;
9507 BaseShAmt = Arg;
9508 break;
9509 }
9510 for (; i != NumElts; ++i) {
9511 SDValue Arg = ShAmtOp.getOperand(i);
9512 if (Arg.getOpcode() == ISD::UNDEF) continue;
9513 if (Arg != BaseShAmt) {
9514 return SDValue();
9515 }
9516 }
9517 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009518 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009519 SDValue InVec = ShAmtOp.getOperand(0);
9520 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9521 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9522 unsigned i = 0;
9523 for (; i != NumElts; ++i) {
9524 SDValue Arg = InVec.getOperand(i);
9525 if (Arg.getOpcode() == ISD::UNDEF) continue;
9526 BaseShAmt = Arg;
9527 break;
9528 }
9529 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9530 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009531 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009532 if (C->getZExtValue() == SplatIdx)
9533 BaseShAmt = InVec.getOperand(1);
9534 }
9535 }
9536 if (BaseShAmt.getNode() == 0)
9537 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9538 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009539 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009540 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009541
Mon P Wangefa42202009-09-03 19:56:25 +00009542 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009543 if (EltVT.bitsGT(MVT::i32))
9544 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9545 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009546 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009547
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009548 // The shift amount is identical so we can do a vector shift.
9549 SDValue ValOp = N->getOperand(0);
9550 switch (N->getOpcode()) {
9551 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009552 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009553 break;
9554 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009555 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009556 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009557 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009558 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009559 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009560 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009561 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009562 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009563 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009564 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009565 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009566 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009567 break;
9568 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009569 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009570 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009571 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009572 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009573 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009574 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009575 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009576 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009577 break;
9578 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009579 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009580 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009581 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009582 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009583 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009584 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009585 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009586 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009587 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009588 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009589 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009590 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009591 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009592 }
9593 return SDValue();
9594}
9595
Evan Cheng760d1942010-01-04 21:22:48 +00009596static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
9597 const X86Subtarget *Subtarget) {
9598 EVT VT = N->getValueType(0);
9599 if (VT != MVT::i64 || !Subtarget->is64Bit())
9600 return SDValue();
9601
9602 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9603 SDValue N0 = N->getOperand(0);
9604 SDValue N1 = N->getOperand(1);
9605 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9606 std::swap(N0, N1);
9607 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9608 return SDValue();
9609
9610 SDValue ShAmt0 = N0.getOperand(1);
9611 if (ShAmt0.getValueType() != MVT::i8)
9612 return SDValue();
9613 SDValue ShAmt1 = N1.getOperand(1);
9614 if (ShAmt1.getValueType() != MVT::i8)
9615 return SDValue();
9616 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9617 ShAmt0 = ShAmt0.getOperand(0);
9618 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9619 ShAmt1 = ShAmt1.getOperand(0);
9620
9621 DebugLoc DL = N->getDebugLoc();
9622 unsigned Opc = X86ISD::SHLD;
9623 SDValue Op0 = N0.getOperand(0);
9624 SDValue Op1 = N1.getOperand(0);
9625 if (ShAmt0.getOpcode() == ISD::SUB) {
9626 Opc = X86ISD::SHRD;
9627 std::swap(Op0, Op1);
9628 std::swap(ShAmt0, ShAmt1);
9629 }
9630
9631 if (ShAmt1.getOpcode() == ISD::SUB) {
9632 SDValue Sum = ShAmt1.getOperand(0);
9633 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
9634 if (SumC->getSExtValue() == 64 &&
9635 ShAmt1.getOperand(1) == ShAmt0)
9636 return DAG.getNode(Opc, DL, VT,
9637 Op0, Op1,
9638 DAG.getNode(ISD::TRUNCATE, DL,
9639 MVT::i8, ShAmt0));
9640 }
9641 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9642 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9643 if (ShAmt0C &&
9644 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == 64)
9645 return DAG.getNode(Opc, DL, VT,
9646 N0.getOperand(0), N1.getOperand(0),
9647 DAG.getNode(ISD::TRUNCATE, DL,
9648 MVT::i8, ShAmt0));
9649 }
9650
9651 return SDValue();
9652}
9653
Chris Lattner149a4e52008-02-22 02:09:43 +00009654/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009655static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009656 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009657 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9658 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009659 // A preferable solution to the general problem is to figure out the right
9660 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009661
9662 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009663 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009664 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009665 if (VT.getSizeInBits() != 64)
9666 return SDValue();
9667
Devang Patel578efa92009-06-05 21:57:13 +00009668 const Function *F = DAG.getMachineFunction().getFunction();
9669 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009670 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009671 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009672 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009673 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009674 isa<LoadSDNode>(St->getValue()) &&
9675 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9676 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009677 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009678 LoadSDNode *Ld = 0;
9679 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009680 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009681 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009682 // Must be a store of a load. We currently handle two cases: the load
9683 // is a direct child, and it's under an intervening TokenFactor. It is
9684 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009685 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009686 Ld = cast<LoadSDNode>(St->getChain());
9687 else if (St->getValue().hasOneUse() &&
9688 ChainVal->getOpcode() == ISD::TokenFactor) {
9689 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009690 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009691 TokenFactorIndex = i;
9692 Ld = cast<LoadSDNode>(St->getValue());
9693 } else
9694 Ops.push_back(ChainVal->getOperand(i));
9695 }
9696 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009697
Evan Cheng536e6672009-03-12 05:59:15 +00009698 if (!Ld || !ISD::isNormalLoad(Ld))
9699 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009700
Evan Cheng536e6672009-03-12 05:59:15 +00009701 // If this is not the MMX case, i.e. we are just turning i64 load/store
9702 // into f64 load/store, avoid the transformation if there are multiple
9703 // uses of the loaded value.
9704 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9705 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009706
Evan Cheng536e6672009-03-12 05:59:15 +00009707 DebugLoc LdDL = Ld->getDebugLoc();
9708 DebugLoc StDL = N->getDebugLoc();
9709 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9710 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9711 // pair instead.
9712 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009713 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009714 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9715 Ld->getBasePtr(), Ld->getSrcValue(),
9716 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009717 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009718 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009719 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009720 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009721 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009722 Ops.size());
9723 }
Evan Cheng536e6672009-03-12 05:59:15 +00009724 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009725 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009726 St->isVolatile(), St->isNonTemporal(),
9727 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009728 }
Evan Cheng536e6672009-03-12 05:59:15 +00009729
9730 // Otherwise, lower to two pairs of 32-bit loads / stores.
9731 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009732 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9733 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009734
Owen Anderson825b72b2009-08-11 20:47:22 +00009735 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009736 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009737 Ld->isVolatile(), Ld->isNonTemporal(),
9738 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009739 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009740 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009741 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009742 MinAlign(Ld->getAlignment(), 4));
9743
9744 SDValue NewChain = LoLd.getValue(1);
9745 if (TokenFactorIndex != -1) {
9746 Ops.push_back(LoLd);
9747 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009748 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009749 Ops.size());
9750 }
9751
9752 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009753 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9754 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009755
9756 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9757 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009758 St->isVolatile(), St->isNonTemporal(),
9759 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009760 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9761 St->getSrcValue(),
9762 St->getSrcValueOffset() + 4,
9763 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009764 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009765 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009766 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009767 }
Dan Gohman475871a2008-07-27 21:46:04 +00009768 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009769}
9770
Chris Lattner6cf73262008-01-25 06:14:17 +00009771/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9772/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009773static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009774 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9775 // F[X]OR(0.0, x) -> x
9776 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009777 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9778 if (C->getValueAPF().isPosZero())
9779 return N->getOperand(1);
9780 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9781 if (C->getValueAPF().isPosZero())
9782 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009783 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009784}
9785
9786/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009787static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009788 // FAND(0.0, x) -> 0.0
9789 // FAND(x, 0.0) -> 0.0
9790 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9791 if (C->getValueAPF().isPosZero())
9792 return N->getOperand(0);
9793 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9794 if (C->getValueAPF().isPosZero())
9795 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009796 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009797}
9798
Dan Gohmane5af2d32009-01-29 01:59:02 +00009799static SDValue PerformBTCombine(SDNode *N,
9800 SelectionDAG &DAG,
9801 TargetLowering::DAGCombinerInfo &DCI) {
9802 // BT ignores high bits in the bit index operand.
9803 SDValue Op1 = N->getOperand(1);
9804 if (Op1.hasOneUse()) {
9805 unsigned BitWidth = Op1.getValueSizeInBits();
9806 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9807 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009808 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
9809 !DCI.isBeforeLegalizeOps());
Dan Gohmane5af2d32009-01-29 01:59:02 +00009810 TargetLowering &TLI = DAG.getTargetLoweringInfo();
9811 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9812 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9813 DCI.CommitTargetLoweringOpt(TLO);
9814 }
9815 return SDValue();
9816}
Chris Lattner83e6c992006-10-04 06:57:07 +00009817
Eli Friedman7a5e5552009-06-07 06:52:44 +00009818static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9819 SDValue Op = N->getOperand(0);
9820 if (Op.getOpcode() == ISD::BIT_CONVERT)
9821 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009822 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009823 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009824 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009825 OpVT.getVectorElementType().getSizeInBits()) {
9826 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9827 }
9828 return SDValue();
9829}
9830
Owen Anderson99177002009-06-29 18:04:45 +00009831// On X86 and X86-64, atomic operations are lowered to locked instructions.
9832// Locked instructions, in turn, have implicit fence semantics (all memory
9833// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009834// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009835// fence-atomic-fence.
9836static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9837 SDValue atomic = N->getOperand(0);
9838 switch (atomic.getOpcode()) {
9839 case ISD::ATOMIC_CMP_SWAP:
9840 case ISD::ATOMIC_SWAP:
9841 case ISD::ATOMIC_LOAD_ADD:
9842 case ISD::ATOMIC_LOAD_SUB:
9843 case ISD::ATOMIC_LOAD_AND:
9844 case ISD::ATOMIC_LOAD_OR:
9845 case ISD::ATOMIC_LOAD_XOR:
9846 case ISD::ATOMIC_LOAD_NAND:
9847 case ISD::ATOMIC_LOAD_MIN:
9848 case ISD::ATOMIC_LOAD_MAX:
9849 case ISD::ATOMIC_LOAD_UMIN:
9850 case ISD::ATOMIC_LOAD_UMAX:
9851 break;
9852 default:
9853 return SDValue();
9854 }
Eric Christopherfd179292009-08-27 18:07:15 +00009855
Owen Anderson99177002009-06-29 18:04:45 +00009856 SDValue fence = atomic.getOperand(0);
9857 if (fence.getOpcode() != ISD::MEMBARRIER)
9858 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009859
Owen Anderson99177002009-06-29 18:04:45 +00009860 switch (atomic.getOpcode()) {
9861 case ISD::ATOMIC_CMP_SWAP:
9862 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9863 atomic.getOperand(1), atomic.getOperand(2),
9864 atomic.getOperand(3));
9865 case ISD::ATOMIC_SWAP:
9866 case ISD::ATOMIC_LOAD_ADD:
9867 case ISD::ATOMIC_LOAD_SUB:
9868 case ISD::ATOMIC_LOAD_AND:
9869 case ISD::ATOMIC_LOAD_OR:
9870 case ISD::ATOMIC_LOAD_XOR:
9871 case ISD::ATOMIC_LOAD_NAND:
9872 case ISD::ATOMIC_LOAD_MIN:
9873 case ISD::ATOMIC_LOAD_MAX:
9874 case ISD::ATOMIC_LOAD_UMIN:
9875 case ISD::ATOMIC_LOAD_UMAX:
9876 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9877 atomic.getOperand(1), atomic.getOperand(2));
9878 default:
9879 return SDValue();
9880 }
9881}
9882
Evan Cheng2e489c42009-12-16 00:53:11 +00009883static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9884 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9885 // (and (i32 x86isd::setcc_carry), 1)
9886 // This eliminates the zext. This transformation is necessary because
9887 // ISD::SETCC is always legalized to i8.
9888 DebugLoc dl = N->getDebugLoc();
9889 SDValue N0 = N->getOperand(0);
9890 EVT VT = N->getValueType(0);
9891 if (N0.getOpcode() == ISD::AND &&
9892 N0.hasOneUse() &&
9893 N0.getOperand(0).hasOneUse()) {
9894 SDValue N00 = N0.getOperand(0);
9895 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9896 return SDValue();
9897 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9898 if (!C || C->getZExtValue() != 1)
9899 return SDValue();
9900 return DAG.getNode(ISD::AND, dl, VT,
9901 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9902 N00.getOperand(0), N00.getOperand(1)),
9903 DAG.getConstant(1, VT));
9904 }
9905
9906 return SDValue();
9907}
9908
Dan Gohman475871a2008-07-27 21:46:04 +00009909SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009910 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009911 SelectionDAG &DAG = DCI.DAG;
9912 switch (N->getOpcode()) {
9913 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009914 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009915 case ISD::EXTRACT_VECTOR_ELT:
9916 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009917 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009918 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009919 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009920 case ISD::SHL:
9921 case ISD::SRA:
9922 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng760d1942010-01-04 21:22:48 +00009923 case ISD::OR: return PerformOrCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009924 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009925 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009926 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9927 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009928 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009929 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009930 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009931 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009932 }
9933
Dan Gohman475871a2008-07-27 21:46:04 +00009934 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009935}
9936
Evan Chenge5b51ac2010-04-17 06:13:15 +00009937/// isTypeDesirableForOp - Return true if the target has native support for
9938/// the specified value type and it is 'desirable' to use the type for the
9939/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
9940/// instruction encodings are longer and some i16 instructions are slow.
9941bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
9942 if (!isTypeLegal(VT))
9943 return false;
9944 if (!Promote16Bit || VT != MVT::i16)
9945 return true;
9946
9947 switch (Opc) {
9948 default:
9949 return true;
9950 case ISD::SHL:
9951 case ISD::SRA:
9952 case ISD::SRL:
9953 case ISD::SUB:
9954 case ISD::ADD:
9955 case ISD::MUL:
9956 case ISD::AND:
9957 case ISD::OR:
9958 case ISD::XOR:
9959 return false;
9960 }
9961}
9962
9963/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +00009964/// beneficial for dag combiner to promote the specified node. If true, it
9965/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009966bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009967 if (!Promote16Bit)
9968 return false;
9969
9970 EVT VT = Op.getValueType();
9971 if (VT != MVT::i16)
9972 return false;
9973
9974 bool Commute = true;
9975 switch (Op.getOpcode()) {
9976 default: return false;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009977 case ISD::SHL:
9978 case ISD::SRA:
9979 case ISD::SRL: {
9980 SDValue N0 = Op.getOperand(0);
9981 // Look out for (store (shl (load), x)).
9982 if (isa<LoadSDNode>(N0) && N0.hasOneUse() &&
9983 Op.hasOneUse() && Op.getNode()->use_begin()->getOpcode() == ISD::STORE)
9984 return false;
9985 break;
9986 }
Evan Cheng64b7bf72010-04-16 06:14:10 +00009987 case ISD::SUB:
9988 Commute = false;
9989 // fallthrough
9990 case ISD::ADD:
9991 case ISD::MUL:
9992 case ISD::AND:
9993 case ISD::OR:
9994 case ISD::XOR: {
9995 SDValue N0 = Op.getOperand(0);
9996 SDValue N1 = Op.getOperand(1);
9997 if (!Commute && isa<LoadSDNode>(N1))
9998 return false;
9999 // Avoid disabling potential load folding opportunities.
10000 if ((isa<LoadSDNode>(N0) && N0.hasOneUse()) && !isa<ConstantSDNode>(N1))
10001 return false;
10002 if ((isa<LoadSDNode>(N1) && N1.hasOneUse()) && !isa<ConstantSDNode>(N0))
10003 return false;
10004 }
10005 }
10006
10007 PVT = MVT::i32;
10008 return true;
10009}
10010
Evan Cheng60c07e12006-07-05 22:17:51 +000010011//===----------------------------------------------------------------------===//
10012// X86 Inline Assembly Support
10013//===----------------------------------------------------------------------===//
10014
Chris Lattnerb8105652009-07-20 17:51:36 +000010015static bool LowerToBSwap(CallInst *CI) {
10016 // FIXME: this should verify that we are targetting a 486 or better. If not,
10017 // we will turn this bswap into something that will be lowered to logical ops
10018 // instead of emitting the bswap asm. For now, we don't support 486 or lower
10019 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +000010020
Chris Lattnerb8105652009-07-20 17:51:36 +000010021 // Verify this is a simple bswap.
10022 if (CI->getNumOperands() != 2 ||
Eric Christopher551754c2010-04-16 23:37:20 +000010023 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010024 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +000010025 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010026
Chris Lattnerb8105652009-07-20 17:51:36 +000010027 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
10028 if (!Ty || Ty->getBitWidth() % 16 != 0)
10029 return false;
Eric Christopherfd179292009-08-27 18:07:15 +000010030
Chris Lattnerb8105652009-07-20 17:51:36 +000010031 // Okay, we can do this xform, do so now.
10032 const Type *Tys[] = { Ty };
10033 Module *M = CI->getParent()->getParent()->getParent();
10034 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +000010035
Eric Christopher551754c2010-04-16 23:37:20 +000010036 Value *Op = CI->getOperand(1);
Chris Lattnerb8105652009-07-20 17:51:36 +000010037 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +000010038
Chris Lattnerb8105652009-07-20 17:51:36 +000010039 CI->replaceAllUsesWith(Op);
10040 CI->eraseFromParent();
10041 return true;
10042}
10043
10044bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
10045 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
10046 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
10047
10048 std::string AsmStr = IA->getAsmString();
10049
10050 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010051 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +000010052 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
10053
10054 switch (AsmPieces.size()) {
10055 default: return false;
10056 case 1:
10057 AsmStr = AsmPieces[0];
10058 AsmPieces.clear();
10059 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
10060
10061 // bswap $0
10062 if (AsmPieces.size() == 2 &&
10063 (AsmPieces[0] == "bswap" ||
10064 AsmPieces[0] == "bswapq" ||
10065 AsmPieces[0] == "bswapl") &&
10066 (AsmPieces[1] == "$0" ||
10067 AsmPieces[1] == "${0:q}")) {
10068 // No need to check constraints, nothing other than the equivalent of
10069 // "=r,0" would be valid here.
10070 return LowerToBSwap(CI);
10071 }
10072 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010073 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010074 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010075 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010076 AsmPieces[1] == "$$8," &&
10077 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000010078 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
10079 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +000010080 const std::string &Constraints = IA->getConstraintString();
10081 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000010082 std::sort(AsmPieces.begin(), AsmPieces.end());
10083 if (AsmPieces.size() == 4 &&
10084 AsmPieces[0] == "~{cc}" &&
10085 AsmPieces[1] == "~{dirflag}" &&
10086 AsmPieces[2] == "~{flags}" &&
10087 AsmPieces[3] == "~{fpsr}") {
10088 return LowerToBSwap(CI);
10089 }
Chris Lattnerb8105652009-07-20 17:51:36 +000010090 }
10091 break;
10092 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010093 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000010094 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010095 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
10096 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
10097 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010098 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000010099 SplitString(AsmPieces[0], Words, " \t");
10100 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
10101 Words.clear();
10102 SplitString(AsmPieces[1], Words, " \t");
10103 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
10104 Words.clear();
10105 SplitString(AsmPieces[2], Words, " \t,");
10106 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
10107 Words[2] == "%edx") {
10108 return LowerToBSwap(CI);
10109 }
10110 }
10111 }
10112 }
10113 break;
10114 }
10115 return false;
10116}
10117
10118
10119
Chris Lattnerf4dff842006-07-11 02:54:03 +000010120/// getConstraintType - Given a constraint letter, return the type of
10121/// constraint it is for this target.
10122X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010123X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10124 if (Constraint.size() == 1) {
10125 switch (Constraint[0]) {
10126 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010127 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010128 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010129 case 'r':
10130 case 'R':
10131 case 'l':
10132 case 'q':
10133 case 'Q':
10134 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010135 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010136 case 'Y':
10137 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010138 case 'e':
10139 case 'Z':
10140 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010141 default:
10142 break;
10143 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010144 }
Chris Lattner4234f572007-03-25 02:14:49 +000010145 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010146}
10147
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010148/// LowerXConstraint - try to replace an X constraint, which matches anything,
10149/// with another that has more specific requirements based on the type of the
10150/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010151const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010152LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010153 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10154 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010155 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010156 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010157 return "Y";
10158 if (Subtarget->hasSSE1())
10159 return "x";
10160 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010161
Chris Lattner5e764232008-04-26 23:02:14 +000010162 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010163}
10164
Chris Lattner48884cd2007-08-25 00:47:38 +000010165/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10166/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010167void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010168 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +000010169 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +000010170 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010171 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010172 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010173
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010174 switch (Constraint) {
10175 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010176 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010177 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010178 if (C->getZExtValue() <= 31) {
10179 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010180 break;
10181 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010182 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010183 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010184 case 'J':
10185 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010186 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010187 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10188 break;
10189 }
10190 }
10191 return;
10192 case 'K':
10193 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010194 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010195 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10196 break;
10197 }
10198 }
10199 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010200 case 'N':
10201 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010202 if (C->getZExtValue() <= 255) {
10203 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010204 break;
10205 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010206 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010207 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010208 case 'e': {
10209 // 32-bit signed value
10210 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10211 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010212 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10213 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010214 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010215 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010216 break;
10217 }
10218 // FIXME gcc accepts some relocatable values here too, but only in certain
10219 // memory models; it's complicated.
10220 }
10221 return;
10222 }
10223 case 'Z': {
10224 // 32-bit unsigned value
10225 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10226 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010227 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10228 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010229 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10230 break;
10231 }
10232 }
10233 // FIXME gcc accepts some relocatable values here too, but only in certain
10234 // memory models; it's complicated.
10235 return;
10236 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010237 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010238 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010239 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010240 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010241 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010242 break;
10243 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010244
Chris Lattnerdc43a882007-05-03 16:52:29 +000010245 // If we are in non-pic codegen mode, we allow the address of a global (with
10246 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010247 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010248 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010249
Chris Lattner49921962009-05-08 18:23:14 +000010250 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10251 while (1) {
10252 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10253 Offset += GA->getOffset();
10254 break;
10255 } else if (Op.getOpcode() == ISD::ADD) {
10256 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10257 Offset += C->getZExtValue();
10258 Op = Op.getOperand(0);
10259 continue;
10260 }
10261 } else if (Op.getOpcode() == ISD::SUB) {
10262 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10263 Offset += -C->getZExtValue();
10264 Op = Op.getOperand(0);
10265 continue;
10266 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010267 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010268
Chris Lattner49921962009-05-08 18:23:14 +000010269 // Otherwise, this isn't something we can handle, reject it.
10270 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010271 }
Eric Christopherfd179292009-08-27 18:07:15 +000010272
Dan Gohman46510a72010-04-15 01:51:59 +000010273 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010274 // If we require an extra load to get this address, as in PIC mode, we
10275 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010276 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10277 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010278 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010279
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010280 if (hasMemory)
10281 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10282 else
10283 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010284 Result = Op;
10285 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010286 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010287 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010288
Gabor Greifba36cb52008-08-28 21:40:38 +000010289 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010290 Ops.push_back(Result);
10291 return;
10292 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010293 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10294 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010295}
10296
Chris Lattner259e97c2006-01-31 19:43:35 +000010297std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010298getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010299 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010300 if (Constraint.size() == 1) {
10301 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010302 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010303 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010304 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10305 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010306 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010307 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10308 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10309 X86::R10D,X86::R11D,X86::R12D,
10310 X86::R13D,X86::R14D,X86::R15D,
10311 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010312 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010313 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10314 X86::SI, X86::DI, X86::R8W,X86::R9W,
10315 X86::R10W,X86::R11W,X86::R12W,
10316 X86::R13W,X86::R14W,X86::R15W,
10317 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010318 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010319 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10320 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10321 X86::R10B,X86::R11B,X86::R12B,
10322 X86::R13B,X86::R14B,X86::R15B,
10323 X86::BPL, X86::SPL, 0);
10324
Owen Anderson825b72b2009-08-11 20:47:22 +000010325 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010326 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10327 X86::RSI, X86::RDI, X86::R8, X86::R9,
10328 X86::R10, X86::R11, X86::R12,
10329 X86::R13, X86::R14, X86::R15,
10330 X86::RBP, X86::RSP, 0);
10331
10332 break;
10333 }
Eric Christopherfd179292009-08-27 18:07:15 +000010334 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010335 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010336 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010337 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010338 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010339 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010340 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010341 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010342 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010343 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10344 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010345 }
10346 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010347
Chris Lattner1efa40f2006-02-22 00:56:39 +000010348 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010349}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010350
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010351std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010352X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010353 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010354 // First, see if this is a constraint that directly corresponds to an LLVM
10355 // register class.
10356 if (Constraint.size() == 1) {
10357 // GCC Constraint Letters
10358 switch (Constraint[0]) {
10359 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010360 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010361 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010362 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010363 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010364 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010365 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010366 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010367 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010368 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010369 case 'R': // LEGACY_REGS
10370 if (VT == MVT::i8)
10371 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10372 if (VT == MVT::i16)
10373 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10374 if (VT == MVT::i32 || !Subtarget->is64Bit())
10375 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10376 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010377 case 'f': // FP Stack registers.
10378 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10379 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010380 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010381 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010382 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010383 return std::make_pair(0U, X86::RFP64RegisterClass);
10384 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010385 case 'y': // MMX_REGS if MMX allowed.
10386 if (!Subtarget->hasMMX()) break;
10387 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010388 case 'Y': // SSE_REGS if SSE2 allowed
10389 if (!Subtarget->hasSSE2()) break;
10390 // FALL THROUGH.
10391 case 'x': // SSE_REGS if SSE1 allowed
10392 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010393
Owen Anderson825b72b2009-08-11 20:47:22 +000010394 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010395 default: break;
10396 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010397 case MVT::f32:
10398 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010399 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010400 case MVT::f64:
10401 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010402 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010403 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010404 case MVT::v16i8:
10405 case MVT::v8i16:
10406 case MVT::v4i32:
10407 case MVT::v2i64:
10408 case MVT::v4f32:
10409 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010410 return std::make_pair(0U, X86::VR128RegisterClass);
10411 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010412 break;
10413 }
10414 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010415
Chris Lattnerf76d1802006-07-31 23:26:50 +000010416 // Use the default implementation in TargetLowering to convert the register
10417 // constraint into a member of a register class.
10418 std::pair<unsigned, const TargetRegisterClass*> Res;
10419 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010420
10421 // Not found as a standard register?
10422 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010423 // Map st(0) -> st(7) -> ST0
10424 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10425 tolower(Constraint[1]) == 's' &&
10426 tolower(Constraint[2]) == 't' &&
10427 Constraint[3] == '(' &&
10428 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10429 Constraint[5] == ')' &&
10430 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010431
Chris Lattner56d77c72009-09-13 22:41:48 +000010432 Res.first = X86::ST0+Constraint[4]-'0';
10433 Res.second = X86::RFP80RegisterClass;
10434 return Res;
10435 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010436
Chris Lattner56d77c72009-09-13 22:41:48 +000010437 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010438 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010439 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010440 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010441 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010442 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010443
10444 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010445 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010446 Res.first = X86::EFLAGS;
10447 Res.second = X86::CCRRegisterClass;
10448 return Res;
10449 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010450
Dale Johannesen330169f2008-11-13 21:52:36 +000010451 // 'A' means EAX + EDX.
10452 if (Constraint == "A") {
10453 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010454 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010455 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010456 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010457 return Res;
10458 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010459
Chris Lattnerf76d1802006-07-31 23:26:50 +000010460 // Otherwise, check to see if this is a register class of the wrong value
10461 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10462 // turn into {ax},{dx}.
10463 if (Res.second->hasType(VT))
10464 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010465
Chris Lattnerf76d1802006-07-31 23:26:50 +000010466 // All of the single-register GCC register classes map their values onto
10467 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10468 // really want an 8-bit or 32-bit register, map to the appropriate register
10469 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010470 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010471 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010472 unsigned DestReg = 0;
10473 switch (Res.first) {
10474 default: break;
10475 case X86::AX: DestReg = X86::AL; break;
10476 case X86::DX: DestReg = X86::DL; break;
10477 case X86::CX: DestReg = X86::CL; break;
10478 case X86::BX: DestReg = X86::BL; break;
10479 }
10480 if (DestReg) {
10481 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010482 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010483 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010484 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010485 unsigned DestReg = 0;
10486 switch (Res.first) {
10487 default: break;
10488 case X86::AX: DestReg = X86::EAX; break;
10489 case X86::DX: DestReg = X86::EDX; break;
10490 case X86::CX: DestReg = X86::ECX; break;
10491 case X86::BX: DestReg = X86::EBX; break;
10492 case X86::SI: DestReg = X86::ESI; break;
10493 case X86::DI: DestReg = X86::EDI; break;
10494 case X86::BP: DestReg = X86::EBP; break;
10495 case X86::SP: DestReg = X86::ESP; break;
10496 }
10497 if (DestReg) {
10498 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010499 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010500 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010501 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010502 unsigned DestReg = 0;
10503 switch (Res.first) {
10504 default: break;
10505 case X86::AX: DestReg = X86::RAX; break;
10506 case X86::DX: DestReg = X86::RDX; break;
10507 case X86::CX: DestReg = X86::RCX; break;
10508 case X86::BX: DestReg = X86::RBX; break;
10509 case X86::SI: DestReg = X86::RSI; break;
10510 case X86::DI: DestReg = X86::RDI; break;
10511 case X86::BP: DestReg = X86::RBP; break;
10512 case X86::SP: DestReg = X86::RSP; break;
10513 }
10514 if (DestReg) {
10515 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010516 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010517 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010518 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010519 } else if (Res.second == X86::FR32RegisterClass ||
10520 Res.second == X86::FR64RegisterClass ||
10521 Res.second == X86::VR128RegisterClass) {
10522 // Handle references to XMM physical registers that got mapped into the
10523 // wrong class. This can happen with constraints like {xmm0} where the
10524 // target independent register mapper will just pick the first match it can
10525 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010526 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010527 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010528 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010529 Res.second = X86::FR64RegisterClass;
10530 else if (X86::VR128RegisterClass->hasType(VT))
10531 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010532 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010533
Chris Lattnerf76d1802006-07-31 23:26:50 +000010534 return Res;
10535}