blob: 0fd87bd7447406ca400d60dfb8e72ab487352ff7 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
Evan Chengb1712452010-01-27 06:25:16 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000016#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000017#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000018#include "X86ISelLowering.h"
19#include "X86TargetMachine.h"
Chris Lattner8c6ed052009-09-16 01:46:41 +000020#include "X86TargetObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000022#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000024#include "llvm/GlobalAlias.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000025#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000026#include "llvm/Function.h"
Chris Lattnerb8105652009-07-20 17:51:36 +000027#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000028#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000029#include "llvm/LLVMContext.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner5e1df8d2010-01-25 23:38:14 +000033#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga844bde2008-02-02 04:07:54 +000034#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000035#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000036#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner589c6f62010-01-26 06:28:43 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000038#include "llvm/MC/MCContext.h"
Daniel Dunbar4e815f82010-03-15 23:51:06 +000039#include "llvm/MC/MCExpr.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000040#include "llvm/MC/MCSymbol.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000041#include "llvm/ADT/BitVector.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000042#include "llvm/ADT/SmallSet.h"
Evan Chengb1712452010-01-27 06:25:16 +000043#include "llvm/ADT/Statistic.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000045#include "llvm/ADT/VectorExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000046#include "llvm/Support/CommandLine.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000047#include "llvm/Support/Debug.h"
Bill Wendlingec041eb2010-03-12 19:20:40 +000048#include "llvm/Support/Dwarf.h"
Chris Lattnerc64daab2010-01-26 05:02:42 +000049#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000051#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052using namespace llvm;
Bill Wendlingec041eb2010-03-12 19:20:40 +000053using namespace dwarf;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054
Evan Chengb1712452010-01-27 06:25:16 +000055STATISTIC(NumTailCalls, "Number of tail calls");
56
Mon P Wang3c81d352008-11-23 04:37:22 +000057static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000058DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000059
Evan Cheng10e86422008-04-25 19:11:04 +000060// Forward declarations.
Owen Andersone50ed302009-08-10 22:56:29 +000061static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +000062 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000063
Chris Lattnerf0144122009-07-28 03:13:23 +000064static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
65 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
66 default: llvm_unreachable("unknown subtarget type");
67 case X86Subtarget::isDarwin:
Bill Wendling757e75b2010-03-15 19:04:37 +000068 if (TM.getSubtarget<X86Subtarget>().is64Bit())
69 return new X8664_MachoTargetObjectFile();
Anton Korobeynikov293d5922010-02-21 20:28:15 +000070 return new TargetLoweringObjectFileMachO();
Chris Lattnerf0144122009-07-28 03:13:23 +000071 case X86Subtarget::isELF:
Anton Korobeynikov9184b252010-02-15 22:35:59 +000072 if (TM.getSubtarget<X86Subtarget>().is64Bit())
73 return new X8664_ELFTargetObjectFile(TM);
74 return new X8632_ELFTargetObjectFile(TM);
Chris Lattnerf0144122009-07-28 03:13:23 +000075 case X86Subtarget::isMingw:
76 case X86Subtarget::isCygwin:
77 case X86Subtarget::isWindows:
78 return new TargetLoweringObjectFileCOFF();
79 }
Chris Lattnerf0144122009-07-28 03:13:23 +000080}
81
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000082X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000083 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000084 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000085 X86ScalarSSEf64 = Subtarget->hasSSE2();
86 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000087 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000088
Anton Korobeynikov2365f512007-07-14 14:06:15 +000089 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000090 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000091
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000092 // Set up the TargetLowering object.
93
94 // X86 is weird, it always uses i8 for shift amounts and setcc results.
Owen Anderson825b72b2009-08-11 20:47:22 +000095 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000096 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng211ffa12010-05-19 20:19:50 +000097 setSchedulingPreference(Sched::RegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000098 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000099
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000100 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +0000101 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000102 setUseUnderscoreSetJmp(false);
103 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000104 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +0000105 // MS runtime is weird: it exports _setjmp, but longjmp!
106 setUseUnderscoreSetJmp(true);
107 setUseUnderscoreLongJmp(false);
108 } else {
109 setUseUnderscoreSetJmp(true);
110 setUseUnderscoreLongJmp(true);
111 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000112
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000113 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000114 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
Dan Gohman71edb242010-04-30 18:30:26 +0000115 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000117 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000119
Owen Anderson825b72b2009-08-11 20:47:22 +0000120 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000121
Scott Michelfdc40a02009-02-17 22:15:04 +0000122 // We don't accept any truncstore of integer registers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000124 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000125 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
Dan Gohman71edb242010-04-30 18:30:26 +0000126 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
128 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000129
130 // SETOEQ and SETUNE require checking two conditions.
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
132 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
133 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
134 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
135 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
136 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000137
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000138 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
139 // operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
141 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
142 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000143
Evan Cheng25ab6902006-09-08 06:48:29 +0000144 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000145 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
146 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000147 } else if (!UseSoftFloat) {
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000148 // We have an algorithm for SSE2->double, and we turn this into a
149 // 64-bit FILD followed by conditional FADD for other targets.
150 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Eli Friedman948e95a2009-05-23 09:59:16 +0000151 // We have an algorithm for SSE2, and we turn this into a 64-bit
152 // FILD for other targets.
Dale Johannesen8d908eb2010-05-15 18:51:12 +0000153 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000154 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000155
156 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
157 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
159 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000160
Devang Patel6a784892009-06-05 18:48:29 +0000161 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000162 // SSE has no i16 to fp conversion, only i32
163 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000165 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000167 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
169 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000170 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000171 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000172 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
173 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000174 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000175
Dale Johannesen73328d12007-09-19 23:55:34 +0000176 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
177 // are Legal, f80 is custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000178 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
179 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000180
Evan Cheng02568ff2006-01-30 22:13:22 +0000181 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
182 // this operation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
184 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
Evan Cheng02568ff2006-01-30 22:13:22 +0000185
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000186 if (X86ScalarSSEf32) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000188 // f32 and f64 cases are Legal, f80 case is not
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000190 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
192 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000193 }
194
195 // Handle FP_TO_UINT by promoting the destination to a larger signed
196 // conversion.
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
198 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
199 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000200
Evan Cheng25ab6902006-09-08 06:48:29 +0000201 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
203 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000204 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000205 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000206 // Expand FP_TO_UINT into a select.
207 // FIXME: We would like to use a Custom expander here eventually to do
208 // the optimal thing for SSE vs. the default expansion in the legalizer.
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000210 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000211 // With SSE3 we can use fisttpll to convert to a signed i64; without
212 // SSE, we're stuck with a fistpll.
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000215
Chris Lattner399610a2006-12-05 18:22:22 +0000216 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenacbf6342010-05-21 18:44:47 +0000217 if (!X86ScalarSSEf64) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
219 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000220 if (Subtarget->is64Bit()) {
Dale Johannesen7d07b482010-05-21 00:52:33 +0000221 setOperationAction(ISD::BIT_CONVERT , MVT::f64 , Expand);
Dale Johannesene39859a2010-05-21 18:40:15 +0000222 // Without SSE, i64->f64 goes through memory; i64->MMX is Legal.
223 if (Subtarget->hasMMX() && !DisableMMX)
224 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Custom);
225 else
226 setOperationAction(ISD::BIT_CONVERT , MVT::i64 , Expand);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000227 }
Chris Lattnerf3597a12006-12-05 18:45:06 +0000228 }
Chris Lattner21f66852005-12-23 05:15:23 +0000229
Dan Gohmanb00ee212008-02-18 19:34:53 +0000230 // Scalar integer divide and remainder are lowered to use operations that
231 // produce two results, to match the available instructions. This exposes
232 // the two-result form to trivial CSE, which is able to combine x/y and x%y
233 // into a single instruction.
234 //
235 // Scalar integer multiply-high is also lowered to use two-result
236 // operations, to match the available instructions. However, plain multiply
237 // (low) operations are left as Legal, as there are single-result
238 // instructions for this in x86. Using the two-result multiply instructions
239 // when both high and low results are needed must be arranged by dagcombine.
Owen Anderson825b72b2009-08-11 20:47:22 +0000240 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
241 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
242 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
243 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
244 setOperationAction(ISD::SREM , MVT::i8 , Expand);
245 setOperationAction(ISD::UREM , MVT::i8 , Expand);
246 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
247 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
248 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
249 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
250 setOperationAction(ISD::SREM , MVT::i16 , Expand);
251 setOperationAction(ISD::UREM , MVT::i16 , Expand);
252 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
253 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
254 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
255 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
256 setOperationAction(ISD::SREM , MVT::i32 , Expand);
257 setOperationAction(ISD::UREM , MVT::i32 , Expand);
258 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
259 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
260 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
261 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
262 setOperationAction(ISD::SREM , MVT::i64 , Expand);
263 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000264
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
266 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
267 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
268 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000269 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000270 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
271 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
272 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
273 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
274 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
275 setOperationAction(ISD::FREM , MVT::f32 , Expand);
276 setOperationAction(ISD::FREM , MVT::f64 , Expand);
277 setOperationAction(ISD::FREM , MVT::f80 , Expand);
278 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000279
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
281 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
282 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
283 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
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::CTPOP , MVT::i32 , Expand);
287 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
288 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000290 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
291 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
292 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000293 }
294
Owen Anderson825b72b2009-08-11 20:47:22 +0000295 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
296 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000297
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000298 // These should be promoted to a larger select which is supported.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000299 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000300 // X86 wants to expand cmov itself.
Dan Gohmancbbea0f2009-08-27 00:14:12 +0000301 setOperationAction(ISD::SELECT , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000302 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
304 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
305 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
306 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
307 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
Dan Gohman71edb242010-04-30 18:30:26 +0000308 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
Owen Anderson825b72b2009-08-11 20:47:22 +0000309 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
310 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
311 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
312 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000314 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
315 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000316 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000317 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000318
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000319 // Darwin ABI issue.
Owen Anderson825b72b2009-08-11 20:47:22 +0000320 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
321 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
322 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
323 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000324 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
326 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000327 setOperationAction(ISD::BlockAddress , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000328 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
330 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
331 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
332 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Dan Gohmanf705adb2009-10-30 01:28:02 +0000333 setOperationAction(ISD::BlockAddress , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000334 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000335 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
337 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
338 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000339 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
341 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
342 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000343 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000344
Evan Chengd2cde682008-03-10 19:38:10 +0000345 if (Subtarget->hasSSE1())
Owen Anderson825b72b2009-08-11 20:47:22 +0000346 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000347
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000348 if (!Subtarget->hasSSE2())
Owen Anderson825b72b2009-08-11 20:47:22 +0000349 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000350
Mon P Wang63307c32008-05-05 19:05:59 +0000351 // Expand certain atomics
Owen Anderson825b72b2009-08-11 20:47:22 +0000352 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
353 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
354 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
355 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000356
Owen Anderson825b72b2009-08-11 20:47:22 +0000357 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
358 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
359 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
360 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000361
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000362 if (!Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000363 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
364 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
365 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
366 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
367 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
368 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
369 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000370 }
371
Evan Cheng3c992d22006-03-07 02:02:57 +0000372 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000373 if (!Subtarget->isTargetDarwin() &&
374 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000375 !Subtarget->isTargetCygMing()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000376 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000377 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000378
Owen Anderson825b72b2009-08-11 20:47:22 +0000379 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
380 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
381 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
382 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000383 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000384 setExceptionPointerRegister(X86::RAX);
385 setExceptionSelectorRegister(X86::RDX);
386 } else {
387 setExceptionPointerRegister(X86::EAX);
388 setExceptionSelectorRegister(X86::EDX);
389 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
391 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000392
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000394
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000396
Nate Begemanacc398c2006-01-25 18:21:52 +0000397 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
Owen Anderson825b72b2009-08-11 20:47:22 +0000398 setOperationAction(ISD::VASTART , MVT::Other, Custom);
399 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000400 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000401 setOperationAction(ISD::VAARG , MVT::Other, Custom);
402 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000403 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000404 setOperationAction(ISD::VAARG , MVT::Other, Expand);
405 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000406 }
Evan Chengae642192007-03-02 23:16:35 +0000407
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
409 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000410 if (Subtarget->is64Bit())
Owen Anderson825b72b2009-08-11 20:47:22 +0000411 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000412 if (Subtarget->isTargetCygMing())
Owen Anderson825b72b2009-08-11 20:47:22 +0000413 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000414 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000415 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000416
Evan Chengc7ce29b2009-02-13 22:36:38 +0000417 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000418 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000419 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000420 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
421 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000422
Evan Cheng223547a2006-01-31 22:28:30 +0000423 // Use ANDPD to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000424 setOperationAction(ISD::FABS , MVT::f64, Custom);
425 setOperationAction(ISD::FABS , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000426
427 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000428 setOperationAction(ISD::FNEG , MVT::f64, Custom);
429 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Evan Cheng223547a2006-01-31 22:28:30 +0000430
Evan Cheng68c47cb2007-01-05 07:55:56 +0000431 // Use ANDPD and ORPD to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
433 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000434
Evan Chengd25e9e82006-02-02 00:28:23 +0000435 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 setOperationAction(ISD::FSIN , MVT::f64, Expand);
437 setOperationAction(ISD::FCOS , MVT::f64, Expand);
438 setOperationAction(ISD::FSIN , MVT::f32, Expand);
439 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000440
Chris Lattnera54aa942006-01-29 06:26:08 +0000441 // Expand FP immediates into loads from the stack, except for the special
442 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000443 addLegalFPImmediate(APFloat(+0.0)); // xorpd
444 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000445 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000446 // Use SSE for f32, x87 for f64.
447 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
449 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000450
451 // Use ANDPS to simulate FABS.
Owen Anderson825b72b2009-08-11 20:47:22 +0000452 setOperationAction(ISD::FABS , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000453
454 // Use XORP to simulate FNEG.
Owen Anderson825b72b2009-08-11 20:47:22 +0000455 setOperationAction(ISD::FNEG , MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000456
Owen Anderson825b72b2009-08-11 20:47:22 +0000457 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000458
459 // Use ANDPS and ORPS to simulate FCOPYSIGN.
Owen Anderson825b72b2009-08-11 20:47:22 +0000460 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
461 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462
463 // We don't support sin/cos/fmod
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 setOperationAction(ISD::FSIN , MVT::f32, Expand);
465 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000466
Nate Begemane1795842008-02-14 08:57:00 +0000467 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000468 addLegalFPImmediate(APFloat(+0.0f)); // xorps
469 addLegalFPImmediate(APFloat(+0.0)); // FLD0
470 addLegalFPImmediate(APFloat(+1.0)); // FLD1
471 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
472 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
473
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000474 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000475 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
476 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000477 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000478 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000479 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000480 // Set up the FP register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000481 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
482 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000483
Owen Anderson825b72b2009-08-11 20:47:22 +0000484 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
485 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
486 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
487 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000488
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000489 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000490 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
491 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000492 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000493 addLegalFPImmediate(APFloat(+0.0)); // FLD0
494 addLegalFPImmediate(APFloat(+1.0)); // FLD1
495 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
496 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000497 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
498 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
499 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
500 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000501 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000502
Dale Johannesen59a58732007-08-05 18:49:15 +0000503 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000504 if (!UseSoftFloat) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
506 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
507 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000508 {
509 bool ignored;
510 APFloat TmpFlt(+0.0);
511 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
512 &ignored);
513 addLegalFPImmediate(TmpFlt); // FLD0
514 TmpFlt.changeSign();
515 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
516 APFloat TmpFlt2(+1.0);
517 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
518 &ignored);
519 addLegalFPImmediate(TmpFlt2); // FLD1
520 TmpFlt2.changeSign();
521 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
522 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000523
Evan Chengc7ce29b2009-02-13 22:36:38 +0000524 if (!UnsafeFPMath) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000525 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
526 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000527 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000528 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000529
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000530 // Always use a library call for pow.
Owen Anderson825b72b2009-08-11 20:47:22 +0000531 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
532 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
533 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000534
Owen Anderson825b72b2009-08-11 20:47:22 +0000535 setOperationAction(ISD::FLOG, MVT::f80, Expand);
536 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
537 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
538 setOperationAction(ISD::FEXP, MVT::f80, Expand);
539 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000540
Mon P Wangf007a8b2008-11-06 05:31:54 +0000541 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000542 // (for widening) or expand (for scalarization). Then we will selectively
543 // turn on ones that can be effectively codegen'd.
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
545 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
546 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
561 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
562 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
571 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
576 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
577 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
578 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
579 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
580 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
581 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
582 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
583 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
584 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
585 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
586 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
587 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
588 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
589 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
590 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
591 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
592 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
593 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Dan Gohman87862e72009-12-11 21:31:27 +0000594 setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT,Expand);
Dan Gohman2e141d72009-12-14 23:40:38 +0000595 setOperationAction(ISD::TRUNCATE, (MVT::SimpleValueType)VT, Expand);
596 setOperationAction(ISD::SIGN_EXTEND, (MVT::SimpleValueType)VT, Expand);
597 setOperationAction(ISD::ZERO_EXTEND, (MVT::SimpleValueType)VT, Expand);
598 setOperationAction(ISD::ANY_EXTEND, (MVT::SimpleValueType)VT, Expand);
599 for (unsigned InnerVT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
600 InnerVT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++InnerVT)
601 setTruncStoreAction((MVT::SimpleValueType)VT,
602 (MVT::SimpleValueType)InnerVT, Expand);
603 setLoadExtAction(ISD::SEXTLOAD, (MVT::SimpleValueType)VT, Expand);
604 setLoadExtAction(ISD::ZEXTLOAD, (MVT::SimpleValueType)VT, Expand);
605 setLoadExtAction(ISD::EXTLOAD, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000606 }
607
Evan Chengc7ce29b2009-02-13 22:36:38 +0000608 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
609 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000610 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Dale Johannesen76090172010-04-20 22:34:09 +0000611 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass, false);
612 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass, false);
613 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass, false);
614 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass, false);
615 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass, false);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000616
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
618 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
619 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
620 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000621
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
623 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
624 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
625 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000626
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
628 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
Bill Wendling74027e92007-03-15 21:24:36 +0000629
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::AND, MVT::v8i8, Promote);
631 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
632 setOperationAction(ISD::AND, MVT::v4i16, Promote);
633 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
634 setOperationAction(ISD::AND, MVT::v2i32, Promote);
635 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
636 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000637
Owen Anderson825b72b2009-08-11 20:47:22 +0000638 setOperationAction(ISD::OR, MVT::v8i8, Promote);
639 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
640 setOperationAction(ISD::OR, MVT::v4i16, Promote);
641 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
642 setOperationAction(ISD::OR, MVT::v2i32, Promote);
643 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
644 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000645
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
647 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
648 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
649 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
650 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
651 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
652 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000653
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
655 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
656 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
657 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
658 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
659 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
660 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
661 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
662 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000663
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
665 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
666 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
667 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
668 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000669
Owen Anderson825b72b2009-08-11 20:47:22 +0000670 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
671 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
672 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
673 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000674
Owen Anderson825b72b2009-08-11 20:47:22 +0000675 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
676 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
677 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
678 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000679
Owen Anderson825b72b2009-08-11 20:47:22 +0000680 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000681
Owen Anderson825b72b2009-08-11 20:47:22 +0000682 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
683 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
684 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
685 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
686 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
687 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
688 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Dale Johannesen7d07b482010-05-21 00:52:33 +0000689
690 if (!X86ScalarSSEf64 && Subtarget->is64Bit()) {
691 setOperationAction(ISD::BIT_CONVERT, MVT::v8i8, Custom);
692 setOperationAction(ISD::BIT_CONVERT, MVT::v4i16, Custom);
693 setOperationAction(ISD::BIT_CONVERT, MVT::v2i32, Custom);
694 setOperationAction(ISD::BIT_CONVERT, MVT::v2f32, Custom);
695 setOperationAction(ISD::BIT_CONVERT, MVT::v1i64, Custom);
696 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000697 }
698
Evan Cheng92722532009-03-26 23:06:32 +0000699 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000701
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
703 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
704 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
705 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
706 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
707 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
708 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
709 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
710 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
711 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
712 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
713 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000714 }
715
Evan Cheng92722532009-03-26 23:06:32 +0000716 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000717 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000718
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000719 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
720 // registers cannot be used even for integer operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
722 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
723 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
724 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000725
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
727 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
728 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
729 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
730 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
731 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
732 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
733 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
734 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
735 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
736 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
737 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
738 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
739 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
740 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
741 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000742
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
744 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
745 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
746 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000747
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
749 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
750 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
751 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
752 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000753
Mon P Wangeb38ebf2010-01-24 00:05:03 +0000754 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2f64, Custom);
755 setOperationAction(ISD::CONCAT_VECTORS, MVT::v2i64, Custom);
756 setOperationAction(ISD::CONCAT_VECTORS, MVT::v16i8, Custom);
757 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i16, Custom);
758 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
759
Evan Cheng2c3ae372006-04-12 21:21:57 +0000760 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Owen Anderson825b72b2009-08-11 20:47:22 +0000761 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
762 EVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000763 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000764 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000765 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000766 // Do not attempt to custom lower non-128-bit vectors
767 if (!VT.is128BitVector())
768 continue;
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::BUILD_VECTOR,
770 VT.getSimpleVT().SimpleTy, Custom);
771 setOperationAction(ISD::VECTOR_SHUFFLE,
772 VT.getSimpleVT().SimpleTy, Custom);
773 setOperationAction(ISD::EXTRACT_VECTOR_ELT,
774 VT.getSimpleVT().SimpleTy, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000775 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000776
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
778 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
779 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
780 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
781 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
782 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000783
Nate Begemancdd1eec2008-02-12 22:51:28 +0000784 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
786 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000787 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000788
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000789 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
791 MVT::SimpleValueType SVT = (MVT::SimpleValueType)i;
Owen Andersone50ed302009-08-10 22:56:29 +0000792 EVT VT = SVT;
David Greene9b9838d2009-06-29 16:47:10 +0000793
794 // Do not attempt to promote non-128-bit vectors
795 if (!VT.is128BitVector()) {
796 continue;
797 }
Eric Christopher4bd24c22010-03-30 01:04:59 +0000798
Owen Andersond6662ad2009-08-10 20:46:15 +0000799 setOperationAction(ISD::AND, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 AddPromotedToType (ISD::AND, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000801 setOperationAction(ISD::OR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000802 AddPromotedToType (ISD::OR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000803 setOperationAction(ISD::XOR, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 AddPromotedToType (ISD::XOR, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000805 setOperationAction(ISD::LOAD, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000806 AddPromotedToType (ISD::LOAD, SVT, MVT::v2i64);
Owen Andersond6662ad2009-08-10 20:46:15 +0000807 setOperationAction(ISD::SELECT, SVT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 AddPromotedToType (ISD::SELECT, SVT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000809 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000810
Owen Anderson825b72b2009-08-11 20:47:22 +0000811 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000812
Evan Cheng2c3ae372006-04-12 21:21:57 +0000813 // Custom lower v2i64 and v2f64 selects.
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
815 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
816 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
817 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000818
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
820 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
Eli Friedman23ef1052009-06-06 03:57:58 +0000821 if (!DisableMMX && Subtarget->hasMMX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000822 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
823 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
Eli Friedman23ef1052009-06-06 03:57:58 +0000824 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000825 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000826
Nate Begeman14d12ca2008-02-11 04:19:36 +0000827 if (Subtarget->hasSSE41()) {
Dale Johannesen54feef22010-05-27 20:12:41 +0000828 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
829 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
830 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
831 setOperationAction(ISD::FRINT, MVT::f32, Legal);
832 setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal);
833 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
834 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
835 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
836 setOperationAction(ISD::FRINT, MVT::f64, Legal);
837 setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal);
838
Nate Begeman14d12ca2008-02-11 04:19:36 +0000839 // FIXME: Do we need to handle scalar-to-vector here?
Owen Anderson825b72b2009-08-11 20:47:22 +0000840 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000841
842 // i8 and i16 vectors are custom , because the source register and source
843 // source memory operand types are not the same width. f32 vectors are
844 // custom since the immediate controlling the insert encodes additional
845 // information.
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
847 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
848 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
849 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000850
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
852 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
853 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
854 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000855
856 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
858 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000859 }
860 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000861
Nate Begeman30a0de92008-07-17 16:51:19 +0000862 if (Subtarget->hasSSE42()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000863 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000864 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000865
David Greene9b9838d2009-06-29 16:47:10 +0000866 if (!UseSoftFloat && Subtarget->hasAVX()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000867 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
868 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
869 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
870 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
David Greened94c1012009-06-29 22:50:51 +0000871
Owen Anderson825b72b2009-08-11 20:47:22 +0000872 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
873 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
874 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
875 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
876 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
877 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
878 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
879 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
880 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
881 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
882 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
883 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
884 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
885 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
886 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000887
888 // Operations to consider commented out -v16i16 v32i8
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
890 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
891 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
892 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
893 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
894 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
895 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
896 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
897 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
898 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
899 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
900 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
901 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
902 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000903
Owen Anderson825b72b2009-08-11 20:47:22 +0000904 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
905 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
906 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
907 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000908
Owen Anderson825b72b2009-08-11 20:47:22 +0000909 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
910 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
911 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
912 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
913 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000914
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
916 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
917 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
918 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
919 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
920 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
David Greene9b9838d2009-06-29 16:47:10 +0000921
922#if 0
923 // Not sure we want to do this since there are no 256-bit integer
924 // operations in AVX
925
926 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
927 // This includes 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
929 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000930
931 // Do not attempt to custom lower non-power-of-2 vectors
932 if (!isPowerOf2_32(VT.getVectorNumElements()))
933 continue;
934
935 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
936 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
937 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
938 }
939
940 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
942 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
Eric Christopherfd179292009-08-27 18:07:15 +0000943 }
David Greene9b9838d2009-06-29 16:47:10 +0000944#endif
945
946#if 0
947 // Not sure we want to do this since there are no 256-bit integer
948 // operations in AVX
949
950 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
951 // Including 256-bit vectors
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
953 EVT VT = (MVT::SimpleValueType)i;
David Greene9b9838d2009-06-29 16:47:10 +0000954
955 if (!VT.is256BitVector()) {
956 continue;
957 }
958 setOperationAction(ISD::AND, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000960 setOperationAction(ISD::OR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000961 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000962 setOperationAction(ISD::XOR, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000963 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000964 setOperationAction(ISD::LOAD, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000965 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000966 setOperationAction(ISD::SELECT, VT, Promote);
Owen Anderson825b72b2009-08-11 20:47:22 +0000967 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
David Greene9b9838d2009-06-29 16:47:10 +0000968 }
969
Owen Anderson825b72b2009-08-11 20:47:22 +0000970 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
David Greene9b9838d2009-06-29 16:47:10 +0000971#endif
972 }
973
Evan Cheng6be2c582006-04-05 23:38:46 +0000974 // We want to custom lower some of our intrinsics.
Owen Anderson825b72b2009-08-11 20:47:22 +0000975 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Evan Cheng6be2c582006-04-05 23:38:46 +0000976
Bill Wendling74c37652008-12-09 22:08:41 +0000977 // Add/Sub/Mul with overflow operations are custom lowered.
Owen Anderson825b72b2009-08-11 20:47:22 +0000978 setOperationAction(ISD::SADDO, MVT::i32, Custom);
979 setOperationAction(ISD::SADDO, MVT::i64, Custom);
980 setOperationAction(ISD::UADDO, MVT::i32, Custom);
981 setOperationAction(ISD::UADDO, MVT::i64, Custom);
982 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
983 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
984 setOperationAction(ISD::USUBO, MVT::i32, Custom);
985 setOperationAction(ISD::USUBO, MVT::i64, Custom);
986 setOperationAction(ISD::SMULO, MVT::i32, Custom);
987 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000988
Evan Chengd54f2d52009-03-31 19:38:51 +0000989 if (!Subtarget->is64Bit()) {
990 // These libcalls are not available in 32-bit.
991 setLibcallName(RTLIB::SHL_I128, 0);
992 setLibcallName(RTLIB::SRL_I128, 0);
993 setLibcallName(RTLIB::SRA_I128, 0);
994 }
995
Evan Cheng206ee9d2006-07-07 08:33:52 +0000996 // We have target-specific dag combine patterns for the following nodes:
997 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Dan Gohman1bbf72b2010-03-15 23:23:03 +0000998 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Evan Chengd880b972008-05-09 21:53:03 +0000999 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +00001000 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +00001001 setTargetDAGCombine(ISD::SHL);
1002 setTargetDAGCombine(ISD::SRA);
1003 setTargetDAGCombine(ISD::SRL);
Evan Cheng760d1942010-01-04 21:22:48 +00001004 setTargetDAGCombine(ISD::OR);
Chris Lattner149a4e52008-02-22 02:09:43 +00001005 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +00001006 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng2e489c42009-12-16 00:53:11 +00001007 setTargetDAGCombine(ISD::ZERO_EXTEND);
Evan Cheng0b0cd912009-03-28 05:57:29 +00001008 if (Subtarget->is64Bit())
1009 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +00001010
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001011 computeRegisterProperties();
1012
Evan Cheng87ed7162006-02-14 08:25:08 +00001013 // FIXME: These should be based on subtarget info. Plus, the values should
1014 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +00001015 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
Evan Cheng255f20f2010-04-01 06:04:33 +00001016 maxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
Dan Gohman87060f52008-06-30 21:00:56 +00001017 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Evan Chengfb8075d2008-02-28 00:43:03 +00001018 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +00001019 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001020}
1021
Scott Michel5b8f82e2008-03-10 15:42:14 +00001022
Owen Anderson825b72b2009-08-11 20:47:22 +00001023MVT::SimpleValueType X86TargetLowering::getSetCCResultType(EVT VT) const {
1024 return MVT::i8;
Scott Michel5b8f82e2008-03-10 15:42:14 +00001025}
1026
1027
Evan Cheng29286502008-01-23 23:17:41 +00001028/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
1029/// the desired ByVal argument alignment.
1030static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
1031 if (MaxAlign == 16)
1032 return;
1033 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1034 if (VTy->getBitWidth() == 128)
1035 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +00001036 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1037 unsigned EltAlign = 0;
1038 getMaxByValAlign(ATy->getElementType(), EltAlign);
1039 if (EltAlign > MaxAlign)
1040 MaxAlign = EltAlign;
1041 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1042 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1043 unsigned EltAlign = 0;
1044 getMaxByValAlign(STy->getElementType(i), EltAlign);
1045 if (EltAlign > MaxAlign)
1046 MaxAlign = EltAlign;
1047 if (MaxAlign == 16)
1048 break;
1049 }
1050 }
1051 return;
1052}
1053
1054/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
1055/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +00001056/// that contain SSE vectors are placed at 16-byte boundaries while the rest
1057/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +00001058unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +00001059 if (Subtarget->is64Bit()) {
1060 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001061 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001062 if (TyAlign > 8)
1063 return TyAlign;
1064 return 8;
1065 }
1066
Evan Cheng29286502008-01-23 23:17:41 +00001067 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001068 if (Subtarget->hasSSE1())
1069 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001070 return Align;
1071}
Chris Lattner2b02a442007-02-25 08:29:00 +00001072
Evan Chengf0df0312008-05-15 08:39:06 +00001073/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Chengc3b0c342010-04-08 07:37:57 +00001074/// and store operations as a result of memset, memcpy, and memmove
1075/// lowering. If DstAlign is zero that means it's safe to destination
1076/// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1077/// means there isn't a need to check it against alignment requirement,
1078/// probably because the source does not need to be loaded. If
1079/// 'NonScalarIntSafe' is true, that means it's safe to return a
1080/// non-scalar-integer type, e.g. empty string source, constant, or loaded
1081/// from memory. 'MemcpyStrSrc' indicates whether the memcpy source is
1082/// constant so it does not need to be loaded.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001083/// It returns EVT::Other if the type should be determined using generic
1084/// target-independent logic.
Owen Andersone50ed302009-08-10 22:56:29 +00001085EVT
Evan Cheng255f20f2010-04-01 06:04:33 +00001086X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1087 unsigned DstAlign, unsigned SrcAlign,
Evan Chengf28f8bc2010-04-02 19:36:14 +00001088 bool NonScalarIntSafe,
Evan Chengc3b0c342010-04-08 07:37:57 +00001089 bool MemcpyStrSrc,
Dan Gohman37f32ee2010-04-16 20:11:05 +00001090 MachineFunction &MF) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001091 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1092 // linux. This is because the stack realignment code can't handle certain
1093 // cases like PR2962. This should be removed when PR2962 is fixed.
Dan Gohman37f32ee2010-04-16 20:11:05 +00001094 const Function *F = MF.getFunction();
Evan Chengf28f8bc2010-04-02 19:36:14 +00001095 if (NonScalarIntSafe &&
1096 !F->hasFnAttr(Attribute::NoImplicitFloat)) {
Evan Cheng255f20f2010-04-01 06:04:33 +00001097 if (Size >= 16 &&
1098 (Subtarget->isUnalignedMemAccessFast() ||
Chandler Carruthae1d41c2010-04-02 01:31:24 +00001099 ((DstAlign == 0 || DstAlign >= 16) &&
1100 (SrcAlign == 0 || SrcAlign >= 16))) &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001101 Subtarget->getStackAlignment() >= 16) {
1102 if (Subtarget->hasSSE2())
1103 return MVT::v4i32;
Evan Chengf28f8bc2010-04-02 19:36:14 +00001104 if (Subtarget->hasSSE1())
Evan Cheng255f20f2010-04-01 06:04:33 +00001105 return MVT::v4f32;
Evan Chengc3b0c342010-04-08 07:37:57 +00001106 } else if (!MemcpyStrSrc && Size >= 8 &&
Evan Cheng3ea97552010-04-01 20:27:45 +00001107 !Subtarget->is64Bit() &&
Evan Cheng255f20f2010-04-01 06:04:33 +00001108 Subtarget->getStackAlignment() >= 8 &&
Evan Chengc3b0c342010-04-08 07:37:57 +00001109 Subtarget->hasSSE2()) {
1110 // Do not use f64 to lower memcpy if source is string constant. It's
1111 // better to use i32 to avoid the loads.
Evan Cheng255f20f2010-04-01 06:04:33 +00001112 return MVT::f64;
Evan Chengc3b0c342010-04-08 07:37:57 +00001113 }
Chris Lattner4002a1b2008-10-28 05:49:35 +00001114 }
Evan Chengf0df0312008-05-15 08:39:06 +00001115 if (Subtarget->is64Bit() && Size >= 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00001116 return MVT::i64;
1117 return MVT::i32;
Evan Chengf0df0312008-05-15 08:39:06 +00001118}
1119
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001120/// getJumpTableEncoding - Return the entry encoding for a jump table in the
1121/// current function. The returned value is a member of the
1122/// MachineJumpTableInfo::JTEntryKind enum.
1123unsigned X86TargetLowering::getJumpTableEncoding() const {
1124 // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
1125 // symbol.
1126 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1127 Subtarget->isPICStyleGOT())
Chris Lattnerc64daab2010-01-26 05:02:42 +00001128 return MachineJumpTableInfo::EK_Custom32;
Chris Lattner5e1df8d2010-01-25 23:38:14 +00001129
1130 // Otherwise, use the normal jump table encoding heuristics.
1131 return TargetLowering::getJumpTableEncoding();
1132}
1133
Chris Lattner589c6f62010-01-26 06:28:43 +00001134/// getPICBaseSymbol - Return the X86-32 PIC base.
1135MCSymbol *
1136X86TargetLowering::getPICBaseSymbol(const MachineFunction *MF,
1137 MCContext &Ctx) const {
1138 const MCAsmInfo &MAI = *getTargetMachine().getMCAsmInfo();
Chris Lattner9b97a732010-03-30 18:10:53 +00001139 return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+
1140 Twine(MF->getFunctionNumber())+"$pb");
Chris Lattner589c6f62010-01-26 06:28:43 +00001141}
1142
1143
Chris Lattnerc64daab2010-01-26 05:02:42 +00001144const MCExpr *
1145X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
1146 const MachineBasicBlock *MBB,
1147 unsigned uid,MCContext &Ctx) const{
1148 assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1149 Subtarget->isPICStyleGOT());
1150 // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
1151 // entries.
Daniel Dunbar4e815f82010-03-15 23:51:06 +00001152 return MCSymbolRefExpr::Create(MBB->getSymbol(),
1153 MCSymbolRefExpr::VK_GOTOFF, Ctx);
Chris Lattnerc64daab2010-01-26 05:02:42 +00001154}
1155
Evan Chengcc415862007-11-09 01:32:10 +00001156/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1157/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001158SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Chris Lattner589c6f62010-01-26 06:28:43 +00001159 SelectionDAG &DAG) const {
Chris Lattnere4df7562009-07-09 03:15:51 +00001160 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001161 // This doesn't have DebugLoc associated with it, but is not really the
1162 // same as a Register.
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001163 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001164 return Table;
1165}
1166
Chris Lattner589c6f62010-01-26 06:28:43 +00001167/// getPICJumpTableRelocBaseExpr - This returns the relocation base for the
1168/// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an
1169/// MCExpr.
1170const MCExpr *X86TargetLowering::
1171getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
1172 MCContext &Ctx) const {
1173 // X86-64 uses RIP relative addressing based on the jump table label.
1174 if (Subtarget->isPICStyleRIPRel())
1175 return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
1176
1177 // Otherwise, the reference is relative to the PIC base.
1178 return MCSymbolRefExpr::Create(getPICBaseSymbol(MF, Ctx), Ctx);
1179}
1180
Bill Wendlingb4202b82009-07-01 18:50:55 +00001181/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001182unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
Dan Gohman25103a22009-08-18 00:20:06 +00001183 return F->hasFnAttr(Attribute::OptimizeForSize) ? 0 : 4;
Bill Wendling20c568f2009-06-30 22:38:32 +00001184}
1185
Chris Lattner2b02a442007-02-25 08:29:00 +00001186//===----------------------------------------------------------------------===//
1187// Return Value Calling Convention Implementation
1188//===----------------------------------------------------------------------===//
1189
Chris Lattner59ed56b2007-02-28 04:55:35 +00001190#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001191
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001192bool
1193X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
1194 const SmallVectorImpl<EVT> &OutTys,
1195 const SmallVectorImpl<ISD::ArgFlagsTy> &ArgsFlags,
Dan Gohmand858e902010-04-17 15:26:15 +00001196 SelectionDAG &DAG) const {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00001197 SmallVector<CCValAssign, 16> RVLocs;
1198 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1199 RVLocs, *DAG.getContext());
1200 return CCInfo.CheckReturn(OutTys, ArgsFlags, RetCC_X86);
1201}
1202
Dan Gohman98ca4f22009-08-05 01:29:28 +00001203SDValue
1204X86TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001205 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001206 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +00001207 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00001208 MachineFunction &MF = DAG.getMachineFunction();
1209 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001210
Chris Lattner9774c912007-02-27 05:28:59 +00001211 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001212 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1213 RVLocs, *DAG.getContext());
1214 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001215
Evan Chengdcea1632010-02-04 02:40:39 +00001216 // Add the regs to the liveout set for the function.
1217 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1218 for (unsigned i = 0; i != RVLocs.size(); ++i)
1219 if (RVLocs[i].isRegLoc() && !MRI.isLiveOut(RVLocs[i].getLocReg()))
1220 MRI.addLiveOut(RVLocs[i].getLocReg());
Scott Michelfdc40a02009-02-17 22:15:04 +00001221
Dan Gohman475871a2008-07-27 21:46:04 +00001222 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001223
Dan Gohman475871a2008-07-27 21:46:04 +00001224 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001225 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1226 // Operand #1 = Bytes To Pop
Dan Gohman1e93df62010-04-17 14:41:14 +00001227 RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(),
1228 MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001229
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001230 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001231 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1232 CCValAssign &VA = RVLocs[i];
1233 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001234 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001235
Chris Lattner447ff682008-03-11 03:23:40 +00001236 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1237 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001238 if (VA.getLocReg() == X86::ST0 ||
1239 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001240 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1241 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001242 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Owen Anderson825b72b2009-08-11 20:47:22 +00001243 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001244 RetOps.push_back(ValToCopy);
1245 // Don't emit a copytoreg.
1246 continue;
1247 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001248
Evan Cheng242b38b2009-02-23 09:03:22 +00001249 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1250 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001251 if (Subtarget->is64Bit()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001252 EVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001253 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001254 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001255 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001257 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001258 }
1259
Dale Johannesendd64c412009-02-04 00:33:20 +00001260 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001261 Flag = Chain.getValue(1);
1262 }
Dan Gohman61a92132008-04-21 23:59:07 +00001263
1264 // The x86-64 ABI for returning structs by value requires that we copy
1265 // the sret argument into %rax for the return. We saved the argument into
1266 // a virtual register in the entry block, so now we copy the value out
1267 // and into %rax.
1268 if (Subtarget->is64Bit() &&
1269 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1270 MachineFunction &MF = DAG.getMachineFunction();
1271 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1272 unsigned Reg = FuncInfo->getSRetReturnReg();
Zhongxing Xuc2798a12010-05-26 08:10:02 +00001273 assert(Reg &&
1274 "SRetReturnReg should have been set in LowerFormalArguments().");
Dale Johannesendd64c412009-02-04 00:33:20 +00001275 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001276
Dale Johannesendd64c412009-02-04 00:33:20 +00001277 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001278 Flag = Chain.getValue(1);
Dan Gohman00326812009-10-12 16:36:12 +00001279
1280 // RAX now acts like a return value.
Evan Chengdcea1632010-02-04 02:40:39 +00001281 MRI.addLiveOut(X86::RAX);
Dan Gohman61a92132008-04-21 23:59:07 +00001282 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001283
Chris Lattner447ff682008-03-11 03:23:40 +00001284 RetOps[0] = Chain; // Update chain.
1285
1286 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001287 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001288 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001289
1290 return DAG.getNode(X86ISD::RET_FLAG, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001291 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001292}
1293
Dan Gohman98ca4f22009-08-05 01:29:28 +00001294/// LowerCallResult - Lower the result values of a call into the
1295/// appropriate copies out of appropriate physical registers.
1296///
1297SDValue
1298X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001299 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001300 const SmallVectorImpl<ISD::InputArg> &Ins,
1301 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001302 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001303
Chris Lattnere32bbf62007-02-28 07:09:55 +00001304 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001305 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001306 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001307 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001308 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001309 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001310
Chris Lattner3085e152007-02-25 08:59:22 +00001311 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001312 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001313 CCValAssign &VA = RVLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001314 EVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001315
Torok Edwin3f142c32009-02-01 18:15:56 +00001316 // If this is x86-64, and we disabled SSE, we can't return FP values
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001318 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Chris Lattner75361b62010-04-07 22:58:41 +00001319 report_fatal_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001320 }
1321
Chris Lattner8e6da152008-03-10 21:08:41 +00001322 // If this is a call to a function that returns an fp value on the floating
1323 // point stack, but where we prefer to use the value in xmm registers, copy
1324 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman37eed792009-02-04 17:28:58 +00001325 if ((VA.getLocReg() == X86::ST0 ||
1326 VA.getLocReg() == X86::ST1) &&
1327 isScalarFPTypeInSSEReg(VA.getValVT())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001328 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001330
Evan Cheng79fb3b42009-02-20 20:43:02 +00001331 SDValue Val;
1332 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001333 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1334 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1335 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001336 MVT::v2i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001337 Val = Chain.getValue(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001338 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1339 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001340 } else {
1341 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001342 MVT::i64, InFlag).getValue(1);
Evan Cheng242b38b2009-02-23 09:03:22 +00001343 Val = Chain.getValue(0);
1344 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001345 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1346 } else {
1347 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1348 CopyVT, InFlag).getValue(1);
1349 Val = Chain.getValue(0);
1350 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001351 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001352
Dan Gohman37eed792009-02-04 17:28:58 +00001353 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001354 // Round the F80 the right size, which also moves to the appropriate xmm
1355 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001356 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001357 // This truncation won't change the value.
1358 DAG.getIntPtrConstant(1));
1359 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001360
Dan Gohman98ca4f22009-08-05 01:29:28 +00001361 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001362 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001363
Dan Gohman98ca4f22009-08-05 01:29:28 +00001364 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001365}
1366
1367
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001368//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001369// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001370//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001371// StdCall calling convention seems to be standard for many Windows' API
1372// routines and around. It differs from C calling convention just a little:
1373// callee should clean up the stack, not caller. Symbols should be also
1374// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001375// For info on fast calling convention see Fast Calling Convention (tail call)
1376// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001377
Dan Gohman98ca4f22009-08-05 01:29:28 +00001378/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001379/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001380static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1381 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001382 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001383
Dan Gohman98ca4f22009-08-05 01:29:28 +00001384 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001385}
1386
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001387/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001388/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001389static bool
1390ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1391 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001392 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001393
Dan Gohman98ca4f22009-08-05 01:29:28 +00001394 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001395}
1396
Dan Gohman095cc292008-09-13 01:54:27 +00001397/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1398/// given CallingConvention value.
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001399CCAssignFn *X86TargetLowering::CCAssignFnForNode(CallingConv::ID CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001400 if (Subtarget->is64Bit()) {
Chris Lattner29689432010-03-11 00:22:57 +00001401 if (CC == CallingConv::GHC)
1402 return CC_X86_64_GHC;
1403 else if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001404 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001405 else
1406 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001407 }
1408
Gordon Henriksen86737662008-01-05 16:56:59 +00001409 if (CC == CallingConv::X86_FastCall)
1410 return CC_X86_32_FastCall;
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001411 else if (CC == CallingConv::X86_ThisCall)
1412 return CC_X86_32_ThisCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001413 else if (CC == CallingConv::Fast)
1414 return CC_X86_32_FastCC;
Chris Lattner29689432010-03-11 00:22:57 +00001415 else if (CC == CallingConv::GHC)
1416 return CC_X86_32_GHC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001417 else
1418 return CC_X86_32_C;
1419}
1420
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001421/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1422/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001423/// the specific parameter attribute. The copy will be passed as a byval
1424/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001425static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001426CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001427 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1428 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001429 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001430 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Mon P Wang20adc9d2010-04-04 03:10:48 +00001431 /*isVolatile*/false, /*AlwaysInline=*/true,
1432 NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001433}
1434
Chris Lattner29689432010-03-11 00:22:57 +00001435/// IsTailCallConvention - Return true if the calling convention is one that
1436/// supports tail call optimization.
1437static bool IsTailCallConvention(CallingConv::ID CC) {
1438 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1439}
1440
Evan Cheng0c439eb2010-01-27 00:07:07 +00001441/// FuncIsMadeTailCallSafe - Return true if the function is being made into
1442/// a tailcall target by changing its ABI.
1443static bool FuncIsMadeTailCallSafe(CallingConv::ID CC) {
Chris Lattner29689432010-03-11 00:22:57 +00001444 return GuaranteedTailCallOpt && IsTailCallConvention(CC);
Evan Cheng0c439eb2010-01-27 00:07:07 +00001445}
1446
Dan Gohman98ca4f22009-08-05 01:29:28 +00001447SDValue
1448X86TargetLowering::LowerMemArgument(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001449 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001450 const SmallVectorImpl<ISD::InputArg> &Ins,
1451 DebugLoc dl, SelectionDAG &DAG,
1452 const CCValAssign &VA,
1453 MachineFrameInfo *MFI,
Dan Gohmand858e902010-04-17 15:26:15 +00001454 unsigned i) const {
Rafael Espindola7effac52007-09-14 15:48:13 +00001455 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001456 ISD::ArgFlagsTy Flags = Ins[i].Flags;
Evan Cheng0c439eb2010-01-27 00:07:07 +00001457 bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001458 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Anton Korobeynikov22472762009-08-14 18:19:10 +00001459 EVT ValVT;
1460
1461 // If value is passed by pointer we have address passed instead of the value
1462 // itself.
1463 if (VA.getLocInfo() == CCValAssign::Indirect)
1464 ValVT = VA.getLocVT();
1465 else
1466 ValVT = VA.getValVT();
Evan Chenge70bb592008-01-10 02:24:25 +00001467
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001468 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001469 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001470 // In case of tail call optimization mark all arguments mutable. Since they
1471 // could be overwritten by lowering of arguments in case of a tail call.
Evan Cheng90567c32010-02-02 23:58:13 +00001472 if (Flags.isByVal()) {
1473 int FI = MFI->CreateFixedObject(Flags.getByValSize(),
1474 VA.getLocMemOffset(), isImmutable, false);
1475 return DAG.getFrameIndex(FI, getPointerTy());
1476 } else {
1477 int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
1478 VA.getLocMemOffset(), isImmutable, false);
1479 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
1480 return DAG.getLoad(ValVT, dl, Chain, FIN,
David Greene67c9d422010-02-15 16:53:33 +00001481 PseudoSourceValue::getFixedStack(FI), 0,
1482 false, false, 0);
Evan Cheng90567c32010-02-02 23:58:13 +00001483 }
Rafael Espindola7effac52007-09-14 15:48:13 +00001484}
1485
Dan Gohman475871a2008-07-27 21:46:04 +00001486SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001487X86TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001488 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001489 bool isVarArg,
1490 const SmallVectorImpl<ISD::InputArg> &Ins,
1491 DebugLoc dl,
1492 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001493 SmallVectorImpl<SDValue> &InVals)
1494 const {
Evan Cheng1bc78042006-04-26 01:20:17 +00001495 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001496 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001497
Gordon Henriksen86737662008-01-05 16:56:59 +00001498 const Function* Fn = MF.getFunction();
1499 if (Fn->hasExternalLinkage() &&
1500 Subtarget->isTargetCygMing() &&
1501 Fn->getName() == "main")
1502 FuncInfo->setForceFramePointer(true);
1503
Evan Cheng1bc78042006-04-26 01:20:17 +00001504 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001505 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001506 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001507
Chris Lattner29689432010-03-11 00:22:57 +00001508 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1509 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001510
Chris Lattner638402b2007-02-28 07:00:42 +00001511 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001512 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001513 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1514 ArgLocs, *DAG.getContext());
1515 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001516
Chris Lattnerf39f7712007-02-28 05:46:49 +00001517 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001518 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001519 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1520 CCValAssign &VA = ArgLocs[i];
1521 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1522 // places.
1523 assert(VA.getValNo() != LastVal &&
1524 "Don't support value assigned to multiple locs yet");
1525 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001526
Chris Lattnerf39f7712007-02-28 05:46:49 +00001527 if (VA.isRegLoc()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001528 EVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001529 TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 if (RegVT == MVT::i32)
Chris Lattnerf39f7712007-02-28 05:46:49 +00001531 RC = X86::GR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001532 else if (Is64Bit && RegVT == MVT::i64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001533 RC = X86::GR64RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001534 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001535 RC = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +00001536 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001537 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001538 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001539 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001540 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1541 RC = X86::VR64RegisterClass;
1542 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001543 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001544
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001545 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001546 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001547
Chris Lattnerf39f7712007-02-28 05:46:49 +00001548 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1549 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1550 // right size.
1551 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001552 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001553 DAG.getValueType(VA.getValVT()));
1554 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001555 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001556 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001557 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001558 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001559
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001560 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001561 // Handle MMX values passed in XMM regs.
1562 if (RegVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001563 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1564 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001565 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1566 } else
1567 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001568 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001569 } else {
1570 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001571 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001572 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001573
1574 // If value is passed via pointer - do a load.
1575 if (VA.getLocInfo() == CCValAssign::Indirect)
David Greene67c9d422010-02-15 16:53:33 +00001576 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0,
1577 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001578
Dan Gohman98ca4f22009-08-05 01:29:28 +00001579 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001580 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001581
Dan Gohman61a92132008-04-21 23:59:07 +00001582 // The x86-64 ABI for returning structs by value requires that we copy
1583 // the sret argument into %rax for the return. Save the argument into
1584 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001585 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001586 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1587 unsigned Reg = FuncInfo->getSRetReturnReg();
1588 if (!Reg) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001589 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
Dan Gohman61a92132008-04-21 23:59:07 +00001590 FuncInfo->setSRetReturnReg(Reg);
1591 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001592 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
Owen Anderson825b72b2009-08-11 20:47:22 +00001593 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001594 }
1595
Chris Lattnerf39f7712007-02-28 05:46:49 +00001596 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng0c439eb2010-01-27 00:07:07 +00001597 // Align stack specially for tail calls.
1598 if (FuncIsMadeTailCallSafe(CallConv))
Gordon Henriksenae636f82008-01-03 16:47:34 +00001599 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001600
Evan Cheng1bc78042006-04-26 01:20:17 +00001601 // If the function takes variable number of arguments, make a frame index for
1602 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001603 if (isVarArg) {
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001604 if (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
1605 CallConv != CallingConv::X86_ThisCall)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001606 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,
1607 true, false));
Gordon Henriksen86737662008-01-05 16:56:59 +00001608 }
1609 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001610 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1611
1612 // FIXME: We should really autogenerate these arrays
1613 static const unsigned GPR64ArgRegsWin64[] = {
1614 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001615 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001616 static const unsigned XMMArgRegsWin64[] = {
1617 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1618 };
1619 static const unsigned GPR64ArgRegs64Bit[] = {
1620 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1621 };
1622 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001623 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1624 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1625 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001626 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1627
1628 if (IsWin64) {
1629 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1630 GPR64ArgRegs = GPR64ArgRegsWin64;
1631 XMMArgRegs = XMMArgRegsWin64;
1632 } else {
1633 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1634 GPR64ArgRegs = GPR64ArgRegs64Bit;
1635 XMMArgRegs = XMMArgRegs64Bit;
1636 }
1637 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1638 TotalNumIntRegs);
1639 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1640 TotalNumXMMRegs);
1641
Devang Patel578efa92009-06-05 21:57:13 +00001642 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001643 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001644 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001645 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001646 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001647 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001648 // Kernel mode asks for SSE to be disabled, so don't push them
1649 // on the stack.
1650 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001651
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 // For X86-64, if there are vararg parameters that are passed via
1653 // registers, then we must store them to their spots on the stack so they
1654 // may be loaded by deferencing the result of va_next.
Dan Gohman1e93df62010-04-17 14:41:14 +00001655 FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
1656 FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16);
1657 FuncInfo->setRegSaveFrameIndex(
1658 MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16,
1659 false));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001660
Gordon Henriksen86737662008-01-05 16:56:59 +00001661 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001662 SmallVector<SDValue, 8> MemOps;
Dan Gohman1e93df62010-04-17 14:41:14 +00001663 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
1664 getPointerTy());
1665 unsigned Offset = FuncInfo->getVarArgsGPOffset();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001666 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Dan Gohmand6708ea2009-08-15 01:38:56 +00001667 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
1668 DAG.getIntPtrConstant(Offset));
Bob Wilson998e1252009-04-20 18:36:57 +00001669 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1670 X86::GR64RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +00001671 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001672 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001673 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohman1e93df62010-04-17 14:41:14 +00001674 PseudoSourceValue::getFixedStack(
1675 FuncInfo->getRegSaveFrameIndex()),
David Greene67c9d422010-02-15 16:53:33 +00001676 Offset, false, false, 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001677 MemOps.push_back(Store);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001678 Offset += 8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001679 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001680
Dan Gohmanface41a2009-08-16 21:24:25 +00001681 if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) {
1682 // Now store the XMM (fp + vector) parameter registers.
1683 SmallVector<SDValue, 11> SaveXMMOps;
1684 SaveXMMOps.push_back(Chain);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001685
Dan Gohmanface41a2009-08-16 21:24:25 +00001686 unsigned AL = MF.addLiveIn(X86::AL, X86::GR8RegisterClass);
1687 SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8);
1688 SaveXMMOps.push_back(ALVal);
Dan Gohmand6708ea2009-08-15 01:38:56 +00001689
Dan Gohman1e93df62010-04-17 14:41:14 +00001690 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1691 FuncInfo->getRegSaveFrameIndex()));
1692 SaveXMMOps.push_back(DAG.getIntPtrConstant(
1693 FuncInfo->getVarArgsFPOffset()));
Dan Gohmand6708ea2009-08-15 01:38:56 +00001694
Dan Gohmanface41a2009-08-16 21:24:25 +00001695 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
1696 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1697 X86::VR128RegisterClass);
1698 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
1699 SaveXMMOps.push_back(Val);
1700 }
1701 MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
1702 MVT::Other,
1703 &SaveXMMOps[0], SaveXMMOps.size()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001704 }
Dan Gohmanface41a2009-08-16 21:24:25 +00001705
1706 if (!MemOps.empty())
1707 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1708 &MemOps[0], MemOps.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001709 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001710 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001711
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 // Some CCs need callee pop.
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001713 if (Subtarget->IsCalleePop(isVarArg, CallConv)) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001714 FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001715 } else {
Dan Gohman1e93df62010-04-17 14:41:14 +00001716 FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001717 // If this is an sret function, the return should pop the hidden pointer.
Chris Lattner29689432010-03-11 00:22:57 +00001718 if (!Is64Bit && !IsTailCallConvention(CallConv) && ArgsAreStructReturn(Ins))
Dan Gohman1e93df62010-04-17 14:41:14 +00001719 FuncInfo->setBytesToPopOnReturn(4);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001720 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001721
Gordon Henriksen86737662008-01-05 16:56:59 +00001722 if (!Is64Bit) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001723 // RegSaveFrameIndex is X86-64 only.
1724 FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001725 if (CallConv == CallingConv::X86_FastCall ||
1726 CallConv == CallingConv::X86_ThisCall)
Dan Gohman1e93df62010-04-17 14:41:14 +00001727 // fastcc functions can't have varargs.
1728 FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
Gordon Henriksen86737662008-01-05 16:56:59 +00001729 }
Evan Cheng25caf632006-05-23 21:06:34 +00001730
Dan Gohman98ca4f22009-08-05 01:29:28 +00001731 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001732}
1733
Dan Gohman475871a2008-07-27 21:46:04 +00001734SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001735X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1736 SDValue StackPtr, SDValue Arg,
1737 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001738 const CCValAssign &VA,
Dan Gohmand858e902010-04-17 15:26:15 +00001739 ISD::ArgFlagsTy Flags) const {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001740 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001741 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001742 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001743 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001744 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001745 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001746 }
Dale Johannesenace16102009-02-03 19:33:06 +00001747 return DAG.getStore(Chain, dl, Arg, PtrOff,
David Greene67c9d422010-02-15 16:53:33 +00001748 PseudoSourceValue::getStack(), LocMemOffset,
1749 false, false, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001750}
1751
Bill Wendling64e87322009-01-16 19:25:27 +00001752/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001753/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001754SDValue
1755X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Evan Chengddc419c2010-01-26 19:04:47 +00001756 SDValue &OutRetAddr, SDValue Chain,
1757 bool IsTailCall, bool Is64Bit,
Dan Gohmand858e902010-04-17 15:26:15 +00001758 int FPDiff, DebugLoc dl) const {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001759 // Adjust the Return address stack slot.
Owen Andersone50ed302009-08-10 22:56:29 +00001760 EVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001761 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001762
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001763 // Load the "old" Return address.
David Greene67c9d422010-02-15 16:53:33 +00001764 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0, false, false, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001765 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001766}
1767
1768/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1769/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001770static SDValue
1771EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001772 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001773 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001774 // Store the return address to the appropriate stack slot.
1775 if (!FPDiff) return Chain;
1776 // Calculate the new stack slot for the return address.
1777 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001778 int NewReturnAddrFI =
Arnold Schwaighofer92652752010-02-22 16:18:09 +00001779 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false, false);
Owen Anderson825b72b2009-08-11 20:47:22 +00001780 EVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001781 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001782 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
David Greene67c9d422010-02-15 16:53:33 +00001783 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0,
1784 false, false, 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001785 return Chain;
1786}
1787
Dan Gohman98ca4f22009-08-05 01:29:28 +00001788SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +00001789X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001790 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +00001791 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001792 const SmallVectorImpl<ISD::OutputArg> &Outs,
1793 const SmallVectorImpl<ISD::InputArg> &Ins,
1794 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001795 SmallVectorImpl<SDValue> &InVals) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001796 MachineFunction &MF = DAG.getMachineFunction();
1797 bool Is64Bit = Subtarget->is64Bit();
1798 bool IsStructRet = CallIsStructReturn(Outs);
Evan Cheng5f941932010-02-05 02:21:12 +00001799 bool IsSibcall = false;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001800
Evan Cheng5f941932010-02-05 02:21:12 +00001801 if (isTailCall) {
Evan Cheng0c439eb2010-01-27 00:07:07 +00001802 // Check if it's really possible to do a tail call.
Evan Chenga375d472010-03-15 18:54:48 +00001803 isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
1804 isVarArg, IsStructRet, MF.getFunction()->hasStructRetAttr(),
Evan Cheng022d9e12010-02-02 23:55:14 +00001805 Outs, Ins, DAG);
Evan Chengf22f9b32010-02-06 03:28:46 +00001806
1807 // Sibcalls are automatically detected tailcalls which do not require
1808 // ABI changes.
Dan Gohman1797ed52010-02-08 20:27:50 +00001809 if (!GuaranteedTailCallOpt && isTailCall)
Evan Cheng5f941932010-02-05 02:21:12 +00001810 IsSibcall = true;
Evan Chengf22f9b32010-02-06 03:28:46 +00001811
1812 if (isTailCall)
1813 ++NumTailCalls;
Evan Cheng5f941932010-02-05 02:21:12 +00001814 }
Evan Cheng0c439eb2010-01-27 00:07:07 +00001815
Chris Lattner29689432010-03-11 00:22:57 +00001816 assert(!(isVarArg && IsTailCallConvention(CallConv)) &&
1817 "Var args not supported with calling convention fastcc or ghc");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001818
Chris Lattner638402b2007-02-28 07:00:42 +00001819 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001820 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001821 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1822 ArgLocs, *DAG.getContext());
1823 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001824
Chris Lattner423c5f42007-02-28 05:31:48 +00001825 // Get a count of how many bytes are to be pushed on the stack.
1826 unsigned NumBytes = CCInfo.getNextStackOffset();
Evan Chengf22f9b32010-02-06 03:28:46 +00001827 if (IsSibcall)
Evan Chengb2c92902010-02-02 02:22:50 +00001828 // This is a sibcall. The memory operands are available in caller's
1829 // own caller's stack.
1830 NumBytes = 0;
Chris Lattner29689432010-03-11 00:22:57 +00001831 else if (GuaranteedTailCallOpt && IsTailCallConvention(CallConv))
Evan Chengf22f9b32010-02-06 03:28:46 +00001832 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001833
Gordon Henriksen86737662008-01-05 16:56:59 +00001834 int FPDiff = 0;
Evan Chengf22f9b32010-02-06 03:28:46 +00001835 if (isTailCall && !IsSibcall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001836 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001837 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001838 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1839 FPDiff = NumBytesCallerPushed - NumBytes;
1840
1841 // Set the delta of movement of the returnaddr stackslot.
1842 // But only set if delta is greater than previous delta.
1843 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1844 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1845 }
1846
Evan Chengf22f9b32010-02-06 03:28:46 +00001847 if (!IsSibcall)
1848 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001849
Dan Gohman475871a2008-07-27 21:46:04 +00001850 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001851 // Load return adress for tail calls.
Evan Chengf22f9b32010-02-06 03:28:46 +00001852 if (isTailCall && FPDiff)
1853 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
1854 Is64Bit, FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001855
Dan Gohman475871a2008-07-27 21:46:04 +00001856 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1857 SmallVector<SDValue, 8> MemOpChains;
1858 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001859
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001860 // Walk the register/memloc assignments, inserting copies/loads. In the case
1861 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001862 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1863 CCValAssign &VA = ArgLocs[i];
Owen Andersone50ed302009-08-10 22:56:29 +00001864 EVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001865 SDValue Arg = Outs[i].Val;
1866 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001867 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001868
Chris Lattner423c5f42007-02-28 05:31:48 +00001869 // Promote the value if needed.
1870 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001871 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001872 case CCValAssign::Full: break;
1873 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001874 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001875 break;
1876 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001877 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001878 break;
1879 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001880 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1881 // Special case: passing MMX values in XMM registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00001882 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1883 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1884 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001885 } else
1886 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1887 break;
1888 case CCValAssign::BCvt:
1889 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001890 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001891 case CCValAssign::Indirect: {
1892 // Store the argument.
1893 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
Evan Chengff89dcb2009-10-18 18:16:27 +00001894 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001895 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
David Greene67c9d422010-02-15 16:53:33 +00001896 PseudoSourceValue::getFixedStack(FI), 0,
1897 false, false, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001898 Arg = SpillSlot;
1899 break;
1900 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001901 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001902
Chris Lattner423c5f42007-02-28 05:31:48 +00001903 if (VA.isRegLoc()) {
1904 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
Evan Chengf22f9b32010-02-06 03:28:46 +00001905 } else if (!IsSibcall && (!isTailCall || isByVal)) {
Evan Cheng5f941932010-02-05 02:21:12 +00001906 assert(VA.isMemLoc());
1907 if (StackPtr.getNode() == 0)
1908 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
1909 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1910 dl, DAG, VA, Flags));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001911 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001912 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001913
Evan Cheng32fe1032006-05-25 00:59:30 +00001914 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00001915 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001916 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001917
Evan Cheng347d5f72006-04-28 21:29:37 +00001918 // Build a sequence of copy-to-reg nodes chained together with token chain
1919 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001920 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001921 // Tail call byval lowering might overwrite argument registers so in case of
1922 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001923 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001924 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001925 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001926 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001927 InFlag = Chain.getValue(1);
1928 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001929
Chris Lattner88e1fd52009-07-09 04:24:46 +00001930 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001931 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1932 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001933 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001934 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1935 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00001936 DebugLoc(), getPointerTy()),
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001937 InFlag);
1938 InFlag = Chain.getValue(1);
1939 } else {
1940 // If we are tail calling and generating PIC/GOT style code load the
1941 // address of the callee into ECX. The value in ecx is used as target of
1942 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1943 // for tail calls on PIC/GOT architectures. Normally we would just put the
1944 // address of GOT into ebx and then call target@PLT. But for tail calls
1945 // ebx would be restored (since ebx is callee saved) before jumping to the
1946 // target@PLT.
1947
1948 // Note: The actual moving to ECX is done further down.
1949 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1950 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1951 !G->getGlobal()->hasProtectedVisibility())
1952 Callee = LowerGlobalAddress(Callee, DAG);
1953 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001954 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001955 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001956 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001957
Gordon Henriksen86737662008-01-05 16:56:59 +00001958 if (Is64Bit && isVarArg) {
1959 // From AMD64 ABI document:
1960 // For calls that may call functions that use varargs or stdargs
1961 // (prototype-less calls or calls to functions containing ellipsis (...) in
1962 // the declaration) %al is used as hidden argument to specify the number
1963 // of SSE registers used. The contents of %al do not need to match exactly
1964 // the number of registers, but must be an ubound on the number of SSE
1965 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001966
1967 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001968 // Count the number of XMM registers allocated.
1969 static const unsigned XMMArgRegs[] = {
1970 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1971 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1972 };
1973 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001974 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001975 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001976
Dale Johannesendd64c412009-02-04 00:33:20 +00001977 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Owen Anderson825b72b2009-08-11 20:47:22 +00001978 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001979 InFlag = Chain.getValue(1);
1980 }
1981
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001982
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001983 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001984 if (isTailCall) {
1985 // Force all the incoming stack arguments to be loaded from the stack
1986 // before any new outgoing arguments are stored to the stack, because the
1987 // outgoing stack slots may alias the incoming argument stack slots, and
1988 // the alias isn't otherwise explicit. This is slightly more conservative
1989 // than necessary, because it means that each store effectively depends
1990 // on every argument instead of just those arguments it would clobber.
1991 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1992
Dan Gohman475871a2008-07-27 21:46:04 +00001993 SmallVector<SDValue, 8> MemOpChains2;
1994 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001995 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001996 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001997 InFlag = SDValue();
Dan Gohman1797ed52010-02-08 20:27:50 +00001998 if (GuaranteedTailCallOpt) {
Evan Chengb2c92902010-02-02 02:22:50 +00001999 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2000 CCValAssign &VA = ArgLocs[i];
2001 if (VA.isRegLoc())
2002 continue;
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002003 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00002004 SDValue Arg = Outs[i].Val;
2005 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00002006 // Create frame index.
2007 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002008 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
David Greene3f2bf852009-11-12 20:49:22 +00002009 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true, false);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002010 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00002011
Duncan Sands276dcbd2008-03-21 09:14:45 +00002012 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002013 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00002014 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00002015 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00002016 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00002017 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00002018 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002019
Dan Gohman98ca4f22009-08-05 01:29:28 +00002020 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
2021 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00002022 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00002023 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00002024 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00002025 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00002026 DAG.getStore(ArgChain, dl, Arg, FIN,
David Greene67c9d422010-02-15 16:53:33 +00002027 PseudoSourceValue::getFixedStack(FI), 0,
2028 false, false, 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002029 }
Gordon Henriksen86737662008-01-05 16:56:59 +00002030 }
2031 }
2032
2033 if (!MemOpChains2.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00002034 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00002035 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002036
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002037 // Copy arguments to their registers.
2038 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002039 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00002040 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00002041 InFlag = Chain.getValue(1);
2042 }
Dan Gohman475871a2008-07-27 21:46:04 +00002043 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002044
Gordon Henriksen86737662008-01-05 16:56:59 +00002045 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00002046 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00002047 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00002048 }
2049
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002050 bool WasGlobalOrExternal = false;
2051 if (getTargetMachine().getCodeModel() == CodeModel::Large) {
2052 assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
2053 // In the 64-bit large code model, we have to make all calls
2054 // through a register, since the call instruction's 32-bit
2055 // pc-relative offset may not be large enough to hold the whole
2056 // address.
2057 } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2058 WasGlobalOrExternal = true;
2059 // If the callee is a GlobalAddress node (quite common, every direct call
2060 // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
2061 // it.
2062
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002063 // We should use extra load for direct calls to dllimported functions in
2064 // non-JIT mode.
Dan Gohman46510a72010-04-15 01:51:59 +00002065 const GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00002066 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002067 unsigned char OpFlags = 0;
Eric Christopherfd179292009-08-27 18:07:15 +00002068
Chris Lattner48a7d022009-07-09 05:02:21 +00002069 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2070 // external symbols most go through the PLT in PIC mode. If the symbol
2071 // has hidden or protected visibility, or if it is static or local, then
2072 // we don't need to use the PLT - we can directly call it.
2073 if (Subtarget->isTargetELF() &&
2074 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002075 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002076 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002077 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002078 (GV->isDeclaration() || GV->isWeakForLinker()) &&
2079 Subtarget->getDarwinVers() < 9) {
2080 // PC-relative references to external symbols should go through $stub,
2081 // unless we're building with the leopard linker or later, which
2082 // automatically synthesizes these stubs.
2083 OpFlags = X86II::MO_DARWIN_STUB;
2084 }
Chris Lattner48a7d022009-07-09 05:02:21 +00002085
Chris Lattner74e726e2009-07-09 05:27:35 +00002086 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00002087 G->getOffset(), OpFlags);
2088 }
Bill Wendling056292f2008-09-16 21:48:12 +00002089 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002090 WasGlobalOrExternal = true;
Chris Lattner48a7d022009-07-09 05:02:21 +00002091 unsigned char OpFlags = 0;
2092
2093 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
2094 // symbols should go through the PLT.
2095 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002096 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00002097 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002098 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00002099 Subtarget->getDarwinVers() < 9) {
2100 // PC-relative references to external symbols should go through $stub,
2101 // unless we're building with the leopard linker or later, which
2102 // automatically synthesizes these stubs.
2103 OpFlags = X86II::MO_DARWIN_STUB;
2104 }
Eric Christopherfd179292009-08-27 18:07:15 +00002105
Chris Lattner48a7d022009-07-09 05:02:21 +00002106 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
2107 OpFlags);
Jeffrey Yasskind1ba06b2009-11-16 22:41:33 +00002108 }
2109
Chris Lattnerd96d0722007-02-25 06:40:16 +00002110 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +00002111 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00002112 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00002113
Evan Chengf22f9b32010-02-06 03:28:46 +00002114 if (!IsSibcall && isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002115 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
2116 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00002117 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00002118 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002119
Nate Begeman4c5dcf52006-02-17 00:03:04 +00002120 Ops.push_back(Chain);
2121 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00002122
Dan Gohman98ca4f22009-08-05 01:29:28 +00002123 if (isTailCall)
Owen Anderson825b72b2009-08-11 20:47:22 +00002124 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00002125
Gordon Henriksen86737662008-01-05 16:56:59 +00002126 // Add argument registers to the end of the list so that they are known live
2127 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00002128 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2129 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
2130 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00002131
Evan Cheng586ccac2008-03-18 23:36:35 +00002132 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002133 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00002134 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
2135
2136 // Add an implicit use of AL for x86 vararg functions.
2137 if (Is64Bit && isVarArg)
Owen Anderson825b72b2009-08-11 20:47:22 +00002138 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
Evan Cheng586ccac2008-03-18 23:36:35 +00002139
Gabor Greifba36cb52008-08-28 21:40:38 +00002140 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00002141 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00002142
Dan Gohman98ca4f22009-08-05 01:29:28 +00002143 if (isTailCall) {
2144 // If this is the first return lowered for this function, add the regs
2145 // to the liveout set for the function.
2146 if (MF.getRegInfo().liveout_empty()) {
2147 SmallVector<CCValAssign, 16> RVLocs;
2148 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
2149 *DAG.getContext());
2150 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2151 for (unsigned i = 0; i != RVLocs.size(); ++i)
2152 if (RVLocs[i].isRegLoc())
2153 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2154 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00002155 return DAG.getNode(X86ISD::TC_RETURN, dl,
2156 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00002157 }
2158
Dale Johannesenace16102009-02-03 19:33:06 +00002159 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00002160 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00002161
Chris Lattner2d297092006-05-23 18:50:38 +00002162 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00002163 unsigned NumBytesForCalleeToPush;
Dan Gohman4d3d6e12010-05-27 18:43:40 +00002164 if (Subtarget->IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00002165 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Chris Lattner29689432010-03-11 00:22:57 +00002166 else if (!Is64Bit && !IsTailCallConvention(CallConv) && IsStructRet)
Dan Gohmanf451cb82010-02-10 16:03:48 +00002167 // If this is a call to a struct-return function, the callee
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00002168 // pops the hidden struct pointer, so we have to push it back.
2169 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002170 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002171 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002172 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002173
Gordon Henriksenae636f82008-01-03 16:47:34 +00002174 // Returns a flag for retval copy to use.
Evan Chengf22f9b32010-02-06 03:28:46 +00002175 if (!IsSibcall) {
2176 Chain = DAG.getCALLSEQ_END(Chain,
2177 DAG.getIntPtrConstant(NumBytes, true),
2178 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2179 true),
2180 InFlag);
2181 InFlag = Chain.getValue(1);
2182 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002183
Chris Lattner3085e152007-02-25 08:59:22 +00002184 // Handle result values, copying them out of physregs into vregs that we
2185 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002186 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2187 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002188}
2189
Evan Cheng25ab6902006-09-08 06:48:29 +00002190
2191//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002192// Fast Calling Convention (tail call) implementation
2193//===----------------------------------------------------------------------===//
2194
2195// Like std call, callee cleans arguments, convention except that ECX is
2196// reserved for storing the tail called function address. Only 2 registers are
2197// free for argument passing (inreg). Tail call optimization is performed
2198// provided:
2199// * tailcallopt is enabled
2200// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002201// On X86_64 architecture with GOT-style position independent code only local
2202// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002203// To keep the stack aligned according to platform abi the function
2204// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2205// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002206// If a tail called function callee has more arguments than the caller the
2207// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002208// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002209// original REtADDR, but before the saved framepointer or the spilled registers
2210// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2211// stack layout:
2212// arg1
2213// arg2
2214// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002215// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002216// move area ]
2217// (possible EBP)
2218// ESI
2219// EDI
2220// local1 ..
2221
2222/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2223/// for a 16 byte align requirement.
Dan Gohmand858e902010-04-17 15:26:15 +00002224unsigned
2225X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
2226 SelectionDAG& DAG) const {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002227 MachineFunction &MF = DAG.getMachineFunction();
2228 const TargetMachine &TM = MF.getTarget();
2229 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2230 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002231 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002232 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002233 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002234 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2235 // Number smaller than 12 so just add the difference.
2236 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2237 } else {
2238 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002239 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002240 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002241 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002242 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002243}
2244
Evan Cheng5f941932010-02-05 02:21:12 +00002245/// MatchingStackOffset - Return true if the given stack call argument is
2246/// already available in the same position (relatively) of the caller's
2247/// incoming argument stack.
2248static
2249bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
2250 MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
2251 const X86InstrInfo *TII) {
Evan Cheng4cae1332010-03-05 08:38:04 +00002252 unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
2253 int FI = INT_MAX;
Evan Cheng5f941932010-02-05 02:21:12 +00002254 if (Arg.getOpcode() == ISD::CopyFromReg) {
2255 unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
2256 if (!VR || TargetRegisterInfo::isPhysicalRegister(VR))
2257 return false;
2258 MachineInstr *Def = MRI->getVRegDef(VR);
2259 if (!Def)
2260 return false;
2261 if (!Flags.isByVal()) {
2262 if (!TII->isLoadFromStackSlot(Def, FI))
2263 return false;
2264 } else {
2265 unsigned Opcode = Def->getOpcode();
2266 if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) &&
2267 Def->getOperand(1).isFI()) {
2268 FI = Def->getOperand(1).getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002269 Bytes = Flags.getByValSize();
Evan Cheng5f941932010-02-05 02:21:12 +00002270 } else
2271 return false;
2272 }
Evan Cheng4cae1332010-03-05 08:38:04 +00002273 } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
2274 if (Flags.isByVal())
2275 // ByVal argument is passed in as a pointer but it's now being
Evan Cheng10718492010-03-05 19:55:55 +00002276 // dereferenced. e.g.
Evan Cheng4cae1332010-03-05 08:38:04 +00002277 // define @foo(%struct.X* %A) {
2278 // tail call @bar(%struct.X* byval %A)
2279 // }
Evan Cheng5f941932010-02-05 02:21:12 +00002280 return false;
2281 SDValue Ptr = Ld->getBasePtr();
2282 FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
2283 if (!FINode)
2284 return false;
2285 FI = FINode->getIndex();
Evan Cheng4cae1332010-03-05 08:38:04 +00002286 } else
2287 return false;
Evan Cheng5f941932010-02-05 02:21:12 +00002288
Evan Cheng4cae1332010-03-05 08:38:04 +00002289 assert(FI != INT_MAX);
Evan Cheng5f941932010-02-05 02:21:12 +00002290 if (!MFI->isFixedObjectIndex(FI))
2291 return false;
Evan Cheng4cae1332010-03-05 08:38:04 +00002292 return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
Evan Cheng5f941932010-02-05 02:21:12 +00002293}
2294
Dan Gohman98ca4f22009-08-05 01:29:28 +00002295/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2296/// for tail call optimization. Targets which want to do tail call
2297/// optimization should implement this function.
2298bool
2299X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00002300 CallingConv::ID CalleeCC,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002301 bool isVarArg,
Evan Chenga375d472010-03-15 18:54:48 +00002302 bool isCalleeStructRet,
2303 bool isCallerStructRet,
Evan Chengb1712452010-01-27 06:25:16 +00002304 const SmallVectorImpl<ISD::OutputArg> &Outs,
2305 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +00002306 SelectionDAG& DAG) const {
Chris Lattner29689432010-03-11 00:22:57 +00002307 if (!IsTailCallConvention(CalleeCC) &&
Evan Chengb1712452010-01-27 06:25:16 +00002308 CalleeCC != CallingConv::C)
2309 return false;
2310
Evan Cheng7096ae42010-01-29 06:45:59 +00002311 // If -tailcallopt is specified, make fastcc functions tail-callable.
Evan Cheng2c12cb42010-03-26 16:26:03 +00002312 const MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng7096ae42010-01-29 06:45:59 +00002313 const Function *CallerF = DAG.getMachineFunction().getFunction();
Evan Cheng13617962010-04-30 01:12:32 +00002314 CallingConv::ID CallerCC = CallerF->getCallingConv();
2315 bool CCMatch = CallerCC == CalleeCC;
2316
Dan Gohman1797ed52010-02-08 20:27:50 +00002317 if (GuaranteedTailCallOpt) {
Evan Cheng13617962010-04-30 01:12:32 +00002318 if (IsTailCallConvention(CalleeCC) && CCMatch)
Evan Cheng843bd692010-01-31 06:44:49 +00002319 return true;
2320 return false;
2321 }
2322
Dale Johannesen2f05cc02010-05-28 23:24:28 +00002323 // Look for obvious safe cases to perform tail call optimization that do not
2324 // require ABI changes. This is what gcc calls sibcall.
Evan Chengb2c92902010-02-02 02:22:50 +00002325
Evan Cheng2c12cb42010-03-26 16:26:03 +00002326 // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
2327 // emit a special epilogue.
2328 if (RegInfo->needsStackRealignment(MF))
2329 return false;
2330
Evan Cheng3c262ee2010-03-26 02:13:13 +00002331 // Do not sibcall optimize vararg calls unless the call site is not passing any
2332 // arguments.
2333 if (isVarArg && !Outs.empty())
Evan Cheng843bd692010-01-31 06:44:49 +00002334 return false;
2335
Evan Chenga375d472010-03-15 18:54:48 +00002336 // Also avoid sibcall optimization if either caller or callee uses struct
2337 // return semantics.
2338 if (isCalleeStructRet || isCallerStructRet)
2339 return false;
2340
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002341 // If the call result is in ST0 / ST1, it needs to be popped off the x87 stack.
2342 // Therefore if it's not used by the call it is not safe to optimize this into
2343 // a sibcall.
2344 bool Unused = false;
2345 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
2346 if (!Ins[i].Used) {
2347 Unused = true;
2348 break;
2349 }
2350 }
2351 if (Unused) {
2352 SmallVector<CCValAssign, 16> RVLocs;
2353 CCState CCInfo(CalleeCC, false, getTargetMachine(),
2354 RVLocs, *DAG.getContext());
2355 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Evan Cheng13617962010-04-30 01:12:32 +00002356 for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
Evan Chengf5b9d6c2010-03-20 02:58:15 +00002357 CCValAssign &VA = RVLocs[i];
2358 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
2359 return false;
2360 }
2361 }
2362
Evan Cheng13617962010-04-30 01:12:32 +00002363 // If the calling conventions do not match, then we'd better make sure the
2364 // results are returned in the same way as what the caller expects.
2365 if (!CCMatch) {
2366 SmallVector<CCValAssign, 16> RVLocs1;
2367 CCState CCInfo1(CalleeCC, false, getTargetMachine(),
2368 RVLocs1, *DAG.getContext());
2369 CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
2370
2371 SmallVector<CCValAssign, 16> RVLocs2;
2372 CCState CCInfo2(CallerCC, false, getTargetMachine(),
2373 RVLocs2, *DAG.getContext());
2374 CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
2375
2376 if (RVLocs1.size() != RVLocs2.size())
2377 return false;
2378 for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
2379 if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
2380 return false;
2381 if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
2382 return false;
2383 if (RVLocs1[i].isRegLoc()) {
2384 if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
2385 return false;
2386 } else {
2387 if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
2388 return false;
2389 }
2390 }
2391 }
2392
Evan Chenga6bff982010-01-30 01:22:00 +00002393 // If the callee takes no arguments then go on to check the results of the
2394 // call.
2395 if (!Outs.empty()) {
2396 // Check if stack adjustment is needed. For now, do not do this if any
2397 // argument is passed on the stack.
2398 SmallVector<CCValAssign, 16> ArgLocs;
2399 CCState CCInfo(CalleeCC, isVarArg, getTargetMachine(),
2400 ArgLocs, *DAG.getContext());
2401 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CalleeCC));
Evan Chengb2c92902010-02-02 02:22:50 +00002402 if (CCInfo.getNextStackOffset()) {
2403 MachineFunction &MF = DAG.getMachineFunction();
2404 if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn())
2405 return false;
2406 if (Subtarget->isTargetWin64())
2407 // Win64 ABI has additional complications.
2408 return false;
2409
2410 // Check if the arguments are already laid out in the right way as
2411 // the caller's fixed stack objects.
2412 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng5f941932010-02-05 02:21:12 +00002413 const MachineRegisterInfo *MRI = &MF.getRegInfo();
2414 const X86InstrInfo *TII =
2415 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
Evan Chengb2c92902010-02-02 02:22:50 +00002416 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2417 CCValAssign &VA = ArgLocs[i];
2418 EVT RegVT = VA.getLocVT();
2419 SDValue Arg = Outs[i].Val;
2420 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Evan Chengb2c92902010-02-02 02:22:50 +00002421 if (VA.getLocInfo() == CCValAssign::Indirect)
2422 return false;
2423 if (!VA.isRegLoc()) {
Evan Cheng5f941932010-02-05 02:21:12 +00002424 if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
2425 MFI, MRI, TII))
Evan Chengb2c92902010-02-02 02:22:50 +00002426 return false;
2427 }
2428 }
2429 }
Evan Cheng9c044672010-05-29 01:35:22 +00002430
2431 // If the tailcall address may be in a register, then make sure it's
2432 // possible to register allocate for it. In 32-bit, the call address can
2433 // only target EAX, EDX, or ECX since the tail call must be scheduled after
2434 // callee-saved registers are restored. In 64-bit, it's RAX, RCX, RDX, RSI,
2435 // RDI, R8, R9, R11.
2436 if (!isa<GlobalAddressSDNode>(Callee) &&
2437 !isa<ExternalSymbolSDNode>(Callee)) {
2438 unsigned Limit = Subtarget->is64Bit() ? 8 : 3;
2439 unsigned NumInRegs = 0;
2440 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2441 CCValAssign &VA = ArgLocs[i];
2442 if (VA.isRegLoc()) {
2443 if (++NumInRegs == Limit)
2444 return false;
2445 }
2446 }
2447 }
Evan Chenga6bff982010-01-30 01:22:00 +00002448 }
Evan Chengb1712452010-01-27 06:25:16 +00002449
Evan Cheng86809cc2010-02-03 03:28:02 +00002450 return true;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002451}
2452
Dan Gohman3df24e62008-09-03 23:12:08 +00002453FastISel *
Chris Lattnered3a8062010-04-05 06:05:26 +00002454X86TargetLowering::createFastISel(MachineFunction &mf,
Evan Chengddc419c2010-01-26 19:04:47 +00002455 DenseMap<const Value *, unsigned> &vm,
2456 DenseMap<const BasicBlock*, MachineBasicBlock*> &bm,
Dan Gohmanf81eca02010-04-22 20:46:50 +00002457 DenseMap<const AllocaInst *, int> &am,
2458 std::vector<std::pair<MachineInstr*, unsigned> > &pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002459#ifndef NDEBUG
Dan Gohman25208642010-04-14 19:53:31 +00002460 , SmallSet<const Instruction *, 8> &cil
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002461#endif
Dan Gohmand858e902010-04-17 15:26:15 +00002462 ) const {
Dan Gohmanf81eca02010-04-22 20:46:50 +00002463 return X86::createFastISel(mf, vm, bm, am, pn
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002464#ifndef NDEBUG
2465 , cil
2466#endif
2467 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002468}
2469
2470
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002471//===----------------------------------------------------------------------===//
2472// Other Lowering Hooks
2473//===----------------------------------------------------------------------===//
2474
2475
Dan Gohmand858e902010-04-17 15:26:15 +00002476SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002477 MachineFunction &MF = DAG.getMachineFunction();
2478 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2479 int ReturnAddrIndex = FuncInfo->getRAIndex();
2480
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002481 if (ReturnAddrIndex == 0) {
2482 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002483 uint64_t SlotSize = TD->getPointerSize();
David Greene3f2bf852009-11-12 20:49:22 +00002484 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize,
Arnold Schwaighofer92652752010-02-22 16:18:09 +00002485 false, false);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002486 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002487 }
2488
Evan Cheng25ab6902006-09-08 06:48:29 +00002489 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002490}
2491
2492
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002493bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
2494 bool hasSymbolicDisplacement) {
2495 // Offset should fit into 32 bit immediate field.
Benjamin Kramer34247a02010-03-29 21:13:41 +00002496 if (!isInt<32>(Offset))
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00002497 return false;
2498
2499 // If we don't have a symbolic displacement - we don't have any extra
2500 // restrictions.
2501 if (!hasSymbolicDisplacement)
2502 return true;
2503
2504 // FIXME: Some tweaks might be needed for medium code model.
2505 if (M != CodeModel::Small && M != CodeModel::Kernel)
2506 return false;
2507
2508 // For small code model we assume that latest object is 16MB before end of 31
2509 // bits boundary. We may also accept pretty large negative constants knowing
2510 // that all objects are in the positive half of address space.
2511 if (M == CodeModel::Small && Offset < 16*1024*1024)
2512 return true;
2513
2514 // For kernel code model we know that all object resist in the negative half
2515 // of 32bits address space. We may not accept negative offsets, since they may
2516 // be just off and we may accept pretty large positive ones.
2517 if (M == CodeModel::Kernel && Offset > 0)
2518 return true;
2519
2520 return false;
2521}
2522
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002523/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2524/// specific condition code, returning the condition code and the LHS/RHS of the
2525/// comparison to make.
2526static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2527 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002528 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002529 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2530 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2531 // X > -1 -> X == 0, jump !sign.
2532 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002533 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002534 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2535 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002536 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002537 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002538 // X < 1 -> X <= 0
2539 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002540 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002541 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002542 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002543
Evan Chengd9558e02006-01-06 00:43:03 +00002544 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002545 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002546 case ISD::SETEQ: return X86::COND_E;
2547 case ISD::SETGT: return X86::COND_G;
2548 case ISD::SETGE: return X86::COND_GE;
2549 case ISD::SETLT: return X86::COND_L;
2550 case ISD::SETLE: return X86::COND_LE;
2551 case ISD::SETNE: return X86::COND_NE;
2552 case ISD::SETULT: return X86::COND_B;
2553 case ISD::SETUGT: return X86::COND_A;
2554 case ISD::SETULE: return X86::COND_BE;
2555 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002556 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002558
Chris Lattner4c78e022008-12-23 23:42:27 +00002559 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002560
Chris Lattner4c78e022008-12-23 23:42:27 +00002561 // If LHS is a foldable load, but RHS is not, flip the condition.
2562 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2563 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2564 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2565 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002566 }
2567
Chris Lattner4c78e022008-12-23 23:42:27 +00002568 switch (SetCCOpcode) {
2569 default: break;
2570 case ISD::SETOLT:
2571 case ISD::SETOLE:
2572 case ISD::SETUGT:
2573 case ISD::SETUGE:
2574 std::swap(LHS, RHS);
2575 break;
2576 }
2577
2578 // On a floating point condition, the flags are set as follows:
2579 // ZF PF CF op
2580 // 0 | 0 | 0 | X > Y
2581 // 0 | 0 | 1 | X < Y
2582 // 1 | 0 | 0 | X == Y
2583 // 1 | 1 | 1 | unordered
2584 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002585 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002586 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002587 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002588 case ISD::SETOLT: // flipped
2589 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002590 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002591 case ISD::SETOLE: // flipped
2592 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002593 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002594 case ISD::SETUGT: // flipped
2595 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002596 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002597 case ISD::SETUGE: // flipped
2598 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002599 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002600 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002601 case ISD::SETNE: return X86::COND_NE;
2602 case ISD::SETUO: return X86::COND_P;
2603 case ISD::SETO: return X86::COND_NP;
Dan Gohman1a492952009-10-20 16:22:37 +00002604 case ISD::SETOEQ:
2605 case ISD::SETUNE: return X86::COND_INVALID;
Chris Lattner4c78e022008-12-23 23:42:27 +00002606 }
Evan Chengd9558e02006-01-06 00:43:03 +00002607}
2608
Evan Cheng4a460802006-01-11 00:33:36 +00002609/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2610/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002611/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002612static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002613 switch (X86CC) {
2614 default:
2615 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002616 case X86::COND_B:
2617 case X86::COND_BE:
2618 case X86::COND_E:
2619 case X86::COND_P:
2620 case X86::COND_A:
2621 case X86::COND_AE:
2622 case X86::COND_NE:
2623 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002624 return true;
2625 }
2626}
2627
Evan Chengeb2f9692009-10-27 19:56:55 +00002628/// isFPImmLegal - Returns true if the target can instruction select the
2629/// specified FP immediate natively. If false, the legalizer will
2630/// materialize the FP immediate as a load from a constant pool.
Evan Chenga1eaa3c2009-10-28 01:43:28 +00002631bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
Evan Chengeb2f9692009-10-27 19:56:55 +00002632 for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
2633 if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
2634 return true;
2635 }
2636 return false;
2637}
2638
Nate Begeman9008ca62009-04-27 18:41:29 +00002639/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2640/// the specified range (L, H].
2641static bool isUndefOrInRange(int Val, int Low, int Hi) {
2642 return (Val < 0) || (Val >= Low && Val < Hi);
2643}
2644
2645/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2646/// specified value.
2647static bool isUndefOrEqual(int Val, int CmpVal) {
2648 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002649 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002650 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002651}
2652
Nate Begeman9008ca62009-04-27 18:41:29 +00002653/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2654/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2655/// the second operand.
Owen Andersone50ed302009-08-10 22:56:29 +00002656static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002657 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
Nate Begeman9008ca62009-04-27 18:41:29 +00002658 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
Owen Anderson825b72b2009-08-11 20:47:22 +00002659 if (VT == MVT::v2f64 || VT == MVT::v2i64)
Nate Begeman9008ca62009-04-27 18:41:29 +00002660 return (Mask[0] < 2 && Mask[1] < 2);
2661 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002662}
2663
Nate Begeman9008ca62009-04-27 18:41:29 +00002664bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002665 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002666 N->getMask(M);
2667 return ::isPSHUFDMask(M, N->getValueType(0));
2668}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002669
Nate Begeman9008ca62009-04-27 18:41:29 +00002670/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2671/// is suitable for input to PSHUFHW.
Owen Andersone50ed302009-08-10 22:56:29 +00002672static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002673 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002674 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002675
Nate Begeman9008ca62009-04-27 18:41:29 +00002676 // Lower quadword copied in order or undef.
2677 for (int i = 0; i != 4; ++i)
2678 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002679 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002680
Evan Cheng506d3df2006-03-29 23:07:14 +00002681 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002682 for (int i = 4; i != 8; ++i)
2683 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002684 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002685
Evan Cheng506d3df2006-03-29 23:07:14 +00002686 return true;
2687}
2688
Nate Begeman9008ca62009-04-27 18:41:29 +00002689bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002690 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002691 N->getMask(M);
2692 return ::isPSHUFHWMask(M, N->getValueType(0));
2693}
Evan Cheng506d3df2006-03-29 23:07:14 +00002694
Nate Begeman9008ca62009-04-27 18:41:29 +00002695/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2696/// is suitable for input to PSHUFLW.
Owen Andersone50ed302009-08-10 22:56:29 +00002697static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002698 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002699 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002700
Rafael Espindola15684b22009-04-24 12:40:33 +00002701 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002702 for (int i = 4; i != 8; ++i)
2703 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002704 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002705
Rafael Espindola15684b22009-04-24 12:40:33 +00002706 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002707 for (int i = 0; i != 4; ++i)
2708 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002709 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002710
Rafael Espindola15684b22009-04-24 12:40:33 +00002711 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002712}
2713
Nate Begeman9008ca62009-04-27 18:41:29 +00002714bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
Eric Christopherfd179292009-08-27 18:07:15 +00002715 SmallVector<int, 8> M;
Nate Begeman9008ca62009-04-27 18:41:29 +00002716 N->getMask(M);
2717 return ::isPSHUFLWMask(M, N->getValueType(0));
2718}
2719
Nate Begemana09008b2009-10-19 02:17:23 +00002720/// isPALIGNRMask - Return true if the node specifies a shuffle of elements that
2721/// is suitable for input to PALIGNR.
2722static bool isPALIGNRMask(const SmallVectorImpl<int> &Mask, EVT VT,
2723 bool hasSSSE3) {
2724 int i, e = VT.getVectorNumElements();
2725
2726 // Do not handle v2i64 / v2f64 shuffles with palignr.
2727 if (e < 4 || !hasSSSE3)
2728 return false;
2729
2730 for (i = 0; i != e; ++i)
2731 if (Mask[i] >= 0)
2732 break;
2733
2734 // All undef, not a palignr.
2735 if (i == e)
2736 return false;
2737
2738 // Determine if it's ok to perform a palignr with only the LHS, since we
2739 // don't have access to the actual shuffle elements to see if RHS is undef.
2740 bool Unary = Mask[i] < (int)e;
2741 bool NeedsUnary = false;
2742
2743 int s = Mask[i] - i;
2744
2745 // Check the rest of the elements to see if they are consecutive.
2746 for (++i; i != e; ++i) {
2747 int m = Mask[i];
2748 if (m < 0)
2749 continue;
2750
2751 Unary = Unary && (m < (int)e);
2752 NeedsUnary = NeedsUnary || (m < s);
2753
2754 if (NeedsUnary && !Unary)
2755 return false;
2756 if (Unary && m != ((s+i) & (e-1)))
2757 return false;
2758 if (!Unary && m != (s+i))
2759 return false;
2760 }
2761 return true;
2762}
2763
2764bool X86::isPALIGNRMask(ShuffleVectorSDNode *N) {
2765 SmallVector<int, 8> M;
2766 N->getMask(M);
2767 return ::isPALIGNRMask(M, N->getValueType(0), true);
2768}
2769
Evan Cheng14aed5e2006-03-24 01:18:28 +00002770/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2771/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Owen Andersone50ed302009-08-10 22:56:29 +00002772static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002773 int NumElems = VT.getVectorNumElements();
2774 if (NumElems != 2 && NumElems != 4)
2775 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002776
Nate Begeman9008ca62009-04-27 18:41:29 +00002777 int Half = NumElems / 2;
2778 for (int i = 0; i < Half; ++i)
2779 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002780 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002781 for (int i = Half; i < NumElems; ++i)
2782 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002783 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002784
Evan Cheng14aed5e2006-03-24 01:18:28 +00002785 return true;
2786}
2787
Nate Begeman9008ca62009-04-27 18:41:29 +00002788bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2789 SmallVector<int, 8> M;
2790 N->getMask(M);
2791 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002792}
2793
Evan Cheng213d2cf2007-05-17 18:45:50 +00002794/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002795/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2796/// half elements to come from vector 1 (which would equal the dest.) and
2797/// the upper half to come from vector 2.
Owen Andersone50ed302009-08-10 22:56:29 +00002798static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002799 int NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00002800
2801 if (NumElems != 2 && NumElems != 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00002802 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002803
Nate Begeman9008ca62009-04-27 18:41:29 +00002804 int Half = NumElems / 2;
2805 for (int i = 0; i < Half; ++i)
2806 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002807 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002808 for (int i = Half; i < NumElems; ++i)
2809 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002810 return false;
2811 return true;
2812}
2813
Nate Begeman9008ca62009-04-27 18:41:29 +00002814static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2815 SmallVector<int, 8> M;
2816 N->getMask(M);
2817 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002818}
2819
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002820/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2821/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002822bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2823 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002824 return false;
2825
Evan Cheng2064a2b2006-03-28 06:50:32 +00002826 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002827 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2828 isUndefOrEqual(N->getMaskElt(1), 7) &&
2829 isUndefOrEqual(N->getMaskElt(2), 2) &&
2830 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002831}
2832
Nate Begeman0b10b912009-11-07 23:17:15 +00002833/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2834/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2835/// <2, 3, 2, 3>
2836bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2837 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2838
2839 if (NumElems != 4)
2840 return false;
2841
2842 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2843 isUndefOrEqual(N->getMaskElt(1), 3) &&
2844 isUndefOrEqual(N->getMaskElt(2), 2) &&
2845 isUndefOrEqual(N->getMaskElt(3), 3);
2846}
2847
Evan Cheng5ced1d82006-04-06 23:23:56 +00002848/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2849/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002850bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2851 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002852
Evan Cheng5ced1d82006-04-06 23:23:56 +00002853 if (NumElems != 2 && NumElems != 4)
2854 return false;
2855
Evan Chengc5cdff22006-04-07 21:53:05 +00002856 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002857 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002858 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002859
Evan Chengc5cdff22006-04-07 21:53:05 +00002860 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002861 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002862 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002863
2864 return true;
2865}
2866
Nate Begeman0b10b912009-11-07 23:17:15 +00002867/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
2868/// specifies a shuffle of elements that is suitable for input to MOVLHPS.
2869bool X86::isMOVLHPSMask(ShuffleVectorSDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002870 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002871
Evan Cheng5ced1d82006-04-06 23:23:56 +00002872 if (NumElems != 2 && NumElems != 4)
2873 return false;
2874
Evan Chengc5cdff22006-04-07 21:53:05 +00002875 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002876 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002877 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002878
Nate Begeman9008ca62009-04-27 18:41:29 +00002879 for (unsigned i = 0; i < NumElems/2; ++i)
2880 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002881 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002882
2883 return true;
2884}
2885
Evan Cheng0038e592006-03-28 00:39:58 +00002886/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2887/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Owen Andersone50ed302009-08-10 22:56:29 +00002888static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002889 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002890 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002891 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002892 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002893
Nate Begeman9008ca62009-04-27 18:41:29 +00002894 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2895 int BitI = Mask[i];
2896 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002897 if (!isUndefOrEqual(BitI, j))
2898 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002899 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002900 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002901 return false;
2902 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002903 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002904 return false;
2905 }
Evan Cheng0038e592006-03-28 00:39:58 +00002906 }
Evan Cheng0038e592006-03-28 00:39:58 +00002907 return true;
2908}
2909
Nate Begeman9008ca62009-04-27 18:41:29 +00002910bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2911 SmallVector<int, 8> M;
2912 N->getMask(M);
2913 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002914}
2915
Evan Cheng4fcb9222006-03-28 02:43:26 +00002916/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2917/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Eric Christopherfd179292009-08-27 18:07:15 +00002918static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, EVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002919 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002920 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002921 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002922 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002923
Nate Begeman9008ca62009-04-27 18:41:29 +00002924 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2925 int BitI = Mask[i];
2926 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002927 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002928 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002929 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002930 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002931 return false;
2932 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002933 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002934 return false;
2935 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002936 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002937 return true;
2938}
2939
Nate Begeman9008ca62009-04-27 18:41:29 +00002940bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2941 SmallVector<int, 8> M;
2942 N->getMask(M);
2943 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002944}
2945
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002946/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2947/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2948/// <0, 0, 1, 1>
Owen Andersone50ed302009-08-10 22:56:29 +00002949static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002950 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002951 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002952 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002953
Nate Begeman9008ca62009-04-27 18:41:29 +00002954 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2955 int BitI = Mask[i];
2956 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002957 if (!isUndefOrEqual(BitI, j))
2958 return false;
2959 if (!isUndefOrEqual(BitI1, j))
2960 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002961 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002962 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002963}
2964
Nate Begeman9008ca62009-04-27 18:41:29 +00002965bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2966 SmallVector<int, 8> M;
2967 N->getMask(M);
2968 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2969}
2970
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002971/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2972/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2973/// <2, 2, 3, 3>
Owen Andersone50ed302009-08-10 22:56:29 +00002974static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002975 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002976 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2977 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00002978
Nate Begeman9008ca62009-04-27 18:41:29 +00002979 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2980 int BitI = Mask[i];
2981 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002982 if (!isUndefOrEqual(BitI, j))
2983 return false;
2984 if (!isUndefOrEqual(BitI1, j))
2985 return false;
2986 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002987 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002988}
2989
Nate Begeman9008ca62009-04-27 18:41:29 +00002990bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2991 SmallVector<int, 8> M;
2992 N->getMask(M);
2993 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2994}
2995
Evan Cheng017dcc62006-04-21 01:05:10 +00002996/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2997/// specifies a shuffle of elements that is suitable for input to MOVSS,
2998/// MOVSD, and MOVD, i.e. setting the lowest element.
Owen Andersone50ed302009-08-10 22:56:29 +00002999static bool isMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00003000 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003001 return false;
Eli Friedman10415532009-06-06 06:05:10 +00003002
3003 int NumElts = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003004
Nate Begeman9008ca62009-04-27 18:41:29 +00003005 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003006 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003007
Nate Begeman9008ca62009-04-27 18:41:29 +00003008 for (int i = 1; i < NumElts; ++i)
3009 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003010 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003011
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003012 return true;
3013}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003014
Nate Begeman9008ca62009-04-27 18:41:29 +00003015bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
3016 SmallVector<int, 8> M;
3017 N->getMask(M);
3018 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00003019}
3020
Evan Cheng017dcc62006-04-21 01:05:10 +00003021/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
3022/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00003023/// element of vector 2 and the other elements to come from vector 1 in order.
Owen Andersone50ed302009-08-10 22:56:29 +00003024static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, EVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003025 bool V2IsSplat = false, bool V2IsUndef = false) {
3026 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00003027 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00003028 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003029
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00003031 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003032
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 for (int i = 1; i < NumOps; ++i)
3034 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
3035 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
3036 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003037 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00003038
Evan Cheng39623da2006-04-20 08:58:49 +00003039 return true;
3040}
3041
Nate Begeman9008ca62009-04-27 18:41:29 +00003042static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00003043 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003044 SmallVector<int, 8> M;
3045 N->getMask(M);
3046 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00003047}
3048
Evan Chengd9539472006-04-14 21:59:03 +00003049/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3050/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003051bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
3052 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003053 return false;
3054
3055 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00003056 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003057 int Elt = N->getMaskElt(i);
3058 if (Elt >= 0 && Elt != 1)
3059 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00003060 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003061
3062 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003063 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003064 int Elt = N->getMaskElt(i);
3065 if (Elt >= 0 && Elt != 3)
3066 return false;
3067 if (Elt == 3)
3068 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003069 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003070 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00003071 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003072 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003073}
3074
3075/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3076/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003077bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
3078 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00003079 return false;
3080
3081 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00003082 for (unsigned i = 0; i < 2; ++i)
3083 if (N->getMaskElt(i) > 0)
3084 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003085
3086 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00003087 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003088 int Elt = N->getMaskElt(i);
3089 if (Elt >= 0 && Elt != 2)
3090 return false;
3091 if (Elt == 2)
3092 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00003093 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003094 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00003095 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00003096}
3097
Evan Cheng0b457f02008-09-25 20:50:48 +00003098/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
3099/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00003100bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
3101 int e = N->getValueType(0).getVectorNumElements() / 2;
Eric Christopherfd179292009-08-27 18:07:15 +00003102
Nate Begeman9008ca62009-04-27 18:41:29 +00003103 for (int i = 0; i < e; ++i)
3104 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003105 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003106 for (int i = 0; i < e; ++i)
3107 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00003108 return false;
3109 return true;
3110}
3111
Evan Cheng63d33002006-03-22 08:01:21 +00003112/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003113/// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions.
Evan Cheng63d33002006-03-22 08:01:21 +00003114unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003115 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3116 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
3117
Evan Chengb9df0ca2006-03-22 02:53:00 +00003118 unsigned Shift = (NumOperands == 4) ? 2 : 1;
3119 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003120 for (int i = 0; i < NumOperands; ++i) {
3121 int Val = SVOp->getMaskElt(NumOperands-i-1);
3122 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00003123 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00003124 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00003125 if (i != NumOperands - 1)
3126 Mask <<= Shift;
3127 }
Evan Cheng63d33002006-03-22 08:01:21 +00003128 return Mask;
3129}
3130
Evan Cheng506d3df2006-03-29 23:07:14 +00003131/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003132/// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003133unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003134 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003135 unsigned Mask = 0;
3136 // 8 nodes, but we only care about the last 4.
3137 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003138 int Val = SVOp->getMaskElt(i);
3139 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00003140 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00003141 if (i != 4)
3142 Mask <<= 2;
3143 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003144 return Mask;
3145}
3146
3147/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
Nate Begemana09008b2009-10-19 02:17:23 +00003148/// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction.
Evan Cheng506d3df2006-03-29 23:07:14 +00003149unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00003151 unsigned Mask = 0;
3152 // 8 nodes, but we only care about the first 4.
3153 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003154 int Val = SVOp->getMaskElt(i);
3155 if (Val >= 0)
3156 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00003157 if (i != 0)
3158 Mask <<= 2;
3159 }
Evan Cheng506d3df2006-03-29 23:07:14 +00003160 return Mask;
3161}
3162
Nate Begemana09008b2009-10-19 02:17:23 +00003163/// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle
3164/// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction.
3165unsigned X86::getShufflePALIGNRImmediate(SDNode *N) {
3166 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
3167 EVT VVT = N->getValueType(0);
3168 unsigned EltSize = VVT.getVectorElementType().getSizeInBits() >> 3;
3169 int Val = 0;
3170
3171 unsigned i, e;
3172 for (i = 0, e = VVT.getVectorNumElements(); i != e; ++i) {
3173 Val = SVOp->getMaskElt(i);
3174 if (Val >= 0)
3175 break;
3176 }
3177 return (Val - i) * EltSize;
3178}
3179
Evan Cheng37b73872009-07-30 08:33:02 +00003180/// isZeroNode - Returns true if Elt is a constant zero or a floating point
3181/// constant +0.0.
3182bool X86::isZeroNode(SDValue Elt) {
3183 return ((isa<ConstantSDNode>(Elt) &&
3184 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
3185 (isa<ConstantFPSDNode>(Elt) &&
3186 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
3187}
3188
Nate Begeman9008ca62009-04-27 18:41:29 +00003189/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
3190/// their permute mask.
3191static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
3192 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003193 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003194 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00003195 SmallVector<int, 8> MaskVec;
Eric Christopherfd179292009-08-27 18:07:15 +00003196
Nate Begeman5a5ca152009-04-29 05:20:52 +00003197 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003198 int idx = SVOp->getMaskElt(i);
3199 if (idx < 0)
3200 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003201 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003202 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003203 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003204 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003205 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003206 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
3207 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00003208}
3209
Evan Cheng779ccea2007-12-07 21:30:01 +00003210/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
3211/// the two vector operands have swapped position.
Owen Andersone50ed302009-08-10 22:56:29 +00003212static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, EVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00003213 unsigned NumElems = VT.getVectorNumElements();
3214 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003215 int idx = Mask[i];
3216 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003217 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003218 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00003219 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003220 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003221 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003222 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003223}
3224
Evan Cheng533a0aa2006-04-19 20:35:22 +00003225/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
3226/// match movhlps. The lower half elements should come from upper half of
3227/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003228/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00003229static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
3230 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00003231 return false;
3232 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003234 return false;
3235 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003236 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003237 return false;
3238 return true;
3239}
3240
Evan Cheng5ced1d82006-04-06 23:23:56 +00003241/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00003242/// is promoted to a vector. It also returns the LoadSDNode by reference if
3243/// required.
3244static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00003245 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
3246 return false;
3247 N = N->getOperand(0).getNode();
3248 if (!ISD::isNON_EXTLoad(N))
3249 return false;
3250 if (LD)
3251 *LD = cast<LoadSDNode>(N);
3252 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003253}
3254
Evan Cheng533a0aa2006-04-19 20:35:22 +00003255/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
3256/// match movlp{s|d}. The lower half elements should come from lower half of
3257/// V1 (and in order), and the upper half elements should come from the upper
3258/// half of V2 (and in order). And since V1 will become the source of the
3259/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003260static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
3261 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00003262 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003263 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00003264 // Is V2 is a vector load, don't do this transformation. We will try to use
3265 // load folding shufps op.
3266 if (ISD::isNON_EXTLoad(V2))
3267 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003268
Nate Begeman5a5ca152009-04-29 05:20:52 +00003269 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003270
Evan Cheng533a0aa2006-04-19 20:35:22 +00003271 if (NumElems != 2 && NumElems != 4)
3272 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003273 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003274 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003275 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00003276 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003277 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00003278 return false;
3279 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003280}
3281
Evan Cheng39623da2006-04-20 08:58:49 +00003282/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
3283/// all the same.
3284static bool isSplatVector(SDNode *N) {
3285 if (N->getOpcode() != ISD::BUILD_VECTOR)
3286 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003287
Dan Gohman475871a2008-07-27 21:46:04 +00003288 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00003289 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
3290 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00003291 return false;
3292 return true;
3293}
3294
Evan Cheng213d2cf2007-05-17 18:45:50 +00003295/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Eric Christopherfd179292009-08-27 18:07:15 +00003296/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00003297/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00003298static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00003299 SDValue V1 = N->getOperand(0);
3300 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003301 unsigned NumElems = N->getValueType(0).getVectorNumElements();
3302 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003303 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003304 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003305 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00003306 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
3307 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003308 if (Opc != ISD::BUILD_VECTOR ||
3309 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00003310 return false;
3311 } else if (Idx >= 0) {
3312 unsigned Opc = V1.getOpcode();
3313 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
3314 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00003315 if (Opc != ISD::BUILD_VECTOR ||
3316 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00003317 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00003318 }
3319 }
3320 return true;
3321}
3322
3323/// getZeroVector - Returns a vector of specified type with all zero elements.
3324///
Owen Andersone50ed302009-08-10 22:56:29 +00003325static SDValue getZeroVector(EVT VT, bool HasSSE2, SelectionDAG &DAG,
Dale Johannesenace16102009-02-03 19:33:06 +00003326 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003327 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003328
Chris Lattner8a594482007-11-25 00:24:49 +00003329 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3330 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00003331 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003332 if (VT.getSizeInBits() == 64) { // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003333 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3334 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003335 } else if (HasSSE2) { // SSE2
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3337 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003338 } else { // SSE1
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
3340 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00003341 }
Dale Johannesenace16102009-02-03 19:33:06 +00003342 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003343}
3344
Chris Lattner8a594482007-11-25 00:24:49 +00003345/// getOnesVector - Returns a vector of specified type with all bits set.
3346///
Owen Andersone50ed302009-08-10 22:56:29 +00003347static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003348 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00003349
Chris Lattner8a594482007-11-25 00:24:49 +00003350 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
3351 // type. This ensures they get CSE'd.
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +00003353 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003354 if (VT.getSizeInBits() == 64) // MMX
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00003356 else // SSE
Owen Anderson825b72b2009-08-11 20:47:22 +00003357 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00003358 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00003359}
3360
3361
Evan Cheng39623da2006-04-20 08:58:49 +00003362/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
3363/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00003364static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003365 EVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00003366 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfd179292009-08-27 18:07:15 +00003367
Evan Cheng39623da2006-04-20 08:58:49 +00003368 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003369 SmallVector<int, 8> MaskVec;
3370 SVOp->getMask(MaskVec);
Eric Christopherfd179292009-08-27 18:07:15 +00003371
Nate Begeman5a5ca152009-04-29 05:20:52 +00003372 for (unsigned i = 0; i != NumElems; ++i) {
3373 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003374 MaskVec[i] = NumElems;
3375 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00003376 }
Evan Cheng39623da2006-04-20 08:58:49 +00003377 }
Evan Cheng39623da2006-04-20 08:58:49 +00003378 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00003379 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
3380 SVOp->getOperand(1), &MaskVec[0]);
3381 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00003382}
3383
Evan Cheng017dcc62006-04-21 01:05:10 +00003384/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
3385/// operation of specified width.
Owen Andersone50ed302009-08-10 22:56:29 +00003386static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003387 SDValue V2) {
3388 unsigned NumElems = VT.getVectorNumElements();
3389 SmallVector<int, 8> Mask;
3390 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00003391 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003392 Mask.push_back(i);
3393 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00003394}
3395
Nate Begeman9008ca62009-04-27 18:41:29 +00003396/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003397static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003398 SDValue V2) {
3399 unsigned NumElems = VT.getVectorNumElements();
3400 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00003401 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003402 Mask.push_back(i);
3403 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00003404 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003405 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00003406}
3407
Nate Begeman9008ca62009-04-27 18:41:29 +00003408/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
Owen Andersone50ed302009-08-10 22:56:29 +00003409static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, EVT VT, SDValue V1,
Nate Begeman9008ca62009-04-27 18:41:29 +00003410 SDValue V2) {
3411 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00003412 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00003413 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00003414 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003415 Mask.push_back(i + Half);
3416 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00003417 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003418 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003419}
3420
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003421/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Eric Christopherfd179292009-08-27 18:07:15 +00003422static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00003423 bool HasSSE2) {
3424 if (SV->getValueType(0).getVectorNumElements() <= 4)
3425 return SDValue(SV, 0);
Eric Christopherfd179292009-08-27 18:07:15 +00003426
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 EVT PVT = MVT::v4f32;
Owen Andersone50ed302009-08-10 22:56:29 +00003428 EVT VT = SV->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00003429 DebugLoc dl = SV->getDebugLoc();
3430 SDValue V1 = SV->getOperand(0);
3431 int NumElems = VT.getVectorNumElements();
3432 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00003433
Nate Begeman9008ca62009-04-27 18:41:29 +00003434 // unpack elements to the correct location
3435 while (NumElems > 4) {
3436 if (EltNo < NumElems/2) {
3437 V1 = getUnpackl(DAG, dl, VT, V1, V1);
3438 } else {
3439 V1 = getUnpackh(DAG, dl, VT, V1, V1);
3440 EltNo -= NumElems/2;
3441 }
3442 NumElems >>= 1;
3443 }
Eric Christopherfd179292009-08-27 18:07:15 +00003444
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 // Perform the splat.
3446 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00003447 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
3449 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00003450}
3451
Evan Chengba05f722006-04-21 23:03:30 +00003452/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003453/// vector of zero or undef vector. This produces a shuffle where the low
3454/// element of V2 is swizzled into the zero/undef vector, landing at element
3455/// 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 +00003456static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003457 bool isZero, bool HasSSE2,
3458 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00003459 EVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003460 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00003461 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
3462 unsigned NumElems = VT.getVectorNumElements();
3463 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003464 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 // If this is the insertion idx, put the low elt of V2 here.
3466 MaskVec.push_back(i == Idx ? NumElems : i);
3467 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00003468}
3469
Evan Chengf26ffe92008-05-29 08:22:04 +00003470/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3471/// a shuffle that is zero.
3472static
Nate Begeman9008ca62009-04-27 18:41:29 +00003473unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3474 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003475 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003476 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003477 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 int Idx = SVOp->getMaskElt(Index);
3479 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003480 ++NumZeros;
3481 continue;
3482 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003483 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003484 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003485 ++NumZeros;
3486 else
3487 break;
3488 }
3489 return NumZeros;
3490}
3491
3492/// isVectorShift - Returns true if the shuffle can be implemented as a
3493/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003494/// FIXME: split into pslldqi, psrldqi, palignr variants.
3495static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003496 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
John McCallb1fb4492010-04-07 01:49:15 +00003497 unsigned NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003498
3499 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003500 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003501 if (!NumZeros) {
3502 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003503 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003504 if (!NumZeros)
3505 return false;
3506 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003507 bool SeenV1 = false;
3508 bool SeenV2 = false;
John McCallb1fb4492010-04-07 01:49:15 +00003509 for (unsigned i = NumZeros; i < NumElems; ++i) {
3510 unsigned Val = isLeft ? (i - NumZeros) : i;
3511 int Idx_ = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3512 if (Idx_ < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003513 continue;
John McCallb1fb4492010-04-07 01:49:15 +00003514 unsigned Idx = (unsigned) Idx_;
Nate Begeman9008ca62009-04-27 18:41:29 +00003515 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003516 SeenV1 = true;
3517 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003518 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003519 SeenV2 = true;
3520 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003521 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003522 return false;
3523 }
3524 if (SeenV1 && SeenV2)
3525 return false;
3526
Nate Begeman9008ca62009-04-27 18:41:29 +00003527 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003528 ShAmt = NumZeros;
3529 return true;
3530}
3531
3532
Evan Chengc78d3b42006-04-24 18:01:45 +00003533/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3534///
Dan Gohman475871a2008-07-27 21:46:04 +00003535static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003536 unsigned NumNonZero, unsigned NumZero,
Dan Gohmand858e902010-04-17 15:26:15 +00003537 SelectionDAG &DAG,
3538 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003539 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003540 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003541
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003542 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003543 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003544 bool First = true;
3545 for (unsigned i = 0; i < 16; ++i) {
3546 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3547 if (ThisIsNonZero && First) {
3548 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003549 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003550 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003551 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003552 First = false;
3553 }
3554
3555 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003556 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003557 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3558 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003559 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003561 }
3562 if (ThisIsNonZero) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003563 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3564 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
3565 ThisElt, DAG.getConstant(8, MVT::i8));
Evan Chengc78d3b42006-04-24 18:01:45 +00003566 if (LastIsNonZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003568 } else
3569 ThisElt = LastElt;
3570
Gabor Greifba36cb52008-08-28 21:40:38 +00003571 if (ThisElt.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003573 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003574 }
3575 }
3576
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003578}
3579
Bill Wendlinga348c562007-03-22 18:42:45 +00003580/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003581///
Dan Gohman475871a2008-07-27 21:46:04 +00003582static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Dan Gohmand858e902010-04-17 15:26:15 +00003583 unsigned NumNonZero, unsigned NumZero,
3584 SelectionDAG &DAG,
3585 const TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003586 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003587 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003588
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003589 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003590 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003591 bool First = true;
3592 for (unsigned i = 0; i < 8; ++i) {
3593 bool isNonZero = (NonZeros & (1 << i)) != 0;
3594 if (isNonZero) {
3595 if (First) {
3596 if (NumZero)
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003598 else
Owen Anderson825b72b2009-08-11 20:47:22 +00003599 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003600 First = false;
3601 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003602 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003603 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003604 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003605 }
3606 }
3607
3608 return V;
3609}
3610
Evan Chengf26ffe92008-05-29 08:22:04 +00003611/// getVShift - Return a vector logical shift node.
3612///
Owen Andersone50ed302009-08-10 22:56:29 +00003613static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003614 unsigned NumBits, SelectionDAG &DAG,
3615 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003616 bool isMMX = VT.getSizeInBits() == 64;
Owen Anderson825b72b2009-08-11 20:47:22 +00003617 EVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003618 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003619 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3620 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3621 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003622 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003623}
3624
Dan Gohman475871a2008-07-27 21:46:04 +00003625SDValue
Evan Chengc3630942009-12-09 21:00:30 +00003626X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, DebugLoc dl,
Dan Gohmand858e902010-04-17 15:26:15 +00003627 SelectionDAG &DAG) const {
Evan Chengc3630942009-12-09 21:00:30 +00003628
3629 // Check if the scalar load can be widened into a vector load. And if
3630 // the address is "base + cst" see if the cst can be "absorbed" into
3631 // the shuffle mask.
3632 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
3633 SDValue Ptr = LD->getBasePtr();
3634 if (!ISD::isNormalLoad(LD) || LD->isVolatile())
3635 return SDValue();
3636 EVT PVT = LD->getValueType(0);
3637 if (PVT != MVT::i32 && PVT != MVT::f32)
3638 return SDValue();
3639
3640 int FI = -1;
3641 int64_t Offset = 0;
3642 if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
3643 FI = FINode->getIndex();
3644 Offset = 0;
3645 } else if (Ptr.getOpcode() == ISD::ADD &&
3646 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
3647 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
3648 FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
3649 Offset = Ptr.getConstantOperandVal(1);
3650 Ptr = Ptr.getOperand(0);
3651 } else {
3652 return SDValue();
3653 }
3654
3655 SDValue Chain = LD->getChain();
3656 // Make sure the stack object alignment is at least 16.
3657 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3658 if (DAG.InferPtrAlignment(Ptr) < 16) {
3659 if (MFI->isFixedObjectIndex(FI)) {
Eric Christophere9625cf2010-01-23 06:02:43 +00003660 // Can't change the alignment. FIXME: It's possible to compute
3661 // the exact stack offset and reference FI + adjust offset instead.
3662 // If someone *really* cares about this. That's the way to implement it.
3663 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003664 } else {
3665 MFI->setObjectAlignment(FI, 16);
3666 }
3667 }
3668
3669 // (Offset % 16) must be multiple of 4. Then address is then
3670 // Ptr + (Offset & ~15).
3671 if (Offset < 0)
3672 return SDValue();
3673 if ((Offset % 16) & 3)
3674 return SDValue();
3675 int64_t StartOffset = Offset & ~15;
3676 if (StartOffset)
3677 Ptr = DAG.getNode(ISD::ADD, Ptr.getDebugLoc(), Ptr.getValueType(),
3678 Ptr,DAG.getConstant(StartOffset, Ptr.getValueType()));
3679
3680 int EltNo = (Offset - StartOffset) >> 2;
3681 int Mask[4] = { EltNo, EltNo, EltNo, EltNo };
3682 EVT VT = (PVT == MVT::i32) ? MVT::v4i32 : MVT::v4f32;
David Greene67c9d422010-02-15 16:53:33 +00003683 SDValue V1 = DAG.getLoad(VT, dl, Chain, Ptr,LD->getSrcValue(),0,
3684 false, false, 0);
Evan Chengc3630942009-12-09 21:00:30 +00003685 // Canonicalize it to a v4i32 shuffle.
3686 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32, V1);
3687 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3688 DAG.getVectorShuffle(MVT::v4i32, dl, V1,
3689 DAG.getUNDEF(MVT::v4i32), &Mask[0]));
3690 }
3691
3692 return SDValue();
3693}
3694
Nate Begeman1449f292010-03-24 22:19:06 +00003695/// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a
3696/// vector of type 'VT', see if the elements can be replaced by a single large
3697/// load which has the same value as a build_vector whose operands are 'elts'.
3698///
3699/// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
3700///
3701/// FIXME: we'd also like to handle the case where the last elements are zero
3702/// rather than undef via VZEXT_LOAD, but we do not detect that case today.
3703/// There's even a handy isZeroNode for that purpose.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003704static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts,
3705 DebugLoc &dl, SelectionDAG &DAG) {
3706 EVT EltVT = VT.getVectorElementType();
3707 unsigned NumElems = Elts.size();
3708
Nate Begemanfdea31a2010-03-24 20:49:50 +00003709 LoadSDNode *LDBase = NULL;
3710 unsigned LastLoadedElt = -1U;
Nate Begeman1449f292010-03-24 22:19:06 +00003711
3712 // For each element in the initializer, see if we've found a load or an undef.
3713 // If we don't find an initial load element, or later load elements are
3714 // non-consecutive, bail out.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003715 for (unsigned i = 0; i < NumElems; ++i) {
3716 SDValue Elt = Elts[i];
3717
3718 if (!Elt.getNode() ||
3719 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
3720 return SDValue();
3721 if (!LDBase) {
3722 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
3723 return SDValue();
3724 LDBase = cast<LoadSDNode>(Elt.getNode());
3725 LastLoadedElt = i;
3726 continue;
3727 }
3728 if (Elt.getOpcode() == ISD::UNDEF)
3729 continue;
3730
3731 LoadSDNode *LD = cast<LoadSDNode>(Elt);
3732 if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i))
3733 return SDValue();
3734 LastLoadedElt = i;
3735 }
Nate Begeman1449f292010-03-24 22:19:06 +00003736
3737 // If we have found an entire vector of loads and undefs, then return a large
3738 // load of the entire vector width starting at the base pointer. If we found
3739 // consecutive loads for the low half, generate a vzext_load node.
Nate Begemanfdea31a2010-03-24 20:49:50 +00003740 if (LastLoadedElt == NumElems - 1) {
3741 if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16)
3742 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3743 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3744 LDBase->isVolatile(), LDBase->isNonTemporal(), 0);
3745 return DAG.getLoad(VT, dl, LDBase->getChain(), LDBase->getBasePtr(),
3746 LDBase->getSrcValue(), LDBase->getSrcValueOffset(),
3747 LDBase->isVolatile(), LDBase->isNonTemporal(),
3748 LDBase->getAlignment());
3749 } else if (NumElems == 4 && LastLoadedElt == 1) {
3750 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
3751 SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
3752 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
3753 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
3754 }
3755 return SDValue();
3756}
3757
Evan Chengc3630942009-12-09 21:00:30 +00003758SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00003759X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003760 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003761 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003762 if (ISD::isBuildVectorAllZeros(Op.getNode())
3763 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003764 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3765 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3766 // eliminated on x86-32 hosts.
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
Chris Lattner8a594482007-11-25 00:24:49 +00003768 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003769
Gabor Greifba36cb52008-08-28 21:40:38 +00003770 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003771 return getOnesVector(Op.getValueType(), DAG, dl);
3772 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003773 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003774
Owen Andersone50ed302009-08-10 22:56:29 +00003775 EVT VT = Op.getValueType();
3776 EVT ExtVT = VT.getVectorElementType();
3777 unsigned EVTBits = ExtVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003778
3779 unsigned NumElems = Op.getNumOperands();
3780 unsigned NumZero = 0;
3781 unsigned NumNonZero = 0;
3782 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003783 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003784 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003785 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003786 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003787 if (Elt.getOpcode() == ISD::UNDEF)
3788 continue;
3789 Values.insert(Elt);
3790 if (Elt.getOpcode() != ISD::Constant &&
3791 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003792 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003793 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003794 NumZero++;
3795 else {
3796 NonZeros |= (1 << i);
3797 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003798 }
3799 }
3800
Dan Gohman7f321562007-06-25 16:23:39 +00003801 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003802 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003803 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003804 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003805
Chris Lattner67f453a2008-03-09 05:42:06 +00003806 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003807 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003808 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003809 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003810
Chris Lattner62098042008-03-09 01:05:04 +00003811 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3812 // the value are obviously zero, truncate the value to i32 and do the
3813 // insertion that way. Only do this if the value is non-constant or if the
3814 // value is a constant being inserted into element 0. It is cheaper to do
3815 // a constant pool load than it is to do a movd + shuffle.
Owen Anderson825b72b2009-08-11 20:47:22 +00003816 if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
Chris Lattner62098042008-03-09 01:05:04 +00003817 (!IsAllConstants || Idx == 0)) {
3818 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3819 // Handle MMX and SSE both.
Owen Anderson825b72b2009-08-11 20:47:22 +00003820 EVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3821 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003822
Chris Lattner62098042008-03-09 01:05:04 +00003823 // Truncate the value (which may itself be a constant) to i32, and
3824 // convert it to a vector with movd (S2V+shuffle to zero extend).
Owen Anderson825b72b2009-08-11 20:47:22 +00003825 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
Dale Johannesenace16102009-02-03 19:33:06 +00003826 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003827 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3828 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003829
Chris Lattner62098042008-03-09 01:05:04 +00003830 // Now we have our 32-bit value zero extended in the low element of
3831 // a vector. If Idx != 0, swizzle it into place.
3832 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003833 SmallVector<int, 4> Mask;
3834 Mask.push_back(Idx);
3835 for (unsigned i = 1; i != VecElts; ++i)
3836 Mask.push_back(i);
3837 Item = DAG.getVectorShuffle(VecVT, dl, Item,
Eric Christopherfd179292009-08-27 18:07:15 +00003838 DAG.getUNDEF(Item.getValueType()),
Nate Begeman9008ca62009-04-27 18:41:29 +00003839 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003840 }
Dale Johannesenace16102009-02-03 19:33:06 +00003841 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003842 }
3843 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003844
Chris Lattner19f79692008-03-08 22:59:52 +00003845 // If we have a constant or non-constant insertion into the low element of
3846 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3847 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003848 // depending on what the source datatype is.
3849 if (Idx == 0) {
3850 if (NumZero == 0) {
3851 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Owen Anderson825b72b2009-08-11 20:47:22 +00003852 } else if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
3853 (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
Eli Friedman10415532009-06-06 06:05:10 +00003854 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3855 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3856 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3857 DAG);
Owen Anderson825b72b2009-08-11 20:47:22 +00003858 } else if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
3859 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3860 EVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
Eli Friedman10415532009-06-06 06:05:10 +00003861 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3862 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3863 Subtarget->hasSSE2(), DAG);
3864 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3865 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003866 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003867
3868 // Is it a vector logical left shift?
3869 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003870 X86::isZeroNode(Op.getOperand(0)) &&
3871 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003872 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003873 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003874 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003875 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003876 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003877 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003878
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003879 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003880 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003881
Chris Lattner19f79692008-03-08 22:59:52 +00003882 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3883 // is a non-constant being inserted into an element other than the low one,
3884 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3885 // movd/movss) to move this into the low element, then shuffle it into
3886 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003887 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003888 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003889
Evan Cheng0db9fe62006-04-25 20:13:52 +00003890 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003891 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3892 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003893 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003894 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003895 MaskVec.push_back(i == Idx ? 0 : 1);
3896 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003897 }
3898 }
3899
Chris Lattner67f453a2008-03-09 05:42:06 +00003900 // Splat is obviously ok. Let legalizer expand it to a shuffle.
Evan Chengc3630942009-12-09 21:00:30 +00003901 if (Values.size() == 1) {
3902 if (EVTBits == 32) {
3903 // Instead of a shuffle like this:
3904 // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
3905 // Check if it's possible to issue this instead.
3906 // shuffle (vload ptr)), undef, <1, 1, 1, 1>
3907 unsigned Idx = CountTrailingZeros_32(NonZeros);
3908 SDValue Item = Op.getOperand(Idx);
3909 if (Op.getNode()->isOnlyUserOf(Item.getNode()))
3910 return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
3911 }
Dan Gohman475871a2008-07-27 21:46:04 +00003912 return SDValue();
Evan Chengc3630942009-12-09 21:00:30 +00003913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003914
Dan Gohmana3941172007-07-24 22:55:08 +00003915 // A vector full of immediates; various special cases are already
3916 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003917 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003918 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003919
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003920 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003921 if (EVTBits == 64) {
3922 if (NumNonZero == 1) {
3923 // One half is zero or undef.
3924 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003925 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003926 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003927 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3928 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003929 }
Dan Gohman475871a2008-07-27 21:46:04 +00003930 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003931 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003932
3933 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003934 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003935 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003936 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003937 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003938 }
3939
Bill Wendling826f36f2007-03-28 00:57:11 +00003940 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003941 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003942 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003943 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003944 }
3945
3946 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003947 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003948 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003949 if (NumElems == 4 && NumZero > 0) {
3950 for (unsigned i = 0; i < 4; ++i) {
3951 bool isZero = !(NonZeros & (1 << i));
3952 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003953 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003954 else
Dale Johannesenace16102009-02-03 19:33:06 +00003955 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003956 }
3957
3958 for (unsigned i = 0; i < 2; ++i) {
3959 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3960 default: break;
3961 case 0:
3962 V[i] = V[i*2]; // Must be a zero vector.
3963 break;
3964 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003965 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003966 break;
3967 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003969 break;
3970 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003971 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003972 break;
3973 }
3974 }
3975
Nate Begeman9008ca62009-04-27 18:41:29 +00003976 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003977 bool Reverse = (NonZeros & 0x3) == 2;
3978 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003979 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003980 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3981 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003982 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3983 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003984 }
3985
Nate Begemanfdea31a2010-03-24 20:49:50 +00003986 if (Values.size() > 1 && VT.getSizeInBits() == 128) {
3987 // Check for a build vector of consecutive loads.
3988 for (unsigned i = 0; i < NumElems; ++i)
3989 V[i] = Op.getOperand(i);
3990
3991 // Check for elements which are consecutive loads.
3992 SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG);
3993 if (LD.getNode())
3994 return LD;
3995
3996 // For SSE 4.1, use inserts into undef.
3997 if (getSubtarget()->hasSSE41()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003998 V[0] = DAG.getUNDEF(VT);
3999 for (unsigned i = 0; i < NumElems; ++i)
4000 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
4001 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
4002 Op.getOperand(i), DAG.getIntPtrConstant(i));
4003 return V[0];
4004 }
Nate Begemanfdea31a2010-03-24 20:49:50 +00004005
4006 // Otherwise, expand into a number of unpckl*
Evan Cheng0db9fe62006-04-25 20:13:52 +00004007 // e.g. for v4f32
4008 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
4009 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
4010 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00004011 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00004012 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004013 NumElems >>= 1;
4014 while (NumElems != 0) {
4015 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004016 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004017 NumElems >>= 1;
4018 }
4019 return V[0];
4020 }
Dan Gohman475871a2008-07-27 21:46:04 +00004021 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004022}
4023
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004024SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004025X86TargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
Mon P Wangeb38ebf2010-01-24 00:05:03 +00004026 // We support concatenate two MMX registers and place them in a MMX
4027 // register. This is better than doing a stack convert.
4028 DebugLoc dl = Op.getDebugLoc();
4029 EVT ResVT = Op.getValueType();
4030 assert(Op.getNumOperands() == 2);
4031 assert(ResVT == MVT::v2i64 || ResVT == MVT::v4i32 ||
4032 ResVT == MVT::v8i16 || ResVT == MVT::v16i8);
4033 int Mask[2];
4034 SDValue InVec = DAG.getNode(ISD::BIT_CONVERT,dl, MVT::v1i64, Op.getOperand(0));
4035 SDValue VecOp = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4036 InVec = Op.getOperand(1);
4037 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4038 unsigned NumElts = ResVT.getVectorNumElements();
4039 VecOp = DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4040 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ResVT, VecOp,
4041 InVec.getOperand(0), DAG.getIntPtrConstant(NumElts/2+1));
4042 } else {
4043 InVec = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v1i64, InVec);
4044 SDValue VecOp2 = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, InVec);
4045 Mask[0] = 0; Mask[1] = 2;
4046 VecOp = DAG.getVectorShuffle(MVT::v2i64, dl, VecOp, VecOp2, Mask);
4047 }
4048 return DAG.getNode(ISD::BIT_CONVERT, dl, ResVT, VecOp);
4049}
4050
Nate Begemanb9a47b82009-02-23 08:49:38 +00004051// v8i16 shuffles - Prefer shuffles in the following order:
4052// 1. [all] pshuflw, pshufhw, optional move
4053// 2. [ssse3] 1 x pshufb
4054// 3. [ssse3] 2 x pshufb + 1 x por
4055// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004056static
Nate Begeman9008ca62009-04-27 18:41:29 +00004057SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004058 SelectionDAG &DAG,
4059 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004060 SDValue V1 = SVOp->getOperand(0);
4061 SDValue V2 = SVOp->getOperand(1);
4062 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004063 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00004064
Nate Begemanb9a47b82009-02-23 08:49:38 +00004065 // Determine if more than 1 of the words in each of the low and high quadwords
4066 // of the result come from the same quadword of one of the two inputs. Undef
4067 // mask values count as coming from any quadword, for better codegen.
4068 SmallVector<unsigned, 4> LoQuad(4);
4069 SmallVector<unsigned, 4> HiQuad(4);
4070 BitVector InputQuads(4);
4071 for (unsigned i = 0; i < 8; ++i) {
4072 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00004073 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004074 MaskVals.push_back(EltIdx);
4075 if (EltIdx < 0) {
4076 ++Quad[0];
4077 ++Quad[1];
4078 ++Quad[2];
4079 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00004080 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004081 }
4082 ++Quad[EltIdx / 4];
4083 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00004084 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004085
Nate Begemanb9a47b82009-02-23 08:49:38 +00004086 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004087 unsigned MaxQuad = 1;
4088 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004089 if (LoQuad[i] > MaxQuad) {
4090 BestLoQuad = i;
4091 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004092 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004093 }
4094
Nate Begemanb9a47b82009-02-23 08:49:38 +00004095 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00004096 MaxQuad = 1;
4097 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004098 if (HiQuad[i] > MaxQuad) {
4099 BestHiQuad = i;
4100 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00004101 }
4102 }
4103
Nate Begemanb9a47b82009-02-23 08:49:38 +00004104 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
Eric Christopherfd179292009-08-27 18:07:15 +00004105 // of the two input vectors, shuffle them into one input vector so only a
Nate Begemanb9a47b82009-02-23 08:49:38 +00004106 // single pshufb instruction is necessary. If There are more than 2 input
4107 // quads, disable the next transformation since it does not help SSSE3.
4108 bool V1Used = InputQuads[0] || InputQuads[1];
4109 bool V2Used = InputQuads[2] || InputQuads[3];
4110 if (TLI.getSubtarget()->hasSSSE3()) {
4111 if (InputQuads.count() == 2 && V1Used && V2Used) {
4112 BestLoQuad = InputQuads.find_first();
4113 BestHiQuad = InputQuads.find_next(BestLoQuad);
4114 }
4115 if (InputQuads.count() > 2) {
4116 BestLoQuad = -1;
4117 BestHiQuad = -1;
4118 }
4119 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004120
Nate Begemanb9a47b82009-02-23 08:49:38 +00004121 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
4122 // the shuffle mask. If a quad is scored as -1, that means that it contains
4123 // words from all 4 input quadwords.
4124 SDValue NewV;
4125 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004126 SmallVector<int, 8> MaskV;
4127 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
4128 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
Eric Christopherfd179292009-08-27 18:07:15 +00004129 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004130 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
4131 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
4132 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004133
Nate Begemanb9a47b82009-02-23 08:49:38 +00004134 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
4135 // source words for the shuffle, to aid later transformations.
4136 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00004137 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00004138 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00004139 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00004140 if (idx != (int)i)
4141 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004142 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00004143 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004144 AllWordsInNewV = false;
4145 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00004146 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00004147
Nate Begemanb9a47b82009-02-23 08:49:38 +00004148 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
4149 if (AllWordsInNewV) {
4150 for (int i = 0; i != 8; ++i) {
4151 int idx = MaskVals[i];
4152 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004153 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004154 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004155 if ((idx != i) && idx < 4)
4156 pshufhw = false;
4157 if ((idx != i) && idx > 3)
4158 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00004159 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00004160 V1 = NewV;
4161 V2Used = false;
4162 BestLoQuad = 0;
4163 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004164 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004165
Nate Begemanb9a47b82009-02-23 08:49:38 +00004166 // If we've eliminated the use of V2, and the new mask is a pshuflw or
4167 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00004168 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Eric Christopherfd179292009-08-27 18:07:15 +00004169 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
Owen Anderson825b72b2009-08-11 20:47:22 +00004170 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00004171 }
Evan Cheng14b32e12007-12-11 01:46:18 +00004172 }
Eric Christopherfd179292009-08-27 18:07:15 +00004173
Nate Begemanb9a47b82009-02-23 08:49:38 +00004174 // If we have SSSE3, and all words of the result are from 1 input vector,
4175 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
4176 // is present, fall back to case 4.
4177 if (TLI.getSubtarget()->hasSSSE3()) {
4178 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004179
Nate Begemanb9a47b82009-02-23 08:49:38 +00004180 // If we have elements from both input vectors, set the high bit of the
Eric Christopherfd179292009-08-27 18:07:15 +00004181 // shuffle mask element to zero out elements that come from V2 in the V1
Nate Begemanb9a47b82009-02-23 08:49:38 +00004182 // mask, and elements that come from V1 in the V2 mask, so that the two
4183 // results can be OR'd together.
4184 bool TwoInputs = V1Used && V2Used;
4185 for (unsigned i = 0; i != 8; ++i) {
4186 int EltIdx = MaskVals[i] * 2;
4187 if (TwoInputs && (EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004188 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4189 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004190 continue;
4191 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004192 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
4193 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004194 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004196 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004197 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004198 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004199 if (!TwoInputs)
Owen Anderson825b72b2009-08-11 20:47:22 +00004200 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Eric Christopherfd179292009-08-27 18:07:15 +00004201
Nate Begemanb9a47b82009-02-23 08:49:38 +00004202 // Calculate the shuffle mask for the second input, shuffle it, and
4203 // OR it with the first shuffled input.
4204 pshufbMask.clear();
4205 for (unsigned i = 0; i != 8; ++i) {
4206 int EltIdx = MaskVals[i] * 2;
4207 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004208 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
4209 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004210 continue;
4211 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004212 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
4213 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004214 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004215 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
Eric Christopherfd179292009-08-27 18:07:15 +00004216 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004217 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 MVT::v16i8, &pshufbMask[0], 16));
4219 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
4220 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004221 }
4222
4223 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
4224 // and update MaskVals with new element order.
4225 BitVector InOrder(8);
4226 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004227 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004228 for (int i = 0; i != 4; ++i) {
4229 int idx = MaskVals[i];
4230 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004231 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004232 InOrder.set(i);
4233 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004234 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004235 InOrder.set(i);
4236 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004237 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004238 }
4239 }
4240 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004241 MaskV.push_back(i);
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004243 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004244 }
Eric Christopherfd179292009-08-27 18:07:15 +00004245
Nate Begemanb9a47b82009-02-23 08:49:38 +00004246 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
4247 // and update MaskVals with the new element order.
4248 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004249 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00004250 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00004251 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004252 for (unsigned i = 4; i != 8; ++i) {
4253 int idx = MaskVals[i];
4254 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004255 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004256 InOrder.set(i);
4257 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004258 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004259 InOrder.set(i);
4260 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004261 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004262 }
4263 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004264 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
Nate Begeman9008ca62009-04-27 18:41:29 +00004265 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004266 }
Eric Christopherfd179292009-08-27 18:07:15 +00004267
Nate Begemanb9a47b82009-02-23 08:49:38 +00004268 // In case BestHi & BestLo were both -1, which means each quadword has a word
4269 // from each of the four input quadwords, calculate the InOrder bitvector now
4270 // before falling through to the insert/extract cleanup.
4271 if (BestLoQuad == -1 && BestHiQuad == -1) {
4272 NewV = V1;
4273 for (int i = 0; i != 8; ++i)
4274 if (MaskVals[i] < 0 || MaskVals[i] == i)
4275 InOrder.set(i);
4276 }
Eric Christopherfd179292009-08-27 18:07:15 +00004277
Nate Begemanb9a47b82009-02-23 08:49:38 +00004278 // The other elements are put in the right place using pextrw and pinsrw.
4279 for (unsigned i = 0; i != 8; ++i) {
4280 if (InOrder[i])
4281 continue;
4282 int EltIdx = MaskVals[i];
4283 if (EltIdx < 0)
4284 continue;
4285 SDValue ExtOp = (EltIdx < 8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004286 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004287 DAG.getIntPtrConstant(EltIdx))
Owen Anderson825b72b2009-08-11 20:47:22 +00004288 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004289 DAG.getIntPtrConstant(EltIdx - 8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004290 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004291 DAG.getIntPtrConstant(i));
4292 }
4293 return NewV;
4294}
4295
4296// v16i8 shuffles - Prefer shuffles in the following order:
4297// 1. [ssse3] 1 x pshufb
4298// 2. [ssse3] 2 x pshufb + 1 x por
4299// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
4300static
Nate Begeman9008ca62009-04-27 18:41:29 +00004301SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
Dan Gohmand858e902010-04-17 15:26:15 +00004302 SelectionDAG &DAG,
4303 const X86TargetLowering &TLI) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004304 SDValue V1 = SVOp->getOperand(0);
4305 SDValue V2 = SVOp->getOperand(1);
4306 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00004307 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00004308 SVOp->getMask(MaskVals);
Eric Christopherfd179292009-08-27 18:07:15 +00004309
Nate Begemanb9a47b82009-02-23 08:49:38 +00004310 // If we have SSSE3, case 1 is generated when all result bytes come from
Eric Christopherfd179292009-08-27 18:07:15 +00004311 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
Nate Begemanb9a47b82009-02-23 08:49:38 +00004312 // present, fall back to case 3.
4313 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
4314 bool V1Only = true;
4315 bool V2Only = true;
4316 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004317 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00004318 if (EltIdx < 0)
4319 continue;
4320 if (EltIdx < 16)
4321 V2Only = false;
4322 else
4323 V1Only = false;
4324 }
Eric Christopherfd179292009-08-27 18:07:15 +00004325
Nate Begemanb9a47b82009-02-23 08:49:38 +00004326 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
4327 if (TLI.getSubtarget()->hasSSSE3()) {
4328 SmallVector<SDValue,16> pshufbMask;
Eric Christopherfd179292009-08-27 18:07:15 +00004329
Nate Begemanb9a47b82009-02-23 08:49:38 +00004330 // If all result elements are from one input vector, then only translate
Eric Christopherfd179292009-08-27 18:07:15 +00004331 // undef mask values to 0x80 (zero out result) in the pshufb mask.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004332 //
4333 // Otherwise, we have elements from both input vectors, and must zero out
4334 // elements that come from V2 in the first mask, and V1 in the second mask
4335 // so that we can OR them together.
4336 bool TwoInputs = !(V1Only || V2Only);
4337 for (unsigned i = 0; i != 16; ++i) {
4338 int EltIdx = MaskVals[i];
4339 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004340 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004341 continue;
4342 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004343 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004344 }
4345 // If all the elements are from V2, assign it to V1 and return after
4346 // building the first pshufb.
4347 if (V2Only)
4348 V1 = V2;
Owen Anderson825b72b2009-08-11 20:47:22 +00004349 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00004350 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004351 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004352 if (!TwoInputs)
4353 return V1;
Eric Christopherfd179292009-08-27 18:07:15 +00004354
Nate Begemanb9a47b82009-02-23 08:49:38 +00004355 // Calculate the shuffle mask for the second input, shuffle it, and
4356 // OR it with the first shuffled input.
4357 pshufbMask.clear();
4358 for (unsigned i = 0; i != 16; ++i) {
4359 int EltIdx = MaskVals[i];
4360 if (EltIdx < 16) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004361 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004362 continue;
4363 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004364 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004365 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004366 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00004367 DAG.getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004368 MVT::v16i8, &pshufbMask[0], 16));
4369 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004370 }
Eric Christopherfd179292009-08-27 18:07:15 +00004371
Nate Begemanb9a47b82009-02-23 08:49:38 +00004372 // No SSSE3 - Calculate in place words and then fix all out of place words
4373 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
4374 // the 16 different words that comprise the two doublequadword input vectors.
Owen Anderson825b72b2009-08-11 20:47:22 +00004375 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
4376 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004377 SDValue NewV = V2Only ? V2 : V1;
4378 for (int i = 0; i != 8; ++i) {
4379 int Elt0 = MaskVals[i*2];
4380 int Elt1 = MaskVals[i*2+1];
Eric Christopherfd179292009-08-27 18:07:15 +00004381
Nate Begemanb9a47b82009-02-23 08:49:38 +00004382 // This word of the result is all undef, skip it.
4383 if (Elt0 < 0 && Elt1 < 0)
4384 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004385
Nate Begemanb9a47b82009-02-23 08:49:38 +00004386 // This word of the result is already in the correct place, skip it.
4387 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
4388 continue;
4389 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
4390 continue;
Eric Christopherfd179292009-08-27 18:07:15 +00004391
Nate Begemanb9a47b82009-02-23 08:49:38 +00004392 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
4393 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
4394 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00004395
4396 // If Elt0 and Elt1 are defined, are consecutive, and can be load
4397 // using a single extract together, load it and store it.
4398 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004399 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004400 DAG.getIntPtrConstant(Elt1 / 2));
Owen Anderson825b72b2009-08-11 20:47:22 +00004401 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Mon P Wang6b3ef692009-03-11 18:47:57 +00004402 DAG.getIntPtrConstant(i));
4403 continue;
4404 }
4405
Nate Begemanb9a47b82009-02-23 08:49:38 +00004406 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00004407 // source byte is not also odd, shift the extracted word left 8 bits
4408 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00004409 if (Elt1 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004410 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004411 DAG.getIntPtrConstant(Elt1 / 2));
4412 if ((Elt1 & 1) == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004413 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004414 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004415 else if (Elt0 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004416 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
4417 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00004418 }
4419 // If Elt0 is defined, extract it from the appropriate source. If the
4420 // source byte is not also even, shift the extracted word right 8 bits. If
4421 // Elt1 was also defined, OR the extracted values together before
4422 // inserting them in the result.
4423 if (Elt0 >= 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004425 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
4426 if ((Elt0 & 1) != 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004427 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004428 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00004429 else if (Elt1 >= 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004430 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
4431 DAG.getConstant(0x00FF, MVT::i16));
4432 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
Nate Begemanb9a47b82009-02-23 08:49:38 +00004433 : InsElt0;
4434 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004435 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
Nate Begemanb9a47b82009-02-23 08:49:38 +00004436 DAG.getIntPtrConstant(i));
4437 }
Owen Anderson825b72b2009-08-11 20:47:22 +00004438 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00004439}
4440
Evan Cheng7a831ce2007-12-15 03:00:47 +00004441/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
4442/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
4443/// done when every pair / quad of shuffle mask elements point to elements in
4444/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00004445/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
4446static
Nate Begeman9008ca62009-04-27 18:41:29 +00004447SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
4448 SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00004449 const TargetLowering &TLI, DebugLoc dl) {
Owen Andersone50ed302009-08-10 22:56:29 +00004450 EVT VT = SVOp->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00004451 SDValue V1 = SVOp->getOperand(0);
4452 SDValue V2 = SVOp->getOperand(1);
4453 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00004454 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Owen Anderson825b72b2009-08-11 20:47:22 +00004455 EVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Owen Andersone50ed302009-08-10 22:56:29 +00004456 EVT MaskEltVT = MaskVT.getVectorElementType();
4457 EVT NewVT = MaskVT;
Owen Anderson825b72b2009-08-11 20:47:22 +00004458 switch (VT.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004459 default: assert(false && "Unexpected!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004460 case MVT::v4f32: NewVT = MVT::v2f64; break;
4461 case MVT::v4i32: NewVT = MVT::v2i64; break;
4462 case MVT::v8i16: NewVT = MVT::v4i32; break;
4463 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004464 }
4465
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004466 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004467 if (VT.isInteger())
Owen Anderson825b72b2009-08-11 20:47:22 +00004468 NewVT = MVT::v2i64;
Evan Cheng7a831ce2007-12-15 03:00:47 +00004469 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004470 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00004471 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004472 int Scale = NumElems / NewWidth;
4473 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00004474 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004475 int StartIdx = -1;
4476 for (int j = 0; j < Scale; ++j) {
4477 int EltIdx = SVOp->getMaskElt(i+j);
4478 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00004479 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00004480 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00004481 StartIdx = EltIdx - (EltIdx % Scale);
4482 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00004483 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004484 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004485 if (StartIdx == -1)
4486 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00004487 else
Nate Begeman9008ca62009-04-27 18:41:29 +00004488 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004489 }
4490
Dale Johannesenace16102009-02-03 19:33:06 +00004491 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
4492 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00004493 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00004494}
4495
Evan Chengd880b972008-05-09 21:53:03 +00004496/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00004497///
Owen Andersone50ed302009-08-10 22:56:29 +00004498static SDValue getVZextMovL(EVT VT, EVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00004499 SDValue SrcOp, SelectionDAG &DAG,
4500 const X86Subtarget *Subtarget, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004501 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004502 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00004503 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00004504 LD = dyn_cast<LoadSDNode>(SrcOp);
4505 if (!LD) {
4506 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
4507 // instead.
Owen Anderson766b5ef2009-08-11 21:59:30 +00004508 MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
4509 if ((ExtVT.SimpleTy != MVT::i64 || Subtarget->is64Bit()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00004510 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4511 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
Owen Anderson766b5ef2009-08-11 21:59:30 +00004512 SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004513 // PR2108
Owen Anderson825b72b2009-08-11 20:47:22 +00004514 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00004515 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4516 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
4517 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4518 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00004519 SrcOp.getOperand(0)
4520 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004521 }
4522 }
4523 }
4524
Dale Johannesenace16102009-02-03 19:33:06 +00004525 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4526 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00004527 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004528 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00004529}
4530
Evan Chengace3c172008-07-22 21:13:36 +00004531/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
4532/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00004533static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00004534LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
4535 SDValue V1 = SVOp->getOperand(0);
4536 SDValue V2 = SVOp->getOperand(1);
4537 DebugLoc dl = SVOp->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00004538 EVT VT = SVOp->getValueType(0);
Eric Christopherfd179292009-08-27 18:07:15 +00004539
Evan Chengace3c172008-07-22 21:13:36 +00004540 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00004541 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00004542 SmallVector<int, 8> Mask1(4U, -1);
4543 SmallVector<int, 8> PermMask;
4544 SVOp->getMask(PermMask);
4545
Evan Chengace3c172008-07-22 21:13:36 +00004546 unsigned NumHi = 0;
4547 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00004548 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004549 int Idx = PermMask[i];
4550 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004551 Locs[i] = std::make_pair(-1, -1);
4552 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004553 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
4554 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004555 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00004556 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004557 NumLo++;
4558 } else {
4559 Locs[i] = std::make_pair(1, NumHi);
4560 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004561 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004562 NumHi++;
4563 }
4564 }
4565 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004566
Evan Chengace3c172008-07-22 21:13:36 +00004567 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004568 // If no more than two elements come from either vector. This can be
4569 // implemented with two shuffles. First shuffle gather the elements.
4570 // The second shuffle, which takes the first shuffle as both of its
4571 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00004572 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004573
Nate Begeman9008ca62009-04-27 18:41:29 +00004574 SmallVector<int, 8> Mask2(4U, -1);
Eric Christopherfd179292009-08-27 18:07:15 +00004575
Evan Chengace3c172008-07-22 21:13:36 +00004576 for (unsigned i = 0; i != 4; ++i) {
4577 if (Locs[i].first == -1)
4578 continue;
4579 else {
4580 unsigned Idx = (i < 2) ? 0 : 4;
4581 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004582 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004583 }
4584 }
4585
Nate Begeman9008ca62009-04-27 18:41:29 +00004586 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004587 } else if (NumLo == 3 || NumHi == 3) {
4588 // Otherwise, we must have three elements from one vector, call it X, and
4589 // one element from the other, call it Y. First, use a shufps to build an
4590 // intermediate vector with the one element from Y and the element from X
4591 // that will be in the same half in the final destination (the indexes don't
4592 // matter). Then, use a shufps to build the final vector, taking the half
4593 // containing the element from Y from the intermediate, and the other half
4594 // from X.
4595 if (NumHi == 3) {
4596 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00004597 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004598 std::swap(V1, V2);
4599 }
4600
4601 // Find the element from V2.
4602 unsigned HiIndex;
4603 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004604 int Val = PermMask[HiIndex];
4605 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004606 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004607 if (Val >= 4)
4608 break;
4609 }
4610
Nate Begeman9008ca62009-04-27 18:41:29 +00004611 Mask1[0] = PermMask[HiIndex];
4612 Mask1[1] = -1;
4613 Mask1[2] = PermMask[HiIndex^1];
4614 Mask1[3] = -1;
4615 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004616
4617 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004618 Mask1[0] = PermMask[0];
4619 Mask1[1] = PermMask[1];
4620 Mask1[2] = HiIndex & 1 ? 6 : 4;
4621 Mask1[3] = HiIndex & 1 ? 4 : 6;
4622 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004623 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00004624 Mask1[0] = HiIndex & 1 ? 2 : 0;
4625 Mask1[1] = HiIndex & 1 ? 0 : 2;
4626 Mask1[2] = PermMask[2];
4627 Mask1[3] = PermMask[3];
4628 if (Mask1[2] >= 0)
4629 Mask1[2] += 4;
4630 if (Mask1[3] >= 0)
4631 Mask1[3] += 4;
4632 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00004633 }
Evan Chengace3c172008-07-22 21:13:36 +00004634 }
4635
4636 // Break it into (shuffle shuffle_hi, shuffle_lo).
4637 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00004638 SmallVector<int,8> LoMask(4U, -1);
4639 SmallVector<int,8> HiMask(4U, -1);
4640
4641 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00004642 unsigned MaskIdx = 0;
4643 unsigned LoIdx = 0;
4644 unsigned HiIdx = 2;
4645 for (unsigned i = 0; i != 4; ++i) {
4646 if (i == 2) {
4647 MaskPtr = &HiMask;
4648 MaskIdx = 1;
4649 LoIdx = 0;
4650 HiIdx = 2;
4651 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004652 int Idx = PermMask[i];
4653 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004654 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004655 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004656 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004657 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004658 LoIdx++;
4659 } else {
4660 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004661 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004662 HiIdx++;
4663 }
4664 }
4665
Nate Begeman9008ca62009-04-27 18:41:29 +00004666 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4667 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4668 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004669 for (unsigned i = 0; i != 4; ++i) {
4670 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004671 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004672 } else {
4673 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004674 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004675 }
4676 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004677 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004678}
4679
Dan Gohman475871a2008-07-27 21:46:04 +00004680SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004681X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00004682 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004683 SDValue V1 = Op.getOperand(0);
4684 SDValue V2 = Op.getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00004685 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004686 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004687 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004688 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004689 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4690 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004691 bool V1IsSplat = false;
4692 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004693
Nate Begeman9008ca62009-04-27 18:41:29 +00004694 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004695 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004696
Nate Begeman9008ca62009-04-27 18:41:29 +00004697 // Promote splats to v4f32.
4698 if (SVOp->isSplat()) {
Eric Christopherfd179292009-08-27 18:07:15 +00004699 if (isMMX || NumElems < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00004700 return Op;
4701 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004702 }
4703
Evan Cheng7a831ce2007-12-15 03:00:47 +00004704 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4705 // do it!
Owen Anderson825b72b2009-08-11 20:47:22 +00004706 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004707 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004708 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004709 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004710 LowerVECTOR_SHUFFLE(NewOp, DAG));
Owen Anderson825b72b2009-08-11 20:47:22 +00004711 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004712 // FIXME: Figure out a cleaner way to do this.
4713 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004714 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004715 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004716 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004717 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4718 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4719 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004720 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004721 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004722 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4723 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004724 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004725 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004726 }
4727 }
Eric Christopherfd179292009-08-27 18:07:15 +00004728
Nate Begeman9008ca62009-04-27 18:41:29 +00004729 if (X86::isPSHUFDMask(SVOp))
4730 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004731
Evan Chengf26ffe92008-05-29 08:22:04 +00004732 // Check if this can be converted into a logical shift.
4733 bool isLeft = false;
4734 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004735 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004736 bool isShift = getSubtarget()->hasSSE2() &&
Evan Chengc3630942009-12-09 21:00:30 +00004737 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004738 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004739 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004740 // v_set0 + movlhps or movhlps, etc.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004741 EVT EltVT = VT.getVectorElementType();
4742 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004743 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004744 }
Eric Christopherfd179292009-08-27 18:07:15 +00004745
Nate Begeman9008ca62009-04-27 18:41:29 +00004746 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004747 if (V1IsUndef)
4748 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004749 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004750 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004751 if (!isMMX)
4752 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004753 }
Eric Christopherfd179292009-08-27 18:07:15 +00004754
Nate Begeman9008ca62009-04-27 18:41:29 +00004755 // FIXME: fold these into legal mask.
4756 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4757 X86::isMOVSLDUPMask(SVOp) ||
4758 X86::isMOVHLPSMask(SVOp) ||
Nate Begeman0b10b912009-11-07 23:17:15 +00004759 X86::isMOVLHPSMask(SVOp) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00004760 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004761 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004762
Nate Begeman9008ca62009-04-27 18:41:29 +00004763 if (ShouldXformToMOVHLPS(SVOp) ||
4764 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4765 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004766
Evan Chengf26ffe92008-05-29 08:22:04 +00004767 if (isShift) {
4768 // No better options. Use a vshl / vsrl.
Dan Gohman8a55ce42009-09-23 21:02:20 +00004769 EVT EltVT = VT.getVectorElementType();
4770 ShAmt *= EltVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004771 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004772 }
Eric Christopherfd179292009-08-27 18:07:15 +00004773
Evan Cheng9eca5e82006-10-25 21:49:50 +00004774 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004775 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4776 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004777 V1IsSplat = isSplatVector(V1.getNode());
4778 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004779
Chris Lattner8a594482007-11-25 00:24:49 +00004780 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004781 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004782 Op = CommuteVectorShuffle(SVOp, DAG);
4783 SVOp = cast<ShuffleVectorSDNode>(Op);
4784 V1 = SVOp->getOperand(0);
4785 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004786 std::swap(V1IsSplat, V2IsSplat);
4787 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004788 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004789 }
4790
Nate Begeman9008ca62009-04-27 18:41:29 +00004791 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4792 // Shuffling low element of v1 into undef, just return v1.
Eric Christopherfd179292009-08-27 18:07:15 +00004793 if (V2IsUndef)
Nate Begeman9008ca62009-04-27 18:41:29 +00004794 return V1;
4795 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4796 // the instruction selector will not match, so get a canonical MOVL with
4797 // swapped operands to undo the commute.
4798 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004799 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004800
Nate Begeman9008ca62009-04-27 18:41:29 +00004801 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4802 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4803 X86::isUNPCKLMask(SVOp) ||
4804 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004805 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004806
Evan Cheng9bbbb982006-10-25 20:48:19 +00004807 if (V2IsSplat) {
4808 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004809 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004810 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004811 SDValue NewMask = NormalizeMask(SVOp, DAG);
4812 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4813 if (NSVOp != SVOp) {
4814 if (X86::isUNPCKLMask(NSVOp, true)) {
4815 return NewMask;
4816 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4817 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004818 }
4819 }
4820 }
4821
Evan Cheng9eca5e82006-10-25 21:49:50 +00004822 if (Commuted) {
4823 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004824 // FIXME: this seems wrong.
4825 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4826 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4827 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4828 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4829 X86::isUNPCKLMask(NewSVOp) ||
4830 X86::isUNPCKHMask(NewSVOp))
4831 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004832 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004833
Nate Begemanb9a47b82009-02-23 08:49:38 +00004834 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004835
4836 // Normalize the node to match x86 shuffle ops if needed
4837 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4838 return CommuteVectorShuffle(SVOp, DAG);
4839
4840 // Check for legal shuffle and return?
4841 SmallVector<int, 16> PermMask;
4842 SVOp->getMask(PermMask);
4843 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004844 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004845
Evan Cheng14b32e12007-12-11 01:46:18 +00004846 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
Owen Anderson825b72b2009-08-11 20:47:22 +00004847 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004848 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004849 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004850 return NewOp;
4851 }
4852
Owen Anderson825b72b2009-08-11 20:47:22 +00004853 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004854 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004855 if (NewOp.getNode())
4856 return NewOp;
4857 }
Eric Christopherfd179292009-08-27 18:07:15 +00004858
Evan Chengace3c172008-07-22 21:13:36 +00004859 // Handle all 4 wide cases with a number of shuffles except for MMX.
4860 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004861 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004862
Dan Gohman475871a2008-07-27 21:46:04 +00004863 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004864}
4865
Dan Gohman475871a2008-07-27 21:46:04 +00004866SDValue
4867X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00004868 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004869 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004870 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004871 if (VT.getSizeInBits() == 8) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004872 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004873 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004874 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004875 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004876 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004877 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004878 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4879 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4880 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004881 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4882 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004883 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004884 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004885 Op.getOperand(0)),
4886 Op.getOperand(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00004887 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004888 Op.getOperand(0), Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004889 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004890 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004891 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Owen Anderson825b72b2009-08-11 20:47:22 +00004892 } else if (VT == MVT::f32) {
Evan Cheng62a3f152008-03-24 21:52:23 +00004893 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4894 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004895 // result has a single use which is a store or a bitcast to i32. And in
4896 // the case of a store, it's not worth it if the index is a constant 0,
4897 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004898 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004899 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004900 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004901 if ((User->getOpcode() != ISD::STORE ||
4902 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4903 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004904 (User->getOpcode() != ISD::BIT_CONVERT ||
Owen Anderson825b72b2009-08-11 20:47:22 +00004905 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004906 return SDValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00004907 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4908 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004909 Op.getOperand(0)),
4910 Op.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004911 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
4912 } else if (VT == MVT::i32) {
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004913 // ExtractPS works with constant index.
4914 if (isa<ConstantSDNode>(Op.getOperand(1)))
4915 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004916 }
Dan Gohman475871a2008-07-27 21:46:04 +00004917 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004918}
4919
4920
Dan Gohman475871a2008-07-27 21:46:04 +00004921SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004922X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
4923 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004924 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004925 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004926
Evan Cheng62a3f152008-03-24 21:52:23 +00004927 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004928 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004929 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004930 return Res;
4931 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004932
Owen Andersone50ed302009-08-10 22:56:29 +00004933 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004934 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004935 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004936 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004937 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004938 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004939 if (Idx == 0)
Owen Anderson825b72b2009-08-11 20:47:22 +00004940 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4941 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004942 DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004943 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004944 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004945 // Transform it so it match pextrw which produces a 32-bit result.
Ken Dyck70d0ef12009-12-17 15:31:52 +00004946 EVT EltVT = MVT::i32;
Dan Gohman8a55ce42009-09-23 21:02:20 +00004947 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004948 Op.getOperand(0), Op.getOperand(1));
Dan Gohman8a55ce42009-09-23 21:02:20 +00004949 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004950 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004951 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004952 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004953 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004954 if (Idx == 0)
4955 return Op;
Eric Christopherfd179292009-08-27 18:07:15 +00004956
Evan Cheng0db9fe62006-04-25 20:13:52 +00004957 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004958 int Mask[4] = { Idx, -1, -1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004959 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004960 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004961 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004962 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004963 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004964 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004965 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4966 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4967 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004968 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004969 if (Idx == 0)
4970 return Op;
4971
4972 // UNPCKHPD the element to the lowest double word, then movsd.
4973 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4974 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004975 int Mask[2] = { 1, -1 };
Owen Andersone50ed302009-08-10 22:56:29 +00004976 EVT VVT = Op.getOperand(0).getValueType();
Eric Christopherfd179292009-08-27 18:07:15 +00004977 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
Nate Begeman9008ca62009-04-27 18:41:29 +00004978 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004979 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004980 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004981 }
4982
Dan Gohman475871a2008-07-27 21:46:04 +00004983 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004984}
4985
Dan Gohman475871a2008-07-27 21:46:04 +00004986SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00004987X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op,
4988 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004989 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00004990 EVT EltVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004991 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004992
Dan Gohman475871a2008-07-27 21:46:04 +00004993 SDValue N0 = Op.getOperand(0);
4994 SDValue N1 = Op.getOperand(1);
4995 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004996
Dan Gohman8a55ce42009-09-23 21:02:20 +00004997 if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) &&
Dan Gohmanef521f12008-08-14 22:53:18 +00004998 isa<ConstantSDNode>(N2)) {
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00004999 unsigned Opc;
5000 if (VT == MVT::v8i16)
5001 Opc = X86ISD::PINSRW;
5002 else if (VT == MVT::v4i16)
5003 Opc = X86ISD::MMX_PINSRW;
5004 else if (VT == MVT::v16i8)
5005 Opc = X86ISD::PINSRB;
5006 else
5007 Opc = X86ISD::PINSRB;
5008
Nate Begeman14d12ca2008-02-11 04:19:36 +00005009 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
5010 // argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005011 if (N1.getValueType() != MVT::i32)
5012 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5013 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005014 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00005015 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005016 } else if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00005017 // Bits [7:6] of the constant are the source select. This will always be
5018 // zero here. The DAG Combiner may combine an extract_elt index into these
5019 // bits. For example (insert (extract, 3), 2) could be matched by putting
5020 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00005021 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00005022 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00005023 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00005024 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005025 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00005026 // Create this as a scalar to vector..
Owen Anderson825b72b2009-08-11 20:47:22 +00005027 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00005028 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005029 } else if (EltVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
Eric Christopherfbd66872009-07-24 00:33:09 +00005030 // PINSR* works with constant index.
5031 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00005032 }
Dan Gohman475871a2008-07-27 21:46:04 +00005033 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005034}
5035
Dan Gohman475871a2008-07-27 21:46:04 +00005036SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005037X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005038 EVT VT = Op.getValueType();
Dan Gohman8a55ce42009-09-23 21:02:20 +00005039 EVT EltVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00005040
5041 if (Subtarget->hasSSE41())
5042 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
5043
Dan Gohman8a55ce42009-09-23 21:02:20 +00005044 if (EltVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00005045 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00005046
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005047 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005048 SDValue N0 = Op.getOperand(0);
5049 SDValue N1 = Op.getOperand(1);
5050 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00005051
Dan Gohman8a55ce42009-09-23 21:02:20 +00005052 if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00005053 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
5054 // as its second argument.
Owen Anderson825b72b2009-08-11 20:47:22 +00005055 if (N1.getValueType() != MVT::i32)
5056 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
5057 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005058 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00005059 return DAG.getNode(VT == MVT::v8i16 ? X86ISD::PINSRW : X86ISD::MMX_PINSRW,
5060 dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005061 }
Dan Gohman475871a2008-07-27 21:46:04 +00005062 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005063}
5064
Dan Gohman475871a2008-07-27 21:46:04 +00005065SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005066X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005067 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00005068 if (Op.getValueType() == MVT::v2f32)
5069 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
5070 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
5071 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00005072 Op.getOperand(0))));
5073
Owen Anderson825b72b2009-08-11 20:47:22 +00005074 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
5075 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00005076
Owen Anderson825b72b2009-08-11 20:47:22 +00005077 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
5078 EVT VT = MVT::v2i32;
5079 switch (Op.getValueType().getSimpleVT().SimpleTy) {
Evan Chengefec7512008-02-18 23:04:32 +00005080 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00005081 case MVT::v16i8:
5082 case MVT::v8i16:
5083 VT = MVT::v4i32;
Evan Chengefec7512008-02-18 23:04:32 +00005084 break;
5085 }
Dale Johannesenace16102009-02-03 19:33:06 +00005086 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
5087 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005088}
5089
Bill Wendling056292f2008-09-16 21:48:12 +00005090// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
5091// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
5092// one of the above mentioned nodes. It has to be wrapped because otherwise
5093// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
5094// be used to form addressing mode. These wrapped nodes will be selected
5095// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00005096SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005097X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005098 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005099
Chris Lattner41621a22009-06-26 19:22:52 +00005100 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5101 // global base reg.
5102 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005103 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005104 CodeModel::Model M = getTargetMachine().getCodeModel();
5105
Chris Lattner4f066492009-07-11 20:29:19 +00005106 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005107 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005108 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005109 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005110 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005111 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005112 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005113
Evan Cheng1606e8e2009-03-13 07:51:59 +00005114 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00005115 CP->getAlignment(),
5116 CP->getOffset(), OpFlag);
5117 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00005118 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005119 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00005120 if (OpFlag) {
5121 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00005122 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005123 DebugLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005124 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005125 }
5126
5127 return Result;
5128}
5129
Dan Gohmand858e902010-04-17 15:26:15 +00005130SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005131 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Eric Christopherfd179292009-08-27 18:07:15 +00005132
Chris Lattner18c59872009-06-27 04:16:01 +00005133 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5134 // global base reg.
5135 unsigned char OpFlag = 0;
5136 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005137 CodeModel::Model M = getTargetMachine().getCodeModel();
5138
Chris Lattner4f066492009-07-11 20:29:19 +00005139 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005140 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005141 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005142 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005143 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005144 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005145 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005146
Chris Lattner18c59872009-06-27 04:16:01 +00005147 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
5148 OpFlag);
5149 DebugLoc DL = JT->getDebugLoc();
5150 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005151
Chris Lattner18c59872009-06-27 04:16:01 +00005152 // With PIC, the address is actually $g + Offset.
5153 if (OpFlag) {
5154 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5155 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005156 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005157 Result);
5158 }
Eric Christopherfd179292009-08-27 18:07:15 +00005159
Chris Lattner18c59872009-06-27 04:16:01 +00005160 return Result;
5161}
5162
5163SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005164X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
Chris Lattner18c59872009-06-27 04:16:01 +00005165 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Eric Christopherfd179292009-08-27 18:07:15 +00005166
Chris Lattner18c59872009-06-27 04:16:01 +00005167 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
5168 // global base reg.
5169 unsigned char OpFlag = 0;
5170 unsigned WrapperKind = X86ISD::Wrapper;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005171 CodeModel::Model M = getTargetMachine().getCodeModel();
5172
Chris Lattner4f066492009-07-11 20:29:19 +00005173 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005174 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattnere4df7562009-07-09 03:15:51 +00005175 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00005176 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005177 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00005178 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00005179 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Eric Christopherfd179292009-08-27 18:07:15 +00005180
Chris Lattner18c59872009-06-27 04:16:01 +00005181 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
Eric Christopherfd179292009-08-27 18:07:15 +00005182
Chris Lattner18c59872009-06-27 04:16:01 +00005183 DebugLoc DL = Op.getDebugLoc();
5184 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Eric Christopherfd179292009-08-27 18:07:15 +00005185
5186
Chris Lattner18c59872009-06-27 04:16:01 +00005187 // With PIC, the address is actually $g + Offset.
5188 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00005189 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00005190 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
5191 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005192 DebugLoc(), getPointerTy()),
Chris Lattner18c59872009-06-27 04:16:01 +00005193 Result);
5194 }
Eric Christopherfd179292009-08-27 18:07:15 +00005195
Chris Lattner18c59872009-06-27 04:16:01 +00005196 return Result;
5197}
5198
Dan Gohman475871a2008-07-27 21:46:04 +00005199SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005200X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman29cbade2009-11-20 23:18:13 +00005201 // Create the TargetBlockAddressAddress node.
5202 unsigned char OpFlags =
5203 Subtarget->ClassifyBlockAddressReference();
Dan Gohmanf705adb2009-10-30 01:28:02 +00005204 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman46510a72010-04-15 01:51:59 +00005205 const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
Dan Gohman29cbade2009-11-20 23:18:13 +00005206 DebugLoc dl = Op.getDebugLoc();
5207 SDValue Result = DAG.getBlockAddress(BA, getPointerTy(),
5208 /*isTarget=*/true, OpFlags);
5209
Dan Gohmanf705adb2009-10-30 01:28:02 +00005210 if (Subtarget->isPICStyleRIPRel() &&
5211 (M == CodeModel::Small || M == CodeModel::Kernel))
Dan Gohman29cbade2009-11-20 23:18:13 +00005212 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5213 else
5214 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohmanf705adb2009-10-30 01:28:02 +00005215
Dan Gohman29cbade2009-11-20 23:18:13 +00005216 // With PIC, the address is actually $g + Offset.
5217 if (isGlobalRelativeToPICBase(OpFlags)) {
5218 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5219 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
5220 Result);
5221 }
Dan Gohmanf705adb2009-10-30 01:28:02 +00005222
5223 return Result;
5224}
5225
5226SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00005227X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00005228 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00005229 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00005230 // Create the TargetGlobalAddress node, folding in the constant
5231 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00005232 unsigned char OpFlags =
5233 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005234 CodeModel::Model M = getTargetMachine().getCodeModel();
Dan Gohman6520e202008-10-18 02:06:02 +00005235 SDValue Result;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005236 if (OpFlags == X86II::MO_NO_FLAG &&
5237 X86::isOffsetSuitableForCodeModel(Offset, M)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00005238 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00005239 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00005240 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005241 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00005242 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005243 }
Eric Christopherfd179292009-08-27 18:07:15 +00005244
Chris Lattner4f066492009-07-11 20:29:19 +00005245 if (Subtarget->isPICStyleRIPRel() &&
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00005246 (M == CodeModel::Small || M == CodeModel::Kernel))
Chris Lattner18c59872009-06-27 04:16:01 +00005247 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
5248 else
5249 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00005250
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005251 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00005252 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005253 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
5254 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00005255 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005256 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005257
Chris Lattner36c25012009-07-10 07:34:39 +00005258 // For globals that require a load from a stub to get the address, emit the
5259 // load.
5260 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00005261 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
David Greene67c9d422010-02-15 16:53:33 +00005262 PseudoSourceValue::getGOT(), 0, false, false, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005263
Dan Gohman6520e202008-10-18 02:06:02 +00005264 // If there was a non-zero offset that we didn't fold, create an explicit
5265 // addition for it.
5266 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005267 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00005268 DAG.getConstant(Offset, getPointerTy()));
5269
Evan Cheng0db9fe62006-04-25 20:13:52 +00005270 return Result;
5271}
5272
Evan Chengda43bcf2008-09-24 00:05:32 +00005273SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005274X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
Evan Chengda43bcf2008-09-24 00:05:32 +00005275 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00005276 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005277 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00005278}
5279
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005280static SDValue
5281GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Owen Andersone50ed302009-08-10 22:56:29 +00005282 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
Chris Lattnerb903bed2009-06-26 21:20:29 +00005283 unsigned char OperandFlags) {
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005284 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Owen Anderson825b72b2009-08-11 20:47:22 +00005285 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005286 DebugLoc dl = GA->getDebugLoc();
5287 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
5288 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005289 GA->getOffset(),
5290 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005291 if (InFlag) {
5292 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005293 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005294 } else {
5295 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00005296 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005297 }
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005298
5299 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
Bill Wendlingb92187a2010-05-14 21:14:32 +00005300 MFI->setAdjustsStack(true);
Anton Korobeynikov817a4642009-12-11 19:39:55 +00005301
Rafael Espindola15f1b662009-04-24 12:59:40 +00005302 SDValue Flag = Chain.getValue(1);
5303 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00005304}
5305
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005306// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005307static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005308LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005309 const EVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005310 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00005311 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
5312 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005313 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005314 DebugLoc(), PtrVT), InFlag);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005315 InFlag = Chain.getValue(1);
5316
Chris Lattnerb903bed2009-06-26 21:20:29 +00005317 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005318}
5319
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005320// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00005321static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005322LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005323 const EVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005324 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
5325 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005326}
5327
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005328// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
5329// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00005330static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Owen Andersone50ed302009-08-10 22:56:29 +00005331 const EVT PtrVT, TLSModel::Model model,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005332 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00005333 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005334 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00005335 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
Chris Lattnerc7f3ace2010-04-02 20:16:16 +00005336 DebugLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00005337 DAG.getRegister(is64Bit? X86::FS : X86::GS,
Owen Anderson825b72b2009-08-11 20:47:22 +00005338 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00005339
5340 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
David Greene67c9d422010-02-15 16:53:33 +00005341 NULL, 0, false, false, 0);
Rafael Espindola094fad32009-04-08 21:14:34 +00005342
Chris Lattnerb903bed2009-06-26 21:20:29 +00005343 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00005344 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
5345 // initialexec.
5346 unsigned WrapperKind = X86ISD::Wrapper;
5347 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00005348 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00005349 } else if (is64Bit) {
5350 assert(model == TLSModel::InitialExec);
5351 OperandFlags = X86II::MO_GOTTPOFF;
5352 WrapperKind = X86ISD::WrapperRIP;
5353 } else {
5354 assert(model == TLSModel::InitialExec);
5355 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00005356 }
Eric Christopherfd179292009-08-27 18:07:15 +00005357
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005358 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
5359 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00005360 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00005361 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00005362 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005363
Rafael Espindola9a580232009-02-27 13:37:18 +00005364 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00005365 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
David Greene67c9d422010-02-15 16:53:33 +00005366 PseudoSourceValue::getGOT(), 0, false, false, 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00005367
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005368 // The address of the thread local variable is the add of the thread
5369 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00005370 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005371}
5372
Dan Gohman475871a2008-07-27 21:46:04 +00005373SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00005374X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005375 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00005376 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005377 assert(Subtarget->isTargetELF() &&
5378 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005379 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00005380 const GlobalValue *GV = GA->getGlobal();
Eric Christopherfd179292009-08-27 18:07:15 +00005381
Chris Lattnerb903bed2009-06-26 21:20:29 +00005382 // If GV is an alias then use the aliasee for determining
5383 // thread-localness.
5384 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
5385 GV = GA->resolveAliasedGlobal(false);
Eric Christopherfd179292009-08-27 18:07:15 +00005386
Chris Lattnerb903bed2009-06-26 21:20:29 +00005387 TLSModel::Model model = getTLSModel(GV,
5388 getTargetMachine().getRelocationModel());
Eric Christopherfd179292009-08-27 18:07:15 +00005389
Chris Lattnerb903bed2009-06-26 21:20:29 +00005390 switch (model) {
5391 case TLSModel::GeneralDynamic:
5392 case TLSModel::LocalDynamic: // not implemented
5393 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00005394 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00005395 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005396
Chris Lattnerb903bed2009-06-26 21:20:29 +00005397 case TLSModel::InitialExec:
5398 case TLSModel::LocalExec:
5399 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
5400 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00005401 }
Eric Christopherfd179292009-08-27 18:07:15 +00005402
Torok Edwinc23197a2009-07-14 16:55:14 +00005403 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00005404 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005405}
5406
Evan Cheng0db9fe62006-04-25 20:13:52 +00005407
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005408/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00005409/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohmand858e902010-04-17 15:26:15 +00005410SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman4c1fa612008-03-03 22:22:09 +00005411 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Owen Andersone50ed302009-08-10 22:56:29 +00005412 EVT VT = Op.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005413 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005414 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005415 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00005416 SDValue ShOpLo = Op.getOperand(0);
5417 SDValue ShOpHi = Op.getOperand(1);
5418 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00005419 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Owen Anderson825b72b2009-08-11 20:47:22 +00005420 DAG.getConstant(VTBits - 1, MVT::i8))
Chris Lattner31dcfe62009-07-29 05:48:09 +00005421 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00005422
Dan Gohman475871a2008-07-27 21:46:04 +00005423 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005424 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005425 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
5426 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005427 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005428 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
5429 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005430 }
Evan Chenge3413162006-01-09 18:33:28 +00005431
Owen Anderson825b72b2009-08-11 20:47:22 +00005432 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
5433 DAG.getConstant(VTBits, MVT::i8));
Chris Lattnerccfea352010-02-22 00:28:59 +00005434 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Owen Anderson825b72b2009-08-11 20:47:22 +00005435 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00005436
Dan Gohman475871a2008-07-27 21:46:04 +00005437 SDValue Hi, Lo;
Owen Anderson825b72b2009-08-11 20:47:22 +00005438 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman475871a2008-07-27 21:46:04 +00005439 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
5440 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00005441
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005442 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00005443 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5444 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005445 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005446 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
5447 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00005448 }
5449
Dan Gohman475871a2008-07-27 21:46:04 +00005450 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00005451 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005452}
Evan Chenga3195e82006-01-12 22:54:21 +00005453
Dan Gohmand858e902010-04-17 15:26:15 +00005454SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
5455 SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005456 EVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00005457
5458 if (SrcVT.isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005459 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005460 return Op;
5461 }
5462 return SDValue();
5463 }
5464
Owen Anderson825b72b2009-08-11 20:47:22 +00005465 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00005466 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00005467
Eli Friedman36df4992009-05-27 00:47:34 +00005468 // These are really Legal; return the operand so the caller accepts it as
5469 // Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005470 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00005471 return Op;
Owen Anderson825b72b2009-08-11 20:47:22 +00005472 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
Eli Friedman36df4992009-05-27 00:47:34 +00005473 Subtarget->is64Bit()) {
5474 return Op;
5475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005476
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005477 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005478 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005479 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005480 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005481 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00005482 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00005483 StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005484 PseudoSourceValue::getFixedStack(SSFI), 0,
5485 false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005486 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
5487}
Evan Cheng0db9fe62006-04-25 20:13:52 +00005488
Owen Andersone50ed302009-08-10 22:56:29 +00005489SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005490 SDValue StackSlot,
Dan Gohmand858e902010-04-17 15:26:15 +00005491 SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005492 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00005493 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00005494 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00005495 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005496 if (useSSE)
Owen Anderson825b72b2009-08-11 20:47:22 +00005497 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005498 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005499 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005500 SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
Dale Johannesenace16102009-02-03 19:33:06 +00005501 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005502 Tys, Ops, array_lengthof(Ops));
Evan Cheng0db9fe62006-04-25 20:13:52 +00005503
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005504 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005505 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00005506 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005507
5508 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
5509 // shouldn't be necessary except that RFP cannot be live across
5510 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005511 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005512 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005513 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Owen Anderson825b72b2009-08-11 20:47:22 +00005514 Tys = DAG.getVTList(MVT::Other);
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00005515 SDValue Ops[] = {
5516 Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
5517 };
5518 Chain = DAG.getNode(X86ISD::FST, dl, Tys, Ops, array_lengthof(Ops));
Dale Johannesenace16102009-02-03 19:33:06 +00005519 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005520 PseudoSourceValue::getFixedStack(SSFI), 0,
5521 false, false, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005522 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005523
Evan Cheng0db9fe62006-04-25 20:13:52 +00005524 return Result;
5525}
5526
Bill Wendling8b8a6362009-01-17 03:56:04 +00005527// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005528SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
5529 SelectionDAG &DAG) const {
Bill Wendling8b8a6362009-01-17 03:56:04 +00005530 // This algorithm is not obvious. Here it is in C code, more or less:
5531 /*
5532 double uint64_to_double( uint32_t hi, uint32_t lo ) {
5533 static const __m128i exp = { 0x4330000045300000ULL, 0 };
5534 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00005535
Bill Wendling8b8a6362009-01-17 03:56:04 +00005536 // Copy ints to xmm registers.
5537 __m128i xh = _mm_cvtsi32_si128( hi );
5538 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00005539
Bill Wendling8b8a6362009-01-17 03:56:04 +00005540 // Combine into low half of a single xmm register.
5541 __m128i x = _mm_unpacklo_epi32( xh, xl );
5542 __m128d d;
5543 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00005544
Bill Wendling8b8a6362009-01-17 03:56:04 +00005545 // Merge in appropriate exponents to give the integer bits the right
5546 // magnitude.
5547 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00005548
Bill Wendling8b8a6362009-01-17 03:56:04 +00005549 // Subtract away the biases to deal with the IEEE-754 double precision
5550 // implicit 1.
5551 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00005552
Bill Wendling8b8a6362009-01-17 03:56:04 +00005553 // All conversions up to here are exact. The correctly rounded result is
5554 // calculated using the current rounding mode using the following
5555 // horizontal add.
5556 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
5557 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
5558 // store doesn't really need to be here (except
5559 // maybe to zero the other double)
5560 return sd;
5561 }
5562 */
Dale Johannesen040225f2008-10-21 23:07:49 +00005563
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005564 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00005565 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00005566
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005567 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00005568 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00005569 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
5570 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
5571 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
5572 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005573 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005574 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005575
Bill Wendling8b8a6362009-01-17 03:56:04 +00005576 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00005577 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005578 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00005579 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005580 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00005581 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005582 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005583
Owen Anderson825b72b2009-08-11 20:47:22 +00005584 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5585 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005586 Op.getOperand(0),
5587 DAG.getIntPtrConstant(1)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005588 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5589 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00005590 Op.getOperand(0),
5591 DAG.getIntPtrConstant(0)));
Owen Anderson825b72b2009-08-11 20:47:22 +00005592 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
5593 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005594 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005595 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005596 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
5597 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
5598 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005599 PseudoSourceValue::getConstantPool(), 0,
David Greene67c9d422010-02-15 16:53:33 +00005600 false, false, 16);
Owen Anderson825b72b2009-08-11 20:47:22 +00005601 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005602
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005603 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00005604 int ShufMask[2] = { 1, -1 };
Owen Anderson825b72b2009-08-11 20:47:22 +00005605 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
5606 DAG.getUNDEF(MVT::v2f64), ShufMask);
5607 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
5608 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005609 DAG.getIntPtrConstant(0));
5610}
5611
Bill Wendling8b8a6362009-01-17 03:56:04 +00005612// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
Dan Gohmand858e902010-04-17 15:26:15 +00005613SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
5614 SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005615 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005616 // FP constant to bias correct the final result.
5617 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
Owen Anderson825b72b2009-08-11 20:47:22 +00005618 MVT::f64);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005619
5620 // Load the 32-bit value into an XMM register.
Owen Anderson825b72b2009-08-11 20:47:22 +00005621 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
5622 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00005623 Op.getOperand(0),
5624 DAG.getIntPtrConstant(0)));
5625
Owen Anderson825b72b2009-08-11 20:47:22 +00005626 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5627 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005628 DAG.getIntPtrConstant(0));
5629
5630 // Or the load with the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005631 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
5632 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005633 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005634 MVT::v2f64, Load)),
5635 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005636 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00005637 MVT::v2f64, Bias)));
5638 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
5639 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00005640 DAG.getIntPtrConstant(0));
5641
5642 // Subtract the bias.
Owen Anderson825b72b2009-08-11 20:47:22 +00005643 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00005644
5645 // Handle final rounding.
Owen Andersone50ed302009-08-10 22:56:29 +00005646 EVT DestVT = Op.getValueType();
Bill Wendling030939c2009-01-17 07:40:19 +00005647
Owen Anderson825b72b2009-08-11 20:47:22 +00005648 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005649 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00005650 DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00005651 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005652 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00005653 }
5654
5655 // Handle final rounding.
5656 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00005657}
5658
Dan Gohmand858e902010-04-17 15:26:15 +00005659SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
5660 SelectionDAG &DAG) const {
Evan Chenga06ec9e2009-01-19 08:08:22 +00005661 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005662 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00005663
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005664 // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
Evan Chenga06ec9e2009-01-19 08:08:22 +00005665 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
5666 // the optimization here.
5667 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00005668 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00005669
Owen Andersone50ed302009-08-10 22:56:29 +00005670 EVT SrcVT = N0.getValueType();
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005671 EVT DstVT = Op.getValueType();
5672 if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005673 return LowerUINT_TO_FP_i64(Op, DAG);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005674 else if (SrcVT == MVT::i32 && X86ScalarSSEf64)
Bill Wendling8b8a6362009-01-17 03:56:04 +00005675 return LowerUINT_TO_FP_i32(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00005676
5677 // Make a 64-bit buffer, and use it to build an FILD.
Owen Anderson825b72b2009-08-11 20:47:22 +00005678 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005679 if (SrcVT == MVT::i32) {
5680 SDValue WordOff = DAG.getConstant(4, getPointerTy());
5681 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
5682 getPointerTy(), StackSlot, WordOff);
5683 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
5684 StackSlot, NULL, 0, false, false, 0);
5685 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
5686 OffsetSlot, NULL, 0, false, false, 0);
5687 SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
5688 return Fild;
5689 }
5690
5691 assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
5692 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
David Greene67c9d422010-02-15 16:53:33 +00005693 StackSlot, NULL, 0, false, false, 0);
Dale Johannesen8d908eb2010-05-15 18:51:12 +00005694 // For i64 source, we need to add the appropriate power of 2 if the input
5695 // was negative. This is the same as the optimization in
5696 // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
5697 // we must be careful to do the computation in x87 extended precision, not
5698 // in SSE. (The generic code can't know it's OK to do this, or how to.)
5699 SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
5700 SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
5701 SDValue Fild = DAG.getNode(X86ISD::FILD, dl, Tys, Ops, 3);
5702
5703 APInt FF(32, 0x5F800000ULL);
5704
5705 // Check whether the sign bit is set.
5706 SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(MVT::i64),
5707 Op.getOperand(0), DAG.getConstant(0, MVT::i64),
5708 ISD::SETLT);
5709
5710 // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
5711 SDValue FudgePtr = DAG.getConstantPool(
5712 ConstantInt::get(*DAG.getContext(), FF.zext(64)),
5713 getPointerTy());
5714
5715 // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
5716 SDValue Zero = DAG.getIntPtrConstant(0);
5717 SDValue Four = DAG.getIntPtrConstant(4);
5718 SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
5719 Zero, Four);
5720 FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset);
5721
5722 // Load the value out, extending it from f32 to f80.
5723 // FIXME: Avoid the extend by constructing the right constant pool?
5724 SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(),
5725 FudgePtr, PseudoSourceValue::getConstantPool(),
5726 0, MVT::f32, false, false, 4);
5727 // Extend everything to 80 bits to force it to be done on x87.
5728 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
5729 return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0));
Bill Wendling8b8a6362009-01-17 03:56:04 +00005730}
5731
Dan Gohman475871a2008-07-27 21:46:04 +00005732std::pair<SDValue,SDValue> X86TargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00005733FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005734 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005735
Owen Andersone50ed302009-08-10 22:56:29 +00005736 EVT DstTy = Op.getValueType();
Eli Friedman948e95a2009-05-23 09:59:16 +00005737
5738 if (!IsSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005739 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5740 DstTy = MVT::i64;
Eli Friedman948e95a2009-05-23 09:59:16 +00005741 }
5742
Owen Anderson825b72b2009-08-11 20:47:22 +00005743 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5744 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005745 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005746
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005747 // These are really Legal.
Owen Anderson825b72b2009-08-11 20:47:22 +00005748 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005749 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005750 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005751 if (Subtarget->is64Bit() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00005752 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005753 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005754 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005755
Evan Cheng87c89352007-10-15 20:11:21 +00005756 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5757 // stack slot.
5758 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005759 unsigned MemSize = DstTy.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00005760 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Dan Gohman475871a2008-07-27 21:46:04 +00005761 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eric Christopherfd179292009-08-27 18:07:15 +00005762
Evan Cheng0db9fe62006-04-25 20:13:52 +00005763 unsigned Opc;
Owen Anderson825b72b2009-08-11 20:47:22 +00005764 switch (DstTy.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005765 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Owen Anderson825b72b2009-08-11 20:47:22 +00005766 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5767 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5768 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005769 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005770
Dan Gohman475871a2008-07-27 21:46:04 +00005771 SDValue Chain = DAG.getEntryNode();
5772 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005773 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005774 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005775 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
David Greene67c9d422010-02-15 16:53:33 +00005776 PseudoSourceValue::getFixedStack(SSFI), 0,
5777 false, false, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +00005778 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005779 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005780 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5781 };
Dale Johannesenace16102009-02-03 19:33:06 +00005782 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005783 Chain = Value.getValue(1);
David Greene3f2bf852009-11-12 20:49:22 +00005784 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005785 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5786 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005787
Evan Cheng0db9fe62006-04-25 20:13:52 +00005788 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005789 SDValue Ops[] = { Chain, Value, StackSlot };
Owen Anderson825b72b2009-08-11 20:47:22 +00005790 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005791
Chris Lattner27a6c732007-11-24 07:07:01 +00005792 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005793}
5794
Dan Gohmand858e902010-04-17 15:26:15 +00005795SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
5796 SelectionDAG &DAG) const {
Eli Friedman23ef1052009-06-06 03:57:58 +00005797 if (Op.getValueType().isVector()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005798 if (Op.getValueType() == MVT::v2i32 &&
5799 Op.getOperand(0).getValueType() == MVT::v2f64) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005800 return Op;
5801 }
5802 return SDValue();
5803 }
5804
Eli Friedman948e95a2009-05-23 09:59:16 +00005805 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005806 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005807 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5808 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005809
Chris Lattner27a6c732007-11-24 07:07:01 +00005810 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005811 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005812 FIST, StackSlot, NULL, 0, false, false, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005813}
5814
Dan Gohmand858e902010-04-17 15:26:15 +00005815SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
5816 SelectionDAG &DAG) const {
Eli Friedman948e95a2009-05-23 09:59:16 +00005817 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5818 SDValue FIST = Vals.first, StackSlot = Vals.second;
5819 assert(FIST.getNode() && "Unexpected failure");
5820
5821 // Load the result.
5822 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
David Greene67c9d422010-02-15 16:53:33 +00005823 FIST, StackSlot, NULL, 0, false, false, 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00005824}
5825
Dan Gohmand858e902010-04-17 15:26:15 +00005826SDValue X86TargetLowering::LowerFABS(SDValue Op,
5827 SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005828 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005829 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005830 EVT VT = Op.getValueType();
5831 EVT EltVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005832 if (VT.isVector())
5833 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005834 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005835 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005836 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005837 CV.push_back(C);
5838 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005839 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005840 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005841 CV.push_back(C);
5842 CV.push_back(C);
5843 CV.push_back(C);
5844 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005845 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005846 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005847 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005848 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005849 PseudoSourceValue::getConstantPool(), 0,
5850 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005851 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005852}
5853
Dan Gohmand858e902010-04-17 15:26:15 +00005854SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005855 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005856 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005857 EVT VT = Op.getValueType();
5858 EVT EltVT = VT;
Duncan Sandsda9ad382009-09-06 19:29:07 +00005859 if (VT.isVector())
Duncan Sands83ec4b62008-06-06 12:08:01 +00005860 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005861 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005862 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005863 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005864 CV.push_back(C);
5865 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005866 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005867 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005868 CV.push_back(C);
5869 CV.push_back(C);
5870 CV.push_back(C);
5871 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005872 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005873 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005874 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005875 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005876 PseudoSourceValue::getConstantPool(), 0,
5877 false, false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005878 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005879 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00005880 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
5881 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005882 Op.getOperand(0)),
Owen Anderson825b72b2009-08-11 20:47:22 +00005883 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005884 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005885 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005886 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005887}
5888
Dan Gohmand858e902010-04-17 15:26:15 +00005889SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005890 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005891 SDValue Op0 = Op.getOperand(0);
5892 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005893 DebugLoc dl = Op.getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00005894 EVT VT = Op.getValueType();
5895 EVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005896
5897 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005898 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005899 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005900 SrcVT = VT;
5901 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005902 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005903 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005904 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005905 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005906 }
5907
5908 // At this point the operands and the result should have the same
5909 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005910
Evan Cheng68c47cb2007-01-05 07:55:56 +00005911 // First get the sign bit of second operand.
5912 std::vector<Constant*> CV;
Owen Anderson825b72b2009-08-11 20:47:22 +00005913 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005914 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5915 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005916 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005917 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5918 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5919 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5920 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005921 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005922 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005923 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005924 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005925 PseudoSourceValue::getConstantPool(), 0,
5926 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005927 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005928
5929 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005930 if (SrcVT.bitsGT(VT)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 // Op0 is MVT::f32, Op1 is MVT::f64.
5932 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5933 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
5934 DAG.getConstant(32, MVT::i32));
5935 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5936 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005937 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005938 }
5939
Evan Cheng73d6cf12007-01-05 21:37:56 +00005940 // Clear first operand sign bit.
5941 CV.clear();
Owen Anderson825b72b2009-08-11 20:47:22 +00005942 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005943 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5944 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005945 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005946 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5947 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5948 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5949 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005950 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005951 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005952 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005953 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
David Greene67c9d422010-02-15 16:53:33 +00005954 PseudoSourceValue::getConstantPool(), 0,
5955 false, false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005956 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005957
5958 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005959 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005960}
5961
Dan Gohman076aee32009-03-04 19:44:21 +00005962/// Emit nodes that will be selected as "test Op0,Op0", or something
5963/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005964SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00005965 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00005966 DebugLoc dl = Op.getDebugLoc();
5967
Dan Gohman31125812009-03-07 01:58:32 +00005968 // CF and OF aren't always set the way we want. Determine which
5969 // of these we need.
5970 bool NeedCF = false;
5971 bool NeedOF = false;
5972 switch (X86CC) {
5973 case X86::COND_A: case X86::COND_AE:
5974 case X86::COND_B: case X86::COND_BE:
5975 NeedCF = true;
5976 break;
5977 case X86::COND_G: case X86::COND_GE:
5978 case X86::COND_L: case X86::COND_LE:
5979 case X86::COND_O: case X86::COND_NO:
5980 NeedOF = true;
5981 break;
5982 default: break;
5983 }
5984
Dan Gohman076aee32009-03-04 19:44:21 +00005985 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005986 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5987 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5988 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005989 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005990 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005991 switch (Op.getNode()->getOpcode()) {
5992 case ISD::ADD:
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00005993 // Due to an isel shortcoming, be conservative if this add is
5994 // likely to be selected as part of a load-modify-store
5995 // instruction. When the root node in a match is a store, isel
5996 // doesn't know how to remap non-chain non-flag uses of other
5997 // nodes in the match, such as the ADD in this case. This leads
5998 // to the ADD being left around and reselected, with the result
5999 // being two adds in the output. Alas, even if none our users
6000 // are stores, that doesn't prove we're O.K. Ergo, if we have
6001 // any parents that aren't CopyToReg or SETCC, eschew INC/DEC.
6002 // A better fix seems to require climbing the DAG back to the
6003 // root, and it doesn't seem to be worth the effort.
Dan Gohman076aee32009-03-04 19:44:21 +00006004 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Stuart Hastings5a6a65b2010-04-28 00:35:10 +00006005 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6006 if (UI->getOpcode() != ISD::CopyToReg && UI->getOpcode() != ISD::SETCC)
Dan Gohman076aee32009-03-04 19:44:21 +00006007 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00006008 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00006009 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
6010 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00006011 if (C->getAPIntValue() == 1) {
6012 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00006013 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00006014 break;
6015 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00006016 // An add of negative one (subtract of one) will be selected as a DEC.
6017 if (C->getAPIntValue().isAllOnesValue()) {
6018 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00006019 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00006020 break;
6021 }
6022 }
Dan Gohman076aee32009-03-04 19:44:21 +00006023 // Otherwise use a regular EFLAGS-setting add.
6024 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00006025 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006026 break;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006027 case ISD::AND: {
6028 // If the primary and result isn't used, don't bother using X86ISD::AND,
6029 // because a TEST instruction will be better.
6030 bool NonFlagUse = false;
6031 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
Evan Cheng17751da2010-01-07 00:54:06 +00006032 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
6033 SDNode *User = *UI;
6034 unsigned UOpNo = UI.getOperandNo();
6035 if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
6036 // Look pass truncate.
6037 UOpNo = User->use_begin().getOperandNo();
6038 User = *User->use_begin();
6039 }
6040 if (User->getOpcode() != ISD::BRCOND &&
6041 User->getOpcode() != ISD::SETCC &&
6042 (User->getOpcode() != ISD::SELECT || UOpNo != 0)) {
Dan Gohmane220c4b2009-09-18 19:59:53 +00006043 NonFlagUse = true;
6044 break;
6045 }
Evan Cheng17751da2010-01-07 00:54:06 +00006046 }
Dan Gohmane220c4b2009-09-18 19:59:53 +00006047 if (!NonFlagUse)
6048 break;
6049 }
6050 // FALL THROUGH
Dan Gohman076aee32009-03-04 19:44:21 +00006051 case ISD::SUB:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006052 case ISD::OR:
6053 case ISD::XOR:
6054 // Due to the ISEL shortcoming noted above, be conservative if this op is
Dan Gohman076aee32009-03-04 19:44:21 +00006055 // likely to be selected as part of a load-modify-store instruction.
6056 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
6057 UE = Op.getNode()->use_end(); UI != UE; ++UI)
6058 if (UI->getOpcode() == ISD::STORE)
6059 goto default_case;
Dan Gohmane220c4b2009-09-18 19:59:53 +00006060 // Otherwise use a regular EFLAGS-setting instruction.
6061 switch (Op.getNode()->getOpcode()) {
6062 case ISD::SUB: Opcode = X86ISD::SUB; break;
6063 case ISD::OR: Opcode = X86ISD::OR; break;
6064 case ISD::XOR: Opcode = X86ISD::XOR; break;
6065 case ISD::AND: Opcode = X86ISD::AND; break;
6066 default: llvm_unreachable("unexpected operator!");
6067 }
Dan Gohman51bb4742009-03-05 21:29:28 +00006068 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00006069 break;
6070 case X86ISD::ADD:
6071 case X86ISD::SUB:
6072 case X86ISD::INC:
6073 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00006074 case X86ISD::OR:
6075 case X86ISD::XOR:
6076 case X86ISD::AND:
Dan Gohman076aee32009-03-04 19:44:21 +00006077 return SDValue(Op.getNode(), 1);
6078 default:
6079 default_case:
6080 break;
6081 }
6082 if (Opcode != 0) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006083 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00006084 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00006085 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00006086 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00006087 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00006088 DAG.ReplaceAllUsesWith(Op, New);
6089 return SDValue(New.getNode(), 1);
6090 }
6091 }
6092
6093 // Otherwise just emit a CMP with 0, which is the TEST pattern.
Owen Anderson825b72b2009-08-11 20:47:22 +00006094 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
Dan Gohman076aee32009-03-04 19:44:21 +00006095 DAG.getConstant(0, Op.getValueType()));
6096}
6097
6098/// Emit nodes that will be selected as "cmp Op0,Op1", or something
6099/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00006100SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
Evan Cheng552f09a2010-04-26 19:06:11 +00006101 SelectionDAG &DAG) const {
Dan Gohman076aee32009-03-04 19:44:21 +00006102 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
6103 if (C->getAPIntValue() == 0)
Evan Cheng552f09a2010-04-26 19:06:11 +00006104 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00006105
6106 DebugLoc dl = Op0.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00006107 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
Dan Gohman076aee32009-03-04 19:44:21 +00006108}
6109
Evan Chengd40d03e2010-01-06 19:38:29 +00006110/// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
6111/// if it's possible.
Evan Cheng5528e7b2010-04-21 01:47:12 +00006112SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
6113 DebugLoc dl, SelectionDAG &DAG) const {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006114 SDValue Op0 = And.getOperand(0);
6115 SDValue Op1 = And.getOperand(1);
6116 if (Op0.getOpcode() == ISD::TRUNCATE)
6117 Op0 = Op0.getOperand(0);
6118 if (Op1.getOpcode() == ISD::TRUNCATE)
6119 Op1 = Op1.getOperand(0);
6120
Evan Chengd40d03e2010-01-06 19:38:29 +00006121 SDValue LHS, RHS;
Evan Cheng2c755ba2010-02-27 07:36:59 +00006122 if (Op1.getOpcode() == ISD::SHL) {
6123 if (ConstantSDNode *And10C = dyn_cast<ConstantSDNode>(Op1.getOperand(0)))
6124 if (And10C->getZExtValue() == 1) {
6125 LHS = Op0;
6126 RHS = Op1.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006127 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006128 } else if (Op0.getOpcode() == ISD::SHL) {
6129 if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0)))
6130 if (And00C->getZExtValue() == 1) {
6131 LHS = Op1;
6132 RHS = Op0.getOperand(1);
Evan Chengd40d03e2010-01-06 19:38:29 +00006133 }
Evan Cheng2c755ba2010-02-27 07:36:59 +00006134 } else if (Op1.getOpcode() == ISD::Constant) {
6135 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
6136 SDValue AndLHS = Op0;
Evan Chengd40d03e2010-01-06 19:38:29 +00006137 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
6138 LHS = AndLHS.getOperand(0);
6139 RHS = AndLHS.getOperand(1);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006140 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006141 }
Evan Cheng0488db92007-09-25 01:57:46 +00006142
Evan Chengd40d03e2010-01-06 19:38:29 +00006143 if (LHS.getNode()) {
Evan Chenge5b51ac2010-04-17 06:13:15 +00006144 // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT
Evan Chengd40d03e2010-01-06 19:38:29 +00006145 // instruction. Since the shift amount is in-range-or-undefined, we know
Evan Chenge5b51ac2010-04-17 06:13:15 +00006146 // that doing a bittest on the i32 value is ok. We extend to i32 because
Evan Chengd40d03e2010-01-06 19:38:29 +00006147 // the encoding for the i16 version is larger than the i32 version.
Evan Chenge5b51ac2010-04-17 06:13:15 +00006148 // Also promote i16 to i32 for performance / code size reason.
6149 if (LHS.getValueType() == MVT::i8 ||
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00006150 LHS.getValueType() == MVT::i16)
Evan Chengd40d03e2010-01-06 19:38:29 +00006151 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00006152
Evan Chengd40d03e2010-01-06 19:38:29 +00006153 // If the operand types disagree, extend the shift amount to match. Since
6154 // BT ignores high bits (like shifts) we can use anyextend.
6155 if (LHS.getValueType() != RHS.getValueType())
6156 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00006157
Evan Chengd40d03e2010-01-06 19:38:29 +00006158 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
6159 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
6160 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6161 DAG.getConstant(Cond, MVT::i8), BT);
Chris Lattnere55484e2008-12-25 05:34:37 +00006162 }
6163
Evan Cheng54de3ea2010-01-05 06:52:31 +00006164 return SDValue();
6165}
6166
Dan Gohmand858e902010-04-17 15:26:15 +00006167SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng54de3ea2010-01-05 06:52:31 +00006168 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
6169 SDValue Op0 = Op.getOperand(0);
6170 SDValue Op1 = Op.getOperand(1);
6171 DebugLoc dl = Op.getDebugLoc();
6172 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
6173
6174 // Optimize to BT if possible.
Evan Chengd40d03e2010-01-06 19:38:29 +00006175 // Lower (X & (1 << N)) == 0 to BT(X, N).
6176 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
6177 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
6178 if (Op0.getOpcode() == ISD::AND &&
6179 Op0.hasOneUse() &&
6180 Op1.getOpcode() == ISD::Constant &&
6181 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
6182 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6183 SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG);
6184 if (NewSetCC.getNode())
6185 return NewSetCC;
6186 }
Evan Cheng54de3ea2010-01-05 06:52:31 +00006187
Evan Cheng2c755ba2010-02-27 07:36:59 +00006188 // Look for "(setcc) == / != 1" to avoid unncessary setcc.
6189 if (Op0.getOpcode() == X86ISD::SETCC &&
6190 Op1.getOpcode() == ISD::Constant &&
6191 (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 ||
6192 cast<ConstantSDNode>(Op1)->isNullValue()) &&
6193 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
6194 X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
6195 bool Invert = (CC == ISD::SETNE) ^
6196 cast<ConstantSDNode>(Op1)->isNullValue();
6197 if (Invert)
6198 CCode = X86::GetOppositeBranchCondition(CCode);
6199 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6200 DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1));
6201 }
6202
Evan Chenge5b51ac2010-04-17 06:13:15 +00006203 bool isFP = Op1.getValueType().isFloatingPoint();
Chris Lattnere55484e2008-12-25 05:34:37 +00006204 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006205 if (X86CC == X86::COND_INVALID)
6206 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00006207
Evan Cheng552f09a2010-04-26 19:06:11 +00006208 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Evan Chengad9c0a32009-12-15 00:53:42 +00006209
6210 // Use sbb x, x to materialize carry bit into a GPR.
Evan Cheng2e489c42009-12-16 00:53:11 +00006211 if (X86CC == X86::COND_B)
Evan Chengad9c0a32009-12-15 00:53:42 +00006212 return DAG.getNode(ISD::AND, dl, MVT::i8,
6213 DAG.getNode(X86ISD::SETCC_CARRY, dl, MVT::i8,
6214 DAG.getConstant(X86CC, MVT::i8), Cond),
6215 DAG.getConstant(1, MVT::i8));
Evan Chengad9c0a32009-12-15 00:53:42 +00006216
Owen Anderson825b72b2009-08-11 20:47:22 +00006217 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6218 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006219}
6220
Dan Gohmand858e902010-04-17 15:26:15 +00006221SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006222 SDValue Cond;
6223 SDValue Op0 = Op.getOperand(0);
6224 SDValue Op1 = Op.getOperand(1);
6225 SDValue CC = Op.getOperand(2);
Owen Andersone50ed302009-08-10 22:56:29 +00006226 EVT VT = Op.getValueType();
Nate Begeman30a0de92008-07-17 16:51:19 +00006227 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
6228 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006229 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00006230
6231 if (isFP) {
6232 unsigned SSECC = 8;
Owen Andersone50ed302009-08-10 22:56:29 +00006233 EVT VT0 = Op0.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00006234 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
6235 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00006236 bool Swap = false;
6237
6238 switch (SetCCOpcode) {
6239 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006240 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00006241 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006242 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00006243 case ISD::SETGT: Swap = true; // Fallthrough
6244 case ISD::SETLT:
6245 case ISD::SETOLT: SSECC = 1; break;
6246 case ISD::SETOGE:
6247 case ISD::SETGE: Swap = true; // Fallthrough
6248 case ISD::SETLE:
6249 case ISD::SETOLE: SSECC = 2; break;
6250 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00006251 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00006252 case ISD::SETNE: SSECC = 4; break;
6253 case ISD::SETULE: Swap = true;
6254 case ISD::SETUGE: SSECC = 5; break;
6255 case ISD::SETULT: Swap = true;
6256 case ISD::SETUGT: SSECC = 6; break;
6257 case ISD::SETO: SSECC = 7; break;
6258 }
6259 if (Swap)
6260 std::swap(Op0, Op1);
6261
Nate Begemanfb8ead02008-07-25 19:05:58 +00006262 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00006263 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00006264 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00006265 SDValue UNORD, EQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006266 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
6267 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006268 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006269 }
6270 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00006271 SDValue ORD, NEQ;
Owen Anderson825b72b2009-08-11 20:47:22 +00006272 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
6273 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00006274 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00006275 }
Torok Edwinc23197a2009-07-14 16:55:14 +00006276 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00006277 }
6278 // Handle all other FP comparisons here.
Owen Anderson825b72b2009-08-11 20:47:22 +00006279 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00006280 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006281
Nate Begeman30a0de92008-07-17 16:51:19 +00006282 // We are handling one of the integer comparisons here. Since SSE only has
6283 // GT and EQ comparisons for integer, swapping operands and multiple
6284 // operations may be required for some comparisons.
6285 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
6286 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006287
Owen Anderson825b72b2009-08-11 20:47:22 +00006288 switch (VT.getSimpleVT().SimpleTy) {
Nate Begeman30a0de92008-07-17 16:51:19 +00006289 default: break;
Owen Anderson825b72b2009-08-11 20:47:22 +00006290 case MVT::v8i8:
6291 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
6292 case MVT::v4i16:
6293 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
6294 case MVT::v2i32:
6295 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
6296 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
Nate Begeman30a0de92008-07-17 16:51:19 +00006297 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006298
Nate Begeman30a0de92008-07-17 16:51:19 +00006299 switch (SetCCOpcode) {
6300 default: break;
6301 case ISD::SETNE: Invert = true;
6302 case ISD::SETEQ: Opc = EQOpc; break;
6303 case ISD::SETLT: Swap = true;
6304 case ISD::SETGT: Opc = GTOpc; break;
6305 case ISD::SETGE: Swap = true;
6306 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
6307 case ISD::SETULT: Swap = true;
6308 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
6309 case ISD::SETUGE: Swap = true;
6310 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
6311 }
6312 if (Swap)
6313 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006314
Nate Begeman30a0de92008-07-17 16:51:19 +00006315 // Since SSE has no unsigned integer comparisons, we need to flip the sign
6316 // bits of the inputs before performing those operations.
6317 if (FlipSigns) {
Owen Andersone50ed302009-08-10 22:56:29 +00006318 EVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006319 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
6320 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00006321 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00006322 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
6323 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00006324 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
6325 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00006326 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006327
Dale Johannesenace16102009-02-03 19:33:06 +00006328 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00006329
6330 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00006331 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00006332 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00006333
Nate Begeman30a0de92008-07-17 16:51:19 +00006334 return Result;
6335}
Evan Cheng0488db92007-09-25 01:57:46 +00006336
Evan Cheng370e5342008-12-03 08:38:43 +00006337// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00006338static bool isX86LogicalCmp(SDValue Op) {
6339 unsigned Opc = Op.getNode()->getOpcode();
6340 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
6341 return true;
6342 if (Op.getResNo() == 1 &&
6343 (Opc == X86ISD::ADD ||
6344 Opc == X86ISD::SUB ||
6345 Opc == X86ISD::SMUL ||
6346 Opc == X86ISD::UMUL ||
6347 Opc == X86ISD::INC ||
Dan Gohmane220c4b2009-09-18 19:59:53 +00006348 Opc == X86ISD::DEC ||
6349 Opc == X86ISD::OR ||
6350 Opc == X86ISD::XOR ||
6351 Opc == X86ISD::AND))
Dan Gohman076aee32009-03-04 19:44:21 +00006352 return true;
6353
6354 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00006355}
6356
Dan Gohmand858e902010-04-17 15:26:15 +00006357SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006358 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006359 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006360 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006361 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00006362
Dan Gohman1a492952009-10-20 16:22:37 +00006363 if (Cond.getOpcode() == ISD::SETCC) {
6364 SDValue NewCond = LowerSETCC(Cond, DAG);
6365 if (NewCond.getNode())
6366 Cond = NewCond;
6367 }
Evan Cheng734503b2006-09-11 02:19:56 +00006368
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006369 // (select (x == 0), -1, 0) -> (sign_bit (x - 1))
6370 SDValue Op1 = Op.getOperand(1);
6371 SDValue Op2 = Op.getOperand(2);
6372 if (Cond.getOpcode() == X86ISD::SETCC &&
6373 cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue() == X86::COND_E) {
6374 SDValue Cmp = Cond.getOperand(1);
6375 if (Cmp.getOpcode() == X86ISD::CMP) {
6376 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op1);
6377 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2);
6378 ConstantSDNode *RHSC =
6379 dyn_cast<ConstantSDNode>(Cmp.getOperand(1).getNode());
6380 if (N1C && N1C->isAllOnesValue() &&
6381 N2C && N2C->isNullValue() &&
6382 RHSC && RHSC->isNullValue()) {
6383 SDValue CmpOp0 = Cmp.getOperand(0);
Chris Lattnerda0688e2010-03-14 18:44:35 +00006384 Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006385 CmpOp0, DAG.getConstant(1, CmpOp0.getValueType()));
6386 return DAG.getNode(X86ISD::SETCC_CARRY, dl, Op.getValueType(),
6387 DAG.getConstant(X86::COND_B, MVT::i8), Cmp);
6388 }
6389 }
6390 }
6391
Evan Chengad9c0a32009-12-15 00:53:42 +00006392 // Look pass (and (setcc_carry (cmp ...)), 1).
6393 if (Cond.getOpcode() == ISD::AND &&
6394 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6395 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6396 if (C && C->getAPIntValue() == 1)
6397 Cond = Cond.getOperand(0);
6398 }
6399
Evan Cheng3f41d662007-10-08 22:16:29 +00006400 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6401 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006402 if (Cond.getOpcode() == X86ISD::SETCC ||
6403 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006404 CC = Cond.getOperand(0);
6405
Dan Gohman475871a2008-07-27 21:46:04 +00006406 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006407 unsigned Opc = Cmp.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +00006408 EVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00006409
Evan Cheng3f41d662007-10-08 22:16:29 +00006410 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006411 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00006412 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00006413 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00006414
Chris Lattnerd1980a52009-03-12 06:52:53 +00006415 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
6416 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00006417 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006418 addTest = false;
6419 }
6420 }
6421
6422 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006423 // Look pass the truncate.
6424 if (Cond.getOpcode() == ISD::TRUNCATE)
6425 Cond = Cond.getOperand(0);
6426
6427 // We know the result of AND is compared against zero. Try to match
6428 // it to BT.
6429 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6430 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6431 if (NewSetCC.getNode()) {
6432 CC = NewSetCC.getOperand(0);
6433 Cond = NewSetCC.getOperand(1);
6434 addTest = false;
6435 }
6436 }
6437 }
6438
6439 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006440 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006441 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006442 }
6443
Evan Cheng0488db92007-09-25 01:57:46 +00006444 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
6445 // condition is true.
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00006446 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
6447 SDValue Ops[] = { Op2, Op1, CC, Cond };
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00006448 return DAG.getNode(X86ISD::CMOV, dl, VTs, Ops, array_lengthof(Ops));
Evan Cheng0488db92007-09-25 01:57:46 +00006449}
6450
Evan Cheng370e5342008-12-03 08:38:43 +00006451// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
6452// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
6453// from the AND / OR.
6454static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
6455 Opc = Op.getOpcode();
6456 if (Opc != ISD::OR && Opc != ISD::AND)
6457 return false;
6458 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6459 Op.getOperand(0).hasOneUse() &&
6460 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
6461 Op.getOperand(1).hasOneUse());
6462}
6463
Evan Cheng961d6d42009-02-02 08:19:07 +00006464// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
6465// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00006466static bool isXor1OfSetCC(SDValue Op) {
6467 if (Op.getOpcode() != ISD::XOR)
6468 return false;
6469 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6470 if (N1C && N1C->getAPIntValue() == 1) {
6471 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
6472 Op.getOperand(0).hasOneUse();
6473 }
6474 return false;
6475}
6476
Dan Gohmand858e902010-04-17 15:26:15 +00006477SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng734503b2006-09-11 02:19:56 +00006478 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006479 SDValue Chain = Op.getOperand(0);
6480 SDValue Cond = Op.getOperand(1);
6481 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006482 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00006483 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00006484
Dan Gohman1a492952009-10-20 16:22:37 +00006485 if (Cond.getOpcode() == ISD::SETCC) {
6486 SDValue NewCond = LowerSETCC(Cond, DAG);
6487 if (NewCond.getNode())
6488 Cond = NewCond;
6489 }
Chris Lattnere55484e2008-12-25 05:34:37 +00006490#if 0
6491 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00006492 else if (Cond.getOpcode() == X86ISD::ADD ||
6493 Cond.getOpcode() == X86ISD::SUB ||
6494 Cond.getOpcode() == X86ISD::SMUL ||
6495 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00006496 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00006497#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00006498
Evan Chengad9c0a32009-12-15 00:53:42 +00006499 // Look pass (and (setcc_carry (cmp ...)), 1).
6500 if (Cond.getOpcode() == ISD::AND &&
6501 Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
6502 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
6503 if (C && C->getAPIntValue() == 1)
6504 Cond = Cond.getOperand(0);
6505 }
6506
Evan Cheng3f41d662007-10-08 22:16:29 +00006507 // If condition flag is set by a X86ISD::CMP, then use it as the condition
6508 // setting operand in place of the X86ISD::SETCC.
Evan Chengad9c0a32009-12-15 00:53:42 +00006509 if (Cond.getOpcode() == X86ISD::SETCC ||
6510 Cond.getOpcode() == X86ISD::SETCC_CARRY) {
Evan Cheng734503b2006-09-11 02:19:56 +00006511 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006512
Dan Gohman475871a2008-07-27 21:46:04 +00006513 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00006514 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00006515 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00006516 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00006517 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00006518 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00006519 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00006520 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006521 default: break;
6522 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00006523 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00006524 // These can only come from an arithmetic instruction with overflow,
6525 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00006526 Cond = Cond.getNode()->getOperand(1);
6527 addTest = false;
6528 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006529 }
Evan Cheng0488db92007-09-25 01:57:46 +00006530 }
Evan Cheng370e5342008-12-03 08:38:43 +00006531 } else {
6532 unsigned CondOpc;
6533 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
6534 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00006535 if (CondOpc == ISD::OR) {
6536 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
6537 // two branches instead of an explicit OR instruction with a
6538 // separate test.
6539 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006540 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00006541 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006542 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006543 Chain, Dest, CC, Cmp);
6544 CC = Cond.getOperand(1).getOperand(0);
6545 Cond = Cmp;
6546 addTest = false;
6547 }
6548 } else { // ISD::AND
6549 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
6550 // two branches instead of an explicit AND instruction with a
6551 // separate test. However, we only do this if this block doesn't
6552 // have a fall-through edge, because this requires an explicit
6553 // jmp when the condition is false.
6554 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00006555 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00006556 Op.getNode()->hasOneUse()) {
6557 X86::CondCode CCode =
6558 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6559 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006560 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006561 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
6562 // Look for an unconditional branch following this conditional branch.
6563 // We need this because we need to reverse the successors in order
6564 // to implement FCMP_OEQ.
6565 if (User.getOpcode() == ISD::BR) {
6566 SDValue FalseBB = User.getOperand(1);
6567 SDValue NewBR =
6568 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
6569 assert(NewBR == User);
6570 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00006571
Dale Johannesene4d209d2009-02-03 20:21:25 +00006572 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00006573 Chain, Dest, CC, Cmp);
6574 X86::CondCode CCode =
6575 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
6576 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006577 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng370e5342008-12-03 08:38:43 +00006578 Cond = Cmp;
6579 addTest = false;
6580 }
6581 }
Dan Gohman279c22e2008-10-21 03:29:32 +00006582 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00006583 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
6584 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
6585 // It should be transformed during dag combiner except when the condition
6586 // is set by a arithmetics with overflow node.
6587 X86::CondCode CCode =
6588 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
6589 CCode = X86::GetOppositeBranchCondition(CCode);
Owen Anderson825b72b2009-08-11 20:47:22 +00006590 CC = DAG.getConstant(CCode, MVT::i8);
Evan Cheng67ad9db2009-02-02 08:07:36 +00006591 Cond = Cond.getOperand(0).getOperand(1);
6592 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00006593 }
Evan Cheng0488db92007-09-25 01:57:46 +00006594 }
6595
6596 if (addTest) {
Evan Chengd40d03e2010-01-06 19:38:29 +00006597 // Look pass the truncate.
6598 if (Cond.getOpcode() == ISD::TRUNCATE)
6599 Cond = Cond.getOperand(0);
6600
6601 // We know the result of AND is compared against zero. Try to match
6602 // it to BT.
6603 if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
6604 SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG);
6605 if (NewSetCC.getNode()) {
6606 CC = NewSetCC.getOperand(0);
6607 Cond = NewSetCC.getOperand(1);
6608 addTest = false;
6609 }
6610 }
6611 }
6612
6613 if (addTest) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006614 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng552f09a2010-04-26 19:06:11 +00006615 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00006616 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00006617 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00006618 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00006619}
6620
Anton Korobeynikove060b532007-04-17 19:34:00 +00006621
6622// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
6623// Calls to _alloca is needed to probe the stack when allocating more than 4k
6624// bytes in one go. Touching the stack at 4K increments is necessary to ensure
6625// that the guard pages used by the OS virtual memory manager are allocated in
6626// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00006627SDValue
6628X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00006629 SelectionDAG &DAG) const {
Anton Korobeynikove060b532007-04-17 19:34:00 +00006630 assert(Subtarget->isTargetCygMing() &&
6631 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006632 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006633
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006634 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00006635 SDValue Chain = Op.getOperand(0);
6636 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006637 // FIXME: Ensure alignment here
6638
Dan Gohman475871a2008-07-27 21:46:04 +00006639 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006640
Owen Andersone50ed302009-08-10 22:56:29 +00006641 EVT IntPtr = getPointerTy();
Owen Anderson825b72b2009-08-11 20:47:22 +00006642 EVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006643
Dale Johannesendd64c412009-02-04 00:33:20 +00006644 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006645 Flag = Chain.getValue(1);
6646
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006647 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00006648
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00006649 Chain = DAG.getNode(X86ISD::MINGW_ALLOCA, dl, NodeTys, Chain, Flag);
6650 Flag = Chain.getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006651
Dale Johannesendd64c412009-02-04 00:33:20 +00006652 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00006653
Dan Gohman475871a2008-07-27 21:46:04 +00006654 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006655 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006656}
6657
Dan Gohmand858e902010-04-17 15:26:15 +00006658SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman1e93df62010-04-17 14:41:14 +00006659 MachineFunction &MF = DAG.getMachineFunction();
6660 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
6661
Dan Gohman69de1932008-02-06 22:27:42 +00006662 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006663 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00006664
Evan Cheng25ab6902006-09-08 06:48:29 +00006665 if (!Subtarget->is64Bit()) {
6666 // vastart just stores the address of the VarArgsFrameIndex slot into the
6667 // memory location argument.
Dan Gohman1e93df62010-04-17 14:41:14 +00006668 SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6669 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006670 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0,
6671 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006672 }
6673
6674 // __va_list_tag:
6675 // gp_offset (0 - 6 * 8)
6676 // fp_offset (48 - 48 + 8 * 16)
6677 // overflow_arg_area (point to parameters coming in memory).
6678 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006679 SmallVector<SDValue, 8> MemOps;
6680 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006681 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006682 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006683 DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
6684 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006685 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006686 MemOps.push_back(Store);
6687
6688 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006689 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006690 FIN, DAG.getIntPtrConstant(4));
6691 Store = DAG.getStore(Op.getOperand(0), dl,
Dan Gohman1e93df62010-04-17 14:41:14 +00006692 DAG.getConstant(FuncInfo->getVarArgsFPOffset(),
6693 MVT::i32),
David Greene67c9d422010-02-15 16:53:33 +00006694 FIN, SV, 0, false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006695 MemOps.push_back(Store);
6696
6697 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006698 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006699 FIN, DAG.getIntPtrConstant(4));
Dan Gohman1e93df62010-04-17 14:41:14 +00006700 SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
6701 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006702 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0,
6703 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006704 MemOps.push_back(Store);
6705
6706 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006707 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006708 FIN, DAG.getIntPtrConstant(8));
Dan Gohman1e93df62010-04-17 14:41:14 +00006709 SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
6710 getPointerTy());
David Greene67c9d422010-02-15 16:53:33 +00006711 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0,
6712 false, false, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006713 MemOps.push_back(Store);
Owen Anderson825b72b2009-08-11 20:47:22 +00006714 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006715 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006716}
6717
Dan Gohmand858e902010-04-17 15:26:15 +00006718SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
Dan Gohman9018e832008-05-10 01:26:14 +00006719 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6720 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006721 SDValue Chain = Op.getOperand(0);
6722 SDValue SrcPtr = Op.getOperand(1);
6723 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006724
Chris Lattner75361b62010-04-07 22:58:41 +00006725 report_fatal_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006726 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006727}
6728
Dan Gohmand858e902010-04-17 15:26:15 +00006729SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) const {
Evan Chengae642192007-03-02 23:16:35 +00006730 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006731 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006732 SDValue Chain = Op.getOperand(0);
6733 SDValue DstPtr = Op.getOperand(1);
6734 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006735 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6736 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006737 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006738
Dale Johannesendd64c412009-02-04 00:33:20 +00006739 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Mon P Wang20adc9d2010-04-04 03:10:48 +00006740 DAG.getIntPtrConstant(24), 8, /*isVolatile*/false,
6741 false, DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006742}
6743
Dan Gohman475871a2008-07-27 21:46:04 +00006744SDValue
Dan Gohmand858e902010-04-17 15:26:15 +00006745X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006746 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006747 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006748 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006749 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006750 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006751 case Intrinsic::x86_sse_comieq_ss:
6752 case Intrinsic::x86_sse_comilt_ss:
6753 case Intrinsic::x86_sse_comile_ss:
6754 case Intrinsic::x86_sse_comigt_ss:
6755 case Intrinsic::x86_sse_comige_ss:
6756 case Intrinsic::x86_sse_comineq_ss:
6757 case Intrinsic::x86_sse_ucomieq_ss:
6758 case Intrinsic::x86_sse_ucomilt_ss:
6759 case Intrinsic::x86_sse_ucomile_ss:
6760 case Intrinsic::x86_sse_ucomigt_ss:
6761 case Intrinsic::x86_sse_ucomige_ss:
6762 case Intrinsic::x86_sse_ucomineq_ss:
6763 case Intrinsic::x86_sse2_comieq_sd:
6764 case Intrinsic::x86_sse2_comilt_sd:
6765 case Intrinsic::x86_sse2_comile_sd:
6766 case Intrinsic::x86_sse2_comigt_sd:
6767 case Intrinsic::x86_sse2_comige_sd:
6768 case Intrinsic::x86_sse2_comineq_sd:
6769 case Intrinsic::x86_sse2_ucomieq_sd:
6770 case Intrinsic::x86_sse2_ucomilt_sd:
6771 case Intrinsic::x86_sse2_ucomile_sd:
6772 case Intrinsic::x86_sse2_ucomigt_sd:
6773 case Intrinsic::x86_sse2_ucomige_sd:
6774 case Intrinsic::x86_sse2_ucomineq_sd: {
6775 unsigned Opc = 0;
6776 ISD::CondCode CC = ISD::SETCC_INVALID;
6777 switch (IntNo) {
6778 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006779 case Intrinsic::x86_sse_comieq_ss:
6780 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006781 Opc = X86ISD::COMI;
6782 CC = ISD::SETEQ;
6783 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006784 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006785 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006786 Opc = X86ISD::COMI;
6787 CC = ISD::SETLT;
6788 break;
6789 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006790 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006791 Opc = X86ISD::COMI;
6792 CC = ISD::SETLE;
6793 break;
6794 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006795 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006796 Opc = X86ISD::COMI;
6797 CC = ISD::SETGT;
6798 break;
6799 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006800 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006801 Opc = X86ISD::COMI;
6802 CC = ISD::SETGE;
6803 break;
6804 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006805 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006806 Opc = X86ISD::COMI;
6807 CC = ISD::SETNE;
6808 break;
6809 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006810 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006811 Opc = X86ISD::UCOMI;
6812 CC = ISD::SETEQ;
6813 break;
6814 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006815 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006816 Opc = X86ISD::UCOMI;
6817 CC = ISD::SETLT;
6818 break;
6819 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006820 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006821 Opc = X86ISD::UCOMI;
6822 CC = ISD::SETLE;
6823 break;
6824 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006825 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006826 Opc = X86ISD::UCOMI;
6827 CC = ISD::SETGT;
6828 break;
6829 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006830 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006831 Opc = X86ISD::UCOMI;
6832 CC = ISD::SETGE;
6833 break;
6834 case Intrinsic::x86_sse_ucomineq_ss:
6835 case Intrinsic::x86_sse2_ucomineq_sd:
6836 Opc = X86ISD::UCOMI;
6837 CC = ISD::SETNE;
6838 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006839 }
Evan Cheng734503b2006-09-11 02:19:56 +00006840
Dan Gohman475871a2008-07-27 21:46:04 +00006841 SDValue LHS = Op.getOperand(1);
6842 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006843 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dan Gohman1a492952009-10-20 16:22:37 +00006844 assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
Owen Anderson825b72b2009-08-11 20:47:22 +00006845 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6846 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
6847 DAG.getConstant(X86CC, MVT::i8), Cond);
6848 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006849 }
Eric Christopher71c67532009-07-29 00:28:05 +00006850 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006851 // an integer value, not just an instruction so lower it to the ptest
6852 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006853 case Intrinsic::x86_sse41_ptestz:
6854 case Intrinsic::x86_sse41_ptestc:
6855 case Intrinsic::x86_sse41_ptestnzc:{
6856 unsigned X86CC = 0;
6857 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006858 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006859 case Intrinsic::x86_sse41_ptestz:
6860 // ZF = 1
6861 X86CC = X86::COND_E;
6862 break;
6863 case Intrinsic::x86_sse41_ptestc:
6864 // CF = 1
6865 X86CC = X86::COND_B;
6866 break;
Eric Christopherfd179292009-08-27 18:07:15 +00006867 case Intrinsic::x86_sse41_ptestnzc:
Eric Christopher71c67532009-07-29 00:28:05 +00006868 // ZF and CF = 0
6869 X86CC = X86::COND_A;
6870 break;
6871 }
Eric Christopherfd179292009-08-27 18:07:15 +00006872
Eric Christopher71c67532009-07-29 00:28:05 +00006873 SDValue LHS = Op.getOperand(1);
6874 SDValue RHS = Op.getOperand(2);
Owen Anderson825b72b2009-08-11 20:47:22 +00006875 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6876 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6877 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6878 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Eric Christopher71c67532009-07-29 00:28:05 +00006879 }
Evan Cheng5759f972008-05-04 09:15:50 +00006880
6881 // Fix vector shift instructions where the last operand is a non-immediate
6882 // i32 value.
6883 case Intrinsic::x86_sse2_pslli_w:
6884 case Intrinsic::x86_sse2_pslli_d:
6885 case Intrinsic::x86_sse2_pslli_q:
6886 case Intrinsic::x86_sse2_psrli_w:
6887 case Intrinsic::x86_sse2_psrli_d:
6888 case Intrinsic::x86_sse2_psrli_q:
6889 case Intrinsic::x86_sse2_psrai_w:
6890 case Intrinsic::x86_sse2_psrai_d:
6891 case Intrinsic::x86_mmx_pslli_w:
6892 case Intrinsic::x86_mmx_pslli_d:
6893 case Intrinsic::x86_mmx_pslli_q:
6894 case Intrinsic::x86_mmx_psrli_w:
6895 case Intrinsic::x86_mmx_psrli_d:
6896 case Intrinsic::x86_mmx_psrli_q:
6897 case Intrinsic::x86_mmx_psrai_w:
6898 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006899 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006900 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006901 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006902
6903 unsigned NewIntNo = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00006904 EVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006905 switch (IntNo) {
6906 case Intrinsic::x86_sse2_pslli_w:
6907 NewIntNo = Intrinsic::x86_sse2_psll_w;
6908 break;
6909 case Intrinsic::x86_sse2_pslli_d:
6910 NewIntNo = Intrinsic::x86_sse2_psll_d;
6911 break;
6912 case Intrinsic::x86_sse2_pslli_q:
6913 NewIntNo = Intrinsic::x86_sse2_psll_q;
6914 break;
6915 case Intrinsic::x86_sse2_psrli_w:
6916 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6917 break;
6918 case Intrinsic::x86_sse2_psrli_d:
6919 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6920 break;
6921 case Intrinsic::x86_sse2_psrli_q:
6922 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6923 break;
6924 case Intrinsic::x86_sse2_psrai_w:
6925 NewIntNo = Intrinsic::x86_sse2_psra_w;
6926 break;
6927 case Intrinsic::x86_sse2_psrai_d:
6928 NewIntNo = Intrinsic::x86_sse2_psra_d;
6929 break;
6930 default: {
Owen Anderson825b72b2009-08-11 20:47:22 +00006931 ShAmtVT = MVT::v2i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006932 switch (IntNo) {
6933 case Intrinsic::x86_mmx_pslli_w:
6934 NewIntNo = Intrinsic::x86_mmx_psll_w;
6935 break;
6936 case Intrinsic::x86_mmx_pslli_d:
6937 NewIntNo = Intrinsic::x86_mmx_psll_d;
6938 break;
6939 case Intrinsic::x86_mmx_pslli_q:
6940 NewIntNo = Intrinsic::x86_mmx_psll_q;
6941 break;
6942 case Intrinsic::x86_mmx_psrli_w:
6943 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6944 break;
6945 case Intrinsic::x86_mmx_psrli_d:
6946 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6947 break;
6948 case Intrinsic::x86_mmx_psrli_q:
6949 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6950 break;
6951 case Intrinsic::x86_mmx_psrai_w:
6952 NewIntNo = Intrinsic::x86_mmx_psra_w;
6953 break;
6954 case Intrinsic::x86_mmx_psrai_d:
6955 NewIntNo = Intrinsic::x86_mmx_psra_d;
6956 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006957 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006958 }
6959 break;
6960 }
6961 }
Mon P Wangefa42202009-09-03 19:56:25 +00006962
6963 // The vector shift intrinsics with scalars uses 32b shift amounts but
6964 // the sse2/mmx shift instructions reads 64 bits. Set the upper 32 bits
6965 // to be zero.
6966 SDValue ShOps[4];
6967 ShOps[0] = ShAmt;
6968 ShOps[1] = DAG.getConstant(0, MVT::i32);
6969 if (ShAmtVT == MVT::v4i32) {
6970 ShOps[2] = DAG.getUNDEF(MVT::i32);
6971 ShOps[3] = DAG.getUNDEF(MVT::i32);
6972 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 4);
6973 } else {
6974 ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, ShAmtVT, &ShOps[0], 2);
6975 }
6976
Owen Andersone50ed302009-08-10 22:56:29 +00006977 EVT VT = Op.getValueType();
Mon P Wangefa42202009-09-03 19:56:25 +00006978 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT, ShAmt);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006979 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00006980 DAG.getConstant(NewIntNo, MVT::i32),
Evan Cheng5759f972008-05-04 09:15:50 +00006981 Op.getOperand(1), ShAmt);
6982 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006983 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006984}
Evan Cheng72261582005-12-20 06:22:03 +00006985
Dan Gohmand858e902010-04-17 15:26:15 +00006986SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
6987 SelectionDAG &DAG) const {
Evan Cheng2457f2c2010-05-22 01:47:14 +00006988 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6989 MFI->setReturnAddressIsTaken(true);
6990
Bill Wendling64e87322009-01-16 19:25:27 +00006991 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006992 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006993
6994 if (Depth > 0) {
6995 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6996 SDValue Offset =
6997 DAG.getConstant(TD->getPointerSize(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006999 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007000 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007001 FrameAddr, Offset),
David Greene67c9d422010-02-15 16:53:33 +00007002 NULL, 0, false, false, 0);
Bill Wendling64e87322009-01-16 19:25:27 +00007003 }
7004
7005 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00007006 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00007007 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
David Greene67c9d422010-02-15 16:53:33 +00007008 RetAddrFI, NULL, 0, false, false, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007009}
7010
Dan Gohmand858e902010-04-17 15:26:15 +00007011SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng184793f2008-09-27 01:56:22 +00007012 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
7013 MFI->setFrameAddressIsTaken(true);
Evan Cheng2457f2c2010-05-22 01:47:14 +00007014
Owen Andersone50ed302009-08-10 22:56:29 +00007015 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007016 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00007017 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
7018 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00007019 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00007020 while (Depth--)
David Greene67c9d422010-02-15 16:53:33 +00007021 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0,
7022 false, false, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00007023 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00007024}
7025
Dan Gohman475871a2008-07-27 21:46:04 +00007026SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007027 SelectionDAG &DAG) const {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007028 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007029}
7030
Dan Gohmand858e902010-04-17 15:26:15 +00007031SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007032 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00007033 SDValue Chain = Op.getOperand(0);
7034 SDValue Offset = Op.getOperand(1);
7035 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007036 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007037
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007038 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
7039 getPointerTy());
7040 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007041
Dale Johannesene4d209d2009-02-03 20:21:25 +00007042 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007043 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007044 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
David Greene67c9d422010-02-15 16:53:33 +00007045 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0, false, false, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00007046 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007047 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007048
Dale Johannesene4d209d2009-02-03 20:21:25 +00007049 return DAG.getNode(X86ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007050 MVT::Other,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00007051 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007052}
7053
Dan Gohman475871a2008-07-27 21:46:04 +00007054SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007055 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007056 SDValue Root = Op.getOperand(0);
7057 SDValue Trmp = Op.getOperand(1); // trampoline
7058 SDValue FPtr = Op.getOperand(2); // nested function
7059 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007060 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007061
Dan Gohman69de1932008-02-06 22:27:42 +00007062 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007063
7064 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007065 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00007066
7067 // Large code-model.
Chris Lattnera62fe662010-02-05 19:20:30 +00007068 const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode.
7069 const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
Duncan Sands339e14f2008-01-16 22:55:25 +00007070
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007071 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
7072 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00007073
7074 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
7075
7076 // Load the pointer to the nested function into R11.
7077 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00007078 SDValue Addr = Trmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007079 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007080 Addr, TrmpAddr, 0, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007081
Owen Anderson825b72b2009-08-11 20:47:22 +00007082 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7083 DAG.getConstant(2, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007084 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2,
7085 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007086
7087 // Load the 'nest' parameter value into R10.
7088 // R10 is specified in X86CallingConv.td
7089 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Owen Anderson825b72b2009-08-11 20:47:22 +00007090 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7091 DAG.getConstant(10, MVT::i64));
7092 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007093 Addr, TrmpAddr, 10, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007094
Owen Anderson825b72b2009-08-11 20:47:22 +00007095 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7096 DAG.getConstant(12, MVT::i64));
David Greene67c9d422010-02-15 16:53:33 +00007097 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12,
7098 false, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00007099
7100 // Jump to the nested function.
7101 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Owen Anderson825b72b2009-08-11 20:47:22 +00007102 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7103 DAG.getConstant(20, MVT::i64));
7104 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
David Greene67c9d422010-02-15 16:53:33 +00007105 Addr, TrmpAddr, 20, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007106
7107 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Owen Anderson825b72b2009-08-11 20:47:22 +00007108 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
7109 DAG.getConstant(22, MVT::i64));
7110 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007111 TrmpAddr, 22, false, false, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00007112
Dan Gohman475871a2008-07-27 21:46:04 +00007113 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007114 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007115 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007116 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00007117 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00007118 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00007119 CallingConv::ID CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00007120 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007121
7122 switch (CC) {
7123 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00007124 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007125 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007126 case CallingConv::X86_StdCall: {
7127 // Pass 'nest' parameter in ECX.
7128 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007129 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007130
7131 // Check that ECX wasn't needed by an 'inreg' parameter.
7132 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00007133 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00007134
Chris Lattner58d74912008-03-12 17:45:29 +00007135 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00007136 unsigned InRegCount = 0;
7137 unsigned Idx = 1;
7138
7139 for (FunctionType::param_iterator I = FTy->param_begin(),
7140 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00007141 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00007142 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00007143 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007144
7145 if (InRegCount > 2) {
Chris Lattner75361b62010-04-07 22:58:41 +00007146 report_fatal_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00007147 }
7148 }
7149 break;
7150 }
7151 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00007152 case CallingConv::X86_ThisCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00007153 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00007154 // Pass 'nest' parameter in EAX.
7155 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00007156 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007157 break;
7158 }
7159
Dan Gohman475871a2008-07-27 21:46:04 +00007160 SDValue OutChains[4];
7161 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00007162
Owen Anderson825b72b2009-08-11 20:47:22 +00007163 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7164 DAG.getConstant(10, MVT::i32));
7165 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007166
Chris Lattnera62fe662010-02-05 19:20:30 +00007167 // This is storing the opcode for MOV32ri.
7168 const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00007169 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007170 OutChains[0] = DAG.getStore(Root, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00007171 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
David Greene67c9d422010-02-15 16:53:33 +00007172 Trmp, TrmpAddr, 0, false, false, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007173
Owen Anderson825b72b2009-08-11 20:47:22 +00007174 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7175 DAG.getConstant(1, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007176 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1,
7177 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007178
Chris Lattnera62fe662010-02-05 19:20:30 +00007179 const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
Owen Anderson825b72b2009-08-11 20:47:22 +00007180 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7181 DAG.getConstant(5, MVT::i32));
7182 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
David Greene67c9d422010-02-15 16:53:33 +00007183 TrmpAddr, 5, false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007184
Owen Anderson825b72b2009-08-11 20:47:22 +00007185 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
7186 DAG.getConstant(6, MVT::i32));
David Greene67c9d422010-02-15 16:53:33 +00007187 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6,
7188 false, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007189
Dan Gohman475871a2008-07-27 21:46:04 +00007190 SDValue Ops[] =
Owen Anderson825b72b2009-08-11 20:47:22 +00007191 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007192 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007193 }
7194}
7195
Dan Gohmand858e902010-04-17 15:26:15 +00007196SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
7197 SelectionDAG &DAG) const {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007198 /*
7199 The rounding mode is in bits 11:10 of FPSR, and has the following
7200 settings:
7201 00 Round to nearest
7202 01 Round to -inf
7203 10 Round to +inf
7204 11 Round to 0
7205
7206 FLT_ROUNDS, on the other hand, expects the following:
7207 -1 Undefined
7208 0 Round to 0
7209 1 Round to nearest
7210 2 Round to +inf
7211 3 Round to -inf
7212
7213 To perform the conversion, we do:
7214 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
7215 */
7216
7217 MachineFunction &MF = DAG.getMachineFunction();
7218 const TargetMachine &TM = MF.getTarget();
7219 const TargetFrameInfo &TFI = *TM.getFrameInfo();
7220 unsigned StackAlignment = TFI.getStackAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007221 EVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007222 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007223
7224 // Save FP Control Word to stack slot
David Greene3f2bf852009-11-12 20:49:22 +00007225 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
Dan Gohman475871a2008-07-27 21:46:04 +00007226 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007227
Owen Anderson825b72b2009-08-11 20:47:22 +00007228 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007229 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007230
7231 // Load FP Control Word from stack slot
David Greene67c9d422010-02-15 16:53:33 +00007232 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0,
7233 false, false, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007234
7235 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00007236 SDValue CWD1 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007237 DAG.getNode(ISD::SRL, dl, MVT::i16,
7238 DAG.getNode(ISD::AND, dl, MVT::i16,
7239 CWD, DAG.getConstant(0x800, MVT::i16)),
7240 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00007241 SDValue CWD2 =
Owen Anderson825b72b2009-08-11 20:47:22 +00007242 DAG.getNode(ISD::SRL, dl, MVT::i16,
7243 DAG.getNode(ISD::AND, dl, MVT::i16,
7244 CWD, DAG.getConstant(0x400, MVT::i16)),
7245 DAG.getConstant(9, MVT::i8));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007246
Dan Gohman475871a2008-07-27 21:46:04 +00007247 SDValue RetVal =
Owen Anderson825b72b2009-08-11 20:47:22 +00007248 DAG.getNode(ISD::AND, dl, MVT::i16,
7249 DAG.getNode(ISD::ADD, dl, MVT::i16,
7250 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
7251 DAG.getConstant(1, MVT::i16)),
7252 DAG.getConstant(3, MVT::i16));
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007253
7254
Duncan Sands83ec4b62008-06-06 12:08:01 +00007255 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00007256 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007257}
7258
Dan Gohmand858e902010-04-17 15:26:15 +00007259SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007260 EVT VT = Op.getValueType();
7261 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007262 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007263 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007264
7265 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007266 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00007267 // Zero extend to i32 since there is not an i8 bsr.
Owen Anderson825b72b2009-08-11 20:47:22 +00007268 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007269 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007270 }
Evan Cheng18efe262007-12-14 02:13:44 +00007271
Evan Cheng152804e2007-12-14 08:30:15 +00007272 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007273 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007274 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007275
7276 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007277 SDValue Ops[] = {
7278 Op,
7279 DAG.getConstant(NumBits+NumBits-1, OpVT),
7280 DAG.getConstant(X86::COND_E, MVT::i8),
7281 Op.getValue(1)
7282 };
7283 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007284
7285 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007286 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00007287
Owen Anderson825b72b2009-08-11 20:47:22 +00007288 if (VT == MVT::i8)
7289 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007290 return Op;
7291}
7292
Dan Gohmand858e902010-04-17 15:26:15 +00007293SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007294 EVT VT = Op.getValueType();
7295 EVT OpVT = VT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007296 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007297 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00007298
7299 Op = Op.getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007300 if (VT == MVT::i8) {
7301 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007302 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007303 }
Evan Cheng152804e2007-12-14 08:30:15 +00007304
7305 // Issue a bsf (scan bits forward) which also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007306 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007307 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00007308
7309 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Benjamin Kramer7f1a5602009-12-29 16:57:26 +00007310 SDValue Ops[] = {
7311 Op,
7312 DAG.getConstant(NumBits, OpVT),
7313 DAG.getConstant(X86::COND_E, MVT::i8),
7314 Op.getValue(1)
7315 };
7316 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops));
Evan Cheng152804e2007-12-14 08:30:15 +00007317
Owen Anderson825b72b2009-08-11 20:47:22 +00007318 if (VT == MVT::i8)
7319 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00007320 return Op;
7321}
7322
Dan Gohmand858e902010-04-17 15:26:15 +00007323SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007324 EVT VT = Op.getValueType();
Owen Anderson825b72b2009-08-11 20:47:22 +00007325 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007326 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007327
Mon P Wangaf9b9522008-12-18 21:42:19 +00007328 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
7329 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
7330 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
7331 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
7332 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
7333 //
7334 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
7335 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
7336 // return AloBlo + AloBhi + AhiBlo;
7337
7338 SDValue A = Op.getOperand(0);
7339 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007340
Dale Johannesene4d209d2009-02-03 20:21:25 +00007341 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007342 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7343 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007344 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007345 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
7346 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007347 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007348 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007349 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007350 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007351 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007352 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007353 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007354 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
Mon P Wangaf9b9522008-12-18 21:42:19 +00007355 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007356 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007357 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7358 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007359 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00007360 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
7361 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007362 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
7363 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007364 return Res;
7365}
7366
7367
Dan Gohmand858e902010-04-17 15:26:15 +00007368SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) const {
Bill Wendling74c37652008-12-09 22:08:41 +00007369 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
7370 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00007371 // looks for this combo and may remove the "setcc" instruction if the "setcc"
7372 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00007373 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00007374 SDValue LHS = N->getOperand(0);
7375 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00007376 unsigned BaseOp = 0;
7377 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007378 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00007379
7380 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007381 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00007382 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00007383 // A subtract of one will be selected as a INC. Note that INC doesn't
7384 // set CF, so we can't do this for UADDO.
7385 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7386 if (C->getAPIntValue() == 1) {
7387 BaseOp = X86ISD::INC;
7388 Cond = X86::COND_O;
7389 break;
7390 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007391 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00007392 Cond = X86::COND_O;
7393 break;
7394 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007395 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00007396 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007397 break;
7398 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00007399 // A subtract of one will be selected as a DEC. Note that DEC doesn't
7400 // set CF, so we can't do this for USUBO.
7401 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
7402 if (C->getAPIntValue() == 1) {
7403 BaseOp = X86ISD::DEC;
7404 Cond = X86::COND_O;
7405 break;
7406 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007407 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00007408 Cond = X86::COND_O;
7409 break;
7410 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007411 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00007412 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007413 break;
7414 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007415 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00007416 Cond = X86::COND_O;
7417 break;
7418 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00007419 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00007420 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00007421 break;
7422 }
Bill Wendling3fafd932008-11-26 22:37:40 +00007423
Bill Wendling61edeb52008-12-02 01:06:39 +00007424 // Also sets EFLAGS.
Owen Anderson825b72b2009-08-11 20:47:22 +00007425 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007426 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00007427
Bill Wendling61edeb52008-12-02 01:06:39 +00007428 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00007429 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Owen Anderson825b72b2009-08-11 20:47:22 +00007430 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00007431
Bill Wendling61edeb52008-12-02 01:06:39 +00007432 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
7433 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00007434}
7435
Dan Gohmand858e902010-04-17 15:26:15 +00007436SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007437 EVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007438 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00007439 unsigned Reg = 0;
7440 unsigned size = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00007441 switch(T.getSimpleVT().SimpleTy) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007442 default:
7443 assert(false && "Invalid value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +00007444 case MVT::i8: Reg = X86::AL; size = 1; break;
7445 case MVT::i16: Reg = X86::AX; size = 2; break;
7446 case MVT::i32: Reg = X86::EAX; size = 4; break;
7447 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00007448 assert(Subtarget->is64Bit() && "Node not type legal!");
7449 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00007450 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00007451 }
Dale Johannesendd64c412009-02-04 00:33:20 +00007452 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00007453 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00007454 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007455 Op.getOperand(1),
7456 Op.getOperand(3),
Owen Anderson825b72b2009-08-11 20:47:22 +00007457 DAG.getTargetConstant(size, MVT::i8),
Evan Cheng8a186ae2008-09-24 23:26:36 +00007458 cpIn.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007459 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007460 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00007461 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00007462 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00007463 return cpOut;
7464}
7465
Duncan Sands1607f052008-12-01 11:39:25 +00007466SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +00007467 SelectionDAG &DAG) const {
Duncan Sands1607f052008-12-01 11:39:25 +00007468 assert(Subtarget->is64Bit() && "Result not type legalized?");
Owen Anderson825b72b2009-08-11 20:47:22 +00007469 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007470 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00007471 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007472 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007473 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
7474 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00007475 rax.getValue(2));
Owen Anderson825b72b2009-08-11 20:47:22 +00007476 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
7477 DAG.getConstant(32, MVT::i8));
Duncan Sands1607f052008-12-01 11:39:25 +00007478 SDValue Ops[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +00007479 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00007480 rdx.getValue(1)
7481 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007482 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007483}
7484
Dale Johannesen7d07b482010-05-21 00:52:33 +00007485SDValue X86TargetLowering::LowerBIT_CONVERT(SDValue Op,
7486 SelectionDAG &DAG) const {
7487 EVT SrcVT = Op.getOperand(0).getValueType();
7488 EVT DstVT = Op.getValueType();
7489 assert((Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
7490 Subtarget->hasMMX() && !DisableMMX) &&
7491 "Unexpected custom BIT_CONVERT");
7492 assert((DstVT == MVT::i64 ||
7493 (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
7494 "Unexpected custom BIT_CONVERT");
7495 // i64 <=> MMX conversions are Legal.
7496 if (SrcVT==MVT::i64 && DstVT.isVector())
7497 return Op;
7498 if (DstVT==MVT::i64 && SrcVT.isVector())
7499 return Op;
Dale Johannesene39859a2010-05-21 18:40:15 +00007500 // MMX <=> MMX conversions are Legal.
7501 if (SrcVT.isVector() && DstVT.isVector())
7502 return Op;
Dale Johannesen7d07b482010-05-21 00:52:33 +00007503 // All other conversions need to be expanded.
7504 return SDValue();
7505}
Dan Gohmand858e902010-04-17 15:26:15 +00007506SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) const {
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007507 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007508 DebugLoc dl = Node->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007509 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007510 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00007511 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00007512 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007513 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00007514 Node->getOperand(0),
7515 Node->getOperand(1), negOp,
7516 cast<AtomicSDNode>(Node)->getSrcValue(),
7517 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00007518}
7519
Evan Cheng0db9fe62006-04-25 20:13:52 +00007520/// LowerOperation - Provide custom lowering hooks for some operations.
7521///
Dan Gohmand858e902010-04-17 15:26:15 +00007522SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Evan Cheng0db9fe62006-04-25 20:13:52 +00007523 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007524 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007525 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
7526 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007527 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
Mon P Wangeb38ebf2010-01-24 00:05:03 +00007528 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007529 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
7530 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
7531 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
7532 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
7533 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
7534 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007535 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00007536 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Dan Gohmanf705adb2009-10-30 01:28:02 +00007537 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007538 case ISD::SHL_PARTS:
7539 case ISD::SRA_PARTS:
7540 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
7541 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00007542 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007543 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00007544 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007545 case ISD::FABS: return LowerFABS(Op, DAG);
7546 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00007547 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007548 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00007549 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00007550 case ISD::SELECT: return LowerSELECT(Op, DAG);
7551 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007552 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007553 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00007554 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00007555 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007556 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00007557 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
7558 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007559 case ISD::FRAME_TO_ARGS_OFFSET:
7560 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00007561 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007562 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00007563 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00007564 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00007565 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
7566 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00007567 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00007568 case ISD::SADDO:
7569 case ISD::UADDO:
7570 case ISD::SSUBO:
7571 case ISD::USUBO:
7572 case ISD::SMULO:
7573 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00007574 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Dale Johannesen7d07b482010-05-21 00:52:33 +00007575 case ISD::BIT_CONVERT: return LowerBIT_CONVERT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00007576 }
Chris Lattner27a6c732007-11-24 07:07:01 +00007577}
7578
Duncan Sands1607f052008-12-01 11:39:25 +00007579void X86TargetLowering::
7580ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007581 SelectionDAG &DAG, unsigned NewOp) const {
Owen Andersone50ed302009-08-10 22:56:29 +00007582 EVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007583 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +00007584 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
Duncan Sands1607f052008-12-01 11:39:25 +00007585
7586 SDValue Chain = Node->getOperand(0);
7587 SDValue In1 = Node->getOperand(1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007588 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007589 Node->getOperand(2), DAG.getIntPtrConstant(0));
Owen Anderson825b72b2009-08-11 20:47:22 +00007590 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007591 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dan Gohmanc76909a2009-09-25 20:36:54 +00007592 SDValue Ops[] = { Chain, In1, In2L, In2H };
Owen Anderson825b72b2009-08-11 20:47:22 +00007593 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dan Gohmanc76909a2009-09-25 20:36:54 +00007594 SDValue Result =
7595 DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, 4, MVT::i64,
7596 cast<MemSDNode>(Node)->getMemOperand());
Duncan Sands1607f052008-12-01 11:39:25 +00007597 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007598 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007599 Results.push_back(Result.getValue(2));
7600}
7601
Duncan Sands126d9072008-07-04 11:47:58 +00007602/// ReplaceNodeResults - Replace a node with an illegal result type
7603/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00007604void X86TargetLowering::ReplaceNodeResults(SDNode *N,
7605 SmallVectorImpl<SDValue>&Results,
Dan Gohmand858e902010-04-17 15:26:15 +00007606 SelectionDAG &DAG) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007607 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00007608 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00007609 default:
Duncan Sands1607f052008-12-01 11:39:25 +00007610 assert(false && "Do not know how to custom type legalize this operation!");
7611 return;
7612 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00007613 std::pair<SDValue,SDValue> Vals =
7614 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00007615 SDValue FIST = Vals.first, StackSlot = Vals.second;
7616 if (FIST.getNode() != 0) {
Owen Andersone50ed302009-08-10 22:56:29 +00007617 EVT VT = N->getValueType(0);
Duncan Sands1607f052008-12-01 11:39:25 +00007618 // Return a load from the stack slot.
David Greene67c9d422010-02-15 16:53:33 +00007619 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0,
7620 false, false, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00007621 }
7622 return;
7623 }
7624 case ISD::READCYCLECOUNTER: {
Owen Anderson825b72b2009-08-11 20:47:22 +00007625 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00007626 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007627 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Owen Anderson825b72b2009-08-11 20:47:22 +00007628 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00007629 rd.getValue(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00007630 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00007631 eax.getValue(2));
7632 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
7633 SDValue Ops[] = { eax, edx };
Owen Anderson825b72b2009-08-11 20:47:22 +00007634 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007635 Results.push_back(edx.getValue(1));
7636 return;
7637 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007638 case ISD::ATOMIC_CMP_SWAP: {
Owen Andersone50ed302009-08-10 22:56:29 +00007639 EVT T = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00007640 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Duncan Sands1607f052008-12-01 11:39:25 +00007641 SDValue cpInL, cpInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007642 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7643 DAG.getConstant(0, MVT::i32));
7644 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
7645 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007646 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
7647 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007648 cpInL.getValue(1));
7649 SDValue swapInL, swapInH;
Owen Anderson825b72b2009-08-11 20:47:22 +00007650 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7651 DAG.getConstant(0, MVT::i32));
7652 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
7653 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00007654 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00007655 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007656 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00007657 swapInL.getValue(1));
7658 SDValue Ops[] = { swapInH.getValue(0),
7659 N->getOperand(1),
7660 swapInH.getValue(1) };
Owen Anderson825b72b2009-08-11 20:47:22 +00007661 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007662 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00007663 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007664 MVT::i32, Result.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00007665 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
Owen Anderson825b72b2009-08-11 20:47:22 +00007666 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00007667 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Owen Anderson825b72b2009-08-11 20:47:22 +00007668 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00007669 Results.push_back(cpOutH.getValue(1));
7670 return;
7671 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007672 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00007673 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
7674 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007675 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00007676 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
7677 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007678 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00007679 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
7680 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007681 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00007682 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
7683 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007684 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00007685 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
7686 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007687 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00007688 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
7689 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00007690 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00007691 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
7692 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00007693 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00007694}
7695
Evan Cheng72261582005-12-20 06:22:03 +00007696const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
7697 switch (Opcode) {
7698 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00007699 case X86ISD::BSF: return "X86ISD::BSF";
7700 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00007701 case X86ISD::SHLD: return "X86ISD::SHLD";
7702 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00007703 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007704 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00007705 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00007706 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00007707 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00007708 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00007709 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
7710 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
7711 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00007712 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00007713 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00007714 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00007715 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00007716 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00007717 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00007718 case X86ISD::COMI: return "X86ISD::COMI";
7719 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00007720 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Chengad9c0a32009-12-15 00:53:42 +00007721 case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY";
Evan Cheng72261582005-12-20 06:22:03 +00007722 case X86ISD::CMOV: return "X86ISD::CMOV";
7723 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00007724 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00007725 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
7726 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00007727 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00007728 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00007729 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007730 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007731 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007732 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7733 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007734 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Chris Lattner8f2b4cc2010-02-23 02:07:48 +00007735 case X86ISD::MMX_PINSRW: return "X86ISD::MMX_PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007736 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007737 case X86ISD::FMAX: return "X86ISD::FMAX";
7738 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007739 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7740 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007741 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007742 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007743 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007744 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007745 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007746 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7747 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007748 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7749 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7750 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7751 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7752 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7753 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007754 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7755 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007756 case X86ISD::VSHL: return "X86ISD::VSHL";
7757 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007758 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7759 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7760 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7761 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7762 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7763 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7764 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7765 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7766 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7767 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007768 case X86ISD::ADD: return "X86ISD::ADD";
7769 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007770 case X86ISD::SMUL: return "X86ISD::SMUL";
7771 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007772 case X86ISD::INC: return "X86ISD::INC";
7773 case X86ISD::DEC: return "X86ISD::DEC";
Dan Gohmane220c4b2009-09-18 19:59:53 +00007774 case X86ISD::OR: return "X86ISD::OR";
7775 case X86ISD::XOR: return "X86ISD::XOR";
7776 case X86ISD::AND: return "X86ISD::AND";
Evan Cheng73f24c92009-03-30 21:36:47 +00007777 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007778 case X86ISD::PTEST: return "X86ISD::PTEST";
Dan Gohmand6708ea2009-08-15 01:38:56 +00007779 case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00007780 case X86ISD::MINGW_ALLOCA: return "X86ISD::MINGW_ALLOCA";
Evan Cheng72261582005-12-20 06:22:03 +00007781 }
7782}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007783
Chris Lattnerc9addb72007-03-30 23:15:24 +00007784// isLegalAddressingMode - Return true if the addressing mode represented
7785// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007786bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007787 const Type *Ty) const {
7788 // X86 supports extremely general addressing modes.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007789 CodeModel::Model M = getTargetMachine().getCodeModel();
Scott Michelfdc40a02009-02-17 22:15:04 +00007790
Chris Lattnerc9addb72007-03-30 23:15:24 +00007791 // X86 allows a sign-extended 32-bit immediate field as a displacement.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007792 if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007793 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007794
Chris Lattnerc9addb72007-03-30 23:15:24 +00007795 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007796 unsigned GVFlags =
7797 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007798
Chris Lattnerdfed4132009-07-10 07:38:24 +00007799 // If a reference to this global requires an extra load, we can't fold it.
7800 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007801 return false;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007802
Chris Lattnerdfed4132009-07-10 07:38:24 +00007803 // If BaseGV requires a register for the PIC base, we cannot also have a
7804 // BaseReg specified.
7805 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007806 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007807
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00007808 // If lower 4G is not available, then we must use rip-relative addressing.
7809 if (Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
7810 return false;
Chris Lattnerc9addb72007-03-30 23:15:24 +00007811 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007812
Chris Lattnerc9addb72007-03-30 23:15:24 +00007813 switch (AM.Scale) {
7814 case 0:
7815 case 1:
7816 case 2:
7817 case 4:
7818 case 8:
7819 // These scales always work.
7820 break;
7821 case 3:
7822 case 5:
7823 case 9:
7824 // These scales are formed with basereg+scalereg. Only accept if there is
7825 // no basereg yet.
7826 if (AM.HasBaseReg)
7827 return false;
7828 break;
7829 default: // Other stuff never works.
7830 return false;
7831 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007832
Chris Lattnerc9addb72007-03-30 23:15:24 +00007833 return true;
7834}
7835
7836
Evan Cheng2bd122c2007-10-26 01:56:11 +00007837bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007838 if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
Evan Cheng2bd122c2007-10-26 01:56:11 +00007839 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007840 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7841 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007842 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007843 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007844 return true;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007845}
7846
Owen Andersone50ed302009-08-10 22:56:29 +00007847bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007848 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007849 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007850 unsigned NumBits1 = VT1.getSizeInBits();
7851 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007852 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007853 return false;
Dan Gohman377fbc02010-02-25 03:04:36 +00007854 return true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007855}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007856
Dan Gohman97121ba2009-04-08 00:15:30 +00007857bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007858 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00007859 return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007860}
7861
Owen Andersone50ed302009-08-10 22:56:29 +00007862bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007863 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Owen Anderson825b72b2009-08-11 20:47:22 +00007864 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
Dan Gohman97121ba2009-04-08 00:15:30 +00007865}
7866
Owen Andersone50ed302009-08-10 22:56:29 +00007867bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
Evan Cheng8b944d32009-05-28 00:35:15 +00007868 // i16 instructions are longer (0x66 prefix) and potentially slower.
Owen Anderson825b72b2009-08-11 20:47:22 +00007869 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
Evan Cheng8b944d32009-05-28 00:35:15 +00007870}
7871
Evan Cheng60c07e12006-07-05 22:17:51 +00007872/// isShuffleMaskLegal - Targets can use this to indicate that they only
7873/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7874/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7875/// are assumed to be legal.
7876bool
Eric Christopherfd179292009-08-27 18:07:15 +00007877X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
Owen Andersone50ed302009-08-10 22:56:29 +00007878 EVT VT) const {
Eric Christophercff6f852010-04-15 01:40:20 +00007879 // Very little shuffling can be done for 64-bit vectors right now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007880 if (VT.getSizeInBits() == 64)
Eric Christophercff6f852010-04-15 01:40:20 +00007881 return isPALIGNRMask(M, VT, Subtarget->hasSSSE3());
Nate Begeman9008ca62009-04-27 18:41:29 +00007882
Nate Begemana09008b2009-10-19 02:17:23 +00007883 // FIXME: pshufb, blends, shifts.
Nate Begeman9008ca62009-04-27 18:41:29 +00007884 return (VT.getVectorNumElements() == 2 ||
7885 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7886 isMOVLMask(M, VT) ||
7887 isSHUFPMask(M, VT) ||
7888 isPSHUFDMask(M, VT) ||
7889 isPSHUFHWMask(M, VT) ||
7890 isPSHUFLWMask(M, VT) ||
Nate Begemana09008b2009-10-19 02:17:23 +00007891 isPALIGNRMask(M, VT, Subtarget->hasSSSE3()) ||
Nate Begeman9008ca62009-04-27 18:41:29 +00007892 isUNPCKLMask(M, VT) ||
7893 isUNPCKHMask(M, VT) ||
7894 isUNPCKL_v_undef_Mask(M, VT) ||
7895 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007896}
7897
Dan Gohman7d8143f2008-04-09 20:09:42 +00007898bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007899X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Owen Andersone50ed302009-08-10 22:56:29 +00007900 EVT VT) const {
Nate Begeman9008ca62009-04-27 18:41:29 +00007901 unsigned NumElts = VT.getVectorNumElements();
7902 // FIXME: This collection of masks seems suspect.
7903 if (NumElts == 2)
7904 return true;
7905 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7906 return (isMOVLMask(Mask, VT) ||
7907 isCommutedMOVLMask(Mask, VT, true) ||
7908 isSHUFPMask(Mask, VT) ||
7909 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007910 }
7911 return false;
7912}
7913
7914//===----------------------------------------------------------------------===//
7915// X86 Scheduler Hooks
7916//===----------------------------------------------------------------------===//
7917
Mon P Wang63307c32008-05-05 19:05:59 +00007918// private utility function
7919MachineBasicBlock *
7920X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7921 MachineBasicBlock *MBB,
7922 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007923 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007924 unsigned LoadOpc,
7925 unsigned CXchgOpc,
7926 unsigned copyOpc,
7927 unsigned notOpc,
7928 unsigned EAXreg,
7929 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007930 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007931 // For the atomic bitwise operator, we generate
7932 // thisMBB:
7933 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007934 // ld t1 = [bitinstr.addr]
7935 // op t2 = t1, [bitinstr.val]
7936 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007937 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7938 // bz newMBB
7939 // fallthrough -->nextMBB
7940 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7941 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007942 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007943 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007944
Mon P Wang63307c32008-05-05 19:05:59 +00007945 /// First build the CFG
7946 MachineFunction *F = MBB->getParent();
7947 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007948 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7949 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7950 F->insert(MBBIter, newMBB);
7951 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007952
Mon P Wang63307c32008-05-05 19:05:59 +00007953 // Move all successors to thisMBB to nextMBB
7954 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007955
Mon P Wang63307c32008-05-05 19:05:59 +00007956 // Update thisMBB to fall through to newMBB
7957 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007958
Mon P Wang63307c32008-05-05 19:05:59 +00007959 // newMBB jumps to itself and fall through to nextMBB
7960 newMBB->addSuccessor(nextMBB);
7961 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007962
Mon P Wang63307c32008-05-05 19:05:59 +00007963 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007964 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007965 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007966 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007967 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007968 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007969 int numArgs = bInstr->getNumOperands() - 1;
7970 for (int i=0; i < numArgs; ++i)
7971 argOpers[i] = &bInstr->getOperand(i+1);
7972
7973 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007974 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7975 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007976
Dale Johannesen140be2d2008-08-19 18:47:28 +00007977 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007978 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007979 for (int i=0; i <= lastAddrIndx; ++i)
7980 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007981
Dale Johannesen140be2d2008-08-19 18:47:28 +00007982 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007983 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007984 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007985 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007986 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007987 tt = t1;
7988
Dale Johannesen140be2d2008-08-19 18:47:28 +00007989 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007990 assert((argOpers[valArgIndx]->isReg() ||
7991 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007992 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007993 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007994 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007995 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007996 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007997 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007998 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007999
Dale Johannesene4d209d2009-02-03 20:21:25 +00008000 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00008001 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008002
Dale Johannesene4d209d2009-02-03 20:21:25 +00008003 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00008004 for (int i=0; i <= lastAddrIndx; ++i)
8005 (*MIB).addOperand(*argOpers[i]);
8006 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00008007 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008008 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8009 bInstr->memoperands_end());
Mon P Wangf5952662008-07-17 04:54:06 +00008010
Dale Johannesene4d209d2009-02-03 20:21:25 +00008011 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00008012 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00008013
Mon P Wang63307c32008-05-05 19:05:59 +00008014 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008015 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008016
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008017 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008018 return nextMBB;
8019}
8020
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00008021// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00008022MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008023X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
8024 MachineBasicBlock *MBB,
8025 unsigned regOpcL,
8026 unsigned regOpcH,
8027 unsigned immOpcL,
8028 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008029 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008030 // For the atomic bitwise operator, we generate
8031 // thisMBB (instructions are in pairs, except cmpxchg8b)
8032 // ld t1,t2 = [bitinstr.addr]
8033 // newMBB:
8034 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
8035 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00008036 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008037 // mov ECX, EBX <- t5, t6
8038 // mov EAX, EDX <- t1, t2
8039 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
8040 // mov t3, t4 <- EAX, EDX
8041 // bz newMBB
8042 // result in out1, out2
8043 // fallthrough -->nextMBB
8044
8045 const TargetRegisterClass *RC = X86::GR32RegisterClass;
8046 const unsigned LoadOpc = X86::MOV32rm;
8047 const unsigned copyOpc = X86::MOV32rr;
8048 const unsigned NotOpc = X86::NOT32r;
8049 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8050 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8051 MachineFunction::iterator MBBIter = MBB;
8052 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008053
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008054 /// First build the CFG
8055 MachineFunction *F = MBB->getParent();
8056 MachineBasicBlock *thisMBB = MBB;
8057 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8058 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8059 F->insert(MBBIter, newMBB);
8060 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008061
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008062 // Move all successors to thisMBB to nextMBB
8063 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008064
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008065 // Update thisMBB to fall through to newMBB
8066 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008067
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008068 // newMBB jumps to itself and fall through to nextMBB
8069 newMBB->addSuccessor(nextMBB);
8070 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008071
Dale Johannesene4d209d2009-02-03 20:21:25 +00008072 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008073 // Insert instructions into newMBB based on incoming instruction
8074 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008075 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008076 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008077 MachineOperand& dest1Oper = bInstr->getOperand(0);
8078 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008079 MachineOperand* argOpers[2 + X86AddrNumOperands];
Dan Gohman71ea4e52010-05-14 21:01:44 +00008080 for (int i=0; i < 2 + X86AddrNumOperands; ++i) {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008081 argOpers[i] = &bInstr->getOperand(i+2);
8082
Dan Gohman71ea4e52010-05-14 21:01:44 +00008083 // We use some of the operands multiple times, so conservatively just
8084 // clear any kill flags that might be present.
8085 if (argOpers[i]->isReg() && argOpers[i]->isUse())
8086 argOpers[i]->setIsKill(false);
8087 }
8088
Evan Chengad5b52f2010-01-08 19:14:57 +00008089 // x86 address has 5 operands: base, index, scale, displacement, and segment.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008090 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00008091
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008092 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008093 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008094 for (int i=0; i <= lastAddrIndx; ++i)
8095 (*MIB).addOperand(*argOpers[i]);
8096 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008097 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00008098 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00008099 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008100 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00008101 MachineOperand newOp3 = *(argOpers[3]);
8102 if (newOp3.isImm())
8103 newOp3.setImm(newOp3.getImm()+4);
8104 else
8105 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008106 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00008107 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008108
8109 // t3/4 are defined later, at the bottom of the loop
8110 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
8111 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008112 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008113 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008114 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008115 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
8116
Evan Cheng306b4ca2010-01-08 23:41:50 +00008117 // The subsequent operations should be using the destination registers of
8118 //the PHI instructions.
Scott Michelfdc40a02009-02-17 22:15:04 +00008119 if (invSrc) {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008120 t1 = F->getRegInfo().createVirtualRegister(RC);
8121 t2 = F->getRegInfo().createVirtualRegister(RC);
8122 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t1).addReg(dest1Oper.getReg());
8123 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), t2).addReg(dest2Oper.getReg());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008124 } else {
Evan Cheng306b4ca2010-01-08 23:41:50 +00008125 t1 = dest1Oper.getReg();
8126 t2 = dest2Oper.getReg();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008127 }
8128
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008129 int valArgIndx = lastAddrIndx + 1;
8130 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00008131 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008132 "invalid operand");
8133 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
8134 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008135 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008136 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008137 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008138 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00008139 if (regOpcL != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008140 MIB.addReg(t1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008141 (*MIB).addOperand(*argOpers[valArgIndx]);
8142 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008143 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008144 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00008145 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008146 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008147 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008148 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008149 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00008150 if (regOpcH != X86::MOV32rr)
Evan Cheng306b4ca2010-01-08 23:41:50 +00008151 MIB.addReg(t2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008152 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008153
Dale Johannesene4d209d2009-02-03 20:21:25 +00008154 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008155 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008156 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008157 MIB.addReg(t2);
8158
Dale Johannesene4d209d2009-02-03 20:21:25 +00008159 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008160 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008161 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008162 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00008163
Dale Johannesene4d209d2009-02-03 20:21:25 +00008164 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008165 for (int i=0; i <= lastAddrIndx; ++i)
8166 (*MIB).addOperand(*argOpers[i]);
8167
8168 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008169 (*MIB).setMemRefs(bInstr->memoperands_begin(),
8170 bInstr->memoperands_end());
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008171
Dale Johannesene4d209d2009-02-03 20:21:25 +00008172 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008173 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008174 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008175 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008176
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008177 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008178 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008179
8180 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
8181 return nextMBB;
8182}
8183
8184// private utility function
8185MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00008186X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
8187 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00008188 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00008189 // For the atomic min/max operator, we generate
8190 // thisMBB:
8191 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00008192 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00008193 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00008194 // cmp t1, t2
8195 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00008196 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00008197 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
8198 // bz newMBB
8199 // fallthrough -->nextMBB
8200 //
8201 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8202 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008203 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00008204 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00008205
Mon P Wang63307c32008-05-05 19:05:59 +00008206 /// First build the CFG
8207 MachineFunction *F = MBB->getParent();
8208 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008209 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
8210 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
8211 F->insert(MBBIter, newMBB);
8212 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008213
Dan Gohmand6708ea2009-08-15 01:38:56 +00008214 // Move all successors of thisMBB to nextMBB
Mon P Wang63307c32008-05-05 19:05:59 +00008215 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008216
Mon P Wang63307c32008-05-05 19:05:59 +00008217 // Update thisMBB to fall through to newMBB
8218 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008219
Mon P Wang63307c32008-05-05 19:05:59 +00008220 // newMBB jumps to newMBB and fall through to nextMBB
8221 newMBB->addSuccessor(nextMBB);
8222 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00008223
Dale Johannesene4d209d2009-02-03 20:21:25 +00008224 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00008225 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008226 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00008227 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00008228 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008229 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00008230 int numArgs = mInstr->getNumOperands() - 1;
8231 for (int i=0; i < numArgs; ++i)
8232 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008233
Mon P Wang63307c32008-05-05 19:05:59 +00008234 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00008235 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
8236 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00008237
Mon P Wangab3e7472008-05-05 22:56:23 +00008238 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008239 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00008240 for (int i=0; i <= lastAddrIndx; ++i)
8241 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00008242
Mon P Wang63307c32008-05-05 19:05:59 +00008243 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00008244 assert((argOpers[valArgIndx]->isReg() ||
8245 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00008246 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00008247
8248 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00008249 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00008250 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008251 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00008252 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00008253 (*MIB).addOperand(*argOpers[valArgIndx]);
8254
Dale Johannesene4d209d2009-02-03 20:21:25 +00008255 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00008256 MIB.addReg(t1);
8257
Dale Johannesene4d209d2009-02-03 20:21:25 +00008258 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00008259 MIB.addReg(t1);
8260 MIB.addReg(t2);
8261
8262 // Generate movc
8263 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00008264 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00008265 MIB.addReg(t2);
8266 MIB.addReg(t1);
8267
8268 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00008269 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00008270 for (int i=0; i <= lastAddrIndx; ++i)
8271 (*MIB).addOperand(*argOpers[i]);
8272 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00008273 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
Dan Gohmanc76909a2009-09-25 20:36:54 +00008274 (*MIB).setMemRefs(mInstr->memoperands_begin(),
8275 mInstr->memoperands_end());
Scott Michelfdc40a02009-02-17 22:15:04 +00008276
Dale Johannesene4d209d2009-02-03 20:21:25 +00008277 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00008278 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00008279
Mon P Wang63307c32008-05-05 19:05:59 +00008280 // insert branch
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008281 BuildMI(newMBB, dl, TII->get(X86::JNE_4)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00008282
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008283 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00008284 return nextMBB;
8285}
8286
Eric Christopherf83a5de2009-08-27 18:08:16 +00008287// FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
8288// all of this code can be replaced with that in the .td file.
Dan Gohmand6708ea2009-08-15 01:38:56 +00008289MachineBasicBlock *
Eric Christopherb120ab42009-08-18 22:50:32 +00008290X86TargetLowering::EmitPCMP(MachineInstr *MI, MachineBasicBlock *BB,
Daniel Dunbara279bc32009-09-20 02:20:51 +00008291 unsigned numArgs, bool memArg) const {
Eric Christopherb120ab42009-08-18 22:50:32 +00008292
8293 MachineFunction *F = BB->getParent();
8294 DebugLoc dl = MI->getDebugLoc();
8295 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8296
8297 unsigned Opc;
Evan Chengce319102009-09-19 09:51:03 +00008298 if (memArg)
8299 Opc = numArgs == 3 ? X86::PCMPISTRM128rm : X86::PCMPESTRM128rm;
8300 else
8301 Opc = numArgs == 3 ? X86::PCMPISTRM128rr : X86::PCMPESTRM128rr;
Eric Christopherb120ab42009-08-18 22:50:32 +00008302
8303 MachineInstrBuilder MIB = BuildMI(BB, dl, TII->get(Opc));
8304
8305 for (unsigned i = 0; i < numArgs; ++i) {
8306 MachineOperand &Op = MI->getOperand(i+1);
8307
8308 if (!(Op.isReg() && Op.isImplicit()))
8309 MIB.addOperand(Op);
8310 }
8311
8312 BuildMI(BB, dl, TII->get(X86::MOVAPSrr), MI->getOperand(0).getReg())
8313 .addReg(X86::XMM0);
8314
8315 F->DeleteMachineInstr(MI);
8316
8317 return BB;
8318}
8319
8320MachineBasicBlock *
Dan Gohmand6708ea2009-08-15 01:38:56 +00008321X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
8322 MachineInstr *MI,
8323 MachineBasicBlock *MBB) const {
8324 // Emit code to save XMM registers to the stack. The ABI says that the
8325 // number of registers to save is given in %al, so it's theoretically
8326 // possible to do an indirect jump trick to avoid saving all of them,
8327 // however this code takes a simpler approach and just executes all
8328 // of the stores if %al is non-zero. It's less code, and it's probably
8329 // easier on the hardware branch predictor, and stores aren't all that
8330 // expensive anyway.
8331
8332 // Create the new basic blocks. One block contains all the XMM stores,
8333 // and one block is the final destination regardless of whether any
8334 // stores were performed.
8335 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
8336 MachineFunction *F = MBB->getParent();
8337 MachineFunction::iterator MBBIter = MBB;
8338 ++MBBIter;
8339 MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
8340 MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
8341 F->insert(MBBIter, XMMSaveMBB);
8342 F->insert(MBBIter, EndMBB);
8343
8344 // Set up the CFG.
8345 // Move any original successors of MBB to the end block.
8346 EndMBB->transferSuccessors(MBB);
8347 // The original block will now fall through to the XMM save block.
8348 MBB->addSuccessor(XMMSaveMBB);
8349 // The XMMSaveMBB will fall through to the end block.
8350 XMMSaveMBB->addSuccessor(EndMBB);
8351
8352 // Now add the instructions.
8353 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8354 DebugLoc DL = MI->getDebugLoc();
8355
8356 unsigned CountReg = MI->getOperand(0).getReg();
8357 int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
8358 int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
8359
8360 if (!Subtarget->isTargetWin64()) {
8361 // If %al is 0, branch around the XMM save block.
8362 BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
Chris Lattnerbd13fb62010-02-11 19:25:55 +00008363 BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008364 MBB->addSuccessor(EndMBB);
8365 }
8366
8367 // In the XMM save block, save all the XMM argument registers.
8368 for (int i = 3, e = MI->getNumOperands(); i != e; ++i) {
8369 int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
Dan Gohmanc76909a2009-09-25 20:36:54 +00008370 MachineMemOperand *MMO =
Evan Chengff89dcb2009-10-18 18:16:27 +00008371 F->getMachineMemOperand(
8372 PseudoSourceValue::getFixedStack(RegSaveFrameIndex),
8373 MachineMemOperand::MOStore, Offset,
8374 /*Size=*/16, /*Align=*/16);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008375 BuildMI(XMMSaveMBB, DL, TII->get(X86::MOVAPSmr))
8376 .addFrameIndex(RegSaveFrameIndex)
8377 .addImm(/*Scale=*/1)
8378 .addReg(/*IndexReg=*/0)
8379 .addImm(/*Disp=*/Offset)
8380 .addReg(/*Segment=*/0)
8381 .addReg(MI->getOperand(i).getReg())
Dan Gohmanc76909a2009-09-25 20:36:54 +00008382 .addMemOperand(MMO);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008383 }
8384
8385 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8386
8387 return EndMBB;
8388}
Mon P Wang63307c32008-05-05 19:05:59 +00008389
Evan Cheng60c07e12006-07-05 22:17:51 +00008390MachineBasicBlock *
Chris Lattner52600972009-09-02 05:57:00 +00008391X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008392 MachineBasicBlock *BB) const {
Chris Lattner52600972009-09-02 05:57:00 +00008393 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8394 DebugLoc DL = MI->getDebugLoc();
Daniel Dunbara279bc32009-09-20 02:20:51 +00008395
Chris Lattner52600972009-09-02 05:57:00 +00008396 // To "insert" a SELECT_CC instruction, we actually have to insert the
8397 // diamond control-flow pattern. The incoming instruction knows the
8398 // destination vreg to set, the condition code register to branch on, the
8399 // true/false values to select between, and a branch opcode to use.
8400 const BasicBlock *LLVM_BB = BB->getBasicBlock();
8401 MachineFunction::iterator It = BB;
8402 ++It;
Daniel Dunbara279bc32009-09-20 02:20:51 +00008403
Chris Lattner52600972009-09-02 05:57:00 +00008404 // thisMBB:
8405 // ...
8406 // TrueVal = ...
8407 // cmpTY ccX, r1, r2
8408 // bCC copy1MBB
8409 // fallthrough --> copy0MBB
8410 MachineBasicBlock *thisMBB = BB;
8411 MachineFunction *F = BB->getParent();
8412 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
8413 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
8414 unsigned Opc =
8415 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
8416 BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
8417 F->insert(It, copy0MBB);
8418 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +00008419 // Update machine-CFG edges by first adding all successors of the current
Chris Lattner52600972009-09-02 05:57:00 +00008420 // block to the new block which will contain the Phi node for the select.
Daniel Dunbara279bc32009-09-20 02:20:51 +00008421 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008422 E = BB->succ_end(); I != E; ++I)
Evan Chengce319102009-09-19 09:51:03 +00008423 sinkMBB->addSuccessor(*I);
Evan Chengce319102009-09-19 09:51:03 +00008424 // Next, remove all successors of the current block, and add the true
8425 // and fallthrough blocks as its successors.
8426 while (!BB->succ_empty())
8427 BB->removeSuccessor(BB->succ_begin());
Chris Lattner52600972009-09-02 05:57:00 +00008428 // Add the true and fallthrough blocks as its successors.
8429 BB->addSuccessor(copy0MBB);
8430 BB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008431
Chris Lattner52600972009-09-02 05:57:00 +00008432 // copy0MBB:
8433 // %FalseValue = ...
8434 // # fallthrough to sinkMBB
Dan Gohman3335a222010-04-30 20:14:26 +00008435 copy0MBB->addSuccessor(sinkMBB);
Daniel Dunbara279bc32009-09-20 02:20:51 +00008436
Chris Lattner52600972009-09-02 05:57:00 +00008437 // sinkMBB:
8438 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
8439 // ...
Dan Gohman3335a222010-04-30 20:14:26 +00008440 BuildMI(sinkMBB, DL, TII->get(X86::PHI), MI->getOperand(0).getReg())
Chris Lattner52600972009-09-02 05:57:00 +00008441 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
8442 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
8443
8444 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Dan Gohman3335a222010-04-30 20:14:26 +00008445 return sinkMBB;
Chris Lattner52600972009-09-02 05:57:00 +00008446}
8447
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008448MachineBasicBlock *
8449X86TargetLowering::EmitLoweredMingwAlloca(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008450 MachineBasicBlock *BB) const {
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008451 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8452 DebugLoc DL = MI->getDebugLoc();
8453 MachineFunction *F = BB->getParent();
8454
8455 // The lowering is pretty easy: we're just emitting the call to _alloca. The
8456 // non-trivial part is impdef of ESP.
8457 // FIXME: The code should be tweaked as soon as we'll try to do codegen for
8458 // mingw-w64.
8459
8460 BuildMI(BB, DL, TII->get(X86::CALLpcrel32))
8461 .addExternalSymbol("_alloca")
8462 .addReg(X86::EAX, RegState::Implicit)
8463 .addReg(X86::ESP, RegState::Implicit)
8464 .addReg(X86::EAX, RegState::Define | RegState::Implicit)
8465 .addReg(X86::ESP, RegState::Define | RegState::Implicit);
8466
8467 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
8468 return BB;
8469}
Chris Lattner52600972009-09-02 05:57:00 +00008470
8471MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00008472X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008473 MachineBasicBlock *BB) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00008474 switch (MI->getOpcode()) {
8475 default: assert(false && "Unexpected instr type to insert");
Anton Korobeynikov043f3c22010-03-06 19:32:29 +00008476 case X86::MINGW_ALLOCA:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008477 return EmitLoweredMingwAlloca(MI, BB);
Dan Gohmancbbea0f2009-08-27 00:14:12 +00008478 case X86::CMOV_GR8:
Mon P Wang9e5ecb82008-12-12 01:25:51 +00008479 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00008480 case X86::CMOV_FR32:
8481 case X86::CMOV_FR64:
8482 case X86::CMOV_V4F32:
8483 case X86::CMOV_V2F64:
Chris Lattner52600972009-09-02 05:57:00 +00008484 case X86::CMOV_V2I64:
Chris Lattner314a1132010-03-14 18:31:44 +00008485 case X86::CMOV_GR16:
8486 case X86::CMOV_GR32:
8487 case X86::CMOV_RFP32:
8488 case X86::CMOV_RFP64:
8489 case X86::CMOV_RFP80:
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00008490 return EmitLoweredSelect(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008491
Dale Johannesen849f2142007-07-03 00:53:03 +00008492 case X86::FP32_TO_INT16_IN_MEM:
8493 case X86::FP32_TO_INT32_IN_MEM:
8494 case X86::FP32_TO_INT64_IN_MEM:
8495 case X86::FP64_TO_INT16_IN_MEM:
8496 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00008497 case X86::FP64_TO_INT64_IN_MEM:
8498 case X86::FP80_TO_INT16_IN_MEM:
8499 case X86::FP80_TO_INT32_IN_MEM:
8500 case X86::FP80_TO_INT64_IN_MEM: {
Chris Lattner52600972009-09-02 05:57:00 +00008501 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
8502 DebugLoc DL = MI->getDebugLoc();
8503
Evan Cheng60c07e12006-07-05 22:17:51 +00008504 // Change the floating point control register to use "round towards zero"
8505 // mode when truncating to an integer value.
8506 MachineFunction *F = BB->getParent();
David Greene3f2bf852009-11-12 20:49:22 +00008507 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
Chris Lattner52600972009-09-02 05:57:00 +00008508 addFrameReference(BuildMI(BB, DL, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008509
8510 // Load the old value of the high byte of the control word...
8511 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00008512 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Chris Lattner52600972009-09-02 05:57:00 +00008513 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00008514 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008515
8516 // Set the high part to be round to zero...
Chris Lattner52600972009-09-02 05:57:00 +00008517 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008518 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00008519
8520 // Reload the modified control word now...
Chris Lattner52600972009-09-02 05:57:00 +00008521 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008522
8523 // Restore the memory image of control word to original value
Chris Lattner52600972009-09-02 05:57:00 +00008524 addFrameReference(BuildMI(BB, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00008525 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00008526
8527 // Get the X86 opcode to use.
8528 unsigned Opc;
8529 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008530 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00008531 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
8532 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
8533 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
8534 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
8535 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
8536 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00008537 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
8538 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
8539 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00008540 }
8541
8542 X86AddressMode AM;
8543 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00008544 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008545 AM.BaseType = X86AddressMode::RegBase;
8546 AM.Base.Reg = Op.getReg();
8547 } else {
8548 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00008549 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00008550 }
8551 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00008552 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008553 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008554 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00008555 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00008556 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008557 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00008558 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00008559 AM.GV = Op.getGlobal();
8560 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00008561 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00008562 }
Chris Lattner52600972009-09-02 05:57:00 +00008563 addFullAddress(BuildMI(BB, DL, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00008564 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00008565
8566 // Reload the original control word now.
Chris Lattner52600972009-09-02 05:57:00 +00008567 addFrameReference(BuildMI(BB, DL, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00008568
Dan Gohman8e5f2c62008-07-07 23:14:23 +00008569 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00008570 return BB;
8571 }
Eric Christopherb120ab42009-08-18 22:50:32 +00008572 // String/text processing lowering.
8573 case X86::PCMPISTRM128REG:
8574 return EmitPCMP(MI, BB, 3, false /* in-mem */);
8575 case X86::PCMPISTRM128MEM:
8576 return EmitPCMP(MI, BB, 3, true /* in-mem */);
8577 case X86::PCMPESTRM128REG:
8578 return EmitPCMP(MI, BB, 5, false /* in mem */);
8579 case X86::PCMPESTRM128MEM:
8580 return EmitPCMP(MI, BB, 5, true /* in mem */);
8581
8582 // Atomic Lowering.
Mon P Wang63307c32008-05-05 19:05:59 +00008583 case X86::ATOMAND32:
8584 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008585 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008586 X86::LCMPXCHG32, X86::MOV32rr,
8587 X86::NOT32r, X86::EAX,
8588 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008589 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00008590 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
8591 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008592 X86::LCMPXCHG32, X86::MOV32rr,
8593 X86::NOT32r, X86::EAX,
8594 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00008595 case X86::ATOMXOR32:
8596 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008597 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008598 X86::LCMPXCHG32, X86::MOV32rr,
8599 X86::NOT32r, X86::EAX,
8600 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00008601 case X86::ATOMNAND32:
8602 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008603 X86::AND32ri, X86::MOV32rm,
8604 X86::LCMPXCHG32, X86::MOV32rr,
8605 X86::NOT32r, X86::EAX,
8606 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00008607 case X86::ATOMMIN32:
8608 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
8609 case X86::ATOMMAX32:
8610 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
8611 case X86::ATOMUMIN32:
8612 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
8613 case X86::ATOMUMAX32:
8614 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00008615
8616 case X86::ATOMAND16:
8617 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8618 X86::AND16ri, X86::MOV16rm,
8619 X86::LCMPXCHG16, X86::MOV16rr,
8620 X86::NOT16r, X86::AX,
8621 X86::GR16RegisterClass);
8622 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00008623 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008624 X86::OR16ri, X86::MOV16rm,
8625 X86::LCMPXCHG16, X86::MOV16rr,
8626 X86::NOT16r, X86::AX,
8627 X86::GR16RegisterClass);
8628 case X86::ATOMXOR16:
8629 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
8630 X86::XOR16ri, X86::MOV16rm,
8631 X86::LCMPXCHG16, X86::MOV16rr,
8632 X86::NOT16r, X86::AX,
8633 X86::GR16RegisterClass);
8634 case X86::ATOMNAND16:
8635 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
8636 X86::AND16ri, X86::MOV16rm,
8637 X86::LCMPXCHG16, X86::MOV16rr,
8638 X86::NOT16r, X86::AX,
8639 X86::GR16RegisterClass, true);
8640 case X86::ATOMMIN16:
8641 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
8642 case X86::ATOMMAX16:
8643 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
8644 case X86::ATOMUMIN16:
8645 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
8646 case X86::ATOMUMAX16:
8647 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
8648
8649 case X86::ATOMAND8:
8650 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8651 X86::AND8ri, X86::MOV8rm,
8652 X86::LCMPXCHG8, X86::MOV8rr,
8653 X86::NOT8r, X86::AL,
8654 X86::GR8RegisterClass);
8655 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00008656 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00008657 X86::OR8ri, X86::MOV8rm,
8658 X86::LCMPXCHG8, X86::MOV8rr,
8659 X86::NOT8r, X86::AL,
8660 X86::GR8RegisterClass);
8661 case X86::ATOMXOR8:
8662 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
8663 X86::XOR8ri, X86::MOV8rm,
8664 X86::LCMPXCHG8, X86::MOV8rr,
8665 X86::NOT8r, X86::AL,
8666 X86::GR8RegisterClass);
8667 case X86::ATOMNAND8:
8668 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
8669 X86::AND8ri, X86::MOV8rm,
8670 X86::LCMPXCHG8, X86::MOV8rr,
8671 X86::NOT8r, X86::AL,
8672 X86::GR8RegisterClass, true);
8673 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008674 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00008675 case X86::ATOMAND64:
8676 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008677 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008678 X86::LCMPXCHG64, X86::MOV64rr,
8679 X86::NOT64r, X86::RAX,
8680 X86::GR64RegisterClass);
8681 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00008682 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
8683 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008684 X86::LCMPXCHG64, X86::MOV64rr,
8685 X86::NOT64r, X86::RAX,
8686 X86::GR64RegisterClass);
8687 case X86::ATOMXOR64:
8688 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00008689 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00008690 X86::LCMPXCHG64, X86::MOV64rr,
8691 X86::NOT64r, X86::RAX,
8692 X86::GR64RegisterClass);
8693 case X86::ATOMNAND64:
8694 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
8695 X86::AND64ri32, X86::MOV64rm,
8696 X86::LCMPXCHG64, X86::MOV64rr,
8697 X86::NOT64r, X86::RAX,
8698 X86::GR64RegisterClass, true);
8699 case X86::ATOMMIN64:
8700 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
8701 case X86::ATOMMAX64:
8702 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
8703 case X86::ATOMUMIN64:
8704 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
8705 case X86::ATOMUMAX64:
8706 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008707
8708 // This group does 64-bit operations on a 32-bit host.
8709 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008710 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008711 X86::AND32rr, X86::AND32rr,
8712 X86::AND32ri, X86::AND32ri,
8713 false);
8714 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008715 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008716 X86::OR32rr, X86::OR32rr,
8717 X86::OR32ri, X86::OR32ri,
8718 false);
8719 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008720 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008721 X86::XOR32rr, X86::XOR32rr,
8722 X86::XOR32ri, X86::XOR32ri,
8723 false);
8724 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008725 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008726 X86::AND32rr, X86::AND32rr,
8727 X86::AND32ri, X86::AND32ri,
8728 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008729 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008730 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008731 X86::ADD32rr, X86::ADC32rr,
8732 X86::ADD32ri, X86::ADC32ri,
8733 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008734 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008735 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00008736 X86::SUB32rr, X86::SBB32rr,
8737 X86::SUB32ri, X86::SBB32ri,
8738 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00008739 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00008740 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00008741 X86::MOV32rr, X86::MOV32rr,
8742 X86::MOV32ri, X86::MOV32ri,
8743 false);
Dan Gohmand6708ea2009-08-15 01:38:56 +00008744 case X86::VASTART_SAVE_XMM_REGS:
8745 return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
Evan Cheng60c07e12006-07-05 22:17:51 +00008746 }
8747}
8748
8749//===----------------------------------------------------------------------===//
8750// X86 Optimization Hooks
8751//===----------------------------------------------------------------------===//
8752
Dan Gohman475871a2008-07-27 21:46:04 +00008753void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00008754 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008755 APInt &KnownZero,
8756 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00008757 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00008758 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008759 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00008760 assert((Opc >= ISD::BUILTIN_OP_END ||
8761 Opc == ISD::INTRINSIC_WO_CHAIN ||
8762 Opc == ISD::INTRINSIC_W_CHAIN ||
8763 Opc == ISD::INTRINSIC_VOID) &&
8764 "Should use MaskedValueIsZero if you don't know whether Op"
8765 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008766
Dan Gohmanf4f92f52008-02-13 23:07:24 +00008767 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008768 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00008769 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008770 case X86ISD::ADD:
8771 case X86ISD::SUB:
8772 case X86ISD::SMUL:
8773 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00008774 case X86ISD::INC:
8775 case X86ISD::DEC:
Dan Gohmane220c4b2009-09-18 19:59:53 +00008776 case X86ISD::OR:
8777 case X86ISD::XOR:
8778 case X86ISD::AND:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00008779 // These nodes' second result is a boolean.
8780 if (Op.getResNo() == 0)
8781 break;
8782 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008783 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00008784 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
8785 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00008786 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008787 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00008788}
Chris Lattner259e97c2006-01-31 19:43:35 +00008789
Evan Cheng206ee9d2006-07-07 08:33:52 +00008790/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00008791/// node is a GlobalAddress + offset.
8792bool X86TargetLowering::isGAPlusOffset(SDNode *N,
Dan Gohman46510a72010-04-15 01:51:59 +00008793 const GlobalValue* &GA,
8794 int64_t &Offset) const {
Evan Chengad4196b2008-05-12 19:56:52 +00008795 if (N->getOpcode() == X86ISD::Wrapper) {
8796 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008797 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00008798 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008799 return true;
8800 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00008801 }
Evan Chengad4196b2008-05-12 19:56:52 +00008802 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008803}
8804
Evan Cheng206ee9d2006-07-07 08:33:52 +00008805/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
8806/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
8807/// if the load addresses are consecutive, non-overlapping, and in the right
Nate Begemanfdea31a2010-03-24 20:49:50 +00008808/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00008809static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00008810 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00008811 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008812 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008813 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
Mon P Wang1e955802009-04-03 02:43:30 +00008814
Eli Friedman7a5e5552009-06-07 06:52:44 +00008815 if (VT.getSizeInBits() != 128)
8816 return SDValue();
8817
Nate Begemanfdea31a2010-03-24 20:49:50 +00008818 SmallVector<SDValue, 16> Elts;
8819 for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
8820 Elts.push_back(DAG.getShuffleScalarElt(SVN, i));
8821
8822 return EltsFromConsecutiveLoads(VT, Elts, dl, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00008823}
Evan Chengd880b972008-05-09 21:53:03 +00008824
Dan Gohman1bbf72b2010-03-15 23:23:03 +00008825/// PerformShuffleCombine - Detect vector gather/scatter index generation
8826/// and convert it from being a bunch of shuffles and extracts to a simple
8827/// store and scalar loads to extract the elements.
8828static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
8829 const TargetLowering &TLI) {
8830 SDValue InputVector = N->getOperand(0);
8831
8832 // Only operate on vectors of 4 elements, where the alternative shuffling
8833 // gets to be more expensive.
8834 if (InputVector.getValueType() != MVT::v4i32)
8835 return SDValue();
8836
8837 // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
8838 // single use which is a sign-extend or zero-extend, and all elements are
8839 // used.
8840 SmallVector<SDNode *, 4> Uses;
8841 unsigned ExtractedElements = 0;
8842 for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
8843 UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
8844 if (UI.getUse().getResNo() != InputVector.getResNo())
8845 return SDValue();
8846
8847 SDNode *Extract = *UI;
8848 if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
8849 return SDValue();
8850
8851 if (Extract->getValueType(0) != MVT::i32)
8852 return SDValue();
8853 if (!Extract->hasOneUse())
8854 return SDValue();
8855 if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
8856 Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
8857 return SDValue();
8858 if (!isa<ConstantSDNode>(Extract->getOperand(1)))
8859 return SDValue();
8860
8861 // Record which element was extracted.
8862 ExtractedElements |=
8863 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
8864
8865 Uses.push_back(Extract);
8866 }
8867
8868 // If not all the elements were used, this may not be worthwhile.
8869 if (ExtractedElements != 15)
8870 return SDValue();
8871
8872 // Ok, we've now decided to do the transformation.
8873 DebugLoc dl = InputVector.getDebugLoc();
8874
8875 // Store the value to a temporary stack slot.
8876 SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
8877 SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, NULL, 0,
8878 false, false, 0);
8879
8880 // Replace each use (extract) with a load of the appropriate element.
8881 for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
8882 UE = Uses.end(); UI != UE; ++UI) {
8883 SDNode *Extract = *UI;
8884
8885 // Compute the element's address.
8886 SDValue Idx = Extract->getOperand(1);
8887 unsigned EltSize =
8888 InputVector.getValueType().getVectorElementType().getSizeInBits()/8;
8889 uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue();
8890 SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy());
8891
8892 SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, Idx.getValueType(), OffsetVal, StackPtr);
8893
8894 // Load the scalar.
8895 SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, ScalarAddr,
8896 NULL, 0, false, false, 0);
8897
8898 // Replace the exact with the load.
8899 DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar);
8900 }
8901
8902 // The replacement was made in place; don't return anything.
8903 return SDValue();
8904}
8905
Chris Lattner83e6c992006-10-04 06:57:07 +00008906/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008907static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00008908 const X86Subtarget *Subtarget) {
8909 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008910 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00008911 // Get the LHS/RHS of the select.
8912 SDValue LHS = N->getOperand(1);
8913 SDValue RHS = N->getOperand(2);
Eric Christopherfd179292009-08-27 18:07:15 +00008914
Dan Gohman670e5392009-09-21 18:03:22 +00008915 // If we have SSE[12] support, try to form min/max nodes. SSE min/max
Dan Gohman8ce05da2010-02-22 04:03:39 +00008916 // instructions match the semantics of the common C idiom x<y?x:y but not
8917 // x<=y?x:y, because of how they handle negative zero (which can be
8918 // ignored in unsafe-math mode).
Chris Lattner83e6c992006-10-04 06:57:07 +00008919 if (Subtarget->hasSSE2() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00008920 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00008921 Cond.getOpcode() == ISD::SETCC) {
8922 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008923
Chris Lattner47b4ce82009-03-11 05:48:52 +00008924 unsigned Opcode = 0;
Dan Gohman670e5392009-09-21 18:03:22 +00008925 // Check for x CC y ? x : y.
Dan Gohmane8326932010-02-24 06:52:40 +00008926 if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
8927 DAG.isEqualTo(RHS, Cond.getOperand(1))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008928 switch (CC) {
8929 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008930 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00008931 // Converting this to a min would handle NaNs incorrectly, and swapping
8932 // the operands would cause it to handle comparisons between positive
8933 // and negative zero incorrectly.
8934 if (!FiniteOnlyFPMath() &&
8935 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8936 if (!UnsafeFPMath &&
8937 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8938 break;
8939 std::swap(LHS, RHS);
8940 }
Dan Gohman670e5392009-09-21 18:03:22 +00008941 Opcode = X86ISD::FMIN;
8942 break;
8943 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00008944 // Converting this to a min would handle comparisons between positive
8945 // and negative zero incorrectly.
8946 if (!UnsafeFPMath &&
8947 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
8948 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008949 Opcode = X86ISD::FMIN;
8950 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008951 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00008952 // Converting this to a min would handle both negative zeros and NaNs
8953 // incorrectly, but we can swap the operands to fix both.
8954 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008955 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008956 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00008957 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008958 Opcode = X86ISD::FMIN;
8959 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008960
Dan Gohman670e5392009-09-21 18:03:22 +00008961 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008962 // Converting this to a max would handle comparisons between positive
8963 // and negative zero incorrectly.
8964 if (!UnsafeFPMath &&
8965 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(LHS))
8966 break;
Dan Gohman670e5392009-09-21 18:03:22 +00008967 Opcode = X86ISD::FMAX;
8968 break;
Chris Lattner47b4ce82009-03-11 05:48:52 +00008969 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00008970 // Converting this to a max would handle NaNs incorrectly, and swapping
8971 // the operands would cause it to handle comparisons between positive
8972 // and negative zero incorrectly.
8973 if (!FiniteOnlyFPMath() &&
8974 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) {
8975 if (!UnsafeFPMath &&
8976 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
8977 break;
8978 std::swap(LHS, RHS);
8979 }
Dan Gohman670e5392009-09-21 18:03:22 +00008980 Opcode = X86ISD::FMAX;
8981 break;
8982 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008983 // Converting this to a max would handle both negative zeros and NaNs
8984 // incorrectly, but we can swap the operands to fix both.
8985 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00008986 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008987 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00008988 case ISD::SETGE:
8989 Opcode = X86ISD::FMAX;
8990 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008991 }
Dan Gohman670e5392009-09-21 18:03:22 +00008992 // Check for x CC y ? y : x -- a min/max with reversed arms.
Dan Gohmane8326932010-02-24 06:52:40 +00008993 } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
8994 DAG.isEqualTo(RHS, Cond.getOperand(0))) {
Chris Lattner47b4ce82009-03-11 05:48:52 +00008995 switch (CC) {
8996 default: break;
Dan Gohman670e5392009-09-21 18:03:22 +00008997 case ISD::SETOGE:
Dan Gohmane8326932010-02-24 06:52:40 +00008998 // Converting this to a min would handle comparisons between positive
8999 // and negative zero incorrectly, and swapping the operands would
9000 // cause it to handle NaNs incorrectly.
9001 if (!UnsafeFPMath &&
9002 !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
9003 if (!FiniteOnlyFPMath() &&
9004 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9005 break;
9006 std::swap(LHS, RHS);
9007 }
Dan Gohman670e5392009-09-21 18:03:22 +00009008 Opcode = X86ISD::FMIN;
Dan Gohman8d44b282009-09-03 20:34:31 +00009009 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009010 case ISD::SETUGT:
Dan Gohmane8326932010-02-24 06:52:40 +00009011 // Converting this to a min would handle NaNs incorrectly.
9012 if (!UnsafeFPMath &&
9013 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9014 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009015 Opcode = X86ISD::FMIN;
9016 break;
9017 case ISD::SETUGE:
Dan Gohmane8326932010-02-24 06:52:40 +00009018 // Converting this to a min would handle both negative zeros and NaNs
9019 // incorrectly, but we can swap the operands to fix both.
9020 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009021 case ISD::SETOGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009022 case ISD::SETGT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009023 case ISD::SETGE:
9024 Opcode = X86ISD::FMIN;
9025 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009026
Dan Gohman670e5392009-09-21 18:03:22 +00009027 case ISD::SETULT:
Dan Gohmane8326932010-02-24 06:52:40 +00009028 // Converting this to a max would handle NaNs incorrectly.
9029 if (!FiniteOnlyFPMath() &&
9030 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9031 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009032 Opcode = X86ISD::FMAX;
Dan Gohman8d44b282009-09-03 20:34:31 +00009033 break;
Dan Gohman670e5392009-09-21 18:03:22 +00009034 case ISD::SETOLE:
Dan Gohmane8326932010-02-24 06:52:40 +00009035 // Converting this to a max would handle comparisons between positive
9036 // and negative zero incorrectly, and swapping the operands would
9037 // cause it to handle NaNs incorrectly.
9038 if (!UnsafeFPMath &&
9039 !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
9040 if (!FiniteOnlyFPMath() &&
9041 (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
9042 break;
9043 std::swap(LHS, RHS);
9044 }
Dan Gohman670e5392009-09-21 18:03:22 +00009045 Opcode = X86ISD::FMAX;
9046 break;
9047 case ISD::SETULE:
Dan Gohmane8326932010-02-24 06:52:40 +00009048 // Converting this to a max would handle both negative zeros and NaNs
9049 // incorrectly, but we can swap the operands to fix both.
9050 std::swap(LHS, RHS);
Dan Gohman670e5392009-09-21 18:03:22 +00009051 case ISD::SETOLT:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009052 case ISD::SETLT:
Dan Gohman670e5392009-09-21 18:03:22 +00009053 case ISD::SETLE:
Chris Lattner47b4ce82009-03-11 05:48:52 +00009054 Opcode = X86ISD::FMAX;
9055 break;
9056 }
Chris Lattner83e6c992006-10-04 06:57:07 +00009057 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009058
Chris Lattner47b4ce82009-03-11 05:48:52 +00009059 if (Opcode)
9060 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00009061 }
Eric Christopherfd179292009-08-27 18:07:15 +00009062
Chris Lattnerd1980a52009-03-12 06:52:53 +00009063 // If this is a select between two integer constants, try to do some
9064 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00009065 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
9066 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00009067 // Don't do this for crazy integer types.
9068 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
9069 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00009070 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009071 bool NeedsCondInvert = false;
Eric Christopherfd179292009-08-27 18:07:15 +00009072
Chris Lattnercee56e72009-03-13 05:53:31 +00009073 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00009074 // Efficiently invertible.
9075 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
9076 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
9077 isa<ConstantSDNode>(Cond.getOperand(1))))) {
9078 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00009079 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009080 }
Eric Christopherfd179292009-08-27 18:07:15 +00009081
Chris Lattnerd1980a52009-03-12 06:52:53 +00009082 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009083 if (FalseC->getAPIntValue() == 0 &&
9084 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00009085 if (NeedsCondInvert) // Invert the condition if needed.
9086 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9087 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009088
Chris Lattnerd1980a52009-03-12 06:52:53 +00009089 // Zero extend the condition if needed.
9090 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009091
Chris Lattnercee56e72009-03-13 05:53:31 +00009092 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00009093 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009094 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009095 }
Eric Christopherfd179292009-08-27 18:07:15 +00009096
Chris Lattner97a29a52009-03-13 05:22:11 +00009097 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00009098 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00009099 if (NeedsCondInvert) // Invert the condition if needed.
9100 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9101 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009102
Chris Lattner97a29a52009-03-13 05:22:11 +00009103 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009104 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9105 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009106 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00009107 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00009108 }
Eric Christopherfd179292009-08-27 18:07:15 +00009109
Chris Lattnercee56e72009-03-13 05:53:31 +00009110 // Optimize cases that will turn into an LEA instruction. This requires
9111 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009112 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009113 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009114 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009115
Chris Lattnercee56e72009-03-13 05:53:31 +00009116 bool isFastMultiplier = false;
9117 if (Diff < 10) {
9118 switch ((unsigned char)Diff) {
9119 default: break;
9120 case 1: // result = add base, cond
9121 case 2: // result = lea base( , cond*2)
9122 case 3: // result = lea base(cond, cond*2)
9123 case 4: // result = lea base( , cond*4)
9124 case 5: // result = lea base(cond, cond*4)
9125 case 8: // result = lea base( , cond*8)
9126 case 9: // result = lea base(cond, cond*8)
9127 isFastMultiplier = true;
9128 break;
9129 }
9130 }
Eric Christopherfd179292009-08-27 18:07:15 +00009131
Chris Lattnercee56e72009-03-13 05:53:31 +00009132 if (isFastMultiplier) {
9133 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9134 if (NeedsCondInvert) // Invert the condition if needed.
9135 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
9136 DAG.getConstant(1, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009137
Chris Lattnercee56e72009-03-13 05:53:31 +00009138 // Zero extend the condition if needed.
9139 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9140 Cond);
9141 // Scale the condition by the difference.
9142 if (Diff != 1)
9143 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9144 DAG.getConstant(Diff, Cond.getValueType()));
Eric Christopherfd179292009-08-27 18:07:15 +00009145
Chris Lattnercee56e72009-03-13 05:53:31 +00009146 // Add the base if non-zero.
9147 if (FalseC->getAPIntValue() != 0)
9148 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9149 SDValue(FalseC, 0));
9150 return Cond;
9151 }
Eric Christopherfd179292009-08-27 18:07:15 +00009152 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009153 }
9154 }
Eric Christopherfd179292009-08-27 18:07:15 +00009155
Dan Gohman475871a2008-07-27 21:46:04 +00009156 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00009157}
9158
Chris Lattnerd1980a52009-03-12 06:52:53 +00009159/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
9160static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
9161 TargetLowering::DAGCombinerInfo &DCI) {
9162 DebugLoc DL = N->getDebugLoc();
Eric Christopherfd179292009-08-27 18:07:15 +00009163
Chris Lattnerd1980a52009-03-12 06:52:53 +00009164 // If the flag operand isn't dead, don't touch this CMOV.
9165 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
9166 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009167
Chris Lattnerd1980a52009-03-12 06:52:53 +00009168 // If this is a select between two integer constants, try to do some
9169 // optimizations. Note that the operands are ordered the opposite of SELECT
9170 // operands.
9171 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
9172 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
9173 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
9174 // larger than FalseC (the false value).
9175 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
Eric Christopherfd179292009-08-27 18:07:15 +00009176
Chris Lattnerd1980a52009-03-12 06:52:53 +00009177 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
9178 CC = X86::GetOppositeBranchCondition(CC);
9179 std::swap(TrueC, FalseC);
9180 }
Eric Christopherfd179292009-08-27 18:07:15 +00009181
Chris Lattnerd1980a52009-03-12 06:52:53 +00009182 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00009183 // This is efficient for any integer data type (including i8/i16) and
9184 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00009185 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
9186 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009187 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9188 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009189
Chris Lattnerd1980a52009-03-12 06:52:53 +00009190 // Zero extend the condition if needed.
9191 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009192
Chris Lattnerd1980a52009-03-12 06:52:53 +00009193 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
9194 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
Owen Anderson825b72b2009-08-11 20:47:22 +00009195 DAG.getConstant(ShAmt, MVT::i8));
Chris Lattnerd1980a52009-03-12 06:52:53 +00009196 if (N->getNumValues() == 2) // Dead flag value?
9197 return DCI.CombineTo(N, Cond, SDValue());
9198 return Cond;
9199 }
Eric Christopherfd179292009-08-27 18:07:15 +00009200
Chris Lattnercee56e72009-03-13 05:53:31 +00009201 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
9202 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00009203 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
9204 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009205 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9206 DAG.getConstant(CC, MVT::i8), Cond);
Eric Christopherfd179292009-08-27 18:07:15 +00009207
Chris Lattner97a29a52009-03-13 05:22:11 +00009208 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00009209 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
9210 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00009211 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9212 SDValue(FalseC, 0));
Eric Christopherfd179292009-08-27 18:07:15 +00009213
Chris Lattner97a29a52009-03-13 05:22:11 +00009214 if (N->getNumValues() == 2) // Dead flag value?
9215 return DCI.CombineTo(N, Cond, SDValue());
9216 return Cond;
9217 }
Eric Christopherfd179292009-08-27 18:07:15 +00009218
Chris Lattnercee56e72009-03-13 05:53:31 +00009219 // Optimize cases that will turn into an LEA instruction. This requires
9220 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
Owen Anderson825b72b2009-08-11 20:47:22 +00009221 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
Chris Lattnercee56e72009-03-13 05:53:31 +00009222 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009223 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
Eric Christopherfd179292009-08-27 18:07:15 +00009224
Chris Lattnercee56e72009-03-13 05:53:31 +00009225 bool isFastMultiplier = false;
9226 if (Diff < 10) {
9227 switch ((unsigned char)Diff) {
9228 default: break;
9229 case 1: // result = add base, cond
9230 case 2: // result = lea base( , cond*2)
9231 case 3: // result = lea base(cond, cond*2)
9232 case 4: // result = lea base( , cond*4)
9233 case 5: // result = lea base(cond, cond*4)
9234 case 8: // result = lea base( , cond*8)
9235 case 9: // result = lea base(cond, cond*8)
9236 isFastMultiplier = true;
9237 break;
9238 }
9239 }
Eric Christopherfd179292009-08-27 18:07:15 +00009240
Chris Lattnercee56e72009-03-13 05:53:31 +00009241 if (isFastMultiplier) {
9242 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
9243 SDValue Cond = N->getOperand(3);
Owen Anderson825b72b2009-08-11 20:47:22 +00009244 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
9245 DAG.getConstant(CC, MVT::i8), Cond);
Chris Lattnercee56e72009-03-13 05:53:31 +00009246 // Zero extend the condition if needed.
9247 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
9248 Cond);
9249 // Scale the condition by the difference.
9250 if (Diff != 1)
9251 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
9252 DAG.getConstant(Diff, Cond.getValueType()));
9253
9254 // Add the base if non-zero.
9255 if (FalseC->getAPIntValue() != 0)
9256 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
9257 SDValue(FalseC, 0));
9258 if (N->getNumValues() == 2) // Dead flag value?
9259 return DCI.CombineTo(N, Cond, SDValue());
9260 return Cond;
9261 }
Eric Christopherfd179292009-08-27 18:07:15 +00009262 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00009263 }
9264 }
9265 return SDValue();
9266}
9267
9268
Evan Cheng0b0cd912009-03-28 05:57:29 +00009269/// PerformMulCombine - Optimize a single multiply with constant into two
9270/// in order to implement it with two cheaper instructions, e.g.
9271/// LEA + SHL, LEA + LEA.
9272static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
9273 TargetLowering::DAGCombinerInfo &DCI) {
Evan Cheng0b0cd912009-03-28 05:57:29 +00009274 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
9275 return SDValue();
9276
Owen Andersone50ed302009-08-10 22:56:29 +00009277 EVT VT = N->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00009278 if (VT != MVT::i64)
Evan Cheng0b0cd912009-03-28 05:57:29 +00009279 return SDValue();
9280
9281 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
9282 if (!C)
9283 return SDValue();
9284 uint64_t MulAmt = C->getZExtValue();
9285 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
9286 return SDValue();
9287
9288 uint64_t MulAmt1 = 0;
9289 uint64_t MulAmt2 = 0;
9290 if ((MulAmt % 9) == 0) {
9291 MulAmt1 = 9;
9292 MulAmt2 = MulAmt / 9;
9293 } else if ((MulAmt % 5) == 0) {
9294 MulAmt1 = 5;
9295 MulAmt2 = MulAmt / 5;
9296 } else if ((MulAmt % 3) == 0) {
9297 MulAmt1 = 3;
9298 MulAmt2 = MulAmt / 3;
9299 }
9300 if (MulAmt2 &&
9301 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
9302 DebugLoc DL = N->getDebugLoc();
9303
9304 if (isPowerOf2_64(MulAmt2) &&
9305 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
9306 // If second multiplifer is pow2, issue it first. We want the multiply by
9307 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
9308 // is an add.
9309 std::swap(MulAmt1, MulAmt2);
9310
9311 SDValue NewMul;
Eric Christopherfd179292009-08-27 18:07:15 +00009312 if (isPowerOf2_64(MulAmt1))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009313 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
Owen Anderson825b72b2009-08-11 20:47:22 +00009314 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
Evan Cheng0b0cd912009-03-28 05:57:29 +00009315 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009316 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00009317 DAG.getConstant(MulAmt1, VT));
9318
Eric Christopherfd179292009-08-27 18:07:15 +00009319 if (isPowerOf2_64(MulAmt2))
Evan Cheng0b0cd912009-03-28 05:57:29 +00009320 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
Owen Anderson825b72b2009-08-11 20:47:22 +00009321 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
Eric Christopherfd179292009-08-27 18:07:15 +00009322 else
Evan Cheng73f24c92009-03-30 21:36:47 +00009323 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00009324 DAG.getConstant(MulAmt2, VT));
9325
9326 // Do not add new nodes to DAG combiner worklist.
9327 DCI.CombineTo(N, NewMul, false);
9328 }
9329 return SDValue();
9330}
9331
Evan Chengad9c0a32009-12-15 00:53:42 +00009332static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
9333 SDValue N0 = N->getOperand(0);
9334 SDValue N1 = N->getOperand(1);
9335 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
9336 EVT VT = N0.getValueType();
9337
9338 // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
9339 // since the result of setcc_c is all zero's or all ones.
9340 if (N1C && N0.getOpcode() == ISD::AND &&
9341 N0.getOperand(1).getOpcode() == ISD::Constant) {
9342 SDValue N00 = N0.getOperand(0);
9343 if (N00.getOpcode() == X86ISD::SETCC_CARRY ||
9344 ((N00.getOpcode() == ISD::ANY_EXTEND ||
9345 N00.getOpcode() == ISD::ZERO_EXTEND) &&
9346 N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) {
9347 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
9348 APInt ShAmt = N1C->getAPIntValue();
9349 Mask = Mask.shl(ShAmt);
9350 if (Mask != 0)
9351 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
9352 N00, DAG.getConstant(Mask, VT));
9353 }
9354 }
9355
9356 return SDValue();
9357}
Evan Cheng0b0cd912009-03-28 05:57:29 +00009358
Nate Begeman740ab032009-01-26 00:52:55 +00009359/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
9360/// when possible.
9361static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
9362 const X86Subtarget *Subtarget) {
Evan Chengad9c0a32009-12-15 00:53:42 +00009363 EVT VT = N->getValueType(0);
9364 if (!VT.isVector() && VT.isInteger() &&
9365 N->getOpcode() == ISD::SHL)
9366 return PerformSHLCombine(N, DAG);
9367
Nate Begeman740ab032009-01-26 00:52:55 +00009368 // On X86 with SSE2 support, we can transform this to a vector shift if
9369 // all elements are shifted by the same amount. We can't do this in legalize
9370 // because the a constant vector is typically transformed to a constant pool
9371 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009372 if (!Subtarget->hasSSE2())
9373 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009374
Owen Anderson825b72b2009-08-11 20:47:22 +00009375 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009376 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009377
Mon P Wang3becd092009-01-28 08:12:05 +00009378 SDValue ShAmtOp = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00009379 EVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00009380 DebugLoc DL = N->getDebugLoc();
Mon P Wangefa42202009-09-03 19:56:25 +00009381 SDValue BaseShAmt = SDValue();
Mon P Wang3becd092009-01-28 08:12:05 +00009382 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
9383 unsigned NumElts = VT.getVectorNumElements();
9384 unsigned i = 0;
9385 for (; i != NumElts; ++i) {
9386 SDValue Arg = ShAmtOp.getOperand(i);
9387 if (Arg.getOpcode() == ISD::UNDEF) continue;
9388 BaseShAmt = Arg;
9389 break;
9390 }
9391 for (; i != NumElts; ++i) {
9392 SDValue Arg = ShAmtOp.getOperand(i);
9393 if (Arg.getOpcode() == ISD::UNDEF) continue;
9394 if (Arg != BaseShAmt) {
9395 return SDValue();
9396 }
9397 }
9398 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00009399 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
Mon P Wangefa42202009-09-03 19:56:25 +00009400 SDValue InVec = ShAmtOp.getOperand(0);
9401 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
9402 unsigned NumElts = InVec.getValueType().getVectorNumElements();
9403 unsigned i = 0;
9404 for (; i != NumElts; ++i) {
9405 SDValue Arg = InVec.getOperand(i);
9406 if (Arg.getOpcode() == ISD::UNDEF) continue;
9407 BaseShAmt = Arg;
9408 break;
9409 }
9410 } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
9411 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
Evan Chengae3ecf92010-02-16 21:09:44 +00009412 unsigned SplatIdx= cast<ShuffleVectorSDNode>(ShAmtOp)->getSplatIndex();
Mon P Wangefa42202009-09-03 19:56:25 +00009413 if (C->getZExtValue() == SplatIdx)
9414 BaseShAmt = InVec.getOperand(1);
9415 }
9416 }
9417 if (BaseShAmt.getNode() == 0)
9418 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
9419 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00009420 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009421 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00009422
Mon P Wangefa42202009-09-03 19:56:25 +00009423 // The shift amount is an i32.
Owen Anderson825b72b2009-08-11 20:47:22 +00009424 if (EltVT.bitsGT(MVT::i32))
9425 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
9426 else if (EltVT.bitsLT(MVT::i32))
Mon P Wangefa42202009-09-03 19:56:25 +00009427 BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00009428
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009429 // The shift amount is identical so we can do a vector shift.
9430 SDValue ValOp = N->getOperand(0);
9431 switch (N->getOpcode()) {
9432 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00009433 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009434 break;
9435 case ISD::SHL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009436 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009437 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009438 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009439 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009440 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009441 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009442 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009443 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009444 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009445 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009446 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009447 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009448 break;
9449 case ISD::SRA:
Owen Anderson825b72b2009-08-11 20:47:22 +00009450 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009451 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009452 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009453 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009454 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009455 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009456 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009457 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009458 break;
9459 case ISD::SRL:
Owen Anderson825b72b2009-08-11 20:47:22 +00009460 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009461 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009462 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009463 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009464 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009465 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009466 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009467 ValOp, BaseShAmt);
Owen Anderson825b72b2009-08-11 20:47:22 +00009468 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00009469 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Owen Anderson825b72b2009-08-11 20:47:22 +00009470 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
Nate Begeman740ab032009-01-26 00:52:55 +00009471 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00009472 break;
Nate Begeman740ab032009-01-26 00:52:55 +00009473 }
9474 return SDValue();
9475}
9476
Evan Cheng760d1942010-01-04 21:22:48 +00009477static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8b1190a2010-04-28 01:18:01 +00009478 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng760d1942010-01-04 21:22:48 +00009479 const X86Subtarget *Subtarget) {
Evan Cheng39cfeec2010-04-28 02:25:18 +00009480 if (DCI.isBeforeLegalizeOps())
Evan Cheng8b1190a2010-04-28 01:18:01 +00009481 return SDValue();
9482
Evan Cheng760d1942010-01-04 21:22:48 +00009483 EVT VT = N->getValueType(0);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009484 if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
Evan Cheng760d1942010-01-04 21:22:48 +00009485 return SDValue();
9486
9487 // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
9488 SDValue N0 = N->getOperand(0);
9489 SDValue N1 = N->getOperand(1);
9490 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
9491 std::swap(N0, N1);
9492 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
9493 return SDValue();
Evan Cheng8b1190a2010-04-28 01:18:01 +00009494 if (!N0.hasOneUse() || !N1.hasOneUse())
9495 return SDValue();
Evan Cheng760d1942010-01-04 21:22:48 +00009496
9497 SDValue ShAmt0 = N0.getOperand(1);
9498 if (ShAmt0.getValueType() != MVT::i8)
9499 return SDValue();
9500 SDValue ShAmt1 = N1.getOperand(1);
9501 if (ShAmt1.getValueType() != MVT::i8)
9502 return SDValue();
9503 if (ShAmt0.getOpcode() == ISD::TRUNCATE)
9504 ShAmt0 = ShAmt0.getOperand(0);
9505 if (ShAmt1.getOpcode() == ISD::TRUNCATE)
9506 ShAmt1 = ShAmt1.getOperand(0);
9507
9508 DebugLoc DL = N->getDebugLoc();
9509 unsigned Opc = X86ISD::SHLD;
9510 SDValue Op0 = N0.getOperand(0);
9511 SDValue Op1 = N1.getOperand(0);
9512 if (ShAmt0.getOpcode() == ISD::SUB) {
9513 Opc = X86ISD::SHRD;
9514 std::swap(Op0, Op1);
9515 std::swap(ShAmt0, ShAmt1);
9516 }
9517
Evan Cheng8b1190a2010-04-28 01:18:01 +00009518 unsigned Bits = VT.getSizeInBits();
Evan Cheng760d1942010-01-04 21:22:48 +00009519 if (ShAmt1.getOpcode() == ISD::SUB) {
9520 SDValue Sum = ShAmt1.getOperand(0);
9521 if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
Evan Cheng8b1190a2010-04-28 01:18:01 +00009522 if (SumC->getSExtValue() == Bits &&
Evan Cheng760d1942010-01-04 21:22:48 +00009523 ShAmt1.getOperand(1) == ShAmt0)
9524 return DAG.getNode(Opc, DL, VT,
9525 Op0, Op1,
9526 DAG.getNode(ISD::TRUNCATE, DL,
9527 MVT::i8, ShAmt0));
9528 }
9529 } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
9530 ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
9531 if (ShAmt0C &&
Evan Cheng8b1190a2010-04-28 01:18:01 +00009532 ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
Evan Cheng760d1942010-01-04 21:22:48 +00009533 return DAG.getNode(Opc, DL, VT,
9534 N0.getOperand(0), N1.getOperand(0),
9535 DAG.getNode(ISD::TRUNCATE, DL,
9536 MVT::i8, ShAmt0));
9537 }
9538
9539 return SDValue();
9540}
9541
Chris Lattner149a4e52008-02-22 02:09:43 +00009542/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009543static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00009544 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00009545 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
9546 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00009547 // A preferable solution to the general problem is to figure out the right
9548 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00009549
9550 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00009551 StoreSDNode *St = cast<StoreSDNode>(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009552 EVT VT = St->getValue().getValueType();
Evan Cheng536e6672009-03-12 05:59:15 +00009553 if (VT.getSizeInBits() != 64)
9554 return SDValue();
9555
Devang Patel578efa92009-06-05 21:57:13 +00009556 const Function *F = DAG.getMachineFunction().getFunction();
9557 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
Eric Christopherfd179292009-08-27 18:07:15 +00009558 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
Devang Patel578efa92009-06-05 21:57:13 +00009559 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00009560 if ((VT.isVector() ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009561 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00009562 isa<LoadSDNode>(St->getValue()) &&
9563 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
9564 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009565 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009566 LoadSDNode *Ld = 0;
9567 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00009568 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00009569 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009570 // Must be a store of a load. We currently handle two cases: the load
9571 // is a direct child, and it's under an intervening TokenFactor. It is
9572 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00009573 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00009574 Ld = cast<LoadSDNode>(St->getChain());
9575 else if (St->getValue().hasOneUse() &&
9576 ChainVal->getOpcode() == ISD::TokenFactor) {
9577 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009578 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00009579 TokenFactorIndex = i;
9580 Ld = cast<LoadSDNode>(St->getValue());
9581 } else
9582 Ops.push_back(ChainVal->getOperand(i));
9583 }
9584 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00009585
Evan Cheng536e6672009-03-12 05:59:15 +00009586 if (!Ld || !ISD::isNormalLoad(Ld))
9587 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009588
Evan Cheng536e6672009-03-12 05:59:15 +00009589 // If this is not the MMX case, i.e. we are just turning i64 load/store
9590 // into f64 load/store, avoid the transformation if there are multiple
9591 // uses of the loaded value.
9592 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
9593 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00009594
Evan Cheng536e6672009-03-12 05:59:15 +00009595 DebugLoc LdDL = Ld->getDebugLoc();
9596 DebugLoc StDL = N->getDebugLoc();
9597 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
9598 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
9599 // pair instead.
9600 if (Subtarget->is64Bit() || F64IsLegal) {
Owen Anderson825b72b2009-08-11 20:47:22 +00009601 EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
Evan Cheng536e6672009-03-12 05:59:15 +00009602 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
9603 Ld->getBasePtr(), Ld->getSrcValue(),
9604 Ld->getSrcValueOffset(), Ld->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009605 Ld->isNonTemporal(), Ld->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009606 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00009607 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00009608 Ops.push_back(NewChain);
Owen Anderson825b72b2009-08-11 20:47:22 +00009609 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00009610 Ops.size());
9611 }
Evan Cheng536e6672009-03-12 05:59:15 +00009612 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00009613 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009614 St->isVolatile(), St->isNonTemporal(),
9615 St->getAlignment());
Chris Lattner149a4e52008-02-22 02:09:43 +00009616 }
Evan Cheng536e6672009-03-12 05:59:15 +00009617
9618 // Otherwise, lower to two pairs of 32-bit loads / stores.
9619 SDValue LoAddr = Ld->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009620 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
9621 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009622
Owen Anderson825b72b2009-08-11 20:47:22 +00009623 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009624 Ld->getSrcValue(), Ld->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009625 Ld->isVolatile(), Ld->isNonTemporal(),
9626 Ld->getAlignment());
Owen Anderson825b72b2009-08-11 20:47:22 +00009627 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
Evan Cheng536e6672009-03-12 05:59:15 +00009628 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
David Greene67c9d422010-02-15 16:53:33 +00009629 Ld->isVolatile(), Ld->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009630 MinAlign(Ld->getAlignment(), 4));
9631
9632 SDValue NewChain = LoLd.getValue(1);
9633 if (TokenFactorIndex != -1) {
9634 Ops.push_back(LoLd);
9635 Ops.push_back(HiLd);
Owen Anderson825b72b2009-08-11 20:47:22 +00009636 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Evan Cheng536e6672009-03-12 05:59:15 +00009637 Ops.size());
9638 }
9639
9640 LoAddr = St->getBasePtr();
Owen Anderson825b72b2009-08-11 20:47:22 +00009641 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
9642 DAG.getConstant(4, MVT::i32));
Evan Cheng536e6672009-03-12 05:59:15 +00009643
9644 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
9645 St->getSrcValue(), St->getSrcValueOffset(),
David Greene67c9d422010-02-15 16:53:33 +00009646 St->isVolatile(), St->isNonTemporal(),
9647 St->getAlignment());
Evan Cheng536e6672009-03-12 05:59:15 +00009648 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
9649 St->getSrcValue(),
9650 St->getSrcValueOffset() + 4,
9651 St->isVolatile(),
David Greene67c9d422010-02-15 16:53:33 +00009652 St->isNonTemporal(),
Evan Cheng536e6672009-03-12 05:59:15 +00009653 MinAlign(St->getAlignment(), 4));
Owen Anderson825b72b2009-08-11 20:47:22 +00009654 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00009655 }
Dan Gohman475871a2008-07-27 21:46:04 +00009656 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00009657}
9658
Chris Lattner6cf73262008-01-25 06:14:17 +00009659/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
9660/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009661static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00009662 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
9663 // F[X]OR(0.0, x) -> x
9664 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00009665 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9666 if (C->getValueAPF().isPosZero())
9667 return N->getOperand(1);
9668 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9669 if (C->getValueAPF().isPosZero())
9670 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00009671 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009672}
9673
9674/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009675static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00009676 // FAND(0.0, x) -> 0.0
9677 // FAND(x, 0.0) -> 0.0
9678 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
9679 if (C->getValueAPF().isPosZero())
9680 return N->getOperand(0);
9681 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
9682 if (C->getValueAPF().isPosZero())
9683 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00009684 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00009685}
9686
Dan Gohmane5af2d32009-01-29 01:59:02 +00009687static SDValue PerformBTCombine(SDNode *N,
9688 SelectionDAG &DAG,
9689 TargetLowering::DAGCombinerInfo &DCI) {
9690 // BT ignores high bits in the bit index operand.
9691 SDValue Op1 = N->getOperand(1);
9692 if (Op1.hasOneUse()) {
9693 unsigned BitWidth = Op1.getValueSizeInBits();
9694 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
9695 APInt KnownZero, KnownOne;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009696 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
9697 !DCI.isBeforeLegalizeOps());
Dan Gohmand858e902010-04-17 15:26:15 +00009698 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmane5af2d32009-01-29 01:59:02 +00009699 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
9700 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
9701 DCI.CommitTargetLoweringOpt(TLO);
9702 }
9703 return SDValue();
9704}
Chris Lattner83e6c992006-10-04 06:57:07 +00009705
Eli Friedman7a5e5552009-06-07 06:52:44 +00009706static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
9707 SDValue Op = N->getOperand(0);
9708 if (Op.getOpcode() == ISD::BIT_CONVERT)
9709 Op = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009710 EVT VT = N->getValueType(0), OpVT = Op.getValueType();
Eli Friedman7a5e5552009-06-07 06:52:44 +00009711 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
Eric Christopherfd179292009-08-27 18:07:15 +00009712 VT.getVectorElementType().getSizeInBits() ==
Eli Friedman7a5e5552009-06-07 06:52:44 +00009713 OpVT.getVectorElementType().getSizeInBits()) {
9714 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
9715 }
9716 return SDValue();
9717}
9718
Owen Anderson99177002009-06-29 18:04:45 +00009719// On X86 and X86-64, atomic operations are lowered to locked instructions.
9720// Locked instructions, in turn, have implicit fence semantics (all memory
9721// operations are flushed before issuing the locked instruction, and the
Eric Christopherfd179292009-08-27 18:07:15 +00009722// are not buffered), so we can fold away the common pattern of
Owen Anderson99177002009-06-29 18:04:45 +00009723// fence-atomic-fence.
9724static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
9725 SDValue atomic = N->getOperand(0);
9726 switch (atomic.getOpcode()) {
9727 case ISD::ATOMIC_CMP_SWAP:
9728 case ISD::ATOMIC_SWAP:
9729 case ISD::ATOMIC_LOAD_ADD:
9730 case ISD::ATOMIC_LOAD_SUB:
9731 case ISD::ATOMIC_LOAD_AND:
9732 case ISD::ATOMIC_LOAD_OR:
9733 case ISD::ATOMIC_LOAD_XOR:
9734 case ISD::ATOMIC_LOAD_NAND:
9735 case ISD::ATOMIC_LOAD_MIN:
9736 case ISD::ATOMIC_LOAD_MAX:
9737 case ISD::ATOMIC_LOAD_UMIN:
9738 case ISD::ATOMIC_LOAD_UMAX:
9739 break;
9740 default:
9741 return SDValue();
9742 }
Eric Christopherfd179292009-08-27 18:07:15 +00009743
Owen Anderson99177002009-06-29 18:04:45 +00009744 SDValue fence = atomic.getOperand(0);
9745 if (fence.getOpcode() != ISD::MEMBARRIER)
9746 return SDValue();
Eric Christopherfd179292009-08-27 18:07:15 +00009747
Owen Anderson99177002009-06-29 18:04:45 +00009748 switch (atomic.getOpcode()) {
9749 case ISD::ATOMIC_CMP_SWAP:
9750 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9751 atomic.getOperand(1), atomic.getOperand(2),
9752 atomic.getOperand(3));
9753 case ISD::ATOMIC_SWAP:
9754 case ISD::ATOMIC_LOAD_ADD:
9755 case ISD::ATOMIC_LOAD_SUB:
9756 case ISD::ATOMIC_LOAD_AND:
9757 case ISD::ATOMIC_LOAD_OR:
9758 case ISD::ATOMIC_LOAD_XOR:
9759 case ISD::ATOMIC_LOAD_NAND:
9760 case ISD::ATOMIC_LOAD_MIN:
9761 case ISD::ATOMIC_LOAD_MAX:
9762 case ISD::ATOMIC_LOAD_UMIN:
9763 case ISD::ATOMIC_LOAD_UMAX:
9764 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
9765 atomic.getOperand(1), atomic.getOperand(2));
9766 default:
9767 return SDValue();
9768 }
9769}
9770
Evan Cheng2e489c42009-12-16 00:53:11 +00009771static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG) {
9772 // (i32 zext (and (i8 x86isd::setcc_carry), 1)) ->
9773 // (and (i32 x86isd::setcc_carry), 1)
9774 // This eliminates the zext. This transformation is necessary because
9775 // ISD::SETCC is always legalized to i8.
9776 DebugLoc dl = N->getDebugLoc();
9777 SDValue N0 = N->getOperand(0);
9778 EVT VT = N->getValueType(0);
9779 if (N0.getOpcode() == ISD::AND &&
9780 N0.hasOneUse() &&
9781 N0.getOperand(0).hasOneUse()) {
9782 SDValue N00 = N0.getOperand(0);
9783 if (N00.getOpcode() != X86ISD::SETCC_CARRY)
9784 return SDValue();
9785 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
9786 if (!C || C->getZExtValue() != 1)
9787 return SDValue();
9788 return DAG.getNode(ISD::AND, dl, VT,
9789 DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
9790 N00.getOperand(0), N00.getOperand(1)),
9791 DAG.getConstant(1, VT));
9792 }
9793
9794 return SDValue();
9795}
9796
Dan Gohman475871a2008-07-27 21:46:04 +00009797SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00009798 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00009799 SelectionDAG &DAG = DCI.DAG;
9800 switch (N->getOpcode()) {
9801 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00009802 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Dan Gohman1bbf72b2010-03-15 23:23:03 +00009803 case ISD::EXTRACT_VECTOR_ELT:
9804 return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00009805 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00009806 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00009807 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00009808 case ISD::SHL:
9809 case ISD::SRA:
9810 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng8b1190a2010-04-28 01:18:01 +00009811 case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00009812 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00009813 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00009814 case X86ISD::FOR: return PerformFORCombine(N, DAG);
9815 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00009816 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00009817 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00009818 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng2e489c42009-12-16 00:53:11 +00009819 case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00009820 }
9821
Dan Gohman475871a2008-07-27 21:46:04 +00009822 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00009823}
9824
Evan Chenge5b51ac2010-04-17 06:13:15 +00009825/// isTypeDesirableForOp - Return true if the target has native support for
9826/// the specified value type and it is 'desirable' to use the type for the
9827/// given node type. e.g. On x86 i16 is legal, but undesirable since i16
9828/// instruction encodings are longer and some i16 instructions are slow.
9829bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
9830 if (!isTypeLegal(VT))
9831 return false;
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009832 if (VT != MVT::i16)
Evan Chenge5b51ac2010-04-17 06:13:15 +00009833 return true;
9834
9835 switch (Opc) {
9836 default:
9837 return true;
Evan Cheng4c26e932010-04-19 19:29:22 +00009838 case ISD::LOAD:
9839 case ISD::SIGN_EXTEND:
9840 case ISD::ZERO_EXTEND:
9841 case ISD::ANY_EXTEND:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009842 case ISD::SHL:
Evan Chenge5b51ac2010-04-17 06:13:15 +00009843 case ISD::SRL:
9844 case ISD::SUB:
9845 case ISD::ADD:
9846 case ISD::MUL:
9847 case ISD::AND:
9848 case ISD::OR:
9849 case ISD::XOR:
9850 return false;
9851 }
9852}
9853
Evan Chengc82c20b2010-04-24 04:44:57 +00009854static bool MayFoldLoad(SDValue Op) {
9855 return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
9856}
9857
9858static bool MayFoldIntoStore(SDValue Op) {
9859 return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
9860}
9861
Evan Chenge5b51ac2010-04-17 06:13:15 +00009862/// IsDesirableToPromoteOp - This method query the target whether it is
Evan Cheng64b7bf72010-04-16 06:14:10 +00009863/// beneficial for dag combiner to promote the specified node. If true, it
9864/// should return the desired promotion type by reference.
Evan Chenge5b51ac2010-04-17 06:13:15 +00009865bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009866 EVT VT = Op.getValueType();
9867 if (VT != MVT::i16)
9868 return false;
9869
Evan Cheng4c26e932010-04-19 19:29:22 +00009870 bool Promote = false;
9871 bool Commute = false;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009872 switch (Op.getOpcode()) {
Evan Cheng4c26e932010-04-19 19:29:22 +00009873 default: break;
9874 case ISD::LOAD: {
9875 LoadSDNode *LD = cast<LoadSDNode>(Op);
9876 // If the non-extending load has a single use and it's not live out, then it
9877 // might be folded.
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009878 if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
9879 Op.hasOneUse()*/) {
9880 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
9881 UE = Op.getNode()->use_end(); UI != UE; ++UI) {
9882 // The only case where we'd want to promote LOAD (rather then it being
9883 // promoted as an operand is when it's only use is liveout.
9884 if (UI->getOpcode() != ISD::CopyToReg)
9885 return false;
9886 }
9887 }
Evan Cheng4c26e932010-04-19 19:29:22 +00009888 Promote = true;
9889 break;
9890 }
9891 case ISD::SIGN_EXTEND:
9892 case ISD::ZERO_EXTEND:
9893 case ISD::ANY_EXTEND:
9894 Promote = true;
9895 break;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009896 case ISD::SHL:
Evan Cheng2bce5f4b2010-04-28 08:30:49 +00009897 case ISD::SRL: {
Evan Chenge5b51ac2010-04-17 06:13:15 +00009898 SDValue N0 = Op.getOperand(0);
9899 // Look out for (store (shl (load), x)).
Evan Chengc82c20b2010-04-24 04:44:57 +00009900 if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
Evan Chenge5b51ac2010-04-17 06:13:15 +00009901 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009902 Promote = true;
Evan Chenge5b51ac2010-04-17 06:13:15 +00009903 break;
9904 }
Evan Cheng64b7bf72010-04-16 06:14:10 +00009905 case ISD::ADD:
9906 case ISD::MUL:
9907 case ISD::AND:
9908 case ISD::OR:
Evan Cheng4c26e932010-04-19 19:29:22 +00009909 case ISD::XOR:
9910 Commute = true;
9911 // fallthrough
9912 case ISD::SUB: {
Evan Cheng64b7bf72010-04-16 06:14:10 +00009913 SDValue N0 = Op.getOperand(0);
9914 SDValue N1 = Op.getOperand(1);
Evan Chengc82c20b2010-04-24 04:44:57 +00009915 if (!Commute && MayFoldLoad(N1))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009916 return false;
9917 // Avoid disabling potential load folding opportunities.
Evan Chengc82c20b2010-04-24 04:44:57 +00009918 if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009919 return false;
Evan Chengc82c20b2010-04-24 04:44:57 +00009920 if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
Evan Cheng64b7bf72010-04-16 06:14:10 +00009921 return false;
Evan Cheng4c26e932010-04-19 19:29:22 +00009922 Promote = true;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009923 }
9924 }
9925
9926 PVT = MVT::i32;
Evan Cheng4c26e932010-04-19 19:29:22 +00009927 return Promote;
Evan Cheng64b7bf72010-04-16 06:14:10 +00009928}
9929
Evan Cheng60c07e12006-07-05 22:17:51 +00009930//===----------------------------------------------------------------------===//
9931// X86 Inline Assembly Support
9932//===----------------------------------------------------------------------===//
9933
Chris Lattnerb8105652009-07-20 17:51:36 +00009934static bool LowerToBSwap(CallInst *CI) {
9935 // FIXME: this should verify that we are targetting a 486 or better. If not,
9936 // we will turn this bswap into something that will be lowered to logical ops
9937 // instead of emitting the bswap asm. For now, we don't support 486 or lower
9938 // so don't worry about this.
Eric Christopherfd179292009-08-27 18:07:15 +00009939
Chris Lattnerb8105652009-07-20 17:51:36 +00009940 // Verify this is a simple bswap.
9941 if (CI->getNumOperands() != 2 ||
Eric Christopher551754c2010-04-16 23:37:20 +00009942 CI->getType() != CI->getOperand(1)->getType() ||
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009943 !CI->getType()->isIntegerTy())
Chris Lattnerb8105652009-07-20 17:51:36 +00009944 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009945
Chris Lattnerb8105652009-07-20 17:51:36 +00009946 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
9947 if (!Ty || Ty->getBitWidth() % 16 != 0)
9948 return false;
Eric Christopherfd179292009-08-27 18:07:15 +00009949
Chris Lattnerb8105652009-07-20 17:51:36 +00009950 // Okay, we can do this xform, do so now.
9951 const Type *Tys[] = { Ty };
9952 Module *M = CI->getParent()->getParent()->getParent();
9953 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
Eric Christopherfd179292009-08-27 18:07:15 +00009954
Eric Christopher551754c2010-04-16 23:37:20 +00009955 Value *Op = CI->getOperand(1);
Chris Lattnerb8105652009-07-20 17:51:36 +00009956 Op = CallInst::Create(Int, Op, CI->getName(), CI);
Eric Christopherfd179292009-08-27 18:07:15 +00009957
Chris Lattnerb8105652009-07-20 17:51:36 +00009958 CI->replaceAllUsesWith(Op);
9959 CI->eraseFromParent();
9960 return true;
9961}
9962
9963bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
9964 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
9965 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
9966
9967 std::string AsmStr = IA->getAsmString();
9968
9969 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
Benjamin Kramerd4f19592010-01-11 18:03:24 +00009970 SmallVector<StringRef, 4> AsmPieces;
Chris Lattnerb8105652009-07-20 17:51:36 +00009971 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
9972
9973 switch (AsmPieces.size()) {
9974 default: return false;
9975 case 1:
9976 AsmStr = AsmPieces[0];
9977 AsmPieces.clear();
9978 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
9979
9980 // bswap $0
9981 if (AsmPieces.size() == 2 &&
9982 (AsmPieces[0] == "bswap" ||
9983 AsmPieces[0] == "bswapq" ||
9984 AsmPieces[0] == "bswapl") &&
9985 (AsmPieces[1] == "$0" ||
9986 AsmPieces[1] == "${0:q}")) {
9987 // No need to check constraints, nothing other than the equivalent of
9988 // "=r,0" would be valid here.
9989 return LowerToBSwap(CI);
9990 }
9991 // rorw $$8, ${0:w} --> llvm.bswap.i16
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00009992 if (CI->getType()->isIntegerTy(16) &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009993 AsmPieces.size() == 3 &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009994 (AsmPieces[0] == "rorw" || AsmPieces[0] == "rolw") &&
Chris Lattnerb8105652009-07-20 17:51:36 +00009995 AsmPieces[1] == "$$8," &&
9996 AsmPieces[2] == "${0:w}" &&
Dan Gohman0ef701e2010-03-04 19:58:08 +00009997 IA->getConstraintString().compare(0, 5, "=r,0,") == 0) {
9998 AsmPieces.clear();
Benjamin Kramer018cbd52010-03-12 13:54:59 +00009999 const std::string &Constraints = IA->getConstraintString();
10000 SplitString(StringRef(Constraints).substr(5), AsmPieces, ",");
Dan Gohman0ef701e2010-03-04 19:58:08 +000010001 std::sort(AsmPieces.begin(), AsmPieces.end());
10002 if (AsmPieces.size() == 4 &&
10003 AsmPieces[0] == "~{cc}" &&
10004 AsmPieces[1] == "~{dirflag}" &&
10005 AsmPieces[2] == "~{flags}" &&
10006 AsmPieces[3] == "~{fpsr}") {
10007 return LowerToBSwap(CI);
10008 }
Chris Lattnerb8105652009-07-20 17:51:36 +000010009 }
10010 break;
10011 case 3:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000010012 if (CI->getType()->isIntegerTy(64) &&
Owen Anderson1d0be152009-08-13 21:58:54 +000010013 Constraints.size() >= 2 &&
Chris Lattnerb8105652009-07-20 17:51:36 +000010014 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
10015 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
10016 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
Benjamin Kramerd4f19592010-01-11 18:03:24 +000010017 SmallVector<StringRef, 4> Words;
Chris Lattnerb8105652009-07-20 17:51:36 +000010018 SplitString(AsmPieces[0], Words, " \t");
10019 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
10020 Words.clear();
10021 SplitString(AsmPieces[1], Words, " \t");
10022 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
10023 Words.clear();
10024 SplitString(AsmPieces[2], Words, " \t,");
10025 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
10026 Words[2] == "%edx") {
10027 return LowerToBSwap(CI);
10028 }
10029 }
10030 }
10031 }
10032 break;
10033 }
10034 return false;
10035}
10036
10037
10038
Chris Lattnerf4dff842006-07-11 02:54:03 +000010039/// getConstraintType - Given a constraint letter, return the type of
10040/// constraint it is for this target.
10041X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +000010042X86TargetLowering::getConstraintType(const std::string &Constraint) const {
10043 if (Constraint.size() == 1) {
10044 switch (Constraint[0]) {
10045 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +000010046 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010047 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +000010048 case 'r':
10049 case 'R':
10050 case 'l':
10051 case 'q':
10052 case 'Q':
10053 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +000010054 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +000010055 case 'Y':
10056 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010057 case 'e':
10058 case 'Z':
10059 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +000010060 default:
10061 break;
10062 }
Chris Lattnerf4dff842006-07-11 02:54:03 +000010063 }
Chris Lattner4234f572007-03-25 02:14:49 +000010064 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +000010065}
10066
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010067/// LowerXConstraint - try to replace an X constraint, which matches anything,
10068/// with another that has more specific requirements based on the type of the
10069/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +000010070const char *X86TargetLowering::
Owen Andersone50ed302009-08-10 22:56:29 +000010071LowerXConstraint(EVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +000010072 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
10073 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +000010074 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010075 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +000010076 return "Y";
10077 if (Subtarget->hasSSE1())
10078 return "x";
10079 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010080
Chris Lattner5e764232008-04-26 23:02:14 +000010081 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +000010082}
10083
Chris Lattner48884cd2007-08-25 00:47:38 +000010084/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
10085/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +000010086void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +000010087 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +000010088 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +000010089 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +000010090 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +000010091 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010092
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010093 switch (Constraint) {
10094 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +000010095 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +000010096 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010097 if (C->getZExtValue() <= 31) {
10098 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010099 break;
10100 }
Devang Patel84f7fd22007-03-17 00:13:28 +000010101 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010102 return;
Evan Cheng364091e2008-09-22 23:57:37 +000010103 case 'J':
10104 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010105 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +000010106 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10107 break;
10108 }
10109 }
10110 return;
10111 case 'K':
10112 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +000010113 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +000010114 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10115 break;
10116 }
10117 }
10118 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +000010119 case 'N':
10120 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010121 if (C->getZExtValue() <= 255) {
10122 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +000010123 break;
10124 }
Chris Lattner188b9fe2007-03-25 01:57:35 +000010125 }
Chris Lattner48884cd2007-08-25 00:47:38 +000010126 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +000010127 case 'e': {
10128 // 32-bit signed value
10129 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10130 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010131 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10132 C->getSExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010133 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010134 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
Dale Johannesen78e3e522009-02-12 20:58:09 +000010135 break;
10136 }
10137 // FIXME gcc accepts some relocatable values here too, but only in certain
10138 // memory models; it's complicated.
10139 }
10140 return;
10141 }
10142 case 'Z': {
10143 // 32-bit unsigned value
10144 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
10145 const ConstantInt *CI = C->getConstantIntValue();
Owen Anderson1d0be152009-08-13 21:58:54 +000010146 if (CI->isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
10147 C->getZExtValue())) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010148 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
10149 break;
10150 }
10151 }
10152 // FIXME gcc accepts some relocatable values here too, but only in certain
10153 // memory models; it's complicated.
10154 return;
10155 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010156 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010157 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +000010158 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +000010159 // Widen to 64 bits here to get it sign extended.
Owen Anderson825b72b2009-08-11 20:47:22 +000010160 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +000010161 break;
10162 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010163
Chris Lattnerdc43a882007-05-03 16:52:29 +000010164 // If we are in non-pic codegen mode, we allow the address of a global (with
10165 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +000010166 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010167 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010168
Chris Lattner49921962009-05-08 18:23:14 +000010169 // Match either (GA), (GA+C), (GA+C1+C2), etc.
10170 while (1) {
10171 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
10172 Offset += GA->getOffset();
10173 break;
10174 } else if (Op.getOpcode() == ISD::ADD) {
10175 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10176 Offset += C->getZExtValue();
10177 Op = Op.getOperand(0);
10178 continue;
10179 }
10180 } else if (Op.getOpcode() == ISD::SUB) {
10181 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
10182 Offset += -C->getZExtValue();
10183 Op = Op.getOperand(0);
10184 continue;
10185 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010186 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010187
Chris Lattner49921962009-05-08 18:23:14 +000010188 // Otherwise, this isn't something we can handle, reject it.
10189 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +000010190 }
Eric Christopherfd179292009-08-27 18:07:15 +000010191
Dan Gohman46510a72010-04-15 01:51:59 +000010192 const GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010193 // If we require an extra load to get this address, as in PIC mode, we
10194 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +000010195 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
10196 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +000010197 return;
Scott Michelfdc40a02009-02-17 22:15:04 +000010198
Dale Johannesen60b3ba02009-07-21 00:12:29 +000010199 if (hasMemory)
10200 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
10201 else
10202 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +000010203 Result = Op;
10204 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010205 }
Chris Lattnerdc43a882007-05-03 16:52:29 +000010206 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010207
Gabor Greifba36cb52008-08-28 21:40:38 +000010208 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +000010209 Ops.push_back(Result);
10210 return;
10211 }
Evan Chengda43bcf2008-09-24 00:05:32 +000010212 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
10213 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +000010214}
10215
Chris Lattner259e97c2006-01-31 19:43:35 +000010216std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +000010217getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010218 EVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +000010219 if (Constraint.size() == 1) {
10220 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +000010221 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +000010222 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +000010223 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
10224 if (Subtarget->is64Bit()) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010225 if (VT == MVT::i32)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010226 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
10227 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
10228 X86::R10D,X86::R11D,X86::R12D,
10229 X86::R13D,X86::R14D,X86::R15D,
10230 X86::EBP, X86::ESP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010231 else if (VT == MVT::i16)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010232 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
10233 X86::SI, X86::DI, X86::R8W,X86::R9W,
10234 X86::R10W,X86::R11W,X86::R12W,
10235 X86::R13W,X86::R14W,X86::R15W,
10236 X86::BP, X86::SP, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010237 else if (VT == MVT::i8)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010238 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
10239 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
10240 X86::R10B,X86::R11B,X86::R12B,
10241 X86::R13B,X86::R14B,X86::R15B,
10242 X86::BPL, X86::SPL, 0);
10243
Owen Anderson825b72b2009-08-11 20:47:22 +000010244 else if (VT == MVT::i64)
Evan Cheng47e9fab2009-07-17 22:13:25 +000010245 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
10246 X86::RSI, X86::RDI, X86::R8, X86::R9,
10247 X86::R10, X86::R11, X86::R12,
10248 X86::R13, X86::R14, X86::R15,
10249 X86::RBP, X86::RSP, 0);
10250
10251 break;
10252 }
Eric Christopherfd179292009-08-27 18:07:15 +000010253 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +000010254 case 'Q': // Q_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010255 if (VT == MVT::i32)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010256 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010257 else if (VT == MVT::i16)
Chris Lattner80a7ecc2006-05-06 00:29:37 +000010258 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010259 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +000010260 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Owen Anderson825b72b2009-08-11 20:47:22 +000010261 else if (VT == MVT::i64)
Chris Lattner03e6c702007-11-04 06:51:12 +000010262 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
10263 break;
Chris Lattner259e97c2006-01-31 19:43:35 +000010264 }
10265 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010266
Chris Lattner1efa40f2006-02-22 00:56:39 +000010267 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +000010268}
Chris Lattnerf76d1802006-07-31 23:26:50 +000010269
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010270std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +000010271X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +000010272 EVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +000010273 // First, see if this is a constraint that directly corresponds to an LLVM
10274 // register class.
10275 if (Constraint.size() == 1) {
10276 // GCC Constraint Letters
10277 switch (Constraint[0]) {
10278 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +000010279 case 'r': // GENERAL_REGS
Chris Lattner0f65cad2007-04-09 05:49:22 +000010280 case 'l': // INDEX_REGS
Owen Anderson825b72b2009-08-11 20:47:22 +000010281 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +000010282 return std::make_pair(0U, X86::GR8RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010283 if (VT == MVT::i16)
Chris Lattner1fa71982008-10-17 18:15:05 +000010284 return std::make_pair(0U, X86::GR16RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010285 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +000010286 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +000010287 return std::make_pair(0U, X86::GR64RegisterClass);
Dale Johannesen5f3663e2009-10-07 22:47:20 +000010288 case 'R': // LEGACY_REGS
10289 if (VT == MVT::i8)
10290 return std::make_pair(0U, X86::GR8_NOREXRegisterClass);
10291 if (VT == MVT::i16)
10292 return std::make_pair(0U, X86::GR16_NOREXRegisterClass);
10293 if (VT == MVT::i32 || !Subtarget->is64Bit())
10294 return std::make_pair(0U, X86::GR32_NOREXRegisterClass);
10295 return std::make_pair(0U, X86::GR64_NOREXRegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010296 case 'f': // FP Stack registers.
10297 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
10298 // value to the correct fpstack register class.
Owen Anderson825b72b2009-08-11 20:47:22 +000010299 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010300 return std::make_pair(0U, X86::RFP32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010301 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
Chris Lattnerfce84ac2008-03-11 19:06:29 +000010302 return std::make_pair(0U, X86::RFP64RegisterClass);
10303 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +000010304 case 'y': // MMX_REGS if MMX allowed.
10305 if (!Subtarget->hasMMX()) break;
10306 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010307 case 'Y': // SSE_REGS if SSE2 allowed
10308 if (!Subtarget->hasSSE2()) break;
10309 // FALL THROUGH.
10310 case 'x': // SSE_REGS if SSE1 allowed
10311 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +000010312
Owen Anderson825b72b2009-08-11 20:47:22 +000010313 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0f65cad2007-04-09 05:49:22 +000010314 default: break;
10315 // Scalar SSE types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010316 case MVT::f32:
10317 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +000010318 return std::make_pair(0U, X86::FR32RegisterClass);
Owen Anderson825b72b2009-08-11 20:47:22 +000010319 case MVT::f64:
10320 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +000010321 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +000010322 // Vector types.
Owen Anderson825b72b2009-08-11 20:47:22 +000010323 case MVT::v16i8:
10324 case MVT::v8i16:
10325 case MVT::v4i32:
10326 case MVT::v2i64:
10327 case MVT::v4f32:
10328 case MVT::v2f64:
Chris Lattner0f65cad2007-04-09 05:49:22 +000010329 return std::make_pair(0U, X86::VR128RegisterClass);
10330 }
Chris Lattnerad043e82007-04-09 05:11:28 +000010331 break;
10332 }
10333 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010334
Chris Lattnerf76d1802006-07-31 23:26:50 +000010335 // Use the default implementation in TargetLowering to convert the register
10336 // constraint into a member of a register class.
10337 std::pair<unsigned, const TargetRegisterClass*> Res;
10338 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +000010339
10340 // Not found as a standard register?
10341 if (Res.second == 0) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010342 // Map st(0) -> st(7) -> ST0
10343 if (Constraint.size() == 7 && Constraint[0] == '{' &&
10344 tolower(Constraint[1]) == 's' &&
10345 tolower(Constraint[2]) == 't' &&
10346 Constraint[3] == '(' &&
10347 (Constraint[4] >= '0' && Constraint[4] <= '7') &&
10348 Constraint[5] == ')' &&
10349 Constraint[6] == '}') {
Daniel Dunbara279bc32009-09-20 02:20:51 +000010350
Chris Lattner56d77c72009-09-13 22:41:48 +000010351 Res.first = X86::ST0+Constraint[4]-'0';
10352 Res.second = X86::RFP80RegisterClass;
10353 return Res;
10354 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010355
Chris Lattner56d77c72009-09-13 22:41:48 +000010356 // GCC allows "st(0)" to be called just plain "st".
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010357 if (StringRef("{st}").equals_lower(Constraint)) {
Chris Lattner1a60aa72006-10-31 19:42:44 +000010358 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +000010359 Res.second = X86::RFP80RegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010360 return Res;
Chris Lattner1a60aa72006-10-31 19:42:44 +000010361 }
Chris Lattner56d77c72009-09-13 22:41:48 +000010362
10363 // flags -> EFLAGS
Benjamin Kramer05872ea2009-11-12 20:36:59 +000010364 if (StringRef("{flags}").equals_lower(Constraint)) {
Chris Lattner56d77c72009-09-13 22:41:48 +000010365 Res.first = X86::EFLAGS;
10366 Res.second = X86::CCRRegisterClass;
10367 return Res;
10368 }
Daniel Dunbara279bc32009-09-20 02:20:51 +000010369
Dale Johannesen330169f2008-11-13 21:52:36 +000010370 // 'A' means EAX + EDX.
10371 if (Constraint == "A") {
10372 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +000010373 Res.second = X86::GR32_ADRegisterClass;
Chris Lattner56d77c72009-09-13 22:41:48 +000010374 return Res;
Dale Johannesen330169f2008-11-13 21:52:36 +000010375 }
Chris Lattner1a60aa72006-10-31 19:42:44 +000010376 return Res;
10377 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010378
Chris Lattnerf76d1802006-07-31 23:26:50 +000010379 // Otherwise, check to see if this is a register class of the wrong value
10380 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
10381 // turn into {ax},{dx}.
10382 if (Res.second->hasType(VT))
10383 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010384
Chris Lattnerf76d1802006-07-31 23:26:50 +000010385 // All of the single-register GCC register classes map their values onto
10386 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
10387 // really want an 8-bit or 32-bit register, map to the appropriate register
10388 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +000010389 if (Res.second == X86::GR16RegisterClass) {
Owen Anderson825b72b2009-08-11 20:47:22 +000010390 if (VT == MVT::i8) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010391 unsigned DestReg = 0;
10392 switch (Res.first) {
10393 default: break;
10394 case X86::AX: DestReg = X86::AL; break;
10395 case X86::DX: DestReg = X86::DL; break;
10396 case X86::CX: DestReg = X86::CL; break;
10397 case X86::BX: DestReg = X86::BL; break;
10398 }
10399 if (DestReg) {
10400 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010401 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010402 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010403 } else if (VT == MVT::i32) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010404 unsigned DestReg = 0;
10405 switch (Res.first) {
10406 default: break;
10407 case X86::AX: DestReg = X86::EAX; break;
10408 case X86::DX: DestReg = X86::EDX; break;
10409 case X86::CX: DestReg = X86::ECX; break;
10410 case X86::BX: DestReg = X86::EBX; break;
10411 case X86::SI: DestReg = X86::ESI; break;
10412 case X86::DI: DestReg = X86::EDI; break;
10413 case X86::BP: DestReg = X86::EBP; break;
10414 case X86::SP: DestReg = X86::ESP; break;
10415 }
10416 if (DestReg) {
10417 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010418 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010419 }
Owen Anderson825b72b2009-08-11 20:47:22 +000010420 } else if (VT == MVT::i64) {
Chris Lattner6ba50a92008-08-26 06:19:02 +000010421 unsigned DestReg = 0;
10422 switch (Res.first) {
10423 default: break;
10424 case X86::AX: DestReg = X86::RAX; break;
10425 case X86::DX: DestReg = X86::RDX; break;
10426 case X86::CX: DestReg = X86::RCX; break;
10427 case X86::BX: DestReg = X86::RBX; break;
10428 case X86::SI: DestReg = X86::RSI; break;
10429 case X86::DI: DestReg = X86::RDI; break;
10430 case X86::BP: DestReg = X86::RBP; break;
10431 case X86::SP: DestReg = X86::RSP; break;
10432 }
10433 if (DestReg) {
10434 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +000010435 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +000010436 }
Chris Lattnerf76d1802006-07-31 23:26:50 +000010437 }
Chris Lattner6ba50a92008-08-26 06:19:02 +000010438 } else if (Res.second == X86::FR32RegisterClass ||
10439 Res.second == X86::FR64RegisterClass ||
10440 Res.second == X86::VR128RegisterClass) {
10441 // Handle references to XMM physical registers that got mapped into the
10442 // wrong class. This can happen with constraints like {xmm0} where the
10443 // target independent register mapper will just pick the first match it can
10444 // find, ignoring the required type.
Owen Anderson825b72b2009-08-11 20:47:22 +000010445 if (VT == MVT::f32)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010446 Res.second = X86::FR32RegisterClass;
Owen Anderson825b72b2009-08-11 20:47:22 +000010447 else if (VT == MVT::f64)
Chris Lattner6ba50a92008-08-26 06:19:02 +000010448 Res.second = X86::FR64RegisterClass;
10449 else if (X86::VR128RegisterClass->hasType(VT))
10450 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +000010451 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000010452
Chris Lattnerf76d1802006-07-31 23:26:50 +000010453 return Res;
10454}