blob: 048b88dfadf025ef95901e1936298c8c217ab4fd [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"
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000019#include "X86ShuffleDecode.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000020#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000021#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000023#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000024#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000025#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000026#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000028#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000029#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000030#include "llvm/LLVMContext.h"
Evan Cheng55d42002011-01-08 01:24:27 +000031#include "llvm/CodeGen/IntrinsicLowering.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000033#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000035#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000036#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000039#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000041#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000042#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000043#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000044#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000045#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000046#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000048#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000050#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000051#include "llvm/Support/ErrorHandling.h"
52#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000053#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000055using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000056
Evan Chengb1712452010-01-27 06:25:16 +000057STATISTIC(NumTailCalls, "Number of tail calls");
58
Evan Cheng10e86422008-04-25 19:11:04 +000059// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000060static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000061 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000062
Chris Lattnerf0144122009-07-28 03:13:23 +000063static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
Evan Cheng2bffee22011-02-01 01:14:13 +000064 const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>();
65 bool is64Bit = Subtarget->is64Bit();
66
67 if (Subtarget->isTargetEnvMacho()) {
Chris Lattnere019ec12010-12-19 20:07:10 +000068 if (is64Bit)
69 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000070 return new TargetLoweringObjectFileMachO();
Michael J. Spencerec38de22010-10-10 22:04:20 +000071 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000072
Evan Cheng2bffee22011-02-01 01:14:13 +000073 if (Subtarget->isTargetELF()) {
Chris Lattnere019ec12010-12-19 20:07:10 +000074 if (is64Bit)
75 return new X8664_ELFTargetObjectFile(TM);
76 return new X8632_ELFTargetObjectFile(TM);
77 }
Evan Cheng2bffee22011-02-01 01:14:13 +000078 if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())
Chris Lattnere019ec12010-12-19 20:07:10 +000079 return new TargetLoweringObjectFileCOFF();
Eric Christopher62f35a22010-07-05 19:26:33 +000080 llvm_unreachable("unknown subtarget type");
Chris Lattnerf0144122009-07-28 03:13:23 +000081}
82
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000083X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000084 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000085 Subtarget = &TM.getSubtarget<X86Subtarget>();
Nate Begeman2ea8ee72010-12-10 00:26:57 +000086 X86ScalarSSEf64 = Subtarget->hasXMMInt();
87 X86ScalarSSEf32 = Subtarget->hasXMM();
Evan Cheng25ab6902006-09-08 06:48:29 +000088 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000089
Anton Korobeynikov2365f512007-07-14 14:06:15 +000090 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000091 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000092
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000093 // Set up the TargetLowering object.
Chris Lattnera34b3cf2010-12-19 20:03:11 +000094 static MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 };
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000095
96 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000097 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000098 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000099 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +0000100 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +0000101
Michael J. Spencer92bf38c2010-10-10 23:11:06 +0000102 if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) {
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000103 // Setup Windows compiler runtime calls.
104 setLibcallName(RTLIB::SDIV_I64, "_alldiv");
Michael J. Spencer335b8062010-10-11 05:29:15 +0000105 setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
106 setLibcallName(RTLIB::FPTOUINT_F64_I64, "_ftol2");
Michael J. Spencer94f7eeb2010-10-19 07:32:52 +0000107 setLibcallName(RTLIB::FPTOUINT_F32_I64, "_ftol2");
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000108 setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer335b8062010-10-11 05:29:15 +0000109 setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
Michael J. Spencer6dad10e2010-10-27 18:52:38 +0000110 setLibcallCallingConv(RTLIB::FPTOUINT_F64_I64, CallingConv::C);
111 setLibcallCallingConv(RTLIB::FPTOUINT_F32_I64, CallingConv::C);
Michael J. Spencer1802a9f2010-10-10 22:04:34 +0000112 }
113
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000114 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000115 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000116 setUseUnderscoreSetJmp(false);
117 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000118 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000119 // MS runtime is weird: it exports _setjmp, but longjmp!
120 setUseUnderscoreSetJmp(true);
121 setUseUnderscoreLongJmp(false);
122 } else {
123 setUseUnderscoreSetJmp(true);
124 setUseUnderscoreLongJmp(true);
125 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000126
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000127 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000129 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000131 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000132 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000133
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000135
Scott Michelfdc40a02009-02-17 22:15:04 +0000136 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000138 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000140 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) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000162 // We have an algorithm for SSE2->double, and we turn this into a
163 // 64-bit FILD followed by conditional FADD for other targets.
164 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000165 // We have an algorithm for SSE2, and we turn this into a 64-bit
166 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000167 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000168 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000169
170 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
171 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
173 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000174
Devang Patel6a784892009-06-05 18:48:29 +0000175 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000176 // SSE has no i16 to fp conversion, only i32
177 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000179 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000181 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
183 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000184 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000185 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000186 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
187 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000188 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000189
Dale Johannesen73328d12007-09-19 23:55:34 +0000190 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
191 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000192 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
193 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000194
Evan Cheng02568ff2006-01-30 22:13:22 +0000195 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
196 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
198 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000199
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000200 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000201 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000202 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000204 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000205 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
206 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000207 }
208
209 // Handle FP_TO_UINT by promoting the destination to a larger signed
210 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
212 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
213 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000214
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
217 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000218 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000219 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000220 // Expand FP_TO_UINT into a select.
221 // FIXME: We would like to use a Custom expander here eventually to do
222 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000223 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000224 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000225 // With SSE3 we can use fisttpll to convert to a signed i64; without
226 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000227 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000228 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229
Chris Lattner399610a2006-12-05 18:22:22 +0000230 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Michael J. Spencerec38de22010-10-10 22:04:20 +0000231 if (!X86ScalarSSEf64) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000232 setOperationAction(ISD::BITCAST , MVT::f32 , Expand);
233 setOperationAction(ISD::BITCAST , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000234 if (Subtarget->is64Bit()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000235 setOperationAction(ISD::BITCAST , MVT::f64 , Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000236 // Without SSE, i64->f64 goes through memory.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000237 setOperationAction(ISD::BITCAST , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000238 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000239 }
Chris Lattner21f66852005-12-23 05:15:23 +0000240
Dan Gohmanb00ee212008-02-18 19:34:53 +0000241 // Scalar integer divide and remainder are lowered to use operations that
242 // produce two results, to match the available instructions. This exposes
243 // the two-result form to trivial CSE, which is able to combine x/y and x%y
244 // into a single instruction.
245 //
246 // Scalar integer multiply-high is also lowered to use two-result
247 // operations, to match the available instructions. However, plain multiply
248 // (low) operations are left as Legal, as there are single-result
249 // instructions for this in x86. Using the two-result multiply instructions
250 // when both high and low results are needed must be arranged by dagcombine.
Chris Lattnere019ec12010-12-19 20:07:10 +0000251 for (unsigned i = 0, e = 4; i != e; ++i) {
252 MVT VT = IntVTs[i];
253 setOperationAction(ISD::MULHS, VT, Expand);
254 setOperationAction(ISD::MULHU, VT, Expand);
255 setOperationAction(ISD::SDIV, VT, Expand);
256 setOperationAction(ISD::UDIV, VT, Expand);
257 setOperationAction(ISD::SREM, VT, Expand);
258 setOperationAction(ISD::UREM, VT, Expand);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000259
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000260 // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
Chris Lattnerd8ff7ec2010-12-20 01:03:27 +0000261 setOperationAction(ISD::ADDC, VT, Custom);
262 setOperationAction(ISD::ADDE, VT, Custom);
263 setOperationAction(ISD::SUBC, VT, Custom);
264 setOperationAction(ISD::SUBE, VT, Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000265 }
Dan Gohmana37c9f72007-09-25 18:23:27 +0000266
Owen Anderson825b72b2009-08-11 20:47:22 +0000267 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
268 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
269 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
270 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000271 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
273 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
274 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
275 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
276 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
277 setOperationAction(ISD::FREM , MVT::f32 , Expand);
278 setOperationAction(ISD::FREM , MVT::f64 , Expand);
279 setOperationAction(ISD::FREM , MVT::f80 , Expand);
280 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000281
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
283 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000284 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
285 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
287 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000288 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
290 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000291 }
292
Benjamin Kramer1292c222010-12-04 20:32:23 +0000293 if (Subtarget->hasPOPCNT()) {
294 setOperationAction(ISD::CTPOP , MVT::i8 , Promote);
295 } else {
296 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
297 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
298 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
299 if (Subtarget->is64Bit())
300 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
301 }
302
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
304 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000305
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000307 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000308 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000309 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Chris Lattnere019ec12010-12-19 20:07:10 +0000310 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000311 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
312 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
313 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
314 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
315 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000316 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
318 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
319 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
320 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
323 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000324 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000326
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000327 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000328 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
329 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
330 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
331 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000332 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000333 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
334 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000335 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000336 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000337 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
338 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
339 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
340 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000341 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000343 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000344 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
345 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
346 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000347 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000348 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
349 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
350 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000351 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000353 if (Subtarget->hasXMM())
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000355
Eric Christopher9a9d2752010-07-22 02:48:34 +0000356 // We may not have a libcall for MEMBARRIER so we should lower this.
357 setOperationAction(ISD::MEMBARRIER , MVT::Other, Custom);
Michael J. Spencerec38de22010-10-10 22:04:20 +0000358
Jim Grosbachf1ab49e2010-06-23 16:25:07 +0000359 // On X86 and X86-64, atomic operations are lowered to locked instructions.
360 // Locked instructions, in turn, have implicit fence semantics (all memory
361 // operations are flushed before issuing the locked instruction, and they
362 // are not buffered), so we can fold away the common pattern of
363 // fence-atomic-fence.
364 setShouldFoldAtomicFences(true);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000365
Mon P Wang63307c32008-05-05 19:05:59 +0000366 // Expand certain atomics
Chris Lattnere019ec12010-12-19 20:07:10 +0000367 for (unsigned i = 0, e = 4; i != e; ++i) {
368 MVT VT = IntVTs[i];
369 setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom);
370 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
371 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000372
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000373 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000374 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
375 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
376 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
377 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
378 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
379 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
380 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000381 }
382
Evan Cheng3c992d22006-03-07 02:02:57 +0000383 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000384 if (!Subtarget->isTargetDarwin() &&
385 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000386 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000388 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000389
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
391 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
392 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
393 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000394 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000395 setExceptionPointerRegister(X86::RAX);
396 setExceptionSelectorRegister(X86::RDX);
397 } else {
398 setExceptionPointerRegister(X86::EAX);
399 setExceptionSelectorRegister(X86::EDX);
400 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
402 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000403
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000405
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000407
Nate Begemanacc398c2006-01-25 18:21:52 +0000408 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000409 setOperationAction(ISD::VASTART , MVT::Other, Custom);
410 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000411 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000412 setOperationAction(ISD::VAARG , MVT::Other, Custom);
413 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000414 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::VAARG , MVT::Other, Expand);
416 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000417 }
Evan Chengae642192007-03-02 23:16:35 +0000418
Owen Anderson825b72b2009-08-11 20:47:22 +0000419 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
420 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000421 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000422 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Michael J. Spencere9c253e2010-10-21 01:41:01 +0000423 if (Subtarget->isTargetCygMing() || Subtarget->isTargetWindows())
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000425 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000426 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000427
Evan Chengc7ce29b2009-02-13 22:36:38 +0000428 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000430 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000431 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
432 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433
Evan Cheng223547a2006-01-31 22:28:30 +0000434 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000435 setOperationAction(ISD::FABS , MVT::f64, Custom);
436 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000437
438 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000439 setOperationAction(ISD::FNEG , MVT::f64, Custom);
440 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000441
Evan Cheng68c47cb2007-01-05 07:55:56 +0000442 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000443 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
444 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000445
Evan Chengd25e9e82006-02-02 00:28:23 +0000446 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000447 setOperationAction(ISD::FSIN , MVT::f64, Expand);
448 setOperationAction(ISD::FCOS , MVT::f64, Expand);
449 setOperationAction(ISD::FSIN , MVT::f32, Expand);
450 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
Chris Lattnera54aa942006-01-29 06:26:08 +0000452 // Expand FP immediates into loads from the stack, except for the special
453 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000454 addLegalFPImmediate(APFloat(+0.0)); // xorpd
455 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000456 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000457 // Use SSE for f32, x87 for f64.
458 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000459 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
460 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000461
462 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464
465 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000467
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000469
470 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
472 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000473
474 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::FSIN , MVT::f32, Expand);
476 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477
Nate Begemane1795842008-02-14 08:57:00 +0000478 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000479 addLegalFPImmediate(APFloat(+0.0f)); // xorps
480 addLegalFPImmediate(APFloat(+0.0)); // FLD0
481 addLegalFPImmediate(APFloat(+1.0)); // FLD1
482 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
483 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
484
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000485 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000486 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
487 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000488 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000489 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000490 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000491 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
493 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000494
Owen Anderson825b72b2009-08-11 20:47:22 +0000495 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
496 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
497 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
498 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000499
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000500 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000501 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
502 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000503 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000504 addLegalFPImmediate(APFloat(+0.0)); // FLD0
505 addLegalFPImmediate(APFloat(+1.0)); // FLD1
506 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
507 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000508 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
509 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
510 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
511 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000512 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000513
Dale Johannesen59a58732007-08-05 18:49:15 +0000514 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000515 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
517 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
518 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000519 {
Benjamin Kramer98383962010-12-04 14:22:24 +0000520 APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000521 addLegalFPImmediate(TmpFlt); // FLD0
522 TmpFlt.changeSign();
523 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
Benjamin Kramer98383962010-12-04 14:22:24 +0000524
525 bool ignored;
Evan Chengc7ce29b2009-02-13 22:36:38 +0000526 APFloat TmpFlt2(+1.0);
527 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
528 &ignored);
529 addLegalFPImmediate(TmpFlt2); // FLD1
530 TmpFlt2.changeSign();
531 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
532 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000533
Evan Chengc7ce29b2009-02-13 22:36:38 +0000534 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
536 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000537 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000538 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000539
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000540 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000541 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
542 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
543 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000544
Owen Anderson825b72b2009-08-11 20:47:22 +0000545 setOperationAction(ISD::FLOG, MVT::f80, Expand);
546 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
547 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
548 setOperationAction(ISD::FEXP, MVT::f80, Expand);
549 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000550
Mon P Wangf007a8b2008-11-06 05:31:54 +0000551 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000552 // (for widening) or expand (for scalarization). Then we will selectively
553 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000554 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
555 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
556 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000571 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
David Greenecfe33c42011-01-26 19:13:22 +0000572 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
573 setOperationAction(ISD::INSERT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
594 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
595 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
599 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
600 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
601 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
602 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
603 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
604 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000605 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000606 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
607 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
608 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
609 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
610 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
611 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
612 setTruncStoreAction((MVT::SimpleValueType)VT,
613 (MVT::SimpleValueType)InnerVT, Expand);
614 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
615 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
616 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000617 }
618
Evan Chengc7ce29b2009-02-13 22:36:38 +0000619 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
620 // with -msoft-float, disable use of MMX as well.
Chris Lattner2a786eb2010-12-19 20:19:20 +0000621 if (!UseSoftFloat && Subtarget->hasMMX()) {
Dale Johannesene93d99c2010-10-20 21:32:10 +0000622 addRegisterClass(MVT::x86mmx, X86::VR64RegisterClass);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000623 // No operations on x86mmx supported, everything uses intrinsics.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000624 }
625
Dale Johannesen0488fb62010-09-30 23:57:10 +0000626 // MMX-sized vectors (other than x86mmx) are expected to be expanded
627 // into smaller operations.
628 setOperationAction(ISD::MULHS, MVT::v8i8, Expand);
629 setOperationAction(ISD::MULHS, MVT::v4i16, Expand);
630 setOperationAction(ISD::MULHS, MVT::v2i32, Expand);
631 setOperationAction(ISD::MULHS, MVT::v1i64, Expand);
632 setOperationAction(ISD::AND, MVT::v8i8, Expand);
633 setOperationAction(ISD::AND, MVT::v4i16, Expand);
634 setOperationAction(ISD::AND, MVT::v2i32, Expand);
635 setOperationAction(ISD::AND, MVT::v1i64, Expand);
636 setOperationAction(ISD::OR, MVT::v8i8, Expand);
637 setOperationAction(ISD::OR, MVT::v4i16, Expand);
638 setOperationAction(ISD::OR, MVT::v2i32, Expand);
639 setOperationAction(ISD::OR, MVT::v1i64, Expand);
640 setOperationAction(ISD::XOR, MVT::v8i8, Expand);
641 setOperationAction(ISD::XOR, MVT::v4i16, Expand);
642 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
643 setOperationAction(ISD::XOR, MVT::v1i64, Expand);
644 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand);
645 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand);
646 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand);
647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand);
648 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand);
649 setOperationAction(ISD::SELECT, MVT::v8i8, Expand);
650 setOperationAction(ISD::SELECT, MVT::v4i16, Expand);
651 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
652 setOperationAction(ISD::SELECT, MVT::v1i64, Expand);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000653 setOperationAction(ISD::BITCAST, MVT::v8i8, Expand);
654 setOperationAction(ISD::BITCAST, MVT::v4i16, Expand);
655 setOperationAction(ISD::BITCAST, MVT::v2i32, Expand);
656 setOperationAction(ISD::BITCAST, MVT::v1i64, Expand);
Dale Johannesen0488fb62010-09-30 23:57:10 +0000657
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000658 if (!UseSoftFloat && Subtarget->hasXMM()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000660
Owen Anderson825b72b2009-08-11 20:47:22 +0000661 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
662 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
663 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
664 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
665 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
666 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
667 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
668 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
669 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
670 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
671 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
672 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000673 }
674
Nate Begeman2ea8ee72010-12-10 00:26:57 +0000675 if (!UseSoftFloat && Subtarget->hasXMMInt()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000676 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000677
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000678 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
679 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
681 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
682 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
683 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000684
Owen Anderson825b72b2009-08-11 20:47:22 +0000685 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
686 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
687 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
688 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
689 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
690 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
691 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
692 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
693 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
694 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
695 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
696 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
697 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
698 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
699 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
700 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
703 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
704 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
705 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000706
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
708 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
709 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
710 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
711 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000712
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000713 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
714 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
715 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
716 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
717 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
718
Evan Cheng2c3ae372006-04-12 21:21:57 +0000719 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
721 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000722 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000723 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000724 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000725 // Do not attempt to custom lower non-128-bit vectors
726 if (!VT.is128BitVector())
727 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 setOperationAction(ISD::BUILD_VECTOR,
729 VT.getSimpleVT().SimpleTy, Custom);
730 setOperationAction(ISD::VECTOR_SHUFFLE,
731 VT.getSimpleVT().SimpleTy, Custom);
732 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
733 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000734 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000735
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
737 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
738 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
739 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
740 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
741 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000742
Nate Begemancdd1eec2008-02-12 22:51:28 +0000743 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000744 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
745 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000746 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000747
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000748 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
750 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000751 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000752
753 // Do not attempt to promote non-128-bit vectors
Chris Lattner32b4b5a2010-07-05 05:53:14 +0000754 if (!VT.is128BitVector())
David Greene9b9838d2009-06-29 16:47:10 +0000755 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +0000756
Owen Andersond6662ad2009-08-10 20:46:15 +0000757 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000758 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000759 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000761 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000763 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000765 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000767 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000768
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000770
Evan Cheng2c3ae372006-04-12 21:21:57 +0000771 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
773 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
774 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
775 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000776
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
778 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000779 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000780
Nate Begeman14d12ca2008-02-11 04:19:36 +0000781 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000782 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
783 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
784 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
785 setOperationAction(ISD::FRINT, MVT::f32, Legal);
786 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
787 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
788 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
789 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
790 setOperationAction(ISD::FRINT, MVT::f64, Legal);
791 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
792
Nate Begeman14d12ca2008-02-11 04:19:36 +0000793 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000795
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000796 // Can turn SHL into an integer multiply.
797 setOperationAction(ISD::SHL, MVT::v4i32, Custom);
Nate Begeman51409212010-07-28 00:21:48 +0000798 setOperationAction(ISD::SHL, MVT::v16i8, Custom);
Nate Begemanbdcb5af2010-07-27 22:37:06 +0000799
Nate Begeman14d12ca2008-02-11 04:19:36 +0000800 // i8 and i16 vectors are custom , because the source register and source
801 // source memory operand types are not the same width. f32 vectors are
802 // custom since the immediate controlling the insert encodes additional
803 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
805 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
806 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
807 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000808
Owen Anderson825b72b2009-08-11 20:47:22 +0000809 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
810 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
811 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
812 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000813
814 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
816 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000817 }
818 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000819
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000820 if (Subtarget->hasSSE42())
Owen Anderson825b72b2009-08-11 20:47:22 +0000821 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000822
David Greene9b9838d2009-06-29 16:47:10 +0000823 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
825 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
826 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
827 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
Bruno Cardoso Lopes405f11b2010-08-10 01:43:16 +0000828 addRegisterClass(MVT::v32i8, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000829
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
831 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
832 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
833 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
David Greene54d8eba2011-01-27 22:38:56 +0000834
Owen Anderson825b72b2009-08-11 20:47:22 +0000835 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
836 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
837 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
838 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
839 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
840 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000841
Owen Anderson825b72b2009-08-11 20:47:22 +0000842 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
843 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
844 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
845 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
846 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
847 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000848
David Greene54d8eba2011-01-27 22:38:56 +0000849 // Custom lower build_vector, vector_shuffle, scalar_to_vector,
850 // insert_vector_elt extract_subvector and extract_vector_elt for
851 // 256-bit types.
852 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
853 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
854 ++i) {
855 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
856 // Do not attempt to custom lower non-256-bit vectors
857 if (!isPowerOf2_32(MVT(VT).getVectorNumElements())
858 || (MVT(VT).getSizeInBits() < 256))
David Greene9b9838d2009-06-29 16:47:10 +0000859 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000860 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
861 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000862 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000863 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
David Greene54d8eba2011-01-27 22:38:56 +0000864 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000865 }
David Greene54d8eba2011-01-27 22:38:56 +0000866 // Custom-lower insert_subvector and extract_subvector based on
867 // the result type.
868 for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
869 i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE;
870 ++i) {
871 MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
872 // Do not attempt to custom lower non-256-bit vectors
873 if (!isPowerOf2_32(MVT(VT).getVectorNumElements()))
David Greene9b9838d2009-06-29 16:47:10 +0000874 continue;
David Greene54d8eba2011-01-27 22:38:56 +0000875
876 if (MVT(VT).getSizeInBits() == 128) {
877 setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000878 }
David Greene54d8eba2011-01-27 22:38:56 +0000879 else if (MVT(VT).getSizeInBits() == 256) {
880 setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom);
881 }
David Greene9b9838d2009-06-29 16:47:10 +0000882 }
883
David Greene54d8eba2011-01-27 22:38:56 +0000884 // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
885 // Don't promote loads because we need them for VPERM vector index versions.
886
887 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
888 VT != (unsigned)MVT::LAST_VECTOR_VALUETYPE;
889 VT++) {
890 if (!isPowerOf2_32(MVT((MVT::SimpleValueType)VT).getVectorNumElements())
891 || (MVT((MVT::SimpleValueType)VT).getSizeInBits() < 256))
892 continue;
893 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
894 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v4i64);
895 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
896 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v4i64);
897 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
898 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v4i64);
899 //setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
900 //AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v4i64);
901 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
902 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v4i64);
903 }
David Greene9b9838d2009-06-29 16:47:10 +0000904 }
905
Evan Cheng6be2c582006-04-05 23:38:46 +0000906 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000908
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000909
Eli Friedman962f5492010-06-02 19:35:46 +0000910 // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
911 // handle type legalization for these operations here.
Dan Gohman71c62a22010-06-02 19:13:40 +0000912 //
Eli Friedman962f5492010-06-02 19:35:46 +0000913 // FIXME: We really should do custom legalization for addition and
914 // subtraction on x86-32 once PR3203 is fixed. We really can't do much better
915 // than generic legalization for 64-bit multiplication-with-overflow, though.
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000916 for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) {
917 // Add/Sub/Mul with overflow operations are custom lowered.
918 MVT VT = IntVTs[i];
919 setOperationAction(ISD::SADDO, VT, Custom);
920 setOperationAction(ISD::UADDO, VT, Custom);
921 setOperationAction(ISD::SSUBO, VT, Custom);
922 setOperationAction(ISD::USUBO, VT, Custom);
923 setOperationAction(ISD::SMULO, VT, Custom);
924 setOperationAction(ISD::UMULO, VT, Custom);
Eli Friedmana993f0a2010-06-02 00:27:18 +0000925 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +0000926
Chris Lattnera34b3cf2010-12-19 20:03:11 +0000927 // There are no 8-bit 3-address imul/mul instructions
928 setOperationAction(ISD::SMULO, MVT::i8, Expand);
929 setOperationAction(ISD::UMULO, MVT::i8, Expand);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000930
Evan Chengd54f2d52009-03-31 19:38:51 +0000931 if (!Subtarget->is64Bit()) {
932 // These libcalls are not available in 32-bit.
933 setLibcallName(RTLIB::SHL_I128, 0);
934 setLibcallName(RTLIB::SRL_I128, 0);
935 setLibcallName(RTLIB::SRA_I128, 0);
936 }
937
Evan Cheng206ee9d2006-07-07 08:33:52 +0000938 // We have target-specific dag combine patterns for the following nodes:
939 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000940 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000941 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000942 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000943 setTargetDAGCombine(ISD::SHL);
944 setTargetDAGCombine(ISD::SRA);
945 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +0000946 setTargetDAGCombine(ISD::OR);
Nate Begemanb65c1752010-12-17 22:55:37 +0000947 setTargetDAGCombine(ISD::AND);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +0000948 setTargetDAGCombine(ISD::ADD);
949 setTargetDAGCombine(ISD::SUB);
Chris Lattner149a4e52008-02-22 02:09:43 +0000950 setTargetDAGCombine(ISD::STORE);
Evan Cheng2e489c42009-12-16 00:53:11 +0000951 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000952 if (Subtarget->is64Bit())
953 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000954
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000955 computeRegisterProperties();
956
Evan Cheng05219282011-01-06 06:52:41 +0000957 // On Darwin, -Os means optimize for size without hurting performance,
958 // do not reduce the limit.
Dan Gohman87060f52008-06-30 21:00:56 +0000959 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000960 maxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8;
Evan Cheng255f20f2010-04-01 06:04:33 +0000961 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Evan Cheng05219282011-01-06 06:52:41 +0000962 maxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
963 maxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
964 maxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4;
Evan Chengfb8075d2008-02-28 00:43:03 +0000965 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000966 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000967}
968
Scott Michel5b8f82e2008-03-10 15:42:14 +0000969
Owen Anderson825b72b2009-08-11 20:47:22 +0000970MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
971 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +0000972}
973
974
Evan Cheng29286502008-01-23 23:17:41 +0000975/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
976/// the desired ByVal argument alignment.
977static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
978 if (MaxAlign == 16)
979 return;
980 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
981 if (VTy->getBitWidth() == 128)
982 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000983 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
984 unsigned EltAlign = 0;
985 getMaxByValAlign(ATy->getElementType(), EltAlign);
986 if (EltAlign > MaxAlign)
987 MaxAlign = EltAlign;
988 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
989 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
990 unsigned EltAlign = 0;
991 getMaxByValAlign(STy->getElementType(i), EltAlign);
992 if (EltAlign > MaxAlign)
993 MaxAlign = EltAlign;
994 if (MaxAlign == 16)
995 break;
996 }
997 }
998 return;
999}
1000
1001/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1002/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001003/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1004/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001005unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001006 if (Subtarget->is64Bit()) {
1007 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001008 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001009 if (TyAlign > 8)
1010 return TyAlign;
1011 return 8;
1012 }
1013
Evan Cheng29286502008-01-23 23:17:41 +00001014 unsigned Align = 4;
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001015 if (Subtarget->hasXMM())
Dale Johannesen0c191872008-02-08 19:48:20 +00001016 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001017 return Align;
1018}
Chris Lattner2b02a442007-02-25 08:29:00 +00001019
Evan Chengf0df0312008-05-15 08:39:06 +00001020/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001021/// and store operations as a result of memset, memcpy, and memmove
1022/// lowering. If DstAlign is zero that means it's safe to destination
1023/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1024/// means there isn't a need to check it against alignment requirement,
1025/// probably because the source does not need to be loaded. If
1026/// 'NonScalarIntSafe' is true, that means it's safe to return a
1027/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1028/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1029/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001030/// It returns EVT::Other if the type should be determined using generic
1031/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001032EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001033X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1034 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001035 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001036 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001037 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001038 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1039 // linux. This is because the stack realignment code can't handle certain
1040 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001041 const Function *F = MF.getFunction();
Evan Chenga5e13622011-01-07 19:35:30 +00001042 if (NonScalarIntSafe &&
1043 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001044 if (Size >= 16 &&
Evan Chenga5e13622011-01-07 19:35:30 +00001045 (Subtarget->isUnalignedMemAccessFast() ||
1046 ((DstAlign == 0 || DstAlign >= 16) &&
1047 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001048 Subtarget->getStackAlignment() >= 16) {
1049 if (Subtarget->hasSSE2())
1050 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001051 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001052 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001053 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001054 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001055 Subtarget->getStackAlignment() >= 8 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001056 Subtarget->hasXMMInt()) {
Evan Chengc3b0c342010-04-08 07:37:57 +00001057 // Do not use f64 to lower memcpy if source is string constant. It's
1058 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001059 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001060 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001061 }
Evan Chengf0df0312008-05-15 08:39:06 +00001062 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 return MVT::i64;
1064 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001065}
1066
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001067/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1068/// current function. The returned value is a member of the
1069/// MachineJumpTableInfo::JTEntryKind enum.
1070unsigned X86TargetLowering::getJumpTableEncoding() const {
1071 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1072 // symbol.
1073 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1074 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001075 return MachineJumpTableInfo::EK_Custom32;
Michael J. Spencerec38de22010-10-10 22:04:20 +00001076
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001077 // Otherwise, use the normal jump table encoding heuristics.
1078 return TargetLowering::getJumpTableEncoding();
1079}
1080
Chris Lattnerc64daab2010-01-26 05:02:42 +00001081const MCExpr *
1082X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1083 const MachineBasicBlock *MBB,
1084 unsigned uid,MCContext &Ctx) const{
1085 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1086 Subtarget->isPICStyleGOT());
1087 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1088 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001089 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1090 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001091}
1092
Evan Chengcc415862007-11-09 01:32:10 +00001093/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1094/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001095SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001096 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001097 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001098 // This doesn't have DebugLoc associated with it, but is not really the
1099 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001100 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001101 return Table;
1102}
1103
Chris Lattner589c6f62010-01-26 06:28:43 +00001104/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1105/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1106/// MCExpr.
1107const MCExpr *X86TargetLowering::
1108getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1109 MCContext &Ctx) const {
1110 // X86-64 uses RIP relative addressing based on the jump table label.
1111 if (Subtarget->isPICStyleRIPRel())
1112 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1113
1114 // Otherwise, the reference is relative to the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +00001115 return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx);
Chris Lattner589c6f62010-01-26 06:28:43 +00001116}
1117
Bill Wendlingb4202b82009-07-01 18:50:55 +00001118/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001119unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001120 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001121}
1122
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001123// FIXME: Why this routine is here? Move to RegInfo!
Evan Chengdee81012010-07-26 21:50:05 +00001124std::pair<const TargetRegisterClass*, uint8_t>
1125X86TargetLowering::findRepresentativeClass(EVT VT) const{
1126 const TargetRegisterClass *RRC = 0;
1127 uint8_t Cost = 1;
1128 switch (VT.getSimpleVT().SimpleTy) {
1129 default:
1130 return TargetLowering::findRepresentativeClass(VT);
1131 case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
1132 RRC = (Subtarget->is64Bit()
1133 ? X86::GR64RegisterClass : X86::GR32RegisterClass);
1134 break;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001135 case MVT::x86mmx:
Evan Chengdee81012010-07-26 21:50:05 +00001136 RRC = X86::VR64RegisterClass;
1137 break;
1138 case MVT::f32: case MVT::f64:
1139 case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
1140 case MVT::v4f32: case MVT::v2f64:
1141 case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
1142 case MVT::v4f64:
1143 RRC = X86::VR128RegisterClass;
1144 break;
1145 }
1146 return std::make_pair(RRC, Cost);
1147}
1148
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001149// FIXME: Why this routine is here? Move to RegInfo!
Evan Cheng70017e42010-07-24 00:39:05 +00001150unsigned
1151X86TargetLowering::getRegPressureLimit(const TargetRegisterClass *RC,
1152 MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001153 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001154
1155 unsigned FPDiff = TFI->hasFP(MF) ? 1 : 0;
Evan Cheng70017e42010-07-24 00:39:05 +00001156 switch (RC->getID()) {
1157 default:
1158 return 0;
1159 case X86::GR32RegClassID:
1160 return 4 - FPDiff;
1161 case X86::GR64RegClassID:
1162 return 8 - FPDiff;
1163 case X86::VR128RegClassID:
1164 return Subtarget->is64Bit() ? 10 : 4;
1165 case X86::VR64RegClassID:
1166 return 4;
1167 }
1168}
1169
Eric Christopherf7a0c7b2010-07-06 05:18:56 +00001170bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
1171 unsigned &Offset) const {
1172 if (!Subtarget->isTargetLinux())
1173 return false;
1174
1175 if (Subtarget->is64Bit()) {
1176 // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
1177 Offset = 0x28;
1178 if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
1179 AddressSpace = 256;
1180 else
1181 AddressSpace = 257;
1182 } else {
1183 // %gs:0x14 on i386
1184 Offset = 0x14;
1185 AddressSpace = 256;
1186 }
1187 return true;
1188}
1189
1190
Chris Lattner2b02a442007-02-25 08:29:00 +00001191//===----------------------------------------------------------------------===//
1192// Return Value Calling Convention Implementation
1193//===----------------------------------------------------------------------===//
1194
Chris Lattner59ed56b2007-02-28 04:55:35 +00001195#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001196
Michael J. Spencerec38de22010-10-10 22:04:20 +00001197bool
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001198X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
Dan Gohman84023e02010-07-10 09:00:22 +00001199 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001200 LLVMContext &Context) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001201 SmallVector<CCValAssign, 16> RVLocs;
1202 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Dan Gohmanc9af33c2010-07-06 22:19:37 +00001203 RVLocs, Context);
Dan Gohman84023e02010-07-10 09:00:22 +00001204 return CCInfo.CheckReturn(Outs, RetCC_X86);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001205}
1206
Dan Gohman98ca4f22009-08-05 01:29:28 +00001207SDValue
1208X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001209 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001210 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001211 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +00001212 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001213 MachineFunction &MF = DAG.getMachineFunction();
1214 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001215
Chris Lattner9774c912007-02-27 05:28:59 +00001216 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1218 RVLocs, *DAG.getContext());
1219 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001220
Evan Chengdcea1632010-02-04 02:40:39 +00001221 // Add the regs to the liveout set for the function.
1222 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1223 for (unsigned i = 0; i != RVLocs.size(); ++i)
1224 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1225 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001226
Dan Gohman475871a2008-07-27 21:46:04 +00001227 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001228
Dan Gohman475871a2008-07-27 21:46:04 +00001229 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001230 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1231 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001232 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1233 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001234
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001235 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001236 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1237 CCValAssign &VA = RVLocs[i];
1238 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohmanc9403652010-07-07 15:54:55 +00001239 SDValue ValToCopy = OutVals[i];
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001240 EVT ValVT = ValToCopy.getValueType();
1241
Dale Johannesenc4510512010-09-24 19:05:48 +00001242 // If this is x86-64, and we disabled SSE, we can't return FP values,
1243 // or SSE or MMX vectors.
1244 if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
1245 VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001246 (Subtarget->is64Bit() && !Subtarget->hasXMM())) {
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001247 report_fatal_error("SSE register return with SSE disabled");
1248 }
1249 // Likewise we can't return F64 values with SSE1 only. gcc does so, but
1250 // llvm-gcc has never done it right and no one has noticed, so this
1251 // should be OK for now.
1252 if (ValVT == MVT::f64 &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001253 (Subtarget->is64Bit() && !Subtarget->hasXMMInt()))
Dale Johannesenc76d23f2010-07-23 00:30:35 +00001254 report_fatal_error("SSE2 register return with SSE2 disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001255
Chris Lattner447ff682008-03-11 03:23:40 +00001256 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1257 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001258 if (VA.getLocReg() == X86::ST0 ||
1259 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001260 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1261 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001262 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001263 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001264 RetOps.push_back(ValToCopy);
1265 // Don't emit a copytoreg.
1266 continue;
1267 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001268
Evan Cheng242b38b2009-02-23 09:03:22 +00001269 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1270 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001271 if (Subtarget->is64Bit()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001272 if (ValVT == MVT::x86mmx) {
Chris Lattner97a2a562010-08-26 05:24:29 +00001273 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001274 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy);
Eric Christopher90eb4022010-07-22 00:26:08 +00001275 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
1276 ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001277 // If we don't have SSE2 available, convert to v4f32 so the generated
1278 // register is legal.
1279 if (!Subtarget->hasSSE2())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001280 ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy);
Chris Lattner97a2a562010-08-26 05:24:29 +00001281 }
Evan Cheng242b38b2009-02-23 09:03:22 +00001282 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001283 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00001284
Dale Johannesendd64c412009-02-04 00:33:20 +00001285 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001286 Flag = Chain.getValue(1);
1287 }
Dan Gohman61a92132008-04-21 23:59:07 +00001288
1289 // The x86-64 ABI for returning structs by value requires that we copy
1290 // the sret argument into %rax for the return. We saved the argument into
1291 // a virtual register in the entry block, so now we copy the value out
1292 // and into %rax.
1293 if (Subtarget->is64Bit() &&
1294 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1295 MachineFunction &MF = DAG.getMachineFunction();
1296 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1297 unsigned Reg = FuncInfo->getSRetReturnReg();
Michael J. Spencerec38de22010-10-10 22:04:20 +00001298 assert(Reg &&
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001299 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001300 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001301
Dale Johannesendd64c412009-02-04 00:33:20 +00001302 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001303 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001304
1305 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001306 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001307 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001308
Chris Lattner447ff682008-03-11 03:23:40 +00001309 RetOps[0] = Chain; // Update chain.
1310
1311 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001312 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001313 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001314
1315 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001316 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001317}
1318
Evan Cheng3d2125c2010-11-30 23:55:39 +00001319bool X86TargetLowering::isUsedByReturnOnly(SDNode *N) const {
1320 if (N->getNumValues() != 1)
1321 return false;
1322 if (!N->hasNUsesOfValue(1, 0))
1323 return false;
1324
1325 SDNode *Copy = *N->use_begin();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001326 if (Copy->getOpcode() != ISD::CopyToReg &&
1327 Copy->getOpcode() != ISD::FP_EXTEND)
Evan Cheng3d2125c2010-11-30 23:55:39 +00001328 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001329
1330 bool HasRet = false;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001331 for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
Evan Cheng1bf891a2010-12-01 22:59:46 +00001332 UI != UE; ++UI) {
Evan Cheng3d2125c2010-11-30 23:55:39 +00001333 if (UI->getOpcode() != X86ISD::RET_FLAG)
1334 return false;
Evan Cheng1bf891a2010-12-01 22:59:46 +00001335 HasRet = true;
1336 }
Evan Cheng3d2125c2010-11-30 23:55:39 +00001337
Evan Cheng1bf891a2010-12-01 22:59:46 +00001338 return HasRet;
Evan Cheng3d2125c2010-11-30 23:55:39 +00001339}
1340
Dan Gohman98ca4f22009-08-05 01:29:28 +00001341/// LowerCallResult - Lower the result values of a call into the
1342/// appropriate copies out of appropriate physical registers.
1343///
1344SDValue
1345X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001346 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001347 const SmallVectorImpl<ISD::InputArg> &Ins,
1348 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001349 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001350
Chris Lattnere32bbf62007-02-28 07:09:55 +00001351 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001352 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001353 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001354 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001355 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001356 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001357
Chris Lattner3085e152007-02-25 08:59:22 +00001358 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001359 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001360 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001361 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001362
Torok Edwin3f142c32009-02-01 18:15:56 +00001363 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001364 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001365 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasXMM())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001366 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001367 }
1368
Evan Cheng79fb3b42009-02-20 20:43:02 +00001369 SDValue Val;
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001370
1371 // If this is a call to a function that returns an fp value on the floating
1372 // point stack, we must guarantee the the value is popped from the stack, so
1373 // a CopyFromReg is not good enough - the copy instruction may be eliminated
1374 // if the return value is not used. We use the FpGET_ST0 instructions
1375 // instead.
1376 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) {
1377 // If we prefer to use the value in xmm registers, copy it out as f80 and
1378 // use a truncate to move it from fp stack reg to xmm reg.
1379 if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80;
1380 bool isST0 = VA.getLocReg() == X86::ST0;
1381 unsigned Opc = 0;
1382 if (CopyVT == MVT::f32) Opc = isST0 ? X86::FpGET_ST0_32:X86::FpGET_ST1_32;
1383 if (CopyVT == MVT::f64) Opc = isST0 ? X86::FpGET_ST0_64:X86::FpGET_ST1_64;
1384 if (CopyVT == MVT::f80) Opc = isST0 ? X86::FpGET_ST0_80:X86::FpGET_ST1_80;
1385 SDValue Ops[] = { Chain, InFlag };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001386 Chain = SDValue(DAG.getMachineNode(Opc, dl, CopyVT, MVT::Other, MVT::Glue,
Jakob Stoklund Olesend737fca2010-07-10 04:04:25 +00001387 Ops, 2), 1);
1388 Val = Chain.getValue(0);
1389
1390 // Round the f80 to the right size, which also moves it to the appropriate
1391 // xmm register.
1392 if (CopyVT != VA.getValVT())
1393 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
1394 // This truncation won't change the value.
1395 DAG.getIntPtrConstant(1));
1396 } else if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001397 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1398 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1399 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001400 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001401 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1403 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001404 } else {
1405 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001406 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001407 Val = Chain.getValue(0);
1408 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001409 Val = DAG.getNode(ISD::BITCAST, dl, CopyVT, Val);
Evan Cheng79fb3b42009-02-20 20:43:02 +00001410 } else {
1411 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1412 CopyVT, InFlag).getValue(1);
1413 Val = Chain.getValue(0);
1414 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001415 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001416 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001417 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001418
Dan Gohman98ca4f22009-08-05 01:29:28 +00001419 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001420}
1421
1422
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001423//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001424// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001425//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001426// StdCall calling convention seems to be standard for many Windows' API
1427// routines and around. It differs from C calling convention just a little:
1428// callee should clean up the stack, not caller. Symbols should be also
1429// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001430// For info on fast calling convention see Fast Calling Convention (tail call)
1431// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001432
Dan Gohman98ca4f22009-08-05 01:29:28 +00001433/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001434/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001435static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1436 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001437 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001438
Dan Gohman98ca4f22009-08-05 01:29:28 +00001439 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001440}
1441
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001442/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001443/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001444static bool
1445ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1446 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001447 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001448
Dan Gohman98ca4f22009-08-05 01:29:28 +00001449 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001450}
1451
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001452/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1453/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001454/// the specific parameter attribute. The copy will be passed as a byval
1455/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001456static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001457CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001458 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1459 DebugLoc dl) {
Chris Lattnere72f2022010-09-21 05:40:29 +00001460 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Michael J. Spencerec38de22010-10-10 22:04:20 +00001461
Dale Johannesendd64c412009-02-04 00:33:20 +00001462 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001463 /*isVolatile*/false, /*AlwaysInline=*/true,
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001464 MachinePointerInfo(), MachinePointerInfo());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001465}
1466
Chris Lattner29689432010-03-11 00:22:57 +00001467/// IsTailCallConvention - Return true if the calling convention is one that
1468/// supports tail call optimization.
1469static bool IsTailCallConvention(CallingConv::ID CC) {
1470 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1471}
1472
Evan Cheng0c439eb2010-01-27 00:07:07 +00001473/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1474/// a tailcall target by changing its ABI.
1475static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001476 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001477}
1478
Dan Gohman98ca4f22009-08-05 01:29:28 +00001479SDValue
1480X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001481 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001482 const SmallVectorImpl<ISD::InputArg> &Ins,
1483 DebugLoc dl, SelectionDAG &DAG,
1484 const CCValAssign &VA,
1485 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001486 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001487 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001488 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001489 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001490 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001491 EVT ValVT;
1492
1493 // If value is passed by pointer we have address passed instead of the value
1494 // itself.
1495 if (VA.getLocInfo() == CCValAssign::Indirect)
1496 ValVT = VA.getLocVT();
1497 else
1498 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001499
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001500 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001501 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001502 // In case of tail call optimization mark all arguments mutable. Since they
1503 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001504 if (Flags.isByVal()) {
1505 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
Evan Chenged2ae132010-07-03 00:40:23 +00001506 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001507 return DAG.getFrameIndex(FI, getPointerTy());
1508 } else {
1509 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
Evan Chenged2ae132010-07-03 00:40:23 +00001510 VA.getLocMemOffset(), isImmutable);
Evan Cheng90567c32010-02-02 23:58:13 +00001511 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1512 return DAG.getLoad(ValVT, dl, Chain, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001513 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001514 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001515 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001516}
1517
Dan Gohman475871a2008-07-27 21:46:04 +00001518SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001519X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001520 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001521 bool isVarArg,
1522 const SmallVectorImpl<ISD::InputArg> &Ins,
1523 DebugLoc dl,
1524 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001525 SmallVectorImpl<SDValue> &InVals)
1526 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001527 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001528 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001529
Gordon Henriksen86737662008-01-05 16:56:59 +00001530 const Function* Fn = MF.getFunction();
1531 if (Fn->hasExternalLinkage() &&
1532 Subtarget->isTargetCygMing() &&
1533 Fn->getName() == "main")
1534 FuncInfo->setForceFramePointer(true);
1535
Evan Cheng1bc78042006-04-26 01:20:17 +00001536 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001537 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001538 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001539
Chris Lattner29689432010-03-11 00:22:57 +00001540 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1541 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001542
Chris Lattner638402b2007-02-28 07:00:42 +00001543 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001544 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001545 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1546 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001547 CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001548
Chris Lattnerf39f7712007-02-28 05:46:49 +00001549 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001550 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001551 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1552 CCValAssign &VA = ArgLocs[i];
1553 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1554 // places.
1555 assert(VA.getValNo() != LastVal &&
1556 "Don't support value assigned to multiple locs yet");
1557 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattnerf39f7712007-02-28 05:46:49 +00001559 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001560 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001561 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001562 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001563 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001564 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001565 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001567 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001568 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001569 RC = X86::FR64RegisterClass;
Bruno Cardoso Lopesac098352010-08-05 23:35:51 +00001570 else if (RegVT.isVector() && RegVT.getSizeInBits() == 256)
1571 RC = X86::VR256RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001572 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001573 RC = X86::VR128RegisterClass;
Dale Johannesen0488fb62010-09-30 23:57:10 +00001574 else if (RegVT == MVT::x86mmx)
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001575 RC = X86::VR64RegisterClass;
1576 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001577 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001578
Devang Patele9a7ea62011-01-31 21:38:14 +00001579 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC, dl);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001580 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Chris Lattnerf39f7712007-02-28 05:46:49 +00001582 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1583 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1584 // right size.
1585 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001586 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001587 DAG.getValueType(VA.getValVT()));
1588 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001589 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001590 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001591 else if (VA.getLocInfo() == CCValAssign::BCvt)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001592 ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001593
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001594 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001595 // Handle MMX values passed in XMM regs.
1596 if (RegVT.isVector()) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00001597 ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(),
1598 ArgValue);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001599 } else
1600 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001601 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001602 } else {
1603 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001604 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001605 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001606
1607 // If value is passed via pointer - do a load.
1608 if (VA.getLocInfo() == CCValAssign::Indirect)
Chris Lattner51abfe42010-09-21 06:02:19 +00001609 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
1610 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001611
Dan Gohman98ca4f22009-08-05 01:29:28 +00001612 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001613 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001614
Dan Gohman61a92132008-04-21 23:59:07 +00001615 // The x86-64 ABI for returning structs by value requires that we copy
1616 // the sret argument into %rax for the return. Save the argument into
1617 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001618 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001619 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1620 unsigned Reg = FuncInfo->getSRetReturnReg();
1621 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001622 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001623 FuncInfo->setSRetReturnReg(Reg);
1624 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001625 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001626 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001627 }
1628
Chris Lattnerf39f7712007-02-28 05:46:49 +00001629 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001630 // Align stack specially for tail calls.
1631 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001632 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001633
Evan Cheng1bc78042006-04-26 01:20:17 +00001634 // If the function takes variable number of arguments, make a frame index for
1635 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001636 if (isVarArg) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001637 if (!IsWin64 && (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1638 CallConv != CallingConv::X86_ThisCall))) {
Jakob Stoklund Olesenb2eeed72010-07-29 17:42:27 +00001639 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true));
Gordon Henriksen86737662008-01-05 16:56:59 +00001640 }
1641 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001642 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1643
1644 // FIXME: We should really autogenerate these arrays
1645 static const unsigned GPR64ArgRegsWin64[] = {
1646 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001647 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001648 static const unsigned GPR64ArgRegs64Bit[] = {
1649 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1650 };
1651 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1653 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1654 };
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001655 const unsigned *GPR64ArgRegs;
1656 unsigned NumXMMRegs = 0;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001657
1658 if (IsWin64) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001659 // The XMM registers which might contain var arg parameters are shadowed
1660 // in their paired GPR. So we only need to save the GPR to their home
1661 // slots.
1662 TotalNumIntRegs = 4;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001663 GPR64ArgRegs = GPR64ArgRegsWin64;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001664 } else {
1665 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1666 GPR64ArgRegs = GPR64ArgRegs64Bit;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001667
1668 NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, TotalNumXMMRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001669 }
1670 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1671 TotalNumIntRegs);
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001672
Devang Patel578efa92009-06-05 21:57:13 +00001673 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001674 assert(!(NumXMMRegs && !Subtarget->hasXMM()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001675 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001676 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001677 "SSE register cannot be used when SSE is disabled!");
Nate Begeman2ea8ee72010-12-10 00:26:57 +00001678 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasXMM())
Torok Edwin3f142c32009-02-01 18:15:56 +00001679 // Kernel mode asks for SSE to be disabled, so don't push them
1680 // on the stack.
1681 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001682
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001683 if (IsWin64) {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001684 const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering();
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001685 // Get to the caller-allocated home save location. Add 8 to account
1686 // for the return address.
1687 int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001688 FuncInfo->setRegSaveFrameIndex(
Cameron Esfahaniec37b002010-10-08 19:24:18 +00001689 MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001690 FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
1691 } else {
1692 // For X86-64, if there are vararg parameters that are passed via
1693 // registers, then we must store them to their spots on the stack so they
1694 // may be loaded by deferencing the result of va_next.
1695 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1696 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1697 FuncInfo->setRegSaveFrameIndex(
1698 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
Dan Gohman1e93df62010-04-17 14:41:14 +00001699 false));
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001700 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001701
Gordon Henriksen86737662008-01-05 16:56:59 +00001702 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001703 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001704 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1705 getPointerTy());
1706 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001707 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001708 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1709 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001710 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001711 X86::GR64RegisterClass, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001713 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001714 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00001715 MachinePointerInfo::getFixedStack(
1716 FuncInfo->getRegSaveFrameIndex(), Offset),
1717 false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001718 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001719 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001720 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001721
Dan Gohmanface41a2009-08-16 21:24:25 +00001722 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1723 // Now store the XMM (fp + vector) parameter registers.
1724 SmallVector<SDValue, 11> SaveXMMOps;
1725 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001726
Devang Patele9a7ea62011-01-31 21:38:14 +00001727 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001728 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1729 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001730
Dan Gohman1e93df62010-04-17 14:41:14 +00001731 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1732 FuncInfo->getRegSaveFrameIndex()));
1733 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1734 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001735
Dan Gohmanface41a2009-08-16 21:24:25 +00001736 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Anton Korobeynikove7beda12010-10-03 22:52:07 +00001737 unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs],
Devang Patele9a7ea62011-01-31 21:38:14 +00001738 X86::VR128RegisterClass, dl);
Dan Gohmanface41a2009-08-16 21:24:25 +00001739 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1740 SaveXMMOps.push_back(Val);
1741 }
1742 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1743 MVT::Other,
1744 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001745 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001746
1747 if (!MemOps.empty())
1748 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1749 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001750 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001751 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001752
Gordon Henriksen86737662008-01-05 16:56:59 +00001753 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001754 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001755 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001756 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001757 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001758 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001759 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001760 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001761 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001762
Gordon Henriksen86737662008-01-05 16:56:59 +00001763 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001764 // RegSaveFrameIndex is X86-64 only.
1765 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001766 if (CallConv == CallingConv::X86_FastCall ||
1767 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001768 // fastcc functions can't have varargs.
1769 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 }
Evan Cheng25caf632006-05-23 21:06:34 +00001771
Dan Gohman98ca4f22009-08-05 01:29:28 +00001772 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001773}
1774
Dan Gohman475871a2008-07-27 21:46:04 +00001775SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001776X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1777 SDValue StackPtr, SDValue Arg,
1778 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001779 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001780 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovc7c62bb2010-09-02 22:31:32 +00001781 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
1782 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001783 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001784 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001785 if (Flags.isByVal())
Dale Johannesendd64c412009-02-04 00:33:20 +00001786 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Chris Lattnerfc448ff2010-09-21 18:51:21 +00001787
1788 return DAG.getStore(Chain, dl, Arg, PtrOff,
1789 MachinePointerInfo::getStack(LocMemOffset),
David Greene67c9d422010-02-15 16:53:33 +00001790 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001791}
1792
Bill Wendling64e87322009-01-16 19:25:27 +00001793/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001794/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001795SDValue
1796X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001797 SDValue &OutRetAddr, SDValue Chain,
1798 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001799 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001800 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001801 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001802 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001803
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001804 // Load the "old" Return address.
Chris Lattner51abfe42010-09-21 06:02:19 +00001805 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
1806 false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001807 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001808}
1809
1810/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1811/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001812static SDValue
1813EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001815 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001816 // Store the return address to the appropriate stack slot.
1817 if (!FPDiff) return Chain;
1818 // Calculate the new stack slot for the return address.
1819 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001820 int NewReturnAddrFI =
Evan Chenged2ae132010-07-03 00:40:23 +00001821 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001822 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001823 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001824 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00001825 MachinePointerInfo::getFixedStack(NewReturnAddrFI),
David Greene67c9d422010-02-15 16:53:33 +00001826 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001827 return Chain;
1828}
1829
Dan Gohman98ca4f22009-08-05 01:29:28 +00001830SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001831X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001832 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001833 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001834 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00001835 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001836 const SmallVectorImpl<ISD::InputArg> &Ins,
1837 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001838 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001839 MachineFunction &MF = DAG.getMachineFunction();
1840 bool Is64Bit = Subtarget->is64Bit();
1841 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001842 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001843
Evan Cheng5f941932010-02-05 02:21:12 +00001844 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001845 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001846 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1847 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Dan Gohmanc9403652010-07-07 15:54:55 +00001848 Outs, OutVals, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001849
1850 // Sibcalls are automatically detected tailcalls which do not require
1851 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001852 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001853 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001854
1855 if (isTailCall)
1856 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001857 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001858
Chris Lattner29689432010-03-11 00:22:57 +00001859 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1860 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001861
Chris Lattner638402b2007-02-28 07:00:42 +00001862 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001863 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001864 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1865 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00001866 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001867
Chris Lattner423c5f42007-02-28 05:31:48 +00001868 // Get a count of how many bytes are to be pushed on the stack.
1869 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001870 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001871 // This is a sibcall. The memory operands are available in caller's
1872 // own caller's stack.
1873 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001874 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001875 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001876
Gordon Henriksen86737662008-01-05 16:56:59 +00001877 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001878 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001879 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001880 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001881 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1882 FPDiff = NumBytesCallerPushed - NumBytes;
1883
1884 // Set the delta of movement of the returnaddr stackslot.
1885 // But only set if delta is greater than previous delta.
1886 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1887 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1888 }
1889
Evan Chengf22f9b32010-02-06 03:28:46 +00001890 if (!IsSibcall)
1891 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001892
Dan Gohman475871a2008-07-27 21:46:04 +00001893 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001894 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001895 if (isTailCall && FPDiff)
1896 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1897 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001898
Dan Gohman475871a2008-07-27 21:46:04 +00001899 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1900 SmallVector<SDValue, 8> MemOpChains;
1901 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001902
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001903 // Walk the register/memloc assignments, inserting copies/loads. In the case
1904 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001905 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1906 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001907 EVT RegVT = VA.getLocVT();
Dan Gohmanc9403652010-07-07 15:54:55 +00001908 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00001909 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001910 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001911
Chris Lattner423c5f42007-02-28 05:31:48 +00001912 // Promote the value if needed.
1913 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001914 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001915 case CCValAssign::Full: break;
1916 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001917 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001918 break;
1919 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001920 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001921 break;
1922 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001923 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1924 // Special case: passing MMX values in XMM registers.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001925 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg);
Owen Anderson825b72b2009-08-11 20:47:22 +00001926 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1927 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001928 } else
1929 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1930 break;
1931 case CCValAssign::BCvt:
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001932 Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001933 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001934 case CCValAssign::Indirect: {
1935 // Store the argument.
1936 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001937 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001938 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00001939 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00001940 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001941 Arg = SpillSlot;
1942 break;
1943 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001944 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001945
Chris Lattner423c5f42007-02-28 05:31:48 +00001946 if (VA.isRegLoc()) {
1947 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Anton Korobeynikovc52bedb2010-08-27 14:43:06 +00001948 if (isVarArg && Subtarget->isTargetWin64()) {
1949 // Win64 ABI requires argument XMM reg to be copied to the corresponding
1950 // shadow reg if callee is a varargs function.
1951 unsigned ShadowReg = 0;
1952 switch (VA.getLocReg()) {
1953 case X86::XMM0: ShadowReg = X86::RCX; break;
1954 case X86::XMM1: ShadowReg = X86::RDX; break;
1955 case X86::XMM2: ShadowReg = X86::R8; break;
1956 case X86::XMM3: ShadowReg = X86::R9; break;
1957 }
1958 if (ShadowReg)
1959 RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
1960 }
Evan Chengf22f9b32010-02-06 03:28:46 +00001961 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001962 assert(VA.isMemLoc());
1963 if (StackPtr.getNode() == 0)
1964 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1965 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1966 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001967 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001968 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001969
Evan Cheng32fe1032006-05-25 00:59:30 +00001970 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001971 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001972 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001973
Evan Cheng347d5f72006-04-28 21:29:37 +00001974 // Build a sequence of copy-to-reg nodes chained together with token chain
1975 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001976 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001977 // Tail call byval lowering might overwrite argument registers so in case of
1978 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001979 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001980 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001981 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001982 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001983 InFlag = Chain.getValue(1);
1984 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001985
Chris Lattner88e1fd52009-07-09 04:24:46 +00001986 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001987 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1988 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001989 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001990 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1991 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001992 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001993 InFlag);
1994 InFlag = Chain.getValue(1);
1995 } else {
1996 // If we are tail calling and generating PIC/GOT style code load the
1997 // address of the callee into ECX. The value in ecx is used as target of
1998 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1999 // for tail calls on PIC/GOT architectures. Normally we would just put the
2000 // address of GOT into ebx and then call target@PLT. But for tail calls
2001 // ebx would be restored (since ebx is callee saved) before jumping to the
2002 // target@PLT.
2003
2004 // Note: The actual moving to ECX is done further down.
2005 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
2006 if (G && !G->getGlobal()->hasHiddenVisibility() &&
2007 !G->getGlobal()->hasProtectedVisibility())
2008 Callee = LowerGlobalAddress(Callee, DAG);
2009 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00002010 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00002011 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002012 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00002013
Nate Begemanc8ea6732010-07-21 20:49:52 +00002014 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64()) {
Gordon Henriksen86737662008-01-05 16:56:59 +00002015 // From AMD64 ABI document:
2016 // For calls that may call functions that use varargs or stdargs
2017 // (prototype-less calls or calls to functions containing ellipsis (...) in
2018 // the declaration) %al is used as hidden argument to specify the number
2019 // of SSE registers used. The contents of %al do not need to match exactly
2020 // the number of registers, but must be an ubound on the number of SSE
2021 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00002022
Gordon Henriksen86737662008-01-05 16:56:59 +00002023 // Count the number of XMM registers allocated.
2024 static const unsigned XMMArgRegs[] = {
2025 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2026 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2027 };
2028 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Nate Begeman2ea8ee72010-12-10 00:26:57 +00002029 assert((Subtarget->hasXMM() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00002030 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00002031
Dale Johannesendd64c412009-02-04 00:33:20 +00002032 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00002033 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002034 InFlag = Chain.getValue(1);
2035 }
2036
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002037
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002038 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002039 if (isTailCall) {
2040 // Force all the incoming stack arguments to be loaded from the stack
2041 // before any new outgoing arguments are stored to the stack, because the
2042 // outgoing stack slots may alias the incoming argument stack slots, and
2043 // the alias isn't otherwise explicit. This is slightly more conservative
2044 // than necessary, because it means that each store effectively depends
2045 // on every argument instead of just those arguments it would clobber.
2046 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
2047
Dan Gohman475871a2008-07-27 21:46:04 +00002048 SmallVector<SDValue, 8> MemOpChains2;
2049 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00002050 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00002051 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00002052 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00002053 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00002054 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2055 CCValAssign &VA = ArgLocs[i];
2056 if (VA.isRegLoc())
2057 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002058 assert(VA.isMemLoc());
Dan Gohmanc9403652010-07-07 15:54:55 +00002059 SDValue Arg = OutVals[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +00002060 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002061 // Create frame index.
2062 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002063 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Evan Chenged2ae132010-07-03 00:40:23 +00002064 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002065 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002066
Duncan Sands276dcbd2008-03-21 09:14:45 +00002067 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002068 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002069 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002070 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002071 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002072 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002073 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002074
Dan Gohman98ca4f22009-08-05 01:29:28 +00002075 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2076 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002077 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002078 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002079 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002080 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002081 DAG.getStore(ArgChain, dl, Arg, FIN,
Chris Lattnere8639032010-09-21 06:22:23 +00002082 MachinePointerInfo::getFixedStack(FI),
David Greene67c9d422010-02-15 16:53:33 +00002083 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002084 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002085 }
2086 }
2087
2088 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002089 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002090 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002091
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002092 // Copy arguments to their registers.
2093 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002094 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002095 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002096 InFlag = Chain.getValue(1);
2097 }
Dan Gohman475871a2008-07-27 21:46:04 +00002098 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002099
Gordon Henriksen86737662008-01-05 16:56:59 +00002100 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002101 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002102 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002103 }
2104
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002105 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2106 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2107 // In the 64-bit large code model, we have to make all calls
2108 // through a register, since the call instruction's 32-bit
2109 // pc-relative offset may not be large enough to hold the whole
2110 // address.
2111 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002112 // If the callee is a GlobalAddress node (quite common, every direct call
2113 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2114 // it.
2115
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002116 // We should use extra load for direct calls to dllimported functions in
2117 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002118 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002119 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002120 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002121
Chris Lattner48a7d022009-07-09 05:02:21 +00002122 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2123 // external symbols most go through the PLT in PIC mode. If the symbol
2124 // has hidden or protected visibility, or if it is static or local, then
2125 // we don't need to use the PLT - we can directly call it.
2126 if (Subtarget->isTargetELF() &&
2127 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002128 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002129 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002130 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner80945782010-09-27 06:34:01 +00002131 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2132 Subtarget->getDarwinVers() < 9) {
Chris Lattner74e726e2009-07-09 05:27:35 +00002133 // PC-relative references to external symbols should go through $stub,
2134 // unless we're building with the leopard linker or later, which
2135 // automatically synthesizes these stubs.
2136 OpFlags = X86II::MO_DARWIN_STUB;
2137 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002138
Devang Patel0d881da2010-07-06 22:08:15 +00002139 Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002140 G->getOffset(), OpFlags);
2141 }
Bill Wendling056292f2008-09-16 21:48:12 +00002142 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002143 unsigned char OpFlags = 0;
2144
Evan Cheng1bf891a2010-12-01 22:59:46 +00002145 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
2146 // external symbols should go through the PLT.
2147 if (Subtarget->isTargetELF() &&
2148 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
2149 OpFlags = X86II::MO_PLT;
2150 } else if (Subtarget->isPICStyleStubAny() &&
2151 Subtarget->getDarwinVers() < 9) {
2152 // PC-relative references to external symbols should go through $stub,
2153 // unless we're building with the leopard linker or later, which
2154 // automatically synthesizes these stubs.
2155 OpFlags = X86II::MO_DARWIN_STUB;
Chris Lattner74e726e2009-07-09 05:27:35 +00002156 }
Eric Christopherfd179292009-08-27 18:07:15 +00002157
Chris Lattner48a7d022009-07-09 05:02:21 +00002158 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2159 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002160 }
2161
Chris Lattnerd96d0722007-02-25 06:40:16 +00002162 // Returns a chain & a flag for retval copy to use.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00002163 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +00002164 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002165
Evan Chengf22f9b32010-02-06 03:28:46 +00002166 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002167 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2168 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002169 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002170 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002171
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002172 Ops.push_back(Chain);
2173 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002174
Dan Gohman98ca4f22009-08-05 01:29:28 +00002175 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002176 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002177
Gordon Henriksen86737662008-01-05 16:56:59 +00002178 // Add argument registers to the end of the list so that they are known live
2179 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002180 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2181 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2182 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002183
Evan Cheng586ccac2008-03-18 23:36:35 +00002184 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002185 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002186 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2187
Anton Korobeynikov3a1e54a2010-08-17 21:06:07 +00002188 // Add an implicit use of AL for non-Windows x86 64-bit vararg functions.
2189 if (Is64Bit && isVarArg && !Subtarget->isTargetWin64())
Owen Anderson825b72b2009-08-11 20:47:22 +00002190 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002191
Gabor Greifba36cb52008-08-28 21:40:38 +00002192 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002193 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002194
Dan Gohman98ca4f22009-08-05 01:29:28 +00002195 if (isTailCall) {
Dale Johannesen88004c22010-06-05 00:30:45 +00002196 // We used to do:
2197 //// If this is the first return lowered for this function, add the regs
2198 //// to the liveout set for the function.
2199 // This isn't right, although it's probably harmless on x86; liveouts
2200 // should be computed from returns not tail calls. Consider a void
2201 // function making a tail call to a function returning int.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002202 return DAG.getNode(X86ISD::TC_RETURN, dl,
2203 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002204 }
2205
Dale Johannesenace16102009-02-03 19:33:06 +00002206 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002207 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002208
Chris Lattner2d297092006-05-23 18:50:38 +00002209 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002210 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002211 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002212 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002213 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002214 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002215 // pops the hidden struct pointer, so we have to push it back.
2216 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002217 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002218 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002219 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002220
Gordon Henriksenae636f82008-01-03 16:47:34 +00002221 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002222 if (!IsSibcall) {
2223 Chain = DAG.getCALLSEQ_END(Chain,
2224 DAG.getIntPtrConstant(NumBytes, true),
2225 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2226 true),
2227 InFlag);
2228 InFlag = Chain.getValue(1);
2229 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002230
Chris Lattner3085e152007-02-25 08:59:22 +00002231 // Handle result values, copying them out of physregs into vregs that we
2232 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002233 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2234 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002235}
2236
Evan Cheng25ab6902006-09-08 06:48:29 +00002237
2238//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002239// Fast Calling Convention (tail call) implementation
2240//===----------------------------------------------------------------------===//
2241
2242// Like std call, callee cleans arguments, convention except that ECX is
2243// reserved for storing the tail called function address. Only 2 registers are
2244// free for argument passing (inreg). Tail call optimization is performed
2245// provided:
2246// * tailcallopt is enabled
2247// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002248// On X86_64 architecture with GOT-style position independent code only local
2249// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002250// To keep the stack aligned according to platform abi the function
2251// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2252// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002253// If a tail called function callee has more arguments than the caller the
2254// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002255// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002256// original REtADDR, but before the saved framepointer or the spilled registers
2257// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2258// stack layout:
2259// arg1
2260// arg2
2261// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002262// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002263// move area ]
2264// (possible EBP)
2265// ESI
2266// EDI
2267// local1 ..
2268
2269/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2270/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002271unsigned
2272X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2273 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002274 MachineFunction &MF = DAG.getMachineFunction();
2275 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00002276 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002277 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002278 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002279 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002280 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002281 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2282 // Number smaller than 12 so just add the difference.
2283 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2284 } else {
2285 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002286 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002287 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002288 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002289 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002290}
2291
Evan Cheng5f941932010-02-05 02:21:12 +00002292/// MatchingStackOffset - Return true if the given stack call argument is
2293/// already available in the same position (relatively) of the caller's
2294/// incoming argument stack.
2295static
2296bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2297 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2298 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002299 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2300 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002301 if (Arg.getOpcode() == ISD::CopyFromReg) {
2302 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +00002303 if (!TargetRegisterInfo::isVirtualRegister(VR))
Evan Cheng5f941932010-02-05 02:21:12 +00002304 return false;
2305 MachineInstr *Def = MRI->getVRegDef(VR);
2306 if (!Def)
2307 return false;
2308 if (!Flags.isByVal()) {
2309 if (!TII->isLoadFromStackSlot(Def, FI))
2310 return false;
2311 } else {
2312 unsigned Opcode = Def->getOpcode();
2313 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2314 Def->getOperand(1).isFI()) {
2315 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002316 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002317 } else
2318 return false;
2319 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002320 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2321 if (Flags.isByVal())
2322 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002323 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002324 // define @foo(%struct.X* %A) {
2325 // tail call @bar(%struct.X* byval %A)
2326 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002327 return false;
2328 SDValue Ptr = Ld->getBasePtr();
2329 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2330 if (!FINode)
2331 return false;
2332 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002333 } else
2334 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002335
Evan Cheng4cae1332010-03-05 08:38:04 +00002336 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002337 if (!MFI->isFixedObjectIndex(FI))
2338 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002339 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002340}
2341
Dan Gohman98ca4f22009-08-05 01:29:28 +00002342/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2343/// for tail call optimization. Targets which want to do tail call
2344/// optimization should implement this function.
2345bool
2346X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002347 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002348 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002349 bool isCalleeStructRet,
2350 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002351 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +00002352 const SmallVectorImpl<SDValue> &OutVals,
Evan Chengb1712452010-01-27 06:25:16 +00002353 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002354 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002355 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002356 CalleeCC != CallingConv::C)
2357 return false;
2358
Evan Cheng7096ae42010-01-29 06:45:59 +00002359 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002360 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002361 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002362 CallingConv::ID CallerCC = CallerF->getCallingConv();
2363 bool CCMatch = CallerCC == CalleeCC;
2364
Dan Gohman1797ed52010-02-08 20:27:50 +00002365 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002366 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002367 return true;
2368 return false;
2369 }
2370
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002371 // Look for obvious safe cases to perform tail call optimization that do not
2372 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002373
Evan Cheng2c12cb42010-03-26 16:26:03 +00002374 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2375 // emit a special epilogue.
2376 if (RegInfo->needsStackRealignment(MF))
2377 return false;
2378
Eric Christopher90eb4022010-07-22 00:26:08 +00002379 // Do not sibcall optimize vararg calls unless the call site is not passing
2380 // any arguments.
Evan Cheng3c262ee2010-03-26 02:13:13 +00002381 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002382 return false;
2383
Evan Chenga375d472010-03-15 18:54:48 +00002384 // Also avoid sibcall optimization if either caller or callee uses struct
2385 // return semantics.
2386 if (isCalleeStructRet || isCallerStructRet)
2387 return false;
2388
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002389 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2390 // Therefore if it's not used by the call it is not safe to optimize this into
2391 // a sibcall.
2392 bool Unused = false;
2393 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2394 if (!Ins[i].Used) {
2395 Unused = true;
2396 break;
2397 }
2398 }
2399 if (Unused) {
2400 SmallVector<CCValAssign, 16> RVLocs;
2401 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2402 RVLocs, *DAG.getContext());
2403 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002404 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002405 CCValAssign &VA = RVLocs[i];
2406 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2407 return false;
2408 }
2409 }
2410
Evan Cheng13617962010-04-30 01:12:32 +00002411 // If the calling conventions do not match, then we'd better make sure the
2412 // results are returned in the same way as what the caller expects.
2413 if (!CCMatch) {
2414 SmallVector<CCValAssign, 16> RVLocs1;
2415 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2416 RVLocs1, *DAG.getContext());
2417 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2418
2419 SmallVector<CCValAssign, 16> RVLocs2;
2420 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2421 RVLocs2, *DAG.getContext());
2422 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2423
2424 if (RVLocs1.size() != RVLocs2.size())
2425 return false;
2426 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2427 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2428 return false;
2429 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2430 return false;
2431 if (RVLocs1[i].isRegLoc()) {
2432 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2433 return false;
2434 } else {
2435 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2436 return false;
2437 }
2438 }
2439 }
2440
Evan Chenga6bff982010-01-30 01:22:00 +00002441 // If the callee takes no arguments then go on to check the results of the
2442 // call.
2443 if (!Outs.empty()) {
2444 // Check if stack adjustment is needed. For now, do not do this if any
2445 // argument is passed on the stack.
2446 SmallVector<CCValAssign, 16> ArgLocs;
2447 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2448 ArgLocs, *DAG.getContext());
Duncan Sands45907662010-10-31 13:21:44 +00002449 CCInfo.AnalyzeCallOperands(Outs, CC_X86);
Evan Chengb2c92902010-02-02 02:22:50 +00002450 if (CCInfo.getNextStackOffset()) {
2451 MachineFunction &MF = DAG.getMachineFunction();
2452 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2453 return false;
Evan Chengb2c92902010-02-02 02:22:50 +00002454
2455 // Check if the arguments are already laid out in the right way as
2456 // the caller's fixed stack objects.
2457 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002458 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2459 const X86InstrInfo *TII =
2460 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002461 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2462 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +00002463 SDValue Arg = OutVals[i];
Evan Chengb2c92902010-02-02 02:22:50 +00002464 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002465 if (VA.getLocInfo() == CCValAssign::Indirect)
2466 return false;
2467 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002468 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2469 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002470 return false;
2471 }
2472 }
2473 }
Evan Cheng9c044672010-05-29 01:35:22 +00002474
2475 // If the tailcall address may be in a register, then make sure it's
2476 // possible to register allocate for it. In 32-bit, the call address can
2477 // only target EAX, EDX, or ECX since the tail call must be scheduled after
Evan Chengdedd9742010-07-14 06:44:01 +00002478 // callee-saved registers are restored. These happen to be the same
2479 // registers used to pass 'inreg' arguments so watch out for those.
2480 if (!Subtarget->is64Bit() &&
2481 !isa<GlobalAddressSDNode>(Callee) &&
Evan Cheng9c044672010-05-29 01:35:22 +00002482 !isa<ExternalSymbolSDNode>(Callee)) {
Evan Cheng9c044672010-05-29 01:35:22 +00002483 unsigned NumInRegs = 0;
2484 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2485 CCValAssign &VA = ArgLocs[i];
Evan Chengdedd9742010-07-14 06:44:01 +00002486 if (!VA.isRegLoc())
2487 continue;
2488 unsigned Reg = VA.getLocReg();
2489 switch (Reg) {
2490 default: break;
2491 case X86::EAX: case X86::EDX: case X86::ECX:
2492 if (++NumInRegs == 3)
Evan Cheng9c044672010-05-29 01:35:22 +00002493 return false;
Evan Chengdedd9742010-07-14 06:44:01 +00002494 break;
Evan Cheng9c044672010-05-29 01:35:22 +00002495 }
2496 }
2497 }
Evan Chenga6bff982010-01-30 01:22:00 +00002498 }
Evan Chengb1712452010-01-27 06:25:16 +00002499
Dale Johannesend155d7e2010-10-25 22:17:05 +00002500 // An stdcall caller is expected to clean up its arguments; the callee
Dale Johannesen0e034562010-11-12 00:43:18 +00002501 // isn't going to do that.
Dale Johannesend155d7e2010-10-25 22:17:05 +00002502 if (!CCMatch && CallerCC==CallingConv::X86_StdCall)
2503 return false;
2504
Evan Cheng86809cc2010-02-03 03:28:02 +00002505 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002506}
2507
Dan Gohman3df24e62008-09-03 23:12:08 +00002508FastISel *
Dan Gohmana4160c32010-07-07 16:29:44 +00002509X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo) const {
2510 return X86::createFastISel(funcInfo);
Dan Gohmand9f3c482008-08-19 21:32:53 +00002511}
2512
2513
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002514//===----------------------------------------------------------------------===//
2515// Other Lowering Hooks
2516//===----------------------------------------------------------------------===//
2517
Bruno Cardoso Lopese654b562010-09-01 00:51:36 +00002518static bool MayFoldLoad(SDValue Op) {
2519 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
2520}
2521
2522static bool MayFoldIntoStore(SDValue Op) {
2523 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
2524}
2525
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002526static bool isTargetShuffle(unsigned Opcode) {
2527 switch(Opcode) {
2528 default: return false;
2529 case X86ISD::PSHUFD:
2530 case X86ISD::PSHUFHW:
2531 case X86ISD::PSHUFLW:
2532 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002533 case X86ISD::PALIGN:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002534 case X86ISD::SHUFPS:
2535 case X86ISD::MOVLHPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002536 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002537 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002538 case X86ISD::MOVLPS:
2539 case X86ISD::MOVLPD:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002540 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002541 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002542 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002543 case X86ISD::MOVSS:
2544 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002545 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002546 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002547 case X86ISD::PUNPCKLWD:
2548 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002549 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002550 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002551 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002552 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002553 case X86ISD::PUNPCKHWD:
2554 case X86ISD::PUNPCKHBW:
2555 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002556 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002557 return true;
2558 }
2559 return false;
2560}
2561
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002562static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002563 SDValue V1, SelectionDAG &DAG) {
2564 switch(Opc) {
2565 default: llvm_unreachable("Unknown x86 shuffle node");
2566 case X86ISD::MOVSHDUP:
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00002567 case X86ISD::MOVSLDUP:
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00002568 case X86ISD::MOVDDUP:
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00002569 return DAG.getNode(Opc, dl, VT, V1);
2570 }
2571
2572 return SDValue();
2573}
2574
2575static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00002576 SDValue V1, unsigned TargetMask, SelectionDAG &DAG) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002577 switch(Opc) {
2578 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002579 case X86ISD::PSHUFD:
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00002580 case X86ISD::PSHUFHW:
2581 case X86ISD::PSHUFLW:
2582 return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8));
2583 }
2584
2585 return SDValue();
2586}
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002587
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002588static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2589 SDValue V1, SDValue V2, unsigned TargetMask, SelectionDAG &DAG) {
2590 switch(Opc) {
2591 default: llvm_unreachable("Unknown x86 shuffle node");
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00002592 case X86ISD::PALIGN:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002593 case X86ISD::SHUFPD:
2594 case X86ISD::SHUFPS:
2595 return DAG.getNode(Opc, dl, VT, V1, V2,
2596 DAG.getConstant(TargetMask, MVT::i8));
2597 }
2598 return SDValue();
2599}
2600
2601static SDValue getTargetShuffleNode(unsigned Opc, DebugLoc dl, EVT VT,
2602 SDValue V1, SDValue V2, SelectionDAG &DAG) {
2603 switch(Opc) {
2604 default: llvm_unreachable("Unknown x86 shuffle node");
2605 case X86ISD::MOVLHPS:
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00002606 case X86ISD::MOVLHPD:
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00002607 case X86ISD::MOVHLPS:
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00002608 case X86ISD::MOVLPS:
2609 case X86ISD::MOVLPD:
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00002610 case X86ISD::MOVSS:
2611 case X86ISD::MOVSD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002612 case X86ISD::UNPCKLPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002613 case X86ISD::UNPCKLPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002614 case X86ISD::PUNPCKLWD:
2615 case X86ISD::PUNPCKLBW:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002616 case X86ISD::PUNPCKLDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002617 case X86ISD::PUNPCKLQDQ:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002618 case X86ISD::UNPCKHPS:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002619 case X86ISD::UNPCKHPD:
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00002620 case X86ISD::PUNPCKHWD:
2621 case X86ISD::PUNPCKHBW:
2622 case X86ISD::PUNPCKHDQ:
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00002623 case X86ISD::PUNPCKHQDQ:
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00002624 return DAG.getNode(Opc, dl, VT, V1, V2);
2625 }
2626 return SDValue();
2627}
2628
Dan Gohmand858e902010-04-17 15:26:15 +00002629SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002630 MachineFunction &MF = DAG.getMachineFunction();
2631 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2632 int ReturnAddrIndex = FuncInfo->getRAIndex();
2633
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002634 if (ReturnAddrIndex == 0) {
2635 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002636 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002637 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Evan Chenged2ae132010-07-03 00:40:23 +00002638 false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002639 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002640 }
2641
Evan Cheng25ab6902006-09-08 06:48:29 +00002642 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002643}
2644
2645
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002646bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2647 bool hasSymbolicDisplacement) {
2648 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002649 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002650 return false;
2651
2652 // If we don't have a symbolic displacement - we don't have any extra
2653 // restrictions.
2654 if (!hasSymbolicDisplacement)
2655 return true;
2656
2657 // FIXME: Some tweaks might be needed for medium code model.
2658 if (M != CodeModel::Small && M != CodeModel::Kernel)
2659 return false;
2660
2661 // For small code model we assume that latest object is 16MB before end of 31
2662 // bits boundary. We may also accept pretty large negative constants knowing
2663 // that all objects are in the positive half of address space.
2664 if (M == CodeModel::Small && Offset < 16*1024*1024)
2665 return true;
2666
2667 // For kernel code model we know that all object resist in the negative half
2668 // of 32bits address space. We may not accept negative offsets, since they may
2669 // be just off and we may accept pretty large positive ones.
2670 if (M == CodeModel::Kernel && Offset > 0)
2671 return true;
2672
2673 return false;
2674}
2675
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002676/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2677/// specific condition code, returning the condition code and the LHS/RHS of the
2678/// comparison to make.
2679static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2680 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002681 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002682 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2683 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2684 // X > -1 -> X == 0, jump !sign.
2685 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002686 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002687 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2688 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002689 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002690 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002691 // X < 1 -> X <= 0
2692 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002693 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002694 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002695 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002696
Evan Chengd9558e02006-01-06 00:43:03 +00002697 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002698 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002699 case ISD::SETEQ: return X86::COND_E;
2700 case ISD::SETGT: return X86::COND_G;
2701 case ISD::SETGE: return X86::COND_GE;
2702 case ISD::SETLT: return X86::COND_L;
2703 case ISD::SETLE: return X86::COND_LE;
2704 case ISD::SETNE: return X86::COND_NE;
2705 case ISD::SETULT: return X86::COND_B;
2706 case ISD::SETUGT: return X86::COND_A;
2707 case ISD::SETULE: return X86::COND_BE;
2708 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002709 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002710 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002711
Chris Lattner4c78e022008-12-23 23:42:27 +00002712 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002713
Chris Lattner4c78e022008-12-23 23:42:27 +00002714 // If LHS is a foldable load, but RHS is not, flip the condition.
Rafael Espindolaf297c932011-02-03 03:58:05 +00002715 if (ISD::isNON_EXTLoad(LHS.getNode()) &&
2716 !ISD::isNON_EXTLoad(RHS.getNode())) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002717 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2718 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002719 }
2720
Chris Lattner4c78e022008-12-23 23:42:27 +00002721 switch (SetCCOpcode) {
2722 default: break;
2723 case ISD::SETOLT:
2724 case ISD::SETOLE:
2725 case ISD::SETUGT:
2726 case ISD::SETUGE:
2727 std::swap(LHS, RHS);
2728 break;
2729 }
2730
2731 // On a floating point condition, the flags are set as follows:
2732 // ZF PF CF op
2733 // 0 | 0 | 0 | X > Y
2734 // 0 | 0 | 1 | X < Y
2735 // 1 | 0 | 0 | X == Y
2736 // 1 | 1 | 1 | unordered
2737 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002738 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002739 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002740 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002741 case ISD::SETOLT: // flipped
2742 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002743 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002744 case ISD::SETOLE: // flipped
2745 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002746 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002747 case ISD::SETUGT: // flipped
2748 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002749 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002750 case ISD::SETUGE: // flipped
2751 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002752 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002753 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002754 case ISD::SETNE: return X86::COND_NE;
2755 case ISD::SETUO: return X86::COND_P;
2756 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002757 case ISD::SETOEQ:
2758 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002759 }
Evan Chengd9558e02006-01-06 00:43:03 +00002760}
2761
Evan Cheng4a460802006-01-11 00:33:36 +00002762/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2763/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002764/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002765static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002766 switch (X86CC) {
2767 default:
2768 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002769 case X86::COND_B:
2770 case X86::COND_BE:
2771 case X86::COND_E:
2772 case X86::COND_P:
2773 case X86::COND_A:
2774 case X86::COND_AE:
2775 case X86::COND_NE:
2776 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002777 return true;
2778 }
2779}
2780
Evan Chengeb2f9692009-10-27 19:56:55 +00002781/// isFPImmLegal - Returns true if the target can instruction select the
2782/// specified FP immediate natively. If false, the legalizer will
2783/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002784bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002785 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2786 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2787 return true;
2788 }
2789 return false;
2790}
2791
Nate Begeman9008ca62009-04-27 18:41:29 +00002792/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2793/// the specified range (L, H].
2794static bool isUndefOrInRange(int Val, int Low, int Hi) {
2795 return (Val < 0) || (Val >= Low && Val < Hi);
2796}
2797
2798/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2799/// specified value.
2800static bool isUndefOrEqual(int Val, int CmpVal) {
2801 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002802 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002803 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002804}
2805
Nate Begeman9008ca62009-04-27 18:41:29 +00002806/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2807/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2808/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002809static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00002810 if (VT == MVT::v4f32 || VT == MVT::v4i32 )
Nate Begeman9008ca62009-04-27 18:41:29 +00002811 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002812 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002813 return (Mask[0] < 2 && Mask[1] < 2);
2814 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002815}
2816
Nate Begeman9008ca62009-04-27 18:41:29 +00002817bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002818 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002819 N->getMask(M);
2820 return ::isPSHUFDMask(M, N->getValueType(0));
2821}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002822
Nate Begeman9008ca62009-04-27 18:41:29 +00002823/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2824/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002825static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002826 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002827 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002828
Nate Begeman9008ca62009-04-27 18:41:29 +00002829 // Lower quadword copied in order or undef.
2830 for (int i = 0; i != 4; ++i)
2831 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002832 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002833
Evan Cheng506d3df2006-03-29 23:07:14 +00002834 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002835 for (int i = 4; i != 8; ++i)
2836 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002837 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002838
Evan Cheng506d3df2006-03-29 23:07:14 +00002839 return true;
2840}
2841
Nate Begeman9008ca62009-04-27 18:41:29 +00002842bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002843 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002844 N->getMask(M);
2845 return ::isPSHUFHWMask(M, N->getValueType(0));
2846}
Evan Cheng506d3df2006-03-29 23:07:14 +00002847
Nate Begeman9008ca62009-04-27 18:41:29 +00002848/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2849/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002850static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002851 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002852 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002853
Rafael Espindola15684b22009-04-24 12:40:33 +00002854 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002855 for (int i = 4; i != 8; ++i)
2856 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002857 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002858
Rafael Espindola15684b22009-04-24 12:40:33 +00002859 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002860 for (int i = 0; i != 4; ++i)
2861 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002862 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002863
Rafael Espindola15684b22009-04-24 12:40:33 +00002864 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002865}
2866
Nate Begeman9008ca62009-04-27 18:41:29 +00002867bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002868 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002869 N->getMask(M);
2870 return ::isPSHUFLWMask(M, N->getValueType(0));
2871}
2872
Nate Begemana09008b2009-10-19 02:17:23 +00002873/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2874/// is suitable for input to PALIGNR.
2875static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2876 bool hasSSSE3) {
2877 int i, e = VT.getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002878
Nate Begemana09008b2009-10-19 02:17:23 +00002879 // Do not handle v2i64 / v2f64 shuffles with palignr.
2880 if (e < 4 || !hasSSSE3)
2881 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002882
Nate Begemana09008b2009-10-19 02:17:23 +00002883 for (i = 0; i != e; ++i)
2884 if (Mask[i] >= 0)
2885 break;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002886
Nate Begemana09008b2009-10-19 02:17:23 +00002887 // All undef, not a palignr.
2888 if (i == e)
2889 return false;
2890
2891 // Determine if it's ok to perform a palignr with only the LHS, since we
2892 // don't have access to the actual shuffle elements to see if RHS is undef.
2893 bool Unary = Mask[i] < (int)e;
2894 bool NeedsUnary = false;
2895
2896 int s = Mask[i] - i;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002897
Nate Begemana09008b2009-10-19 02:17:23 +00002898 // Check the rest of the elements to see if they are consecutive.
2899 for (++i; i != e; ++i) {
2900 int m = Mask[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00002901 if (m < 0)
Nate Begemana09008b2009-10-19 02:17:23 +00002902 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002903
Nate Begemana09008b2009-10-19 02:17:23 +00002904 Unary = Unary && (m < (int)e);
2905 NeedsUnary = NeedsUnary || (m < s);
2906
2907 if (NeedsUnary && !Unary)
2908 return false;
2909 if (Unary && m != ((s+i) & (e-1)))
2910 return false;
2911 if (!Unary && m != (s+i))
2912 return false;
2913 }
2914 return true;
2915}
2916
2917bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2918 SmallVector<int, 8> M;
2919 N->getMask(M);
2920 return ::isPALIGNRMask(M, N->getValueType(0), true);
2921}
2922
Evan Cheng14aed5e2006-03-24 01:18:28 +00002923/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2924/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002925static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002926 int NumElems = VT.getVectorNumElements();
2927 if (NumElems != 2 && NumElems != 4)
2928 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002929
Nate Begeman9008ca62009-04-27 18:41:29 +00002930 int Half = NumElems / 2;
2931 for (int i = 0; i < Half; ++i)
2932 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002933 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002934 for (int i = Half; i < NumElems; ++i)
2935 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002936 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002937
Evan Cheng14aed5e2006-03-24 01:18:28 +00002938 return true;
2939}
2940
Nate Begeman9008ca62009-04-27 18:41:29 +00002941bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2942 SmallVector<int, 8> M;
2943 N->getMask(M);
2944 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002945}
2946
Evan Cheng213d2cf2007-05-17 18:45:50 +00002947/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002948/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2949/// half elements to come from vector 1 (which would equal the dest.) and
2950/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002951static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002952 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002953
2954 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002955 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002956
Nate Begeman9008ca62009-04-27 18:41:29 +00002957 int Half = NumElems / 2;
2958 for (int i = 0; i < Half; ++i)
2959 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002960 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002961 for (int i = Half; i < NumElems; ++i)
2962 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002963 return false;
2964 return true;
2965}
2966
Nate Begeman9008ca62009-04-27 18:41:29 +00002967static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2968 SmallVector<int, 8> M;
2969 N->getMask(M);
2970 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002971}
2972
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002973/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2974/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002975bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2976 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002977 return false;
2978
Evan Cheng2064a2b2006-03-28 06:50:32 +00002979 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002980 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2981 isUndefOrEqual(N->getMaskElt(1), 7) &&
2982 isUndefOrEqual(N->getMaskElt(2), 2) &&
2983 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002984}
2985
Nate Begeman0b10b912009-11-07 23:17:15 +00002986/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2987/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2988/// <2, 3, 2, 3>
2989bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2990 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Michael J. Spencerec38de22010-10-10 22:04:20 +00002991
Nate Begeman0b10b912009-11-07 23:17:15 +00002992 if (NumElems != 4)
2993 return false;
Michael J. Spencerec38de22010-10-10 22:04:20 +00002994
Nate Begeman0b10b912009-11-07 23:17:15 +00002995 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2996 isUndefOrEqual(N->getMaskElt(1), 3) &&
2997 isUndefOrEqual(N->getMaskElt(2), 2) &&
2998 isUndefOrEqual(N->getMaskElt(3), 3);
2999}
3000
Evan Cheng5ced1d82006-04-06 23:23:56 +00003001/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
3002/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00003003bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
3004 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003005
Evan Cheng5ced1d82006-04-06 23:23:56 +00003006 if (NumElems != 2 && NumElems != 4)
3007 return false;
3008
Evan Chengc5cdff22006-04-07 21:53:05 +00003009 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003010 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003011 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003012
Evan Chengc5cdff22006-04-07 21:53:05 +00003013 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003014 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003015 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003016
3017 return true;
3018}
3019
Nate Begeman0b10b912009-11-07 23:17:15 +00003020/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
3021/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
3022bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003023 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00003024
Evan Cheng5ced1d82006-04-06 23:23:56 +00003025 if (NumElems != 2 && NumElems != 4)
3026 return false;
3027
Evan Chengc5cdff22006-04-07 21:53:05 +00003028 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003029 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00003030 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003031
Nate Begeman9008ca62009-04-27 18:41:29 +00003032 for (unsigned i = 0; i < NumElems/2; ++i)
3033 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00003034 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003035
3036 return true;
3037}
3038
Evan Cheng0038e592006-03-28 00:39:58 +00003039/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
3040/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00003041static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003042 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003043 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003044 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00003045 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003046
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3048 int BitI = Mask[i];
3049 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003050 if (!isUndefOrEqual(BitI, j))
3051 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003052 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003053 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003054 return false;
3055 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003056 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003057 return false;
3058 }
Evan Cheng0038e592006-03-28 00:39:58 +00003059 }
Evan Cheng0038e592006-03-28 00:39:58 +00003060 return true;
3061}
3062
Nate Begeman9008ca62009-04-27 18:41:29 +00003063bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3064 SmallVector<int, 8> M;
3065 N->getMask(M);
3066 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003067}
3068
Evan Cheng4fcb9222006-03-28 02:43:26 +00003069/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
3070/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00003071static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00003072 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003073 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003074 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00003075 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003076
Nate Begeman9008ca62009-04-27 18:41:29 +00003077 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
3078 int BitI = Mask[i];
3079 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00003080 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00003081 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00003082 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00003083 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003084 return false;
3085 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00003086 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00003087 return false;
3088 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003089 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00003090 return true;
3091}
3092
Nate Begeman9008ca62009-04-27 18:41:29 +00003093bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
3094 SmallVector<int, 8> M;
3095 N->getMask(M);
3096 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00003097}
3098
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003099/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
3100/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
3101/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00003102static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003103 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003104 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003105 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003106
Nate Begeman9008ca62009-04-27 18:41:29 +00003107 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
3108 int BitI = Mask[i];
3109 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00003110 if (!isUndefOrEqual(BitI, j))
3111 return false;
3112 if (!isUndefOrEqual(BitI1, j))
3113 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003114 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003115 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003116}
3117
Nate Begeman9008ca62009-04-27 18:41:29 +00003118bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
3119 SmallVector<int, 8> M;
3120 N->getMask(M);
3121 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
3122}
3123
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003124/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
3125/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
3126/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00003127static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003128 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003129 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
3130 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003131
Nate Begeman9008ca62009-04-27 18:41:29 +00003132 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
3133 int BitI = Mask[i];
3134 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003135 if (!isUndefOrEqual(BitI, j))
3136 return false;
3137 if (!isUndefOrEqual(BitI1, j))
3138 return false;
3139 }
Rafael Espindola15684b22009-04-24 12:40:33 +00003140 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00003141}
3142
Nate Begeman9008ca62009-04-27 18:41:29 +00003143bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
3144 SmallVector<int, 8> M;
3145 N->getMask(M);
3146 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
3147}
3148
Evan Cheng017dcc62006-04-21 01:05:10 +00003149/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
3150/// specifies a shuffle of elements that is suitable for input to MOVSS,
3151/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00003152static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003153 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003154 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003155
3156 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003157
Nate Begeman9008ca62009-04-27 18:41:29 +00003158 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003159 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003160
Nate Begeman9008ca62009-04-27 18:41:29 +00003161 for (int i = 1; i < NumElts; ++i)
3162 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003163 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003164
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003165 return true;
3166}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003167
Nate Begeman9008ca62009-04-27 18:41:29 +00003168bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3169 SmallVector<int, 8> M;
3170 N->getMask(M);
3171 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003172}
3173
Evan Cheng017dcc62006-04-21 01:05:10 +00003174/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3175/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003176/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003177static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003178 bool V2IsSplat = false, bool V2IsUndef = false) {
3179 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003180 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003181 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003182
Nate Begeman9008ca62009-04-27 18:41:29 +00003183 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003184 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003185
Nate Begeman9008ca62009-04-27 18:41:29 +00003186 for (int i = 1; i < NumOps; ++i)
3187 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3188 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3189 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003190 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003191
Evan Cheng39623da2006-04-20 08:58:49 +00003192 return true;
3193}
3194
Nate Begeman9008ca62009-04-27 18:41:29 +00003195static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003196 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003197 SmallVector<int, 8> M;
3198 N->getMask(M);
3199 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003200}
3201
Evan Chengd9539472006-04-14 21:59:03 +00003202/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3203/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003204bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3205 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003206 return false;
3207
3208 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003209 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003210 int Elt = N->getMaskElt(i);
3211 if (Elt >= 0 && Elt != 1)
3212 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003213 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003214
3215 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003216 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 int Elt = N->getMaskElt(i);
3218 if (Elt >= 0 && Elt != 3)
3219 return false;
3220 if (Elt == 3)
3221 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003222 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003223 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003224 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003225 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003226}
3227
3228/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3229/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003230bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3231 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003232 return false;
3233
3234 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003235 for (unsigned i = 0; i < 2; ++i)
3236 if (N->getMaskElt(i) > 0)
3237 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003238
3239 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003240 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003241 int Elt = N->getMaskElt(i);
3242 if (Elt >= 0 && Elt != 2)
3243 return false;
3244 if (Elt == 2)
3245 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003246 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003247 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003248 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003249}
3250
Evan Cheng0b457f02008-09-25 20:50:48 +00003251/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3252/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003253bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3254 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003255
Nate Begeman9008ca62009-04-27 18:41:29 +00003256 for (int i = 0; i < e; ++i)
3257 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003258 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003259 for (int i = 0; i < e; ++i)
3260 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003261 return false;
3262 return true;
3263}
3264
David Greenec38a03e2011-02-03 15:50:00 +00003265/// isVEXTRACTF128Index - Return true if the specified
3266/// EXTRACT_SUBVECTOR operand specifies a vector extract that is
3267/// suitable for input to VEXTRACTF128.
3268bool X86::isVEXTRACTF128Index(SDNode *N) {
3269 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3270 return false;
3271
3272 // The index should be aligned on a 128-bit boundary.
3273 uint64_t Index =
3274 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3275
3276 unsigned VL = N->getValueType(0).getVectorNumElements();
3277 unsigned VBits = N->getValueType(0).getSizeInBits();
3278 unsigned ElSize = VBits / VL;
3279 bool Result = (Index * ElSize) % 128 == 0;
3280
3281 return Result;
3282}
3283
Evan Cheng63d33002006-03-22 08:01:21 +00003284/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003285/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003286unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3288 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3289
Evan Chengb9df0ca2006-03-22 02:53:00 +00003290 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3291 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003292 for (int i = 0; i < NumOperands; ++i) {
3293 int Val = SVOp->getMaskElt(NumOperands-i-1);
3294 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003295 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003296 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003297 if (i != NumOperands - 1)
3298 Mask <<= Shift;
3299 }
Evan Cheng63d33002006-03-22 08:01:21 +00003300 return Mask;
3301}
3302
Evan Cheng506d3df2006-03-29 23:07:14 +00003303/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003304/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003305unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003306 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003307 unsigned Mask = 0;
3308 // 8 nodes, but we only care about the last 4.
3309 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003310 int Val = SVOp->getMaskElt(i);
3311 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003312 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003313 if (i != 4)
3314 Mask <<= 2;
3315 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003316 return Mask;
3317}
3318
3319/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003320/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003321unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003322 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003323 unsigned Mask = 0;
3324 // 8 nodes, but we only care about the first 4.
3325 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003326 int Val = SVOp->getMaskElt(i);
3327 if (Val >= 0)
3328 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003329 if (i != 0)
3330 Mask <<= 2;
3331 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003332 return Mask;
3333}
3334
Nate Begemana09008b2009-10-19 02:17:23 +00003335/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3336/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3337unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3338 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3339 EVT VVT = N->getValueType(0);
3340 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3341 int Val = 0;
3342
3343 unsigned i, e;
3344 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3345 Val = SVOp->getMaskElt(i);
3346 if (Val >= 0)
3347 break;
3348 }
3349 return (Val - i) * EltSize;
3350}
3351
David Greenec38a03e2011-02-03 15:50:00 +00003352/// getExtractVEXTRACTF128Immediate - Return the appropriate immediate
3353/// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128
3354/// instructions.
3355unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) {
3356 if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
3357 llvm_unreachable("Illegal extract subvector for VEXTRACTF128");
3358
3359 uint64_t Index =
3360 cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
3361
3362 EVT VecVT = N->getOperand(0).getValueType();
3363 EVT ElVT = VecVT.getVectorElementType();
3364
3365 unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits();
3366
3367 return Index / NumElemsPerChunk;
3368}
3369
Evan Cheng37b73872009-07-30 08:33:02 +00003370/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3371/// constant +0.0.
3372bool X86::isZeroNode(SDValue Elt) {
3373 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmane368b462010-06-18 14:22:04 +00003374 cast<ConstantSDNode>(Elt)->isNullValue()) ||
Evan Cheng37b73872009-07-30 08:33:02 +00003375 (isa<ConstantFPSDNode>(Elt) &&
3376 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3377}
3378
Nate Begeman9008ca62009-04-27 18:41:29 +00003379/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3380/// their permute mask.
3381static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3382 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003383 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003384 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003385 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003386
Nate Begeman5a5ca152009-04-29 05:20:52 +00003387 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003388 int idx = SVOp->getMaskElt(i);
3389 if (idx < 0)
3390 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003391 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003393 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003395 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003396 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3397 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003398}
3399
Evan Cheng779ccea2007-12-07 21:30:01 +00003400/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3401/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003402static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003403 unsigned NumElems = VT.getVectorNumElements();
3404 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003405 int idx = Mask[i];
3406 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003407 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003408 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003409 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003410 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003411 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003412 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003413}
3414
Evan Cheng533a0aa2006-04-19 20:35:22 +00003415/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3416/// match movhlps. The lower half elements should come from upper half of
3417/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003418/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003419static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3420 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003421 return false;
3422 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003423 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003424 return false;
3425 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003426 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003427 return false;
3428 return true;
3429}
3430
Evan Cheng5ced1d82006-04-06 23:23:56 +00003431/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003432/// is promoted to a vector. It also returns the LoadSDNode by reference if
3433/// required.
3434static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003435 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3436 return false;
3437 N = N->getOperand(0).getNode();
3438 if (!ISD::isNON_EXTLoad(N))
3439 return false;
3440 if (LD)
3441 *LD = cast<LoadSDNode>(N);
3442 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003443}
3444
Evan Cheng533a0aa2006-04-19 20:35:22 +00003445/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3446/// match movlp{s|d}. The lower half elements should come from lower half of
3447/// V1 (and in order), and the upper half elements should come from the upper
3448/// half of V2 (and in order). And since V1 will become the source of the
3449/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003450static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3451 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003452 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003453 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003454 // Is V2 is a vector load, don't do this transformation. We will try to use
3455 // load folding shufps op.
3456 if (ISD::isNON_EXTLoad(V2))
3457 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003458
Nate Begeman5a5ca152009-04-29 05:20:52 +00003459 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003460
Evan Cheng533a0aa2006-04-19 20:35:22 +00003461 if (NumElems != 2 && NumElems != 4)
3462 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003463 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003464 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003465 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003466 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003467 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003468 return false;
3469 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003470}
3471
Evan Cheng39623da2006-04-20 08:58:49 +00003472/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3473/// all the same.
3474static bool isSplatVector(SDNode *N) {
3475 if (N->getOpcode() != ISD::BUILD_VECTOR)
3476 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003477
Dan Gohman475871a2008-07-27 21:46:04 +00003478 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003479 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3480 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003481 return false;
3482 return true;
3483}
3484
Evan Cheng213d2cf2007-05-17 18:45:50 +00003485/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003486/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003487/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003488static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003489 SDValue V1 = N->getOperand(0);
3490 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003491 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3492 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003493 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003494 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003495 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003496 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3497 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003498 if (Opc != ISD::BUILD_VECTOR ||
3499 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003500 return false;
3501 } else if (Idx >= 0) {
3502 unsigned Opc = V1.getOpcode();
3503 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3504 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003505 if (Opc != ISD::BUILD_VECTOR ||
3506 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003507 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003508 }
3509 }
3510 return true;
3511}
3512
3513/// getZeroVector - Returns a vector of specified type with all zero elements.
3514///
Owen Andersone50ed302009-08-10 22:56:29 +00003515static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003516 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003517 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003518
Dale Johannesen0488fb62010-09-30 23:57:10 +00003519 // Always build SSE zero vectors as <4 x i32> bitcasted
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003520 // to their dest type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003521 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003522 if (VT.getSizeInBits() == 128) { // SSE
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003523 if (HasSSE2) { // SSE2
3524 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3525 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
3526 } else { // SSE1
3527 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3528 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
3529 }
3530 } else if (VT.getSizeInBits() == 256) { // AVX
3531 // 256-bit logic and arithmetic instructions in AVX are
3532 // all floating-point, no support for integer ops. Default
3533 // to emitting fp zeroed vectors then.
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00003535 SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
3536 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, 8);
Evan Chengf0df0312008-05-15 08:39:06 +00003537 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003538 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003539}
3540
Chris Lattner8a594482007-11-25 00:24:49 +00003541/// getOnesVector - Returns a vector of specified type with all bits set.
3542///
Owen Andersone50ed302009-08-10 22:56:29 +00003543static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003544 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003545
Chris Lattner8a594482007-11-25 00:24:49 +00003546 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3547 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003549 SDValue Vec;
Dale Johannesen0488fb62010-09-30 23:57:10 +00003550 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003551 return DAG.getNode(ISD::BITCAST, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003552}
3553
3554
Evan Cheng39623da2006-04-20 08:58:49 +00003555/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3556/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003557static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003558 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003559 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003560
Evan Cheng39623da2006-04-20 08:58:49 +00003561 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003562 SmallVector<int, 8> MaskVec;
3563 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003564
Nate Begeman5a5ca152009-04-29 05:20:52 +00003565 for (unsigned i = 0; i != NumElems; ++i) {
3566 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003567 MaskVec[i] = NumElems;
3568 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003569 }
Evan Cheng39623da2006-04-20 08:58:49 +00003570 }
Evan Cheng39623da2006-04-20 08:58:49 +00003571 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003572 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3573 SVOp->getOperand(1), &MaskVec[0]);
3574 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003575}
3576
Evan Cheng017dcc62006-04-21 01:05:10 +00003577/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3578/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003579static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003580 SDValue V2) {
3581 unsigned NumElems = VT.getVectorNumElements();
3582 SmallVector<int, 8> Mask;
3583 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003584 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003585 Mask.push_back(i);
3586 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003587}
3588
Nate Begeman9008ca62009-04-27 18:41:29 +00003589/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003590static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003591 SDValue V2) {
3592 unsigned NumElems = VT.getVectorNumElements();
3593 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003594 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003595 Mask.push_back(i);
3596 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003597 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003598 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003599}
3600
Nate Begeman9008ca62009-04-27 18:41:29 +00003601/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003602static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003603 SDValue V2) {
3604 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003605 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003606 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003607 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003608 Mask.push_back(i + Half);
3609 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003610 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003611 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003612}
3613
Bruno Cardoso Lopesbb0a9482010-08-13 17:50:47 +00003614/// PromoteSplat - Promote a splat of v4i32, v8i16 or v16i8 to v4f32.
3615static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003616 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003617 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003618 DebugLoc dl = SV->getDebugLoc();
3619 SDValue V1 = SV->getOperand(0);
3620 int NumElems = VT.getVectorNumElements();
3621 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003622
Nate Begeman9008ca62009-04-27 18:41:29 +00003623 // unpack elements to the correct location
3624 while (NumElems > 4) {
3625 if (EltNo < NumElems/2) {
3626 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3627 } else {
3628 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3629 EltNo -= NumElems/2;
3630 }
3631 NumElems >>= 1;
3632 }
Eric Christopherfd179292009-08-27 18:07:15 +00003633
Nate Begeman9008ca62009-04-27 18:41:29 +00003634 // Perform the splat.
3635 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003636 V1 = DAG.getNode(ISD::BITCAST, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003637 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003638 return DAG.getNode(ISD::BITCAST, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003639}
3640
Evan Chengba05f722006-04-21 23:03:30 +00003641/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003642/// vector of zero or undef vector. This produces a shuffle where the low
3643/// element of V2 is swizzled into the zero/undef vector, landing at element
3644/// 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 +00003645static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003646 bool isZero, bool HasSSE2,
3647 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003648 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003649 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003650 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3651 unsigned NumElems = VT.getVectorNumElements();
3652 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003653 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003654 // If this is the insertion idx, put the low elt of V2 here.
3655 MaskVec.push_back(i == Idx ? NumElems : i);
3656 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003657}
3658
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003659/// getShuffleScalarElt - Returns the scalar element that will make up the ith
3660/// element of the result of the vector shuffle.
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003661SDValue getShuffleScalarElt(SDNode *N, int Index, SelectionDAG &DAG,
3662 unsigned Depth) {
3663 if (Depth == 6)
3664 return SDValue(); // Limit search depth.
3665
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003666 SDValue V = SDValue(N, 0);
3667 EVT VT = V.getValueType();
3668 unsigned Opcode = V.getOpcode();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003669
3670 // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
3671 if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
3672 Index = SV->getMaskElt(Index);
3673
3674 if (Index < 0)
3675 return DAG.getUNDEF(VT.getVectorElementType());
3676
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003677 int NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003678 SDValue NewV = (Index < NumElems) ? SV->getOperand(0) : SV->getOperand(1);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003679 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG, Depth+1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003680 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003681
3682 // Recurse into target specific vector shuffles to find scalars.
3683 if (isTargetShuffle(Opcode)) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003684 int NumElems = VT.getVectorNumElements();
3685 SmallVector<unsigned, 16> ShuffleMask;
3686 SDValue ImmN;
3687
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003688 switch(Opcode) {
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003689 case X86ISD::SHUFPS:
3690 case X86ISD::SHUFPD:
3691 ImmN = N->getOperand(N->getNumOperands()-1);
3692 DecodeSHUFPSMask(NumElems,
3693 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3694 ShuffleMask);
3695 break;
3696 case X86ISD::PUNPCKHBW:
3697 case X86ISD::PUNPCKHWD:
3698 case X86ISD::PUNPCKHDQ:
3699 case X86ISD::PUNPCKHQDQ:
3700 DecodePUNPCKHMask(NumElems, ShuffleMask);
3701 break;
3702 case X86ISD::UNPCKHPS:
3703 case X86ISD::UNPCKHPD:
3704 DecodeUNPCKHPMask(NumElems, ShuffleMask);
3705 break;
3706 case X86ISD::PUNPCKLBW:
3707 case X86ISD::PUNPCKLWD:
3708 case X86ISD::PUNPCKLDQ:
3709 case X86ISD::PUNPCKLQDQ:
3710 DecodePUNPCKLMask(NumElems, ShuffleMask);
3711 break;
3712 case X86ISD::UNPCKLPS:
3713 case X86ISD::UNPCKLPD:
3714 DecodeUNPCKLPMask(NumElems, ShuffleMask);
3715 break;
3716 case X86ISD::MOVHLPS:
3717 DecodeMOVHLPSMask(NumElems, ShuffleMask);
3718 break;
3719 case X86ISD::MOVLHPS:
3720 DecodeMOVLHPSMask(NumElems, ShuffleMask);
3721 break;
3722 case X86ISD::PSHUFD:
3723 ImmN = N->getOperand(N->getNumOperands()-1);
3724 DecodePSHUFMask(NumElems,
3725 cast<ConstantSDNode>(ImmN)->getZExtValue(),
3726 ShuffleMask);
3727 break;
3728 case X86ISD::PSHUFHW:
3729 ImmN = N->getOperand(N->getNumOperands()-1);
3730 DecodePSHUFHWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3731 ShuffleMask);
3732 break;
3733 case X86ISD::PSHUFLW:
3734 ImmN = N->getOperand(N->getNumOperands()-1);
3735 DecodePSHUFLWMask(cast<ConstantSDNode>(ImmN)->getZExtValue(),
3736 ShuffleMask);
3737 break;
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003738 case X86ISD::MOVSS:
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003739 case X86ISD::MOVSD: {
3740 // The index 0 always comes from the first element of the second source,
3741 // this is why MOVSS and MOVSD are used in the first place. The other
3742 // elements come from the other positions of the first source vector.
3743 unsigned OpNum = (Index == 0) ? 1 : 0;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003744 return getShuffleScalarElt(V.getOperand(OpNum).getNode(), Index, DAG,
3745 Depth+1);
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00003746 }
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003747 default:
3748 assert("not implemented for target shuffle node");
3749 return SDValue();
3750 }
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003751
3752 Index = ShuffleMask[Index];
3753 if (Index < 0)
3754 return DAG.getUNDEF(VT.getVectorElementType());
3755
3756 SDValue NewV = (Index < NumElems) ? N->getOperand(0) : N->getOperand(1);
3757 return getShuffleScalarElt(NewV.getNode(), Index % NumElems, DAG,
3758 Depth+1);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003759 }
3760
3761 // Actual nodes that may contain scalar elements
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003762 if (Opcode == ISD::BITCAST) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003763 V = V.getOperand(0);
3764 EVT SrcVT = V.getValueType();
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003765 unsigned NumElems = VT.getVectorNumElements();
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003766
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00003767 if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003768 return SDValue();
3769 }
3770
3771 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
3772 return (Index == 0) ? V.getOperand(0)
3773 : DAG.getUNDEF(VT.getVectorElementType());
3774
3775 if (V.getOpcode() == ISD::BUILD_VECTOR)
3776 return V.getOperand(Index);
3777
3778 return SDValue();
3779}
3780
3781/// getNumOfConsecutiveZeros - Return the number of elements of a vector
3782/// shuffle operation which come from a consecutively from a zero. The
3783/// search can start in two diferent directions, from left or right.
3784static
3785unsigned getNumOfConsecutiveZeros(SDNode *N, int NumElems,
3786 bool ZerosFromLeft, SelectionDAG &DAG) {
3787 int i = 0;
3788
3789 while (i < NumElems) {
3790 unsigned Index = ZerosFromLeft ? i : NumElems-i-1;
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00003791 SDValue Elt = getShuffleScalarElt(N, Index, DAG, 0);
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003792 if (!(Elt.getNode() &&
3793 (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt))))
3794 break;
3795 ++i;
3796 }
3797
3798 return i;
3799}
3800
3801/// isShuffleMaskConsecutive - Check if the shuffle mask indicies from MaskI to
3802/// MaskE correspond consecutively to elements from one of the vector operands,
3803/// starting from its index OpIdx. Also tell OpNum which source vector operand.
3804static
3805bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, int MaskI, int MaskE,
3806 int OpIdx, int NumElems, unsigned &OpNum) {
3807 bool SeenV1 = false;
3808 bool SeenV2 = false;
3809
3810 for (int i = MaskI; i <= MaskE; ++i, ++OpIdx) {
3811 int Idx = SVOp->getMaskElt(i);
3812 // Ignore undef indicies
3813 if (Idx < 0)
3814 continue;
3815
3816 if (Idx < NumElems)
3817 SeenV1 = true;
3818 else
3819 SeenV2 = true;
3820
3821 // Only accept consecutive elements from the same vector
3822 if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2))
3823 return false;
3824 }
3825
3826 OpNum = SeenV1 ? 0 : 1;
3827 return true;
3828}
3829
3830/// isVectorShiftRight - Returns true if the shuffle can be implemented as a
3831/// logical left shift of a vector.
3832static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3833 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3834 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3835 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3836 false /* check zeros from right */, DAG);
3837 unsigned OpSrc;
3838
3839 if (!NumZeros)
3840 return false;
3841
3842 // Considering the elements in the mask that are not consecutive zeros,
3843 // check if they consecutively come from only one of the source vectors.
3844 //
3845 // V1 = {X, A, B, C} 0
3846 // \ \ \ /
3847 // vector_shuffle V1, V2 <1, 2, 3, X>
3848 //
3849 if (!isShuffleMaskConsecutive(SVOp,
3850 0, // Mask Start Index
3851 NumElems-NumZeros-1, // Mask End Index
3852 NumZeros, // Where to start looking in the src vector
3853 NumElems, // Number of elements in vector
3854 OpSrc)) // Which source operand ?
3855 return false;
3856
3857 isLeft = false;
3858 ShAmt = NumZeros;
3859 ShVal = SVOp->getOperand(OpSrc);
3860 return true;
3861}
3862
3863/// isVectorShiftLeft - Returns true if the shuffle can be implemented as a
3864/// logical left shift of a vector.
3865static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
3866 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
3867 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
3868 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems,
3869 true /* check zeros from left */, DAG);
3870 unsigned OpSrc;
3871
3872 if (!NumZeros)
3873 return false;
3874
3875 // Considering the elements in the mask that are not consecutive zeros,
3876 // check if they consecutively come from only one of the source vectors.
3877 //
3878 // 0 { A, B, X, X } = V2
3879 // / \ / /
3880 // vector_shuffle V1, V2 <X, X, 4, 5>
3881 //
3882 if (!isShuffleMaskConsecutive(SVOp,
3883 NumZeros, // Mask Start Index
3884 NumElems-1, // Mask End Index
3885 0, // Where to start looking in the src vector
3886 NumElems, // Number of elements in vector
3887 OpSrc)) // Which source operand ?
3888 return false;
3889
3890 isLeft = true;
3891 ShAmt = NumZeros;
3892 ShVal = SVOp->getOperand(OpSrc);
3893 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003894}
3895
3896/// isVectorShift - Returns true if the shuffle can be implemented as a
3897/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003898static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003899 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003900 if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) ||
3901 isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt))
3902 return true;
Evan Chengf26ffe92008-05-29 08:22:04 +00003903
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +00003904 return false;
Evan Chengf26ffe92008-05-29 08:22:04 +00003905}
3906
Evan Chengc78d3b42006-04-24 18:01:45 +00003907/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3908///
Dan Gohman475871a2008-07-27 21:46:04 +00003909static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003910 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003911 SelectionDAG &DAG,
3912 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003913 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003914 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003915
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003916 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003917 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003918 bool First = true;
3919 for (unsigned i = 0; i < 16; ++i) {
3920 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3921 if (ThisIsNonZero && First) {
3922 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003924 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003925 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003926 First = false;
3927 }
3928
3929 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003930 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003931 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3932 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003933 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003934 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003935 }
3936 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003937 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3938 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3939 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003940 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003941 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003942 } else
3943 ThisElt = LastElt;
3944
Gabor Greifba36cb52008-08-28 21:40:38 +00003945 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003946 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003947 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003948 }
3949 }
3950
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003951 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003952}
3953
Bill Wendlinga348c562007-03-22 18:42:45 +00003954/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003955///
Dan Gohman475871a2008-07-27 21:46:04 +00003956static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003957 unsigned NumNonZero, unsigned NumZero,
3958 SelectionDAG &DAG,
3959 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003960 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003961 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003962
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003963 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003964 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003965 bool First = true;
3966 for (unsigned i = 0; i < 8; ++i) {
3967 bool isNonZero = (NonZeros & (1 << i)) != 0;
3968 if (isNonZero) {
3969 if (First) {
3970 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003971 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003972 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003973 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003974 First = false;
3975 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003976 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003977 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003978 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003979 }
3980 }
3981
3982 return V;
3983}
3984
Evan Chengf26ffe92008-05-29 08:22:04 +00003985/// getVShift - Return a vector logical shift node.
3986///
Owen Andersone50ed302009-08-10 22:56:29 +00003987static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003988 unsigned NumBits, SelectionDAG &DAG,
3989 const TargetLowering &TLI, DebugLoc dl) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00003990 EVT ShVT = MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003991 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003992 SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp);
3993 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003994 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003995 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003996}
3997
Dan Gohman475871a2008-07-27 21:46:04 +00003998SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003999X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00004000 SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00004001
Evan Chengc3630942009-12-09 21:00:30 +00004002 // Check if the scalar load can be widened into a vector load. And if
4003 // the address is "base + cst" see if the cst can be "absorbed" into
4004 // the shuffle mask.
4005 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
4006 SDValue Ptr = LD->getBasePtr();
4007 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
4008 return SDValue();
4009 EVT PVT = LD->getValueType(0);
4010 if (PVT != MVT::i32 && PVT != MVT::f32)
4011 return SDValue();
4012
4013 int FI = -1;
4014 int64_t Offset = 0;
4015 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
4016 FI = FINode->getIndex();
4017 Offset = 0;
4018 } else if (Ptr.getOpcode() == ISD::ADD &&
4019 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4020 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4021 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4022 Offset = Ptr.getConstantOperandVal(1);
4023 Ptr = Ptr.getOperand(0);
4024 } else {
4025 return SDValue();
4026 }
4027
4028 SDValue Chain = LD->getChain();
4029 // Make sure the stack object alignment is at least 16.
4030 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
4031 if (DAG.InferPtrAlignment(Ptr) < 16) {
4032 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00004033 // Can't change the alignment. FIXME: It's possible to compute
4034 // the exact stack offset and reference FI + adjust offset instead.
4035 // If someone *really* cares about this. That's the way to implement it.
4036 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004037 } else {
4038 MFI->setObjectAlignment(FI, 16);
4039 }
4040 }
4041
4042 // (Offset % 16) must be multiple of 4. Then address is then
4043 // Ptr + (Offset & ~15).
4044 if (Offset < 0)
4045 return SDValue();
4046 if ((Offset % 16) & 3)
4047 return SDValue();
4048 int64_t StartOffset = Offset & ~15;
4049 if (StartOffset)
4050 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
4051 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
4052
4053 int EltNo = (Offset - StartOffset) >> 2;
4054 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
4055 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
Chris Lattner51abfe42010-09-21 06:02:19 +00004056 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,
4057 LD->getPointerInfo().getWithOffset(StartOffset),
David Greene67c9d422010-02-15 16:53:33 +00004058 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00004059 // Canonicalize it to a v4i32 shuffle.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004060 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, V1);
4061 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Chengc3630942009-12-09 21:00:30 +00004062 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
Chris Lattner51abfe42010-09-21 06:02:19 +00004063 DAG.getUNDEF(MVT::v4i32),&Mask[0]));
Evan Chengc3630942009-12-09 21:00:30 +00004064 }
4065
4066 return SDValue();
4067}
4068
Michael J. Spencerec38de22010-10-10 22:04:20 +00004069/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
4070/// vector of type 'VT', see if the elements can be replaced by a single large
Nate Begeman1449f292010-03-24 22:19:06 +00004071/// load which has the same value as a build_vector whose operands are 'elts'.
4072///
4073/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
Michael J. Spencerec38de22010-10-10 22:04:20 +00004074///
Nate Begeman1449f292010-03-24 22:19:06 +00004075/// FIXME: we'd also like to handle the case where the last elements are zero
4076/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
4077/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004078static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
Chris Lattner88641552010-09-22 00:34:38 +00004079 DebugLoc &DL, SelectionDAG &DAG) {
Nate Begemanfdea31a2010-03-24 20:49:50 +00004080 EVT EltVT = VT.getVectorElementType();
4081 unsigned NumElems = Elts.size();
Michael J. Spencerec38de22010-10-10 22:04:20 +00004082
Nate Begemanfdea31a2010-03-24 20:49:50 +00004083 LoadSDNode *LDBase = NULL;
4084 unsigned LastLoadedElt = -1U;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004085
Nate Begeman1449f292010-03-24 22:19:06 +00004086 // For each element in the initializer, see if we've found a load or an undef.
Michael J. Spencerec38de22010-10-10 22:04:20 +00004087 // If we don't find an initial load element, or later load elements are
Nate Begeman1449f292010-03-24 22:19:06 +00004088 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004089 for (unsigned i = 0; i < NumElems; ++i) {
4090 SDValue Elt = Elts[i];
Michael J. Spencerec38de22010-10-10 22:04:20 +00004091
Nate Begemanfdea31a2010-03-24 20:49:50 +00004092 if (!Elt.getNode() ||
4093 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
4094 return SDValue();
4095 if (!LDBase) {
4096 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
4097 return SDValue();
4098 LDBase = cast<LoadSDNode>(Elt.getNode());
4099 LastLoadedElt = i;
4100 continue;
4101 }
4102 if (Elt.getOpcode() == ISD::UNDEF)
4103 continue;
4104
4105 LoadSDNode *LD = cast<LoadSDNode>(Elt);
4106 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
4107 return SDValue();
4108 LastLoadedElt = i;
4109 }
Nate Begeman1449f292010-03-24 22:19:06 +00004110
4111 // If we have found an entire vector of loads and undefs, then return a large
4112 // load of the entire vector width starting at the base pointer. If we found
4113 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004114 if (LastLoadedElt == NumElems - 1) {
4115 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
Chris Lattner88641552010-09-22 00:34:38 +00004116 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004117 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004118 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
Chris Lattner88641552010-09-22 00:34:38 +00004119 return DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +00004120 LDBase->getPointerInfo(),
Nate Begemanfdea31a2010-03-24 20:49:50 +00004121 LDBase->isVolatile(), LDBase->isNonTemporal(),
4122 LDBase->getAlignment());
4123 } else if (NumElems == 4 && LastLoadedElt == 1) {
4124 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
4125 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
Chris Lattner88641552010-09-22 00:34:38 +00004126 SDValue ResNode = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys,
4127 Ops, 2, MVT::i32,
4128 LDBase->getMemOperand());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004129 return DAG.getNode(ISD::BITCAST, DL, VT, ResNode);
Nate Begemanfdea31a2010-03-24 20:49:50 +00004130 }
4131 return SDValue();
4132}
4133
Evan Chengc3630942009-12-09 21:00:30 +00004134SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004135X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004136 DebugLoc dl = Op.getDebugLoc();
Chris Lattner6e80e442010-08-28 17:15:43 +00004137 // All zero's are handled with pxor in SSE2 and above, xorps in SSE1.
4138 // All one's are handled with pcmpeqd. In AVX, zero's are handled with
Bruno Cardoso Lopes8c05a852010-08-12 02:06:36 +00004139 // vpxor in 128-bit and xor{pd,ps} in 256-bit, but no 256 version of pcmpeqd
4140 // is present, so AllOnes is ignored.
4141 if (ISD::isBuildVectorAllZeros(Op.getNode()) ||
4142 (Op.getValueType().getSizeInBits() != 256 &&
4143 ISD::isBuildVectorAllOnes(Op.getNode()))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004144 // Canonicalize this to <4 x i32> (SSE) to
Chris Lattner8a594482007-11-25 00:24:49 +00004145 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
4146 // eliminated on x86-32 hosts.
Dale Johannesen0488fb62010-09-30 23:57:10 +00004147 if (Op.getValueType() == MVT::v4i32)
Chris Lattner8a594482007-11-25 00:24:49 +00004148 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004149
Gabor Greifba36cb52008-08-28 21:40:38 +00004150 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004151 return getOnesVector(Op.getValueType(), DAG, dl);
4152 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00004153 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004154
Owen Andersone50ed302009-08-10 22:56:29 +00004155 EVT VT = Op.getValueType();
4156 EVT ExtVT = VT.getVectorElementType();
4157 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004158
4159 unsigned NumElems = Op.getNumOperands();
4160 unsigned NumZero = 0;
4161 unsigned NumNonZero = 0;
4162 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004163 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004164 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004165 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004166 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00004167 if (Elt.getOpcode() == ISD::UNDEF)
4168 continue;
4169 Values.insert(Elt);
4170 if (Elt.getOpcode() != ISD::Constant &&
4171 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004172 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00004173 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00004174 NumZero++;
4175 else {
4176 NonZeros |= (1 << i);
4177 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004178 }
4179 }
4180
Chris Lattner97a2a562010-08-26 05:24:29 +00004181 // All undef vector. Return an UNDEF. All zero vectors were handled above.
4182 if (NumNonZero == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00004183 return DAG.getUNDEF(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004184
Chris Lattner67f453a2008-03-09 05:42:06 +00004185 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00004186 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004187 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00004188 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00004189
Chris Lattner62098042008-03-09 01:05:04 +00004190 // If this is an insertion of an i64 value on x86-32, and if the top bits of
4191 // the value are obviously zero, truncate the value to i32 and do the
4192 // insertion that way. Only do this if the value is non-constant or if the
4193 // value is a constant being inserted into element 0. It is cheaper to do
4194 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00004196 (!IsAllConstants || Idx == 0)) {
4197 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
Dale Johannesen0488fb62010-09-30 23:57:10 +00004198 // Handle SSE only.
4199 assert(VT == MVT::v2i64 && "Expected an SSE value type!");
4200 EVT VecVT = MVT::v4i32;
4201 unsigned VecElts = 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00004202
Chris Lattner62098042008-03-09 01:05:04 +00004203 // Truncate the value (which may itself be a constant) to i32, and
4204 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00004205 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00004206 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00004207 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4208 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00004209
Chris Lattner62098042008-03-09 01:05:04 +00004210 // Now we have our 32-bit value zero extended in the low element of
4211 // a vector. If Idx != 0, swizzle it into place.
4212 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004213 SmallVector<int, 4> Mask;
4214 Mask.push_back(Idx);
4215 for (unsigned i = 1; i != VecElts; ++i)
4216 Mask.push_back(i);
4217 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00004218 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00004219 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00004220 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004221 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00004222 }
4223 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004224
Chris Lattner19f79692008-03-08 22:59:52 +00004225 // If we have a constant or non-constant insertion into the low element of
4226 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
4227 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00004228 // depending on what the source datatype is.
4229 if (Idx == 0) {
4230 if (NumZero == 0) {
4231 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
4233 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00004234 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
4235 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
4236 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
4237 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
4239 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
Dale Johannesen0488fb62010-09-30 23:57:10 +00004240 assert(VT.getSizeInBits() == 128 && "Expected an SSE value type!");
4241 EVT MiddleVT = MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00004242 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
4243 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
4244 Subtarget->hasSSE2(), DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004245 return DAG.getNode(ISD::BITCAST, dl, VT, Item);
Eli Friedman10415532009-06-06 06:05:10 +00004246 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004247 }
Evan Chengf26ffe92008-05-29 08:22:04 +00004248
4249 // Is it a vector logical left shift?
4250 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00004251 X86::isZeroNode(Op.getOperand(0)) &&
4252 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004253 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004254 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004255 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004256 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00004257 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004258 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004259
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004260 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00004261 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004262
Chris Lattner19f79692008-03-08 22:59:52 +00004263 // Otherwise, if this is a vector with i32 or f32 elements, and the element
4264 // is a non-constant being inserted into an element other than the low one,
4265 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
4266 // movd/movss) to move this into the low element, then shuffle it into
4267 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004268 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00004269 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00004270
Evan Cheng0db9fe62006-04-25 20:13:52 +00004271 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00004272 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
4273 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00004274 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004275 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00004276 MaskVec.push_back(i == Idx ? 0 : 1);
4277 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004278 }
4279 }
4280
Chris Lattner67f453a2008-03-09 05:42:06 +00004281 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00004282 if (Values.size() == 1) {
4283 if (EVTBits == 32) {
4284 // Instead of a shuffle like this:
4285 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
4286 // Check if it's possible to issue this instead.
4287 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
4288 unsigned Idx = CountTrailingZeros_32(NonZeros);
4289 SDValue Item = Op.getOperand(Idx);
4290 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
4291 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
4292 }
Dan Gohman475871a2008-07-27 21:46:04 +00004293 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00004294 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004295
Dan Gohmana3941172007-07-24 22:55:08 +00004296 // A vector full of immediates; various special cases are already
4297 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00004298 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00004299 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00004300
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004301 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004302 if (EVTBits == 64) {
4303 if (NumNonZero == 1) {
4304 // One half is zero or undef.
4305 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00004306 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004307 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00004308 return getShuffleVectorZeroOrUndef(V2, Idx, true,
4309 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00004310 }
Dan Gohman475871a2008-07-27 21:46:04 +00004311 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00004312 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004313
4314 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00004315 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004316 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00004317 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004318 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004319 }
4320
Bill Wendling826f36f2007-03-28 00:57:11 +00004321 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004322 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Chris Lattner97a2a562010-08-26 05:24:29 +00004323 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004324 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004325 }
4326
4327 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004328 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00004329 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 if (NumElems == 4 && NumZero > 0) {
4331 for (unsigned i = 0; i < 4; ++i) {
4332 bool isZero = !(NonZeros & (1 << i));
4333 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00004334 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004335 else
Dale Johannesenace16102009-02-03 19:33:06 +00004336 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004337 }
4338
4339 for (unsigned i = 0; i < 2; ++i) {
4340 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
4341 default: break;
4342 case 0:
4343 V[i] = V[i*2]; // Must be a zero vector.
4344 break;
4345 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00004346 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004347 break;
4348 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00004349 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004350 break;
4351 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00004352 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004353 break;
4354 }
4355 }
4356
Nate Begeman9008ca62009-04-27 18:41:29 +00004357 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004358 bool Reverse = (NonZeros & 0x3) == 2;
4359 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004360 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004361 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
4362 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004363 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
4364 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004365 }
4366
Nate Begemanfdea31a2010-03-24 20:49:50 +00004367 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
4368 // Check for a build vector of consecutive loads.
4369 for (unsigned i = 0; i < NumElems; ++i)
4370 V[i] = Op.getOperand(i);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004371
Nate Begemanfdea31a2010-03-24 20:49:50 +00004372 // Check for elements which are consecutive loads.
4373 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
4374 if (LD.getNode())
4375 return LD;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004376
4377 // For SSE 4.1, use insertps to put the high elements into the low element.
Nate Begemanfdea31a2010-03-24 20:49:50 +00004378 if (getSubtarget()->hasSSE41()) {
Chris Lattner24faf612010-08-28 17:59:08 +00004379 SDValue Result;
4380 if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
4381 Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
4382 else
4383 Result = DAG.getUNDEF(VT);
Michael J. Spencerec38de22010-10-10 22:04:20 +00004384
Chris Lattner24faf612010-08-28 17:59:08 +00004385 for (unsigned i = 1; i < NumElems; ++i) {
4386 if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
4387 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
Nate Begeman9008ca62009-04-27 18:41:29 +00004388 Op.getOperand(i), DAG.getIntPtrConstant(i));
Chris Lattner24faf612010-08-28 17:59:08 +00004389 }
4390 return Result;
Nate Begeman9008ca62009-04-27 18:41:29 +00004391 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00004392
Chris Lattner6e80e442010-08-28 17:15:43 +00004393 // Otherwise, expand into a number of unpckl*, start by extending each of
4394 // our (non-undef) elements to the full vector width with the element in the
4395 // bottom slot of the vector (which generates no code for SSE).
4396 for (unsigned i = 0; i < NumElems; ++i) {
4397 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4398 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
4399 else
4400 V[i] = DAG.getUNDEF(VT);
4401 }
4402
4403 // Next, we iteratively mix elements, e.g. for v4f32:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004404 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4405 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4406 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Chris Lattner6e80e442010-08-28 17:15:43 +00004407 unsigned EltStride = NumElems >> 1;
4408 while (EltStride != 0) {
Chris Lattner3ddcc432010-08-28 17:28:30 +00004409 for (unsigned i = 0; i < EltStride; ++i) {
4410 // If V[i+EltStride] is undef and this is the first round of mixing,
4411 // then it is safe to just drop this shuffle: V[i] is already in the
4412 // right place, the one element (since it's the first round) being
4413 // inserted as undef can be dropped. This isn't safe for successive
4414 // rounds because they will permute elements within both vectors.
4415 if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
4416 EltStride == NumElems/2)
4417 continue;
Michael J. Spencerec38de22010-10-10 22:04:20 +00004418
Chris Lattner6e80e442010-08-28 17:15:43 +00004419 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
Chris Lattner3ddcc432010-08-28 17:28:30 +00004420 }
Chris Lattner6e80e442010-08-28 17:15:43 +00004421 EltStride >>= 1;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422 }
4423 return V[0];
4424 }
Dan Gohman475871a2008-07-27 21:46:04 +00004425 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004426}
4427
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004428SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004429X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004430 // We support concatenate two MMX registers and place them in a MMX
4431 // register. This is better than doing a stack convert.
4432 DebugLoc dl = Op.getDebugLoc();
4433 EVT ResVT = Op.getValueType();
4434 assert(Op.getNumOperands() == 2);
4435 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4436 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4437 int Mask[2];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004438 SDValue InVec = DAG.getNode(ISD::BITCAST,dl, MVT::v1i64, Op.getOperand(0));
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004439 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4440 InVec = Op.getOperand(1);
4441 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4442 unsigned NumElts = ResVT.getVectorNumElements();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004443 VecOp = DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004444 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4445 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4446 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004447 InVec = DAG.getNode(ISD::BITCAST, dl, MVT::v1i64, InVec);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004448 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4449 Mask[0] = 0; Mask[1] = 2;
4450 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4451 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004452 return DAG.getNode(ISD::BITCAST, dl, ResVT, VecOp);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004453}
4454
Nate Begemanb9a47b82009-02-23 08:49:38 +00004455// v8i16 shuffles - Prefer shuffles in the following order:
4456// 1. [all] pshuflw, pshufhw, optional move
4457// 2. [ssse3] 1 x pshufb
4458// 3. [ssse3] 2 x pshufb + 1 x por
4459// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004460SDValue
4461X86TargetLowering::LowerVECTOR_SHUFFLEv8i16(SDValue Op,
4462 SelectionDAG &DAG) const {
4463 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Nate Begeman9008ca62009-04-27 18:41:29 +00004464 SDValue V1 = SVOp->getOperand(0);
4465 SDValue V2 = SVOp->getOperand(1);
4466 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004467 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004468
Nate Begemanb9a47b82009-02-23 08:49:38 +00004469 // Determine if more than 1 of the words in each of the low and high quadwords
4470 // of the result come from the same quadword of one of the two inputs. Undef
4471 // mask values count as coming from any quadword, for better codegen.
4472 SmallVector<unsigned, 4> LoQuad(4);
4473 SmallVector<unsigned, 4> HiQuad(4);
4474 BitVector InputQuads(4);
4475 for (unsigned i = 0; i < 8; ++i) {
4476 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004477 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004478 MaskVals.push_back(EltIdx);
4479 if (EltIdx < 0) {
4480 ++Quad[0];
4481 ++Quad[1];
4482 ++Quad[2];
4483 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004484 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004485 }
4486 ++Quad[EltIdx / 4];
4487 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004488 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004489
Nate Begemanb9a47b82009-02-23 08:49:38 +00004490 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004491 unsigned MaxQuad = 1;
4492 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004493 if (LoQuad[i] > MaxQuad) {
4494 BestLoQuad = i;
4495 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004496 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004497 }
4498
Nate Begemanb9a47b82009-02-23 08:49:38 +00004499 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004500 MaxQuad = 1;
4501 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004502 if (HiQuad[i] > MaxQuad) {
4503 BestHiQuad = i;
4504 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004505 }
4506 }
4507
Nate Begemanb9a47b82009-02-23 08:49:38 +00004508 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004509 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004510 // single pshufb instruction is necessary. If There are more than 2 input
4511 // quads, disable the next transformation since it does not help SSSE3.
4512 bool V1Used = InputQuads[0] || InputQuads[1];
4513 bool V2Used = InputQuads[2] || InputQuads[3];
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004514 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004515 if (InputQuads.count() == 2 && V1Used && V2Used) {
4516 BestLoQuad = InputQuads.find_first();
4517 BestHiQuad = InputQuads.find_next(BestLoQuad);
4518 }
4519 if (InputQuads.count() > 2) {
4520 BestLoQuad = -1;
4521 BestHiQuad = -1;
4522 }
4523 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004524
Nate Begemanb9a47b82009-02-23 08:49:38 +00004525 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4526 // the shuffle mask. If a quad is scored as -1, that means that it contains
4527 // words from all 4 input quadwords.
4528 SDValue NewV;
4529 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004530 SmallVector<int, 8> MaskV;
4531 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4532 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004533 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004534 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1),
4535 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]);
4536 NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004537
Nate Begemanb9a47b82009-02-23 08:49:38 +00004538 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4539 // source words for the shuffle, to aid later transformations.
4540 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004541 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004542 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004543 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004544 if (idx != (int)i)
4545 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004546 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004547 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004548 AllWordsInNewV = false;
4549 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004550 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004551
Nate Begemanb9a47b82009-02-23 08:49:38 +00004552 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4553 if (AllWordsInNewV) {
4554 for (int i = 0; i != 8; ++i) {
4555 int idx = MaskVals[i];
4556 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004557 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004558 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004559 if ((idx != i) && idx < 4)
4560 pshufhw = false;
4561 if ((idx != i) && idx > 3)
4562 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004563 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004564 V1 = NewV;
4565 V2Used = false;
4566 BestLoQuad = 0;
4567 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004568 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004569
Nate Begemanb9a47b82009-02-23 08:49:38 +00004570 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4571 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004572 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004573 unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW;
4574 unsigned TargetMask = 0;
4575 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004576 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Bruno Cardoso Lopes3efc0772010-08-23 20:41:02 +00004577 TargetMask = pshufhw ? X86::getShufflePSHUFHWImmediate(NewV.getNode()):
4578 X86::getShufflePSHUFLWImmediate(NewV.getNode());
4579 V1 = NewV.getOperand(0);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004580 return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG);
Evan Cheng14b32e12007-12-11 01:46:18 +00004581 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004582 }
Eric Christopherfd179292009-08-27 18:07:15 +00004583
Nate Begemanb9a47b82009-02-23 08:49:38 +00004584 // If we have SSSE3, and all words of the result are from 1 input vector,
4585 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4586 // is present, fall back to case 4.
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00004587 if (Subtarget->hasSSSE3()) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004588 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004589
Nate Begemanb9a47b82009-02-23 08:49:38 +00004590 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004591 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004592 // mask, and elements that come from V1 in the V2 mask, so that the two
4593 // results can be OR'd together.
4594 bool TwoInputs = V1Used && V2Used;
4595 for (unsigned i = 0; i != 8; ++i) {
4596 int EltIdx = MaskVals[i] * 2;
4597 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004598 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4599 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004600 continue;
4601 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004602 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4603 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004604 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004605 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004606 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004607 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004608 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004609 if (!TwoInputs)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004610 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004611
Nate Begemanb9a47b82009-02-23 08:49:38 +00004612 // Calculate the shuffle mask for the second input, shuffle it, and
4613 // OR it with the first shuffled input.
4614 pshufbMask.clear();
4615 for (unsigned i = 0; i != 8; ++i) {
4616 int EltIdx = MaskVals[i] * 2;
4617 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004618 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4619 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004620 continue;
4621 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004622 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4623 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004624 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004625 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004626 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004627 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004628 MVT::v16i8, &pshufbMask[0], 16));
4629 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004630 return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004631 }
4632
4633 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4634 // and update MaskVals with new element order.
4635 BitVector InOrder(8);
4636 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004637 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004638 for (int i = 0; i != 4; ++i) {
4639 int idx = MaskVals[i];
4640 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004641 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004642 InOrder.set(i);
4643 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004644 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004645 InOrder.set(i);
4646 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004647 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004648 }
4649 }
4650 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004651 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004652 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004653 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004654
4655 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4656 NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16,
4657 NewV.getOperand(0),
4658 X86::getShufflePSHUFLWImmediate(NewV.getNode()),
4659 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004660 }
Eric Christopherfd179292009-08-27 18:07:15 +00004661
Nate Begemanb9a47b82009-02-23 08:49:38 +00004662 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4663 // and update MaskVals with the new element order.
4664 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004665 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004666 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004667 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004668 for (unsigned i = 4; i != 8; ++i) {
4669 int idx = MaskVals[i];
4670 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004671 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004672 InOrder.set(i);
4673 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004675 InOrder.set(i);
4676 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004678 }
4679 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004680 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004681 &MaskV[0]);
Bruno Cardoso Lopes8878e212010-08-24 01:16:15 +00004682
4683 if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3())
4684 NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16,
4685 NewV.getOperand(0),
4686 X86::getShufflePSHUFHWImmediate(NewV.getNode()),
4687 DAG);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004688 }
Eric Christopherfd179292009-08-27 18:07:15 +00004689
Nate Begemanb9a47b82009-02-23 08:49:38 +00004690 // In case BestHi & BestLo were both -1, which means each quadword has a word
4691 // from each of the four input quadwords, calculate the InOrder bitvector now
4692 // before falling through to the insert/extract cleanup.
4693 if (BestLoQuad == -1 && BestHiQuad == -1) {
4694 NewV = V1;
4695 for (int i = 0; i != 8; ++i)
4696 if (MaskVals[i] < 0 || MaskVals[i] == i)
4697 InOrder.set(i);
4698 }
Eric Christopherfd179292009-08-27 18:07:15 +00004699
Nate Begemanb9a47b82009-02-23 08:49:38 +00004700 // The other elements are put in the right place using pextrw and pinsrw.
4701 for (unsigned i = 0; i != 8; ++i) {
4702 if (InOrder[i])
4703 continue;
4704 int EltIdx = MaskVals[i];
4705 if (EltIdx < 0)
4706 continue;
4707 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004709 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004710 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004711 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004712 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004713 DAG.getIntPtrConstant(i));
4714 }
4715 return NewV;
4716}
4717
4718// v16i8 shuffles - Prefer shuffles in the following order:
4719// 1. [ssse3] 1 x pshufb
4720// 2. [ssse3] 2 x pshufb + 1 x por
4721// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4722static
Nate Begeman9008ca62009-04-27 18:41:29 +00004723SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004724 SelectionDAG &DAG,
4725 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004726 SDValue V1 = SVOp->getOperand(0);
4727 SDValue V2 = SVOp->getOperand(1);
4728 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004729 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004730 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004731
Nate Begemanb9a47b82009-02-23 08:49:38 +00004732 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004733 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004734 // present, fall back to case 3.
4735 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4736 bool V1Only = true;
4737 bool V2Only = true;
4738 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004739 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004740 if (EltIdx < 0)
4741 continue;
4742 if (EltIdx < 16)
4743 V2Only = false;
4744 else
4745 V1Only = false;
4746 }
Eric Christopherfd179292009-08-27 18:07:15 +00004747
Nate Begemanb9a47b82009-02-23 08:49:38 +00004748 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4749 if (TLI.getSubtarget()->hasSSSE3()) {
4750 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004751
Nate Begemanb9a47b82009-02-23 08:49:38 +00004752 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004753 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004754 //
4755 // Otherwise, we have elements from both input vectors, and must zero out
4756 // elements that come from V2 in the first mask, and V1 in the second mask
4757 // so that we can OR them together.
4758 bool TwoInputs = !(V1Only || V2Only);
4759 for (unsigned i = 0; i != 16; ++i) {
4760 int EltIdx = MaskVals[i];
4761 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004762 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004763 continue;
4764 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004765 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004766 }
4767 // If all the elements are from V2, assign it to V1 and return after
4768 // building the first pshufb.
4769 if (V2Only)
4770 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004771 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004772 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004773 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004774 if (!TwoInputs)
4775 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004776
Nate Begemanb9a47b82009-02-23 08:49:38 +00004777 // Calculate the shuffle mask for the second input, shuffle it, and
4778 // OR it with the first shuffled input.
4779 pshufbMask.clear();
4780 for (unsigned i = 0; i != 16; ++i) {
4781 int EltIdx = MaskVals[i];
4782 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004783 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004784 continue;
4785 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004786 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004787 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004788 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004789 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 MVT::v16i8, &pshufbMask[0], 16));
4791 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004792 }
Eric Christopherfd179292009-08-27 18:07:15 +00004793
Nate Begemanb9a47b82009-02-23 08:49:38 +00004794 // No SSSE3 - Calculate in place words and then fix all out of place words
4795 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4796 // the 16 different words that comprise the two doublequadword input vectors.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004797 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1);
4798 V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004799 SDValue NewV = V2Only ? V2 : V1;
4800 for (int i = 0; i != 8; ++i) {
4801 int Elt0 = MaskVals[i*2];
4802 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004803
Nate Begemanb9a47b82009-02-23 08:49:38 +00004804 // This word of the result is all undef, skip it.
4805 if (Elt0 < 0 && Elt1 < 0)
4806 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004807
Nate Begemanb9a47b82009-02-23 08:49:38 +00004808 // This word of the result is already in the correct place, skip it.
4809 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4810 continue;
4811 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4812 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004813
Nate Begemanb9a47b82009-02-23 08:49:38 +00004814 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4815 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4816 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004817
4818 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4819 // using a single extract together, load it and store it.
4820 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004821 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004822 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004823 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004824 DAG.getIntPtrConstant(i));
4825 continue;
4826 }
4827
Nate Begemanb9a47b82009-02-23 08:49:38 +00004828 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004829 // source byte is not also odd, shift the extracted word left 8 bits
4830 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004831 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004832 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004833 DAG.getIntPtrConstant(Elt1 / 2));
4834 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004836 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004837 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4839 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004840 }
4841 // If Elt0 is defined, extract it from the appropriate source. If the
4842 // source byte is not also even, shift the extracted word right 8 bits. If
4843 // Elt1 was also defined, OR the extracted values together before
4844 // inserting them in the result.
4845 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004846 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004847 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4848 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004849 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004850 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004851 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004852 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4853 DAG.getConstant(0x00FF, MVT::i16));
4854 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004855 : InsElt0;
4856 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004857 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004858 DAG.getIntPtrConstant(i));
4859 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004860 return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004861}
4862
Evan Cheng7a831ce2007-12-15 03:00:47 +00004863/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004864/// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be
Evan Cheng7a831ce2007-12-15 03:00:47 +00004865/// done when every pair / quad of shuffle mask elements point to elements in
4866/// the right sequence. e.g.
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004867/// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15>
Evan Cheng14b32e12007-12-11 01:46:18 +00004868static
Nate Begeman9008ca62009-04-27 18:41:29 +00004869SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00004870 SelectionDAG &DAG, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004871 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004872 SDValue V1 = SVOp->getOperand(0);
4873 SDValue V2 = SVOp->getOperand(1);
4874 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004875 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Bruno Cardoso Lopes0a7dd4f2010-09-08 18:12:31 +00004876 EVT NewVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004877 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004878 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004879 case MVT::v4f32: NewVT = MVT::v2f64; break;
4880 case MVT::v4i32: NewVT = MVT::v2i64; break;
4881 case MVT::v8i16: NewVT = MVT::v4i32; break;
4882 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004883 }
4884
Nate Begeman9008ca62009-04-27 18:41:29 +00004885 int Scale = NumElems / NewWidth;
4886 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004887 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004888 int StartIdx = -1;
4889 for (int j = 0; j < Scale; ++j) {
4890 int EltIdx = SVOp->getMaskElt(i+j);
4891 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004892 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004893 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004894 StartIdx = EltIdx - (EltIdx % Scale);
4895 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004896 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004897 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004898 if (StartIdx == -1)
4899 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004900 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004901 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004902 }
4903
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004904 V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, V1);
4905 V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004906 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004907}
4908
Evan Chengd880b972008-05-09 21:53:03 +00004909/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004910///
Owen Andersone50ed302009-08-10 22:56:29 +00004911static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004912 SDValue SrcOp, SelectionDAG &DAG,
4913 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004914 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004915 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004916 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004917 LD = dyn_cast<LoadSDNode>(SrcOp);
4918 if (!LD) {
4919 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4920 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004921 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Duncan Sandscdfad362010-11-03 12:17:33 +00004922 if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004923 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004924 SrcOp.getOperand(0).getOpcode() == ISD::BITCAST &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004925 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004926 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004927 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004928 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004929 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4930 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4931 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004932 SrcOp.getOperand(0)
4933 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004934 }
4935 }
4936 }
4937
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004938 return DAG.getNode(ISD::BITCAST, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004939 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004940 DAG.getNode(ISD::BITCAST, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004941 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004942}
4943
Evan Chengace3c172008-07-22 21:13:36 +00004944/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4945/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004946static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004947LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4948 SDValue V1 = SVOp->getOperand(0);
4949 SDValue V2 = SVOp->getOperand(1);
4950 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004951 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004952
Evan Chengace3c172008-07-22 21:13:36 +00004953 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004954 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004955 SmallVector<int, 8> Mask1(4U, -1);
4956 SmallVector<int, 8> PermMask;
4957 SVOp->getMask(PermMask);
4958
Evan Chengace3c172008-07-22 21:13:36 +00004959 unsigned NumHi = 0;
4960 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004961 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004962 int Idx = PermMask[i];
4963 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004964 Locs[i] = std::make_pair(-1, -1);
4965 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004966 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4967 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004968 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004969 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004970 NumLo++;
4971 } else {
4972 Locs[i] = std::make_pair(1, NumHi);
4973 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004974 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004975 NumHi++;
4976 }
4977 }
4978 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004979
Evan Chengace3c172008-07-22 21:13:36 +00004980 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004981 // If no more than two elements come from either vector. This can be
4982 // implemented with two shuffles. First shuffle gather the elements.
4983 // The second shuffle, which takes the first shuffle as both of its
4984 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004985 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004986
Nate Begeman9008ca62009-04-27 18:41:29 +00004987 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004988
Evan Chengace3c172008-07-22 21:13:36 +00004989 for (unsigned i = 0; i != 4; ++i) {
4990 if (Locs[i].first == -1)
4991 continue;
4992 else {
4993 unsigned Idx = (i < 2) ? 0 : 4;
4994 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004995 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004996 }
4997 }
4998
Nate Begeman9008ca62009-04-27 18:41:29 +00004999 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005000 } else if (NumLo == 3 || NumHi == 3) {
5001 // Otherwise, we must have three elements from one vector, call it X, and
5002 // one element from the other, call it Y. First, use a shufps to build an
5003 // intermediate vector with the one element from Y and the element from X
5004 // that will be in the same half in the final destination (the indexes don't
5005 // matter). Then, use a shufps to build the final vector, taking the half
5006 // containing the element from Y from the intermediate, and the other half
5007 // from X.
5008 if (NumHi == 3) {
5009 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00005010 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005011 std::swap(V1, V2);
5012 }
5013
5014 // Find the element from V2.
5015 unsigned HiIndex;
5016 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005017 int Val = PermMask[HiIndex];
5018 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005019 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005020 if (Val >= 4)
5021 break;
5022 }
5023
Nate Begeman9008ca62009-04-27 18:41:29 +00005024 Mask1[0] = PermMask[HiIndex];
5025 Mask1[1] = -1;
5026 Mask1[2] = PermMask[HiIndex^1];
5027 Mask1[3] = -1;
5028 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005029
5030 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005031 Mask1[0] = PermMask[0];
5032 Mask1[1] = PermMask[1];
5033 Mask1[2] = HiIndex & 1 ? 6 : 4;
5034 Mask1[3] = HiIndex & 1 ? 4 : 6;
5035 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005036 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00005037 Mask1[0] = HiIndex & 1 ? 2 : 0;
5038 Mask1[1] = HiIndex & 1 ? 0 : 2;
5039 Mask1[2] = PermMask[2];
5040 Mask1[3] = PermMask[3];
5041 if (Mask1[2] >= 0)
5042 Mask1[2] += 4;
5043 if (Mask1[3] >= 0)
5044 Mask1[3] += 4;
5045 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00005046 }
Evan Chengace3c172008-07-22 21:13:36 +00005047 }
5048
5049 // Break it into (shuffle shuffle_hi, shuffle_lo).
5050 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00005051 SmallVector<int,8> LoMask(4U, -1);
5052 SmallVector<int,8> HiMask(4U, -1);
5053
5054 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00005055 unsigned MaskIdx = 0;
5056 unsigned LoIdx = 0;
5057 unsigned HiIdx = 2;
5058 for (unsigned i = 0; i != 4; ++i) {
5059 if (i == 2) {
5060 MaskPtr = &HiMask;
5061 MaskIdx = 1;
5062 LoIdx = 0;
5063 HiIdx = 2;
5064 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005065 int Idx = PermMask[i];
5066 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00005067 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00005068 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00005069 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005070 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005071 LoIdx++;
5072 } else {
5073 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00005074 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00005075 HiIdx++;
5076 }
5077 }
5078
Nate Begeman9008ca62009-04-27 18:41:29 +00005079 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
5080 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
5081 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00005082 for (unsigned i = 0; i != 4; ++i) {
5083 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005084 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00005085 } else {
5086 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00005087 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00005088 }
5089 }
Nate Begeman9008ca62009-04-27 18:41:29 +00005090 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00005091}
5092
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005093static bool MayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005094 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005095 V = V.getOperand(0);
5096 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5097 V = V.getOperand(0);
5098 if (MayFoldLoad(V))
5099 return true;
5100 return false;
5101}
5102
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005103// FIXME: the version above should always be used. Since there's
5104// a bug where several vector shuffles can't be folded because the
5105// DAG is not updated during lowering and a node claims to have two
5106// uses while it only has one, use this version, and let isel match
5107// another instruction if the load really happens to have more than
5108// one use. Remove this version after this bug get fixed.
Evan Cheng835580f2010-10-07 20:50:20 +00005109// rdar://8434668, PR8156
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005110static bool RelaxedMayFoldVectorLoad(SDValue V) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005111 if (V.hasOneUse() && V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005112 V = V.getOperand(0);
5113 if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5114 V = V.getOperand(0);
5115 if (ISD::isNormalLoad(V.getNode()))
5116 return true;
5117 return false;
5118}
5119
5120/// CanFoldShuffleIntoVExtract - Check if the current shuffle is used by
5121/// a vector extract, and if both can be later optimized into a single load.
5122/// This is done in visitEXTRACT_VECTOR_ELT and the conditions are checked
5123/// here because otherwise a target specific shuffle node is going to be
5124/// emitted for this shuffle, and the optimization not done.
5125/// FIXME: This is probably not the best approach, but fix the problem
5126/// until the right path is decided.
5127static
5128bool CanXFormVExtractWithShuffleIntoLoad(SDValue V, SelectionDAG &DAG,
5129 const TargetLowering &TLI) {
5130 EVT VT = V.getValueType();
5131 ShuffleVectorSDNode *SVOp = dyn_cast<ShuffleVectorSDNode>(V);
5132
5133 // Be sure that the vector shuffle is present in a pattern like this:
5134 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), c) -> (f32 load $addr)
5135 if (!V.hasOneUse())
5136 return false;
5137
5138 SDNode *N = *V.getNode()->use_begin();
5139 if (N->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
5140 return false;
5141
5142 SDValue EltNo = N->getOperand(1);
5143 if (!isa<ConstantSDNode>(EltNo))
5144 return false;
5145
5146 // If the bit convert changed the number of elements, it is unsafe
5147 // to examine the mask.
5148 bool HasShuffleIntoBitcast = false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005149 if (V.getOpcode() == ISD::BITCAST) {
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005150 EVT SrcVT = V.getOperand(0).getValueType();
5151 if (SrcVT.getVectorNumElements() != VT.getVectorNumElements())
5152 return false;
5153 V = V.getOperand(0);
5154 HasShuffleIntoBitcast = true;
5155 }
5156
5157 // Select the input vector, guarding against out of range extract vector.
5158 unsigned NumElems = VT.getVectorNumElements();
5159 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5160 int Idx = (Elt > NumElems) ? -1 : SVOp->getMaskElt(Elt);
5161 V = (Idx < (int)NumElems) ? V.getOperand(0) : V.getOperand(1);
5162
5163 // Skip one more bit_convert if necessary
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005164 if (V.getOpcode() == ISD::BITCAST)
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005165 V = V.getOperand(0);
5166
5167 if (ISD::isNormalLoad(V.getNode())) {
5168 // Is the original load suitable?
5169 LoadSDNode *LN0 = cast<LoadSDNode>(V);
5170
5171 // FIXME: avoid the multi-use bug that is preventing lots of
5172 // of foldings to be detected, this is still wrong of course, but
5173 // give the temporary desired behavior, and if it happens that
5174 // the load has real more uses, during isel it will not fold, and
5175 // will generate poor code.
5176 if (!LN0 || LN0->isVolatile()) // || !LN0->hasOneUse()
5177 return false;
5178
5179 if (!HasShuffleIntoBitcast)
5180 return true;
5181
5182 // If there's a bitcast before the shuffle, check if the load type and
5183 // alignment is valid.
5184 unsigned Align = LN0->getAlignment();
5185 unsigned NewAlign =
5186 TLI.getTargetData()->getABITypeAlignment(
5187 VT.getTypeForEVT(*DAG.getContext()));
5188
5189 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT))
5190 return false;
5191 }
5192
5193 return true;
5194}
5195
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005196static
Evan Cheng835580f2010-10-07 20:50:20 +00005197SDValue getMOVDDup(SDValue &Op, DebugLoc &dl, SDValue V1, SelectionDAG &DAG) {
5198 EVT VT = Op.getValueType();
5199
5200 // Canonizalize to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005201 V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1);
5202 return DAG.getNode(ISD::BITCAST, dl, VT,
Evan Cheng835580f2010-10-07 20:50:20 +00005203 getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64,
5204 V1, DAG));
5205}
5206
5207static
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005208SDValue getMOVLowToHigh(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG,
5209 bool HasSSE2) {
5210 SDValue V1 = Op.getOperand(0);
5211 SDValue V2 = Op.getOperand(1);
5212 EVT VT = Op.getValueType();
5213
5214 assert(VT != MVT::v2i64 && "unsupported shuffle type");
5215
5216 if (HasSSE2 && VT == MVT::v2f64)
5217 return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG);
5218
5219 // v4f32 or v4i32
5220 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V2, DAG);
5221}
5222
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005223static
5224SDValue getMOVHighToLow(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG) {
5225 SDValue V1 = Op.getOperand(0);
5226 SDValue V2 = Op.getOperand(1);
5227 EVT VT = Op.getValueType();
5228
5229 assert((VT == MVT::v4i32 || VT == MVT::v4f32) &&
5230 "unsupported shuffle type");
5231
5232 if (V2.getOpcode() == ISD::UNDEF)
5233 V2 = V1;
5234
5235 // v4i32 or v4f32
5236 return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG);
5237}
5238
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005239static
5240SDValue getMOVLP(SDValue &Op, DebugLoc &dl, SelectionDAG &DAG, bool HasSSE2) {
5241 SDValue V1 = Op.getOperand(0);
5242 SDValue V2 = Op.getOperand(1);
5243 EVT VT = Op.getValueType();
5244 unsigned NumElems = VT.getVectorNumElements();
5245
5246 // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second
5247 // operand of these instructions is only memory, so check if there's a
5248 // potencial load folding here, otherwise use SHUFPS or MOVSD to match the
5249 // same masks.
5250 bool CanFoldLoad = false;
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005251
Bruno Cardoso Lopesd00bfe12010-09-02 02:35:51 +00005252 // Trivial case, when V2 comes from a load.
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005253 if (MayFoldVectorLoad(V2))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005254 CanFoldLoad = true;
5255
5256 // When V1 is a load, it can be folded later into a store in isel, example:
5257 // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1)
5258 // turns into:
5259 // (MOVLPSmr addr:$src1, VR128:$src2)
5260 // So, recognize this potential and also use MOVLPS or MOVLPD
Bruno Cardoso Lopes2a446062010-09-03 20:20:02 +00005261 if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op))
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00005262 CanFoldLoad = true;
5263
5264 if (CanFoldLoad) {
5265 if (HasSSE2 && NumElems == 2)
5266 return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG);
5267
5268 if (NumElems == 4)
5269 return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG);
5270 }
5271
5272 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5273 // movl and movlp will both match v2i64, but v2i64 is never matched by
5274 // movl earlier because we make it strict to avoid messing with the movlp load
5275 // folding logic (see the code above getMOVLP call). Match it here then,
5276 // this is horrible, but will stay like this until we move all shuffle
5277 // matching to x86 specific nodes. Note that for the 1st condition all
5278 // types are matched with movsd.
5279 if ((HasSSE2 && NumElems == 2) || !X86::isMOVLMask(SVOp))
5280 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5281 else if (HasSSE2)
5282 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5283
5284
5285 assert(VT != MVT::v4i32 && "unsupported shuffle type");
5286
5287 // Invert the operand order and use SHUFPS to match it.
5288 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V2, V1,
5289 X86::getShuffleSHUFImmediate(SVOp), DAG);
5290}
5291
Bruno Cardoso Lopesbe8b0842010-09-03 20:10:35 +00005292static inline unsigned getUNPCKLOpcode(EVT VT) {
5293 switch(VT.getSimpleVT().SimpleTy) {
5294 case MVT::v4i32: return X86ISD::PUNPCKLDQ;
5295 case MVT::v2i64: return X86ISD::PUNPCKLQDQ;
5296 case MVT::v4f32: return X86ISD::UNPCKLPS;
5297 case MVT::v2f64: return X86ISD::UNPCKLPD;
5298 case MVT::v16i8: return X86ISD::PUNPCKLBW;
5299 case MVT::v8i16: return X86ISD::PUNPCKLWD;
5300 default:
5301 llvm_unreachable("Unknow type for unpckl");
5302 }
5303 return 0;
5304}
5305
5306static inline unsigned getUNPCKHOpcode(EVT VT) {
5307 switch(VT.getSimpleVT().SimpleTy) {
5308 case MVT::v4i32: return X86ISD::PUNPCKHDQ;
5309 case MVT::v2i64: return X86ISD::PUNPCKHQDQ;
5310 case MVT::v4f32: return X86ISD::UNPCKHPS;
5311 case MVT::v2f64: return X86ISD::UNPCKHPD;
5312 case MVT::v16i8: return X86ISD::PUNPCKHBW;
5313 case MVT::v8i16: return X86ISD::PUNPCKHWD;
5314 default:
5315 llvm_unreachable("Unknow type for unpckh");
5316 }
5317 return 0;
5318}
5319
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005320static
5321SDValue NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG,
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005322 const TargetLowering &TLI,
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005323 const X86Subtarget *Subtarget) {
5324 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5325 EVT VT = Op.getValueType();
5326 DebugLoc dl = Op.getDebugLoc();
5327 SDValue V1 = Op.getOperand(0);
5328 SDValue V2 = Op.getOperand(1);
5329
5330 if (isZeroShuffle(SVOp))
5331 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
5332
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005333 // Handle splat operations
5334 if (SVOp->isSplat()) {
5335 // Special case, this is the only place now where it's
5336 // allowed to return a vector_shuffle operation without
5337 // using a target specific node, because *hopefully* it
5338 // will be optimized away by the dag combiner.
5339 if (VT.getVectorNumElements() <= 4 &&
5340 CanXFormVExtractWithShuffleIntoLoad(Op, DAG, TLI))
5341 return Op;
5342
5343 // Handle splats by matching through known masks
5344 if (VT.getVectorNumElements() <= 4)
5345 return SDValue();
5346
Evan Cheng835580f2010-10-07 20:50:20 +00005347 // Canonicalize all of the remaining to v4f32.
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005348 return PromoteSplat(SVOp, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005349 }
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005350
5351 // If the shuffle can be profitably rewritten as a narrower shuffle, then
5352 // do it!
5353 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
5354 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5355 if (NewOp.getNode())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005356 return DAG.getNode(ISD::BITCAST, dl, VT, NewOp);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005357 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
5358 // FIXME: Figure out a cleaner way to do this.
5359 // Try to make use of movq to zero out the top part.
5360 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
5361 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5362 if (NewOp.getNode()) {
5363 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
5364 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
5365 DAG, Subtarget, dl);
5366 }
5367 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
5368 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, dl);
5369 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
5370 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
5371 DAG, Subtarget, dl);
5372 }
5373 }
5374 return SDValue();
5375}
5376
Dan Gohman475871a2008-07-27 21:46:04 +00005377SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005378X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00005379 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00005380 SDValue V1 = Op.getOperand(0);
5381 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005382 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005383 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00005384 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005385 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005386 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
5387 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00005388 bool V1IsSplat = false;
5389 bool V2IsSplat = false;
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005390 bool HasSSE2 = Subtarget->hasSSE2() || Subtarget->hasAVX();
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005391 bool HasSSE3 = Subtarget->hasSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005392 bool HasSSSE3 = Subtarget->hasSSSE3() || Subtarget->hasAVX();
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005393 MachineFunction &MF = DAG.getMachineFunction();
5394 bool OptForSize = MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005395
Dale Johannesen0488fb62010-09-30 23:57:10 +00005396 // Shuffle operations on MMX not supported.
5397 if (isMMX)
Bruno Cardoso Lopes58277b12010-09-07 18:41:45 +00005398 return Op;
5399
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005400 // Vector shuffle lowering takes 3 steps:
5401 //
5402 // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable
5403 // narrowing and commutation of operands should be handled.
5404 // 2) Matching of shuffles with known shuffle masks to x86 target specific
5405 // shuffle nodes.
5406 // 3) Rewriting of unmatched masks into new generic shuffle operations,
5407 // so the shuffle can be broken into other shuffles and the legalizer can
5408 // try the lowering again.
5409 //
5410 // The general ideia is that no vector_shuffle operation should be left to
5411 // be matched during isel, all of them must be converted to a target specific
5412 // node here.
Bruno Cardoso Lopes0d1340b2010-09-07 20:20:27 +00005413
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005414 // Normalize the input vectors. Here splats, zeroed vectors, profitable
5415 // narrowing and commutation of operands should be handled. The actual code
5416 // doesn't include all of those, work in progress...
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005417 SDValue NewOp = NormalizeVectorShuffle(Op, DAG, *this, Subtarget);
Bruno Cardoso Lopes90462b42010-09-07 21:03:14 +00005418 if (NewOp.getNode())
5419 return NewOp;
Eric Christopherfd179292009-08-27 18:07:15 +00005420
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005421 // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and
5422 // unpckh_undef). Only use pshufd if speed is more important than size.
5423 if (OptForSize && X86::isUNPCKL_v_undef_Mask(SVOp))
5424 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5425 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5426 if (OptForSize && X86::isUNPCKH_v_undef_Mask(SVOp))
5427 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5428 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
Bruno Cardoso Lopes3722f002010-09-02 05:23:12 +00005429
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005430 if (X86::isMOVDDUPMask(SVOp) && HasSSE3 && V2IsUndef &&
Dale Johannesen0488fb62010-09-30 23:57:10 +00005431 RelaxedMayFoldVectorLoad(V1))
Evan Cheng835580f2010-10-07 20:50:20 +00005432 return getMOVDDup(Op, dl, V1, DAG);
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005433
Dale Johannesen0488fb62010-09-30 23:57:10 +00005434 if (X86::isMOVHLPS_v_undef_Mask(SVOp))
Bruno Cardoso Lopes1485cc22010-09-08 17:43:25 +00005435 return getMOVHighToLow(Op, dl, DAG);
5436
5437 // Use to match splats
5438 if (HasSSE2 && X86::isUNPCKHMask(SVOp) && V2IsUndef &&
5439 (VT == MVT::v2f64 || VT == MVT::v2i64))
5440 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5441
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005442 if (X86::isPSHUFDMask(SVOp)) {
5443 // The actual implementation will match the mask in the if above and then
5444 // during isel it can match several different instructions, not only pshufd
5445 // as its name says, sad but true, emulate the behavior for now...
5446 if (X86::isMOVDDUPMask(SVOp) && ((VT == MVT::v4f32 || VT == MVT::v2i64)))
5447 return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG);
5448
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005449 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5450
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005451 if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005452 return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG);
5453
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005454 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes7338bbd2010-08-25 02:35:37 +00005455 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V1,
5456 TargetMask, DAG);
5457
5458 if (VT == MVT::v4f32)
5459 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V1,
5460 TargetMask, DAG);
5461 }
Eric Christopherfd179292009-08-27 18:07:15 +00005462
Evan Chengf26ffe92008-05-29 08:22:04 +00005463 // Check if this can be converted into a logical shift.
5464 bool isLeft = false;
5465 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00005466 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00005467 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00005468 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00005469 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005470 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00005471 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005472 EVT EltVT = VT.getVectorElementType();
5473 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005474 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005475 }
Eric Christopherfd179292009-08-27 18:07:15 +00005476
Nate Begeman9008ca62009-04-27 18:41:29 +00005477 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00005478 if (V1IsUndef)
5479 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00005480 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00005481 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Dale Johannesen0488fb62010-09-30 23:57:10 +00005482 if (!X86::isMOVLPMask(SVOp)) {
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005483 if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64))
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005484 return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG);
5485
Bruno Cardoso Lopes4783a3e2010-09-01 22:59:03 +00005486 if (VT == MVT::v4i32 || VT == MVT::v4f32)
Bruno Cardoso Lopes20a07f42010-08-31 02:26:40 +00005487 return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG);
5488 }
Evan Cheng7e2ff772008-05-08 00:57:18 +00005489 }
Eric Christopherfd179292009-08-27 18:07:15 +00005490
Nate Begeman9008ca62009-04-27 18:41:29 +00005491 // FIXME: fold these into legal mask.
Dale Johannesen0488fb62010-09-30 23:57:10 +00005492 if (X86::isMOVLHPSMask(SVOp) && !X86::isUNPCKLMask(SVOp))
5493 return getMOVLowToHigh(Op, dl, DAG, HasSSE2);
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00005494
Dale Johannesen0488fb62010-09-30 23:57:10 +00005495 if (X86::isMOVHLPSMask(SVOp))
5496 return getMOVHighToLow(Op, dl, DAG);
Bruno Cardoso Lopes7ff30bb2010-08-31 21:38:49 +00005497
Dale Johannesen0488fb62010-09-30 23:57:10 +00005498 if (X86::isMOVSHDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5499 return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes5023ef22010-08-31 22:22:11 +00005500
Dale Johannesen0488fb62010-09-30 23:57:10 +00005501 if (X86::isMOVSLDUPMask(SVOp) && HasSSE3 && V2IsUndef && NumElems == 4)
5502 return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG);
Bruno Cardoso Lopes013bb3d2010-08-31 22:35:05 +00005503
Dale Johannesen0488fb62010-09-30 23:57:10 +00005504 if (X86::isMOVLPMask(SVOp))
5505 return getMOVLP(Op, dl, DAG, HasSSE2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005506
Nate Begeman9008ca62009-04-27 18:41:29 +00005507 if (ShouldXformToMOVHLPS(SVOp) ||
5508 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
5509 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005510
Evan Chengf26ffe92008-05-29 08:22:04 +00005511 if (isShift) {
5512 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00005513 EVT EltVT = VT.getVectorElementType();
5514 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00005515 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00005516 }
Eric Christopherfd179292009-08-27 18:07:15 +00005517
Evan Cheng9eca5e82006-10-25 21:49:50 +00005518 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00005519 // FIXME: This should also accept a bitcast of a splat? Be careful, not
5520 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00005521 V1IsSplat = isSplatVector(V1.getNode());
5522 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005523
Chris Lattner8a594482007-11-25 00:24:49 +00005524 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00005525 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005526 Op = CommuteVectorShuffle(SVOp, DAG);
5527 SVOp = cast<ShuffleVectorSDNode>(Op);
5528 V1 = SVOp->getOperand(0);
5529 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00005530 std::swap(V1IsSplat, V2IsSplat);
5531 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005532 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00005533 }
5534
Nate Begeman9008ca62009-04-27 18:41:29 +00005535 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
5536 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00005537 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00005538 return V1;
5539 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
5540 // the instruction selector will not match, so get a canonical MOVL with
5541 // swapped operands to undo the commute.
5542 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00005543 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005544
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005545 if (X86::isUNPCKLMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005546 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V2, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005547
5548 if (X86::isUNPCKHMask(SVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005549 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V2, DAG);
Evan Chenge1113032006-10-04 18:33:38 +00005550
Evan Cheng9bbbb982006-10-25 20:48:19 +00005551 if (V2IsSplat) {
5552 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005553 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00005554 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00005555 SDValue NewMask = NormalizeMask(SVOp, DAG);
5556 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
5557 if (NSVOp != SVOp) {
5558 if (X86::isUNPCKLMask(NSVOp, true)) {
5559 return NewMask;
5560 } else if (X86::isUNPCKHMask(NSVOp, true)) {
5561 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005562 }
5563 }
5564 }
5565
Evan Cheng9eca5e82006-10-25 21:49:50 +00005566 if (Commuted) {
5567 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00005568 // FIXME: this seems wrong.
5569 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
5570 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005571
5572 if (X86::isUNPCKLMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005573 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V2, V1, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +00005574
5575 if (X86::isUNPCKHMask(NewSVOp))
Dale Johannesen0488fb62010-09-30 23:57:10 +00005576 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V2, V1, DAG);
Evan Cheng9eca5e82006-10-25 21:49:50 +00005577 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005578
Nate Begeman9008ca62009-04-27 18:41:29 +00005579 // Normalize the node to match x86 shuffle ops if needed
Dale Johannesen0488fb62010-09-30 23:57:10 +00005580 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
Nate Begeman9008ca62009-04-27 18:41:29 +00005581 return CommuteVectorShuffle(SVOp, DAG);
5582
Bruno Cardoso Lopes7256e222010-09-03 23:24:06 +00005583 // The checks below are all present in isShuffleMaskLegal, but they are
5584 // inlined here right now to enable us to directly emit target specific
5585 // nodes, and remove one by one until they don't return Op anymore.
5586 SmallVector<int, 16> M;
5587 SVOp->getMask(M);
5588
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +00005589 if (isPALIGNRMask(M, VT, HasSSSE3))
5590 return getTargetShuffleNode(X86ISD::PALIGN, dl, VT, V1, V2,
5591 X86::getShufflePALIGNRImmediate(SVOp),
5592 DAG);
5593
Bruno Cardoso Lopesc800c0d2010-09-04 02:02:14 +00005594 if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) &&
5595 SVOp->getSplatIndex() == 0 && V2IsUndef) {
5596 if (VT == MVT::v2f64)
5597 return getTargetShuffleNode(X86ISD::UNPCKLPD, dl, VT, V1, V1, DAG);
5598 if (VT == MVT::v2i64)
5599 return getTargetShuffleNode(X86ISD::PUNPCKLQDQ, dl, VT, V1, V1, DAG);
5600 }
5601
Bruno Cardoso Lopesbbfc3102010-09-04 01:36:45 +00005602 if (isPSHUFHWMask(M, VT))
5603 return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1,
5604 X86::getShufflePSHUFHWImmediate(SVOp),
5605 DAG);
5606
5607 if (isPSHUFLWMask(M, VT))
5608 return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1,
5609 X86::getShufflePSHUFLWImmediate(SVOp),
5610 DAG);
5611
Bruno Cardoso Lopes4c827f52010-09-04 01:22:57 +00005612 if (isSHUFPMask(M, VT)) {
5613 unsigned TargetMask = X86::getShuffleSHUFImmediate(SVOp);
5614 if (VT == MVT::v4f32 || VT == MVT::v4i32)
5615 return getTargetShuffleNode(X86ISD::SHUFPS, dl, VT, V1, V2,
5616 TargetMask, DAG);
5617 if (VT == MVT::v2f64 || VT == MVT::v2i64)
5618 return getTargetShuffleNode(X86ISD::SHUFPD, dl, VT, V1, V2,
5619 TargetMask, DAG);
5620 }
5621
Bruno Cardoso Lopesa22c8452010-09-04 00:39:43 +00005622 if (X86::isUNPCKL_v_undef_Mask(SVOp))
5623 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5624 return getTargetShuffleNode(getUNPCKLOpcode(VT), dl, VT, V1, V1, DAG);
5625 if (X86::isUNPCKH_v_undef_Mask(SVOp))
5626 if (VT != MVT::v2i64 && VT != MVT::v2f64)
5627 return getTargetShuffleNode(getUNPCKHOpcode(VT), dl, VT, V1, V1, DAG);
5628
Evan Cheng14b32e12007-12-11 01:46:18 +00005629 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00005630 if (VT == MVT::v8i16) {
Bruno Cardoso Lopesbf8154a2010-08-21 01:32:18 +00005631 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005632 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00005633 return NewOp;
5634 }
5635
Owen Anderson825b72b2009-08-11 20:47:22 +00005636 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00005637 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00005638 if (NewOp.getNode())
5639 return NewOp;
5640 }
Eric Christopherfd179292009-08-27 18:07:15 +00005641
Dale Johannesen0488fb62010-09-30 23:57:10 +00005642 // Handle all 4 wide cases with a number of shuffles.
5643 if (NumElems == 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00005644 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005645
Dan Gohman475871a2008-07-27 21:46:04 +00005646 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005647}
5648
Dan Gohman475871a2008-07-27 21:46:04 +00005649SDValue
5650X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00005651 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005652 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005653 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005654 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005655 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005656 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005657 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005658 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005659 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005660 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00005661 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
5662 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
5663 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005664 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5665 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005666 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005667 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00005668 Op.getOperand(0)),
5669 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005670 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005671 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00005672 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00005673 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005674 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00005675 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00005676 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
5677 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005678 // result has a single use which is a store or a bitcast to i32. And in
5679 // the case of a store, it's not worth it if the index is a constant 0,
5680 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00005681 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00005682 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00005683 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00005684 if ((User->getOpcode() != ISD::STORE ||
5685 (isa<ConstantSDNode>(Op.getOperand(1)) &&
5686 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005687 (User->getOpcode() != ISD::BITCAST ||
Owen Anderson825b72b2009-08-11 20:47:22 +00005688 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00005689 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00005690 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005691 DAG.getNode(ISD::BITCAST, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00005692 Op.getOperand(0)),
5693 Op.getOperand(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005694 return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract);
Owen Anderson825b72b2009-08-11 20:47:22 +00005695 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00005696 // ExtractPS works with constant index.
5697 if (isa<ConstantSDNode>(Op.getOperand(1)))
5698 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005699 }
Dan Gohman475871a2008-07-27 21:46:04 +00005700 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005701}
5702
5703
Dan Gohman475871a2008-07-27 21:46:04 +00005704SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005705X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
5706 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005707 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00005708 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005709
Evan Cheng62a3f152008-03-24 21:52:23 +00005710 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005711 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005712 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00005713 return Res;
5714 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00005715
Owen Andersone50ed302009-08-10 22:56:29 +00005716 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005717 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005718 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005719 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00005720 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005721 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00005722 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00005723 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
5724 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005725 DAG.getNode(ISD::BITCAST, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005726 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00005727 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005728 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00005729 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00005730 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005731 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00005732 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00005733 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00005734 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005735 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005736 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005737 if (Idx == 0)
5738 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00005739
Evan Cheng0db9fe62006-04-25 20:13:52 +00005740 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00005741 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005742 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005743 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005744 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005745 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005746 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00005747 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005748 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
5749 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
5750 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005751 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005752 if (Idx == 0)
5753 return Op;
5754
5755 // UNPCKHPD the element to the lowest double word, then movsd.
5756 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
5757 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00005758 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00005759 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00005760 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00005761 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00005762 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00005763 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005764 }
5765
Dan Gohman475871a2008-07-27 21:46:04 +00005766 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005767}
5768
Dan Gohman475871a2008-07-27 21:46:04 +00005769SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005770X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
5771 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005772 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005773 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005774 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005775
Dan Gohman475871a2008-07-27 21:46:04 +00005776 SDValue N0 = Op.getOperand(0);
5777 SDValue N1 = Op.getOperand(1);
5778 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00005779
Dan Gohman8a55ce42009-09-23 21:02:20 +00005780 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00005781 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005782 unsigned Opc;
5783 if (VT == MVT::v8i16)
5784 Opc = X86ISD::PINSRW;
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005785 else if (VT == MVT::v16i8)
5786 Opc = X86ISD::PINSRB;
5787 else
5788 Opc = X86ISD::PINSRB;
5789
Nate Begeman14d12ca2008-02-11 04:19:36 +00005790 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5791 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005792 if (N1.getValueType() != MVT::i32)
5793 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5794 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005795 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005796 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005797 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005798 // Bits [7:6] of the constant are the source select. This will always be
5799 // zero here. The DAG Combiner may combine an extract_elt index into these
5800 // bits. For example (insert (extract, 3), 2) could be matched by putting
5801 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005802 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005803 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005804 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005805 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005806 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005807 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005808 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005809 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005810 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005811 // PINSR* works with constant index.
5812 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005813 }
Dan Gohman475871a2008-07-27 21:46:04 +00005814 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005815}
5816
Dan Gohman475871a2008-07-27 21:46:04 +00005817SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005818X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005819 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005820 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005821
5822 if (Subtarget->hasSSE41())
5823 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5824
Dan Gohman8a55ce42009-09-23 21:02:20 +00005825 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005826 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005827
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005828 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005829 SDValue N0 = Op.getOperand(0);
5830 SDValue N1 = Op.getOperand(1);
5831 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005832
Dan Gohman8a55ce42009-09-23 21:02:20 +00005833 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005834 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5835 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005836 if (N1.getValueType() != MVT::i32)
5837 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5838 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005839 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesen0488fb62010-09-30 23:57:10 +00005840 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005841 }
Dan Gohman475871a2008-07-27 21:46:04 +00005842 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005843}
5844
Dan Gohman475871a2008-07-27 21:46:04 +00005845SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005846X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005847 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00005848
Chris Lattnerf172ecd2010-07-04 23:07:25 +00005849 if (Op.getValueType() == MVT::v1i64 &&
5850 Op.getOperand(0).getValueType() == MVT::i64)
Owen Anderson825b72b2009-08-11 20:47:22 +00005851 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005852
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Dale Johannesen0488fb62010-09-30 23:57:10 +00005854 assert(Op.getValueType().getSimpleVT().getSizeInBits() == 128 &&
5855 "Expected an SSE type!");
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005856 return DAG.getNode(ISD::BITCAST, dl, Op.getValueType(),
Dale Johannesen0488fb62010-09-30 23:57:10 +00005857 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005858}
5859
David Greene91585092011-01-26 15:38:49 +00005860// Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in
5861// a simple subregister reference or explicit instructions to grab
5862// upper bits of a vector.
5863SDValue
5864X86TargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5865 if (Subtarget->hasAVX()) {
5866 // TODO
5867 }
5868 return SDValue();
5869}
5870
David Greenecfe33c42011-01-26 19:13:22 +00005871// Lower a node with an INSERT_SUBVECTOR opcode. This may result in a
5872// simple superregister reference or explicit instructions to insert
5873// the upper bits of a vector.
5874SDValue
5875X86TargetLowering::LowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const {
5876 if (Subtarget->hasAVX()) {
5877 DebugLoc dl = Op.getNode()->getDebugLoc();
5878 SDValue Vec = Op.getNode()->getOperand(0);
5879 SDValue SubVec = Op.getNode()->getOperand(1);
5880 SDValue Idx = Op.getNode()->getOperand(2);
5881
5882 if (Op.getNode()->getValueType(0).getSizeInBits() == 256
5883 && SubVec.getNode()->getValueType(0).getSizeInBits() == 128) {
5884 // TODO
5885 }
5886 }
5887 return SDValue();
5888}
5889
Bill Wendling056292f2008-09-16 21:48:12 +00005890// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5891// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5892// one of the above mentioned nodes. It has to be wrapped because otherwise
5893// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5894// be used to form addressing mode. These wrapped nodes will be selected
5895// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005896SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005897X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005898 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005899
Chris Lattner41621a22009-06-26 19:22:52 +00005900 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5901 // global base reg.
5902 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005903 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005904 CodeModel::Model M = getTargetMachine().getCodeModel();
5905
Chris Lattner4f066492009-07-11 20:29:19 +00005906 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005907 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005908 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005909 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005910 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005911 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005912 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005913
Evan Cheng1606e8e2009-03-13 07:51:59 +00005914 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005915 CP->getAlignment(),
5916 CP->getOffset(), OpFlag);
5917 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005918 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005919 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005920 if (OpFlag) {
5921 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005922 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005923 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005924 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005925 }
5926
5927 return Result;
5928}
5929
Dan Gohmand858e902010-04-17 15:26:15 +00005930SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005931 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005932
Chris Lattner18c59872009-06-27 04:16:01 +00005933 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5934 // global base reg.
5935 unsigned char OpFlag = 0;
5936 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005937 CodeModel::Model M = getTargetMachine().getCodeModel();
5938
Chris Lattner4f066492009-07-11 20:29:19 +00005939 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005940 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005941 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005942 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005943 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005944 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005945 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005946
Chris Lattner18c59872009-06-27 04:16:01 +00005947 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5948 OpFlag);
5949 DebugLoc DL = JT->getDebugLoc();
5950 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005951
Chris Lattner18c59872009-06-27 04:16:01 +00005952 // With PIC, the address is actually $g + Offset.
Chris Lattner1e61e692010-11-15 02:46:57 +00005953 if (OpFlag)
Chris Lattner18c59872009-06-27 04:16:01 +00005954 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5955 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005956 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005957 Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005958
Chris Lattner18c59872009-06-27 04:16:01 +00005959 return Result;
5960}
5961
5962SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005963X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005964 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005965
Chris Lattner18c59872009-06-27 04:16:01 +00005966 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5967 // global base reg.
5968 unsigned char OpFlag = 0;
5969 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005970 CodeModel::Model M = getTargetMachine().getCodeModel();
5971
Chris Lattner4f066492009-07-11 20:29:19 +00005972 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005973 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005974 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005975 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005976 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005977 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005978 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005979
Chris Lattner18c59872009-06-27 04:16:01 +00005980 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005981
Chris Lattner18c59872009-06-27 04:16:01 +00005982 DebugLoc DL = Op.getDebugLoc();
5983 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005984
5985
Chris Lattner18c59872009-06-27 04:16:01 +00005986 // With PIC, the address is actually $g + Offset.
5987 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005988 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005989 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5990 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005991 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005992 Result);
5993 }
Eric Christopherfd179292009-08-27 18:07:15 +00005994
Chris Lattner18c59872009-06-27 04:16:01 +00005995 return Result;
5996}
5997
Dan Gohman475871a2008-07-27 21:46:04 +00005998SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005999X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00006000 // Create the TargetBlockAddressAddress node.
6001 unsigned char OpFlags =
6002 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00006003 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00006004 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00006005 DebugLoc dl = Op.getDebugLoc();
6006 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
6007 /*isTarget=*/true, OpFlags);
6008
Dan Gohmanf705adb2009-10-30 01:28:02 +00006009 if (Subtarget->isPICStyleRIPRel() &&
6010 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00006011 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6012 else
6013 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00006014
Dan Gohman29cbade2009-11-20 23:18:13 +00006015 // With PIC, the address is actually $g + Offset.
6016 if (isGlobalRelativeToPICBase(OpFlags)) {
6017 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6018 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
6019 Result);
6020 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00006021
6022 return Result;
6023}
6024
6025SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00006026X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00006027 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00006028 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00006029 // Create the TargetGlobalAddress node, folding in the constant
6030 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00006031 unsigned char OpFlags =
6032 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006033 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00006034 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006035 if (OpFlags == X86II::MO_NO_FLAG &&
6036 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00006037 // A direct static reference to a global.
Devang Patel0d881da2010-07-06 22:08:15 +00006038 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00006039 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006040 } else {
Devang Patel0d881da2010-07-06 22:08:15 +00006041 Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006042 }
Eric Christopherfd179292009-08-27 18:07:15 +00006043
Chris Lattner4f066492009-07-11 20:29:19 +00006044 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00006045 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00006046 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
6047 else
6048 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00006049
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006050 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00006051 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006052 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
6053 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00006054 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006056
Chris Lattner36c25012009-07-10 07:34:39 +00006057 // For globals that require a load from a stub to get the address, emit the
6058 // load.
6059 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00006060 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006061 MachinePointerInfo::getGOT(), false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006062
Dan Gohman6520e202008-10-18 02:06:02 +00006063 // If there was a non-zero offset that we didn't fold, create an explicit
6064 // addition for it.
6065 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006066 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00006067 DAG.getConstant(Offset, getPointerTy()));
6068
Evan Cheng0db9fe62006-04-25 20:13:52 +00006069 return Result;
6070}
6071
Evan Chengda43bcf2008-09-24 00:05:32 +00006072SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006073X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00006074 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00006075 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006076 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00006077}
6078
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006079static SDValue
6080GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00006081 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00006082 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006083 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006084 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006085 DebugLoc dl = GA->getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006086 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006087 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006088 GA->getOffset(),
6089 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006090 if (InFlag) {
6091 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006092 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006093 } else {
6094 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00006095 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006096 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006097
6098 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00006099 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00006100
Rafael Espindola15f1b662009-04-24 12:59:40 +00006101 SDValue Flag = Chain.getValue(1);
6102 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00006103}
6104
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006105// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006106static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006107LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006108 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00006109 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00006110 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
6111 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006112 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00006113 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006114 InFlag = Chain.getValue(1);
6115
Chris Lattnerb903bed2009-06-26 21:20:29 +00006116 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006117}
6118
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006119// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00006120static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006121LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006122 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006123 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
6124 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006125}
6126
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006127// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
6128// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00006129static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00006130 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00006131 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00006132 DebugLoc dl = GA->getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00006133
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006134 // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
6135 Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
6136 is64Bit ? 257 : 256));
Rafael Espindola094fad32009-04-08 21:14:34 +00006137
Michael J. Spencerec38de22010-10-10 22:04:20 +00006138 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
Chris Lattnerf93b90c2010-09-22 04:39:11 +00006139 DAG.getIntPtrConstant(0),
6140 MachinePointerInfo(Ptr), false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00006141
Chris Lattnerb903bed2009-06-26 21:20:29 +00006142 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00006143 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
6144 // initialexec.
6145 unsigned WrapperKind = X86ISD::Wrapper;
6146 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00006147 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00006148 } else if (is64Bit) {
6149 assert(model == TLSModel::InitialExec);
6150 OperandFlags = X86II::MO_GOTTPOFF;
6151 WrapperKind = X86ISD::WrapperRIP;
6152 } else {
6153 assert(model == TLSModel::InitialExec);
6154 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00006155 }
Eric Christopherfd179292009-08-27 18:07:15 +00006156
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006157 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
6158 // exec)
Michael J. Spencerec38de22010-10-10 22:04:20 +00006159 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
Devang Patel0d881da2010-07-06 22:08:15 +00006160 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00006161 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00006162 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006163
Rafael Espindola9a580232009-02-27 13:37:18 +00006164 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00006165 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00006166 MachinePointerInfo::getGOT(), false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00006167
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006168 // The address of the thread local variable is the add of the thread
6169 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00006170 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006171}
6172
Dan Gohman475871a2008-07-27 21:46:04 +00006173SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006174X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Michael J. Spencerec38de22010-10-10 22:04:20 +00006175
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006176 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00006177 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00006178
Eric Christopher30ef0e52010-06-03 04:07:48 +00006179 if (Subtarget->isTargetELF()) {
6180 // TODO: implement the "local dynamic" model
6181 // TODO: implement the "initial exec"model for pic executables
Michael J. Spencerec38de22010-10-10 22:04:20 +00006182
Eric Christopher30ef0e52010-06-03 04:07:48 +00006183 // If GV is an alias then use the aliasee for determining
6184 // thread-localness.
6185 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
6186 GV = GA->resolveAliasedGlobal(false);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006187
6188 TLSModel::Model model
Eric Christopher30ef0e52010-06-03 04:07:48 +00006189 = getTLSModel(GV, getTargetMachine().getRelocationModel());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006190
Eric Christopher30ef0e52010-06-03 04:07:48 +00006191 switch (model) {
6192 case TLSModel::GeneralDynamic:
6193 case TLSModel::LocalDynamic: // not implemented
6194 if (Subtarget->is64Bit())
6195 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
6196 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Michael J. Spencerec38de22010-10-10 22:04:20 +00006197
Eric Christopher30ef0e52010-06-03 04:07:48 +00006198 case TLSModel::InitialExec:
6199 case TLSModel::LocalExec:
6200 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
6201 Subtarget->is64Bit());
6202 }
6203 } else if (Subtarget->isTargetDarwin()) {
6204 // Darwin only has one model of TLS. Lower to that.
6205 unsigned char OpFlag = 0;
6206 unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
6207 X86ISD::WrapperRIP : X86ISD::Wrapper;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006208
Eric Christopher30ef0e52010-06-03 04:07:48 +00006209 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
6210 // global base reg.
6211 bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) &&
6212 !Subtarget->is64Bit();
6213 if (PIC32)
6214 OpFlag = X86II::MO_TLVP_PIC_BASE;
6215 else
6216 OpFlag = X86II::MO_TLVP;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006217 DebugLoc DL = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +00006218 SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
Eric Christopherd8c05362010-12-09 06:25:53 +00006219 GA->getValueType(0),
Eric Christopher30ef0e52010-06-03 04:07:48 +00006220 GA->getOffset(), OpFlag);
Eric Christopher30ef0e52010-06-03 04:07:48 +00006221 SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006222
Eric Christopher30ef0e52010-06-03 04:07:48 +00006223 // With PIC32, the address is actually $g + Offset.
6224 if (PIC32)
6225 Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(),
6226 DAG.getNode(X86ISD::GlobalBaseReg,
6227 DebugLoc(), getPointerTy()),
6228 Offset);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006229
Eric Christopher30ef0e52010-06-03 04:07:48 +00006230 // Lowering the machine isd will make sure everything is in the right
6231 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006232 SDValue Chain = DAG.getEntryNode();
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006233 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Eric Christopherd8c05362010-12-09 06:25:53 +00006234 SDValue Args[] = { Chain, Offset };
6235 Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006236
Eric Christopher30ef0e52010-06-03 04:07:48 +00006237 // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
6238 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6239 MFI->setAdjustsStack(true);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00006240
Eric Christopher30ef0e52010-06-03 04:07:48 +00006241 // And our return value (tls address) is in the standard call return value
6242 // location.
Eric Christopherd8c05362010-12-09 06:25:53 +00006243 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
6244 return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00006245 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006246
Eric Christopher30ef0e52010-06-03 04:07:48 +00006247 assert(false &&
6248 "TLS not implemented for this target.");
Eric Christopherfd179292009-08-27 18:07:15 +00006249
Torok Edwinc23197a2009-07-14 16:55:14 +00006250 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00006251 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006252}
6253
Evan Cheng0db9fe62006-04-25 20:13:52 +00006254
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006255/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00006256/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00006257SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00006258 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00006259 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006260 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006261 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006262 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00006263 SDValue ShOpLo = Op.getOperand(0);
6264 SDValue ShOpHi = Op.getOperand(1);
6265 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00006266 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00006267 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00006268 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00006269
Dan Gohman475871a2008-07-27 21:46:04 +00006270 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006271 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006272 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
6273 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006274 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006275 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
6276 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006277 }
Evan Chenge3413162006-01-09 18:33:28 +00006278
Owen Anderson825b72b2009-08-11 20:47:22 +00006279 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
6280 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00006281 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00006282 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00006283
Dan Gohman475871a2008-07-27 21:46:04 +00006284 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00006285 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00006286 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
6287 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00006288
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006289 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00006290 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6291 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006292 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006293 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
6294 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00006295 }
6296
Dan Gohman475871a2008-07-27 21:46:04 +00006297 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00006298 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006299}
Evan Chenga3195e82006-01-12 22:54:21 +00006300
Dan Gohmand858e902010-04-17 15:26:15 +00006301SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
6302 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00006303 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00006304
Dale Johannesen0488fb62010-09-30 23:57:10 +00006305 if (SrcVT.isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006306 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006307
Owen Anderson825b72b2009-08-11 20:47:22 +00006308 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00006309 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00006310
Eli Friedman36df4992009-05-27 00:47:34 +00006311 // These are really Legal; return the operand so the caller accepts it as
6312 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006313 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00006314 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00006315 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00006316 Subtarget->is64Bit()) {
6317 return Op;
6318 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006319
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006320 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006321 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006322 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006323 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006324 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00006325 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00006326 StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006327 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006328 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006329 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
6330}
Evan Cheng0db9fe62006-04-25 20:13:52 +00006331
Owen Andersone50ed302009-08-10 22:56:29 +00006332SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Michael J. Spencerec38de22010-10-10 22:04:20 +00006333 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00006334 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006335 // Build the FILD
Chris Lattner492a43e2010-09-22 01:28:21 +00006336 DebugLoc DL = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00006337 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00006338 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006339 if (useSSE)
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00006340 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
Chris Lattner5a88b832007-02-25 07:10:00 +00006341 else
Owen Anderson825b72b2009-08-11 20:47:22 +00006342 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006343
Chris Lattner492a43e2010-09-22 01:28:21 +00006344 unsigned ByteSize = SrcVT.getSizeInBits()/8;
Michael J. Spencerec38de22010-10-10 22:04:20 +00006345
Chris Lattner492a43e2010-09-22 01:28:21 +00006346 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6347 MachineMemOperand *MMO =
6348 DAG.getMachineFunction()
6349 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6350 MachineMemOperand::MOLoad, ByteSize, ByteSize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006351
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006352 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006353 SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
6354 X86ISD::FILD, DL,
6355 Tys, Ops, array_lengthof(Ops),
6356 SrcVT, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006357
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006358 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006359 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006360 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006361
6362 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
6363 // shouldn't be necessary except that RFP cannot be live across
6364 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006365 MachineFunction &MF = DAG.getMachineFunction();
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006366 unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
6367 int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006368 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00006369 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006370 SDValue Ops[] = {
6371 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
6372 };
Chris Lattner492a43e2010-09-22 01:28:21 +00006373 MachineMemOperand *MMO =
6374 DAG.getMachineFunction()
6375 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
Bob Wilsoneafca4e2010-09-22 17:35:14 +00006376 MachineMemOperand::MOStore, SSFISize, SSFISize);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006377
Chris Lattner492a43e2010-09-22 01:28:21 +00006378 Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
6379 Ops, array_lengthof(Ops),
6380 Op.getValueType(), MMO);
6381 Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006382 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006383 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006384 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006385
Evan Cheng0db9fe62006-04-25 20:13:52 +00006386 return Result;
6387}
6388
Bill Wendling8b8a6362009-01-17 03:56:04 +00006389// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006390SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
6391 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00006392 // This algorithm is not obvious. Here it is in C code, more or less:
6393 /*
6394 double uint64_to_double( uint32_t hi, uint32_t lo ) {
6395 static const __m128i exp = { 0x4330000045300000ULL, 0 };
6396 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00006397
Bill Wendling8b8a6362009-01-17 03:56:04 +00006398 // Copy ints to xmm registers.
6399 __m128i xh = _mm_cvtsi32_si128( hi );
6400 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00006401
Bill Wendling8b8a6362009-01-17 03:56:04 +00006402 // Combine into low half of a single xmm register.
6403 __m128i x = _mm_unpacklo_epi32( xh, xl );
6404 __m128d d;
6405 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00006406
Bill Wendling8b8a6362009-01-17 03:56:04 +00006407 // Merge in appropriate exponents to give the integer bits the right
6408 // magnitude.
6409 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00006410
Bill Wendling8b8a6362009-01-17 03:56:04 +00006411 // Subtract away the biases to deal with the IEEE-754 double precision
6412 // implicit 1.
6413 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00006414
Bill Wendling8b8a6362009-01-17 03:56:04 +00006415 // All conversions up to here are exact. The correctly rounded result is
6416 // calculated using the current rounding mode using the following
6417 // horizontal add.
6418 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
6419 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
6420 // store doesn't really need to be here (except
6421 // maybe to zero the other double)
6422 return sd;
6423 }
6424 */
Dale Johannesen040225f2008-10-21 23:07:49 +00006425
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006426 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00006427 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00006428
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006429 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00006430 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00006431 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
6432 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
6433 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
6434 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006435 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006436 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006437
Bill Wendling8b8a6362009-01-17 03:56:04 +00006438 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00006439 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006440 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00006441 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006442 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00006443 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006444 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006445
Owen Anderson825b72b2009-08-11 20:47:22 +00006446 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6447 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006448 Op.getOperand(0),
6449 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006450 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6451 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00006452 Op.getOperand(0),
6453 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00006454 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
6455 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Chris Lattnere8639032010-09-21 06:22:23 +00006456 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006457 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006458 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006459 SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006460 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Chris Lattnere8639032010-09-21 06:22:23 +00006461 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006462 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00006463 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006464
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006465 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00006466 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00006467 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
6468 DAG.getUNDEF(MVT::v2f64), ShufMask);
6469 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
6470 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006471 DAG.getIntPtrConstant(0));
6472}
6473
Bill Wendling8b8a6362009-01-17 03:56:04 +00006474// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00006475SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
6476 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006477 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006478 // FP constant to bias correct the final result.
6479 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00006480 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006481
6482 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00006483 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
6484 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00006485 Op.getOperand(0),
6486 DAG.getIntPtrConstant(0)));
6487
Owen Anderson825b72b2009-08-11 20:47:22 +00006488 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006489 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006490 DAG.getIntPtrConstant(0));
6491
6492 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006493 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006494 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006495 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006496 MVT::v2f64, Load)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006497 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006498 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00006499 MVT::v2f64, Bias)));
6500 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006501 DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00006502 DAG.getIntPtrConstant(0));
6503
6504 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00006505 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00006506
6507 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00006508 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00006509
Owen Anderson825b72b2009-08-11 20:47:22 +00006510 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006511 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00006512 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00006513 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006514 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00006515 }
6516
6517 // Handle final rounding.
6518 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00006519}
6520
Dan Gohmand858e902010-04-17 15:26:15 +00006521SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
6522 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00006523 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006524 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00006525
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006526 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00006527 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
6528 // the optimization here.
6529 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00006530 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00006531
Owen Andersone50ed302009-08-10 22:56:29 +00006532 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006533 EVT DstVT = Op.getValueType();
6534 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006535 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006536 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00006537 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006538
6539 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00006540 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006541 if (SrcVT == MVT::i32) {
6542 SDValue WordOff = DAG.getConstant(4, getPointerTy());
6543 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
6544 getPointerTy(), StackSlot, WordOff);
6545 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006546 StackSlot, MachinePointerInfo(),
6547 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006548 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006549 OffsetSlot, MachinePointerInfo(),
6550 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006551 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
6552 return Fild;
6553 }
6554
6555 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
6556 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Chris Lattner8026a9d2010-09-21 17:50:43 +00006557 StackSlot, MachinePointerInfo(),
6558 false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006559 // For i64 source, we need to add the appropriate power of 2 if the input
6560 // was negative. This is the same as the optimization in
6561 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
6562 // we must be careful to do the computation in x87 extended precision, not
6563 // in SSE. (The generic code can't know it's OK to do this, or how to.)
Chris Lattner492a43e2010-09-22 01:28:21 +00006564 int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
6565 MachineMemOperand *MMO =
6566 DAG.getMachineFunction()
6567 .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6568 MachineMemOperand::MOLoad, 8, 8);
Michael J. Spencerec38de22010-10-10 22:04:20 +00006569
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006570 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
6571 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
Chris Lattner492a43e2010-09-22 01:28:21 +00006572 SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, 3,
6573 MVT::i64, MMO);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006574
6575 APInt FF(32, 0x5F800000ULL);
6576
6577 // Check whether the sign bit is set.
6578 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
6579 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
6580 ISD::SETLT);
6581
6582 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
6583 SDValue FudgePtr = DAG.getConstantPool(
6584 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
6585 getPointerTy());
6586
6587 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
6588 SDValue Zero = DAG.getIntPtrConstant(0);
6589 SDValue Four = DAG.getIntPtrConstant(4);
6590 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
6591 Zero, Four);
6592 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
6593
6594 // Load the value out, extending it from f32 to f80.
6595 // FIXME: Avoid the extend by constructing the right constant pool?
Evan Chengbcc80172010-07-07 22:15:37 +00006596 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, MVT::f80, dl, DAG.getEntryNode(),
Chris Lattnere8639032010-09-21 06:22:23 +00006597 FudgePtr, MachinePointerInfo::getConstantPool(),
6598 MVT::f32, false, false, 4);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00006599 // Extend everything to 80 bits to force it to be done on x87.
6600 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
6601 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00006602}
6603
Dan Gohman475871a2008-07-27 21:46:04 +00006604std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00006605FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Chris Lattner07290932010-09-22 01:05:16 +00006606 DebugLoc DL = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00006607
Owen Andersone50ed302009-08-10 22:56:29 +00006608 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00006609
6610 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006611 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
6612 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00006613 }
6614
Owen Anderson825b72b2009-08-11 20:47:22 +00006615 assert(DstTy.getSimpleVT() <= MVT::i64 &&
6616 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00006617 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00006618
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006619 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00006620 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00006621 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006622 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00006623 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006624 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00006625 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00006626 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00006627
Evan Cheng87c89352007-10-15 20:11:21 +00006628 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
6629 // stack slot.
6630 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00006631 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00006632 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00006633 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00006634
Michael J. Spencerec38de22010-10-10 22:04:20 +00006635
6636
Evan Cheng0db9fe62006-04-25 20:13:52 +00006637 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00006638 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006639 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006640 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
6641 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
6642 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00006643 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006644
Dan Gohman475871a2008-07-27 21:46:04 +00006645 SDValue Chain = DAG.getEntryNode();
6646 SDValue Value = Op.getOperand(0);
Chris Lattner492a43e2010-09-22 01:28:21 +00006647 EVT TheVT = Op.getOperand(0).getValueType();
6648 if (isScalarFPTypeInSSEReg(TheVT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006649 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Chris Lattner07290932010-09-22 01:05:16 +00006650 Chain = DAG.getStore(Chain, DL, Value, StackSlot,
Chris Lattnere8639032010-09-21 06:22:23 +00006651 MachinePointerInfo::getFixedStack(SSFI),
David Greene67c9d422010-02-15 16:53:33 +00006652 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00006653 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00006654 SDValue Ops[] = {
Chris Lattner492a43e2010-09-22 01:28:21 +00006655 Chain, StackSlot, DAG.getValueType(TheVT)
Chris Lattner5a88b832007-02-25 07:10:00 +00006656 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00006657
Chris Lattner492a43e2010-09-22 01:28:21 +00006658 MachineMemOperand *MMO =
6659 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6660 MachineMemOperand::MOLoad, MemSize, MemSize);
6661 Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, 3,
6662 DstTy, MMO);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006663 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00006664 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006665 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
6666 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00006667
Chris Lattner07290932010-09-22 01:05:16 +00006668 MachineMemOperand *MMO =
6669 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
6670 MachineMemOperand::MOStore, MemSize, MemSize);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006671
Evan Cheng0db9fe62006-04-25 20:13:52 +00006672 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00006673 SDValue Ops[] = { Chain, Value, StackSlot };
Chris Lattner07290932010-09-22 01:05:16 +00006674 SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
6675 Ops, 3, DstTy, MMO);
Evan Chengd9558e02006-01-06 00:43:03 +00006676
Chris Lattner27a6c732007-11-24 07:07:01 +00006677 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006678}
6679
Dan Gohmand858e902010-04-17 15:26:15 +00006680SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
6681 SelectionDAG &DAG) const {
Dale Johannesen0488fb62010-09-30 23:57:10 +00006682 if (Op.getValueType().isVector())
Eli Friedman23ef1052009-06-06 03:57:58 +00006683 return SDValue();
Eli Friedman23ef1052009-06-06 03:57:58 +00006684
Eli Friedman948e95a2009-05-23 09:59:16 +00006685 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00006686 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00006687 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
6688 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00006689
Chris Lattner27a6c732007-11-24 07:07:01 +00006690 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006691 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006692 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00006693}
6694
Dan Gohmand858e902010-04-17 15:26:15 +00006695SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
6696 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00006697 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
6698 SDValue FIST = Vals.first, StackSlot = Vals.second;
6699 assert(FIST.getNode() && "Unexpected failure");
6700
6701 // Load the result.
6702 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Chris Lattner51abfe42010-09-21 06:02:19 +00006703 FIST, StackSlot, MachinePointerInfo(), false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00006704}
6705
Dan Gohmand858e902010-04-17 15:26:15 +00006706SDValue X86TargetLowering::LowerFABS(SDValue Op,
6707 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006708 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006709 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006710 EVT VT = Op.getValueType();
6711 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006712 if (VT.isVector())
6713 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006714 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006715 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006716 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00006717 CV.push_back(C);
6718 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006719 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006720 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00006721 CV.push_back(C);
6722 CV.push_back(C);
6723 CV.push_back(C);
6724 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006725 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006726 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006727 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006728 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006729 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006730 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006731 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006732}
6733
Dan Gohmand858e902010-04-17 15:26:15 +00006734SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006735 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006736 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006737 EVT VT = Op.getValueType();
6738 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00006739 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00006740 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006741 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006742 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006743 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00006744 CV.push_back(C);
6745 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006746 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006747 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00006748 CV.push_back(C);
6749 CV.push_back(C);
6750 CV.push_back(C);
6751 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006752 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006753 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006754 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006755 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006756 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006757 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006758 if (VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006759 return DAG.getNode(ISD::BITCAST, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006760 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006761 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00006762 Op.getOperand(0)),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006763 DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00006764 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00006765 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00006766 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006767}
6768
Dan Gohmand858e902010-04-17 15:26:15 +00006769SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00006770 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00006771 SDValue Op0 = Op.getOperand(0);
6772 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006773 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00006774 EVT VT = Op.getValueType();
6775 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00006776
6777 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006778 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006779 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006780 SrcVT = VT;
6781 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006782 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006783 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00006784 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006785 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00006786 }
6787
6788 // At this point the operands and the result should have the same
6789 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00006790
Evan Cheng68c47cb2007-01-05 07:55:56 +00006791 // First get the sign bit of second operand.
6792 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00006793 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006794 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
6795 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006796 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006797 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
6798 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6799 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6800 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006801 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006802 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006803 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006804 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006805 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006806 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006807 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006808
6809 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00006810 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006811 // Op0 is MVT::f32, Op1 is MVT::f64.
6812 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
6813 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
6814 DAG.getConstant(32, MVT::i32));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006815 SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit);
Owen Anderson825b72b2009-08-11 20:47:22 +00006816 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00006817 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00006818 }
6819
Evan Cheng73d6cf12007-01-05 21:37:56 +00006820 // Clear first operand sign bit.
6821 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00006822 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006823 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
6824 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006825 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00006826 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
6827 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6828 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
6829 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00006830 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00006831 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00006832 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006833 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00006834 MachinePointerInfo::getConstantPool(),
David Greene67c9d422010-02-15 16:53:33 +00006835 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00006836 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00006837
6838 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00006839 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006840}
6841
Dan Gohman076aee32009-03-04 19:44:21 +00006842/// Emit nodes that will be selected as "test Op0,Op0", or something
6843/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006844SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006845 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006846 DebugLoc dl = Op.getDebugLoc();
6847
Dan Gohman31125812009-03-07 01:58:32 +00006848 // CF and OF aren't always set the way we want. Determine which
6849 // of these we need.
6850 bool NeedCF = false;
6851 bool NeedOF = false;
6852 switch (X86CC) {
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006853 default: break;
Dan Gohman31125812009-03-07 01:58:32 +00006854 case X86::COND_A: case X86::COND_AE:
6855 case X86::COND_B: case X86::COND_BE:
6856 NeedCF = true;
6857 break;
6858 case X86::COND_G: case X86::COND_GE:
6859 case X86::COND_L: case X86::COND_LE:
6860 case X86::COND_O: case X86::COND_NO:
6861 NeedOF = true;
6862 break;
Dan Gohman31125812009-03-07 01:58:32 +00006863 }
6864
Dan Gohman076aee32009-03-04 19:44:21 +00006865 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00006866 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
6867 // we prove that the arithmetic won't overflow, we can't use OF or CF.
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006868 if (Op.getResNo() != 0 || NeedOF || NeedCF)
6869 // Emit a CMP with 0, which is the TEST pattern.
6870 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6871 DAG.getConstant(0, Op.getValueType()));
6872
6873 unsigned Opcode = 0;
6874 unsigned NumOperands = 0;
6875 switch (Op.getNode()->getOpcode()) {
6876 case ISD::ADD:
6877 // Due to an isel shortcoming, be conservative if this add is likely to be
6878 // selected as part of a load-modify-store instruction. When the root node
6879 // in a match is a store, isel doesn't know how to remap non-chain non-flag
6880 // uses of other nodes in the match, such as the ADD in this case. This
6881 // leads to the ADD being left around and reselected, with the result being
6882 // two adds in the output. Alas, even if none our users are stores, that
6883 // doesn't prove we're O.K. Ergo, if we have any parents that aren't
6884 // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require
6885 // climbing the DAG back to the root, and it doesn't seem to be worth the
6886 // effort.
6887 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Dan Gohman076aee32009-03-04 19:44:21 +00006888 UE = Op.getNode()->use_end(); UI != UE; ++UI)
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006889 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
6890 goto default_case;
6891
6892 if (ConstantSDNode *C =
6893 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6894 // An add of one will be selected as an INC.
6895 if (C->getAPIntValue() == 1) {
6896 Opcode = X86ISD::INC;
6897 NumOperands = 1;
6898 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006899 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006900
6901 // An add of negative one (subtract of one) will be selected as a DEC.
6902 if (C->getAPIntValue().isAllOnesValue()) {
6903 Opcode = X86ISD::DEC;
6904 NumOperands = 1;
6905 break;
6906 }
Dan Gohman076aee32009-03-04 19:44:21 +00006907 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006908
6909 // Otherwise use a regular EFLAGS-setting add.
6910 Opcode = X86ISD::ADD;
6911 NumOperands = 2;
6912 break;
6913 case ISD::AND: {
6914 // If the primary and result isn't used, don't bother using X86ISD::AND,
6915 // because a TEST instruction will be better.
6916 bool NonFlagUse = false;
6917 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6918 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6919 SDNode *User = *UI;
6920 unsigned UOpNo = UI.getOperandNo();
6921 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6922 // Look pass truncate.
6923 UOpNo = User->use_begin().getOperandNo();
6924 User = *User->use_begin();
6925 }
6926
6927 if (User->getOpcode() != ISD::BRCOND &&
6928 User->getOpcode() != ISD::SETCC &&
6929 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
6930 NonFlagUse = true;
6931 break;
6932 }
Dan Gohman076aee32009-03-04 19:44:21 +00006933 }
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006934
6935 if (!NonFlagUse)
6936 break;
6937 }
6938 // FALL THROUGH
6939 case ISD::SUB:
6940 case ISD::OR:
6941 case ISD::XOR:
6942 // Due to the ISEL shortcoming noted above, be conservative if this op is
6943 // likely to be selected as part of a load-modify-store instruction.
6944 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6945 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6946 if (UI->getOpcode() == ISD::STORE)
6947 goto default_case;
6948
6949 // Otherwise use a regular EFLAGS-setting instruction.
6950 switch (Op.getNode()->getOpcode()) {
6951 default: llvm_unreachable("unexpected operator!");
6952 case ISD::SUB: Opcode = X86ISD::SUB; break;
6953 case ISD::OR: Opcode = X86ISD::OR; break;
6954 case ISD::XOR: Opcode = X86ISD::XOR; break;
6955 case ISD::AND: Opcode = X86ISD::AND; break;
6956 }
6957
6958 NumOperands = 2;
6959 break;
6960 case X86ISD::ADD:
6961 case X86ISD::SUB:
6962 case X86ISD::INC:
6963 case X86ISD::DEC:
6964 case X86ISD::OR:
6965 case X86ISD::XOR:
6966 case X86ISD::AND:
6967 return SDValue(Op.getNode(), 1);
6968 default:
6969 default_case:
6970 break;
Dan Gohman076aee32009-03-04 19:44:21 +00006971 }
6972
Bill Wendlingc25ccf82010-06-28 21:08:32 +00006973 if (Opcode == 0)
6974 // Emit a CMP with 0, which is the TEST pattern.
6975 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
6976 DAG.getConstant(0, Op.getValueType()));
6977
6978 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
6979 SmallVector<SDValue, 4> Ops;
6980 for (unsigned i = 0; i != NumOperands; ++i)
6981 Ops.push_back(Op.getOperand(i));
6982
6983 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
6984 DAG.ReplaceAllUsesWith(Op, New);
6985 return SDValue(New.getNode(), 1);
Dan Gohman076aee32009-03-04 19:44:21 +00006986}
6987
6988/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6989/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006990SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006991 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6993 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006994 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006995
6996 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006997 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006998}
6999
Evan Chengd40d03e2010-01-06 19:38:29 +00007000/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
7001/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00007002SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
7003 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007004 SDValue Op0 = And.getOperand(0);
7005 SDValue Op1 = And.getOperand(1);
7006 if (Op0.getOpcode() == ISD::TRUNCATE)
7007 Op0 = Op0.getOperand(0);
7008 if (Op1.getOpcode() == ISD::TRUNCATE)
7009 Op1 = Op1.getOperand(0);
7010
Evan Chengd40d03e2010-01-06 19:38:29 +00007011 SDValue LHS, RHS;
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007012 if (Op1.getOpcode() == ISD::SHL)
7013 std::swap(Op0, Op1);
7014 if (Op0.getOpcode() == ISD::SHL) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00007015 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
7016 if (And00C->getZExtValue() == 1) {
Dan Gohman6b13cbc2010-06-24 02:07:59 +00007017 // If we looked past a truncate, check that it's only truncating away
7018 // known zeros.
7019 unsigned BitWidth = Op0.getValueSizeInBits();
7020 unsigned AndBitWidth = And.getValueSizeInBits();
7021 if (BitWidth > AndBitWidth) {
7022 APInt Mask = APInt::getAllOnesValue(BitWidth), Zeros, Ones;
7023 DAG.ComputeMaskedBits(Op0, Mask, Zeros, Ones);
7024 if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
7025 return SDValue();
7026 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007027 LHS = Op1;
7028 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00007029 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007030 } else if (Op1.getOpcode() == ISD::Constant) {
7031 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
7032 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00007033 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
7034 LHS = AndLHS.getOperand(0);
7035 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007036 }
Evan Chengd40d03e2010-01-06 19:38:29 +00007037 }
Evan Cheng0488db92007-09-25 01:57:46 +00007038
Evan Chengd40d03e2010-01-06 19:38:29 +00007039 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00007040 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00007041 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00007042 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00007043 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00007044 // Also promote i16 to i32 for performance / code size reason.
7045 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00007046 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00007047 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00007048
Evan Chengd40d03e2010-01-06 19:38:29 +00007049 // If the operand types disagree, extend the shift amount to match. Since
7050 // BT ignores high bits (like shifts) we can use anyextend.
7051 if (LHS.getValueType() != RHS.getValueType())
7052 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007053
Evan Chengd40d03e2010-01-06 19:38:29 +00007054 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
7055 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
7056 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7057 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00007058 }
7059
Evan Cheng54de3ea2010-01-05 06:52:31 +00007060 return SDValue();
7061}
7062
Dan Gohmand858e902010-04-17 15:26:15 +00007063SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00007064 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
7065 SDValue Op0 = Op.getOperand(0);
7066 SDValue Op1 = Op.getOperand(1);
7067 DebugLoc dl = Op.getDebugLoc();
7068 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
7069
7070 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00007071 // Lower (X & (1 << N)) == 0 to BT(X, N).
7072 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
7073 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Chris Lattner481eebc2010-12-19 21:23:48 +00007074 if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007075 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane368b462010-06-18 14:22:04 +00007076 cast<ConstantSDNode>(Op1)->isNullValue() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00007077 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
7078 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
7079 if (NewSetCC.getNode())
7080 return NewSetCC;
7081 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00007082
Chris Lattner481eebc2010-12-19 21:23:48 +00007083 // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of
7084 // these.
7085 if (Op1.getOpcode() == ISD::Constant &&
Evan Cheng2c755ba2010-02-27 07:36:59 +00007086 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
7087 cast<ConstantSDNode>(Op1)->isNullValue()) &&
7088 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007089
Chris Lattner481eebc2010-12-19 21:23:48 +00007090 // If the input is a setcc, then reuse the input setcc or use a new one with
7091 // the inverted condition.
7092 if (Op0.getOpcode() == X86ISD::SETCC) {
7093 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
7094 bool Invert = (CC == ISD::SETNE) ^
7095 cast<ConstantSDNode>(Op1)->isNullValue();
7096 if (!Invert) return Op0;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007097
Evan Cheng2c755ba2010-02-27 07:36:59 +00007098 CCode = X86::GetOppositeBranchCondition(CCode);
Chris Lattner481eebc2010-12-19 21:23:48 +00007099 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7100 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
7101 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00007102 }
7103
Evan Chenge5b51ac2010-04-17 06:13:15 +00007104 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00007105 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007106 if (X86CC == X86::COND_INVALID)
7107 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007108
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007109 SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00007110 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnerc19d1c32010-12-19 22:08:31 +00007111 DAG.getConstant(X86CC, MVT::i8), EFLAGS);
Evan Cheng0488db92007-09-25 01:57:46 +00007112}
7113
Dan Gohmand858e902010-04-17 15:26:15 +00007114SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007115 SDValue Cond;
7116 SDValue Op0 = Op.getOperand(0);
7117 SDValue Op1 = Op.getOperand(1);
7118 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00007119 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00007120 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
7121 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007122 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00007123
7124 if (isFP) {
7125 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007126 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007127 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
7128 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00007129 bool Swap = false;
7130
7131 switch (SetCCOpcode) {
7132 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007133 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00007134 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00007135 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00007136 case ISD::SETGT: Swap = true; // Fallthrough
7137 case ISD::SETLT:
7138 case ISD::SETOLT: SSECC = 1; break;
7139 case ISD::SETOGE:
7140 case ISD::SETGE: Swap = true; // Fallthrough
7141 case ISD::SETLE:
7142 case ISD::SETOLE: SSECC = 2; break;
7143 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00007144 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00007145 case ISD::SETNE: SSECC = 4; break;
7146 case ISD::SETULE: Swap = true;
7147 case ISD::SETUGE: SSECC = 5; break;
7148 case ISD::SETULT: Swap = true;
7149 case ISD::SETUGT: SSECC = 6; break;
7150 case ISD::SETO: SSECC = 7; break;
7151 }
7152 if (Swap)
7153 std::swap(Op0, Op1);
7154
Nate Begemanfb8ead02008-07-25 19:05:58 +00007155 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00007156 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00007157 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00007158 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007159 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
7160 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007161 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007162 }
7163 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00007164 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00007165 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
7166 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00007167 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00007168 }
Torok Edwinc23197a2009-07-14 16:55:14 +00007169 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00007170 }
7171 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00007172 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00007173 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007174
Nate Begeman30a0de92008-07-17 16:51:19 +00007175 // We are handling one of the integer comparisons here. Since SSE only has
7176 // GT and EQ comparisons for integer, swapping operands and multiple
7177 // operations may be required for some comparisons.
7178 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
7179 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007180
Owen Anderson825b72b2009-08-11 20:47:22 +00007181 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00007182 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007183 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007184 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007185 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
7186 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00007187 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007188
Nate Begeman30a0de92008-07-17 16:51:19 +00007189 switch (SetCCOpcode) {
7190 default: break;
7191 case ISD::SETNE: Invert = true;
7192 case ISD::SETEQ: Opc = EQOpc; break;
7193 case ISD::SETLT: Swap = true;
7194 case ISD::SETGT: Opc = GTOpc; break;
7195 case ISD::SETGE: Swap = true;
7196 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
7197 case ISD::SETULT: Swap = true;
7198 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
7199 case ISD::SETUGE: Swap = true;
7200 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
7201 }
7202 if (Swap)
7203 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007204
Nate Begeman30a0de92008-07-17 16:51:19 +00007205 // Since SSE has no unsigned integer comparisons, we need to flip the sign
7206 // bits of the inputs before performing those operations.
7207 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00007208 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00007209 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
7210 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00007211 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00007212 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
7213 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00007214 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
7215 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00007216 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007217
Dale Johannesenace16102009-02-03 19:33:06 +00007218 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00007219
7220 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00007221 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00007222 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00007223
Nate Begeman30a0de92008-07-17 16:51:19 +00007224 return Result;
7225}
Evan Cheng0488db92007-09-25 01:57:46 +00007226
Evan Cheng370e5342008-12-03 08:38:43 +00007227// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00007228static bool isX86LogicalCmp(SDValue Op) {
7229 unsigned Opc = Op.getNode()->getOpcode();
7230 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
7231 return true;
7232 if (Op.getResNo() == 1 &&
7233 (Opc == X86ISD::ADD ||
7234 Opc == X86ISD::SUB ||
Chris Lattner5b856542010-12-20 00:59:46 +00007235 Opc == X86ISD::ADC ||
7236 Opc == X86ISD::SBB ||
Dan Gohman076aee32009-03-04 19:44:21 +00007237 Opc == X86ISD::SMUL ||
7238 Opc == X86ISD::UMUL ||
7239 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00007240 Opc == X86ISD::DEC ||
7241 Opc == X86ISD::OR ||
7242 Opc == X86ISD::XOR ||
7243 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00007244 return true;
7245
Chris Lattner9637d5b2010-12-05 07:49:54 +00007246 if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
7247 return true;
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007248
Dan Gohman076aee32009-03-04 19:44:21 +00007249 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00007250}
7251
Chris Lattnera2b56002010-12-05 01:23:24 +00007252static bool isZero(SDValue V) {
7253 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7254 return C && C->isNullValue();
7255}
7256
Chris Lattner96908b12010-12-05 02:00:51 +00007257static bool isAllOnes(SDValue V) {
7258 ConstantSDNode *C = dyn_cast<ConstantSDNode>(V);
7259 return C && C->isAllOnesValue();
7260}
7261
Dan Gohmand858e902010-04-17 15:26:15 +00007262SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007263 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007264 SDValue Cond = Op.getOperand(0);
Chris Lattnera2b56002010-12-05 01:23:24 +00007265 SDValue Op1 = Op.getOperand(1);
7266 SDValue Op2 = Op.getOperand(2);
7267 DebugLoc DL = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007268 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00007269
Dan Gohman1a492952009-10-20 16:22:37 +00007270 if (Cond.getOpcode() == ISD::SETCC) {
7271 SDValue NewCond = LowerSETCC(Cond, DAG);
7272 if (NewCond.getNode())
7273 Cond = NewCond;
7274 }
Evan Cheng734503b2006-09-11 02:19:56 +00007275
Chris Lattnera2b56002010-12-05 01:23:24 +00007276 // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007277 // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
Chris Lattnera2b56002010-12-05 01:23:24 +00007278 // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
Chris Lattner96908b12010-12-05 02:00:51 +00007279 // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007280 if (Cond.getOpcode() == X86ISD::SETCC &&
Chris Lattner96908b12010-12-05 02:00:51 +00007281 Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
7282 isZero(Cond.getOperand(1).getOperand(1))) {
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007283 SDValue Cmp = Cond.getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007284
Chris Lattnera2b56002010-12-05 01:23:24 +00007285 unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007286
7287 if ((isAllOnes(Op1) || isAllOnes(Op2)) &&
Chris Lattner96908b12010-12-05 02:00:51 +00007288 (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
7289 SDValue Y = isAllOnes(Op2) ? Op1 : Op2;
Chris Lattnera2b56002010-12-05 01:23:24 +00007290
7291 SDValue CmpOp0 = Cmp.getOperand(0);
7292 Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
7293 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007294
Chris Lattner96908b12010-12-05 02:00:51 +00007295 SDValue Res = // Res = 0 or -1.
Chris Lattnera2b56002010-12-05 01:23:24 +00007296 DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7297 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007298
Chris Lattner96908b12010-12-05 02:00:51 +00007299 if (isAllOnes(Op1) != (CondCode == X86::COND_E))
7300 Res = DAG.getNOT(DL, Res, Res.getValueType());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00007301
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007302 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
Chris Lattnera2b56002010-12-05 01:23:24 +00007303 if (N2C == 0 || !N2C->isNullValue())
7304 Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
7305 return Res;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007306 }
7307 }
7308
Chris Lattnera2b56002010-12-05 01:23:24 +00007309 // Look past (and (setcc_carry (cmp ...)), 1).
Evan Chengad9c0a32009-12-15 00:53:42 +00007310 if (Cond.getOpcode() == ISD::AND &&
7311 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7312 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007313 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007314 Cond = Cond.getOperand(0);
7315 }
7316
Evan Cheng3f41d662007-10-08 22:16:29 +00007317 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7318 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007319 if (Cond.getOpcode() == X86ISD::SETCC ||
7320 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007321 CC = Cond.getOperand(0);
7322
Dan Gohman475871a2008-07-27 21:46:04 +00007323 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007324 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00007325 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00007326
Evan Cheng3f41d662007-10-08 22:16:29 +00007327 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007328 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00007329 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00007330 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00007331
Chris Lattnerd1980a52009-03-12 06:52:53 +00007332 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
7333 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00007334 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007335 addTest = false;
7336 }
7337 }
7338
7339 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007340 // Look pass the truncate.
7341 if (Cond.getOpcode() == ISD::TRUNCATE)
7342 Cond = Cond.getOperand(0);
7343
7344 // We know the result of AND is compared against zero. Try to match
7345 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007346 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Chris Lattnera2b56002010-12-05 01:23:24 +00007347 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG);
Evan Chengd40d03e2010-01-06 19:38:29 +00007348 if (NewSetCC.getNode()) {
7349 CC = NewSetCC.getOperand(0);
7350 Cond = NewSetCC.getOperand(1);
7351 addTest = false;
7352 }
7353 }
7354 }
7355
7356 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007357 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007358 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007359 }
7360
Benjamin Kramere915ff32010-12-22 23:09:28 +00007361 // a < b ? -1 : 0 -> RES = ~setcc_carry
7362 // a < b ? 0 : -1 -> RES = setcc_carry
7363 // a >= b ? -1 : 0 -> RES = setcc_carry
7364 // a >= b ? 0 : -1 -> RES = ~setcc_carry
7365 if (Cond.getOpcode() == X86ISD::CMP) {
7366 unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
7367
7368 if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
7369 (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) {
7370 SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
7371 DAG.getConstant(X86::COND_B, MVT::i8), Cond);
7372 if (isAllOnes(Op1) != (CondCode == X86::COND_B))
7373 return DAG.getNOT(DL, Res, Res.getValueType());
7374 return Res;
7375 }
7376 }
7377
Evan Cheng0488db92007-09-25 01:57:46 +00007378 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
7379 // condition is true.
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007380 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00007381 SDValue Ops[] = { Op2, Op1, CC, Cond };
Chris Lattnera2b56002010-12-05 01:23:24 +00007382 return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00007383}
7384
Evan Cheng370e5342008-12-03 08:38:43 +00007385// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
7386// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
7387// from the AND / OR.
7388static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
7389 Opc = Op.getOpcode();
7390 if (Opc != ISD::OR && Opc != ISD::AND)
7391 return false;
7392 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7393 Op.getOperand(0).hasOneUse() &&
7394 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
7395 Op.getOperand(1).hasOneUse());
7396}
7397
Evan Cheng961d6d42009-02-02 08:19:07 +00007398// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
7399// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00007400static bool isXor1OfSetCC(SDValue Op) {
7401 if (Op.getOpcode() != ISD::XOR)
7402 return false;
7403 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7404 if (N1C && N1C->getAPIntValue() == 1) {
7405 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
7406 Op.getOperand(0).hasOneUse();
7407 }
7408 return false;
7409}
7410
Dan Gohmand858e902010-04-17 15:26:15 +00007411SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00007412 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007413 SDValue Chain = Op.getOperand(0);
7414 SDValue Cond = Op.getOperand(1);
7415 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007416 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007417 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00007418
Dan Gohman1a492952009-10-20 16:22:37 +00007419 if (Cond.getOpcode() == ISD::SETCC) {
7420 SDValue NewCond = LowerSETCC(Cond, DAG);
7421 if (NewCond.getNode())
7422 Cond = NewCond;
7423 }
Chris Lattnere55484e2008-12-25 05:34:37 +00007424#if 0
7425 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00007426 else if (Cond.getOpcode() == X86ISD::ADD ||
7427 Cond.getOpcode() == X86ISD::SUB ||
7428 Cond.getOpcode() == X86ISD::SMUL ||
7429 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00007430 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00007431#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00007432
Evan Chengad9c0a32009-12-15 00:53:42 +00007433 // Look pass (and (setcc_carry (cmp ...)), 1).
7434 if (Cond.getOpcode() == ISD::AND &&
7435 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
7436 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
Michael J. Spencerec38de22010-10-10 22:04:20 +00007437 if (C && C->getAPIntValue() == 1)
Evan Chengad9c0a32009-12-15 00:53:42 +00007438 Cond = Cond.getOperand(0);
7439 }
7440
Evan Cheng3f41d662007-10-08 22:16:29 +00007441 // If condition flag is set by a X86ISD::CMP, then use it as the condition
7442 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00007443 if (Cond.getOpcode() == X86ISD::SETCC ||
7444 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00007445 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007446
Dan Gohman475871a2008-07-27 21:46:04 +00007447 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00007448 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00007449 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00007450 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00007451 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00007452 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00007453 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00007454 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007455 default: break;
7456 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00007457 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00007458 // These can only come from an arithmetic instruction with overflow,
7459 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00007460 Cond = Cond.getNode()->getOperand(1);
7461 addTest = false;
7462 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007463 }
Evan Cheng0488db92007-09-25 01:57:46 +00007464 }
Evan Cheng370e5342008-12-03 08:38:43 +00007465 } else {
7466 unsigned CondOpc;
7467 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
7468 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00007469 if (CondOpc == ISD::OR) {
7470 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
7471 // two branches instead of an explicit OR instruction with a
7472 // separate test.
7473 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007474 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00007475 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007476 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007477 Chain, Dest, CC, Cmp);
7478 CC = Cond.getOperand(1).getOperand(0);
7479 Cond = Cmp;
7480 addTest = false;
7481 }
7482 } else { // ISD::AND
7483 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
7484 // two branches instead of an explicit AND instruction with a
7485 // separate test. However, we only do this if this block doesn't
7486 // have a fall-through edge, because this requires an explicit
7487 // jmp when the condition is false.
7488 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00007489 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00007490 Op.getNode()->hasOneUse()) {
7491 X86::CondCode CCode =
7492 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7493 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007494 CC = DAG.getConstant(CCode, MVT::i8);
Dan Gohman027657d2010-06-18 15:30:29 +00007495 SDNode *User = *Op.getNode()->use_begin();
Evan Cheng370e5342008-12-03 08:38:43 +00007496 // Look for an unconditional branch following this conditional branch.
7497 // We need this because we need to reverse the successors in order
7498 // to implement FCMP_OEQ.
Dan Gohman027657d2010-06-18 15:30:29 +00007499 if (User->getOpcode() == ISD::BR) {
7500 SDValue FalseBB = User->getOperand(1);
7501 SDNode *NewBR =
7502 DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
Evan Cheng370e5342008-12-03 08:38:43 +00007503 assert(NewBR == User);
Nick Lewycky2a3ee5e2010-06-20 20:27:42 +00007504 (void)NewBR;
Evan Cheng370e5342008-12-03 08:38:43 +00007505 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00007506
Dale Johannesene4d209d2009-02-03 20:21:25 +00007507 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00007508 Chain, Dest, CC, Cmp);
7509 X86::CondCode CCode =
7510 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
7511 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007512 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00007513 Cond = Cmp;
7514 addTest = false;
7515 }
7516 }
Dan Gohman279c22e2008-10-21 03:29:32 +00007517 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00007518 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
7519 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
7520 // It should be transformed during dag combiner except when the condition
7521 // is set by a arithmetics with overflow node.
7522 X86::CondCode CCode =
7523 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
7524 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00007525 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00007526 Cond = Cond.getOperand(0).getOperand(1);
7527 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00007528 }
Evan Cheng0488db92007-09-25 01:57:46 +00007529 }
7530
7531 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007532 // Look pass the truncate.
7533 if (Cond.getOpcode() == ISD::TRUNCATE)
7534 Cond = Cond.getOperand(0);
7535
7536 // We know the result of AND is compared against zero. Try to match
7537 // it to BT.
Michael J. Spencerec38de22010-10-10 22:04:20 +00007538 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00007539 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
7540 if (NewSetCC.getNode()) {
7541 CC = NewSetCC.getOperand(0);
7542 Cond = NewSetCC.getOperand(1);
7543 addTest = false;
7544 }
7545 }
7546 }
7547
7548 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007549 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00007550 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00007551 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007552 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00007553 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00007554}
7555
Anton Korobeynikove060b532007-04-17 19:34:00 +00007556
7557// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
7558// Calls to _alloca is needed to probe the stack when allocating more than 4k
7559// bytes in one go. Touching the stack at 4K increments is necessary to ensure
7560// that the guard pages used by the OS virtual memory manager are allocated in
7561// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00007562SDValue
7563X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007564 SelectionDAG &DAG) const {
Duncan Sands1e1ca0b2010-10-21 16:02:12 +00007565 assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows()) &&
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007566 "This should be used only on Windows targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007567 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007568
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007569 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00007570 SDValue Chain = Op.getOperand(0);
7571 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007572 // FIXME: Ensure alignment here
7573
Dan Gohman475871a2008-07-27 21:46:04 +00007574 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007575
Owen Anderson825b72b2009-08-11 20:47:22 +00007576 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007577
Dale Johannesendd64c412009-02-04 00:33:20 +00007578 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007579 Flag = Chain.getValue(1);
7580
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00007581 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00007582
Michael J. Spencere9c253e2010-10-21 01:41:01 +00007583 Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007584 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007585
Dale Johannesendd64c412009-02-04 00:33:20 +00007586 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00007587
Dan Gohman475871a2008-07-27 21:46:04 +00007588 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007589 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007590}
7591
Dan Gohmand858e902010-04-17 15:26:15 +00007592SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00007593 MachineFunction &MF = DAG.getMachineFunction();
7594 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
7595
Dan Gohman69de1932008-02-06 22:27:42 +00007596 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner8026a9d2010-09-21 17:50:43 +00007597 DebugLoc DL = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00007598
Anton Korobeynikove7beda12010-10-03 22:52:07 +00007599 if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00007600 // vastart just stores the address of the VarArgsFrameIndex slot into the
7601 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00007602 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7603 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007604 return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
7605 MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007606 }
7607
7608 // __va_list_tag:
7609 // gp_offset (0 - 6 * 8)
7610 // fp_offset (48 - 48 + 8 * 16)
7611 // overflow_arg_area (point to parameters coming in memory).
7612 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00007613 SmallVector<SDValue, 8> MemOps;
7614 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00007615 // Store gp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007616 SDValue Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007617 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
7618 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007619 FIN, MachinePointerInfo(SV), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007620 MemOps.push_back(Store);
7621
7622 // Store fp_offset
Chris Lattner8026a9d2010-09-21 17:50:43 +00007623 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007624 FIN, DAG.getIntPtrConstant(4));
Chris Lattner8026a9d2010-09-21 17:50:43 +00007625 Store = DAG.getStore(Op.getOperand(0), DL,
Dan Gohman1e93df62010-04-17 14:41:14 +00007626 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
7627 MVT::i32),
Chris Lattner8026a9d2010-09-21 17:50:43 +00007628 FIN, MachinePointerInfo(SV, 4), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007629 MemOps.push_back(Store);
7630
7631 // Store ptr to overflow_arg_area
Chris Lattner8026a9d2010-09-21 17:50:43 +00007632 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007633 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00007634 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
7635 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007636 Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
7637 MachinePointerInfo(SV, 8),
David Greene67c9d422010-02-15 16:53:33 +00007638 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007639 MemOps.push_back(Store);
7640
7641 // Store ptr to reg_save_area.
Chris Lattner8026a9d2010-09-21 17:50:43 +00007642 FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007643 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00007644 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
7645 getPointerTy());
Chris Lattner8026a9d2010-09-21 17:50:43 +00007646 Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN,
7647 MachinePointerInfo(SV, 16), false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00007648 MemOps.push_back(Store);
Chris Lattner8026a9d2010-09-21 17:50:43 +00007649 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00007650 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00007651}
7652
Dan Gohmand858e902010-04-17 15:26:15 +00007653SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman320afb82010-10-12 18:00:49 +00007654 assert(Subtarget->is64Bit() &&
7655 "LowerVAARG only handles 64-bit va_arg!");
7656 assert((Subtarget->isTargetLinux() ||
7657 Subtarget->isTargetDarwin()) &&
7658 "Unhandled target in LowerVAARG");
7659 assert(Op.getNode()->getNumOperands() == 4);
7660 SDValue Chain = Op.getOperand(0);
7661 SDValue SrcPtr = Op.getOperand(1);
7662 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
7663 unsigned Align = Op.getConstantOperandVal(3);
7664 DebugLoc dl = Op.getDebugLoc();
Dan Gohman9018e832008-05-10 01:26:14 +00007665
Dan Gohman320afb82010-10-12 18:00:49 +00007666 EVT ArgVT = Op.getNode()->getValueType(0);
7667 const Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
7668 uint32_t ArgSize = getTargetData()->getTypeAllocSize(ArgTy);
7669 uint8_t ArgMode;
7670
7671 // Decide which area this value should be read from.
7672 // TODO: Implement the AMD64 ABI in its entirety. This simple
7673 // selection mechanism works only for the basic types.
7674 if (ArgVT == MVT::f80) {
7675 llvm_unreachable("va_arg for f80 not yet implemented");
7676 } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
7677 ArgMode = 2; // Argument passed in XMM register. Use fp_offset.
7678 } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
7679 ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset.
7680 } else {
7681 llvm_unreachable("Unhandled argument type in LowerVAARG");
7682 }
7683
7684 if (ArgMode == 2) {
7685 // Sanity Check: Make sure using fp_offset makes sense.
Michael J. Spencer87b86652010-10-19 07:32:42 +00007686 assert(!UseSoftFloat &&
Eric Christopher52b45052010-10-12 19:44:17 +00007687 !(DAG.getMachineFunction()
7688 .getFunction()->hasFnAttr(Attribute::NoImplicitFloat)) &&
Nate Begeman2ea8ee72010-12-10 00:26:57 +00007689 Subtarget->hasXMM());
Dan Gohman320afb82010-10-12 18:00:49 +00007690 }
7691
7692 // Insert VAARG_64 node into the DAG
7693 // VAARG_64 returns two values: Variable Argument Address, Chain
7694 SmallVector<SDValue, 11> InstOps;
7695 InstOps.push_back(Chain);
7696 InstOps.push_back(SrcPtr);
7697 InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32));
7698 InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8));
7699 InstOps.push_back(DAG.getConstant(Align, MVT::i32));
7700 SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other);
7701 SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
7702 VTs, &InstOps[0], InstOps.size(),
7703 MVT::i64,
7704 MachinePointerInfo(SV),
7705 /*Align=*/0,
7706 /*Volatile=*/false,
7707 /*ReadMem=*/true,
7708 /*WriteMem=*/true);
7709 Chain = VAARG.getValue(1);
7710
7711 // Load the next argument and return it
7712 return DAG.getLoad(ArgVT, dl,
7713 Chain,
7714 VAARG,
7715 MachinePointerInfo(),
7716 false, false, 0);
Dan Gohman9018e832008-05-10 01:26:14 +00007717}
7718
Dan Gohmand858e902010-04-17 15:26:15 +00007719SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00007720 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00007721 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00007722 SDValue Chain = Op.getOperand(0);
7723 SDValue DstPtr = Op.getOperand(1);
7724 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00007725 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
7726 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Chris Lattnere72f2022010-09-21 05:40:29 +00007727 DebugLoc DL = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00007728
Chris Lattnere72f2022010-09-21 05:40:29 +00007729 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00007730 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
Michael J. Spencerec38de22010-10-10 22:04:20 +00007731 false,
Chris Lattnere72f2022010-09-21 05:40:29 +00007732 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
Evan Chengae642192007-03-02 23:16:35 +00007733}
7734
Dan Gohman475871a2008-07-27 21:46:04 +00007735SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00007736X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007737 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007738 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00007739 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00007740 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00007741 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00007742 case Intrinsic::x86_sse_comieq_ss:
7743 case Intrinsic::x86_sse_comilt_ss:
7744 case Intrinsic::x86_sse_comile_ss:
7745 case Intrinsic::x86_sse_comigt_ss:
7746 case Intrinsic::x86_sse_comige_ss:
7747 case Intrinsic::x86_sse_comineq_ss:
7748 case Intrinsic::x86_sse_ucomieq_ss:
7749 case Intrinsic::x86_sse_ucomilt_ss:
7750 case Intrinsic::x86_sse_ucomile_ss:
7751 case Intrinsic::x86_sse_ucomigt_ss:
7752 case Intrinsic::x86_sse_ucomige_ss:
7753 case Intrinsic::x86_sse_ucomineq_ss:
7754 case Intrinsic::x86_sse2_comieq_sd:
7755 case Intrinsic::x86_sse2_comilt_sd:
7756 case Intrinsic::x86_sse2_comile_sd:
7757 case Intrinsic::x86_sse2_comigt_sd:
7758 case Intrinsic::x86_sse2_comige_sd:
7759 case Intrinsic::x86_sse2_comineq_sd:
7760 case Intrinsic::x86_sse2_ucomieq_sd:
7761 case Intrinsic::x86_sse2_ucomilt_sd:
7762 case Intrinsic::x86_sse2_ucomile_sd:
7763 case Intrinsic::x86_sse2_ucomigt_sd:
7764 case Intrinsic::x86_sse2_ucomige_sd:
7765 case Intrinsic::x86_sse2_ucomineq_sd: {
7766 unsigned Opc = 0;
7767 ISD::CondCode CC = ISD::SETCC_INVALID;
7768 switch (IntNo) {
7769 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007770 case Intrinsic::x86_sse_comieq_ss:
7771 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007772 Opc = X86ISD::COMI;
7773 CC = ISD::SETEQ;
7774 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007775 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007776 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007777 Opc = X86ISD::COMI;
7778 CC = ISD::SETLT;
7779 break;
7780 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007781 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007782 Opc = X86ISD::COMI;
7783 CC = ISD::SETLE;
7784 break;
7785 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007786 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007787 Opc = X86ISD::COMI;
7788 CC = ISD::SETGT;
7789 break;
7790 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007791 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007792 Opc = X86ISD::COMI;
7793 CC = ISD::SETGE;
7794 break;
7795 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007796 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007797 Opc = X86ISD::COMI;
7798 CC = ISD::SETNE;
7799 break;
7800 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007801 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007802 Opc = X86ISD::UCOMI;
7803 CC = ISD::SETEQ;
7804 break;
7805 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007806 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007807 Opc = X86ISD::UCOMI;
7808 CC = ISD::SETLT;
7809 break;
7810 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007811 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007812 Opc = X86ISD::UCOMI;
7813 CC = ISD::SETLE;
7814 break;
7815 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007816 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007817 Opc = X86ISD::UCOMI;
7818 CC = ISD::SETGT;
7819 break;
7820 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00007821 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00007822 Opc = X86ISD::UCOMI;
7823 CC = ISD::SETGE;
7824 break;
7825 case Intrinsic::x86_sse_ucomineq_ss:
7826 case Intrinsic::x86_sse2_ucomineq_sd:
7827 Opc = X86ISD::UCOMI;
7828 CC = ISD::SETNE;
7829 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00007830 }
Evan Cheng734503b2006-09-11 02:19:56 +00007831
Dan Gohman475871a2008-07-27 21:46:04 +00007832 SDValue LHS = Op.getOperand(1);
7833 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00007834 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00007835 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007836 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
7837 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
7838 DAG.getConstant(X86CC, MVT::i8), Cond);
7839 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00007840 }
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007841 // ptest and testp intrinsics. The intrinsic these come from are designed to
7842 // return an integer value, not just an instruction so lower it to the ptest
7843 // or testp pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00007844 case Intrinsic::x86_sse41_ptestz:
7845 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007846 case Intrinsic::x86_sse41_ptestnzc:
7847 case Intrinsic::x86_avx_ptestz_256:
7848 case Intrinsic::x86_avx_ptestc_256:
7849 case Intrinsic::x86_avx_ptestnzc_256:
7850 case Intrinsic::x86_avx_vtestz_ps:
7851 case Intrinsic::x86_avx_vtestc_ps:
7852 case Intrinsic::x86_avx_vtestnzc_ps:
7853 case Intrinsic::x86_avx_vtestz_pd:
7854 case Intrinsic::x86_avx_vtestc_pd:
7855 case Intrinsic::x86_avx_vtestnzc_pd:
7856 case Intrinsic::x86_avx_vtestz_ps_256:
7857 case Intrinsic::x86_avx_vtestc_ps_256:
7858 case Intrinsic::x86_avx_vtestnzc_ps_256:
7859 case Intrinsic::x86_avx_vtestz_pd_256:
7860 case Intrinsic::x86_avx_vtestc_pd_256:
7861 case Intrinsic::x86_avx_vtestnzc_pd_256: {
7862 bool IsTestPacked = false;
Eric Christopher71c67532009-07-29 00:28:05 +00007863 unsigned X86CC = 0;
7864 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00007865 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007866 case Intrinsic::x86_avx_vtestz_ps:
7867 case Intrinsic::x86_avx_vtestz_pd:
7868 case Intrinsic::x86_avx_vtestz_ps_256:
7869 case Intrinsic::x86_avx_vtestz_pd_256:
7870 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007871 case Intrinsic::x86_sse41_ptestz:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007872 case Intrinsic::x86_avx_ptestz_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007873 // ZF = 1
7874 X86CC = X86::COND_E;
7875 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007876 case Intrinsic::x86_avx_vtestc_ps:
7877 case Intrinsic::x86_avx_vtestc_pd:
7878 case Intrinsic::x86_avx_vtestc_ps_256:
7879 case Intrinsic::x86_avx_vtestc_pd_256:
7880 IsTestPacked = true; // Fallthrough
Eric Christopher71c67532009-07-29 00:28:05 +00007881 case Intrinsic::x86_sse41_ptestc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007882 case Intrinsic::x86_avx_ptestc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007883 // CF = 1
7884 X86CC = X86::COND_B;
7885 break;
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007886 case Intrinsic::x86_avx_vtestnzc_ps:
7887 case Intrinsic::x86_avx_vtestnzc_pd:
7888 case Intrinsic::x86_avx_vtestnzc_ps_256:
7889 case Intrinsic::x86_avx_vtestnzc_pd_256:
7890 IsTestPacked = true; // Fallthrough
Eric Christopherfd179292009-08-27 18:07:15 +00007891 case Intrinsic::x86_sse41_ptestnzc:
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007892 case Intrinsic::x86_avx_ptestnzc_256:
Eric Christopher71c67532009-07-29 00:28:05 +00007893 // ZF and CF = 0
7894 X86CC = X86::COND_A;
7895 break;
7896 }
Eric Christopherfd179292009-08-27 18:07:15 +00007897
Eric Christopher71c67532009-07-29 00:28:05 +00007898 SDValue LHS = Op.getOperand(1);
7899 SDValue RHS = Op.getOperand(2);
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00007900 unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
7901 SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
Owen Anderson825b72b2009-08-11 20:47:22 +00007902 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
7903 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
7904 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00007905 }
Evan Cheng5759f972008-05-04 09:15:50 +00007906
7907 // Fix vector shift instructions where the last operand is a non-immediate
7908 // i32 value.
7909 case Intrinsic::x86_sse2_pslli_w:
7910 case Intrinsic::x86_sse2_pslli_d:
7911 case Intrinsic::x86_sse2_pslli_q:
7912 case Intrinsic::x86_sse2_psrli_w:
7913 case Intrinsic::x86_sse2_psrli_d:
7914 case Intrinsic::x86_sse2_psrli_q:
7915 case Intrinsic::x86_sse2_psrai_w:
7916 case Intrinsic::x86_sse2_psrai_d:
7917 case Intrinsic::x86_mmx_pslli_w:
7918 case Intrinsic::x86_mmx_pslli_d:
7919 case Intrinsic::x86_mmx_pslli_q:
7920 case Intrinsic::x86_mmx_psrli_w:
7921 case Intrinsic::x86_mmx_psrli_d:
7922 case Intrinsic::x86_mmx_psrli_q:
7923 case Intrinsic::x86_mmx_psrai_w:
7924 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00007925 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00007926 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00007927 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00007928
7929 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007930 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007931 switch (IntNo) {
7932 case Intrinsic::x86_sse2_pslli_w:
7933 NewIntNo = Intrinsic::x86_sse2_psll_w;
7934 break;
7935 case Intrinsic::x86_sse2_pslli_d:
7936 NewIntNo = Intrinsic::x86_sse2_psll_d;
7937 break;
7938 case Intrinsic::x86_sse2_pslli_q:
7939 NewIntNo = Intrinsic::x86_sse2_psll_q;
7940 break;
7941 case Intrinsic::x86_sse2_psrli_w:
7942 NewIntNo = Intrinsic::x86_sse2_psrl_w;
7943 break;
7944 case Intrinsic::x86_sse2_psrli_d:
7945 NewIntNo = Intrinsic::x86_sse2_psrl_d;
7946 break;
7947 case Intrinsic::x86_sse2_psrli_q:
7948 NewIntNo = Intrinsic::x86_sse2_psrl_q;
7949 break;
7950 case Intrinsic::x86_sse2_psrai_w:
7951 NewIntNo = Intrinsic::x86_sse2_psra_w;
7952 break;
7953 case Intrinsic::x86_sse2_psrai_d:
7954 NewIntNo = Intrinsic::x86_sse2_psra_d;
7955 break;
7956 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007957 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00007958 switch (IntNo) {
7959 case Intrinsic::x86_mmx_pslli_w:
7960 NewIntNo = Intrinsic::x86_mmx_psll_w;
7961 break;
7962 case Intrinsic::x86_mmx_pslli_d:
7963 NewIntNo = Intrinsic::x86_mmx_psll_d;
7964 break;
7965 case Intrinsic::x86_mmx_pslli_q:
7966 NewIntNo = Intrinsic::x86_mmx_psll_q;
7967 break;
7968 case Intrinsic::x86_mmx_psrli_w:
7969 NewIntNo = Intrinsic::x86_mmx_psrl_w;
7970 break;
7971 case Intrinsic::x86_mmx_psrli_d:
7972 NewIntNo = Intrinsic::x86_mmx_psrl_d;
7973 break;
7974 case Intrinsic::x86_mmx_psrli_q:
7975 NewIntNo = Intrinsic::x86_mmx_psrl_q;
7976 break;
7977 case Intrinsic::x86_mmx_psrai_w:
7978 NewIntNo = Intrinsic::x86_mmx_psra_w;
7979 break;
7980 case Intrinsic::x86_mmx_psrai_d:
7981 NewIntNo = Intrinsic::x86_mmx_psra_d;
7982 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00007983 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00007984 }
7985 break;
7986 }
7987 }
Mon P Wangefa42202009-09-03 19:56:25 +00007988
7989 // The vector shift intrinsics with scalars uses 32b shift amounts but
7990 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
7991 // to be zero.
7992 SDValue ShOps[4];
7993 ShOps[0] = ShAmt;
7994 ShOps[1] = DAG.getConstant(0, MVT::i32);
7995 if (ShAmtVT == MVT::v4i32) {
7996 ShOps[2] = DAG.getUNDEF(MVT::i32);
7997 ShOps[3] = DAG.getUNDEF(MVT::i32);
7998 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
7999 } else {
8000 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
Dale Johannesen0488fb62010-09-30 23:57:10 +00008001// FIXME this must be lowered to get rid of the invalid type.
Mon P Wangefa42202009-09-03 19:56:25 +00008002 }
8003
Owen Andersone50ed302009-08-10 22:56:29 +00008004 EVT VT = Op.getValueType();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008005 ShAmt = DAG.getNode(ISD::BITCAST, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008006 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008007 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00008008 Op.getOperand(1), ShAmt);
8009 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00008010 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00008011}
Evan Cheng72261582005-12-20 06:22:03 +00008012
Dan Gohmand858e902010-04-17 15:26:15 +00008013SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
8014 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00008015 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8016 MFI->setReturnAddressIsTaken(true);
8017
Bill Wendling64e87322009-01-16 19:25:27 +00008018 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008019 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00008020
8021 if (Depth > 0) {
8022 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
8023 SDValue Offset =
8024 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008025 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008026 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008027 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008028 FrameAddr, Offset),
Chris Lattner51abfe42010-09-21 06:02:19 +00008029 MachinePointerInfo(), false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00008030 }
8031
8032 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00008033 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008034 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008035 RetAddrFI, MachinePointerInfo(), false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008036}
8037
Dan Gohmand858e902010-04-17 15:26:15 +00008038SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00008039 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8040 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00008041
Owen Andersone50ed302009-08-10 22:56:29 +00008042 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008043 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00008044 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
8045 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00008046 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00008047 while (Depth--)
Chris Lattner51abfe42010-09-21 06:02:19 +00008048 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
8049 MachinePointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +00008050 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00008051 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00008052}
8053
Dan Gohman475871a2008-07-27 21:46:04 +00008054SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008055 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008056 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008057}
8058
Dan Gohmand858e902010-04-17 15:26:15 +00008059SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008060 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00008061 SDValue Chain = Op.getOperand(0);
8062 SDValue Offset = Op.getOperand(1);
8063 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008064 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008065
Dan Gohmand8816272010-08-11 18:14:00 +00008066 SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl,
8067 Subtarget->is64Bit() ? X86::RBP : X86::EBP,
8068 getPointerTy());
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008069 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008070
Dan Gohmand8816272010-08-11 18:14:00 +00008071 SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), Frame,
8072 DAG.getIntPtrConstant(TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008073 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
Chris Lattner8026a9d2010-09-21 17:50:43 +00008074 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
8075 false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00008076 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008077 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008078
Dale Johannesene4d209d2009-02-03 20:21:25 +00008079 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008080 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00008081 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008082}
8083
Dan Gohman475871a2008-07-27 21:46:04 +00008084SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008085 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008086 SDValue Root = Op.getOperand(0);
8087 SDValue Trmp = Op.getOperand(1); // trampoline
8088 SDValue FPtr = Op.getOperand(2); // nested function
8089 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008090 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008091
Dan Gohman69de1932008-02-06 22:27:42 +00008092 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008093
8094 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00008095 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00008096
8097 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00008098 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
8099 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00008100
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008101 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
8102 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00008103
8104 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
8105
8106 // Load the pointer to the nested function into R11.
8107 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00008108 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008109 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008110 Addr, MachinePointerInfo(TrmpAddr),
8111 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008112
Owen Anderson825b72b2009-08-11 20:47:22 +00008113 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8114 DAG.getConstant(2, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008115 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
8116 MachinePointerInfo(TrmpAddr, 2),
David Greene67c9d422010-02-15 16:53:33 +00008117 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008118
8119 // Load the 'nest' parameter value into R10.
8120 // R10 is specified in X86CallingConv.td
8121 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00008122 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8123 DAG.getConstant(10, MVT::i64));
8124 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008125 Addr, MachinePointerInfo(TrmpAddr, 10),
8126 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008127
Owen Anderson825b72b2009-08-11 20:47:22 +00008128 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8129 DAG.getConstant(12, MVT::i64));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008130 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
8131 MachinePointerInfo(TrmpAddr, 12),
David Greene67c9d422010-02-15 16:53:33 +00008132 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00008133
8134 // Jump to the nested function.
8135 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00008136 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8137 DAG.getConstant(20, MVT::i64));
8138 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008139 Addr, MachinePointerInfo(TrmpAddr, 20),
8140 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008141
8142 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00008143 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
8144 DAG.getConstant(22, MVT::i64));
8145 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008146 MachinePointerInfo(TrmpAddr, 22),
8147 false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00008148
Dan Gohman475871a2008-07-27 21:46:04 +00008149 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008150 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008151 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008152 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00008153 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00008154 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00008155 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00008156 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008157
8158 switch (CC) {
8159 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008160 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008161 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008162 case CallingConv::X86_StdCall: {
8163 // Pass 'nest' parameter in ECX.
8164 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008165 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008166
8167 // Check that ECX wasn't needed by an 'inreg' parameter.
8168 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00008169 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00008170
Chris Lattner58d74912008-03-12 17:45:29 +00008171 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00008172 unsigned InRegCount = 0;
8173 unsigned Idx = 1;
8174
8175 for (FunctionType::param_iterator I = FTy->param_begin(),
8176 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00008177 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00008178 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00008179 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008180
8181 if (InRegCount > 2) {
Eric Christopher90eb4022010-07-22 00:26:08 +00008182 report_fatal_error("Nest register in use - reduce number of inreg"
8183 " parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00008184 }
8185 }
8186 break;
8187 }
8188 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00008189 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00008190 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00008191 // Pass 'nest' parameter in EAX.
8192 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00008193 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008194 break;
8195 }
8196
Dan Gohman475871a2008-07-27 21:46:04 +00008197 SDValue OutChains[4];
8198 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00008199
Owen Anderson825b72b2009-08-11 20:47:22 +00008200 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8201 DAG.getConstant(10, MVT::i32));
8202 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008203
Chris Lattnera62fe662010-02-05 19:20:30 +00008204 // This is storing the opcode for MOV32ri.
8205 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00008206 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008207 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00008208 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Chris Lattner8026a9d2010-09-21 17:50:43 +00008209 Trmp, MachinePointerInfo(TrmpAddr),
8210 false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008211
Owen Anderson825b72b2009-08-11 20:47:22 +00008212 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8213 DAG.getConstant(1, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008214 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
8215 MachinePointerInfo(TrmpAddr, 1),
David Greene67c9d422010-02-15 16:53:33 +00008216 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008217
Chris Lattnera62fe662010-02-05 19:20:30 +00008218 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00008219 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8220 DAG.getConstant(5, MVT::i32));
8221 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Chris Lattner8026a9d2010-09-21 17:50:43 +00008222 MachinePointerInfo(TrmpAddr, 5),
8223 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008224
Owen Anderson825b72b2009-08-11 20:47:22 +00008225 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
8226 DAG.getConstant(6, MVT::i32));
Chris Lattner8026a9d2010-09-21 17:50:43 +00008227 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
8228 MachinePointerInfo(TrmpAddr, 6),
David Greene67c9d422010-02-15 16:53:33 +00008229 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008230
Dan Gohman475871a2008-07-27 21:46:04 +00008231 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00008232 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008233 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008234 }
8235}
8236
Dan Gohmand858e902010-04-17 15:26:15 +00008237SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
8238 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008239 /*
8240 The rounding mode is in bits 11:10 of FPSR, and has the following
8241 settings:
8242 00 Round to nearest
8243 01 Round to -inf
8244 10 Round to +inf
8245 11 Round to 0
8246
8247 FLT_ROUNDS, on the other hand, expects the following:
8248 -1 Undefined
8249 0 Round to 0
8250 1 Round to nearest
8251 2 Round to +inf
8252 3 Round to -inf
8253
8254 To perform the conversion, we do:
8255 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
8256 */
8257
8258 MachineFunction &MF = DAG.getMachineFunction();
8259 const TargetMachine &TM = MF.getTarget();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00008260 const TargetFrameLowering &TFI = *TM.getFrameLowering();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008261 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008262 EVT VT = Op.getValueType();
Chris Lattner2156b792010-09-22 01:11:26 +00008263 DebugLoc DL = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008264
8265 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00008266 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00008267 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008268
Michael J. Spencerec38de22010-10-10 22:04:20 +00008269
Chris Lattner2156b792010-09-22 01:11:26 +00008270 MachineMemOperand *MMO =
8271 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI),
8272 MachineMemOperand::MOStore, 2, 2);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008273
Chris Lattner2156b792010-09-22 01:11:26 +00008274 SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
8275 SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
8276 DAG.getVTList(MVT::Other),
8277 Ops, 2, MVT::i16, MMO);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008278
8279 // Load FP Control Word from stack slot
Chris Lattner2156b792010-09-22 01:11:26 +00008280 SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
Chris Lattner51abfe42010-09-21 06:02:19 +00008281 MachinePointerInfo(), false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008282
8283 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00008284 SDValue CWD1 =
Chris Lattner2156b792010-09-22 01:11:26 +00008285 DAG.getNode(ISD::SRL, DL, MVT::i16,
8286 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008287 CWD, DAG.getConstant(0x800, MVT::i16)),
8288 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00008289 SDValue CWD2 =
Chris Lattner2156b792010-09-22 01:11:26 +00008290 DAG.getNode(ISD::SRL, DL, MVT::i16,
8291 DAG.getNode(ISD::AND, DL, MVT::i16,
Owen Anderson825b72b2009-08-11 20:47:22 +00008292 CWD, DAG.getConstant(0x400, MVT::i16)),
8293 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008294
Dan Gohman475871a2008-07-27 21:46:04 +00008295 SDValue RetVal =
Chris Lattner2156b792010-09-22 01:11:26 +00008296 DAG.getNode(ISD::AND, DL, MVT::i16,
8297 DAG.getNode(ISD::ADD, DL, MVT::i16,
8298 DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
Owen Anderson825b72b2009-08-11 20:47:22 +00008299 DAG.getConstant(1, MVT::i16)),
8300 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008301
8302
Duncan Sands83ec4b62008-06-06 12:08:01 +00008303 return DAG.getNode((VT.getSizeInBits() < 16 ?
Chris Lattner2156b792010-09-22 01:11:26 +00008304 ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008305}
8306
Dan Gohmand858e902010-04-17 15:26:15 +00008307SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008308 EVT VT = Op.getValueType();
8309 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008310 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008311 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008312
8313 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008314 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00008315 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00008316 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008317 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008318 }
Evan Cheng18efe262007-12-14 02:13:44 +00008319
Evan Cheng152804e2007-12-14 08:30:15 +00008320 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008321 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008322 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008323
8324 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008325 SDValue Ops[] = {
8326 Op,
8327 DAG.getConstant(NumBits+NumBits-1, OpVT),
8328 DAG.getConstant(X86::COND_E, MVT::i8),
8329 Op.getValue(1)
8330 };
8331 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008332
8333 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00008334 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00008335
Owen Anderson825b72b2009-08-11 20:47:22 +00008336 if (VT == MVT::i8)
8337 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008338 return Op;
8339}
8340
Dan Gohmand858e902010-04-17 15:26:15 +00008341SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008342 EVT VT = Op.getValueType();
8343 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008344 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008345 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00008346
8347 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008348 if (VT == MVT::i8) {
8349 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00008350 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008351 }
Evan Cheng152804e2007-12-14 08:30:15 +00008352
8353 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008354 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008355 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00008356
8357 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00008358 SDValue Ops[] = {
8359 Op,
8360 DAG.getConstant(NumBits, OpVT),
8361 DAG.getConstant(X86::COND_E, MVT::i8),
8362 Op.getValue(1)
8363 };
8364 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00008365
Owen Anderson825b72b2009-08-11 20:47:22 +00008366 if (VT == MVT::i8)
8367 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00008368 return Op;
8369}
8370
Dan Gohmand858e902010-04-17 15:26:15 +00008371SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008372 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00008373 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008374 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008375
Mon P Wangaf9b9522008-12-18 21:42:19 +00008376 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
8377 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
8378 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
8379 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
8380 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
8381 //
8382 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
8383 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
8384 // return AloBlo + AloBhi + AhiBlo;
8385
8386 SDValue A = Op.getOperand(0);
8387 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008388
Dale Johannesene4d209d2009-02-03 20:21:25 +00008389 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008390 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8391 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008392 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008393 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8394 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008395 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008396 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008397 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008398 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008399 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008400 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008401 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008402 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00008403 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008404 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008405 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8406 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008407 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00008408 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8409 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008410 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
8411 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008412 return Res;
8413}
8414
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008415SDValue X86TargetLowering::LowerSHL(SDValue Op, SelectionDAG &DAG) const {
8416 EVT VT = Op.getValueType();
8417 DebugLoc dl = Op.getDebugLoc();
8418 SDValue R = Op.getOperand(0);
8419
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008420 LLVMContext *Context = DAG.getContext();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008421
Nate Begeman51409212010-07-28 00:21:48 +00008422 assert(Subtarget->hasSSE41() && "Cannot lower SHL without SSE4.1 or later");
8423
8424 if (VT == MVT::v4i32) {
8425 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8426 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8427 Op.getOperand(1), DAG.getConstant(23, MVT::i32));
8428
8429 ConstantInt *CI = ConstantInt::get(*Context, APInt(32, 0x3f800000U));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008430
Nate Begeman51409212010-07-28 00:21:48 +00008431 std::vector<Constant*> CV(4, CI);
8432 Constant *C = ConstantVector::get(CV);
8433 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8434 SDValue Addend = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008435 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008436 false, false, 16);
8437
8438 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Addend);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008439 Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008440 Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
8441 return DAG.getNode(ISD::MUL, dl, VT, Op, R);
8442 }
8443 if (VT == MVT::v16i8) {
8444 // a = a << 5;
8445 Op = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8446 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8447 Op.getOperand(1), DAG.getConstant(5, MVT::i32));
8448
8449 ConstantInt *CM1 = ConstantInt::get(*Context, APInt(8, 15));
8450 ConstantInt *CM2 = ConstantInt::get(*Context, APInt(8, 63));
8451
8452 std::vector<Constant*> CVM1(16, CM1);
8453 std::vector<Constant*> CVM2(16, CM2);
8454 Constant *C = ConstantVector::get(CVM1);
8455 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8456 SDValue M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008457 MachinePointerInfo::getConstantPool(),
Nate Begeman51409212010-07-28 00:21:48 +00008458 false, false, 16);
8459
8460 // r = pblendv(r, psllw(r & (char16)15, 4), a);
8461 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8462 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8463 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8464 DAG.getConstant(4, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008465 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008466 // a += a
8467 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008468
Nate Begeman51409212010-07-28 00:21:48 +00008469 C = ConstantVector::get(CVM2);
8470 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
8471 M = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Chris Lattnere8639032010-09-21 06:22:23 +00008472 MachinePointerInfo::getConstantPool(),
Chris Lattner51abfe42010-09-21 06:02:19 +00008473 false, false, 16);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008474
Nate Begeman51409212010-07-28 00:21:48 +00008475 // r = pblendv(r, psllw(r & (char16)63, 2), a);
8476 M = DAG.getNode(ISD::AND, dl, VT, R, M);
8477 M = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
8478 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32), M,
8479 DAG.getConstant(2, MVT::i32));
Nate Begeman672fb622010-12-20 22:04:24 +00008480 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT, R, M, Op);
Nate Begeman51409212010-07-28 00:21:48 +00008481 // a += a
8482 Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008483
Nate Begeman51409212010-07-28 00:21:48 +00008484 // return pblendv(r, r+r, a);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008485 R = DAG.getNode(X86ISD::PBLENDVB, dl, VT,
Nate Begeman51409212010-07-28 00:21:48 +00008486 R, DAG.getNode(ISD::ADD, dl, VT, R, R), Op);
8487 return R;
8488 }
8489 return SDValue();
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008490}
Mon P Wangaf9b9522008-12-18 21:42:19 +00008491
Dan Gohmand858e902010-04-17 15:26:15 +00008492SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00008493 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
8494 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00008495 // looks for this combo and may remove the "setcc" instruction if the "setcc"
8496 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00008497 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00008498 SDValue LHS = N->getOperand(0);
8499 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00008500 unsigned BaseOp = 0;
8501 unsigned Cond = 0;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008502 DebugLoc DL = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00008503 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008504 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00008505 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00008506 // A subtract of one will be selected as a INC. Note that INC doesn't
8507 // set CF, so we can't do this for UADDO.
8508 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8509 if (C->getAPIntValue() == 1) {
8510 BaseOp = X86ISD::INC;
8511 Cond = X86::COND_O;
8512 break;
8513 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008514 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00008515 Cond = X86::COND_O;
8516 break;
8517 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008518 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00008519 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008520 break;
8521 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00008522 // A subtract of one will be selected as a DEC. Note that DEC doesn't
8523 // set CF, so we can't do this for USUBO.
8524 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
8525 if (C->getAPIntValue() == 1) {
8526 BaseOp = X86ISD::DEC;
8527 Cond = X86::COND_O;
8528 break;
8529 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008530 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00008531 Cond = X86::COND_O;
8532 break;
8533 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008534 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00008535 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00008536 break;
8537 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00008538 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00008539 Cond = X86::COND_O;
8540 break;
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008541 case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
8542 SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
8543 MVT::i32);
8544 SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008545
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008546 SDValue SetCC =
8547 DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8548 DAG.getConstant(X86::COND_O, MVT::i32),
8549 SDValue(Sum.getNode(), 2));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008550
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008551 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8552 return Sum;
8553 }
Bill Wendling74c37652008-12-09 22:08:41 +00008554 }
Bill Wendling3fafd932008-11-26 22:37:40 +00008555
Bill Wendling61edeb52008-12-02 01:06:39 +00008556 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00008557 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008558 SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00008559
Bill Wendling61edeb52008-12-02 01:06:39 +00008560 SDValue SetCC =
Chris Lattnerb20e0b12010-12-05 07:30:36 +00008561 DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
8562 DAG.getConstant(Cond, MVT::i32),
8563 SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00008564
Bill Wendling61edeb52008-12-02 01:06:39 +00008565 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
8566 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00008567}
8568
Eric Christopher9a9d2752010-07-22 02:48:34 +00008569SDValue X86TargetLowering::LowerMEMBARRIER(SDValue Op, SelectionDAG &DAG) const{
8570 DebugLoc dl = Op.getDebugLoc();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008571
Eric Christopherb6729dc2010-08-04 23:03:04 +00008572 if (!Subtarget->hasSSE2()) {
Eric Christopherc0b2a202010-08-14 21:51:50 +00008573 SDValue Chain = Op.getOperand(0);
Michael J. Spencerec38de22010-10-10 22:04:20 +00008574 SDValue Zero = DAG.getConstant(0,
Eric Christopherb6729dc2010-08-04 23:03:04 +00008575 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Eric Christopherc0b2a202010-08-14 21:51:50 +00008576 SDValue Ops[] = {
8577 DAG.getRegister(X86::ESP, MVT::i32), // Base
8578 DAG.getTargetConstant(1, MVT::i8), // Scale
8579 DAG.getRegister(0, MVT::i32), // Index
8580 DAG.getTargetConstant(0, MVT::i32), // Disp
8581 DAG.getRegister(0, MVT::i32), // Segment.
8582 Zero,
8583 Chain
8584 };
Michael J. Spencerec38de22010-10-10 22:04:20 +00008585 SDNode *Res =
Eric Christopherc0b2a202010-08-14 21:51:50 +00008586 DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops,
8587 array_lengthof(Ops));
8588 return SDValue(Res, 0);
Eric Christopherb6729dc2010-08-04 23:03:04 +00008589 }
Michael J. Spencerec38de22010-10-10 22:04:20 +00008590
Eric Christopher9a9d2752010-07-22 02:48:34 +00008591 unsigned isDev = cast<ConstantSDNode>(Op.getOperand(5))->getZExtValue();
Chris Lattner132929a2010-08-14 17:26:09 +00008592 if (!isDev)
Eric Christopher9a9d2752010-07-22 02:48:34 +00008593 return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008594
Chris Lattner132929a2010-08-14 17:26:09 +00008595 unsigned Op1 = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
8596 unsigned Op2 = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
8597 unsigned Op3 = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
8598 unsigned Op4 = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
Michael J. Spencerec38de22010-10-10 22:04:20 +00008599
Chris Lattner132929a2010-08-14 17:26:09 +00008600 // def : Pat<(membarrier (i8 0), (i8 0), (i8 0), (i8 1), (i8 1)), (SFENCE)>;
8601 if (!Op1 && !Op2 && !Op3 && Op4)
8602 return DAG.getNode(X86ISD::SFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008603
Chris Lattner132929a2010-08-14 17:26:09 +00008604 // def : Pat<(membarrier (i8 1), (i8 0), (i8 0), (i8 0), (i8 1)), (LFENCE)>;
8605 if (Op1 && !Op2 && !Op3 && !Op4)
8606 return DAG.getNode(X86ISD::LFENCE, dl, MVT::Other, Op.getOperand(0));
Michael J. Spencerec38de22010-10-10 22:04:20 +00008607
8608 // def : Pat<(membarrier (i8 imm), (i8 imm), (i8 imm), (i8 imm), (i8 1)),
Chris Lattner132929a2010-08-14 17:26:09 +00008609 // (MFENCE)>;
8610 return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
Eric Christopher9a9d2752010-07-22 02:48:34 +00008611}
8612
Dan Gohmand858e902010-04-17 15:26:15 +00008613SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008614 EVT T = Op.getValueType();
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008615 DebugLoc DL = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00008616 unsigned Reg = 0;
8617 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00008618 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00008619 default:
8620 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00008621 case MVT::i8: Reg = X86::AL; size = 1; break;
8622 case MVT::i16: Reg = X86::AX; size = 2; break;
8623 case MVT::i32: Reg = X86::EAX; size = 4; break;
8624 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00008625 assert(Subtarget->is64Bit() && "Node not type legal!");
8626 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00008627 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00008628 }
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008629 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00008630 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00008631 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008632 Op.getOperand(1),
8633 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00008634 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00008635 cpIn.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008636 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008637 MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
8638 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
8639 Ops, 5, T, MMO);
Scott Michelfdc40a02009-02-17 22:15:04 +00008640 SDValue cpOut =
Chris Lattner93c4a5b2010-09-21 23:59:42 +00008641 DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00008642 return cpOut;
8643}
8644
Duncan Sands1607f052008-12-01 11:39:25 +00008645SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00008646 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00008647 assert(Subtarget->is64Bit() && "Result not type legalized?");
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008648 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008649 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00008650 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008651 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008652 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
8653 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00008654 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00008655 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
8656 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00008657 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00008658 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00008659 rdx.getValue(1)
8660 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00008661 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008662}
8663
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008664SDValue X86TargetLowering::LowerBITCAST(SDValue Op,
Dale Johannesen7d07b482010-05-21 00:52:33 +00008665 SelectionDAG &DAG) const {
8666 EVT SrcVT = Op.getOperand(0).getValueType();
8667 EVT DstVT = Op.getValueType();
Chris Lattner2a786eb2010-12-19 20:19:20 +00008668 assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
8669 Subtarget->hasMMX() && "Unexpected custom BITCAST");
Michael J. Spencerec38de22010-10-10 22:04:20 +00008670 assert((DstVT == MVT::i64 ||
Dale Johannesen7d07b482010-05-21 00:52:33 +00008671 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008672 "Unexpected custom BITCAST");
Dale Johannesen7d07b482010-05-21 00:52:33 +00008673 // i64 <=> MMX conversions are Legal.
8674 if (SrcVT==MVT::i64 && DstVT.isVector())
8675 return Op;
8676 if (DstVT==MVT::i64 && SrcVT.isVector())
8677 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00008678 // MMX <=> MMX conversions are Legal.
8679 if (SrcVT.isVector() && DstVT.isVector())
8680 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00008681 // All other conversions need to be expanded.
8682 return SDValue();
8683}
Chris Lattner5b856542010-12-20 00:59:46 +00008684
Dan Gohmand858e902010-04-17 15:26:15 +00008685SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008686 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00008687 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008688 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008689 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00008690 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00008691 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008692 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00008693 Node->getOperand(0),
8694 Node->getOperand(1), negOp,
8695 cast<AtomicSDNode>(Node)->getSrcValue(),
8696 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00008697}
8698
Chris Lattner5b856542010-12-20 00:59:46 +00008699static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
8700 EVT VT = Op.getNode()->getValueType(0);
8701
8702 // Let legalize expand this if it isn't a legal type yet.
8703 if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
8704 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008705
Chris Lattner5b856542010-12-20 00:59:46 +00008706 SDVTList VTs = DAG.getVTList(VT, MVT::i32);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008707
Chris Lattner5b856542010-12-20 00:59:46 +00008708 unsigned Opc;
8709 bool ExtraOp = false;
8710 switch (Op.getOpcode()) {
8711 default: assert(0 && "Invalid code");
8712 case ISD::ADDC: Opc = X86ISD::ADD; break;
8713 case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
8714 case ISD::SUBC: Opc = X86ISD::SUB; break;
8715 case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
8716 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00008717
Chris Lattner5b856542010-12-20 00:59:46 +00008718 if (!ExtraOp)
8719 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8720 Op.getOperand(1));
8721 return DAG.getNode(Opc, Op->getDebugLoc(), VTs, Op.getOperand(0),
8722 Op.getOperand(1), Op.getOperand(2));
8723}
8724
Evan Cheng0db9fe62006-04-25 20:13:52 +00008725/// LowerOperation - Provide custom lowering hooks for some operations.
8726///
Dan Gohmand858e902010-04-17 15:26:15 +00008727SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00008728 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008729 default: llvm_unreachable("Should not custom lower this!");
Eric Christopher9a9d2752010-07-22 02:48:34 +00008730 case ISD::MEMBARRIER: return LowerMEMBARRIER(Op,DAG);
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008731 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
8732 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008733 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00008734 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008735 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
8736 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
8737 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
David Greene91585092011-01-26 15:38:49 +00008738 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
David Greenecfe33c42011-01-26 19:13:22 +00008739 case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008740 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
8741 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
8742 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008743 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00008744 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00008745 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008746 case ISD::SHL_PARTS:
8747 case ISD::SRA_PARTS:
8748 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
8749 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00008750 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008751 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00008752 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008753 case ISD::FABS: return LowerFABS(Op, DAG);
8754 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00008755 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008756 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00008757 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00008758 case ISD::SELECT: return LowerSELECT(Op, DAG);
8759 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008760 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008761 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00008762 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00008763 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008764 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00008765 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
8766 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008767 case ISD::FRAME_TO_ARGS_OFFSET:
8768 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00008769 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008770 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00008771 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00008772 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00008773 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
8774 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00008775 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Nate Begemanbdcb5af2010-07-27 22:37:06 +00008776 case ISD::SHL: return LowerSHL(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00008777 case ISD::SADDO:
8778 case ISD::UADDO:
8779 case ISD::SSUBO:
8780 case ISD::USUBO:
8781 case ISD::SMULO:
8782 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00008783 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008784 case ISD::BITCAST: return LowerBITCAST(Op, DAG);
Chris Lattner5b856542010-12-20 00:59:46 +00008785 case ISD::ADDC:
8786 case ISD::ADDE:
8787 case ISD::SUBC:
8788 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00008789 }
Chris Lattner27a6c732007-11-24 07:07:01 +00008790}
8791
Duncan Sands1607f052008-12-01 11:39:25 +00008792void X86TargetLowering::
8793ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008794 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00008795 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008796 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00008797 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00008798
8799 SDValue Chain = Node->getOperand(0);
8800 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008801 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008802 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00008803 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008804 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00008805 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00008806 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00008807 SDValue Result =
8808 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
8809 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00008810 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008811 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008812 Results.push_back(Result.getValue(2));
8813}
8814
Duncan Sands126d9072008-07-04 11:47:58 +00008815/// ReplaceNodeResults - Replace a node with an illegal result type
8816/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00008817void X86TargetLowering::ReplaceNodeResults(SDNode *N,
8818 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00008819 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008820 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00008821 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00008822 default:
Duncan Sands1607f052008-12-01 11:39:25 +00008823 assert(false && "Do not know how to custom type legalize this operation!");
8824 return;
Chris Lattner5b856542010-12-20 00:59:46 +00008825 case ISD::ADDC:
8826 case ISD::ADDE:
8827 case ISD::SUBC:
8828 case ISD::SUBE:
8829 // We don't want to expand or promote these.
8830 return;
Duncan Sands1607f052008-12-01 11:39:25 +00008831 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00008832 std::pair<SDValue,SDValue> Vals =
8833 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00008834 SDValue FIST = Vals.first, StackSlot = Vals.second;
8835 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00008836 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00008837 // Return a load from the stack slot.
Chris Lattner51abfe42010-09-21 06:02:19 +00008838 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
8839 MachinePointerInfo(), false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00008840 }
8841 return;
8842 }
8843 case ISD::READCYCLECOUNTER: {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008844 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Duncan Sands1607f052008-12-01 11:39:25 +00008845 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008846 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00008847 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00008848 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00008849 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00008850 eax.getValue(2));
8851 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
8852 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00008853 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008854 Results.push_back(edx.getValue(1));
8855 return;
8856 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008857 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00008858 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00008859 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00008860 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008861 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8862 DAG.getConstant(0, MVT::i32));
8863 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
8864 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008865 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
8866 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008867 cpInL.getValue(1));
8868 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00008869 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8870 DAG.getConstant(0, MVT::i32));
8871 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
8872 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00008873 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00008874 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008875 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00008876 swapInL.getValue(1));
8877 SDValue Ops[] = { swapInH.getValue(0),
8878 N->getOperand(1),
8879 swapInH.getValue(1) };
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00008880 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Andrew Trick1a2cf3b2010-10-11 19:02:04 +00008881 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
8882 SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys,
8883 Ops, 3, T, MMO);
Dale Johannesendd64c412009-02-04 00:33:20 +00008884 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008885 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00008886 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00008887 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00008888 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00008889 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00008890 Results.push_back(cpOutH.getValue(1));
8891 return;
8892 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008893 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00008894 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
8895 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008896 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00008897 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
8898 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008899 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00008900 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
8901 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008902 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00008903 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
8904 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008905 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00008906 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
8907 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008908 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00008909 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
8910 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008911 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00008912 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
8913 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00008914 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00008915}
8916
Evan Cheng72261582005-12-20 06:22:03 +00008917const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
8918 switch (Opcode) {
8919 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00008920 case X86ISD::BSF: return "X86ISD::BSF";
8921 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00008922 case X86ISD::SHLD: return "X86ISD::SHLD";
8923 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00008924 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008925 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00008926 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00008927 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00008928 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00008929 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00008930 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
8931 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
8932 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00008933 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00008934 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00008935 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00008936 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00008937 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00008938 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00008939 case X86ISD::COMI: return "X86ISD::COMI";
8940 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00008941 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00008942 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00008943 case X86ISD::CMOV: return "X86ISD::CMOV";
8944 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00008945 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00008946 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
8947 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00008948 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00008949 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00008950 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008951 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00008952 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00008953 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
8954 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00008955 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00008956 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Nate Begemanb65c1752010-12-17 22:55:37 +00008957 case X86ISD::PANDN: return "X86ISD::PANDN";
8958 case X86ISD::PSIGNB: return "X86ISD::PSIGNB";
8959 case X86ISD::PSIGNW: return "X86ISD::PSIGNW";
8960 case X86ISD::PSIGND: return "X86ISD::PSIGND";
Nate Begeman672fb622010-12-20 22:04:24 +00008961 case X86ISD::PBLENDVB: return "X86ISD::PBLENDVB";
Evan Cheng8ca29322006-11-10 21:43:37 +00008962 case X86ISD::FMAX: return "X86ISD::FMAX";
8963 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00008964 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
8965 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00008966 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Eric Christopher30ef0e52010-06-03 04:07:48 +00008967 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00008968 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00008969 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00008970 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00008971 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
8972 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008973 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
8974 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
8975 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
8976 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
8977 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
8978 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00008979 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
8980 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00008981 case X86ISD::VSHL: return "X86ISD::VSHL";
8982 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00008983 case X86ISD::CMPPD: return "X86ISD::CMPPD";
8984 case X86ISD::CMPPS: return "X86ISD::CMPPS";
8985 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
8986 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
8987 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
8988 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
8989 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
8990 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
8991 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
8992 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00008993 case X86ISD::ADD: return "X86ISD::ADD";
8994 case X86ISD::SUB: return "X86ISD::SUB";
Chris Lattner5b856542010-12-20 00:59:46 +00008995 case X86ISD::ADC: return "X86ISD::ADC";
8996 case X86ISD::SBB: return "X86ISD::SBB";
Bill Wendlingd350e022008-12-12 21:15:41 +00008997 case X86ISD::SMUL: return "X86ISD::SMUL";
8998 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00008999 case X86ISD::INC: return "X86ISD::INC";
9000 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00009001 case X86ISD::OR: return "X86ISD::OR";
9002 case X86ISD::XOR: return "X86ISD::XOR";
9003 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00009004 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00009005 case X86ISD::PTEST: return "X86ISD::PTEST";
Bruno Cardoso Lopes045573c2010-08-10 23:25:42 +00009006 case X86ISD::TESTP: return "X86ISD::TESTP";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009007 case X86ISD::PALIGN: return "X86ISD::PALIGN";
9008 case X86ISD::PSHUFD: return "X86ISD::PSHUFD";
9009 case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW";
9010 case X86ISD::PSHUFHW_LD: return "X86ISD::PSHUFHW_LD";
9011 case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW";
9012 case X86ISD::PSHUFLW_LD: return "X86ISD::PSHUFLW_LD";
9013 case X86ISD::SHUFPS: return "X86ISD::SHUFPS";
9014 case X86ISD::SHUFPD: return "X86ISD::SHUFPD";
9015 case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009016 case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD";
Bruno Cardoso Lopesf2db5b42010-08-31 21:15:21 +00009017 case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009018 case X86ISD::MOVHLPD: return "X86ISD::MOVHLPD";
Bruno Cardoso Lopes56098f52010-09-01 05:08:25 +00009019 case X86ISD::MOVLPS: return "X86ISD::MOVLPS";
9020 case X86ISD::MOVLPD: return "X86ISD::MOVLPD";
Bruno Cardoso Lopes3157ef12010-08-20 22:55:05 +00009021 case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP";
9022 case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP";
9023 case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP";
9024 case X86ISD::MOVSHDUP_LD: return "X86ISD::MOVSHDUP_LD";
9025 case X86ISD::MOVSLDUP_LD: return "X86ISD::MOVSLDUP_LD";
9026 case X86ISD::MOVSD: return "X86ISD::MOVSD";
9027 case X86ISD::MOVSS: return "X86ISD::MOVSS";
9028 case X86ISD::UNPCKLPS: return "X86ISD::UNPCKLPS";
9029 case X86ISD::UNPCKLPD: return "X86ISD::UNPCKLPD";
9030 case X86ISD::UNPCKHPS: return "X86ISD::UNPCKHPS";
9031 case X86ISD::UNPCKHPD: return "X86ISD::UNPCKHPD";
9032 case X86ISD::PUNPCKLBW: return "X86ISD::PUNPCKLBW";
9033 case X86ISD::PUNPCKLWD: return "X86ISD::PUNPCKLWD";
9034 case X86ISD::PUNPCKLDQ: return "X86ISD::PUNPCKLDQ";
9035 case X86ISD::PUNPCKLQDQ: return "X86ISD::PUNPCKLQDQ";
9036 case X86ISD::PUNPCKHBW: return "X86ISD::PUNPCKHBW";
9037 case X86ISD::PUNPCKHWD: return "X86ISD::PUNPCKHWD";
9038 case X86ISD::PUNPCKHDQ: return "X86ISD::PUNPCKHDQ";
9039 case X86ISD::PUNPCKHQDQ: return "X86ISD::PUNPCKHQDQ";
Dan Gohmand6708ea2009-08-15 01:38:56 +00009040 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Dan Gohman320afb82010-10-12 18:00:49 +00009041 case X86ISD::VAARG_64: return "X86ISD::VAARG_64";
Michael J. Spencere9c253e2010-10-21 01:41:01 +00009042 case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00009043 }
9044}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00009045
Chris Lattnerc9addb72007-03-30 23:15:24 +00009046// isLegalAddressingMode - Return true if the addressing mode represented
9047// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00009048bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00009049 const Type *Ty) const {
9050 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009051 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman92b651f2010-08-24 15:55:12 +00009052 Reloc::Model R = getTargetMachine().getRelocationModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00009053
Chris Lattnerc9addb72007-03-30 23:15:24 +00009054 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009055 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009056 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00009057
Chris Lattnerc9addb72007-03-30 23:15:24 +00009058 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00009059 unsigned GVFlags =
9060 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009061
Chris Lattnerdfed4132009-07-10 07:38:24 +00009062 // If a reference to this global requires an extra load, we can't fold it.
9063 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00009064 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009065
Chris Lattnerdfed4132009-07-10 07:38:24 +00009066 // If BaseGV requires a register for the PIC base, we cannot also have a
9067 // BaseReg specified.
9068 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00009069 return false;
Evan Cheng52787842007-08-01 23:46:47 +00009070
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009071 // If lower 4G is not available, then we must use rip-relative addressing.
Dan Gohman92b651f2010-08-24 15:55:12 +00009072 if ((M != CodeModel::Small || R != Reloc::Static) &&
9073 Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00009074 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00009075 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009076
Chris Lattnerc9addb72007-03-30 23:15:24 +00009077 switch (AM.Scale) {
9078 case 0:
9079 case 1:
9080 case 2:
9081 case 4:
9082 case 8:
9083 // These scales always work.
9084 break;
9085 case 3:
9086 case 5:
9087 case 9:
9088 // These scales are formed with basereg+scalereg. Only accept if there is
9089 // no basereg yet.
9090 if (AM.HasBaseReg)
9091 return false;
9092 break;
9093 default: // Other stuff never works.
9094 return false;
9095 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009096
Chris Lattnerc9addb72007-03-30 23:15:24 +00009097 return true;
9098}
9099
9100
Evan Cheng2bd122c2007-10-26 01:56:11 +00009101bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009102 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00009103 return false;
Evan Chenge127a732007-10-29 07:57:50 +00009104 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
9105 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009106 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00009107 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009108 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00009109}
9110
Owen Andersone50ed302009-08-10 22:56:29 +00009111bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00009112 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009113 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009114 unsigned NumBits1 = VT1.getSizeInBits();
9115 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00009116 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009117 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00009118 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00009119}
Evan Cheng2bd122c2007-10-26 01:56:11 +00009120
Dan Gohman97121ba2009-04-08 00:15:30 +00009121bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009122 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009123 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009124}
9125
Owen Andersone50ed302009-08-10 22:56:29 +00009126bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00009127 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00009128 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00009129}
9130
Owen Andersone50ed302009-08-10 22:56:29 +00009131bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00009132 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00009133 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00009134}
9135
Evan Cheng60c07e12006-07-05 22:17:51 +00009136/// isShuffleMaskLegal - Targets can use this to indicate that they only
9137/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
9138/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
9139/// are assumed to be legal.
9140bool
Eric Christopherfd179292009-08-27 18:07:15 +00009141X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00009142 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00009143 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00009144 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00009145 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00009146
Nate Begemana09008b2009-10-19 02:17:23 +00009147 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00009148 return (VT.getVectorNumElements() == 2 ||
9149 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
9150 isMOVLMask(M, VT) ||
9151 isSHUFPMask(M, VT) ||
9152 isPSHUFDMask(M, VT) ||
9153 isPSHUFHWMask(M, VT) ||
9154 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00009155 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00009156 isUNPCKLMask(M, VT) ||
9157 isUNPCKHMask(M, VT) ||
9158 isUNPCKL_v_undef_Mask(M, VT) ||
9159 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009160}
9161
Dan Gohman7d8143f2008-04-09 20:09:42 +00009162bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00009163X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00009164 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00009165 unsigned NumElts = VT.getVectorNumElements();
9166 // FIXME: This collection of masks seems suspect.
9167 if (NumElts == 2)
9168 return true;
9169 if (NumElts == 4 && VT.getSizeInBits() == 128) {
9170 return (isMOVLMask(Mask, VT) ||
9171 isCommutedMOVLMask(Mask, VT, true) ||
9172 isSHUFPMask(Mask, VT) ||
9173 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00009174 }
9175 return false;
9176}
9177
9178//===----------------------------------------------------------------------===//
9179// X86 Scheduler Hooks
9180//===----------------------------------------------------------------------===//
9181
Mon P Wang63307c32008-05-05 19:05:59 +00009182// private utility function
9183MachineBasicBlock *
9184X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
9185 MachineBasicBlock *MBB,
9186 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009187 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009188 unsigned LoadOpc,
9189 unsigned CXchgOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00009190 unsigned notOpc,
9191 unsigned EAXreg,
9192 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009193 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009194 // For the atomic bitwise operator, we generate
9195 // thisMBB:
9196 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009197 // ld t1 = [bitinstr.addr]
9198 // op t2 = t1, [bitinstr.val]
9199 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009200 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9201 // bz newMBB
9202 // fallthrough -->nextMBB
9203 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9204 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009205 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009206 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009207
Mon P Wang63307c32008-05-05 19:05:59 +00009208 /// First build the CFG
9209 MachineFunction *F = MBB->getParent();
9210 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009211 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9212 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9213 F->insert(MBBIter, newMBB);
9214 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009215
Dan Gohman14152b42010-07-06 20:24:04 +00009216 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9217 nextMBB->splice(nextMBB->begin(), thisMBB,
9218 llvm::next(MachineBasicBlock::iterator(bInstr)),
9219 thisMBB->end());
9220 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009221
Mon P Wang63307c32008-05-05 19:05:59 +00009222 // Update thisMBB to fall through to newMBB
9223 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009224
Mon P Wang63307c32008-05-05 19:05:59 +00009225 // newMBB jumps to itself and fall through to nextMBB
9226 newMBB->addSuccessor(nextMBB);
9227 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009228
Mon P Wang63307c32008-05-05 19:05:59 +00009229 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009230 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009231 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00009232 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009233 MachineOperand& destOper = bInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009234 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009235 int numArgs = bInstr->getNumOperands() - 1;
9236 for (int i=0; i < numArgs; ++i)
9237 argOpers[i] = &bInstr->getOperand(i+1);
9238
9239 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009240 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009241 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009242
Dale Johannesen140be2d2008-08-19 18:47:28 +00009243 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009244 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009245 for (int i=0; i <= lastAddrIndx; ++i)
9246 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009247
Dale Johannesen140be2d2008-08-19 18:47:28 +00009248 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009249 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00009250 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009251 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009252 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009253 tt = t1;
9254
Dale Johannesen140be2d2008-08-19 18:47:28 +00009255 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00009256 assert((argOpers[valArgIndx]->isReg() ||
9257 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009258 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00009259 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009260 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009261 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009262 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009263 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00009264 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00009265
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009266 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00009267 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009268
Dale Johannesene4d209d2009-02-03 20:21:25 +00009269 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00009270 for (int i=0; i <= lastAddrIndx; ++i)
9271 (*MIB).addOperand(*argOpers[i]);
9272 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00009273 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009274 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9275 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00009276
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009277 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00009278 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00009279
Mon P Wang63307c32008-05-05 19:05:59 +00009280 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009281 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009282
Dan Gohman14152b42010-07-06 20:24:04 +00009283 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009284 return nextMBB;
9285}
9286
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00009287// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00009288MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009289X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
9290 MachineBasicBlock *MBB,
9291 unsigned regOpcL,
9292 unsigned regOpcH,
9293 unsigned immOpcL,
9294 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009295 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009296 // For the atomic bitwise operator, we generate
9297 // thisMBB (instructions are in pairs, except cmpxchg8b)
9298 // ld t1,t2 = [bitinstr.addr]
9299 // newMBB:
9300 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
9301 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00009302 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009303 // mov ECX, EBX <- t5, t6
9304 // mov EAX, EDX <- t1, t2
9305 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
9306 // mov t3, t4 <- EAX, EDX
9307 // bz newMBB
9308 // result in out1, out2
9309 // fallthrough -->nextMBB
9310
9311 const TargetRegisterClass *RC = X86::GR32RegisterClass;
9312 const unsigned LoadOpc = X86::MOV32rm;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009313 const unsigned NotOpc = X86::NOT32r;
9314 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9315 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9316 MachineFunction::iterator MBBIter = MBB;
9317 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009318
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009319 /// First build the CFG
9320 MachineFunction *F = MBB->getParent();
9321 MachineBasicBlock *thisMBB = MBB;
9322 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9323 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9324 F->insert(MBBIter, newMBB);
9325 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009326
Dan Gohman14152b42010-07-06 20:24:04 +00009327 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9328 nextMBB->splice(nextMBB->begin(), thisMBB,
9329 llvm::next(MachineBasicBlock::iterator(bInstr)),
9330 thisMBB->end());
9331 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009332
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009333 // Update thisMBB to fall through to newMBB
9334 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009335
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009336 // newMBB jumps to itself and fall through to nextMBB
9337 newMBB->addSuccessor(nextMBB);
9338 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009339
Dale Johannesene4d209d2009-02-03 20:21:25 +00009340 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009341 // Insert instructions into newMBB based on incoming instruction
9342 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009343 assert(bInstr->getNumOperands() < X86::AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009344 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009345 MachineOperand& dest1Oper = bInstr->getOperand(0);
9346 MachineOperand& dest2Oper = bInstr->getOperand(1);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009347 MachineOperand* argOpers[2 + X86::AddrNumOperands];
9348 for (int i=0; i < 2 + X86::AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009349 argOpers[i] = &bInstr->getOperand(i+2);
9350
Dan Gohman71ea4e52010-05-14 21:01:44 +00009351 // We use some of the operands multiple times, so conservatively just
9352 // clear any kill flags that might be present.
9353 if (argOpers[i]->isReg() && argOpers[i]->isUse())
9354 argOpers[i]->setIsKill(false);
9355 }
9356
Evan Chengad5b52f2010-01-08 19:14:57 +00009357 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009358 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00009359
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009360 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009361 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009362 for (int i=0; i <= lastAddrIndx; ++i)
9363 (*MIB).addOperand(*argOpers[i]);
9364 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009365 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00009366 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00009367 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009368 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00009369 MachineOperand newOp3 = *(argOpers[3]);
9370 if (newOp3.isImm())
9371 newOp3.setImm(newOp3.getImm()+4);
9372 else
9373 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009374 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00009375 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009376
9377 // t3/4 are defined later, at the bottom of the loop
9378 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
9379 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009380 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009381 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009382 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009383 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
9384
Evan Cheng306b4ca2010-01-08 23:41:50 +00009385 // The subsequent operations should be using the destination registers of
9386 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00009387 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009388 t1 = F->getRegInfo().createVirtualRegister(RC);
9389 t2 = F->getRegInfo().createVirtualRegister(RC);
9390 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
9391 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009392 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00009393 t1 = dest1Oper.getReg();
9394 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009395 }
9396
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009397 int valArgIndx = lastAddrIndx + 1;
9398 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00009399 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009400 "invalid operand");
9401 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
9402 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009403 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009404 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009405 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009406 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00009407 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009408 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009409 (*MIB).addOperand(*argOpers[valArgIndx]);
9410 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009411 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009412 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00009413 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009414 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00009415 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009416 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009417 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00009418 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00009419 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009420 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009421
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009422 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009423 MIB.addReg(t1);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009424 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009425 MIB.addReg(t2);
9426
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009427 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009428 MIB.addReg(t5);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009429 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009430 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00009431
Dale Johannesene4d209d2009-02-03 20:21:25 +00009432 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009433 for (int i=0; i <= lastAddrIndx; ++i)
9434 (*MIB).addOperand(*argOpers[i]);
9435
9436 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009437 (*MIB).setMemRefs(bInstr->memoperands_begin(),
9438 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009439
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009440 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009441 MIB.addReg(X86::EAX);
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009442 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009443 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009444
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009445 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009446 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009447
Dan Gohman14152b42010-07-06 20:24:04 +00009448 bInstr->eraseFromParent(); // The pseudo instruction is gone now.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00009449 return nextMBB;
9450}
9451
9452// private utility function
9453MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00009454X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
9455 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00009456 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00009457 // For the atomic min/max operator, we generate
9458 // thisMBB:
9459 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00009460 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00009461 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00009462 // cmp t1, t2
9463 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00009464 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00009465 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
9466 // bz newMBB
9467 // fallthrough -->nextMBB
9468 //
9469 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9470 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009471 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00009472 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00009473
Mon P Wang63307c32008-05-05 19:05:59 +00009474 /// First build the CFG
9475 MachineFunction *F = MBB->getParent();
9476 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00009477 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
9478 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
9479 F->insert(MBBIter, newMBB);
9480 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009481
Dan Gohman14152b42010-07-06 20:24:04 +00009482 // Transfer the remainder of thisMBB and its successor edges to nextMBB.
9483 nextMBB->splice(nextMBB->begin(), thisMBB,
9484 llvm::next(MachineBasicBlock::iterator(mInstr)),
9485 thisMBB->end());
9486 nextMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009487
Mon P Wang63307c32008-05-05 19:05:59 +00009488 // Update thisMBB to fall through to newMBB
9489 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009490
Mon P Wang63307c32008-05-05 19:05:59 +00009491 // newMBB jumps to newMBB and fall through to nextMBB
9492 newMBB->addSuccessor(nextMBB);
9493 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00009494
Dale Johannesene4d209d2009-02-03 20:21:25 +00009495 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00009496 // Insert instructions into newMBB based on incoming instruction
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009497 assert(mInstr->getNumOperands() < X86::AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00009498 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00009499 MachineOperand& destOper = mInstr->getOperand(0);
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009500 MachineOperand* argOpers[2 + X86::AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00009501 int numArgs = mInstr->getNumOperands() - 1;
9502 for (int i=0; i < numArgs; ++i)
9503 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00009504
Mon P Wang63307c32008-05-05 19:05:59 +00009505 // x86 address has 4 operands: base, index, scale, and displacement
Chris Lattnerac0ed5d2010-07-08 22:41:28 +00009506 int lastAddrIndx = X86::AddrNumOperands - 1; // [0,3]
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00009507 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00009508
Mon P Wangab3e7472008-05-05 22:56:23 +00009509 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009510 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00009511 for (int i=0; i <= lastAddrIndx; ++i)
9512 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00009513
Mon P Wang63307c32008-05-05 19:05:59 +00009514 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00009515 assert((argOpers[valArgIndx]->isReg() ||
9516 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00009517 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00009518
9519 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00009520 if (argOpers[valArgIndx]->isReg())
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009521 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00009522 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00009523 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00009524 (*MIB).addOperand(*argOpers[valArgIndx]);
9525
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009526 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00009527 MIB.addReg(t1);
9528
Dale Johannesene4d209d2009-02-03 20:21:25 +00009529 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00009530 MIB.addReg(t1);
9531 MIB.addReg(t2);
9532
9533 // Generate movc
9534 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00009535 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00009536 MIB.addReg(t2);
9537 MIB.addReg(t1);
9538
9539 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00009540 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00009541 for (int i=0; i <= lastAddrIndx; ++i)
9542 (*MIB).addOperand(*argOpers[i]);
9543 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00009544 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00009545 (*MIB).setMemRefs(mInstr->memoperands_begin(),
9546 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00009547
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +00009548 MIB = BuildMI(newMBB, dl, TII->get(TargetOpcode::COPY), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00009549 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00009550
Mon P Wang63307c32008-05-05 19:05:59 +00009551 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009552 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00009553
Dan Gohman14152b42010-07-06 20:24:04 +00009554 mInstr->eraseFromParent(); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00009555 return nextMBB;
9556}
9557
Eric Christopherf83a5de2009-08-27 18:08:16 +00009558// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009559// or XMM0_V32I8 in AVX all of this code can be replaced with that
9560// in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009561MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00009562X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00009563 unsigned numArgs, bool memArg) const {
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009564 assert((Subtarget->hasSSE42() || Subtarget->hasAVX()) &&
9565 "Target must have SSE4.2 or AVX features enabled");
9566
Eric Christopherb120ab42009-08-18 22:50:32 +00009567 DebugLoc dl = MI->getDebugLoc();
9568 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Eric Christopherb120ab42009-08-18 22:50:32 +00009569 unsigned Opc;
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +00009570 if (!Subtarget->hasAVX()) {
9571 if (memArg)
9572 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
9573 else
9574 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
9575 } else {
9576 if (memArg)
9577 Opc = numArgs == 3 ? X86::VPCMPISTRM128rm : X86::VPCMPESTRM128rm;
9578 else
9579 Opc = numArgs == 3 ? X86::VPCMPISTRM128rr : X86::VPCMPESTRM128rr;
9580 }
Eric Christopherb120ab42009-08-18 22:50:32 +00009581
Eric Christopher41c902f2010-11-30 08:20:21 +00009582 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
Eric Christopherb120ab42009-08-18 22:50:32 +00009583 for (unsigned i = 0; i < numArgs; ++i) {
9584 MachineOperand &Op = MI->getOperand(i+1);
Eric Christopherb120ab42009-08-18 22:50:32 +00009585 if (!(Op.isReg() && Op.isImplicit()))
9586 MIB.addOperand(Op);
9587 }
Eric Christopher41c902f2010-11-30 08:20:21 +00009588 BuildMI(*BB, MI, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
Eric Christopherb120ab42009-08-18 22:50:32 +00009589 .addReg(X86::XMM0);
9590
Dan Gohman14152b42010-07-06 20:24:04 +00009591 MI->eraseFromParent();
Eric Christopherb120ab42009-08-18 22:50:32 +00009592 return BB;
9593}
9594
9595MachineBasicBlock *
Eric Christopher228232b2010-11-30 07:20:12 +00009596X86TargetLowering::EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009597 DebugLoc dl = MI->getDebugLoc();
9598 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009599
Eric Christopher228232b2010-11-30 07:20:12 +00009600 // Address into RAX/EAX, other two args into ECX, EDX.
9601 unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
9602 unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
9603 MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
9604 for (int i = 0; i < X86::AddrNumOperands; ++i)
Eric Christopher82be2202010-11-30 08:10:28 +00009605 MIB.addOperand(MI->getOperand(i));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009606
Eric Christopher228232b2010-11-30 07:20:12 +00009607 unsigned ValOps = X86::AddrNumOperands;
9608 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9609 .addReg(MI->getOperand(ValOps).getReg());
9610 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
9611 .addReg(MI->getOperand(ValOps+1).getReg());
9612
9613 // The instruction doesn't actually take any operands though.
9614 BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009615
Eric Christopher228232b2010-11-30 07:20:12 +00009616 MI->eraseFromParent(); // The pseudo is gone now.
9617 return BB;
9618}
9619
9620MachineBasicBlock *
9621X86TargetLowering::EmitMwait(MachineInstr *MI, MachineBasicBlock *BB) const {
Eric Christopher228232b2010-11-30 07:20:12 +00009622 DebugLoc dl = MI->getDebugLoc();
9623 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009624
Eric Christopher228232b2010-11-30 07:20:12 +00009625 // First arg in ECX, the second in EAX.
9626 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
9627 .addReg(MI->getOperand(0).getReg());
9628 BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EAX)
9629 .addReg(MI->getOperand(1).getReg());
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009630
Eric Christopher228232b2010-11-30 07:20:12 +00009631 // The instruction doesn't actually take any operands though.
9632 BuildMI(*BB, MI, dl, TII->get(X86::MWAITrr));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +00009633
Eric Christopher228232b2010-11-30 07:20:12 +00009634 MI->eraseFromParent(); // The pseudo is gone now.
9635 return BB;
9636}
9637
9638MachineBasicBlock *
Dan Gohman320afb82010-10-12 18:00:49 +00009639X86TargetLowering::EmitVAARG64WithCustomInserter(
9640 MachineInstr *MI,
9641 MachineBasicBlock *MBB) const {
9642 // Emit va_arg instruction on X86-64.
9643
9644 // Operands to this pseudo-instruction:
9645 // 0 ) Output : destination address (reg)
9646 // 1-5) Input : va_list address (addr, i64mem)
9647 // 6 ) ArgSize : Size (in bytes) of vararg type
9648 // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset
9649 // 8 ) Align : Alignment of type
9650 // 9 ) EFLAGS (implicit-def)
9651
9652 assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
9653 assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
9654
9655 unsigned DestReg = MI->getOperand(0).getReg();
9656 MachineOperand &Base = MI->getOperand(1);
9657 MachineOperand &Scale = MI->getOperand(2);
9658 MachineOperand &Index = MI->getOperand(3);
9659 MachineOperand &Disp = MI->getOperand(4);
9660 MachineOperand &Segment = MI->getOperand(5);
9661 unsigned ArgSize = MI->getOperand(6).getImm();
9662 unsigned ArgMode = MI->getOperand(7).getImm();
9663 unsigned Align = MI->getOperand(8).getImm();
9664
9665 // Memory Reference
9666 assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
9667 MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
9668 MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
9669
9670 // Machine Information
9671 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9672 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
9673 const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
9674 const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
9675 DebugLoc DL = MI->getDebugLoc();
9676
9677 // struct va_list {
9678 // i32 gp_offset
9679 // i32 fp_offset
9680 // i64 overflow_area (address)
9681 // i64 reg_save_area (address)
9682 // }
9683 // sizeof(va_list) = 24
9684 // alignment(va_list) = 8
9685
9686 unsigned TotalNumIntRegs = 6;
9687 unsigned TotalNumXMMRegs = 8;
9688 bool UseGPOffset = (ArgMode == 1);
9689 bool UseFPOffset = (ArgMode == 2);
9690 unsigned MaxOffset = TotalNumIntRegs * 8 +
9691 (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
9692
9693 /* Align ArgSize to a multiple of 8 */
9694 unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
9695 bool NeedsAlign = (Align > 8);
9696
9697 MachineBasicBlock *thisMBB = MBB;
9698 MachineBasicBlock *overflowMBB;
9699 MachineBasicBlock *offsetMBB;
9700 MachineBasicBlock *endMBB;
9701
9702 unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB
9703 unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB
9704 unsigned OffsetReg = 0;
9705
9706 if (!UseGPOffset && !UseFPOffset) {
9707 // If we only pull from the overflow region, we don't create a branch.
9708 // We don't need to alter control flow.
9709 OffsetDestReg = 0; // unused
9710 OverflowDestReg = DestReg;
9711
9712 offsetMBB = NULL;
9713 overflowMBB = thisMBB;
9714 endMBB = thisMBB;
9715 } else {
9716 // First emit code to check if gp_offset (or fp_offset) is below the bound.
9717 // If so, pull the argument from reg_save_area. (branch to offsetMBB)
9718 // If not, pull from overflow_area. (branch to overflowMBB)
9719 //
9720 // thisMBB
9721 // | .
9722 // | .
9723 // offsetMBB overflowMBB
9724 // | .
9725 // | .
9726 // endMBB
9727
9728 // Registers for the PHI in endMBB
9729 OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
9730 OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
9731
9732 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9733 MachineFunction *MF = MBB->getParent();
9734 overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9735 offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9736 endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
9737
9738 MachineFunction::iterator MBBIter = MBB;
9739 ++MBBIter;
9740
9741 // Insert the new basic blocks
9742 MF->insert(MBBIter, offsetMBB);
9743 MF->insert(MBBIter, overflowMBB);
9744 MF->insert(MBBIter, endMBB);
9745
9746 // Transfer the remainder of MBB and its successor edges to endMBB.
9747 endMBB->splice(endMBB->begin(), thisMBB,
9748 llvm::next(MachineBasicBlock::iterator(MI)),
9749 thisMBB->end());
9750 endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
9751
9752 // Make offsetMBB and overflowMBB successors of thisMBB
9753 thisMBB->addSuccessor(offsetMBB);
9754 thisMBB->addSuccessor(overflowMBB);
9755
9756 // endMBB is a successor of both offsetMBB and overflowMBB
9757 offsetMBB->addSuccessor(endMBB);
9758 overflowMBB->addSuccessor(endMBB);
9759
9760 // Load the offset value into a register
9761 OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9762 BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
9763 .addOperand(Base)
9764 .addOperand(Scale)
9765 .addOperand(Index)
9766 .addDisp(Disp, UseFPOffset ? 4 : 0)
9767 .addOperand(Segment)
9768 .setMemRefs(MMOBegin, MMOEnd);
9769
9770 // Check if there is enough room left to pull this argument.
9771 BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
9772 .addReg(OffsetReg)
9773 .addImm(MaxOffset + 8 - ArgSizeA8);
9774
9775 // Branch to "overflowMBB" if offset >= max
9776 // Fall through to "offsetMBB" otherwise
9777 BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
9778 .addMBB(overflowMBB);
9779 }
9780
9781 // In offsetMBB, emit code to use the reg_save_area.
9782 if (offsetMBB) {
9783 assert(OffsetReg != 0);
9784
9785 // Read the reg_save_area address.
9786 unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
9787 BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
9788 .addOperand(Base)
9789 .addOperand(Scale)
9790 .addOperand(Index)
9791 .addDisp(Disp, 16)
9792 .addOperand(Segment)
9793 .setMemRefs(MMOBegin, MMOEnd);
9794
9795 // Zero-extend the offset
9796 unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
9797 BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
9798 .addImm(0)
9799 .addReg(OffsetReg)
9800 .addImm(X86::sub_32bit);
9801
9802 // Add the offset to the reg_save_area to get the final address.
9803 BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
9804 .addReg(OffsetReg64)
9805 .addReg(RegSaveReg);
9806
9807 // Compute the offset for the next argument
9808 unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
9809 BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
9810 .addReg(OffsetReg)
9811 .addImm(UseFPOffset ? 16 : 8);
9812
9813 // Store it back into the va_list.
9814 BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
9815 .addOperand(Base)
9816 .addOperand(Scale)
9817 .addOperand(Index)
9818 .addDisp(Disp, UseFPOffset ? 4 : 0)
9819 .addOperand(Segment)
9820 .addReg(NextOffsetReg)
9821 .setMemRefs(MMOBegin, MMOEnd);
9822
9823 // Jump to endMBB
9824 BuildMI(offsetMBB, DL, TII->get(X86::JMP_4))
9825 .addMBB(endMBB);
9826 }
9827
9828 //
9829 // Emit code to use overflow area
9830 //
9831
9832 // Load the overflow_area address into a register.
9833 unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
9834 BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
9835 .addOperand(Base)
9836 .addOperand(Scale)
9837 .addOperand(Index)
9838 .addDisp(Disp, 8)
9839 .addOperand(Segment)
9840 .setMemRefs(MMOBegin, MMOEnd);
9841
9842 // If we need to align it, do so. Otherwise, just copy the address
9843 // to OverflowDestReg.
9844 if (NeedsAlign) {
9845 // Align the overflow address
9846 assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
9847 unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
9848
9849 // aligned_addr = (addr + (align-1)) & ~(align-1)
9850 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
9851 .addReg(OverflowAddrReg)
9852 .addImm(Align-1);
9853
9854 BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
9855 .addReg(TmpReg)
9856 .addImm(~(uint64_t)(Align-1));
9857 } else {
9858 BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
9859 .addReg(OverflowAddrReg);
9860 }
9861
9862 // Compute the next overflow address after this argument.
9863 // (the overflow address should be kept 8-byte aligned)
9864 unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
9865 BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
9866 .addReg(OverflowDestReg)
9867 .addImm(ArgSizeA8);
9868
9869 // Store the new overflow address.
9870 BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
9871 .addOperand(Base)
9872 .addOperand(Scale)
9873 .addOperand(Index)
9874 .addDisp(Disp, 8)
9875 .addOperand(Segment)
9876 .addReg(NextAddrReg)
9877 .setMemRefs(MMOBegin, MMOEnd);
9878
9879 // If we branched, emit the PHI to the front of endMBB.
9880 if (offsetMBB) {
9881 BuildMI(*endMBB, endMBB->begin(), DL,
9882 TII->get(X86::PHI), DestReg)
9883 .addReg(OffsetDestReg).addMBB(offsetMBB)
9884 .addReg(OverflowDestReg).addMBB(overflowMBB);
9885 }
9886
9887 // Erase the pseudo instruction
9888 MI->eraseFromParent();
9889
9890 return endMBB;
9891}
9892
9893MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00009894X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
9895 MachineInstr *MI,
9896 MachineBasicBlock *MBB) const {
9897 // Emit code to save XMM registers to the stack. The ABI says that the
9898 // number of registers to save is given in %al, so it's theoretically
9899 // possible to do an indirect jump trick to avoid saving all of them,
9900 // however this code takes a simpler approach and just executes all
9901 // of the stores if %al is non-zero. It's less code, and it's probably
9902 // easier on the hardware branch predictor, and stores aren't all that
9903 // expensive anyway.
9904
9905 // Create the new basic blocks. One block contains all the XMM stores,
9906 // and one block is the final destination regardless of whether any
9907 // stores were performed.
9908 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
9909 MachineFunction *F = MBB->getParent();
9910 MachineFunction::iterator MBBIter = MBB;
9911 ++MBBIter;
9912 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
9913 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
9914 F->insert(MBBIter, XMMSaveMBB);
9915 F->insert(MBBIter, EndMBB);
9916
Dan Gohman14152b42010-07-06 20:24:04 +00009917 // Transfer the remainder of MBB and its successor edges to EndMBB.
9918 EndMBB->splice(EndMBB->begin(), MBB,
9919 llvm::next(MachineBasicBlock::iterator(MI)),
9920 MBB->end());
9921 EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
9922
Dan Gohmand6708ea2009-08-15 01:38:56 +00009923 // The original block will now fall through to the XMM save block.
9924 MBB->addSuccessor(XMMSaveMBB);
9925 // The XMMSaveMBB will fall through to the end block.
9926 XMMSaveMBB->addSuccessor(EndMBB);
9927
9928 // Now add the instructions.
9929 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9930 DebugLoc DL = MI->getDebugLoc();
9931
9932 unsigned CountReg = MI->getOperand(0).getReg();
9933 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
9934 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
9935
9936 if (!Subtarget->isTargetWin64()) {
9937 // If %al is 0, branch around the XMM save block.
9938 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00009939 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009940 MBB->addSuccessor(EndMBB);
9941 }
9942
9943 // In the XMM save block, save all the XMM argument registers.
9944 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
9945 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00009946 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00009947 F->getMachineMemOperand(
Chris Lattnere8639032010-09-21 06:22:23 +00009948 MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset),
Chris Lattner59db5492010-09-21 04:39:43 +00009949 MachineMemOperand::MOStore,
Evan Chengff89dcb2009-10-18 18:16:27 +00009950 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009951 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
9952 .addFrameIndex(RegSaveFrameIndex)
9953 .addImm(/*Scale=*/1)
9954 .addReg(/*IndexReg=*/0)
9955 .addImm(/*Disp=*/Offset)
9956 .addReg(/*Segment=*/0)
9957 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00009958 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00009959 }
9960
Dan Gohman14152b42010-07-06 20:24:04 +00009961 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohmand6708ea2009-08-15 01:38:56 +00009962
9963 return EndMBB;
9964}
Mon P Wang63307c32008-05-05 19:05:59 +00009965
Evan Cheng60c07e12006-07-05 22:17:51 +00009966MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00009967X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00009968 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00009969 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
9970 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00009971
Chris Lattner52600972009-09-02 05:57:00 +00009972 // To "insert" a SELECT_CC instruction, we actually have to insert the
9973 // diamond control-flow pattern. The incoming instruction knows the
9974 // destination vreg to set, the condition code register to branch on, the
9975 // true/false values to select between, and a branch opcode to use.
9976 const BasicBlock *LLVM_BB = BB->getBasicBlock();
9977 MachineFunction::iterator It = BB;
9978 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00009979
Chris Lattner52600972009-09-02 05:57:00 +00009980 // thisMBB:
9981 // ...
9982 // TrueVal = ...
9983 // cmpTY ccX, r1, r2
9984 // bCC copy1MBB
9985 // fallthrough --> copy0MBB
9986 MachineBasicBlock *thisMBB = BB;
9987 MachineFunction *F = BB->getParent();
9988 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
9989 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Chris Lattner52600972009-09-02 05:57:00 +00009990 F->insert(It, copy0MBB);
9991 F->insert(It, sinkMBB);
Bill Wendling730c07e2010-06-25 20:48:10 +00009992
Bill Wendling730c07e2010-06-25 20:48:10 +00009993 // If the EFLAGS register isn't dead in the terminator, then claim that it's
9994 // live into the sink and copy blocks.
9995 const MachineFunction *MF = BB->getParent();
9996 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo();
9997 BitVector ReservedRegs = TRI->getReservedRegs(*MF);
Bill Wendling730c07e2010-06-25 20:48:10 +00009998
Dan Gohman14152b42010-07-06 20:24:04 +00009999 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
10000 const MachineOperand &MO = MI->getOperand(I);
10001 if (!MO.isReg() || !MO.isUse() || MO.isKill()) continue;
Bill Wendling730c07e2010-06-25 20:48:10 +000010002 unsigned Reg = MO.getReg();
10003 if (Reg != X86::EFLAGS) continue;
10004 copy0MBB->addLiveIn(Reg);
10005 sinkMBB->addLiveIn(Reg);
10006 }
10007
Dan Gohman14152b42010-07-06 20:24:04 +000010008 // Transfer the remainder of BB and its successor edges to sinkMBB.
10009 sinkMBB->splice(sinkMBB->begin(), BB,
10010 llvm::next(MachineBasicBlock::iterator(MI)),
10011 BB->end());
10012 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
10013
10014 // Add the true and fallthrough blocks as its successors.
10015 BB->addSuccessor(copy0MBB);
10016 BB->addSuccessor(sinkMBB);
10017
10018 // Create the conditional branch instruction.
10019 unsigned Opc =
10020 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
10021 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
10022
Chris Lattner52600972009-09-02 05:57:00 +000010023 // copy0MBB:
10024 // %FalseValue = ...
10025 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +000010026 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +000010027
Chris Lattner52600972009-09-02 05:57:00 +000010028 // sinkMBB:
10029 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
10030 // ...
Dan Gohman14152b42010-07-06 20:24:04 +000010031 BuildMI(*sinkMBB, sinkMBB->begin(), DL,
10032 TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +000010033 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
10034 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
10035
Dan Gohman14152b42010-07-06 20:24:04 +000010036 MI->eraseFromParent(); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +000010037 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +000010038}
10039
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010040MachineBasicBlock *
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010041X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010042 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010043 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10044 DebugLoc DL = MI->getDebugLoc();
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010045
10046 // The lowering is pretty easy: we're just emitting the call to _alloca. The
10047 // non-trivial part is impdef of ESP.
10048 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
10049 // mingw-w64.
10050
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010051 const char *StackProbeSymbol =
10052 Subtarget->isTargetWindows() ? "_chkstk" : "_alloca";
10053
Dan Gohman14152b42010-07-06 20:24:04 +000010054 BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32))
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010055 .addExternalSymbol(StackProbeSymbol)
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010056 .addReg(X86::EAX, RegState::Implicit)
10057 .addReg(X86::ESP, RegState::Implicit)
10058 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
Anton Korobeynikov9f7f83b2010-08-25 07:50:11 +000010059 .addReg(X86::ESP, RegState::Define | RegState::Implicit)
10060 .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit);
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010061
Dan Gohman14152b42010-07-06 20:24:04 +000010062 MI->eraseFromParent(); // The pseudo instruction is gone now.
Anton Korobeynikov043f3c22010-03-06 19:32:29 +000010063 return BB;
10064}
Chris Lattner52600972009-09-02 05:57:00 +000010065
10066MachineBasicBlock *
Eric Christopher30ef0e52010-06-03 04:07:48 +000010067X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
10068 MachineBasicBlock *BB) const {
10069 // This is pretty easy. We're taking the value that we received from
10070 // our load from the relocation, sticking it in either RDI (x86-64)
10071 // or EAX and doing an indirect call. The return value will then
10072 // be in the normal return register.
Michael J. Spencerec38de22010-10-10 22:04:20 +000010073 const X86InstrInfo *TII
Eric Christopher54415362010-06-08 22:04:25 +000010074 = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo());
Eric Christopher30ef0e52010-06-03 04:07:48 +000010075 DebugLoc DL = MI->getDebugLoc();
10076 MachineFunction *F = BB->getParent();
Eric Christopher722d3152010-09-27 06:01:51 +000010077
10078 assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
Eric Christopher54415362010-06-08 22:04:25 +000010079 assert(MI->getOperand(3).isGlobal() && "This should be a global");
Michael J. Spencerec38de22010-10-10 22:04:20 +000010080
Eric Christopher30ef0e52010-06-03 04:07:48 +000010081 if (Subtarget->is64Bit()) {
Dan Gohman14152b42010-07-06 20:24:04 +000010082 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10083 TII->get(X86::MOV64rm), X86::RDI)
Eric Christopher54415362010-06-08 22:04:25 +000010084 .addReg(X86::RIP)
10085 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010086 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010087 MI->getOperand(3).getTargetFlags())
10088 .addReg(0);
Eric Christopher722d3152010-09-27 06:01:51 +000010089 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
Chris Lattner599b5312010-07-08 23:46:44 +000010090 addDirectMem(MIB, X86::RDI);
Eric Christopher61025492010-06-15 23:08:42 +000010091 } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
Dan Gohman14152b42010-07-06 20:24:04 +000010092 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10093 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher61025492010-06-15 23:08:42 +000010094 .addReg(0)
10095 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010096 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher61025492010-06-15 23:08:42 +000010097 MI->getOperand(3).getTargetFlags())
10098 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010099 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010100 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010101 } else {
Dan Gohman14152b42010-07-06 20:24:04 +000010102 MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
10103 TII->get(X86::MOV32rm), X86::EAX)
Eric Christopher54415362010-06-08 22:04:25 +000010104 .addReg(TII->getGlobalBaseReg(F))
10105 .addImm(0).addReg(0)
Michael J. Spencerec38de22010-10-10 22:04:20 +000010106 .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
Eric Christopher54415362010-06-08 22:04:25 +000010107 MI->getOperand(3).getTargetFlags())
10108 .addReg(0);
Dan Gohman14152b42010-07-06 20:24:04 +000010109 MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
Chris Lattner599b5312010-07-08 23:46:44 +000010110 addDirectMem(MIB, X86::EAX);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010111 }
Michael J. Spencerec38de22010-10-10 22:04:20 +000010112
Dan Gohman14152b42010-07-06 20:24:04 +000010113 MI->eraseFromParent(); // The pseudo instruction is gone now.
Eric Christopher30ef0e52010-06-03 04:07:48 +000010114 return BB;
10115}
10116
10117MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +000010118X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010119 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +000010120 switch (MI->getOpcode()) {
10121 default: assert(false && "Unexpected instr type to insert");
NAKAMURA Takumi7754f852011-01-26 02:04:09 +000010122 case X86::TAILJMPd64:
10123 case X86::TAILJMPr64:
10124 case X86::TAILJMPm64:
10125 assert(!"TAILJMP64 would not be touched here.");
10126 case X86::TCRETURNdi64:
10127 case X86::TCRETURNri64:
10128 case X86::TCRETURNmi64:
10129 // Defs of TCRETURNxx64 has Win64's callee-saved registers, as subset.
10130 // On AMD64, additional defs should be added before register allocation.
10131 if (!Subtarget->isTargetWin64()) {
10132 MI->addRegisterDefined(X86::RSI);
10133 MI->addRegisterDefined(X86::RDI);
10134 MI->addRegisterDefined(X86::XMM6);
10135 MI->addRegisterDefined(X86::XMM7);
10136 MI->addRegisterDefined(X86::XMM8);
10137 MI->addRegisterDefined(X86::XMM9);
10138 MI->addRegisterDefined(X86::XMM10);
10139 MI->addRegisterDefined(X86::XMM11);
10140 MI->addRegisterDefined(X86::XMM12);
10141 MI->addRegisterDefined(X86::XMM13);
10142 MI->addRegisterDefined(X86::XMM14);
10143 MI->addRegisterDefined(X86::XMM15);
10144 }
10145 return BB;
Michael J. Spencere9c253e2010-10-21 01:41:01 +000010146 case X86::WIN_ALLOCA:
10147 return EmitLoweredWinAlloca(MI, BB);
Eric Christopher30ef0e52010-06-03 04:07:48 +000010148 case X86::TLSCall_32:
10149 case X86::TLSCall_64:
10150 return EmitLoweredTLSCall(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +000010151 case X86::CMOV_GR8:
Evan Cheng60c07e12006-07-05 22:17:51 +000010152 case X86::CMOV_FR32:
10153 case X86::CMOV_FR64:
10154 case X86::CMOV_V4F32:
10155 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +000010156 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +000010157 case X86::CMOV_GR16:
10158 case X86::CMOV_GR32:
10159 case X86::CMOV_RFP32:
10160 case X86::CMOV_RFP64:
10161 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +000010162 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010163
Dale Johannesen849f2142007-07-03 00:53:03 +000010164 case X86::FP32_TO_INT16_IN_MEM:
10165 case X86::FP32_TO_INT32_IN_MEM:
10166 case X86::FP32_TO_INT64_IN_MEM:
10167 case X86::FP64_TO_INT16_IN_MEM:
10168 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +000010169 case X86::FP64_TO_INT64_IN_MEM:
10170 case X86::FP80_TO_INT16_IN_MEM:
10171 case X86::FP80_TO_INT32_IN_MEM:
10172 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +000010173 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
10174 DebugLoc DL = MI->getDebugLoc();
10175
Evan Cheng60c07e12006-07-05 22:17:51 +000010176 // Change the floating point control register to use "round towards zero"
10177 // mode when truncating to an integer value.
10178 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +000010179 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Dan Gohman14152b42010-07-06 20:24:04 +000010180 addFrameReference(BuildMI(*BB, MI, DL,
10181 TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010182
10183 // Load the old value of the high byte of the control word...
10184 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +000010185 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Dan Gohman14152b42010-07-06 20:24:04 +000010186 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +000010187 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010188
10189 // Set the high part to be round to zero...
Dan Gohman14152b42010-07-06 20:24:04 +000010190 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010191 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +000010192
10193 // Reload the modified control word now...
Dan Gohman14152b42010-07-06 20:24:04 +000010194 addFrameReference(BuildMI(*BB, MI, DL,
10195 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010196
10197 // Restore the memory image of control word to original value
Dan Gohman14152b42010-07-06 20:24:04 +000010198 addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +000010199 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +000010200
10201 // Get the X86 opcode to use.
10202 unsigned Opc;
10203 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +000010204 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +000010205 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
10206 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
10207 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
10208 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
10209 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
10210 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +000010211 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
10212 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
10213 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +000010214 }
10215
10216 X86AddressMode AM;
10217 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +000010218 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010219 AM.BaseType = X86AddressMode::RegBase;
10220 AM.Base.Reg = Op.getReg();
10221 } else {
10222 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +000010223 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +000010224 }
10225 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +000010226 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010227 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010228 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +000010229 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +000010230 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010231 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +000010232 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +000010233 AM.GV = Op.getGlobal();
10234 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +000010235 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +000010236 }
Dan Gohman14152b42010-07-06 20:24:04 +000010237 addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
Chris Lattnerac0ed5d2010-07-08 22:41:28 +000010238 .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +000010239
10240 // Reload the original control word now.
Dan Gohman14152b42010-07-06 20:24:04 +000010241 addFrameReference(BuildMI(*BB, MI, DL,
10242 TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +000010243
Dan Gohman14152b42010-07-06 20:24:04 +000010244 MI->eraseFromParent(); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +000010245 return BB;
10246 }
Eric Christopherb120ab42009-08-18 22:50:32 +000010247 // String/text processing lowering.
10248 case X86::PCMPISTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010249 case X86::VPCMPISTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010250 return EmitPCMP(MI, BB, 3, false /* in-mem */);
10251 case X86::PCMPISTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010252 case X86::VPCMPISTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010253 return EmitPCMP(MI, BB, 3, true /* in-mem */);
10254 case X86::PCMPESTRM128REG:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010255 case X86::VPCMPESTRM128REG:
Eric Christopherb120ab42009-08-18 22:50:32 +000010256 return EmitPCMP(MI, BB, 5, false /* in mem */);
10257 case X86::PCMPESTRM128MEM:
Bruno Cardoso Lopes98f98562010-07-30 19:54:33 +000010258 case X86::VPCMPESTRM128MEM:
Eric Christopherb120ab42009-08-18 22:50:32 +000010259 return EmitPCMP(MI, BB, 5, true /* in mem */);
10260
Eric Christopher228232b2010-11-30 07:20:12 +000010261 // Thread synchronization.
10262 case X86::MONITOR:
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000010263 return EmitMonitor(MI, BB);
Eric Christopher228232b2010-11-30 07:20:12 +000010264 case X86::MWAIT:
10265 return EmitMwait(MI, BB);
10266
Eric Christopherb120ab42009-08-18 22:50:32 +000010267 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +000010268 case X86::ATOMAND32:
10269 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010270 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010271 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010272 X86::NOT32r, X86::EAX,
10273 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010274 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +000010275 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
10276 X86::OR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010277 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010278 X86::NOT32r, X86::EAX,
10279 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +000010280 case X86::ATOMXOR32:
10281 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010282 X86::XOR32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010283 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010284 X86::NOT32r, X86::EAX,
10285 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +000010286 case X86::ATOMNAND32:
10287 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010288 X86::AND32ri, X86::MOV32rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010289 X86::LCMPXCHG32,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010290 X86::NOT32r, X86::EAX,
10291 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +000010292 case X86::ATOMMIN32:
10293 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
10294 case X86::ATOMMAX32:
10295 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
10296 case X86::ATOMUMIN32:
10297 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
10298 case X86::ATOMUMAX32:
10299 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +000010300
10301 case X86::ATOMAND16:
10302 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10303 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010304 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010305 X86::NOT16r, X86::AX,
10306 X86::GR16RegisterClass);
10307 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +000010308 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010309 X86::OR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010310 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010311 X86::NOT16r, X86::AX,
10312 X86::GR16RegisterClass);
10313 case X86::ATOMXOR16:
10314 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
10315 X86::XOR16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010316 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010317 X86::NOT16r, X86::AX,
10318 X86::GR16RegisterClass);
10319 case X86::ATOMNAND16:
10320 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
10321 X86::AND16ri, X86::MOV16rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010322 X86::LCMPXCHG16,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010323 X86::NOT16r, X86::AX,
10324 X86::GR16RegisterClass, true);
10325 case X86::ATOMMIN16:
10326 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
10327 case X86::ATOMMAX16:
10328 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
10329 case X86::ATOMUMIN16:
10330 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
10331 case X86::ATOMUMAX16:
10332 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
10333
10334 case X86::ATOMAND8:
10335 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10336 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010337 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010338 X86::NOT8r, X86::AL,
10339 X86::GR8RegisterClass);
10340 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +000010341 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010342 X86::OR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010343 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010344 X86::NOT8r, X86::AL,
10345 X86::GR8RegisterClass);
10346 case X86::ATOMXOR8:
10347 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
10348 X86::XOR8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010349 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010350 X86::NOT8r, X86::AL,
10351 X86::GR8RegisterClass);
10352 case X86::ATOMNAND8:
10353 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
10354 X86::AND8ri, X86::MOV8rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010355 X86::LCMPXCHG8,
Dale Johannesen140be2d2008-08-19 18:47:28 +000010356 X86::NOT8r, X86::AL,
10357 X86::GR8RegisterClass, true);
10358 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010359 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +000010360 case X86::ATOMAND64:
10361 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010362 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010363 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010364 X86::NOT64r, X86::RAX,
10365 X86::GR64RegisterClass);
10366 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +000010367 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
10368 X86::OR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010369 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010370 X86::NOT64r, X86::RAX,
10371 X86::GR64RegisterClass);
10372 case X86::ATOMXOR64:
10373 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +000010374 X86::XOR64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010375 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010376 X86::NOT64r, X86::RAX,
10377 X86::GR64RegisterClass);
10378 case X86::ATOMNAND64:
10379 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
10380 X86::AND64ri32, X86::MOV64rm,
Jakob Stoklund Olesenb5378ea2010-07-14 23:50:27 +000010381 X86::LCMPXCHG64,
Dale Johannesena99e3842008-08-20 00:48:50 +000010382 X86::NOT64r, X86::RAX,
10383 X86::GR64RegisterClass, true);
10384 case X86::ATOMMIN64:
10385 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
10386 case X86::ATOMMAX64:
10387 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
10388 case X86::ATOMUMIN64:
10389 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
10390 case X86::ATOMUMAX64:
10391 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010392
10393 // This group does 64-bit operations on a 32-bit host.
10394 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010395 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010396 X86::AND32rr, X86::AND32rr,
10397 X86::AND32ri, X86::AND32ri,
10398 false);
10399 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010400 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010401 X86::OR32rr, X86::OR32rr,
10402 X86::OR32ri, X86::OR32ri,
10403 false);
10404 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010405 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010406 X86::XOR32rr, X86::XOR32rr,
10407 X86::XOR32ri, X86::XOR32ri,
10408 false);
10409 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010410 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010411 X86::AND32rr, X86::AND32rr,
10412 X86::AND32ri, X86::AND32ri,
10413 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010414 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010415 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010416 X86::ADD32rr, X86::ADC32rr,
10417 X86::ADD32ri, X86::ADC32ri,
10418 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010419 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010420 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +000010421 X86::SUB32rr, X86::SBB32rr,
10422 X86::SUB32ri, X86::SBB32ri,
10423 false);
Dale Johannesen880ae362008-10-03 22:25:52 +000010424 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +000010425 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +000010426 X86::MOV32rr, X86::MOV32rr,
10427 X86::MOV32ri, X86::MOV32ri,
10428 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +000010429 case X86::VASTART_SAVE_XMM_REGS:
10430 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Dan Gohman320afb82010-10-12 18:00:49 +000010431
10432 case X86::VAARG_64:
10433 return EmitVAARG64WithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +000010434 }
10435}
10436
10437//===----------------------------------------------------------------------===//
10438// X86 Optimization Hooks
10439//===----------------------------------------------------------------------===//
10440
Dan Gohman475871a2008-07-27 21:46:04 +000010441void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +000010442 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010443 APInt &KnownZero,
10444 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +000010445 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +000010446 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010447 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +000010448 assert((Opc >= ISD::BUILTIN_OP_END ||
10449 Opc == ISD::INTRINSIC_WO_CHAIN ||
10450 Opc == ISD::INTRINSIC_W_CHAIN ||
10451 Opc == ISD::INTRINSIC_VOID) &&
10452 "Should use MaskedValueIsZero if you don't know whether Op"
10453 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010454
Dan Gohmanf4f92f52008-02-13 23:07:24 +000010455 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010456 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +000010457 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010458 case X86ISD::ADD:
10459 case X86ISD::SUB:
Chris Lattner5b856542010-12-20 00:59:46 +000010460 case X86ISD::ADC:
10461 case X86ISD::SBB:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010462 case X86ISD::SMUL:
10463 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +000010464 case X86ISD::INC:
10465 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +000010466 case X86ISD::OR:
10467 case X86ISD::XOR:
10468 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +000010469 // These nodes' second result is a boolean.
10470 if (Op.getResNo() == 0)
10471 break;
10472 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010473 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +000010474 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
10475 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +000010476 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010477 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +000010478}
Chris Lattner259e97c2006-01-31 19:43:35 +000010479
Owen Andersonbc146b02010-09-21 20:42:50 +000010480unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op,
10481 unsigned Depth) const {
10482 // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
10483 if (Op.getOpcode() == X86ISD::SETCC_CARRY)
10484 return Op.getValueType().getScalarType().getSizeInBits();
Michael J. Spencerec38de22010-10-10 22:04:20 +000010485
Owen Andersonbc146b02010-09-21 20:42:50 +000010486 // Fallback case.
10487 return 1;
10488}
10489
Evan Cheng206ee9d2006-07-07 08:33:52 +000010490/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +000010491/// node is a GlobalAddress + offset.
10492bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +000010493 const GlobalValue* &GA,
10494 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +000010495 if (N->getOpcode() == X86ISD::Wrapper) {
10496 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +000010497 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +000010498 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +000010499 return true;
10500 }
Evan Cheng206ee9d2006-07-07 08:33:52 +000010501 }
Evan Chengad4196b2008-05-12 19:56:52 +000010502 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +000010503}
10504
Evan Cheng206ee9d2006-07-07 08:33:52 +000010505/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
10506/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
10507/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +000010508/// order.
Dan Gohman475871a2008-07-27 21:46:04 +000010509static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010510 TargetLowering::DAGCombinerInfo &DCI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +000010511 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +000010512 EVT VT = N->getValueType(0);
Mon P Wang1e955802009-04-03 02:43:30 +000010513
Eli Friedman7a5e5552009-06-07 06:52:44 +000010514 if (VT.getSizeInBits() != 128)
10515 return SDValue();
10516
Mon P Wanga0fd0d52010-12-19 23:55:53 +000010517 // Don't create instructions with illegal types after legalize types has run.
10518 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
10519 if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
10520 return SDValue();
10521
Nate Begemanfdea31a2010-03-24 20:49:50 +000010522 SmallVector<SDValue, 16> Elts;
10523 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000010524 Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
Bruno Cardoso Lopes27f12792010-08-28 02:46:39 +000010525
Nate Begemanfdea31a2010-03-24 20:49:50 +000010526 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +000010527}
Evan Chengd880b972008-05-09 21:53:03 +000010528
Bruno Cardoso Lopesb3e06692010-09-03 19:55:05 +000010529/// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
10530/// generation and convert it from being a bunch of shuffles and extracts
10531/// to a simple store and scalar loads to extract the elements.
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010532static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
10533 const TargetLowering &TLI) {
10534 SDValue InputVector = N->getOperand(0);
10535
10536 // Only operate on vectors of 4 elements, where the alternative shuffling
10537 // gets to be more expensive.
10538 if (InputVector.getValueType() != MVT::v4i32)
10539 return SDValue();
10540
10541 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
10542 // single use which is a sign-extend or zero-extend, and all elements are
10543 // used.
10544 SmallVector<SDNode *, 4> Uses;
10545 unsigned ExtractedElements = 0;
10546 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
10547 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
10548 if (UI.getUse().getResNo() != InputVector.getResNo())
10549 return SDValue();
10550
10551 SDNode *Extract = *UI;
10552 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
10553 return SDValue();
10554
10555 if (Extract->getValueType(0) != MVT::i32)
10556 return SDValue();
10557 if (!Extract->hasOneUse())
10558 return SDValue();
10559 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
10560 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
10561 return SDValue();
10562 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
10563 return SDValue();
10564
10565 // Record which element was extracted.
10566 ExtractedElements |=
10567 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
10568
10569 Uses.push_back(Extract);
10570 }
10571
10572 // If not all the elements were used, this may not be worthwhile.
10573 if (ExtractedElements != 15)
10574 return SDValue();
10575
10576 // Ok, we've now decided to do the transformation.
10577 DebugLoc dl = InputVector.getDebugLoc();
10578
10579 // Store the value to a temporary stack slot.
10580 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
Chris Lattner8026a9d2010-09-21 17:50:43 +000010581 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
10582 MachinePointerInfo(), false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010583
10584 // Replace each use (extract) with a load of the appropriate element.
10585 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
10586 UE = Uses.end(); UI != UE; ++UI) {
10587 SDNode *Extract = *UI;
10588
10589 // Compute the element's address.
10590 SDValue Idx = Extract->getOperand(1);
10591 unsigned EltSize =
10592 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
10593 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
10594 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
10595
Eric Christopher90eb4022010-07-22 00:26:08 +000010596 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(),
Chris Lattner51abfe42010-09-21 06:02:19 +000010597 StackPtr, OffsetVal);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010598
10599 // Load the scalar.
Eric Christopher90eb4022010-07-22 00:26:08 +000010600 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch,
Chris Lattner51abfe42010-09-21 06:02:19 +000010601 ScalarAddr, MachinePointerInfo(),
10602 false, false, 0);
Dan Gohman1bbf72b2010-03-15 23:23:03 +000010603
10604 // Replace the exact with the load.
10605 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
10606 }
10607
10608 // The replacement was made in place; don't return anything.
10609 return SDValue();
10610}
10611
Chris Lattner83e6c992006-10-04 06:57:07 +000010612/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000010613static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +000010614 const X86Subtarget *Subtarget) {
10615 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +000010616 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +000010617 // Get the LHS/RHS of the select.
10618 SDValue LHS = N->getOperand(1);
10619 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010620
Dan Gohman670e5392009-09-21 18:03:22 +000010621 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +000010622 // instructions match the semantics of the common C idiom x<y?x:y but not
10623 // x<=y?x:y, because of how they handle negative zero (which can be
10624 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +000010625 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +000010626 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +000010627 Cond.getOpcode() == ISD::SETCC) {
10628 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010629
Chris Lattner47b4ce82009-03-11 05:48:52 +000010630 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +000010631 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +000010632 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
10633 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010634 switch (CC) {
10635 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010636 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010637 // Converting this to a min would handle NaNs incorrectly, and swapping
10638 // the operands would cause it to handle comparisons between positive
10639 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010640 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010641 if (!UnsafeFPMath &&
10642 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10643 break;
10644 std::swap(LHS, RHS);
10645 }
Dan Gohman670e5392009-09-21 18:03:22 +000010646 Opcode = X86ISD::FMIN;
10647 break;
10648 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010649 // Converting this to a min would handle comparisons between positive
10650 // and negative zero incorrectly.
10651 if (!UnsafeFPMath &&
10652 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
10653 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010654 Opcode = X86ISD::FMIN;
10655 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010656 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010657 // Converting this to a min would handle both negative zeros and NaNs
10658 // incorrectly, but we can swap the operands to fix both.
10659 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010660 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010661 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010662 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010663 Opcode = X86ISD::FMIN;
10664 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010665
Dan Gohman670e5392009-09-21 18:03:22 +000010666 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010667 // Converting this to a max would handle comparisons between positive
10668 // and negative zero incorrectly.
10669 if (!UnsafeFPMath &&
10670 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
10671 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010672 Opcode = X86ISD::FMAX;
10673 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +000010674 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010675 // Converting this to a max would handle NaNs incorrectly, and swapping
10676 // the operands would cause it to handle comparisons between positive
10677 // and negative zero incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010678 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
Dan Gohmane8326932010-02-24 06:52:40 +000010679 if (!UnsafeFPMath &&
10680 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
10681 break;
10682 std::swap(LHS, RHS);
10683 }
Dan Gohman670e5392009-09-21 18:03:22 +000010684 Opcode = X86ISD::FMAX;
10685 break;
10686 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010687 // Converting this to a max would handle both negative zeros and NaNs
10688 // incorrectly, but we can swap the operands to fix both.
10689 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010690 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010691 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010692 case ISD::SETGE:
10693 Opcode = X86ISD::FMAX;
10694 break;
Chris Lattner83e6c992006-10-04 06:57:07 +000010695 }
Dan Gohman670e5392009-09-21 18:03:22 +000010696 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +000010697 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
10698 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +000010699 switch (CC) {
10700 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +000010701 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010702 // Converting this to a min would handle comparisons between positive
10703 // and negative zero incorrectly, and swapping the operands would
10704 // cause it to handle NaNs incorrectly.
10705 if (!UnsafeFPMath &&
10706 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
Evan Cheng60108e92010-07-15 22:07:12 +000010707 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010708 break;
10709 std::swap(LHS, RHS);
10710 }
Dan Gohman670e5392009-09-21 18:03:22 +000010711 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +000010712 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010713 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +000010714 // Converting this to a min would handle NaNs incorrectly.
10715 if (!UnsafeFPMath &&
10716 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
10717 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010718 Opcode = X86ISD::FMIN;
10719 break;
10720 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +000010721 // Converting this to a min would handle both negative zeros and NaNs
10722 // incorrectly, but we can swap the operands to fix both.
10723 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010724 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010725 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010726 case ISD::SETGE:
10727 Opcode = X86ISD::FMIN;
10728 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010729
Dan Gohman670e5392009-09-21 18:03:22 +000010730 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +000010731 // Converting this to a max would handle NaNs incorrectly.
Evan Cheng60108e92010-07-15 22:07:12 +000010732 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010733 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010734 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +000010735 break;
Dan Gohman670e5392009-09-21 18:03:22 +000010736 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +000010737 // Converting this to a max would handle comparisons between positive
10738 // and negative zero incorrectly, and swapping the operands would
10739 // cause it to handle NaNs incorrectly.
10740 if (!UnsafeFPMath &&
10741 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
Evan Cheng60108e92010-07-15 22:07:12 +000010742 if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
Dan Gohmane8326932010-02-24 06:52:40 +000010743 break;
10744 std::swap(LHS, RHS);
10745 }
Dan Gohman670e5392009-09-21 18:03:22 +000010746 Opcode = X86ISD::FMAX;
10747 break;
10748 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +000010749 // Converting this to a max would handle both negative zeros and NaNs
10750 // incorrectly, but we can swap the operands to fix both.
10751 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +000010752 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010753 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +000010754 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +000010755 Opcode = X86ISD::FMAX;
10756 break;
10757 }
Chris Lattner83e6c992006-10-04 06:57:07 +000010758 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010759
Chris Lattner47b4ce82009-03-11 05:48:52 +000010760 if (Opcode)
10761 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +000010762 }
Eric Christopherfd179292009-08-27 18:07:15 +000010763
Chris Lattnerd1980a52009-03-12 06:52:53 +000010764 // If this is a select between two integer constants, try to do some
10765 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +000010766 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
10767 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +000010768 // Don't do this for crazy integer types.
10769 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
10770 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +000010771 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010772 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +000010773
Chris Lattnercee56e72009-03-13 05:53:31 +000010774 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +000010775 // Efficiently invertible.
10776 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
10777 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
10778 isa<ConstantSDNode>(Cond.getOperand(1))))) {
10779 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +000010780 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +000010781 }
Eric Christopherfd179292009-08-27 18:07:15 +000010782
Chris Lattnerd1980a52009-03-12 06:52:53 +000010783 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010784 if (FalseC->getAPIntValue() == 0 &&
10785 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +000010786 if (NeedsCondInvert) // Invert the condition if needed.
10787 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10788 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010789
Chris Lattnerd1980a52009-03-12 06:52:53 +000010790 // Zero extend the condition if needed.
10791 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010792
Chris Lattnercee56e72009-03-13 05:53:31 +000010793 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +000010794 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010795 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010796 }
Eric Christopherfd179292009-08-27 18:07:15 +000010797
Chris Lattner97a29a52009-03-13 05:22:11 +000010798 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +000010799 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +000010800 if (NeedsCondInvert) // Invert the condition if needed.
10801 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10802 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010803
Chris Lattner97a29a52009-03-13 05:22:11 +000010804 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010805 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10806 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010807 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +000010808 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +000010809 }
Eric Christopherfd179292009-08-27 18:07:15 +000010810
Chris Lattnercee56e72009-03-13 05:53:31 +000010811 // Optimize cases that will turn into an LEA instruction. This requires
10812 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010813 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010814 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010815 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010816
Chris Lattnercee56e72009-03-13 05:53:31 +000010817 bool isFastMultiplier = false;
10818 if (Diff < 10) {
10819 switch ((unsigned char)Diff) {
10820 default: break;
10821 case 1: // result = add base, cond
10822 case 2: // result = lea base( , cond*2)
10823 case 3: // result = lea base(cond, cond*2)
10824 case 4: // result = lea base( , cond*4)
10825 case 5: // result = lea base(cond, cond*4)
10826 case 8: // result = lea base( , cond*8)
10827 case 9: // result = lea base(cond, cond*8)
10828 isFastMultiplier = true;
10829 break;
10830 }
10831 }
Eric Christopherfd179292009-08-27 18:07:15 +000010832
Chris Lattnercee56e72009-03-13 05:53:31 +000010833 if (isFastMultiplier) {
10834 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10835 if (NeedsCondInvert) // Invert the condition if needed.
10836 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
10837 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010838
Chris Lattnercee56e72009-03-13 05:53:31 +000010839 // Zero extend the condition if needed.
10840 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10841 Cond);
10842 // Scale the condition by the difference.
10843 if (Diff != 1)
10844 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10845 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +000010846
Chris Lattnercee56e72009-03-13 05:53:31 +000010847 // Add the base if non-zero.
10848 if (FalseC->getAPIntValue() != 0)
10849 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10850 SDValue(FalseC, 0));
10851 return Cond;
10852 }
Eric Christopherfd179292009-08-27 18:07:15 +000010853 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010854 }
10855 }
Eric Christopherfd179292009-08-27 18:07:15 +000010856
Dan Gohman475871a2008-07-27 21:46:04 +000010857 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +000010858}
10859
Chris Lattnerd1980a52009-03-12 06:52:53 +000010860/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
10861static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
10862 TargetLowering::DAGCombinerInfo &DCI) {
10863 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +000010864
Chris Lattnerd1980a52009-03-12 06:52:53 +000010865 // If the flag operand isn't dead, don't touch this CMOV.
10866 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
10867 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +000010868
Chris Lattnerd1980a52009-03-12 06:52:53 +000010869 // If this is a select between two integer constants, try to do some
10870 // optimizations. Note that the operands are ordered the opposite of SELECT
10871 // operands.
10872 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
10873 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
10874 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
10875 // larger than FalseC (the false value).
10876 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +000010877
Chris Lattnerd1980a52009-03-12 06:52:53 +000010878 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
10879 CC = X86::GetOppositeBranchCondition(CC);
10880 std::swap(TrueC, FalseC);
10881 }
Eric Christopherfd179292009-08-27 18:07:15 +000010882
Chris Lattnerd1980a52009-03-12 06:52:53 +000010883 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +000010884 // This is efficient for any integer data type (including i8/i16) and
10885 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +000010886 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
10887 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010888 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10889 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010890
Chris Lattnerd1980a52009-03-12 06:52:53 +000010891 // Zero extend the condition if needed.
10892 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010893
Chris Lattnerd1980a52009-03-12 06:52:53 +000010894 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
10895 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +000010896 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +000010897 if (N->getNumValues() == 2) // Dead flag value?
10898 return DCI.CombineTo(N, Cond, SDValue());
10899 return Cond;
10900 }
Eric Christopherfd179292009-08-27 18:07:15 +000010901
Chris Lattnercee56e72009-03-13 05:53:31 +000010902 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
10903 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +000010904 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
10905 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010906 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10907 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +000010908
Chris Lattner97a29a52009-03-13 05:22:11 +000010909 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +000010910 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
10911 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +000010912 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10913 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +000010914
Chris Lattner97a29a52009-03-13 05:22:11 +000010915 if (N->getNumValues() == 2) // Dead flag value?
10916 return DCI.CombineTo(N, Cond, SDValue());
10917 return Cond;
10918 }
Eric Christopherfd179292009-08-27 18:07:15 +000010919
Chris Lattnercee56e72009-03-13 05:53:31 +000010920 // Optimize cases that will turn into an LEA instruction. This requires
10921 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +000010922 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +000010923 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +000010924 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +000010925
Chris Lattnercee56e72009-03-13 05:53:31 +000010926 bool isFastMultiplier = false;
10927 if (Diff < 10) {
10928 switch ((unsigned char)Diff) {
10929 default: break;
10930 case 1: // result = add base, cond
10931 case 2: // result = lea base( , cond*2)
10932 case 3: // result = lea base(cond, cond*2)
10933 case 4: // result = lea base( , cond*4)
10934 case 5: // result = lea base(cond, cond*4)
10935 case 8: // result = lea base( , cond*8)
10936 case 9: // result = lea base(cond, cond*8)
10937 isFastMultiplier = true;
10938 break;
10939 }
10940 }
Eric Christopherfd179292009-08-27 18:07:15 +000010941
Chris Lattnercee56e72009-03-13 05:53:31 +000010942 if (isFastMultiplier) {
10943 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
10944 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +000010945 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
10946 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +000010947 // Zero extend the condition if needed.
10948 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
10949 Cond);
10950 // Scale the condition by the difference.
10951 if (Diff != 1)
10952 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
10953 DAG.getConstant(Diff, Cond.getValueType()));
10954
10955 // Add the base if non-zero.
10956 if (FalseC->getAPIntValue() != 0)
10957 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
10958 SDValue(FalseC, 0));
10959 if (N->getNumValues() == 2) // Dead flag value?
10960 return DCI.CombineTo(N, Cond, SDValue());
10961 return Cond;
10962 }
Eric Christopherfd179292009-08-27 18:07:15 +000010963 }
Chris Lattnerd1980a52009-03-12 06:52:53 +000010964 }
10965 }
10966 return SDValue();
10967}
10968
10969
Evan Cheng0b0cd912009-03-28 05:57:29 +000010970/// PerformMulCombine - Optimize a single multiply with constant into two
10971/// in order to implement it with two cheaper instructions, e.g.
10972/// LEA + SHL, LEA + LEA.
10973static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
10974 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +000010975 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
10976 return SDValue();
10977
Owen Andersone50ed302009-08-10 22:56:29 +000010978 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010979 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +000010980 return SDValue();
10981
10982 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
10983 if (!C)
10984 return SDValue();
10985 uint64_t MulAmt = C->getZExtValue();
10986 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
10987 return SDValue();
10988
10989 uint64_t MulAmt1 = 0;
10990 uint64_t MulAmt2 = 0;
10991 if ((MulAmt % 9) == 0) {
10992 MulAmt1 = 9;
10993 MulAmt2 = MulAmt / 9;
10994 } else if ((MulAmt % 5) == 0) {
10995 MulAmt1 = 5;
10996 MulAmt2 = MulAmt / 5;
10997 } else if ((MulAmt % 3) == 0) {
10998 MulAmt1 = 3;
10999 MulAmt2 = MulAmt / 3;
11000 }
11001 if (MulAmt2 &&
11002 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
11003 DebugLoc DL = N->getDebugLoc();
11004
11005 if (isPowerOf2_64(MulAmt2) &&
11006 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
11007 // If second multiplifer is pow2, issue it first. We want the multiply by
11008 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
11009 // is an add.
11010 std::swap(MulAmt1, MulAmt2);
11011
11012 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +000011013 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011014 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +000011015 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +000011016 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011017 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +000011018 DAG.getConstant(MulAmt1, VT));
11019
Eric Christopherfd179292009-08-27 18:07:15 +000011020 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +000011021 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +000011022 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +000011023 else
Evan Cheng73f24c92009-03-30 21:36:47 +000011024 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +000011025 DAG.getConstant(MulAmt2, VT));
11026
11027 // Do not add new nodes to DAG combiner worklist.
11028 DCI.CombineTo(N, NewMul, false);
11029 }
11030 return SDValue();
11031}
11032
Evan Chengad9c0a32009-12-15 00:53:42 +000011033static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
11034 SDValue N0 = N->getOperand(0);
11035 SDValue N1 = N->getOperand(1);
11036 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
11037 EVT VT = N0.getValueType();
11038
11039 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
11040 // since the result of setcc_c is all zero's or all ones.
11041 if (N1C && N0.getOpcode() == ISD::AND &&
11042 N0.getOperand(1).getOpcode() == ISD::Constant) {
11043 SDValue N00 = N0.getOperand(0);
11044 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
11045 ((N00.getOpcode() == ISD::ANY_EXTEND ||
11046 N00.getOpcode() == ISD::ZERO_EXTEND) &&
11047 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
11048 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
11049 APInt ShAmt = N1C->getAPIntValue();
11050 Mask = Mask.shl(ShAmt);
11051 if (Mask != 0)
11052 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
11053 N00, DAG.getConstant(Mask, VT));
11054 }
11055 }
11056
11057 return SDValue();
11058}
Evan Cheng0b0cd912009-03-28 05:57:29 +000011059
Nate Begeman740ab032009-01-26 00:52:55 +000011060/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
11061/// when possible.
11062static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
11063 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +000011064 EVT VT = N->getValueType(0);
11065 if (!VT.isVector() && VT.isInteger() &&
11066 N->getOpcode() == ISD::SHL)
11067 return PerformSHLCombine(N, DAG);
11068
Nate Begeman740ab032009-01-26 00:52:55 +000011069 // On X86 with SSE2 support, we can transform this to a vector shift if
11070 // all elements are shifted by the same amount. We can't do this in legalize
11071 // because the a constant vector is typically transformed to a constant pool
11072 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011073 if (!Subtarget->hasSSE2())
11074 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011075
Owen Anderson825b72b2009-08-11 20:47:22 +000011076 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011077 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000011078
Mon P Wang3becd092009-01-28 08:12:05 +000011079 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +000011080 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +000011081 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +000011082 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +000011083 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
11084 unsigned NumElts = VT.getVectorNumElements();
11085 unsigned i = 0;
11086 for (; i != NumElts; ++i) {
11087 SDValue Arg = ShAmtOp.getOperand(i);
11088 if (Arg.getOpcode() == ISD::UNDEF) continue;
11089 BaseShAmt = Arg;
11090 break;
11091 }
11092 for (; i != NumElts; ++i) {
11093 SDValue Arg = ShAmtOp.getOperand(i);
11094 if (Arg.getOpcode() == ISD::UNDEF) continue;
11095 if (Arg != BaseShAmt) {
11096 return SDValue();
11097 }
11098 }
11099 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +000011100 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +000011101 SDValue InVec = ShAmtOp.getOperand(0);
11102 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
11103 unsigned NumElts = InVec.getValueType().getVectorNumElements();
11104 unsigned i = 0;
11105 for (; i != NumElts; ++i) {
11106 SDValue Arg = InVec.getOperand(i);
11107 if (Arg.getOpcode() == ISD::UNDEF) continue;
11108 BaseShAmt = Arg;
11109 break;
11110 }
11111 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
11112 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +000011113 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +000011114 if (C->getZExtValue() == SplatIdx)
11115 BaseShAmt = InVec.getOperand(1);
11116 }
11117 }
11118 if (BaseShAmt.getNode() == 0)
11119 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
11120 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +000011121 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011122 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +000011123
Mon P Wangefa42202009-09-03 19:56:25 +000011124 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +000011125 if (EltVT.bitsGT(MVT::i32))
11126 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
11127 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +000011128 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +000011129
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011130 // The shift amount is identical so we can do a vector shift.
11131 SDValue ValOp = N->getOperand(0);
11132 switch (N->getOpcode()) {
11133 default:
Torok Edwinc23197a2009-07-14 16:55:14 +000011134 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011135 break;
11136 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011137 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011138 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011139 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011140 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011141 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011142 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011143 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011144 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011145 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011146 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011147 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011148 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011149 break;
11150 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +000011151 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011152 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011153 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011154 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011155 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011156 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011157 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011158 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011159 break;
11160 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +000011161 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011162 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011163 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011164 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011165 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011166 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011167 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011168 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +000011169 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +000011170 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +000011171 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +000011172 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +000011173 break;
Nate Begeman740ab032009-01-26 00:52:55 +000011174 }
11175 return SDValue();
11176}
11177
Nate Begemanb65c1752010-12-17 22:55:37 +000011178
11179static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
11180 TargetLowering::DAGCombinerInfo &DCI,
11181 const X86Subtarget *Subtarget) {
11182 if (DCI.isBeforeLegalizeOps())
11183 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011184
Nate Begemanb65c1752010-12-17 22:55:37 +000011185 // Want to form PANDN nodes, in the hopes of then easily combining them with
11186 // OR and AND nodes to form PBLEND/PSIGN.
11187 EVT VT = N->getValueType(0);
11188 if (VT != MVT::v2i64)
11189 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011190
Nate Begemanb65c1752010-12-17 22:55:37 +000011191 SDValue N0 = N->getOperand(0);
11192 SDValue N1 = N->getOperand(1);
11193 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011194
Nate Begemanb65c1752010-12-17 22:55:37 +000011195 // Check LHS for vnot
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011196 if (N0.getOpcode() == ISD::XOR &&
Nate Begemanb65c1752010-12-17 22:55:37 +000011197 ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
11198 return DAG.getNode(X86ISD::PANDN, DL, VT, N0.getOperand(0), N1);
11199
11200 // Check RHS for vnot
11201 if (N1.getOpcode() == ISD::XOR &&
11202 ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
11203 return DAG.getNode(X86ISD::PANDN, DL, VT, N1.getOperand(0), N0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011204
Nate Begemanb65c1752010-12-17 22:55:37 +000011205 return SDValue();
11206}
11207
Evan Cheng760d1942010-01-04 21:22:48 +000011208static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +000011209 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +000011210 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +000011211 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +000011212 return SDValue();
11213
Evan Cheng760d1942010-01-04 21:22:48 +000011214 EVT VT = N->getValueType(0);
Nate Begemanb65c1752010-12-17 22:55:37 +000011215 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64 && VT != MVT::v2i64)
Evan Cheng760d1942010-01-04 21:22:48 +000011216 return SDValue();
11217
Evan Cheng760d1942010-01-04 21:22:48 +000011218 SDValue N0 = N->getOperand(0);
11219 SDValue N1 = N->getOperand(1);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011220
Nate Begemanb65c1752010-12-17 22:55:37 +000011221 // look for psign/blend
11222 if (Subtarget->hasSSSE3()) {
11223 if (VT == MVT::v2i64) {
11224 // Canonicalize pandn to RHS
11225 if (N0.getOpcode() == X86ISD::PANDN)
11226 std::swap(N0, N1);
11227 // or (and (m, x), (pandn m, y))
11228 if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::PANDN) {
11229 SDValue Mask = N1.getOperand(0);
11230 SDValue X = N1.getOperand(1);
11231 SDValue Y;
11232 if (N0.getOperand(0) == Mask)
11233 Y = N0.getOperand(1);
11234 if (N0.getOperand(1) == Mask)
11235 Y = N0.getOperand(0);
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011236
Nate Begemanb65c1752010-12-17 22:55:37 +000011237 // Check to see if the mask appeared in both the AND and PANDN and
11238 if (!Y.getNode())
11239 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011240
Nate Begemanb65c1752010-12-17 22:55:37 +000011241 // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
11242 if (Mask.getOpcode() != ISD::BITCAST ||
11243 X.getOpcode() != ISD::BITCAST ||
11244 Y.getOpcode() != ISD::BITCAST)
11245 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011246
Nate Begemanb65c1752010-12-17 22:55:37 +000011247 // Look through mask bitcast.
11248 Mask = Mask.getOperand(0);
11249 EVT MaskVT = Mask.getValueType();
11250
11251 // Validate that the Mask operand is a vector sra node. The sra node
11252 // will be an intrinsic.
11253 if (Mask.getOpcode() != ISD::INTRINSIC_WO_CHAIN)
11254 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011255
Nate Begemanb65c1752010-12-17 22:55:37 +000011256 // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
11257 // there is no psrai.b
11258 switch (cast<ConstantSDNode>(Mask.getOperand(0))->getZExtValue()) {
11259 case Intrinsic::x86_sse2_psrai_w:
11260 case Intrinsic::x86_sse2_psrai_d:
11261 break;
11262 default: return SDValue();
11263 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011264
Nate Begemanb65c1752010-12-17 22:55:37 +000011265 // Check that the SRA is all signbits.
11266 SDValue SraC = Mask.getOperand(2);
11267 unsigned SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue();
11268 unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
11269 if ((SraAmt + 1) != EltBits)
11270 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011271
Nate Begemanb65c1752010-12-17 22:55:37 +000011272 DebugLoc DL = N->getDebugLoc();
11273
11274 // Now we know we at least have a plendvb with the mask val. See if
11275 // we can form a psignb/w/d.
11276 // psign = x.type == y.type == mask.type && y = sub(0, x);
11277 X = X.getOperand(0);
11278 Y = Y.getOperand(0);
11279 if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
11280 ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
11281 X.getValueType() == MaskVT && X.getValueType() == Y.getValueType()){
11282 unsigned Opc = 0;
11283 switch (EltBits) {
11284 case 8: Opc = X86ISD::PSIGNB; break;
11285 case 16: Opc = X86ISD::PSIGNW; break;
11286 case 32: Opc = X86ISD::PSIGND; break;
11287 default: break;
11288 }
11289 if (Opc) {
11290 SDValue Sign = DAG.getNode(Opc, DL, MaskVT, X, Mask.getOperand(1));
11291 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Sign);
11292 }
11293 }
11294 // PBLENDVB only available on SSE 4.1
11295 if (!Subtarget->hasSSE41())
11296 return SDValue();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011297
Nate Begemanb65c1752010-12-17 22:55:37 +000011298 X = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, X);
11299 Y = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Y);
11300 Mask = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Mask);
Nate Begeman672fb622010-12-20 22:04:24 +000011301 Mask = DAG.getNode(X86ISD::PBLENDVB, DL, MVT::v16i8, X, Y, Mask);
Nate Begemanb65c1752010-12-17 22:55:37 +000011302 return DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Mask);
11303 }
11304 }
11305 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011306
Nate Begemanb65c1752010-12-17 22:55:37 +000011307 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
Evan Cheng760d1942010-01-04 21:22:48 +000011308 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
11309 std::swap(N0, N1);
11310 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
11311 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +000011312 if (!N0.hasOneUse() || !N1.hasOneUse())
11313 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +000011314
11315 SDValue ShAmt0 = N0.getOperand(1);
11316 if (ShAmt0.getValueType() != MVT::i8)
11317 return SDValue();
11318 SDValue ShAmt1 = N1.getOperand(1);
11319 if (ShAmt1.getValueType() != MVT::i8)
11320 return SDValue();
11321 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
11322 ShAmt0 = ShAmt0.getOperand(0);
11323 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
11324 ShAmt1 = ShAmt1.getOperand(0);
11325
11326 DebugLoc DL = N->getDebugLoc();
11327 unsigned Opc = X86ISD::SHLD;
11328 SDValue Op0 = N0.getOperand(0);
11329 SDValue Op1 = N1.getOperand(0);
11330 if (ShAmt0.getOpcode() == ISD::SUB) {
11331 Opc = X86ISD::SHRD;
11332 std::swap(Op0, Op1);
11333 std::swap(ShAmt0, ShAmt1);
11334 }
11335
Evan Cheng8b1190a2010-04-28 01:18:01 +000011336 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +000011337 if (ShAmt1.getOpcode() == ISD::SUB) {
11338 SDValue Sum = ShAmt1.getOperand(0);
11339 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Dan Gohman4e39e9d2010-06-24 14:30:44 +000011340 SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
11341 if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
11342 ShAmt1Op1 = ShAmt1Op1.getOperand(0);
11343 if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
Evan Cheng760d1942010-01-04 21:22:48 +000011344 return DAG.getNode(Opc, DL, VT,
11345 Op0, Op1,
11346 DAG.getNode(ISD::TRUNCATE, DL,
11347 MVT::i8, ShAmt0));
11348 }
11349 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
11350 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
11351 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +000011352 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +000011353 return DAG.getNode(Opc, DL, VT,
11354 N0.getOperand(0), N1.getOperand(0),
11355 DAG.getNode(ISD::TRUNCATE, DL,
11356 MVT::i8, ShAmt0));
11357 }
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011358
Evan Cheng760d1942010-01-04 21:22:48 +000011359 return SDValue();
11360}
11361
Chris Lattner149a4e52008-02-22 02:09:43 +000011362/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011363static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +000011364 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +000011365 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
11366 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +000011367 // A preferable solution to the general problem is to figure out the right
11368 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +000011369
11370 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +000011371 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +000011372 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +000011373 if (VT.getSizeInBits() != 64)
11374 return SDValue();
11375
Devang Patel578efa92009-06-05 21:57:13 +000011376 const Function *F = DAG.getMachineFunction().getFunction();
11377 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +000011378 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +000011379 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +000011380 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +000011381 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +000011382 isa<LoadSDNode>(St->getValue()) &&
11383 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
11384 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011385 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011386 LoadSDNode *Ld = 0;
11387 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +000011388 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +000011389 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011390 // Must be a store of a load. We currently handle two cases: the load
11391 // is a direct child, and it's under an intervening TokenFactor. It is
11392 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +000011393 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +000011394 Ld = cast<LoadSDNode>(St->getChain());
11395 else if (St->getValue().hasOneUse() &&
11396 ChainVal->getOpcode() == ISD::TokenFactor) {
11397 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +000011398 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +000011399 TokenFactorIndex = i;
11400 Ld = cast<LoadSDNode>(St->getValue());
11401 } else
11402 Ops.push_back(ChainVal->getOperand(i));
11403 }
11404 }
Dale Johannesen079f2a62008-02-25 19:20:14 +000011405
Evan Cheng536e6672009-03-12 05:59:15 +000011406 if (!Ld || !ISD::isNormalLoad(Ld))
11407 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011408
Evan Cheng536e6672009-03-12 05:59:15 +000011409 // If this is not the MMX case, i.e. we are just turning i64 load/store
11410 // into f64 load/store, avoid the transformation if there are multiple
11411 // uses of the loaded value.
11412 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
11413 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +000011414
Evan Cheng536e6672009-03-12 05:59:15 +000011415 DebugLoc LdDL = Ld->getDebugLoc();
11416 DebugLoc StDL = N->getDebugLoc();
11417 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
11418 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
11419 // pair instead.
11420 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +000011421 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Chris Lattner51abfe42010-09-21 06:02:19 +000011422 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
11423 Ld->getPointerInfo(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011424 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011425 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +000011426 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +000011427 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +000011428 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +000011429 Ops.size());
11430 }
Evan Cheng536e6672009-03-12 05:59:15 +000011431 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner51abfe42010-09-21 06:02:19 +000011432 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011433 St->isVolatile(), St->isNonTemporal(),
11434 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +000011435 }
Evan Cheng536e6672009-03-12 05:59:15 +000011436
11437 // Otherwise, lower to two pairs of 32-bit loads / stores.
11438 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011439 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
11440 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011441
Owen Anderson825b72b2009-08-11 20:47:22 +000011442 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011443 Ld->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011444 Ld->isVolatile(), Ld->isNonTemporal(),
11445 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +000011446 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Chris Lattner51abfe42010-09-21 06:02:19 +000011447 Ld->getPointerInfo().getWithOffset(4),
David Greene67c9d422010-02-15 16:53:33 +000011448 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011449 MinAlign(Ld->getAlignment(), 4));
11450
11451 SDValue NewChain = LoLd.getValue(1);
11452 if (TokenFactorIndex != -1) {
11453 Ops.push_back(LoLd);
11454 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +000011455 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +000011456 Ops.size());
11457 }
11458
11459 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +000011460 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
11461 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +000011462
11463 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011464 St->getPointerInfo(),
David Greene67c9d422010-02-15 16:53:33 +000011465 St->isVolatile(), St->isNonTemporal(),
11466 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +000011467 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
Chris Lattner8026a9d2010-09-21 17:50:43 +000011468 St->getPointerInfo().getWithOffset(4),
Evan Cheng536e6672009-03-12 05:59:15 +000011469 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +000011470 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +000011471 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +000011472 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +000011473 }
Dan Gohman475871a2008-07-27 21:46:04 +000011474 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +000011475}
11476
Chris Lattner6cf73262008-01-25 06:14:17 +000011477/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
11478/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011479static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +000011480 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
11481 // F[X]OR(0.0, x) -> x
11482 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +000011483 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11484 if (C->getValueAPF().isPosZero())
11485 return N->getOperand(1);
11486 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11487 if (C->getValueAPF().isPosZero())
11488 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +000011489 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011490}
11491
11492/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +000011493static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +000011494 // FAND(0.0, x) -> 0.0
11495 // FAND(x, 0.0) -> 0.0
11496 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
11497 if (C->getValueAPF().isPosZero())
11498 return N->getOperand(0);
11499 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
11500 if (C->getValueAPF().isPosZero())
11501 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +000011502 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +000011503}
11504
Dan Gohmane5af2d32009-01-29 01:59:02 +000011505static SDValue PerformBTCombine(SDNode *N,
11506 SelectionDAG &DAG,
11507 TargetLowering::DAGCombinerInfo &DCI) {
11508 // BT ignores high bits in the bit index operand.
11509 SDValue Op1 = N->getOperand(1);
11510 if (Op1.hasOneUse()) {
11511 unsigned BitWidth = Op1.getValueSizeInBits();
11512 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
11513 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011514 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
11515 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +000011516 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +000011517 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
11518 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
11519 DCI.CommitTargetLoweringOpt(TLO);
11520 }
11521 return SDValue();
11522}
Chris Lattner83e6c992006-10-04 06:57:07 +000011523
Eli Friedman7a5e5552009-06-07 06:52:44 +000011524static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
11525 SDValue Op = N->getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011526 if (Op.getOpcode() == ISD::BITCAST)
Eli Friedman7a5e5552009-06-07 06:52:44 +000011527 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +000011528 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +000011529 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +000011530 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +000011531 OpVT.getVectorElementType().getSizeInBits()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011532 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, Op);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011533 }
11534 return SDValue();
11535}
11536
Evan Cheng2e489c42009-12-16 00:53:11 +000011537static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
11538 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
11539 // (and (i32 x86isd::setcc_carry), 1)
11540 // This eliminates the zext. This transformation is necessary because
11541 // ISD::SETCC is always legalized to i8.
11542 DebugLoc dl = N->getDebugLoc();
11543 SDValue N0 = N->getOperand(0);
11544 EVT VT = N->getValueType(0);
11545 if (N0.getOpcode() == ISD::AND &&
11546 N0.hasOneUse() &&
11547 N0.getOperand(0).hasOneUse()) {
11548 SDValue N00 = N0.getOperand(0);
11549 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
11550 return SDValue();
11551 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
11552 if (!C || C->getZExtValue() != 1)
11553 return SDValue();
11554 return DAG.getNode(ISD::AND, dl, VT,
11555 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
11556 N00.getOperand(0), N00.getOperand(1)),
11557 DAG.getConstant(1, VT));
11558 }
11559
11560 return SDValue();
11561}
11562
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011563// Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
11564static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG) {
11565 unsigned X86CC = N->getConstantOperandVal(0);
11566 SDValue EFLAG = N->getOperand(1);
11567 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011568
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011569 // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
11570 // a zext and produces an all-ones bit which is more useful than 0/1 in some
11571 // cases.
11572 if (X86CC == X86::COND_B)
11573 return DAG.getNode(ISD::AND, DL, MVT::i8,
11574 DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
11575 DAG.getConstant(X86CC, MVT::i8), EFLAG),
11576 DAG.getConstant(1, MVT::i8));
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011577
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011578 return SDValue();
11579}
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011580
Chris Lattner23a01992010-12-20 01:37:09 +000011581// Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
11582static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
11583 X86TargetLowering::DAGCombinerInfo &DCI) {
11584 // If the LHS and RHS of the ADC node are zero, then it can't overflow and
11585 // the result is either zero or one (depending on the input carry bit).
11586 // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
11587 if (X86::isZeroNode(N->getOperand(0)) &&
11588 X86::isZeroNode(N->getOperand(1)) &&
11589 // We don't have a good way to replace an EFLAGS use, so only do this when
11590 // dead right now.
11591 SDValue(N, 1).use_empty()) {
11592 DebugLoc DL = N->getDebugLoc();
11593 EVT VT = N->getValueType(0);
11594 SDValue CarryOut = DAG.getConstant(0, N->getValueType(1));
11595 SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
11596 DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
11597 DAG.getConstant(X86::COND_B,MVT::i8),
11598 N->getOperand(2)),
11599 DAG.getConstant(1, VT));
11600 return DCI.CombineTo(N, Res1, CarryOut);
11601 }
11602
11603 return SDValue();
11604}
11605
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011606// fold (add Y, (sete X, 0)) -> adc 0, Y
11607// (add Y, (setne X, 0)) -> sbb -1, Y
11608// (sub (sete X, 0), Y) -> sbb 0, Y
11609// (sub (setne X, 0), Y) -> adc -1, Y
11610static SDValue OptimizeConditonalInDecrement(SDNode *N, SelectionDAG &DAG) {
11611 DebugLoc DL = N->getDebugLoc();
NAKAMURA Takumie5fffe92011-01-26 02:03:37 +000011612
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011613 // Look through ZExts.
11614 SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
11615 if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
11616 return SDValue();
11617
11618 SDValue SetCC = Ext.getOperand(0);
11619 if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
11620 return SDValue();
11621
11622 X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
11623 if (CC != X86::COND_E && CC != X86::COND_NE)
11624 return SDValue();
11625
11626 SDValue Cmp = SetCC.getOperand(1);
11627 if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
Chris Lattner9cd3da42011-01-16 02:56:53 +000011628 !X86::isZeroNode(Cmp.getOperand(1)) ||
11629 !Cmp.getOperand(0).getValueType().isInteger())
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011630 return SDValue();
11631
11632 SDValue CmpOp0 = Cmp.getOperand(0);
11633 SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
11634 DAG.getConstant(1, CmpOp0.getValueType()));
11635
11636 SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
11637 if (CC == X86::COND_NE)
11638 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
11639 DL, OtherVal.getValueType(), OtherVal,
11640 DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp);
11641 return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
11642 DL, OtherVal.getValueType(), OtherVal,
11643 DAG.getConstant(0, OtherVal.getValueType()), NewCmp);
11644}
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011645
Dan Gohman475871a2008-07-27 21:46:04 +000011646SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +000011647 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +000011648 SelectionDAG &DAG = DCI.DAG;
11649 switch (N->getOpcode()) {
11650 default: break;
Dan Gohman1bbf72b2010-03-15 23:23:03 +000011651 case ISD::EXTRACT_VECTOR_ELT:
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011652 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +000011653 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +000011654 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Benjamin Kramer7d6fe132010-12-21 21:41:44 +000011655 case ISD::ADD:
11656 case ISD::SUB: return OptimizeConditonalInDecrement(N, DAG);
Chris Lattner23a01992010-12-20 01:37:09 +000011657 case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +000011658 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +000011659 case ISD::SHL:
11660 case ISD::SRA:
11661 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Nate Begemanb65c1752010-12-17 22:55:37 +000011662 case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +000011663 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +000011664 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +000011665 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +000011666 case X86ISD::FOR: return PerformFORCombine(N, DAG);
11667 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +000011668 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +000011669 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +000011670 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Chris Lattnerc19d1c32010-12-19 22:08:31 +000011671 case X86ISD::SETCC: return PerformSETCCCombine(N, DAG);
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011672 case X86ISD::SHUFPS: // Handle all target specific shuffles
11673 case X86ISD::SHUFPD:
Bruno Cardoso Lopesaace0f22010-09-04 02:36:07 +000011674 case X86ISD::PALIGN:
Bruno Cardoso Lopese8f279c2010-09-03 22:09:41 +000011675 case X86ISD::PUNPCKHBW:
11676 case X86ISD::PUNPCKHWD:
11677 case X86ISD::PUNPCKHDQ:
11678 case X86ISD::PUNPCKHQDQ:
11679 case X86ISD::UNPCKHPS:
11680 case X86ISD::UNPCKHPD:
11681 case X86ISD::PUNPCKLBW:
11682 case X86ISD::PUNPCKLWD:
11683 case X86ISD::PUNPCKLDQ:
11684 case X86ISD::PUNPCKLQDQ:
11685 case X86ISD::UNPCKLPS:
11686 case X86ISD::UNPCKLPD:
11687 case X86ISD::MOVHLPS:
11688 case X86ISD::MOVLHPS:
11689 case X86ISD::PSHUFD:
11690 case X86ISD::PSHUFHW:
11691 case X86ISD::PSHUFLW:
11692 case X86ISD::MOVSS:
11693 case X86ISD::MOVSD:
Mon P Wanga0fd0d52010-12-19 23:55:53 +000011694 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +000011695 }
11696
Dan Gohman475871a2008-07-27 21:46:04 +000011697 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +000011698}
11699
Evan Chenge5b51ac2010-04-17 06:13:15 +000011700/// isTypeDesirableForOp - Return true if the target has native support for
11701/// the specified value type and it is 'desirable' to use the type for the
11702/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
11703/// instruction encodings are longer and some i16 instructions are slow.
11704bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
11705 if (!isTypeLegal(VT))
11706 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011707 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +000011708 return true;
11709
11710 switch (Opc) {
11711 default:
11712 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +000011713 case ISD::LOAD:
11714 case ISD::SIGN_EXTEND:
11715 case ISD::ZERO_EXTEND:
11716 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011717 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +000011718 case ISD::SRL:
11719 case ISD::SUB:
11720 case ISD::ADD:
11721 case ISD::MUL:
11722 case ISD::AND:
11723 case ISD::OR:
11724 case ISD::XOR:
11725 return false;
11726 }
11727}
11728
11729/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +000011730/// beneficial for dag combiner to promote the specified node. If true, it
11731/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +000011732bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011733 EVT VT = Op.getValueType();
11734 if (VT != MVT::i16)
11735 return false;
11736
Evan Cheng4c26e932010-04-19 19:29:22 +000011737 bool Promote = false;
11738 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011739 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +000011740 default: break;
11741 case ISD::LOAD: {
11742 LoadSDNode *LD = cast<LoadSDNode>(Op);
11743 // If the non-extending load has a single use and it's not live out, then it
11744 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011745 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
11746 Op.hasOneUse()*/) {
11747 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
11748 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
11749 // The only case where we'd want to promote LOAD (rather then it being
11750 // promoted as an operand is when it's only use is liveout.
11751 if (UI->getOpcode() != ISD::CopyToReg)
11752 return false;
11753 }
11754 }
Evan Cheng4c26e932010-04-19 19:29:22 +000011755 Promote = true;
11756 break;
11757 }
11758 case ISD::SIGN_EXTEND:
11759 case ISD::ZERO_EXTEND:
11760 case ISD::ANY_EXTEND:
11761 Promote = true;
11762 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011763 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +000011764 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +000011765 SDValue N0 = Op.getOperand(0);
11766 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +000011767 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +000011768 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011769 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +000011770 break;
11771 }
Evan Cheng64b7bf72010-04-16 06:14:10 +000011772 case ISD::ADD:
11773 case ISD::MUL:
11774 case ISD::AND:
11775 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +000011776 case ISD::XOR:
11777 Commute = true;
11778 // fallthrough
11779 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +000011780 SDValue N0 = Op.getOperand(0);
11781 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +000011782 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011783 return false;
11784 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +000011785 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011786 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +000011787 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +000011788 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +000011789 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011790 }
11791 }
11792
11793 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +000011794 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +000011795}
11796
Evan Cheng60c07e12006-07-05 22:17:51 +000011797//===----------------------------------------------------------------------===//
11798// X86 Inline Assembly Support
11799//===----------------------------------------------------------------------===//
11800
Chris Lattnerb8105652009-07-20 17:51:36 +000011801bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
11802 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
Chris Lattnerb8105652009-07-20 17:51:36 +000011803
11804 std::string AsmStr = IA->getAsmString();
11805
11806 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +000011807 SmallVector<StringRef, 4> AsmPieces;
Peter Collingbourne98361182010-11-13 19:54:23 +000011808 SplitString(AsmStr, AsmPieces, ";\n");
Chris Lattnerb8105652009-07-20 17:51:36 +000011809
11810 switch (AsmPieces.size()) {
11811 default: return false;
11812 case 1:
11813 AsmStr = AsmPieces[0];
11814 AsmPieces.clear();
11815 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
11816
Evan Cheng55d42002011-01-08 01:24:27 +000011817 // FIXME: this should verify that we are targetting a 486 or better. If not,
11818 // we will turn this bswap into something that will be lowered to logical ops
11819 // instead of emitting the bswap asm. For now, we don't support 486 or lower
11820 // so don't worry about this.
Chris Lattnerb8105652009-07-20 17:51:36 +000011821 // bswap $0
11822 if (AsmPieces.size() == 2 &&
11823 (AsmPieces[0] == "bswap" ||
11824 AsmPieces[0] == "bswapq" ||
11825 AsmPieces[0] == "bswapl") &&
11826 (AsmPieces[1] == "$0" ||
11827 AsmPieces[1] == "${0:q}")) {
11828 // No need to check constraints, nothing other than the equivalent of
11829 // "=r,0" would be valid here.
Evan Cheng55d42002011-01-08 01:24:27 +000011830 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11831 if (!Ty || Ty->getBitWidth() % 16 != 0)
11832 return false;
11833 return IntrinsicLowering::LowerToByteSwap(CI);
Chris Lattnerb8105652009-07-20 17:51:36 +000011834 }
11835 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000011836 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011837 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011838 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +000011839 AsmPieces[1] == "$$8," &&
11840 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +000011841 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11842 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011843 const std::string &ConstraintsStr = IA->getConstraintString();
11844 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000011845 std::sort(AsmPieces.begin(), AsmPieces.end());
11846 if (AsmPieces.size() == 4 &&
11847 AsmPieces[0] == "~{cc}" &&
11848 AsmPieces[1] == "~{dirflag}" &&
11849 AsmPieces[2] == "~{flags}" &&
11850 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011851 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11852 if (!Ty || Ty->getBitWidth() % 16 != 0)
11853 return false;
11854 return IntrinsicLowering::LowerToByteSwap(CI);
Dan Gohman0ef701e2010-03-04 19:58:08 +000011855 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011856 }
11857 break;
11858 case 3:
Peter Collingbourne948cf022010-11-13 19:54:30 +000011859 if (CI->getType()->isIntegerTy(32) &&
11860 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
11861 SmallVector<StringRef, 4> Words;
11862 SplitString(AsmPieces[0], Words, " \t,");
11863 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11864 Words[2] == "${0:w}") {
11865 Words.clear();
11866 SplitString(AsmPieces[1], Words, " \t,");
11867 if (Words.size() == 3 && Words[0] == "rorl" && Words[1] == "$$16" &&
11868 Words[2] == "$0") {
11869 Words.clear();
11870 SplitString(AsmPieces[2], Words, " \t,");
11871 if (Words.size() == 3 && Words[0] == "rorw" && Words[1] == "$$8" &&
11872 Words[2] == "${0:w}") {
11873 AsmPieces.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011874 const std::string &ConstraintsStr = IA->getConstraintString();
11875 SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
Peter Collingbourne948cf022010-11-13 19:54:30 +000011876 std::sort(AsmPieces.begin(), AsmPieces.end());
11877 if (AsmPieces.size() == 4 &&
11878 AsmPieces[0] == "~{cc}" &&
11879 AsmPieces[1] == "~{dirflag}" &&
11880 AsmPieces[2] == "~{flags}" &&
11881 AsmPieces[3] == "~{fpsr}") {
Evan Cheng55d42002011-01-08 01:24:27 +000011882 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11883 if (!Ty || Ty->getBitWidth() % 16 != 0)
11884 return false;
11885 return IntrinsicLowering::LowerToByteSwap(CI);
Peter Collingbourne948cf022010-11-13 19:54:30 +000011886 }
11887 }
11888 }
11889 }
11890 }
Evan Cheng55d42002011-01-08 01:24:27 +000011891
11892 if (CI->getType()->isIntegerTy(64)) {
11893 InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
11894 if (Constraints.size() >= 2 &&
11895 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
11896 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
11897 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
11898 SmallVector<StringRef, 4> Words;
11899 SplitString(AsmPieces[0], Words, " \t");
11900 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
Chris Lattnerb8105652009-07-20 17:51:36 +000011901 Words.clear();
Evan Cheng55d42002011-01-08 01:24:27 +000011902 SplitString(AsmPieces[1], Words, " \t");
11903 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
11904 Words.clear();
11905 SplitString(AsmPieces[2], Words, " \t,");
11906 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
11907 Words[2] == "%edx") {
11908 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
11909 if (!Ty || Ty->getBitWidth() % 16 != 0)
11910 return false;
11911 return IntrinsicLowering::LowerToByteSwap(CI);
11912 }
Chris Lattnerb8105652009-07-20 17:51:36 +000011913 }
11914 }
11915 }
11916 }
11917 break;
11918 }
11919 return false;
11920}
11921
11922
11923
Chris Lattnerf4dff842006-07-11 02:54:03 +000011924/// getConstraintType - Given a constraint letter, return the type of
11925/// constraint it is for this target.
11926X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000011927X86TargetLowering::getConstraintType(const std::string &Constraint) const {
11928 if (Constraint.size() == 1) {
11929 switch (Constraint[0]) {
Chris Lattner4234f572007-03-25 02:14:49 +000011930 case 'R':
Chris Lattner4234f572007-03-25 02:14:49 +000011931 case 'q':
11932 case 'Q':
John Thompson44ab89e2010-10-29 17:29:13 +000011933 case 'f':
11934 case 't':
11935 case 'u':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000011936 case 'y':
John Thompson44ab89e2010-10-29 17:29:13 +000011937 case 'x':
Chris Lattner4234f572007-03-25 02:14:49 +000011938 case 'Y':
11939 return C_RegisterClass;
John Thompson44ab89e2010-10-29 17:29:13 +000011940 case 'a':
11941 case 'b':
11942 case 'c':
11943 case 'd':
11944 case 'S':
11945 case 'D':
11946 case 'A':
11947 return C_Register;
11948 case 'I':
11949 case 'J':
11950 case 'K':
11951 case 'L':
11952 case 'M':
11953 case 'N':
11954 case 'G':
11955 case 'C':
Dale Johannesen78e3e522009-02-12 20:58:09 +000011956 case 'e':
11957 case 'Z':
11958 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000011959 default:
11960 break;
11961 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000011962 }
Chris Lattner4234f572007-03-25 02:14:49 +000011963 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000011964}
11965
John Thompson44ab89e2010-10-29 17:29:13 +000011966/// Examine constraint type and operand type and determine a weight value.
John Thompsoneac6e1d2010-09-13 18:15:37 +000011967/// This object must already have been set up with the operand type
11968/// and the current alternative constraint selected.
John Thompson44ab89e2010-10-29 17:29:13 +000011969TargetLowering::ConstraintWeight
11970 X86TargetLowering::getSingleConstraintMatchWeight(
John Thompsoneac6e1d2010-09-13 18:15:37 +000011971 AsmOperandInfo &info, const char *constraint) const {
John Thompson44ab89e2010-10-29 17:29:13 +000011972 ConstraintWeight weight = CW_Invalid;
John Thompsoneac6e1d2010-09-13 18:15:37 +000011973 Value *CallOperandVal = info.CallOperandVal;
11974 // If we don't have a value, we can't do a match,
11975 // but allow it at the lowest weight.
11976 if (CallOperandVal == NULL)
John Thompson44ab89e2010-10-29 17:29:13 +000011977 return CW_Default;
11978 const Type *type = CallOperandVal->getType();
John Thompsoneac6e1d2010-09-13 18:15:37 +000011979 // Look at the constraint type.
11980 switch (*constraint) {
11981 default:
John Thompson44ab89e2010-10-29 17:29:13 +000011982 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11983 case 'R':
11984 case 'q':
11985 case 'Q':
11986 case 'a':
11987 case 'b':
11988 case 'c':
11989 case 'd':
11990 case 'S':
11991 case 'D':
11992 case 'A':
11993 if (CallOperandVal->getType()->isIntegerTy())
11994 weight = CW_SpecificReg;
11995 break;
11996 case 'f':
11997 case 't':
11998 case 'u':
11999 if (type->isFloatingPointTy())
12000 weight = CW_SpecificReg;
12001 break;
12002 case 'y':
Chris Lattner2a786eb2010-12-19 20:19:20 +000012003 if (type->isX86_MMXTy() && Subtarget->hasMMX())
John Thompson44ab89e2010-10-29 17:29:13 +000012004 weight = CW_SpecificReg;
12005 break;
12006 case 'x':
12007 case 'Y':
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012008 if ((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasXMM())
John Thompson44ab89e2010-10-29 17:29:13 +000012009 weight = CW_Register;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012010 break;
12011 case 'I':
12012 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
12013 if (C->getZExtValue() <= 31)
John Thompson44ab89e2010-10-29 17:29:13 +000012014 weight = CW_Constant;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012015 }
12016 break;
John Thompson44ab89e2010-10-29 17:29:13 +000012017 case 'J':
12018 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12019 if (C->getZExtValue() <= 63)
12020 weight = CW_Constant;
12021 }
12022 break;
12023 case 'K':
12024 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12025 if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
12026 weight = CW_Constant;
12027 }
12028 break;
12029 case 'L':
12030 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12031 if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
12032 weight = CW_Constant;
12033 }
12034 break;
12035 case 'M':
12036 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12037 if (C->getZExtValue() <= 3)
12038 weight = CW_Constant;
12039 }
12040 break;
12041 case 'N':
12042 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12043 if (C->getZExtValue() <= 0xff)
12044 weight = CW_Constant;
12045 }
12046 break;
12047 case 'G':
12048 case 'C':
12049 if (dyn_cast<ConstantFP>(CallOperandVal)) {
12050 weight = CW_Constant;
12051 }
12052 break;
12053 case 'e':
12054 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12055 if ((C->getSExtValue() >= -0x80000000LL) &&
12056 (C->getSExtValue() <= 0x7fffffffLL))
12057 weight = CW_Constant;
12058 }
12059 break;
12060 case 'Z':
12061 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
12062 if (C->getZExtValue() <= 0xffffffff)
12063 weight = CW_Constant;
12064 }
12065 break;
John Thompsoneac6e1d2010-09-13 18:15:37 +000012066 }
12067 return weight;
12068}
12069
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012070/// LowerXConstraint - try to replace an X constraint, which matches anything,
12071/// with another that has more specific requirements based on the type of the
12072/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000012073const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000012074LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000012075 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
12076 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000012077 if (ConstraintVT.isFloatingPoint()) {
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012078 if (Subtarget->hasXMMInt())
Chris Lattner5e764232008-04-26 23:02:14 +000012079 return "Y";
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012080 if (Subtarget->hasXMM())
Chris Lattner5e764232008-04-26 23:02:14 +000012081 return "x";
12082 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012083
Chris Lattner5e764232008-04-26 23:02:14 +000012084 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000012085}
12086
Chris Lattner48884cd2007-08-25 00:47:38 +000012087/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
12088/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000012089void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000012090 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +000012091 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000012092 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000012093 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000012094
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012095 switch (Constraint) {
12096 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000012097 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000012098 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012099 if (C->getZExtValue() <= 31) {
12100 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012101 break;
12102 }
Devang Patel84f7fd22007-03-17 00:13:28 +000012103 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012104 return;
Evan Cheng364091e2008-09-22 23:57:37 +000012105 case 'J':
12106 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012107 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000012108 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12109 break;
12110 }
12111 }
12112 return;
12113 case 'K':
12114 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000012115 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000012116 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12117 break;
12118 }
12119 }
12120 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000012121 case 'N':
12122 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000012123 if (C->getZExtValue() <= 255) {
12124 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000012125 break;
12126 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000012127 }
Chris Lattner48884cd2007-08-25 00:47:38 +000012128 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000012129 case 'e': {
12130 // 32-bit signed value
12131 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012132 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12133 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012134 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012135 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000012136 break;
12137 }
12138 // FIXME gcc accepts some relocatable values here too, but only in certain
12139 // memory models; it's complicated.
12140 }
12141 return;
12142 }
12143 case 'Z': {
12144 // 32-bit unsigned value
12145 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohman7720cb32010-06-18 14:01:07 +000012146 if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
12147 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012148 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
12149 break;
12150 }
12151 }
12152 // FIXME gcc accepts some relocatable values here too, but only in certain
12153 // memory models; it's complicated.
12154 return;
12155 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012156 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012157 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000012158 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000012159 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000012160 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000012161 break;
12162 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012163
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012164 // In any sort of PIC mode addresses need to be computed at runtime by
12165 // adding in a register or some sort of table lookup. These can't
12166 // be used as immediates.
Dale Johannesene2b448c2010-07-06 23:27:00 +000012167 if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
Dale Johannesene5ff9ef2010-06-24 20:14:51 +000012168 return;
12169
Chris Lattnerdc43a882007-05-03 16:52:29 +000012170 // If we are in non-pic codegen mode, we allow the address of a global (with
12171 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000012172 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012173 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000012174
Chris Lattner49921962009-05-08 18:23:14 +000012175 // Match either (GA), (GA+C), (GA+C1+C2), etc.
12176 while (1) {
12177 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
12178 Offset += GA->getOffset();
12179 break;
12180 } else if (Op.getOpcode() == ISD::ADD) {
12181 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12182 Offset += C->getZExtValue();
12183 Op = Op.getOperand(0);
12184 continue;
12185 }
12186 } else if (Op.getOpcode() == ISD::SUB) {
12187 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
12188 Offset += -C->getZExtValue();
12189 Op = Op.getOperand(0);
12190 continue;
12191 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012192 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012193
Chris Lattner49921962009-05-08 18:23:14 +000012194 // Otherwise, this isn't something we can handle, reject it.
12195 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000012196 }
Eric Christopherfd179292009-08-27 18:07:15 +000012197
Dan Gohman46510a72010-04-15 01:51:59 +000012198 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012199 // If we require an extra load to get this address, as in PIC mode, we
12200 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000012201 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
12202 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000012203 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000012204
Devang Patel0d881da2010-07-06 22:08:15 +000012205 Result = DAG.getTargetGlobalAddress(GV, Op.getDebugLoc(),
12206 GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000012207 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012208 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000012209 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012210
Gabor Greifba36cb52008-08-28 21:40:38 +000012211 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000012212 Ops.push_back(Result);
12213 return;
12214 }
Dale Johannesen1784d162010-06-25 21:55:36 +000012215 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000012216}
12217
Chris Lattner259e97c2006-01-31 19:43:35 +000012218std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000012219getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012220 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000012221 if (Constraint.size() == 1) {
12222 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000012223 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000012224 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000012225 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
12226 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012227 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012228 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
12229 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
12230 X86::R10D,X86::R11D,X86::R12D,
12231 X86::R13D,X86::R14D,X86::R15D,
12232 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012233 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012234 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
12235 X86::SI, X86::DI, X86::R8W,X86::R9W,
12236 X86::R10W,X86::R11W,X86::R12W,
12237 X86::R13W,X86::R14W,X86::R15W,
12238 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012239 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012240 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
12241 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
12242 X86::R10B,X86::R11B,X86::R12B,
12243 X86::R13B,X86::R14B,X86::R15B,
12244 X86::BPL, X86::SPL, 0);
12245
Owen Anderson825b72b2009-08-11 20:47:22 +000012246 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000012247 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
12248 X86::RSI, X86::RDI, X86::R8, X86::R9,
12249 X86::R10, X86::R11, X86::R12,
12250 X86::R13, X86::R14, X86::R15,
12251 X86::RBP, X86::RSP, 0);
12252
12253 break;
12254 }
Eric Christopherfd179292009-08-27 18:07:15 +000012255 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000012256 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012257 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012258 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012259 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000012260 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012261 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000012262 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000012263 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000012264 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
12265 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000012266 }
12267 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012268
Chris Lattner1efa40f2006-02-22 00:56:39 +000012269 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000012270}
Chris Lattnerf76d1802006-07-31 23:26:50 +000012271
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012272std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000012273X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000012274 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000012275 // First, see if this is a constraint that directly corresponds to an LLVM
12276 // register class.
12277 if (Constraint.size() == 1) {
12278 // GCC Constraint Letters
12279 switch (Constraint[0]) {
12280 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012281 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000012282 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000012283 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000012284 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012285 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000012286 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012287 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000012288 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000012289 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000012290 case 'R': // LEGACY_REGS
12291 if (VT == MVT::i8)
12292 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
12293 if (VT == MVT::i16)
12294 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
12295 if (VT == MVT::i32 || !Subtarget->is64Bit())
12296 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
12297 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012298 case 'f': // FP Stack registers.
12299 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
12300 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000012301 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012302 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012303 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000012304 return std::make_pair(0U, X86::RFP64RegisterClass);
12305 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000012306 case 'y': // MMX_REGS if MMX allowed.
12307 if (!Subtarget->hasMMX()) break;
12308 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012309 case 'Y': // SSE_REGS if SSE2 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012310 if (!Subtarget->hasXMMInt()) break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000012311 // FALL THROUGH.
12312 case 'x': // SSE_REGS if SSE1 allowed
Nate Begeman2ea8ee72010-12-10 00:26:57 +000012313 if (!Subtarget->hasXMM()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000012314
Owen Anderson825b72b2009-08-11 20:47:22 +000012315 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000012316 default: break;
12317 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012318 case MVT::f32:
12319 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000012320 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000012321 case MVT::f64:
12322 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000012323 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000012324 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000012325 case MVT::v16i8:
12326 case MVT::v8i16:
12327 case MVT::v4i32:
12328 case MVT::v2i64:
12329 case MVT::v4f32:
12330 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000012331 return std::make_pair(0U, X86::VR128RegisterClass);
12332 }
Chris Lattnerad043e82007-04-09 05:11:28 +000012333 break;
12334 }
12335 }
Scott Michelfdc40a02009-02-17 22:15:04 +000012336
Chris Lattnerf76d1802006-07-31 23:26:50 +000012337 // Use the default implementation in TargetLowering to convert the register
12338 // constraint into a member of a register class.
12339 std::pair<unsigned, const TargetRegisterClass*> Res;
12340 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000012341
12342 // Not found as a standard register?
12343 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012344 // Map st(0) -> st(7) -> ST0
12345 if (Constraint.size() == 7 && Constraint[0] == '{' &&
12346 tolower(Constraint[1]) == 's' &&
12347 tolower(Constraint[2]) == 't' &&
12348 Constraint[3] == '(' &&
12349 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
12350 Constraint[5] == ')' &&
12351 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000012352
Chris Lattner56d77c72009-09-13 22:41:48 +000012353 Res.first = X86::ST0+Constraint[4]-'0';
12354 Res.second = X86::RFP80RegisterClass;
12355 return Res;
12356 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012357
Chris Lattner56d77c72009-09-13 22:41:48 +000012358 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012359 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000012360 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000012361 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012362 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000012363 }
Chris Lattner56d77c72009-09-13 22:41:48 +000012364
12365 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000012366 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000012367 Res.first = X86::EFLAGS;
12368 Res.second = X86::CCRRegisterClass;
12369 return Res;
12370 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000012371
Dale Johannesen330169f2008-11-13 21:52:36 +000012372 // 'A' means EAX + EDX.
12373 if (Constraint == "A") {
12374 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000012375 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000012376 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000012377 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000012378 return Res;
12379 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012380
Chris Lattnerf76d1802006-07-31 23:26:50 +000012381 // Otherwise, check to see if this is a register class of the wrong value
12382 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
12383 // turn into {ax},{dx}.
12384 if (Res.second->hasType(VT))
12385 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012386
Chris Lattnerf76d1802006-07-31 23:26:50 +000012387 // All of the single-register GCC register classes map their values onto
12388 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
12389 // really want an 8-bit or 32-bit register, map to the appropriate register
12390 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000012391 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000012392 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012393 unsigned DestReg = 0;
12394 switch (Res.first) {
12395 default: break;
12396 case X86::AX: DestReg = X86::AL; break;
12397 case X86::DX: DestReg = X86::DL; break;
12398 case X86::CX: DestReg = X86::CL; break;
12399 case X86::BX: DestReg = X86::BL; break;
12400 }
12401 if (DestReg) {
12402 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012403 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012404 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012405 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012406 unsigned DestReg = 0;
12407 switch (Res.first) {
12408 default: break;
12409 case X86::AX: DestReg = X86::EAX; break;
12410 case X86::DX: DestReg = X86::EDX; break;
12411 case X86::CX: DestReg = X86::ECX; break;
12412 case X86::BX: DestReg = X86::EBX; break;
12413 case X86::SI: DestReg = X86::ESI; break;
12414 case X86::DI: DestReg = X86::EDI; break;
12415 case X86::BP: DestReg = X86::EBP; break;
12416 case X86::SP: DestReg = X86::ESP; break;
12417 }
12418 if (DestReg) {
12419 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012420 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012421 }
Owen Anderson825b72b2009-08-11 20:47:22 +000012422 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000012423 unsigned DestReg = 0;
12424 switch (Res.first) {
12425 default: break;
12426 case X86::AX: DestReg = X86::RAX; break;
12427 case X86::DX: DestReg = X86::RDX; break;
12428 case X86::CX: DestReg = X86::RCX; break;
12429 case X86::BX: DestReg = X86::RBX; break;
12430 case X86::SI: DestReg = X86::RSI; break;
12431 case X86::DI: DestReg = X86::RDI; break;
12432 case X86::BP: DestReg = X86::RBP; break;
12433 case X86::SP: DestReg = X86::RSP; break;
12434 }
12435 if (DestReg) {
12436 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000012437 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000012438 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000012439 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000012440 } else if (Res.second == X86::FR32RegisterClass ||
12441 Res.second == X86::FR64RegisterClass ||
12442 Res.second == X86::VR128RegisterClass) {
12443 // Handle references to XMM physical registers that got mapped into the
12444 // wrong class. This can happen with constraints like {xmm0} where the
12445 // target independent register mapper will just pick the first match it can
12446 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000012447 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012448 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000012449 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000012450 Res.second = X86::FR64RegisterClass;
12451 else if (X86::VR128RegisterClass->hasType(VT))
12452 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000012453 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000012454
Chris Lattnerf76d1802006-07-31 23:26:50 +000012455 return Res;
12456}