| Arnold Schwaighofer | 92226dd | 2007-10-12 21:53:12 +0000 | [diff] [blame] | 1 | //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===// |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the interfaces that X86 uses to lower LLVM code into a |
| 11 | // selection DAG. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| Evan Cheng | b171245 | 2010-01-27 06:25:16 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "x86-isel" |
| Craig Topper | 1bf724b | 2012-02-19 07:15:48 +0000 | [diff] [blame] | 16 | #include "X86ISelLowering.h" |
| Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "Utils/X86ShuffleDecode.h" |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 18 | #include "X86.h" |
| Evan Cheng | 0cc3945 | 2006-01-16 21:21:29 +0000 | [diff] [blame] | 19 | #include "X86InstrBuilder.h" |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 20 | #include "X86TargetMachine.h" |
| Chris Lattner | 8c6ed05 | 2009-09-16 01:46:41 +0000 | [diff] [blame] | 21 | #include "X86TargetObjectFile.h" |
| Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallSet.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/ADT/StringExtras.h" |
| 25 | #include "llvm/ADT/VariadicFunction.h" |
| Evan Cheng | 55d4200 | 2011-01-08 01:24:27 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/IntrinsicLowering.h" |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunction.h" |
| 29 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Chris Lattner | 5e1df8d | 2010-01-25 23:38:14 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
| Evan Cheng | a844bde | 2008-02-02 04:07:54 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| Chris Lattner | 84bc542 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/CallingConv.h" |
| 34 | #include "llvm/IR/Constants.h" |
| 35 | #include "llvm/IR/DerivedTypes.h" |
| 36 | #include "llvm/IR/Function.h" |
| 37 | #include "llvm/IR/GlobalAlias.h" |
| 38 | #include "llvm/IR/GlobalVariable.h" |
| 39 | #include "llvm/IR/Instructions.h" |
| 40 | #include "llvm/IR/Intrinsics.h" |
| 41 | #include "llvm/IR/LLVMContext.h" |
| Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 42 | #include "llvm/MC/MCAsmInfo.h" |
| Chris Lattner | c64daab | 2010-01-26 05:02:42 +0000 | [diff] [blame] | 43 | #include "llvm/MC/MCContext.h" |
| Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 44 | #include "llvm/MC/MCExpr.h" |
| Chris Lattner | c64daab | 2010-01-26 05:02:42 +0000 | [diff] [blame] | 45 | #include "llvm/MC/MCSymbol.h" |
| Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CallSite.h" |
| Chris Lattner | c64daab | 2010-01-26 05:02:42 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
| 48 | #include "llvm/Support/ErrorHandling.h" |
| 49 | #include "llvm/Support/MathExtras.h" |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 50 | #include "llvm/Target/TargetOptions.h" |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 51 | #include <bitset> |
| Joerg Sonnenberger | 78cab94 | 2012-08-10 10:53:56 +0000 | [diff] [blame] | 52 | #include <cctype> |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 53 | using namespace llvm; |
| 54 | |
| Evan Cheng | b171245 | 2010-01-27 06:25:16 +0000 | [diff] [blame] | 55 | STATISTIC(NumTailCalls, "Number of tail calls"); |
| 56 | |
| Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 57 | // Forward declarations. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 58 | static SDValue getMOVL(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1, |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 59 | SDValue V2); |
| Evan Cheng | 10e8642 | 2008-04-25 19:11:04 +0000 | [diff] [blame] | 60 | |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 61 | /// Generate a DAG to grab 128-bits from a vector > 128 bits. This |
| 62 | /// sets things up to match to an AVX VEXTRACTF128 instruction or a |
| David Greene | 74a579d | 2011-02-10 16:57:36 +0000 | [diff] [blame] | 63 | /// simple subregister reference. Idx is an index in the 128 bits we |
| 64 | /// want. It need not be aligned to a 128-bit bounday. That makes |
| 65 | /// lowering EXTRACT_VECTOR_ELT operations easier. |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 66 | static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 67 | SelectionDAG &DAG, SDLoc dl) { |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 68 | EVT VT = Vec.getValueType(); |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 69 | assert(VT.is256BitVector() && "Unexpected vector size!"); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 70 | EVT ElVT = VT.getVectorElementType(); |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 71 | unsigned Factor = VT.getSizeInBits()/128; |
| Bruno Cardoso Lopes | 67727ca | 2011-07-21 01:55:27 +0000 | [diff] [blame] | 72 | EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT, |
| 73 | VT.getVectorNumElements()/Factor); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 74 | |
| 75 | // Extract from UNDEF is UNDEF. |
| 76 | if (Vec.getOpcode() == ISD::UNDEF) |
| Craig Topper | 767b4f6 | 2012-04-22 19:29:34 +0000 | [diff] [blame] | 77 | return DAG.getUNDEF(ResultVT); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 78 | |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 79 | // Extract the relevant 128 bits. Generate an EXTRACT_SUBVECTOR |
| 80 | // we can match to VEXTRACTF128. |
| 81 | unsigned ElemsPerChunk = 128 / ElVT.getSizeInBits(); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 82 | |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 83 | // This is the index of the first element of the 128-bit chunk |
| 84 | // we want. |
| 85 | unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits()) / 128) |
| 86 | * ElemsPerChunk); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 87 | |
| Benjamin Kramer | 02c2ecf | 2013-03-07 18:48:40 +0000 | [diff] [blame] | 88 | // If the input is a buildvector just emit a smaller one. |
| 89 | if (Vec.getOpcode() == ISD::BUILD_VECTOR) |
| 90 | return DAG.getNode(ISD::BUILD_VECTOR, dl, ResultVT, |
| 91 | Vec->op_begin()+NormalizedIdxVal, ElemsPerChunk); |
| 92 | |
| Craig Topper | b8d9da1 | 2012-09-06 06:09:01 +0000 | [diff] [blame] | 93 | SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal); |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 94 | SDValue Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec, |
| 95 | VecIdx); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 96 | |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 97 | return Result; |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /// Generate a DAG to put 128-bits into a vector > 128 bits. This |
| 101 | /// sets things up to match to an AVX VINSERTF128 instruction or a |
| David Greene | 6b38126 | 2011-02-09 15:32:06 +0000 | [diff] [blame] | 102 | /// simple superregister reference. Idx is an index in the 128 bits |
| 103 | /// we want. It need not be aligned to a 128-bit bounday. That makes |
| 104 | /// lowering INSERT_VECTOR_ELT operations easier. |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 105 | static SDValue Insert128BitVector(SDValue Result, SDValue Vec, |
| 106 | unsigned IdxVal, SelectionDAG &DAG, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 107 | SDLoc dl) { |
| Craig Topper | 703c38b | 2012-06-20 05:39:26 +0000 | [diff] [blame] | 108 | // Inserting UNDEF is Result |
| 109 | if (Vec.getOpcode() == ISD::UNDEF) |
| 110 | return Result; |
| 111 | |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 112 | EVT VT = Vec.getValueType(); |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 113 | assert(VT.is128BitVector() && "Unexpected vector size!"); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 114 | |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 115 | EVT ElVT = VT.getVectorElementType(); |
| 116 | EVT ResultVT = Result.getValueType(); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 117 | |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 118 | // Insert the relevant 128 bits. |
| 119 | unsigned ElemsPerChunk = 128/ElVT.getSizeInBits(); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 120 | |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 121 | // This is the index of the first element of the 128-bit chunk |
| 122 | // we want. |
| 123 | unsigned NormalizedIdxVal = (((IdxVal * ElVT.getSizeInBits())/128) |
| 124 | * ElemsPerChunk); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 125 | |
| Craig Topper | b8d9da1 | 2012-09-06 06:09:01 +0000 | [diff] [blame] | 126 | SDValue VecIdx = DAG.getIntPtrConstant(NormalizedIdxVal); |
| Craig Topper | 703c38b | 2012-06-20 05:39:26 +0000 | [diff] [blame] | 127 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec, |
| 128 | VecIdx); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| Craig Topper | 4c7972d | 2012-04-22 18:15:59 +0000 | [diff] [blame] | 131 | /// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128 |
| 132 | /// instructions. This is used because creating CONCAT_VECTOR nodes of |
| 133 | /// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower |
| 134 | /// large BUILD_VECTORS. |
| 135 | static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT, |
| 136 | unsigned NumElems, SelectionDAG &DAG, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 137 | SDLoc dl) { |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 138 | SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl); |
| 139 | return Insert128BitVector(V, V2, NumElems/2, DAG, dl); |
| Craig Topper | 4c7972d | 2012-04-22 18:15:59 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 142 | static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) { |
| Evan Cheng | 2bffee2 | 2011-02-01 01:14:13 +0000 | [diff] [blame] | 143 | const X86Subtarget *Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| 144 | bool is64Bit = Subtarget->is64Bit(); |
| NAKAMURA Takumi | 2763538 | 2011-02-05 15:10:54 +0000 | [diff] [blame] | 145 | |
| Evan Cheng | 2bffee2 | 2011-02-01 01:14:13 +0000 | [diff] [blame] | 146 | if (Subtarget->isTargetEnvMacho()) { |
| Chris Lattner | e019ec1 | 2010-12-19 20:07:10 +0000 | [diff] [blame] | 147 | if (is64Bit) |
| Bill Wendling | a44489d | 2012-06-26 10:05:06 +0000 | [diff] [blame] | 148 | return new X86_64MachoTargetObjectFile(); |
| Anton Korobeynikov | 293d592 | 2010-02-21 20:28:15 +0000 | [diff] [blame] | 149 | return new TargetLoweringObjectFileMachO(); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 150 | } |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 151 | |
| Rafael Espindola | d6b43a3 | 2012-06-19 00:48:28 +0000 | [diff] [blame] | 152 | if (Subtarget->isTargetLinux()) |
| 153 | return new X86LinuxTargetObjectFile(); |
| Evan Cheng | 203576a | 2011-07-20 19:50:42 +0000 | [diff] [blame] | 154 | if (Subtarget->isTargetELF()) |
| 155 | return new TargetLoweringObjectFileELF(); |
| Evan Cheng | 2bffee2 | 2011-02-01 01:14:13 +0000 | [diff] [blame] | 156 | if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho()) |
| Chris Lattner | e019ec1 | 2010-12-19 20:07:10 +0000 | [diff] [blame] | 157 | return new TargetLoweringObjectFileCOFF(); |
| Eric Christopher | 62f35a2 | 2010-07-05 19:26:33 +0000 | [diff] [blame] | 158 | llvm_unreachable("unknown subtarget type"); |
| Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| Dan Gohman | c9f5f3f | 2008-05-14 01:58:56 +0000 | [diff] [blame] | 161 | X86TargetLowering::X86TargetLowering(X86TargetMachine &TM) |
| Chris Lattner | f014412 | 2009-07-28 03:13:23 +0000 | [diff] [blame] | 162 | : TargetLowering(TM, createTLOF(TM)) { |
| Evan Cheng | 559806f | 2006-01-27 08:10:46 +0000 | [diff] [blame] | 163 | Subtarget = &TM.getSubtarget<X86Subtarget>(); |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 164 | X86ScalarSSEf64 = Subtarget->hasSSE2(); |
| 165 | X86ScalarSSEf32 = Subtarget->hasSSE1(); |
| Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 166 | TD = getDataLayout(); |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 167 | |
| Bill Wendling | 13bbe1f | 2013-04-05 21:52:40 +0000 | [diff] [blame] | 168 | resetOperationActions(); |
| 169 | } |
| 170 | |
| 171 | void X86TargetLowering::resetOperationActions() { |
| 172 | const TargetMachine &TM = getTargetMachine(); |
| 173 | static bool FirstTimeThrough = true; |
| 174 | |
| 175 | // If none of the target options have changed, then we don't need to reset the |
| 176 | // operation actions. |
| 177 | if (!FirstTimeThrough && TO == TM.Options) return; |
| 178 | |
| 179 | if (!FirstTimeThrough) { |
| 180 | // Reinitialize the actions. |
| 181 | initActions(); |
| 182 | FirstTimeThrough = false; |
| 183 | } |
| 184 | |
| 185 | TO = TM.Options; |
| 186 | |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 187 | // Set up the TargetLowering object. |
| Craig Topper | 9e401f2 | 2012-04-21 18:58:38 +0000 | [diff] [blame] | 188 | static const MVT IntVTs[] = { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }; |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 189 | |
| 190 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
| Duncan Sands | 0322808 | 2008-11-23 15:47:28 +0000 | [diff] [blame] | 191 | setBooleanContents(ZeroOrOneBooleanContent); |
| Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 192 | // X86-SSE is even stranger. It uses -1 or 0 for vector masks. |
| 193 | setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 194 | |
| Eric Christopher | de5e101 | 2011-03-11 01:05:58 +0000 | [diff] [blame] | 195 | // For 64-bit since we have so many registers use the ILP scheduler, for |
| 196 | // 32-bit code use the register pressure specific scheduling. |
| Preston Gurd | c0f0a93 | 2012-05-02 22:02:02 +0000 | [diff] [blame] | 197 | // For Atom, always use ILP scheduling. |
| Chad Rosier | a20e1e7 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 198 | if (Subtarget->isAtom()) |
| Eric Christopher | de5e101 | 2011-03-11 01:05:58 +0000 | [diff] [blame] | 199 | setSchedulingPreference(Sched::ILP); |
| Preston Gurd | c0f0a93 | 2012-05-02 22:02:02 +0000 | [diff] [blame] | 200 | else if (Subtarget->is64Bit()) |
| 201 | setSchedulingPreference(Sched::ILP); |
| Eric Christopher | de5e101 | 2011-03-11 01:05:58 +0000 | [diff] [blame] | 202 | else |
| 203 | setSchedulingPreference(Sched::RegPressure); |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 204 | const X86RegisterInfo *RegInfo = |
| 205 | static_cast<const X86RegisterInfo*>(TM.getRegisterInfo()); |
| Michael Liao | c5c970e | 2012-10-31 04:14:09 +0000 | [diff] [blame] | 206 | setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister()); |
| Evan Cheng | 714554d | 2006-03-16 21:47:42 +0000 | [diff] [blame] | 207 | |
| Preston Gurd | 9a2cfff | 2013-03-04 18:13:57 +0000 | [diff] [blame] | 208 | // Bypass expensive divides on Atom when compiling with O2 |
| 209 | if (Subtarget->hasSlowDivide() && TM.getOptLevel() >= CodeGenOpt::Default) { |
| Preston Gurd | 8d662b5 | 2012-10-04 21:33:40 +0000 | [diff] [blame] | 210 | addBypassSlowDiv(32, 8); |
| Preston Gurd | 9a2cfff | 2013-03-04 18:13:57 +0000 | [diff] [blame] | 211 | if (Subtarget->is64Bit()) |
| 212 | addBypassSlowDiv(64, 16); |
| 213 | } |
| Preston Gurd | 2e2efd9 | 2012-09-04 18:22:17 +0000 | [diff] [blame] | 214 | |
| Michael J. Spencer | 92bf38c | 2010-10-10 23:11:06 +0000 | [diff] [blame] | 215 | if (Subtarget->isTargetWindows() && !Subtarget->isTargetCygMing()) { |
| Michael J. Spencer | 1802a9f | 2010-10-10 22:04:34 +0000 | [diff] [blame] | 216 | // Setup Windows compiler runtime calls. |
| 217 | setLibcallName(RTLIB::SDIV_I64, "_alldiv"); |
| Michael J. Spencer | 335b806 | 2010-10-11 05:29:15 +0000 | [diff] [blame] | 218 | setLibcallName(RTLIB::UDIV_I64, "_aulldiv"); |
| Julien Lerouge | f296082 | 2011-07-08 21:40:25 +0000 | [diff] [blame] | 219 | setLibcallName(RTLIB::SREM_I64, "_allrem"); |
| 220 | setLibcallName(RTLIB::UREM_I64, "_aullrem"); |
| 221 | setLibcallName(RTLIB::MUL_I64, "_allmul"); |
| Michael J. Spencer | 1802a9f | 2010-10-10 22:04:34 +0000 | [diff] [blame] | 222 | setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall); |
| Michael J. Spencer | 335b806 | 2010-10-11 05:29:15 +0000 | [diff] [blame] | 223 | setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall); |
| Julien Lerouge | f296082 | 2011-07-08 21:40:25 +0000 | [diff] [blame] | 224 | setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall); |
| 225 | setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall); |
| 226 | setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall); |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 227 | |
| 228 | // The _ftol2 runtime function has an unusual calling conv, which |
| 229 | // is modeled by a special pseudo-instruction. |
| 230 | setLibcallName(RTLIB::FPTOUINT_F64_I64, 0); |
| 231 | setLibcallName(RTLIB::FPTOUINT_F32_I64, 0); |
| 232 | setLibcallName(RTLIB::FPTOUINT_F64_I32, 0); |
| 233 | setLibcallName(RTLIB::FPTOUINT_F32_I32, 0); |
| Michael J. Spencer | 1802a9f | 2010-10-10 22:04:34 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 236 | if (Subtarget->isTargetDarwin()) { |
| Evan Cheng | df57fa0 | 2006-03-17 20:31:41 +0000 | [diff] [blame] | 237 | // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp. |
| Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 238 | setUseUnderscoreSetJmp(false); |
| 239 | setUseUnderscoreLongJmp(false); |
| Anton Korobeynikov | 317848f | 2007-01-03 11:43:14 +0000 | [diff] [blame] | 240 | } else if (Subtarget->isTargetMingw()) { |
| Anton Korobeynikov | d27a258 | 2006-12-10 23:12:42 +0000 | [diff] [blame] | 241 | // MS runtime is weird: it exports _setjmp, but longjmp! |
| 242 | setUseUnderscoreSetJmp(true); |
| 243 | setUseUnderscoreLongJmp(false); |
| 244 | } else { |
| 245 | setUseUnderscoreSetJmp(true); |
| 246 | setUseUnderscoreLongJmp(true); |
| 247 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 248 | |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 249 | // Set up the register classes. |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 250 | addRegisterClass(MVT::i8, &X86::GR8RegClass); |
| 251 | addRegisterClass(MVT::i16, &X86::GR16RegClass); |
| 252 | addRegisterClass(MVT::i32, &X86::GR32RegClass); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 253 | if (Subtarget->is64Bit()) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 254 | addRegisterClass(MVT::i64, &X86::GR64RegClass); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 255 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 256 | setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); |
| Evan Cheng | c548428 | 2006-10-04 00:56:09 +0000 | [diff] [blame] | 257 | |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 258 | // We don't accept any truncstore of integer registers. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 259 | setTruncStoreAction(MVT::i64, MVT::i32, Expand); |
| Dan Gohman | 71edb24 | 2010-04-30 18:30:26 +0000 | [diff] [blame] | 260 | setTruncStoreAction(MVT::i64, MVT::i16, Expand); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 261 | setTruncStoreAction(MVT::i64, MVT::i8 , Expand); |
| Dan Gohman | 71edb24 | 2010-04-30 18:30:26 +0000 | [diff] [blame] | 262 | setTruncStoreAction(MVT::i32, MVT::i16, Expand); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 263 | setTruncStoreAction(MVT::i32, MVT::i8 , Expand); |
| 264 | setTruncStoreAction(MVT::i16, MVT::i8, Expand); |
| Evan Cheng | 7f04268 | 2008-10-15 02:05:31 +0000 | [diff] [blame] | 265 | |
| 266 | // SETOEQ and SETUNE require checking two conditions. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 267 | setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand); |
| 268 | setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand); |
| 269 | setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand); |
| 270 | setCondCodeAction(ISD::SETUNE, MVT::f32, Expand); |
| 271 | setCondCodeAction(ISD::SETUNE, MVT::f64, Expand); |
| 272 | setCondCodeAction(ISD::SETUNE, MVT::f80, Expand); |
| Chris Lattner | ddf8956 | 2008-01-17 19:59:44 +0000 | [diff] [blame] | 273 | |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 274 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 275 | // operation. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 276 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 277 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 278 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
| Evan Cheng | 6892f28 | 2006-01-17 02:32:49 +0000 | [diff] [blame] | 279 | |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 280 | if (Subtarget->is64Bit()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 281 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 282 | setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom); |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 283 | } else if (!TM.Options.UseSoftFloat) { |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 284 | // We have an algorithm for SSE2->double, and we turn this into a |
| 285 | // 64-bit FILD followed by conditional FADD for other targets. |
| 286 | setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom); |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 287 | // We have an algorithm for SSE2, and we turn this into a 64-bit |
| 288 | // FILD for other targets. |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 289 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 290 | } |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 291 | |
| 292 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 293 | // this operation. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 294 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 295 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
| Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 296 | |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 297 | if (!TM.Options.UseSoftFloat) { |
| Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 298 | // SSE has no i16 to fp conversion, only i32 |
| 299 | if (X86ScalarSSEf32) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 300 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
| Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 301 | // f32 and f64 cases are Legal, f80 case is not |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 302 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 303 | } else { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 304 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom); |
| 305 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom); |
| Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 306 | } |
| Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 307 | } else { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 308 | setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote); |
| 309 | setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote); |
| Evan Cheng | 5298bcc | 2006-02-17 07:01:52 +0000 | [diff] [blame] | 310 | } |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 311 | |
| Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 312 | // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64 |
| 313 | // are Legal, f80 is custom lowered. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 314 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 315 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
| Evan Cheng | 6dab053 | 2006-01-30 08:02:57 +0000 | [diff] [blame] | 316 | |
| Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 317 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 318 | // this operation. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 319 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 320 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 321 | |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 322 | if (X86ScalarSSEf32) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 323 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); |
| Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 324 | // f32 and f64 cases are Legal, f80 case is not |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 325 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
| Evan Cheng | 02568ff | 2006-01-30 22:13:22 +0000 | [diff] [blame] | 326 | } else { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 327 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); |
| 328 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 332 | // conversion. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 333 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 334 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 335 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 336 | |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 337 | if (Subtarget->is64Bit()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 338 | setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand); |
| 339 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 340 | } else if (!TM.Options.UseSoftFloat) { |
| Bruno Cardoso Lopes | 0c4b9ff | 2011-09-15 18:27:36 +0000 | [diff] [blame] | 341 | // Since AVX is a superset of SSE3, only check for SSE here. |
| 342 | if (Subtarget->hasSSE1() && !Subtarget->hasSSE3()) |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 343 | // Expand FP_TO_UINT into a select. |
| 344 | // FIXME: We would like to use a Custom expander here eventually to do |
| 345 | // the optimal thing for SSE vs. the default expansion in the legalizer. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 346 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 347 | else |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 348 | // With SSE3 we can use fisttpll to convert to a signed i64; without |
| 349 | // SSE, we're stuck with a fistpll. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 350 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 351 | } |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 352 | |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 353 | if (isTargetFTOL()) { |
| 354 | // Use the _ftol2 runtime function, which has a pseudo-instruction |
| 355 | // to handle its weird calling convention. |
| 356 | setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Custom); |
| 357 | } |
| 358 | |
| Chris Lattner | 399610a | 2006-12-05 18:22:22 +0000 | [diff] [blame] | 359 | // TODO: when we have SSE, these could be more efficient, by using movd/movq. |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 360 | if (!X86ScalarSSEf64) { |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 361 | setOperationAction(ISD::BITCAST , MVT::f32 , Expand); |
| 362 | setOperationAction(ISD::BITCAST , MVT::i32 , Expand); |
| Dale Johannesen | e39859a | 2010-05-21 18:40:15 +0000 | [diff] [blame] | 363 | if (Subtarget->is64Bit()) { |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 364 | setOperationAction(ISD::BITCAST , MVT::f64 , Expand); |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 365 | // Without SSE, i64->f64 goes through memory. |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 366 | setOperationAction(ISD::BITCAST , MVT::i64 , Expand); |
| Dale Johannesen | 7d07b48 | 2010-05-21 00:52:33 +0000 | [diff] [blame] | 367 | } |
| Chris Lattner | f3597a1 | 2006-12-05 18:45:06 +0000 | [diff] [blame] | 368 | } |
| Chris Lattner | 21f6685 | 2005-12-23 05:15:23 +0000 | [diff] [blame] | 369 | |
| Dan Gohman | b00ee21 | 2008-02-18 19:34:53 +0000 | [diff] [blame] | 370 | // Scalar integer divide and remainder are lowered to use operations that |
| 371 | // produce two results, to match the available instructions. This exposes |
| 372 | // the two-result form to trivial CSE, which is able to combine x/y and x%y |
| 373 | // into a single instruction. |
| 374 | // |
| 375 | // Scalar integer multiply-high is also lowered to use two-result |
| 376 | // operations, to match the available instructions. However, plain multiply |
| 377 | // (low) operations are left as Legal, as there are single-result |
| 378 | // instructions for this in x86. Using the two-result multiply instructions |
| 379 | // when both high and low results are needed must be arranged by dagcombine. |
| Craig Topper | 9e401f2 | 2012-04-21 18:58:38 +0000 | [diff] [blame] | 380 | for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) { |
| Chris Lattner | e019ec1 | 2010-12-19 20:07:10 +0000 | [diff] [blame] | 381 | MVT VT = IntVTs[i]; |
| 382 | setOperationAction(ISD::MULHS, VT, Expand); |
| 383 | setOperationAction(ISD::MULHU, VT, Expand); |
| 384 | setOperationAction(ISD::SDIV, VT, Expand); |
| 385 | setOperationAction(ISD::UDIV, VT, Expand); |
| 386 | setOperationAction(ISD::SREM, VT, Expand); |
| 387 | setOperationAction(ISD::UREM, VT, Expand); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 388 | |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 389 | // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences. |
| Chris Lattner | d8ff7ec | 2010-12-20 01:03:27 +0000 | [diff] [blame] | 390 | setOperationAction(ISD::ADDC, VT, Custom); |
| 391 | setOperationAction(ISD::ADDE, VT, Custom); |
| 392 | setOperationAction(ISD::SUBC, VT, Custom); |
| 393 | setOperationAction(ISD::SUBE, VT, Custom); |
| Chris Lattner | e019ec1 | 2010-12-19 20:07:10 +0000 | [diff] [blame] | 394 | } |
| Dan Gohman | a37c9f7 | 2007-09-25 18:23:27 +0000 | [diff] [blame] | 395 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 396 | setOperationAction(ISD::BR_JT , MVT::Other, Expand); |
| 397 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); |
| Tom Stellard | 3ef5383 | 2013-03-08 15:36:57 +0000 | [diff] [blame] | 398 | setOperationAction(ISD::BR_CC , MVT::f32, Expand); |
| 399 | setOperationAction(ISD::BR_CC , MVT::f64, Expand); |
| 400 | setOperationAction(ISD::BR_CC , MVT::f80, Expand); |
| 401 | setOperationAction(ISD::BR_CC , MVT::i8, Expand); |
| 402 | setOperationAction(ISD::BR_CC , MVT::i16, Expand); |
| 403 | setOperationAction(ISD::BR_CC , MVT::i32, Expand); |
| 404 | setOperationAction(ISD::BR_CC , MVT::i64, Expand); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 405 | setOperationAction(ISD::SELECT_CC , MVT::Other, Expand); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 406 | if (Subtarget->is64Bit()) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 407 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); |
| 408 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal); |
| 409 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal); |
| 410 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
| 411 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
| 412 | setOperationAction(ISD::FREM , MVT::f32 , Expand); |
| 413 | setOperationAction(ISD::FREM , MVT::f64 , Expand); |
| 414 | setOperationAction(ISD::FREM , MVT::f80 , Expand); |
| 415 | setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 416 | |
| Chandler Carruth | 7782102 | 2011-12-24 12:12:34 +0000 | [diff] [blame] | 417 | // Promote the i8 variants and force them on up to i32 which has a shorter |
| 418 | // encoding. |
| 419 | setOperationAction(ISD::CTTZ , MVT::i8 , Promote); |
| 420 | AddPromotedToType (ISD::CTTZ , MVT::i8 , MVT::i32); |
| 421 | setOperationAction(ISD::CTTZ_ZERO_UNDEF , MVT::i8 , Promote); |
| 422 | AddPromotedToType (ISD::CTTZ_ZERO_UNDEF , MVT::i8 , MVT::i32); |
| Craig Topper | 909652f | 2011-10-14 03:21:46 +0000 | [diff] [blame] | 423 | if (Subtarget->hasBMI()) { |
| Chandler Carruth | d873a4b | 2011-12-24 11:11:38 +0000 | [diff] [blame] | 424 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Expand); |
| 425 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Expand); |
| 426 | if (Subtarget->is64Bit()) |
| 427 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand); |
| Craig Topper | 909652f | 2011-10-14 03:21:46 +0000 | [diff] [blame] | 428 | } else { |
| Craig Topper | 909652f | 2011-10-14 03:21:46 +0000 | [diff] [blame] | 429 | setOperationAction(ISD::CTTZ , MVT::i16 , Custom); |
| 430 | setOperationAction(ISD::CTTZ , MVT::i32 , Custom); |
| 431 | if (Subtarget->is64Bit()) |
| 432 | setOperationAction(ISD::CTTZ , MVT::i64 , Custom); |
| 433 | } |
| Craig Topper | 37f2167 | 2011-10-11 06:44:02 +0000 | [diff] [blame] | 434 | |
| 435 | if (Subtarget->hasLZCNT()) { |
| Chandler Carruth | 7782102 | 2011-12-24 12:12:34 +0000 | [diff] [blame] | 436 | // When promoting the i8 variants, force them to i32 for a shorter |
| 437 | // encoding. |
| Craig Topper | 37f2167 | 2011-10-11 06:44:02 +0000 | [diff] [blame] | 438 | setOperationAction(ISD::CTLZ , MVT::i8 , Promote); |
| Chandler Carruth | 7782102 | 2011-12-24 12:12:34 +0000 | [diff] [blame] | 439 | AddPromotedToType (ISD::CTLZ , MVT::i8 , MVT::i32); |
| 440 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Promote); |
| 441 | AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32); |
| Chandler Carruth | acc068e | 2011-12-24 10:55:54 +0000 | [diff] [blame] | 442 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Expand); |
| 443 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Expand); |
| 444 | if (Subtarget->is64Bit()) |
| 445 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand); |
| Craig Topper | 37f2167 | 2011-10-11 06:44:02 +0000 | [diff] [blame] | 446 | } else { |
| 447 | setOperationAction(ISD::CTLZ , MVT::i8 , Custom); |
| 448 | setOperationAction(ISD::CTLZ , MVT::i16 , Custom); |
| 449 | setOperationAction(ISD::CTLZ , MVT::i32 , Custom); |
| Chandler Carruth | acc068e | 2011-12-24 10:55:54 +0000 | [diff] [blame] | 450 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , Custom); |
| 451 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16 , Custom); |
| 452 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32 , Custom); |
| 453 | if (Subtarget->is64Bit()) { |
| Craig Topper | 37f2167 | 2011-10-11 06:44:02 +0000 | [diff] [blame] | 454 | setOperationAction(ISD::CTLZ , MVT::i64 , Custom); |
| Chandler Carruth | acc068e | 2011-12-24 10:55:54 +0000 | [diff] [blame] | 455 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom); |
| 456 | } |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| Benjamin Kramer | 1292c22 | 2010-12-04 20:32:23 +0000 | [diff] [blame] | 459 | if (Subtarget->hasPOPCNT()) { |
| 460 | setOperationAction(ISD::CTPOP , MVT::i8 , Promote); |
| 461 | } else { |
| 462 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
| 463 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
| 464 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
| 465 | if (Subtarget->is64Bit()) |
| 466 | setOperationAction(ISD::CTPOP , MVT::i64 , Expand); |
| 467 | } |
| 468 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 469 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); |
| 470 | setOperationAction(ISD::BSWAP , MVT::i16 , Expand); |
| Nate Begeman | 35ef913 | 2006-01-11 21:21:00 +0000 | [diff] [blame] | 471 | |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 472 | // These should be promoted to a larger select which is supported. |
| Dan Gohman | cbbea0f | 2009-08-27 00:14:12 +0000 | [diff] [blame] | 473 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 474 | // X86 wants to expand cmov itself. |
| Dan Gohman | cbbea0f | 2009-08-27 00:14:12 +0000 | [diff] [blame] | 475 | setOperationAction(ISD::SELECT , MVT::i8 , Custom); |
| Chris Lattner | e019ec1 | 2010-12-19 20:07:10 +0000 | [diff] [blame] | 476 | setOperationAction(ISD::SELECT , MVT::i16 , Custom); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 477 | setOperationAction(ISD::SELECT , MVT::i32 , Custom); |
| 478 | setOperationAction(ISD::SELECT , MVT::f32 , Custom); |
| 479 | setOperationAction(ISD::SELECT , MVT::f64 , Custom); |
| 480 | setOperationAction(ISD::SELECT , MVT::f80 , Custom); |
| 481 | setOperationAction(ISD::SETCC , MVT::i8 , Custom); |
| Dan Gohman | 71edb24 | 2010-04-30 18:30:26 +0000 | [diff] [blame] | 482 | setOperationAction(ISD::SETCC , MVT::i16 , Custom); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 483 | setOperationAction(ISD::SETCC , MVT::i32 , Custom); |
| 484 | setOperationAction(ISD::SETCC , MVT::f32 , Custom); |
| 485 | setOperationAction(ISD::SETCC , MVT::f64 , Custom); |
| 486 | setOperationAction(ISD::SETCC , MVT::f80 , Custom); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 487 | if (Subtarget->is64Bit()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 488 | setOperationAction(ISD::SELECT , MVT::i64 , Custom); |
| Andrew Trick | f6c3941 | 2011-03-23 23:11:02 +0000 | [diff] [blame] | 489 | setOperationAction(ISD::SETCC , MVT::i64 , Custom); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 490 | } |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 491 | setOperationAction(ISD::EH_RETURN , MVT::Other, Custom); |
| Hal Finkel | e915047 | 2013-03-27 19:10:42 +0000 | [diff] [blame] | 492 | // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 493 | // SjLj exception handling but a light-weight setjmp/longjmp replacement to |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 494 | // support continuation, user-level threading, and etc.. As a result, no |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 495 | // other SjLj exception interfaces are implemented and please don't build |
| 496 | // your own exception handling based on them. |
| 497 | // LLVM/Clang supports zero-cost DWARF exception handling. |
| 498 | setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); |
| 499 | setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 500 | |
| Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 501 | // Darwin ABI issue. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 502 | setOperationAction(ISD::ConstantPool , MVT::i32 , Custom); |
| 503 | setOperationAction(ISD::JumpTable , MVT::i32 , Custom); |
| 504 | setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom); |
| 505 | setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom); |
| Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 506 | if (Subtarget->is64Bit()) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 507 | setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom); |
| 508 | setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom); |
| Dan Gohman | f705adb | 2009-10-30 01:28:02 +0000 | [diff] [blame] | 509 | setOperationAction(ISD::BlockAddress , MVT::i32 , Custom); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 510 | if (Subtarget->is64Bit()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 511 | setOperationAction(ISD::ConstantPool , MVT::i64 , Custom); |
| 512 | setOperationAction(ISD::JumpTable , MVT::i64 , Custom); |
| 513 | setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom); |
| 514 | setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom); |
| Dan Gohman | f705adb | 2009-10-30 01:28:02 +0000 | [diff] [blame] | 515 | setOperationAction(ISD::BlockAddress , MVT::i64 , Custom); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 516 | } |
| Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 517 | // 64-bit addm sub, shl, sra, srl (iff 32-bit x86) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 518 | setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom); |
| 519 | setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom); |
| 520 | setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom); |
| Dan Gohman | 4c1fa61 | 2008-03-03 22:22:09 +0000 | [diff] [blame] | 521 | if (Subtarget->is64Bit()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 522 | setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom); |
| 523 | setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom); |
| 524 | setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom); |
| Dan Gohman | 4c1fa61 | 2008-03-03 22:22:09 +0000 | [diff] [blame] | 525 | } |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 526 | |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 527 | if (Subtarget->hasSSE1()) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 528 | setOperationAction(ISD::PREFETCH , MVT::Other, Legal); |
| Evan Cheng | 27b7db5 | 2008-03-08 00:58:38 +0000 | [diff] [blame] | 529 | |
| Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 530 | setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 531 | |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 532 | // Expand certain atomics |
| Craig Topper | 9e401f2 | 2012-04-21 18:58:38 +0000 | [diff] [blame] | 533 | for (unsigned i = 0; i != array_lengthof(IntVTs); ++i) { |
| Chris Lattner | e019ec1 | 2010-12-19 20:07:10 +0000 | [diff] [blame] | 534 | MVT VT = IntVTs[i]; |
| 535 | setOperationAction(ISD::ATOMIC_CMP_SWAP, VT, Custom); |
| 536 | setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom); |
| Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 537 | setOperationAction(ISD::ATOMIC_STORE, VT, Custom); |
| Chris Lattner | e019ec1 | 2010-12-19 20:07:10 +0000 | [diff] [blame] | 538 | } |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 539 | |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 540 | if (!Subtarget->is64Bit()) { |
| Eli Friedman | f8f90f0 | 2011-08-24 22:33:28 +0000 | [diff] [blame] | 541 | setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 542 | setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom); |
| 543 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom); |
| 544 | setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom); |
| 545 | setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom); |
| 546 | setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom); |
| 547 | setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom); |
| 548 | setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom); |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 549 | setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i64, Custom); |
| 550 | setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i64, Custom); |
| 551 | setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i64, Custom); |
| 552 | setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i64, Custom); |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| Eli Friedman | 43f51ae | 2011-08-26 21:21:21 +0000 | [diff] [blame] | 555 | if (Subtarget->hasCmpxchg16b()) { |
| 556 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom); |
| 557 | } |
| 558 | |
| Evan Cheng | 3c992d2 | 2006-03-07 02:02:57 +0000 | [diff] [blame] | 559 | // FIXME - use subtarget debug flags |
| Anton Korobeynikov | ab4022f | 2006-10-31 08:31:24 +0000 | [diff] [blame] | 560 | if (!Subtarget->isTargetDarwin() && |
| 561 | !Subtarget->isTargetELF() && |
| Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 562 | !Subtarget->isTargetCygMing()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 563 | setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); |
| Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 564 | } |
| Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 565 | |
| Anton Korobeynikov | ce3b465 | 2007-05-02 19:53:33 +0000 | [diff] [blame] | 566 | if (Subtarget->is64Bit()) { |
| Anton Korobeynikov | ce3b465 | 2007-05-02 19:53:33 +0000 | [diff] [blame] | 567 | setExceptionPointerRegister(X86::RAX); |
| 568 | setExceptionSelectorRegister(X86::RDX); |
| 569 | } else { |
| 570 | setExceptionPointerRegister(X86::EAX); |
| 571 | setExceptionSelectorRegister(X86::EDX); |
| 572 | } |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 573 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom); |
| 574 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom); |
| Anton Korobeynikov | 260a6b8 | 2008-09-08 21:12:11 +0000 | [diff] [blame] | 575 | |
| Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 576 | setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom); |
| 577 | setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 578 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 579 | setOperationAction(ISD::TRAP, MVT::Other, Legal); |
| Shuxin Yang | 970755e | 2012-10-19 20:11:16 +0000 | [diff] [blame] | 580 | setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal); |
| Anton Korobeynikov | 66fac79 | 2008-01-15 07:02:33 +0000 | [diff] [blame] | 581 | |
| Nate Begeman | acc398c | 2006-01-25 18:21:52 +0000 | [diff] [blame] | 582 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 583 | setOperationAction(ISD::VASTART , MVT::Other, Custom); |
| 584 | setOperationAction(ISD::VAEND , MVT::Other, Expand); |
| Nico Rieck | 63e7778 | 2013-07-08 11:19:44 +0000 | [diff] [blame] | 585 | if (Subtarget->is64Bit()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 586 | setOperationAction(ISD::VAARG , MVT::Other, Custom); |
| 587 | setOperationAction(ISD::VACOPY , MVT::Other, Custom); |
| Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 588 | } else { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 589 | setOperationAction(ISD::VAARG , MVT::Other, Expand); |
| 590 | setOperationAction(ISD::VACOPY , MVT::Other, Expand); |
| Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 591 | } |
| Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 592 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 593 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); |
| 594 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); |
| Eric Christopher | c967ad8 | 2011-08-31 04:17:21 +0000 | [diff] [blame] | 595 | |
| 596 | if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho()) |
| 597 | setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ? |
| 598 | MVT::i64 : MVT::i32, Custom); |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 599 | else if (TM.Options.EnableSegmentedStacks) |
| Eric Christopher | c967ad8 | 2011-08-31 04:17:21 +0000 | [diff] [blame] | 600 | setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ? |
| 601 | MVT::i64 : MVT::i32, Custom); |
| 602 | else |
| 603 | setOperationAction(ISD::DYNAMIC_STACKALLOC, Subtarget->is64Bit() ? |
| 604 | MVT::i64 : MVT::i32, Expand); |
| Chris Lattner | b99329e | 2006-01-13 02:42:53 +0000 | [diff] [blame] | 605 | |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 606 | if (!TM.Options.UseSoftFloat && X86ScalarSSEf64) { |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 607 | // f32 and f64 use SSE. |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 608 | // Set up the FP register classes. |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 609 | addRegisterClass(MVT::f32, &X86::FR32RegClass); |
| 610 | addRegisterClass(MVT::f64, &X86::FR64RegClass); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 611 | |
| Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 612 | // Use ANDPD to simulate FABS. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 613 | setOperationAction(ISD::FABS , MVT::f64, Custom); |
| 614 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 615 | |
| 616 | // Use XORP to simulate FNEG. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 617 | setOperationAction(ISD::FNEG , MVT::f64, Custom); |
| 618 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 619 | |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 620 | // Use ANDPD and ORPD to simulate FCOPYSIGN. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 621 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom); |
| 622 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 623 | |
| Stuart Hastings | 4fd0dee | 2011-06-01 04:39:42 +0000 | [diff] [blame] | 624 | // Lower this to FGETSIGNx86 plus an AND. |
| 625 | setOperationAction(ISD::FGETSIGN, MVT::i64, Custom); |
| 626 | setOperationAction(ISD::FGETSIGN, MVT::i32, Custom); |
| 627 | |
| Evan Cheng | d25e9e8 | 2006-02-02 00:28:23 +0000 | [diff] [blame] | 628 | // We don't support sin/cos/fmod |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 629 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 630 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
| 631 | setOperationAction(ISD::FSINCOS, MVT::f64, Expand); |
| 632 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 633 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| 634 | setOperationAction(ISD::FSINCOS, MVT::f32, Expand); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 635 | |
| Chris Lattner | a54aa94 | 2006-01-29 06:26:08 +0000 | [diff] [blame] | 636 | // Expand FP immediates into loads from the stack, except for the special |
| 637 | // cases we handle. |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 638 | addLegalFPImmediate(APFloat(+0.0)); // xorpd |
| 639 | addLegalFPImmediate(APFloat(+0.0f)); // xorps |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 640 | } else if (!TM.Options.UseSoftFloat && X86ScalarSSEf32) { |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 641 | // Use SSE for f32, x87 for f64. |
| 642 | // Set up the FP register classes. |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 643 | addRegisterClass(MVT::f32, &X86::FR32RegClass); |
| 644 | addRegisterClass(MVT::f64, &X86::RFP64RegClass); |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 645 | |
| 646 | // Use ANDPS to simulate FABS. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 647 | setOperationAction(ISD::FABS , MVT::f32, Custom); |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 648 | |
| 649 | // Use XORP to simulate FNEG. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 650 | setOperationAction(ISD::FNEG , MVT::f32, Custom); |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 651 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 652 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 653 | |
| 654 | // Use ANDPS and ORPS to simulate FCOPYSIGN. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 655 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 656 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 657 | |
| 658 | // We don't support sin/cos/fmod |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 659 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 660 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| 661 | setOperationAction(ISD::FSINCOS, MVT::f32, Expand); |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 662 | |
| Nate Begeman | e179584 | 2008-02-14 08:57:00 +0000 | [diff] [blame] | 663 | // Special cases we handle for FP constants. |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 664 | addLegalFPImmediate(APFloat(+0.0f)); // xorps |
| 665 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 |
| 666 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 |
| 667 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS |
| 668 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS |
| 669 | |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 670 | if (!TM.Options.UnsafeFPMath) { |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 671 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 672 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
| 673 | setOperationAction(ISD::FSINCOS, MVT::f64, Expand); |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 674 | } |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 675 | } else if (!TM.Options.UseSoftFloat) { |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 676 | // f32 and f64 in x87. |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 677 | // Set up the FP register classes. |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 678 | addRegisterClass(MVT::f64, &X86::RFP64RegClass); |
| 679 | addRegisterClass(MVT::f32, &X86::RFP32RegClass); |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 680 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 681 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); |
| 682 | setOperationAction(ISD::UNDEF, MVT::f32, Expand); |
| 683 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); |
| 684 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); |
| Dale Johannesen | 5411a39 | 2007-08-09 01:04:01 +0000 | [diff] [blame] | 685 | |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 686 | if (!TM.Options.UnsafeFPMath) { |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 687 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 688 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 689 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
| 690 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| 691 | setOperationAction(ISD::FSINCOS, MVT::f64, Expand); |
| 692 | setOperationAction(ISD::FSINCOS, MVT::f32, Expand); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 693 | } |
| Dale Johannesen | f04afdb | 2007-08-30 00:23:21 +0000 | [diff] [blame] | 694 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 |
| 695 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 |
| 696 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS |
| 697 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS |
| Dale Johannesen | f1fc3a8 | 2007-09-23 14:52:20 +0000 | [diff] [blame] | 698 | addLegalFPImmediate(APFloat(+0.0f)); // FLD0 |
| 699 | addLegalFPImmediate(APFloat(+1.0f)); // FLD1 |
| 700 | addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS |
| 701 | addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 702 | } |
| Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 703 | |
| Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 704 | // We don't support FMA. |
| 705 | setOperationAction(ISD::FMA, MVT::f64, Expand); |
| 706 | setOperationAction(ISD::FMA, MVT::f32, Expand); |
| 707 | |
| Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 708 | // Long double always uses X87. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 709 | if (!TM.Options.UseSoftFloat) { |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 710 | addRegisterClass(MVT::f80, &X86::RFP80RegClass); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 711 | setOperationAction(ISD::UNDEF, MVT::f80, Expand); |
| 712 | setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand); |
| Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 713 | { |
| Benjamin Kramer | 9838396 | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 714 | APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended); |
| Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 715 | addLegalFPImmediate(TmpFlt); // FLD0 |
| 716 | TmpFlt.changeSign(); |
| 717 | addLegalFPImmediate(TmpFlt); // FLD0/FCHS |
| Benjamin Kramer | 9838396 | 2010-12-04 14:22:24 +0000 | [diff] [blame] | 718 | |
| 719 | bool ignored; |
| Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 720 | APFloat TmpFlt2(+1.0); |
| 721 | TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven, |
| 722 | &ignored); |
| 723 | addLegalFPImmediate(TmpFlt2); // FLD1 |
| 724 | TmpFlt2.changeSign(); |
| 725 | addLegalFPImmediate(TmpFlt2); // FLD1/FCHS |
| 726 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 727 | |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 728 | if (!TM.Options.UnsafeFPMath) { |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 729 | setOperationAction(ISD::FSIN , MVT::f80, Expand); |
| 730 | setOperationAction(ISD::FCOS , MVT::f80, Expand); |
| 731 | setOperationAction(ISD::FSINCOS, MVT::f80, Expand); |
| Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 732 | } |
| Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 733 | |
| Owen Anderson | 4a4fdf3 | 2011-12-08 19:32:14 +0000 | [diff] [blame] | 734 | setOperationAction(ISD::FFLOOR, MVT::f80, Expand); |
| 735 | setOperationAction(ISD::FCEIL, MVT::f80, Expand); |
| 736 | setOperationAction(ISD::FTRUNC, MVT::f80, Expand); |
| 737 | setOperationAction(ISD::FRINT, MVT::f80, Expand); |
| 738 | setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand); |
| Cameron Zwarich | 3339084 | 2011-07-08 21:39:21 +0000 | [diff] [blame] | 739 | setOperationAction(ISD::FMA, MVT::f80, Expand); |
| Dale Johannesen | 2f42901 | 2007-09-26 21:10:55 +0000 | [diff] [blame] | 740 | } |
| Dale Johannesen | 59a5873 | 2007-08-05 18:49:15 +0000 | [diff] [blame] | 741 | |
| Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 742 | // Always use a library call for pow. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 743 | setOperationAction(ISD::FPOW , MVT::f32 , Expand); |
| 744 | setOperationAction(ISD::FPOW , MVT::f64 , Expand); |
| 745 | setOperationAction(ISD::FPOW , MVT::f80 , Expand); |
| Dan Gohman | f96e4de | 2007-10-11 23:21:31 +0000 | [diff] [blame] | 746 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 747 | setOperationAction(ISD::FLOG, MVT::f80, Expand); |
| 748 | setOperationAction(ISD::FLOG2, MVT::f80, Expand); |
| 749 | setOperationAction(ISD::FLOG10, MVT::f80, Expand); |
| 750 | setOperationAction(ISD::FEXP, MVT::f80, Expand); |
| 751 | setOperationAction(ISD::FEXP2, MVT::f80, Expand); |
| Dale Johannesen | 7794f2a | 2008-09-04 00:47:13 +0000 | [diff] [blame] | 752 | |
| Mon P Wang | f007a8b | 2008-11-06 05:31:54 +0000 | [diff] [blame] | 753 | // First set operation action for all vector types to either promote |
| Mon P Wang | 0c39719 | 2008-10-30 08:01:45 +0000 | [diff] [blame] | 754 | // (for widening) or expand (for scalarization). Then we will selectively |
| 755 | // turn on ones that can be effectively codegen'd. |
| Craig Topper | 55de339 | 2012-11-14 06:41:09 +0000 | [diff] [blame] | 756 | for (int i = MVT::FIRST_VECTOR_VALUETYPE; |
| 757 | i <= MVT::LAST_VECTOR_VALUETYPE; ++i) { |
| Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 758 | MVT VT = (MVT::SimpleValueType)i; |
| Craig Topper | 55de339 | 2012-11-14 06:41:09 +0000 | [diff] [blame] | 759 | setOperationAction(ISD::ADD , VT, Expand); |
| 760 | setOperationAction(ISD::SUB , VT, Expand); |
| 761 | setOperationAction(ISD::FADD, VT, Expand); |
| 762 | setOperationAction(ISD::FNEG, VT, Expand); |
| 763 | setOperationAction(ISD::FSUB, VT, Expand); |
| 764 | setOperationAction(ISD::MUL , VT, Expand); |
| 765 | setOperationAction(ISD::FMUL, VT, Expand); |
| 766 | setOperationAction(ISD::SDIV, VT, Expand); |
| 767 | setOperationAction(ISD::UDIV, VT, Expand); |
| 768 | setOperationAction(ISD::FDIV, VT, Expand); |
| 769 | setOperationAction(ISD::SREM, VT, Expand); |
| 770 | setOperationAction(ISD::UREM, VT, Expand); |
| 771 | setOperationAction(ISD::LOAD, VT, Expand); |
| 772 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand); |
| 773 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand); |
| 774 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand); |
| 775 | setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand); |
| 776 | setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand); |
| 777 | setOperationAction(ISD::FABS, VT, Expand); |
| 778 | setOperationAction(ISD::FSIN, VT, Expand); |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 779 | setOperationAction(ISD::FSINCOS, VT, Expand); |
| Craig Topper | 55de339 | 2012-11-14 06:41:09 +0000 | [diff] [blame] | 780 | setOperationAction(ISD::FCOS, VT, Expand); |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 781 | setOperationAction(ISD::FSINCOS, VT, Expand); |
| Craig Topper | 55de339 | 2012-11-14 06:41:09 +0000 | [diff] [blame] | 782 | setOperationAction(ISD::FREM, VT, Expand); |
| 783 | setOperationAction(ISD::FMA, VT, Expand); |
| 784 | setOperationAction(ISD::FPOWI, VT, Expand); |
| 785 | setOperationAction(ISD::FSQRT, VT, Expand); |
| 786 | setOperationAction(ISD::FCOPYSIGN, VT, Expand); |
| 787 | setOperationAction(ISD::FFLOOR, VT, Expand); |
| Craig Topper | 4901047 | 2012-11-15 06:51:10 +0000 | [diff] [blame] | 788 | setOperationAction(ISD::FCEIL, VT, Expand); |
| 789 | setOperationAction(ISD::FTRUNC, VT, Expand); |
| 790 | setOperationAction(ISD::FRINT, VT, Expand); |
| 791 | setOperationAction(ISD::FNEARBYINT, VT, Expand); |
| Craig Topper | 55de339 | 2012-11-14 06:41:09 +0000 | [diff] [blame] | 792 | setOperationAction(ISD::SMUL_LOHI, VT, Expand); |
| 793 | setOperationAction(ISD::UMUL_LOHI, VT, Expand); |
| 794 | setOperationAction(ISD::SDIVREM, VT, Expand); |
| 795 | setOperationAction(ISD::UDIVREM, VT, Expand); |
| 796 | setOperationAction(ISD::FPOW, VT, Expand); |
| 797 | setOperationAction(ISD::CTPOP, VT, Expand); |
| 798 | setOperationAction(ISD::CTTZ, VT, Expand); |
| 799 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand); |
| 800 | setOperationAction(ISD::CTLZ, VT, Expand); |
| 801 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand); |
| 802 | setOperationAction(ISD::SHL, VT, Expand); |
| 803 | setOperationAction(ISD::SRA, VT, Expand); |
| 804 | setOperationAction(ISD::SRL, VT, Expand); |
| 805 | setOperationAction(ISD::ROTL, VT, Expand); |
| 806 | setOperationAction(ISD::ROTR, VT, Expand); |
| 807 | setOperationAction(ISD::BSWAP, VT, Expand); |
| 808 | setOperationAction(ISD::SETCC, VT, Expand); |
| 809 | setOperationAction(ISD::FLOG, VT, Expand); |
| 810 | setOperationAction(ISD::FLOG2, VT, Expand); |
| 811 | setOperationAction(ISD::FLOG10, VT, Expand); |
| 812 | setOperationAction(ISD::FEXP, VT, Expand); |
| 813 | setOperationAction(ISD::FEXP2, VT, Expand); |
| 814 | setOperationAction(ISD::FP_TO_UINT, VT, Expand); |
| 815 | setOperationAction(ISD::FP_TO_SINT, VT, Expand); |
| 816 | setOperationAction(ISD::UINT_TO_FP, VT, Expand); |
| 817 | setOperationAction(ISD::SINT_TO_FP, VT, Expand); |
| 818 | setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand); |
| 819 | setOperationAction(ISD::TRUNCATE, VT, Expand); |
| 820 | setOperationAction(ISD::SIGN_EXTEND, VT, Expand); |
| 821 | setOperationAction(ISD::ZERO_EXTEND, VT, Expand); |
| 822 | setOperationAction(ISD::ANY_EXTEND, VT, Expand); |
| 823 | setOperationAction(ISD::VSELECT, VT, Expand); |
| Jakub Staszak | 6610b1d | 2012-04-29 20:52:53 +0000 | [diff] [blame] | 824 | for (int InnerVT = MVT::FIRST_VECTOR_VALUETYPE; |
| 825 | InnerVT <= MVT::LAST_VECTOR_VALUETYPE; ++InnerVT) |
| Craig Topper | 55de339 | 2012-11-14 06:41:09 +0000 | [diff] [blame] | 826 | setTruncStoreAction(VT, |
| Dan Gohman | 2e141d7 | 2009-12-14 23:40:38 +0000 | [diff] [blame] | 827 | (MVT::SimpleValueType)InnerVT, Expand); |
| Craig Topper | 55de339 | 2012-11-14 06:41:09 +0000 | [diff] [blame] | 828 | setLoadExtAction(ISD::SEXTLOAD, VT, Expand); |
| 829 | setLoadExtAction(ISD::ZEXTLOAD, VT, Expand); |
| 830 | setLoadExtAction(ISD::EXTLOAD, VT, Expand); |
| Evan Cheng | d30bf01 | 2006-03-01 01:11:20 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 833 | // FIXME: In order to prevent SSE instructions being expanded to MMX ones |
| 834 | // with -msoft-float, disable use of MMX as well. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 835 | if (!TM.Options.UseSoftFloat && Subtarget->hasMMX()) { |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 836 | addRegisterClass(MVT::x86mmx, &X86::VR64RegClass); |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 837 | // No operations on x86mmx supported, everything uses intrinsics. |
| Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 840 | // MMX-sized vectors (other than x86mmx) are expected to be expanded |
| 841 | // into smaller operations. |
| 842 | setOperationAction(ISD::MULHS, MVT::v8i8, Expand); |
| 843 | setOperationAction(ISD::MULHS, MVT::v4i16, Expand); |
| 844 | setOperationAction(ISD::MULHS, MVT::v2i32, Expand); |
| 845 | setOperationAction(ISD::MULHS, MVT::v1i64, Expand); |
| 846 | setOperationAction(ISD::AND, MVT::v8i8, Expand); |
| 847 | setOperationAction(ISD::AND, MVT::v4i16, Expand); |
| 848 | setOperationAction(ISD::AND, MVT::v2i32, Expand); |
| 849 | setOperationAction(ISD::AND, MVT::v1i64, Expand); |
| 850 | setOperationAction(ISD::OR, MVT::v8i8, Expand); |
| 851 | setOperationAction(ISD::OR, MVT::v4i16, Expand); |
| 852 | setOperationAction(ISD::OR, MVT::v2i32, Expand); |
| 853 | setOperationAction(ISD::OR, MVT::v1i64, Expand); |
| 854 | setOperationAction(ISD::XOR, MVT::v8i8, Expand); |
| 855 | setOperationAction(ISD::XOR, MVT::v4i16, Expand); |
| 856 | setOperationAction(ISD::XOR, MVT::v2i32, Expand); |
| 857 | setOperationAction(ISD::XOR, MVT::v1i64, Expand); |
| 858 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Expand); |
| 859 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Expand); |
| 860 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Expand); |
| 861 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Expand); |
| 862 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v1i64, Expand); |
| 863 | setOperationAction(ISD::SELECT, MVT::v8i8, Expand); |
| 864 | setOperationAction(ISD::SELECT, MVT::v4i16, Expand); |
| 865 | setOperationAction(ISD::SELECT, MVT::v2i32, Expand); |
| 866 | setOperationAction(ISD::SELECT, MVT::v1i64, Expand); |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 867 | setOperationAction(ISD::BITCAST, MVT::v8i8, Expand); |
| 868 | setOperationAction(ISD::BITCAST, MVT::v4i16, Expand); |
| 869 | setOperationAction(ISD::BITCAST, MVT::v2i32, Expand); |
| 870 | setOperationAction(ISD::BITCAST, MVT::v1i64, Expand); |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 871 | |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 872 | if (!TM.Options.UseSoftFloat && Subtarget->hasSSE1()) { |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 873 | addRegisterClass(MVT::v4f32, &X86::VR128RegClass); |
| Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 874 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 875 | setOperationAction(ISD::FADD, MVT::v4f32, Legal); |
| 876 | setOperationAction(ISD::FSUB, MVT::v4f32, Legal); |
| 877 | setOperationAction(ISD::FMUL, MVT::v4f32, Legal); |
| 878 | setOperationAction(ISD::FDIV, MVT::v4f32, Legal); |
| 879 | setOperationAction(ISD::FSQRT, MVT::v4f32, Legal); |
| 880 | setOperationAction(ISD::FNEG, MVT::v4f32, Custom); |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 881 | setOperationAction(ISD::FABS, MVT::v4f32, Custom); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 882 | setOperationAction(ISD::LOAD, MVT::v4f32, Legal); |
| 883 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); |
| 884 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); |
| 885 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
| 886 | setOperationAction(ISD::SELECT, MVT::v4f32, Custom); |
| Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 889 | if (!TM.Options.UseSoftFloat && Subtarget->hasSSE2()) { |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 890 | addRegisterClass(MVT::v2f64, &X86::VR128RegClass); |
| Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 891 | |
| Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 892 | // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM |
| 893 | // registers cannot be used even for integer operations. |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 894 | addRegisterClass(MVT::v16i8, &X86::VR128RegClass); |
| 895 | addRegisterClass(MVT::v8i16, &X86::VR128RegClass); |
| 896 | addRegisterClass(MVT::v4i32, &X86::VR128RegClass); |
| 897 | addRegisterClass(MVT::v2i64, &X86::VR128RegClass); |
| Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 898 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 899 | setOperationAction(ISD::ADD, MVT::v16i8, Legal); |
| 900 | setOperationAction(ISD::ADD, MVT::v8i16, Legal); |
| 901 | setOperationAction(ISD::ADD, MVT::v4i32, Legal); |
| 902 | setOperationAction(ISD::ADD, MVT::v2i64, Legal); |
| Benjamin Kramer | 2f8a6cd | 2012-12-22 16:07:56 +0000 | [diff] [blame] | 903 | setOperationAction(ISD::MUL, MVT::v4i32, Custom); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 904 | setOperationAction(ISD::MUL, MVT::v2i64, Custom); |
| 905 | setOperationAction(ISD::SUB, MVT::v16i8, Legal); |
| 906 | setOperationAction(ISD::SUB, MVT::v8i16, Legal); |
| 907 | setOperationAction(ISD::SUB, MVT::v4i32, Legal); |
| 908 | setOperationAction(ISD::SUB, MVT::v2i64, Legal); |
| 909 | setOperationAction(ISD::MUL, MVT::v8i16, Legal); |
| 910 | setOperationAction(ISD::FADD, MVT::v2f64, Legal); |
| 911 | setOperationAction(ISD::FSUB, MVT::v2f64, Legal); |
| 912 | setOperationAction(ISD::FMUL, MVT::v2f64, Legal); |
| 913 | setOperationAction(ISD::FDIV, MVT::v2f64, Legal); |
| 914 | setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); |
| 915 | setOperationAction(ISD::FNEG, MVT::v2f64, Custom); |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 916 | setOperationAction(ISD::FABS, MVT::v2f64, Custom); |
| Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 917 | |
| Nadav Rotem | 354efd8 | 2011-09-18 14:57:03 +0000 | [diff] [blame] | 918 | setOperationAction(ISD::SETCC, MVT::v2i64, Custom); |
| Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 919 | setOperationAction(ISD::SETCC, MVT::v16i8, Custom); |
| 920 | setOperationAction(ISD::SETCC, MVT::v8i16, Custom); |
| 921 | setOperationAction(ISD::SETCC, MVT::v4i32, Custom); |
| Nate Begeman | c2616e4 | 2008-05-12 20:34:32 +0000 | [diff] [blame] | 922 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 923 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom); |
| 924 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom); |
| 925 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
| 926 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
| 927 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
| Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 928 | |
| Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 929 | // Custom lower build_vector, vector_shuffle, and extract_vector_elt. |
| Jakub Staszak | 6610b1d | 2012-04-29 20:52:53 +0000 | [diff] [blame] | 930 | for (int i = MVT::v16i8; i != MVT::v2i64; ++i) { |
| Craig Topper | 0d1f176 | 2012-08-12 00:34:56 +0000 | [diff] [blame] | 931 | MVT VT = (MVT::SimpleValueType)i; |
| Nate Begeman | 844e0f9 | 2007-12-11 01:41:33 +0000 | [diff] [blame] | 932 | // Do not attempt to custom lower non-power-of-2 vectors |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 933 | if (!isPowerOf2_32(VT.getVectorNumElements())) |
| Nate Begeman | 844e0f9 | 2007-12-11 01:41:33 +0000 | [diff] [blame] | 934 | continue; |
| David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 935 | // Do not attempt to custom lower non-128-bit vectors |
| 936 | if (!VT.is128BitVector()) |
| 937 | continue; |
| Craig Topper | 0d1f176 | 2012-08-12 00:34:56 +0000 | [diff] [blame] | 938 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); |
| 939 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); |
| 940 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); |
| Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 941 | } |
| Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 942 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 943 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom); |
| 944 | setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom); |
| 945 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom); |
| 946 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom); |
| 947 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom); |
| 948 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); |
| Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 949 | |
| Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 950 | if (Subtarget->is64Bit()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 951 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom); |
| 952 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); |
| Nate Begeman | cdd1eec | 2008-02-12 22:51:28 +0000 | [diff] [blame] | 953 | } |
| Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 954 | |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 955 | // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64. |
| Craig Topper | 31a207a | 2012-05-04 06:39:13 +0000 | [diff] [blame] | 956 | for (int i = MVT::v16i8; i != MVT::v2i64; ++i) { |
| Craig Topper | 0d1f176 | 2012-08-12 00:34:56 +0000 | [diff] [blame] | 957 | MVT VT = (MVT::SimpleValueType)i; |
| David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 958 | |
| 959 | // Do not attempt to promote non-128-bit vectors |
| Chris Lattner | 32b4b5a | 2010-07-05 05:53:14 +0000 | [diff] [blame] | 960 | if (!VT.is128BitVector()) |
| David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 961 | continue; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 962 | |
| Craig Topper | 0d1f176 | 2012-08-12 00:34:56 +0000 | [diff] [blame] | 963 | setOperationAction(ISD::AND, VT, Promote); |
| 964 | AddPromotedToType (ISD::AND, VT, MVT::v2i64); |
| 965 | setOperationAction(ISD::OR, VT, Promote); |
| 966 | AddPromotedToType (ISD::OR, VT, MVT::v2i64); |
| 967 | setOperationAction(ISD::XOR, VT, Promote); |
| 968 | AddPromotedToType (ISD::XOR, VT, MVT::v2i64); |
| 969 | setOperationAction(ISD::LOAD, VT, Promote); |
| 970 | AddPromotedToType (ISD::LOAD, VT, MVT::v2i64); |
| 971 | setOperationAction(ISD::SELECT, VT, Promote); |
| 972 | AddPromotedToType (ISD::SELECT, VT, MVT::v2i64); |
| Evan Cheng | f7c378e | 2006-04-10 07:23:14 +0000 | [diff] [blame] | 973 | } |
| Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 974 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 975 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| Chris Lattner | d43d00c | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 976 | |
| Evan Cheng | 2c3ae37 | 2006-04-12 21:21:57 +0000 | [diff] [blame] | 977 | // Custom lower v2i64 and v2f64 selects. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 978 | setOperationAction(ISD::LOAD, MVT::v2f64, Legal); |
| 979 | setOperationAction(ISD::LOAD, MVT::v2i64, Legal); |
| 980 | setOperationAction(ISD::SELECT, MVT::v2f64, Custom); |
| 981 | setOperationAction(ISD::SELECT, MVT::v2i64, Custom); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 982 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 983 | setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal); |
| 984 | setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal); |
| Michael Liao | b8150d8 | 2012-09-10 18:33:51 +0000 | [diff] [blame] | 985 | |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 986 | setOperationAction(ISD::UINT_TO_FP, MVT::v4i8, Custom); |
| 987 | setOperationAction(ISD::UINT_TO_FP, MVT::v4i16, Custom); |
| Michael Liao | 991b6a2 | 2012-10-24 04:09:32 +0000 | [diff] [blame] | 988 | // As there is no 64-bit GPR available, we need build a special custom |
| 989 | // sequence to convert from v2i32 to v2f32. |
| 990 | if (!Subtarget->is64Bit()) |
| 991 | setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom); |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 992 | |
| Michael Liao | 9d796db | 2012-10-10 16:32:15 +0000 | [diff] [blame] | 993 | setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom); |
| Michael Liao | 44c2d61 | 2012-10-10 16:53:28 +0000 | [diff] [blame] | 994 | setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom); |
| Michael Liao | 9d796db | 2012-10-10 16:32:15 +0000 | [diff] [blame] | 995 | |
| Michael Liao | b8150d8 | 2012-09-10 18:33:51 +0000 | [diff] [blame] | 996 | setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, Legal); |
| Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 997 | } |
| Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 998 | |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 999 | if (Subtarget->hasSSE41()) { |
| Benjamin Kramer | b653397 | 2011-12-09 15:44:03 +0000 | [diff] [blame] | 1000 | setOperationAction(ISD::FFLOOR, MVT::f32, Legal); |
| 1001 | setOperationAction(ISD::FCEIL, MVT::f32, Legal); |
| 1002 | setOperationAction(ISD::FTRUNC, MVT::f32, Legal); |
| 1003 | setOperationAction(ISD::FRINT, MVT::f32, Legal); |
| 1004 | setOperationAction(ISD::FNEARBYINT, MVT::f32, Legal); |
| 1005 | setOperationAction(ISD::FFLOOR, MVT::f64, Legal); |
| 1006 | setOperationAction(ISD::FCEIL, MVT::f64, Legal); |
| 1007 | setOperationAction(ISD::FTRUNC, MVT::f64, Legal); |
| 1008 | setOperationAction(ISD::FRINT, MVT::f64, Legal); |
| 1009 | setOperationAction(ISD::FNEARBYINT, MVT::f64, Legal); |
| 1010 | |
| Craig Topper | 12fb5c6 | 2012-09-08 17:42:27 +0000 | [diff] [blame] | 1011 | setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal); |
| Craig Topper | d577552 | 2012-11-16 06:37:56 +0000 | [diff] [blame] | 1012 | setOperationAction(ISD::FCEIL, MVT::v4f32, Legal); |
| 1013 | setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal); |
| 1014 | setOperationAction(ISD::FRINT, MVT::v4f32, Legal); |
| 1015 | setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal); |
| Craig Topper | 12fb5c6 | 2012-09-08 17:42:27 +0000 | [diff] [blame] | 1016 | setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal); |
| Craig Topper | d577552 | 2012-11-16 06:37:56 +0000 | [diff] [blame] | 1017 | setOperationAction(ISD::FCEIL, MVT::v2f64, Legal); |
| 1018 | setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal); |
| 1019 | setOperationAction(ISD::FRINT, MVT::v2f64, Legal); |
| 1020 | setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal); |
| Craig Topper | 12fb5c6 | 2012-09-08 17:42:27 +0000 | [diff] [blame] | 1021 | |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 1022 | // FIXME: Do we need to handle scalar-to-vector here? |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1023 | setOperationAction(ISD::MUL, MVT::v4i32, Legal); |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 1024 | |
| Nadav Rotem | fbad25e | 2011-09-11 15:02:23 +0000 | [diff] [blame] | 1025 | setOperationAction(ISD::VSELECT, MVT::v2f64, Legal); |
| 1026 | setOperationAction(ISD::VSELECT, MVT::v2i64, Legal); |
| 1027 | setOperationAction(ISD::VSELECT, MVT::v16i8, Legal); |
| 1028 | setOperationAction(ISD::VSELECT, MVT::v4i32, Legal); |
| 1029 | setOperationAction(ISD::VSELECT, MVT::v4f32, Legal); |
| Nadav Rotem | ffe3e7d | 2011-09-08 08:11:19 +0000 | [diff] [blame] | 1030 | |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 1031 | // i8 and i16 vectors are custom , because the source register and source |
| 1032 | // source memory operand types are not the same width. f32 vectors are |
| 1033 | // custom since the immediate controlling the insert encodes additional |
| 1034 | // information. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1035 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom); |
| 1036 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); |
| 1037 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); |
| 1038 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 1039 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1040 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom); |
| 1041 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom); |
| 1042 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom); |
| 1043 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 1044 | |
| Pete Cooper | a77214a | 2011-11-14 19:38:42 +0000 | [diff] [blame] | 1045 | // FIXME: these should be Legal but thats only for the case where |
| Chad Rosier | 30450e8 | 2011-12-22 22:35:21 +0000 | [diff] [blame] | 1046 | // the index is constant. For now custom expand to deal with that. |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 1047 | if (Subtarget->is64Bit()) { |
| Pete Cooper | a77214a | 2011-11-14 19:38:42 +0000 | [diff] [blame] | 1048 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom); |
| 1049 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom); |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 1050 | } |
| 1051 | } |
| Evan Cheng | 470a6ad | 2006-02-22 02:26:30 +0000 | [diff] [blame] | 1052 | |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 1053 | if (Subtarget->hasSSE2()) { |
| Bruno Cardoso Lopes | 328a9d4 | 2011-08-08 21:31:08 +0000 | [diff] [blame] | 1054 | setOperationAction(ISD::SRL, MVT::v8i16, Custom); |
| Craig Topper | 7be5dfd | 2011-11-12 09:58:49 +0000 | [diff] [blame] | 1055 | setOperationAction(ISD::SRL, MVT::v16i8, Custom); |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 1056 | |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 1057 | setOperationAction(ISD::SHL, MVT::v8i16, Custom); |
| Craig Topper | 7be5dfd | 2011-11-12 09:58:49 +0000 | [diff] [blame] | 1058 | setOperationAction(ISD::SHL, MVT::v16i8, Custom); |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 1059 | |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 1060 | setOperationAction(ISD::SRA, MVT::v8i16, Custom); |
| Eli Friedman | f6aa6b1 | 2011-11-01 21:18:39 +0000 | [diff] [blame] | 1061 | setOperationAction(ISD::SRA, MVT::v16i8, Custom); |
| Craig Topper | 7be5dfd | 2011-11-12 09:58:49 +0000 | [diff] [blame] | 1062 | |
| Michael Liao | 5c5f190 | 2013-03-20 02:28:20 +0000 | [diff] [blame] | 1063 | // In the customized shift lowering, the legal cases in AVX2 will be |
| 1064 | // recognized. |
| 1065 | setOperationAction(ISD::SRL, MVT::v2i64, Custom); |
| 1066 | setOperationAction(ISD::SRL, MVT::v4i32, Custom); |
| Craig Topper | 7be5dfd | 2011-11-12 09:58:49 +0000 | [diff] [blame] | 1067 | |
| Michael Liao | 5c5f190 | 2013-03-20 02:28:20 +0000 | [diff] [blame] | 1068 | setOperationAction(ISD::SHL, MVT::v2i64, Custom); |
| 1069 | setOperationAction(ISD::SHL, MVT::v4i32, Custom); |
| Craig Topper | 7be5dfd | 2011-11-12 09:58:49 +0000 | [diff] [blame] | 1070 | |
| Michael Liao | 5c5f190 | 2013-03-20 02:28:20 +0000 | [diff] [blame] | 1071 | setOperationAction(ISD::SRA, MVT::v4i32, Custom); |
| Craig Topper | 7be5dfd | 2011-11-12 09:58:49 +0000 | [diff] [blame] | 1072 | |
| Nadav Rotem | 13f8cf5 | 2013-01-09 05:14:33 +0000 | [diff] [blame] | 1073 | setOperationAction(ISD::SDIV, MVT::v8i16, Custom); |
| 1074 | setOperationAction(ISD::SDIV, MVT::v4i32, Custom); |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 1077 | if (!TM.Options.UseSoftFloat && Subtarget->hasFp256()) { |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1078 | addRegisterClass(MVT::v32i8, &X86::VR256RegClass); |
| 1079 | addRegisterClass(MVT::v16i16, &X86::VR256RegClass); |
| 1080 | addRegisterClass(MVT::v8i32, &X86::VR256RegClass); |
| 1081 | addRegisterClass(MVT::v8f32, &X86::VR256RegClass); |
| 1082 | addRegisterClass(MVT::v4i64, &X86::VR256RegClass); |
| 1083 | addRegisterClass(MVT::v4f64, &X86::VR256RegClass); |
| David Greene | d94c101 | 2009-06-29 22:50:51 +0000 | [diff] [blame] | 1084 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1085 | setOperationAction(ISD::LOAD, MVT::v8f32, Legal); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1086 | setOperationAction(ISD::LOAD, MVT::v4f64, Legal); |
| 1087 | setOperationAction(ISD::LOAD, MVT::v4i64, Legal); |
| David Greene | 54d8eba | 2011-01-27 22:38:56 +0000 | [diff] [blame] | 1088 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1089 | setOperationAction(ISD::FADD, MVT::v8f32, Legal); |
| 1090 | setOperationAction(ISD::FSUB, MVT::v8f32, Legal); |
| 1091 | setOperationAction(ISD::FMUL, MVT::v8f32, Legal); |
| 1092 | setOperationAction(ISD::FDIV, MVT::v8f32, Legal); |
| 1093 | setOperationAction(ISD::FSQRT, MVT::v8f32, Legal); |
| Craig Topper | 12fb5c6 | 2012-09-08 17:42:27 +0000 | [diff] [blame] | 1094 | setOperationAction(ISD::FFLOOR, MVT::v8f32, Legal); |
| Craig Topper | d577552 | 2012-11-16 06:37:56 +0000 | [diff] [blame] | 1095 | setOperationAction(ISD::FCEIL, MVT::v8f32, Legal); |
| 1096 | setOperationAction(ISD::FTRUNC, MVT::v8f32, Legal); |
| 1097 | setOperationAction(ISD::FRINT, MVT::v8f32, Legal); |
| 1098 | setOperationAction(ISD::FNEARBYINT, MVT::v8f32, Legal); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1099 | setOperationAction(ISD::FNEG, MVT::v8f32, Custom); |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 1100 | setOperationAction(ISD::FABS, MVT::v8f32, Custom); |
| David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 1101 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1102 | setOperationAction(ISD::FADD, MVT::v4f64, Legal); |
| 1103 | setOperationAction(ISD::FSUB, MVT::v4f64, Legal); |
| 1104 | setOperationAction(ISD::FMUL, MVT::v4f64, Legal); |
| 1105 | setOperationAction(ISD::FDIV, MVT::v4f64, Legal); |
| 1106 | setOperationAction(ISD::FSQRT, MVT::v4f64, Legal); |
| Craig Topper | 12fb5c6 | 2012-09-08 17:42:27 +0000 | [diff] [blame] | 1107 | setOperationAction(ISD::FFLOOR, MVT::v4f64, Legal); |
| Craig Topper | d577552 | 2012-11-16 06:37:56 +0000 | [diff] [blame] | 1108 | setOperationAction(ISD::FCEIL, MVT::v4f64, Legal); |
| 1109 | setOperationAction(ISD::FTRUNC, MVT::v4f64, Legal); |
| 1110 | setOperationAction(ISD::FRINT, MVT::v4f64, Legal); |
| 1111 | setOperationAction(ISD::FNEARBYINT, MVT::v4f64, Legal); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1112 | setOperationAction(ISD::FNEG, MVT::v4f64, Custom); |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 1113 | setOperationAction(ISD::FABS, MVT::v4f64, Custom); |
| David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 1114 | |
| Michael Liao | bedcbd4 | 2012-10-16 18:14:11 +0000 | [diff] [blame] | 1115 | setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom); |
| Nadav Rotem | 3c22a44 | 2012-12-27 07:45:10 +0000 | [diff] [blame] | 1116 | setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom); |
| Michael Liao | bedcbd4 | 2012-10-16 18:14:11 +0000 | [diff] [blame] | 1117 | |
| 1118 | setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom); |
| 1119 | |
| Bruno Cardoso Lopes | 2e64ae4 | 2011-07-28 01:26:39 +0000 | [diff] [blame] | 1120 | setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal); |
| Benjamin Kramer | b8f0d89 | 2013-03-31 12:49:15 +0000 | [diff] [blame] | 1121 | setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Promote); |
| Bruno Cardoso Lopes | 2e64ae4 | 2011-07-28 01:26:39 +0000 | [diff] [blame] | 1122 | setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal); |
| Bruno Cardoso Lopes | 55244ce | 2011-08-01 21:54:09 +0000 | [diff] [blame] | 1123 | setOperationAction(ISD::FP_ROUND, MVT::v4f32, Legal); |
| Bruno Cardoso Lopes | 2e64ae4 | 2011-07-28 01:26:39 +0000 | [diff] [blame] | 1124 | |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 1125 | setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom); |
| 1126 | setOperationAction(ISD::UINT_TO_FP, MVT::v8i8, Custom); |
| 1127 | setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Custom); |
| 1128 | |
| Michael Liao | b8150d8 | 2012-09-10 18:33:51 +0000 | [diff] [blame] | 1129 | setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, Legal); |
| 1130 | |
| Bruno Cardoso Lopes | 328a9d4 | 2011-08-08 21:31:08 +0000 | [diff] [blame] | 1131 | setOperationAction(ISD::SRL, MVT::v16i16, Custom); |
| 1132 | setOperationAction(ISD::SRL, MVT::v32i8, Custom); |
| 1133 | |
| Bruno Cardoso Lopes | 328a9d4 | 2011-08-08 21:31:08 +0000 | [diff] [blame] | 1134 | setOperationAction(ISD::SHL, MVT::v16i16, Custom); |
| 1135 | setOperationAction(ISD::SHL, MVT::v32i8, Custom); |
| 1136 | |
| Bruno Cardoso Lopes | 328a9d4 | 2011-08-08 21:31:08 +0000 | [diff] [blame] | 1137 | setOperationAction(ISD::SRA, MVT::v16i16, Custom); |
| Craig Topper | 7be5dfd | 2011-11-12 09:58:49 +0000 | [diff] [blame] | 1138 | setOperationAction(ISD::SRA, MVT::v32i8, Custom); |
| Bruno Cardoso Lopes | 328a9d4 | 2011-08-08 21:31:08 +0000 | [diff] [blame] | 1139 | |
| Nadav Rotem | 13f8cf5 | 2013-01-09 05:14:33 +0000 | [diff] [blame] | 1140 | setOperationAction(ISD::SDIV, MVT::v16i16, Custom); |
| 1141 | |
| Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 1142 | setOperationAction(ISD::SETCC, MVT::v32i8, Custom); |
| 1143 | setOperationAction(ISD::SETCC, MVT::v16i16, Custom); |
| 1144 | setOperationAction(ISD::SETCC, MVT::v8i32, Custom); |
| 1145 | setOperationAction(ISD::SETCC, MVT::v4i64, Custom); |
| Bruno Cardoso Lopes | 0f0e0a0 | 2011-08-09 00:46:57 +0000 | [diff] [blame] | 1146 | |
| Bruno Cardoso Lopes | d40aa24 | 2011-08-09 23:27:13 +0000 | [diff] [blame] | 1147 | setOperationAction(ISD::SELECT, MVT::v4f64, Custom); |
| 1148 | setOperationAction(ISD::SELECT, MVT::v4i64, Custom); |
| 1149 | setOperationAction(ISD::SELECT, MVT::v8f32, Custom); |
| 1150 | |
| Craig Topper | aaa643c | 2011-11-09 07:28:55 +0000 | [diff] [blame] | 1151 | setOperationAction(ISD::VSELECT, MVT::v4f64, Legal); |
| 1152 | setOperationAction(ISD::VSELECT, MVT::v4i64, Legal); |
| 1153 | setOperationAction(ISD::VSELECT, MVT::v8i32, Legal); |
| 1154 | setOperationAction(ISD::VSELECT, MVT::v8f32, Legal); |
| Nadav Rotem | 8ffad56 | 2011-09-09 20:29:17 +0000 | [diff] [blame] | 1155 | |
| Nadav Rotem | 0509db2 | 2012-12-28 05:45:24 +0000 | [diff] [blame] | 1156 | setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom); |
| 1157 | setOperationAction(ISD::SIGN_EXTEND, MVT::v8i32, Custom); |
| 1158 | setOperationAction(ISD::ZERO_EXTEND, MVT::v4i64, Custom); |
| 1159 | setOperationAction(ISD::ZERO_EXTEND, MVT::v8i32, Custom); |
| 1160 | setOperationAction(ISD::ANY_EXTEND, MVT::v4i64, Custom); |
| 1161 | setOperationAction(ISD::ANY_EXTEND, MVT::v8i32, Custom); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 1162 | |
| Craig Topper | bf40437 | 2012-08-31 15:40:30 +0000 | [diff] [blame] | 1163 | if (Subtarget->hasFMA() || Subtarget->hasFMA4()) { |
| Craig Topper | 3dcefc8 | 2012-11-21 05:36:24 +0000 | [diff] [blame] | 1164 | setOperationAction(ISD::FMA, MVT::v8f32, Legal); |
| 1165 | setOperationAction(ISD::FMA, MVT::v4f64, Legal); |
| 1166 | setOperationAction(ISD::FMA, MVT::v4f32, Legal); |
| 1167 | setOperationAction(ISD::FMA, MVT::v2f64, Legal); |
| 1168 | setOperationAction(ISD::FMA, MVT::f32, Legal); |
| 1169 | setOperationAction(ISD::FMA, MVT::f64, Legal); |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 1170 | } |
| Craig Topper | 880ef45 | 2012-08-11 22:34:26 +0000 | [diff] [blame] | 1171 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 1172 | if (Subtarget->hasInt256()) { |
| Craig Topper | aaa643c | 2011-11-09 07:28:55 +0000 | [diff] [blame] | 1173 | setOperationAction(ISD::ADD, MVT::v4i64, Legal); |
| 1174 | setOperationAction(ISD::ADD, MVT::v8i32, Legal); |
| 1175 | setOperationAction(ISD::ADD, MVT::v16i16, Legal); |
| 1176 | setOperationAction(ISD::ADD, MVT::v32i8, Legal); |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 1177 | |
| Craig Topper | aaa643c | 2011-11-09 07:28:55 +0000 | [diff] [blame] | 1178 | setOperationAction(ISD::SUB, MVT::v4i64, Legal); |
| 1179 | setOperationAction(ISD::SUB, MVT::v8i32, Legal); |
| 1180 | setOperationAction(ISD::SUB, MVT::v16i16, Legal); |
| 1181 | setOperationAction(ISD::SUB, MVT::v32i8, Legal); |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 1182 | |
| Craig Topper | aaa643c | 2011-11-09 07:28:55 +0000 | [diff] [blame] | 1183 | setOperationAction(ISD::MUL, MVT::v4i64, Custom); |
| 1184 | setOperationAction(ISD::MUL, MVT::v8i32, Legal); |
| 1185 | setOperationAction(ISD::MUL, MVT::v16i16, Legal); |
| Craig Topper | 46154eb | 2011-11-11 07:39:23 +0000 | [diff] [blame] | 1186 | // Don't lower v32i8 because there is no 128-bit byte mul |
| Nadav Rotem | bb539bf | 2011-11-09 13:21:28 +0000 | [diff] [blame] | 1187 | |
| 1188 | setOperationAction(ISD::VSELECT, MVT::v32i8, Legal); |
| Craig Topper | 7be5dfd | 2011-11-12 09:58:49 +0000 | [diff] [blame] | 1189 | |
| Nadav Rotem | 13f8cf5 | 2013-01-09 05:14:33 +0000 | [diff] [blame] | 1190 | setOperationAction(ISD::SDIV, MVT::v8i32, Custom); |
| Craig Topper | aaa643c | 2011-11-09 07:28:55 +0000 | [diff] [blame] | 1191 | } else { |
| 1192 | setOperationAction(ISD::ADD, MVT::v4i64, Custom); |
| 1193 | setOperationAction(ISD::ADD, MVT::v8i32, Custom); |
| 1194 | setOperationAction(ISD::ADD, MVT::v16i16, Custom); |
| 1195 | setOperationAction(ISD::ADD, MVT::v32i8, Custom); |
| 1196 | |
| 1197 | setOperationAction(ISD::SUB, MVT::v4i64, Custom); |
| 1198 | setOperationAction(ISD::SUB, MVT::v8i32, Custom); |
| 1199 | setOperationAction(ISD::SUB, MVT::v16i16, Custom); |
| 1200 | setOperationAction(ISD::SUB, MVT::v32i8, Custom); |
| 1201 | |
| 1202 | setOperationAction(ISD::MUL, MVT::v4i64, Custom); |
| 1203 | setOperationAction(ISD::MUL, MVT::v8i32, Custom); |
| 1204 | setOperationAction(ISD::MUL, MVT::v16i16, Custom); |
| 1205 | // Don't lower v32i8 because there is no 128-bit byte mul |
| 1206 | } |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 1207 | |
| Michael Liao | 5c5f190 | 2013-03-20 02:28:20 +0000 | [diff] [blame] | 1208 | // In the customized shift lowering, the legal cases in AVX2 will be |
| 1209 | // recognized. |
| 1210 | setOperationAction(ISD::SRL, MVT::v4i64, Custom); |
| 1211 | setOperationAction(ISD::SRL, MVT::v8i32, Custom); |
| 1212 | |
| 1213 | setOperationAction(ISD::SHL, MVT::v4i64, Custom); |
| 1214 | setOperationAction(ISD::SHL, MVT::v8i32, Custom); |
| 1215 | |
| 1216 | setOperationAction(ISD::SRA, MVT::v8i32, Custom); |
| 1217 | |
| Bruno Cardoso Lopes | 5bc37dd | 2011-07-15 22:24:33 +0000 | [diff] [blame] | 1218 | // Custom lower several nodes for 256-bit types. |
| Jakub Staszak | 6610b1d | 2012-04-29 20:52:53 +0000 | [diff] [blame] | 1219 | for (int i = MVT::FIRST_VECTOR_VALUETYPE; |
| 1220 | i <= MVT::LAST_VECTOR_VALUETYPE; ++i) { |
| Craig Topper | 0d1f176 | 2012-08-12 00:34:56 +0000 | [diff] [blame] | 1221 | MVT VT = (MVT::SimpleValueType)i; |
| Bruno Cardoso Lopes | 5bc37dd | 2011-07-15 22:24:33 +0000 | [diff] [blame] | 1222 | |
| 1223 | // Extract subvector is special because the value type |
| 1224 | // (result) is 128-bit but the source is 256-bit wide. |
| 1225 | if (VT.is128BitVector()) |
| Craig Topper | 0d1f176 | 2012-08-12 00:34:56 +0000 | [diff] [blame] | 1226 | setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom); |
| Bruno Cardoso Lopes | 5bc37dd | 2011-07-15 22:24:33 +0000 | [diff] [blame] | 1227 | |
| 1228 | // Do not attempt to custom lower other non-256-bit vectors |
| 1229 | if (!VT.is256BitVector()) |
| David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 1230 | continue; |
| David Greene | 54d8eba | 2011-01-27 22:38:56 +0000 | [diff] [blame] | 1231 | |
| Craig Topper | 0d1f176 | 2012-08-12 00:34:56 +0000 | [diff] [blame] | 1232 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); |
| 1233 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); |
| 1234 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); |
| 1235 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); |
| 1236 | setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom); |
| 1237 | setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom); |
| 1238 | setOperationAction(ISD::CONCAT_VECTORS, VT, Custom); |
| David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| David Greene | 54d8eba | 2011-01-27 22:38:56 +0000 | [diff] [blame] | 1241 | // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64. |
| Jakub Staszak | 6610b1d | 2012-04-29 20:52:53 +0000 | [diff] [blame] | 1242 | for (int i = MVT::v32i8; i != MVT::v4i64; ++i) { |
| Craig Topper | 0d1f176 | 2012-08-12 00:34:56 +0000 | [diff] [blame] | 1243 | MVT VT = (MVT::SimpleValueType)i; |
| David Greene | 54d8eba | 2011-01-27 22:38:56 +0000 | [diff] [blame] | 1244 | |
| Bruno Cardoso Lopes | 5bc37dd | 2011-07-15 22:24:33 +0000 | [diff] [blame] | 1245 | // Do not attempt to promote non-256-bit vectors |
| 1246 | if (!VT.is256BitVector()) |
| David Greene | 54d8eba | 2011-01-27 22:38:56 +0000 | [diff] [blame] | 1247 | continue; |
| Bruno Cardoso Lopes | 5bc37dd | 2011-07-15 22:24:33 +0000 | [diff] [blame] | 1248 | |
| Craig Topper | 0d1f176 | 2012-08-12 00:34:56 +0000 | [diff] [blame] | 1249 | setOperationAction(ISD::AND, VT, Promote); |
| 1250 | AddPromotedToType (ISD::AND, VT, MVT::v4i64); |
| 1251 | setOperationAction(ISD::OR, VT, Promote); |
| 1252 | AddPromotedToType (ISD::OR, VT, MVT::v4i64); |
| 1253 | setOperationAction(ISD::XOR, VT, Promote); |
| 1254 | AddPromotedToType (ISD::XOR, VT, MVT::v4i64); |
| 1255 | setOperationAction(ISD::LOAD, VT, Promote); |
| 1256 | AddPromotedToType (ISD::LOAD, VT, MVT::v4i64); |
| 1257 | setOperationAction(ISD::SELECT, VT, Promote); |
| 1258 | AddPromotedToType (ISD::SELECT, VT, MVT::v4i64); |
| David Greene | 54d8eba | 2011-01-27 22:38:56 +0000 | [diff] [blame] | 1259 | } |
| David Greene | 9b9838d | 2009-06-29 16:47:10 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| Nadav Rotem | d0f3ef8 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 1262 | // SIGN_EXTEND_INREGs are evaluated by the extend type. Handle the expansion |
| 1263 | // of this type with custom code. |
| Jakub Staszak | 6610b1d | 2012-04-29 20:52:53 +0000 | [diff] [blame] | 1264 | for (int VT = MVT::FIRST_VECTOR_VALUETYPE; |
| 1265 | VT != MVT::LAST_VECTOR_VALUETYPE; VT++) { |
| Chad Rosier | 30450e8 | 2011-12-22 22:35:21 +0000 | [diff] [blame] | 1266 | setOperationAction(ISD::SIGN_EXTEND_INREG, (MVT::SimpleValueType)VT, |
| 1267 | Custom); |
| Nadav Rotem | d0f3ef8 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 1270 | // We want to custom lower some of our intrinsics. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1271 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 1272 | setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 1273 | |
| Eli Friedman | 962f549 | 2010-06-02 19:35:46 +0000 | [diff] [blame] | 1274 | // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't |
| 1275 | // handle type legalization for these operations here. |
| Dan Gohman | 71c62a2 | 2010-06-02 19:13:40 +0000 | [diff] [blame] | 1276 | // |
| Eli Friedman | 962f549 | 2010-06-02 19:35:46 +0000 | [diff] [blame] | 1277 | // FIXME: We really should do custom legalization for addition and |
| 1278 | // subtraction on x86-32 once PR3203 is fixed. We really can't do much better |
| 1279 | // than generic legalization for 64-bit multiplication-with-overflow, though. |
| Chris Lattner | a34b3cf | 2010-12-19 20:03:11 +0000 | [diff] [blame] | 1280 | for (unsigned i = 0, e = 3+Subtarget->is64Bit(); i != e; ++i) { |
| 1281 | // Add/Sub/Mul with overflow operations are custom lowered. |
| 1282 | MVT VT = IntVTs[i]; |
| 1283 | setOperationAction(ISD::SADDO, VT, Custom); |
| 1284 | setOperationAction(ISD::UADDO, VT, Custom); |
| 1285 | setOperationAction(ISD::SSUBO, VT, Custom); |
| 1286 | setOperationAction(ISD::USUBO, VT, Custom); |
| 1287 | setOperationAction(ISD::SMULO, VT, Custom); |
| 1288 | setOperationAction(ISD::UMULO, VT, Custom); |
| Eli Friedman | a993f0a | 2010-06-02 00:27:18 +0000 | [diff] [blame] | 1289 | } |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 1290 | |
| Chris Lattner | a34b3cf | 2010-12-19 20:03:11 +0000 | [diff] [blame] | 1291 | // There are no 8-bit 3-address imul/mul instructions |
| 1292 | setOperationAction(ISD::SMULO, MVT::i8, Expand); |
| 1293 | setOperationAction(ISD::UMULO, MVT::i8, Expand); |
| Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 1294 | |
| Evan Cheng | d54f2d5 | 2009-03-31 19:38:51 +0000 | [diff] [blame] | 1295 | if (!Subtarget->is64Bit()) { |
| 1296 | // These libcalls are not available in 32-bit. |
| 1297 | setLibcallName(RTLIB::SHL_I128, 0); |
| 1298 | setLibcallName(RTLIB::SRL_I128, 0); |
| 1299 | setLibcallName(RTLIB::SRA_I128, 0); |
| 1300 | } |
| 1301 | |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 1302 | // Combine sin / cos into one node or libcall if possible. |
| 1303 | if (Subtarget->hasSinCos()) { |
| 1304 | setLibcallName(RTLIB::SINCOS_F32, "sincosf"); |
| 1305 | setLibcallName(RTLIB::SINCOS_F64, "sincos"); |
| Evan Cheng | a66f40a | 2013-01-30 22:56:35 +0000 | [diff] [blame] | 1306 | if (Subtarget->isTargetDarwin()) { |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 1307 | // For MacOSX, we don't want to the normal expansion of a libcall to |
| 1308 | // sincos. We want to issue a libcall to __sincos_stret to avoid memory |
| 1309 | // traffic. |
| 1310 | setOperationAction(ISD::FSINCOS, MVT::f64, Custom); |
| 1311 | setOperationAction(ISD::FSINCOS, MVT::f32, Custom); |
| 1312 | } |
| 1313 | } |
| 1314 | |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 1315 | // We have target-specific dag combine patterns for the following nodes: |
| 1316 | setTargetDAGCombine(ISD::VECTOR_SHUFFLE); |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 1317 | setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); |
| Duncan Sands | 6bcd219 | 2011-09-17 16:49:39 +0000 | [diff] [blame] | 1318 | setTargetDAGCombine(ISD::VSELECT); |
| Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 1319 | setTargetDAGCombine(ISD::SELECT); |
| Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 1320 | setTargetDAGCombine(ISD::SHL); |
| 1321 | setTargetDAGCombine(ISD::SRA); |
| 1322 | setTargetDAGCombine(ISD::SRL); |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 1323 | setTargetDAGCombine(ISD::OR); |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 1324 | setTargetDAGCombine(ISD::AND); |
| Benjamin Kramer | 7d6fe13 | 2010-12-21 21:41:44 +0000 | [diff] [blame] | 1325 | setTargetDAGCombine(ISD::ADD); |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 1326 | setTargetDAGCombine(ISD::FADD); |
| 1327 | setTargetDAGCombine(ISD::FSUB); |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 1328 | setTargetDAGCombine(ISD::FMA); |
| Benjamin Kramer | 7d6fe13 | 2010-12-21 21:41:44 +0000 | [diff] [blame] | 1329 | setTargetDAGCombine(ISD::SUB); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 1330 | setTargetDAGCombine(ISD::LOAD); |
| Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 1331 | setTargetDAGCombine(ISD::STORE); |
| Evan Cheng | 2e489c4 | 2009-12-16 00:53:11 +0000 | [diff] [blame] | 1332 | setTargetDAGCombine(ISD::ZERO_EXTEND); |
| Elena Demikhovsky | 1da5867 | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 1333 | setTargetDAGCombine(ISD::ANY_EXTEND); |
| Elena Demikhovsky | dcabc7b | 2012-02-02 09:10:43 +0000 | [diff] [blame] | 1334 | setTargetDAGCombine(ISD::SIGN_EXTEND); |
| Elena Demikhovsky | 52981c4 | 2013-02-20 12:42:54 +0000 | [diff] [blame] | 1335 | setTargetDAGCombine(ISD::SIGN_EXTEND_INREG); |
| Elena Demikhovsky | 3ae9815 | 2012-02-01 07:56:44 +0000 | [diff] [blame] | 1336 | setTargetDAGCombine(ISD::TRUNCATE); |
| Stuart Hastings | f99a4b8 | 2011-06-06 23:15:58 +0000 | [diff] [blame] | 1337 | setTargetDAGCombine(ISD::SINT_TO_FP); |
| Chad Rosier | a73b6fc | 2012-04-27 22:33:25 +0000 | [diff] [blame] | 1338 | setTargetDAGCombine(ISD::SETCC); |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 1339 | if (Subtarget->is64Bit()) |
| 1340 | setTargetDAGCombine(ISD::MUL); |
| Manman Ren | 9236362 | 2012-06-07 22:39:10 +0000 | [diff] [blame] | 1341 | setTargetDAGCombine(ISD::XOR); |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 1342 | |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1343 | computeRegisterProperties(); |
| 1344 | |
| Evan Cheng | 0521928 | 2011-01-06 06:52:41 +0000 | [diff] [blame] | 1345 | // On Darwin, -Os means optimize for size without hurting performance, |
| 1346 | // do not reduce the limit. |
| Jim Grosbach | 3450f80 | 2013-02-20 21:13:59 +0000 | [diff] [blame] | 1347 | MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores |
| 1348 | MaxStoresPerMemsetOptSize = Subtarget->isTargetDarwin() ? 16 : 8; |
| 1349 | MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores |
| 1350 | MaxStoresPerMemcpyOptSize = Subtarget->isTargetDarwin() ? 8 : 4; |
| 1351 | MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores |
| 1352 | MaxStoresPerMemmoveOptSize = Subtarget->isTargetDarwin() ? 8 : 4; |
| Jakob Stoklund Olesen | 8c741b8 | 2011-12-06 01:26:19 +0000 | [diff] [blame] | 1353 | setPrefLoopAlignment(4); // 2^4 bytes. |
| Eli Friedman | fc5d305 | 2011-05-06 20:34:06 +0000 | [diff] [blame] | 1354 | |
| Benjamin Kramer | aaf723d | 2012-05-05 12:49:14 +0000 | [diff] [blame] | 1355 | // Predictable cmov don't hurt on atom because it's in-order. |
| Jim Grosbach | 3450f80 | 2013-02-20 21:13:59 +0000 | [diff] [blame] | 1356 | PredictableSelectIsExpensive = !Subtarget->isAtom(); |
| Benjamin Kramer | aaf723d | 2012-05-05 12:49:14 +0000 | [diff] [blame] | 1357 | |
| Jakob Stoklund Olesen | 8c741b8 | 2011-12-06 01:26:19 +0000 | [diff] [blame] | 1358 | setPrefFunctionAlignment(4); // 2^4 bytes. |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1359 | } |
| 1360 | |
| Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 1361 | EVT X86TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { |
| Duncan Sands | 28b77e9 | 2011-09-06 19:07:46 +0000 | [diff] [blame] | 1362 | if (!VT.isVector()) return MVT::i8; |
| 1363 | return VT.changeVectorElementTypeToInteger(); |
| Scott Michel | 5b8f82e | 2008-03-10 15:42:14 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
| Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1366 | /// getMaxByValAlign - Helper for getByValTypeAlignment to determine |
| 1367 | /// the desired ByVal argument alignment. |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1368 | static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) { |
| Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1369 | if (MaxAlign == 16) |
| 1370 | return; |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1371 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1372 | if (VTy->getBitWidth() == 128) |
| 1373 | MaxAlign = 16; |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1374 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { |
| Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1375 | unsigned EltAlign = 0; |
| 1376 | getMaxByValAlign(ATy->getElementType(), EltAlign); |
| 1377 | if (EltAlign > MaxAlign) |
| 1378 | MaxAlign = EltAlign; |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1379 | } else if (StructType *STy = dyn_cast<StructType>(Ty)) { |
| Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1380 | for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) { |
| 1381 | unsigned EltAlign = 0; |
| 1382 | getMaxByValAlign(STy->getElementType(i), EltAlign); |
| 1383 | if (EltAlign > MaxAlign) |
| 1384 | MaxAlign = EltAlign; |
| 1385 | if (MaxAlign == 16) |
| 1386 | break; |
| 1387 | } |
| 1388 | } |
| Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate |
| 1392 | /// function arguments in the caller parameter area. For X86, aggregates |
| Dale Johannesen | 0c19187 | 2008-02-08 19:48:20 +0000 | [diff] [blame] | 1393 | /// that contain SSE vectors are placed at 16-byte boundaries while the rest |
| 1394 | /// are at 4-byte boundaries. |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1395 | unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty) const { |
| Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 1396 | if (Subtarget->is64Bit()) { |
| 1397 | // Max of 8 and alignment of type. |
| Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 1398 | unsigned TyAlign = TD->getABITypeAlignment(Ty); |
| Evan Cheng | 1887c1c | 2008-08-21 21:00:15 +0000 | [diff] [blame] | 1399 | if (TyAlign > 8) |
| 1400 | return TyAlign; |
| 1401 | return 8; |
| 1402 | } |
| 1403 | |
| Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1404 | unsigned Align = 4; |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 1405 | if (Subtarget->hasSSE1()) |
| Dale Johannesen | 0c19187 | 2008-02-08 19:48:20 +0000 | [diff] [blame] | 1406 | getMaxByValAlign(Ty, Align); |
| Evan Cheng | 2928650 | 2008-01-23 23:17:41 +0000 | [diff] [blame] | 1407 | return Align; |
| 1408 | } |
| Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 1409 | |
| Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 1410 | /// getOptimalMemOpType - Returns the target specific optimal type for load |
| Evan Cheng | c3b0c34 | 2010-04-08 07:37:57 +0000 | [diff] [blame] | 1411 | /// and store operations as a result of memset, memcpy, and memmove |
| 1412 | /// lowering. If DstAlign is zero that means it's safe to destination |
| 1413 | /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it |
| 1414 | /// means there isn't a need to check it against alignment requirement, |
| Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 1415 | /// probably because the source does not need to be loaded. If 'IsMemset' is |
| 1416 | /// true, that means it's expanding a memset. If 'ZeroMemset' is true, that |
| 1417 | /// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy |
| 1418 | /// source is constant so it does not need to be loaded. |
| Dan Gohman | 37f32ee | 2010-04-16 20:11:05 +0000 | [diff] [blame] | 1419 | /// It returns EVT::Other if the type should be determined using generic |
| 1420 | /// target-independent logic. |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1421 | EVT |
| Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 1422 | X86TargetLowering::getOptimalMemOpType(uint64_t Size, |
| 1423 | unsigned DstAlign, unsigned SrcAlign, |
| Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 1424 | bool IsMemset, bool ZeroMemset, |
| Evan Cheng | c3b0c34 | 2010-04-08 07:37:57 +0000 | [diff] [blame] | 1425 | bool MemcpyStrSrc, |
| Dan Gohman | 37f32ee | 2010-04-16 20:11:05 +0000 | [diff] [blame] | 1426 | MachineFunction &MF) const { |
| Dan Gohman | 37f32ee | 2010-04-16 20:11:05 +0000 | [diff] [blame] | 1427 | const Function *F = MF.getFunction(); |
| Evan Cheng | 946a3a9 | 2012-12-12 02:34:41 +0000 | [diff] [blame] | 1428 | if ((!IsMemset || ZeroMemset) && |
| Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 1429 | !F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 1430 | Attribute::NoImplicitFloat)) { |
| Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 1431 | if (Size >= 16 && |
| Evan Cheng | a5e1362 | 2011-01-07 19:35:30 +0000 | [diff] [blame] | 1432 | (Subtarget->isUnalignedMemAccessFast() || |
| 1433 | ((DstAlign == 0 || DstAlign >= 16) && |
| Benjamin Kramer | 2dbe929 | 2012-11-14 20:08:40 +0000 | [diff] [blame] | 1434 | (SrcAlign == 0 || SrcAlign >= 16)))) { |
| 1435 | if (Size >= 32) { |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 1436 | if (Subtarget->hasInt256()) |
| Craig Topper | 562659f | 2012-01-13 08:32:21 +0000 | [diff] [blame] | 1437 | return MVT::v8i32; |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 1438 | if (Subtarget->hasFp256()) |
| Craig Topper | 562659f | 2012-01-13 08:32:21 +0000 | [diff] [blame] | 1439 | return MVT::v8f32; |
| 1440 | } |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 1441 | if (Subtarget->hasSSE2()) |
| Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 1442 | return MVT::v4i32; |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 1443 | if (Subtarget->hasSSE1()) |
| Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 1444 | return MVT::v4f32; |
| Evan Cheng | c3b0c34 | 2010-04-08 07:37:57 +0000 | [diff] [blame] | 1445 | } else if (!MemcpyStrSrc && Size >= 8 && |
| Evan Cheng | 3ea9755 | 2010-04-01 20:27:45 +0000 | [diff] [blame] | 1446 | !Subtarget->is64Bit() && |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 1447 | Subtarget->hasSSE2()) { |
| Evan Cheng | c3b0c34 | 2010-04-08 07:37:57 +0000 | [diff] [blame] | 1448 | // Do not use f64 to lower memcpy if source is string constant. It's |
| 1449 | // better to use i32 to avoid the loads. |
| Evan Cheng | 255f20f | 2010-04-01 06:04:33 +0000 | [diff] [blame] | 1450 | return MVT::f64; |
| Evan Cheng | c3b0c34 | 2010-04-08 07:37:57 +0000 | [diff] [blame] | 1451 | } |
| Chris Lattner | 4002a1b | 2008-10-28 05:49:35 +0000 | [diff] [blame] | 1452 | } |
| Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 1453 | if (Subtarget->is64Bit() && Size >= 8) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1454 | return MVT::i64; |
| 1455 | return MVT::i32; |
| Evan Cheng | f0df031 | 2008-05-15 08:39:06 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| Evan Cheng | 7d34267 | 2012-12-12 01:32:07 +0000 | [diff] [blame] | 1458 | bool X86TargetLowering::isSafeMemOpType(MVT VT) const { |
| Evan Cheng | 61f4dfe | 2012-12-12 00:42:09 +0000 | [diff] [blame] | 1459 | if (VT == MVT::f32) |
| 1460 | return X86ScalarSSEf32; |
| 1461 | else if (VT == MVT::f64) |
| 1462 | return X86ScalarSSEf64; |
| Evan Cheng | 7d34267 | 2012-12-12 01:32:07 +0000 | [diff] [blame] | 1463 | return true; |
| Evan Cheng | 61f4dfe | 2012-12-12 00:42:09 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| Evan Cheng | 376642e | 2012-12-10 23:21:26 +0000 | [diff] [blame] | 1466 | bool |
| 1467 | X86TargetLowering::allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const { |
| 1468 | if (Fast) |
| 1469 | *Fast = Subtarget->isUnalignedMemAccessFast(); |
| 1470 | return true; |
| 1471 | } |
| 1472 | |
| Chris Lattner | 5e1df8d | 2010-01-25 23:38:14 +0000 | [diff] [blame] | 1473 | /// getJumpTableEncoding - Return the entry encoding for a jump table in the |
| 1474 | /// current function. The returned value is a member of the |
| 1475 | /// MachineJumpTableInfo::JTEntryKind enum. |
| 1476 | unsigned X86TargetLowering::getJumpTableEncoding() const { |
| 1477 | // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF |
| 1478 | // symbol. |
| 1479 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 1480 | Subtarget->isPICStyleGOT()) |
| Chris Lattner | c64daab | 2010-01-26 05:02:42 +0000 | [diff] [blame] | 1481 | return MachineJumpTableInfo::EK_Custom32; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 1482 | |
| Chris Lattner | 5e1df8d | 2010-01-25 23:38:14 +0000 | [diff] [blame] | 1483 | // Otherwise, use the normal jump table encoding heuristics. |
| 1484 | return TargetLowering::getJumpTableEncoding(); |
| 1485 | } |
| 1486 | |
| Chris Lattner | c64daab | 2010-01-26 05:02:42 +0000 | [diff] [blame] | 1487 | const MCExpr * |
| 1488 | X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI, |
| 1489 | const MachineBasicBlock *MBB, |
| 1490 | unsigned uid,MCContext &Ctx) const{ |
| 1491 | assert(getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| 1492 | Subtarget->isPICStyleGOT()); |
| 1493 | // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF |
| 1494 | // entries. |
| Daniel Dunbar | 4e815f8 | 2010-03-15 23:51:06 +0000 | [diff] [blame] | 1495 | return MCSymbolRefExpr::Create(MBB->getSymbol(), |
| 1496 | MCSymbolRefExpr::VK_GOTOFF, Ctx); |
| Chris Lattner | c64daab | 2010-01-26 05:02:42 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1499 | /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC |
| 1500 | /// jumptable. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1501 | SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table, |
| Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 1502 | SelectionDAG &DAG) const { |
| Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 1503 | if (!Subtarget->is64Bit()) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1504 | // This doesn't have SDLoc associated with it, but is not really the |
| Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 1505 | // same as a Register. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1506 | return DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), getPointerTy()); |
| Evan Cheng | cc41586 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 1507 | return Table; |
| 1508 | } |
| 1509 | |
| Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 1510 | /// getPICJumpTableRelocBaseExpr - This returns the relocation base for the |
| 1511 | /// given PIC jumptable, the same as getPICJumpTableRelocBase, but as an |
| 1512 | /// MCExpr. |
| 1513 | const MCExpr *X86TargetLowering:: |
| 1514 | getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI, |
| 1515 | MCContext &Ctx) const { |
| 1516 | // X86-64 uses RIP relative addressing based on the jump table label. |
| 1517 | if (Subtarget->isPICStyleRIPRel()) |
| 1518 | return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx); |
| 1519 | |
| 1520 | // Otherwise, the reference is relative to the PIC base. |
| Chris Lattner | 142b531 | 2010-11-14 22:48:15 +0000 | [diff] [blame] | 1521 | return MCSymbolRefExpr::Create(MF->getPICBaseSymbol(), Ctx); |
| Chris Lattner | 589c6f6 | 2010-01-26 06:28:43 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 1524 | // FIXME: Why this routine is here? Move to RegInfo! |
| Evan Cheng | dee8101 | 2010-07-26 21:50:05 +0000 | [diff] [blame] | 1525 | std::pair<const TargetRegisterClass*, uint8_t> |
| Patrik Hagglund | 0340557 | 2012-12-19 11:30:36 +0000 | [diff] [blame] | 1526 | X86TargetLowering::findRepresentativeClass(MVT VT) const{ |
| Evan Cheng | dee8101 | 2010-07-26 21:50:05 +0000 | [diff] [blame] | 1527 | const TargetRegisterClass *RRC = 0; |
| 1528 | uint8_t Cost = 1; |
| Patrik Hagglund | 0340557 | 2012-12-19 11:30:36 +0000 | [diff] [blame] | 1529 | switch (VT.SimpleTy) { |
| Evan Cheng | dee8101 | 2010-07-26 21:50:05 +0000 | [diff] [blame] | 1530 | default: |
| 1531 | return TargetLowering::findRepresentativeClass(VT); |
| 1532 | case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64: |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1533 | RRC = Subtarget->is64Bit() ? |
| 1534 | (const TargetRegisterClass*)&X86::GR64RegClass : |
| 1535 | (const TargetRegisterClass*)&X86::GR32RegClass; |
| Evan Cheng | dee8101 | 2010-07-26 21:50:05 +0000 | [diff] [blame] | 1536 | break; |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 1537 | case MVT::x86mmx: |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1538 | RRC = &X86::VR64RegClass; |
| Evan Cheng | dee8101 | 2010-07-26 21:50:05 +0000 | [diff] [blame] | 1539 | break; |
| 1540 | case MVT::f32: case MVT::f64: |
| 1541 | case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: |
| 1542 | case MVT::v4f32: case MVT::v2f64: |
| 1543 | case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32: |
| 1544 | case MVT::v4f64: |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1545 | RRC = &X86::VR128RegClass; |
| Evan Cheng | dee8101 | 2010-07-26 21:50:05 +0000 | [diff] [blame] | 1546 | break; |
| 1547 | } |
| 1548 | return std::make_pair(RRC, Cost); |
| 1549 | } |
| 1550 | |
| Eric Christopher | f7a0c7b | 2010-07-06 05:18:56 +0000 | [diff] [blame] | 1551 | bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace, |
| 1552 | unsigned &Offset) const { |
| 1553 | if (!Subtarget->isTargetLinux()) |
| 1554 | return false; |
| 1555 | |
| 1556 | if (Subtarget->is64Bit()) { |
| 1557 | // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs: |
| 1558 | Offset = 0x28; |
| 1559 | if (getTargetMachine().getCodeModel() == CodeModel::Kernel) |
| 1560 | AddressSpace = 256; |
| 1561 | else |
| 1562 | AddressSpace = 257; |
| 1563 | } else { |
| 1564 | // %gs:0x14 on i386 |
| 1565 | Offset = 0x14; |
| 1566 | AddressSpace = 256; |
| 1567 | } |
| 1568 | return true; |
| 1569 | } |
| 1570 | |
| Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 1571 | //===----------------------------------------------------------------------===// |
| 1572 | // Return Value Calling Convention Implementation |
| 1573 | //===----------------------------------------------------------------------===// |
| 1574 | |
| Chris Lattner | 59ed56b | 2007-02-28 04:55:35 +0000 | [diff] [blame] | 1575 | #include "X86GenCallingConv.inc" |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1576 | |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 1577 | bool |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1578 | X86TargetLowering::CanLowerReturn(CallingConv::ID CallConv, |
| Craig Topper | 0fbf364 | 2012-04-23 03:28:34 +0000 | [diff] [blame] | 1579 | MachineFunction &MF, bool isVarArg, |
| Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1580 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| Dan Gohman | c9af33c | 2010-07-06 22:19:37 +0000 | [diff] [blame] | 1581 | LLVMContext &Context) const { |
| Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 1582 | SmallVector<CCValAssign, 16> RVLocs; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1583 | CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), |
| Dan Gohman | c9af33c | 2010-07-06 22:19:37 +0000 | [diff] [blame] | 1584 | RVLocs, Context); |
| Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 1585 | return CCInfo.CheckReturn(Outs, RetCC_X86); |
| Kenneth Uildriks | b4997ae | 2009-11-07 02:11:54 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1588 | SDValue |
| 1589 | X86TargetLowering::LowerReturn(SDValue Chain, |
| Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1590 | CallingConv::ID CallConv, bool isVarArg, |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1591 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1592 | const SmallVectorImpl<SDValue> &OutVals, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1593 | SDLoc dl, SelectionDAG &DAG) const { |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 1594 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1595 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1596 | |
| Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 1597 | SmallVector<CCValAssign, 16> RVLocs; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1598 | CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1599 | RVLocs, *DAG.getContext()); |
| 1600 | CCInfo.AnalyzeReturn(Outs, RetCC_X86); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1601 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1602 | SDValue Flag; |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1603 | SmallVector<SDValue, 6> RetOps; |
| Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1604 | RetOps.push_back(Chain); // Operand #0 = Chain (updated below) |
| 1605 | // Operand #1 = Bytes To Pop |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 1606 | RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(), |
| 1607 | MVT::i16)); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1608 | |
| Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1609 | // Copy the result values into the output registers. |
| Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1610 | for (unsigned i = 0; i != RVLocs.size(); ++i) { |
| 1611 | CCValAssign &VA = RVLocs[i]; |
| 1612 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 1613 | SDValue ValToCopy = OutVals[i]; |
| Dale Johannesen | c76d23f | 2010-07-23 00:30:35 +0000 | [diff] [blame] | 1614 | EVT ValVT = ValToCopy.getValueType(); |
| 1615 | |
| Jakob Stoklund Olesen | ee66b41 | 2012-05-31 17:28:20 +0000 | [diff] [blame] | 1616 | // Promote values to the appropriate types |
| 1617 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 1618 | ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy); |
| 1619 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 1620 | ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy); |
| 1621 | else if (VA.getLocInfo() == CCValAssign::AExt) |
| 1622 | ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy); |
| 1623 | else if (VA.getLocInfo() == CCValAssign::BCvt) |
| 1624 | ValToCopy = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), ValToCopy); |
| 1625 | |
| Dale Johannesen | c451051 | 2010-09-24 19:05:48 +0000 | [diff] [blame] | 1626 | // If this is x86-64, and we disabled SSE, we can't return FP values, |
| 1627 | // or SSE or MMX vectors. |
| 1628 | if ((ValVT == MVT::f32 || ValVT == MVT::f64 || |
| 1629 | VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) && |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 1630 | (Subtarget->is64Bit() && !Subtarget->hasSSE1())) { |
| Dale Johannesen | c76d23f | 2010-07-23 00:30:35 +0000 | [diff] [blame] | 1631 | report_fatal_error("SSE register return with SSE disabled"); |
| 1632 | } |
| 1633 | // Likewise we can't return F64 values with SSE1 only. gcc does so, but |
| 1634 | // llvm-gcc has never done it right and no one has noticed, so this |
| 1635 | // should be OK for now. |
| 1636 | if (ValVT == MVT::f64 && |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 1637 | (Subtarget->is64Bit() && !Subtarget->hasSSE2())) |
| Dale Johannesen | c76d23f | 2010-07-23 00:30:35 +0000 | [diff] [blame] | 1638 | report_fatal_error("SSE2 register return with SSE2 disabled"); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1639 | |
| Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1640 | // Returns in ST0/ST1 are handled specially: these are pushed as operands to |
| 1641 | // the RET instruction and handled by the FP Stackifier. |
| Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1642 | if (VA.getLocReg() == X86::ST0 || |
| 1643 | VA.getLocReg() == X86::ST1) { |
| Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1644 | // If this is a copy from an xmm register to ST(0), use an FPExtend to |
| 1645 | // change the value to the FP stack register class. |
| Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1646 | if (isScalarFPTypeInSSEReg(VA.getValVT())) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1647 | ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy); |
| Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1648 | RetOps.push_back(ValToCopy); |
| 1649 | // Don't emit a copytoreg. |
| 1650 | continue; |
| 1651 | } |
| Dale Johannesen | a68f901 | 2008-06-24 22:01:44 +0000 | [diff] [blame] | 1652 | |
| Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1653 | // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64 |
| 1654 | // which is returned in RAX / RDX. |
| Evan Cheng | 6140a8b | 2009-02-22 08:05:12 +0000 | [diff] [blame] | 1655 | if (Subtarget->is64Bit()) { |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 1656 | if (ValVT == MVT::x86mmx) { |
| Chris Lattner | 97a2a56 | 2010-08-26 05:24:29 +0000 | [diff] [blame] | 1657 | if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) { |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1658 | ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::i64, ValToCopy); |
| Eric Christopher | 90eb402 | 2010-07-22 00:26:08 +0000 | [diff] [blame] | 1659 | ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, |
| 1660 | ValToCopy); |
| Chris Lattner | 97a2a56 | 2010-08-26 05:24:29 +0000 | [diff] [blame] | 1661 | // If we don't have SSE2 available, convert to v4f32 so the generated |
| 1662 | // register is legal. |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 1663 | if (!Subtarget->hasSSE2()) |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 1664 | ValToCopy = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32,ValToCopy); |
| Chris Lattner | 97a2a56 | 2010-08-26 05:24:29 +0000 | [diff] [blame] | 1665 | } |
| Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 1666 | } |
| Evan Cheng | 6140a8b | 2009-02-22 08:05:12 +0000 | [diff] [blame] | 1667 | } |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 1668 | |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1669 | Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag); |
| Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1670 | Flag = Chain.getValue(1); |
| Jakob Stoklund Olesen | c3afc76 | 2013-02-05 17:59:48 +0000 | [diff] [blame] | 1671 | RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); |
| Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1672 | } |
| Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1673 | |
| Eli Bendersky | a5597f0 | 2013-01-25 22:07:43 +0000 | [diff] [blame] | 1674 | // The x86-64 ABIs require that for returning structs by value we copy |
| 1675 | // the sret argument into %rax/%eax (depending on ABI) for the return. |
| Timur Iskhodzhanov | a46f82d | 2013-03-28 21:30:04 +0000 | [diff] [blame] | 1676 | // Win32 requires us to put the sret argument to %eax as well. |
| Eli Bendersky | a5597f0 | 2013-01-25 22:07:43 +0000 | [diff] [blame] | 1677 | // We saved the argument into a virtual register in the entry block, |
| 1678 | // so now we copy the value out and into %rax/%eax. |
| Timur Iskhodzhanov | a46f82d | 2013-03-28 21:30:04 +0000 | [diff] [blame] | 1679 | if (DAG.getMachineFunction().getFunction()->hasStructRetAttr() && |
| 1680 | (Subtarget->is64Bit() || Subtarget->isTargetWindows())) { |
| Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1681 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1682 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 1683 | unsigned Reg = FuncInfo->getSRetReturnReg(); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 1684 | assert(Reg && |
| Zhongxing Xu | c2798a1 | 2010-05-26 08:10:02 +0000 | [diff] [blame] | 1685 | "SRetReturnReg should have been set in LowerFormalArguments()."); |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1686 | SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy()); |
| Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1687 | |
| Timur Iskhodzhanov | a46f82d | 2013-03-28 21:30:04 +0000 | [diff] [blame] | 1688 | unsigned RetValReg |
| 1689 | = (Subtarget->is64Bit() && !Subtarget->isTarget64BitILP32()) ? |
| 1690 | X86::RAX : X86::EAX; |
| Eli Bendersky | a5597f0 | 2013-01-25 22:07:43 +0000 | [diff] [blame] | 1691 | Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag); |
| Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1692 | Flag = Chain.getValue(1); |
| Dan Gohman | 0032681 | 2009-10-12 16:36:12 +0000 | [diff] [blame] | 1693 | |
| Eli Bendersky | a5597f0 | 2013-01-25 22:07:43 +0000 | [diff] [blame] | 1694 | // RAX/EAX now acts like a return value. |
| Timur Iskhodzhanov | a46f82d | 2013-03-28 21:30:04 +0000 | [diff] [blame] | 1695 | RetOps.push_back(DAG.getRegister(RetValReg, getPointerTy())); |
| Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 1696 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1697 | |
| Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1698 | RetOps[0] = Chain; // Update chain. |
| 1699 | |
| 1700 | // Add the flag if we have it. |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1701 | if (Flag.getNode()) |
| Chris Lattner | 447ff68 | 2008-03-11 03:23:40 +0000 | [diff] [blame] | 1702 | RetOps.push_back(Flag); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1703 | |
| 1704 | return DAG.getNode(X86ISD::RET_FLAG, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1705 | MVT::Other, &RetOps[0], RetOps.size()); |
| Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| Evan Cheng | bf010eb | 2012-04-10 01:51:00 +0000 | [diff] [blame] | 1708 | bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { |
| Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 1709 | if (N->getNumValues() != 1) |
| 1710 | return false; |
| 1711 | if (!N->hasNUsesOfValue(1, 0)) |
| 1712 | return false; |
| 1713 | |
| Evan Cheng | bf010eb | 2012-04-10 01:51:00 +0000 | [diff] [blame] | 1714 | SDValue TCChain = Chain; |
| Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 1715 | SDNode *Copy = *N->use_begin(); |
| Chad Rosier | c8d7eea | 2012-03-05 19:27:12 +0000 | [diff] [blame] | 1716 | if (Copy->getOpcode() == ISD::CopyToReg) { |
| 1717 | // If the copy has a glue operand, we conservatively assume it isn't safe to |
| 1718 | // perform a tail call. |
| 1719 | if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) |
| 1720 | return false; |
| Evan Cheng | bf010eb | 2012-04-10 01:51:00 +0000 | [diff] [blame] | 1721 | TCChain = Copy->getOperand(0); |
| Chad Rosier | c8d7eea | 2012-03-05 19:27:12 +0000 | [diff] [blame] | 1722 | } else if (Copy->getOpcode() != ISD::FP_EXTEND) |
| Chad Rosier | 74bab7f | 2012-03-02 02:50:46 +0000 | [diff] [blame] | 1723 | return false; |
| 1724 | |
| Evan Cheng | 1bf891a | 2010-12-01 22:59:46 +0000 | [diff] [blame] | 1725 | bool HasRet = false; |
| Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 1726 | for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); |
| Evan Cheng | 1bf891a | 2010-12-01 22:59:46 +0000 | [diff] [blame] | 1727 | UI != UE; ++UI) { |
| Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 1728 | if (UI->getOpcode() != X86ISD::RET_FLAG) |
| 1729 | return false; |
| Evan Cheng | 1bf891a | 2010-12-01 22:59:46 +0000 | [diff] [blame] | 1730 | HasRet = true; |
| 1731 | } |
| Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 1732 | |
| Evan Cheng | bf010eb | 2012-04-10 01:51:00 +0000 | [diff] [blame] | 1733 | if (!HasRet) |
| 1734 | return false; |
| 1735 | |
| 1736 | Chain = TCChain; |
| 1737 | return true; |
| Evan Cheng | 3d2125c | 2010-11-30 23:55:39 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
| Patrik Hagglund | e5c6591 | 2012-12-19 12:02:25 +0000 | [diff] [blame] | 1740 | MVT |
| 1741 | X86TargetLowering::getTypeForExtArgOrReturn(MVT VT, |
| Cameron Zwarich | 4457968 | 2011-03-17 14:21:56 +0000 | [diff] [blame] | 1742 | ISD::NodeType ExtendKind) const { |
| Cameron Zwarich | 7bbf0ee | 2011-03-17 14:53:37 +0000 | [diff] [blame] | 1743 | MVT ReturnMVT; |
| Cameron Zwarich | ebe8173 | 2011-03-16 22:20:18 +0000 | [diff] [blame] | 1744 | // TODO: Is this also valid on 32-bit? |
| 1745 | if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND) |
| Cameron Zwarich | 7bbf0ee | 2011-03-17 14:53:37 +0000 | [diff] [blame] | 1746 | ReturnMVT = MVT::i8; |
| 1747 | else |
| 1748 | ReturnMVT = MVT::i32; |
| 1749 | |
| Patrik Hagglund | e5c6591 | 2012-12-19 12:02:25 +0000 | [diff] [blame] | 1750 | MVT MinVT = getRegisterType(ReturnMVT); |
| Cameron Zwarich | 7bbf0ee | 2011-03-17 14:53:37 +0000 | [diff] [blame] | 1751 | return VT.bitsLT(MinVT) ? MinVT : VT; |
| Cameron Zwarich | ebe8173 | 2011-03-16 22:20:18 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1754 | /// LowerCallResult - Lower the result values of a call into the |
| 1755 | /// appropriate copies out of appropriate physical registers. |
| 1756 | /// |
| 1757 | SDValue |
| 1758 | X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag, |
| Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1759 | CallingConv::ID CallConv, bool isVarArg, |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1760 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1761 | SDLoc dl, SelectionDAG &DAG, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1762 | SmallVectorImpl<SDValue> &InVals) const { |
| Chris Lattner | 2a9bdd7 | 2007-02-25 09:12:39 +0000 | [diff] [blame] | 1763 | |
| Chris Lattner | e32bbf6 | 2007-02-28 07:09:55 +0000 | [diff] [blame] | 1764 | // Assign locations to each value returned by this call. |
| Chris Lattner | 9774c91 | 2007-02-27 05:28:59 +0000 | [diff] [blame] | 1765 | SmallVector<CCValAssign, 16> RVLocs; |
| Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1766 | bool Is64Bit = Subtarget->is64Bit(); |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1767 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), |
| Craig Topper | 0fbf364 | 2012-04-23 03:28:34 +0000 | [diff] [blame] | 1768 | getTargetMachine(), RVLocs, *DAG.getContext()); |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1769 | CCInfo.AnalyzeCallResult(Ins, RetCC_X86); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1770 | |
| Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1771 | // Copy all of the result registers out of their specified physreg. |
| Jakub Staszak | c20323a | 2012-12-29 15:57:26 +0000 | [diff] [blame] | 1772 | for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { |
| Dan Gohman | 37eed79 | 2009-02-04 17:28:58 +0000 | [diff] [blame] | 1773 | CCValAssign &VA = RVLocs[i]; |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1774 | EVT CopyVT = VA.getValVT(); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1775 | |
| Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1776 | // If this is x86-64, and we disabled SSE, we can't return FP values |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1777 | if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) && |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 1778 | ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) { |
| Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 1779 | report_fatal_error("SSE register return with SSE disabled"); |
| Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| Evan Cheng | 79fb3b4 | 2009-02-20 20:43:02 +0000 | [diff] [blame] | 1782 | SDValue Val; |
| Jakob Stoklund Olesen | d737fca | 2010-07-10 04:04:25 +0000 | [diff] [blame] | 1783 | |
| 1784 | // If this is a call to a function that returns an fp value on the floating |
| Sylvestre Ledru | c8e41c5 | 2012-07-23 08:51:15 +0000 | [diff] [blame] | 1785 | // point stack, we must guarantee the value is popped from the stack, so |
| Jakob Stoklund Olesen | d737fca | 2010-07-10 04:04:25 +0000 | [diff] [blame] | 1786 | // a CopyFromReg is not good enough - the copy instruction may be eliminated |
| Jakob Stoklund Olesen | 9bbe4d6 | 2011-06-28 18:32:28 +0000 | [diff] [blame] | 1787 | // if the return value is not used. We use the FpPOP_RETVAL instruction |
| Jakob Stoklund Olesen | d737fca | 2010-07-10 04:04:25 +0000 | [diff] [blame] | 1788 | // instead. |
| 1789 | if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) { |
| 1790 | // If we prefer to use the value in xmm registers, copy it out as f80 and |
| 1791 | // use a truncate to move it from fp stack reg to xmm reg. |
| 1792 | if (isScalarFPTypeInSSEReg(VA.getValVT())) CopyVT = MVT::f80; |
| Jakob Stoklund Olesen | d737fca | 2010-07-10 04:04:25 +0000 | [diff] [blame] | 1793 | SDValue Ops[] = { Chain, InFlag }; |
| Jakob Stoklund Olesen | 9bbe4d6 | 2011-06-28 18:32:28 +0000 | [diff] [blame] | 1794 | Chain = SDValue(DAG.getMachineNode(X86::FpPOP_RETVAL, dl, CopyVT, |
| Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 1795 | MVT::Other, MVT::Glue, Ops), 1); |
| Jakob Stoklund Olesen | d737fca | 2010-07-10 04:04:25 +0000 | [diff] [blame] | 1796 | Val = Chain.getValue(0); |
| 1797 | |
| 1798 | // Round the f80 to the right size, which also moves it to the appropriate |
| 1799 | // xmm register. |
| 1800 | if (CopyVT != VA.getValVT()) |
| 1801 | Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val, |
| 1802 | // This truncation won't change the value. |
| 1803 | DAG.getIntPtrConstant(1)); |
| Evan Cheng | 79fb3b4 | 2009-02-20 20:43:02 +0000 | [diff] [blame] | 1804 | } else { |
| 1805 | Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), |
| 1806 | CopyVT, InFlag).getValue(1); |
| 1807 | Val = Chain.getValue(0); |
| 1808 | } |
| Chris Lattner | 8e6da15 | 2008-03-10 21:08:41 +0000 | [diff] [blame] | 1809 | InFlag = Chain.getValue(2); |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1810 | InVals.push_back(Val); |
| Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 1811 | } |
| Duncan Sands | 4bdcb61 | 2008-07-02 17:40:58 +0000 | [diff] [blame] | 1812 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1813 | return Chain; |
| Chris Lattner | 2b02a44 | 2007-02-25 08:29:00 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1816 | //===----------------------------------------------------------------------===// |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1817 | // C & StdCall & Fast Calling Convention implementation |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1818 | //===----------------------------------------------------------------------===// |
| Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 1819 | // StdCall calling convention seems to be standard for many Windows' API |
| 1820 | // routines and around. It differs from C calling convention just a little: |
| 1821 | // callee should clean up the stack, not caller. Symbols should be also |
| 1822 | // decorated in some fancy way :) It doesn't support any vector arguments. |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 1823 | // For info on fast calling convention see Fast Calling Convention (tail call) |
| 1824 | // implementation LowerX86_32FastCCCallTo. |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1825 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1826 | /// CallIsStructReturn - Determines whether a call uses struct return |
| Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1827 | /// semantics. |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 1828 | enum StructReturnType { |
| 1829 | NotStructReturn, |
| 1830 | RegStructReturn, |
| 1831 | StackStructReturn |
| 1832 | }; |
| 1833 | static StructReturnType |
| 1834 | callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) { |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1835 | if (Outs.empty()) |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 1836 | return NotStructReturn; |
| Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1837 | |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 1838 | const ISD::ArgFlagsTy &Flags = Outs[0].Flags; |
| 1839 | if (!Flags.isSRet()) |
| 1840 | return NotStructReturn; |
| 1841 | if (Flags.isInReg()) |
| 1842 | return RegStructReturn; |
| 1843 | return StackStructReturn; |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| Dan Gohman | 7e77b0f | 2009-08-01 19:14:37 +0000 | [diff] [blame] | 1846 | /// ArgsAreStructReturn - Determines whether a function uses struct |
| Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1847 | /// return semantics. |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 1848 | static StructReturnType |
| 1849 | argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) { |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1850 | if (Ins.empty()) |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 1851 | return NotStructReturn; |
| Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1852 | |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 1853 | const ISD::ArgFlagsTy &Flags = Ins[0].Flags; |
| 1854 | if (!Flags.isSRet()) |
| 1855 | return NotStructReturn; |
| 1856 | if (Flags.isInReg()) |
| 1857 | return RegStructReturn; |
| 1858 | return StackStructReturn; |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
| Arnold Schwaighofer | 16a3e52 | 2008-02-26 17:50:59 +0000 | [diff] [blame] | 1861 | /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified |
| 1862 | /// by "Src" to address "Dst" with size and alignment information specified by |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 1863 | /// the specific parameter attribute. The copy will be passed as a byval |
| 1864 | /// function parameter. |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1865 | static SDValue |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1866 | CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain, |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1867 | ISD::ArgFlagsTy Flags, SelectionDAG &DAG, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1868 | SDLoc dl) { |
| Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 1869 | SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 1870 | |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 1871 | return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(), |
| Stuart Hastings | 03d5826 | 2011-03-10 00:25:53 +0000 | [diff] [blame] | 1872 | /*isVolatile*/false, /*AlwaysInline=*/true, |
| Chris Lattner | fc448ff | 2010-09-21 18:51:21 +0000 | [diff] [blame] | 1873 | MachinePointerInfo(), MachinePointerInfo()); |
| Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| Chris Lattner | 2968943 | 2010-03-11 00:22:57 +0000 | [diff] [blame] | 1876 | /// IsTailCallConvention - Return true if the calling convention is one that |
| 1877 | /// supports tail call optimization. |
| 1878 | static bool IsTailCallConvention(CallingConv::ID CC) { |
| Duncan Sands | dc7f174 | 2012-11-16 12:36:39 +0000 | [diff] [blame] | 1879 | return (CC == CallingConv::Fast || CC == CallingConv::GHC || |
| 1880 | CC == CallingConv::HiPE); |
| Chris Lattner | 2968943 | 2010-03-11 00:22:57 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
| Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1883 | bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { |
| Nick Lewycky | 22de16d | 2012-01-19 00:34:10 +0000 | [diff] [blame] | 1884 | if (!CI->isTailCall() || getTargetMachine().Options.DisableTailCalls) |
| Evan Cheng | 485fafc | 2011-03-21 01:19:09 +0000 | [diff] [blame] | 1885 | return false; |
| 1886 | |
| 1887 | CallSite CS(CI); |
| 1888 | CallingConv::ID CalleeCC = CS.getCallingConv(); |
| 1889 | if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C) |
| 1890 | return false; |
| 1891 | |
| 1892 | return true; |
| 1893 | } |
| 1894 | |
| Evan Cheng | 0c439eb | 2010-01-27 00:07:07 +0000 | [diff] [blame] | 1895 | /// FuncIsMadeTailCallSafe - Return true if the function is being made into |
| 1896 | /// a tailcall target by changing its ABI. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1897 | static bool FuncIsMadeTailCallSafe(CallingConv::ID CC, |
| 1898 | bool GuaranteedTailCallOpt) { |
| Chris Lattner | 2968943 | 2010-03-11 00:22:57 +0000 | [diff] [blame] | 1899 | return GuaranteedTailCallOpt && IsTailCallConvention(CC); |
| Evan Cheng | 0c439eb | 2010-01-27 00:07:07 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1902 | SDValue |
| 1903 | X86TargetLowering::LowerMemArgument(SDValue Chain, |
| Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1904 | CallingConv::ID CallConv, |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1905 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1906 | SDLoc dl, SelectionDAG &DAG, |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1907 | const CCValAssign &VA, |
| 1908 | MachineFrameInfo *MFI, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1909 | unsigned i) const { |
| Rafael Espindola | 7effac5 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1910 | // Create the nodes corresponding to a load from this parameter slot. |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1911 | ISD::ArgFlagsTy Flags = Ins[i].Flags; |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 1912 | bool AlwaysUseMutable = FuncIsMadeTailCallSafe(CallConv, |
| 1913 | getTargetMachine().Options.GuaranteedTailCallOpt); |
| Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 1914 | bool isImmutable = !AlwaysUseMutable && !Flags.isByVal(); |
| Anton Korobeynikov | 2247276 | 2009-08-14 18:19:10 +0000 | [diff] [blame] | 1915 | EVT ValVT; |
| 1916 | |
| 1917 | // If value is passed by pointer we have address passed instead of the value |
| 1918 | // itself. |
| 1919 | if (VA.getLocInfo() == CCValAssign::Indirect) |
| 1920 | ValVT = VA.getLocVT(); |
| 1921 | else |
| 1922 | ValVT = VA.getValVT(); |
| Evan Cheng | e70bb59 | 2008-01-10 02:24:25 +0000 | [diff] [blame] | 1923 | |
| Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1924 | // FIXME: For now, all byval parameter objects are marked mutable. This can be |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1925 | // changed with more analysis. |
| Arnold Schwaighofer | 865c681 | 2008-02-26 09:19:59 +0000 | [diff] [blame] | 1926 | // In case of tail call optimization mark all arguments mutable. Since they |
| 1927 | // could be overwritten by lowering of arguments in case of a tail call. |
| Evan Cheng | 90567c3 | 2010-02-02 23:58:13 +0000 | [diff] [blame] | 1928 | if (Flags.isByVal()) { |
| Evan Cheng | ee2e0e3 | 2011-03-30 23:44:13 +0000 | [diff] [blame] | 1929 | unsigned Bytes = Flags.getByValSize(); |
| 1930 | if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects. |
| 1931 | int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable); |
| Evan Cheng | 90567c3 | 2010-02-02 23:58:13 +0000 | [diff] [blame] | 1932 | return DAG.getFrameIndex(FI, getPointerTy()); |
| 1933 | } else { |
| 1934 | int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8, |
| Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 1935 | VA.getLocMemOffset(), isImmutable); |
| Evan Cheng | 90567c3 | 2010-02-02 23:58:13 +0000 | [diff] [blame] | 1936 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
| 1937 | return DAG.getLoad(ValVT, dl, Chain, FIN, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 1938 | MachinePointerInfo::getFixedStack(FI), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 1939 | false, false, false, 0); |
| Evan Cheng | 90567c3 | 2010-02-02 23:58:13 +0000 | [diff] [blame] | 1940 | } |
| Rafael Espindola | 7effac5 | 2007-09-14 15:48:13 +0000 | [diff] [blame] | 1941 | } |
| 1942 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1943 | SDValue |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1944 | X86TargetLowering::LowerFormalArguments(SDValue Chain, |
| Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 1945 | CallingConv::ID CallConv, |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1946 | bool isVarArg, |
| 1947 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1948 | SDLoc dl, |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1949 | SelectionDAG &DAG, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1950 | SmallVectorImpl<SDValue> &InVals) |
| 1951 | const { |
| Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1952 | MachineFunction &MF = DAG.getMachineFunction(); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1953 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1954 | |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1955 | const Function* Fn = MF.getFunction(); |
| 1956 | if (Fn->hasExternalLinkage() && |
| 1957 | Subtarget->isTargetCygMing() && |
| 1958 | Fn->getName() == "main") |
| 1959 | FuncInfo->setForceFramePointer(true); |
| 1960 | |
| Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 1961 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 1962 | bool Is64Bit = Subtarget->is64Bit(); |
| Eli Friedman | 9a2478a | 2012-01-20 00:05:46 +0000 | [diff] [blame] | 1963 | bool IsWindows = Subtarget->isTargetWindows(); |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 1964 | bool IsWin64 = Subtarget->isTargetWin64(); |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1965 | |
| Chris Lattner | 2968943 | 2010-03-11 00:22:57 +0000 | [diff] [blame] | 1966 | assert(!(isVarArg && IsTailCallConvention(CallConv)) && |
| Duncan Sands | dc7f174 | 2012-11-16 12:36:39 +0000 | [diff] [blame] | 1967 | "Var args not supported with calling convention fastcc, ghc or hipe"); |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 1968 | |
| Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 1969 | // Assign locations to all of the incoming arguments. |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1970 | SmallVector<CCValAssign, 16> ArgLocs; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 1971 | CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 1972 | ArgLocs, *DAG.getContext()); |
| NAKAMURA Takumi | 3f4be4f | 2011-02-05 15:11:32 +0000 | [diff] [blame] | 1973 | |
| 1974 | // Allocate shadow area for Win64 |
| 1975 | if (IsWin64) { |
| 1976 | CCInfo.AllocateStack(32, 8); |
| 1977 | } |
| 1978 | |
| Duncan Sands | 4590766 | 2010-10-31 13:21:44 +0000 | [diff] [blame] | 1979 | CCInfo.AnalyzeFormalArguments(Ins, CC_X86); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1980 | |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1981 | unsigned LastVal = ~0U; |
| Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 1982 | SDValue ArgValue; |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1983 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 1984 | CCValAssign &VA = ArgLocs[i]; |
| 1985 | // TODO: If an arg is passed in two places (e.g. reg and stack), skip later |
| 1986 | // places. |
| 1987 | assert(VA.getValNo() != LastVal && |
| 1988 | "Don't support value assigned to multiple locs yet"); |
| Duncan Sands | 17001ce | 2011-10-18 12:44:00 +0000 | [diff] [blame] | 1989 | (void)LastVal; |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1990 | LastVal = VA.getValNo(); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 1991 | |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 1992 | if (VA.isRegLoc()) { |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1993 | EVT RegVT = VA.getLocVT(); |
| Craig Topper | 44d2382 | 2012-02-22 05:59:10 +0000 | [diff] [blame] | 1994 | const TargetRegisterClass *RC; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1995 | if (RegVT == MVT::i32) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1996 | RC = &X86::GR32RegClass; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1997 | else if (Is64Bit && RegVT == MVT::i64) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 1998 | RC = &X86::GR64RegClass; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1999 | else if (RegVT == MVT::f32) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 2000 | RC = &X86::FR32RegClass; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2001 | else if (RegVT == MVT::f64) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 2002 | RC = &X86::FR64RegClass; |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 2003 | else if (RegVT.is256BitVector()) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 2004 | RC = &X86::VR256RegClass; |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 2005 | else if (RegVT.is128BitVector()) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 2006 | RC = &X86::VR128RegClass; |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2007 | else if (RegVT == MVT::x86mmx) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 2008 | RC = &X86::VR64RegClass; |
| Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 2009 | else |
| Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2010 | llvm_unreachable("Unknown argument type!"); |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2011 | |
| Devang Patel | 68e6bee | 2011-02-21 23:21:26 +0000 | [diff] [blame] | 2012 | unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC); |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2013 | ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2014 | |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 2015 | // If this is an 8 or 16-bit value, it is really passed promoted to 32 |
| 2016 | // bits. Insert an assert[sz]ext to capture this, then truncate to the |
| 2017 | // right size. |
| 2018 | if (VA.getLocInfo() == CCValAssign::SExt) |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2019 | ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 2020 | DAG.getValueType(VA.getValVT())); |
| 2021 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2022 | ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 2023 | DAG.getValueType(VA.getValVT())); |
| Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 2024 | else if (VA.getLocInfo() == CCValAssign::BCvt) |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2025 | ArgValue = DAG.getNode(ISD::BITCAST, dl, VA.getValVT(), ArgValue); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2026 | |
| Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 2027 | if (VA.isExtInLoc()) { |
| Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 2028 | // Handle MMX values passed in XMM regs. |
| Jakub Staszak | c20323a | 2012-12-29 15:57:26 +0000 | [diff] [blame] | 2029 | if (RegVT.isVector()) |
| 2030 | ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue); |
| 2031 | else |
| Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 2032 | ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); |
| Evan Cheng | 44c0fd1 | 2008-04-25 20:13:28 +0000 | [diff] [blame] | 2033 | } |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 2034 | } else { |
| 2035 | assert(VA.isMemLoc()); |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2036 | ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i); |
| Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 2037 | } |
| Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 2038 | |
| 2039 | // If value is passed via pointer - do a load. |
| 2040 | if (VA.getLocInfo() == CCValAssign::Indirect) |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 2041 | ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 2042 | MachinePointerInfo(), false, false, false, 0); |
| Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 2043 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2044 | InVals.push_back(ArgValue); |
| Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 2045 | } |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2046 | |
| Eli Bendersky | a5597f0 | 2013-01-25 22:07:43 +0000 | [diff] [blame] | 2047 | // The x86-64 ABIs require that for returning structs by value we copy |
| 2048 | // the sret argument into %rax/%eax (depending on ABI) for the return. |
| Timur Iskhodzhanov | a46f82d | 2013-03-28 21:30:04 +0000 | [diff] [blame] | 2049 | // Win32 requires us to put the sret argument to %eax as well. |
| Eli Bendersky | a5597f0 | 2013-01-25 22:07:43 +0000 | [diff] [blame] | 2050 | // Save the argument into a virtual register so that we can access it |
| 2051 | // from the return points. |
| Timur Iskhodzhanov | a46f82d | 2013-03-28 21:30:04 +0000 | [diff] [blame] | 2052 | if (MF.getFunction()->hasStructRetAttr() && |
| 2053 | (Subtarget->is64Bit() || Subtarget->isTargetWindows())) { |
| Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 2054 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 2055 | unsigned Reg = FuncInfo->getSRetReturnReg(); |
| 2056 | if (!Reg) { |
| Eli Bendersky | a5597f0 | 2013-01-25 22:07:43 +0000 | [diff] [blame] | 2057 | MVT PtrTy = getPointerTy(); |
| 2058 | Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy)); |
| Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 2059 | FuncInfo->setSRetReturnReg(Reg); |
| 2060 | } |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2061 | SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2062 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain); |
| Dan Gohman | 61a9213 | 2008-04-21 23:59:07 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 2065 | unsigned StackSize = CCInfo.getNextStackOffset(); |
| Evan Cheng | 0c439eb | 2010-01-27 00:07:07 +0000 | [diff] [blame] | 2066 | // Align stack specially for tail calls. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2067 | if (FuncIsMadeTailCallSafe(CallConv, |
| 2068 | MF.getTarget().Options.GuaranteedTailCallOpt)) |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2069 | StackSize = GetAlignedArgumentStackSize(StackSize, DAG); |
| Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 2070 | |
| Evan Cheng | 1bc7804 | 2006-04-26 01:20:17 +0000 | [diff] [blame] | 2071 | // If the function takes variable number of arguments, make a frame index for |
| 2072 | // the start of the first vararg value... for expansion of llvm.va_start. |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2073 | if (isVarArg) { |
| NAKAMURA Takumi | 3ca9943 | 2011-03-09 11:33:15 +0000 | [diff] [blame] | 2074 | if (Is64Bit || (CallConv != CallingConv::X86_FastCall && |
| 2075 | CallConv != CallingConv::X86_ThisCall)) { |
| Jakob Stoklund Olesen | b2eeed7 | 2010-07-29 17:42:27 +0000 | [diff] [blame] | 2076 | FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize,true)); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2077 | } |
| 2078 | if (Is64Bit) { |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2079 | unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0; |
| 2080 | |
| 2081 | // FIXME: We should really autogenerate these arrays |
| Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 2082 | static const uint16_t GPR64ArgRegsWin64[] = { |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2083 | X86::RCX, X86::RDX, X86::R8, X86::R9 |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2084 | }; |
| Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 2085 | static const uint16_t GPR64ArgRegs64Bit[] = { |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2086 | X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 |
| 2087 | }; |
| Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 2088 | static const uint16_t XMMArgRegs64Bit[] = { |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2089 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 2090 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 2091 | }; |
| Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 2092 | const uint16_t *GPR64ArgRegs; |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 2093 | unsigned NumXMMRegs = 0; |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2094 | |
| 2095 | if (IsWin64) { |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 2096 | // The XMM registers which might contain var arg parameters are shadowed |
| 2097 | // in their paired GPR. So we only need to save the GPR to their home |
| 2098 | // slots. |
| 2099 | TotalNumIntRegs = 4; |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2100 | GPR64ArgRegs = GPR64ArgRegsWin64; |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2101 | } else { |
| 2102 | TotalNumIntRegs = 6; TotalNumXMMRegs = 8; |
| 2103 | GPR64ArgRegs = GPR64ArgRegs64Bit; |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 2104 | |
| Chad Rosier | 30450e8 | 2011-12-22 22:35:21 +0000 | [diff] [blame] | 2105 | NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs64Bit, |
| 2106 | TotalNumXMMRegs); |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2107 | } |
| 2108 | unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, |
| 2109 | TotalNumIntRegs); |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2110 | |
| Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 2111 | bool NoImplicitFloatOps = Fn->getAttributes(). |
| 2112 | hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat); |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 2113 | assert(!(NumXMMRegs && !Subtarget->hasSSE1()) && |
| Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 2114 | "SSE register cannot be used when SSE is disabled!"); |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2115 | assert(!(NumXMMRegs && MF.getTarget().Options.UseSoftFloat && |
| 2116 | NoImplicitFloatOps) && |
| Evan Cheng | c7ce29b | 2009-02-13 22:36:38 +0000 | [diff] [blame] | 2117 | "SSE register cannot be used when SSE is disabled!"); |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2118 | if (MF.getTarget().Options.UseSoftFloat || NoImplicitFloatOps || |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 2119 | !Subtarget->hasSSE1()) |
| Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 2120 | // Kernel mode asks for SSE to be disabled, so don't push them |
| 2121 | // on the stack. |
| 2122 | TotalNumXMMRegs = 0; |
| Bill Wendling | f9abd7e | 2009-03-11 22:30:01 +0000 | [diff] [blame] | 2123 | |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 2124 | if (IsWin64) { |
| Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 2125 | const TargetFrameLowering &TFI = *getTargetMachine().getFrameLowering(); |
| Cameron Esfahani | ec37b00 | 2010-10-08 19:24:18 +0000 | [diff] [blame] | 2126 | // Get to the caller-allocated home save location. Add 8 to account |
| 2127 | // for the return address. |
| 2128 | int HomeOffset = TFI.getOffsetOfLocalArea() + 8; |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 2129 | FuncInfo->setRegSaveFrameIndex( |
| Cameron Esfahani | ec37b00 | 2010-10-08 19:24:18 +0000 | [diff] [blame] | 2130 | MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false)); |
| NAKAMURA Takumi | 3ca9943 | 2011-03-09 11:33:15 +0000 | [diff] [blame] | 2131 | // Fixup to set vararg frame on shadow area (4 x i64). |
| 2132 | if (NumIntRegs < 4) |
| 2133 | FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex()); |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 2134 | } else { |
| 2135 | // For X86-64, if there are vararg parameters that are passed via |
| Chad Rosier | 30450e8 | 2011-12-22 22:35:21 +0000 | [diff] [blame] | 2136 | // registers, then we must store them to their spots on the stack so |
| 2137 | // they may be loaded by deferencing the result of va_next. |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 2138 | FuncInfo->setVarArgsGPOffset(NumIntRegs * 8); |
| 2139 | FuncInfo->setVarArgsFPOffset(TotalNumIntRegs * 8 + NumXMMRegs * 16); |
| 2140 | FuncInfo->setRegSaveFrameIndex( |
| 2141 | MFI->CreateStackObject(TotalNumIntRegs * 8 + TotalNumXMMRegs * 16, 16, |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2142 | false)); |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 2143 | } |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2144 | |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2145 | // Store the integer parameter registers. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2146 | SmallVector<SDValue, 8> MemOps; |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2147 | SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), |
| 2148 | getPointerTy()); |
| 2149 | unsigned Offset = FuncInfo->getVarArgsGPOffset(); |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2150 | for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) { |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 2151 | SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN, |
| 2152 | DAG.getIntPtrConstant(Offset)); |
| Bob Wilson | 998e125 | 2009-04-20 18:36:57 +0000 | [diff] [blame] | 2153 | unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs], |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 2154 | &X86::GR64RegClass); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2155 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2156 | SDValue Store = |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2157 | DAG.getStore(Val.getValue(1), dl, Val, FIN, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 2158 | MachinePointerInfo::getFixedStack( |
| 2159 | FuncInfo->getRegSaveFrameIndex(), Offset), |
| 2160 | false, false, 0); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2161 | MemOps.push_back(Store); |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 2162 | Offset += 8; |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2163 | } |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2164 | |
| Dan Gohman | face41a | 2009-08-16 21:24:25 +0000 | [diff] [blame] | 2165 | if (TotalNumXMMRegs != 0 && NumXMMRegs != TotalNumXMMRegs) { |
| 2166 | // Now store the XMM (fp + vector) parameter registers. |
| 2167 | SmallVector<SDValue, 11> SaveXMMOps; |
| 2168 | SaveXMMOps.push_back(Chain); |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 2169 | |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 2170 | unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass); |
| Dan Gohman | face41a | 2009-08-16 21:24:25 +0000 | [diff] [blame] | 2171 | SDValue ALVal = DAG.getCopyFromReg(DAG.getEntryNode(), dl, AL, MVT::i8); |
| 2172 | SaveXMMOps.push_back(ALVal); |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 2173 | |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2174 | SaveXMMOps.push_back(DAG.getIntPtrConstant( |
| 2175 | FuncInfo->getRegSaveFrameIndex())); |
| 2176 | SaveXMMOps.push_back(DAG.getIntPtrConstant( |
| 2177 | FuncInfo->getVarArgsFPOffset())); |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 2178 | |
| Dan Gohman | face41a | 2009-08-16 21:24:25 +0000 | [diff] [blame] | 2179 | for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) { |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 2180 | unsigned VReg = MF.addLiveIn(XMMArgRegs64Bit[NumXMMRegs], |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 2181 | &X86::VR128RegClass); |
| Dan Gohman | face41a | 2009-08-16 21:24:25 +0000 | [diff] [blame] | 2182 | SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32); |
| 2183 | SaveXMMOps.push_back(Val); |
| 2184 | } |
| 2185 | MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl, |
| 2186 | MVT::Other, |
| 2187 | &SaveXMMOps[0], SaveXMMOps.size())); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2188 | } |
| Dan Gohman | face41a | 2009-08-16 21:24:25 +0000 | [diff] [blame] | 2189 | |
| 2190 | if (!MemOps.empty()) |
| 2191 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 2192 | &MemOps[0], MemOps.size()); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2193 | } |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2194 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2195 | |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2196 | // Some CCs need callee pop. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2197 | if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, |
| 2198 | MF.getTarget().Options.GuaranteedTailCallOpt)) { |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2199 | FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything. |
| Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 2200 | } else { |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2201 | FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing. |
| Chris Lattner | f39f771 | 2007-02-28 05:46:49 +0000 | [diff] [blame] | 2202 | // If this is an sret function, the return should pop the hidden pointer. |
| Eli Friedman | 9a2478a | 2012-01-20 00:05:46 +0000 | [diff] [blame] | 2203 | if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows && |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 2204 | argsAreStructReturn(Ins) == StackStructReturn) |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2205 | FuncInfo->setBytesToPopOnReturn(4); |
| Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 2206 | } |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2207 | |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2208 | if (!Is64Bit) { |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2209 | // RegSaveFrameIndex is X86-64 only. |
| 2210 | FuncInfo->setRegSaveFrameIndex(0xAAAAAAA); |
| Anton Korobeynikov | ded05e3 | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 2211 | if (CallConv == CallingConv::X86_FastCall || |
| 2212 | CallConv == CallingConv::X86_ThisCall) |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 2213 | // fastcc functions can't have varargs. |
| 2214 | FuncInfo->setVarArgsFrameIndex(0xAAAAAAA); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2215 | } |
| Evan Cheng | 25caf63 | 2006-05-23 21:06:34 +0000 | [diff] [blame] | 2216 | |
| Rafael Espindola | 76927d75 | 2011-08-30 19:39:58 +0000 | [diff] [blame] | 2217 | FuncInfo->setArgumentStackSize(StackSize); |
| 2218 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2219 | return Chain; |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2222 | SDValue |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2223 | X86TargetLowering::LowerMemOpCallTo(SDValue Chain, |
| 2224 | SDValue StackPtr, SDValue Arg, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2225 | SDLoc dl, SelectionDAG &DAG, |
| Evan Cheng | dffbd83 | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 2226 | const CCValAssign &VA, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 2227 | ISD::ArgFlagsTy Flags) const { |
| NAKAMURA Takumi | 3f4be4f | 2011-02-05 15:11:32 +0000 | [diff] [blame] | 2228 | unsigned LocMemOffset = VA.getLocMemOffset(); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2229 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2230 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff); |
| Chris Lattner | fc448ff | 2010-09-21 18:51:21 +0000 | [diff] [blame] | 2231 | if (Flags.isByVal()) |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 2232 | return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl); |
| Chris Lattner | fc448ff | 2010-09-21 18:51:21 +0000 | [diff] [blame] | 2233 | |
| 2234 | return DAG.getStore(Chain, dl, Arg, PtrOff, |
| 2235 | MachinePointerInfo::getStack(LocMemOffset), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 2236 | false, false, 0); |
| Evan Cheng | dffbd83 | 2008-01-10 00:09:10 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 2239 | /// EmitTailCallLoadRetAddr - Emit a load of return address if tail call |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2240 | /// optimization is performed and it is required. |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2241 | SDValue |
| 2242 | X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG, |
| Evan Cheng | ddc419c | 2010-01-26 19:04:47 +0000 | [diff] [blame] | 2243 | SDValue &OutRetAddr, SDValue Chain, |
| 2244 | bool IsTailCall, bool Is64Bit, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2245 | int FPDiff, SDLoc dl) const { |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2246 | // Adjust the Return address stack slot. |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2247 | EVT VT = getPointerTy(); |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2248 | OutRetAddr = getReturnAddressFrameIndex(DAG); |
| Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 2249 | |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2250 | // Load the "old" Return address. |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 2251 | OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 2252 | false, false, false, 0); |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2253 | return SDValue(OutRetAddr.getNode(), 1); |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 2256 | /// EmitTailCallStoreRetAddr - Emit a store of the return address if tail call |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2257 | /// optimization is performed and it is required (FPDiff!=0). |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2258 | static SDValue |
| 2259 | EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF, |
| Michael Liao | aa3c2c0 | 2012-10-25 06:29:14 +0000 | [diff] [blame] | 2260 | SDValue Chain, SDValue RetAddrFrIdx, EVT PtrVT, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2261 | unsigned SlotSize, int FPDiff, SDLoc dl) { |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2262 | // Store the return address to the appropriate stack slot. |
| 2263 | if (!FPDiff) return Chain; |
| 2264 | // Calculate the new stack slot for the return address. |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2265 | int NewReturnAddrFI = |
| Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 2266 | MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize, false); |
| Michael Liao | aa3c2c0 | 2012-10-25 06:29:14 +0000 | [diff] [blame] | 2267 | SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2268 | Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 2269 | MachinePointerInfo::getFixedStack(NewReturnAddrFI), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 2270 | false, false, 0); |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2271 | return Chain; |
| 2272 | } |
| 2273 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2274 | SDValue |
| Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 2275 | X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 2276 | SmallVectorImpl<SDValue> &InVals) const { |
| Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 2277 | SelectionDAG &DAG = CLI.DAG; |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2278 | SDLoc &dl = CLI.DL; |
| Justin Holewinski | d2ea0e1 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 2279 | SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs; |
| 2280 | SmallVector<SDValue, 32> &OutVals = CLI.OutVals; |
| 2281 | SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins; |
| 2282 | SDValue Chain = CLI.Chain; |
| 2283 | SDValue Callee = CLI.Callee; |
| 2284 | CallingConv::ID CallConv = CLI.CallConv; |
| 2285 | bool &isTailCall = CLI.IsTailCall; |
| 2286 | bool isVarArg = CLI.IsVarArg; |
| 2287 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2288 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2289 | bool Is64Bit = Subtarget->is64Bit(); |
| NAKAMURA Takumi | fb840c9 | 2011-02-05 15:11:13 +0000 | [diff] [blame] | 2290 | bool IsWin64 = Subtarget->isTargetWin64(); |
| Eli Friedman | 9a2478a | 2012-01-20 00:05:46 +0000 | [diff] [blame] | 2291 | bool IsWindows = Subtarget->isTargetWindows(); |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 2292 | StructReturnType SR = callIsStructReturn(Outs); |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2293 | bool IsSibcall = false; |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2294 | |
| Nick Lewycky | 22de16d | 2012-01-19 00:34:10 +0000 | [diff] [blame] | 2295 | if (MF.getTarget().Options.DisableTailCalls) |
| 2296 | isTailCall = false; |
| 2297 | |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2298 | if (isTailCall) { |
| Evan Cheng | 0c439eb | 2010-01-27 00:07:07 +0000 | [diff] [blame] | 2299 | // Check if it's really possible to do a tail call. |
| Evan Cheng | a375d47 | 2010-03-15 18:54:48 +0000 | [diff] [blame] | 2300 | isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv, |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 2301 | isVarArg, SR != NotStructReturn, |
| Evan Cheng | b1cacc7 | 2012-09-25 05:32:34 +0000 | [diff] [blame] | 2302 | MF.getFunction()->hasStructRetAttr(), CLI.RetTy, |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 2303 | Outs, OutVals, Ins, DAG); |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2304 | |
| 2305 | // Sibcalls are automatically detected tailcalls which do not require |
| 2306 | // ABI changes. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2307 | if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall) |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2308 | IsSibcall = true; |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2309 | |
| 2310 | if (isTailCall) |
| 2311 | ++NumTailCalls; |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2312 | } |
| Evan Cheng | 0c439eb | 2010-01-27 00:07:07 +0000 | [diff] [blame] | 2313 | |
| Chris Lattner | 2968943 | 2010-03-11 00:22:57 +0000 | [diff] [blame] | 2314 | assert(!(isVarArg && IsTailCallConvention(CallConv)) && |
| Duncan Sands | dc7f174 | 2012-11-16 12:36:39 +0000 | [diff] [blame] | 2315 | "Var args not supported with calling convention fastcc, ghc or hipe"); |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2316 | |
| Chris Lattner | 638402b | 2007-02-28 07:00:42 +0000 | [diff] [blame] | 2317 | // Analyze operands of the call, assigning locations to each operand. |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2318 | SmallVector<CCValAssign, 16> ArgLocs; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 2319 | CCState CCInfo(CallConv, isVarArg, MF, getTargetMachine(), |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2320 | ArgLocs, *DAG.getContext()); |
| NAKAMURA Takumi | 3f4be4f | 2011-02-05 15:11:32 +0000 | [diff] [blame] | 2321 | |
| 2322 | // Allocate shadow area for Win64 |
| 2323 | if (IsWin64) { |
| 2324 | CCInfo.AllocateStack(32, 8); |
| 2325 | } |
| 2326 | |
| Duncan Sands | 4590766 | 2010-10-31 13:21:44 +0000 | [diff] [blame] | 2327 | CCInfo.AnalyzeCallOperands(Outs, CC_X86); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2328 | |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2329 | // Get a count of how many bytes are to be pushed on the stack. |
| 2330 | unsigned NumBytes = CCInfo.getNextStackOffset(); |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2331 | if (IsSibcall) |
| Evan Cheng | b2c9290 | 2010-02-02 02:22:50 +0000 | [diff] [blame] | 2332 | // This is a sibcall. The memory operands are available in caller's |
| 2333 | // own caller's stack. |
| 2334 | NumBytes = 0; |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2335 | else if (getTargetMachine().Options.GuaranteedTailCallOpt && |
| 2336 | IsTailCallConvention(CallConv)) |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2337 | NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2338 | |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2339 | int FPDiff = 0; |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2340 | if (isTailCall && !IsSibcall) { |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2341 | // Lower arguments at fp - stackoffset + fpdiff. |
| Jakub Staszak | 96df437 | 2012-10-29 22:02:26 +0000 | [diff] [blame] | 2342 | X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>(); |
| 2343 | unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn(); |
| 2344 | |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2345 | FPDiff = NumBytesCallerPushed - NumBytes; |
| 2346 | |
| 2347 | // Set the delta of movement of the returnaddr stackslot. |
| 2348 | // But only set if delta is greater than previous delta. |
| Jakub Staszak | 96df437 | 2012-10-29 22:02:26 +0000 | [diff] [blame] | 2349 | if (FPDiff < X86Info->getTCReturnAddrDelta()) |
| 2350 | X86Info->setTCReturnAddrDelta(FPDiff); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2353 | if (!IsSibcall) |
| Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 2354 | Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| 2355 | dl); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2356 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2357 | SDValue RetAddrFrIdx; |
| Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 2358 | // Load return address for tail calls. |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2359 | if (isTailCall && FPDiff) |
| 2360 | Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, |
| 2361 | Is64Bit, FPDiff, dl); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2362 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2363 | SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; |
| 2364 | SmallVector<SDValue, 8> MemOpChains; |
| 2365 | SDValue StackPtr; |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2366 | |
| Arnold Schwaighofer | 30e62c0 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 2367 | // Walk the register/memloc assignments, inserting copies/loads. In the case |
| 2368 | // of tail call optimization arguments are handle later. |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 2369 | const X86RegisterInfo *RegInfo = |
| 2370 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2371 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 2372 | CCValAssign &VA = ArgLocs[i]; |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 2373 | EVT RegVT = VA.getLocVT(); |
| Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 2374 | SDValue Arg = OutVals[i]; |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2375 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| Dan Gohman | 095cc29 | 2008-09-13 01:54:27 +0000 | [diff] [blame] | 2376 | bool isByVal = Flags.isByVal(); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2377 | |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2378 | // Promote the value if needed. |
| 2379 | switch (VA.getLocInfo()) { |
| Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2380 | default: llvm_unreachable("Unknown loc info!"); |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2381 | case CCValAssign::Full: break; |
| 2382 | case CCValAssign::SExt: |
| Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 2383 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg); |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2384 | break; |
| 2385 | case CCValAssign::ZExt: |
| Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 2386 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg); |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2387 | break; |
| 2388 | case CCValAssign::AExt: |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 2389 | if (RegVT.is128BitVector()) { |
| Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 2390 | // Special case: passing MMX values in XMM registers. |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2391 | Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i64, Arg); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2392 | Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg); |
| 2393 | Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg); |
| Anton Korobeynikov | 80cb8aa | 2009-08-03 08:13:24 +0000 | [diff] [blame] | 2394 | } else |
| 2395 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg); |
| 2396 | break; |
| 2397 | case CCValAssign::BCvt: |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 2398 | Arg = DAG.getNode(ISD::BITCAST, dl, RegVT, Arg); |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2399 | break; |
| Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 2400 | case CCValAssign::Indirect: { |
| 2401 | // Store the argument. |
| 2402 | SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT()); |
| Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 2403 | int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); |
| Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 2404 | Chain = DAG.getStore(Chain, dl, Arg, SpillSlot, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 2405 | MachinePointerInfo::getFixedStack(FI), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 2406 | false, false, 0); |
| Anton Korobeynikov | 4ab1553 | 2009-08-03 08:13:56 +0000 | [diff] [blame] | 2407 | Arg = SpillSlot; |
| 2408 | break; |
| 2409 | } |
| Evan Cheng | 6b5783d | 2006-05-25 18:56:34 +0000 | [diff] [blame] | 2410 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2411 | |
| Chris Lattner | 423c5f4 | 2007-02-28 05:31:48 +0000 | [diff] [blame] | 2412 | if (VA.isRegLoc()) { |
| Stuart Hastings | 2aa0f23 | 2011-05-26 04:09:49 +0000 | [diff] [blame] | 2413 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); |
| 2414 | if (isVarArg && IsWin64) { |
| 2415 | // Win64 ABI requires argument XMM reg to be copied to the corresponding |
| 2416 | // shadow reg if callee is a varargs function. |
| 2417 | unsigned ShadowReg = 0; |
| 2418 | switch (VA.getLocReg()) { |
| 2419 | case X86::XMM0: ShadowReg = X86::RCX; break; |
| 2420 | case X86::XMM1: ShadowReg = X86::RDX; break; |
| 2421 | case X86::XMM2: ShadowReg = X86::R8; break; |
| 2422 | case X86::XMM3: ShadowReg = X86::R9; break; |
| Anton Korobeynikov | c52bedb | 2010-08-27 14:43:06 +0000 | [diff] [blame] | 2423 | } |
| Stuart Hastings | 2aa0f23 | 2011-05-26 04:09:49 +0000 | [diff] [blame] | 2424 | if (ShadowReg) |
| 2425 | RegsToPass.push_back(std::make_pair(ShadowReg, Arg)); |
| Anton Korobeynikov | c52bedb | 2010-08-27 14:43:06 +0000 | [diff] [blame] | 2426 | } |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2427 | } else if (!IsSibcall && (!isTailCall || isByVal)) { |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2428 | assert(VA.isMemLoc()); |
| 2429 | if (StackPtr.getNode() == 0) |
| Michael Liao | c5c970e | 2012-10-31 04:14:09 +0000 | [diff] [blame] | 2430 | StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(), |
| 2431 | getPointerTy()); |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2432 | MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, |
| 2433 | dl, DAG, VA, Flags)); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2434 | } |
| Stuart Hastings | 2aa0f23 | 2011-05-26 04:09:49 +0000 | [diff] [blame] | 2435 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2436 | |
| Evan Cheng | 32fe103 | 2006-05-25 00:59:30 +0000 | [diff] [blame] | 2437 | if (!MemOpChains.empty()) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2438 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| Chris Lattner | bd564bf | 2006-08-08 02:23:42 +0000 | [diff] [blame] | 2439 | &MemOpChains[0], MemOpChains.size()); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2440 | |
| Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 2441 | if (Subtarget->isPICStyleGOT()) { |
| Chris Lattner | b133a0a | 2009-07-09 02:55:47 +0000 | [diff] [blame] | 2442 | // ELF / PIC requires GOT in the EBX register before function calls via PLT |
| 2443 | // GOT pointer. |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2444 | if (!isTailCall) { |
| Jakob Stoklund Olesen | b872078 | 2012-07-04 19:28:31 +0000 | [diff] [blame] | 2445 | RegsToPass.push_back(std::make_pair(unsigned(X86::EBX), |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2446 | DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), getPointerTy()))); |
| Chris Lattner | b133a0a | 2009-07-09 02:55:47 +0000 | [diff] [blame] | 2447 | } else { |
| 2448 | // If we are tail calling and generating PIC/GOT style code load the |
| 2449 | // address of the callee into ECX. The value in ecx is used as target of |
| 2450 | // the tail jump. This is done to circumvent the ebx/callee-saved problem |
| 2451 | // for tail calls on PIC/GOT architectures. Normally we would just put the |
| 2452 | // address of GOT into ebx and then call target@PLT. But for tail calls |
| 2453 | // ebx would be restored (since ebx is callee saved) before jumping to the |
| 2454 | // target@PLT. |
| 2455 | |
| 2456 | // Note: The actual moving to ECX is done further down. |
| 2457 | GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee); |
| 2458 | if (G && !G->getGlobal()->hasHiddenVisibility() && |
| 2459 | !G->getGlobal()->hasProtectedVisibility()) |
| 2460 | Callee = LowerGlobalAddress(Callee, DAG); |
| 2461 | else if (isa<ExternalSymbolSDNode>(Callee)) |
| Chris Lattner | 15a380a | 2009-07-09 04:39:06 +0000 | [diff] [blame] | 2462 | Callee = LowerExternalSymbol(Callee, DAG); |
| Chris Lattner | b133a0a | 2009-07-09 02:55:47 +0000 | [diff] [blame] | 2463 | } |
| Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 2464 | } |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2465 | |
| NAKAMURA Takumi | fb840c9 | 2011-02-05 15:11:13 +0000 | [diff] [blame] | 2466 | if (Is64Bit && isVarArg && !IsWin64) { |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2467 | // From AMD64 ABI document: |
| 2468 | // For calls that may call functions that use varargs or stdargs |
| 2469 | // (prototype-less calls or calls to functions containing ellipsis (...) in |
| 2470 | // the declaration) %al is used as hidden argument to specify the number |
| 2471 | // of SSE registers used. The contents of %al do not need to match exactly |
| 2472 | // the number of registers, but must be an ubound on the number of SSE |
| 2473 | // registers used and is in the range 0 - 8 inclusive. |
| Anton Korobeynikov | 998a5bc | 2008-04-27 23:15:03 +0000 | [diff] [blame] | 2474 | |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2475 | // Count the number of XMM registers allocated. |
| Craig Topper | c5eaae4 | 2012-03-11 07:57:25 +0000 | [diff] [blame] | 2476 | static const uint16_t XMMArgRegs[] = { |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2477 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, |
| 2478 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 |
| 2479 | }; |
| 2480 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8); |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 2481 | assert((Subtarget->hasSSE1() || !NumXMMRegs) |
| Torok Edwin | 3f142c3 | 2009-02-01 18:15:56 +0000 | [diff] [blame] | 2482 | && "SSE registers cannot be used when SSE is disabled"); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2483 | |
| Jakob Stoklund Olesen | b872078 | 2012-07-04 19:28:31 +0000 | [diff] [blame] | 2484 | RegsToPass.push_back(std::make_pair(unsigned(X86::AL), |
| 2485 | DAG.getConstant(NumXMMRegs, MVT::i8))); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
| Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 2488 | // For tail calls lower the arguments to the 'real' stack slot. |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2489 | if (isTailCall) { |
| 2490 | // Force all the incoming stack arguments to be loaded from the stack |
| 2491 | // before any new outgoing arguments are stored to the stack, because the |
| 2492 | // outgoing stack slots may alias the incoming argument stack slots, and |
| 2493 | // the alias isn't otherwise explicit. This is slightly more conservative |
| 2494 | // than necessary, because it means that each store effectively depends |
| 2495 | // on every argument instead of just those arguments it would clobber. |
| 2496 | SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain); |
| 2497 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2498 | SmallVector<SDValue, 8> MemOpChains2; |
| 2499 | SDValue FIN; |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2500 | int FI = 0; |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2501 | if (getTargetMachine().Options.GuaranteedTailCallOpt) { |
| Evan Cheng | b2c9290 | 2010-02-02 02:22:50 +0000 | [diff] [blame] | 2502 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 2503 | CCValAssign &VA = ArgLocs[i]; |
| 2504 | if (VA.isRegLoc()) |
| 2505 | continue; |
| Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 2506 | assert(VA.isMemLoc()); |
| Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 2507 | SDValue Arg = OutVals[i]; |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2508 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2509 | // Create frame index. |
| 2510 | int32_t Offset = VA.getLocMemOffset()+FPDiff; |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 2511 | uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8; |
| Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 2512 | FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true); |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2513 | FIN = DAG.getFrameIndex(FI, getPointerTy()); |
| Arnold Schwaighofer | c8ab8cd | 2008-01-11 16:49:42 +0000 | [diff] [blame] | 2514 | |
| Duncan Sands | 276dcbd | 2008-03-21 09:14:45 +0000 | [diff] [blame] | 2515 | if (Flags.isByVal()) { |
| Evan Cheng | 8e5712b | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 2516 | // Copy relative to framepointer. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2517 | SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset()); |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2518 | if (StackPtr.getNode() == 0) |
| Michael Liao | c5c970e | 2012-10-31 04:14:09 +0000 | [diff] [blame] | 2519 | StackPtr = DAG.getCopyFromReg(Chain, dl, |
| 2520 | RegInfo->getStackRegister(), |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 2521 | getPointerTy()); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2522 | Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source); |
| Arnold Schwaighofer | 4b5324a | 2008-04-12 18:11:06 +0000 | [diff] [blame] | 2523 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2524 | MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, |
| 2525 | ArgChain, |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 2526 | Flags, DAG, dl)); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2527 | } else { |
| Evan Cheng | 8e5712b | 2008-01-12 01:08:07 +0000 | [diff] [blame] | 2528 | // Store relative to framepointer. |
| Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 2529 | MemOpChains2.push_back( |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2530 | DAG.getStore(ArgChain, dl, Arg, FIN, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 2531 | MachinePointerInfo::getFixedStack(FI), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 2532 | false, false, 0)); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2533 | } |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2534 | } |
| 2535 | } |
| 2536 | |
| 2537 | if (!MemOpChains2.empty()) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2538 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| Arnold Schwaighofer | 719eb02 | 2008-01-11 14:34:56 +0000 | [diff] [blame] | 2539 | &MemOpChains2[0], MemOpChains2.size()); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2540 | |
| 2541 | // Store the return address to the appropriate stack slot. |
| Michael Liao | aa3c2c0 | 2012-10-25 06:29:14 +0000 | [diff] [blame] | 2542 | Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, |
| 2543 | getPointerTy(), RegInfo->getSlotSize(), |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2544 | FPDiff, dl); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| Jakob Stoklund Olesen | b872078 | 2012-07-04 19:28:31 +0000 | [diff] [blame] | 2547 | // Build a sequence of copy-to-reg nodes chained together with token chain |
| 2548 | // and flag operands which copy the outgoing args into registers. |
| 2549 | SDValue InFlag; |
| 2550 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { |
| 2551 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, |
| 2552 | RegsToPass[i].second, InFlag); |
| 2553 | InFlag = Chain.getValue(1); |
| 2554 | } |
| 2555 | |
| Jeffrey Yasskin | d1ba06b | 2009-11-16 22:41:33 +0000 | [diff] [blame] | 2556 | if (getTargetMachine().getCodeModel() == CodeModel::Large) { |
| 2557 | assert(Is64Bit && "Large code model is only legal in 64-bit mode."); |
| 2558 | // In the 64-bit large code model, we have to make all calls |
| 2559 | // through a register, since the call instruction's 32-bit |
| 2560 | // pc-relative offset may not be large enough to hold the whole |
| 2561 | // address. |
| 2562 | } else if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
| Jeffrey Yasskin | d1ba06b | 2009-11-16 22:41:33 +0000 | [diff] [blame] | 2563 | // If the callee is a GlobalAddress node (quite common, every direct call |
| 2564 | // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack |
| 2565 | // it. |
| 2566 | |
| Anton Korobeynikov | 2b2bc68 | 2006-12-22 22:29:05 +0000 | [diff] [blame] | 2567 | // We should use extra load for direct calls to dllimported functions in |
| 2568 | // non-JIT mode. |
| Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 2569 | const GlobalValue *GV = G->getGlobal(); |
| Chris Lattner | 754b765 | 2009-07-10 05:48:03 +0000 | [diff] [blame] | 2570 | if (!GV->hasDLLImportLinkage()) { |
| Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 2571 | unsigned char OpFlags = 0; |
| John McCall | 3a3465b | 2011-06-15 20:36:13 +0000 | [diff] [blame] | 2572 | bool ExtraLoad = false; |
| 2573 | unsigned WrapperKind = ISD::DELETED_NODE; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 2574 | |
| Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 2575 | // On ELF targets, in both X86-64 and X86-32 mode, direct calls to |
| 2576 | // external symbols most go through the PLT in PIC mode. If the symbol |
| 2577 | // has hidden or protected visibility, or if it is static or local, then |
| 2578 | // we don't need to use the PLT - we can directly call it. |
| 2579 | if (Subtarget->isTargetELF() && |
| 2580 | getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 2581 | GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) { |
| Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 2582 | OpFlags = X86II::MO_PLT; |
| Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 2583 | } else if (Subtarget->isPICStyleStubAny() && |
| Chris Lattner | 8094578 | 2010-09-27 06:34:01 +0000 | [diff] [blame] | 2584 | (GV->isDeclaration() || GV->isWeakForLinker()) && |
| Daniel Dunbar | 558692f | 2011-04-20 00:14:25 +0000 | [diff] [blame] | 2585 | (!Subtarget->getTargetTriple().isMacOSX() || |
| 2586 | Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) { |
| Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 2587 | // PC-relative references to external symbols should go through $stub, |
| 2588 | // unless we're building with the leopard linker or later, which |
| 2589 | // automatically synthesizes these stubs. |
| 2590 | OpFlags = X86II::MO_DARWIN_STUB; |
| John McCall | 3a3465b | 2011-06-15 20:36:13 +0000 | [diff] [blame] | 2591 | } else if (Subtarget->isPICStyleRIPRel() && |
| 2592 | isa<Function>(GV) && |
| Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 2593 | cast<Function>(GV)->getAttributes(). |
| 2594 | hasAttribute(AttributeSet::FunctionIndex, |
| 2595 | Attribute::NonLazyBind)) { |
| John McCall | 3a3465b | 2011-06-15 20:36:13 +0000 | [diff] [blame] | 2596 | // If the function is marked as non-lazy, generate an indirect call |
| 2597 | // which loads from the GOT directly. This avoids runtime overhead |
| 2598 | // at the cost of eager binding (and one extra byte of encoding). |
| 2599 | OpFlags = X86II::MO_GOTPCREL; |
| 2600 | WrapperKind = X86ISD::WrapperRIP; |
| 2601 | ExtraLoad = true; |
| Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 2602 | } |
| Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 2603 | |
| Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 2604 | Callee = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), |
| Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 2605 | G->getOffset(), OpFlags); |
| John McCall | 3a3465b | 2011-06-15 20:36:13 +0000 | [diff] [blame] | 2606 | |
| 2607 | // Add a wrapper if needed. |
| 2608 | if (WrapperKind != ISD::DELETED_NODE) |
| 2609 | Callee = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Callee); |
| 2610 | // Add extra indirection if needed. |
| 2611 | if (ExtraLoad) |
| 2612 | Callee = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Callee, |
| 2613 | MachinePointerInfo::getGOT(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 2614 | false, false, false, 0); |
| Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 2615 | } |
| Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 2616 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
| Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 2617 | unsigned char OpFlags = 0; |
| 2618 | |
| Evan Cheng | 1bf891a | 2010-12-01 22:59:46 +0000 | [diff] [blame] | 2619 | // On ELF targets, in either X86-64 or X86-32 mode, direct calls to |
| 2620 | // external symbols should go through the PLT. |
| 2621 | if (Subtarget->isTargetELF() && |
| 2622 | getTargetMachine().getRelocationModel() == Reloc::PIC_) { |
| 2623 | OpFlags = X86II::MO_PLT; |
| 2624 | } else if (Subtarget->isPICStyleStubAny() && |
| Daniel Dunbar | 558692f | 2011-04-20 00:14:25 +0000 | [diff] [blame] | 2625 | (!Subtarget->getTargetTriple().isMacOSX() || |
| 2626 | Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) { |
| Evan Cheng | 1bf891a | 2010-12-01 22:59:46 +0000 | [diff] [blame] | 2627 | // PC-relative references to external symbols should go through $stub, |
| 2628 | // unless we're building with the leopard linker or later, which |
| 2629 | // automatically synthesizes these stubs. |
| 2630 | OpFlags = X86II::MO_DARWIN_STUB; |
| Chris Lattner | 74e726e | 2009-07-09 05:27:35 +0000 | [diff] [blame] | 2631 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 2632 | |
| Chris Lattner | 48a7d02 | 2009-07-09 05:02:21 +0000 | [diff] [blame] | 2633 | Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(), |
| 2634 | OpFlags); |
| Jeffrey Yasskin | d1ba06b | 2009-11-16 22:41:33 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
| Chris Lattner | d96d072 | 2007-02-25 06:40:16 +0000 | [diff] [blame] | 2637 | // Returns a chain & a flag for retval copy to use. |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 2638 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 2639 | SmallVector<SDValue, 8> Ops; |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2640 | |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2641 | if (!IsSibcall && isTailCall) { |
| Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 2642 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true), |
| Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 2643 | DAG.getIntPtrConstant(0, true), InFlag, dl); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2644 | InFlag = Chain.getValue(1); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2645 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2646 | |
| Nate Begeman | 4c5dcf5 | 2006-02-17 00:03:04 +0000 | [diff] [blame] | 2647 | Ops.push_back(Chain); |
| 2648 | Ops.push_back(Callee); |
| Evan Cheng | b69d113 | 2006-06-14 18:17:40 +0000 | [diff] [blame] | 2649 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2650 | if (isTailCall) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2651 | Ops.push_back(DAG.getConstant(FPDiff, MVT::i32)); |
| Evan Cheng | f468471 | 2007-02-21 21:18:14 +0000 | [diff] [blame] | 2652 | |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2653 | // Add argument registers to the end of the list so that they are known live |
| 2654 | // into the call. |
| Evan Cheng | 9b44944 | 2008-01-07 23:08:23 +0000 | [diff] [blame] | 2655 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) |
| 2656 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, |
| 2657 | RegsToPass[i].second.getValueType())); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2658 | |
| Jakob Stoklund Olesen | 8bcde2a | 2012-02-16 00:02:50 +0000 | [diff] [blame] | 2659 | // Add a register mask operand representing the call-preserved registers. |
| 2660 | const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo(); |
| 2661 | const uint32_t *Mask = TRI->getCallPreservedMask(CallConv); |
| 2662 | assert(Mask && "Missing call preserved mask for calling convention"); |
| 2663 | Ops.push_back(DAG.getRegisterMask(Mask)); |
| Jakob Stoklund Olesen | c38c456 | 2012-01-18 23:52:22 +0000 | [diff] [blame] | 2664 | |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 2665 | if (InFlag.getNode()) |
| Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 2666 | Ops.push_back(InFlag); |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2667 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2668 | if (isTailCall) { |
| Dale Johannesen | 88004c2 | 2010-06-05 00:30:45 +0000 | [diff] [blame] | 2669 | // We used to do: |
| 2670 | //// If this is the first return lowered for this function, add the regs |
| 2671 | //// to the liveout set for the function. |
| 2672 | // This isn't right, although it's probably harmless on x86; liveouts |
| 2673 | // should be computed from returns not tail calls. Consider a void |
| 2674 | // function making a tail call to a function returning int. |
| Jakub Staszak | 30fcfc3 | 2013-02-16 13:34:26 +0000 | [diff] [blame] | 2675 | return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, &Ops[0], Ops.size()); |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 2678 | Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size()); |
| Evan Cheng | 347d5f7 | 2006-04-28 21:29:37 +0000 | [diff] [blame] | 2679 | InFlag = Chain.getValue(1); |
| Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 2680 | |
| Chris Lattner | 2d29709 | 2006-05-23 18:50:38 +0000 | [diff] [blame] | 2681 | // Create the CALLSEQ_END node. |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2682 | unsigned NumBytesForCalleeToPush; |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2683 | if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, |
| 2684 | getTargetMachine().Options.GuaranteedTailCallOpt)) |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2685 | NumBytesForCalleeToPush = NumBytes; // Callee pops everything |
| Eli Friedman | 9a2478a | 2012-01-20 00:05:46 +0000 | [diff] [blame] | 2686 | else if (!Is64Bit && !IsTailCallConvention(CallConv) && !IsWindows && |
| Rafael Espindola | 1cee710 | 2012-07-25 13:41:10 +0000 | [diff] [blame] | 2687 | SR == StackStructReturn) |
| Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 2688 | // If this is a call to a struct-return function, the callee |
| Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 2689 | // pops the hidden struct pointer, so we have to push it back. |
| 2690 | // This is common for Darwin/X86, Linux & Mingw32 targets. |
| Eli Friedman | 9a2478a | 2012-01-20 00:05:46 +0000 | [diff] [blame] | 2691 | // For MSVC Win32 targets, the caller pops the hidden struct pointer. |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2692 | NumBytesForCalleeToPush = 4; |
| Gordon Henriksen | 8673766 | 2008-01-05 16:56:59 +0000 | [diff] [blame] | 2693 | else |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2694 | NumBytesForCalleeToPush = 0; // Callee pops nothing. |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2695 | |
| Gordon Henriksen | ae636f8 | 2008-01-03 16:47:34 +0000 | [diff] [blame] | 2696 | // Returns a flag for retval copy to use. |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2697 | if (!IsSibcall) { |
| 2698 | Chain = DAG.getCALLSEQ_END(Chain, |
| 2699 | DAG.getIntPtrConstant(NumBytes, true), |
| 2700 | DAG.getIntPtrConstant(NumBytesForCalleeToPush, |
| 2701 | true), |
| Andrew Trick | 6e0b2a0 | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 2702 | InFlag, dl); |
| Evan Cheng | f22f9b3 | 2010-02-06 03:28:46 +0000 | [diff] [blame] | 2703 | InFlag = Chain.getValue(1); |
| 2704 | } |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 2705 | |
| Chris Lattner | 3085e15 | 2007-02-25 08:59:22 +0000 | [diff] [blame] | 2706 | // Handle result values, copying them out of physregs into vregs that we |
| 2707 | // return. |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2708 | return LowerCallResult(Chain, InFlag, CallConv, isVarArg, |
| 2709 | Ins, dl, DAG, InVals); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 2712 | //===----------------------------------------------------------------------===// |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2713 | // Fast Calling Convention (tail call) implementation |
| 2714 | //===----------------------------------------------------------------------===// |
| 2715 | |
| 2716 | // Like std call, callee cleans arguments, convention except that ECX is |
| 2717 | // reserved for storing the tail called function address. Only 2 registers are |
| 2718 | // free for argument passing (inreg). Tail call optimization is performed |
| 2719 | // provided: |
| 2720 | // * tailcallopt is enabled |
| 2721 | // * caller/callee are fastcc |
| Arnold Schwaighofer | a2a4b47 | 2008-02-26 10:21:54 +0000 | [diff] [blame] | 2722 | // On X86_64 architecture with GOT-style position independent code only local |
| 2723 | // (within module) calls are supported at the moment. |
| Arnold Schwaighofer | 48abc5c | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 2724 | // To keep the stack aligned according to platform abi the function |
| 2725 | // GetAlignedArgumentStackSize ensures that argument delta is always multiples |
| 2726 | // of stack alignment. (Dynamic linkers need this - darwin's dyld for example) |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2727 | // If a tail called function callee has more arguments than the caller the |
| 2728 | // caller needs to make sure that there is room to move the RETADDR to. This is |
| Arnold Schwaighofer | 48abc5c | 2007-10-12 21:30:57 +0000 | [diff] [blame] | 2729 | // achieved by reserving an area the size of the argument delta right after the |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2730 | // original REtADDR, but before the saved framepointer or the spilled registers |
| 2731 | // e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4) |
| 2732 | // stack layout: |
| 2733 | // arg1 |
| 2734 | // arg2 |
| 2735 | // RETADDR |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2736 | // [ new RETADDR |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2737 | // move area ] |
| 2738 | // (possible EBP) |
| 2739 | // ESI |
| 2740 | // EDI |
| 2741 | // local1 .. |
| 2742 | |
| 2743 | /// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned |
| 2744 | /// for a 16 byte align requirement. |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 2745 | unsigned |
| 2746 | X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize, |
| 2747 | SelectionDAG& DAG) const { |
| Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2748 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2749 | const TargetMachine &TM = MF.getTarget(); |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 2750 | const X86RegisterInfo *RegInfo = |
| 2751 | static_cast<const X86RegisterInfo*>(TM.getRegisterInfo()); |
| Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 2752 | const TargetFrameLowering &TFI = *TM.getFrameLowering(); |
| Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2753 | unsigned StackAlignment = TFI.getStackAlignment(); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2754 | uint64_t AlignMask = StackAlignment - 1; |
| Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2755 | int64_t Offset = StackSize; |
| Michael Liao | aa3c2c0 | 2012-10-25 06:29:14 +0000 | [diff] [blame] | 2756 | unsigned SlotSize = RegInfo->getSlotSize(); |
| Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2757 | if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) { |
| 2758 | // Number smaller than 12 so just add the difference. |
| 2759 | Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask)); |
| 2760 | } else { |
| 2761 | // Mask out lower bits, add stackalignment once plus the 12 bytes. |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 2762 | Offset = ((~AlignMask) & Offset) + StackAlignment + |
| Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2763 | (StackAlignment-SlotSize); |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2764 | } |
| Evan Cheng | e9ac9e6 | 2008-09-07 09:07:23 +0000 | [diff] [blame] | 2765 | return Offset; |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2768 | /// MatchingStackOffset - Return true if the given stack call argument is |
| 2769 | /// already available in the same position (relatively) of the caller's |
| 2770 | /// incoming argument stack. |
| 2771 | static |
| 2772 | bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, |
| 2773 | MachineFrameInfo *MFI, const MachineRegisterInfo *MRI, |
| 2774 | const X86InstrInfo *TII) { |
| Evan Cheng | 4cae133 | 2010-03-05 08:38:04 +0000 | [diff] [blame] | 2775 | unsigned Bytes = Arg.getValueType().getSizeInBits() / 8; |
| 2776 | int FI = INT_MAX; |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2777 | if (Arg.getOpcode() == ISD::CopyFromReg) { |
| 2778 | unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); |
| Jakob Stoklund Olesen | c9df025 | 2011-01-10 02:58:51 +0000 | [diff] [blame] | 2779 | if (!TargetRegisterInfo::isVirtualRegister(VR)) |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2780 | return false; |
| 2781 | MachineInstr *Def = MRI->getVRegDef(VR); |
| 2782 | if (!Def) |
| 2783 | return false; |
| 2784 | if (!Flags.isByVal()) { |
| 2785 | if (!TII->isLoadFromStackSlot(Def, FI)) |
| 2786 | return false; |
| 2787 | } else { |
| 2788 | unsigned Opcode = Def->getOpcode(); |
| 2789 | if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r) && |
| 2790 | Def->getOperand(1).isFI()) { |
| 2791 | FI = Def->getOperand(1).getIndex(); |
| Evan Cheng | 4cae133 | 2010-03-05 08:38:04 +0000 | [diff] [blame] | 2792 | Bytes = Flags.getByValSize(); |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2793 | } else |
| 2794 | return false; |
| 2795 | } |
| Evan Cheng | 4cae133 | 2010-03-05 08:38:04 +0000 | [diff] [blame] | 2796 | } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { |
| 2797 | if (Flags.isByVal()) |
| 2798 | // ByVal argument is passed in as a pointer but it's now being |
| Evan Cheng | 1071849 | 2010-03-05 19:55:55 +0000 | [diff] [blame] | 2799 | // dereferenced. e.g. |
| Evan Cheng | 4cae133 | 2010-03-05 08:38:04 +0000 | [diff] [blame] | 2800 | // define @foo(%struct.X* %A) { |
| 2801 | // tail call @bar(%struct.X* byval %A) |
| 2802 | // } |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2803 | return false; |
| 2804 | SDValue Ptr = Ld->getBasePtr(); |
| 2805 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); |
| 2806 | if (!FINode) |
| 2807 | return false; |
| 2808 | FI = FINode->getIndex(); |
| Chad Rosier | df78fcd | 2011-06-25 02:04:56 +0000 | [diff] [blame] | 2809 | } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) { |
| Chad Rosier | 14d71aa | 2011-06-25 18:51:28 +0000 | [diff] [blame] | 2810 | FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg); |
| Chad Rosier | df78fcd | 2011-06-25 02:04:56 +0000 | [diff] [blame] | 2811 | FI = FINode->getIndex(); |
| 2812 | Bytes = Flags.getByValSize(); |
| Evan Cheng | 4cae133 | 2010-03-05 08:38:04 +0000 | [diff] [blame] | 2813 | } else |
| 2814 | return false; |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2815 | |
| Evan Cheng | 4cae133 | 2010-03-05 08:38:04 +0000 | [diff] [blame] | 2816 | assert(FI != INT_MAX); |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2817 | if (!MFI->isFixedObjectIndex(FI)) |
| 2818 | return false; |
| Evan Cheng | 4cae133 | 2010-03-05 08:38:04 +0000 | [diff] [blame] | 2819 | return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI); |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2822 | /// IsEligibleForTailCallOptimization - Check whether the call is eligible |
| 2823 | /// for tail call optimization. Targets which want to do tail call |
| 2824 | /// optimization should implement this function. |
| 2825 | bool |
| 2826 | X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee, |
| Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 2827 | CallingConv::ID CalleeCC, |
| Dan Gohman | 98ca4f2 | 2009-08-05 01:29:28 +0000 | [diff] [blame] | 2828 | bool isVarArg, |
| Evan Cheng | a375d47 | 2010-03-15 18:54:48 +0000 | [diff] [blame] | 2829 | bool isCalleeStructRet, |
| 2830 | bool isCallerStructRet, |
| Evan Cheng | b1cacc7 | 2012-09-25 05:32:34 +0000 | [diff] [blame] | 2831 | Type *RetTy, |
| Evan Cheng | b171245 | 2010-01-27 06:25:16 +0000 | [diff] [blame] | 2832 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 2833 | const SmallVectorImpl<SDValue> &OutVals, |
| Evan Cheng | b171245 | 2010-01-27 06:25:16 +0000 | [diff] [blame] | 2834 | const SmallVectorImpl<ISD::InputArg> &Ins, |
| Nick Lewycky | 48aaf5f | 2013-02-13 21:59:15 +0000 | [diff] [blame] | 2835 | SelectionDAG &DAG) const { |
| Chris Lattner | 2968943 | 2010-03-11 00:22:57 +0000 | [diff] [blame] | 2836 | if (!IsTailCallConvention(CalleeCC) && |
| Evan Cheng | b171245 | 2010-01-27 06:25:16 +0000 | [diff] [blame] | 2837 | CalleeCC != CallingConv::C) |
| 2838 | return false; |
| 2839 | |
| Evan Cheng | 7096ae4 | 2010-01-29 06:45:59 +0000 | [diff] [blame] | 2840 | // If -tailcallopt is specified, make fastcc functions tail-callable. |
| Evan Cheng | 2c12cb4 | 2010-03-26 16:26:03 +0000 | [diff] [blame] | 2841 | const MachineFunction &MF = DAG.getMachineFunction(); |
| Evan Cheng | 7096ae4 | 2010-01-29 06:45:59 +0000 | [diff] [blame] | 2842 | const Function *CallerF = DAG.getMachineFunction().getFunction(); |
| Evan Cheng | b1cacc7 | 2012-09-25 05:32:34 +0000 | [diff] [blame] | 2843 | |
| 2844 | // If the function return type is x86_fp80 and the callee return type is not, |
| 2845 | // then the FP_EXTEND of the call result is not a nop. It's not safe to |
| 2846 | // perform a tailcall optimization here. |
| 2847 | if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty()) |
| 2848 | return false; |
| 2849 | |
| Evan Cheng | 1361796 | 2010-04-30 01:12:32 +0000 | [diff] [blame] | 2850 | CallingConv::ID CallerCC = CallerF->getCallingConv(); |
| 2851 | bool CCMatch = CallerCC == CalleeCC; |
| 2852 | |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 2853 | if (getTargetMachine().Options.GuaranteedTailCallOpt) { |
| Evan Cheng | 1361796 | 2010-04-30 01:12:32 +0000 | [diff] [blame] | 2854 | if (IsTailCallConvention(CalleeCC) && CCMatch) |
| Evan Cheng | 843bd69 | 2010-01-31 06:44:49 +0000 | [diff] [blame] | 2855 | return true; |
| 2856 | return false; |
| 2857 | } |
| 2858 | |
| Dale Johannesen | 2f05cc0 | 2010-05-28 23:24:28 +0000 | [diff] [blame] | 2859 | // Look for obvious safe cases to perform tail call optimization that do not |
| 2860 | // require ABI changes. This is what gcc calls sibcall. |
| Evan Cheng | b2c9290 | 2010-02-02 02:22:50 +0000 | [diff] [blame] | 2861 | |
| Evan Cheng | 2c12cb4 | 2010-03-26 16:26:03 +0000 | [diff] [blame] | 2862 | // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to |
| 2863 | // emit a special epilogue. |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 2864 | const X86RegisterInfo *RegInfo = |
| 2865 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| Evan Cheng | 2c12cb4 | 2010-03-26 16:26:03 +0000 | [diff] [blame] | 2866 | if (RegInfo->needsStackRealignment(MF)) |
| 2867 | return false; |
| 2868 | |
| Evan Cheng | a375d47 | 2010-03-15 18:54:48 +0000 | [diff] [blame] | 2869 | // Also avoid sibcall optimization if either caller or callee uses struct |
| 2870 | // return semantics. |
| 2871 | if (isCalleeStructRet || isCallerStructRet) |
| 2872 | return false; |
| 2873 | |
| Chad Rosier | 2416da3 | 2011-06-24 21:15:36 +0000 | [diff] [blame] | 2874 | // An stdcall caller is expected to clean up its arguments; the callee |
| 2875 | // isn't going to do that. |
| Nick Lewycky | 48aaf5f | 2013-02-13 21:59:15 +0000 | [diff] [blame] | 2876 | if (!CCMatch && CallerCC == CallingConv::X86_StdCall) |
| Chad Rosier | 2416da3 | 2011-06-24 21:15:36 +0000 | [diff] [blame] | 2877 | return false; |
| 2878 | |
| Chad Rosier | 871f664 | 2011-05-18 19:59:50 +0000 | [diff] [blame] | 2879 | // Do not sibcall optimize vararg calls unless all arguments are passed via |
| Chad Rosier | a166089 | 2011-05-20 00:59:28 +0000 | [diff] [blame] | 2880 | // registers. |
| Chad Rosier | 871f664 | 2011-05-18 19:59:50 +0000 | [diff] [blame] | 2881 | if (isVarArg && !Outs.empty()) { |
| Chad Rosier | a166089 | 2011-05-20 00:59:28 +0000 | [diff] [blame] | 2882 | |
| 2883 | // Optimizing for varargs on Win64 is unlikely to be safe without |
| 2884 | // additional testing. |
| 2885 | if (Subtarget->isTargetWin64()) |
| 2886 | return false; |
| 2887 | |
| Chad Rosier | 871f664 | 2011-05-18 19:59:50 +0000 | [diff] [blame] | 2888 | SmallVector<CCValAssign, 16> ArgLocs; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 2889 | CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), |
| Craig Topper | 0fbf364 | 2012-04-23 03:28:34 +0000 | [diff] [blame] | 2890 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
| Chad Rosier | 871f664 | 2011-05-18 19:59:50 +0000 | [diff] [blame] | 2891 | |
| Chad Rosier | 871f664 | 2011-05-18 19:59:50 +0000 | [diff] [blame] | 2892 | CCInfo.AnalyzeCallOperands(Outs, CC_X86); |
| 2893 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) |
| 2894 | if (!ArgLocs[i].isRegLoc()) |
| 2895 | return false; |
| 2896 | } |
| 2897 | |
| Chad Rosier | 30450e8 | 2011-12-22 22:35:21 +0000 | [diff] [blame] | 2898 | // If the call result is in ST0 / ST1, it needs to be popped off the x87 |
| 2899 | // stack. Therefore, if it's not used by the call it is not safe to optimize |
| 2900 | // this into a sibcall. |
| Evan Cheng | f5b9d6c | 2010-03-20 02:58:15 +0000 | [diff] [blame] | 2901 | bool Unused = false; |
| 2902 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { |
| 2903 | if (!Ins[i].Used) { |
| 2904 | Unused = true; |
| 2905 | break; |
| 2906 | } |
| 2907 | } |
| 2908 | if (Unused) { |
| 2909 | SmallVector<CCValAssign, 16> RVLocs; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 2910 | CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(), |
| Craig Topper | 0fbf364 | 2012-04-23 03:28:34 +0000 | [diff] [blame] | 2911 | getTargetMachine(), RVLocs, *DAG.getContext()); |
| Evan Cheng | f5b9d6c | 2010-03-20 02:58:15 +0000 | [diff] [blame] | 2912 | CCInfo.AnalyzeCallResult(Ins, RetCC_X86); |
| Evan Cheng | 1361796 | 2010-04-30 01:12:32 +0000 | [diff] [blame] | 2913 | for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { |
| Evan Cheng | f5b9d6c | 2010-03-20 02:58:15 +0000 | [diff] [blame] | 2914 | CCValAssign &VA = RVLocs[i]; |
| 2915 | if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1) |
| 2916 | return false; |
| 2917 | } |
| 2918 | } |
| 2919 | |
| Evan Cheng | 1361796 | 2010-04-30 01:12:32 +0000 | [diff] [blame] | 2920 | // If the calling conventions do not match, then we'd better make sure the |
| 2921 | // results are returned in the same way as what the caller expects. |
| 2922 | if (!CCMatch) { |
| 2923 | SmallVector<CCValAssign, 16> RVLocs1; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 2924 | CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), |
| Craig Topper | 0fbf364 | 2012-04-23 03:28:34 +0000 | [diff] [blame] | 2925 | getTargetMachine(), RVLocs1, *DAG.getContext()); |
| Evan Cheng | 1361796 | 2010-04-30 01:12:32 +0000 | [diff] [blame] | 2926 | CCInfo1.AnalyzeCallResult(Ins, RetCC_X86); |
| 2927 | |
| 2928 | SmallVector<CCValAssign, 16> RVLocs2; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 2929 | CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), |
| Craig Topper | 0fbf364 | 2012-04-23 03:28:34 +0000 | [diff] [blame] | 2930 | getTargetMachine(), RVLocs2, *DAG.getContext()); |
| Evan Cheng | 1361796 | 2010-04-30 01:12:32 +0000 | [diff] [blame] | 2931 | CCInfo2.AnalyzeCallResult(Ins, RetCC_X86); |
| 2932 | |
| 2933 | if (RVLocs1.size() != RVLocs2.size()) |
| 2934 | return false; |
| 2935 | for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) { |
| 2936 | if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc()) |
| 2937 | return false; |
| 2938 | if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo()) |
| 2939 | return false; |
| 2940 | if (RVLocs1[i].isRegLoc()) { |
| 2941 | if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg()) |
| 2942 | return false; |
| 2943 | } else { |
| 2944 | if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset()) |
| 2945 | return false; |
| 2946 | } |
| 2947 | } |
| 2948 | } |
| 2949 | |
| Evan Cheng | a6bff98 | 2010-01-30 01:22:00 +0000 | [diff] [blame] | 2950 | // If the callee takes no arguments then go on to check the results of the |
| 2951 | // call. |
| 2952 | if (!Outs.empty()) { |
| 2953 | // Check if stack adjustment is needed. For now, do not do this if any |
| 2954 | // argument is passed on the stack. |
| 2955 | SmallVector<CCValAssign, 16> ArgLocs; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 2956 | CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), |
| Craig Topper | 0fbf364 | 2012-04-23 03:28:34 +0000 | [diff] [blame] | 2957 | getTargetMachine(), ArgLocs, *DAG.getContext()); |
| NAKAMURA Takumi | 3f4be4f | 2011-02-05 15:11:32 +0000 | [diff] [blame] | 2958 | |
| 2959 | // Allocate shadow area for Win64 |
| 2960 | if (Subtarget->isTargetWin64()) { |
| 2961 | CCInfo.AllocateStack(32, 8); |
| 2962 | } |
| 2963 | |
| Duncan Sands | 4590766 | 2010-10-31 13:21:44 +0000 | [diff] [blame] | 2964 | CCInfo.AnalyzeCallOperands(Outs, CC_X86); |
| Stuart Hastings | 6db2c2f | 2011-05-17 16:59:46 +0000 | [diff] [blame] | 2965 | if (CCInfo.getNextStackOffset()) { |
| Evan Cheng | b2c9290 | 2010-02-02 02:22:50 +0000 | [diff] [blame] | 2966 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2967 | if (MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn()) |
| 2968 | return false; |
| Evan Cheng | b2c9290 | 2010-02-02 02:22:50 +0000 | [diff] [blame] | 2969 | |
| 2970 | // Check if the arguments are already laid out in the right way as |
| 2971 | // the caller's fixed stack objects. |
| 2972 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2973 | const MachineRegisterInfo *MRI = &MF.getRegInfo(); |
| 2974 | const X86InstrInfo *TII = |
| Roman Divacky | 5932429 | 2012-09-05 22:26:57 +0000 | [diff] [blame] | 2975 | ((const X86TargetMachine&)getTargetMachine()).getInstrInfo(); |
| Evan Cheng | b2c9290 | 2010-02-02 02:22:50 +0000 | [diff] [blame] | 2976 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 2977 | CCValAssign &VA = ArgLocs[i]; |
| Dan Gohman | c940365 | 2010-07-07 15:54:55 +0000 | [diff] [blame] | 2978 | SDValue Arg = OutVals[i]; |
| Evan Cheng | b2c9290 | 2010-02-02 02:22:50 +0000 | [diff] [blame] | 2979 | ISD::ArgFlagsTy Flags = Outs[i].Flags; |
| Evan Cheng | b2c9290 | 2010-02-02 02:22:50 +0000 | [diff] [blame] | 2980 | if (VA.getLocInfo() == CCValAssign::Indirect) |
| 2981 | return false; |
| 2982 | if (!VA.isRegLoc()) { |
| Evan Cheng | 5f94193 | 2010-02-05 02:21:12 +0000 | [diff] [blame] | 2983 | if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, |
| 2984 | MFI, MRI, TII)) |
| Evan Cheng | b2c9290 | 2010-02-02 02:22:50 +0000 | [diff] [blame] | 2985 | return false; |
| 2986 | } |
| 2987 | } |
| 2988 | } |
| Evan Cheng | 9c04467 | 2010-05-29 01:35:22 +0000 | [diff] [blame] | 2989 | |
| 2990 | // If the tailcall address may be in a register, then make sure it's |
| 2991 | // possible to register allocate for it. In 32-bit, the call address can |
| 2992 | // only target EAX, EDX, or ECX since the tail call must be scheduled after |
| Evan Cheng | dedd974 | 2010-07-14 06:44:01 +0000 | [diff] [blame] | 2993 | // callee-saved registers are restored. These happen to be the same |
| 2994 | // registers used to pass 'inreg' arguments so watch out for those. |
| 2995 | if (!Subtarget->is64Bit() && |
| Nick Lewycky | 48aaf5f | 2013-02-13 21:59:15 +0000 | [diff] [blame] | 2996 | ((!isa<GlobalAddressSDNode>(Callee) && |
| 2997 | !isa<ExternalSymbolSDNode>(Callee)) || |
| 2998 | getTargetMachine().getRelocationModel() == Reloc::PIC_)) { |
| Evan Cheng | 9c04467 | 2010-05-29 01:35:22 +0000 | [diff] [blame] | 2999 | unsigned NumInRegs = 0; |
| Nick Lewycky | 48aaf5f | 2013-02-13 21:59:15 +0000 | [diff] [blame] | 3000 | // In PIC we need an extra register to formulate the address computation |
| 3001 | // for the callee. |
| 3002 | unsigned MaxInRegs = |
| 3003 | (getTargetMachine().getRelocationModel() == Reloc::PIC_) ? 2 : 3; |
| 3004 | |
| Evan Cheng | 9c04467 | 2010-05-29 01:35:22 +0000 | [diff] [blame] | 3005 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { |
| 3006 | CCValAssign &VA = ArgLocs[i]; |
| Evan Cheng | dedd974 | 2010-07-14 06:44:01 +0000 | [diff] [blame] | 3007 | if (!VA.isRegLoc()) |
| 3008 | continue; |
| 3009 | unsigned Reg = VA.getLocReg(); |
| 3010 | switch (Reg) { |
| 3011 | default: break; |
| 3012 | case X86::EAX: case X86::EDX: case X86::ECX: |
| Nick Lewycky | 48aaf5f | 2013-02-13 21:59:15 +0000 | [diff] [blame] | 3013 | if (++NumInRegs == MaxInRegs) |
| Evan Cheng | 9c04467 | 2010-05-29 01:35:22 +0000 | [diff] [blame] | 3014 | return false; |
| Evan Cheng | dedd974 | 2010-07-14 06:44:01 +0000 | [diff] [blame] | 3015 | break; |
| Evan Cheng | 9c04467 | 2010-05-29 01:35:22 +0000 | [diff] [blame] | 3016 | } |
| 3017 | } |
| 3018 | } |
| Evan Cheng | a6bff98 | 2010-01-30 01:22:00 +0000 | [diff] [blame] | 3019 | } |
| Evan Cheng | b171245 | 2010-01-27 06:25:16 +0000 | [diff] [blame] | 3020 | |
| Evan Cheng | 86809cc | 2010-02-03 03:28:02 +0000 | [diff] [blame] | 3021 | return true; |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
| Dan Gohman | 3df24e6 | 2008-09-03 23:12:08 +0000 | [diff] [blame] | 3024 | FastISel * |
| Bob Wilson | d49edb7 | 2012-08-03 04:06:28 +0000 | [diff] [blame] | 3025 | X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, |
| 3026 | const TargetLibraryInfo *libInfo) const { |
| 3027 | return X86::createFastISel(funcInfo, libInfo); |
| Dan Gohman | d9f3c48 | 2008-08-19 21:32:53 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 3030 | //===----------------------------------------------------------------------===// |
| 3031 | // Other Lowering Hooks |
| 3032 | //===----------------------------------------------------------------------===// |
| 3033 | |
| Bruno Cardoso Lopes | e654b56 | 2010-09-01 00:51:36 +0000 | [diff] [blame] | 3034 | static bool MayFoldLoad(SDValue Op) { |
| 3035 | return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode()); |
| 3036 | } |
| 3037 | |
| 3038 | static bool MayFoldIntoStore(SDValue Op) { |
| 3039 | return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin()); |
| 3040 | } |
| 3041 | |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 3042 | static bool isTargetShuffle(unsigned Opcode) { |
| 3043 | switch(Opcode) { |
| 3044 | default: return false; |
| 3045 | case X86ISD::PSHUFD: |
| 3046 | case X86ISD::PSHUFHW: |
| 3047 | case X86ISD::PSHUFLW: |
| Craig Topper | b3982da | 2011-12-31 23:50:21 +0000 | [diff] [blame] | 3048 | case X86ISD::SHUFP: |
| Craig Topper | 4aee1bb | 2013-01-28 06:48:25 +0000 | [diff] [blame] | 3049 | case X86ISD::PALIGNR: |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 3050 | case X86ISD::MOVLHPS: |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 3051 | case X86ISD::MOVLHPD: |
| Bruno Cardoso Lopes | 7ff30bb | 2010-08-31 21:38:49 +0000 | [diff] [blame] | 3052 | case X86ISD::MOVHLPS: |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 3053 | case X86ISD::MOVLPS: |
| 3054 | case X86ISD::MOVLPD: |
| Bruno Cardoso Lopes | 5023ef2 | 2010-08-31 22:22:11 +0000 | [diff] [blame] | 3055 | case X86ISD::MOVSHDUP: |
| Bruno Cardoso Lopes | 013bb3d | 2010-08-31 22:35:05 +0000 | [diff] [blame] | 3056 | case X86ISD::MOVSLDUP: |
| Bruno Cardoso Lopes | 1485cc2 | 2010-09-08 17:43:25 +0000 | [diff] [blame] | 3057 | case X86ISD::MOVDDUP: |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 3058 | case X86ISD::MOVSS: |
| 3059 | case X86ISD::MOVSD: |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 3060 | case X86ISD::UNPCKL: |
| 3061 | case X86ISD::UNPCKH: |
| Craig Topper | 316cd2a | 2011-11-30 06:25:25 +0000 | [diff] [blame] | 3062 | case X86ISD::VPERMILP: |
| Craig Topper | ec24e61 | 2011-11-30 07:47:51 +0000 | [diff] [blame] | 3063 | case X86ISD::VPERM2X128: |
| Craig Topper | bdcbcb3 | 2012-05-06 18:54:26 +0000 | [diff] [blame] | 3064 | case X86ISD::VPERMI: |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 3065 | return true; |
| 3066 | } |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3069 | static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT, |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 3070 | SDValue V1, SelectionDAG &DAG) { |
| Bruno Cardoso Lopes | 5023ef2 | 2010-08-31 22:22:11 +0000 | [diff] [blame] | 3071 | switch(Opc) { |
| 3072 | default: llvm_unreachable("Unknown x86 shuffle node"); |
| 3073 | case X86ISD::MOVSHDUP: |
| Bruno Cardoso Lopes | 013bb3d | 2010-08-31 22:35:05 +0000 | [diff] [blame] | 3074 | case X86ISD::MOVSLDUP: |
| Bruno Cardoso Lopes | 1485cc2 | 2010-09-08 17:43:25 +0000 | [diff] [blame] | 3075 | case X86ISD::MOVDDUP: |
| Bruno Cardoso Lopes | 5023ef2 | 2010-08-31 22:22:11 +0000 | [diff] [blame] | 3076 | return DAG.getNode(Opc, dl, VT, V1); |
| 3077 | } |
| Bruno Cardoso Lopes | 5023ef2 | 2010-08-31 22:22:11 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3080 | static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT, |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 3081 | SDValue V1, unsigned TargetMask, |
| 3082 | SelectionDAG &DAG) { |
| Bruno Cardoso Lopes | 3efc077 | 2010-08-23 20:41:02 +0000 | [diff] [blame] | 3083 | switch(Opc) { |
| 3084 | default: llvm_unreachable("Unknown x86 shuffle node"); |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 3085 | case X86ISD::PSHUFD: |
| Bruno Cardoso Lopes | 3efc077 | 2010-08-23 20:41:02 +0000 | [diff] [blame] | 3086 | case X86ISD::PSHUFHW: |
| 3087 | case X86ISD::PSHUFLW: |
| Craig Topper | 316cd2a | 2011-11-30 06:25:25 +0000 | [diff] [blame] | 3088 | case X86ISD::VPERMILP: |
| Craig Topper | 8325c11 | 2012-04-16 00:41:45 +0000 | [diff] [blame] | 3089 | case X86ISD::VPERMI: |
| Bruno Cardoso Lopes | 3efc077 | 2010-08-23 20:41:02 +0000 | [diff] [blame] | 3090 | return DAG.getNode(Opc, dl, VT, V1, DAG.getConstant(TargetMask, MVT::i8)); |
| 3091 | } |
| Bruno Cardoso Lopes | 3efc077 | 2010-08-23 20:41:02 +0000 | [diff] [blame] | 3092 | } |
| Chris Lattner | fcf1a3d | 2007-02-28 06:10:12 +0000 | [diff] [blame] | 3093 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3094 | static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT, |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 3095 | SDValue V1, SDValue V2, unsigned TargetMask, |
| 3096 | SelectionDAG &DAG) { |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 3097 | switch(Opc) { |
| 3098 | default: llvm_unreachable("Unknown x86 shuffle node"); |
| Craig Topper | 4aee1bb | 2013-01-28 06:48:25 +0000 | [diff] [blame] | 3099 | case X86ISD::PALIGNR: |
| Craig Topper | b3982da | 2011-12-31 23:50:21 +0000 | [diff] [blame] | 3100 | case X86ISD::SHUFP: |
| Craig Topper | ec24e61 | 2011-11-30 07:47:51 +0000 | [diff] [blame] | 3101 | case X86ISD::VPERM2X128: |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 3102 | return DAG.getNode(Opc, dl, VT, V1, V2, |
| 3103 | DAG.getConstant(TargetMask, MVT::i8)); |
| 3104 | } |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3107 | static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, EVT VT, |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 3108 | SDValue V1, SDValue V2, SelectionDAG &DAG) { |
| 3109 | switch(Opc) { |
| 3110 | default: llvm_unreachable("Unknown x86 shuffle node"); |
| 3111 | case X86ISD::MOVLHPS: |
| Bruno Cardoso Lopes | f2db5b4 | 2010-08-31 21:15:21 +0000 | [diff] [blame] | 3112 | case X86ISD::MOVLHPD: |
| Bruno Cardoso Lopes | 7ff30bb | 2010-08-31 21:38:49 +0000 | [diff] [blame] | 3113 | case X86ISD::MOVHLPS: |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 3114 | case X86ISD::MOVLPS: |
| 3115 | case X86ISD::MOVLPD: |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 3116 | case X86ISD::MOVSS: |
| 3117 | case X86ISD::MOVSD: |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 3118 | case X86ISD::UNPCKL: |
| 3119 | case X86ISD::UNPCKH: |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 3120 | return DAG.getNode(Opc, dl, VT, V1, V2); |
| 3121 | } |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 3124 | SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const { |
| Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 3125 | MachineFunction &MF = DAG.getMachineFunction(); |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 3126 | const X86RegisterInfo *RegInfo = |
| 3127 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 3128 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 3129 | int ReturnAddrIndex = FuncInfo->getRAIndex(); |
| 3130 | |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3131 | if (ReturnAddrIndex == 0) { |
| 3132 | // Set up a frame object for the return address. |
| Michael Liao | aa3c2c0 | 2012-10-25 06:29:14 +0000 | [diff] [blame] | 3133 | unsigned SlotSize = RegInfo->getSlotSize(); |
| David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 3134 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize, |
| Evan Cheng | ed2ae13 | 2010-07-03 00:40:23 +0000 | [diff] [blame] | 3135 | false); |
| Anton Korobeynikov | a2780e1 | 2007-08-15 17:12:32 +0000 | [diff] [blame] | 3136 | FuncInfo->setRAIndex(ReturnAddrIndex); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 3139 | return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy()); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 3142 | bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M, |
| 3143 | bool hasSymbolicDisplacement) { |
| 3144 | // Offset should fit into 32 bit immediate field. |
| Benjamin Kramer | 34247a0 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 3145 | if (!isInt<32>(Offset)) |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 3146 | return false; |
| 3147 | |
| 3148 | // If we don't have a symbolic displacement - we don't have any extra |
| 3149 | // restrictions. |
| 3150 | if (!hasSymbolicDisplacement) |
| 3151 | return true; |
| 3152 | |
| 3153 | // FIXME: Some tweaks might be needed for medium code model. |
| 3154 | if (M != CodeModel::Small && M != CodeModel::Kernel) |
| 3155 | return false; |
| 3156 | |
| 3157 | // For small code model we assume that latest object is 16MB before end of 31 |
| 3158 | // bits boundary. We may also accept pretty large negative constants knowing |
| 3159 | // that all objects are in the positive half of address space. |
| 3160 | if (M == CodeModel::Small && Offset < 16*1024*1024) |
| 3161 | return true; |
| 3162 | |
| 3163 | // For kernel code model we know that all object resist in the negative half |
| 3164 | // of 32bits address space. We may not accept negative offsets, since they may |
| 3165 | // be just off and we may accept pretty large positive ones. |
| 3166 | if (M == CodeModel::Kernel && Offset > 0) |
| 3167 | return true; |
| 3168 | |
| 3169 | return false; |
| 3170 | } |
| 3171 | |
| Evan Cheng | ef41ff6 | 2011-06-23 17:54:54 +0000 | [diff] [blame] | 3172 | /// isCalleePop - Determines whether the callee is required to pop its |
| 3173 | /// own arguments. Callee pop is necessary to support tail calls. |
| 3174 | bool X86::isCalleePop(CallingConv::ID CallingConv, |
| 3175 | bool is64Bit, bool IsVarArg, bool TailCallOpt) { |
| 3176 | if (IsVarArg) |
| 3177 | return false; |
| 3178 | |
| 3179 | switch (CallingConv) { |
| 3180 | default: |
| 3181 | return false; |
| 3182 | case CallingConv::X86_StdCall: |
| 3183 | return !is64Bit; |
| 3184 | case CallingConv::X86_FastCall: |
| 3185 | return !is64Bit; |
| 3186 | case CallingConv::X86_ThisCall: |
| 3187 | return !is64Bit; |
| 3188 | case CallingConv::Fast: |
| 3189 | return TailCallOpt; |
| 3190 | case CallingConv::GHC: |
| 3191 | return TailCallOpt; |
| Duncan Sands | dc7f174 | 2012-11-16 12:36:39 +0000 | [diff] [blame] | 3192 | case CallingConv::HiPE: |
| 3193 | return TailCallOpt; |
| Evan Cheng | ef41ff6 | 2011-06-23 17:54:54 +0000 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3197 | /// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86 |
| 3198 | /// specific condition code, returning the condition code and the LHS/RHS of the |
| 3199 | /// comparison to make. |
| 3200 | static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP, |
| 3201 | SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) { |
| Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3202 | if (!isFP) { |
| Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 3203 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { |
| 3204 | if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) { |
| 3205 | // X > -1 -> X == 0, jump !sign. |
| 3206 | RHS = DAG.getConstant(0, RHS.getValueType()); |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3207 | return X86::COND_NS; |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 3208 | } |
| 3209 | if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) { |
| Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 3210 | // X < 0 -> X == 0, jump on sign. |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3211 | return X86::COND_S; |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 3212 | } |
| 3213 | if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) { |
| Dan Gohman | 5f6913c | 2007-09-17 14:49:27 +0000 | [diff] [blame] | 3214 | // X < 1 -> X <= 0 |
| 3215 | RHS = DAG.getConstant(0, RHS.getValueType()); |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3216 | return X86::COND_LE; |
| Chris Lattner | bfd68a7 | 2006-09-13 17:04:54 +0000 | [diff] [blame] | 3217 | } |
| Chris Lattner | f957051 | 2006-09-13 03:22:10 +0000 | [diff] [blame] | 3218 | } |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 3219 | |
| Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3220 | switch (SetCCOpcode) { |
| Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3221 | default: llvm_unreachable("Invalid integer condition!"); |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3222 | case ISD::SETEQ: return X86::COND_E; |
| 3223 | case ISD::SETGT: return X86::COND_G; |
| 3224 | case ISD::SETGE: return X86::COND_GE; |
| 3225 | case ISD::SETLT: return X86::COND_L; |
| 3226 | case ISD::SETLE: return X86::COND_LE; |
| 3227 | case ISD::SETNE: return X86::COND_NE; |
| 3228 | case ISD::SETULT: return X86::COND_B; |
| 3229 | case ISD::SETUGT: return X86::COND_A; |
| 3230 | case ISD::SETULE: return X86::COND_BE; |
| 3231 | case ISD::SETUGE: return X86::COND_AE; |
| Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3232 | } |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3233 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 3234 | |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3235 | // First determine if it is required or is profitable to flip the operands. |
| Duncan Sands | 4047f4a | 2008-10-24 13:03:10 +0000 | [diff] [blame] | 3236 | |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3237 | // If LHS is a foldable load, but RHS is not, flip the condition. |
| Rafael Espindola | f297c93 | 2011-02-03 03:58:05 +0000 | [diff] [blame] | 3238 | if (ISD::isNON_EXTLoad(LHS.getNode()) && |
| 3239 | !ISD::isNON_EXTLoad(RHS.getNode())) { |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3240 | SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode); |
| 3241 | std::swap(LHS, RHS); |
| Evan Cheng | 4d46d0a | 2008-08-28 23:48:31 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3244 | switch (SetCCOpcode) { |
| 3245 | default: break; |
| 3246 | case ISD::SETOLT: |
| 3247 | case ISD::SETOLE: |
| 3248 | case ISD::SETUGT: |
| 3249 | case ISD::SETUGE: |
| 3250 | std::swap(LHS, RHS); |
| 3251 | break; |
| 3252 | } |
| 3253 | |
| 3254 | // On a floating point condition, the flags are set as follows: |
| 3255 | // ZF PF CF op |
| 3256 | // 0 | 0 | 0 | X > Y |
| 3257 | // 0 | 0 | 1 | X < Y |
| 3258 | // 1 | 0 | 0 | X == Y |
| 3259 | // 1 | 1 | 1 | unordered |
| 3260 | switch (SetCCOpcode) { |
| Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3261 | default: llvm_unreachable("Condcode should be pre-legalized away"); |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3262 | case ISD::SETUEQ: |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3263 | case ISD::SETEQ: return X86::COND_E; |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3264 | case ISD::SETOLT: // flipped |
| 3265 | case ISD::SETOGT: |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3266 | case ISD::SETGT: return X86::COND_A; |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3267 | case ISD::SETOLE: // flipped |
| 3268 | case ISD::SETOGE: |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3269 | case ISD::SETGE: return X86::COND_AE; |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3270 | case ISD::SETUGT: // flipped |
| 3271 | case ISD::SETULT: |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3272 | case ISD::SETLT: return X86::COND_B; |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3273 | case ISD::SETUGE: // flipped |
| 3274 | case ISD::SETULE: |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3275 | case ISD::SETLE: return X86::COND_BE; |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3276 | case ISD::SETONE: |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 3277 | case ISD::SETNE: return X86::COND_NE; |
| 3278 | case ISD::SETUO: return X86::COND_P; |
| 3279 | case ISD::SETO: return X86::COND_NP; |
| Dan Gohman | 1a49295 | 2009-10-20 16:22:37 +0000 | [diff] [blame] | 3280 | case ISD::SETOEQ: |
| 3281 | case ISD::SETUNE: return X86::COND_INVALID; |
| Chris Lattner | 4c78e02 | 2008-12-23 23:42:27 +0000 | [diff] [blame] | 3282 | } |
| Evan Cheng | d9558e0 | 2006-01-06 00:43:03 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
| Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 3285 | /// hasFPCMov - is there a floating point cmov for the specific X86 condition |
| 3286 | /// code. Current x86 isa includes the following FP cmov instructions: |
| Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 3287 | /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. |
| Evan Cheng | 4a46080 | 2006-01-11 00:33:36 +0000 | [diff] [blame] | 3288 | static bool hasFPCMov(unsigned X86CC) { |
| Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 3289 | switch (X86CC) { |
| 3290 | default: |
| 3291 | return false; |
| Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 3292 | case X86::COND_B: |
| 3293 | case X86::COND_BE: |
| 3294 | case X86::COND_E: |
| 3295 | case X86::COND_P: |
| 3296 | case X86::COND_A: |
| 3297 | case X86::COND_AE: |
| 3298 | case X86::COND_NE: |
| 3299 | case X86::COND_NP: |
| Evan Cheng | aaca22c | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 3300 | return true; |
| 3301 | } |
| 3302 | } |
| 3303 | |
| Evan Cheng | eb2f969 | 2009-10-27 19:56:55 +0000 | [diff] [blame] | 3304 | /// isFPImmLegal - Returns true if the target can instruction select the |
| 3305 | /// specified FP immediate natively. If false, the legalizer will |
| 3306 | /// materialize the FP immediate as a load from a constant pool. |
| Evan Cheng | a1eaa3c | 2009-10-28 01:43:28 +0000 | [diff] [blame] | 3307 | bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { |
| Evan Cheng | eb2f969 | 2009-10-27 19:56:55 +0000 | [diff] [blame] | 3308 | for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) { |
| 3309 | if (Imm.bitwiseIsEqual(LegalFPImmediates[i])) |
| 3310 | return true; |
| 3311 | } |
| 3312 | return false; |
| 3313 | } |
| 3314 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3315 | /// isUndefOrInRange - Return true if Val is undef or if its value falls within |
| 3316 | /// the specified range (L, H]. |
| 3317 | static bool isUndefOrInRange(int Val, int Low, int Hi) { |
| 3318 | return (Val < 0) || (Val >= Low && Val < Hi); |
| 3319 | } |
| 3320 | |
| 3321 | /// isUndefOrEqual - Val is either less than zero (undef) or equal to the |
| 3322 | /// specified value. |
| 3323 | static bool isUndefOrEqual(int Val, int CmpVal) { |
| Jakub Staszak | b2af3a0 | 2012-12-06 18:22:59 +0000 | [diff] [blame] | 3324 | return (Val < 0 || Val == CmpVal); |
| Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
| Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 3327 | /// isSequentialOrUndefInRange - Return true if every element in Mask, beginning |
| Bruno Cardoso Lopes | 4002d7e | 2011-08-12 21:54:42 +0000 | [diff] [blame] | 3328 | /// from position Pos and ending in Pos+Size, falls within the specified |
| 3329 | /// sequential range (L, L+Pos]. or is undef. |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 3330 | static bool isSequentialOrUndefInRange(ArrayRef<int> Mask, |
| Craig Topper | b607264 | 2012-05-03 07:26:59 +0000 | [diff] [blame] | 3331 | unsigned Pos, unsigned Size, int Low) { |
| 3332 | for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low) |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3333 | if (!isUndefOrEqual(Mask[i], Low)) |
| 3334 | return false; |
| 3335 | return true; |
| 3336 | } |
| 3337 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3338 | /// isPSHUFDMask - Return true if the node specifies a shuffle of elements that |
| 3339 | /// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference |
| 3340 | /// the second operand. |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 3341 | static bool isPSHUFDMask(ArrayRef<int> Mask, EVT VT) { |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3342 | if (VT == MVT::v4f32 || VT == MVT::v4i32 ) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3343 | return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 3344 | if (VT == MVT::v2f64 || VT == MVT::v2i64) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3345 | return (Mask[0] < 2 && Mask[1] < 2); |
| 3346 | return false; |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3349 | /// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that |
| 3350 | /// is suitable for input to PSHUFHW. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3351 | static bool isPSHUFHWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) { |
| 3352 | if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16)) |
| Evan Cheng | 0188ecb | 2006-03-22 18:59:22 +0000 | [diff] [blame] | 3353 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3354 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3355 | // Lower quadword copied in order or undef. |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3356 | if (!isSequentialOrUndefInRange(Mask, 0, 4, 0)) |
| 3357 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3358 | |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 3359 | // Upper quadword shuffled. |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3360 | for (unsigned i = 4; i != 8; ++i) |
| Craig Topper | a9a568a | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 3361 | if (!isUndefOrInRange(Mask[i], 4, 8)) |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 3362 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3363 | |
| Craig Topper | a9a568a | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 3364 | if (VT == MVT::v16i16) { |
| 3365 | // Lower quadword copied in order or undef. |
| 3366 | if (!isSequentialOrUndefInRange(Mask, 8, 4, 8)) |
| 3367 | return false; |
| 3368 | |
| 3369 | // Upper quadword shuffled. |
| 3370 | for (unsigned i = 12; i != 16; ++i) |
| 3371 | if (!isUndefOrInRange(Mask[i], 12, 16)) |
| 3372 | return false; |
| 3373 | } |
| 3374 | |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 3375 | return true; |
| 3376 | } |
| 3377 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3378 | /// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that |
| 3379 | /// is suitable for input to PSHUFLW. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3380 | static bool isPSHUFLWMask(ArrayRef<int> Mask, EVT VT, bool HasInt256) { |
| 3381 | if (VT != MVT::v8i16 && (!HasInt256 || VT != MVT::v16i16)) |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 3382 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3383 | |
| Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 3384 | // Upper quadword copied in order. |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3385 | if (!isSequentialOrUndefInRange(Mask, 4, 4, 4)) |
| 3386 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3387 | |
| Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 3388 | // Lower quadword shuffled. |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3389 | for (unsigned i = 0; i != 4; ++i) |
| Craig Topper | a9a568a | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 3390 | if (!isUndefOrInRange(Mask[i], 0, 4)) |
| Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 3391 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3392 | |
| Craig Topper | a9a568a | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 3393 | if (VT == MVT::v16i16) { |
| 3394 | // Upper quadword copied in order. |
| 3395 | if (!isSequentialOrUndefInRange(Mask, 12, 4, 12)) |
| 3396 | return false; |
| 3397 | |
| 3398 | // Lower quadword shuffled. |
| 3399 | for (unsigned i = 8; i != 12; ++i) |
| 3400 | if (!isUndefOrInRange(Mask[i], 8, 12)) |
| 3401 | return false; |
| 3402 | } |
| 3403 | |
| Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 3404 | return true; |
| Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 3407 | /// isPALIGNRMask - Return true if the node specifies a shuffle of elements that |
| 3408 | /// is suitable for input to PALIGNR. |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 3409 | static bool isPALIGNRMask(ArrayRef<int> Mask, EVT VT, |
| 3410 | const X86Subtarget *Subtarget) { |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3411 | if ((VT.is128BitVector() && !Subtarget->hasSSSE3()) || |
| 3412 | (VT.is256BitVector() && !Subtarget->hasInt256())) |
| Bruno Cardoso Lopes | 9065d4b | 2011-07-29 01:30:59 +0000 | [diff] [blame] | 3413 | return false; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 3414 | |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 3415 | unsigned NumElts = VT.getVectorNumElements(); |
| 3416 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 3417 | unsigned NumLaneElts = NumElts/NumLanes; |
| 3418 | |
| 3419 | // Do not handle 64-bit element shuffles with palignr. |
| 3420 | if (NumLaneElts == 2) |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 3421 | return false; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 3422 | |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 3423 | for (unsigned l = 0; l != NumElts; l+=NumLaneElts) { |
| 3424 | unsigned i; |
| 3425 | for (i = 0; i != NumLaneElts; ++i) { |
| 3426 | if (Mask[i+l] >= 0) |
| 3427 | break; |
| 3428 | } |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 3429 | |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 3430 | // Lane is all undef, go to next lane |
| 3431 | if (i == NumLaneElts) |
| 3432 | continue; |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 3433 | |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 3434 | int Start = Mask[i+l]; |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 3435 | |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 3436 | // Make sure its in this lane in one of the sources |
| 3437 | if (!isUndefOrInRange(Start, l, l+NumLaneElts) && |
| 3438 | !isUndefOrInRange(Start, l+NumElts, l+NumElts+NumLaneElts)) |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 3439 | return false; |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 3440 | |
| 3441 | // If not lane 0, then we must match lane 0 |
| 3442 | if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Start, Mask[i]+l)) |
| 3443 | return false; |
| 3444 | |
| 3445 | // Correct second source to be contiguous with first source |
| 3446 | if (Start >= (int)NumElts) |
| 3447 | Start -= NumElts - NumLaneElts; |
| 3448 | |
| 3449 | // Make sure we're shifting in the right direction. |
| 3450 | if (Start <= (int)(i+l)) |
| 3451 | return false; |
| 3452 | |
| 3453 | Start -= i; |
| 3454 | |
| 3455 | // Check the rest of the elements to see if they are consecutive. |
| 3456 | for (++i; i != NumLaneElts; ++i) { |
| 3457 | int Idx = Mask[i+l]; |
| 3458 | |
| 3459 | // Make sure its in this lane |
| 3460 | if (!isUndefOrInRange(Idx, l, l+NumLaneElts) && |
| 3461 | !isUndefOrInRange(Idx, l+NumElts, l+NumElts+NumLaneElts)) |
| 3462 | return false; |
| 3463 | |
| 3464 | // If not lane 0, then we must match lane 0 |
| 3465 | if (l != 0 && Mask[i] >= 0 && !isUndefOrEqual(Idx, Mask[i]+l)) |
| 3466 | return false; |
| 3467 | |
| 3468 | if (Idx >= (int)NumElts) |
| 3469 | Idx -= NumElts - NumLaneElts; |
| 3470 | |
| 3471 | if (!isUndefOrEqual(Idx, Start+i)) |
| 3472 | return false; |
| 3473 | |
| 3474 | } |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 3475 | } |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 3476 | |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 3477 | return true; |
| 3478 | } |
| 3479 | |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 3480 | /// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming |
| 3481 | /// the two vector operands have swapped position. |
| 3482 | static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, |
| 3483 | unsigned NumElems) { |
| 3484 | for (unsigned i = 0; i != NumElems; ++i) { |
| 3485 | int idx = Mask[i]; |
| 3486 | if (idx < 0) |
| 3487 | continue; |
| 3488 | else if (idx < (int)NumElems) |
| 3489 | Mask[i] = idx + NumElems; |
| 3490 | else |
| 3491 | Mask[i] = idx - NumElems; |
| 3492 | } |
| 3493 | } |
| Bruno Cardoso Lopes | 07b7f67 | 2011-08-25 02:58:26 +0000 | [diff] [blame] | 3494 | |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 3495 | /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 3496 | /// specifies a shuffle of elements that is suitable for input to 128/256-bit |
| 3497 | /// SHUFPS and SHUFPD. If Commuted is true, then it checks for sources to be |
| 3498 | /// reverse of what x86 shuffles want. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3499 | static bool isSHUFPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256, |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 3500 | bool Commuted = false) { |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3501 | if (!HasFp256 && VT.is256BitVector()) |
| Bruno Cardoso Lopes | 07b7f67 | 2011-08-25 02:58:26 +0000 | [diff] [blame] | 3502 | return false; |
| 3503 | |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 3504 | unsigned NumElems = VT.getVectorNumElements(); |
| 3505 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 3506 | unsigned NumLaneElems = NumElems/NumLanes; |
| 3507 | |
| 3508 | if (NumLaneElems != 2 && NumLaneElems != 4) |
| Bruno Cardoso Lopes | 07b7f67 | 2011-08-25 02:58:26 +0000 | [diff] [blame] | 3509 | return false; |
| 3510 | |
| 3511 | // VSHUFPSY divides the resulting vector into 4 chunks. |
| 3512 | // The sources are also splitted into 4 chunks, and each destination |
| 3513 | // chunk must come from a different source chunk. |
| 3514 | // |
| 3515 | // SRC1 => X7 X6 X5 X4 X3 X2 X1 X0 |
| 3516 | // SRC2 => Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y9 |
| 3517 | // |
| 3518 | // DST => Y7..Y4, Y7..Y4, X7..X4, X7..X4, |
| 3519 | // Y3..Y0, Y3..Y0, X3..X0, X3..X0 |
| 3520 | // |
| Craig Topper | 9d7025b | 2011-11-27 21:41:12 +0000 | [diff] [blame] | 3521 | // VSHUFPDY divides the resulting vector into 4 chunks. |
| 3522 | // The sources are also splitted into 4 chunks, and each destination |
| 3523 | // chunk must come from a different source chunk. |
| 3524 | // |
| 3525 | // SRC1 => X3 X2 X1 X0 |
| 3526 | // SRC2 => Y3 Y2 Y1 Y0 |
| 3527 | // |
| 3528 | // DST => Y3..Y2, X3..X2, Y1..Y0, X1..X0 |
| 3529 | // |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 3530 | unsigned HalfLaneElems = NumLaneElems/2; |
| 3531 | for (unsigned l = 0; l != NumElems; l += NumLaneElems) { |
| 3532 | for (unsigned i = 0; i != NumLaneElems; ++i) { |
| 3533 | int Idx = Mask[i+l]; |
| 3534 | unsigned RngStart = l + ((Commuted == (i<HalfLaneElems)) ? NumElems : 0); |
| 3535 | if (!isUndefOrInRange(Idx, RngStart, RngStart+NumLaneElems)) |
| 3536 | return false; |
| 3537 | // For VSHUFPSY, the mask of the second half must be the same as the |
| 3538 | // first but with the appropriate offsets. This works in the same way as |
| 3539 | // VPERMILPS works with masks. |
| 3540 | if (NumElems != 8 || l == 0 || Mask[i] < 0) |
| 3541 | continue; |
| 3542 | if (!isUndefOrEqual(Idx, Mask[i]+l)) |
| 3543 | return false; |
| Craig Topper | 1ff73d7 | 2011-12-06 04:59:07 +0000 | [diff] [blame] | 3544 | } |
| Bruno Cardoso Lopes | 07b7f67 | 2011-08-25 02:58:26 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
| 3547 | return true; |
| 3548 | } |
| 3549 | |
| Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 3550 | /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand |
| 3551 | /// specifies a shuffle of elements that is suitable for input to MOVHLPS. |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3552 | static bool isMOVHLPSMask(ArrayRef<int> Mask, EVT VT) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3553 | if (!VT.is128BitVector()) |
| Bruno Cardoso Lopes | 6126005 | 2011-07-29 02:05:28 +0000 | [diff] [blame] | 3554 | return false; |
| 3555 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3556 | unsigned NumElems = VT.getVectorNumElements(); |
| 3557 | |
| Bruno Cardoso Lopes | 6126005 | 2011-07-29 02:05:28 +0000 | [diff] [blame] | 3558 | if (NumElems != 4) |
| Evan Cheng | 2c0dbd0 | 2006-03-24 02:58:06 +0000 | [diff] [blame] | 3559 | return false; |
| 3560 | |
| Evan Cheng | 2064a2b | 2006-03-28 06:50:32 +0000 | [diff] [blame] | 3561 | // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3 |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3562 | return isUndefOrEqual(Mask[0], 6) && |
| 3563 | isUndefOrEqual(Mask[1], 7) && |
| 3564 | isUndefOrEqual(Mask[2], 2) && |
| 3565 | isUndefOrEqual(Mask[3], 3); |
| Evan Cheng | 6e56e2c | 2006-11-07 22:14:24 +0000 | [diff] [blame] | 3566 | } |
| 3567 | |
| Nate Begeman | 0b10b91 | 2009-11-07 23:17:15 +0000 | [diff] [blame] | 3568 | /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form |
| 3569 | /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef, |
| 3570 | /// <2, 3, 2, 3> |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3571 | static bool isMOVHLPS_v_undef_Mask(ArrayRef<int> Mask, EVT VT) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3572 | if (!VT.is128BitVector()) |
| Bruno Cardoso Lopes | 6126005 | 2011-07-29 02:05:28 +0000 | [diff] [blame] | 3573 | return false; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 3574 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3575 | unsigned NumElems = VT.getVectorNumElements(); |
| 3576 | |
| Nate Begeman | 0b10b91 | 2009-11-07 23:17:15 +0000 | [diff] [blame] | 3577 | if (NumElems != 4) |
| 3578 | return false; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 3579 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3580 | return isUndefOrEqual(Mask[0], 2) && |
| 3581 | isUndefOrEqual(Mask[1], 3) && |
| 3582 | isUndefOrEqual(Mask[2], 2) && |
| 3583 | isUndefOrEqual(Mask[3], 3); |
| Nate Begeman | 0b10b91 | 2009-11-07 23:17:15 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3586 | /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 3587 | /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}. |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3588 | static bool isMOVLPMask(ArrayRef<int> Mask, EVT VT) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3589 | if (!VT.is128BitVector()) |
| Elena Demikhovsky | 021c0a2 | 2011-12-28 08:14:01 +0000 | [diff] [blame] | 3590 | return false; |
| 3591 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3592 | unsigned NumElems = VT.getVectorNumElements(); |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3593 | |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3594 | if (NumElems != 2 && NumElems != 4) |
| 3595 | return false; |
| 3596 | |
| Chad Rosier | 238ae31 | 2012-04-30 17:47:15 +0000 | [diff] [blame] | 3597 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3598 | if (!isUndefOrEqual(Mask[i], i + NumElems)) |
| Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 3599 | return false; |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3600 | |
| Chad Rosier | 238ae31 | 2012-04-30 17:47:15 +0000 | [diff] [blame] | 3601 | for (unsigned i = NumElems/2, e = NumElems; i != e; ++i) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3602 | if (!isUndefOrEqual(Mask[i], i)) |
| Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 3603 | return false; |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3604 | |
| 3605 | return true; |
| 3606 | } |
| 3607 | |
| Nate Begeman | 0b10b91 | 2009-11-07 23:17:15 +0000 | [diff] [blame] | 3608 | /// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand |
| 3609 | /// specifies a shuffle of elements that is suitable for input to MOVLHPS. |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3610 | static bool isMOVLHPSMask(ArrayRef<int> Mask, EVT VT) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3611 | if (!VT.is128BitVector()) |
| 3612 | return false; |
| 3613 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3614 | unsigned NumElems = VT.getVectorNumElements(); |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3615 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3616 | if (NumElems != 2 && NumElems != 4) |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3617 | return false; |
| 3618 | |
| Chad Rosier | 238ae31 | 2012-04-30 17:47:15 +0000 | [diff] [blame] | 3619 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3620 | if (!isUndefOrEqual(Mask[i], i)) |
| Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 3621 | return false; |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3622 | |
| Chad Rosier | 238ae31 | 2012-04-30 17:47:15 +0000 | [diff] [blame] | 3623 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) |
| 3624 | if (!isUndefOrEqual(Mask[i + e], i + NumElems)) |
| Evan Cheng | c5cdff2 | 2006-04-07 21:53:05 +0000 | [diff] [blame] | 3625 | return false; |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 3626 | |
| 3627 | return true; |
| 3628 | } |
| 3629 | |
| Elena Demikhovsky | 1596373 | 2012-06-26 08:04:10 +0000 | [diff] [blame] | 3630 | // |
| 3631 | // Some special combinations that can be optimized. |
| 3632 | // |
| 3633 | static |
| 3634 | SDValue Compact8x32ShuffleNode(ShuffleVectorSDNode *SVOp, |
| 3635 | SelectionDAG &DAG) { |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 3636 | MVT VT = SVOp->getValueType(0).getSimpleVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3637 | SDLoc dl(SVOp); |
| Elena Demikhovsky | 1596373 | 2012-06-26 08:04:10 +0000 | [diff] [blame] | 3638 | |
| 3639 | if (VT != MVT::v8i32 && VT != MVT::v8f32) |
| 3640 | return SDValue(); |
| 3641 | |
| 3642 | ArrayRef<int> Mask = SVOp->getMask(); |
| 3643 | |
| 3644 | // These are the special masks that may be optimized. |
| 3645 | static const int MaskToOptimizeEven[] = {0, 8, 2, 10, 4, 12, 6, 14}; |
| 3646 | static const int MaskToOptimizeOdd[] = {1, 9, 3, 11, 5, 13, 7, 15}; |
| 3647 | bool MatchEvenMask = true; |
| 3648 | bool MatchOddMask = true; |
| 3649 | for (int i=0; i<8; ++i) { |
| 3650 | if (!isUndefOrEqual(Mask[i], MaskToOptimizeEven[i])) |
| 3651 | MatchEvenMask = false; |
| 3652 | if (!isUndefOrEqual(Mask[i], MaskToOptimizeOdd[i])) |
| 3653 | MatchOddMask = false; |
| 3654 | } |
| Elena Demikhovsky | 1596373 | 2012-06-26 08:04:10 +0000 | [diff] [blame] | 3655 | |
| Elena Demikhovsky | 3251020 | 2012-09-04 12:49:02 +0000 | [diff] [blame] | 3656 | if (!MatchEvenMask && !MatchOddMask) |
| Elena Demikhovsky | 1596373 | 2012-06-26 08:04:10 +0000 | [diff] [blame] | 3657 | return SDValue(); |
| Michael Liao | 471b917 | 2012-10-03 23:43:52 +0000 | [diff] [blame] | 3658 | |
| Elena Demikhovsky | 1596373 | 2012-06-26 08:04:10 +0000 | [diff] [blame] | 3659 | SDValue UndefNode = DAG.getNode(ISD::UNDEF, dl, VT); |
| 3660 | |
| Elena Demikhovsky | 3251020 | 2012-09-04 12:49:02 +0000 | [diff] [blame] | 3661 | SDValue Op0 = SVOp->getOperand(0); |
| 3662 | SDValue Op1 = SVOp->getOperand(1); |
| 3663 | |
| 3664 | if (MatchEvenMask) { |
| 3665 | // Shift the second operand right to 32 bits. |
| 3666 | static const int ShiftRightMask[] = {-1, 0, -1, 2, -1, 4, -1, 6 }; |
| 3667 | Op1 = DAG.getVectorShuffle(VT, dl, Op1, UndefNode, ShiftRightMask); |
| 3668 | } else { |
| 3669 | // Shift the first operand left to 32 bits. |
| 3670 | static const int ShiftLeftMask[] = {1, -1, 3, -1, 5, -1, 7, -1 }; |
| 3671 | Op0 = DAG.getVectorShuffle(VT, dl, Op0, UndefNode, ShiftLeftMask); |
| 3672 | } |
| 3673 | static const int BlendMask[] = {0, 9, 2, 11, 4, 13, 6, 15}; |
| 3674 | return DAG.getVectorShuffle(VT, dl, Op0, Op1, BlendMask); |
| Elena Demikhovsky | 1596373 | 2012-06-26 08:04:10 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 3677 | /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 3678 | /// specifies a shuffle of elements that is suitable for input to UNPCKL. |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 3679 | static bool isUNPCKLMask(ArrayRef<int> Mask, EVT VT, |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3680 | bool HasInt256, bool V2IsSplat = false) { |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3681 | unsigned NumElts = VT.getVectorNumElements(); |
| Bruno Cardoso Lopes | 4ea4968 | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 3682 | |
| 3683 | assert((VT.is128BitVector() || VT.is256BitVector()) && |
| 3684 | "Unsupported vector type for unpckh"); |
| 3685 | |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3686 | if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 && |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3687 | (!HasInt256 || (NumElts != 16 && NumElts != 32))) |
| Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 3688 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3689 | |
| Bruno Cardoso Lopes | 4ea4968 | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 3690 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 3691 | // independently on 128-bit lanes. |
| 3692 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 3693 | unsigned NumLaneElts = NumElts/NumLanes; |
| David Greene | a20244d | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 3694 | |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3695 | for (unsigned l = 0; l != NumLanes; ++l) { |
| 3696 | for (unsigned i = l*NumLaneElts, j = l*NumLaneElts; |
| 3697 | i != (l+1)*NumLaneElts; |
| David Greene | a20244d | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 3698 | i += 2, ++j) { |
| 3699 | int BitI = Mask[i]; |
| 3700 | int BitI1 = Mask[i+1]; |
| 3701 | if (!isUndefOrEqual(BitI, j)) |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 3702 | return false; |
| David Greene | a20244d | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 3703 | if (V2IsSplat) { |
| 3704 | if (!isUndefOrEqual(BitI1, NumElts)) |
| 3705 | return false; |
| 3706 | } else { |
| 3707 | if (!isUndefOrEqual(BitI1, j + NumElts)) |
| 3708 | return false; |
| 3709 | } |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 3710 | } |
| Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 3711 | } |
| David Greene | a20244d | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 3712 | |
| Evan Cheng | 0038e59 | 2006-03-28 00:39:58 +0000 | [diff] [blame] | 3713 | return true; |
| 3714 | } |
| 3715 | |
| Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 3716 | /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand |
| 3717 | /// specifies a shuffle of elements that is suitable for input to UNPCKH. |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 3718 | static bool isUNPCKHMask(ArrayRef<int> Mask, EVT VT, |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3719 | bool HasInt256, bool V2IsSplat = false) { |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3720 | unsigned NumElts = VT.getVectorNumElements(); |
| Bruno Cardoso Lopes | 4ea4968 | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 3721 | |
| 3722 | assert((VT.is128BitVector() || VT.is256BitVector()) && |
| 3723 | "Unsupported vector type for unpckh"); |
| 3724 | |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3725 | if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 && |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3726 | (!HasInt256 || (NumElts != 16 && NumElts != 32))) |
| Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 3727 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3728 | |
| Bruno Cardoso Lopes | 4ea4968 | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 3729 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 3730 | // independently on 128-bit lanes. |
| 3731 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 3732 | unsigned NumLaneElts = NumElts/NumLanes; |
| 3733 | |
| Bruno Cardoso Lopes | 4ea4968 | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 3734 | for (unsigned l = 0; l != NumLanes; ++l) { |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3735 | for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2; |
| 3736 | i != (l+1)*NumLaneElts; i += 2, ++j) { |
| Bruno Cardoso Lopes | 4ea4968 | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 3737 | int BitI = Mask[i]; |
| 3738 | int BitI1 = Mask[i+1]; |
| 3739 | if (!isUndefOrEqual(BitI, j)) |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 3740 | return false; |
| Bruno Cardoso Lopes | 4ea4968 | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 3741 | if (V2IsSplat) { |
| 3742 | if (isUndefOrEqual(BitI1, NumElts)) |
| 3743 | return false; |
| 3744 | } else { |
| 3745 | if (!isUndefOrEqual(BitI1, j+NumElts)) |
| 3746 | return false; |
| 3747 | } |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 3748 | } |
| Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 3749 | } |
| Evan Cheng | 4fcb922 | 2006-03-28 02:43:26 +0000 | [diff] [blame] | 3750 | return true; |
| 3751 | } |
| 3752 | |
| Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 3753 | /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form |
| 3754 | /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef, |
| 3755 | /// <0, 0, 1, 1> |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3756 | static bool isUNPCKL_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) { |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3757 | unsigned NumElts = VT.getVectorNumElements(); |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3758 | bool Is256BitVec = VT.is256BitVector(); |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3759 | |
| 3760 | assert((VT.is128BitVector() || VT.is256BitVector()) && |
| 3761 | "Unsupported vector type for unpckh"); |
| 3762 | |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3763 | if (Is256BitVec && NumElts != 4 && NumElts != 8 && |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3764 | (!HasInt256 || (NumElts != 16 && NumElts != 32))) |
| Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 3765 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3766 | |
| Bruno Cardoso Lopes | 6292ece | 2011-08-25 21:40:37 +0000 | [diff] [blame] | 3767 | // For 256-bit i64/f64, use MOVDDUPY instead, so reject the matching pattern |
| 3768 | // FIXME: Need a better way to get rid of this, there's no latency difference |
| 3769 | // between UNPCKLPD and MOVDDUP, the later should always be checked first and |
| 3770 | // the former later. We should also remove the "_undef" special mask. |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3771 | if (NumElts == 4 && Is256BitVec) |
| Bruno Cardoso Lopes | 6292ece | 2011-08-25 21:40:37 +0000 | [diff] [blame] | 3772 | return false; |
| 3773 | |
| Bruno Cardoso Lopes | 4ea4968 | 2011-07-26 22:03:40 +0000 | [diff] [blame] | 3774 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 3775 | // independently on 128-bit lanes. |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3776 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 3777 | unsigned NumLaneElts = NumElts/NumLanes; |
| David Greene | a20244d | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 3778 | |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3779 | for (unsigned l = 0; l != NumLanes; ++l) { |
| 3780 | for (unsigned i = l*NumLaneElts, j = l*NumLaneElts; |
| 3781 | i != (l+1)*NumLaneElts; |
| David Greene | a20244d | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 3782 | i += 2, ++j) { |
| 3783 | int BitI = Mask[i]; |
| 3784 | int BitI1 = Mask[i+1]; |
| 3785 | |
| 3786 | if (!isUndefOrEqual(BitI, j)) |
| 3787 | return false; |
| 3788 | if (!isUndefOrEqual(BitI1, j)) |
| 3789 | return false; |
| 3790 | } |
| Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 3791 | } |
| David Greene | a20244d | 2011-03-02 17:23:43 +0000 | [diff] [blame] | 3792 | |
| Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 3793 | return true; |
| Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
| Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 3796 | /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form |
| 3797 | /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef, |
| 3798 | /// <2, 2, 3, 3> |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3799 | static bool isUNPCKH_v_undef_Mask(ArrayRef<int> Mask, EVT VT, bool HasInt256) { |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3800 | unsigned NumElts = VT.getVectorNumElements(); |
| 3801 | |
| 3802 | assert((VT.is128BitVector() || VT.is256BitVector()) && |
| 3803 | "Unsupported vector type for unpckh"); |
| 3804 | |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3805 | if (VT.is256BitVector() && NumElts != 4 && NumElts != 8 && |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3806 | (!HasInt256 || (NumElts != 16 && NumElts != 32))) |
| Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 3807 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3808 | |
| Craig Topper | 94438ba | 2011-12-16 08:06:31 +0000 | [diff] [blame] | 3809 | // Handle 128 and 256-bit vector lengths. AVX defines UNPCK* to operate |
| 3810 | // independently on 128-bit lanes. |
| 3811 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 3812 | unsigned NumLaneElts = NumElts/NumLanes; |
| 3813 | |
| 3814 | for (unsigned l = 0; l != NumLanes; ++l) { |
| 3815 | for (unsigned i = l*NumLaneElts, j = (l*NumLaneElts)+NumLaneElts/2; |
| 3816 | i != (l+1)*NumLaneElts; i += 2, ++j) { |
| 3817 | int BitI = Mask[i]; |
| 3818 | int BitI1 = Mask[i+1]; |
| 3819 | if (!isUndefOrEqual(BitI, j)) |
| 3820 | return false; |
| 3821 | if (!isUndefOrEqual(BitI1, j)) |
| 3822 | return false; |
| 3823 | } |
| Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 3824 | } |
| Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 3825 | return true; |
| Nate Begeman | b706d29 | 2009-04-24 03:42:54 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
| Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 3828 | /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand |
| 3829 | /// specifies a shuffle of elements that is suitable for input to MOVSS, |
| 3830 | /// MOVSD, and MOVD, i.e. setting the lowest element. |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 3831 | static bool isMOVLMask(ArrayRef<int> Mask, EVT VT) { |
| Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 3832 | if (VT.getVectorElementType().getSizeInBits() < 32) |
| Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 3833 | return false; |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3834 | if (!VT.is128BitVector()) |
| Elena Demikhovsky | 021c0a2 | 2011-12-28 08:14:01 +0000 | [diff] [blame] | 3835 | return false; |
| Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 3836 | |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3837 | unsigned NumElts = VT.getVectorNumElements(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3838 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3839 | if (!isUndefOrEqual(Mask[0], NumElts)) |
| Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 3840 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3841 | |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3842 | for (unsigned i = 1; i != NumElts; ++i) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3843 | if (!isUndefOrEqual(Mask[i], i)) |
| Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 3844 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3845 | |
| Evan Cheng | d6d1cbd | 2006-04-11 00:19:04 +0000 | [diff] [blame] | 3846 | return true; |
| 3847 | } |
| Evan Cheng | 1d5a8cc | 2006-04-05 07:20:06 +0000 | [diff] [blame] | 3848 | |
| Craig Topper | 70b883b | 2011-11-28 10:14:51 +0000 | [diff] [blame] | 3849 | /// isVPERM2X128Mask - Match 256-bit shuffles where the elements are considered |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3850 | /// as permutations between 128-bit chunks or halves. As an example: this |
| 3851 | /// shuffle bellow: |
| 3852 | /// vector_shuffle <4, 5, 6, 7, 12, 13, 14, 15> |
| 3853 | /// The first half comes from the second half of V1 and the second half from the |
| 3854 | /// the second half of V2. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3855 | static bool isVPERM2X128Mask(ArrayRef<int> Mask, EVT VT, bool HasFp256) { |
| 3856 | if (!HasFp256 || !VT.is256BitVector()) |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3857 | return false; |
| 3858 | |
| 3859 | // The shuffle result is divided into half A and half B. In total the two |
| 3860 | // sources have 4 halves, namely: C, D, E, F. The final values of A and |
| 3861 | // B must come from C, D, E or F. |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3862 | unsigned HalfSize = VT.getVectorNumElements()/2; |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3863 | bool MatchA = false, MatchB = false; |
| 3864 | |
| 3865 | // Check if A comes from one of C, D, E, F. |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3866 | for (unsigned Half = 0; Half != 4; ++Half) { |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3867 | if (isSequentialOrUndefInRange(Mask, 0, HalfSize, Half*HalfSize)) { |
| 3868 | MatchA = true; |
| 3869 | break; |
| 3870 | } |
| 3871 | } |
| 3872 | |
| 3873 | // Check if B comes from one of C, D, E, F. |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3874 | for (unsigned Half = 0; Half != 4; ++Half) { |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3875 | if (isSequentialOrUndefInRange(Mask, HalfSize, HalfSize, Half*HalfSize)) { |
| 3876 | MatchB = true; |
| 3877 | break; |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | return MatchA && MatchB; |
| 3882 | } |
| 3883 | |
| Craig Topper | 70b883b | 2011-11-28 10:14:51 +0000 | [diff] [blame] | 3884 | /// getShuffleVPERM2X128Immediate - Return the appropriate immediate to shuffle |
| 3885 | /// the specified VECTOR_MASK mask with VPERM2F128/VPERM2I128 instructions. |
| Craig Topper | d93e4c3 | 2011-12-11 19:12:35 +0000 | [diff] [blame] | 3886 | static unsigned getShuffleVPERM2X128Immediate(ShuffleVectorSDNode *SVOp) { |
| Craig Topper | cfcab21 | 2013-01-19 08:27:45 +0000 | [diff] [blame] | 3887 | MVT VT = SVOp->getValueType(0).getSimpleVT(); |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3888 | |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3889 | unsigned HalfSize = VT.getVectorNumElements()/2; |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3890 | |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3891 | unsigned FstHalf = 0, SndHalf = 0; |
| 3892 | for (unsigned i = 0; i < HalfSize; ++i) { |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3893 | if (SVOp->getMaskElt(i) > 0) { |
| 3894 | FstHalf = SVOp->getMaskElt(i)/HalfSize; |
| 3895 | break; |
| 3896 | } |
| 3897 | } |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3898 | for (unsigned i = HalfSize; i < HalfSize*2; ++i) { |
| Bruno Cardoso Lopes | 53cae13 | 2011-08-12 21:48:26 +0000 | [diff] [blame] | 3899 | if (SVOp->getMaskElt(i) > 0) { |
| 3900 | SndHalf = SVOp->getMaskElt(i)/HalfSize; |
| 3901 | break; |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | return (FstHalf | (SndHalf << 4)); |
| 3906 | } |
| 3907 | |
| Craig Topper | 70b883b | 2011-11-28 10:14:51 +0000 | [diff] [blame] | 3908 | /// isVPERMILPMask - Return true if the specified VECTOR_SHUFFLE operand |
| Bruno Cardoso Lopes | cea34e4 | 2011-07-27 00:56:34 +0000 | [diff] [blame] | 3909 | /// specifies a shuffle of elements that is suitable for input to VPERMILPD*. |
| 3910 | /// Note that VPERMIL mask matching is different depending whether theunderlying |
| 3911 | /// type is 32 or 64. In the VPERMILPS the high half of the mask should point |
| 3912 | /// to the same elements of the low, but to the higher half of the source. |
| 3913 | /// In VPERMILPD the two lanes could be shuffled independently of each other |
| Craig Topper | dbd98a4 | 2012-02-07 06:28:42 +0000 | [diff] [blame] | 3914 | /// with the same restriction that lanes can't be crossed. Also handles PSHUFDY. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 3915 | static bool isVPERMILPMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) { |
| 3916 | if (!HasFp256) |
| Bruno Cardoso Lopes | cea34e4 | 2011-07-27 00:56:34 +0000 | [diff] [blame] | 3917 | return false; |
| 3918 | |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3919 | unsigned NumElts = VT.getVectorNumElements(); |
| Craig Topper | 70b883b | 2011-11-28 10:14:51 +0000 | [diff] [blame] | 3920 | // Only match 256-bit with 32/64-bit types |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3921 | if (!VT.is256BitVector() || (NumElts != 4 && NumElts != 8)) |
| Bruno Cardoso Lopes | cea34e4 | 2011-07-27 00:56:34 +0000 | [diff] [blame] | 3922 | return false; |
| 3923 | |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3924 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 3925 | unsigned LaneSize = NumElts/NumLanes; |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 3926 | for (unsigned l = 0; l != NumElts; l += LaneSize) { |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3927 | for (unsigned i = 0; i != LaneSize; ++i) { |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 3928 | if (!isUndefOrInRange(Mask[i+l], l, l+LaneSize)) |
| Craig Topper | 70b883b | 2011-11-28 10:14:51 +0000 | [diff] [blame] | 3929 | return false; |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 3930 | if (NumElts != 8 || l == 0) |
| Craig Topper | 70b883b | 2011-11-28 10:14:51 +0000 | [diff] [blame] | 3931 | continue; |
| 3932 | // VPERMILPS handling |
| 3933 | if (Mask[i] < 0) |
| 3934 | continue; |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 3935 | if (!isUndefOrEqual(Mask[i+l], Mask[i]+l)) |
| Bruno Cardoso Lopes | cea34e4 | 2011-07-27 00:56:34 +0000 | [diff] [blame] | 3936 | return false; |
| 3937 | } |
| Bruno Cardoso Lopes | 65b74e1 | 2011-07-21 01:55:47 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | return true; |
| 3941 | } |
| 3942 | |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 3943 | /// isCommutedMOVLMask - Returns true if the shuffle mask is except the reverse |
| Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 3944 | /// of what x86 movss want. X86 movs requires the lowest element to be lowest |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 3945 | /// element of vector 2 and the other elements to come from vector 1 in order. |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 3946 | static bool isCommutedMOVLMask(ArrayRef<int> Mask, EVT VT, |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3947 | bool V2IsSplat = false, bool V2IsUndef = false) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3948 | if (!VT.is128BitVector()) |
| Craig Topper | 97327dc | 2012-03-18 22:50:10 +0000 | [diff] [blame] | 3949 | return false; |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 3950 | |
| 3951 | unsigned NumOps = VT.getVectorNumElements(); |
| Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 3952 | if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16) |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 3953 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3954 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3955 | if (!isUndefOrEqual(Mask[0], 0)) |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 3956 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3957 | |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 3958 | for (unsigned i = 1; i != NumOps; ++i) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3959 | if (!(isUndefOrEqual(Mask[i], i+NumOps) || |
| 3960 | (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) || |
| 3961 | (V2IsSplat && isUndefOrEqual(Mask[i], NumOps)))) |
| Evan Cheng | 8cf723d | 2006-09-08 01:50:06 +0000 | [diff] [blame] | 3962 | return false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 3963 | |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 3964 | return true; |
| 3965 | } |
| 3966 | |
| Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 3967 | /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 3968 | /// specifies a shuffle of elements that is suitable for input to MOVSHDUP. |
| Bruno Cardoso Lopes | 9123c6f | 2011-07-26 02:39:28 +0000 | [diff] [blame] | 3969 | /// Masks to match: <1, 1, 3, 3> or <1, 1, 3, 3, 5, 5, 7, 7> |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3970 | static bool isMOVSHDUPMask(ArrayRef<int> Mask, EVT VT, |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 3971 | const X86Subtarget *Subtarget) { |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 3972 | if (!Subtarget->hasSSE3()) |
| Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 3973 | return false; |
| 3974 | |
| Bruno Cardoso Lopes | 9123c6f | 2011-07-26 02:39:28 +0000 | [diff] [blame] | 3975 | unsigned NumElems = VT.getVectorNumElements(); |
| 3976 | |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 3977 | if ((VT.is128BitVector() && NumElems != 4) || |
| 3978 | (VT.is256BitVector() && NumElems != 8)) |
| Bruno Cardoso Lopes | 9123c6f | 2011-07-26 02:39:28 +0000 | [diff] [blame] | 3979 | return false; |
| 3980 | |
| 3981 | // "i+1" is the value the indexed mask element must have |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3982 | for (unsigned i = 0; i != NumElems; i += 2) |
| 3983 | if (!isUndefOrEqual(Mask[i], i+1) || |
| 3984 | !isUndefOrEqual(Mask[i+1], i+1)) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 3985 | return false; |
| Bruno Cardoso Lopes | 9123c6f | 2011-07-26 02:39:28 +0000 | [diff] [blame] | 3986 | |
| 3987 | return true; |
| Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
| 3990 | /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| 3991 | /// specifies a shuffle of elements that is suitable for input to MOVSLDUP. |
| Bruno Cardoso Lopes | 9123c6f | 2011-07-26 02:39:28 +0000 | [diff] [blame] | 3992 | /// Masks to match: <0, 0, 2, 2> or <0, 0, 2, 2, 4, 4, 6, 6> |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 3993 | static bool isMOVSLDUPMask(ArrayRef<int> Mask, EVT VT, |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 3994 | const X86Subtarget *Subtarget) { |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 3995 | if (!Subtarget->hasSSE3()) |
| Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 3996 | return false; |
| 3997 | |
| Bruno Cardoso Lopes | 9123c6f | 2011-07-26 02:39:28 +0000 | [diff] [blame] | 3998 | unsigned NumElems = VT.getVectorNumElements(); |
| 3999 | |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4000 | if ((VT.is128BitVector() && NumElems != 4) || |
| 4001 | (VT.is256BitVector() && NumElems != 8)) |
| Bruno Cardoso Lopes | 9123c6f | 2011-07-26 02:39:28 +0000 | [diff] [blame] | 4002 | return false; |
| 4003 | |
| 4004 | // "i" is the value the indexed mask element must have |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 4005 | for (unsigned i = 0; i != NumElems; i += 2) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4006 | if (!isUndefOrEqual(Mask[i], i) || |
| 4007 | !isUndefOrEqual(Mask[i+1], i)) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4008 | return false; |
| Evan Cheng | 57ebe9f | 2006-04-15 05:37:34 +0000 | [diff] [blame] | 4009 | |
| Bruno Cardoso Lopes | 9123c6f | 2011-07-26 02:39:28 +0000 | [diff] [blame] | 4010 | return true; |
| Evan Cheng | d953947 | 2006-04-14 21:59:03 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
| Bruno Cardoso Lopes | 6292ece | 2011-08-25 21:40:37 +0000 | [diff] [blame] | 4013 | /// isMOVDDUPYMask - Return true if the specified VECTOR_SHUFFLE operand |
| 4014 | /// specifies a shuffle of elements that is suitable for input to 256-bit |
| 4015 | /// version of MOVDDUP. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 4016 | static bool isMOVDDUPYMask(ArrayRef<int> Mask, EVT VT, bool HasFp256) { |
| 4017 | if (!HasFp256 || !VT.is256BitVector()) |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 4018 | return false; |
| Bruno Cardoso Lopes | 6292ece | 2011-08-25 21:40:37 +0000 | [diff] [blame] | 4019 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 4020 | unsigned NumElts = VT.getVectorNumElements(); |
| 4021 | if (NumElts != 4) |
| Bruno Cardoso Lopes | 6292ece | 2011-08-25 21:40:37 +0000 | [diff] [blame] | 4022 | return false; |
| 4023 | |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 4024 | for (unsigned i = 0; i != NumElts/2; ++i) |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 4025 | if (!isUndefOrEqual(Mask[i], 0)) |
| Bruno Cardoso Lopes | 6292ece | 2011-08-25 21:40:37 +0000 | [diff] [blame] | 4026 | return false; |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 4027 | for (unsigned i = NumElts/2; i != NumElts; ++i) |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 4028 | if (!isUndefOrEqual(Mask[i], NumElts/2)) |
| Bruno Cardoso Lopes | 6292ece | 2011-08-25 21:40:37 +0000 | [diff] [blame] | 4029 | return false; |
| 4030 | return true; |
| 4031 | } |
| 4032 | |
| Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 4033 | /// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand |
| Bruno Cardoso Lopes | 06ef923 | 2011-08-25 21:40:34 +0000 | [diff] [blame] | 4034 | /// specifies a shuffle of elements that is suitable for input to 128-bit |
| 4035 | /// version of MOVDDUP. |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4036 | static bool isMOVDDUPMask(ArrayRef<int> Mask, EVT VT) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 4037 | if (!VT.is128BitVector()) |
| Bruno Cardoso Lopes | 06ef923 | 2011-08-25 21:40:34 +0000 | [diff] [blame] | 4038 | return false; |
| 4039 | |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 4040 | unsigned e = VT.getVectorNumElements() / 2; |
| 4041 | for (unsigned i = 0; i != e; ++i) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4042 | if (!isUndefOrEqual(Mask[i], i)) |
| Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 4043 | return false; |
| Craig Topper | c612d79 | 2012-01-02 09:17:37 +0000 | [diff] [blame] | 4044 | for (unsigned i = 0; i != e; ++i) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4045 | if (!isUndefOrEqual(Mask[e+i], i)) |
| Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 4046 | return false; |
| 4047 | return true; |
| 4048 | } |
| 4049 | |
| David Greene | c38a03e | 2011-02-03 15:50:00 +0000 | [diff] [blame] | 4050 | /// isVEXTRACTF128Index - Return true if the specified |
| 4051 | /// EXTRACT_SUBVECTOR operand specifies a vector extract that is |
| 4052 | /// suitable for input to VEXTRACTF128. |
| 4053 | bool X86::isVEXTRACTF128Index(SDNode *N) { |
| 4054 | if (!isa<ConstantSDNode>(N->getOperand(1).getNode())) |
| 4055 | return false; |
| 4056 | |
| 4057 | // The index should be aligned on a 128-bit boundary. |
| 4058 | uint64_t Index = |
| 4059 | cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue(); |
| 4060 | |
| Craig Topper | 5141d97 | 2013-01-18 08:41:28 +0000 | [diff] [blame] | 4061 | MVT VT = N->getValueType(0).getSimpleVT(); |
| 4062 | unsigned ElSize = VT.getVectorElementType().getSizeInBits(); |
| David Greene | c38a03e | 2011-02-03 15:50:00 +0000 | [diff] [blame] | 4063 | bool Result = (Index * ElSize) % 128 == 0; |
| 4064 | |
| 4065 | return Result; |
| 4066 | } |
| 4067 | |
| David Greene | ccacdc1 | 2011-02-04 16:08:29 +0000 | [diff] [blame] | 4068 | /// isVINSERTF128Index - Return true if the specified INSERT_SUBVECTOR |
| 4069 | /// operand specifies a subvector insert that is suitable for input to |
| 4070 | /// VINSERTF128. |
| 4071 | bool X86::isVINSERTF128Index(SDNode *N) { |
| 4072 | if (!isa<ConstantSDNode>(N->getOperand(2).getNode())) |
| 4073 | return false; |
| 4074 | |
| 4075 | // The index should be aligned on a 128-bit boundary. |
| 4076 | uint64_t Index = |
| 4077 | cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue(); |
| 4078 | |
| Craig Topper | 5141d97 | 2013-01-18 08:41:28 +0000 | [diff] [blame] | 4079 | MVT VT = N->getValueType(0).getSimpleVT(); |
| 4080 | unsigned ElSize = VT.getVectorElementType().getSizeInBits(); |
| David Greene | ccacdc1 | 2011-02-04 16:08:29 +0000 | [diff] [blame] | 4081 | bool Result = (Index * ElSize) % 128 == 0; |
| 4082 | |
| 4083 | return Result; |
| 4084 | } |
| 4085 | |
| Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 4086 | /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 4087 | /// the specified VECTOR_SHUFFLE mask with PSHUF* and SHUFP* instructions. |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 4088 | /// Handles 128-bit and 256-bit. |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 4089 | static unsigned getShuffleSHUFImmediate(ShuffleVectorSDNode *N) { |
| Craig Topper | cfcab21 | 2013-01-19 08:27:45 +0000 | [diff] [blame] | 4090 | MVT VT = N->getValueType(0).getSimpleVT(); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4091 | |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 4092 | assert((VT.is128BitVector() || VT.is256BitVector()) && |
| 4093 | "Unsupported vector type for PSHUF/SHUFP"); |
| 4094 | |
| 4095 | // Handle 128 and 256-bit vector lengths. AVX defines PSHUF/SHUFP to operate |
| 4096 | // independently on 128-bit lanes. |
| 4097 | unsigned NumElts = VT.getVectorNumElements(); |
| 4098 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 4099 | unsigned NumLaneElts = NumElts/NumLanes; |
| 4100 | |
| 4101 | assert((NumLaneElts == 2 || NumLaneElts == 4) && |
| 4102 | "Only supports 2 or 4 elements per lane"); |
| 4103 | |
| 4104 | unsigned Shift = (NumLaneElts == 4) ? 1 : 0; |
| Evan Cheng | b9df0ca | 2006-03-22 02:53:00 +0000 | [diff] [blame] | 4105 | unsigned Mask = 0; |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 4106 | for (unsigned i = 0; i != NumElts; ++i) { |
| 4107 | int Elt = N->getMaskElt(i); |
| 4108 | if (Elt < 0) continue; |
| Craig Topper | 6b28d35 | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 4109 | Elt &= NumLaneElts - 1; |
| 4110 | unsigned ShAmt = (i << Shift) % 8; |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 4111 | Mask |= Elt << ShAmt; |
| Evan Cheng | 36b27f3 | 2006-03-28 23:41:33 +0000 | [diff] [blame] | 4112 | } |
| Craig Topper | 1a7700a | 2012-01-19 08:19:12 +0000 | [diff] [blame] | 4113 | |
| Evan Cheng | 63d3300 | 2006-03-22 08:01:21 +0000 | [diff] [blame] | 4114 | return Mask; |
| 4115 | } |
| 4116 | |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 4117 | /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 4118 | /// the specified VECTOR_SHUFFLE mask with the PSHUFHW instruction. |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4119 | static unsigned getShufflePSHUFHWImmediate(ShuffleVectorSDNode *N) { |
| Craig Topper | cfcab21 | 2013-01-19 08:27:45 +0000 | [diff] [blame] | 4120 | MVT VT = N->getValueType(0).getSimpleVT(); |
| Craig Topper | 6b28d35 | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 4121 | |
| 4122 | assert((VT == MVT::v8i16 || VT == MVT::v16i16) && |
| 4123 | "Unsupported vector type for PSHUFHW"); |
| 4124 | |
| 4125 | unsigned NumElts = VT.getVectorNumElements(); |
| 4126 | |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 4127 | unsigned Mask = 0; |
| Craig Topper | 6b28d35 | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 4128 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 4129 | // 8 nodes per lane, but we only care about the last 4. |
| 4130 | for (unsigned i = 0; i < 4; ++i) { |
| 4131 | int Elt = N->getMaskElt(l+i+4); |
| 4132 | if (Elt < 0) continue; |
| 4133 | Elt &= 0x3; // only 2-bits. |
| 4134 | Mask |= Elt << (i * 2); |
| 4135 | } |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 4136 | } |
| Craig Topper | 6b28d35 | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 4137 | |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 4138 | return Mask; |
| 4139 | } |
| 4140 | |
| 4141 | /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 4142 | /// the specified VECTOR_SHUFFLE mask with the PSHUFLW instruction. |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4143 | static unsigned getShufflePSHUFLWImmediate(ShuffleVectorSDNode *N) { |
| Craig Topper | cfcab21 | 2013-01-19 08:27:45 +0000 | [diff] [blame] | 4144 | MVT VT = N->getValueType(0).getSimpleVT(); |
| Craig Topper | 6b28d35 | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 4145 | |
| 4146 | assert((VT == MVT::v8i16 || VT == MVT::v16i16) && |
| 4147 | "Unsupported vector type for PSHUFHW"); |
| 4148 | |
| 4149 | unsigned NumElts = VT.getVectorNumElements(); |
| 4150 | |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 4151 | unsigned Mask = 0; |
| Craig Topper | 6b28d35 | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 4152 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 4153 | // 8 nodes per lane, but we only care about the first 4. |
| 4154 | for (unsigned i = 0; i < 4; ++i) { |
| 4155 | int Elt = N->getMaskElt(l+i); |
| 4156 | if (Elt < 0) continue; |
| 4157 | Elt &= 0x3; // only 2-bits |
| 4158 | Mask |= Elt << (i * 2); |
| 4159 | } |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 4160 | } |
| Craig Topper | 6b28d35 | 2012-05-03 07:12:59 +0000 | [diff] [blame] | 4161 | |
| Evan Cheng | 506d3df | 2006-03-29 23:07:14 +0000 | [diff] [blame] | 4162 | return Mask; |
| 4163 | } |
| 4164 | |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 4165 | /// getShufflePALIGNRImmediate - Return the appropriate immediate to shuffle |
| 4166 | /// the specified VECTOR_SHUFFLE mask with the PALIGNR instruction. |
| Craig Topper | d93e4c3 | 2011-12-11 19:12:35 +0000 | [diff] [blame] | 4167 | static unsigned getShufflePALIGNRImmediate(ShuffleVectorSDNode *SVOp) { |
| Craig Topper | cfcab21 | 2013-01-19 08:27:45 +0000 | [diff] [blame] | 4168 | MVT VT = SVOp->getValueType(0).getSimpleVT(); |
| Craig Topper | d93e4c3 | 2011-12-11 19:12:35 +0000 | [diff] [blame] | 4169 | unsigned EltSize = VT.getVectorElementType().getSizeInBits() >> 3; |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 4170 | |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 4171 | unsigned NumElts = VT.getVectorNumElements(); |
| 4172 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 4173 | unsigned NumLaneElts = NumElts/NumLanes; |
| 4174 | |
| 4175 | int Val = 0; |
| 4176 | unsigned i; |
| 4177 | for (i = 0; i != NumElts; ++i) { |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 4178 | Val = SVOp->getMaskElt(i); |
| 4179 | if (Val >= 0) |
| 4180 | break; |
| 4181 | } |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 4182 | if (Val >= (int)NumElts) |
| 4183 | Val -= NumElts - NumLaneElts; |
| 4184 | |
| Eli Friedman | 63f8dde | 2011-07-25 21:36:45 +0000 | [diff] [blame] | 4185 | assert(Val - i > 0 && "PALIGNR imm should be positive"); |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 4186 | return (Val - i) * EltSize; |
| 4187 | } |
| 4188 | |
| David Greene | c38a03e | 2011-02-03 15:50:00 +0000 | [diff] [blame] | 4189 | /// getExtractVEXTRACTF128Immediate - Return the appropriate immediate |
| 4190 | /// to extract the specified EXTRACT_SUBVECTOR index with VEXTRACTF128 |
| 4191 | /// instructions. |
| 4192 | unsigned X86::getExtractVEXTRACTF128Immediate(SDNode *N) { |
| 4193 | if (!isa<ConstantSDNode>(N->getOperand(1).getNode())) |
| 4194 | llvm_unreachable("Illegal extract subvector for VEXTRACTF128"); |
| 4195 | |
| 4196 | uint64_t Index = |
| 4197 | cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue(); |
| 4198 | |
| Craig Topper | cfcab21 | 2013-01-19 08:27:45 +0000 | [diff] [blame] | 4199 | MVT VecVT = N->getOperand(0).getValueType().getSimpleVT(); |
| 4200 | MVT ElVT = VecVT.getVectorElementType(); |
| David Greene | c38a03e | 2011-02-03 15:50:00 +0000 | [diff] [blame] | 4201 | |
| 4202 | unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits(); |
| David Greene | c38a03e | 2011-02-03 15:50:00 +0000 | [diff] [blame] | 4203 | return Index / NumElemsPerChunk; |
| 4204 | } |
| 4205 | |
| David Greene | ccacdc1 | 2011-02-04 16:08:29 +0000 | [diff] [blame] | 4206 | /// getInsertVINSERTF128Immediate - Return the appropriate immediate |
| 4207 | /// to insert at the specified INSERT_SUBVECTOR index with VINSERTF128 |
| 4208 | /// instructions. |
| 4209 | unsigned X86::getInsertVINSERTF128Immediate(SDNode *N) { |
| 4210 | if (!isa<ConstantSDNode>(N->getOperand(2).getNode())) |
| 4211 | llvm_unreachable("Illegal insert subvector for VINSERTF128"); |
| 4212 | |
| 4213 | uint64_t Index = |
| NAKAMURA Takumi | 2763538 | 2011-02-05 15:10:54 +0000 | [diff] [blame] | 4214 | cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue(); |
| David Greene | ccacdc1 | 2011-02-04 16:08:29 +0000 | [diff] [blame] | 4215 | |
| Craig Topper | cfcab21 | 2013-01-19 08:27:45 +0000 | [diff] [blame] | 4216 | MVT VecVT = N->getValueType(0).getSimpleVT(); |
| 4217 | MVT ElVT = VecVT.getVectorElementType(); |
| David Greene | ccacdc1 | 2011-02-04 16:08:29 +0000 | [diff] [blame] | 4218 | |
| 4219 | unsigned NumElemsPerChunk = 128 / ElVT.getSizeInBits(); |
| David Greene | ccacdc1 | 2011-02-04 16:08:29 +0000 | [diff] [blame] | 4220 | return Index / NumElemsPerChunk; |
| 4221 | } |
| 4222 | |
| Elena Demikhovsky | 73c504a | 2012-04-15 11:18:59 +0000 | [diff] [blame] | 4223 | /// getShuffleCLImmediate - Return the appropriate immediate to shuffle |
| 4224 | /// the specified VECTOR_SHUFFLE mask with VPERMQ and VPERMPD instructions. |
| 4225 | /// Handles 256-bit. |
| 4226 | static unsigned getShuffleCLImmediate(ShuffleVectorSDNode *N) { |
| Craig Topper | cfcab21 | 2013-01-19 08:27:45 +0000 | [diff] [blame] | 4227 | MVT VT = N->getValueType(0).getSimpleVT(); |
| Elena Demikhovsky | 73c504a | 2012-04-15 11:18:59 +0000 | [diff] [blame] | 4228 | |
| Elena Demikhovsky | 73c504a | 2012-04-15 11:18:59 +0000 | [diff] [blame] | 4229 | unsigned NumElts = VT.getVectorNumElements(); |
| 4230 | |
| Craig Topper | 095c528 | 2012-04-15 23:48:57 +0000 | [diff] [blame] | 4231 | assert((VT.is256BitVector() && NumElts == 4) && |
| 4232 | "Unsupported vector type for VPERMQ/VPERMPD"); |
| 4233 | |
| Elena Demikhovsky | 73c504a | 2012-04-15 11:18:59 +0000 | [diff] [blame] | 4234 | unsigned Mask = 0; |
| 4235 | for (unsigned i = 0; i != NumElts; ++i) { |
| 4236 | int Elt = N->getMaskElt(i); |
| Craig Topper | 095c528 | 2012-04-15 23:48:57 +0000 | [diff] [blame] | 4237 | if (Elt < 0) |
| Elena Demikhovsky | 73c504a | 2012-04-15 11:18:59 +0000 | [diff] [blame] | 4238 | continue; |
| 4239 | Mask |= Elt << (i*2); |
| 4240 | } |
| 4241 | |
| 4242 | return Mask; |
| 4243 | } |
| Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 4244 | /// isZeroNode - Returns true if Elt is a constant zero or a floating point |
| 4245 | /// constant +0.0. |
| 4246 | bool X86::isZeroNode(SDValue Elt) { |
| Jakub Staszak | 30fcfc3 | 2013-02-16 13:34:26 +0000 | [diff] [blame] | 4247 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Elt)) |
| 4248 | return CN->isNullValue(); |
| 4249 | if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Elt)) |
| 4250 | return CFP->getValueAPF().isPosZero(); |
| 4251 | return false; |
| Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4254 | /// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in |
| 4255 | /// their permute mask. |
| 4256 | static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp, |
| 4257 | SelectionDAG &DAG) { |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 4258 | MVT VT = SVOp->getValueType(0).getSimpleVT(); |
| Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 4259 | unsigned NumElems = VT.getVectorNumElements(); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4260 | SmallVector<int, 8> MaskVec; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 4261 | |
| Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 4262 | for (unsigned i = 0; i != NumElems; ++i) { |
| Craig Topper | 8ae97ba | 2012-05-21 06:40:16 +0000 | [diff] [blame] | 4263 | int Idx = SVOp->getMaskElt(i); |
| 4264 | if (Idx >= 0) { |
| 4265 | if (Idx < (int)NumElems) |
| 4266 | Idx += NumElems; |
| 4267 | else |
| 4268 | Idx -= NumElems; |
| 4269 | } |
| 4270 | MaskVec.push_back(Idx); |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 4271 | } |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4272 | return DAG.getVectorShuffle(VT, SDLoc(SVOp), SVOp->getOperand(1), |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4273 | SVOp->getOperand(0), &MaskVec[0]); |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 4274 | } |
| 4275 | |
| Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 4276 | /// ShouldXformToMOVHLPS - Return true if the node should be transformed to |
| 4277 | /// match movhlps. The lower half elements should come from upper half of |
| 4278 | /// V1 (and in order), and the upper half elements should come from the upper |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 4279 | /// half of V2 (and in order). |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4280 | static bool ShouldXformToMOVHLPS(ArrayRef<int> Mask, EVT VT) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 4281 | if (!VT.is128BitVector()) |
| Bruno Cardoso Lopes | 59353b4 | 2011-08-11 18:59:13 +0000 | [diff] [blame] | 4282 | return false; |
| 4283 | if (VT.getVectorNumElements() != 4) |
| Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 4284 | return false; |
| 4285 | for (unsigned i = 0, e = 2; i != e; ++i) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4286 | if (!isUndefOrEqual(Mask[i], i+2)) |
| Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 4287 | return false; |
| 4288 | for (unsigned i = 2; i != 4; ++i) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4289 | if (!isUndefOrEqual(Mask[i], i+4)) |
| Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 4290 | return false; |
| 4291 | return true; |
| 4292 | } |
| 4293 | |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 4294 | /// isScalarLoadToVector - Returns true if the node is a scalar load that |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 4295 | /// is promoted to a vector. It also returns the LoadSDNode by reference if |
| 4296 | /// required. |
| 4297 | static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) { |
| Evan Cheng | 0b457f0 | 2008-09-25 20:50:48 +0000 | [diff] [blame] | 4298 | if (N->getOpcode() != ISD::SCALAR_TO_VECTOR) |
| 4299 | return false; |
| 4300 | N = N->getOperand(0).getNode(); |
| 4301 | if (!ISD::isNON_EXTLoad(N)) |
| 4302 | return false; |
| 4303 | if (LD) |
| 4304 | *LD = cast<LoadSDNode>(N); |
| 4305 | return true; |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 4306 | } |
| 4307 | |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 4308 | // Test whether the given value is a vector value which will be legalized |
| 4309 | // into a load. |
| 4310 | static bool WillBeConstantPoolLoad(SDNode *N) { |
| 4311 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 4312 | return false; |
| 4313 | |
| 4314 | // Check for any non-constant elements. |
| 4315 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) |
| 4316 | switch (N->getOperand(i).getNode()->getOpcode()) { |
| 4317 | case ISD::UNDEF: |
| 4318 | case ISD::ConstantFP: |
| 4319 | case ISD::Constant: |
| 4320 | break; |
| 4321 | default: |
| 4322 | return false; |
| 4323 | } |
| 4324 | |
| 4325 | // Vectors of all-zeros and all-ones are materialized with special |
| 4326 | // instructions rather than being loaded. |
| 4327 | return !ISD::isBuildVectorAllZeros(N) && |
| 4328 | !ISD::isBuildVectorAllOnes(N); |
| 4329 | } |
| 4330 | |
| Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 4331 | /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to |
| 4332 | /// match movlp{s|d}. The lower half elements should come from lower half of |
| 4333 | /// V1 (and in order), and the upper half elements should come from the upper |
| 4334 | /// half of V2 (and in order). And since V1 will become the source of the |
| 4335 | /// MOVLP, it must be either a vector load or a scalar load to vector. |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4336 | static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4337 | ArrayRef<int> Mask, EVT VT) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 4338 | if (!VT.is128BitVector()) |
| Bruno Cardoso Lopes | 59353b4 | 2011-08-11 18:59:13 +0000 | [diff] [blame] | 4339 | return false; |
| 4340 | |
| Evan Cheng | 466685d | 2006-10-09 20:57:25 +0000 | [diff] [blame] | 4341 | if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1)) |
| Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 4342 | return false; |
| Evan Cheng | 23425f5 | 2006-10-09 21:39:25 +0000 | [diff] [blame] | 4343 | // Is V2 is a vector load, don't do this transformation. We will try to use |
| 4344 | // load folding shufps op. |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 4345 | if (ISD::isNON_EXTLoad(V2) || WillBeConstantPoolLoad(V2)) |
| Evan Cheng | 23425f5 | 2006-10-09 21:39:25 +0000 | [diff] [blame] | 4346 | return false; |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 4347 | |
| Bruno Cardoso Lopes | 59353b4 | 2011-08-11 18:59:13 +0000 | [diff] [blame] | 4348 | unsigned NumElems = VT.getVectorNumElements(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 4349 | |
| Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 4350 | if (NumElems != 2 && NumElems != 4) |
| 4351 | return false; |
| Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 4352 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4353 | if (!isUndefOrEqual(Mask[i], i)) |
| Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 4354 | return false; |
| Chad Rosier | 238ae31 | 2012-04-30 17:47:15 +0000 | [diff] [blame] | 4355 | for (unsigned i = NumElems/2, e = NumElems; i != e; ++i) |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 4356 | if (!isUndefOrEqual(Mask[i], i+NumElems)) |
| Evan Cheng | 533a0aa | 2006-04-19 20:35:22 +0000 | [diff] [blame] | 4357 | return false; |
| 4358 | return true; |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4361 | /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are |
| 4362 | /// all the same. |
| 4363 | static bool isSplatVector(SDNode *N) { |
| 4364 | if (N->getOpcode() != ISD::BUILD_VECTOR) |
| 4365 | return false; |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 4366 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4367 | SDValue SplatValue = N->getOperand(0); |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4368 | for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) |
| 4369 | if (N->getOperand(i) != SplatValue) |
| Evan Cheng | 5ced1d8 | 2006-04-06 23:23:56 +0000 | [diff] [blame] | 4370 | return false; |
| 4371 | return true; |
| 4372 | } |
| 4373 | |
| Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 4374 | /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 4375 | /// to an zero vector. |
| Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 4376 | /// FIXME: move to dag combiner / method on ShuffleVectorSDNode |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4377 | static bool isZeroShuffle(ShuffleVectorSDNode *N) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4378 | SDValue V1 = N->getOperand(0); |
| 4379 | SDValue V2 = N->getOperand(1); |
| Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 4380 | unsigned NumElems = N->getValueType(0).getVectorNumElements(); |
| 4381 | for (unsigned i = 0; i != NumElems; ++i) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4382 | int Idx = N->getMaskElt(i); |
| Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 4383 | if (Idx >= (int)NumElems) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4384 | unsigned Opc = V2.getOpcode(); |
| Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 4385 | if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode())) |
| 4386 | continue; |
| Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 4387 | if (Opc != ISD::BUILD_VECTOR || |
| 4388 | !X86::isZeroNode(V2.getOperand(Idx-NumElems))) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4389 | return false; |
| 4390 | } else if (Idx >= 0) { |
| 4391 | unsigned Opc = V1.getOpcode(); |
| 4392 | if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode())) |
| 4393 | continue; |
| Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 4394 | if (Opc != ISD::BUILD_VECTOR || |
| 4395 | !X86::isZeroNode(V1.getOperand(Idx))) |
| Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 4396 | return false; |
| Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 4397 | } |
| 4398 | } |
| 4399 | return true; |
| 4400 | } |
| 4401 | |
| 4402 | /// getZeroVector - Returns a vector of specified type with all zero elements. |
| 4403 | /// |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 4404 | static SDValue getZeroVector(EVT VT, const X86Subtarget *Subtarget, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4405 | SelectionDAG &DAG, SDLoc dl) { |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4406 | assert(VT.isVector() && "Expected a vector type"); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4407 | |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4408 | // Always build SSE zero vectors as <4 x i32> bitcasted |
| Bruno Cardoso Lopes | 8c05a85 | 2010-08-12 02:06:36 +0000 | [diff] [blame] | 4409 | // to their dest type. This ensures they get CSE'd. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4410 | SDValue Vec; |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4411 | if (VT.is128BitVector()) { // SSE |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 4412 | if (Subtarget->hasSSE2()) { // SSE2 |
| Bruno Cardoso Lopes | 8c05a85 | 2010-08-12 02:06:36 +0000 | [diff] [blame] | 4413 | SDValue Cst = DAG.getTargetConstant(0, MVT::i32); |
| 4414 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst); |
| 4415 | } else { // SSE1 |
| 4416 | SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32); |
| 4417 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst); |
| 4418 | } |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4419 | } else if (VT.is256BitVector()) { // AVX |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 4420 | if (Subtarget->hasInt256()) { // AVX2 |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 4421 | SDValue Cst = DAG.getTargetConstant(0, MVT::i32); |
| 4422 | SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst }; |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 4423 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, |
| 4424 | array_lengthof(Ops)); |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 4425 | } else { |
| 4426 | // 256-bit logic and arithmetic instructions in AVX are all |
| 4427 | // floating-point, no support for integer ops. Emit fp zeroed vectors. |
| 4428 | SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32); |
| 4429 | SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst }; |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 4430 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops, |
| 4431 | array_lengthof(Ops)); |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 4432 | } |
| Craig Topper | 9d35240 | 2012-04-23 07:24:41 +0000 | [diff] [blame] | 4433 | } else |
| 4434 | llvm_unreachable("Unexpected vector type"); |
| 4435 | |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4436 | return DAG.getNode(ISD::BITCAST, dl, VT, Vec); |
| Evan Cheng | 213d2cf | 2007-05-17 18:45:50 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
| Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 4439 | /// getOnesVector - Returns a vector of specified type with all bits set. |
| Craig Topper | 745a86b | 2011-11-19 22:34:59 +0000 | [diff] [blame] | 4440 | /// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with |
| 4441 | /// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately. |
| 4442 | /// Then bitcast to their original type, ensuring they get CSE'd. |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 4443 | static SDValue getOnesVector(MVT VT, bool HasInt256, SelectionDAG &DAG, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4444 | SDLoc dl) { |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 4445 | assert(VT.isVector() && "Expected a vector type"); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4446 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4447 | SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32); |
| Craig Topper | 745a86b | 2011-11-19 22:34:59 +0000 | [diff] [blame] | 4448 | SDValue Vec; |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4449 | if (VT.is256BitVector()) { |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 4450 | if (HasInt256) { // AVX2 |
| Craig Topper | 745a86b | 2011-11-19 22:34:59 +0000 | [diff] [blame] | 4451 | SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst }; |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 4452 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops, |
| 4453 | array_lengthof(Ops)); |
| Craig Topper | 745a86b | 2011-11-19 22:34:59 +0000 | [diff] [blame] | 4454 | } else { // AVX |
| 4455 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst); |
| Craig Topper | 4c7972d | 2012-04-22 18:15:59 +0000 | [diff] [blame] | 4456 | Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl); |
| Craig Topper | 745a86b | 2011-11-19 22:34:59 +0000 | [diff] [blame] | 4457 | } |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4458 | } else if (VT.is128BitVector()) { |
| Craig Topper | 745a86b | 2011-11-19 22:34:59 +0000 | [diff] [blame] | 4459 | Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst); |
| Craig Topper | 9d35240 | 2012-04-23 07:24:41 +0000 | [diff] [blame] | 4460 | } else |
| 4461 | llvm_unreachable("Unexpected vector type"); |
| Bruno Cardoso Lopes | 863bd9d | 2011-07-25 23:05:32 +0000 | [diff] [blame] | 4462 | |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4463 | return DAG.getNode(ISD::BITCAST, dl, VT, Vec); |
| Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4466 | /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements |
| 4467 | /// that point to V2 points to its first element. |
| Craig Topper | 39a9e48 | 2012-02-11 06:24:48 +0000 | [diff] [blame] | 4468 | static void NormalizeMask(SmallVectorImpl<int> &Mask, unsigned NumElems) { |
| Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 4469 | for (unsigned i = 0; i != NumElems; ++i) { |
| Craig Topper | 39a9e48 | 2012-02-11 06:24:48 +0000 | [diff] [blame] | 4470 | if (Mask[i] > (int)NumElems) { |
| 4471 | Mask[i] = NumElems; |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4472 | } |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4473 | } |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
| Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 4476 | /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd |
| 4477 | /// operation of specified width. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4478 | static SDValue getMOVL(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1, |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4479 | SDValue V2) { |
| 4480 | unsigned NumElems = VT.getVectorNumElements(); |
| 4481 | SmallVector<int, 8> Mask; |
| 4482 | Mask.push_back(NumElems); |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4483 | for (unsigned i = 1; i != NumElems; ++i) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4484 | Mask.push_back(i); |
| 4485 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4488 | /// getUnpackl - Returns a vector_shuffle node for an unpackl operation. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4489 | static SDValue getUnpackl(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1, |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4490 | SDValue V2) { |
| 4491 | unsigned NumElems = VT.getVectorNumElements(); |
| 4492 | SmallVector<int, 8> Mask; |
| Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 4493 | for (unsigned i = 0, e = NumElems/2; i != e; ++i) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4494 | Mask.push_back(i); |
| 4495 | Mask.push_back(i + NumElems); |
| Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 4496 | } |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4497 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); |
| Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4500 | /// getUnpackh - Returns a vector_shuffle node for an unpackh operation. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4501 | static SDValue getUnpackh(SelectionDAG &DAG, SDLoc dl, EVT VT, SDValue V1, |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4502 | SDValue V2) { |
| 4503 | unsigned NumElems = VT.getVectorNumElements(); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4504 | SmallVector<int, 8> Mask; |
| Chad Rosier | 238ae31 | 2012-04-30 17:47:15 +0000 | [diff] [blame] | 4505 | for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4506 | Mask.push_back(i + Half); |
| 4507 | Mask.push_back(i + NumElems + Half); |
| Evan Cheng | 39623da | 2006-04-20 08:58:49 +0000 | [diff] [blame] | 4508 | } |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4509 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]); |
| Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 4510 | } |
| 4511 | |
| Bruno Cardoso Lopes | 5f1d8ab | 2011-08-11 02:49:44 +0000 | [diff] [blame] | 4512 | // PromoteSplati8i16 - All i16 and i8 vector types can't be used directly by |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4513 | // a generic shuffle instruction because the target has no such instructions. |
| 4514 | // Generate shuffles which repeat i16 and i8 several times until they can be |
| 4515 | // represented by v4f32 and then be manipulated by target suported shuffles. |
| Bruno Cardoso Lopes | 5f1d8ab | 2011-08-11 02:49:44 +0000 | [diff] [blame] | 4516 | static SDValue PromoteSplati8i16(SDValue V, SelectionDAG &DAG, int &EltNo) { |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4517 | EVT VT = V.getValueType(); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4518 | int NumElems = VT.getVectorNumElements(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4519 | SDLoc dl(V); |
| Rafael Espindola | 15684b2 | 2009-04-24 12:40:33 +0000 | [diff] [blame] | 4520 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4521 | while (NumElems > 4) { |
| 4522 | if (EltNo < NumElems/2) { |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4523 | V = getUnpackl(DAG, dl, VT, V, V); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4524 | } else { |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4525 | V = getUnpackh(DAG, dl, VT, V, V); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4526 | EltNo -= NumElems/2; |
| 4527 | } |
| 4528 | NumElems >>= 1; |
| 4529 | } |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4530 | return V; |
| 4531 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 4532 | |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4533 | /// getLegalSplat - Generate a legal splat with supported x86 shuffles |
| 4534 | static SDValue getLegalSplat(SelectionDAG &DAG, SDValue V, int EltNo) { |
| 4535 | EVT VT = V.getValueType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4536 | SDLoc dl(V); |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4537 | |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4538 | if (VT.is128BitVector()) { |
| Bruno Cardoso Lopes | d8b7dd5 | 2011-08-23 22:06:37 +0000 | [diff] [blame] | 4539 | V = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V); |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4540 | int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo }; |
| Bruno Cardoso Lopes | d8b7dd5 | 2011-08-23 22:06:37 +0000 | [diff] [blame] | 4541 | V = DAG.getVectorShuffle(MVT::v4f32, dl, V, DAG.getUNDEF(MVT::v4f32), |
| 4542 | &SplatMask[0]); |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4543 | } else if (VT.is256BitVector()) { |
| Bruno Cardoso Lopes | d8b7dd5 | 2011-08-23 22:06:37 +0000 | [diff] [blame] | 4544 | // To use VPERMILPS to splat scalars, the second half of indicies must |
| 4545 | // refer to the higher part, which is a duplication of the lower one, |
| 4546 | // because VPERMILPS can only handle in-lane permutations. |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4547 | int SplatMask[8] = { EltNo, EltNo, EltNo, EltNo, |
| 4548 | EltNo+4, EltNo+4, EltNo+4, EltNo+4 }; |
| Bruno Cardoso Lopes | d8b7dd5 | 2011-08-23 22:06:37 +0000 | [diff] [blame] | 4549 | |
| 4550 | V = DAG.getNode(ISD::BITCAST, dl, MVT::v8f32, V); |
| 4551 | V = DAG.getVectorShuffle(MVT::v8f32, dl, V, DAG.getUNDEF(MVT::v8f32), |
| 4552 | &SplatMask[0]); |
| Craig Topper | 9d35240 | 2012-04-23 07:24:41 +0000 | [diff] [blame] | 4553 | } else |
| 4554 | llvm_unreachable("Vector size not supported"); |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4555 | |
| 4556 | return DAG.getNode(ISD::BITCAST, dl, VT, V); |
| 4557 | } |
| 4558 | |
| Bruno Cardoso Lopes | 8a5b262 | 2011-08-17 02:29:13 +0000 | [diff] [blame] | 4559 | /// PromoteSplat - Splat is promoted to target supported vector shuffles. |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4560 | static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG) { |
| 4561 | EVT SrcVT = SV->getValueType(0); |
| 4562 | SDValue V1 = SV->getOperand(0); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4563 | SDLoc dl(SV); |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4564 | |
| 4565 | int EltNo = SV->getSplatIndex(); |
| 4566 | int NumElems = SrcVT.getVectorNumElements(); |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4567 | bool Is256BitVec = SrcVT.is256BitVector(); |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4568 | |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4569 | assert(((SrcVT.is128BitVector() && NumElems > 4) || Is256BitVec) && |
| 4570 | "Unknown how to promote splat for type"); |
| Bruno Cardoso Lopes | d8b7dd5 | 2011-08-23 22:06:37 +0000 | [diff] [blame] | 4571 | |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4572 | // Extract the 128-bit part containing the splat element and update |
| 4573 | // the splat element index when it refers to the higher register. |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4574 | if (Is256BitVec) { |
| Craig Topper | 7d1e3dc | 2012-04-30 05:17:10 +0000 | [diff] [blame] | 4575 | V1 = Extract128BitVector(V1, EltNo, DAG, dl); |
| 4576 | if (EltNo >= NumElems/2) |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4577 | EltNo -= NumElems/2; |
| 4578 | } |
| 4579 | |
| Bruno Cardoso Lopes | 8a5b262 | 2011-08-17 02:29:13 +0000 | [diff] [blame] | 4580 | // All i16 and i8 vector types can't be used directly by a generic shuffle |
| 4581 | // instruction because the target has no such instruction. Generate shuffles |
| 4582 | // which repeat i16 and i8 several times until they fit in i32, and then can |
| Bruno Cardoso Lopes | d8b7dd5 | 2011-08-23 22:06:37 +0000 | [diff] [blame] | 4583 | // be manipulated by target suported shuffles. |
| Bruno Cardoso Lopes | 5f1d8ab | 2011-08-11 02:49:44 +0000 | [diff] [blame] | 4584 | EVT EltVT = SrcVT.getVectorElementType(); |
| Bruno Cardoso Lopes | d8b7dd5 | 2011-08-23 22:06:37 +0000 | [diff] [blame] | 4585 | if (EltVT == MVT::i8 || EltVT == MVT::i16) |
| Bruno Cardoso Lopes | 5f1d8ab | 2011-08-11 02:49:44 +0000 | [diff] [blame] | 4586 | V1 = PromoteSplati8i16(V1, DAG, EltNo); |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4587 | |
| 4588 | // Recreate the 256-bit vector and place the same 128-bit vector |
| 4589 | // into the low and high part. This is necessary because we want |
| Bruno Cardoso Lopes | d8b7dd5 | 2011-08-23 22:06:37 +0000 | [diff] [blame] | 4590 | // to use VPERM* to shuffle the vectors |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 4591 | if (Is256BitVec) { |
| Craig Topper | 4c7972d | 2012-04-22 18:15:59 +0000 | [diff] [blame] | 4592 | V1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, V1, V1); |
| Bruno Cardoso Lopes | 9283b66 | 2011-07-21 01:55:42 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | return getLegalSplat(DAG, V1, EltNo); |
| Evan Cheng | c575ca2 | 2006-04-17 20:43:08 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
| Evan Cheng | ba05f72 | 2006-04-21 23:03:30 +0000 | [diff] [blame] | 4598 | /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified |
| Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 4599 | /// vector of zero or undef vector. This produces a shuffle where the low |
| 4600 | /// element of V2 is swizzled into the zero/undef vector, landing at element |
| 4601 | /// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3). |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4602 | static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx, |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 4603 | bool IsZero, |
| 4604 | const X86Subtarget *Subtarget, |
| Bruno Cardoso Lopes | 0c4b9ff | 2011-09-15 18:27:36 +0000 | [diff] [blame] | 4605 | SelectionDAG &DAG) { |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4606 | EVT VT = V2.getValueType(); |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 4607 | SDValue V1 = IsZero |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4608 | ? getZeroVector(VT, Subtarget, DAG, SDLoc(V2)) : DAG.getUNDEF(VT); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4609 | unsigned NumElems = VT.getVectorNumElements(); |
| 4610 | SmallVector<int, 16> MaskVec; |
| Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 4611 | for (unsigned i = 0; i != NumElems; ++i) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4612 | // If this is the insertion idx, put the low elt of V2 here. |
| 4613 | MaskVec.push_back(i == Idx ? NumElems : i); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4614 | return DAG.getVectorShuffle(VT, SDLoc(V2), V1, V2, &MaskVec[0]); |
| Evan Cheng | 017dcc6 | 2006-04-21 01:05:10 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4617 | /// getTargetShuffleMask - Calculates the shuffle mask corresponding to the |
| 4618 | /// target specific opcode. Returns true if the Mask could be calculated. |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 4619 | /// Sets IsUnary to true if only uses one source. |
| Craig Topper | d978c54 | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 4620 | static bool getTargetShuffleMask(SDNode *N, MVT VT, |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 4621 | SmallVectorImpl<int> &Mask, bool &IsUnary) { |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4622 | unsigned NumElems = VT.getVectorNumElements(); |
| 4623 | SDValue ImmN; |
| 4624 | |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 4625 | IsUnary = false; |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4626 | switch(N->getOpcode()) { |
| 4627 | case X86ISD::SHUFP: |
| 4628 | ImmN = N->getOperand(N->getNumOperands()-1); |
| 4629 | DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask); |
| 4630 | break; |
| 4631 | case X86ISD::UNPCKH: |
| 4632 | DecodeUNPCKHMask(VT, Mask); |
| 4633 | break; |
| 4634 | case X86ISD::UNPCKL: |
| 4635 | DecodeUNPCKLMask(VT, Mask); |
| 4636 | break; |
| 4637 | case X86ISD::MOVHLPS: |
| 4638 | DecodeMOVHLPSMask(NumElems, Mask); |
| 4639 | break; |
| 4640 | case X86ISD::MOVLHPS: |
| 4641 | DecodeMOVLHPSMask(NumElems, Mask); |
| 4642 | break; |
| Craig Topper | 4aee1bb | 2013-01-28 06:48:25 +0000 | [diff] [blame] | 4643 | case X86ISD::PALIGNR: |
| Benjamin Kramer | 200b306 | 2013-01-26 13:31:37 +0000 | [diff] [blame] | 4644 | ImmN = N->getOperand(N->getNumOperands()-1); |
| Craig Topper | 4aee1bb | 2013-01-28 06:48:25 +0000 | [diff] [blame] | 4645 | DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask); |
| Benjamin Kramer | 200b306 | 2013-01-26 13:31:37 +0000 | [diff] [blame] | 4646 | break; |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4647 | case X86ISD::PSHUFD: |
| 4648 | case X86ISD::VPERMILP: |
| 4649 | ImmN = N->getOperand(N->getNumOperands()-1); |
| 4650 | DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask); |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 4651 | IsUnary = true; |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4652 | break; |
| 4653 | case X86ISD::PSHUFHW: |
| 4654 | ImmN = N->getOperand(N->getNumOperands()-1); |
| Craig Topper | a9a568a | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 4655 | DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask); |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 4656 | IsUnary = true; |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4657 | break; |
| 4658 | case X86ISD::PSHUFLW: |
| 4659 | ImmN = N->getOperand(N->getNumOperands()-1); |
| Craig Topper | a9a568a | 2012-05-02 08:03:44 +0000 | [diff] [blame] | 4660 | DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask); |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 4661 | IsUnary = true; |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4662 | break; |
| Craig Topper | bdcbcb3 | 2012-05-06 18:54:26 +0000 | [diff] [blame] | 4663 | case X86ISD::VPERMI: |
| 4664 | ImmN = N->getOperand(N->getNumOperands()-1); |
| 4665 | DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask); |
| 4666 | IsUnary = true; |
| 4667 | break; |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4668 | case X86ISD::MOVSS: |
| 4669 | case X86ISD::MOVSD: { |
| 4670 | // The index 0 always comes from the first element of the second source, |
| 4671 | // this is why MOVSS and MOVSD are used in the first place. The other |
| 4672 | // elements come from the other positions of the first source vector |
| 4673 | Mask.push_back(NumElems); |
| 4674 | for (unsigned i = 1; i != NumElems; ++i) { |
| 4675 | Mask.push_back(i); |
| 4676 | } |
| 4677 | break; |
| 4678 | } |
| 4679 | case X86ISD::VPERM2X128: |
| 4680 | ImmN = N->getOperand(N->getNumOperands()-1); |
| 4681 | DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask); |
| Craig Topper | 2091df3 | 2012-04-17 05:54:54 +0000 | [diff] [blame] | 4682 | if (Mask.empty()) return false; |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4683 | break; |
| 4684 | case X86ISD::MOVDDUP: |
| 4685 | case X86ISD::MOVLHPD: |
| 4686 | case X86ISD::MOVLPD: |
| 4687 | case X86ISD::MOVLPS: |
| 4688 | case X86ISD::MOVSHDUP: |
| 4689 | case X86ISD::MOVSLDUP: |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4690 | // Not yet implemented |
| 4691 | return false; |
| 4692 | default: llvm_unreachable("unknown target shuffle node"); |
| 4693 | } |
| 4694 | |
| 4695 | return true; |
| 4696 | } |
| 4697 | |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4698 | /// getShuffleScalarElt - Returns the scalar element that will make up the ith |
| 4699 | /// element of the result of the vector shuffle. |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4700 | static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG, |
| Benjamin Kramer | 050db52 | 2011-03-26 12:38:19 +0000 | [diff] [blame] | 4701 | unsigned Depth) { |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 4702 | if (Depth == 6) |
| 4703 | return SDValue(); // Limit search depth. |
| 4704 | |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4705 | SDValue V = SDValue(N, 0); |
| 4706 | EVT VT = V.getValueType(); |
| 4707 | unsigned Opcode = V.getOpcode(); |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4708 | |
| 4709 | // Recurse into ISD::VECTOR_SHUFFLE node to find scalars. |
| 4710 | if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) { |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4711 | int Elt = SV->getMaskElt(Index); |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4712 | |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4713 | if (Elt < 0) |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4714 | return DAG.getUNDEF(VT.getVectorElementType()); |
| 4715 | |
| Craig Topper | d156dc1 | 2012-02-06 07:17:51 +0000 | [diff] [blame] | 4716 | unsigned NumElems = VT.getVectorNumElements(); |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4717 | SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0) |
| 4718 | : SV->getOperand(1); |
| 4719 | return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1); |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4720 | } |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4721 | |
| 4722 | // Recurse into target specific vector shuffles to find scalars. |
| 4723 | if (isTargetShuffle(Opcode)) { |
| Craig Topper | d978c54 | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 4724 | MVT ShufVT = V.getValueType().getSimpleVT(); |
| 4725 | unsigned NumElems = ShufVT.getVectorNumElements(); |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4726 | SmallVector<int, 16> ShuffleMask; |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 4727 | bool IsUnary; |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 4728 | |
| Craig Topper | d978c54 | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 4729 | if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary)) |
| Craig Topper | a1ffc68 | 2012-03-20 06:42:26 +0000 | [diff] [blame] | 4730 | return SDValue(); |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 4731 | |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4732 | int Elt = ShuffleMask[Index]; |
| 4733 | if (Elt < 0) |
| Craig Topper | d978c54 | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 4734 | return DAG.getUNDEF(ShufVT.getVectorElementType()); |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 4735 | |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4736 | SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0) |
| Craig Topper | d978c54 | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 4737 | : N->getOperand(1); |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4738 | return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 4739 | Depth+1); |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | // Actual nodes that may contain scalar elements |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4743 | if (Opcode == ISD::BITCAST) { |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4744 | V = V.getOperand(0); |
| 4745 | EVT SrcVT = V.getValueType(); |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 4746 | unsigned NumElems = VT.getVectorNumElements(); |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4747 | |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 4748 | if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems) |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4749 | return SDValue(); |
| 4750 | } |
| 4751 | |
| 4752 | if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) |
| 4753 | return (Index == 0) ? V.getOperand(0) |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4754 | : DAG.getUNDEF(VT.getVectorElementType()); |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4755 | |
| 4756 | if (V.getOpcode() == ISD::BUILD_VECTOR) |
| 4757 | return V.getOperand(Index); |
| 4758 | |
| 4759 | return SDValue(); |
| 4760 | } |
| 4761 | |
| 4762 | /// getNumOfConsecutiveZeros - Return the number of elements of a vector |
| 4763 | /// shuffle operation which come from a consecutively from a zero. The |
| Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 4764 | /// search can start in two different directions, from left or right. |
| Benjamin Kramer | a0de26c | 2013-05-17 14:48:34 +0000 | [diff] [blame] | 4765 | /// We count undefs as zeros until PreferredNum is reached. |
| 4766 | static unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, |
| 4767 | unsigned NumElems, bool ZerosFromLeft, |
| 4768 | SelectionDAG &DAG, |
| 4769 | unsigned PreferredNum = -1U) { |
| 4770 | unsigned NumZeros = 0; |
| 4771 | for (unsigned i = 0; i != NumElems; ++i) { |
| 4772 | unsigned Index = ZerosFromLeft ? i : NumElems - i - 1; |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4773 | SDValue Elt = getShuffleScalarElt(SVOp, Index, DAG, 0); |
| Benjamin Kramer | a0de26c | 2013-05-17 14:48:34 +0000 | [diff] [blame] | 4774 | if (!Elt.getNode()) |
| 4775 | break; |
| 4776 | |
| 4777 | if (X86::isZeroNode(Elt)) |
| 4778 | ++NumZeros; |
| 4779 | else if (Elt.getOpcode() == ISD::UNDEF) // Undef as zero up to PreferredNum. |
| 4780 | NumZeros = std::min(NumZeros + 1, PreferredNum); |
| 4781 | else |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4782 | break; |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4783 | } |
| 4784 | |
| Benjamin Kramer | a0de26c | 2013-05-17 14:48:34 +0000 | [diff] [blame] | 4785 | return NumZeros; |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4788 | /// isShuffleMaskConsecutive - Check if the shuffle mask indicies [MaskI, MaskE) |
| 4789 | /// correspond consecutively to elements from one of the vector operands, |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4790 | /// starting from its index OpIdx. Also tell OpNum which source vector operand. |
| 4791 | static |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4792 | bool isShuffleMaskConsecutive(ShuffleVectorSDNode *SVOp, |
| 4793 | unsigned MaskI, unsigned MaskE, unsigned OpIdx, |
| 4794 | unsigned NumElems, unsigned &OpNum) { |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4795 | bool SeenV1 = false; |
| 4796 | bool SeenV2 = false; |
| 4797 | |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4798 | for (unsigned i = MaskI; i != MaskE; ++i, ++OpIdx) { |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4799 | int Idx = SVOp->getMaskElt(i); |
| 4800 | // Ignore undef indicies |
| 4801 | if (Idx < 0) |
| 4802 | continue; |
| 4803 | |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4804 | if (Idx < (int)NumElems) |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4805 | SeenV1 = true; |
| 4806 | else |
| 4807 | SeenV2 = true; |
| 4808 | |
| 4809 | // Only accept consecutive elements from the same vector |
| 4810 | if ((Idx % NumElems != OpIdx) || (SeenV1 && SeenV2)) |
| 4811 | return false; |
| 4812 | } |
| 4813 | |
| 4814 | OpNum = SeenV1 ? 0 : 1; |
| 4815 | return true; |
| 4816 | } |
| 4817 | |
| 4818 | /// isVectorShiftRight - Returns true if the shuffle can be implemented as a |
| 4819 | /// logical left shift of a vector. |
| 4820 | static bool isVectorShiftRight(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG, |
| 4821 | bool &isLeft, SDValue &ShVal, unsigned &ShAmt) { |
| 4822 | unsigned NumElems = SVOp->getValueType(0).getVectorNumElements(); |
| Benjamin Kramer | a0de26c | 2013-05-17 14:48:34 +0000 | [diff] [blame] | 4823 | unsigned NumZeros = getNumOfConsecutiveZeros( |
| 4824 | SVOp, NumElems, false /* check zeros from right */, DAG, |
| 4825 | SVOp->getMaskElt(0)); |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4826 | unsigned OpSrc; |
| 4827 | |
| 4828 | if (!NumZeros) |
| 4829 | return false; |
| 4830 | |
| 4831 | // Considering the elements in the mask that are not consecutive zeros, |
| 4832 | // check if they consecutively come from only one of the source vectors. |
| 4833 | // |
| 4834 | // V1 = {X, A, B, C} 0 |
| 4835 | // \ \ \ / |
| 4836 | // vector_shuffle V1, V2 <1, 2, 3, X> |
| 4837 | // |
| 4838 | if (!isShuffleMaskConsecutive(SVOp, |
| 4839 | 0, // Mask Start Index |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4840 | NumElems-NumZeros, // Mask End Index(exclusive) |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4841 | NumZeros, // Where to start looking in the src vector |
| 4842 | NumElems, // Number of elements in vector |
| 4843 | OpSrc)) // Which source operand ? |
| 4844 | return false; |
| 4845 | |
| 4846 | isLeft = false; |
| 4847 | ShAmt = NumZeros; |
| 4848 | ShVal = SVOp->getOperand(OpSrc); |
| 4849 | return true; |
| 4850 | } |
| 4851 | |
| 4852 | /// isVectorShiftLeft - Returns true if the shuffle can be implemented as a |
| 4853 | /// logical left shift of a vector. |
| 4854 | static bool isVectorShiftLeft(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG, |
| 4855 | bool &isLeft, SDValue &ShVal, unsigned &ShAmt) { |
| 4856 | unsigned NumElems = SVOp->getValueType(0).getVectorNumElements(); |
| Benjamin Kramer | a0de26c | 2013-05-17 14:48:34 +0000 | [diff] [blame] | 4857 | unsigned NumZeros = getNumOfConsecutiveZeros( |
| 4858 | SVOp, NumElems, true /* check zeros from left */, DAG, |
| 4859 | NumElems - SVOp->getMaskElt(NumElems - 1) - 1); |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4860 | unsigned OpSrc; |
| 4861 | |
| 4862 | if (!NumZeros) |
| 4863 | return false; |
| 4864 | |
| 4865 | // Considering the elements in the mask that are not consecutive zeros, |
| 4866 | // check if they consecutively come from only one of the source vectors. |
| 4867 | // |
| 4868 | // 0 { A, B, X, X } = V2 |
| 4869 | // / \ / / |
| 4870 | // vector_shuffle V1, V2 <X, X, 4, 5> |
| 4871 | // |
| 4872 | if (!isShuffleMaskConsecutive(SVOp, |
| 4873 | NumZeros, // Mask Start Index |
| Craig Topper | 3d092db | 2012-03-21 02:14:01 +0000 | [diff] [blame] | 4874 | NumElems, // Mask End Index(exclusive) |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4875 | 0, // Where to start looking in the src vector |
| 4876 | NumElems, // Number of elements in vector |
| 4877 | OpSrc)) // Which source operand ? |
| 4878 | return false; |
| 4879 | |
| 4880 | isLeft = true; |
| 4881 | ShAmt = NumZeros; |
| 4882 | ShVal = SVOp->getOperand(OpSrc); |
| 4883 | return true; |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | /// isVectorShift - Returns true if the shuffle can be implemented as a |
| 4887 | /// logical left or right shift of a vector. |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4888 | static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG, |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4889 | bool &isLeft, SDValue &ShVal, unsigned &ShAmt) { |
| Bruno Cardoso Lopes | 0c4b9ff | 2011-09-15 18:27:36 +0000 | [diff] [blame] | 4890 | // Although the logic below support any bitwidth size, there are no |
| 4891 | // shift instructions which handle more than 128-bit vectors. |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 4892 | if (!SVOp->getValueType(0).is128BitVector()) |
| Bruno Cardoso Lopes | 0c4b9ff | 2011-09-15 18:27:36 +0000 | [diff] [blame] | 4893 | return false; |
| 4894 | |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4895 | if (isVectorShiftLeft(SVOp, DAG, isLeft, ShVal, ShAmt) || |
| 4896 | isVectorShiftRight(SVOp, DAG, isLeft, ShVal, ShAmt)) |
| 4897 | return true; |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4898 | |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 4899 | return false; |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4902 | /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8. |
| 4903 | /// |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4904 | static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros, |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4905 | unsigned NumNonZero, unsigned NumZero, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4906 | SelectionDAG &DAG, |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 4907 | const X86Subtarget* Subtarget, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4908 | const TargetLowering &TLI) { |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4909 | if (NumNonZero > 8) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4910 | return SDValue(); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4911 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4912 | SDLoc dl(Op); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4913 | SDValue V(0, 0); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4914 | bool First = true; |
| 4915 | for (unsigned i = 0; i < 16; ++i) { |
| 4916 | bool ThisIsNonZero = (NonZeros & (1 << i)) != 0; |
| 4917 | if (ThisIsNonZero && First) { |
| 4918 | if (NumZero) |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 4919 | V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4920 | else |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4921 | V = DAG.getUNDEF(MVT::v8i16); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4922 | First = false; |
| 4923 | } |
| 4924 | |
| 4925 | if ((i & 1) != 0) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4926 | SDValue ThisElt(0, 0), LastElt(0, 0); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4927 | bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0; |
| 4928 | if (LastIsNonZero) { |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4929 | LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4930 | MVT::i16, Op.getOperand(i-1)); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4931 | } |
| 4932 | if (ThisIsNonZero) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4933 | ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i)); |
| 4934 | ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16, |
| 4935 | ThisElt, DAG.getConstant(8, MVT::i8)); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4936 | if (LastIsNonZero) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4937 | ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4938 | } else |
| 4939 | ThisElt = LastElt; |
| 4940 | |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 4941 | if (ThisElt.getNode()) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4942 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt, |
| Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4943 | DAG.getIntPtrConstant(i/2)); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4944 | } |
| 4945 | } |
| 4946 | |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4947 | return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4948 | } |
| 4949 | |
| Bill Wendling | a348c56 | 2007-03-22 18:42:45 +0000 | [diff] [blame] | 4950 | /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16. |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4951 | /// |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4952 | static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4953 | unsigned NumNonZero, unsigned NumZero, |
| 4954 | SelectionDAG &DAG, |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 4955 | const X86Subtarget* Subtarget, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4956 | const TargetLowering &TLI) { |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4957 | if (NumNonZero > 4) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4958 | return SDValue(); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4959 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4960 | SDLoc dl(Op); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4961 | SDValue V(0, 0); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4962 | bool First = true; |
| 4963 | for (unsigned i = 0; i < 8; ++i) { |
| 4964 | bool isNonZero = (NonZeros & (1 << i)) != 0; |
| 4965 | if (isNonZero) { |
| 4966 | if (First) { |
| 4967 | if (NumZero) |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 4968 | V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4969 | else |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4970 | V = DAG.getUNDEF(MVT::v8i16); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4971 | First = false; |
| 4972 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 4973 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 4974 | MVT::v8i16, V, Op.getOperand(i), |
| Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 4975 | DAG.getIntPtrConstant(i)); |
| Evan Cheng | c78d3b4 | 2006-04-24 18:01:45 +0000 | [diff] [blame] | 4976 | } |
| 4977 | } |
| 4978 | |
| 4979 | return V; |
| 4980 | } |
| 4981 | |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4982 | /// getVShift - Return a vector logical shift node. |
| 4983 | /// |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 4984 | static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp, |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 4985 | unsigned NumBits, SelectionDAG &DAG, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4986 | const TargetLowering &TLI, SDLoc dl) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 4987 | assert(VT.is128BitVector() && "Unknown type for VShift"); |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 4988 | EVT ShVT = MVT::v2i64; |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 4989 | unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ; |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 4990 | SrcOp = DAG.getNode(ISD::BITCAST, dl, ShVT, SrcOp); |
| 4991 | return DAG.getNode(ISD::BITCAST, dl, VT, |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 4992 | DAG.getNode(Opc, dl, ShVT, SrcOp, |
| Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 4993 | DAG.getConstant(NumBits, |
| Michael Liao | a6b20ce | 2013-03-01 18:40:30 +0000 | [diff] [blame] | 4994 | TLI.getScalarShiftAmountTy(SrcOp.getValueType())))); |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 4995 | } |
| 4996 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 4997 | SDValue |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 4998 | X86TargetLowering::LowerAsSplatVectorLoad(SDValue SrcOp, EVT VT, SDLoc dl, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 4999 | SelectionDAG &DAG) const { |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5000 | |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5001 | // Check if the scalar load can be widened into a vector load. And if |
| 5002 | // the address is "base + cst" see if the cst can be "absorbed" into |
| 5003 | // the shuffle mask. |
| 5004 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) { |
| 5005 | SDValue Ptr = LD->getBasePtr(); |
| 5006 | if (!ISD::isNormalLoad(LD) || LD->isVolatile()) |
| 5007 | return SDValue(); |
| 5008 | EVT PVT = LD->getValueType(0); |
| 5009 | if (PVT != MVT::i32 && PVT != MVT::f32) |
| 5010 | return SDValue(); |
| 5011 | |
| 5012 | int FI = -1; |
| 5013 | int64_t Offset = 0; |
| 5014 | if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) { |
| 5015 | FI = FINode->getIndex(); |
| 5016 | Offset = 0; |
| Chris Lattner | 0a9481f | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 5017 | } else if (DAG.isBaseWithConstantOffset(Ptr) && |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5018 | isa<FrameIndexSDNode>(Ptr.getOperand(0))) { |
| 5019 | FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex(); |
| 5020 | Offset = Ptr.getConstantOperandVal(1); |
| 5021 | Ptr = Ptr.getOperand(0); |
| 5022 | } else { |
| 5023 | return SDValue(); |
| 5024 | } |
| 5025 | |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5026 | // FIXME: 256-bit vector instructions don't require a strict alignment, |
| 5027 | // improve this code to support it better. |
| 5028 | unsigned RequiredAlign = VT.getSizeInBits()/8; |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5029 | SDValue Chain = LD->getChain(); |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5030 | // Make sure the stack object alignment is at least 16 or 32. |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5031 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5032 | if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) { |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5033 | if (MFI->isFixedObjectIndex(FI)) { |
| Eric Christopher | e9625cf | 2010-01-23 06:02:43 +0000 | [diff] [blame] | 5034 | // Can't change the alignment. FIXME: It's possible to compute |
| 5035 | // the exact stack offset and reference FI + adjust offset instead. |
| 5036 | // If someone *really* cares about this. That's the way to implement it. |
| 5037 | return SDValue(); |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5038 | } else { |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5039 | MFI->setObjectAlignment(FI, RequiredAlign); |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5040 | } |
| 5041 | } |
| 5042 | |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5043 | // (Offset % 16 or 32) must be multiple of 4. Then address is then |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5044 | // Ptr + (Offset & ~15). |
| 5045 | if (Offset < 0) |
| 5046 | return SDValue(); |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5047 | if ((Offset % RequiredAlign) & 3) |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5048 | return SDValue(); |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5049 | int64_t StartOffset = Offset & ~(RequiredAlign-1); |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5050 | if (StartOffset) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5051 | Ptr = DAG.getNode(ISD::ADD, SDLoc(Ptr), Ptr.getValueType(), |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5052 | Ptr,DAG.getConstant(StartOffset, Ptr.getValueType())); |
| 5053 | |
| 5054 | int EltNo = (Offset - StartOffset) >> 2; |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 5055 | unsigned NumElems = VT.getVectorNumElements(); |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5056 | |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5057 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems); |
| 5058 | SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr, |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 5059 | LD->getPointerInfo().getWithOffset(StartOffset), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 5060 | false, false, false, 0); |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5061 | |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5062 | SmallVector<int, 8> Mask; |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 5063 | for (unsigned i = 0; i != NumElems; ++i) |
| Bruno Cardoso Lopes | ac5f13f | 2011-08-02 16:06:18 +0000 | [diff] [blame] | 5064 | Mask.push_back(EltNo); |
| 5065 | |
| Craig Topper | cc300063 | 2012-01-30 07:50:31 +0000 | [diff] [blame] | 5066 | return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]); |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | return SDValue(); |
| 5070 | } |
| 5071 | |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5072 | /// EltsFromConsecutiveLoads - Given the initializing elements 'Elts' of a |
| 5073 | /// vector of type 'VT', see if the elements can be replaced by a single large |
| Nate Begeman | 1449f29 | 2010-03-24 22:19:06 +0000 | [diff] [blame] | 5074 | /// load which has the same value as a build_vector whose operands are 'elts'. |
| 5075 | /// |
| 5076 | /// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5077 | /// |
| Nate Begeman | 1449f29 | 2010-03-24 22:19:06 +0000 | [diff] [blame] | 5078 | /// FIXME: we'd also like to handle the case where the last elements are zero |
| 5079 | /// rather than undef via VZEXT_LOAD, but we do not detect that case today. |
| 5080 | /// There's even a handy isZeroNode for that purpose. |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5081 | static SDValue EltsFromConsecutiveLoads(EVT VT, SmallVectorImpl<SDValue> &Elts, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5082 | SDLoc &DL, SelectionDAG &DAG) { |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5083 | EVT EltVT = VT.getVectorElementType(); |
| 5084 | unsigned NumElems = Elts.size(); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5085 | |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5086 | LoadSDNode *LDBase = NULL; |
| 5087 | unsigned LastLoadedElt = -1U; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5088 | |
| Nate Begeman | 1449f29 | 2010-03-24 22:19:06 +0000 | [diff] [blame] | 5089 | // For each element in the initializer, see if we've found a load or an undef. |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5090 | // If we don't find an initial load element, or later load elements are |
| Nate Begeman | 1449f29 | 2010-03-24 22:19:06 +0000 | [diff] [blame] | 5091 | // non-consecutive, bail out. |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5092 | for (unsigned i = 0; i < NumElems; ++i) { |
| 5093 | SDValue Elt = Elts[i]; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5094 | |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5095 | if (!Elt.getNode() || |
| 5096 | (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode()))) |
| 5097 | return SDValue(); |
| 5098 | if (!LDBase) { |
| 5099 | if (Elt.getNode()->getOpcode() == ISD::UNDEF) |
| 5100 | return SDValue(); |
| 5101 | LDBase = cast<LoadSDNode>(Elt.getNode()); |
| 5102 | LastLoadedElt = i; |
| 5103 | continue; |
| 5104 | } |
| 5105 | if (Elt.getOpcode() == ISD::UNDEF) |
| 5106 | continue; |
| 5107 | |
| 5108 | LoadSDNode *LD = cast<LoadSDNode>(Elt); |
| 5109 | if (!DAG.isConsecutiveLoad(LD, LDBase, EltVT.getSizeInBits()/8, i)) |
| 5110 | return SDValue(); |
| 5111 | LastLoadedElt = i; |
| 5112 | } |
| Nate Begeman | 1449f29 | 2010-03-24 22:19:06 +0000 | [diff] [blame] | 5113 | |
| 5114 | // If we have found an entire vector of loads and undefs, then return a large |
| 5115 | // load of the entire vector width starting at the base pointer. If we found |
| 5116 | // consecutive loads for the low half, generate a vzext_load node. |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5117 | if (LastLoadedElt == NumElems - 1) { |
| Nadav Rotem | 23d1d5e | 2013-05-22 19:28:41 +0000 | [diff] [blame] | 5118 | SDValue NewLd = SDValue(); |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5119 | if (DAG.InferPtrAlignment(LDBase->getBasePtr()) >= 16) |
| Nadav Rotem | 23d1d5e | 2013-05-22 19:28:41 +0000 | [diff] [blame] | 5120 | NewLd = DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(), |
| 5121 | LDBase->getPointerInfo(), |
| 5122 | LDBase->isVolatile(), LDBase->isNonTemporal(), |
| 5123 | LDBase->isInvariant(), 0); |
| 5124 | NewLd = DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(), |
| 5125 | LDBase->getPointerInfo(), |
| 5126 | LDBase->isVolatile(), LDBase->isNonTemporal(), |
| 5127 | LDBase->isInvariant(), LDBase->getAlignment()); |
| 5128 | |
| 5129 | if (LDBase->hasAnyUseOfValue(1)) { |
| 5130 | SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, |
| 5131 | SDValue(LDBase, 1), |
| 5132 | SDValue(NewLd.getNode(), 1)); |
| 5133 | DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain); |
| 5134 | DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1), |
| 5135 | SDValue(NewLd.getNode(), 1)); |
| 5136 | } |
| 5137 | |
| 5138 | return NewLd; |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 5139 | } |
| 5140 | if (NumElems == 4 && LastLoadedElt == 1 && |
| 5141 | DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) { |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5142 | SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other); |
| 5143 | SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() }; |
| Eli Friedman | 322ea08 | 2011-09-14 23:42:45 +0000 | [diff] [blame] | 5144 | SDValue ResNode = |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 5145 | DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, |
| 5146 | array_lengthof(Ops), MVT::i64, |
| Eli Friedman | 322ea08 | 2011-09-14 23:42:45 +0000 | [diff] [blame] | 5147 | LDBase->getPointerInfo(), |
| 5148 | LDBase->getAlignment(), |
| 5149 | false/*isVolatile*/, true/*ReadMem*/, |
| 5150 | false/*WriteMem*/); |
| Manman Ren | 2b7a2e8 | 2012-08-31 23:16:57 +0000 | [diff] [blame] | 5151 | |
| 5152 | // Make sure the newly-created LOAD is in the same position as LDBase in |
| 5153 | // terms of dependency. We create a TokenFactor for LDBase and ResNode, and |
| 5154 | // update uses of LDBase's output chain to use the TokenFactor. |
| 5155 | if (LDBase->hasAnyUseOfValue(1)) { |
| 5156 | SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, |
| 5157 | SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1)); |
| 5158 | DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain); |
| 5159 | DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1), |
| 5160 | SDValue(ResNode.getNode(), 1)); |
| 5161 | } |
| 5162 | |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5163 | return DAG.getNode(ISD::BITCAST, DL, VT, ResNode); |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5164 | } |
| 5165 | return SDValue(); |
| 5166 | } |
| 5167 | |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5168 | /// LowerVectorBroadcast - Attempt to use the vbroadcast instruction |
| 5169 | /// to generate a splat value for the following cases: |
| 5170 | /// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant. |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5171 | /// 2. A splat shuffle which uses a scalar_to_vector node which comes from |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5172 | /// a scalar load, or a constant. |
| 5173 | /// The VBROADCAST node is returned when a pattern is found, |
| Nadav Rotem | cbbe33f | 2011-11-18 02:49:55 +0000 | [diff] [blame] | 5174 | /// or SDValue() otherwise. |
| Nadav Rotem | 154819d | 2012-04-09 07:45:58 +0000 | [diff] [blame] | 5175 | SDValue |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 5176 | X86TargetLowering::LowerVectorBroadcast(SDValue Op, SelectionDAG &DAG) const { |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 5177 | if (!Subtarget->hasFp256()) |
| Craig Topper | a937633 | 2012-01-10 08:23:59 +0000 | [diff] [blame] | 5178 | return SDValue(); |
| 5179 | |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 5180 | MVT VT = Op.getValueType().getSimpleVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5181 | SDLoc dl(Op); |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5182 | |
| Craig Topper | 5da8a80 | 2012-05-04 05:49:51 +0000 | [diff] [blame] | 5183 | assert((VT.is128BitVector() || VT.is256BitVector()) && |
| 5184 | "Unsupported vector type for broadcast."); |
| 5185 | |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5186 | SDValue Ld; |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5187 | bool ConstSplatVal; |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5188 | |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5189 | switch (Op.getOpcode()) { |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5190 | default: |
| 5191 | // Unknown pattern found. |
| 5192 | return SDValue(); |
| 5193 | |
| 5194 | case ISD::BUILD_VECTOR: { |
| 5195 | // The BUILD_VECTOR node must be a splat. |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5196 | if (!isSplatVector(Op.getNode())) |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5197 | return SDValue(); |
| 5198 | |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5199 | Ld = Op.getOperand(0); |
| 5200 | ConstSplatVal = (Ld.getOpcode() == ISD::Constant || |
| 5201 | Ld.getOpcode() == ISD::ConstantFP); |
| Nadav Rotem | cbbe33f | 2011-11-18 02:49:55 +0000 | [diff] [blame] | 5202 | |
| 5203 | // The suspected load node has several users. Make sure that all |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5204 | // of its users are from the BUILD_VECTOR node. |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5205 | // Constants may have multiple users. |
| 5206 | if (!ConstSplatVal && !Ld->hasNUsesOfValue(VT.getVectorNumElements(), 0)) |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5207 | return SDValue(); |
| Nadav Rotem | cbbe33f | 2011-11-18 02:49:55 +0000 | [diff] [blame] | 5208 | break; |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | case ISD::VECTOR_SHUFFLE: { |
| 5212 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
| 5213 | |
| 5214 | // Shuffles must have a splat mask where the first element is |
| 5215 | // broadcasted. |
| Nadav Rotem | cbbe33f | 2011-11-18 02:49:55 +0000 | [diff] [blame] | 5216 | if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0) |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5217 | return SDValue(); |
| 5218 | |
| 5219 | SDValue Sc = Op.getOperand(0); |
| Nadav Rotem | b88e8dd | 2012-05-10 12:50:02 +0000 | [diff] [blame] | 5220 | if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR && |
| Elena Demikhovsky | 8f40f7b | 2012-07-01 06:12:26 +0000 | [diff] [blame] | 5221 | Sc.getOpcode() != ISD::BUILD_VECTOR) { |
| 5222 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 5223 | if (!Subtarget->hasInt256()) |
| Elena Demikhovsky | 8f40f7b | 2012-07-01 06:12:26 +0000 | [diff] [blame] | 5224 | return SDValue(); |
| 5225 | |
| 5226 | // Use the register form of the broadcast instruction available on AVX2. |
| 5227 | if (VT.is256BitVector()) |
| 5228 | Sc = Extract128BitVector(Sc, 0, DAG, dl); |
| 5229 | return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc); |
| 5230 | } |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5231 | |
| 5232 | Ld = Sc.getOperand(0); |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5233 | ConstSplatVal = (Ld.getOpcode() == ISD::Constant || |
| Nadav Rotem | 154819d | 2012-04-09 07:45:58 +0000 | [diff] [blame] | 5234 | Ld.getOpcode() == ISD::ConstantFP); |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5235 | |
| 5236 | // The scalar_to_vector node and the suspected |
| 5237 | // load node must have exactly one user. |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5238 | // Constants may have multiple users. |
| 5239 | if (!ConstSplatVal && (!Sc.hasOneUse() || !Ld.hasOneUse())) |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5240 | return SDValue(); |
| 5241 | break; |
| 5242 | } |
| 5243 | } |
| Nadav Rotem | cbbe33f | 2011-11-18 02:49:55 +0000 | [diff] [blame] | 5244 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 5245 | bool Is256 = VT.is256BitVector(); |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5246 | |
| 5247 | // Handle the broadcasting a single constant scalar from the constant pool |
| 5248 | // into a vector. On Sandybridge it is still better to load a constant vector |
| 5249 | // from the constant pool and not to broadcast it from a scalar. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 5250 | if (ConstSplatVal && Subtarget->hasInt256()) { |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5251 | EVT CVT = Ld.getValueType(); |
| 5252 | assert(!CVT.isVector() && "Must not broadcast a vector type"); |
| 5253 | unsigned ScalarSize = CVT.getSizeInBits(); |
| 5254 | |
| Craig Topper | 5da8a80 | 2012-05-04 05:49:51 +0000 | [diff] [blame] | 5255 | if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) { |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5256 | const Constant *C = 0; |
| 5257 | if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld)) |
| 5258 | C = CI->getConstantIntValue(); |
| 5259 | else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld)) |
| 5260 | C = CF->getConstantFPValue(); |
| 5261 | |
| 5262 | assert(C && "Invalid constant type"); |
| 5263 | |
| Nadav Rotem | 154819d | 2012-04-09 07:45:58 +0000 | [diff] [blame] | 5264 | SDValue CP = DAG.getConstantPool(C, getPointerTy()); |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5265 | unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment(); |
| Nadav Rotem | 154819d | 2012-04-09 07:45:58 +0000 | [diff] [blame] | 5266 | Ld = DAG.getLoad(CVT, dl, DAG.getEntryNode(), CP, |
| Craig Topper | 6643d9c | 2012-05-04 06:18:33 +0000 | [diff] [blame] | 5267 | MachinePointerInfo::getConstantPool(), |
| 5268 | false, false, false, Alignment); |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5269 | |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5270 | return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld); |
| 5271 | } |
| 5272 | } |
| 5273 | |
| Nadav Rotem | 4fc8a5d | 2012-05-19 19:57:37 +0000 | [diff] [blame] | 5274 | bool IsLoad = ISD::isNormalLoad(Ld.getNode()); |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5275 | unsigned ScalarSize = Ld.getValueType().getSizeInBits(); |
| 5276 | |
| Nadav Rotem | 4fc8a5d | 2012-05-19 19:57:37 +0000 | [diff] [blame] | 5277 | // Handle AVX2 in-register broadcasts. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 5278 | if (!IsLoad && Subtarget->hasInt256() && |
| Nadav Rotem | 4fc8a5d | 2012-05-19 19:57:37 +0000 | [diff] [blame] | 5279 | (ScalarSize == 32 || (Is256 && ScalarSize == 64))) |
| 5280 | return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld); |
| 5281 | |
| 5282 | // The scalar source must be a normal load. |
| 5283 | if (!IsLoad) |
| 5284 | return SDValue(); |
| 5285 | |
| Craig Topper | 5da8a80 | 2012-05-04 05:49:51 +0000 | [diff] [blame] | 5286 | if (ScalarSize == 32 || (Is256 && ScalarSize == 64)) |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5287 | return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld); |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5288 | |
| Craig Topper | a937633 | 2012-01-10 08:23:59 +0000 | [diff] [blame] | 5289 | // The integer check is needed for the 64-bit into 128-bit so it doesn't match |
| Craig Topper | 5da8a80 | 2012-05-04 05:49:51 +0000 | [diff] [blame] | 5290 | // double since there is no vbroadcastsd xmm |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 5291 | if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) { |
| Craig Topper | 5da8a80 | 2012-05-04 05:49:51 +0000 | [diff] [blame] | 5292 | if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64) |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5293 | return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld); |
| Craig Topper | a937633 | 2012-01-10 08:23:59 +0000 | [diff] [blame] | 5294 | } |
| Nadav Rotem | cbbe33f | 2011-11-18 02:49:55 +0000 | [diff] [blame] | 5295 | |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5296 | // Unsupported broadcast. |
| 5297 | return SDValue(); |
| 5298 | } |
| 5299 | |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5300 | SDValue |
| Michael Liao | facace8 | 2012-10-19 17:15:18 +0000 | [diff] [blame] | 5301 | X86TargetLowering::buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) const { |
| 5302 | EVT VT = Op.getValueType(); |
| 5303 | |
| 5304 | // Skip if insert_vec_elt is not supported. |
| 5305 | if (!isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT)) |
| 5306 | return SDValue(); |
| 5307 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5308 | SDLoc DL(Op); |
| Michael Liao | facace8 | 2012-10-19 17:15:18 +0000 | [diff] [blame] | 5309 | unsigned NumElems = Op.getNumOperands(); |
| 5310 | |
| 5311 | SDValue VecIn1; |
| 5312 | SDValue VecIn2; |
| 5313 | SmallVector<unsigned, 4> InsertIndices; |
| 5314 | SmallVector<int, 8> Mask(NumElems, -1); |
| 5315 | |
| 5316 | for (unsigned i = 0; i != NumElems; ++i) { |
| 5317 | unsigned Opc = Op.getOperand(i).getOpcode(); |
| 5318 | |
| 5319 | if (Opc == ISD::UNDEF) |
| 5320 | continue; |
| 5321 | |
| 5322 | if (Opc != ISD::EXTRACT_VECTOR_ELT) { |
| 5323 | // Quit if more than 1 elements need inserting. |
| 5324 | if (InsertIndices.size() > 1) |
| 5325 | return SDValue(); |
| 5326 | |
| 5327 | InsertIndices.push_back(i); |
| 5328 | continue; |
| 5329 | } |
| 5330 | |
| 5331 | SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0); |
| 5332 | SDValue ExtIdx = Op.getOperand(i).getOperand(1); |
| 5333 | |
| 5334 | // Quit if extracted from vector of different type. |
| 5335 | if (ExtractedFromVec.getValueType() != VT) |
| 5336 | return SDValue(); |
| 5337 | |
| 5338 | // Quit if non-constant index. |
| 5339 | if (!isa<ConstantSDNode>(ExtIdx)) |
| 5340 | return SDValue(); |
| 5341 | |
| 5342 | if (VecIn1.getNode() == 0) |
| 5343 | VecIn1 = ExtractedFromVec; |
| 5344 | else if (VecIn1 != ExtractedFromVec) { |
| 5345 | if (VecIn2.getNode() == 0) |
| 5346 | VecIn2 = ExtractedFromVec; |
| 5347 | else if (VecIn2 != ExtractedFromVec) |
| 5348 | // Quit if more than 2 vectors to shuffle |
| 5349 | return SDValue(); |
| 5350 | } |
| 5351 | |
| 5352 | unsigned Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue(); |
| 5353 | |
| 5354 | if (ExtractedFromVec == VecIn1) |
| 5355 | Mask[i] = Idx; |
| 5356 | else if (ExtractedFromVec == VecIn2) |
| 5357 | Mask[i] = Idx + NumElems; |
| 5358 | } |
| 5359 | |
| 5360 | if (VecIn1.getNode() == 0) |
| 5361 | return SDValue(); |
| 5362 | |
| 5363 | VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT); |
| 5364 | SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]); |
| 5365 | for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) { |
| 5366 | unsigned Idx = InsertIndices[i]; |
| 5367 | NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx), |
| 5368 | DAG.getIntPtrConstant(Idx)); |
| 5369 | } |
| 5370 | |
| 5371 | return NV; |
| 5372 | } |
| 5373 | |
| 5374 | SDValue |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 5375 | X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5376 | SDLoc dl(Op); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 5377 | |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 5378 | MVT VT = Op.getValueType().getSimpleVT(); |
| 5379 | MVT ExtVT = VT.getVectorElementType(); |
| David Greene | f125a29 | 2011-02-08 19:04:41 +0000 | [diff] [blame] | 5380 | unsigned NumElems = Op.getNumOperands(); |
| 5381 | |
| Bruno Cardoso Lopes | 531f19f | 2011-08-01 19:51:53 +0000 | [diff] [blame] | 5382 | // Vectors containing all zeros can be matched by pxor and xorps later |
| 5383 | if (ISD::isBuildVectorAllZeros(Op.getNode())) { |
| 5384 | // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd |
| 5385 | // and 2) ensure that i64 scalars are eliminated on x86-32 hosts. |
| Craig Topper | 07a2762 | 2012-01-22 03:07:48 +0000 | [diff] [blame] | 5386 | if (VT == MVT::v4i32 || VT == MVT::v8i32) |
| Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 5387 | return Op; |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5388 | |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 5389 | return getZeroVector(VT, Subtarget, DAG, dl); |
| Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 5390 | } |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5391 | |
| Bruno Cardoso Lopes | 531f19f | 2011-08-01 19:51:53 +0000 | [diff] [blame] | 5392 | // Vectors containing all ones can be matched by pcmpeqd on 128-bit width |
| Craig Topper | 745a86b | 2011-11-19 22:34:59 +0000 | [diff] [blame] | 5393 | // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use |
| 5394 | // vpcmpeqd on 256-bit vectors. |
| Michael Liao | d09318f | 2013-02-25 23:16:36 +0000 | [diff] [blame] | 5395 | if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) { |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 5396 | if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256())) |
| Bruno Cardoso Lopes | 531f19f | 2011-08-01 19:51:53 +0000 | [diff] [blame] | 5397 | return Op; |
| 5398 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 5399 | return getOnesVector(VT, Subtarget->hasInt256(), DAG, dl); |
| Bruno Cardoso Lopes | 531f19f | 2011-08-01 19:51:53 +0000 | [diff] [blame] | 5400 | } |
| 5401 | |
| Nadav Rotem | 154819d | 2012-04-09 07:45:58 +0000 | [diff] [blame] | 5402 | SDValue Broadcast = LowerVectorBroadcast(Op, DAG); |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 5403 | if (Broadcast.getNode()) |
| 5404 | return Broadcast; |
| Nadav Rotem | f8c10e5 | 2011-11-15 22:50:37 +0000 | [diff] [blame] | 5405 | |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 5406 | unsigned EVTBits = ExtVT.getSizeInBits(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5407 | |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5408 | unsigned NumZero = 0; |
| 5409 | unsigned NumNonZero = 0; |
| 5410 | unsigned NonZeros = 0; |
| Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 5411 | bool IsAllConstants = true; |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5412 | SmallSet<SDValue, 8> Values; |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5413 | for (unsigned i = 0; i < NumElems; ++i) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5414 | SDValue Elt = Op.getOperand(i); |
| Evan Cheng | db2d524 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 5415 | if (Elt.getOpcode() == ISD::UNDEF) |
| 5416 | continue; |
| 5417 | Values.insert(Elt); |
| 5418 | if (Elt.getOpcode() != ISD::Constant && |
| 5419 | Elt.getOpcode() != ISD::ConstantFP) |
| Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 5420 | IsAllConstants = false; |
| Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 5421 | if (X86::isZeroNode(Elt)) |
| Evan Cheng | db2d524 | 2007-12-12 06:45:40 +0000 | [diff] [blame] | 5422 | NumZero++; |
| 5423 | else { |
| 5424 | NonZeros |= (1 << i); |
| 5425 | NumNonZero++; |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5426 | } |
| 5427 | } |
| 5428 | |
| Chris Lattner | 97a2a56 | 2010-08-26 05:24:29 +0000 | [diff] [blame] | 5429 | // All undef vector. Return an UNDEF. All zero vectors were handled above. |
| 5430 | if (NumNonZero == 0) |
| Dale Johannesen | e8d7230 | 2009-02-06 23:05:02 +0000 | [diff] [blame] | 5431 | return DAG.getUNDEF(VT); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5432 | |
| Chris Lattner | 67f453a | 2008-03-09 05:42:06 +0000 | [diff] [blame] | 5433 | // Special case for single non-zero, non-undef, element. |
| Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 5434 | if (NumNonZero == 1) { |
| Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 5435 | unsigned Idx = countTrailingZeros(NonZeros); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5436 | SDValue Item = Op.getOperand(Idx); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5437 | |
| Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 5438 | // If this is an insertion of an i64 value on x86-32, and if the top bits of |
| 5439 | // the value are obviously zero, truncate the value to i32 and do the |
| 5440 | // insertion that way. Only do this if the value is non-constant or if the |
| 5441 | // value is a constant being inserted into element 0. It is cheaper to do |
| 5442 | // a constant pool load than it is to do a movd + shuffle. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5443 | if (ExtVT == MVT::i64 && !Subtarget->is64Bit() && |
| Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 5444 | (!IsAllConstants || Idx == 0)) { |
| 5445 | if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) { |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 5446 | // Handle SSE only. |
| 5447 | assert(VT == MVT::v2i64 && "Expected an SSE value type!"); |
| 5448 | EVT VecVT = MVT::v4i32; |
| 5449 | unsigned VecElts = 4; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5450 | |
| Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 5451 | // Truncate the value (which may itself be a constant) to i32, and |
| 5452 | // convert it to a vector with movd (S2V+shuffle to zero extend). |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5453 | Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5454 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item); |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 5455 | Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5456 | |
| Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 5457 | // Now we have our 32-bit value zero extended in the low element of |
| 5458 | // a vector. If Idx != 0, swizzle it into place. |
| 5459 | if (Idx != 0) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5460 | SmallVector<int, 4> Mask; |
| 5461 | Mask.push_back(Idx); |
| 5462 | for (unsigned i = 1; i != VecElts; ++i) |
| 5463 | Mask.push_back(i); |
| Craig Topper | df966f6 | 2012-04-22 19:17:57 +0000 | [diff] [blame] | 5464 | Item = DAG.getVectorShuffle(VecVT, dl, Item, DAG.getUNDEF(VecVT), |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5465 | &Mask[0]); |
| Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 5466 | } |
| Craig Topper | 07a2762 | 2012-01-22 03:07:48 +0000 | [diff] [blame] | 5467 | return DAG.getNode(ISD::BITCAST, dl, VT, Item); |
| Chris Lattner | 6209804 | 2008-03-09 01:05:04 +0000 | [diff] [blame] | 5468 | } |
| 5469 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5470 | |
| Chris Lattner | 19f7969 | 2008-03-08 22:59:52 +0000 | [diff] [blame] | 5471 | // If we have a constant or non-constant insertion into the low element of |
| 5472 | // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into |
| 5473 | // the rest of the elements. This will be matched as movd/movq/movss/movsd |
| Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 5474 | // depending on what the source datatype is. |
| 5475 | if (Idx == 0) { |
| Craig Topper | d62c16e | 2011-12-29 03:20:51 +0000 | [diff] [blame] | 5476 | if (NumZero == 0) |
| Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 5477 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); |
| Craig Topper | d62c16e | 2011-12-29 03:20:51 +0000 | [diff] [blame] | 5478 | |
| 5479 | if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 || |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5480 | (ExtVT == MVT::i64 && Subtarget->is64Bit())) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 5481 | if (VT.is256BitVector()) { |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 5482 | SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl); |
| Nadav Rotem | 394a1f5 | 2012-01-11 14:07:51 +0000 | [diff] [blame] | 5483 | return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec, |
| 5484 | Item, DAG.getIntPtrConstant(0)); |
| Elena Demikhovsky | 021c0a2 | 2011-12-28 08:14:01 +0000 | [diff] [blame] | 5485 | } |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 5486 | assert(VT.is128BitVector() && "Expected an SSE value type!"); |
| Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 5487 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); |
| 5488 | // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector. |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 5489 | return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG); |
| Craig Topper | d62c16e | 2011-12-29 03:20:51 +0000 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | if (ExtVT == MVT::i16 || ExtVT == MVT::i8) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5493 | Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item); |
| Craig Topper | 3224e6b | 2011-12-29 03:09:33 +0000 | [diff] [blame] | 5494 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item); |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 5495 | if (VT.is256BitVector()) { |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 5496 | SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl); |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 5497 | Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl); |
| Craig Topper | 19ec2a9 | 2011-12-29 03:34:54 +0000 | [diff] [blame] | 5498 | } else { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 5499 | assert(VT.is128BitVector() && "Expected an SSE value type!"); |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 5500 | Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG); |
| Craig Topper | 19ec2a9 | 2011-12-29 03:34:54 +0000 | [diff] [blame] | 5501 | } |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5502 | return DAG.getNode(ISD::BITCAST, dl, VT, Item); |
| Eli Friedman | 1041553 | 2009-06-06 06:05:10 +0000 | [diff] [blame] | 5503 | } |
| Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 5504 | } |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 5505 | |
| 5506 | // Is it a vector logical left shift? |
| 5507 | if (NumElems == 2 && Idx == 1 && |
| Evan Cheng | 37b7387 | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 5508 | X86::isZeroNode(Op.getOperand(0)) && |
| 5509 | !X86::isZeroNode(Op.getOperand(1))) { |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 5510 | unsigned NumBits = VT.getSizeInBits(); |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 5511 | return getVShift(true, VT, |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5512 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
| Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 5513 | VT, Op.getOperand(1)), |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5514 | NumBits/2, DAG, *this, dl); |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 5515 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5516 | |
| Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 5517 | if (IsAllConstants) // Otherwise, it's better to do a constpool load. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5518 | return SDValue(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5519 | |
| Chris Lattner | 19f7969 | 2008-03-08 22:59:52 +0000 | [diff] [blame] | 5520 | // Otherwise, if this is a vector with i32 or f32 elements, and the element |
| 5521 | // is a non-constant being inserted into an element other than the low one, |
| 5522 | // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka |
| 5523 | // movd/movss) to move this into the low element, then shuffle it into |
| 5524 | // place. |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5525 | if (EVTBits == 32) { |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5526 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5527 | |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5528 | // Turn it into a shuffle of zero and zero-extended scalar to vector. |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 5529 | Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0, Subtarget, DAG); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5530 | SmallVector<int, 8> MaskVec; |
| Craig Topper | 31a207a | 2012-05-04 06:39:13 +0000 | [diff] [blame] | 5531 | for (unsigned i = 0; i != NumElems; ++i) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5532 | MaskVec.push_back(i == Idx ? 0 : 1); |
| 5533 | return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5534 | } |
| 5535 | } |
| 5536 | |
| Chris Lattner | 67f453a | 2008-03-09 05:42:06 +0000 | [diff] [blame] | 5537 | // Splat is obviously ok. Let legalizer expand it to a shuffle. |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5538 | if (Values.size() == 1) { |
| 5539 | if (EVTBits == 32) { |
| 5540 | // Instead of a shuffle like this: |
| 5541 | // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0> |
| 5542 | // Check if it's possible to issue this instead. |
| 5543 | // shuffle (vload ptr)), undef, <1, 1, 1, 1> |
| Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 5544 | unsigned Idx = countTrailingZeros(NonZeros); |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5545 | SDValue Item = Op.getOperand(Idx); |
| 5546 | if (Op.getNode()->isOnlyUserOf(Item.getNode())) |
| 5547 | return LowerAsSplatVectorLoad(Item, VT, dl, DAG); |
| 5548 | } |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5549 | return SDValue(); |
| Evan Cheng | c363094 | 2009-12-09 21:00:30 +0000 | [diff] [blame] | 5550 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 5551 | |
| Dan Gohman | a394117 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 5552 | // A vector full of immediates; various special cases are already |
| 5553 | // handled, so this is best done with a single constant-pool load. |
| Chris Lattner | c9517fb | 2008-03-08 22:48:29 +0000 | [diff] [blame] | 5554 | if (IsAllConstants) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5555 | return SDValue(); |
| Dan Gohman | a394117 | 2007-07-24 22:55:08 +0000 | [diff] [blame] | 5556 | |
| Bruno Cardoso Lopes | 6683efb | 2011-07-22 00:15:07 +0000 | [diff] [blame] | 5557 | // For AVX-length vectors, build the individual 128-bit pieces and use |
| 5558 | // shuffles to put them in place. |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 5559 | if (VT.is256BitVector()) { |
| Bruno Cardoso Lopes | 6683efb | 2011-07-22 00:15:07 +0000 | [diff] [blame] | 5560 | SmallVector<SDValue, 32> V; |
| Craig Topper | fa5b70e | 2012-02-03 06:32:21 +0000 | [diff] [blame] | 5561 | for (unsigned i = 0; i != NumElems; ++i) |
| Bruno Cardoso Lopes | 6683efb | 2011-07-22 00:15:07 +0000 | [diff] [blame] | 5562 | V.push_back(Op.getOperand(i)); |
| 5563 | |
| 5564 | EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2); |
| 5565 | |
| 5566 | // Build both the lower and upper subvector. |
| 5567 | SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[0], NumElems/2); |
| 5568 | SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT, &V[NumElems / 2], |
| 5569 | NumElems/2); |
| 5570 | |
| 5571 | // Recreate the wider vector with the lower and upper part. |
| Craig Topper | 4c7972d | 2012-04-22 18:15:59 +0000 | [diff] [blame] | 5572 | return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl); |
| Bruno Cardoso Lopes | 6683efb | 2011-07-22 00:15:07 +0000 | [diff] [blame] | 5573 | } |
| 5574 | |
| Bill Wendling | 2f9bb1a | 2007-04-24 21:16:55 +0000 | [diff] [blame] | 5575 | // Let legalizer expand 2-wide build_vectors. |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 5576 | if (EVTBits == 64) { |
| 5577 | if (NumNonZero == 1) { |
| 5578 | // One half is zero or undef. |
| Michael J. Spencer | c6af243 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 5579 | unsigned Idx = countTrailingZeros(NonZeros); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5580 | SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 5581 | Op.getOperand(Idx)); |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 5582 | return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG); |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 5583 | } |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5584 | return SDValue(); |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 5585 | } |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5586 | |
| 5587 | // If element VT is < 32 bits, convert it to inserts into a zero vector. |
| Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 5588 | if (EVTBits == 8 && NumElems == 16) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5589 | SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG, |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 5590 | Subtarget, *this); |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5591 | if (V.getNode()) return V; |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
| Bill Wendling | 826f36f | 2007-03-28 00:57:11 +0000 | [diff] [blame] | 5594 | if (EVTBits == 16 && NumElems == 8) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5595 | SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG, |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 5596 | Subtarget, *this); |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 5597 | if (V.getNode()) return V; |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5598 | } |
| 5599 | |
| 5600 | // If element VT is == 32 bits, turn it into a number of shuffles. |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 5601 | SmallVector<SDValue, 8> V(NumElems); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5602 | if (NumElems == 4 && NumZero > 0) { |
| 5603 | for (unsigned i = 0; i < 4; ++i) { |
| 5604 | bool isZero = !(NonZeros & (1 << i)); |
| 5605 | if (isZero) |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 5606 | V[i] = getZeroVector(VT, Subtarget, DAG, dl); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5607 | else |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 5608 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i)); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | for (unsigned i = 0; i < 2; ++i) { |
| 5612 | switch ((NonZeros & (0x3 << i*2)) >> (i*2)) { |
| 5613 | default: break; |
| 5614 | case 0: |
| 5615 | V[i] = V[i*2]; // Must be a zero vector. |
| 5616 | break; |
| 5617 | case 1: |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5618 | V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5619 | break; |
| 5620 | case 2: |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5621 | V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5622 | break; |
| 5623 | case 3: |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5624 | V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5625 | break; |
| 5626 | } |
| 5627 | } |
| 5628 | |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 5629 | bool Reverse1 = (NonZeros & 0x3) == 2; |
| 5630 | bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2; |
| 5631 | int MaskVec[] = { |
| 5632 | Reverse1 ? 1 : 0, |
| 5633 | Reverse1 ? 0 : 1, |
| Benjamin Kramer | 630ecf0 | 2012-01-30 20:01:35 +0000 | [diff] [blame] | 5634 | static_cast<int>(Reverse2 ? NumElems+1 : NumElems), |
| 5635 | static_cast<int>(Reverse2 ? NumElems : NumElems+1) |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 5636 | }; |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5637 | return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5638 | } |
| 5639 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 5640 | if (Values.size() > 1 && VT.is128BitVector()) { |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5641 | // Check for a build vector of consecutive loads. |
| 5642 | for (unsigned i = 0; i < NumElems; ++i) |
| 5643 | V[i] = Op.getOperand(i); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5644 | |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 5645 | // Check for elements which are consecutive loads. |
| 5646 | SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG); |
| 5647 | if (LD.getNode()) |
| 5648 | return LD; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5649 | |
| Michael Liao | facace8 | 2012-10-19 17:15:18 +0000 | [diff] [blame] | 5650 | // Check for a build vector from mostly shuffle plus few inserting. |
| 5651 | SDValue Sh = buildFromShuffleMostly(Op, DAG); |
| 5652 | if (Sh.getNode()) |
| 5653 | return Sh; |
| 5654 | |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5655 | // For SSE 4.1, use insertps to put the high elements into the low element. |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 5656 | if (getSubtarget()->hasSSE41()) { |
| Chris Lattner | 24faf61 | 2010-08-28 17:59:08 +0000 | [diff] [blame] | 5657 | SDValue Result; |
| 5658 | if (Op.getOperand(0).getOpcode() != ISD::UNDEF) |
| 5659 | Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0)); |
| 5660 | else |
| 5661 | Result = DAG.getUNDEF(VT); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5662 | |
| Chris Lattner | 24faf61 | 2010-08-28 17:59:08 +0000 | [diff] [blame] | 5663 | for (unsigned i = 1; i < NumElems; ++i) { |
| 5664 | if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue; |
| 5665 | Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result, |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5666 | Op.getOperand(i), DAG.getIntPtrConstant(i)); |
| Chris Lattner | 24faf61 | 2010-08-28 17:59:08 +0000 | [diff] [blame] | 5667 | } |
| 5668 | return Result; |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5669 | } |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5670 | |
| Chris Lattner | 6e80e44 | 2010-08-28 17:15:43 +0000 | [diff] [blame] | 5671 | // Otherwise, expand into a number of unpckl*, start by extending each of |
| 5672 | // our (non-undef) elements to the full vector width with the element in the |
| 5673 | // bottom slot of the vector (which generates no code for SSE). |
| 5674 | for (unsigned i = 0; i < NumElems; ++i) { |
| 5675 | if (Op.getOperand(i).getOpcode() != ISD::UNDEF) |
| 5676 | V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i)); |
| 5677 | else |
| 5678 | V[i] = DAG.getUNDEF(VT); |
| 5679 | } |
| 5680 | |
| 5681 | // Next, we iteratively mix elements, e.g. for v4f32: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5682 | // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0> |
| 5683 | // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1> |
| 5684 | // Step 2: unpcklps X, Y ==> <3, 2, 1, 0> |
| Chris Lattner | 6e80e44 | 2010-08-28 17:15:43 +0000 | [diff] [blame] | 5685 | unsigned EltStride = NumElems >> 1; |
| 5686 | while (EltStride != 0) { |
| Chris Lattner | 3ddcc43 | 2010-08-28 17:28:30 +0000 | [diff] [blame] | 5687 | for (unsigned i = 0; i < EltStride; ++i) { |
| 5688 | // If V[i+EltStride] is undef and this is the first round of mixing, |
| 5689 | // then it is safe to just drop this shuffle: V[i] is already in the |
| 5690 | // right place, the one element (since it's the first round) being |
| 5691 | // inserted as undef can be dropped. This isn't safe for successive |
| 5692 | // rounds because they will permute elements within both vectors. |
| 5693 | if (V[i+EltStride].getOpcode() == ISD::UNDEF && |
| 5694 | EltStride == NumElems/2) |
| 5695 | continue; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 5696 | |
| Chris Lattner | 6e80e44 | 2010-08-28 17:15:43 +0000 | [diff] [blame] | 5697 | V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]); |
| Chris Lattner | 3ddcc43 | 2010-08-28 17:28:30 +0000 | [diff] [blame] | 5698 | } |
| Chris Lattner | 6e80e44 | 2010-08-28 17:15:43 +0000 | [diff] [blame] | 5699 | EltStride >>= 1; |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5700 | } |
| 5701 | return V[0]; |
| 5702 | } |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 5703 | return SDValue(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 5704 | } |
| 5705 | |
| Bruno Cardoso Lopes | 8af2451 | 2011-08-01 21:54:02 +0000 | [diff] [blame] | 5706 | // LowerAVXCONCAT_VECTORS - 256-bit AVX can use the vinsertf128 instruction |
| 5707 | // to create 256-bit vectors from two other 128-bit ones. |
| 5708 | static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5709 | SDLoc dl(Op); |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 5710 | MVT ResVT = Op.getValueType().getSimpleVT(); |
| Bruno Cardoso Lopes | 8af2451 | 2011-08-01 21:54:02 +0000 | [diff] [blame] | 5711 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 5712 | assert(ResVT.is256BitVector() && "Value type must be 256-bit wide"); |
| Bruno Cardoso Lopes | 8af2451 | 2011-08-01 21:54:02 +0000 | [diff] [blame] | 5713 | |
| 5714 | SDValue V1 = Op.getOperand(0); |
| 5715 | SDValue V2 = Op.getOperand(1); |
| 5716 | unsigned NumElems = ResVT.getVectorNumElements(); |
| 5717 | |
| Craig Topper | 4c7972d | 2012-04-22 18:15:59 +0000 | [diff] [blame] | 5718 | return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl); |
| Bruno Cardoso Lopes | 8af2451 | 2011-08-01 21:54:02 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 5721 | static SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) { |
| Bruno Cardoso Lopes | 8af2451 | 2011-08-01 21:54:02 +0000 | [diff] [blame] | 5722 | assert(Op.getNumOperands() == 2); |
| Bruno Cardoso Lopes | 8af2451 | 2011-08-01 21:54:02 +0000 | [diff] [blame] | 5723 | |
| 5724 | // 256-bit AVX can use the vinsertf128 instruction to create 256-bit vectors |
| 5725 | // from two other 128-bit ones. |
| 5726 | return LowerAVXCONCAT_VECTORS(Op, DAG); |
| 5727 | } |
| 5728 | |
| Nadav Rotem | e80aa7c | 2012-04-09 08:33:21 +0000 | [diff] [blame] | 5729 | // Try to lower a shuffle node into a simple blend instruction. |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 5730 | static SDValue |
| 5731 | LowerVECTOR_SHUFFLEtoBlend(ShuffleVectorSDNode *SVOp, |
| 5732 | const X86Subtarget *Subtarget, SelectionDAG &DAG) { |
| Nadav Rotem | e80aa7c | 2012-04-09 08:33:21 +0000 | [diff] [blame] | 5733 | SDValue V1 = SVOp->getOperand(0); |
| 5734 | SDValue V2 = SVOp->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5735 | SDLoc dl(SVOp); |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 5736 | MVT VT = SVOp->getValueType(0).getSimpleVT(); |
| 5737 | MVT EltVT = VT.getVectorElementType(); |
| Craig Topper | 1842ba0 | 2012-04-23 06:38:28 +0000 | [diff] [blame] | 5738 | unsigned NumElems = VT.getVectorNumElements(); |
| Nadav Rotem | e80aa7c | 2012-04-09 08:33:21 +0000 | [diff] [blame] | 5739 | |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5740 | if (!Subtarget->hasSSE41() || EltVT == MVT::i8) |
| 5741 | return SDValue(); |
| 5742 | if (!Subtarget->hasInt256() && VT == MVT::v16i16) |
| Nadav Rotem | e80aa7c | 2012-04-09 08:33:21 +0000 | [diff] [blame] | 5743 | return SDValue(); |
| 5744 | |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5745 | // Check the mask for BLEND and build the value. |
| 5746 | unsigned MaskValue = 0; |
| 5747 | // There are 2 lanes if (NumElems > 8), and 1 lane otherwise. |
| Craig Topper | 9b33ef7 | 2013-01-21 06:57:59 +0000 | [diff] [blame] | 5748 | unsigned NumLanes = (NumElems-1)/8 + 1; |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5749 | unsigned NumElemsInLane = NumElems / NumLanes; |
| Nadav Rotem | e611378 | 2012-04-11 06:40:27 +0000 | [diff] [blame] | 5750 | |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5751 | // Blend for v16i16 should be symetric for the both lanes. |
| 5752 | for (unsigned i = 0; i < NumElemsInLane; ++i) { |
| Nadav Rotem | e611378 | 2012-04-11 06:40:27 +0000 | [diff] [blame] | 5753 | |
| Craig Topper | 9b33ef7 | 2013-01-21 06:57:59 +0000 | [diff] [blame] | 5754 | int SndLaneEltIdx = (NumLanes == 2) ? |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5755 | SVOp->getMaskElt(i + NumElemsInLane) : -1; |
| Nadav Rotem | e80aa7c | 2012-04-09 08:33:21 +0000 | [diff] [blame] | 5756 | int EltIdx = SVOp->getMaskElt(i); |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5757 | |
| Craig Topper | 04f74a1 | 2013-01-21 07:25:16 +0000 | [diff] [blame] | 5758 | if ((EltIdx < 0 || EltIdx == (int)i) && |
| 5759 | (SndLaneEltIdx < 0 || SndLaneEltIdx == (int)(i + NumElemsInLane))) |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5760 | continue; |
| 5761 | |
| Craig Topper | 9b33ef7 | 2013-01-21 06:57:59 +0000 | [diff] [blame] | 5762 | if (((unsigned)EltIdx == (i + NumElems)) && |
| Craig Topper | 04f74a1 | 2013-01-21 07:25:16 +0000 | [diff] [blame] | 5763 | (SndLaneEltIdx < 0 || |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5764 | (unsigned)SndLaneEltIdx == i + NumElems + NumElemsInLane)) |
| 5765 | MaskValue |= (1<<i); |
| Craig Topper | 9b33ef7 | 2013-01-21 06:57:59 +0000 | [diff] [blame] | 5766 | else |
| Craig Topper | 1842ba0 | 2012-04-23 06:38:28 +0000 | [diff] [blame] | 5767 | return SDValue(); |
| Nadav Rotem | e80aa7c | 2012-04-09 08:33:21 +0000 | [diff] [blame] | 5768 | } |
| 5769 | |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5770 | // Convert i32 vectors to floating point if it is not AVX2. |
| 5771 | // AVX2 introduced VPBLENDD instruction for 128 and 256-bit vectors. |
| Craig Topper | bbf9d3e | 2013-01-21 07:19:54 +0000 | [diff] [blame] | 5772 | MVT BlendVT = VT; |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5773 | if (EltVT == MVT::i64 || (EltVT == MVT::i32 && !Subtarget->hasInt256())) { |
| Craig Topper | bbf9d3e | 2013-01-21 07:19:54 +0000 | [diff] [blame] | 5774 | BlendVT = MVT::getVectorVT(MVT::getFloatingPointVT(EltVT.getSizeInBits()), |
| 5775 | NumElems); |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 5776 | V1 = DAG.getNode(ISD::BITCAST, dl, VT, V1); |
| 5777 | V2 = DAG.getNode(ISD::BITCAST, dl, VT, V2); |
| 5778 | } |
| Craig Topper | 9b33ef7 | 2013-01-21 06:57:59 +0000 | [diff] [blame] | 5779 | |
| Craig Topper | bbf9d3e | 2013-01-21 07:19:54 +0000 | [diff] [blame] | 5780 | SDValue Ret = DAG.getNode(X86ISD::BLENDI, dl, BlendVT, V1, V2, |
| 5781 | DAG.getConstant(MaskValue, MVT::i32)); |
| Nadav Rotem | e611378 | 2012-04-11 06:40:27 +0000 | [diff] [blame] | 5782 | return DAG.getNode(ISD::BITCAST, dl, VT, Ret); |
| Nadav Rotem | e80aa7c | 2012-04-09 08:33:21 +0000 | [diff] [blame] | 5783 | } |
| 5784 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5785 | // v8i16 shuffles - Prefer shuffles in the following order: |
| 5786 | // 1. [all] pshuflw, pshufhw, optional move |
| 5787 | // 2. [ssse3] 1 x pshufb |
| 5788 | // 3. [ssse3] 2 x pshufb + 1 x por |
| 5789 | // 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw) |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 5790 | static SDValue |
| 5791 | LowerVECTOR_SHUFFLEv8i16(SDValue Op, const X86Subtarget *Subtarget, |
| 5792 | SelectionDAG &DAG) { |
| Bruno Cardoso Lopes | bf8154a | 2010-08-21 01:32:18 +0000 | [diff] [blame] | 5793 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5794 | SDValue V1 = SVOp->getOperand(0); |
| 5795 | SDValue V2 = SVOp->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 5796 | SDLoc dl(SVOp); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5797 | SmallVector<int, 8> MaskVals; |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5798 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5799 | // Determine if more than 1 of the words in each of the low and high quadwords |
| 5800 | // of the result come from the same quadword of one of the two inputs. Undef |
| 5801 | // mask values count as coming from any quadword, for better codegen. |
| Benjamin Kramer | 003fad9 | 2011-10-15 13:28:31 +0000 | [diff] [blame] | 5802 | unsigned LoQuad[] = { 0, 0, 0, 0 }; |
| 5803 | unsigned HiQuad[] = { 0, 0, 0, 0 }; |
| Benjamin Kramer | 699ddcb | 2012-02-06 12:06:18 +0000 | [diff] [blame] | 5804 | std::bitset<4> InputQuads; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5805 | for (unsigned i = 0; i < 8; ++i) { |
| Benjamin Kramer | 003fad9 | 2011-10-15 13:28:31 +0000 | [diff] [blame] | 5806 | unsigned *Quad = i < 4 ? LoQuad : HiQuad; |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5807 | int EltIdx = SVOp->getMaskElt(i); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5808 | MaskVals.push_back(EltIdx); |
| 5809 | if (EltIdx < 0) { |
| 5810 | ++Quad[0]; |
| 5811 | ++Quad[1]; |
| 5812 | ++Quad[2]; |
| 5813 | ++Quad[3]; |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5814 | continue; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5815 | } |
| 5816 | ++Quad[EltIdx / 4]; |
| 5817 | InputQuads.set(EltIdx / 4); |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5818 | } |
| Bill Wendling | e85dc49 | 2008-08-21 22:35:37 +0000 | [diff] [blame] | 5819 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5820 | int BestLoQuad = -1; |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5821 | unsigned MaxQuad = 1; |
| 5822 | for (unsigned i = 0; i < 4; ++i) { |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5823 | if (LoQuad[i] > MaxQuad) { |
| 5824 | BestLoQuad = i; |
| 5825 | MaxQuad = LoQuad[i]; |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5826 | } |
| Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 5827 | } |
| 5828 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5829 | int BestHiQuad = -1; |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5830 | MaxQuad = 1; |
| 5831 | for (unsigned i = 0; i < 4; ++i) { |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5832 | if (HiQuad[i] > MaxQuad) { |
| 5833 | BestHiQuad = i; |
| 5834 | MaxQuad = HiQuad[i]; |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5835 | } |
| 5836 | } |
| 5837 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5838 | // For SSSE3, If all 8 words of the result come from only 1 quadword of each |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5839 | // of the two input vectors, shuffle them into one input vector so only a |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5840 | // single pshufb instruction is necessary. If There are more than 2 input |
| 5841 | // quads, disable the next transformation since it does not help SSSE3. |
| 5842 | bool V1Used = InputQuads[0] || InputQuads[1]; |
| 5843 | bool V2Used = InputQuads[2] || InputQuads[3]; |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 5844 | if (Subtarget->hasSSSE3()) { |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5845 | if (InputQuads.count() == 2 && V1Used && V2Used) { |
| Benjamin Kramer | 699ddcb | 2012-02-06 12:06:18 +0000 | [diff] [blame] | 5846 | BestLoQuad = InputQuads[0] ? 0 : 1; |
| 5847 | BestHiQuad = InputQuads[2] ? 2 : 3; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5848 | } |
| 5849 | if (InputQuads.count() > 2) { |
| 5850 | BestLoQuad = -1; |
| 5851 | BestHiQuad = -1; |
| 5852 | } |
| 5853 | } |
| Bill Wendling | e85dc49 | 2008-08-21 22:35:37 +0000 | [diff] [blame] | 5854 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5855 | // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update |
| 5856 | // the shuffle mask. If a quad is scored as -1, that means that it contains |
| 5857 | // words from all 4 input quadwords. |
| 5858 | SDValue NewV; |
| 5859 | if (BestLoQuad >= 0 || BestHiQuad >= 0) { |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 5860 | int MaskV[] = { |
| 5861 | BestLoQuad < 0 ? 0 : BestLoQuad, |
| 5862 | BestHiQuad < 0 ? 1 : BestHiQuad |
| 5863 | }; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5864 | NewV = DAG.getVectorShuffle(MVT::v2i64, dl, |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5865 | DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V1), |
| 5866 | DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, V2), &MaskV[0]); |
| 5867 | NewV = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, NewV); |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5868 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5869 | // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the |
| 5870 | // source words for the shuffle, to aid later transformations. |
| 5871 | bool AllWordsInNewV = true; |
| Mon P Wang | 37b9a19 | 2009-03-11 06:35:11 +0000 | [diff] [blame] | 5872 | bool InOrder[2] = { true, true }; |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5873 | for (unsigned i = 0; i != 8; ++i) { |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5874 | int idx = MaskVals[i]; |
| Mon P Wang | 37b9a19 | 2009-03-11 06:35:11 +0000 | [diff] [blame] | 5875 | if (idx != (int)i) |
| 5876 | InOrder[i/4] = false; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5877 | if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad) |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5878 | continue; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5879 | AllWordsInNewV = false; |
| 5880 | break; |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5881 | } |
| Bill Wendling | e85dc49 | 2008-08-21 22:35:37 +0000 | [diff] [blame] | 5882 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5883 | bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV; |
| 5884 | if (AllWordsInNewV) { |
| 5885 | for (int i = 0; i != 8; ++i) { |
| 5886 | int idx = MaskVals[i]; |
| 5887 | if (idx < 0) |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5888 | continue; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5889 | idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5890 | if ((idx != i) && idx < 4) |
| 5891 | pshufhw = false; |
| 5892 | if ((idx != i) && idx > 3) |
| 5893 | pshuflw = false; |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5894 | } |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5895 | V1 = NewV; |
| 5896 | V2Used = false; |
| 5897 | BestLoQuad = 0; |
| 5898 | BestHiQuad = 1; |
| Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 5899 | } |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5900 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5901 | // If we've eliminated the use of V2, and the new mask is a pshuflw or |
| 5902 | // pshufhw, that's as cheap as it gets. Return the new shuffle. |
| Mon P Wang | 37b9a19 | 2009-03-11 06:35:11 +0000 | [diff] [blame] | 5903 | if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) { |
| Bruno Cardoso Lopes | 3efc077 | 2010-08-23 20:41:02 +0000 | [diff] [blame] | 5904 | unsigned Opc = pshufhw ? X86ISD::PSHUFHW : X86ISD::PSHUFLW; |
| 5905 | unsigned TargetMask = 0; |
| 5906 | NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5907 | DAG.getUNDEF(MVT::v8i16), &MaskVals[0]); |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 5908 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode()); |
| 5909 | TargetMask = pshufhw ? getShufflePSHUFHWImmediate(SVOp): |
| 5910 | getShufflePSHUFLWImmediate(SVOp); |
| Bruno Cardoso Lopes | 3efc077 | 2010-08-23 20:41:02 +0000 | [diff] [blame] | 5911 | V1 = NewV.getOperand(0); |
| Bruno Cardoso Lopes | 8878e21 | 2010-08-24 01:16:15 +0000 | [diff] [blame] | 5912 | return getTargetShuffleNode(Opc, dl, MVT::v8i16, V1, TargetMask, DAG); |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5913 | } |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 5914 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5915 | |
| Benjamin Kramer | 11f2bf7 | 2013-01-26 11:44:21 +0000 | [diff] [blame] | 5916 | // Promote splats to a larger type which usually leads to more efficient code. |
| 5917 | // FIXME: Is this true if pshufb is available? |
| 5918 | if (SVOp->isSplat()) |
| 5919 | return PromoteSplat(SVOp, DAG); |
| 5920 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5921 | // If we have SSSE3, and all words of the result are from 1 input vector, |
| 5922 | // case 2 is generated, otherwise case 3 is generated. If no SSSE3 |
| 5923 | // is present, fall back to case 4. |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 5924 | if (Subtarget->hasSSSE3()) { |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5925 | SmallVector<SDValue,16> pshufbMask; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5926 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5927 | // If we have elements from both input vectors, set the high bit of the |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5928 | // shuffle mask element to zero out elements that come from V2 in the V1 |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5929 | // mask, and elements that come from V1 in the V2 mask, so that the two |
| 5930 | // results can be OR'd together. |
| 5931 | bool TwoInputs = V1Used && V2Used; |
| 5932 | for (unsigned i = 0; i != 8; ++i) { |
| 5933 | int EltIdx = MaskVals[i] * 2; |
| Craig Topper | be97ae9 | 2012-05-18 07:07:36 +0000 | [diff] [blame] | 5934 | int Idx0 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx; |
| 5935 | int Idx1 = (TwoInputs && (EltIdx >= 16)) ? 0x80 : EltIdx+1; |
| Craig Topper | e6d8fa7 | 2013-01-18 07:27:20 +0000 | [diff] [blame] | 5936 | pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8)); |
| Craig Topper | be97ae9 | 2012-05-18 07:07:36 +0000 | [diff] [blame] | 5937 | pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8)); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5938 | } |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5939 | V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V1); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5940 | V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1, |
| Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5941 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5942 | MVT::v16i8, &pshufbMask[0], 16)); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5943 | if (!TwoInputs) |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5944 | return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5945 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5946 | // Calculate the shuffle mask for the second input, shuffle it, and |
| 5947 | // OR it with the first shuffled input. |
| 5948 | pshufbMask.clear(); |
| 5949 | for (unsigned i = 0; i != 8; ++i) { |
| 5950 | int EltIdx = MaskVals[i] * 2; |
| Craig Topper | be97ae9 | 2012-05-18 07:07:36 +0000 | [diff] [blame] | 5951 | int Idx0 = (EltIdx < 16) ? 0x80 : EltIdx - 16; |
| 5952 | int Idx1 = (EltIdx < 16) ? 0x80 : EltIdx - 15; |
| 5953 | pshufbMask.push_back(DAG.getConstant(Idx0, MVT::i8)); |
| 5954 | pshufbMask.push_back(DAG.getConstant(Idx1, MVT::i8)); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5955 | } |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5956 | V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, V2); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5957 | V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2, |
| Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 5958 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5959 | MVT::v16i8, &pshufbMask[0], 16)); |
| 5960 | V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2); |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 5961 | return DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5962 | } |
| 5963 | |
| 5964 | // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order, |
| 5965 | // and update MaskVals with new element order. |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 5966 | std::bitset<8> InOrder; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5967 | if (BestLoQuad >= 0) { |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 5968 | int MaskV[] = { -1, -1, -1, -1, 4, 5, 6, 7 }; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5969 | for (int i = 0; i != 4; ++i) { |
| 5970 | int idx = MaskVals[i]; |
| 5971 | if (idx < 0) { |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5972 | InOrder.set(i); |
| 5973 | } else if ((idx / 4) == BestLoQuad) { |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 5974 | MaskV[i] = idx & 3; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5975 | InOrder.set(i); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5976 | } |
| 5977 | } |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 5978 | NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16), |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 5979 | &MaskV[0]); |
| Bruno Cardoso Lopes | 8878e21 | 2010-08-24 01:16:15 +0000 | [diff] [blame] | 5980 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 5981 | if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) { |
| 5982 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode()); |
| Bruno Cardoso Lopes | 8878e21 | 2010-08-24 01:16:15 +0000 | [diff] [blame] | 5983 | NewV = getTargetShuffleNode(X86ISD::PSHUFLW, dl, MVT::v8i16, |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 5984 | NewV.getOperand(0), |
| 5985 | getShufflePSHUFLWImmediate(SVOp), DAG); |
| 5986 | } |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5987 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 5988 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5989 | // If BestHi >= 0, generate a pshufhw to put the high elements in order, |
| 5990 | // and update MaskVals with the new element order. |
| 5991 | if (BestHiQuad >= 0) { |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 5992 | int MaskV[] = { 0, 1, 2, 3, -1, -1, -1, -1 }; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5993 | for (unsigned i = 4; i != 8; ++i) { |
| 5994 | int idx = MaskVals[i]; |
| 5995 | if (idx < 0) { |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5996 | InOrder.set(i); |
| 5997 | } else if ((idx / 4) == BestHiQuad) { |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 5998 | MaskV[i] = (idx & 3) + 4; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 5999 | InOrder.set(i); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6000 | } |
| 6001 | } |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6002 | NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16), |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6003 | &MaskV[0]); |
| Bruno Cardoso Lopes | 8878e21 | 2010-08-24 01:16:15 +0000 | [diff] [blame] | 6004 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6005 | if (NewV.getOpcode() == ISD::VECTOR_SHUFFLE && Subtarget->hasSSSE3()) { |
| 6006 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(NewV.getNode()); |
| Bruno Cardoso Lopes | 8878e21 | 2010-08-24 01:16:15 +0000 | [diff] [blame] | 6007 | NewV = getTargetShuffleNode(X86ISD::PSHUFHW, dl, MVT::v8i16, |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6008 | NewV.getOperand(0), |
| 6009 | getShufflePSHUFHWImmediate(SVOp), DAG); |
| 6010 | } |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6011 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6012 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6013 | // In case BestHi & BestLo were both -1, which means each quadword has a word |
| 6014 | // from each of the four input quadwords, calculate the InOrder bitvector now |
| 6015 | // before falling through to the insert/extract cleanup. |
| 6016 | if (BestLoQuad == -1 && BestHiQuad == -1) { |
| 6017 | NewV = V1; |
| 6018 | for (int i = 0; i != 8; ++i) |
| 6019 | if (MaskVals[i] < 0 || MaskVals[i] == i) |
| 6020 | InOrder.set(i); |
| 6021 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6022 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6023 | // The other elements are put in the right place using pextrw and pinsrw. |
| 6024 | for (unsigned i = 0; i != 8; ++i) { |
| 6025 | if (InOrder[i]) |
| 6026 | continue; |
| 6027 | int EltIdx = MaskVals[i]; |
| 6028 | if (EltIdx < 0) |
| 6029 | continue; |
| Craig Topper | 6643d9c | 2012-05-04 06:18:33 +0000 | [diff] [blame] | 6030 | SDValue ExtOp = (EltIdx < 8) ? |
| 6031 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1, |
| 6032 | DAG.getIntPtrConstant(EltIdx)) : |
| 6033 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2, |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6034 | DAG.getIntPtrConstant(EltIdx - 8)); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6035 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp, |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6036 | DAG.getIntPtrConstant(i)); |
| 6037 | } |
| 6038 | return NewV; |
| 6039 | } |
| 6040 | |
| 6041 | // v16i8 shuffles - Prefer shuffles in the following order: |
| 6042 | // 1. [ssse3] 1 x pshufb |
| 6043 | // 2. [ssse3] 2 x pshufb + 1 x por |
| 6044 | // 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw |
| 6045 | static |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6046 | SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6047 | SelectionDAG &DAG, |
| 6048 | const X86TargetLowering &TLI) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6049 | SDValue V1 = SVOp->getOperand(0); |
| 6050 | SDValue V2 = SVOp->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6051 | SDLoc dl(SVOp); |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 6052 | ArrayRef<int> MaskVals = SVOp->getMask(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6053 | |
| Benjamin Kramer | 11f2bf7 | 2013-01-26 11:44:21 +0000 | [diff] [blame] | 6054 | // Promote splats to a larger type which usually leads to more efficient code. |
| 6055 | // FIXME: Is this true if pshufb is available? |
| 6056 | if (SVOp->isSplat()) |
| 6057 | return PromoteSplat(SVOp, DAG); |
| 6058 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6059 | // If we have SSSE3, case 1 is generated when all result bytes come from |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6060 | // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6061 | // present, fall back to case 3. |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6062 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6063 | // If SSSE3, use 1 pshufb instruction per vector with elements in the result. |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 6064 | if (TLI.getSubtarget()->hasSSSE3()) { |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6065 | SmallVector<SDValue,16> pshufbMask; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6066 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6067 | // If all result elements are from one input vector, then only translate |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6068 | // undef mask values to 0x80 (zero out result) in the pshufb mask. |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6069 | // |
| 6070 | // Otherwise, we have elements from both input vectors, and must zero out |
| 6071 | // elements that come from V2 in the first mask, and V1 in the second mask |
| 6072 | // so that we can OR them together. |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6073 | for (unsigned i = 0; i != 16; ++i) { |
| 6074 | int EltIdx = MaskVals[i]; |
| Craig Topper | b82b5ab | 2012-05-18 06:42:06 +0000 | [diff] [blame] | 6075 | if (EltIdx < 0 || EltIdx >= 16) |
| 6076 | EltIdx = 0x80; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6077 | pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8)); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6078 | } |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6079 | V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1, |
| Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 6080 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6081 | MVT::v16i8, &pshufbMask[0], 16)); |
| Michael Liao | 265bcb1 | 2012-08-31 20:12:31 +0000 | [diff] [blame] | 6082 | |
| 6083 | // As PSHUFB will zero elements with negative indices, it's safe to ignore |
| 6084 | // the 2nd operand if it's undefined or zero. |
| 6085 | if (V2.getOpcode() == ISD::UNDEF || |
| 6086 | ISD::isBuildVectorAllZeros(V2.getNode())) |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6087 | return V1; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6088 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6089 | // Calculate the shuffle mask for the second input, shuffle it, and |
| 6090 | // OR it with the first shuffled input. |
| 6091 | pshufbMask.clear(); |
| 6092 | for (unsigned i = 0; i != 16; ++i) { |
| 6093 | int EltIdx = MaskVals[i]; |
| Craig Topper | b82b5ab | 2012-05-18 06:42:06 +0000 | [diff] [blame] | 6094 | EltIdx = (EltIdx < 16) ? 0x80 : EltIdx - 16; |
| Craig Topper | 85b9e56 | 2012-05-22 06:09:38 +0000 | [diff] [blame] | 6095 | pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8)); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6096 | } |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6097 | V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2, |
| Evan Cheng | a87008d | 2009-02-25 22:49:59 +0000 | [diff] [blame] | 6098 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6099 | MVT::v16i8, &pshufbMask[0], 16)); |
| 6100 | return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6101 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6102 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6103 | // No SSSE3 - Calculate in place words and then fix all out of place words |
| 6104 | // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from |
| 6105 | // the 16 different words that comprise the two doublequadword input vectors. |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6106 | V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V1); |
| 6107 | V2 = DAG.getNode(ISD::BITCAST, dl, MVT::v8i16, V2); |
| Craig Topper | b82b5ab | 2012-05-18 06:42:06 +0000 | [diff] [blame] | 6108 | SDValue NewV = V1; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6109 | for (int i = 0; i != 8; ++i) { |
| 6110 | int Elt0 = MaskVals[i*2]; |
| 6111 | int Elt1 = MaskVals[i*2+1]; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6112 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6113 | // This word of the result is all undef, skip it. |
| 6114 | if (Elt0 < 0 && Elt1 < 0) |
| 6115 | continue; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6116 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6117 | // This word of the result is already in the correct place, skip it. |
| Craig Topper | b82b5ab | 2012-05-18 06:42:06 +0000 | [diff] [blame] | 6118 | if ((Elt0 == i*2) && (Elt1 == i*2+1)) |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6119 | continue; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6120 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6121 | SDValue Elt0Src = Elt0 < 16 ? V1 : V2; |
| 6122 | SDValue Elt1Src = Elt1 < 16 ? V1 : V2; |
| 6123 | SDValue InsElt; |
| Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 6124 | |
| 6125 | // If Elt0 and Elt1 are defined, are consecutive, and can be load |
| 6126 | // using a single extract together, load it and store it. |
| 6127 | if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6128 | InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src, |
| Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 6129 | DAG.getIntPtrConstant(Elt1 / 2)); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6130 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt, |
| Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 6131 | DAG.getIntPtrConstant(i)); |
| 6132 | continue; |
| 6133 | } |
| 6134 | |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6135 | // If Elt1 is defined, extract it from the appropriate source. If the |
| Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 6136 | // source byte is not also odd, shift the extracted word left 8 bits |
| 6137 | // otherwise clear the bottom 8 bits if we need to do an or. |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6138 | if (Elt1 >= 0) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6139 | InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src, |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6140 | DAG.getIntPtrConstant(Elt1 / 2)); |
| 6141 | if ((Elt1 & 1) == 0) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6142 | InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt, |
| Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 6143 | DAG.getConstant(8, |
| 6144 | TLI.getShiftAmountTy(InsElt.getValueType()))); |
| Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 6145 | else if (Elt0 >= 0) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6146 | InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt, |
| 6147 | DAG.getConstant(0xFF00, MVT::i16)); |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6148 | } |
| 6149 | // If Elt0 is defined, extract it from the appropriate source. If the |
| 6150 | // source byte is not also even, shift the extracted word right 8 bits. If |
| 6151 | // Elt1 was also defined, OR the extracted values together before |
| 6152 | // inserting them in the result. |
| 6153 | if (Elt0 >= 0) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6154 | SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6155 | Elt0Src, DAG.getIntPtrConstant(Elt0 / 2)); |
| 6156 | if ((Elt0 & 1) != 0) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6157 | InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0, |
| Owen Anderson | 95771af | 2011-02-25 21:41:48 +0000 | [diff] [blame] | 6158 | DAG.getConstant(8, |
| 6159 | TLI.getShiftAmountTy(InsElt0.getValueType()))); |
| Mon P Wang | 6b3ef69 | 2009-03-11 18:47:57 +0000 | [diff] [blame] | 6160 | else if (Elt1 >= 0) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6161 | InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0, |
| 6162 | DAG.getConstant(0x00FF, MVT::i16)); |
| 6163 | InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0) |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6164 | : InsElt0; |
| 6165 | } |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6166 | NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt, |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 6167 | DAG.getIntPtrConstant(i)); |
| 6168 | } |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6169 | return DAG.getNode(ISD::BITCAST, dl, MVT::v16i8, NewV); |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 6170 | } |
| 6171 | |
| Elena Demikhovsky | 4178946 | 2012-09-06 12:42:01 +0000 | [diff] [blame] | 6172 | // v32i8 shuffles - Translate to VPSHUFB if possible. |
| 6173 | static |
| 6174 | SDValue LowerVECTOR_SHUFFLEv32i8(ShuffleVectorSDNode *SVOp, |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 6175 | const X86Subtarget *Subtarget, |
| 6176 | SelectionDAG &DAG) { |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6177 | MVT VT = SVOp->getValueType(0).getSimpleVT(); |
| Elena Demikhovsky | 4178946 | 2012-09-06 12:42:01 +0000 | [diff] [blame] | 6178 | SDValue V1 = SVOp->getOperand(0); |
| 6179 | SDValue V2 = SVOp->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6180 | SDLoc dl(SVOp); |
| Elena Demikhovsky | 8100d24 | 2012-09-10 12:13:11 +0000 | [diff] [blame] | 6181 | SmallVector<int, 32> MaskVals(SVOp->getMask().begin(), SVOp->getMask().end()); |
| Elena Demikhovsky | 4178946 | 2012-09-06 12:42:01 +0000 | [diff] [blame] | 6182 | |
| 6183 | bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; |
| Elena Demikhovsky | 8100d24 | 2012-09-10 12:13:11 +0000 | [diff] [blame] | 6184 | bool V1IsAllZero = ISD::isBuildVectorAllZeros(V1.getNode()); |
| 6185 | bool V2IsAllZero = ISD::isBuildVectorAllZeros(V2.getNode()); |
| Elena Demikhovsky | 4178946 | 2012-09-06 12:42:01 +0000 | [diff] [blame] | 6186 | |
| Michael Liao | 471b917 | 2012-10-03 23:43:52 +0000 | [diff] [blame] | 6187 | // VPSHUFB may be generated if |
| Elena Demikhovsky | 8100d24 | 2012-09-10 12:13:11 +0000 | [diff] [blame] | 6188 | // (1) one of input vector is undefined or zeroinitializer. |
| 6189 | // The mask value 0x80 puts 0 in the corresponding slot of the vector. |
| 6190 | // And (2) the mask indexes don't cross the 128-bit lane. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6191 | if (VT != MVT::v32i8 || !Subtarget->hasInt256() || |
| Elena Demikhovsky | 8100d24 | 2012-09-10 12:13:11 +0000 | [diff] [blame] | 6192 | (!V2IsUndef && !V2IsAllZero && !V1IsAllZero)) |
| Elena Demikhovsky | 4178946 | 2012-09-06 12:42:01 +0000 | [diff] [blame] | 6193 | return SDValue(); |
| 6194 | |
| Elena Demikhovsky | 8100d24 | 2012-09-10 12:13:11 +0000 | [diff] [blame] | 6195 | if (V1IsAllZero && !V2IsAllZero) { |
| 6196 | CommuteVectorShuffleMask(MaskVals, 32); |
| 6197 | V1 = V2; |
| 6198 | } |
| 6199 | SmallVector<SDValue, 32> pshufbMask; |
| Elena Demikhovsky | 4178946 | 2012-09-06 12:42:01 +0000 | [diff] [blame] | 6200 | for (unsigned i = 0; i != 32; i++) { |
| 6201 | int EltIdx = MaskVals[i]; |
| 6202 | if (EltIdx < 0 || EltIdx >= 32) |
| 6203 | EltIdx = 0x80; |
| 6204 | else { |
| 6205 | if ((EltIdx >= 16 && i < 16) || (EltIdx < 16 && i >= 16)) |
| 6206 | // Cross lane is not allowed. |
| 6207 | return SDValue(); |
| 6208 | EltIdx &= 0xf; |
| 6209 | } |
| 6210 | pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8)); |
| 6211 | } |
| 6212 | return DAG.getNode(X86ISD::PSHUFB, dl, MVT::v32i8, V1, |
| 6213 | DAG.getNode(ISD::BUILD_VECTOR, dl, |
| 6214 | MVT::v32i8, &pshufbMask[0], 32)); |
| 6215 | } |
| 6216 | |
| Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 6217 | /// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide |
| Bruno Cardoso Lopes | 0a7dd4f | 2010-09-08 18:12:31 +0000 | [diff] [blame] | 6218 | /// ones, or rewriting v4i32 / v4f32 as 2 wide ones if possible. This can be |
| Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 6219 | /// done when every pair / quad of shuffle mask elements point to elements in |
| 6220 | /// the right sequence. e.g. |
| Bruno Cardoso Lopes | 0a7dd4f | 2010-09-08 18:12:31 +0000 | [diff] [blame] | 6221 | /// vector_shuffle X, Y, <2, 3, | 10, 11, | 0, 1, | 14, 15> |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 6222 | static |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6223 | SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp, |
| Craig Topper | 3b2aba0 | 2013-01-20 00:43:42 +0000 | [diff] [blame] | 6224 | SelectionDAG &DAG) { |
| Craig Topper | 11ac1f8 | 2012-05-04 04:08:44 +0000 | [diff] [blame] | 6225 | MVT VT = SVOp->getValueType(0).getSimpleVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6226 | SDLoc dl(SVOp); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6227 | unsigned NumElems = VT.getVectorNumElements(); |
| Craig Topper | 11ac1f8 | 2012-05-04 04:08:44 +0000 | [diff] [blame] | 6228 | MVT NewVT; |
| 6229 | unsigned Scale; |
| 6230 | switch (VT.SimpleTy) { |
| Craig Topper | abb94d0 | 2012-02-05 03:43:23 +0000 | [diff] [blame] | 6231 | default: llvm_unreachable("Unexpected!"); |
| Craig Topper | f3640d7 | 2012-05-04 04:44:49 +0000 | [diff] [blame] | 6232 | case MVT::v4f32: NewVT = MVT::v2f64; Scale = 2; break; |
| 6233 | case MVT::v4i32: NewVT = MVT::v2i64; Scale = 2; break; |
| 6234 | case MVT::v8i16: NewVT = MVT::v4i32; Scale = 2; break; |
| 6235 | case MVT::v16i8: NewVT = MVT::v4i32; Scale = 4; break; |
| 6236 | case MVT::v16i16: NewVT = MVT::v8i32; Scale = 2; break; |
| 6237 | case MVT::v32i8: NewVT = MVT::v8i32; Scale = 4; break; |
| Evan Cheng | 7a831ce | 2007-12-15 03:00:47 +0000 | [diff] [blame] | 6238 | } |
| 6239 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6240 | SmallVector<int, 8> MaskVec; |
| Craig Topper | 11ac1f8 | 2012-05-04 04:08:44 +0000 | [diff] [blame] | 6241 | for (unsigned i = 0; i != NumElems; i += Scale) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6242 | int StartIdx = -1; |
| Craig Topper | 11ac1f8 | 2012-05-04 04:08:44 +0000 | [diff] [blame] | 6243 | for (unsigned j = 0; j != Scale; ++j) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6244 | int EltIdx = SVOp->getMaskElt(i+j); |
| 6245 | if (EltIdx < 0) |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 6246 | continue; |
| Craig Topper | 11ac1f8 | 2012-05-04 04:08:44 +0000 | [diff] [blame] | 6247 | if (StartIdx < 0) |
| 6248 | StartIdx = (EltIdx / Scale); |
| 6249 | if (EltIdx != (int)(StartIdx*Scale + j)) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6250 | return SDValue(); |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 6251 | } |
| Craig Topper | 11ac1f8 | 2012-05-04 04:08:44 +0000 | [diff] [blame] | 6252 | MaskVec.push_back(StartIdx); |
| Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 6253 | } |
| 6254 | |
| Craig Topper | 11ac1f8 | 2012-05-04 04:08:44 +0000 | [diff] [blame] | 6255 | SDValue V1 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(0)); |
| 6256 | SDValue V2 = DAG.getNode(ISD::BITCAST, dl, NewVT, SVOp->getOperand(1)); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6257 | return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]); |
| Evan Cheng | 8a86c3f | 2007-12-07 08:07:39 +0000 | [diff] [blame] | 6258 | } |
| 6259 | |
| Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 6260 | /// getVZextMovL - Return a zero-extending vector move low node. |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 6261 | /// |
| Craig Topper | f84b750 | 2013-01-20 00:50:58 +0000 | [diff] [blame] | 6262 | static SDValue getVZextMovL(MVT VT, EVT OpVT, |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6263 | SDValue SrcOp, SelectionDAG &DAG, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6264 | const X86Subtarget *Subtarget, SDLoc dl) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6265 | if (VT == MVT::v2f64 || VT == MVT::v4f32) { |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 6266 | LoadSDNode *LD = NULL; |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6267 | if (!isScalarLoadToVector(SrcOp.getNode(), &LD)) |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 6268 | LD = dyn_cast<LoadSDNode>(SrcOp); |
| 6269 | if (!LD) { |
| 6270 | // movssrr and movsdrr do not clear top bits. Try to use movd, movq |
| 6271 | // instead. |
| Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 6272 | MVT ExtVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32; |
| Duncan Sands | cdfad36 | 2010-11-03 12:17:33 +0000 | [diff] [blame] | 6273 | if ((ExtVT != MVT::i64 || Subtarget->is64Bit()) && |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 6274 | SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR && |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6275 | SrcOp.getOperand(0).getOpcode() == ISD::BITCAST && |
| Owen Anderson | 766b5ef | 2009-08-11 21:59:30 +0000 | [diff] [blame] | 6276 | SrcOp.getOperand(0).getOperand(0).getValueType() == ExtVT) { |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 6277 | // PR2108 |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 6278 | OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32; |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6279 | return DAG.getNode(ISD::BITCAST, dl, VT, |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 6280 | DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT, |
| 6281 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
| 6282 | OpVT, |
| Gabor Greif | 327ef03 | 2008-08-28 23:19:51 +0000 | [diff] [blame] | 6283 | SrcOp.getOperand(0) |
| 6284 | .getOperand(0)))); |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 6285 | } |
| 6286 | } |
| 6287 | } |
| 6288 | |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6289 | return DAG.getNode(ISD::BITCAST, dl, VT, |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 6290 | DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT, |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6291 | DAG.getNode(ISD::BITCAST, dl, |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 6292 | OpVT, SrcOp))); |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 6293 | } |
| 6294 | |
| Bruno Cardoso Lopes | d088834 | 2011-07-22 00:14:56 +0000 | [diff] [blame] | 6295 | /// LowerVECTOR_SHUFFLE_256 - Handle all 256-bit wide vectors shuffles |
| 6296 | /// which could not be matched by any known target speficic shuffle |
| 6297 | static SDValue |
| 6298 | LowerVECTOR_SHUFFLE_256(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) { |
| Elena Demikhovsky | 1596373 | 2012-06-26 08:04:10 +0000 | [diff] [blame] | 6299 | |
| 6300 | SDValue NewOp = Compact8x32ShuffleNode(SVOp, DAG); |
| 6301 | if (NewOp.getNode()) |
| 6302 | return NewOp; |
| 6303 | |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6304 | MVT VT = SVOp->getValueType(0).getSimpleVT(); |
| Bruno Cardoso Lopes | 3b86598 | 2011-08-16 18:21:54 +0000 | [diff] [blame] | 6305 | |
| Craig Topper | 8f35c13 | 2012-01-20 09:29:03 +0000 | [diff] [blame] | 6306 | unsigned NumElems = VT.getVectorNumElements(); |
| 6307 | unsigned NumLaneElems = NumElems / 2; |
| 6308 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6309 | SDLoc dl(SVOp); |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6310 | MVT EltVT = VT.getVectorElementType(); |
| 6311 | MVT NVT = MVT::getVectorVT(EltVT, NumLaneElems); |
| Craig Topper | 8ae97ba | 2012-05-21 06:40:16 +0000 | [diff] [blame] | 6312 | SDValue Output[2]; |
| Craig Topper | 8f35c13 | 2012-01-20 09:29:03 +0000 | [diff] [blame] | 6313 | |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6314 | SmallVector<int, 16> Mask; |
| Craig Topper | 8f35c13 | 2012-01-20 09:29:03 +0000 | [diff] [blame] | 6315 | for (unsigned l = 0; l < 2; ++l) { |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6316 | // Build a shuffle mask for the output, discovering on the fly which |
| 6317 | // input vectors to use as shuffle operands (recorded in InputUsed). |
| 6318 | // If building a suitable shuffle vector proves too hard, then bail |
| Craig Topper | 8ae97ba | 2012-05-21 06:40:16 +0000 | [diff] [blame] | 6319 | // out with UseBuildVector set. |
| 6320 | bool UseBuildVector = false; |
| Benjamin Kramer | 9e5512a | 2012-04-06 13:33:52 +0000 | [diff] [blame] | 6321 | int InputUsed[2] = { -1, -1 }; // Not yet discovered. |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6322 | unsigned LaneStart = l * NumLaneElems; |
| 6323 | for (unsigned i = 0; i != NumLaneElems; ++i) { |
| 6324 | // The mask element. This indexes into the input. |
| 6325 | int Idx = SVOp->getMaskElt(i+LaneStart); |
| 6326 | if (Idx < 0) { |
| 6327 | // the mask element does not index into any input vector. |
| 6328 | Mask.push_back(-1); |
| 6329 | continue; |
| 6330 | } |
| Craig Topper | 8f35c13 | 2012-01-20 09:29:03 +0000 | [diff] [blame] | 6331 | |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6332 | // The input vector this mask element indexes into. |
| 6333 | int Input = Idx / NumLaneElems; |
| Craig Topper | 8f35c13 | 2012-01-20 09:29:03 +0000 | [diff] [blame] | 6334 | |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6335 | // Turn the index into an offset from the start of the input vector. |
| 6336 | Idx -= Input * NumLaneElems; |
| 6337 | |
| 6338 | // Find or create a shuffle vector operand to hold this input. |
| 6339 | unsigned OpNo; |
| 6340 | for (OpNo = 0; OpNo < array_lengthof(InputUsed); ++OpNo) { |
| 6341 | if (InputUsed[OpNo] == Input) |
| 6342 | // This input vector is already an operand. |
| 6343 | break; |
| 6344 | if (InputUsed[OpNo] < 0) { |
| 6345 | // Create a new operand for this input vector. |
| 6346 | InputUsed[OpNo] = Input; |
| 6347 | break; |
| 6348 | } |
| 6349 | } |
| 6350 | |
| 6351 | if (OpNo >= array_lengthof(InputUsed)) { |
| Craig Topper | 8ae97ba | 2012-05-21 06:40:16 +0000 | [diff] [blame] | 6352 | // More than two input vectors used! Give up on trying to create a |
| 6353 | // shuffle vector. Insert all elements into a BUILD_VECTOR instead. |
| 6354 | UseBuildVector = true; |
| 6355 | break; |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6356 | } |
| 6357 | |
| 6358 | // Add the mask index for the new shuffle vector. |
| 6359 | Mask.push_back(Idx + OpNo * NumLaneElems); |
| 6360 | } |
| 6361 | |
| Craig Topper | 8ae97ba | 2012-05-21 06:40:16 +0000 | [diff] [blame] | 6362 | if (UseBuildVector) { |
| 6363 | SmallVector<SDValue, 16> SVOps; |
| 6364 | for (unsigned i = 0; i != NumLaneElems; ++i) { |
| 6365 | // The mask element. This indexes into the input. |
| 6366 | int Idx = SVOp->getMaskElt(i+LaneStart); |
| 6367 | if (Idx < 0) { |
| 6368 | SVOps.push_back(DAG.getUNDEF(EltVT)); |
| 6369 | continue; |
| 6370 | } |
| 6371 | |
| 6372 | // The input vector this mask element indexes into. |
| 6373 | int Input = Idx / NumElems; |
| 6374 | |
| 6375 | // Turn the index into an offset from the start of the input vector. |
| 6376 | Idx -= Input * NumElems; |
| 6377 | |
| 6378 | // Extract the vector element by hand. |
| 6379 | SVOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, |
| 6380 | SVOp->getOperand(Input), |
| 6381 | DAG.getIntPtrConstant(Idx))); |
| 6382 | } |
| 6383 | |
| 6384 | // Construct the output using a BUILD_VECTOR. |
| 6385 | Output[l] = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, &SVOps[0], |
| 6386 | SVOps.size()); |
| 6387 | } else if (InputUsed[0] < 0) { |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6388 | // No input vectors were used! The result is undefined. |
| Craig Topper | 8ae97ba | 2012-05-21 06:40:16 +0000 | [diff] [blame] | 6389 | Output[l] = DAG.getUNDEF(NVT); |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6390 | } else { |
| 6391 | SDValue Op0 = Extract128BitVector(SVOp->getOperand(InputUsed[0] / 2), |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 6392 | (InputUsed[0] % 2) * NumLaneElems, |
| 6393 | DAG, dl); |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6394 | // If only one input was used, use an undefined vector for the other. |
| 6395 | SDValue Op1 = (InputUsed[1] < 0) ? DAG.getUNDEF(NVT) : |
| 6396 | Extract128BitVector(SVOp->getOperand(InputUsed[1] / 2), |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 6397 | (InputUsed[1] % 2) * NumLaneElems, DAG, dl); |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6398 | // At least one input vector was used. Create a new shuffle vector. |
| Craig Topper | 8ae97ba | 2012-05-21 06:40:16 +0000 | [diff] [blame] | 6399 | Output[l] = DAG.getVectorShuffle(NVT, dl, Op0, Op1, &Mask[0]); |
| Craig Topper | 9a2b6e1 | 2012-04-06 07:45:23 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | Mask.clear(); |
| 6403 | } |
| Craig Topper | 8f35c13 | 2012-01-20 09:29:03 +0000 | [diff] [blame] | 6404 | |
| 6405 | // Concatenate the result back |
| Craig Topper | 8ae97ba | 2012-05-21 06:40:16 +0000 | [diff] [blame] | 6406 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Output[0], Output[1]); |
| Bruno Cardoso Lopes | d088834 | 2011-07-22 00:14:56 +0000 | [diff] [blame] | 6407 | } |
| 6408 | |
| Bruno Cardoso Lopes | 589b897 | 2011-07-22 00:14:53 +0000 | [diff] [blame] | 6409 | /// LowerVECTOR_SHUFFLE_128v4 - Handle all 128-bit wide vectors with |
| 6410 | /// 4 elements, and match them with several different shuffle types. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6411 | static SDValue |
| Bruno Cardoso Lopes | 589b897 | 2011-07-22 00:14:53 +0000 | [diff] [blame] | 6412 | LowerVECTOR_SHUFFLE_128v4(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6413 | SDValue V1 = SVOp->getOperand(0); |
| 6414 | SDValue V2 = SVOp->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6415 | SDLoc dl(SVOp); |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6416 | MVT VT = SVOp->getValueType(0).getSimpleVT(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6417 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 6418 | assert(VT.is128BitVector() && "Unsupported vector size"); |
| Bruno Cardoso Lopes | 589b897 | 2011-07-22 00:14:53 +0000 | [diff] [blame] | 6419 | |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 6420 | std::pair<int, int> Locs[4]; |
| 6421 | int Mask1[] = { -1, -1, -1, -1 }; |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 6422 | SmallVector<int, 8> PermMask(SVOp->getMask().begin(), SVOp->getMask().end()); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6423 | |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6424 | unsigned NumHi = 0; |
| 6425 | unsigned NumLo = 0; |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6426 | for (unsigned i = 0; i != 4; ++i) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6427 | int Idx = PermMask[i]; |
| 6428 | if (Idx < 0) { |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6429 | Locs[i] = std::make_pair(-1, -1); |
| 6430 | } else { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6431 | assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!"); |
| 6432 | if (Idx < 4) { |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6433 | Locs[i] = std::make_pair(0, NumLo); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6434 | Mask1[NumLo] = Idx; |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6435 | NumLo++; |
| 6436 | } else { |
| 6437 | Locs[i] = std::make_pair(1, NumHi); |
| 6438 | if (2+NumHi < 4) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6439 | Mask1[2+NumHi] = Idx; |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6440 | NumHi++; |
| 6441 | } |
| 6442 | } |
| 6443 | } |
| Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 6444 | |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6445 | if (NumLo <= 2 && NumHi <= 2) { |
| Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 6446 | // If no more than two elements come from either vector. This can be |
| 6447 | // implemented with two shuffles. First shuffle gather the elements. |
| 6448 | // The second shuffle, which takes the first shuffle as both of its |
| 6449 | // vector operands, put the elements into the right order. |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6450 | V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); |
| Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 6451 | |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 6452 | int Mask2[] = { -1, -1, -1, -1 }; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6453 | |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 6454 | for (unsigned i = 0; i != 4; ++i) |
| 6455 | if (Locs[i].first != -1) { |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6456 | unsigned Idx = (i < 2) ? 0 : 4; |
| 6457 | Idx += Locs[i].first * 2 + Locs[i].second; |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6458 | Mask2[i] = Idx; |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6459 | } |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6460 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6461 | return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 6462 | } |
| 6463 | |
| 6464 | if (NumLo == 3 || NumHi == 3) { |
| Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 6465 | // Otherwise, we must have three elements from one vector, call it X, and |
| 6466 | // one element from the other, call it Y. First, use a shufps to build an |
| 6467 | // intermediate vector with the one element from Y and the element from X |
| 6468 | // that will be in the same half in the final destination (the indexes don't |
| 6469 | // matter). Then, use a shufps to build the final vector, taking the half |
| 6470 | // containing the element from Y from the intermediate, and the other half |
| 6471 | // from X. |
| 6472 | if (NumHi == 3) { |
| 6473 | // Normalize it so the 3 elements come from V1. |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 6474 | CommuteVectorShuffleMask(PermMask, 4); |
| Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 6475 | std::swap(V1, V2); |
| 6476 | } |
| 6477 | |
| 6478 | // Find the element from V2. |
| 6479 | unsigned HiIndex; |
| 6480 | for (HiIndex = 0; HiIndex < 3; ++HiIndex) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6481 | int Val = PermMask[HiIndex]; |
| 6482 | if (Val < 0) |
| Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 6483 | continue; |
| Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 6484 | if (Val >= 4) |
| 6485 | break; |
| 6486 | } |
| 6487 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6488 | Mask1[0] = PermMask[HiIndex]; |
| 6489 | Mask1[1] = -1; |
| 6490 | Mask1[2] = PermMask[HiIndex^1]; |
| 6491 | Mask1[3] = -1; |
| 6492 | V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); |
| Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 6493 | |
| 6494 | if (HiIndex >= 2) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6495 | Mask1[0] = PermMask[0]; |
| 6496 | Mask1[1] = PermMask[1]; |
| 6497 | Mask1[2] = HiIndex & 1 ? 6 : 4; |
| 6498 | Mask1[3] = HiIndex & 1 ? 4 : 6; |
| 6499 | return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]); |
| Evan Cheng | 5e6ebaf | 2008-07-23 00:22:17 +0000 | [diff] [blame] | 6500 | } |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 6501 | |
| 6502 | Mask1[0] = HiIndex & 1 ? 2 : 0; |
| 6503 | Mask1[1] = HiIndex & 1 ? 0 : 2; |
| 6504 | Mask1[2] = PermMask[2]; |
| 6505 | Mask1[3] = PermMask[3]; |
| 6506 | if (Mask1[2] >= 0) |
| 6507 | Mask1[2] += 4; |
| 6508 | if (Mask1[3] >= 0) |
| 6509 | Mask1[3] += 4; |
| 6510 | return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]); |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6511 | } |
| 6512 | |
| 6513 | // Break it into (shuffle shuffle_hi, shuffle_lo). |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 6514 | int LoMask[] = { -1, -1, -1, -1 }; |
| 6515 | int HiMask[] = { -1, -1, -1, -1 }; |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6516 | |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 6517 | int *MaskPtr = LoMask; |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6518 | unsigned MaskIdx = 0; |
| 6519 | unsigned LoIdx = 0; |
| 6520 | unsigned HiIdx = 2; |
| 6521 | for (unsigned i = 0; i != 4; ++i) { |
| 6522 | if (i == 2) { |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 6523 | MaskPtr = HiMask; |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6524 | MaskIdx = 1; |
| 6525 | LoIdx = 0; |
| 6526 | HiIdx = 2; |
| 6527 | } |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6528 | int Idx = PermMask[i]; |
| 6529 | if (Idx < 0) { |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6530 | Locs[i] = std::make_pair(-1, -1); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6531 | } else if (Idx < 4) { |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6532 | Locs[i] = std::make_pair(MaskIdx, LoIdx); |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 6533 | MaskPtr[LoIdx] = Idx; |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6534 | LoIdx++; |
| 6535 | } else { |
| 6536 | Locs[i] = std::make_pair(MaskIdx, HiIdx); |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 6537 | MaskPtr[HiIdx] = Idx; |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6538 | HiIdx++; |
| 6539 | } |
| 6540 | } |
| 6541 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6542 | SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]); |
| 6543 | SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]); |
| Benjamin Kramer | 9c68354 | 2012-01-30 15:16:21 +0000 | [diff] [blame] | 6544 | int MaskOps[] = { -1, -1, -1, -1 }; |
| 6545 | for (unsigned i = 0; i != 4; ++i) |
| 6546 | if (Locs[i].first != -1) |
| 6547 | MaskOps[i] = Locs[i].first * 4 + Locs[i].second; |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6548 | return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]); |
| Evan Cheng | ace3c17 | 2008-07-22 21:13:36 +0000 | [diff] [blame] | 6549 | } |
| 6550 | |
| Bruno Cardoso Lopes | 2a44606 | 2010-09-03 20:20:02 +0000 | [diff] [blame] | 6551 | static bool MayFoldVectorLoad(SDValue V) { |
| Jakub Staszak | a24262a | 2012-10-30 00:01:57 +0000 | [diff] [blame] | 6552 | while (V.hasOneUse() && V.getOpcode() == ISD::BITCAST) |
| Bruno Cardoso Lopes | 2a44606 | 2010-09-03 20:20:02 +0000 | [diff] [blame] | 6553 | V = V.getOperand(0); |
| Jakub Staszak | a24262a | 2012-10-30 00:01:57 +0000 | [diff] [blame] | 6554 | |
| Bruno Cardoso Lopes | 2a44606 | 2010-09-03 20:20:02 +0000 | [diff] [blame] | 6555 | if (V.hasOneUse() && V.getOpcode() == ISD::SCALAR_TO_VECTOR) |
| 6556 | V = V.getOperand(0); |
| Evan Cheng | 7bc389b | 2011-11-08 00:31:58 +0000 | [diff] [blame] | 6557 | if (V.hasOneUse() && V.getOpcode() == ISD::BUILD_VECTOR && |
| 6558 | V.getNumOperands() == 2 && V.getOperand(1).getOpcode() == ISD::UNDEF) |
| 6559 | // BUILD_VECTOR (load), undef |
| 6560 | V = V.getOperand(0); |
| Jakub Staszak | a24262a | 2012-10-30 00:01:57 +0000 | [diff] [blame] | 6561 | |
| 6562 | return MayFoldLoad(V); |
| Bruno Cardoso Lopes | 2a44606 | 2010-09-03 20:20:02 +0000 | [diff] [blame] | 6563 | } |
| 6564 | |
| Bruno Cardoso Lopes | f2db5b4 | 2010-08-31 21:15:21 +0000 | [diff] [blame] | 6565 | static |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6566 | SDValue getMOVDDup(SDValue &Op, SDLoc &dl, SDValue V1, SelectionDAG &DAG) { |
| Evan Cheng | 835580f | 2010-10-07 20:50:20 +0000 | [diff] [blame] | 6567 | EVT VT = Op.getValueType(); |
| 6568 | |
| 6569 | // Canonizalize to v2f64. |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6570 | V1 = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, V1); |
| 6571 | return DAG.getNode(ISD::BITCAST, dl, VT, |
| Evan Cheng | 835580f | 2010-10-07 20:50:20 +0000 | [diff] [blame] | 6572 | getTargetShuffleNode(X86ISD::MOVDDUP, dl, MVT::v2f64, |
| 6573 | V1, DAG)); |
| 6574 | } |
| 6575 | |
| 6576 | static |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6577 | SDValue getMOVLowToHigh(SDValue &Op, SDLoc &dl, SelectionDAG &DAG, |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6578 | bool HasSSE2) { |
| Bruno Cardoso Lopes | f2db5b4 | 2010-08-31 21:15:21 +0000 | [diff] [blame] | 6579 | SDValue V1 = Op.getOperand(0); |
| 6580 | SDValue V2 = Op.getOperand(1); |
| 6581 | EVT VT = Op.getValueType(); |
| 6582 | |
| 6583 | assert(VT != MVT::v2i64 && "unsupported shuffle type"); |
| 6584 | |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6585 | if (HasSSE2 && VT == MVT::v2f64) |
| Bruno Cardoso Lopes | f2db5b4 | 2010-08-31 21:15:21 +0000 | [diff] [blame] | 6586 | return getTargetShuffleNode(X86ISD::MOVLHPD, dl, VT, V1, V2, DAG); |
| 6587 | |
| Evan Cheng | 0899f5c | 2011-08-31 02:05:24 +0000 | [diff] [blame] | 6588 | // v4f32 or v4i32: canonizalized to v4f32 (which is legal for SSE1) |
| 6589 | return DAG.getNode(ISD::BITCAST, dl, VT, |
| 6590 | getTargetShuffleNode(X86ISD::MOVLHPS, dl, MVT::v4f32, |
| 6591 | DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V1), |
| 6592 | DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, V2), DAG)); |
| Bruno Cardoso Lopes | f2db5b4 | 2010-08-31 21:15:21 +0000 | [diff] [blame] | 6593 | } |
| 6594 | |
| Bruno Cardoso Lopes | 7ff30bb | 2010-08-31 21:38:49 +0000 | [diff] [blame] | 6595 | static |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6596 | SDValue getMOVHighToLow(SDValue &Op, SDLoc &dl, SelectionDAG &DAG) { |
| Bruno Cardoso Lopes | 7ff30bb | 2010-08-31 21:38:49 +0000 | [diff] [blame] | 6597 | SDValue V1 = Op.getOperand(0); |
| 6598 | SDValue V2 = Op.getOperand(1); |
| 6599 | EVT VT = Op.getValueType(); |
| 6600 | |
| 6601 | assert((VT == MVT::v4i32 || VT == MVT::v4f32) && |
| 6602 | "unsupported shuffle type"); |
| 6603 | |
| 6604 | if (V2.getOpcode() == ISD::UNDEF) |
| 6605 | V2 = V1; |
| 6606 | |
| 6607 | // v4i32 or v4f32 |
| 6608 | return getTargetShuffleNode(X86ISD::MOVHLPS, dl, VT, V1, V2, DAG); |
| 6609 | } |
| 6610 | |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6611 | static |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6612 | SDValue getMOVLP(SDValue &Op, SDLoc &dl, SelectionDAG &DAG, bool HasSSE2) { |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6613 | SDValue V1 = Op.getOperand(0); |
| 6614 | SDValue V2 = Op.getOperand(1); |
| 6615 | EVT VT = Op.getValueType(); |
| 6616 | unsigned NumElems = VT.getVectorNumElements(); |
| 6617 | |
| 6618 | // Use MOVLPS and MOVLPD in case V1 or V2 are loads. During isel, the second |
| 6619 | // operand of these instructions is only memory, so check if there's a |
| 6620 | // potencial load folding here, otherwise use SHUFPS or MOVSD to match the |
| 6621 | // same masks. |
| 6622 | bool CanFoldLoad = false; |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6623 | |
| Bruno Cardoso Lopes | d00bfe1 | 2010-09-02 02:35:51 +0000 | [diff] [blame] | 6624 | // Trivial case, when V2 comes from a load. |
| Bruno Cardoso Lopes | 2a44606 | 2010-09-03 20:20:02 +0000 | [diff] [blame] | 6625 | if (MayFoldVectorLoad(V2)) |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6626 | CanFoldLoad = true; |
| 6627 | |
| 6628 | // When V1 is a load, it can be folded later into a store in isel, example: |
| 6629 | // (store (v4f32 (X86Movlps (load addr:$src1), VR128:$src2)), addr:$src1) |
| 6630 | // turns into: |
| 6631 | // (MOVLPSmr addr:$src1, VR128:$src2) |
| 6632 | // So, recognize this potential and also use MOVLPS or MOVLPD |
| Evan Cheng | 7bc389b | 2011-11-08 00:31:58 +0000 | [diff] [blame] | 6633 | else if (MayFoldVectorLoad(V1) && MayFoldIntoStore(Op)) |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6634 | CanFoldLoad = true; |
| 6635 | |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 6636 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6637 | if (CanFoldLoad) { |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6638 | if (HasSSE2 && NumElems == 2) |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6639 | return getTargetShuffleNode(X86ISD::MOVLPD, dl, VT, V1, V2, DAG); |
| 6640 | |
| 6641 | if (NumElems == 4) |
| Benjamin Kramer | d9b0b02 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 6642 | // If we don't care about the second element, proceed to use movss. |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 6643 | if (SVOp->getMaskElt(1) != -1) |
| 6644 | return getTargetShuffleNode(X86ISD::MOVLPS, dl, VT, V1, V2, DAG); |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6645 | } |
| 6646 | |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6647 | // movl and movlp will both match v2i64, but v2i64 is never matched by |
| 6648 | // movl earlier because we make it strict to avoid messing with the movlp load |
| 6649 | // folding logic (see the code above getMOVLP call). Match it here then, |
| 6650 | // this is horrible, but will stay like this until we move all shuffle |
| 6651 | // matching to x86 specific nodes. Note that for the 1st condition all |
| 6652 | // types are matched with movsd. |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6653 | if (HasSSE2) { |
| Bruno Cardoso Lopes | 5ca0d14 | 2011-09-14 02:36:14 +0000 | [diff] [blame] | 6654 | // FIXME: isMOVLMask should be checked and matched before getMOVLP, |
| 6655 | // as to remove this logic from here, as much as possible |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 6656 | if (NumElems == 2 || !isMOVLMask(SVOp->getMask(), VT)) |
| Bruno Cardoso Lopes | 57d6a5e | 2011-08-31 03:04:20 +0000 | [diff] [blame] | 6657 | return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG); |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6658 | return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG); |
| Bruno Cardoso Lopes | 57d6a5e | 2011-08-31 03:04:20 +0000 | [diff] [blame] | 6659 | } |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6660 | |
| 6661 | assert(VT != MVT::v4i32 && "unsupported shuffle type"); |
| 6662 | |
| 6663 | // Invert the operand order and use SHUFPS to match it. |
| Craig Topper | b3982da | 2011-12-31 23:50:21 +0000 | [diff] [blame] | 6664 | return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V2, V1, |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 6665 | getShuffleSHUFImmediate(SVOp), DAG); |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6668 | // Reduce a vector shuffle to zext. |
| 6669 | SDValue |
| Craig Topper | 00a312c | 2013-01-19 23:14:09 +0000 | [diff] [blame] | 6670 | X86TargetLowering::LowerVectorIntExtend(SDValue Op, SelectionDAG &DAG) const { |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6671 | // PMOVZX is only available from SSE41. |
| 6672 | if (!Subtarget->hasSSE41()) |
| 6673 | return SDValue(); |
| 6674 | |
| 6675 | EVT VT = Op.getValueType(); |
| 6676 | |
| 6677 | // Only AVX2 support 256-bit vector integer extending. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6678 | if (!Subtarget->hasInt256() && VT.is256BitVector()) |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6679 | return SDValue(); |
| 6680 | |
| 6681 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6682 | SDLoc DL(Op); |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6683 | SDValue V1 = Op.getOperand(0); |
| 6684 | SDValue V2 = Op.getOperand(1); |
| 6685 | unsigned NumElems = VT.getVectorNumElements(); |
| 6686 | |
| 6687 | // Extending is an unary operation and the element type of the source vector |
| 6688 | // won't be equal to or larger than i64. |
| 6689 | if (V2.getOpcode() != ISD::UNDEF || !VT.isInteger() || |
| 6690 | VT.getVectorElementType() == MVT::i64) |
| 6691 | return SDValue(); |
| 6692 | |
| 6693 | // Find the expansion ratio, e.g. expanding from i8 to i32 has a ratio of 4. |
| 6694 | unsigned Shift = 1; // Start from 2, i.e. 1 << 1. |
| Duncan Sands | 3473905 | 2012-10-29 11:29:53 +0000 | [diff] [blame] | 6695 | while ((1U << Shift) < NumElems) { |
| 6696 | if (SVOp->getMaskElt(1U << Shift) == 1) |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6697 | break; |
| 6698 | Shift += 1; |
| 6699 | // The maximal ratio is 8, i.e. from i8 to i64. |
| 6700 | if (Shift > 3) |
| 6701 | return SDValue(); |
| 6702 | } |
| 6703 | |
| 6704 | // Check the shuffle mask. |
| 6705 | unsigned Mask = (1U << Shift) - 1; |
| 6706 | for (unsigned i = 0; i != NumElems; ++i) { |
| 6707 | int EltIdx = SVOp->getMaskElt(i); |
| 6708 | if ((i & Mask) != 0 && EltIdx != -1) |
| 6709 | return SDValue(); |
| Matt Beaumont-Gay | a999de0 | 2012-10-23 19:46:36 +0000 | [diff] [blame] | 6710 | if ((i & Mask) == 0 && (unsigned)EltIdx != (i >> Shift)) |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6711 | return SDValue(); |
| 6712 | } |
| 6713 | |
| Elena Demikhovsky | 60b3e18 | 2013-02-14 08:20:26 +0000 | [diff] [blame] | 6714 | LLVMContext *Context = DAG.getContext(); |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6715 | unsigned NBits = VT.getVectorElementType().getSizeInBits() << Shift; |
| Elena Demikhovsky | 60b3e18 | 2013-02-14 08:20:26 +0000 | [diff] [blame] | 6716 | EVT NeVT = EVT::getIntegerVT(*Context, NBits); |
| 6717 | EVT NVT = EVT::getVectorVT(*Context, NeVT, NumElems >> Shift); |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6718 | |
| 6719 | if (!isTypeLegal(NVT)) |
| 6720 | return SDValue(); |
| 6721 | |
| 6722 | // Simplify the operand as it's prepared to be fed into shuffle. |
| 6723 | unsigned SignificantBits = NVT.getSizeInBits() >> Shift; |
| 6724 | if (V1.getOpcode() == ISD::BITCAST && |
| 6725 | V1.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR && |
| 6726 | V1.getOperand(0).getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 6727 | V1.getOperand(0) |
| 6728 | .getOperand(0).getValueType().getSizeInBits() == SignificantBits) { |
| 6729 | // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x) |
| 6730 | SDValue V = V1.getOperand(0).getOperand(0).getOperand(0); |
| Michael Liao | 0787274 | 2012-10-23 21:40:15 +0000 | [diff] [blame] | 6731 | ConstantSDNode *CIdx = |
| 6732 | dyn_cast<ConstantSDNode>(V1.getOperand(0).getOperand(0).getOperand(1)); |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6733 | // If it's foldable, i.e. normal load with single use, we will let code |
| 6734 | // selection to fold it. Otherwise, we will short the conversion sequence. |
| Michael Liao | 0787274 | 2012-10-23 21:40:15 +0000 | [diff] [blame] | 6735 | if (CIdx && CIdx->getZExtValue() == 0 && |
| Elena Demikhovsky | 60b3e18 | 2013-02-14 08:20:26 +0000 | [diff] [blame] | 6736 | (!ISD::isNormalLoad(V.getNode()) || !V.hasOneUse())) { |
| 6737 | if (V.getValueSizeInBits() > V1.getValueSizeInBits()) { |
| 6738 | // The "ext_vec_elt" node is wider than the result node. |
| 6739 | // In this case we should extract subvector from V. |
| 6740 | // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast (extract_subvector x)). |
| 6741 | unsigned Ratio = V.getValueSizeInBits() / V1.getValueSizeInBits(); |
| 6742 | EVT FullVT = V.getValueType(); |
| Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 6743 | EVT SubVecVT = EVT::getVectorVT(*Context, |
| Elena Demikhovsky | 60b3e18 | 2013-02-14 08:20:26 +0000 | [diff] [blame] | 6744 | FullVT.getVectorElementType(), |
| 6745 | FullVT.getVectorNumElements()/Ratio); |
| Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 6746 | V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVecVT, V, |
| Elena Demikhovsky | 60b3e18 | 2013-02-14 08:20:26 +0000 | [diff] [blame] | 6747 | DAG.getIntPtrConstant(0)); |
| 6748 | } |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6749 | V1 = DAG.getNode(ISD::BITCAST, DL, V1.getValueType(), V); |
| Elena Demikhovsky | 60b3e18 | 2013-02-14 08:20:26 +0000 | [diff] [blame] | 6750 | } |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6751 | } |
| 6752 | |
| 6753 | return DAG.getNode(ISD::BITCAST, DL, VT, |
| 6754 | DAG.getNode(X86ISD::VZEXT, DL, NVT, V1)); |
| 6755 | } |
| 6756 | |
| Nadav Rotem | 154819d | 2012-04-09 07:45:58 +0000 | [diff] [blame] | 6757 | SDValue |
| 6758 | X86TargetLowering::NormalizeVectorShuffle(SDValue Op, SelectionDAG &DAG) const { |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6759 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6760 | MVT VT = Op.getValueType().getSimpleVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6761 | SDLoc dl(Op); |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6762 | SDValue V1 = Op.getOperand(0); |
| 6763 | SDValue V2 = Op.getOperand(1); |
| 6764 | |
| 6765 | if (isZeroShuffle(SVOp)) |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 6766 | return getZeroVector(VT, Subtarget, DAG, dl); |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6767 | |
| Bruno Cardoso Lopes | 1485cc2 | 2010-09-08 17:43:25 +0000 | [diff] [blame] | 6768 | // Handle splat operations |
| 6769 | if (SVOp->isSplat()) { |
| Bruno Cardoso Lopes | 0e6d230 | 2011-08-17 02:29:19 +0000 | [diff] [blame] | 6770 | // Use vbroadcast whenever the splat comes from a foldable load |
| Nadav Rotem | 154819d | 2012-04-09 07:45:58 +0000 | [diff] [blame] | 6771 | SDValue Broadcast = LowerVectorBroadcast(Op, DAG); |
| Nadav Rotem | 9d68b06 | 2012-04-08 12:54:54 +0000 | [diff] [blame] | 6772 | if (Broadcast.getNode()) |
| 6773 | return Broadcast; |
| Bruno Cardoso Lopes | 1485cc2 | 2010-09-08 17:43:25 +0000 | [diff] [blame] | 6774 | } |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6775 | |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6776 | // Check integer expanding shuffles. |
| Craig Topper | 00a312c | 2013-01-19 23:14:09 +0000 | [diff] [blame] | 6777 | SDValue NewOp = LowerVectorIntExtend(Op, DAG); |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 6778 | if (NewOp.getNode()) |
| 6779 | return NewOp; |
| 6780 | |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6781 | // If the shuffle can be profitably rewritten as a narrower shuffle, then |
| 6782 | // do it! |
| Craig Topper | f3640d7 | 2012-05-04 04:44:49 +0000 | [diff] [blame] | 6783 | if (VT == MVT::v8i16 || VT == MVT::v16i8 || |
| 6784 | VT == MVT::v16i16 || VT == MVT::v32i8) { |
| Craig Topper | 3b2aba0 | 2013-01-20 00:43:42 +0000 | [diff] [blame] | 6785 | SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG); |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6786 | if (NewOp.getNode()) |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 6787 | return DAG.getNode(ISD::BITCAST, dl, VT, NewOp); |
| Bruno Cardoso Lopes | 0c4b9ff | 2011-09-15 18:27:36 +0000 | [diff] [blame] | 6788 | } else if ((VT == MVT::v4i32 || |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6789 | (VT == MVT::v4f32 && Subtarget->hasSSE2()))) { |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6790 | // FIXME: Figure out a cleaner way to do this. |
| 6791 | // Try to make use of movq to zero out the top part. |
| 6792 | if (ISD::isBuildVectorAllZeros(V2.getNode())) { |
| Craig Topper | 3b2aba0 | 2013-01-20 00:43:42 +0000 | [diff] [blame] | 6793 | SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG); |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6794 | if (NewOp.getNode()) { |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6795 | MVT NewVT = NewOp.getValueType().getSimpleVT(); |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 6796 | if (isCommutedMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), |
| 6797 | NewVT, true, false)) |
| 6798 | return getVZextMovL(VT, NewVT, NewOp.getOperand(0), |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6799 | DAG, Subtarget, dl); |
| 6800 | } |
| 6801 | } else if (ISD::isBuildVectorAllZeros(V1.getNode())) { |
| Craig Topper | 3b2aba0 | 2013-01-20 00:43:42 +0000 | [diff] [blame] | 6802 | SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG); |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 6803 | if (NewOp.getNode()) { |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6804 | MVT NewVT = NewOp.getValueType().getSimpleVT(); |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 6805 | if (isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)->getMask(), NewVT)) |
| 6806 | return getVZextMovL(VT, NewVT, NewOp.getOperand(1), |
| 6807 | DAG, Subtarget, dl); |
| 6808 | } |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6809 | } |
| 6810 | } |
| 6811 | return SDValue(); |
| 6812 | } |
| 6813 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6814 | SDValue |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 6815 | X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6816 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6817 | SDValue V1 = Op.getOperand(0); |
| 6818 | SDValue V2 = Op.getOperand(1); |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6819 | MVT VT = Op.getValueType().getSimpleVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 6820 | SDLoc dl(Op); |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6821 | unsigned NumElems = VT.getVectorNumElements(); |
| Elena Demikhovsky | 16db710 | 2012-01-12 20:33:10 +0000 | [diff] [blame] | 6822 | bool V1IsUndef = V1.getOpcode() == ISD::UNDEF; |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6823 | bool V2IsUndef = V2.getOpcode() == ISD::UNDEF; |
| Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 6824 | bool V1IsSplat = false; |
| 6825 | bool V2IsSplat = false; |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6826 | bool HasSSE2 = Subtarget->hasSSE2(); |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6827 | bool HasFp256 = Subtarget->hasFp256(); |
| 6828 | bool HasInt256 = Subtarget->hasInt256(); |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 6829 | MachineFunction &MF = DAG.getMachineFunction(); |
| Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 6830 | bool OptForSize = MF.getFunction()->getAttributes(). |
| 6831 | hasAttribute(AttributeSet::FunctionIndex, Attribute::OptimizeForSize); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6832 | |
| Craig Topper | 3426a3e | 2011-11-14 06:46:21 +0000 | [diff] [blame] | 6833 | assert(VT.getSizeInBits() != 64 && "Can't lower MMX shuffles"); |
| Bruno Cardoso Lopes | 58277b1 | 2010-09-07 18:41:45 +0000 | [diff] [blame] | 6834 | |
| Elena Demikhovsky | 16db710 | 2012-01-12 20:33:10 +0000 | [diff] [blame] | 6835 | if (V1IsUndef && V2IsUndef) |
| 6836 | return DAG.getUNDEF(VT); |
| 6837 | |
| 6838 | assert(!V1IsUndef && "Op 1 of shuffle should not be undef"); |
| Craig Topper | 38034c5 | 2011-11-26 22:55:48 +0000 | [diff] [blame] | 6839 | |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6840 | // Vector shuffle lowering takes 3 steps: |
| 6841 | // |
| 6842 | // 1) Normalize the input vectors. Here splats, zeroed vectors, profitable |
| 6843 | // narrowing and commutation of operands should be handled. |
| 6844 | // 2) Matching of shuffles with known shuffle masks to x86 target specific |
| 6845 | // shuffle nodes. |
| 6846 | // 3) Rewriting of unmatched masks into new generic shuffle operations, |
| 6847 | // so the shuffle can be broken into other shuffles and the legalizer can |
| 6848 | // try the lowering again. |
| 6849 | // |
| Craig Topper | 3426a3e | 2011-11-14 06:46:21 +0000 | [diff] [blame] | 6850 | // The general idea is that no vector_shuffle operation should be left to |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6851 | // be matched during isel, all of them must be converted to a target specific |
| 6852 | // node here. |
| Bruno Cardoso Lopes | 0d1340b | 2010-09-07 20:20:27 +0000 | [diff] [blame] | 6853 | |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6854 | // Normalize the input vectors. Here splats, zeroed vectors, profitable |
| 6855 | // narrowing and commutation of operands should be handled. The actual code |
| 6856 | // doesn't include all of those, work in progress... |
| Nadav Rotem | 154819d | 2012-04-09 07:45:58 +0000 | [diff] [blame] | 6857 | SDValue NewOp = NormalizeVectorShuffle(Op, DAG); |
| Bruno Cardoso Lopes | 90462b4 | 2010-09-07 21:03:14 +0000 | [diff] [blame] | 6858 | if (NewOp.getNode()) |
| 6859 | return NewOp; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6860 | |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 6861 | SmallVector<int, 8> M(SVOp->getMask().begin(), SVOp->getMask().end()); |
| 6862 | |
| Bruno Cardoso Lopes | a22c845 | 2010-09-04 00:39:43 +0000 | [diff] [blame] | 6863 | // NOTE: isPSHUFDMask can also match both masks below (unpckl_undef and |
| 6864 | // unpckh_undef). Only use pshufd if speed is more important than size. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6865 | if (OptForSize && isUNPCKL_v_undef_Mask(M, VT, HasInt256)) |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 6866 | return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG); |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6867 | if (OptForSize && isUNPCKH_v_undef_Mask(M, VT, HasInt256)) |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 6868 | return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG); |
| Bruno Cardoso Lopes | 3722f00 | 2010-09-02 05:23:12 +0000 | [diff] [blame] | 6869 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6870 | if (isMOVDDUPMask(M, VT) && Subtarget->hasSSE3() && |
| Jakub Staszak | d3a0563 | 2012-12-06 19:05:46 +0000 | [diff] [blame] | 6871 | V2IsUndef && MayFoldVectorLoad(V1)) |
| Evan Cheng | 835580f | 2010-10-07 20:50:20 +0000 | [diff] [blame] | 6872 | return getMOVDDup(Op, dl, V1, DAG); |
| Bruno Cardoso Lopes | 1485cc2 | 2010-09-08 17:43:25 +0000 | [diff] [blame] | 6873 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6874 | if (isMOVHLPS_v_undef_Mask(M, VT)) |
| Bruno Cardoso Lopes | 1485cc2 | 2010-09-08 17:43:25 +0000 | [diff] [blame] | 6875 | return getMOVHighToLow(Op, dl, DAG); |
| 6876 | |
| 6877 | // Use to match splats |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6878 | if (HasSSE2 && isUNPCKHMask(M, VT, HasInt256) && V2IsUndef && |
| Bruno Cardoso Lopes | 1485cc2 | 2010-09-08 17:43:25 +0000 | [diff] [blame] | 6879 | (VT == MVT::v2f64 || VT == MVT::v2i64)) |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 6880 | return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG); |
| Bruno Cardoso Lopes | 1485cc2 | 2010-09-08 17:43:25 +0000 | [diff] [blame] | 6881 | |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 6882 | if (isPSHUFDMask(M, VT)) { |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 6883 | // The actual implementation will match the mask in the if above and then |
| 6884 | // during isel it can match several different instructions, not only pshufd |
| 6885 | // as its name says, sad but true, emulate the behavior for now... |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6886 | if (isMOVDDUPMask(M, VT) && ((VT == MVT::v4f32 || VT == MVT::v2i64))) |
| 6887 | return getTargetShuffleNode(X86ISD::MOVLHPS, dl, VT, V1, V1, DAG); |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 6888 | |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 6889 | unsigned TargetMask = getShuffleSHUFImmediate(SVOp); |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 6890 | |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6891 | if (HasSSE2 && (VT == MVT::v4f32 || VT == MVT::v4i32)) |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 6892 | return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, TargetMask, DAG); |
| 6893 | |
| Nadav Rotem | e4ccfef | 2012-12-07 19:01:13 +0000 | [diff] [blame] | 6894 | if (HasFp256 && (VT == MVT::v4f32 || VT == MVT::v2f64)) |
| 6895 | return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, TargetMask, |
| 6896 | DAG); |
| 6897 | |
| Craig Topper | b3982da | 2011-12-31 23:50:21 +0000 | [diff] [blame] | 6898 | return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V1, |
| Bruno Cardoso Lopes | 07b7f67 | 2011-08-25 02:58:26 +0000 | [diff] [blame] | 6899 | TargetMask, DAG); |
| Bruno Cardoso Lopes | 7338bbd | 2010-08-25 02:35:37 +0000 | [diff] [blame] | 6900 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6901 | |
| Benjamin Kramer | a0de26c | 2013-05-17 14:48:34 +0000 | [diff] [blame] | 6902 | if (isPALIGNRMask(M, VT, Subtarget)) |
| 6903 | return getTargetShuffleNode(X86ISD::PALIGNR, dl, VT, V1, V2, |
| 6904 | getShufflePALIGNRImmediate(SVOp), |
| 6905 | DAG); |
| 6906 | |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 6907 | // Check if this can be converted into a logical shift. |
| 6908 | bool isLeft = false; |
| 6909 | unsigned ShAmt = 0; |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 6910 | SDValue ShVal; |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6911 | bool isShift = HasSSE2 && isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt); |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 6912 | if (isShift && ShVal.hasOneUse()) { |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6913 | // If the shifted value has multiple uses, it may be cheaper to use |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 6914 | // v_set0 + movlhps or movhlps, etc. |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6915 | MVT EltVT = VT.getVectorElementType(); |
| Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 6916 | ShAmt *= EltVT.getSizeInBits(); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 6917 | return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl); |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 6918 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6919 | |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 6920 | if (isMOVLMask(M, VT)) { |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6921 | if (ISD::isBuildVectorAllZeros(V1.getNode())) |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 6922 | return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl); |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6923 | if (!isMOVLPMask(M, VT)) { |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6924 | if (HasSSE2 && (VT == MVT::v2i64 || VT == MVT::v2f64)) |
| Bruno Cardoso Lopes | 20a07f4 | 2010-08-31 02:26:40 +0000 | [diff] [blame] | 6925 | return getTargetShuffleNode(X86ISD::MOVSD, dl, VT, V1, V2, DAG); |
| 6926 | |
| Bruno Cardoso Lopes | 4783a3e | 2010-09-01 22:59:03 +0000 | [diff] [blame] | 6927 | if (VT == MVT::v4i32 || VT == MVT::v4f32) |
| Bruno Cardoso Lopes | 20a07f4 | 2010-08-31 02:26:40 +0000 | [diff] [blame] | 6928 | return getTargetShuffleNode(X86ISD::MOVSS, dl, VT, V1, V2, DAG); |
| 6929 | } |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 6930 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6931 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6932 | // FIXME: fold these into legal mask. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6933 | if (isMOVLHPSMask(M, VT) && !isUNPCKLMask(M, VT, HasInt256)) |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6934 | return getMOVLowToHigh(Op, dl, DAG, HasSSE2); |
| Bruno Cardoso Lopes | f2db5b4 | 2010-08-31 21:15:21 +0000 | [diff] [blame] | 6935 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6936 | if (isMOVHLPSMask(M, VT)) |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 6937 | return getMOVHighToLow(Op, dl, DAG); |
| Bruno Cardoso Lopes | 7ff30bb | 2010-08-31 21:38:49 +0000 | [diff] [blame] | 6938 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6939 | if (V2IsUndef && isMOVSHDUPMask(M, VT, Subtarget)) |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 6940 | return getTargetShuffleNode(X86ISD::MOVSHDUP, dl, VT, V1, DAG); |
| Bruno Cardoso Lopes | 5023ef2 | 2010-08-31 22:22:11 +0000 | [diff] [blame] | 6941 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6942 | if (V2IsUndef && isMOVSLDUPMask(M, VT, Subtarget)) |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 6943 | return getTargetShuffleNode(X86ISD::MOVSLDUP, dl, VT, V1, DAG); |
| Bruno Cardoso Lopes | 013bb3d | 2010-08-31 22:35:05 +0000 | [diff] [blame] | 6944 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6945 | if (isMOVLPMask(M, VT)) |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 6946 | return getMOVLP(Op, dl, DAG, HasSSE2); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6947 | |
| Craig Topper | dd637ae | 2012-02-19 05:41:45 +0000 | [diff] [blame] | 6948 | if (ShouldXformToMOVHLPS(M, VT) || |
| 6949 | ShouldXformToMOVLP(V1.getNode(), V2.getNode(), M, VT)) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6950 | return CommuteVectorShuffle(SVOp, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6951 | |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 6952 | if (isShift) { |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 6953 | // No better options. Use a vshldq / vsrldq. |
| Craig Topper | 657a99c | 2013-01-19 23:36:09 +0000 | [diff] [blame] | 6954 | MVT EltVT = VT.getVectorElementType(); |
| Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 6955 | ShAmt *= EltVT.getSizeInBits(); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 6956 | return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl); |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 6957 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6958 | |
| Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 6959 | bool Commuted = false; |
| Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 6960 | // FIXME: This should also accept a bitcast of a splat? Be careful, not |
| 6961 | // 1,1,1,1 -> v8i16 though. |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 6962 | V1IsSplat = isSplatVector(V1.getNode()); |
| 6963 | V2IsSplat = isSplatVector(V2.getNode()); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 6964 | |
| Chris Lattner | 8a59448 | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 6965 | // Canonicalize the splat or undef, if present, to be on the RHS. |
| Craig Topper | 39a9e48 | 2012-02-11 06:24:48 +0000 | [diff] [blame] | 6966 | if (!V2IsUndef && V1IsSplat && !V2IsSplat) { |
| 6967 | CommuteVectorShuffleMask(M, NumElems); |
| 6968 | std::swap(V1, V2); |
| Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 6969 | std::swap(V1IsSplat, V2IsSplat); |
| Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 6970 | Commuted = true; |
| Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 6971 | } |
| 6972 | |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 6973 | if (isCommutedMOVLMask(M, VT, V2IsSplat, V2IsUndef)) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6974 | // Shuffling low element of v1 into undef, just return v1. |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 6975 | if (V2IsUndef) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 6976 | return V1; |
| 6977 | // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which |
| 6978 | // the instruction selector will not match, so get a canonical MOVL with |
| 6979 | // swapped operands to undo the commute. |
| 6980 | return getMOVL(DAG, dl, VT, V2, V1); |
| Evan Cheng | d9b8e40 | 2006-10-16 06:36:00 +0000 | [diff] [blame] | 6981 | } |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6982 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6983 | if (isUNPCKLMask(M, VT, HasInt256)) |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 6984 | return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG); |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 6985 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6986 | if (isUNPCKHMask(M, VT, HasInt256)) |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 6987 | return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG); |
| Evan Cheng | e111303 | 2006-10-04 18:33:38 +0000 | [diff] [blame] | 6988 | |
| Evan Cheng | 9bbbb98 | 2006-10-25 20:48:19 +0000 | [diff] [blame] | 6989 | if (V2IsSplat) { |
| 6990 | // Normalize mask so all entries that point to V2 points to its first |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 6991 | // element then try to match unpck{h|l} again. If match, return a |
| Craig Topper | 39a9e48 | 2012-02-11 06:24:48 +0000 | [diff] [blame] | 6992 | // new vector_shuffle with the corrected mask.p |
| 6993 | SmallVector<int, 8> NewMask(M.begin(), M.end()); |
| 6994 | NormalizeMask(NewMask, NumElems); |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6995 | if (isUNPCKLMask(NewMask, VT, HasInt256, true)) |
| Craig Topper | 39a9e48 | 2012-02-11 06:24:48 +0000 | [diff] [blame] | 6996 | return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG); |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 6997 | if (isUNPCKHMask(NewMask, VT, HasInt256, true)) |
| Craig Topper | 39a9e48 | 2012-02-11 06:24:48 +0000 | [diff] [blame] | 6998 | return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 6999 | } |
| 7000 | |
| Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 7001 | if (Commuted) { |
| 7002 | // Commute is back and try unpck* again. |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7003 | // FIXME: this seems wrong. |
| Craig Topper | 39a9e48 | 2012-02-11 06:24:48 +0000 | [diff] [blame] | 7004 | CommuteVectorShuffleMask(M, NumElems); |
| 7005 | std::swap(V1, V2); |
| 7006 | std::swap(V1IsSplat, V2IsSplat); |
| 7007 | Commuted = false; |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 7008 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7009 | if (isUNPCKLMask(M, VT, HasInt256)) |
| Craig Topper | 39a9e48 | 2012-02-11 06:24:48 +0000 | [diff] [blame] | 7010 | return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V2, DAG); |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 7011 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7012 | if (isUNPCKHMask(M, VT, HasInt256)) |
| Craig Topper | 39a9e48 | 2012-02-11 06:24:48 +0000 | [diff] [blame] | 7013 | return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V2, DAG); |
| Evan Cheng | 9eca5e8 | 2006-10-25 21:49:50 +0000 | [diff] [blame] | 7014 | } |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7015 | |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7016 | // Normalize the node to match x86 shuffle ops if needed |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7017 | if (!V2IsUndef && (isSHUFPMask(M, VT, HasFp256, /* Commuted */ true))) |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7018 | return CommuteVectorShuffle(SVOp, DAG); |
| 7019 | |
| Bruno Cardoso Lopes | 7256e22 | 2010-09-03 23:24:06 +0000 | [diff] [blame] | 7020 | // The checks below are all present in isShuffleMaskLegal, but they are |
| 7021 | // inlined here right now to enable us to directly emit target specific |
| 7022 | // nodes, and remove one by one until they don't return Op anymore. |
| Bruno Cardoso Lopes | 7256e22 | 2010-09-03 23:24:06 +0000 | [diff] [blame] | 7023 | |
| Bruno Cardoso Lopes | c800c0d | 2010-09-04 02:02:14 +0000 | [diff] [blame] | 7024 | if (ShuffleVectorSDNode::isSplatMask(&M[0], VT) && |
| 7025 | SVOp->getSplatIndex() == 0 && V2IsUndef) { |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 7026 | if (VT == MVT::v2f64 || VT == MVT::v2i64) |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 7027 | return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG); |
| Bruno Cardoso Lopes | c800c0d | 2010-09-04 02:02:14 +0000 | [diff] [blame] | 7028 | } |
| 7029 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7030 | if (isPSHUFHWMask(M, VT, HasInt256)) |
| Bruno Cardoso Lopes | bbfc310 | 2010-09-04 01:36:45 +0000 | [diff] [blame] | 7031 | return getTargetShuffleNode(X86ISD::PSHUFHW, dl, VT, V1, |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 7032 | getShufflePSHUFHWImmediate(SVOp), |
| Bruno Cardoso Lopes | bbfc310 | 2010-09-04 01:36:45 +0000 | [diff] [blame] | 7033 | DAG); |
| 7034 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7035 | if (isPSHUFLWMask(M, VT, HasInt256)) |
| Bruno Cardoso Lopes | bbfc310 | 2010-09-04 01:36:45 +0000 | [diff] [blame] | 7036 | return getTargetShuffleNode(X86ISD::PSHUFLW, dl, VT, V1, |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 7037 | getShufflePSHUFLWImmediate(SVOp), |
| Bruno Cardoso Lopes | bbfc310 | 2010-09-04 01:36:45 +0000 | [diff] [blame] | 7038 | DAG); |
| 7039 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7040 | if (isSHUFPMask(M, VT, HasFp256)) |
| Craig Topper | b3982da | 2011-12-31 23:50:21 +0000 | [diff] [blame] | 7041 | return getTargetShuffleNode(X86ISD::SHUFP, dl, VT, V1, V2, |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 7042 | getShuffleSHUFImmediate(SVOp), DAG); |
| Bruno Cardoso Lopes | 4c827f5 | 2010-09-04 01:22:57 +0000 | [diff] [blame] | 7043 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7044 | if (isUNPCKL_v_undef_Mask(M, VT, HasInt256)) |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 7045 | return getTargetShuffleNode(X86ISD::UNPCKL, dl, VT, V1, V1, DAG); |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7046 | if (isUNPCKH_v_undef_Mask(M, VT, HasInt256)) |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 7047 | return getTargetShuffleNode(X86ISD::UNPCKH, dl, VT, V1, V1, DAG); |
| Bruno Cardoso Lopes | a22c845 | 2010-09-04 00:39:43 +0000 | [diff] [blame] | 7048 | |
| Bruno Cardoso Lopes | d088834 | 2011-07-22 00:14:56 +0000 | [diff] [blame] | 7049 | //===--------------------------------------------------------------------===// |
| Bruno Cardoso Lopes | cea34e4 | 2011-07-27 00:56:34 +0000 | [diff] [blame] | 7050 | // Generate target specific nodes for 128 or 256-bit shuffles only |
| 7051 | // supported in the AVX instruction set. |
| 7052 | // |
| Bruno Cardoso Lopes | d088834 | 2011-07-22 00:14:56 +0000 | [diff] [blame] | 7053 | |
| Bruno Cardoso Lopes | 6292ece | 2011-08-25 21:40:37 +0000 | [diff] [blame] | 7054 | // Handle VMOVDDUPY permutations |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7055 | if (V2IsUndef && isMOVDDUPYMask(M, VT, HasFp256)) |
| Bruno Cardoso Lopes | 6292ece | 2011-08-25 21:40:37 +0000 | [diff] [blame] | 7056 | return getTargetShuffleNode(X86ISD::MOVDDUP, dl, VT, V1, DAG); |
| 7057 | |
| Craig Topper | 70b883b | 2011-11-28 10:14:51 +0000 | [diff] [blame] | 7058 | // Handle VPERMILPS/D* permutations |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7059 | if (isVPERMILPMask(M, VT, HasFp256)) { |
| 7060 | if (HasInt256 && VT == MVT::v8i32) |
| Craig Topper | dbd98a4 | 2012-02-07 06:28:42 +0000 | [diff] [blame] | 7061 | return getTargetShuffleNode(X86ISD::PSHUFD, dl, VT, V1, |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 7062 | getShuffleSHUFImmediate(SVOp), DAG); |
| Craig Topper | 316cd2a | 2011-11-30 06:25:25 +0000 | [diff] [blame] | 7063 | return getTargetShuffleNode(X86ISD::VPERMILP, dl, VT, V1, |
| Craig Topper | 5aaffa8 | 2012-02-19 02:53:47 +0000 | [diff] [blame] | 7064 | getShuffleSHUFImmediate(SVOp), DAG); |
| Craig Topper | dbd98a4 | 2012-02-07 06:28:42 +0000 | [diff] [blame] | 7065 | } |
| Bruno Cardoso Lopes | cea34e4 | 2011-07-27 00:56:34 +0000 | [diff] [blame] | 7066 | |
| Craig Topper | 70b883b | 2011-11-28 10:14:51 +0000 | [diff] [blame] | 7067 | // Handle VPERM2F128/VPERM2I128 permutations |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7068 | if (isVPERM2X128Mask(M, VT, HasFp256)) |
| Craig Topper | ec24e61 | 2011-11-30 07:47:51 +0000 | [diff] [blame] | 7069 | return getTargetShuffleNode(X86ISD::VPERM2X128, dl, VT, V1, |
| Craig Topper | 70b883b | 2011-11-28 10:14:51 +0000 | [diff] [blame] | 7070 | V2, getShuffleVPERM2X128Immediate(SVOp), DAG); |
| Bruno Cardoso Lopes | cea34e4 | 2011-07-27 00:56:34 +0000 | [diff] [blame] | 7071 | |
| Craig Topper | 1842ba0 | 2012-04-23 06:38:28 +0000 | [diff] [blame] | 7072 | SDValue BlendOp = LowerVECTOR_SHUFFLEtoBlend(SVOp, Subtarget, DAG); |
| Nadav Rotem | e80aa7c | 2012-04-09 08:33:21 +0000 | [diff] [blame] | 7073 | if (BlendOp.getNode()) |
| 7074 | return BlendOp; |
| Craig Topper | 095c528 | 2012-04-15 23:48:57 +0000 | [diff] [blame] | 7075 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7076 | if (V2IsUndef && HasInt256 && (VT == MVT::v8i32 || VT == MVT::v8f32)) { |
| Craig Topper | 095c528 | 2012-04-15 23:48:57 +0000 | [diff] [blame] | 7077 | SmallVector<SDValue, 8> permclMask; |
| Elena Demikhovsky | 73c504a | 2012-04-15 11:18:59 +0000 | [diff] [blame] | 7078 | for (unsigned i = 0; i != 8; ++i) { |
| Craig Topper | 095c528 | 2012-04-15 23:48:57 +0000 | [diff] [blame] | 7079 | permclMask.push_back(DAG.getConstant((M[i]>=0) ? M[i] : 0, MVT::i32)); |
| Elena Demikhovsky | 73c504a | 2012-04-15 11:18:59 +0000 | [diff] [blame] | 7080 | } |
| Craig Topper | 9204074 | 2012-04-16 06:43:40 +0000 | [diff] [blame] | 7081 | SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, |
| 7082 | &permclMask[0], 8); |
| 7083 | // Bitcast is for VPERMPS since mask is v8i32 but node takes v8f32 |
| Craig Topper | 8325c11 | 2012-04-16 00:41:45 +0000 | [diff] [blame] | 7084 | return DAG.getNode(X86ISD::VPERMV, dl, VT, |
| Craig Topper | 9204074 | 2012-04-16 06:43:40 +0000 | [diff] [blame] | 7085 | DAG.getNode(ISD::BITCAST, dl, VT, Mask), V1); |
| Elena Demikhovsky | 73c504a | 2012-04-15 11:18:59 +0000 | [diff] [blame] | 7086 | } |
| Craig Topper | 095c528 | 2012-04-15 23:48:57 +0000 | [diff] [blame] | 7087 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7088 | if (V2IsUndef && HasInt256 && (VT == MVT::v4i64 || VT == MVT::v4f64)) |
| Craig Topper | 8325c11 | 2012-04-16 00:41:45 +0000 | [diff] [blame] | 7089 | return getTargetShuffleNode(X86ISD::VPERMI, dl, VT, V1, |
| Elena Demikhovsky | 73c504a | 2012-04-15 11:18:59 +0000 | [diff] [blame] | 7090 | getShuffleCLImmediate(SVOp), DAG); |
| 7091 | |
| Bruno Cardoso Lopes | cea34e4 | 2011-07-27 00:56:34 +0000 | [diff] [blame] | 7092 | //===--------------------------------------------------------------------===// |
| 7093 | // Since no target specific shuffle was selected for this generic one, |
| 7094 | // lower it into other known shuffles. FIXME: this isn't true yet, but |
| 7095 | // this is the plan. |
| 7096 | // |
| Bruno Cardoso Lopes | 65b74e1 | 2011-07-21 01:55:47 +0000 | [diff] [blame] | 7097 | |
| Bruno Cardoso Lopes | 9b4ad12 | 2011-07-27 00:56:37 +0000 | [diff] [blame] | 7098 | // Handle v8i16 specifically since SSE can do byte extraction and insertion. |
| 7099 | if (VT == MVT::v8i16) { |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 7100 | SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(Op, Subtarget, DAG); |
| Bruno Cardoso Lopes | 9b4ad12 | 2011-07-27 00:56:37 +0000 | [diff] [blame] | 7101 | if (NewOp.getNode()) |
| 7102 | return NewOp; |
| 7103 | } |
| 7104 | |
| 7105 | if (VT == MVT::v16i8) { |
| 7106 | SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this); |
| 7107 | if (NewOp.getNode()) |
| 7108 | return NewOp; |
| 7109 | } |
| 7110 | |
| Elena Demikhovsky | 4178946 | 2012-09-06 12:42:01 +0000 | [diff] [blame] | 7111 | if (VT == MVT::v32i8) { |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 7112 | SDValue NewOp = LowerVECTOR_SHUFFLEv32i8(SVOp, Subtarget, DAG); |
| Elena Demikhovsky | 4178946 | 2012-09-06 12:42:01 +0000 | [diff] [blame] | 7113 | if (NewOp.getNode()) |
| 7114 | return NewOp; |
| 7115 | } |
| 7116 | |
| Bruno Cardoso Lopes | 9b4ad12 | 2011-07-27 00:56:37 +0000 | [diff] [blame] | 7117 | // Handle all 128-bit wide vectors with 4 elements, and match them with |
| 7118 | // several different shuffle types. |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 7119 | if (NumElems == 4 && VT.is128BitVector()) |
| Bruno Cardoso Lopes | 9b4ad12 | 2011-07-27 00:56:37 +0000 | [diff] [blame] | 7120 | return LowerVECTOR_SHUFFLE_128v4(SVOp, DAG); |
| 7121 | |
| Bruno Cardoso Lopes | d088834 | 2011-07-22 00:14:56 +0000 | [diff] [blame] | 7122 | // Handle general 256-bit shuffles |
| 7123 | if (VT.is256BitVector()) |
| 7124 | return LowerVECTOR_SHUFFLE_256(SVOp, DAG); |
| 7125 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7126 | return SDValue(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7127 | } |
| 7128 | |
| Craig Topper | f84b750 | 2013-01-20 00:50:58 +0000 | [diff] [blame] | 7129 | static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) { |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7130 | MVT VT = Op.getValueType().getSimpleVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7131 | SDLoc dl(Op); |
| Bruno Cardoso Lopes | 0b0a09f | 2011-07-29 01:31:02 +0000 | [diff] [blame] | 7132 | |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7133 | if (!Op.getOperand(0).getValueType().getSimpleVT().is128BitVector()) |
| Bruno Cardoso Lopes | 0b0a09f | 2011-07-29 01:31:02 +0000 | [diff] [blame] | 7134 | return SDValue(); |
| 7135 | |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7136 | if (VT.getSizeInBits() == 8) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7137 | SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32, |
| Craig Topper | 7c02284 | 2012-09-12 06:20:41 +0000 | [diff] [blame] | 7138 | Op.getOperand(0), Op.getOperand(1)); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7139 | SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract, |
| Craig Topper | 7c02284 | 2012-09-12 06:20:41 +0000 | [diff] [blame] | 7140 | DAG.getValueType(VT)); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7141 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 7142 | } |
| 7143 | |
| 7144 | if (VT.getSizeInBits() == 16) { |
| Evan Cheng | 52ceafa | 2009-01-02 05:29:08 +0000 | [diff] [blame] | 7145 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 7146 | // If Idx is 0, it's cheaper to do a move instead of a pextrw. |
| 7147 | if (Idx == 0) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7148 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, |
| 7149 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7150 | DAG.getNode(ISD::BITCAST, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7151 | MVT::v4i32, |
| Evan Cheng | 52ceafa | 2009-01-02 05:29:08 +0000 | [diff] [blame] | 7152 | Op.getOperand(0)), |
| 7153 | Op.getOperand(1))); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7154 | SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32, |
| Craig Topper | 7c02284 | 2012-09-12 06:20:41 +0000 | [diff] [blame] | 7155 | Op.getOperand(0), Op.getOperand(1)); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7156 | SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract, |
| Craig Topper | 7c02284 | 2012-09-12 06:20:41 +0000 | [diff] [blame] | 7157 | DAG.getValueType(VT)); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7158 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 7159 | } |
| 7160 | |
| 7161 | if (VT == MVT::f32) { |
| Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 7162 | // EXTRACTPS outputs to a GPR32 register which will require a movd to copy |
| 7163 | // the result back to FR32 register. It's only worth matching if the |
| Dan Gohman | d17cfbe | 2008-10-31 00:57:24 +0000 | [diff] [blame] | 7164 | // result has a single use which is a store or a bitcast to i32. And in |
| 7165 | // the case of a store, it's not worth it if the index is a constant 0, |
| 7166 | // because a MOVSSmr can be used instead, which is smaller and faster. |
| Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 7167 | if (!Op.hasOneUse()) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7168 | return SDValue(); |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7169 | SDNode *User = *Op.getNode()->use_begin(); |
| Dan Gohman | d17cfbe | 2008-10-31 00:57:24 +0000 | [diff] [blame] | 7170 | if ((User->getOpcode() != ISD::STORE || |
| 7171 | (isa<ConstantSDNode>(Op.getOperand(1)) && |
| 7172 | cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) && |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7173 | (User->getOpcode() != ISD::BITCAST || |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7174 | User->getValueType(0) != MVT::i32)) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7175 | return SDValue(); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7176 | SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7177 | DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7178 | Op.getOperand(0)), |
| 7179 | Op.getOperand(1)); |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7180 | return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Extract); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 7181 | } |
| 7182 | |
| 7183 | if (VT == MVT::i32 || VT == MVT::i64) { |
| Pete Cooper | a77214a | 2011-11-14 19:38:42 +0000 | [diff] [blame] | 7184 | // ExtractPS/pextrq works with constant index. |
| Mon P Wang | f0fcdd8 | 2009-01-15 21:10:20 +0000 | [diff] [blame] | 7185 | if (isa<ConstantSDNode>(Op.getOperand(1))) |
| 7186 | return Op; |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7187 | } |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7188 | return SDValue(); |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7189 | } |
| 7190 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7191 | SDValue |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7192 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, |
| 7193 | SelectionDAG &DAG) const { |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7194 | if (!isa<ConstantSDNode>(Op.getOperand(1))) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7195 | return SDValue(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7196 | |
| David Greene | 74a579d | 2011-02-10 16:57:36 +0000 | [diff] [blame] | 7197 | SDValue Vec = Op.getOperand(0); |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7198 | MVT VecVT = Vec.getValueType().getSimpleVT(); |
| David Greene | 74a579d | 2011-02-10 16:57:36 +0000 | [diff] [blame] | 7199 | |
| Bruno Cardoso Lopes | 0b0a09f | 2011-07-29 01:31:02 +0000 | [diff] [blame] | 7200 | // If this is a 256-bit vector result, first extract the 128-bit vector and |
| 7201 | // then extract the element from the 128-bit vector. |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 7202 | if (VecVT.is256BitVector()) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7203 | SDLoc dl(Op.getNode()); |
| David Greene | 74a579d | 2011-02-10 16:57:36 +0000 | [diff] [blame] | 7204 | unsigned NumElems = VecVT.getVectorNumElements(); |
| 7205 | SDValue Idx = Op.getOperand(1); |
| David Greene | 74a579d | 2011-02-10 16:57:36 +0000 | [diff] [blame] | 7206 | unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue(); |
| 7207 | |
| 7208 | // Get the 128-bit vector. |
| Craig Topper | 7d1e3dc | 2012-04-30 05:17:10 +0000 | [diff] [blame] | 7209 | Vec = Extract128BitVector(Vec, IdxVal, DAG, dl); |
| David Greene | 74a579d | 2011-02-10 16:57:36 +0000 | [diff] [blame] | 7210 | |
| Craig Topper | 7d1e3dc | 2012-04-30 05:17:10 +0000 | [diff] [blame] | 7211 | if (IdxVal >= NumElems/2) |
| 7212 | IdxVal -= NumElems/2; |
| David Greene | 74a579d | 2011-02-10 16:57:36 +0000 | [diff] [blame] | 7213 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec, |
| Craig Topper | 7d1e3dc | 2012-04-30 05:17:10 +0000 | [diff] [blame] | 7214 | DAG.getConstant(IdxVal, MVT::i32)); |
| David Greene | 74a579d | 2011-02-10 16:57:36 +0000 | [diff] [blame] | 7215 | } |
| 7216 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 7217 | assert(VecVT.is128BitVector() && "Unexpected vector length"); |
| David Greene | 74a579d | 2011-02-10 16:57:36 +0000 | [diff] [blame] | 7218 | |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 7219 | if (Subtarget->hasSSE41()) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7220 | SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG); |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 7221 | if (Res.getNode()) |
| Evan Cheng | 62a3f15 | 2008-03-24 21:52:23 +0000 | [diff] [blame] | 7222 | return Res; |
| 7223 | } |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7224 | |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7225 | MVT VT = Op.getValueType().getSimpleVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7226 | SDLoc dl(Op); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7227 | // TODO: handle v16i8. |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7228 | if (VT.getSizeInBits() == 16) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7229 | SDValue Vec = Op.getOperand(0); |
| Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7230 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 7231 | if (Idx == 0) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7232 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, |
| 7233 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 7234 | DAG.getNode(ISD::BITCAST, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7235 | MVT::v4i32, Vec), |
| Evan Cheng | 14b32e1 | 2007-12-11 01:46:18 +0000 | [diff] [blame] | 7236 | Op.getOperand(1))); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7237 | // Transform it so it match pextrw which produces a 32-bit result. |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7238 | MVT EltVT = MVT::i32; |
| Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 7239 | SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT, |
| Craig Topper | 7c02284 | 2012-09-12 06:20:41 +0000 | [diff] [blame] | 7240 | Op.getOperand(0), Op.getOperand(1)); |
| Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 7241 | SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract, |
| Craig Topper | 7c02284 | 2012-09-12 06:20:41 +0000 | [diff] [blame] | 7242 | DAG.getValueType(VT)); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7243 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 7244 | } |
| 7245 | |
| 7246 | if (VT.getSizeInBits() == 32) { |
| Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7247 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7248 | if (Idx == 0) |
| 7249 | return Op; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7250 | |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7251 | // SHUFPS the element to the lowest double word, then movss. |
| Jeffrey Yasskin | a44defe | 2011-07-27 06:22:51 +0000 | [diff] [blame] | 7252 | int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 }; |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7253 | MVT VVT = Op.getOperand(0).getValueType().getSimpleVT(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7254 | SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0), |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7255 | DAG.getUNDEF(VVT), Mask); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7256 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, |
| Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 7257 | DAG.getIntPtrConstant(0)); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 7258 | } |
| 7259 | |
| 7260 | if (VT.getSizeInBits() == 64) { |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7261 | // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b |
| 7262 | // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught |
| 7263 | // to match extract_elt for f64. |
| Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7264 | unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7265 | if (Idx == 0) |
| 7266 | return Op; |
| 7267 | |
| 7268 | // UNPCKHPD the element to the lowest double word, then movsd. |
| 7269 | // Note if the lower 64 bits of the result of the UNPCKHPD is then stored |
| 7270 | // to a f64mem, the whole operation is folded into a single MOVHPDmr. |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7271 | int Mask[2] = { 1, -1 }; |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7272 | MVT VVT = Op.getOperand(0).getValueType().getSimpleVT(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7273 | SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0), |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 7274 | DAG.getUNDEF(VVT), Mask); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7275 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, |
| Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 7276 | DAG.getIntPtrConstant(0)); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7277 | } |
| 7278 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7279 | return SDValue(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7280 | } |
| 7281 | |
| Craig Topper | f84b750 | 2013-01-20 00:50:58 +0000 | [diff] [blame] | 7282 | static SDValue LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) { |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7283 | MVT VT = Op.getValueType().getSimpleVT(); |
| 7284 | MVT EltVT = VT.getVectorElementType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7285 | SDLoc dl(Op); |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7286 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7287 | SDValue N0 = Op.getOperand(0); |
| 7288 | SDValue N1 = Op.getOperand(1); |
| 7289 | SDValue N2 = Op.getOperand(2); |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7290 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 7291 | if (!VT.is128BitVector()) |
| Bruno Cardoso Lopes | 0b0a09f | 2011-07-29 01:31:02 +0000 | [diff] [blame] | 7292 | return SDValue(); |
| 7293 | |
| Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 7294 | if ((EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) && |
| Dan Gohman | ef521f1 | 2008-08-14 22:53:18 +0000 | [diff] [blame] | 7295 | isa<ConstantSDNode>(N2)) { |
| Chris Lattner | 8f2b4cc | 2010-02-23 02:07:48 +0000 | [diff] [blame] | 7296 | unsigned Opc; |
| 7297 | if (VT == MVT::v8i16) |
| 7298 | Opc = X86ISD::PINSRW; |
| Chris Lattner | 8f2b4cc | 2010-02-23 02:07:48 +0000 | [diff] [blame] | 7299 | else if (VT == MVT::v16i8) |
| 7300 | Opc = X86ISD::PINSRB; |
| 7301 | else |
| 7302 | Opc = X86ISD::PINSRB; |
| 7303 | |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7304 | // Transform it so it match pinsr{b,w} which expects a GR32 as its second |
| 7305 | // argument. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7306 | if (N1.getValueType() != MVT::i32) |
| 7307 | N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1); |
| 7308 | if (N2.getValueType() != MVT::i32) |
| Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7309 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue()); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7310 | return DAG.getNode(Opc, dl, VT, N0, N1, N2); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 7311 | } |
| 7312 | |
| 7313 | if (EltVT == MVT::f32 && isa<ConstantSDNode>(N2)) { |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7314 | // Bits [7:6] of the constant are the source select. This will always be |
| 7315 | // zero here. The DAG Combiner may combine an extract_elt index into these |
| 7316 | // bits. For example (insert (extract, 3), 2) could be matched by putting |
| 7317 | // the '3' into bits [7:6] of X86ISD::INSERTPS. |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7318 | // Bits [5:4] of the constant are the destination select. This is the |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7319 | // value of the incoming immediate. |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7320 | // Bits [3:0] of the constant are the zero mask. The DAG Combiner may |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7321 | // combine either bitwise AND or insert of float 0.0 to set these bits. |
| Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7322 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4); |
| Eric Christopher | fbd6687 | 2009-07-24 00:33:09 +0000 | [diff] [blame] | 7323 | // Create this as a scalar to vector.. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7324 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7325 | return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 7326 | } |
| 7327 | |
| 7328 | if ((EltVT == MVT::i32 || EltVT == MVT::i64) && isa<ConstantSDNode>(N2)) { |
| Eric Christopher | fbd6687 | 2009-07-24 00:33:09 +0000 | [diff] [blame] | 7329 | // PINSR* works with constant index. |
| 7330 | return Op; |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7331 | } |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7332 | return SDValue(); |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7333 | } |
| 7334 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7335 | SDValue |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7336 | X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const { |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7337 | MVT VT = Op.getValueType().getSimpleVT(); |
| 7338 | MVT EltVT = VT.getVectorElementType(); |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7339 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7340 | SDLoc dl(Op); |
| David Greene | 6b38126 | 2011-02-09 15:32:06 +0000 | [diff] [blame] | 7341 | SDValue N0 = Op.getOperand(0); |
| 7342 | SDValue N1 = Op.getOperand(1); |
| 7343 | SDValue N2 = Op.getOperand(2); |
| 7344 | |
| Bruno Cardoso Lopes | 0b0a09f | 2011-07-29 01:31:02 +0000 | [diff] [blame] | 7345 | // If this is a 256-bit vector result, first extract the 128-bit vector, |
| 7346 | // insert the element into the extracted half and then place it back. |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 7347 | if (VT.is256BitVector()) { |
| David Greene | 6b38126 | 2011-02-09 15:32:06 +0000 | [diff] [blame] | 7348 | if (!isa<ConstantSDNode>(N2)) |
| 7349 | return SDValue(); |
| 7350 | |
| Bruno Cardoso Lopes | 0b0a09f | 2011-07-29 01:31:02 +0000 | [diff] [blame] | 7351 | // Get the desired 128-bit vector half. |
| David Greene | 6b38126 | 2011-02-09 15:32:06 +0000 | [diff] [blame] | 7352 | unsigned NumElems = VT.getVectorNumElements(); |
| 7353 | unsigned IdxVal = cast<ConstantSDNode>(N2)->getZExtValue(); |
| Craig Topper | 7d1e3dc | 2012-04-30 05:17:10 +0000 | [diff] [blame] | 7354 | SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl); |
| David Greene | 6b38126 | 2011-02-09 15:32:06 +0000 | [diff] [blame] | 7355 | |
| Bruno Cardoso Lopes | 0b0a09f | 2011-07-29 01:31:02 +0000 | [diff] [blame] | 7356 | // Insert the element into the desired half. |
| Craig Topper | 7d1e3dc | 2012-04-30 05:17:10 +0000 | [diff] [blame] | 7357 | bool Upper = IdxVal >= NumElems/2; |
| 7358 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1, |
| 7359 | DAG.getConstant(Upper ? IdxVal-NumElems/2 : IdxVal, MVT::i32)); |
| David Greene | 6b38126 | 2011-02-09 15:32:06 +0000 | [diff] [blame] | 7360 | |
| Bruno Cardoso Lopes | 0b0a09f | 2011-07-29 01:31:02 +0000 | [diff] [blame] | 7361 | // Insert the changed part back to the 256-bit vector |
| Craig Topper | 7d1e3dc | 2012-04-30 05:17:10 +0000 | [diff] [blame] | 7362 | return Insert128BitVector(N0, V, IdxVal, DAG, dl); |
| David Greene | 6b38126 | 2011-02-09 15:32:06 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 7365 | if (Subtarget->hasSSE41()) |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 7366 | return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG); |
| 7367 | |
| Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 7368 | if (EltVT == MVT::i8) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7369 | return SDValue(); |
| Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 7370 | |
| Dan Gohman | 8a55ce4 | 2009-09-23 21:02:20 +0000 | [diff] [blame] | 7371 | if (EltVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) { |
| Evan Cheng | 794405e | 2007-12-12 07:55:34 +0000 | [diff] [blame] | 7372 | // Transform it so it match pinsrw which expects a 16-bit value in a GR32 |
| 7373 | // as its second argument. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7374 | if (N1.getValueType() != MVT::i32) |
| 7375 | N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1); |
| 7376 | if (N2.getValueType() != MVT::i32) |
| Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 7377 | N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue()); |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 7378 | return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7379 | } |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7380 | return SDValue(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7381 | } |
| 7382 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 7383 | static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) { |
| Bruno Cardoso Lopes | 233fa39 | 2011-07-25 23:05:16 +0000 | [diff] [blame] | 7384 | LLVMContext *Context = DAG.getContext(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7385 | SDLoc dl(Op); |
| Craig Topper | 45e1c75 | 2013-01-20 00:38:18 +0000 | [diff] [blame] | 7386 | MVT OpVT = Op.getValueType().getSimpleVT(); |
| David Greene | 2fcdfb4 | 2011-02-10 23:11:29 +0000 | [diff] [blame] | 7387 | |
| Bruno Cardoso Lopes | 233fa39 | 2011-07-25 23:05:16 +0000 | [diff] [blame] | 7388 | // If this is a 256-bit vector result, first insert into a 128-bit |
| 7389 | // vector and then insert into the 256-bit vector. |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 7390 | if (!OpVT.is128BitVector()) { |
| Bruno Cardoso Lopes | 233fa39 | 2011-07-25 23:05:16 +0000 | [diff] [blame] | 7391 | // Insert into a 128-bit vector. |
| 7392 | EVT VT128 = EVT::getVectorVT(*Context, |
| 7393 | OpVT.getVectorElementType(), |
| 7394 | OpVT.getVectorNumElements() / 2); |
| 7395 | |
| 7396 | Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0)); |
| 7397 | |
| 7398 | // Insert the 128-bit vector. |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 7399 | return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl); |
| Bruno Cardoso Lopes | 233fa39 | 2011-07-25 23:05:16 +0000 | [diff] [blame] | 7400 | } |
| 7401 | |
| Craig Topper | d77d2fe | 2012-04-29 20:22:05 +0000 | [diff] [blame] | 7402 | if (OpVT == MVT::v1i64 && |
| Chris Lattner | f172ecd | 2010-07-04 23:07:25 +0000 | [diff] [blame] | 7403 | Op.getOperand(0).getValueType() == MVT::i64) |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7404 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0)); |
| Rafael Espindola | def390a | 2009-08-03 02:45:34 +0000 | [diff] [blame] | 7405 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7406 | SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0)); |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 7407 | assert(OpVT.is128BitVector() && "Expected an SSE type!"); |
| Craig Topper | d77d2fe | 2012-04-29 20:22:05 +0000 | [diff] [blame] | 7408 | return DAG.getNode(ISD::BITCAST, dl, OpVT, |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 7409 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,AnyExt)); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7410 | } |
| 7411 | |
| David Greene | 9158509 | 2011-01-26 15:38:49 +0000 | [diff] [blame] | 7412 | // Lower a node with an EXTRACT_SUBVECTOR opcode. This may result in |
| 7413 | // a simple subregister reference or explicit instructions to grab |
| 7414 | // upper bits of a vector. |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 7415 | static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget, |
| 7416 | SelectionDAG &DAG) { |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7417 | if (Subtarget->hasFp256()) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7418 | SDLoc dl(Op.getNode()); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 7419 | SDValue Vec = Op.getNode()->getOperand(0); |
| 7420 | SDValue Idx = Op.getNode()->getOperand(1); |
| 7421 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 7422 | if (Op.getNode()->getValueType(0).is128BitVector() && |
| 7423 | Vec.getNode()->getValueType(0).is256BitVector() && |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 7424 | isa<ConstantSDNode>(Idx)) { |
| 7425 | unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue(); |
| 7426 | return Extract128BitVector(Vec, IdxVal, DAG, dl); |
| David Greene | a5f2601 | 2011-02-07 19:36:54 +0000 | [diff] [blame] | 7427 | } |
| David Greene | 9158509 | 2011-01-26 15:38:49 +0000 | [diff] [blame] | 7428 | } |
| 7429 | return SDValue(); |
| 7430 | } |
| 7431 | |
| David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 7432 | // Lower a node with an INSERT_SUBVECTOR opcode. This may result in a |
| 7433 | // simple superregister reference or explicit instructions to insert |
| 7434 | // the upper bits of a vector. |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 7435 | static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget, |
| 7436 | SelectionDAG &DAG) { |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 7437 | if (Subtarget->hasFp256()) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7438 | SDLoc dl(Op.getNode()); |
| David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 7439 | SDValue Vec = Op.getNode()->getOperand(0); |
| 7440 | SDValue SubVec = Op.getNode()->getOperand(1); |
| 7441 | SDValue Idx = Op.getNode()->getOperand(2); |
| 7442 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 7443 | if (Op.getNode()->getValueType(0).is256BitVector() && |
| 7444 | SubVec.getNode()->getValueType(0).is128BitVector() && |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 7445 | isa<ConstantSDNode>(Idx)) { |
| 7446 | unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue(); |
| 7447 | return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl); |
| David Greene | cfe33c4 | 2011-01-26 19:13:22 +0000 | [diff] [blame] | 7448 | } |
| 7449 | } |
| 7450 | return SDValue(); |
| 7451 | } |
| 7452 | |
| Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 7453 | // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as |
| 7454 | // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is |
| 7455 | // one of the above mentioned nodes. It has to be wrapped because otherwise |
| 7456 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only |
| 7457 | // be used to form addressing mode. These wrapped nodes will be selected |
| 7458 | // into MOV32ri. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7459 | SDValue |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7460 | X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const { |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7461 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7462 | |
| Chris Lattner | 41621a2 | 2009-06-26 19:22:52 +0000 | [diff] [blame] | 7463 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the |
| 7464 | // global base reg. |
| 7465 | unsigned char OpFlag = 0; |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7466 | unsigned WrapperKind = X86ISD::Wrapper; |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 7467 | CodeModel::Model M = getTargetMachine().getCodeModel(); |
| 7468 | |
| Chris Lattner | 4f06649 | 2009-07-11 20:29:19 +0000 | [diff] [blame] | 7469 | if (Subtarget->isPICStyleRIPRel() && |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 7470 | (M == CodeModel::Small || M == CodeModel::Kernel)) |
| Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 7471 | WrapperKind = X86ISD::WrapperRIP; |
| Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 7472 | else if (Subtarget->isPICStyleGOT()) |
| Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 7473 | OpFlag = X86II::MO_GOTOFF; |
| Chris Lattner | e2c9208 | 2009-07-10 21:00:45 +0000 | [diff] [blame] | 7474 | else if (Subtarget->isPICStyleStubPIC()) |
| Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 7475 | OpFlag = X86II::MO_PIC_BASE_OFFSET; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7476 | |
| Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 7477 | SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(), |
| Chris Lattner | 41621a2 | 2009-06-26 19:22:52 +0000 | [diff] [blame] | 7478 | CP->getAlignment(), |
| 7479 | CP->getOffset(), OpFlag); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7480 | SDLoc DL(CP); |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7481 | Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); |
| Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 7482 | // With PIC, the address is actually $g + Offset. |
| Chris Lattner | 41621a2 | 2009-06-26 19:22:52 +0000 | [diff] [blame] | 7483 | if (OpFlag) { |
| 7484 | Result = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
| Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 7485 | DAG.getNode(X86ISD::GlobalBaseReg, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7486 | SDLoc(), getPointerTy()), |
| Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 7487 | Result); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7488 | } |
| 7489 | |
| 7490 | return Result; |
| 7491 | } |
| 7492 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7493 | SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7494 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7495 | |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7496 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the |
| 7497 | // global base reg. |
| 7498 | unsigned char OpFlag = 0; |
| 7499 | unsigned WrapperKind = X86ISD::Wrapper; |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 7500 | CodeModel::Model M = getTargetMachine().getCodeModel(); |
| 7501 | |
| Chris Lattner | 4f06649 | 2009-07-11 20:29:19 +0000 | [diff] [blame] | 7502 | if (Subtarget->isPICStyleRIPRel() && |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 7503 | (M == CodeModel::Small || M == CodeModel::Kernel)) |
| Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 7504 | WrapperKind = X86ISD::WrapperRIP; |
| Chris Lattner | 3b67e9b | 2009-07-10 20:47:30 +0000 | [diff] [blame] | 7505 | else if (Subtarget->isPICStyleGOT()) |
| Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 7506 | OpFlag = X86II::MO_GOTOFF; |
| Chris Lattner | e2c9208 | 2009-07-10 21:00:45 +0000 | [diff] [blame] | 7507 | else if (Subtarget->isPICStyleStubPIC()) |
| Chris Lattner | 88e1fd5 | 2009-07-09 04:24:46 +0000 | [diff] [blame] | 7508 | OpFlag = X86II::MO_PIC_BASE_OFFSET; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7509 | |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7510 | SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(), |
| 7511 | OpFlag); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7512 | SDLoc DL(JT); |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7513 | Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7514 | |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7515 | // With PIC, the address is actually $g + Offset. |
| Chris Lattner | 1e61e69 | 2010-11-15 02:46:57 +0000 | [diff] [blame] | 7516 | if (OpFlag) |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7517 | Result = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
| 7518 | DAG.getNode(X86ISD::GlobalBaseReg, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7519 | SDLoc(), getPointerTy()), |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7520 | Result); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7521 | |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7522 | return Result; |
| 7523 | } |
| 7524 | |
| 7525 | SDValue |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7526 | X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const { |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7527 | const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7528 | |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7529 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the |
| 7530 | // global base reg. |
| 7531 | unsigned char OpFlag = 0; |
| 7532 | unsigned WrapperKind = X86ISD::Wrapper; |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 7533 | CodeModel::Model M = getTargetMachine().getCodeModel(); |
| 7534 | |
| Chris Lattner | 4f06649 | 2009-07-11 20:29:19 +0000 | [diff] [blame] | 7535 | if (Subtarget->isPICStyleRIPRel() && |
| Eli Friedman | 586272d | 2011-08-11 01:48:05 +0000 | [diff] [blame] | 7536 | (M == CodeModel::Small || M == CodeModel::Kernel)) { |
| 7537 | if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF()) |
| 7538 | OpFlag = X86II::MO_GOTPCREL; |
| Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 7539 | WrapperKind = X86ISD::WrapperRIP; |
| Eli Friedman | 586272d | 2011-08-11 01:48:05 +0000 | [diff] [blame] | 7540 | } else if (Subtarget->isPICStyleGOT()) { |
| 7541 | OpFlag = X86II::MO_GOT; |
| 7542 | } else if (Subtarget->isPICStyleStubPIC()) { |
| 7543 | OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE; |
| 7544 | } else if (Subtarget->isPICStyleStubNoDynamic()) { |
| 7545 | OpFlag = X86II::MO_DARWIN_NONLAZY; |
| 7546 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7547 | |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7548 | SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7549 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7550 | SDLoc DL(Op); |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7551 | Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7552 | |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7553 | // With PIC, the address is actually $g + Offset. |
| 7554 | if (getTargetMachine().getRelocationModel() == Reloc::PIC_ && |
| Chris Lattner | e4df756 | 2009-07-09 03:15:51 +0000 | [diff] [blame] | 7555 | !Subtarget->is64Bit()) { |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7556 | Result = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
| 7557 | DAG.getNode(X86ISD::GlobalBaseReg, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7558 | SDLoc(), getPointerTy()), |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7559 | Result); |
| 7560 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7561 | |
| Eli Friedman | 586272d | 2011-08-11 01:48:05 +0000 | [diff] [blame] | 7562 | // For symbols that require a load from a stub to get the address, emit the |
| 7563 | // load. |
| 7564 | if (isGlobalStubReference(OpFlag)) |
| 7565 | Result = DAG.getLoad(getPointerTy(), DL, DAG.getEntryNode(), Result, |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7566 | MachinePointerInfo::getGOT(), false, false, false, 0); |
| Eli Friedman | 586272d | 2011-08-11 01:48:05 +0000 | [diff] [blame] | 7567 | |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7568 | return Result; |
| 7569 | } |
| 7570 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7571 | SDValue |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7572 | X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const { |
| Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 7573 | // Create the TargetBlockAddressAddress node. |
| 7574 | unsigned char OpFlags = |
| 7575 | Subtarget->ClassifyBlockAddressReference(); |
| Dan Gohman | f705adb | 2009-10-30 01:28:02 +0000 | [diff] [blame] | 7576 | CodeModel::Model M = getTargetMachine().getCodeModel(); |
| Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 7577 | const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); |
| Michael Liao | 6c7ccaa | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 7578 | int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7579 | SDLoc dl(Op); |
| Michael Liao | 6c7ccaa | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 7580 | SDValue Result = DAG.getTargetBlockAddress(BA, getPointerTy(), Offset, |
| 7581 | OpFlags); |
| Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 7582 | |
| Dan Gohman | f705adb | 2009-10-30 01:28:02 +0000 | [diff] [blame] | 7583 | if (Subtarget->isPICStyleRIPRel() && |
| 7584 | (M == CodeModel::Small || M == CodeModel::Kernel)) |
| Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 7585 | Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result); |
| 7586 | else |
| 7587 | Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result); |
| Dan Gohman | f705adb | 2009-10-30 01:28:02 +0000 | [diff] [blame] | 7588 | |
| Dan Gohman | 29cbade | 2009-11-20 23:18:13 +0000 | [diff] [blame] | 7589 | // With PIC, the address is actually $g + Offset. |
| 7590 | if (isGlobalRelativeToPICBase(OpFlags)) { |
| 7591 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
| 7592 | DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()), |
| 7593 | Result); |
| 7594 | } |
| Dan Gohman | f705adb | 2009-10-30 01:28:02 +0000 | [diff] [blame] | 7595 | |
| 7596 | return Result; |
| 7597 | } |
| 7598 | |
| 7599 | SDValue |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7600 | X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, SDLoc dl, |
| Craig Topper | b99bafe | 2013-01-21 06:21:54 +0000 | [diff] [blame] | 7601 | int64_t Offset, SelectionDAG &DAG) const { |
| Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 7602 | // Create the TargetGlobalAddress node, folding in the constant |
| 7603 | // offset if it is legal. |
| Chris Lattner | d392bd9 | 2009-07-10 07:20:05 +0000 | [diff] [blame] | 7604 | unsigned char OpFlags = |
| 7605 | Subtarget->ClassifyGlobalReference(GV, getTargetMachine()); |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 7606 | CodeModel::Model M = getTargetMachine().getCodeModel(); |
| Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 7607 | SDValue Result; |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 7608 | if (OpFlags == X86II::MO_NO_FLAG && |
| 7609 | X86::isOffsetSuitableForCodeModel(Offset, M)) { |
| Chris Lattner | 4aa21aa | 2009-07-09 00:58:53 +0000 | [diff] [blame] | 7610 | // A direct static reference to a global. |
| Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 7611 | Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), Offset); |
| Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 7612 | Offset = 0; |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7613 | } else { |
| Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 7614 | Result = DAG.getTargetGlobalAddress(GV, dl, getPointerTy(), 0, OpFlags); |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7615 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7616 | |
| Chris Lattner | 4f06649 | 2009-07-11 20:29:19 +0000 | [diff] [blame] | 7617 | if (Subtarget->isPICStyleRIPRel() && |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 7618 | (M == CodeModel::Small || M == CodeModel::Kernel)) |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7619 | Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result); |
| 7620 | else |
| 7621 | Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result); |
| Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 7622 | |
| Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 7623 | // With PIC, the address is actually $g + Offset. |
| Chris Lattner | 36c2501 | 2009-07-10 07:34:39 +0000 | [diff] [blame] | 7624 | if (isGlobalRelativeToPICBase(OpFlags)) { |
| Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 7625 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), |
| 7626 | DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()), |
| Anton Korobeynikov | 7f70559 | 2007-01-12 19:20:47 +0000 | [diff] [blame] | 7627 | Result); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7628 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7629 | |
| Chris Lattner | 36c2501 | 2009-07-10 07:34:39 +0000 | [diff] [blame] | 7630 | // For globals that require a load from a stub to get the address, emit the |
| 7631 | // load. |
| 7632 | if (isGlobalStubReference(OpFlags)) |
| Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 7633 | Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result, |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7634 | MachinePointerInfo::getGOT(), false, false, false, 0); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7635 | |
| Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 7636 | // If there was a non-zero offset that we didn't fold, create an explicit |
| 7637 | // addition for it. |
| 7638 | if (Offset != 0) |
| Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 7639 | Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result, |
| Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 7640 | DAG.getConstant(Offset, getPointerTy())); |
| 7641 | |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7642 | return Result; |
| 7643 | } |
| 7644 | |
| Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 7645 | SDValue |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7646 | X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { |
| Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 7647 | const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
| Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 7648 | int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7649 | return LowerGlobalAddress(GV, SDLoc(Op), Offset, DAG); |
| Evan Cheng | da43bcf | 2008-09-24 00:05:32 +0000 | [diff] [blame] | 7650 | } |
| 7651 | |
| Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 7652 | static SDValue |
| 7653 | GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA, |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7654 | SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg, |
| Hans Wennborg | f0234fc | 2012-06-01 16:27:21 +0000 | [diff] [blame] | 7655 | unsigned char OperandFlags, bool LocalDynamic = false) { |
| Anton Korobeynikov | 817a464 | 2009-12-11 19:39:55 +0000 | [diff] [blame] | 7656 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 7657 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7658 | SDLoc dl(GA); |
| Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 7659 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, |
| Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 7660 | GA->getValueType(0), |
| Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 7661 | GA->getOffset(), |
| 7662 | OperandFlags); |
| Hans Wennborg | f0234fc | 2012-06-01 16:27:21 +0000 | [diff] [blame] | 7663 | |
| 7664 | X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR |
| 7665 | : X86ISD::TLSADDR; |
| 7666 | |
| Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 7667 | if (InFlag) { |
| 7668 | SDValue Ops[] = { Chain, TGA, *InFlag }; |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 7669 | Chain = DAG.getNode(CallType, dl, NodeTys, Ops, array_lengthof(Ops)); |
| Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 7670 | } else { |
| 7671 | SDValue Ops[] = { Chain, TGA }; |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 7672 | Chain = DAG.getNode(CallType, dl, NodeTys, Ops, array_lengthof(Ops)); |
| Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 7673 | } |
| Anton Korobeynikov | 817a464 | 2009-12-11 19:39:55 +0000 | [diff] [blame] | 7674 | |
| 7675 | // TLSADDR will be codegen'ed as call. Inform MFI that function has calls. |
| Bill Wendling | b92187a | 2010-05-14 21:14:32 +0000 | [diff] [blame] | 7676 | MFI->setAdjustsStack(true); |
| Anton Korobeynikov | 817a464 | 2009-12-11 19:39:55 +0000 | [diff] [blame] | 7677 | |
| Rafael Espindola | 15f1b66 | 2009-04-24 12:59:40 +0000 | [diff] [blame] | 7678 | SDValue Flag = Chain.getValue(1); |
| 7679 | return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag); |
| Rafael Espindola | 2ee3db3 | 2009-04-17 14:35:58 +0000 | [diff] [blame] | 7680 | } |
| 7681 | |
| Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 7682 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7683 | static SDValue |
| Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 7684 | LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7685 | const EVT PtrVT) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7686 | SDValue InFlag; |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7687 | SDLoc dl(GA); // ? function entry point might be better |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 7688 | SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX, |
| Craig Topper | 7c02284 | 2012-09-12 06:20:41 +0000 | [diff] [blame] | 7689 | DAG.getNode(X86ISD::GlobalBaseReg, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7690 | SDLoc(), PtrVT), InFlag); |
| Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 7691 | InFlag = Chain.getValue(1); |
| 7692 | |
| Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 7693 | return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD); |
| Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 7694 | } |
| 7695 | |
| Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 7696 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7697 | static SDValue |
| Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 7698 | LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7699 | const EVT PtrVT) { |
| Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 7700 | return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, |
| 7701 | X86::RAX, X86II::MO_TLSGD); |
| Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 7702 | } |
| 7703 | |
| Hans Wennborg | f0234fc | 2012-06-01 16:27:21 +0000 | [diff] [blame] | 7704 | static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA, |
| 7705 | SelectionDAG &DAG, |
| 7706 | const EVT PtrVT, |
| 7707 | bool is64Bit) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7708 | SDLoc dl(GA); |
| Hans Wennborg | f0234fc | 2012-06-01 16:27:21 +0000 | [diff] [blame] | 7709 | |
| 7710 | // Get the start address of the TLS block for this module. |
| 7711 | X86MachineFunctionInfo* MFI = DAG.getMachineFunction() |
| 7712 | .getInfo<X86MachineFunctionInfo>(); |
| 7713 | MFI->incNumLocalDynamicTLSAccesses(); |
| 7714 | |
| 7715 | SDValue Base; |
| 7716 | if (is64Bit) { |
| 7717 | Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX, |
| 7718 | X86II::MO_TLSLD, /*LocalDynamic=*/true); |
| 7719 | } else { |
| 7720 | SDValue InFlag; |
| 7721 | SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7722 | DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), InFlag); |
| Hans Wennborg | f0234fc | 2012-06-01 16:27:21 +0000 | [diff] [blame] | 7723 | InFlag = Chain.getValue(1); |
| 7724 | Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, |
| 7725 | X86II::MO_TLSLDM, /*LocalDynamic=*/true); |
| 7726 | } |
| 7727 | |
| 7728 | // Note: the CleanupLocalDynamicTLSPass will remove redundant computations |
| 7729 | // of Base. |
| 7730 | |
| 7731 | // Build x@dtpoff. |
| 7732 | unsigned char OperandFlags = X86II::MO_DTPOFF; |
| 7733 | unsigned WrapperKind = X86ISD::Wrapper; |
| 7734 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, |
| 7735 | GA->getValueType(0), |
| 7736 | GA->getOffset(), OperandFlags); |
| 7737 | SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA); |
| 7738 | |
| 7739 | // Add x@dtpoff with the base. |
| 7740 | return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base); |
| 7741 | } |
| 7742 | |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7743 | // Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7744 | static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG, |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7745 | const EVT PtrVT, TLSModel::Model model, |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7746 | bool is64Bit, bool isPIC) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7747 | SDLoc dl(GA); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7748 | |
| Chris Lattner | f93b90c | 2010-09-22 04:39:11 +0000 | [diff] [blame] | 7749 | // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit). |
| 7750 | Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(), |
| 7751 | is64Bit ? 257 : 256)); |
| Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 7752 | |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7753 | SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), |
| Chris Lattner | f93b90c | 2010-09-22 04:39:11 +0000 | [diff] [blame] | 7754 | DAG.getIntPtrConstant(0), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 7755 | MachinePointerInfo(Ptr), |
| 7756 | false, false, false, 0); |
| Rafael Espindola | 094fad3 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 7757 | |
| Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 7758 | unsigned char OperandFlags = 0; |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7759 | // Most TLS accesses are not RIP relative, even on x86-64. One exception is |
| 7760 | // initialexec. |
| 7761 | unsigned WrapperKind = X86ISD::Wrapper; |
| 7762 | if (model == TLSModel::LocalExec) { |
| Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 7763 | OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF; |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7764 | } else if (model == TLSModel::InitialExec) { |
| 7765 | if (is64Bit) { |
| 7766 | OperandFlags = X86II::MO_GOTTPOFF; |
| 7767 | WrapperKind = X86ISD::WrapperRIP; |
| 7768 | } else { |
| 7769 | OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF; |
| 7770 | } |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7771 | } else { |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7772 | llvm_unreachable("Unexpected model"); |
| Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 7773 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7774 | |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7775 | // emit "addl x@ntpoff,%eax" (local exec) |
| 7776 | // or "addl x@indntpoff,%eax" (initial exec) |
| 7777 | // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic) |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7778 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, |
| Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 7779 | GA->getValueType(0), |
| Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 7780 | GA->getOffset(), OperandFlags); |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 7781 | SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA); |
| Lauro Ramos Venancio | 7d2cc2b | 2007-04-22 22:50:52 +0000 | [diff] [blame] | 7782 | |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7783 | if (model == TLSModel::InitialExec) { |
| 7784 | if (isPIC && !is64Bit) { |
| 7785 | Offset = DAG.getNode(ISD::ADD, dl, PtrVT, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7786 | DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7787 | Offset); |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7788 | } |
| Rafael Espindola | 94e3b38 | 2012-06-29 04:22:35 +0000 | [diff] [blame] | 7789 | |
| 7790 | Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset, |
| 7791 | MachinePointerInfo::getGOT(), false, false, false, |
| 7792 | 0); |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7793 | } |
| Lauro Ramos Venancio | 7d2cc2b | 2007-04-22 22:50:52 +0000 | [diff] [blame] | 7794 | |
| Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 7795 | // The address of the thread local variable is the add of the thread |
| 7796 | // pointer with the offset of the variable. |
| Dale Johannesen | 33c960f | 2009-02-04 20:06:27 +0000 | [diff] [blame] | 7797 | return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); |
| Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 7798 | } |
| 7799 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7800 | SDValue |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7801 | X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7802 | |
| Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 7803 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); |
| Chris Lattner | b903bed | 2009-06-26 21:20:29 +0000 | [diff] [blame] | 7804 | const GlobalValue *GV = GA->getGlobal(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 7805 | |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7806 | if (Subtarget->isTargetELF()) { |
| Chandler Carruth | 3479713 | 2012-04-08 17:20:55 +0000 | [diff] [blame] | 7807 | TLSModel::Model model = getTargetMachine().getTLSModel(GV); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7808 | |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7809 | switch (model) { |
| 7810 | case TLSModel::GeneralDynamic: |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7811 | if (Subtarget->is64Bit()) |
| 7812 | return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy()); |
| 7813 | return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy()); |
| Hans Wennborg | f0234fc | 2012-06-01 16:27:21 +0000 | [diff] [blame] | 7814 | case TLSModel::LocalDynamic: |
| 7815 | return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(), |
| 7816 | Subtarget->is64Bit()); |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7817 | case TLSModel::InitialExec: |
| 7818 | case TLSModel::LocalExec: |
| 7819 | return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, |
| Hans Wennborg | 228756c | 2012-05-11 10:11:01 +0000 | [diff] [blame] | 7820 | Subtarget->is64Bit(), |
| Craig Topper | b99bafe | 2013-01-21 06:21:54 +0000 | [diff] [blame] | 7821 | getTargetMachine().getRelocationModel() == Reloc::PIC_); |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7822 | } |
| Craig Topper | e8eb116 | 2012-04-23 03:26:18 +0000 | [diff] [blame] | 7823 | llvm_unreachable("Unknown TLS model."); |
| 7824 | } |
| 7825 | |
| 7826 | if (Subtarget->isTargetDarwin()) { |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7827 | // Darwin only has one model of TLS. Lower to that. |
| 7828 | unsigned char OpFlag = 0; |
| 7829 | unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ? |
| 7830 | X86ISD::WrapperRIP : X86ISD::Wrapper; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7831 | |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7832 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the |
| 7833 | // global base reg. |
| 7834 | bool PIC32 = (getTargetMachine().getRelocationModel() == Reloc::PIC_) && |
| 7835 | !Subtarget->is64Bit(); |
| 7836 | if (PIC32) |
| 7837 | OpFlag = X86II::MO_TLVP_PIC_BASE; |
| 7838 | else |
| 7839 | OpFlag = X86II::MO_TLVP; |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7840 | SDLoc DL(Op); |
| Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 7841 | SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL, |
| Eric Christopher | d8c0536 | 2010-12-09 06:25:53 +0000 | [diff] [blame] | 7842 | GA->getValueType(0), |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7843 | GA->getOffset(), OpFlag); |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7844 | SDValue Offset = DAG.getNode(WrapperKind, DL, getPointerTy(), Result); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7845 | |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7846 | // With PIC32, the address is actually $g + Offset. |
| 7847 | if (PIC32) |
| 7848 | Offset = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
| 7849 | DAG.getNode(X86ISD::GlobalBaseReg, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7850 | SDLoc(), getPointerTy()), |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7851 | Offset); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7852 | |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7853 | // Lowering the machine isd will make sure everything is in the right |
| 7854 | // location. |
| Eric Christopher | d8c0536 | 2010-12-09 06:25:53 +0000 | [diff] [blame] | 7855 | SDValue Chain = DAG.getEntryNode(); |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 7856 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
| Eric Christopher | d8c0536 | 2010-12-09 06:25:53 +0000 | [diff] [blame] | 7857 | SDValue Args[] = { Chain, Offset }; |
| 7858 | Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args, 2); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7859 | |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7860 | // TLSCALL will be codegen'ed as call. Inform MFI that function has calls. |
| 7861 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 7862 | MFI->setAdjustsStack(true); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 7863 | |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 7864 | // And our return value (tls address) is in the standard call return value |
| 7865 | // location. |
| Eric Christopher | d8c0536 | 2010-12-09 06:25:53 +0000 | [diff] [blame] | 7866 | unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX; |
| Evan Cheng | fd230df | 2011-10-19 22:22:54 +0000 | [diff] [blame] | 7867 | return DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy(), |
| 7868 | Chain.getValue(1)); |
| Craig Topper | e8eb116 | 2012-04-23 03:26:18 +0000 | [diff] [blame] | 7869 | } |
| 7870 | |
| Anton Korobeynikov | 2ee4e42 | 2013-03-18 08:12:28 +0000 | [diff] [blame] | 7871 | if (Subtarget->isTargetWindows() || Subtarget->isTargetMingw()) { |
| Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 7872 | // Just use the implicit TLS architecture |
| 7873 | // Need to generate someting similar to: |
| 7874 | // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage |
| 7875 | // ; from TEB |
| 7876 | // mov ecx, dword [rel _tls_index]: Load index (from C runtime) |
| 7877 | // mov rcx, qword [rdx+rcx*8] |
| 7878 | // mov eax, .tls$:tlsvar |
| 7879 | // [rax+rcx] contains the address |
| 7880 | // Windows 64bit: gs:0x58 |
| 7881 | // Windows 32bit: fs:__tls_array |
| 7882 | |
| 7883 | // If GV is an alias then use the aliasee for determining |
| 7884 | // thread-localness. |
| 7885 | if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV)) |
| 7886 | GV = GA->resolveAliasedGlobal(false); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7887 | SDLoc dl(GA); |
| Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 7888 | SDValue Chain = DAG.getEntryNode(); |
| 7889 | |
| 7890 | // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or |
| Anton Korobeynikov | 2ee4e42 | 2013-03-18 08:12:28 +0000 | [diff] [blame] | 7891 | // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly |
| 7892 | // use its literal value of 0x2C. |
| Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 7893 | Value *Ptr = Constant::getNullValue(Subtarget->is64Bit() |
| 7894 | ? Type::getInt8PtrTy(*DAG.getContext(), |
| 7895 | 256) |
| 7896 | : Type::getInt32PtrTy(*DAG.getContext(), |
| 7897 | 257)); |
| 7898 | |
| Anton Korobeynikov | 2ee4e42 | 2013-03-18 08:12:28 +0000 | [diff] [blame] | 7899 | SDValue TlsArray = Subtarget->is64Bit() ? DAG.getIntPtrConstant(0x58) : |
| 7900 | (Subtarget->isTargetMingw() ? DAG.getIntPtrConstant(0x2C) : |
| 7901 | DAG.getExternalSymbol("_tls_array", getPointerTy())); |
| 7902 | |
| 7903 | SDValue ThreadPointer = DAG.getLoad(getPointerTy(), dl, Chain, TlsArray, |
| Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 7904 | MachinePointerInfo(Ptr), |
| 7905 | false, false, false, 0); |
| 7906 | |
| 7907 | // Load the _tls_index variable |
| 7908 | SDValue IDX = DAG.getExternalSymbol("_tls_index", getPointerTy()); |
| 7909 | if (Subtarget->is64Bit()) |
| 7910 | IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, getPointerTy(), Chain, |
| 7911 | IDX, MachinePointerInfo(), MVT::i32, |
| 7912 | false, false, 0); |
| 7913 | else |
| 7914 | IDX = DAG.getLoad(getPointerTy(), dl, Chain, IDX, MachinePointerInfo(), |
| 7915 | false, false, false, 0); |
| 7916 | |
| Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 7917 | SDValue Scale = DAG.getConstant(Log2_64_Ceil(TD->getPointerSize()), |
| Craig Topper | 0fbf364 | 2012-04-23 03:28:34 +0000 | [diff] [blame] | 7918 | getPointerTy()); |
| Anton Korobeynikov | d4a19b6 | 2012-02-11 17:26:53 +0000 | [diff] [blame] | 7919 | IDX = DAG.getNode(ISD::SHL, dl, getPointerTy(), IDX, Scale); |
| 7920 | |
| 7921 | SDValue res = DAG.getNode(ISD::ADD, dl, getPointerTy(), ThreadPointer, IDX); |
| 7922 | res = DAG.getLoad(getPointerTy(), dl, Chain, res, MachinePointerInfo(), |
| 7923 | false, false, false, 0); |
| 7924 | |
| 7925 | // Get the offset of start of .tls section |
| 7926 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, |
| 7927 | GA->getValueType(0), |
| 7928 | GA->getOffset(), X86II::MO_SECREL); |
| 7929 | SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), TGA); |
| 7930 | |
| 7931 | // The address of the thread local variable is the add of the thread |
| 7932 | // pointer with the offset of the variable. |
| 7933 | return DAG.getNode(ISD::ADD, dl, getPointerTy(), res, Offset); |
| Anton Korobeynikov | 6625eff | 2008-05-04 21:36:32 +0000 | [diff] [blame] | 7934 | } |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 7935 | |
| David Blaikie | 4d6ccb5 | 2012-01-20 21:51:11 +0000 | [diff] [blame] | 7936 | llvm_unreachable("TLS not implemented for this target."); |
| Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 7937 | } |
| 7938 | |
| Chad Rosier | b90d2a9 | 2012-01-03 23:19:12 +0000 | [diff] [blame] | 7939 | /// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values |
| 7940 | /// and take a 2 x i32 value to shift plus a shift amount. |
| 7941 | SDValue X86TargetLowering::LowerShiftParts(SDValue Op, SelectionDAG &DAG) const{ |
| Dan Gohman | 4c1fa61 | 2008-03-03 22:22:09 +0000 | [diff] [blame] | 7942 | assert(Op.getNumOperands() == 3 && "Not a double-shift!"); |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7943 | EVT VT = Op.getValueType(); |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 7944 | unsigned VTBits = VT.getSizeInBits(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 7945 | SDLoc dl(Op); |
| Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 7946 | bool isSRA = Op.getOpcode() == ISD::SRA_PARTS; |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7947 | SDValue ShOpLo = Op.getOperand(0); |
| 7948 | SDValue ShOpHi = Op.getOperand(1); |
| 7949 | SDValue ShAmt = Op.getOperand(2); |
| Chris Lattner | 31dcfe6 | 2009-07-29 05:48:09 +0000 | [diff] [blame] | 7950 | SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7951 | DAG.getConstant(VTBits - 1, MVT::i8)) |
| Chris Lattner | 31dcfe6 | 2009-07-29 05:48:09 +0000 | [diff] [blame] | 7952 | : DAG.getConstant(0, VT); |
| Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 7953 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7954 | SDValue Tmp2, Tmp3; |
| Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 7955 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7956 | Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt); |
| 7957 | Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt); |
| Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 7958 | } else { |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7959 | Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt); |
| 7960 | Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt); |
| Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 7961 | } |
| Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 7962 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7963 | SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt, |
| 7964 | DAG.getConstant(VTBits, MVT::i8)); |
| Chris Lattner | ccfea35 | 2010-02-22 00:28:59 +0000 | [diff] [blame] | 7965 | SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7966 | AndNode, DAG.getConstant(0, MVT::i8)); |
| Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 7967 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7968 | SDValue Hi, Lo; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7969 | SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7970 | SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond }; |
| 7971 | SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond }; |
| Duncan Sands | f951620 | 2008-06-30 10:19:09 +0000 | [diff] [blame] | 7972 | |
| Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 7973 | if (Op.getOpcode() == ISD::SHL_PARTS) { |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7974 | Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4); |
| 7975 | Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4); |
| Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 7976 | } else { |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 7977 | Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4); |
| 7978 | Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4); |
| Chris Lattner | 2ff75ee | 2007-10-17 06:02:13 +0000 | [diff] [blame] | 7979 | } |
| 7980 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 7981 | SDValue Ops[2] = { Lo, Hi }; |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 7982 | return DAG.getMergeValues(Ops, array_lengthof(Ops), dl); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 7983 | } |
| Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 7984 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 7985 | SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, |
| 7986 | SelectionDAG &DAG) const { |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 7987 | EVT SrcVT = Op.getOperand(0).getValueType(); |
| Eli Friedman | 23ef105 | 2009-06-06 03:57:58 +0000 | [diff] [blame] | 7988 | |
| Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 7989 | if (SrcVT.isVector()) |
| Eli Friedman | 23ef105 | 2009-06-06 03:57:58 +0000 | [diff] [blame] | 7990 | return SDValue(); |
| Eli Friedman | 23ef105 | 2009-06-06 03:57:58 +0000 | [diff] [blame] | 7991 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7992 | assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 && |
| Chris Lattner | b09916b | 2008-02-27 05:57:41 +0000 | [diff] [blame] | 7993 | "Unknown SINT_TO_FP to lower!"); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 7994 | |
| Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 7995 | // These are really Legal; return the operand so the caller accepts it as |
| 7996 | // Legal. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7997 | if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType())) |
| Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 7998 | return Op; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 7999 | if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) && |
| Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 8000 | Subtarget->is64Bit()) { |
| 8001 | return Op; |
| 8002 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8003 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8004 | SDLoc dl(Op); |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8005 | unsigned Size = SrcVT.getSizeInBits()/8; |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8006 | MachineFunction &MF = DAG.getMachineFunction(); |
| David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 8007 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8008 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8009 | SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), |
| Bill Wendling | 105be5a | 2009-03-13 08:41:47 +0000 | [diff] [blame] | 8010 | StackSlot, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8011 | MachinePointerInfo::getFixedStack(SSFI), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 8012 | false, false, 0); |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 8013 | return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG); |
| 8014 | } |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8015 | |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8016 | SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain, |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 8017 | SDValue StackSlot, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8018 | SelectionDAG &DAG) const { |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8019 | // Build the FILD |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8020 | SDLoc DL(Op); |
| Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 8021 | SDVTList Tys; |
| Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 8022 | bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType()); |
| Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 8023 | if (useSSE) |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 8024 | Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue); |
| Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 8025 | else |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8026 | Tys = DAG.getVTList(Op.getValueType(), MVT::Other); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 8027 | |
| Chris Lattner | 492a43e | 2010-09-22 01:28:21 +0000 | [diff] [blame] | 8028 | unsigned ByteSize = SrcVT.getSizeInBits()/8; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 8029 | |
| Stuart Hastings | 84be958 | 2011-06-02 15:57:11 +0000 | [diff] [blame] | 8030 | FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot); |
| 8031 | MachineMemOperand *MMO; |
| 8032 | if (FI) { |
| 8033 | int SSFI = FI->getIndex(); |
| 8034 | MMO = |
| 8035 | DAG.getMachineFunction() |
| 8036 | .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI), |
| 8037 | MachineMemOperand::MOLoad, ByteSize, ByteSize); |
| 8038 | } else { |
| 8039 | MMO = cast<LoadSDNode>(StackSlot)->getMemOperand(); |
| 8040 | StackSlot = StackSlot.getOperand(1); |
| 8041 | } |
| Benjamin Kramer | 7f1a560 | 2009-12-29 16:57:26 +0000 | [diff] [blame] | 8042 | SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) }; |
| Chris Lattner | 492a43e | 2010-09-22 01:28:21 +0000 | [diff] [blame] | 8043 | SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG : |
| 8044 | X86ISD::FILD, DL, |
| 8045 | Tys, Ops, array_lengthof(Ops), |
| 8046 | SrcVT, MMO); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8047 | |
| Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 8048 | if (useSSE) { |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8049 | Chain = Result.getValue(1); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8050 | SDValue InFlag = Result.getValue(2); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8051 | |
| 8052 | // FIXME: Currently the FST is flagged to the FILD_FLAG. This |
| 8053 | // shouldn't be necessary except that RFP cannot be live across |
| 8054 | // multiple blocks. When stackifier is fixed, they can be uncoupled. |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 8055 | MachineFunction &MF = DAG.getMachineFunction(); |
| Bob Wilson | eafca4e | 2010-09-22 17:35:14 +0000 | [diff] [blame] | 8056 | unsigned SSFISize = Op.getValueType().getSizeInBits()/8; |
| 8057 | int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8058 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8059 | Tys = DAG.getVTList(MVT::Other); |
| Benjamin Kramer | 7f1a560 | 2009-12-29 16:57:26 +0000 | [diff] [blame] | 8060 | SDValue Ops[] = { |
| 8061 | Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag |
| 8062 | }; |
| Chris Lattner | 492a43e | 2010-09-22 01:28:21 +0000 | [diff] [blame] | 8063 | MachineMemOperand *MMO = |
| 8064 | DAG.getMachineFunction() |
| 8065 | .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI), |
| Bob Wilson | eafca4e | 2010-09-22 17:35:14 +0000 | [diff] [blame] | 8066 | MachineMemOperand::MOStore, SSFISize, SSFISize); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 8067 | |
| Chris Lattner | 492a43e | 2010-09-22 01:28:21 +0000 | [diff] [blame] | 8068 | Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys, |
| 8069 | Ops, array_lengthof(Ops), |
| 8070 | Op.getValueType(), MMO); |
| 8071 | Result = DAG.getLoad(Op.getValueType(), DL, Chain, StackSlot, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8072 | MachinePointerInfo::getFixedStack(SSFI), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8073 | false, false, false, 0); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 8074 | } |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 8075 | |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8076 | return Result; |
| 8077 | } |
| 8078 | |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8079 | // LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion. |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8080 | SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, |
| 8081 | SelectionDAG &DAG) const { |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8082 | // This algorithm is not obvious. Here it is what we're trying to output: |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8083 | /* |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8084 | movq %rax, %xmm0 |
| 8085 | punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U } |
| 8086 | subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 } |
| 8087 | #ifdef __SSE3__ |
| Chad Rosier | a20e1e7 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 8088 | haddpd %xmm0, %xmm0 |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8089 | #else |
| Chad Rosier | a20e1e7 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 8090 | pshufd $0x4e, %xmm0, %xmm1 |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8091 | addpd %xmm1, %xmm0 |
| 8092 | #endif |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8093 | */ |
| Dale Johannesen | 040225f | 2008-10-21 23:07:49 +0000 | [diff] [blame] | 8094 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8095 | SDLoc dl(Op); |
| Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 8096 | LLVMContext *Context = DAG.getContext(); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8097 | |
| Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 8098 | // Build some magic constants. |
| Chris Lattner | 7302d80 | 2012-02-06 21:56:39 +0000 | [diff] [blame] | 8099 | const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 }; |
| 8100 | Constant *C0 = ConstantDataVector::get(*Context, CV0); |
| Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 8101 | SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16); |
| Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 8102 | |
| Chris Lattner | 9748479 | 2012-01-25 09:56:22 +0000 | [diff] [blame] | 8103 | SmallVector<Constant*,2> CV1; |
| 8104 | CV1.push_back( |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8105 | ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble, |
| 8106 | APInt(64, 0x4330000000000000ULL)))); |
| Chris Lattner | 9748479 | 2012-01-25 09:56:22 +0000 | [diff] [blame] | 8107 | CV1.push_back( |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8108 | ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble, |
| 8109 | APInt(64, 0x4530000000000000ULL)))); |
| Chris Lattner | 9748479 | 2012-01-25 09:56:22 +0000 | [diff] [blame] | 8110 | Constant *C1 = ConstantVector::get(CV1); |
| Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 8111 | SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16); |
| Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 8112 | |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8113 | // Load the 64-bit value into an XMM register. |
| 8114 | SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, |
| 8115 | Op.getOperand(0)); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8116 | SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8117 | MachinePointerInfo::getConstantPool(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8118 | false, false, false, 16); |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8119 | SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, |
| 8120 | DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, XR1), |
| 8121 | CLod0); |
| 8122 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8123 | SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8124 | MachinePointerInfo::getConstantPool(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8125 | false, false, false, 16); |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8126 | SDValue XR2F = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Unpck1); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8127 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1); |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8128 | SDValue Result; |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8129 | |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 8130 | if (Subtarget->hasSSE3()) { |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8131 | // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'. |
| 8132 | Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub); |
| 8133 | } else { |
| 8134 | SDValue S2F = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Sub); |
| 8135 | SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32, |
| 8136 | S2F, 0x4E, DAG); |
| 8137 | Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64, |
| 8138 | DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Shuffle), |
| 8139 | Sub); |
| 8140 | } |
| 8141 | |
| 8142 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result, |
| Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 8143 | DAG.getIntPtrConstant(0)); |
| 8144 | } |
| 8145 | |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8146 | // LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion. |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8147 | SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, |
| 8148 | SelectionDAG &DAG) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8149 | SDLoc dl(Op); |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8150 | // FP constant to bias correct the final result. |
| 8151 | SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8152 | MVT::f64); |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8153 | |
| 8154 | // Load the 32-bit value into an XMM register. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8155 | SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, |
| Eli Friedman | 6cdc1f4 | 2011-08-02 18:38:35 +0000 | [diff] [blame] | 8156 | Op.getOperand(0)); |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8157 | |
| Eli Friedman | f370476 | 2011-08-29 21:15:46 +0000 | [diff] [blame] | 8158 | // Zero out the upper parts of the register. |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 8159 | Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG); |
| Eli Friedman | f370476 | 2011-08-29 21:15:46 +0000 | [diff] [blame] | 8160 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8161 | Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8162 | DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Load), |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8163 | DAG.getIntPtrConstant(0)); |
| 8164 | |
| 8165 | // Or the load with the bias. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8166 | SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8167 | DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8168 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8169 | MVT::v2f64, Load)), |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8170 | DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8171 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8172 | MVT::v2f64, Bias))); |
| 8173 | Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8174 | DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or), |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8175 | DAG.getIntPtrConstant(0)); |
| 8176 | |
| 8177 | // Subtract the bias. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8178 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias); |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8179 | |
| 8180 | // Handle final rounding. |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8181 | EVT DestVT = Op.getValueType(); |
| Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 8182 | |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 8183 | if (DestVT.bitsLT(MVT::f64)) |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8184 | return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub, |
| Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 8185 | DAG.getIntPtrConstant(0)); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 8186 | if (DestVT.bitsGT(MVT::f64)) |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8187 | return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub); |
| Bill Wendling | 030939c | 2009-01-17 07:40:19 +0000 | [diff] [blame] | 8188 | |
| 8189 | // Handle final rounding. |
| 8190 | return Sub; |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8191 | } |
| 8192 | |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8193 | SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op, |
| 8194 | SelectionDAG &DAG) const { |
| 8195 | SDValue N0 = Op.getOperand(0); |
| 8196 | EVT SVT = N0.getValueType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8197 | SDLoc dl(Op); |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8198 | |
| 8199 | assert((SVT == MVT::v4i8 || SVT == MVT::v4i16 || |
| 8200 | SVT == MVT::v8i8 || SVT == MVT::v8i16) && |
| 8201 | "Custom UINT_TO_FP is not supported!"); |
| 8202 | |
| Craig Topper | b99bafe | 2013-01-21 06:21:54 +0000 | [diff] [blame] | 8203 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, |
| 8204 | SVT.getVectorNumElements()); |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8205 | return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), |
| 8206 | DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0)); |
| 8207 | } |
| 8208 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8209 | SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, |
| 8210 | SelectionDAG &DAG) const { |
| Evan Cheng | a06ec9e | 2009-01-19 08:08:22 +0000 | [diff] [blame] | 8211 | SDValue N0 = Op.getOperand(0); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8212 | SDLoc dl(Op); |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8213 | |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8214 | if (Op.getValueType().isVector()) |
| 8215 | return lowerUINT_TO_FP_vec(Op, DAG); |
| 8216 | |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8217 | // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't |
| Evan Cheng | a06ec9e | 2009-01-19 08:08:22 +0000 | [diff] [blame] | 8218 | // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform |
| 8219 | // the optimization here. |
| 8220 | if (DAG.SignBitIsZero(N0)) |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8221 | return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0); |
| Evan Cheng | a06ec9e | 2009-01-19 08:08:22 +0000 | [diff] [blame] | 8222 | |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8223 | EVT SrcVT = N0.getValueType(); |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8224 | EVT DstVT = Op.getValueType(); |
| 8225 | if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64) |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8226 | return LowerUINT_TO_FP_i64(Op, DAG); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 8227 | if (SrcVT == MVT::i32 && X86ScalarSSEf64) |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8228 | return LowerUINT_TO_FP_i32(Op, DAG); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 8229 | if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32) |
| Bill Wendling | 397ae21 | 2012-01-05 02:13:20 +0000 | [diff] [blame] | 8230 | return SDValue(); |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 8231 | |
| 8232 | // Make a 64-bit buffer, and use it to build an FILD. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8233 | SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64); |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8234 | if (SrcVT == MVT::i32) { |
| 8235 | SDValue WordOff = DAG.getConstant(4, getPointerTy()); |
| 8236 | SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl, |
| 8237 | getPointerTy(), StackSlot, WordOff); |
| 8238 | SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 8239 | StackSlot, MachinePointerInfo(), |
| 8240 | false, false, 0); |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8241 | SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32), |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 8242 | OffsetSlot, MachinePointerInfo(), |
| 8243 | false, false, 0); |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8244 | SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG); |
| 8245 | return Fild; |
| 8246 | } |
| 8247 | |
| 8248 | assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP"); |
| 8249 | SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0), |
| Bill Wendling | f6c0747 | 2012-01-10 19:41:30 +0000 | [diff] [blame] | 8250 | StackSlot, MachinePointerInfo(), |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 8251 | false, false, 0); |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8252 | // For i64 source, we need to add the appropriate power of 2 if the input |
| 8253 | // was negative. This is the same as the optimization in |
| 8254 | // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here, |
| 8255 | // we must be careful to do the computation in x87 extended precision, not |
| 8256 | // in SSE. (The generic code can't know it's OK to do this, or how to.) |
| Chris Lattner | 492a43e | 2010-09-22 01:28:21 +0000 | [diff] [blame] | 8257 | int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex(); |
| 8258 | MachineMemOperand *MMO = |
| 8259 | DAG.getMachineFunction() |
| 8260 | .getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI), |
| 8261 | MachineMemOperand::MOLoad, 8, 8); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 8262 | |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8263 | SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other); |
| 8264 | SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) }; |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 8265 | SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, |
| 8266 | array_lengthof(Ops), MVT::i64, MMO); |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8267 | |
| 8268 | APInt FF(32, 0x5F800000ULL); |
| 8269 | |
| 8270 | // Check whether the sign bit is set. |
| Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 8271 | SDValue SignSet = DAG.getSetCC(dl, |
| 8272 | getSetCCResultType(*DAG.getContext(), MVT::i64), |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8273 | Op.getOperand(0), DAG.getConstant(0, MVT::i64), |
| 8274 | ISD::SETLT); |
| 8275 | |
| 8276 | // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits. |
| 8277 | SDValue FudgePtr = DAG.getConstantPool( |
| 8278 | ConstantInt::get(*DAG.getContext(), FF.zext(64)), |
| 8279 | getPointerTy()); |
| 8280 | |
| 8281 | // Get a pointer to FF if the sign bit was set, or to 0 otherwise. |
| 8282 | SDValue Zero = DAG.getIntPtrConstant(0); |
| 8283 | SDValue Four = DAG.getIntPtrConstant(4); |
| 8284 | SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet, |
| 8285 | Zero, Four); |
| 8286 | FudgePtr = DAG.getNode(ISD::ADD, dl, getPointerTy(), FudgePtr, Offset); |
| 8287 | |
| 8288 | // Load the value out, extending it from f32 to f80. |
| 8289 | // FIXME: Avoid the extend by constructing the right constant pool? |
| Stuart Hastings | a901129 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 8290 | SDValue Fudge = DAG.getExtLoad(ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(), |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8291 | FudgePtr, MachinePointerInfo::getConstantPool(), |
| 8292 | MVT::f32, false, false, 4); |
| Dale Johannesen | 8d908eb | 2010-05-15 18:51:12 +0000 | [diff] [blame] | 8293 | // Extend everything to 80 bits to force it to be done on x87. |
| 8294 | SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge); |
| 8295 | return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, DAG.getIntPtrConstant(0)); |
| Bill Wendling | 8b8a636 | 2009-01-17 03:56:04 +0000 | [diff] [blame] | 8296 | } |
| 8297 | |
| Craig Topper | b99bafe | 2013-01-21 06:21:54 +0000 | [diff] [blame] | 8298 | std::pair<SDValue,SDValue> |
| 8299 | X86TargetLowering:: FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, |
| 8300 | bool IsSigned, bool IsReplace) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8301 | SDLoc DL(Op); |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 8302 | |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 8303 | EVT DstTy = Op.getValueType(); |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 8304 | |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8305 | if (!IsSigned && !isIntegerTypeFTOL(DstTy)) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8306 | assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT"); |
| 8307 | DstTy = MVT::i64; |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 8308 | } |
| 8309 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8310 | assert(DstTy.getSimpleVT() <= MVT::i64 && |
| 8311 | DstTy.getSimpleVT() >= MVT::i16 && |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8312 | "Unknown FP_TO_INT to lower!"); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8313 | |
| Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 8314 | // These are really Legal. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8315 | if (DstTy == MVT::i32 && |
| Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 8316 | isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8317 | return std::make_pair(SDValue(), SDValue()); |
| Dale Johannesen | 73328d1 | 2007-09-19 23:55:34 +0000 | [diff] [blame] | 8318 | if (Subtarget->is64Bit() && |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8319 | DstTy == MVT::i64 && |
| Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 8320 | isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8321 | return std::make_pair(SDValue(), SDValue()); |
| Dale Johannesen | 9e3d3ab | 2007-09-14 22:26:36 +0000 | [diff] [blame] | 8322 | |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8323 | // We lower FP->int64 either into FISTP64 followed by a load from a temporary |
| 8324 | // stack slot, or into the FTOL runtime function. |
| Evan Cheng | 87c8935 | 2007-10-15 20:11:21 +0000 | [diff] [blame] | 8325 | MachineFunction &MF = DAG.getMachineFunction(); |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 8326 | unsigned MemSize = DstTy.getSizeInBits()/8; |
| David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 8327 | int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8328 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 8329 | |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8330 | unsigned Opc; |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8331 | if (!IsSigned && isIntegerTypeFTOL(DstTy)) |
| 8332 | Opc = X86ISD::WIN_FTOL; |
| 8333 | else |
| 8334 | switch (DstTy.getSimpleVT().SimpleTy) { |
| 8335 | default: llvm_unreachable("Invalid FP_TO_SINT to lower!"); |
| 8336 | case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; |
| 8337 | case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; |
| 8338 | case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; |
| 8339 | } |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 8340 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8341 | SDValue Chain = DAG.getEntryNode(); |
| 8342 | SDValue Value = Op.getOperand(0); |
| Chris Lattner | 492a43e | 2010-09-22 01:28:21 +0000 | [diff] [blame] | 8343 | EVT TheVT = Op.getOperand(0).getValueType(); |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8344 | // FIXME This causes a redundant load/store if the SSE-class value is already |
| 8345 | // in memory, such as if it is on the callstack. |
| Chris Lattner | 492a43e | 2010-09-22 01:28:21 +0000 | [diff] [blame] | 8346 | if (isScalarFPTypeInSSEReg(TheVT)) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8347 | assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!"); |
| Chris Lattner | 0729093 | 2010-09-22 01:05:16 +0000 | [diff] [blame] | 8348 | Chain = DAG.getStore(Chain, DL, Value, StackSlot, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8349 | MachinePointerInfo::getFixedStack(SSFI), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 8350 | false, false, 0); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8351 | SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8352 | SDValue Ops[] = { |
| Chris Lattner | 492a43e | 2010-09-22 01:28:21 +0000 | [diff] [blame] | 8353 | Chain, StackSlot, DAG.getValueType(TheVT) |
| Chris Lattner | 5a88b83 | 2007-02-25 07:10:00 +0000 | [diff] [blame] | 8354 | }; |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 8355 | |
| Chris Lattner | 492a43e | 2010-09-22 01:28:21 +0000 | [diff] [blame] | 8356 | MachineMemOperand *MMO = |
| 8357 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI), |
| 8358 | MachineMemOperand::MOLoad, MemSize, MemSize); |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 8359 | Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, |
| 8360 | array_lengthof(Ops), DstTy, MMO); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8361 | Chain = Value.getValue(1); |
| David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 8362 | SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8363 | StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 8364 | } |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 8365 | |
| Chris Lattner | 0729093 | 2010-09-22 01:05:16 +0000 | [diff] [blame] | 8366 | MachineMemOperand *MMO = |
| 8367 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI), |
| 8368 | MachineMemOperand::MOStore, MemSize, MemSize); |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 8369 | |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8370 | if (Opc != X86ISD::WIN_FTOL) { |
| 8371 | // Build the FP_TO_INT*_IN_MEM |
| 8372 | SDValue Ops[] = { Chain, Value, StackSlot }; |
| 8373 | SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other), |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 8374 | Ops, array_lengthof(Ops), DstTy, |
| 8375 | MMO); |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8376 | return std::make_pair(FIST, StackSlot); |
| 8377 | } else { |
| 8378 | SDValue ftol = DAG.getNode(X86ISD::WIN_FTOL, DL, |
| 8379 | DAG.getVTList(MVT::Other, MVT::Glue), |
| 8380 | Chain, Value); |
| 8381 | SDValue eax = DAG.getCopyFromReg(ftol, DL, X86::EAX, |
| 8382 | MVT::i32, ftol.getValue(1)); |
| 8383 | SDValue edx = DAG.getCopyFromReg(eax.getValue(1), DL, X86::EDX, |
| 8384 | MVT::i32, eax.getValue(2)); |
| NAKAMURA Takumi | 9a68fdc | 2012-02-25 03:37:25 +0000 | [diff] [blame] | 8385 | SDValue Ops[] = { eax, edx }; |
| 8386 | SDValue pair = IsReplace |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 8387 | ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops, array_lengthof(Ops)) |
| 8388 | : DAG.getMergeValues(Ops, array_lengthof(Ops), DL); |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8389 | return std::make_pair(pair, SDValue()); |
| 8390 | } |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8391 | } |
| 8392 | |
| Nadav Rotem | 0509db2 | 2012-12-28 05:45:24 +0000 | [diff] [blame] | 8393 | static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG, |
| 8394 | const X86Subtarget *Subtarget) { |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8395 | MVT VT = Op->getValueType(0).getSimpleVT(); |
| Nadav Rotem | 0509db2 | 2012-12-28 05:45:24 +0000 | [diff] [blame] | 8396 | SDValue In = Op->getOperand(0); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8397 | MVT InVT = In.getValueType().getSimpleVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8398 | SDLoc dl(Op); |
| Nadav Rotem | 0509db2 | 2012-12-28 05:45:24 +0000 | [diff] [blame] | 8399 | |
| 8400 | // Optimize vectors in AVX mode: |
| 8401 | // |
| 8402 | // v8i16 -> v8i32 |
| 8403 | // Use vpunpcklwd for 4 lower elements v8i16 -> v4i32. |
| 8404 | // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32. |
| 8405 | // Concat upper and lower parts. |
| 8406 | // |
| 8407 | // v4i32 -> v4i64 |
| 8408 | // Use vpunpckldq for 4 lower elements v4i32 -> v2i64. |
| 8409 | // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64. |
| 8410 | // Concat upper and lower parts. |
| 8411 | // |
| 8412 | |
| 8413 | if (((VT != MVT::v8i32) || (InVT != MVT::v8i16)) && |
| 8414 | ((VT != MVT::v4i64) || (InVT != MVT::v4i32))) |
| 8415 | return SDValue(); |
| 8416 | |
| 8417 | if (Subtarget->hasInt256()) |
| 8418 | return DAG.getNode(X86ISD::VZEXT_MOVL, dl, VT, In); |
| 8419 | |
| 8420 | SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl); |
| 8421 | SDValue Undef = DAG.getUNDEF(InVT); |
| 8422 | bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND; |
| 8423 | SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef); |
| 8424 | SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef); |
| 8425 | |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8426 | MVT HVT = MVT::getVectorVT(VT.getVectorElementType(), |
| Nadav Rotem | 0509db2 | 2012-12-28 05:45:24 +0000 | [diff] [blame] | 8427 | VT.getVectorNumElements()/2); |
| 8428 | |
| 8429 | OpLo = DAG.getNode(ISD::BITCAST, dl, HVT, OpLo); |
| 8430 | OpHi = DAG.getNode(ISD::BITCAST, dl, HVT, OpHi); |
| 8431 | |
| 8432 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi); |
| 8433 | } |
| 8434 | |
| 8435 | SDValue X86TargetLowering::LowerANY_EXTEND(SDValue Op, |
| 8436 | SelectionDAG &DAG) const { |
| 8437 | if (Subtarget->hasFp256()) { |
| 8438 | SDValue Res = LowerAVXExtend(Op, DAG, Subtarget); |
| 8439 | if (Res.getNode()) |
| 8440 | return Res; |
| 8441 | } |
| 8442 | |
| 8443 | return SDValue(); |
| 8444 | } |
| Nadav Rotem | 40ef8b7 | 2012-12-28 07:28:43 +0000 | [diff] [blame] | 8445 | SDValue X86TargetLowering::LowerZERO_EXTEND(SDValue Op, |
| 8446 | SelectionDAG &DAG) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8447 | SDLoc DL(Op); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8448 | MVT VT = Op.getValueType().getSimpleVT(); |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8449 | SDValue In = Op.getOperand(0); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8450 | MVT SVT = In.getValueType().getSimpleVT(); |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8451 | |
| Nadav Rotem | 0509db2 | 2012-12-28 05:45:24 +0000 | [diff] [blame] | 8452 | if (Subtarget->hasFp256()) { |
| 8453 | SDValue Res = LowerAVXExtend(Op, DAG, Subtarget); |
| 8454 | if (Res.getNode()) |
| 8455 | return Res; |
| 8456 | } |
| 8457 | |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8458 | if (!VT.is256BitVector() || !SVT.is128BitVector() || |
| 8459 | VT.getVectorNumElements() != SVT.getVectorNumElements()) |
| 8460 | return SDValue(); |
| 8461 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 8462 | assert(Subtarget->hasFp256() && "256-bit vector is observed without AVX!"); |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8463 | |
| 8464 | // AVX2 has better support of integer extending. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 8465 | if (Subtarget->hasInt256()) |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8466 | return DAG.getNode(X86ISD::VZEXT, DL, VT, In); |
| 8467 | |
| 8468 | SDValue Lo = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, In); |
| 8469 | static const int Mask[] = {4, 5, 6, 7, -1, -1, -1, -1}; |
| 8470 | SDValue Hi = DAG.getNode(X86ISD::VZEXT, DL, MVT::v4i32, |
| Nadav Rotem | 40ef8b7 | 2012-12-28 07:28:43 +0000 | [diff] [blame] | 8471 | DAG.getVectorShuffle(MVT::v8i16, DL, In, |
| 8472 | DAG.getUNDEF(MVT::v8i16), |
| 8473 | &Mask[0])); |
| Michael Liao | a755463 | 2012-10-23 17:36:08 +0000 | [diff] [blame] | 8474 | |
| 8475 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i32, Lo, Hi); |
| 8476 | } |
| 8477 | |
| Craig Topper | d713c0f | 2013-01-20 21:34:37 +0000 | [diff] [blame] | 8478 | SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8479 | SDLoc DL(Op); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8480 | MVT VT = Op.getValueType().getSimpleVT(); |
| Nadav Rotem | 3c22a44 | 2012-12-27 07:45:10 +0000 | [diff] [blame] | 8481 | SDValue In = Op.getOperand(0); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8482 | MVT SVT = In.getValueType().getSimpleVT(); |
| Michael Liao | bedcbd4 | 2012-10-16 18:14:11 +0000 | [diff] [blame] | 8483 | |
| Nadav Rotem | 3c22a44 | 2012-12-27 07:45:10 +0000 | [diff] [blame] | 8484 | if ((VT == MVT::v4i32) && (SVT == MVT::v4i64)) { |
| 8485 | // On AVX2, v4i64 -> v4i32 becomes VPERMD. |
| 8486 | if (Subtarget->hasInt256()) { |
| 8487 | static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1}; |
| 8488 | In = DAG.getNode(ISD::BITCAST, DL, MVT::v8i32, In); |
| 8489 | In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32), |
| 8490 | ShufMask); |
| 8491 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In, |
| 8492 | DAG.getIntPtrConstant(0)); |
| 8493 | } |
| 8494 | |
| 8495 | // On AVX, v4i64 -> v4i32 becomes a sequence that uses PSHUFD and MOVLHPS. |
| 8496 | SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In, |
| 8497 | DAG.getIntPtrConstant(0)); |
| 8498 | SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In, |
| 8499 | DAG.getIntPtrConstant(2)); |
| 8500 | |
| 8501 | OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo); |
| 8502 | OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi); |
| 8503 | |
| 8504 | // The PSHUFD mask: |
| 8505 | static const int ShufMask1[] = {0, 2, 0, 0}; |
| 8506 | SDValue Undef = DAG.getUNDEF(VT); |
| 8507 | OpLo = DAG.getVectorShuffle(VT, DL, OpLo, Undef, ShufMask1); |
| 8508 | OpHi = DAG.getVectorShuffle(VT, DL, OpHi, Undef, ShufMask1); |
| 8509 | |
| 8510 | // The MOVLHPS mask: |
| 8511 | static const int ShufMask2[] = {0, 1, 4, 5}; |
| 8512 | return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask2); |
| 8513 | } |
| 8514 | |
| 8515 | if ((VT == MVT::v8i16) && (SVT == MVT::v8i32)) { |
| 8516 | // On AVX2, v8i32 -> v8i16 becomed PSHUFB. |
| 8517 | if (Subtarget->hasInt256()) { |
| 8518 | In = DAG.getNode(ISD::BITCAST, DL, MVT::v32i8, In); |
| 8519 | |
| 8520 | SmallVector<SDValue,32> pshufbMask; |
| 8521 | for (unsigned i = 0; i < 2; ++i) { |
| 8522 | pshufbMask.push_back(DAG.getConstant(0x0, MVT::i8)); |
| 8523 | pshufbMask.push_back(DAG.getConstant(0x1, MVT::i8)); |
| 8524 | pshufbMask.push_back(DAG.getConstant(0x4, MVT::i8)); |
| 8525 | pshufbMask.push_back(DAG.getConstant(0x5, MVT::i8)); |
| 8526 | pshufbMask.push_back(DAG.getConstant(0x8, MVT::i8)); |
| 8527 | pshufbMask.push_back(DAG.getConstant(0x9, MVT::i8)); |
| 8528 | pshufbMask.push_back(DAG.getConstant(0xc, MVT::i8)); |
| 8529 | pshufbMask.push_back(DAG.getConstant(0xd, MVT::i8)); |
| 8530 | for (unsigned j = 0; j < 8; ++j) |
| 8531 | pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8)); |
| 8532 | } |
| 8533 | SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8, |
| 8534 | &pshufbMask[0], 32); |
| 8535 | In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV); |
| 8536 | In = DAG.getNode(ISD::BITCAST, DL, MVT::v4i64, In); |
| 8537 | |
| 8538 | static const int ShufMask[] = {0, 2, -1, -1}; |
| 8539 | In = DAG.getVectorShuffle(MVT::v4i64, DL, In, DAG.getUNDEF(MVT::v4i64), |
| 8540 | &ShufMask[0]); |
| 8541 | In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In, |
| 8542 | DAG.getIntPtrConstant(0)); |
| 8543 | return DAG.getNode(ISD::BITCAST, DL, VT, In); |
| 8544 | } |
| 8545 | |
| 8546 | SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In, |
| 8547 | DAG.getIntPtrConstant(0)); |
| 8548 | |
| 8549 | SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In, |
| 8550 | DAG.getIntPtrConstant(4)); |
| 8551 | |
| 8552 | OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpLo); |
| 8553 | OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, OpHi); |
| 8554 | |
| 8555 | // The PSHUFB mask: |
| 8556 | static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13, |
| 8557 | -1, -1, -1, -1, -1, -1, -1, -1}; |
| 8558 | |
| 8559 | SDValue Undef = DAG.getUNDEF(MVT::v16i8); |
| 8560 | OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1); |
| 8561 | OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1); |
| 8562 | |
| 8563 | OpLo = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpLo); |
| 8564 | OpHi = DAG.getNode(ISD::BITCAST, DL, MVT::v4i32, OpHi); |
| 8565 | |
| 8566 | // The MOVLHPS Mask: |
| 8567 | static const int ShufMask2[] = {0, 1, 4, 5}; |
| 8568 | SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2); |
| 8569 | return DAG.getNode(ISD::BITCAST, DL, MVT::v8i16, res); |
| 8570 | } |
| 8571 | |
| 8572 | // Handle truncation of V256 to V128 using shuffles. |
| 8573 | if (!VT.is128BitVector() || !SVT.is256BitVector()) |
| Michael Liao | bedcbd4 | 2012-10-16 18:14:11 +0000 | [diff] [blame] | 8574 | return SDValue(); |
| 8575 | |
| Nadav Rotem | 3c22a44 | 2012-12-27 07:45:10 +0000 | [diff] [blame] | 8576 | assert(VT.getVectorNumElements() != SVT.getVectorNumElements() && |
| 8577 | "Invalid op"); |
| 8578 | assert(Subtarget->hasFp256() && "256-bit vector without AVX!"); |
| Michael Liao | bedcbd4 | 2012-10-16 18:14:11 +0000 | [diff] [blame] | 8579 | |
| 8580 | unsigned NumElems = VT.getVectorNumElements(); |
| 8581 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), VT.getVectorElementType(), |
| 8582 | NumElems * 2); |
| 8583 | |
| Michael Liao | bedcbd4 | 2012-10-16 18:14:11 +0000 | [diff] [blame] | 8584 | SmallVector<int, 16> MaskVec(NumElems * 2, -1); |
| 8585 | // Prepare truncation shuffle mask |
| 8586 | for (unsigned i = 0; i != NumElems; ++i) |
| 8587 | MaskVec[i] = i * 2; |
| 8588 | SDValue V = DAG.getVectorShuffle(NVT, DL, |
| 8589 | DAG.getNode(ISD::BITCAST, DL, NVT, In), |
| 8590 | DAG.getUNDEF(NVT), &MaskVec[0]); |
| 8591 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V, |
| 8592 | DAG.getIntPtrConstant(0)); |
| 8593 | } |
| 8594 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8595 | SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, |
| 8596 | SelectionDAG &DAG) const { |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8597 | MVT VT = Op.getValueType().getSimpleVT(); |
| 8598 | if (VT.isVector()) { |
| 8599 | if (VT == MVT::v8i16) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8600 | return DAG.getNode(ISD::TRUNCATE, SDLoc(Op), VT, |
| 8601 | DAG.getNode(ISD::FP_TO_SINT, SDLoc(Op), |
| Michael Liao | bedcbd4 | 2012-10-16 18:14:11 +0000 | [diff] [blame] | 8602 | MVT::v8i32, Op.getOperand(0))); |
| Eli Friedman | 23ef105 | 2009-06-06 03:57:58 +0000 | [diff] [blame] | 8603 | return SDValue(); |
| Michael Liao | bedcbd4 | 2012-10-16 18:14:11 +0000 | [diff] [blame] | 8604 | } |
| Eli Friedman | 23ef105 | 2009-06-06 03:57:58 +0000 | [diff] [blame] | 8605 | |
| NAKAMURA Takumi | 9a68fdc | 2012-02-25 03:37:25 +0000 | [diff] [blame] | 8606 | std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, |
| 8607 | /*IsSigned=*/ true, /*IsReplace=*/ false); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8608 | SDValue FIST = Vals.first, StackSlot = Vals.second; |
| Eli Friedman | 36df499 | 2009-05-27 00:47:34 +0000 | [diff] [blame] | 8609 | // If FP_TO_INTHelper failed, the node is actually supposed to be Legal. |
| 8610 | if (FIST.getNode() == 0) return Op; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 8611 | |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8612 | if (StackSlot.getNode()) |
| 8613 | // Load the result. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8614 | return DAG.getLoad(Op.getValueType(), SDLoc(Op), |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 8615 | FIST, StackSlot, MachinePointerInfo(), |
| 8616 | false, false, false, 0); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 8617 | |
| 8618 | // The node is the result. |
| 8619 | return FIST; |
| Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 8620 | } |
| 8621 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8622 | SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, |
| 8623 | SelectionDAG &DAG) const { |
| NAKAMURA Takumi | 9a68fdc | 2012-02-25 03:37:25 +0000 | [diff] [blame] | 8624 | std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, |
| 8625 | /*IsSigned=*/ false, /*IsReplace=*/ false); |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 8626 | SDValue FIST = Vals.first, StackSlot = Vals.second; |
| 8627 | assert(FIST.getNode() && "Unexpected failure"); |
| 8628 | |
| NAKAMURA Takumi | 9a68fdc | 2012-02-25 03:37:25 +0000 | [diff] [blame] | 8629 | if (StackSlot.getNode()) |
| 8630 | // Load the result. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8631 | return DAG.getLoad(Op.getValueType(), SDLoc(Op), |
| NAKAMURA Takumi | 9a68fdc | 2012-02-25 03:37:25 +0000 | [diff] [blame] | 8632 | FIST, StackSlot, MachinePointerInfo(), |
| 8633 | false, false, false, 0); |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 8634 | |
| 8635 | // The node is the result. |
| 8636 | return FIST; |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 8637 | } |
| 8638 | |
| Craig Topper | b84b423 | 2013-01-21 06:13:28 +0000 | [diff] [blame] | 8639 | static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8640 | SDLoc DL(Op); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8641 | MVT VT = Op.getValueType().getSimpleVT(); |
| Michael Liao | 9d796db | 2012-10-10 16:32:15 +0000 | [diff] [blame] | 8642 | SDValue In = Op.getOperand(0); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8643 | MVT SVT = In.getValueType().getSimpleVT(); |
| Michael Liao | 9d796db | 2012-10-10 16:32:15 +0000 | [diff] [blame] | 8644 | |
| 8645 | assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!"); |
| 8646 | |
| 8647 | return DAG.getNode(X86ISD::VFPEXT, DL, VT, |
| 8648 | DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32, |
| 8649 | In, DAG.getUNDEF(SVT))); |
| 8650 | } |
| 8651 | |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 8652 | SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) const { |
| Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 8653 | LLVMContext *Context = DAG.getContext(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8654 | SDLoc dl(Op); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8655 | MVT VT = Op.getValueType().getSimpleVT(); |
| 8656 | MVT EltVT = VT; |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 8657 | unsigned NumElts = VT == MVT::f64 ? 2 : 4; |
| 8658 | if (VT.isVector()) { |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8659 | EltVT = VT.getVectorElementType(); |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 8660 | NumElts = VT.getVectorNumElements(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8661 | } |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 8662 | Constant *C; |
| 8663 | if (EltVT == MVT::f64) |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8664 | C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble, |
| 8665 | APInt(64, ~(1ULL << 63)))); |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 8666 | else |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8667 | C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle, |
| 8668 | APInt(32, ~(1U << 31)))); |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 8669 | C = ConstantVector::getSplat(NumElts, C); |
| 8670 | SDValue CPIdx = DAG.getConstantPool(C, getPointerTy()); |
| 8671 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8672 | SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8673 | MachinePointerInfo::getConstantPool(), |
| Craig Topper | 4362067 | 2012-09-08 07:31:51 +0000 | [diff] [blame] | 8674 | false, false, false, Alignment); |
| 8675 | if (VT.isVector()) { |
| 8676 | MVT ANDVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64; |
| 8677 | return DAG.getNode(ISD::BITCAST, dl, VT, |
| 8678 | DAG.getNode(ISD::AND, dl, ANDVT, |
| 8679 | DAG.getNode(ISD::BITCAST, dl, ANDVT, |
| 8680 | Op.getOperand(0)), |
| 8681 | DAG.getNode(ISD::BITCAST, dl, ANDVT, Mask))); |
| 8682 | } |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8683 | return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8684 | } |
| 8685 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8686 | SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) const { |
| Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 8687 | LLVMContext *Context = DAG.getContext(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8688 | SDLoc dl(Op); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8689 | MVT VT = Op.getValueType().getSimpleVT(); |
| 8690 | MVT EltVT = VT; |
| Chad Rosier | a860b18 | 2011-12-15 01:02:25 +0000 | [diff] [blame] | 8691 | unsigned NumElts = VT == MVT::f64 ? 2 : 4; |
| 8692 | if (VT.isVector()) { |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8693 | EltVT = VT.getVectorElementType(); |
| Chad Rosier | a860b18 | 2011-12-15 01:02:25 +0000 | [diff] [blame] | 8694 | NumElts = VT.getVectorNumElements(); |
| 8695 | } |
| Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 8696 | Constant *C; |
| 8697 | if (EltVT == MVT::f64) |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8698 | C = ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble, |
| 8699 | APInt(64, 1ULL << 63))); |
| Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 8700 | else |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8701 | C = ConstantFP::get(*Context, APFloat(APFloat::IEEEsingle, |
| 8702 | APInt(32, 1U << 31))); |
| Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 8703 | C = ConstantVector::getSplat(NumElts, C); |
| Craig Topper | cacd9d6 | 2012-09-08 07:46:05 +0000 | [diff] [blame] | 8704 | SDValue CPIdx = DAG.getConstantPool(C, getPointerTy()); |
| 8705 | unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment(); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8706 | SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8707 | MachinePointerInfo::getConstantPool(), |
| Craig Topper | cacd9d6 | 2012-09-08 07:46:05 +0000 | [diff] [blame] | 8708 | false, false, false, Alignment); |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 8709 | if (VT.isVector()) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 8710 | MVT XORVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64; |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8711 | return DAG.getNode(ISD::BITCAST, dl, VT, |
| Chad Rosier | a860b18 | 2011-12-15 01:02:25 +0000 | [diff] [blame] | 8712 | DAG.getNode(ISD::XOR, dl, XORVT, |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 8713 | DAG.getNode(ISD::BITCAST, dl, XORVT, |
| 8714 | Op.getOperand(0)), |
| 8715 | DAG.getNode(ISD::BITCAST, dl, XORVT, Mask))); |
| Evan Cheng | d4d01b7 | 2007-07-19 23:36:01 +0000 | [diff] [blame] | 8716 | } |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 8717 | |
| 8718 | return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 8719 | } |
| 8720 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 8721 | SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const { |
| Owen Anderson | a90b3dc | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 8722 | LLVMContext *Context = DAG.getContext(); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 8723 | SDValue Op0 = Op.getOperand(0); |
| 8724 | SDValue Op1 = Op.getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8725 | SDLoc dl(Op); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8726 | MVT VT = Op.getValueType().getSimpleVT(); |
| 8727 | MVT SrcVT = Op1.getValueType().getSimpleVT(); |
| Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 8728 | |
| 8729 | // If second operand is smaller, extend it first. |
| Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 8730 | if (SrcVT.bitsLT(VT)) { |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8731 | Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1); |
| Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 8732 | SrcVT = VT; |
| 8733 | } |
| Dale Johannesen | 61c7ef3 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 8734 | // And if it is bigger, shrink it first. |
| Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 8735 | if (SrcVT.bitsGT(VT)) { |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8736 | Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1)); |
| Dale Johannesen | 61c7ef3 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 8737 | SrcVT = VT; |
| Dale Johannesen | 61c7ef3 | 2007-10-21 01:07:44 +0000 | [diff] [blame] | 8738 | } |
| 8739 | |
| 8740 | // At this point the operands and the result should have the same |
| 8741 | // type, and that won't be f80 since that is not custom lowered. |
| Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 8742 | |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 8743 | // First get the sign bit of second operand. |
| Chad Rosier | 01d426e | 2011-12-15 01:16:09 +0000 | [diff] [blame] | 8744 | SmallVector<Constant*,4> CV; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8745 | if (SrcVT == MVT::f64) { |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8746 | const fltSemantics &Sem = APFloat::IEEEdouble; |
| 8747 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 1ULL << 63)))); |
| 8748 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0)))); |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 8749 | } else { |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8750 | const fltSemantics &Sem = APFloat::IEEEsingle; |
| 8751 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 1U << 31)))); |
| 8752 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0)))); |
| 8753 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0)))); |
| 8754 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0)))); |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 8755 | } |
| Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 8756 | Constant *C = ConstantVector::get(CV); |
| Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 8757 | SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8758 | SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8759 | MachinePointerInfo::getConstantPool(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8760 | false, false, false, 16); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8761 | SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1); |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 8762 | |
| 8763 | // Shift sign bit right or left if the two operands have different types. |
| Duncan Sands | 8e4eb09 | 2008-06-08 20:54:56 +0000 | [diff] [blame] | 8764 | if (SrcVT.bitsGT(VT)) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8765 | // Op0 is MVT::f32, Op1 is MVT::f64. |
| 8766 | SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit); |
| 8767 | SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit, |
| 8768 | DAG.getConstant(32, MVT::i32)); |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 8769 | SignBit = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, SignBit); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8770 | SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit, |
| Chris Lattner | 0bd4893 | 2008-01-17 07:00:52 +0000 | [diff] [blame] | 8771 | DAG.getIntPtrConstant(0)); |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 8772 | } |
| 8773 | |
| Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 8774 | // Clear first operand sign bit. |
| 8775 | CV.clear(); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 8776 | if (VT == MVT::f64) { |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8777 | const fltSemantics &Sem = APFloat::IEEEdouble; |
| 8778 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, |
| 8779 | APInt(64, ~(1ULL << 63))))); |
| 8780 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(64, 0)))); |
| Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 8781 | } else { |
| Tim Northover | 0a29cb0 | 2013-01-22 09:46:31 +0000 | [diff] [blame] | 8782 | const fltSemantics &Sem = APFloat::IEEEsingle; |
| 8783 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, |
| 8784 | APInt(32, ~(1U << 31))))); |
| 8785 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0)))); |
| 8786 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0)))); |
| 8787 | CV.push_back(ConstantFP::get(*Context, APFloat(Sem, APInt(32, 0)))); |
| Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 8788 | } |
| Owen Anderson | af7ec97 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 8789 | C = ConstantVector::get(CV); |
| Evan Cheng | 1606e8e | 2009-03-13 07:51:59 +0000 | [diff] [blame] | 8790 | CPIdx = DAG.getConstantPool(C, getPointerTy(), 16); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8791 | SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx, |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 8792 | MachinePointerInfo::getConstantPool(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 8793 | false, false, false, 16); |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8794 | SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2); |
| Evan Cheng | 73d6cf1 | 2007-01-05 21:37:56 +0000 | [diff] [blame] | 8795 | |
| 8796 | // Or the value with the sign bit. |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 8797 | return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit); |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 8798 | } |
| 8799 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 8800 | static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) { |
| Stuart Hastings | 4fd0dee | 2011-06-01 04:39:42 +0000 | [diff] [blame] | 8801 | SDValue N0 = Op.getOperand(0); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8802 | SDLoc dl(Op); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 8803 | MVT VT = Op.getValueType().getSimpleVT(); |
| Stuart Hastings | 4fd0dee | 2011-06-01 04:39:42 +0000 | [diff] [blame] | 8804 | |
| 8805 | // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1). |
| 8806 | SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0, |
| 8807 | DAG.getConstant(1, VT)); |
| 8808 | return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, VT)); |
| 8809 | } |
| 8810 | |
| Michael Liao | f966e4e | 2012-09-13 20:24:54 +0000 | [diff] [blame] | 8811 | // LowerVectorAllZeroTest - Check whether an OR'd tree is PTEST-able. |
| 8812 | // |
| Craig Topper | b99bafe | 2013-01-21 06:21:54 +0000 | [diff] [blame] | 8813 | SDValue X86TargetLowering::LowerVectorAllZeroTest(SDValue Op, |
| 8814 | SelectionDAG &DAG) const { |
| Michael Liao | f966e4e | 2012-09-13 20:24:54 +0000 | [diff] [blame] | 8815 | assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree."); |
| 8816 | |
| 8817 | if (!Subtarget->hasSSE41()) |
| 8818 | return SDValue(); |
| 8819 | |
| 8820 | if (!Op->hasOneUse()) |
| 8821 | return SDValue(); |
| 8822 | |
| 8823 | SDNode *N = Op.getNode(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8824 | SDLoc DL(N); |
| Michael Liao | f966e4e | 2012-09-13 20:24:54 +0000 | [diff] [blame] | 8825 | |
| 8826 | SmallVector<SDValue, 8> Opnds; |
| 8827 | DenseMap<SDValue, unsigned> VecInMap; |
| 8828 | EVT VT = MVT::Other; |
| 8829 | |
| 8830 | // Recognize a special case where a vector is casted into wide integer to |
| 8831 | // test all 0s. |
| 8832 | Opnds.push_back(N->getOperand(0)); |
| 8833 | Opnds.push_back(N->getOperand(1)); |
| 8834 | |
| 8835 | for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) { |
| Craig Topper | 365ef0b | 2013-07-03 15:07:05 +0000 | [diff] [blame] | 8836 | SmallVectorImpl<SDValue>::const_iterator I = Opnds.begin() + Slot; |
| Michael Liao | f966e4e | 2012-09-13 20:24:54 +0000 | [diff] [blame] | 8837 | // BFS traverse all OR'd operands. |
| 8838 | if (I->getOpcode() == ISD::OR) { |
| 8839 | Opnds.push_back(I->getOperand(0)); |
| 8840 | Opnds.push_back(I->getOperand(1)); |
| 8841 | // Re-evaluate the number of nodes to be traversed. |
| 8842 | e += 2; // 2 more nodes (LHS and RHS) are pushed. |
| 8843 | continue; |
| 8844 | } |
| 8845 | |
| 8846 | // Quit if a non-EXTRACT_VECTOR_ELT |
| 8847 | if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT) |
| 8848 | return SDValue(); |
| 8849 | |
| 8850 | // Quit if without a constant index. |
| 8851 | SDValue Idx = I->getOperand(1); |
| 8852 | if (!isa<ConstantSDNode>(Idx)) |
| 8853 | return SDValue(); |
| 8854 | |
| 8855 | SDValue ExtractedFromVec = I->getOperand(0); |
| 8856 | DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec); |
| 8857 | if (M == VecInMap.end()) { |
| 8858 | VT = ExtractedFromVec.getValueType(); |
| 8859 | // Quit if not 128/256-bit vector. |
| 8860 | if (!VT.is128BitVector() && !VT.is256BitVector()) |
| 8861 | return SDValue(); |
| 8862 | // Quit if not the same type. |
| 8863 | if (VecInMap.begin() != VecInMap.end() && |
| 8864 | VT != VecInMap.begin()->first.getValueType()) |
| 8865 | return SDValue(); |
| 8866 | M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first; |
| 8867 | } |
| 8868 | M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue(); |
| 8869 | } |
| 8870 | |
| 8871 | assert((VT.is128BitVector() || VT.is256BitVector()) && |
| Michael Liao | 9aba7ea | 2012-09-13 20:30:16 +0000 | [diff] [blame] | 8872 | "Not extracted from 128-/256-bit vector."); |
| Michael Liao | f966e4e | 2012-09-13 20:24:54 +0000 | [diff] [blame] | 8873 | |
| 8874 | unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U; |
| 8875 | SmallVector<SDValue, 8> VecIns; |
| 8876 | |
| 8877 | for (DenseMap<SDValue, unsigned>::const_iterator |
| 8878 | I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) { |
| 8879 | // Quit if not all elements are used. |
| 8880 | if (I->second != FullMask) |
| 8881 | return SDValue(); |
| 8882 | VecIns.push_back(I->first); |
| 8883 | } |
| 8884 | |
| 8885 | EVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64; |
| 8886 | |
| 8887 | // Cast all vectors into TestVT for PTEST. |
| 8888 | for (unsigned i = 0, e = VecIns.size(); i < e; ++i) |
| 8889 | VecIns[i] = DAG.getNode(ISD::BITCAST, DL, TestVT, VecIns[i]); |
| 8890 | |
| 8891 | // If more than one full vectors are evaluated, OR them first before PTEST. |
| 8892 | for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) { |
| 8893 | // Each iteration will OR 2 nodes and append the result until there is only |
| 8894 | // 1 node left, i.e. the final OR'd value of all vectors. |
| 8895 | SDValue LHS = VecIns[Slot]; |
| 8896 | SDValue RHS = VecIns[Slot + 1]; |
| 8897 | VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS)); |
| 8898 | } |
| 8899 | |
| 8900 | return DAG.getNode(X86ISD::PTEST, DL, MVT::i32, |
| 8901 | VecIns.back(), VecIns.back()); |
| 8902 | } |
| 8903 | |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 8904 | /// Emit nodes that will be selected as "test Op0,Op0", or something |
| 8905 | /// equivalent. |
| Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 8906 | SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC, |
| Evan Cheng | 552f09a | 2010-04-26 19:06:11 +0000 | [diff] [blame] | 8907 | SelectionDAG &DAG) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 8908 | SDLoc dl(Op); |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 8909 | |
| Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 8910 | // CF and OF aren't always set the way we want. Determine which |
| 8911 | // of these we need. |
| 8912 | bool NeedCF = false; |
| 8913 | bool NeedOF = false; |
| 8914 | switch (X86CC) { |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 8915 | default: break; |
| Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 8916 | case X86::COND_A: case X86::COND_AE: |
| 8917 | case X86::COND_B: case X86::COND_BE: |
| 8918 | NeedCF = true; |
| 8919 | break; |
| 8920 | case X86::COND_G: case X86::COND_GE: |
| 8921 | case X86::COND_L: case X86::COND_LE: |
| 8922 | case X86::COND_O: case X86::COND_NO: |
| 8923 | NeedOF = true; |
| 8924 | break; |
| Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 8925 | } |
| 8926 | |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 8927 | // See if we can use the EFLAGS value from the operand instead of |
| Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 8928 | // doing a separate TEST. TEST always sets OF and CF to 0, so unless |
| 8929 | // we prove that the arithmetic won't overflow, we can't use OF or CF. |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 8930 | if (Op.getResNo() != 0 || NeedOF || NeedCF) |
| 8931 | // Emit a CMP with 0, which is the TEST pattern. |
| 8932 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, |
| 8933 | DAG.getConstant(0, Op.getValueType())); |
| 8934 | |
| 8935 | unsigned Opcode = 0; |
| 8936 | unsigned NumOperands = 0; |
| Nadav Rotem | b9d6b84 | 2012-08-18 17:53:03 +0000 | [diff] [blame] | 8937 | |
| 8938 | // Truncate operations may prevent the merge of the SETCC instruction |
| 8939 | // and the arithmetic intruction before it. Attempt to truncate the operands |
| 8940 | // of the arithmetic instruction and use a reduced bit-width instruction. |
| 8941 | bool NeedTruncation = false; |
| 8942 | SDValue ArithOp = Op; |
| 8943 | if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) { |
| 8944 | SDValue Arith = Op->getOperand(0); |
| 8945 | // Both the trunc and the arithmetic op need to have one user each. |
| 8946 | if (Arith->hasOneUse()) |
| 8947 | switch (Arith.getOpcode()) { |
| 8948 | default: break; |
| 8949 | case ISD::ADD: |
| 8950 | case ISD::SUB: |
| 8951 | case ISD::AND: |
| 8952 | case ISD::OR: |
| 8953 | case ISD::XOR: { |
| 8954 | NeedTruncation = true; |
| 8955 | ArithOp = Arith; |
| 8956 | } |
| 8957 | } |
| 8958 | } |
| 8959 | |
| 8960 | // NOTICE: In the code below we use ArithOp to hold the arithmetic operation |
| 8961 | // which may be the result of a CAST. We use the variable 'Op', which is the |
| 8962 | // non-casted variable when we check for possible users. |
| 8963 | switch (ArithOp.getOpcode()) { |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 8964 | case ISD::ADD: |
| 8965 | // Due to an isel shortcoming, be conservative if this add is likely to be |
| 8966 | // selected as part of a load-modify-store instruction. When the root node |
| 8967 | // in a match is a store, isel doesn't know how to remap non-chain non-flag |
| 8968 | // uses of other nodes in the match, such as the ADD in this case. This |
| 8969 | // leads to the ADD being left around and reselected, with the result being |
| 8970 | // two adds in the output. Alas, even if none our users are stores, that |
| 8971 | // doesn't prove we're O.K. Ergo, if we have any parents that aren't |
| 8972 | // CopyToReg or SETCC, eschew INC/DEC. A better fix seems to require |
| 8973 | // climbing the DAG back to the root, and it doesn't seem to be worth the |
| 8974 | // effort. |
| 8975 | for (SDNode::use_iterator UI = Op.getNode()->use_begin(), |
| Pete Cooper | 2d49689 | 2011-11-15 21:57:53 +0000 | [diff] [blame] | 8976 | UE = Op.getNode()->use_end(); UI != UE; ++UI) |
| 8977 | if (UI->getOpcode() != ISD::CopyToReg && |
| 8978 | UI->getOpcode() != ISD::SETCC && |
| 8979 | UI->getOpcode() != ISD::STORE) |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 8980 | goto default_case; |
| 8981 | |
| 8982 | if (ConstantSDNode *C = |
| Nadav Rotem | b9d6b84 | 2012-08-18 17:53:03 +0000 | [diff] [blame] | 8983 | dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) { |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 8984 | // An add of one will be selected as an INC. |
| 8985 | if (C->getAPIntValue() == 1) { |
| 8986 | Opcode = X86ISD::INC; |
| 8987 | NumOperands = 1; |
| 8988 | break; |
| Dan Gohman | e220c4b | 2009-09-18 19:59:53 +0000 | [diff] [blame] | 8989 | } |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 8990 | |
| 8991 | // An add of negative one (subtract of one) will be selected as a DEC. |
| 8992 | if (C->getAPIntValue().isAllOnesValue()) { |
| 8993 | Opcode = X86ISD::DEC; |
| 8994 | NumOperands = 1; |
| 8995 | break; |
| 8996 | } |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 8997 | } |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 8998 | |
| 8999 | // Otherwise use a regular EFLAGS-setting add. |
| 9000 | Opcode = X86ISD::ADD; |
| 9001 | NumOperands = 2; |
| 9002 | break; |
| 9003 | case ISD::AND: { |
| 9004 | // If the primary and result isn't used, don't bother using X86ISD::AND, |
| 9005 | // because a TEST instruction will be better. |
| 9006 | bool NonFlagUse = false; |
| 9007 | for (SDNode::use_iterator UI = Op.getNode()->use_begin(), |
| 9008 | UE = Op.getNode()->use_end(); UI != UE; ++UI) { |
| 9009 | SDNode *User = *UI; |
| 9010 | unsigned UOpNo = UI.getOperandNo(); |
| 9011 | if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) { |
| 9012 | // Look pass truncate. |
| 9013 | UOpNo = User->use_begin().getOperandNo(); |
| 9014 | User = *User->use_begin(); |
| 9015 | } |
| 9016 | |
| 9017 | if (User->getOpcode() != ISD::BRCOND && |
| 9018 | User->getOpcode() != ISD::SETCC && |
| Nadav Rotem | b9d6b84 | 2012-08-18 17:53:03 +0000 | [diff] [blame] | 9019 | !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) { |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 9020 | NonFlagUse = true; |
| 9021 | break; |
| 9022 | } |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9023 | } |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 9024 | |
| 9025 | if (!NonFlagUse) |
| 9026 | break; |
| 9027 | } |
| 9028 | // FALL THROUGH |
| 9029 | case ISD::SUB: |
| 9030 | case ISD::OR: |
| 9031 | case ISD::XOR: |
| 9032 | // Due to the ISEL shortcoming noted above, be conservative if this op is |
| 9033 | // likely to be selected as part of a load-modify-store instruction. |
| 9034 | for (SDNode::use_iterator UI = Op.getNode()->use_begin(), |
| 9035 | UE = Op.getNode()->use_end(); UI != UE; ++UI) |
| 9036 | if (UI->getOpcode() == ISD::STORE) |
| 9037 | goto default_case; |
| 9038 | |
| 9039 | // Otherwise use a regular EFLAGS-setting instruction. |
| Nadav Rotem | b9d6b84 | 2012-08-18 17:53:03 +0000 | [diff] [blame] | 9040 | switch (ArithOp.getOpcode()) { |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 9041 | default: llvm_unreachable("unexpected operator!"); |
| Nadav Rotem | b9d6b84 | 2012-08-18 17:53:03 +0000 | [diff] [blame] | 9042 | case ISD::SUB: Opcode = X86ISD::SUB; break; |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 9043 | case ISD::XOR: Opcode = X86ISD::XOR; break; |
| 9044 | case ISD::AND: Opcode = X86ISD::AND; break; |
| Michael Liao | f966e4e | 2012-09-13 20:24:54 +0000 | [diff] [blame] | 9045 | case ISD::OR: { |
| 9046 | if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) { |
| 9047 | SDValue EFLAGS = LowerVectorAllZeroTest(Op, DAG); |
| 9048 | if (EFLAGS.getNode()) |
| 9049 | return EFLAGS; |
| 9050 | } |
| 9051 | Opcode = X86ISD::OR; |
| 9052 | break; |
| 9053 | } |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 9054 | } |
| 9055 | |
| 9056 | NumOperands = 2; |
| 9057 | break; |
| 9058 | case X86ISD::ADD: |
| 9059 | case X86ISD::SUB: |
| 9060 | case X86ISD::INC: |
| 9061 | case X86ISD::DEC: |
| 9062 | case X86ISD::OR: |
| 9063 | case X86ISD::XOR: |
| 9064 | case X86ISD::AND: |
| 9065 | return SDValue(Op.getNode(), 1); |
| 9066 | default: |
| 9067 | default_case: |
| 9068 | break; |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9069 | } |
| 9070 | |
| Nadav Rotem | b9d6b84 | 2012-08-18 17:53:03 +0000 | [diff] [blame] | 9071 | // If we found that truncation is beneficial, perform the truncation and |
| 9072 | // update 'Op'. |
| 9073 | if (NeedTruncation) { |
| 9074 | EVT VT = Op.getValueType(); |
| 9075 | SDValue WideVal = Op->getOperand(0); |
| 9076 | EVT WideVT = WideVal.getValueType(); |
| 9077 | unsigned ConvertedOp = 0; |
| 9078 | // Use a target machine opcode to prevent further DAGCombine |
| 9079 | // optimizations that may separate the arithmetic operations |
| 9080 | // from the setcc node. |
| 9081 | switch (WideVal.getOpcode()) { |
| 9082 | default: break; |
| 9083 | case ISD::ADD: ConvertedOp = X86ISD::ADD; break; |
| 9084 | case ISD::SUB: ConvertedOp = X86ISD::SUB; break; |
| 9085 | case ISD::AND: ConvertedOp = X86ISD::AND; break; |
| 9086 | case ISD::OR: ConvertedOp = X86ISD::OR; break; |
| 9087 | case ISD::XOR: ConvertedOp = X86ISD::XOR; break; |
| 9088 | } |
| 9089 | |
| 9090 | if (ConvertedOp) { |
| 9091 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 9092 | if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) { |
| 9093 | SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0)); |
| 9094 | SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1)); |
| 9095 | Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1); |
| 9096 | } |
| 9097 | } |
| 9098 | } |
| 9099 | |
| Bill Wendling | c25ccf8 | 2010-06-28 21:08:32 +0000 | [diff] [blame] | 9100 | if (Opcode == 0) |
| 9101 | // Emit a CMP with 0, which is the TEST pattern. |
| 9102 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, |
| 9103 | DAG.getConstant(0, Op.getValueType())); |
| 9104 | |
| 9105 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); |
| 9106 | SmallVector<SDValue, 4> Ops; |
| 9107 | for (unsigned i = 0; i != NumOperands; ++i) |
| 9108 | Ops.push_back(Op.getOperand(i)); |
| 9109 | |
| 9110 | SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands); |
| 9111 | DAG.ReplaceAllUsesWith(Op, New); |
| 9112 | return SDValue(New.getNode(), 1); |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9113 | } |
| 9114 | |
| 9115 | /// Emit nodes that will be selected as "cmp Op0,Op1", or something |
| 9116 | /// equivalent. |
| Dan Gohman | 3112581 | 2009-03-07 01:58:32 +0000 | [diff] [blame] | 9117 | SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC, |
| Evan Cheng | 552f09a | 2010-04-26 19:06:11 +0000 | [diff] [blame] | 9118 | SelectionDAG &DAG) const { |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9119 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1)) |
| 9120 | if (C->getAPIntValue() == 0) |
| Evan Cheng | 552f09a | 2010-04-26 19:06:11 +0000 | [diff] [blame] | 9121 | return EmitTest(Op0, X86CC, DAG); |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9122 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9123 | SDLoc dl(Op0); |
| Manman Ren | 39ad568 | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 9124 | if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 || |
| 9125 | Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) { |
| 9126 | // Use SUB instead of CMP to enable CSE between SUB and CMP. |
| 9127 | SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32); |
| 9128 | SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs, |
| 9129 | Op0, Op1); |
| 9130 | return SDValue(Sub.getNode(), 1); |
| 9131 | } |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9132 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1); |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9133 | } |
| 9134 | |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 9135 | /// Convert a comparison if required by the subtarget. |
| 9136 | SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp, |
| 9137 | SelectionDAG &DAG) const { |
| 9138 | // If the subtarget does not support the FUCOMI instruction, floating-point |
| 9139 | // comparisons have to be converted. |
| 9140 | if (Subtarget->hasCMov() || |
| 9141 | Cmp.getOpcode() != X86ISD::CMP || |
| 9142 | !Cmp.getOperand(0).getValueType().isFloatingPoint() || |
| 9143 | !Cmp.getOperand(1).getValueType().isFloatingPoint()) |
| 9144 | return Cmp; |
| 9145 | |
| 9146 | // The instruction selector will select an FUCOM instruction instead of |
| 9147 | // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence |
| 9148 | // build an SDNode sequence that transfers the result from FPSW into EFLAGS: |
| 9149 | // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8)))) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9150 | SDLoc dl(Cmp); |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 9151 | SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp); |
| 9152 | SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW); |
| 9153 | SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW, |
| 9154 | DAG.getConstant(8, MVT::i8)); |
| 9155 | SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl); |
| 9156 | return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl); |
| 9157 | } |
| 9158 | |
| Evan Cheng | 4e54480 | 2012-12-05 00:10:38 +0000 | [diff] [blame] | 9159 | static bool isAllOnes(SDValue V) { |
| 9160 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(V); |
| 9161 | return C && C->isAllOnesValue(); |
| 9162 | } |
| 9163 | |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9164 | /// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node |
| 9165 | /// if it's possible. |
| Evan Cheng | 5528e7b | 2010-04-21 01:47:12 +0000 | [diff] [blame] | 9166 | SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC, |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9167 | SDLoc dl, SelectionDAG &DAG) const { |
| Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 9168 | SDValue Op0 = And.getOperand(0); |
| 9169 | SDValue Op1 = And.getOperand(1); |
| 9170 | if (Op0.getOpcode() == ISD::TRUNCATE) |
| 9171 | Op0 = Op0.getOperand(0); |
| 9172 | if (Op1.getOpcode() == ISD::TRUNCATE) |
| 9173 | Op1 = Op1.getOperand(0); |
| 9174 | |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9175 | SDValue LHS, RHS; |
| Dan Gohman | 6b13cbc | 2010-06-24 02:07:59 +0000 | [diff] [blame] | 9176 | if (Op1.getOpcode() == ISD::SHL) |
| 9177 | std::swap(Op0, Op1); |
| 9178 | if (Op0.getOpcode() == ISD::SHL) { |
| Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 9179 | if (ConstantSDNode *And00C = dyn_cast<ConstantSDNode>(Op0.getOperand(0))) |
| 9180 | if (And00C->getZExtValue() == 1) { |
| Dan Gohman | 6b13cbc | 2010-06-24 02:07:59 +0000 | [diff] [blame] | 9181 | // If we looked past a truncate, check that it's only truncating away |
| 9182 | // known zeros. |
| 9183 | unsigned BitWidth = Op0.getValueSizeInBits(); |
| 9184 | unsigned AndBitWidth = And.getValueSizeInBits(); |
| 9185 | if (BitWidth > AndBitWidth) { |
| Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 9186 | APInt Zeros, Ones; |
| 9187 | DAG.ComputeMaskedBits(Op0, Zeros, Ones); |
| Dan Gohman | 6b13cbc | 2010-06-24 02:07:59 +0000 | [diff] [blame] | 9188 | if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth) |
| 9189 | return SDValue(); |
| 9190 | } |
| Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 9191 | LHS = Op1; |
| 9192 | RHS = Op0.getOperand(1); |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9193 | } |
| Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 9194 | } else if (Op1.getOpcode() == ISD::Constant) { |
| 9195 | ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1); |
| Benjamin Kramer | f238f50 | 2011-11-23 13:54:17 +0000 | [diff] [blame] | 9196 | uint64_t AndRHSVal = AndRHS->getZExtValue(); |
| Evan Cheng | 2c755ba | 2010-02-27 07:36:59 +0000 | [diff] [blame] | 9197 | SDValue AndLHS = Op0; |
| Benjamin Kramer | f238f50 | 2011-11-23 13:54:17 +0000 | [diff] [blame] | 9198 | |
| 9199 | if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) { |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9200 | LHS = AndLHS.getOperand(0); |
| 9201 | RHS = AndLHS.getOperand(1); |
| Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 9202 | } |
| Benjamin Kramer | f238f50 | 2011-11-23 13:54:17 +0000 | [diff] [blame] | 9203 | |
| 9204 | // Use BT if the immediate can't be encoded in a TEST instruction. |
| 9205 | if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) { |
| 9206 | LHS = AndLHS; |
| 9207 | RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), LHS.getValueType()); |
| 9208 | } |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9209 | } |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 9210 | |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9211 | if (LHS.getNode()) { |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 9212 | // If LHS is i8, promote it to i32 with any_extend. There is no i8 BT |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9213 | // instruction. Since the shift amount is in-range-or-undefined, we know |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 9214 | // that doing a bittest on the i32 value is ok. We extend to i32 because |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9215 | // the encoding for the i16 version is larger than the i32 version. |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 9216 | // Also promote i16 to i32 for performance / code size reason. |
| 9217 | if (LHS.getValueType() == MVT::i8 || |
| Evan Cheng | 2bce5f4b | 2010-04-28 08:30:49 +0000 | [diff] [blame] | 9218 | LHS.getValueType() == MVT::i16) |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9219 | LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS); |
| Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 9220 | |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9221 | // If the operand types disagree, extend the shift amount to match. Since |
| 9222 | // BT ignores high bits (like shifts) we can use anyextend. |
| 9223 | if (LHS.getValueType() != RHS.getValueType()) |
| 9224 | RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS); |
| Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 9225 | |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9226 | SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS); |
| Evan Cheng | 4e54480 | 2012-12-05 00:10:38 +0000 | [diff] [blame] | 9227 | X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B; |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9228 | return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
| 9229 | DAG.getConstant(Cond, MVT::i8), BT); |
| Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 9230 | } |
| 9231 | |
| Evan Cheng | 54de3ea | 2010-01-05 06:52:31 +0000 | [diff] [blame] | 9232 | return SDValue(); |
| 9233 | } |
| 9234 | |
| Craig Topper | 89af15e | 2011-09-18 08:03:58 +0000 | [diff] [blame] | 9235 | // Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128 |
| Bruno Cardoso Lopes | 2ac8111 | 2011-08-22 20:31:04 +0000 | [diff] [blame] | 9236 | // ones, and then concatenate the result back. |
| Craig Topper | 89af15e | 2011-09-18 08:03:58 +0000 | [diff] [blame] | 9237 | static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) { |
| Craig Topper | 26827f3 | 2013-01-20 09:02:22 +0000 | [diff] [blame] | 9238 | MVT VT = Op.getValueType().getSimpleVT(); |
| Bruno Cardoso Lopes | 2ac8111 | 2011-08-22 20:31:04 +0000 | [diff] [blame] | 9239 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 9240 | assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC && |
| Bruno Cardoso Lopes | 2ac8111 | 2011-08-22 20:31:04 +0000 | [diff] [blame] | 9241 | "Unsupported value type for operation"); |
| 9242 | |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 9243 | unsigned NumElems = VT.getVectorNumElements(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9244 | SDLoc dl(Op); |
| Bruno Cardoso Lopes | 2ac8111 | 2011-08-22 20:31:04 +0000 | [diff] [blame] | 9245 | SDValue CC = Op.getOperand(2); |
| Bruno Cardoso Lopes | 2ac8111 | 2011-08-22 20:31:04 +0000 | [diff] [blame] | 9246 | |
| 9247 | // Extract the LHS vectors |
| 9248 | SDValue LHS = Op.getOperand(0); |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 9249 | SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl); |
| 9250 | SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl); |
| Bruno Cardoso Lopes | 2ac8111 | 2011-08-22 20:31:04 +0000 | [diff] [blame] | 9251 | |
| 9252 | // Extract the RHS vectors |
| 9253 | SDValue RHS = Op.getOperand(1); |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 9254 | SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl); |
| 9255 | SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl); |
| Bruno Cardoso Lopes | 2ac8111 | 2011-08-22 20:31:04 +0000 | [diff] [blame] | 9256 | |
| 9257 | // Issue the operation on the smaller types and concatenate the result back |
| Craig Topper | 26827f3 | 2013-01-20 09:02:22 +0000 | [diff] [blame] | 9258 | MVT EltVT = VT.getVectorElementType(); |
| 9259 | MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2); |
| Bruno Cardoso Lopes | 2ac8111 | 2011-08-22 20:31:04 +0000 | [diff] [blame] | 9260 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, |
| 9261 | DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC), |
| 9262 | DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC)); |
| 9263 | } |
| 9264 | |
| Craig Topper | 26827f3 | 2013-01-20 09:02:22 +0000 | [diff] [blame] | 9265 | static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget, |
| 9266 | SelectionDAG &DAG) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9267 | SDValue Cond; |
| 9268 | SDValue Op0 = Op.getOperand(0); |
| 9269 | SDValue Op1 = Op.getOperand(1); |
| 9270 | SDValue CC = Op.getOperand(2); |
| Craig Topper | 26827f3 | 2013-01-20 09:02:22 +0000 | [diff] [blame] | 9271 | MVT VT = Op.getValueType().getSimpleVT(); |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9272 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); |
| Craig Topper | 26827f3 | 2013-01-20 09:02:22 +0000 | [diff] [blame] | 9273 | bool isFP = Op.getOperand(1).getValueType().getSimpleVT().isFloatingPoint(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9274 | SDLoc dl(Op); |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9275 | |
| 9276 | if (isFP) { |
| Craig Topper | 523908d | 2012-08-13 02:34:03 +0000 | [diff] [blame] | 9277 | #ifndef NDEBUG |
| Craig Topper | 26827f3 | 2013-01-20 09:02:22 +0000 | [diff] [blame] | 9278 | MVT EltVT = Op0.getValueType().getVectorElementType().getSimpleVT(); |
| Craig Topper | 523908d | 2012-08-13 02:34:03 +0000 | [diff] [blame] | 9279 | assert(EltVT == MVT::f32 || EltVT == MVT::f64); |
| 9280 | #endif |
| Bruno Cardoso Lopes | 0f0e0a0 | 2011-08-09 00:46:57 +0000 | [diff] [blame] | 9281 | |
| Craig Topper | 523908d | 2012-08-13 02:34:03 +0000 | [diff] [blame] | 9282 | unsigned SSECC; |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9283 | bool Swap = false; |
| 9284 | |
| Bruno Cardoso Lopes | 8e03a82 | 2011-09-12 19:30:40 +0000 | [diff] [blame] | 9285 | // SSE Condition code mapping: |
| 9286 | // 0 - EQ |
| 9287 | // 1 - LT |
| 9288 | // 2 - LE |
| 9289 | // 3 - UNORD |
| 9290 | // 4 - NEQ |
| 9291 | // 5 - NLT |
| 9292 | // 6 - NLE |
| 9293 | // 7 - ORD |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9294 | switch (SetCCOpcode) { |
| Craig Topper | 2f1b2ec | 2012-08-13 03:42:38 +0000 | [diff] [blame] | 9295 | default: llvm_unreachable("Unexpected SETCC condition"); |
| Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 9296 | case ISD::SETOEQ: |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9297 | case ISD::SETEQ: SSECC = 0; break; |
| Bruno Cardoso Lopes | 8e03a82 | 2011-09-12 19:30:40 +0000 | [diff] [blame] | 9298 | case ISD::SETOGT: |
| 9299 | case ISD::SETGT: Swap = true; // Fallthrough |
| Bruno Cardoso Lopes | 457d53d | 2011-09-12 21:24:07 +0000 | [diff] [blame] | 9300 | case ISD::SETLT: |
| 9301 | case ISD::SETOLT: SSECC = 1; break; |
| 9302 | case ISD::SETOGE: |
| 9303 | case ISD::SETGE: Swap = true; // Fallthrough |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9304 | case ISD::SETLE: |
| 9305 | case ISD::SETOLE: SSECC = 2; break; |
| 9306 | case ISD::SETUO: SSECC = 3; break; |
| Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 9307 | case ISD::SETUNE: |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9308 | case ISD::SETNE: SSECC = 4; break; |
| Craig Topper | 523908d | 2012-08-13 02:34:03 +0000 | [diff] [blame] | 9309 | case ISD::SETULE: Swap = true; // Fallthrough |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9310 | case ISD::SETUGE: SSECC = 5; break; |
| Craig Topper | 523908d | 2012-08-13 02:34:03 +0000 | [diff] [blame] | 9311 | case ISD::SETULT: Swap = true; // Fallthrough |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9312 | case ISD::SETUGT: SSECC = 6; break; |
| 9313 | case ISD::SETO: SSECC = 7; break; |
| Craig Topper | 523908d | 2012-08-13 02:34:03 +0000 | [diff] [blame] | 9314 | case ISD::SETUEQ: |
| 9315 | case ISD::SETONE: SSECC = 8; break; |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9316 | } |
| 9317 | if (Swap) |
| 9318 | std::swap(Op0, Op1); |
| 9319 | |
| Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 9320 | // In the two special cases we can't handle, emit two comparisons. |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9321 | if (SSECC == 8) { |
| Craig Topper | 523908d | 2012-08-13 02:34:03 +0000 | [diff] [blame] | 9322 | unsigned CC0, CC1; |
| 9323 | unsigned CombineOpc; |
| Nate Begeman | fb8ead0 | 2008-07-25 19:05:58 +0000 | [diff] [blame] | 9324 | if (SetCCOpcode == ISD::SETUEQ) { |
| Craig Topper | 523908d | 2012-08-13 02:34:03 +0000 | [diff] [blame] | 9325 | CC0 = 3; CC1 = 0; CombineOpc = ISD::OR; |
| 9326 | } else { |
| 9327 | assert(SetCCOpcode == ISD::SETONE); |
| 9328 | CC0 = 7; CC1 = 4; CombineOpc = ISD::AND; |
| Craig Topper | 69947b9 | 2012-04-23 06:57:04 +0000 | [diff] [blame] | 9329 | } |
| Craig Topper | 523908d | 2012-08-13 02:34:03 +0000 | [diff] [blame] | 9330 | |
| 9331 | SDValue Cmp0 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1, |
| 9332 | DAG.getConstant(CC0, MVT::i8)); |
| 9333 | SDValue Cmp1 = DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1, |
| 9334 | DAG.getConstant(CC1, MVT::i8)); |
| 9335 | return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1); |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9336 | } |
| 9337 | // Handle all other FP comparisons here. |
| Craig Topper | 1906d32 | 2012-01-22 23:36:02 +0000 | [diff] [blame] | 9338 | return DAG.getNode(X86ISD::CMPP, dl, VT, Op0, Op1, |
| 9339 | DAG.getConstant(SSECC, MVT::i8)); |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9340 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9341 | |
| Bruno Cardoso Lopes | 2ac8111 | 2011-08-22 20:31:04 +0000 | [diff] [blame] | 9342 | // Break 256-bit integer vector compare into smaller ones. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 9343 | if (VT.is256BitVector() && !Subtarget->hasInt256()) |
| Craig Topper | 89af15e | 2011-09-18 08:03:58 +0000 | [diff] [blame] | 9344 | return Lower256IntVSETCC(Op, DAG); |
| Bruno Cardoso Lopes | 0f0e0a0 | 2011-08-09 00:46:57 +0000 | [diff] [blame] | 9345 | |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9346 | // We are handling one of the integer comparisons here. Since SSE only has |
| 9347 | // GT and EQ comparisons for integer, swapping operands and multiple |
| 9348 | // operations may be required for some comparisons. |
| Craig Topper | 2f1b2ec | 2012-08-13 03:42:38 +0000 | [diff] [blame] | 9349 | unsigned Opc; |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9350 | bool Swap = false, Invert = false, FlipSigns = false; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9351 | |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9352 | switch (SetCCOpcode) { |
| Craig Topper | 2f1b2ec | 2012-08-13 03:42:38 +0000 | [diff] [blame] | 9353 | default: llvm_unreachable("Unexpected SETCC condition"); |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9354 | case ISD::SETNE: Invert = true; |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 9355 | case ISD::SETEQ: Opc = X86ISD::PCMPEQ; break; |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9356 | case ISD::SETLT: Swap = true; |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 9357 | case ISD::SETGT: Opc = X86ISD::PCMPGT; break; |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9358 | case ISD::SETGE: Swap = true; |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 9359 | case ISD::SETLE: Opc = X86ISD::PCMPGT; Invert = true; break; |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9360 | case ISD::SETULT: Swap = true; |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 9361 | case ISD::SETUGT: Opc = X86ISD::PCMPGT; FlipSigns = true; break; |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9362 | case ISD::SETUGE: Swap = true; |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 9363 | case ISD::SETULE: Opc = X86ISD::PCMPGT; FlipSigns = true; Invert = true; break; |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9364 | } |
| 9365 | if (Swap) |
| 9366 | std::swap(Op0, Op1); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9367 | |
| Eli Friedman | 7d3e2b7 | 2011-09-28 21:00:25 +0000 | [diff] [blame] | 9368 | // Check that the operation in question is available (most are plain SSE2, |
| 9369 | // but PCMPGTQ and PCMPEQQ have different requirements). |
| Craig Topper | 2f1b2ec | 2012-08-13 03:42:38 +0000 | [diff] [blame] | 9370 | if (VT == MVT::v2i64) { |
| Benjamin Kramer | fcba22d | 2013-04-18 21:37:45 +0000 | [diff] [blame] | 9371 | if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42()) { |
| 9372 | assert(Subtarget->hasSSE2() && "Don't know how to lower!"); |
| 9373 | |
| Benjamin Kramer | f106d8b | 2013-05-21 09:58:54 +0000 | [diff] [blame] | 9374 | // First cast everything to the right type. |
| Benjamin Kramer | fcba22d | 2013-04-18 21:37:45 +0000 | [diff] [blame] | 9375 | Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0); |
| 9376 | Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1); |
| 9377 | |
| Benjamin Kramer | f106d8b | 2013-05-21 09:58:54 +0000 | [diff] [blame] | 9378 | // Since SSE has no unsigned integer comparisons, we need to flip the sign |
| Benjamin Kramer | 60ef6c9 | 2013-05-22 17:01:12 +0000 | [diff] [blame] | 9379 | // bits of the inputs before performing those operations. The lower |
| 9380 | // compare is always unsigned. |
| 9381 | SDValue SB; |
| Benjamin Kramer | f106d8b | 2013-05-21 09:58:54 +0000 | [diff] [blame] | 9382 | if (FlipSigns) { |
| Benjamin Kramer | 60ef6c9 | 2013-05-22 17:01:12 +0000 | [diff] [blame] | 9383 | SB = DAG.getConstant(0x80000000U, MVT::v4i32); |
| 9384 | } else { |
| 9385 | SDValue Sign = DAG.getConstant(0x80000000U, MVT::i32); |
| 9386 | SDValue Zero = DAG.getConstant(0x00000000U, MVT::i32); |
| 9387 | SB = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, |
| 9388 | Sign, Zero, Sign, Zero); |
| Benjamin Kramer | f106d8b | 2013-05-21 09:58:54 +0000 | [diff] [blame] | 9389 | } |
| Benjamin Kramer | 60ef6c9 | 2013-05-22 17:01:12 +0000 | [diff] [blame] | 9390 | Op0 = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Op0, SB); |
| 9391 | Op1 = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Op1, SB); |
| Benjamin Kramer | f106d8b | 2013-05-21 09:58:54 +0000 | [diff] [blame] | 9392 | |
| Benjamin Kramer | fcba22d | 2013-04-18 21:37:45 +0000 | [diff] [blame] | 9393 | // Emulate PCMPGTQ with (hi1 > hi2) | ((hi1 == hi2) & (lo1 > lo2)) |
| 9394 | SDValue GT = DAG.getNode(X86ISD::PCMPGT, dl, MVT::v4i32, Op0, Op1); |
| 9395 | SDValue EQ = DAG.getNode(X86ISD::PCMPEQ, dl, MVT::v4i32, Op0, Op1); |
| 9396 | |
| 9397 | // Create masks for only the low parts/high parts of the 64 bit integers. |
| 9398 | const int MaskHi[] = { 1, 1, 3, 3 }; |
| 9399 | const int MaskLo[] = { 0, 0, 2, 2 }; |
| 9400 | SDValue EQHi = DAG.getVectorShuffle(MVT::v4i32, dl, EQ, EQ, MaskHi); |
| 9401 | SDValue GTLo = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskLo); |
| 9402 | SDValue GTHi = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskHi); |
| 9403 | |
| 9404 | SDValue Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, EQHi, GTLo); |
| 9405 | Result = DAG.getNode(ISD::OR, dl, MVT::v4i32, Result, GTHi); |
| 9406 | |
| 9407 | if (Invert) |
| 9408 | Result = DAG.getNOT(dl, Result, MVT::v4i32); |
| 9409 | |
| 9410 | return DAG.getNode(ISD::BITCAST, dl, VT, Result); |
| 9411 | } |
| 9412 | |
| Benjamin Kramer | 382ed78 | 2012-12-25 12:54:19 +0000 | [diff] [blame] | 9413 | if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) { |
| 9414 | // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with |
| Benjamin Kramer | 99f7806 | 2012-12-25 13:09:08 +0000 | [diff] [blame] | 9415 | // pcmpeqd + pshufd + pand. |
| Benjamin Kramer | 382ed78 | 2012-12-25 12:54:19 +0000 | [diff] [blame] | 9416 | assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!"); |
| 9417 | |
| Benjamin Kramer | f106d8b | 2013-05-21 09:58:54 +0000 | [diff] [blame] | 9418 | // First cast everything to the right type. |
| Benjamin Kramer | 382ed78 | 2012-12-25 12:54:19 +0000 | [diff] [blame] | 9419 | Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op0); |
| 9420 | Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Op1); |
| 9421 | |
| 9422 | // Do the compare. |
| 9423 | SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1); |
| 9424 | |
| 9425 | // Make sure the lower and upper halves are both all-ones. |
| Benjamin Kramer | 99f7806 | 2012-12-25 13:09:08 +0000 | [diff] [blame] | 9426 | const int Mask[] = { 1, 0, 3, 2 }; |
| 9427 | SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask); |
| 9428 | Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf); |
| Benjamin Kramer | 382ed78 | 2012-12-25 12:54:19 +0000 | [diff] [blame] | 9429 | |
| 9430 | if (Invert) |
| 9431 | Result = DAG.getNOT(dl, Result, MVT::v4i32); |
| 9432 | |
| 9433 | return DAG.getNode(ISD::BITCAST, dl, VT, Result); |
| 9434 | } |
| Craig Topper | 2f1b2ec | 2012-08-13 03:42:38 +0000 | [diff] [blame] | 9435 | } |
| Eli Friedman | 7d3e2b7 | 2011-09-28 21:00:25 +0000 | [diff] [blame] | 9436 | |
| Benjamin Kramer | f106d8b | 2013-05-21 09:58:54 +0000 | [diff] [blame] | 9437 | // Since SSE has no unsigned integer comparisons, we need to flip the sign |
| 9438 | // bits of the inputs before performing those operations. |
| 9439 | if (FlipSigns) { |
| 9440 | EVT EltVT = VT.getVectorElementType(); |
| 9441 | SDValue SB = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()), VT); |
| 9442 | Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SB); |
| 9443 | Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SB); |
| 9444 | } |
| 9445 | |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 9446 | SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1); |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9447 | |
| 9448 | // If the logical-not of the result is required, perform that now. |
| Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 9449 | if (Invert) |
| Dale Johannesen | ace1610 | 2009-02-03 19:33:06 +0000 | [diff] [blame] | 9450 | Result = DAG.getNOT(dl, Result, VT); |
| Bob Wilson | 4c24546 | 2009-01-22 17:39:32 +0000 | [diff] [blame] | 9451 | |
| Nate Begeman | 30a0de9 | 2008-07-17 16:51:19 +0000 | [diff] [blame] | 9452 | return Result; |
| 9453 | } |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 9454 | |
| Craig Topper | 26827f3 | 2013-01-20 09:02:22 +0000 | [diff] [blame] | 9455 | SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { |
| 9456 | |
| 9457 | MVT VT = Op.getValueType().getSimpleVT(); |
| 9458 | |
| 9459 | if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG); |
| 9460 | |
| 9461 | assert(VT == MVT::i8 && "SetCC type must be 8-bit integer"); |
| 9462 | SDValue Op0 = Op.getOperand(0); |
| 9463 | SDValue Op1 = Op.getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9464 | SDLoc dl(Op); |
| Craig Topper | 26827f3 | 2013-01-20 09:02:22 +0000 | [diff] [blame] | 9465 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
| 9466 | |
| 9467 | // Optimize to BT if possible. |
| 9468 | // Lower (X & (1 << N)) == 0 to BT(X, N). |
| 9469 | // Lower ((X >>u N) & 1) != 0 to BT(X, N). |
| 9470 | // Lower ((X >>s N) & 1) != 0 to BT(X, N). |
| 9471 | if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() && |
| 9472 | Op1.getOpcode() == ISD::Constant && |
| 9473 | cast<ConstantSDNode>(Op1)->isNullValue() && |
| 9474 | (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
| 9475 | SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG); |
| 9476 | if (NewSetCC.getNode()) |
| 9477 | return NewSetCC; |
| 9478 | } |
| 9479 | |
| 9480 | // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of |
| 9481 | // these. |
| 9482 | if (Op1.getOpcode() == ISD::Constant && |
| 9483 | (cast<ConstantSDNode>(Op1)->getZExtValue() == 1 || |
| 9484 | cast<ConstantSDNode>(Op1)->isNullValue()) && |
| 9485 | (CC == ISD::SETEQ || CC == ISD::SETNE)) { |
| 9486 | |
| 9487 | // If the input is a setcc, then reuse the input setcc or use a new one with |
| 9488 | // the inverted condition. |
| 9489 | if (Op0.getOpcode() == X86ISD::SETCC) { |
| 9490 | X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0); |
| 9491 | bool Invert = (CC == ISD::SETNE) ^ |
| 9492 | cast<ConstantSDNode>(Op1)->isNullValue(); |
| 9493 | if (!Invert) return Op0; |
| 9494 | |
| 9495 | CCode = X86::GetOppositeBranchCondition(CCode); |
| 9496 | return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
| 9497 | DAG.getConstant(CCode, MVT::i8), Op0.getOperand(1)); |
| 9498 | } |
| 9499 | } |
| 9500 | |
| 9501 | bool isFP = Op1.getValueType().getSimpleVT().isFloatingPoint(); |
| 9502 | unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG); |
| 9503 | if (X86CC == X86::COND_INVALID) |
| 9504 | return SDValue(); |
| 9505 | |
| 9506 | SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, DAG); |
| 9507 | EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG); |
| 9508 | return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
| 9509 | DAG.getConstant(X86CC, MVT::i8), EFLAGS); |
| 9510 | } |
| 9511 | |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9512 | // isX86LogicalCmp - Return true if opcode is a X86 logical comparison. |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9513 | static bool isX86LogicalCmp(SDValue Op) { |
| 9514 | unsigned Opc = Op.getNode()->getOpcode(); |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 9515 | if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI || |
| 9516 | Opc == X86ISD::SAHF) |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9517 | return true; |
| 9518 | if (Op.getResNo() == 1 && |
| 9519 | (Opc == X86ISD::ADD || |
| 9520 | Opc == X86ISD::SUB || |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 9521 | Opc == X86ISD::ADC || |
| 9522 | Opc == X86ISD::SBB || |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9523 | Opc == X86ISD::SMUL || |
| 9524 | Opc == X86ISD::UMUL || |
| 9525 | Opc == X86ISD::INC || |
| Dan Gohman | e220c4b | 2009-09-18 19:59:53 +0000 | [diff] [blame] | 9526 | Opc == X86ISD::DEC || |
| 9527 | Opc == X86ISD::OR || |
| 9528 | Opc == X86ISD::XOR || |
| 9529 | Opc == X86ISD::AND)) |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9530 | return true; |
| 9531 | |
| Chris Lattner | 9637d5b | 2010-12-05 07:49:54 +0000 | [diff] [blame] | 9532 | if (Op.getResNo() == 2 && Opc == X86ISD::UMUL) |
| 9533 | return true; |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 9534 | |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9535 | return false; |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9536 | } |
| 9537 | |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9538 | static bool isZero(SDValue V) { |
| 9539 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(V); |
| 9540 | return C && C->isNullValue(); |
| 9541 | } |
| 9542 | |
| Evan Cheng | b64dd5f | 2012-08-07 22:21:00 +0000 | [diff] [blame] | 9543 | static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) { |
| 9544 | if (V.getOpcode() != ISD::TRUNCATE) |
| 9545 | return false; |
| 9546 | |
| 9547 | SDValue VOp0 = V.getOperand(0); |
| 9548 | unsigned InBits = VOp0.getValueSizeInBits(); |
| 9549 | unsigned Bits = V.getValueSizeInBits(); |
| 9550 | return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits)); |
| 9551 | } |
| 9552 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 9553 | SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { |
| Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 9554 | bool addTest = true; |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9555 | SDValue Cond = Op.getOperand(0); |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9556 | SDValue Op1 = Op.getOperand(1); |
| 9557 | SDValue Op2 = Op.getOperand(2); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9558 | SDLoc DL(Op); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9559 | SDValue CC; |
| Evan Cheng | 9bba894 | 2006-01-26 02:13:10 +0000 | [diff] [blame] | 9560 | |
| Dan Gohman | 1a49295 | 2009-10-20 16:22:37 +0000 | [diff] [blame] | 9561 | if (Cond.getOpcode() == ISD::SETCC) { |
| 9562 | SDValue NewCond = LowerSETCC(Cond, DAG); |
| 9563 | if (NewCond.getNode()) |
| 9564 | Cond = NewCond; |
| 9565 | } |
| Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 9566 | |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9567 | // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y |
| Chris Lattner | 96908b1 | 2010-12-05 02:00:51 +0000 | [diff] [blame] | 9568 | // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9569 | // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y |
| Chris Lattner | 96908b1 | 2010-12-05 02:00:51 +0000 | [diff] [blame] | 9570 | // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y |
| Evan Cheng | 8c7ecaf | 2010-01-26 02:00:44 +0000 | [diff] [blame] | 9571 | if (Cond.getOpcode() == X86ISD::SETCC && |
| Chris Lattner | 96908b1 | 2010-12-05 02:00:51 +0000 | [diff] [blame] | 9572 | Cond.getOperand(1).getOpcode() == X86ISD::CMP && |
| 9573 | isZero(Cond.getOperand(1).getOperand(1))) { |
| Evan Cheng | 8c7ecaf | 2010-01-26 02:00:44 +0000 | [diff] [blame] | 9574 | SDValue Cmp = Cond.getOperand(1); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 9575 | |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9576 | unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue(); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 9577 | |
| 9578 | if ((isAllOnes(Op1) || isAllOnes(Op2)) && |
| Chris Lattner | 96908b1 | 2010-12-05 02:00:51 +0000 | [diff] [blame] | 9579 | (CondCode == X86::COND_E || CondCode == X86::COND_NE)) { |
| 9580 | SDValue Y = isAllOnes(Op2) ? Op1 : Op2; |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9581 | |
| 9582 | SDValue CmpOp0 = Cmp.getOperand(0); |
| Manman Ren | ed57984 | 2012-05-07 18:06:23 +0000 | [diff] [blame] | 9583 | // Apply further optimizations for special cases |
| 9584 | // (select (x != 0), -1, 0) -> neg & sbb |
| 9585 | // (select (x == 0), 0, -1) -> neg & sbb |
| 9586 | if (ConstantSDNode *YC = dyn_cast<ConstantSDNode>(Y)) |
| Chad Rosier | a20e1e7 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 9587 | if (YC->isNullValue() && |
| Manman Ren | ed57984 | 2012-05-07 18:06:23 +0000 | [diff] [blame] | 9588 | (isAllOnes(Op1) == (CondCode == X86::COND_NE))) { |
| 9589 | SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32); |
| Chad Rosier | a20e1e7 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 9590 | SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs, |
| 9591 | DAG.getConstant(0, CmpOp0.getValueType()), |
| Manman Ren | ed57984 | 2012-05-07 18:06:23 +0000 | [diff] [blame] | 9592 | CmpOp0); |
| 9593 | SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(), |
| 9594 | DAG.getConstant(X86::COND_B, MVT::i8), |
| 9595 | SDValue(Neg.getNode(), 1)); |
| 9596 | return Res; |
| 9597 | } |
| 9598 | |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9599 | Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, |
| 9600 | CmpOp0, DAG.getConstant(1, CmpOp0.getValueType())); |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 9601 | Cmp = ConvertCmpIfNecessary(Cmp, DAG); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 9602 | |
| Chris Lattner | 96908b1 | 2010-12-05 02:00:51 +0000 | [diff] [blame] | 9603 | SDValue Res = // Res = 0 or -1. |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9604 | DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(), |
| 9605 | DAG.getConstant(X86::COND_B, MVT::i8), Cmp); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 9606 | |
| Chris Lattner | 96908b1 | 2010-12-05 02:00:51 +0000 | [diff] [blame] | 9607 | if (isAllOnes(Op1) != (CondCode == X86::COND_E)) |
| 9608 | Res = DAG.getNOT(DL, Res, Res.getValueType()); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 9609 | |
| Evan Cheng | 8c7ecaf | 2010-01-26 02:00:44 +0000 | [diff] [blame] | 9610 | ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(Op2); |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9611 | if (N2C == 0 || !N2C->isNullValue()) |
| 9612 | Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y); |
| 9613 | return Res; |
| Evan Cheng | 8c7ecaf | 2010-01-26 02:00:44 +0000 | [diff] [blame] | 9614 | } |
| 9615 | } |
| 9616 | |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9617 | // Look past (and (setcc_carry (cmp ...)), 1). |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 9618 | if (Cond.getOpcode() == ISD::AND && |
| 9619 | Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) { |
| 9620 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1)); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 9621 | if (C && C->getAPIntValue() == 1) |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 9622 | Cond = Cond.getOperand(0); |
| 9623 | } |
| 9624 | |
| Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 9625 | // If condition flag is set by a X86ISD::CMP, then use it as the condition |
| 9626 | // setting operand in place of the X86ISD::SETCC. |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 9627 | unsigned CondOpcode = Cond.getOpcode(); |
| 9628 | if (CondOpcode == X86ISD::SETCC || |
| 9629 | CondOpcode == X86ISD::SETCC_CARRY) { |
| Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 9630 | CC = Cond.getOperand(0); |
| 9631 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9632 | SDValue Cmp = Cond.getOperand(1); |
| Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 9633 | unsigned Opc = Cmp.getOpcode(); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 9634 | MVT VT = Op.getValueType().getSimpleVT(); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9635 | |
| Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 9636 | bool IllegalFPCMov = false; |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 9637 | if (VT.isFloatingPoint() && !VT.isVector() && |
| Chris Lattner | 7863116 | 2008-01-16 06:24:21 +0000 | [diff] [blame] | 9638 | !isScalarFPTypeInSSEReg(VT)) // FPStack? |
| Dan Gohman | 7810bfe | 2008-09-26 21:54:37 +0000 | [diff] [blame] | 9639 | IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue()); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9640 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 9641 | if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) || |
| 9642 | Opc == X86ISD::BT) { // FIXME |
| Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 9643 | Cond = Cmp; |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 9644 | addTest = false; |
| 9645 | } |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 9646 | } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO || |
| 9647 | CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO || |
| 9648 | ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) && |
| 9649 | Cond.getOperand(0).getValueType() != MVT::i8)) { |
| 9650 | SDValue LHS = Cond.getOperand(0); |
| 9651 | SDValue RHS = Cond.getOperand(1); |
| 9652 | unsigned X86Opcode; |
| 9653 | unsigned X86Cond; |
| 9654 | SDVTList VTs; |
| 9655 | switch (CondOpcode) { |
| 9656 | case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break; |
| 9657 | case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break; |
| 9658 | case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break; |
| 9659 | case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break; |
| 9660 | case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break; |
| 9661 | case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break; |
| 9662 | default: llvm_unreachable("unexpected overflowing operator"); |
| 9663 | } |
| 9664 | if (CondOpcode == ISD::UMULO) |
| 9665 | VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(), |
| 9666 | MVT::i32); |
| 9667 | else |
| 9668 | VTs = DAG.getVTList(LHS.getValueType(), MVT::i32); |
| 9669 | |
| 9670 | SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS); |
| 9671 | |
| 9672 | if (CondOpcode == ISD::UMULO) |
| 9673 | Cond = X86Op.getValue(2); |
| 9674 | else |
| 9675 | Cond = X86Op.getValue(1); |
| 9676 | |
| 9677 | CC = DAG.getConstant(X86Cond, MVT::i8); |
| 9678 | addTest = false; |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 9679 | } |
| 9680 | |
| 9681 | if (addTest) { |
| Evan Cheng | b64dd5f | 2012-08-07 22:21:00 +0000 | [diff] [blame] | 9682 | // Look pass the truncate if the high bits are known zero. |
| 9683 | if (isTruncWithZeroHighBitsInput(Cond, DAG)) |
| 9684 | Cond = Cond.getOperand(0); |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9685 | |
| 9686 | // We know the result of AND is compared against zero. Try to match |
| 9687 | // it to BT. |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 9688 | if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) { |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9689 | SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG); |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 9690 | if (NewSetCC.getNode()) { |
| 9691 | CC = NewSetCC.getOperand(0); |
| 9692 | Cond = NewSetCC.getOperand(1); |
| 9693 | addTest = false; |
| 9694 | } |
| 9695 | } |
| 9696 | } |
| 9697 | |
| 9698 | if (addTest) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9699 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
| Evan Cheng | 552f09a | 2010-04-26 19:06:11 +0000 | [diff] [blame] | 9700 | Cond = EmitTest(Cond, X86::COND_NE, DAG); |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 9701 | } |
| 9702 | |
| Benjamin Kramer | e915ff3 | 2010-12-22 23:09:28 +0000 | [diff] [blame] | 9703 | // a < b ? -1 : 0 -> RES = ~setcc_carry |
| 9704 | // a < b ? 0 : -1 -> RES = setcc_carry |
| 9705 | // a >= b ? -1 : 0 -> RES = setcc_carry |
| 9706 | // a >= b ? 0 : -1 -> RES = ~setcc_carry |
| Manman Ren | 39ad568 | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 9707 | if (Cond.getOpcode() == X86ISD::SUB) { |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 9708 | Cond = ConvertCmpIfNecessary(Cond, DAG); |
| Benjamin Kramer | e915ff3 | 2010-12-22 23:09:28 +0000 | [diff] [blame] | 9709 | unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue(); |
| 9710 | |
| 9711 | if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) && |
| 9712 | (isAllOnes(Op1) || isAllOnes(Op2)) && (isZero(Op1) || isZero(Op2))) { |
| 9713 | SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(), |
| 9714 | DAG.getConstant(X86::COND_B, MVT::i8), Cond); |
| 9715 | if (isAllOnes(Op1) != (CondCode == X86::COND_B)) |
| 9716 | return DAG.getNOT(DL, Res, Res.getValueType()); |
| 9717 | return Res; |
| 9718 | } |
| 9719 | } |
| 9720 | |
| Benjamin Kramer | 444dcce | 2012-10-13 10:39:49 +0000 | [diff] [blame] | 9721 | // X86 doesn't have an i8 cmov. If both operands are the result of a truncate |
| 9722 | // widen the cmov and push the truncate through. This avoids introducing a new |
| 9723 | // branch during isel and doesn't add any extensions. |
| 9724 | if (Op.getValueType() == MVT::i8 && |
| 9725 | Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) { |
| 9726 | SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0); |
| 9727 | if (T1.getValueType() == T2.getValueType() && |
| 9728 | // Blacklist CopyFromReg to avoid partial register stalls. |
| 9729 | T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){ |
| 9730 | SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue); |
| Benjamin Kramer | f8b65aa | 2012-10-13 12:50:19 +0000 | [diff] [blame] | 9731 | SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond); |
| Benjamin Kramer | 444dcce | 2012-10-13 10:39:49 +0000 | [diff] [blame] | 9732 | return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov); |
| 9733 | } |
| 9734 | } |
| 9735 | |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 9736 | // X86ISD::CMOV means set the result (which is operand 1) to the RHS if |
| 9737 | // condition is true. |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 9738 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); |
| Evan Cheng | 8c7ecaf | 2010-01-26 02:00:44 +0000 | [diff] [blame] | 9739 | SDValue Ops[] = { Op2, Op1, CC, Cond }; |
| Chris Lattner | a2b5600 | 2010-12-05 01:23:24 +0000 | [diff] [blame] | 9740 | return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops, array_lengthof(Ops)); |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 9741 | } |
| 9742 | |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9743 | SDValue X86TargetLowering::LowerSIGN_EXTEND(SDValue Op, |
| 9744 | SelectionDAG &DAG) const { |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 9745 | MVT VT = Op->getValueType(0).getSimpleVT(); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9746 | SDValue In = Op->getOperand(0); |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 9747 | MVT InVT = In.getValueType().getSimpleVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9748 | SDLoc dl(Op); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9749 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9750 | if ((VT != MVT::v4i64 || InVT != MVT::v4i32) && |
| 9751 | (VT != MVT::v8i32 || InVT != MVT::v8i16)) |
| 9752 | return SDValue(); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9753 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9754 | if (Subtarget->hasInt256()) |
| 9755 | return DAG.getNode(X86ISD::VSEXT_MOVL, dl, VT, In); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9756 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9757 | // Optimize vectors in AVX mode |
| 9758 | // Sign extend v8i16 to v8i32 and |
| 9759 | // v4i32 to v4i64 |
| 9760 | // |
| 9761 | // Divide input vector into two parts |
| 9762 | // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1} |
| 9763 | // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32 |
| 9764 | // concat the vectors to original VT |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9765 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9766 | unsigned NumElems = InVT.getVectorNumElements(); |
| 9767 | SDValue Undef = DAG.getUNDEF(InVT); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9768 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9769 | SmallVector<int,8> ShufMask1(NumElems, -1); |
| 9770 | for (unsigned i = 0; i != NumElems/2; ++i) |
| 9771 | ShufMask1[i] = i; |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9772 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9773 | SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9774 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9775 | SmallVector<int,8> ShufMask2(NumElems, -1); |
| 9776 | for (unsigned i = 0; i != NumElems/2; ++i) |
| 9777 | ShufMask2[i] = i + NumElems/2; |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9778 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9779 | SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9780 | |
| Craig Topper | a080daf | 2013-01-20 21:50:27 +0000 | [diff] [blame] | 9781 | MVT HalfVT = MVT::getVectorVT(VT.getScalarType(), |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9782 | VT.getVectorNumElements()/2); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9783 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9784 | OpLo = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpLo); |
| 9785 | OpHi = DAG.getNode(X86ISD::VSEXT_MOVL, dl, HalfVT, OpHi); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9786 | |
| Nadav Rotem | 587fb1d | 2012-12-27 23:08:05 +0000 | [diff] [blame] | 9787 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi); |
| Nadav Rotem | 1a330af | 2012-12-27 22:47:16 +0000 | [diff] [blame] | 9788 | } |
| 9789 | |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9790 | // isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or |
| 9791 | // ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart |
| 9792 | // from the AND / OR. |
| 9793 | static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) { |
| 9794 | Opc = Op.getOpcode(); |
| 9795 | if (Opc != ISD::OR && Opc != ISD::AND) |
| 9796 | return false; |
| 9797 | return (Op.getOperand(0).getOpcode() == X86ISD::SETCC && |
| 9798 | Op.getOperand(0).hasOneUse() && |
| 9799 | Op.getOperand(1).getOpcode() == X86ISD::SETCC && |
| 9800 | Op.getOperand(1).hasOneUse()); |
| 9801 | } |
| 9802 | |
| Evan Cheng | 961d6d4 | 2009-02-02 08:19:07 +0000 | [diff] [blame] | 9803 | // isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and |
| 9804 | // 1 and that the SETCC node has a single use. |
| Evan Cheng | 67ad9db | 2009-02-02 08:07:36 +0000 | [diff] [blame] | 9805 | static bool isXor1OfSetCC(SDValue Op) { |
| 9806 | if (Op.getOpcode() != ISD::XOR) |
| 9807 | return false; |
| 9808 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); |
| 9809 | if (N1C && N1C->getAPIntValue() == 1) { |
| 9810 | return Op.getOperand(0).getOpcode() == X86ISD::SETCC && |
| 9811 | Op.getOperand(0).hasOneUse(); |
| 9812 | } |
| 9813 | return false; |
| 9814 | } |
| 9815 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 9816 | SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const { |
| Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 9817 | bool addTest = true; |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9818 | SDValue Chain = Op.getOperand(0); |
| 9819 | SDValue Cond = Op.getOperand(1); |
| 9820 | SDValue Dest = Op.getOperand(2); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 9821 | SDLoc dl(Op); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9822 | SDValue CC; |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 9823 | bool Inverted = false; |
| Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 9824 | |
| Dan Gohman | 1a49295 | 2009-10-20 16:22:37 +0000 | [diff] [blame] | 9825 | if (Cond.getOpcode() == ISD::SETCC) { |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 9826 | // Check for setcc([su]{add,sub,mul}o == 0). |
| 9827 | if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ && |
| 9828 | isa<ConstantSDNode>(Cond.getOperand(1)) && |
| 9829 | cast<ConstantSDNode>(Cond.getOperand(1))->isNullValue() && |
| 9830 | Cond.getOperand(0).getResNo() == 1 && |
| 9831 | (Cond.getOperand(0).getOpcode() == ISD::SADDO || |
| 9832 | Cond.getOperand(0).getOpcode() == ISD::UADDO || |
| 9833 | Cond.getOperand(0).getOpcode() == ISD::SSUBO || |
| 9834 | Cond.getOperand(0).getOpcode() == ISD::USUBO || |
| 9835 | Cond.getOperand(0).getOpcode() == ISD::SMULO || |
| 9836 | Cond.getOperand(0).getOpcode() == ISD::UMULO)) { |
| 9837 | Inverted = true; |
| 9838 | Cond = Cond.getOperand(0); |
| 9839 | } else { |
| 9840 | SDValue NewCond = LowerSETCC(Cond, DAG); |
| 9841 | if (NewCond.getNode()) |
| 9842 | Cond = NewCond; |
| 9843 | } |
| Dan Gohman | 1a49295 | 2009-10-20 16:22:37 +0000 | [diff] [blame] | 9844 | } |
| Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 9845 | #if 0 |
| 9846 | // FIXME: LowerXALUO doesn't handle these!! |
| Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 9847 | else if (Cond.getOpcode() == X86ISD::ADD || |
| 9848 | Cond.getOpcode() == X86ISD::SUB || |
| 9849 | Cond.getOpcode() == X86ISD::SMUL || |
| 9850 | Cond.getOpcode() == X86ISD::UMUL) |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 9851 | Cond = LowerXALUO(Cond, DAG); |
| Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 9852 | #endif |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 9853 | |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 9854 | // Look pass (and (setcc_carry (cmp ...)), 1). |
| 9855 | if (Cond.getOpcode() == ISD::AND && |
| 9856 | Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) { |
| 9857 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Cond.getOperand(1)); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 9858 | if (C && C->getAPIntValue() == 1) |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 9859 | Cond = Cond.getOperand(0); |
| 9860 | } |
| 9861 | |
| Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 9862 | // If condition flag is set by a X86ISD::CMP, then use it as the condition |
| 9863 | // setting operand in place of the X86ISD::SETCC. |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 9864 | unsigned CondOpcode = Cond.getOpcode(); |
| 9865 | if (CondOpcode == X86ISD::SETCC || |
| 9866 | CondOpcode == X86ISD::SETCC_CARRY) { |
| Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 9867 | CC = Cond.getOperand(0); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 9868 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 9869 | SDValue Cmp = Cond.getOperand(1); |
| Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 9870 | unsigned Opc = Cmp.getOpcode(); |
| Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 9871 | // FIXME: WHY THE SPECIAL CASING OF LogicalCmp?? |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9872 | if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) { |
| Evan Cheng | 3f41d66 | 2007-10-08 22:16:29 +0000 | [diff] [blame] | 9873 | Cond = Cmp; |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 9874 | addTest = false; |
| Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 9875 | } else { |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9876 | switch (cast<ConstantSDNode>(CC)->getZExtValue()) { |
| Bill Wendling | 0ea25cb | 2008-12-03 08:32:02 +0000 | [diff] [blame] | 9877 | default: break; |
| 9878 | case X86::COND_O: |
| Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 9879 | case X86::COND_B: |
| Chris Lattner | e55484e | 2008-12-25 05:34:37 +0000 | [diff] [blame] | 9880 | // These can only come from an arithmetic instruction with overflow, |
| 9881 | // e.g. SADDO, UADDO. |
| Bill Wendling | 0ea25cb | 2008-12-03 08:32:02 +0000 | [diff] [blame] | 9882 | Cond = Cond.getNode()->getOperand(1); |
| 9883 | addTest = false; |
| 9884 | break; |
| Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 9885 | } |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 9886 | } |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 9887 | } |
| 9888 | CondOpcode = Cond.getOpcode(); |
| 9889 | if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO || |
| 9890 | CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO || |
| 9891 | ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) && |
| 9892 | Cond.getOperand(0).getValueType() != MVT::i8)) { |
| 9893 | SDValue LHS = Cond.getOperand(0); |
| 9894 | SDValue RHS = Cond.getOperand(1); |
| 9895 | unsigned X86Opcode; |
| 9896 | unsigned X86Cond; |
| 9897 | SDVTList VTs; |
| 9898 | switch (CondOpcode) { |
| 9899 | case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break; |
| 9900 | case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break; |
| 9901 | case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break; |
| 9902 | case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break; |
| 9903 | case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break; |
| 9904 | case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break; |
| 9905 | default: llvm_unreachable("unexpected overflowing operator"); |
| 9906 | } |
| 9907 | if (Inverted) |
| 9908 | X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond); |
| 9909 | if (CondOpcode == ISD::UMULO) |
| 9910 | VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(), |
| 9911 | MVT::i32); |
| 9912 | else |
| 9913 | VTs = DAG.getVTList(LHS.getValueType(), MVT::i32); |
| 9914 | |
| 9915 | SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS); |
| 9916 | |
| 9917 | if (CondOpcode == ISD::UMULO) |
| 9918 | Cond = X86Op.getValue(2); |
| 9919 | else |
| 9920 | Cond = X86Op.getValue(1); |
| 9921 | |
| 9922 | CC = DAG.getConstant(X86Cond, MVT::i8); |
| 9923 | addTest = false; |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9924 | } else { |
| 9925 | unsigned CondOpc; |
| 9926 | if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) { |
| 9927 | SDValue Cmp = Cond.getOperand(0).getOperand(1); |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9928 | if (CondOpc == ISD::OR) { |
| 9929 | // Also, recognize the pattern generated by an FCMP_UNE. We can emit |
| 9930 | // two branches instead of an explicit OR instruction with a |
| 9931 | // separate test. |
| 9932 | if (Cmp == Cond.getOperand(1).getOperand(1) && |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9933 | isX86LogicalCmp(Cmp)) { |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9934 | CC = Cond.getOperand(0).getOperand(0); |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 9935 | Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9936 | Chain, Dest, CC, Cmp); |
| 9937 | CC = Cond.getOperand(1).getOperand(0); |
| 9938 | Cond = Cmp; |
| 9939 | addTest = false; |
| 9940 | } |
| 9941 | } else { // ISD::AND |
| 9942 | // Also, recognize the pattern generated by an FCMP_OEQ. We can emit |
| 9943 | // two branches instead of an explicit AND instruction with a |
| 9944 | // separate test. However, we only do this if this block doesn't |
| 9945 | // have a fall-through edge, because this requires an explicit |
| 9946 | // jmp when the condition is false. |
| 9947 | if (Cmp == Cond.getOperand(1).getOperand(1) && |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 9948 | isX86LogicalCmp(Cmp) && |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9949 | Op.getNode()->hasOneUse()) { |
| 9950 | X86::CondCode CCode = |
| 9951 | (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0); |
| 9952 | CCode = X86::GetOppositeBranchCondition(CCode); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9953 | CC = DAG.getConstant(CCode, MVT::i8); |
| Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 9954 | SDNode *User = *Op.getNode()->use_begin(); |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9955 | // Look for an unconditional branch following this conditional branch. |
| 9956 | // We need this because we need to reverse the successors in order |
| 9957 | // to implement FCMP_OEQ. |
| Dan Gohman | 027657d | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 9958 | if (User->getOpcode() == ISD::BR) { |
| 9959 | SDValue FalseBB = User->getOperand(1); |
| 9960 | SDNode *NewBR = |
| 9961 | DAG.UpdateNodeOperands(User, User->getOperand(0), Dest); |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9962 | assert(NewBR == User); |
| Nick Lewycky | 2a3ee5e | 2010-06-20 20:27:42 +0000 | [diff] [blame] | 9963 | (void)NewBR; |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9964 | Dest = FalseBB; |
| Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 9965 | |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 9966 | Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9967 | Chain, Dest, CC, Cmp); |
| 9968 | X86::CondCode CCode = |
| 9969 | (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0); |
| 9970 | CCode = X86::GetOppositeBranchCondition(CCode); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9971 | CC = DAG.getConstant(CCode, MVT::i8); |
| Evan Cheng | 370e534 | 2008-12-03 08:38:43 +0000 | [diff] [blame] | 9972 | Cond = Cmp; |
| 9973 | addTest = false; |
| 9974 | } |
| 9975 | } |
| Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 9976 | } |
| Evan Cheng | 67ad9db | 2009-02-02 08:07:36 +0000 | [diff] [blame] | 9977 | } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) { |
| 9978 | // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition. |
| 9979 | // It should be transformed during dag combiner except when the condition |
| 9980 | // is set by a arithmetics with overflow node. |
| 9981 | X86::CondCode CCode = |
| 9982 | (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0); |
| 9983 | CCode = X86::GetOppositeBranchCondition(CCode); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 9984 | CC = DAG.getConstant(CCode, MVT::i8); |
| Evan Cheng | 67ad9db | 2009-02-02 08:07:36 +0000 | [diff] [blame] | 9985 | Cond = Cond.getOperand(0).getOperand(1); |
| 9986 | addTest = false; |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 9987 | } else if (Cond.getOpcode() == ISD::SETCC && |
| 9988 | cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) { |
| 9989 | // For FCMP_OEQ, we can emit |
| 9990 | // two branches instead of an explicit AND instruction with a |
| 9991 | // separate test. However, we only do this if this block doesn't |
| 9992 | // have a fall-through edge, because this requires an explicit |
| 9993 | // jmp when the condition is false. |
| 9994 | if (Op.getNode()->hasOneUse()) { |
| 9995 | SDNode *User = *Op.getNode()->use_begin(); |
| 9996 | // Look for an unconditional branch following this conditional branch. |
| 9997 | // We need this because we need to reverse the successors in order |
| 9998 | // to implement FCMP_OEQ. |
| 9999 | if (User->getOpcode() == ISD::BR) { |
| 10000 | SDValue FalseBB = User->getOperand(1); |
| 10001 | SDNode *NewBR = |
| 10002 | DAG.UpdateNodeOperands(User, User->getOperand(0), Dest); |
| 10003 | assert(NewBR == User); |
| 10004 | (void)NewBR; |
| 10005 | Dest = FalseBB; |
| 10006 | |
| 10007 | SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32, |
| 10008 | Cond.getOperand(0), Cond.getOperand(1)); |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 10009 | Cmp = ConvertCmpIfNecessary(Cmp, DAG); |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 10010 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
| 10011 | Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
| 10012 | Chain, Dest, CC, Cmp); |
| 10013 | CC = DAG.getConstant(X86::COND_P, MVT::i8); |
| 10014 | Cond = Cmp; |
| 10015 | addTest = false; |
| 10016 | } |
| 10017 | } |
| 10018 | } else if (Cond.getOpcode() == ISD::SETCC && |
| 10019 | cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) { |
| 10020 | // For FCMP_UNE, we can emit |
| 10021 | // two branches instead of an explicit AND instruction with a |
| 10022 | // separate test. However, we only do this if this block doesn't |
| 10023 | // have a fall-through edge, because this requires an explicit |
| 10024 | // jmp when the condition is false. |
| 10025 | if (Op.getNode()->hasOneUse()) { |
| 10026 | SDNode *User = *Op.getNode()->use_begin(); |
| 10027 | // Look for an unconditional branch following this conditional branch. |
| 10028 | // We need this because we need to reverse the successors in order |
| 10029 | // to implement FCMP_UNE. |
| 10030 | if (User->getOpcode() == ISD::BR) { |
| 10031 | SDValue FalseBB = User->getOperand(1); |
| 10032 | SDNode *NewBR = |
| 10033 | DAG.UpdateNodeOperands(User, User->getOperand(0), Dest); |
| 10034 | assert(NewBR == User); |
| 10035 | (void)NewBR; |
| 10036 | |
| 10037 | SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32, |
| 10038 | Cond.getOperand(0), Cond.getOperand(1)); |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 10039 | Cmp = ConvertCmpIfNecessary(Cmp, DAG); |
| Dan Gohman | 65fd656 | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 10040 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
| 10041 | Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
| 10042 | Chain, Dest, CC, Cmp); |
| 10043 | CC = DAG.getConstant(X86::COND_NP, MVT::i8); |
| 10044 | Cond = Cmp; |
| 10045 | addTest = false; |
| 10046 | Dest = FalseBB; |
| 10047 | } |
| 10048 | } |
| Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 10049 | } |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 10050 | } |
| 10051 | |
| 10052 | if (addTest) { |
| Evan Cheng | b64dd5f | 2012-08-07 22:21:00 +0000 | [diff] [blame] | 10053 | // Look pass the truncate if the high bits are known zero. |
| 10054 | if (isTruncWithZeroHighBitsInput(Cond, DAG)) |
| 10055 | Cond = Cond.getOperand(0); |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 10056 | |
| 10057 | // We know the result of AND is compared against zero. Try to match |
| 10058 | // it to BT. |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 10059 | if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) { |
| Evan Cheng | d40d03e | 2010-01-06 19:38:29 +0000 | [diff] [blame] | 10060 | SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG); |
| 10061 | if (NewSetCC.getNode()) { |
| 10062 | CC = NewSetCC.getOperand(0); |
| 10063 | Cond = NewSetCC.getOperand(1); |
| 10064 | addTest = false; |
| 10065 | } |
| 10066 | } |
| 10067 | } |
| 10068 | |
| 10069 | if (addTest) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 10070 | CC = DAG.getConstant(X86::COND_NE, MVT::i8); |
| Evan Cheng | 552f09a | 2010-04-26 19:06:11 +0000 | [diff] [blame] | 10071 | Cond = EmitTest(Cond, X86::COND_NE, DAG); |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 10072 | } |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 10073 | Cond = ConvertCmpIfNecessary(Cond, DAG); |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 10074 | return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(), |
| Dan Gohman | 279c22e | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 10075 | Chain, Dest, CC, Cond); |
| Evan Cheng | 0488db9 | 2007-09-25 01:57:46 +0000 | [diff] [blame] | 10076 | } |
| 10077 | |
| Anton Korobeynikov | e060b53 | 2007-04-17 19:34:00 +0000 | [diff] [blame] | 10078 | // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets. |
| 10079 | // Calls to _alloca is needed to probe the stack when allocating more than 4k |
| 10080 | // bytes in one go. Touching the stack at 4K increments is necessary to ensure |
| 10081 | // that the guard pages used by the OS virtual memory manager are allocated in |
| 10082 | // correct sequence. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10083 | SDValue |
| 10084 | X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 10085 | SelectionDAG &DAG) const { |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10086 | assert((Subtarget->isTargetCygMing() || Subtarget->isTargetWindows() || |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 10087 | getTargetMachine().Options.EnableSegmentedStacks) && |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10088 | "This should be used only on Windows targets or when segmented stacks " |
| Rafael Espindola | 96428ce | 2011-09-06 18:43:08 +0000 | [diff] [blame] | 10089 | "are being used"); |
| 10090 | assert(!Subtarget->isTargetEnvMacho() && "Not implemented"); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10091 | SDLoc dl(Op); |
| Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 10092 | |
| Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 10093 | // Get the inputs. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10094 | SDValue Chain = Op.getOperand(0); |
| 10095 | SDValue Size = Op.getOperand(1); |
| Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 10096 | // FIXME: Ensure alignment here |
| 10097 | |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10098 | bool Is64Bit = Subtarget->is64Bit(); |
| 10099 | EVT SPTy = Is64Bit ? MVT::i64 : MVT::i32; |
| Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 10100 | |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 10101 | if (getTargetMachine().Options.EnableSegmentedStacks) { |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10102 | MachineFunction &MF = DAG.getMachineFunction(); |
| 10103 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 10104 | |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10105 | if (Is64Bit) { |
| 10106 | // The 64 bit implementation of segmented stacks needs to clobber both r10 |
| Rafael Espindola | 96428ce | 2011-09-06 18:43:08 +0000 | [diff] [blame] | 10107 | // r11. This makes it impossible to use it along with nested parameters. |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10108 | const Function *F = MF.getFunction(); |
| Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 10109 | |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10110 | for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); |
| Craig Topper | 31a207a | 2012-05-04 06:39:13 +0000 | [diff] [blame] | 10111 | I != E; ++I) |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10112 | if (I->hasNestAttr()) |
| 10113 | report_fatal_error("Cannot use segmented stacks with functions that " |
| 10114 | "have nested arguments."); |
| 10115 | } |
| Anton Korobeynikov | 4304bcc | 2007-07-05 20:36:08 +0000 | [diff] [blame] | 10116 | |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10117 | const TargetRegisterClass *AddrRegClass = |
| 10118 | getRegClassFor(Subtarget->is64Bit() ? MVT::i64:MVT::i32); |
| 10119 | unsigned Vreg = MRI.createVirtualRegister(AddrRegClass); |
| 10120 | Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size); |
| 10121 | SDValue Value = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain, |
| 10122 | DAG.getRegister(Vreg, SPTy)); |
| 10123 | SDValue Ops1[2] = { Value, Chain }; |
| 10124 | return DAG.getMergeValues(Ops1, 2, dl); |
| 10125 | } else { |
| 10126 | SDValue Flag; |
| 10127 | unsigned Reg = (Subtarget->is64Bit() ? X86::RAX : X86::EAX); |
| Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 10128 | |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10129 | Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag); |
| 10130 | Flag = Chain.getValue(1); |
| 10131 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
| Anton Korobeynikov | 096b461 | 2008-06-11 20:16:42 +0000 | [diff] [blame] | 10132 | |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10133 | Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag); |
| 10134 | Flag = Chain.getValue(1); |
| 10135 | |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 10136 | const X86RegisterInfo *RegInfo = |
| 10137 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| Michael Liao | c5c970e | 2012-10-31 04:14:09 +0000 | [diff] [blame] | 10138 | Chain = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(), |
| 10139 | SPTy).getValue(1); |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 10140 | |
| 10141 | SDValue Ops1[2] = { Chain.getValue(0), Chain }; |
| 10142 | return DAG.getMergeValues(Ops1, 2, dl); |
| 10143 | } |
| Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 10144 | } |
| 10145 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 10146 | SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const { |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 10147 | MachineFunction &MF = DAG.getMachineFunction(); |
| 10148 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); |
| 10149 | |
| Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 10150 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10151 | SDLoc DL(Op); |
| Evan Cheng | 8b2794a | 2006-10-13 21:14:26 +0000 | [diff] [blame] | 10152 | |
| Anton Korobeynikov | e7beda1 | 2010-10-03 22:52:07 +0000 | [diff] [blame] | 10153 | if (!Subtarget->is64Bit() || Subtarget->isTargetWin64()) { |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 10154 | // vastart just stores the address of the VarArgsFrameIndex slot into the |
| 10155 | // memory location argument. |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 10156 | SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), |
| 10157 | getPointerTy()); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10158 | return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1), |
| 10159 | MachinePointerInfo(SV), false, false, 0); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 10160 | } |
| 10161 | |
| 10162 | // __va_list_tag: |
| 10163 | // gp_offset (0 - 6 * 8) |
| 10164 | // fp_offset (48 - 48 + 8 * 16) |
| 10165 | // overflow_arg_area (point to parameters coming in memory). |
| 10166 | // reg_save_area |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10167 | SmallVector<SDValue, 8> MemOps; |
| 10168 | SDValue FIN = Op.getOperand(1); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 10169 | // Store gp_offset |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10170 | SDValue Store = DAG.getStore(Op.getOperand(0), DL, |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 10171 | DAG.getConstant(FuncInfo->getVarArgsGPOffset(), |
| 10172 | MVT::i32), |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10173 | FIN, MachinePointerInfo(SV), false, false, 0); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 10174 | MemOps.push_back(Store); |
| 10175 | |
| 10176 | // Store fp_offset |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10177 | FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 10178 | FIN, DAG.getIntPtrConstant(4)); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10179 | Store = DAG.getStore(Op.getOperand(0), DL, |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 10180 | DAG.getConstant(FuncInfo->getVarArgsFPOffset(), |
| 10181 | MVT::i32), |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10182 | FIN, MachinePointerInfo(SV, 4), false, false, 0); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 10183 | MemOps.push_back(Store); |
| 10184 | |
| 10185 | // Store ptr to overflow_arg_area |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10186 | FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 10187 | FIN, DAG.getIntPtrConstant(4)); |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 10188 | SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), |
| 10189 | getPointerTy()); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10190 | Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN, |
| 10191 | MachinePointerInfo(SV, 8), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 10192 | false, false, 0); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 10193 | MemOps.push_back(Store); |
| 10194 | |
| 10195 | // Store ptr to reg_save_area. |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10196 | FIN = DAG.getNode(ISD::ADD, DL, getPointerTy(), |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 10197 | FIN, DAG.getIntPtrConstant(8)); |
| Dan Gohman | 1e93df6 | 2010-04-17 14:41:14 +0000 | [diff] [blame] | 10198 | SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), |
| 10199 | getPointerTy()); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10200 | Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN, |
| 10201 | MachinePointerInfo(SV, 16), false, false, 0); |
| Evan Cheng | 25ab690 | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 10202 | MemOps.push_back(Store); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 10203 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 10204 | &MemOps[0], MemOps.size()); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10205 | } |
| 10206 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 10207 | SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const { |
| Dan Gohman | 320afb8 | 2010-10-12 18:00:49 +0000 | [diff] [blame] | 10208 | assert(Subtarget->is64Bit() && |
| 10209 | "LowerVAARG only handles 64-bit va_arg!"); |
| 10210 | assert((Subtarget->isTargetLinux() || |
| 10211 | Subtarget->isTargetDarwin()) && |
| 10212 | "Unhandled target in LowerVAARG"); |
| 10213 | assert(Op.getNode()->getNumOperands() == 4); |
| 10214 | SDValue Chain = Op.getOperand(0); |
| 10215 | SDValue SrcPtr = Op.getOperand(1); |
| 10216 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
| 10217 | unsigned Align = Op.getConstantOperandVal(3); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10218 | SDLoc dl(Op); |
| Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 10219 | |
| Dan Gohman | 320afb8 | 2010-10-12 18:00:49 +0000 | [diff] [blame] | 10220 | EVT ArgVT = Op.getNode()->getValueType(0); |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 10221 | Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); |
| Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 10222 | uint32_t ArgSize = getDataLayout()->getTypeAllocSize(ArgTy); |
| Dan Gohman | 320afb8 | 2010-10-12 18:00:49 +0000 | [diff] [blame] | 10223 | uint8_t ArgMode; |
| 10224 | |
| 10225 | // Decide which area this value should be read from. |
| 10226 | // TODO: Implement the AMD64 ABI in its entirety. This simple |
| 10227 | // selection mechanism works only for the basic types. |
| 10228 | if (ArgVT == MVT::f80) { |
| 10229 | llvm_unreachable("va_arg for f80 not yet implemented"); |
| 10230 | } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) { |
| 10231 | ArgMode = 2; // Argument passed in XMM register. Use fp_offset. |
| 10232 | } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) { |
| 10233 | ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset. |
| 10234 | } else { |
| 10235 | llvm_unreachable("Unhandled argument type in LowerVAARG"); |
| 10236 | } |
| 10237 | |
| 10238 | if (ArgMode == 2) { |
| 10239 | // Sanity Check: Make sure using fp_offset makes sense. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 10240 | assert(!getTargetMachine().Options.UseSoftFloat && |
| Eric Christopher | 52b4505 | 2010-10-12 19:44:17 +0000 | [diff] [blame] | 10241 | !(DAG.getMachineFunction() |
| Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 10242 | .getFunction()->getAttributes() |
| 10243 | .hasAttribute(AttributeSet::FunctionIndex, |
| 10244 | Attribute::NoImplicitFloat)) && |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 10245 | Subtarget->hasSSE1()); |
| Dan Gohman | 320afb8 | 2010-10-12 18:00:49 +0000 | [diff] [blame] | 10246 | } |
| 10247 | |
| 10248 | // Insert VAARG_64 node into the DAG |
| 10249 | // VAARG_64 returns two values: Variable Argument Address, Chain |
| 10250 | SmallVector<SDValue, 11> InstOps; |
| 10251 | InstOps.push_back(Chain); |
| 10252 | InstOps.push_back(SrcPtr); |
| 10253 | InstOps.push_back(DAG.getConstant(ArgSize, MVT::i32)); |
| 10254 | InstOps.push_back(DAG.getConstant(ArgMode, MVT::i8)); |
| 10255 | InstOps.push_back(DAG.getConstant(Align, MVT::i32)); |
| 10256 | SDVTList VTs = DAG.getVTList(getPointerTy(), MVT::Other); |
| 10257 | SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl, |
| 10258 | VTs, &InstOps[0], InstOps.size(), |
| 10259 | MVT::i64, |
| 10260 | MachinePointerInfo(SV), |
| 10261 | /*Align=*/0, |
| 10262 | /*Volatile=*/false, |
| 10263 | /*ReadMem=*/true, |
| 10264 | /*WriteMem=*/true); |
| 10265 | Chain = VAARG.getValue(1); |
| 10266 | |
| 10267 | // Load the next argument and return it |
| 10268 | return DAG.getLoad(ArgVT, dl, |
| 10269 | Chain, |
| 10270 | VAARG, |
| 10271 | MachinePointerInfo(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 10272 | false, false, false, 0); |
| Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 10273 | } |
| 10274 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 10275 | static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget, |
| 10276 | SelectionDAG &DAG) { |
| Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 10277 | // X86-64 va_list is a struct { i32, i32, i8*, i8* }. |
| Dan Gohman | 2826913 | 2008-04-18 20:55:41 +0000 | [diff] [blame] | 10278 | assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!"); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10279 | SDValue Chain = Op.getOperand(0); |
| 10280 | SDValue DstPtr = Op.getOperand(1); |
| 10281 | SDValue SrcPtr = Op.getOperand(2); |
| Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 10282 | const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); |
| 10283 | const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10284 | SDLoc DL(Op); |
| Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 10285 | |
| Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 10286 | return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, |
| Mon P Wang | 20adc9d | 2010-04-04 03:10:48 +0000 | [diff] [blame] | 10287 | DAG.getIntPtrConstant(24), 8, /*isVolatile*/false, |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 10288 | false, |
| Chris Lattner | e72f202 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 10289 | MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV)); |
| Evan Cheng | ae64219 | 2007-03-02 23:16:35 +0000 | [diff] [blame] | 10290 | } |
| 10291 | |
| Craig Topper | ff3139f | 2013-02-19 07:43:59 +0000 | [diff] [blame] | 10292 | // getTargetVShiftNode - Handle vector element shifts where the shift amount |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10293 | // may or may not be a constant. Takes immediate version of shift as input. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10294 | static SDValue getTargetVShiftNode(unsigned Opc, SDLoc dl, EVT VT, |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10295 | SDValue SrcOp, SDValue ShAmt, |
| 10296 | SelectionDAG &DAG) { |
| 10297 | assert(ShAmt.getValueType() == MVT::i32 && "ShAmt is not i32"); |
| 10298 | |
| 10299 | if (isa<ConstantSDNode>(ShAmt)) { |
| Nadav Rotem | d896e24 | 2012-07-15 20:27:43 +0000 | [diff] [blame] | 10300 | // Constant may be a TargetConstant. Use a regular constant. |
| 10301 | uint32_t ShiftAmt = cast<ConstantSDNode>(ShAmt)->getZExtValue(); |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10302 | switch (Opc) { |
| 10303 | default: llvm_unreachable("Unknown target vector shift node"); |
| 10304 | case X86ISD::VSHLI: |
| 10305 | case X86ISD::VSRLI: |
| 10306 | case X86ISD::VSRAI: |
| Nadav Rotem | d896e24 | 2012-07-15 20:27:43 +0000 | [diff] [blame] | 10307 | return DAG.getNode(Opc, dl, VT, SrcOp, |
| 10308 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10309 | } |
| 10310 | } |
| 10311 | |
| 10312 | // Change opcode to non-immediate version |
| 10313 | switch (Opc) { |
| 10314 | default: llvm_unreachable("Unknown target vector shift node"); |
| 10315 | case X86ISD::VSHLI: Opc = X86ISD::VSHL; break; |
| 10316 | case X86ISD::VSRLI: Opc = X86ISD::VSRL; break; |
| 10317 | case X86ISD::VSRAI: Opc = X86ISD::VSRA; break; |
| 10318 | } |
| 10319 | |
| 10320 | // Need to build a vector containing shift amount |
| 10321 | // Shift amount is 32-bits, but SSE instructions read 64-bit, so fill with 0 |
| 10322 | SDValue ShOps[4]; |
| 10323 | ShOps[0] = ShAmt; |
| 10324 | ShOps[1] = DAG.getConstant(0, MVT::i32); |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10325 | ShOps[2] = ShOps[3] = DAG.getUNDEF(MVT::i32); |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10326 | ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, &ShOps[0], 4); |
| Nadav Rotem | 65f489f | 2012-07-14 22:26:05 +0000 | [diff] [blame] | 10327 | |
| 10328 | // The return type has to be a 128-bit type with the same element |
| 10329 | // type as the input type. |
| 10330 | MVT EltVT = VT.getVectorElementType().getSimpleVT(); |
| 10331 | EVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits()); |
| 10332 | |
| 10333 | ShAmt = DAG.getNode(ISD::BITCAST, dl, ShVT, ShAmt); |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10334 | return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt); |
| 10335 | } |
| 10336 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 10337 | static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10338 | SDLoc dl(Op); |
| Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 10339 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10340 | switch (IntNo) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10341 | default: return SDValue(); // Don't custom lower most intrinsics. |
| Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 10342 | // Comparison intrinsics. |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10343 | case Intrinsic::x86_sse_comieq_ss: |
| 10344 | case Intrinsic::x86_sse_comilt_ss: |
| 10345 | case Intrinsic::x86_sse_comile_ss: |
| 10346 | case Intrinsic::x86_sse_comigt_ss: |
| 10347 | case Intrinsic::x86_sse_comige_ss: |
| 10348 | case Intrinsic::x86_sse_comineq_ss: |
| 10349 | case Intrinsic::x86_sse_ucomieq_ss: |
| 10350 | case Intrinsic::x86_sse_ucomilt_ss: |
| 10351 | case Intrinsic::x86_sse_ucomile_ss: |
| 10352 | case Intrinsic::x86_sse_ucomigt_ss: |
| 10353 | case Intrinsic::x86_sse_ucomige_ss: |
| 10354 | case Intrinsic::x86_sse_ucomineq_ss: |
| 10355 | case Intrinsic::x86_sse2_comieq_sd: |
| 10356 | case Intrinsic::x86_sse2_comilt_sd: |
| 10357 | case Intrinsic::x86_sse2_comile_sd: |
| 10358 | case Intrinsic::x86_sse2_comigt_sd: |
| 10359 | case Intrinsic::x86_sse2_comige_sd: |
| 10360 | case Intrinsic::x86_sse2_comineq_sd: |
| 10361 | case Intrinsic::x86_sse2_ucomieq_sd: |
| 10362 | case Intrinsic::x86_sse2_ucomilt_sd: |
| 10363 | case Intrinsic::x86_sse2_ucomile_sd: |
| 10364 | case Intrinsic::x86_sse2_ucomigt_sd: |
| 10365 | case Intrinsic::x86_sse2_ucomige_sd: |
| 10366 | case Intrinsic::x86_sse2_ucomineq_sd: { |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10367 | unsigned Opc; |
| 10368 | ISD::CondCode CC; |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10369 | switch (IntNo) { |
| Craig Topper | 86c7c58 | 2012-01-30 01:10:15 +0000 | [diff] [blame] | 10370 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 10371 | case Intrinsic::x86_sse_comieq_ss: |
| 10372 | case Intrinsic::x86_sse2_comieq_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10373 | Opc = X86ISD::COMI; |
| 10374 | CC = ISD::SETEQ; |
| 10375 | break; |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10376 | case Intrinsic::x86_sse_comilt_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10377 | case Intrinsic::x86_sse2_comilt_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10378 | Opc = X86ISD::COMI; |
| 10379 | CC = ISD::SETLT; |
| 10380 | break; |
| 10381 | case Intrinsic::x86_sse_comile_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10382 | case Intrinsic::x86_sse2_comile_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10383 | Opc = X86ISD::COMI; |
| 10384 | CC = ISD::SETLE; |
| 10385 | break; |
| 10386 | case Intrinsic::x86_sse_comigt_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10387 | case Intrinsic::x86_sse2_comigt_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10388 | Opc = X86ISD::COMI; |
| 10389 | CC = ISD::SETGT; |
| 10390 | break; |
| 10391 | case Intrinsic::x86_sse_comige_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10392 | case Intrinsic::x86_sse2_comige_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10393 | Opc = X86ISD::COMI; |
| 10394 | CC = ISD::SETGE; |
| 10395 | break; |
| 10396 | case Intrinsic::x86_sse_comineq_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10397 | case Intrinsic::x86_sse2_comineq_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10398 | Opc = X86ISD::COMI; |
| 10399 | CC = ISD::SETNE; |
| 10400 | break; |
| 10401 | case Intrinsic::x86_sse_ucomieq_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10402 | case Intrinsic::x86_sse2_ucomieq_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10403 | Opc = X86ISD::UCOMI; |
| 10404 | CC = ISD::SETEQ; |
| 10405 | break; |
| 10406 | case Intrinsic::x86_sse_ucomilt_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10407 | case Intrinsic::x86_sse2_ucomilt_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10408 | Opc = X86ISD::UCOMI; |
| 10409 | CC = ISD::SETLT; |
| 10410 | break; |
| 10411 | case Intrinsic::x86_sse_ucomile_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10412 | case Intrinsic::x86_sse2_ucomile_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10413 | Opc = X86ISD::UCOMI; |
| 10414 | CC = ISD::SETLE; |
| 10415 | break; |
| 10416 | case Intrinsic::x86_sse_ucomigt_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10417 | case Intrinsic::x86_sse2_ucomigt_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10418 | Opc = X86ISD::UCOMI; |
| 10419 | CC = ISD::SETGT; |
| 10420 | break; |
| 10421 | case Intrinsic::x86_sse_ucomige_ss: |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10422 | case Intrinsic::x86_sse2_ucomige_sd: |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 10423 | Opc = X86ISD::UCOMI; |
| 10424 | CC = ISD::SETGE; |
| 10425 | break; |
| 10426 | case Intrinsic::x86_sse_ucomineq_ss: |
| 10427 | case Intrinsic::x86_sse2_ucomineq_sd: |
| 10428 | Opc = X86ISD::UCOMI; |
| 10429 | CC = ISD::SETNE; |
| 10430 | break; |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10431 | } |
| Evan Cheng | 734503b | 2006-09-11 02:19:56 +0000 | [diff] [blame] | 10432 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 10433 | SDValue LHS = Op.getOperand(1); |
| 10434 | SDValue RHS = Op.getOperand(2); |
| Chris Lattner | 1c39d4c | 2008-12-24 23:53:05 +0000 | [diff] [blame] | 10435 | unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG); |
| Dan Gohman | 1a49295 | 2009-10-20 16:22:37 +0000 | [diff] [blame] | 10436 | assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!"); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 10437 | SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS); |
| 10438 | SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
| 10439 | DAG.getConstant(X86CC, MVT::i8), Cond); |
| 10440 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 10441 | } |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10442 | |
| Duncan Sands | 04aa4ae | 2011-09-23 16:10:22 +0000 | [diff] [blame] | 10443 | // Arithmetic intrinsics. |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 10444 | case Intrinsic::x86_sse2_pmulu_dq: |
| 10445 | case Intrinsic::x86_avx2_pmulu_dq: |
| 10446 | return DAG.getNode(X86ISD::PMULUDQ, dl, Op.getValueType(), |
| 10447 | Op.getOperand(1), Op.getOperand(2)); |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10448 | |
| Benjamin Kramer | 388fc6a | 2012-12-15 16:47:44 +0000 | [diff] [blame] | 10449 | // SSE2/AVX2 sub with unsigned saturation intrinsics |
| 10450 | case Intrinsic::x86_sse2_psubus_b: |
| 10451 | case Intrinsic::x86_sse2_psubus_w: |
| 10452 | case Intrinsic::x86_avx2_psubus_b: |
| 10453 | case Intrinsic::x86_avx2_psubus_w: |
| 10454 | return DAG.getNode(X86ISD::SUBUS, dl, Op.getValueType(), |
| 10455 | Op.getOperand(1), Op.getOperand(2)); |
| 10456 | |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10457 | // SSE3/AVX horizontal add/sub intrinsics |
| Duncan Sands | 04aa4ae | 2011-09-23 16:10:22 +0000 | [diff] [blame] | 10458 | case Intrinsic::x86_sse3_hadd_ps: |
| 10459 | case Intrinsic::x86_sse3_hadd_pd: |
| 10460 | case Intrinsic::x86_avx_hadd_ps_256: |
| 10461 | case Intrinsic::x86_avx_hadd_pd_256: |
| Duncan Sands | 04aa4ae | 2011-09-23 16:10:22 +0000 | [diff] [blame] | 10462 | case Intrinsic::x86_sse3_hsub_ps: |
| 10463 | case Intrinsic::x86_sse3_hsub_pd: |
| 10464 | case Intrinsic::x86_avx_hsub_ps_256: |
| 10465 | case Intrinsic::x86_avx_hsub_pd_256: |
| Craig Topper | 4bb3f34 | 2012-01-25 05:37:32 +0000 | [diff] [blame] | 10466 | case Intrinsic::x86_ssse3_phadd_w_128: |
| 10467 | case Intrinsic::x86_ssse3_phadd_d_128: |
| 10468 | case Intrinsic::x86_avx2_phadd_w: |
| 10469 | case Intrinsic::x86_avx2_phadd_d: |
| Craig Topper | 4bb3f34 | 2012-01-25 05:37:32 +0000 | [diff] [blame] | 10470 | case Intrinsic::x86_ssse3_phsub_w_128: |
| 10471 | case Intrinsic::x86_ssse3_phsub_d_128: |
| 10472 | case Intrinsic::x86_avx2_phsub_w: |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10473 | case Intrinsic::x86_avx2_phsub_d: { |
| 10474 | unsigned Opcode; |
| 10475 | switch (IntNo) { |
| 10476 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 10477 | case Intrinsic::x86_sse3_hadd_ps: |
| 10478 | case Intrinsic::x86_sse3_hadd_pd: |
| 10479 | case Intrinsic::x86_avx_hadd_ps_256: |
| 10480 | case Intrinsic::x86_avx_hadd_pd_256: |
| 10481 | Opcode = X86ISD::FHADD; |
| 10482 | break; |
| 10483 | case Intrinsic::x86_sse3_hsub_ps: |
| 10484 | case Intrinsic::x86_sse3_hsub_pd: |
| 10485 | case Intrinsic::x86_avx_hsub_ps_256: |
| 10486 | case Intrinsic::x86_avx_hsub_pd_256: |
| 10487 | Opcode = X86ISD::FHSUB; |
| 10488 | break; |
| 10489 | case Intrinsic::x86_ssse3_phadd_w_128: |
| 10490 | case Intrinsic::x86_ssse3_phadd_d_128: |
| 10491 | case Intrinsic::x86_avx2_phadd_w: |
| 10492 | case Intrinsic::x86_avx2_phadd_d: |
| 10493 | Opcode = X86ISD::HADD; |
| 10494 | break; |
| 10495 | case Intrinsic::x86_ssse3_phsub_w_128: |
| 10496 | case Intrinsic::x86_ssse3_phsub_d_128: |
| 10497 | case Intrinsic::x86_avx2_phsub_w: |
| 10498 | case Intrinsic::x86_avx2_phsub_d: |
| 10499 | Opcode = X86ISD::HSUB; |
| 10500 | break; |
| 10501 | } |
| 10502 | return DAG.getNode(Opcode, dl, Op.getValueType(), |
| Craig Topper | 4bb3f34 | 2012-01-25 05:37:32 +0000 | [diff] [blame] | 10503 | Op.getOperand(1), Op.getOperand(2)); |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10504 | } |
| 10505 | |
| Benjamin Kramer | 739c7a8 | 2012-12-21 14:04:55 +0000 | [diff] [blame] | 10506 | // SSE2/SSE41/AVX2 integer max/min intrinsics. |
| 10507 | case Intrinsic::x86_sse2_pmaxu_b: |
| 10508 | case Intrinsic::x86_sse41_pmaxuw: |
| 10509 | case Intrinsic::x86_sse41_pmaxud: |
| 10510 | case Intrinsic::x86_avx2_pmaxu_b: |
| 10511 | case Intrinsic::x86_avx2_pmaxu_w: |
| 10512 | case Intrinsic::x86_avx2_pmaxu_d: |
| Benjamin Kramer | 739c7a8 | 2012-12-21 14:04:55 +0000 | [diff] [blame] | 10513 | case Intrinsic::x86_sse2_pminu_b: |
| 10514 | case Intrinsic::x86_sse41_pminuw: |
| 10515 | case Intrinsic::x86_sse41_pminud: |
| 10516 | case Intrinsic::x86_avx2_pminu_b: |
| 10517 | case Intrinsic::x86_avx2_pminu_w: |
| 10518 | case Intrinsic::x86_avx2_pminu_d: |
| Benjamin Kramer | 739c7a8 | 2012-12-21 14:04:55 +0000 | [diff] [blame] | 10519 | case Intrinsic::x86_sse41_pmaxsb: |
| 10520 | case Intrinsic::x86_sse2_pmaxs_w: |
| 10521 | case Intrinsic::x86_sse41_pmaxsd: |
| 10522 | case Intrinsic::x86_avx2_pmaxs_b: |
| 10523 | case Intrinsic::x86_avx2_pmaxs_w: |
| 10524 | case Intrinsic::x86_avx2_pmaxs_d: |
| Benjamin Kramer | 739c7a8 | 2012-12-21 14:04:55 +0000 | [diff] [blame] | 10525 | case Intrinsic::x86_sse41_pminsb: |
| 10526 | case Intrinsic::x86_sse2_pmins_w: |
| 10527 | case Intrinsic::x86_sse41_pminsd: |
| 10528 | case Intrinsic::x86_avx2_pmins_b: |
| 10529 | case Intrinsic::x86_avx2_pmins_w: |
| Craig Topper | 6f57f39 | 2012-12-29 17:19:06 +0000 | [diff] [blame] | 10530 | case Intrinsic::x86_avx2_pmins_d: { |
| 10531 | unsigned Opcode; |
| 10532 | switch (IntNo) { |
| 10533 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 10534 | case Intrinsic::x86_sse2_pmaxu_b: |
| 10535 | case Intrinsic::x86_sse41_pmaxuw: |
| 10536 | case Intrinsic::x86_sse41_pmaxud: |
| 10537 | case Intrinsic::x86_avx2_pmaxu_b: |
| 10538 | case Intrinsic::x86_avx2_pmaxu_w: |
| 10539 | case Intrinsic::x86_avx2_pmaxu_d: |
| 10540 | Opcode = X86ISD::UMAX; |
| 10541 | break; |
| 10542 | case Intrinsic::x86_sse2_pminu_b: |
| 10543 | case Intrinsic::x86_sse41_pminuw: |
| 10544 | case Intrinsic::x86_sse41_pminud: |
| 10545 | case Intrinsic::x86_avx2_pminu_b: |
| 10546 | case Intrinsic::x86_avx2_pminu_w: |
| 10547 | case Intrinsic::x86_avx2_pminu_d: |
| 10548 | Opcode = X86ISD::UMIN; |
| 10549 | break; |
| 10550 | case Intrinsic::x86_sse41_pmaxsb: |
| 10551 | case Intrinsic::x86_sse2_pmaxs_w: |
| 10552 | case Intrinsic::x86_sse41_pmaxsd: |
| 10553 | case Intrinsic::x86_avx2_pmaxs_b: |
| 10554 | case Intrinsic::x86_avx2_pmaxs_w: |
| 10555 | case Intrinsic::x86_avx2_pmaxs_d: |
| 10556 | Opcode = X86ISD::SMAX; |
| 10557 | break; |
| 10558 | case Intrinsic::x86_sse41_pminsb: |
| 10559 | case Intrinsic::x86_sse2_pmins_w: |
| 10560 | case Intrinsic::x86_sse41_pminsd: |
| 10561 | case Intrinsic::x86_avx2_pmins_b: |
| 10562 | case Intrinsic::x86_avx2_pmins_w: |
| 10563 | case Intrinsic::x86_avx2_pmins_d: |
| 10564 | Opcode = X86ISD::SMIN; |
| 10565 | break; |
| 10566 | } |
| 10567 | return DAG.getNode(Opcode, dl, Op.getValueType(), |
| Benjamin Kramer | 739c7a8 | 2012-12-21 14:04:55 +0000 | [diff] [blame] | 10568 | Op.getOperand(1), Op.getOperand(2)); |
| Craig Topper | 6f57f39 | 2012-12-29 17:19:06 +0000 | [diff] [blame] | 10569 | } |
| Benjamin Kramer | 739c7a8 | 2012-12-21 14:04:55 +0000 | [diff] [blame] | 10570 | |
| Craig Topper | 6d183e4 | 2012-12-29 16:44:25 +0000 | [diff] [blame] | 10571 | // SSE/SSE2/AVX floating point max/min intrinsics. |
| 10572 | case Intrinsic::x86_sse_max_ps: |
| 10573 | case Intrinsic::x86_sse2_max_pd: |
| 10574 | case Intrinsic::x86_avx_max_ps_256: |
| 10575 | case Intrinsic::x86_avx_max_pd_256: |
| 10576 | case Intrinsic::x86_sse_min_ps: |
| 10577 | case Intrinsic::x86_sse2_min_pd: |
| 10578 | case Intrinsic::x86_avx_min_ps_256: |
| 10579 | case Intrinsic::x86_avx_min_pd_256: { |
| 10580 | unsigned Opcode; |
| 10581 | switch (IntNo) { |
| 10582 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 10583 | case Intrinsic::x86_sse_max_ps: |
| 10584 | case Intrinsic::x86_sse2_max_pd: |
| 10585 | case Intrinsic::x86_avx_max_ps_256: |
| 10586 | case Intrinsic::x86_avx_max_pd_256: |
| 10587 | Opcode = X86ISD::FMAX; |
| 10588 | break; |
| 10589 | case Intrinsic::x86_sse_min_ps: |
| 10590 | case Intrinsic::x86_sse2_min_pd: |
| 10591 | case Intrinsic::x86_avx_min_ps_256: |
| 10592 | case Intrinsic::x86_avx_min_pd_256: |
| 10593 | Opcode = X86ISD::FMIN; |
| 10594 | break; |
| 10595 | } |
| 10596 | return DAG.getNode(Opcode, dl, Op.getValueType(), |
| 10597 | Op.getOperand(1), Op.getOperand(2)); |
| 10598 | } |
| 10599 | |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10600 | // AVX2 variable shift intrinsics |
| Craig Topper | 98fc729 | 2011-11-19 17:46:46 +0000 | [diff] [blame] | 10601 | case Intrinsic::x86_avx2_psllv_d: |
| 10602 | case Intrinsic::x86_avx2_psllv_q: |
| 10603 | case Intrinsic::x86_avx2_psllv_d_256: |
| 10604 | case Intrinsic::x86_avx2_psllv_q_256: |
| Craig Topper | 98fc729 | 2011-11-19 17:46:46 +0000 | [diff] [blame] | 10605 | case Intrinsic::x86_avx2_psrlv_d: |
| 10606 | case Intrinsic::x86_avx2_psrlv_q: |
| 10607 | case Intrinsic::x86_avx2_psrlv_d_256: |
| 10608 | case Intrinsic::x86_avx2_psrlv_q_256: |
| Craig Topper | 98fc729 | 2011-11-19 17:46:46 +0000 | [diff] [blame] | 10609 | case Intrinsic::x86_avx2_psrav_d: |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10610 | case Intrinsic::x86_avx2_psrav_d_256: { |
| 10611 | unsigned Opcode; |
| 10612 | switch (IntNo) { |
| 10613 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 10614 | case Intrinsic::x86_avx2_psllv_d: |
| 10615 | case Intrinsic::x86_avx2_psllv_q: |
| 10616 | case Intrinsic::x86_avx2_psllv_d_256: |
| 10617 | case Intrinsic::x86_avx2_psllv_q_256: |
| 10618 | Opcode = ISD::SHL; |
| 10619 | break; |
| 10620 | case Intrinsic::x86_avx2_psrlv_d: |
| 10621 | case Intrinsic::x86_avx2_psrlv_q: |
| 10622 | case Intrinsic::x86_avx2_psrlv_d_256: |
| 10623 | case Intrinsic::x86_avx2_psrlv_q_256: |
| 10624 | Opcode = ISD::SRL; |
| 10625 | break; |
| 10626 | case Intrinsic::x86_avx2_psrav_d: |
| 10627 | case Intrinsic::x86_avx2_psrav_d_256: |
| 10628 | Opcode = ISD::SRA; |
| 10629 | break; |
| 10630 | } |
| 10631 | return DAG.getNode(Opcode, dl, Op.getValueType(), |
| 10632 | Op.getOperand(1), Op.getOperand(2)); |
| 10633 | } |
| 10634 | |
| Craig Topper | 969ba28 | 2012-01-25 06:43:11 +0000 | [diff] [blame] | 10635 | case Intrinsic::x86_ssse3_pshuf_b_128: |
| 10636 | case Intrinsic::x86_avx2_pshuf_b: |
| 10637 | return DAG.getNode(X86ISD::PSHUFB, dl, Op.getValueType(), |
| 10638 | Op.getOperand(1), Op.getOperand(2)); |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10639 | |
| Craig Topper | 969ba28 | 2012-01-25 06:43:11 +0000 | [diff] [blame] | 10640 | case Intrinsic::x86_ssse3_psign_b_128: |
| 10641 | case Intrinsic::x86_ssse3_psign_w_128: |
| 10642 | case Intrinsic::x86_ssse3_psign_d_128: |
| 10643 | case Intrinsic::x86_avx2_psign_b: |
| 10644 | case Intrinsic::x86_avx2_psign_w: |
| 10645 | case Intrinsic::x86_avx2_psign_d: |
| 10646 | return DAG.getNode(X86ISD::PSIGN, dl, Op.getValueType(), |
| 10647 | Op.getOperand(1), Op.getOperand(2)); |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10648 | |
| Craig Topper | e566cd0 | 2012-01-26 07:18:03 +0000 | [diff] [blame] | 10649 | case Intrinsic::x86_sse41_insertps: |
| 10650 | return DAG.getNode(X86ISD::INSERTPS, dl, Op.getValueType(), |
| 10651 | Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10652 | |
| Craig Topper | e566cd0 | 2012-01-26 07:18:03 +0000 | [diff] [blame] | 10653 | case Intrinsic::x86_avx_vperm2f128_ps_256: |
| 10654 | case Intrinsic::x86_avx_vperm2f128_pd_256: |
| 10655 | case Intrinsic::x86_avx_vperm2f128_si_256: |
| 10656 | case Intrinsic::x86_avx2_vperm2i128: |
| 10657 | return DAG.getNode(X86ISD::VPERM2X128, dl, Op.getValueType(), |
| 10658 | Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10659 | |
| Craig Topper | ffa6c40 | 2012-04-16 07:13:00 +0000 | [diff] [blame] | 10660 | case Intrinsic::x86_avx2_permd: |
| 10661 | case Intrinsic::x86_avx2_permps: |
| 10662 | // Operands intentionally swapped. Mask is last operand to intrinsic, |
| 10663 | // but second operand for node/intruction. |
| 10664 | return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(), |
| 10665 | Op.getOperand(2), Op.getOperand(1)); |
| Craig Topper | 98fc729 | 2011-11-19 17:46:46 +0000 | [diff] [blame] | 10666 | |
| Craig Topper | 22d8f0d | 2012-12-29 18:18:20 +0000 | [diff] [blame] | 10667 | case Intrinsic::x86_sse_sqrt_ps: |
| 10668 | case Intrinsic::x86_sse2_sqrt_pd: |
| 10669 | case Intrinsic::x86_avx_sqrt_ps_256: |
| 10670 | case Intrinsic::x86_avx_sqrt_pd_256: |
| 10671 | return DAG.getNode(ISD::FSQRT, dl, Op.getValueType(), Op.getOperand(1)); |
| 10672 | |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 10673 | // ptest and testp intrinsics. The intrinsic these come from are designed to |
| 10674 | // return an integer value, not just an instruction so lower it to the ptest |
| 10675 | // or testp pattern and a setcc for the result. |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 10676 | case Intrinsic::x86_sse41_ptestz: |
| 10677 | case Intrinsic::x86_sse41_ptestc: |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 10678 | case Intrinsic::x86_sse41_ptestnzc: |
| 10679 | case Intrinsic::x86_avx_ptestz_256: |
| 10680 | case Intrinsic::x86_avx_ptestc_256: |
| 10681 | case Intrinsic::x86_avx_ptestnzc_256: |
| 10682 | case Intrinsic::x86_avx_vtestz_ps: |
| 10683 | case Intrinsic::x86_avx_vtestc_ps: |
| 10684 | case Intrinsic::x86_avx_vtestnzc_ps: |
| 10685 | case Intrinsic::x86_avx_vtestz_pd: |
| 10686 | case Intrinsic::x86_avx_vtestc_pd: |
| 10687 | case Intrinsic::x86_avx_vtestnzc_pd: |
| 10688 | case Intrinsic::x86_avx_vtestz_ps_256: |
| 10689 | case Intrinsic::x86_avx_vtestc_ps_256: |
| 10690 | case Intrinsic::x86_avx_vtestnzc_ps_256: |
| 10691 | case Intrinsic::x86_avx_vtestz_pd_256: |
| 10692 | case Intrinsic::x86_avx_vtestc_pd_256: |
| 10693 | case Intrinsic::x86_avx_vtestnzc_pd_256: { |
| 10694 | bool IsTestPacked = false; |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10695 | unsigned X86CC; |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 10696 | switch (IntNo) { |
| Eric Christopher | 978dae3 | 2009-07-29 18:14:04 +0000 | [diff] [blame] | 10697 | default: llvm_unreachable("Bad fallthrough in Intrinsic lowering."); |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 10698 | case Intrinsic::x86_avx_vtestz_ps: |
| 10699 | case Intrinsic::x86_avx_vtestz_pd: |
| 10700 | case Intrinsic::x86_avx_vtestz_ps_256: |
| 10701 | case Intrinsic::x86_avx_vtestz_pd_256: |
| 10702 | IsTestPacked = true; // Fallthrough |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 10703 | case Intrinsic::x86_sse41_ptestz: |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 10704 | case Intrinsic::x86_avx_ptestz_256: |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 10705 | // ZF = 1 |
| 10706 | X86CC = X86::COND_E; |
| 10707 | break; |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 10708 | case Intrinsic::x86_avx_vtestc_ps: |
| 10709 | case Intrinsic::x86_avx_vtestc_pd: |
| 10710 | case Intrinsic::x86_avx_vtestc_ps_256: |
| 10711 | case Intrinsic::x86_avx_vtestc_pd_256: |
| 10712 | IsTestPacked = true; // Fallthrough |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 10713 | case Intrinsic::x86_sse41_ptestc: |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 10714 | case Intrinsic::x86_avx_ptestc_256: |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 10715 | // CF = 1 |
| 10716 | X86CC = X86::COND_B; |
| 10717 | break; |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 10718 | case Intrinsic::x86_avx_vtestnzc_ps: |
| 10719 | case Intrinsic::x86_avx_vtestnzc_pd: |
| 10720 | case Intrinsic::x86_avx_vtestnzc_ps_256: |
| 10721 | case Intrinsic::x86_avx_vtestnzc_pd_256: |
| 10722 | IsTestPacked = true; // Fallthrough |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 10723 | case Intrinsic::x86_sse41_ptestnzc: |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 10724 | case Intrinsic::x86_avx_ptestnzc_256: |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 10725 | // ZF and CF = 0 |
| 10726 | X86CC = X86::COND_A; |
| 10727 | break; |
| 10728 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 10729 | |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 10730 | SDValue LHS = Op.getOperand(1); |
| 10731 | SDValue RHS = Op.getOperand(2); |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 10732 | unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST; |
| 10733 | SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 10734 | SDValue CC = DAG.getConstant(X86CC, MVT::i8); |
| 10735 | SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test); |
| 10736 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 10737 | } |
| Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 10738 | |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10739 | // SSE/AVX shift intrinsics |
| 10740 | case Intrinsic::x86_sse2_psll_w: |
| 10741 | case Intrinsic::x86_sse2_psll_d: |
| 10742 | case Intrinsic::x86_sse2_psll_q: |
| 10743 | case Intrinsic::x86_avx2_psll_w: |
| 10744 | case Intrinsic::x86_avx2_psll_d: |
| 10745 | case Intrinsic::x86_avx2_psll_q: |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10746 | case Intrinsic::x86_sse2_psrl_w: |
| 10747 | case Intrinsic::x86_sse2_psrl_d: |
| 10748 | case Intrinsic::x86_sse2_psrl_q: |
| 10749 | case Intrinsic::x86_avx2_psrl_w: |
| 10750 | case Intrinsic::x86_avx2_psrl_d: |
| 10751 | case Intrinsic::x86_avx2_psrl_q: |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10752 | case Intrinsic::x86_sse2_psra_w: |
| 10753 | case Intrinsic::x86_sse2_psra_d: |
| 10754 | case Intrinsic::x86_avx2_psra_w: |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10755 | case Intrinsic::x86_avx2_psra_d: { |
| 10756 | unsigned Opcode; |
| 10757 | switch (IntNo) { |
| 10758 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 10759 | case Intrinsic::x86_sse2_psll_w: |
| 10760 | case Intrinsic::x86_sse2_psll_d: |
| 10761 | case Intrinsic::x86_sse2_psll_q: |
| 10762 | case Intrinsic::x86_avx2_psll_w: |
| 10763 | case Intrinsic::x86_avx2_psll_d: |
| 10764 | case Intrinsic::x86_avx2_psll_q: |
| 10765 | Opcode = X86ISD::VSHL; |
| 10766 | break; |
| 10767 | case Intrinsic::x86_sse2_psrl_w: |
| 10768 | case Intrinsic::x86_sse2_psrl_d: |
| 10769 | case Intrinsic::x86_sse2_psrl_q: |
| 10770 | case Intrinsic::x86_avx2_psrl_w: |
| 10771 | case Intrinsic::x86_avx2_psrl_d: |
| 10772 | case Intrinsic::x86_avx2_psrl_q: |
| 10773 | Opcode = X86ISD::VSRL; |
| 10774 | break; |
| 10775 | case Intrinsic::x86_sse2_psra_w: |
| 10776 | case Intrinsic::x86_sse2_psra_d: |
| 10777 | case Intrinsic::x86_avx2_psra_w: |
| 10778 | case Intrinsic::x86_avx2_psra_d: |
| 10779 | Opcode = X86ISD::VSRA; |
| 10780 | break; |
| 10781 | } |
| 10782 | return DAG.getNode(Opcode, dl, Op.getValueType(), |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10783 | Op.getOperand(1), Op.getOperand(2)); |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10784 | } |
| 10785 | |
| 10786 | // SSE/AVX immediate shift intrinsics |
| Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 10787 | case Intrinsic::x86_sse2_pslli_w: |
| 10788 | case Intrinsic::x86_sse2_pslli_d: |
| 10789 | case Intrinsic::x86_sse2_pslli_q: |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10790 | case Intrinsic::x86_avx2_pslli_w: |
| 10791 | case Intrinsic::x86_avx2_pslli_d: |
| 10792 | case Intrinsic::x86_avx2_pslli_q: |
| Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 10793 | case Intrinsic::x86_sse2_psrli_w: |
| 10794 | case Intrinsic::x86_sse2_psrli_d: |
| 10795 | case Intrinsic::x86_sse2_psrli_q: |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10796 | case Intrinsic::x86_avx2_psrli_w: |
| 10797 | case Intrinsic::x86_avx2_psrli_d: |
| 10798 | case Intrinsic::x86_avx2_psrli_q: |
| Evan Cheng | 5759f97 | 2008-05-04 09:15:50 +0000 | [diff] [blame] | 10799 | case Intrinsic::x86_sse2_psrai_w: |
| 10800 | case Intrinsic::x86_sse2_psrai_d: |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10801 | case Intrinsic::x86_avx2_psrai_w: |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10802 | case Intrinsic::x86_avx2_psrai_d: { |
| 10803 | unsigned Opcode; |
| 10804 | switch (IntNo) { |
| 10805 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 10806 | case Intrinsic::x86_sse2_pslli_w: |
| 10807 | case Intrinsic::x86_sse2_pslli_d: |
| 10808 | case Intrinsic::x86_sse2_pslli_q: |
| 10809 | case Intrinsic::x86_avx2_pslli_w: |
| 10810 | case Intrinsic::x86_avx2_pslli_d: |
| 10811 | case Intrinsic::x86_avx2_pslli_q: |
| 10812 | Opcode = X86ISD::VSHLI; |
| 10813 | break; |
| 10814 | case Intrinsic::x86_sse2_psrli_w: |
| 10815 | case Intrinsic::x86_sse2_psrli_d: |
| 10816 | case Intrinsic::x86_sse2_psrli_q: |
| 10817 | case Intrinsic::x86_avx2_psrli_w: |
| 10818 | case Intrinsic::x86_avx2_psrli_d: |
| 10819 | case Intrinsic::x86_avx2_psrli_q: |
| 10820 | Opcode = X86ISD::VSRLI; |
| 10821 | break; |
| 10822 | case Intrinsic::x86_sse2_psrai_w: |
| 10823 | case Intrinsic::x86_sse2_psrai_d: |
| 10824 | case Intrinsic::x86_avx2_psrai_w: |
| 10825 | case Intrinsic::x86_avx2_psrai_d: |
| 10826 | Opcode = X86ISD::VSRAI; |
| 10827 | break; |
| 10828 | } |
| 10829 | return getTargetVShiftNode(Opcode, dl, Op.getValueType(), |
| Craig Topper | 80e4636 | 2012-01-23 06:16:53 +0000 | [diff] [blame] | 10830 | Op.getOperand(1), Op.getOperand(2), DAG); |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10831 | } |
| 10832 | |
| Craig Topper | 4feb647 | 2012-08-06 06:22:36 +0000 | [diff] [blame] | 10833 | case Intrinsic::x86_sse42_pcmpistria128: |
| 10834 | case Intrinsic::x86_sse42_pcmpestria128: |
| 10835 | case Intrinsic::x86_sse42_pcmpistric128: |
| 10836 | case Intrinsic::x86_sse42_pcmpestric128: |
| 10837 | case Intrinsic::x86_sse42_pcmpistrio128: |
| 10838 | case Intrinsic::x86_sse42_pcmpestrio128: |
| 10839 | case Intrinsic::x86_sse42_pcmpistris128: |
| 10840 | case Intrinsic::x86_sse42_pcmpestris128: |
| 10841 | case Intrinsic::x86_sse42_pcmpistriz128: |
| 10842 | case Intrinsic::x86_sse42_pcmpestriz128: { |
| 10843 | unsigned Opcode; |
| 10844 | unsigned X86CC; |
| 10845 | switch (IntNo) { |
| 10846 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 10847 | case Intrinsic::x86_sse42_pcmpistria128: |
| 10848 | Opcode = X86ISD::PCMPISTRI; |
| 10849 | X86CC = X86::COND_A; |
| 10850 | break; |
| 10851 | case Intrinsic::x86_sse42_pcmpestria128: |
| 10852 | Opcode = X86ISD::PCMPESTRI; |
| 10853 | X86CC = X86::COND_A; |
| 10854 | break; |
| 10855 | case Intrinsic::x86_sse42_pcmpistric128: |
| 10856 | Opcode = X86ISD::PCMPISTRI; |
| 10857 | X86CC = X86::COND_B; |
| 10858 | break; |
| 10859 | case Intrinsic::x86_sse42_pcmpestric128: |
| 10860 | Opcode = X86ISD::PCMPESTRI; |
| 10861 | X86CC = X86::COND_B; |
| 10862 | break; |
| 10863 | case Intrinsic::x86_sse42_pcmpistrio128: |
| 10864 | Opcode = X86ISD::PCMPISTRI; |
| 10865 | X86CC = X86::COND_O; |
| 10866 | break; |
| 10867 | case Intrinsic::x86_sse42_pcmpestrio128: |
| 10868 | Opcode = X86ISD::PCMPESTRI; |
| 10869 | X86CC = X86::COND_O; |
| 10870 | break; |
| 10871 | case Intrinsic::x86_sse42_pcmpistris128: |
| 10872 | Opcode = X86ISD::PCMPISTRI; |
| 10873 | X86CC = X86::COND_S; |
| 10874 | break; |
| 10875 | case Intrinsic::x86_sse42_pcmpestris128: |
| 10876 | Opcode = X86ISD::PCMPESTRI; |
| 10877 | X86CC = X86::COND_S; |
| 10878 | break; |
| 10879 | case Intrinsic::x86_sse42_pcmpistriz128: |
| 10880 | Opcode = X86ISD::PCMPISTRI; |
| 10881 | X86CC = X86::COND_E; |
| 10882 | break; |
| 10883 | case Intrinsic::x86_sse42_pcmpestriz128: |
| 10884 | Opcode = X86ISD::PCMPESTRI; |
| 10885 | X86CC = X86::COND_E; |
| 10886 | break; |
| 10887 | } |
| 10888 | SmallVector<SDValue, 5> NewOps; |
| 10889 | NewOps.append(Op->op_begin()+1, Op->op_end()); |
| 10890 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); |
| 10891 | SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size()); |
| 10892 | SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
| 10893 | DAG.getConstant(X86CC, MVT::i8), |
| 10894 | SDValue(PCMP.getNode(), 1)); |
| 10895 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); |
| 10896 | } |
| Craig Topper | 6d68815 | 2012-08-14 07:43:25 +0000 | [diff] [blame] | 10897 | |
| Craig Topper | 4feb647 | 2012-08-06 06:22:36 +0000 | [diff] [blame] | 10898 | case Intrinsic::x86_sse42_pcmpistri128: |
| 10899 | case Intrinsic::x86_sse42_pcmpestri128: { |
| 10900 | unsigned Opcode; |
| 10901 | if (IntNo == Intrinsic::x86_sse42_pcmpistri128) |
| 10902 | Opcode = X86ISD::PCMPISTRI; |
| 10903 | else |
| 10904 | Opcode = X86ISD::PCMPESTRI; |
| 10905 | |
| 10906 | SmallVector<SDValue, 5> NewOps; |
| 10907 | NewOps.append(Op->op_begin()+1, Op->op_end()); |
| 10908 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); |
| 10909 | return DAG.getNode(Opcode, dl, VTs, NewOps.data(), NewOps.size()); |
| 10910 | } |
| Craig Topper | 0e29237 | 2012-08-24 04:03:22 +0000 | [diff] [blame] | 10911 | case Intrinsic::x86_fma_vfmadd_ps: |
| 10912 | case Intrinsic::x86_fma_vfmadd_pd: |
| 10913 | case Intrinsic::x86_fma_vfmsub_ps: |
| 10914 | case Intrinsic::x86_fma_vfmsub_pd: |
| 10915 | case Intrinsic::x86_fma_vfnmadd_ps: |
| 10916 | case Intrinsic::x86_fma_vfnmadd_pd: |
| 10917 | case Intrinsic::x86_fma_vfnmsub_ps: |
| 10918 | case Intrinsic::x86_fma_vfnmsub_pd: |
| 10919 | case Intrinsic::x86_fma_vfmaddsub_ps: |
| 10920 | case Intrinsic::x86_fma_vfmaddsub_pd: |
| 10921 | case Intrinsic::x86_fma_vfmsubadd_ps: |
| 10922 | case Intrinsic::x86_fma_vfmsubadd_pd: |
| 10923 | case Intrinsic::x86_fma_vfmadd_ps_256: |
| 10924 | case Intrinsic::x86_fma_vfmadd_pd_256: |
| 10925 | case Intrinsic::x86_fma_vfmsub_ps_256: |
| 10926 | case Intrinsic::x86_fma_vfmsub_pd_256: |
| 10927 | case Intrinsic::x86_fma_vfnmadd_ps_256: |
| 10928 | case Intrinsic::x86_fma_vfnmadd_pd_256: |
| 10929 | case Intrinsic::x86_fma_vfnmsub_ps_256: |
| 10930 | case Intrinsic::x86_fma_vfnmsub_pd_256: |
| 10931 | case Intrinsic::x86_fma_vfmaddsub_ps_256: |
| 10932 | case Intrinsic::x86_fma_vfmaddsub_pd_256: |
| 10933 | case Intrinsic::x86_fma_vfmsubadd_ps_256: |
| 10934 | case Intrinsic::x86_fma_vfmsubadd_pd_256: { |
| Craig Topper | 0e29237 | 2012-08-24 04:03:22 +0000 | [diff] [blame] | 10935 | unsigned Opc; |
| 10936 | switch (IntNo) { |
| 10937 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| 10938 | case Intrinsic::x86_fma_vfmadd_ps: |
| 10939 | case Intrinsic::x86_fma_vfmadd_pd: |
| 10940 | case Intrinsic::x86_fma_vfmadd_ps_256: |
| 10941 | case Intrinsic::x86_fma_vfmadd_pd_256: |
| 10942 | Opc = X86ISD::FMADD; |
| 10943 | break; |
| 10944 | case Intrinsic::x86_fma_vfmsub_ps: |
| 10945 | case Intrinsic::x86_fma_vfmsub_pd: |
| 10946 | case Intrinsic::x86_fma_vfmsub_ps_256: |
| 10947 | case Intrinsic::x86_fma_vfmsub_pd_256: |
| 10948 | Opc = X86ISD::FMSUB; |
| 10949 | break; |
| 10950 | case Intrinsic::x86_fma_vfnmadd_ps: |
| 10951 | case Intrinsic::x86_fma_vfnmadd_pd: |
| 10952 | case Intrinsic::x86_fma_vfnmadd_ps_256: |
| 10953 | case Intrinsic::x86_fma_vfnmadd_pd_256: |
| 10954 | Opc = X86ISD::FNMADD; |
| 10955 | break; |
| 10956 | case Intrinsic::x86_fma_vfnmsub_ps: |
| 10957 | case Intrinsic::x86_fma_vfnmsub_pd: |
| 10958 | case Intrinsic::x86_fma_vfnmsub_ps_256: |
| 10959 | case Intrinsic::x86_fma_vfnmsub_pd_256: |
| 10960 | Opc = X86ISD::FNMSUB; |
| 10961 | break; |
| 10962 | case Intrinsic::x86_fma_vfmaddsub_ps: |
| 10963 | case Intrinsic::x86_fma_vfmaddsub_pd: |
| 10964 | case Intrinsic::x86_fma_vfmaddsub_ps_256: |
| 10965 | case Intrinsic::x86_fma_vfmaddsub_pd_256: |
| 10966 | Opc = X86ISD::FMADDSUB; |
| 10967 | break; |
| 10968 | case Intrinsic::x86_fma_vfmsubadd_ps: |
| 10969 | case Intrinsic::x86_fma_vfmsubadd_pd: |
| 10970 | case Intrinsic::x86_fma_vfmsubadd_ps_256: |
| 10971 | case Intrinsic::x86_fma_vfmsubadd_pd_256: |
| 10972 | Opc = X86ISD::FMSUBADD; |
| 10973 | break; |
| 10974 | } |
| 10975 | |
| 10976 | return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1), |
| 10977 | Op.getOperand(2), Op.getOperand(3)); |
| 10978 | } |
| Evan Cheng | 38bcbaf | 2005-12-23 07:31:11 +0000 | [diff] [blame] | 10979 | } |
| Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 10980 | } |
| Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 10981 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 10982 | static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 10983 | SDLoc dl(Op); |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 10984 | unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 10985 | switch (IntNo) { |
| 10986 | default: return SDValue(); // Don't custom lower most intrinsics. |
| 10987 | |
| Michael Liao | c26392a | 2013-03-28 23:41:26 +0000 | [diff] [blame] | 10988 | // RDRAND/RDSEED intrinsics. |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 10989 | case Intrinsic::x86_rdrand_16: |
| 10990 | case Intrinsic::x86_rdrand_32: |
| Michael Liao | c26392a | 2013-03-28 23:41:26 +0000 | [diff] [blame] | 10991 | case Intrinsic::x86_rdrand_64: |
| 10992 | case Intrinsic::x86_rdseed_16: |
| 10993 | case Intrinsic::x86_rdseed_32: |
| 10994 | case Intrinsic::x86_rdseed_64: { |
| 10995 | unsigned Opcode = (IntNo == Intrinsic::x86_rdseed_16 || |
| 10996 | IntNo == Intrinsic::x86_rdseed_32 || |
| 10997 | IntNo == Intrinsic::x86_rdseed_64) ? X86ISD::RDSEED : |
| 10998 | X86ISD::RDRAND; |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 10999 | // Emit the node with the right value type. |
| Benjamin Kramer | feae00a | 2012-07-12 18:14:57 +0000 | [diff] [blame] | 11000 | SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other); |
| Michael Liao | c26392a | 2013-03-28 23:41:26 +0000 | [diff] [blame] | 11001 | SDValue Result = DAG.getNode(Opcode, dl, VTs, Op.getOperand(0)); |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 11002 | |
| Michael Liao | c26392a | 2013-03-28 23:41:26 +0000 | [diff] [blame] | 11003 | // If the value returned by RDRAND/RDSEED was valid (CF=1), return 1. |
| 11004 | // Otherwise return the value from Rand, which is always 0, casted to i32. |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 11005 | SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)), |
| 11006 | DAG.getConstant(1, Op->getValueType(1)), |
| 11007 | DAG.getConstant(X86::COND_B, MVT::i32), |
| 11008 | SDValue(Result.getNode(), 1) }; |
| 11009 | SDValue isValid = DAG.getNode(X86ISD::CMOV, dl, |
| 11010 | DAG.getVTList(Op->getValueType(1), MVT::Glue), |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 11011 | Ops, array_lengthof(Ops)); |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 11012 | |
| 11013 | // Return { result, isValid, chain }. |
| 11014 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid, |
| Benjamin Kramer | feae00a | 2012-07-12 18:14:57 +0000 | [diff] [blame] | 11015 | SDValue(Result.getNode(), 2)); |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 11016 | } |
| Michael Liao | f8fd883 | 2013-03-26 22:47:01 +0000 | [diff] [blame] | 11017 | |
| 11018 | // XTEST intrinsics. |
| 11019 | case Intrinsic::x86_xtest: { |
| 11020 | SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Other); |
| 11021 | SDValue InTrans = DAG.getNode(X86ISD::XTEST, dl, VTs, Op.getOperand(0)); |
| 11022 | SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, |
| 11023 | DAG.getConstant(X86::COND_NE, MVT::i8), |
| 11024 | InTrans); |
| 11025 | SDValue Ret = DAG.getNode(ISD::ZERO_EXTEND, dl, Op->getValueType(0), SetCC); |
| 11026 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), |
| 11027 | Ret, SDValue(InTrans.getNode(), 1)); |
| 11028 | } |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 11029 | } |
| 11030 | } |
| 11031 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 11032 | SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, |
| 11033 | SelectionDAG &DAG) const { |
| Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 11034 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 11035 | MFI->setReturnAddressIsTaken(true); |
| 11036 | |
| Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 11037 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11038 | SDLoc dl(Op); |
| Michael Liao | aa3c2c0 | 2012-10-25 06:29:14 +0000 | [diff] [blame] | 11039 | EVT PtrVT = getPointerTy(); |
| Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 11040 | |
| 11041 | if (Depth > 0) { |
| 11042 | SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 11043 | const X86RegisterInfo *RegInfo = |
| 11044 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| 11045 | SDValue Offset = DAG.getConstant(RegInfo->getSlotSize(), PtrVT); |
| Michael Liao | aa3c2c0 | 2012-10-25 06:29:14 +0000 | [diff] [blame] | 11046 | return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), |
| 11047 | DAG.getNode(ISD::ADD, dl, PtrVT, |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 11048 | FrameAddr, Offset), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 11049 | MachinePointerInfo(), false, false, false, 0); |
| Bill Wendling | 64e8732 | 2009-01-16 19:25:27 +0000 | [diff] [blame] | 11050 | } |
| 11051 | |
| 11052 | // Just load the return address. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11053 | SDValue RetAddrFI = getReturnAddressFrameIndex(DAG); |
| Michael Liao | aa3c2c0 | 2012-10-25 06:29:14 +0000 | [diff] [blame] | 11054 | return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 11055 | RetAddrFI, MachinePointerInfo(), false, false, false, 0); |
| Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 11056 | } |
| 11057 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 11058 | SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { |
| Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 11059 | MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); |
| 11060 | MFI->setFrameAddressIsTaken(true); |
| Evan Cheng | 2457f2c | 2010-05-22 01:47:14 +0000 | [diff] [blame] | 11061 | |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 11062 | EVT VT = Op.getValueType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11063 | SDLoc dl(Op); // FIXME probably not meaningful |
| Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 11064 | unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 11065 | const X86RegisterInfo *RegInfo = |
| 11066 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| Michael Liao | b9cca13 | 2013-05-02 08:21:56 +0000 | [diff] [blame] | 11067 | unsigned FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction()); |
| 11068 | assert(((FrameReg == X86::RBP && VT == MVT::i64) || |
| Michael Liao | 299eb2e | 2013-05-02 09:22:04 +0000 | [diff] [blame] | 11069 | (FrameReg == X86::EBP && VT == MVT::i32)) && |
| 11070 | "Invalid Frame Register!"); |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 11071 | SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); |
| Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 11072 | while (Depth--) |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 11073 | FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, |
| 11074 | MachinePointerInfo(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 11075 | false, false, false, 0); |
| Evan Cheng | 184793f | 2008-09-27 01:56:22 +0000 | [diff] [blame] | 11076 | return FrameAddr; |
| Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 11077 | } |
| 11078 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11079 | SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 11080 | SelectionDAG &DAG) const { |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 11081 | const X86RegisterInfo *RegInfo = |
| 11082 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| Michael Liao | aa3c2c0 | 2012-10-25 06:29:14 +0000 | [diff] [blame] | 11083 | return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize()); |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 11084 | } |
| 11085 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 11086 | SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11087 | SDValue Chain = Op.getOperand(0); |
| 11088 | SDValue Offset = Op.getOperand(1); |
| 11089 | SDValue Handler = Op.getOperand(2); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11090 | SDLoc dl (Op); |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 11091 | |
| Michael Liao | db7da20 | 2013-05-02 09:18:38 +0000 | [diff] [blame] | 11092 | EVT PtrVT = getPointerTy(); |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 11093 | const X86RegisterInfo *RegInfo = |
| 11094 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| Michael Liao | db7da20 | 2013-05-02 09:18:38 +0000 | [diff] [blame] | 11095 | unsigned FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction()); |
| 11096 | assert(((FrameReg == X86::RBP && PtrVT == MVT::i64) || |
| 11097 | (FrameReg == X86::EBP && PtrVT == MVT::i32)) && |
| 11098 | "Invalid Frame Register!"); |
| 11099 | SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, PtrVT); |
| 11100 | unsigned StoreAddrReg = (PtrVT == MVT::i64) ? X86::RCX : X86::ECX; |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 11101 | |
| Michael Liao | db7da20 | 2013-05-02 09:18:38 +0000 | [diff] [blame] | 11102 | SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, Frame, |
| Michael Liao | 299eb2e | 2013-05-02 09:22:04 +0000 | [diff] [blame] | 11103 | DAG.getIntPtrConstant(RegInfo->getSlotSize())); |
| Michael Liao | db7da20 | 2013-05-02 09:18:38 +0000 | [diff] [blame] | 11104 | StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, StoreAddr, Offset); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11105 | Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(), |
| 11106 | false, false, 0); |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 11107 | Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr); |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 11108 | |
| Michael Liao | db7da20 | 2013-05-02 09:18:38 +0000 | [diff] [blame] | 11109 | return DAG.getNode(X86ISD::EH_RETURN, dl, MVT::Other, Chain, |
| 11110 | DAG.getRegister(StoreAddrReg, PtrVT)); |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 11111 | } |
| 11112 | |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 11113 | SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op, |
| 11114 | SelectionDAG &DAG) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11115 | SDLoc DL(Op); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 11116 | return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL, |
| 11117 | DAG.getVTList(MVT::i32, MVT::Other), |
| 11118 | Op.getOperand(0), Op.getOperand(1)); |
| 11119 | } |
| 11120 | |
| 11121 | SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op, |
| 11122 | SelectionDAG &DAG) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11123 | SDLoc DL(Op); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 11124 | return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other, |
| 11125 | Op.getOperand(0), Op.getOperand(1)); |
| 11126 | } |
| 11127 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 11128 | static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) { |
| Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 11129 | return Op.getOperand(0); |
| 11130 | } |
| 11131 | |
| 11132 | SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op, |
| 11133 | SelectionDAG &DAG) const { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11134 | SDValue Root = Op.getOperand(0); |
| 11135 | SDValue Trmp = Op.getOperand(1); // trampoline |
| 11136 | SDValue FPtr = Op.getOperand(2); // nested function |
| 11137 | SDValue Nest = Op.getOperand(3); // 'nest' parameter value |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11138 | SDLoc dl (Op); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11139 | |
| Dan Gohman | 69de193 | 2008-02-06 22:27:42 +0000 | [diff] [blame] | 11140 | const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); |
| Michael Liao | 7abf67a | 2012-10-04 19:50:43 +0000 | [diff] [blame] | 11141 | const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo(); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11142 | |
| 11143 | if (Subtarget->is64Bit()) { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11144 | SDValue OutChains[6]; |
| Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 11145 | |
| 11146 | // Large code-model. |
| Chris Lattner | a62fe66 | 2010-02-05 19:20:30 +0000 | [diff] [blame] | 11147 | const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode. |
| 11148 | const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode. |
| Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 11149 | |
| Michael Liao | 7abf67a | 2012-10-04 19:50:43 +0000 | [diff] [blame] | 11150 | const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7; |
| 11151 | const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7; |
| Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 11152 | |
| 11153 | const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix |
| 11154 | |
| 11155 | // Load the pointer to the nested function into R11. |
| 11156 | unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11 |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11157 | SDValue Addr = Trmp; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11158 | OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11159 | Addr, MachinePointerInfo(TrmpAddr), |
| 11160 | false, false, 0); |
| Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 11161 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11162 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
| 11163 | DAG.getConstant(2, MVT::i64)); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11164 | OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, |
| 11165 | MachinePointerInfo(TrmpAddr, 2), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 11166 | false, false, 2); |
| Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 11167 | |
| 11168 | // Load the 'nest' parameter value into R10. |
| 11169 | // R10 is specified in X86CallingConv.td |
| 11170 | OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10 |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11171 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
| 11172 | DAG.getConstant(10, MVT::i64)); |
| 11173 | OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11174 | Addr, MachinePointerInfo(TrmpAddr, 10), |
| 11175 | false, false, 0); |
| Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 11176 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11177 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
| 11178 | DAG.getConstant(12, MVT::i64)); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11179 | OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, |
| 11180 | MachinePointerInfo(TrmpAddr, 12), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 11181 | false, false, 2); |
| Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 11182 | |
| 11183 | // Jump to the nested function. |
| 11184 | OpCode = (JMP64r << 8) | REX_WB; // jmpq *... |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11185 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
| 11186 | DAG.getConstant(20, MVT::i64)); |
| 11187 | OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16), |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11188 | Addr, MachinePointerInfo(TrmpAddr, 20), |
| 11189 | false, false, 0); |
| Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 11190 | |
| 11191 | unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11 |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11192 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, |
| 11193 | DAG.getConstant(22, MVT::i64)); |
| 11194 | OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr, |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11195 | MachinePointerInfo(TrmpAddr, 22), |
| 11196 | false, false, 0); |
| Duncan Sands | 339e14f | 2008-01-16 22:55:25 +0000 | [diff] [blame] | 11197 | |
| Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 11198 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11199 | } else { |
| Dan Gohman | bbfb9c5 | 2008-01-31 01:01:48 +0000 | [diff] [blame] | 11200 | const Function *Func = |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11201 | cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue()); |
| Sandeep Patel | 65c3c8f | 2009-09-02 08:44:58 +0000 | [diff] [blame] | 11202 | CallingConv::ID CC = Func->getCallingConv(); |
| Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 11203 | unsigned NestReg; |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11204 | |
| 11205 | switch (CC) { |
| 11206 | default: |
| Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 11207 | llvm_unreachable("Unsupported calling convention"); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11208 | case CallingConv::C: |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11209 | case CallingConv::X86_StdCall: { |
| 11210 | // Pass 'nest' parameter in ECX. |
| 11211 | // Must be kept in sync with X86CallingConv.td |
| Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 11212 | NestReg = X86::ECX; |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11213 | |
| 11214 | // Check that ECX wasn't needed by an 'inreg' parameter. |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 11215 | FunctionType *FTy = Func->getFunctionType(); |
| Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 11216 | const AttributeSet &Attrs = Func->getAttributes(); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11217 | |
| Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 11218 | if (!Attrs.isEmpty() && !Func->isVarArg()) { |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11219 | unsigned InRegCount = 0; |
| 11220 | unsigned Idx = 1; |
| 11221 | |
| 11222 | for (FunctionType::param_iterator I = FTy->param_begin(), |
| 11223 | E = FTy->param_end(); I != E; ++I, ++Idx) |
| Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 11224 | if (Attrs.hasAttribute(Idx, Attribute::InReg)) |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11225 | // FIXME: should only count parameters that are lowered to integers. |
| Anton Korobeynikov | bff66b0 | 2008-09-09 18:22:57 +0000 | [diff] [blame] | 11226 | InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32; |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11227 | |
| 11228 | if (InRegCount > 2) { |
| Eric Christopher | 90eb402 | 2010-07-22 00:26:08 +0000 | [diff] [blame] | 11229 | report_fatal_error("Nest register in use - reduce number of inreg" |
| 11230 | " parameters!"); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11231 | } |
| 11232 | } |
| 11233 | break; |
| 11234 | } |
| 11235 | case CallingConv::X86_FastCall: |
| Anton Korobeynikov | ded05e3 | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 11236 | case CallingConv::X86_ThisCall: |
| Duncan Sands | bf53c29 | 2008-09-10 13:22:10 +0000 | [diff] [blame] | 11237 | case CallingConv::Fast: |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11238 | // Pass 'nest' parameter in EAX. |
| 11239 | // Must be kept in sync with X86CallingConv.td |
| Duncan Sands | ee46574 | 2007-08-29 19:01:20 +0000 | [diff] [blame] | 11240 | NestReg = X86::EAX; |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11241 | break; |
| 11242 | } |
| 11243 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11244 | SDValue OutChains[4]; |
| 11245 | SDValue Addr, Disp; |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11246 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11247 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
| 11248 | DAG.getConstant(10, MVT::i32)); |
| 11249 | Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11250 | |
| Chris Lattner | a62fe66 | 2010-02-05 19:20:30 +0000 | [diff] [blame] | 11251 | // This is storing the opcode for MOV32ri. |
| 11252 | const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte. |
| Michael Liao | 7abf67a | 2012-10-04 19:50:43 +0000 | [diff] [blame] | 11253 | const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 11254 | OutChains[0] = DAG.getStore(Root, dl, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11255 | DAG.getConstant(MOV32ri|N86Reg, MVT::i8), |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11256 | Trmp, MachinePointerInfo(TrmpAddr), |
| 11257 | false, false, 0); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11258 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11259 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
| 11260 | DAG.getConstant(1, MVT::i32)); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11261 | OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, |
| 11262 | MachinePointerInfo(TrmpAddr, 1), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 11263 | false, false, 1); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11264 | |
| Chris Lattner | a62fe66 | 2010-02-05 19:20:30 +0000 | [diff] [blame] | 11265 | const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11266 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
| 11267 | DAG.getConstant(5, MVT::i32)); |
| 11268 | OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr, |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11269 | MachinePointerInfo(TrmpAddr, 5), |
| 11270 | false, false, 1); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11271 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11272 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, |
| 11273 | DAG.getConstant(6, MVT::i32)); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 11274 | OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, |
| 11275 | MachinePointerInfo(TrmpAddr, 6), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 11276 | false, false, 1); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11277 | |
| Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 11278 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4); |
| Duncan Sands | b116fac | 2007-07-27 20:02:49 +0000 | [diff] [blame] | 11279 | } |
| 11280 | } |
| 11281 | |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 11282 | SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, |
| 11283 | SelectionDAG &DAG) const { |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 11284 | /* |
| 11285 | The rounding mode is in bits 11:10 of FPSR, and has the following |
| 11286 | settings: |
| 11287 | 00 Round to nearest |
| 11288 | 01 Round to -inf |
| 11289 | 10 Round to +inf |
| 11290 | 11 Round to 0 |
| 11291 | |
| 11292 | FLT_ROUNDS, on the other hand, expects the following: |
| 11293 | -1 Undefined |
| 11294 | 0 Round to 0 |
| 11295 | 1 Round to nearest |
| 11296 | 2 Round to +inf |
| 11297 | 3 Round to -inf |
| 11298 | |
| 11299 | To perform the conversion, we do: |
| 11300 | (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3) |
| 11301 | */ |
| 11302 | |
| 11303 | MachineFunction &MF = DAG.getMachineFunction(); |
| 11304 | const TargetMachine &TM = MF.getTarget(); |
| Anton Korobeynikov | 16c29b5 | 2011-01-10 12:39:04 +0000 | [diff] [blame] | 11305 | const TargetFrameLowering &TFI = *TM.getFrameLowering(); |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 11306 | unsigned StackAlignment = TFI.getStackAlignment(); |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 11307 | EVT VT = Op.getValueType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11308 | SDLoc DL(Op); |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 11309 | |
| 11310 | // Save FP Control Word to stack slot |
| David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 11311 | int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11312 | SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 11313 | |
| Chris Lattner | 2156b79 | 2010-09-22 01:11:26 +0000 | [diff] [blame] | 11314 | MachineMemOperand *MMO = |
| 11315 | MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(SSFI), |
| 11316 | MachineMemOperand::MOStore, 2, 2); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 11317 | |
| Chris Lattner | 2156b79 | 2010-09-22 01:11:26 +0000 | [diff] [blame] | 11318 | SDValue Ops[] = { DAG.getEntryNode(), StackSlot }; |
| 11319 | SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL, |
| 11320 | DAG.getVTList(MVT::Other), |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 11321 | Ops, array_lengthof(Ops), MVT::i16, |
| 11322 | MMO); |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 11323 | |
| 11324 | // Load FP Control Word from stack slot |
| Chris Lattner | 2156b79 | 2010-09-22 01:11:26 +0000 | [diff] [blame] | 11325 | SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot, |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 11326 | MachinePointerInfo(), false, false, false, 0); |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 11327 | |
| 11328 | // Transform as necessary |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11329 | SDValue CWD1 = |
| Chris Lattner | 2156b79 | 2010-09-22 01:11:26 +0000 | [diff] [blame] | 11330 | DAG.getNode(ISD::SRL, DL, MVT::i16, |
| 11331 | DAG.getNode(ISD::AND, DL, MVT::i16, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11332 | CWD, DAG.getConstant(0x800, MVT::i16)), |
| 11333 | DAG.getConstant(11, MVT::i8)); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11334 | SDValue CWD2 = |
| Chris Lattner | 2156b79 | 2010-09-22 01:11:26 +0000 | [diff] [blame] | 11335 | DAG.getNode(ISD::SRL, DL, MVT::i16, |
| 11336 | DAG.getNode(ISD::AND, DL, MVT::i16, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11337 | CWD, DAG.getConstant(0x400, MVT::i16)), |
| 11338 | DAG.getConstant(9, MVT::i8)); |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 11339 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 11340 | SDValue RetVal = |
| Chris Lattner | 2156b79 | 2010-09-22 01:11:26 +0000 | [diff] [blame] | 11341 | DAG.getNode(ISD::AND, DL, MVT::i16, |
| 11342 | DAG.getNode(ISD::ADD, DL, MVT::i16, |
| 11343 | DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2), |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11344 | DAG.getConstant(1, MVT::i16)), |
| 11345 | DAG.getConstant(3, MVT::i16)); |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 11346 | |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 11347 | return DAG.getNode((VT.getSizeInBits() < 16 ? |
| Chris Lattner | 2156b79 | 2010-09-22 01:11:26 +0000 | [diff] [blame] | 11348 | ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal); |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 11349 | } |
| 11350 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 11351 | static SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) { |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 11352 | EVT VT = Op.getValueType(); |
| 11353 | EVT OpVT = VT; |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 11354 | unsigned NumBits = VT.getSizeInBits(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11355 | SDLoc dl(Op); |
| Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 11356 | |
| 11357 | Op = Op.getOperand(0); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11358 | if (VT == MVT::i8) { |
| Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 11359 | // Zero extend to i32 since there is not an i8 bsr. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11360 | OpVT = MVT::i32; |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 11361 | Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op); |
| Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 11362 | } |
| Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 11363 | |
| Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 11364 | // Issue a bsr (scan bits in reverse) which also sets EFLAGS. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11365 | SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 11366 | Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op); |
| Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 11367 | |
| 11368 | // If src is zero (i.e. bsr sets ZF), returns NumBits. |
| Benjamin Kramer | 7f1a560 | 2009-12-29 16:57:26 +0000 | [diff] [blame] | 11369 | SDValue Ops[] = { |
| 11370 | Op, |
| 11371 | DAG.getConstant(NumBits+NumBits-1, OpVT), |
| 11372 | DAG.getConstant(X86::COND_E, MVT::i8), |
| 11373 | Op.getValue(1) |
| 11374 | }; |
| 11375 | Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops, array_lengthof(Ops)); |
| Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 11376 | |
| 11377 | // Finally xor with NumBits-1. |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 11378 | Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT)); |
| Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 11379 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 11380 | if (VT == MVT::i8) |
| 11381 | Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op); |
| Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 11382 | return Op; |
| 11383 | } |
| 11384 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 11385 | static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, SelectionDAG &DAG) { |
| Chandler Carruth | acc068e | 2011-12-24 10:55:54 +0000 | [diff] [blame] | 11386 | EVT VT = Op.getValueType(); |
| 11387 | EVT OpVT = VT; |
| 11388 | unsigned NumBits = VT.getSizeInBits(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11389 | SDLoc dl(Op); |
| Chandler Carruth | acc068e | 2011-12-24 10:55:54 +0000 | [diff] [blame] | 11390 | |
| 11391 | Op = Op.getOperand(0); |
| 11392 | if (VT == MVT::i8) { |
| 11393 | // Zero extend to i32 since there is not an i8 bsr. |
| 11394 | OpVT = MVT::i32; |
| 11395 | Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op); |
| 11396 | } |
| 11397 | |
| 11398 | // Issue a bsr (scan bits in reverse). |
| 11399 | SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); |
| 11400 | Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op); |
| 11401 | |
| 11402 | // And xor with NumBits-1. |
| 11403 | Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT)); |
| 11404 | |
| 11405 | if (VT == MVT::i8) |
| 11406 | Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op); |
| 11407 | return Op; |
| 11408 | } |
| 11409 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 11410 | static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) { |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 11411 | EVT VT = Op.getValueType(); |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 11412 | unsigned NumBits = VT.getSizeInBits(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11413 | SDLoc dl(Op); |
| Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 11414 | Op = Op.getOperand(0); |
| Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 11415 | |
| 11416 | // Issue a bsf (scan bits forward) which also sets EFLAGS. |
| Chandler Carruth | 7782102 | 2011-12-24 12:12:34 +0000 | [diff] [blame] | 11417 | SDVTList VTs = DAG.getVTList(VT, MVT::i32); |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 11418 | Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op); |
| Evan Cheng | 152804e | 2007-12-14 08:30:15 +0000 | [diff] [blame] | 11419 | |
| 11420 | // If src is zero (i.e. bsf sets ZF), returns NumBits. |
| Benjamin Kramer | 7f1a560 | 2009-12-29 16:57:26 +0000 | [diff] [blame] | 11421 | SDValue Ops[] = { |
| 11422 | Op, |
| Chandler Carruth | 7782102 | 2011-12-24 12:12:34 +0000 | [diff] [blame] | 11423 | DAG.getConstant(NumBits, VT), |
| Benjamin Kramer | 7f1a560 | 2009-12-29 16:57:26 +0000 | [diff] [blame] | 11424 | DAG.getConstant(X86::COND_E, MVT::i8), |
| 11425 | Op.getValue(1) |
| 11426 | }; |
| Chandler Carruth | 7782102 | 2011-12-24 12:12:34 +0000 | [diff] [blame] | 11427 | return DAG.getNode(X86ISD::CMOV, dl, VT, Ops, array_lengthof(Ops)); |
| Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 11428 | } |
| 11429 | |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11430 | // Lower256IntArith - Break a 256-bit integer operation into two new 128-bit |
| 11431 | // ones, and then concatenate the result back. |
| 11432 | static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) { |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 11433 | EVT VT = Op.getValueType(); |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11434 | |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 11435 | assert(VT.is256BitVector() && VT.isInteger() && |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11436 | "Unsupported value type for operation"); |
| 11437 | |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 11438 | unsigned NumElems = VT.getVectorNumElements(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11439 | SDLoc dl(Op); |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11440 | |
| 11441 | // Extract the LHS vectors |
| 11442 | SDValue LHS = Op.getOperand(0); |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 11443 | SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl); |
| 11444 | SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl); |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11445 | |
| 11446 | // Extract the RHS vectors |
| 11447 | SDValue RHS = Op.getOperand(1); |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 11448 | SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl); |
| 11449 | SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl); |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11450 | |
| 11451 | MVT EltVT = VT.getVectorElementType().getSimpleVT(); |
| 11452 | EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2); |
| 11453 | |
| 11454 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, |
| 11455 | DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1), |
| 11456 | DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2)); |
| 11457 | } |
| 11458 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 11459 | static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 11460 | assert(Op.getValueType().is256BitVector() && |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11461 | Op.getValueType().isInteger() && |
| 11462 | "Only handle AVX 256-bit vector integer operation"); |
| 11463 | return Lower256IntArith(Op, DAG); |
| 11464 | } |
| 11465 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 11466 | static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) { |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 11467 | assert(Op.getValueType().is256BitVector() && |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11468 | Op.getValueType().isInteger() && |
| 11469 | "Only handle AVX 256-bit vector integer operation"); |
| 11470 | return Lower256IntArith(Op, DAG); |
| 11471 | } |
| 11472 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 11473 | static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget, |
| 11474 | SelectionDAG &DAG) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11475 | SDLoc dl(Op); |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11476 | EVT VT = Op.getValueType(); |
| 11477 | |
| 11478 | // Decompose 256-bit ops into smaller 128-bit ops. |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 11479 | if (VT.is256BitVector() && !Subtarget->hasInt256()) |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 11480 | return Lower256IntArith(Op, DAG); |
| 11481 | |
| Benjamin Kramer | 2f8a6cd | 2012-12-22 16:07:56 +0000 | [diff] [blame] | 11482 | SDValue A = Op.getOperand(0); |
| 11483 | SDValue B = Op.getOperand(1); |
| 11484 | |
| 11485 | // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle. |
| 11486 | if (VT == MVT::v4i32) { |
| 11487 | assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() && |
| 11488 | "Should not custom lower when pmuldq is available!"); |
| 11489 | |
| 11490 | // Extract the odd parts. |
| 11491 | const int UnpackMask[] = { 1, -1, 3, -1 }; |
| 11492 | SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask); |
| 11493 | SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask); |
| 11494 | |
| 11495 | // Multiply the even parts. |
| 11496 | SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B); |
| 11497 | // Now multiply odd parts. |
| 11498 | SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds); |
| 11499 | |
| 11500 | Evens = DAG.getNode(ISD::BITCAST, dl, VT, Evens); |
| 11501 | Odds = DAG.getNode(ISD::BITCAST, dl, VT, Odds); |
| 11502 | |
| 11503 | // Merge the two vectors back together with a shuffle. This expands into 2 |
| 11504 | // shuffles. |
| 11505 | const int ShufMask[] = { 0, 4, 2, 6 }; |
| 11506 | return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask); |
| 11507 | } |
| 11508 | |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 11509 | assert((VT == MVT::v2i64 || VT == MVT::v4i64) && |
| 11510 | "Only know how to lower V2I64/V4I64 multiply"); |
| 11511 | |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 11512 | // Ahi = psrlqi(a, 32); |
| 11513 | // Bhi = psrlqi(b, 32); |
| 11514 | // |
| 11515 | // AloBlo = pmuludq(a, b); |
| 11516 | // AloBhi = pmuludq(a, Bhi); |
| 11517 | // AhiBlo = pmuludq(Ahi, b); |
| 11518 | |
| 11519 | // AloBhi = psllqi(AloBhi, 32); |
| 11520 | // AhiBlo = psllqi(AhiBlo, 32); |
| 11521 | // return AloBlo + AloBhi + AhiBlo; |
| 11522 | |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 11523 | SDValue ShAmt = DAG.getConstant(32, MVT::i32); |
| Craig Topper | aaa643c | 2011-11-09 07:28:55 +0000 | [diff] [blame] | 11524 | |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 11525 | SDValue Ahi = DAG.getNode(X86ISD::VSRLI, dl, VT, A, ShAmt); |
| 11526 | SDValue Bhi = DAG.getNode(X86ISD::VSRLI, dl, VT, B, ShAmt); |
| Craig Topper | aaa643c | 2011-11-09 07:28:55 +0000 | [diff] [blame] | 11527 | |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 11528 | // Bit cast to 32-bit vectors for MULUDQ |
| 11529 | EVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 : MVT::v8i32; |
| 11530 | A = DAG.getNode(ISD::BITCAST, dl, MulVT, A); |
| 11531 | B = DAG.getNode(ISD::BITCAST, dl, MulVT, B); |
| 11532 | Ahi = DAG.getNode(ISD::BITCAST, dl, MulVT, Ahi); |
| 11533 | Bhi = DAG.getNode(ISD::BITCAST, dl, MulVT, Bhi); |
| Craig Topper | aaa643c | 2011-11-09 07:28:55 +0000 | [diff] [blame] | 11534 | |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 11535 | SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B); |
| 11536 | SDValue AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi); |
| 11537 | SDValue AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B); |
| Craig Topper | aaa643c | 2011-11-09 07:28:55 +0000 | [diff] [blame] | 11538 | |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 11539 | AloBhi = DAG.getNode(X86ISD::VSHLI, dl, VT, AloBhi, ShAmt); |
| 11540 | AhiBlo = DAG.getNode(X86ISD::VSHLI, dl, VT, AhiBlo, ShAmt); |
| Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 11541 | |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 11542 | SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi); |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 11543 | return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo); |
| Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 11544 | } |
| 11545 | |
| Nadav Rotem | 13f8cf5 | 2013-01-09 05:14:33 +0000 | [diff] [blame] | 11546 | SDValue X86TargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const { |
| 11547 | EVT VT = Op.getValueType(); |
| 11548 | EVT EltTy = VT.getVectorElementType(); |
| 11549 | unsigned NumElts = VT.getVectorNumElements(); |
| 11550 | SDValue N0 = Op.getOperand(0); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11551 | SDLoc dl(Op); |
| Nadav Rotem | 13f8cf5 | 2013-01-09 05:14:33 +0000 | [diff] [blame] | 11552 | |
| 11553 | // Lower sdiv X, pow2-const. |
| 11554 | BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(Op.getOperand(1)); |
| 11555 | if (!C) |
| 11556 | return SDValue(); |
| 11557 | |
| 11558 | APInt SplatValue, SplatUndef; |
| Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 11559 | unsigned SplatBitSize; |
| Nadav Rotem | 13f8cf5 | 2013-01-09 05:14:33 +0000 | [diff] [blame] | 11560 | bool HasAnyUndefs; |
| Elena Demikhovsky | 87070fe | 2013-06-26 10:55:03 +0000 | [diff] [blame] | 11561 | if (!C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize, |
| 11562 | HasAnyUndefs) || |
| 11563 | EltTy.getSizeInBits() < SplatBitSize) |
| Nadav Rotem | 13f8cf5 | 2013-01-09 05:14:33 +0000 | [diff] [blame] | 11564 | return SDValue(); |
| 11565 | |
| 11566 | if ((SplatValue != 0) && |
| 11567 | (SplatValue.isPowerOf2() || (-SplatValue).isPowerOf2())) { |
| 11568 | unsigned lg2 = SplatValue.countTrailingZeros(); |
| 11569 | // Splat the sign bit. |
| 11570 | SDValue Sz = DAG.getConstant(EltTy.getSizeInBits()-1, MVT::i32); |
| 11571 | SDValue SGN = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, N0, Sz, DAG); |
| 11572 | // Add (N0 < 0) ? abs2 - 1 : 0; |
| 11573 | SDValue Amt = DAG.getConstant(EltTy.getSizeInBits() - lg2, MVT::i32); |
| 11574 | SDValue SRL = getTargetVShiftNode(X86ISD::VSRLI, dl, VT, SGN, Amt, DAG); |
| 11575 | SDValue ADD = DAG.getNode(ISD::ADD, dl, VT, N0, SRL); |
| 11576 | SDValue Lg2Amt = DAG.getConstant(lg2, MVT::i32); |
| 11577 | SDValue SRA = getTargetVShiftNode(X86ISD::VSRAI, dl, VT, ADD, Lg2Amt, DAG); |
| 11578 | |
| 11579 | // If we're dividing by a positive value, we're done. Otherwise, we must |
| 11580 | // negate the result. |
| 11581 | if (SplatValue.isNonNegative()) |
| 11582 | return SRA; |
| 11583 | |
| 11584 | SmallVector<SDValue, 16> V(NumElts, DAG.getConstant(0, EltTy)); |
| 11585 | SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], NumElts); |
| 11586 | return DAG.getNode(ISD::SUB, dl, VT, Zero, SRA); |
| 11587 | } |
| 11588 | return SDValue(); |
| 11589 | } |
| 11590 | |
| Michael Liao | 4b7ab12 | 2013-03-20 02:20:36 +0000 | [diff] [blame] | 11591 | static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG, |
| 11592 | const X86Subtarget *Subtarget) { |
| Nate Begeman | bdcb5af | 2010-07-27 22:37:06 +0000 | [diff] [blame] | 11593 | EVT VT = Op.getValueType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11594 | SDLoc dl(Op); |
| Nate Begeman | bdcb5af | 2010-07-27 22:37:06 +0000 | [diff] [blame] | 11595 | SDValue R = Op.getOperand(0); |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 11596 | SDValue Amt = Op.getOperand(1); |
| Nate Begeman | bdcb5af | 2010-07-27 22:37:06 +0000 | [diff] [blame] | 11597 | |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 11598 | // Optimize shl/srl/sra with constant shift amount. |
| 11599 | if (isSplatVector(Amt.getNode())) { |
| 11600 | SDValue SclrAmt = Amt->getOperand(0); |
| 11601 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) { |
| 11602 | uint64_t ShiftAmt = C->getZExtValue(); |
| 11603 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11604 | if (VT == MVT::v2i64 || VT == MVT::v4i32 || VT == MVT::v8i16 || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 11605 | (Subtarget->hasInt256() && |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11606 | (VT == MVT::v4i64 || VT == MVT::v8i32 || VT == MVT::v16i16))) { |
| 11607 | if (Op.getOpcode() == ISD::SHL) |
| 11608 | return DAG.getNode(X86ISD::VSHLI, dl, VT, R, |
| 11609 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| 11610 | if (Op.getOpcode() == ISD::SRL) |
| 11611 | return DAG.getNode(X86ISD::VSRLI, dl, VT, R, |
| 11612 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| 11613 | if (Op.getOpcode() == ISD::SRA && VT != MVT::v2i64 && VT != MVT::v4i64) |
| 11614 | return DAG.getNode(X86ISD::VSRAI, dl, VT, R, |
| 11615 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| Benjamin Kramer | dade3c1 | 2011-10-30 17:31:21 +0000 | [diff] [blame] | 11616 | } |
| 11617 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11618 | if (VT == MVT::v16i8) { |
| 11619 | if (Op.getOpcode() == ISD::SHL) { |
| 11620 | // Make a large shift. |
| 11621 | SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v8i16, R, |
| 11622 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| 11623 | SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL); |
| 11624 | // Zero out the rightmost bits. |
| 11625 | SmallVector<SDValue, 16> V(16, |
| 11626 | DAG.getConstant(uint8_t(-1U << ShiftAmt), |
| 11627 | MVT::i8)); |
| 11628 | return DAG.getNode(ISD::AND, dl, VT, SHL, |
| 11629 | DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16)); |
| Eli Friedman | f6aa6b1 | 2011-11-01 21:18:39 +0000 | [diff] [blame] | 11630 | } |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11631 | if (Op.getOpcode() == ISD::SRL) { |
| 11632 | // Make a large shift. |
| 11633 | SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v8i16, R, |
| 11634 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| 11635 | SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL); |
| 11636 | // Zero out the leftmost bits. |
| 11637 | SmallVector<SDValue, 16> V(16, |
| 11638 | DAG.getConstant(uint8_t(-1U) >> ShiftAmt, |
| 11639 | MVT::i8)); |
| 11640 | return DAG.getNode(ISD::AND, dl, VT, SRL, |
| 11641 | DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16)); |
| 11642 | } |
| 11643 | if (Op.getOpcode() == ISD::SRA) { |
| 11644 | if (ShiftAmt == 7) { |
| 11645 | // R s>> 7 === R s< 0 |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 11646 | SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl); |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 11647 | return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R); |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11648 | } |
| Eli Friedman | f6aa6b1 | 2011-11-01 21:18:39 +0000 | [diff] [blame] | 11649 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11650 | // R s>> a === ((R u>> a) ^ m) - m |
| 11651 | SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt); |
| 11652 | SmallVector<SDValue, 16> V(16, DAG.getConstant(128 >> ShiftAmt, |
| 11653 | MVT::i8)); |
| 11654 | SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 16); |
| 11655 | Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask); |
| 11656 | Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask); |
| 11657 | return Res; |
| 11658 | } |
| Craig Topper | 731dfd0 | 2012-04-23 03:42:40 +0000 | [diff] [blame] | 11659 | llvm_unreachable("Unknown shift opcode."); |
| Eli Friedman | f6aa6b1 | 2011-11-01 21:18:39 +0000 | [diff] [blame] | 11660 | } |
| Craig Topper | 46154eb | 2011-11-11 07:39:23 +0000 | [diff] [blame] | 11661 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 11662 | if (Subtarget->hasInt256() && VT == MVT::v32i8) { |
| Craig Topper | 0d86d46 | 2011-11-20 00:12:05 +0000 | [diff] [blame] | 11663 | if (Op.getOpcode() == ISD::SHL) { |
| 11664 | // Make a large shift. |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11665 | SDValue SHL = DAG.getNode(X86ISD::VSHLI, dl, MVT::v16i16, R, |
| 11666 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| 11667 | SHL = DAG.getNode(ISD::BITCAST, dl, VT, SHL); |
| Craig Topper | 0d86d46 | 2011-11-20 00:12:05 +0000 | [diff] [blame] | 11668 | // Zero out the rightmost bits. |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11669 | SmallVector<SDValue, 32> V(32, |
| 11670 | DAG.getConstant(uint8_t(-1U << ShiftAmt), |
| 11671 | MVT::i8)); |
| Craig Topper | 0d86d46 | 2011-11-20 00:12:05 +0000 | [diff] [blame] | 11672 | return DAG.getNode(ISD::AND, dl, VT, SHL, |
| 11673 | DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32)); |
| Craig Topper | 46154eb | 2011-11-11 07:39:23 +0000 | [diff] [blame] | 11674 | } |
| Craig Topper | 0d86d46 | 2011-11-20 00:12:05 +0000 | [diff] [blame] | 11675 | if (Op.getOpcode() == ISD::SRL) { |
| 11676 | // Make a large shift. |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11677 | SDValue SRL = DAG.getNode(X86ISD::VSRLI, dl, MVT::v16i16, R, |
| 11678 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| 11679 | SRL = DAG.getNode(ISD::BITCAST, dl, VT, SRL); |
| Craig Topper | 0d86d46 | 2011-11-20 00:12:05 +0000 | [diff] [blame] | 11680 | // Zero out the leftmost bits. |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11681 | SmallVector<SDValue, 32> V(32, |
| 11682 | DAG.getConstant(uint8_t(-1U) >> ShiftAmt, |
| 11683 | MVT::i8)); |
| Craig Topper | 0d86d46 | 2011-11-20 00:12:05 +0000 | [diff] [blame] | 11684 | return DAG.getNode(ISD::AND, dl, VT, SRL, |
| 11685 | DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32)); |
| 11686 | } |
| 11687 | if (Op.getOpcode() == ISD::SRA) { |
| 11688 | if (ShiftAmt == 7) { |
| 11689 | // R s>> 7 === R s< 0 |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 11690 | SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl); |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 11691 | return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R); |
| Craig Topper | 0d86d46 | 2011-11-20 00:12:05 +0000 | [diff] [blame] | 11692 | } |
| 11693 | |
| 11694 | // R s>> a === ((R u>> a) ^ m) - m |
| 11695 | SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt); |
| 11696 | SmallVector<SDValue, 32> V(32, DAG.getConstant(128 >> ShiftAmt, |
| 11697 | MVT::i8)); |
| 11698 | SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &V[0], 32); |
| 11699 | Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask); |
| 11700 | Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask); |
| 11701 | return Res; |
| 11702 | } |
| Craig Topper | 731dfd0 | 2012-04-23 03:42:40 +0000 | [diff] [blame] | 11703 | llvm_unreachable("Unknown shift opcode."); |
| Craig Topper | 0d86d46 | 2011-11-20 00:12:05 +0000 | [diff] [blame] | 11704 | } |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 11705 | } |
| 11706 | } |
| 11707 | |
| Michael Liao | 42317cc | 2013-03-20 02:33:21 +0000 | [diff] [blame] | 11708 | // Special case in 32-bit mode, where i64 is expanded into high and low parts. |
| 11709 | if (!Subtarget->is64Bit() && |
| 11710 | (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) && |
| 11711 | Amt.getOpcode() == ISD::BITCAST && |
| 11712 | Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) { |
| 11713 | Amt = Amt.getOperand(0); |
| 11714 | unsigned Ratio = Amt.getValueType().getVectorNumElements() / |
| 11715 | VT.getVectorNumElements(); |
| 11716 | unsigned RatioInLog2 = Log2_32_Ceil(Ratio); |
| 11717 | uint64_t ShiftAmt = 0; |
| 11718 | for (unsigned i = 0; i != Ratio; ++i) { |
| 11719 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Amt.getOperand(i)); |
| 11720 | if (C == 0) |
| 11721 | return SDValue(); |
| 11722 | // 6 == Log2(64) |
| 11723 | ShiftAmt |= C->getZExtValue() << (i * (1 << (6 - RatioInLog2))); |
| 11724 | } |
| 11725 | // Check remaining shift amounts. |
| 11726 | for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) { |
| 11727 | uint64_t ShAmt = 0; |
| 11728 | for (unsigned j = 0; j != Ratio; ++j) { |
| 11729 | ConstantSDNode *C = |
| 11730 | dyn_cast<ConstantSDNode>(Amt.getOperand(i + j)); |
| 11731 | if (C == 0) |
| 11732 | return SDValue(); |
| 11733 | // 6 == Log2(64) |
| 11734 | ShAmt |= C->getZExtValue() << (j * (1 << (6 - RatioInLog2))); |
| 11735 | } |
| 11736 | if (ShAmt != ShiftAmt) |
| 11737 | return SDValue(); |
| 11738 | } |
| 11739 | switch (Op.getOpcode()) { |
| 11740 | default: |
| 11741 | llvm_unreachable("Unknown shift opcode!"); |
| 11742 | case ISD::SHL: |
| 11743 | return DAG.getNode(X86ISD::VSHLI, dl, VT, R, |
| 11744 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| 11745 | case ISD::SRL: |
| 11746 | return DAG.getNode(X86ISD::VSRLI, dl, VT, R, |
| 11747 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| 11748 | case ISD::SRA: |
| 11749 | return DAG.getNode(X86ISD::VSRAI, dl, VT, R, |
| 11750 | DAG.getConstant(ShiftAmt, MVT::i32)); |
| 11751 | } |
| 11752 | } |
| 11753 | |
| 11754 | return SDValue(); |
| 11755 | } |
| 11756 | |
| 11757 | static SDValue LowerScalarVariableShift(SDValue Op, SelectionDAG &DAG, |
| 11758 | const X86Subtarget* Subtarget) { |
| 11759 | EVT VT = Op.getValueType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11760 | SDLoc dl(Op); |
| Michael Liao | 42317cc | 2013-03-20 02:33:21 +0000 | [diff] [blame] | 11761 | SDValue R = Op.getOperand(0); |
| 11762 | SDValue Amt = Op.getOperand(1); |
| 11763 | |
| 11764 | if ((VT == MVT::v2i64 && Op.getOpcode() != ISD::SRA) || |
| 11765 | VT == MVT::v4i32 || VT == MVT::v8i16 || |
| 11766 | (Subtarget->hasInt256() && |
| 11767 | ((VT == MVT::v4i64 && Op.getOpcode() != ISD::SRA) || |
| 11768 | VT == MVT::v8i32 || VT == MVT::v16i16))) { |
| 11769 | SDValue BaseShAmt; |
| 11770 | EVT EltVT = VT.getVectorElementType(); |
| 11771 | |
| 11772 | if (Amt.getOpcode() == ISD::BUILD_VECTOR) { |
| 11773 | unsigned NumElts = VT.getVectorNumElements(); |
| 11774 | unsigned i, j; |
| 11775 | for (i = 0; i != NumElts; ++i) { |
| 11776 | if (Amt.getOperand(i).getOpcode() == ISD::UNDEF) |
| 11777 | continue; |
| 11778 | break; |
| 11779 | } |
| 11780 | for (j = i; j != NumElts; ++j) { |
| 11781 | SDValue Arg = Amt.getOperand(j); |
| 11782 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 11783 | if (Arg != Amt.getOperand(i)) |
| 11784 | break; |
| 11785 | } |
| 11786 | if (i != NumElts && j == NumElts) |
| 11787 | BaseShAmt = Amt.getOperand(i); |
| 11788 | } else { |
| 11789 | if (Amt.getOpcode() == ISD::EXTRACT_SUBVECTOR) |
| 11790 | Amt = Amt.getOperand(0); |
| 11791 | if (Amt.getOpcode() == ISD::VECTOR_SHUFFLE && |
| 11792 | cast<ShuffleVectorSDNode>(Amt)->isSplat()) { |
| 11793 | SDValue InVec = Amt.getOperand(0); |
| 11794 | if (InVec.getOpcode() == ISD::BUILD_VECTOR) { |
| 11795 | unsigned NumElts = InVec.getValueType().getVectorNumElements(); |
| 11796 | unsigned i = 0; |
| 11797 | for (; i != NumElts; ++i) { |
| 11798 | SDValue Arg = InVec.getOperand(i); |
| 11799 | if (Arg.getOpcode() == ISD::UNDEF) continue; |
| 11800 | BaseShAmt = Arg; |
| 11801 | break; |
| 11802 | } |
| 11803 | } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) { |
| 11804 | if (ConstantSDNode *C = |
| 11805 | dyn_cast<ConstantSDNode>(InVec.getOperand(2))) { |
| 11806 | unsigned SplatIdx = |
| 11807 | cast<ShuffleVectorSDNode>(Amt)->getSplatIndex(); |
| 11808 | if (C->getZExtValue() == SplatIdx) |
| 11809 | BaseShAmt = InVec.getOperand(1); |
| 11810 | } |
| 11811 | } |
| 11812 | if (BaseShAmt.getNode() == 0) |
| 11813 | BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Amt, |
| 11814 | DAG.getIntPtrConstant(0)); |
| 11815 | } |
| 11816 | } |
| 11817 | |
| 11818 | if (BaseShAmt.getNode()) { |
| 11819 | if (EltVT.bitsGT(MVT::i32)) |
| 11820 | BaseShAmt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BaseShAmt); |
| 11821 | else if (EltVT.bitsLT(MVT::i32)) |
| 11822 | BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt); |
| 11823 | |
| 11824 | switch (Op.getOpcode()) { |
| 11825 | default: |
| 11826 | llvm_unreachable("Unknown shift opcode!"); |
| 11827 | case ISD::SHL: |
| 11828 | switch (VT.getSimpleVT().SimpleTy) { |
| 11829 | default: return SDValue(); |
| 11830 | case MVT::v2i64: |
| 11831 | case MVT::v4i32: |
| 11832 | case MVT::v8i16: |
| 11833 | case MVT::v4i64: |
| 11834 | case MVT::v8i32: |
| 11835 | case MVT::v16i16: |
| 11836 | return getTargetVShiftNode(X86ISD::VSHLI, dl, VT, R, BaseShAmt, DAG); |
| 11837 | } |
| 11838 | case ISD::SRA: |
| 11839 | switch (VT.getSimpleVT().SimpleTy) { |
| 11840 | default: return SDValue(); |
| 11841 | case MVT::v4i32: |
| 11842 | case MVT::v8i16: |
| 11843 | case MVT::v8i32: |
| 11844 | case MVT::v16i16: |
| 11845 | return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, R, BaseShAmt, DAG); |
| 11846 | } |
| 11847 | case ISD::SRL: |
| 11848 | switch (VT.getSimpleVT().SimpleTy) { |
| 11849 | default: return SDValue(); |
| 11850 | case MVT::v2i64: |
| 11851 | case MVT::v4i32: |
| 11852 | case MVT::v8i16: |
| 11853 | case MVT::v4i64: |
| 11854 | case MVT::v8i32: |
| 11855 | case MVT::v16i16: |
| 11856 | return getTargetVShiftNode(X86ISD::VSRLI, dl, VT, R, BaseShAmt, DAG); |
| 11857 | } |
| 11858 | } |
| 11859 | } |
| 11860 | } |
| 11861 | |
| 11862 | // Special case in 32-bit mode, where i64 is expanded into high and low parts. |
| 11863 | if (!Subtarget->is64Bit() && |
| 11864 | (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) && |
| 11865 | Amt.getOpcode() == ISD::BITCAST && |
| 11866 | Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) { |
| 11867 | Amt = Amt.getOperand(0); |
| 11868 | unsigned Ratio = Amt.getValueType().getVectorNumElements() / |
| 11869 | VT.getVectorNumElements(); |
| 11870 | std::vector<SDValue> Vals(Ratio); |
| 11871 | for (unsigned i = 0; i != Ratio; ++i) |
| 11872 | Vals[i] = Amt.getOperand(i); |
| 11873 | for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) { |
| 11874 | for (unsigned j = 0; j != Ratio; ++j) |
| 11875 | if (Vals[j] != Amt.getOperand(i + j)) |
| 11876 | return SDValue(); |
| 11877 | } |
| 11878 | switch (Op.getOpcode()) { |
| 11879 | default: |
| 11880 | llvm_unreachable("Unknown shift opcode!"); |
| 11881 | case ISD::SHL: |
| 11882 | return DAG.getNode(X86ISD::VSHL, dl, VT, R, Op.getOperand(1)); |
| 11883 | case ISD::SRL: |
| 11884 | return DAG.getNode(X86ISD::VSRL, dl, VT, R, Op.getOperand(1)); |
| 11885 | case ISD::SRA: |
| 11886 | return DAG.getNode(X86ISD::VSRA, dl, VT, R, Op.getOperand(1)); |
| 11887 | } |
| 11888 | } |
| 11889 | |
| Michael Liao | 4b7ab12 | 2013-03-20 02:20:36 +0000 | [diff] [blame] | 11890 | return SDValue(); |
| 11891 | } |
| 11892 | |
| 11893 | SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const { |
| 11894 | |
| 11895 | EVT VT = Op.getValueType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 11896 | SDLoc dl(Op); |
| Michael Liao | 4b7ab12 | 2013-03-20 02:20:36 +0000 | [diff] [blame] | 11897 | SDValue R = Op.getOperand(0); |
| 11898 | SDValue Amt = Op.getOperand(1); |
| 11899 | SDValue V; |
| 11900 | |
| 11901 | if (!Subtarget->hasSSE2()) |
| 11902 | return SDValue(); |
| 11903 | |
| 11904 | V = LowerScalarImmediateShift(Op, DAG, Subtarget); |
| 11905 | if (V.getNode()) |
| 11906 | return V; |
| 11907 | |
| Michael Liao | 42317cc | 2013-03-20 02:33:21 +0000 | [diff] [blame] | 11908 | V = LowerScalarVariableShift(Op, DAG, Subtarget); |
| 11909 | if (V.getNode()) |
| 11910 | return V; |
| 11911 | |
| Michael Liao | 5c5f190 | 2013-03-20 02:28:20 +0000 | [diff] [blame] | 11912 | // AVX2 has VPSLLV/VPSRAV/VPSRLV. |
| 11913 | if (Subtarget->hasInt256()) { |
| 11914 | if (Op.getOpcode() == ISD::SRL && |
| 11915 | (VT == MVT::v2i64 || VT == MVT::v4i32 || |
| 11916 | VT == MVT::v4i64 || VT == MVT::v8i32)) |
| 11917 | return Op; |
| 11918 | if (Op.getOpcode() == ISD::SHL && |
| 11919 | (VT == MVT::v2i64 || VT == MVT::v4i32 || |
| 11920 | VT == MVT::v4i64 || VT == MVT::v8i32)) |
| 11921 | return Op; |
| 11922 | if (Op.getOpcode() == ISD::SRA && (VT == MVT::v4i32 || VT == MVT::v8i32)) |
| 11923 | return Op; |
| 11924 | } |
| 11925 | |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 11926 | // Lower SHL with variable shift amount. |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 11927 | if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) { |
| Benjamin Kramer | a220aeb | 2013-02-04 15:19:33 +0000 | [diff] [blame] | 11928 | Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT)); |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 11929 | |
| Benjamin Kramer | 9fa9251 | 2013-02-04 15:19:25 +0000 | [diff] [blame] | 11930 | Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT)); |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 11931 | Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op); |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 11932 | Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op); |
| 11933 | return DAG.getNode(ISD::MUL, dl, VT, Op, R); |
| 11934 | } |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 11935 | if (VT == MVT::v16i8 && Op->getOpcode() == ISD::SHL) { |
| Craig Topper | 8b5a6b6 | 2012-01-17 08:23:44 +0000 | [diff] [blame] | 11936 | assert(Subtarget->hasSSE2() && "Need SSE2 for pslli/pcmpeq."); |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11937 | |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 11938 | // a = a << 5; |
| Benjamin Kramer | a220aeb | 2013-02-04 15:19:33 +0000 | [diff] [blame] | 11939 | Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(5, VT)); |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11940 | Op = DAG.getNode(ISD::BITCAST, dl, VT, Op); |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 11941 | |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11942 | // Turn 'a' into a mask suitable for VSELECT |
| 11943 | SDValue VSelM = DAG.getConstant(0x80, VT); |
| 11944 | SDValue OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op); |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 11945 | OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM); |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 11946 | |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11947 | SDValue CM1 = DAG.getConstant(0x0f, VT); |
| 11948 | SDValue CM2 = DAG.getConstant(0x3f, VT); |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 11949 | |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11950 | // r = VSELECT(r, psllw(r & (char16)15, 4), a); |
| 11951 | SDValue M = DAG.getNode(ISD::AND, dl, VT, R, CM1); |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11952 | M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M, |
| 11953 | DAG.getConstant(4, MVT::i32), DAG); |
| 11954 | M = DAG.getNode(ISD::BITCAST, dl, VT, M); |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11955 | R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R); |
| 11956 | |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 11957 | // a += a |
| 11958 | Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op); |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11959 | OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op); |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 11960 | OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 11961 | |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11962 | // r = VSELECT(r, psllw(r & (char16)63, 2), a); |
| 11963 | M = DAG.getNode(ISD::AND, dl, VT, R, CM2); |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11964 | M = getTargetVShiftNode(X86ISD::VSHLI, dl, MVT::v8i16, M, |
| 11965 | DAG.getConstant(2, MVT::i32), DAG); |
| 11966 | M = DAG.getNode(ISD::BITCAST, dl, VT, M); |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11967 | R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, M, R); |
| 11968 | |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 11969 | // a += a |
| 11970 | Op = DAG.getNode(ISD::ADD, dl, VT, Op, Op); |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11971 | OpVSel = DAG.getNode(ISD::AND, dl, VT, VSelM, Op); |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 11972 | OpVSel = DAG.getNode(X86ISD::PCMPEQ, dl, VT, OpVSel, VSelM); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 11973 | |
| Lang Hames | 8b99c1e | 2011-12-17 01:08:46 +0000 | [diff] [blame] | 11974 | // return VSELECT(r, r+r, a); |
| 11975 | R = DAG.getNode(ISD::VSELECT, dl, VT, OpVSel, |
| Lang Hames | a0a2513 | 2011-12-15 18:57:27 +0000 | [diff] [blame] | 11976 | DAG.getNode(ISD::ADD, dl, VT, R, R), R); |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 11977 | return R; |
| 11978 | } |
| Craig Topper | 46154eb | 2011-11-11 07:39:23 +0000 | [diff] [blame] | 11979 | |
| 11980 | // Decompose 256-bit shifts into smaller 128-bit shifts. |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 11981 | if (VT.is256BitVector()) { |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11982 | unsigned NumElems = VT.getVectorNumElements(); |
| Craig Topper | 46154eb | 2011-11-11 07:39:23 +0000 | [diff] [blame] | 11983 | MVT EltVT = VT.getVectorElementType().getSimpleVT(); |
| 11984 | EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2); |
| 11985 | |
| 11986 | // Extract the two vectors |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 11987 | SDValue V1 = Extract128BitVector(R, 0, DAG, dl); |
| 11988 | SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl); |
| Craig Topper | 46154eb | 2011-11-11 07:39:23 +0000 | [diff] [blame] | 11989 | |
| 11990 | // Recreate the shift amount vectors |
| 11991 | SDValue Amt1, Amt2; |
| 11992 | if (Amt.getOpcode() == ISD::BUILD_VECTOR) { |
| 11993 | // Constant shift amount |
| 11994 | SmallVector<SDValue, 4> Amt1Csts; |
| 11995 | SmallVector<SDValue, 4> Amt2Csts; |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11996 | for (unsigned i = 0; i != NumElems/2; ++i) |
| Craig Topper | 46154eb | 2011-11-11 07:39:23 +0000 | [diff] [blame] | 11997 | Amt1Csts.push_back(Amt->getOperand(i)); |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 11998 | for (unsigned i = NumElems/2; i != NumElems; ++i) |
| Craig Topper | 46154eb | 2011-11-11 07:39:23 +0000 | [diff] [blame] | 11999 | Amt2Csts.push_back(Amt->getOperand(i)); |
| 12000 | |
| 12001 | Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT, |
| 12002 | &Amt1Csts[0], NumElems/2); |
| 12003 | Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT, |
| 12004 | &Amt2Csts[0], NumElems/2); |
| 12005 | } else { |
| 12006 | // Variable shift amount |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 12007 | Amt1 = Extract128BitVector(Amt, 0, DAG, dl); |
| 12008 | Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl); |
| Craig Topper | 46154eb | 2011-11-11 07:39:23 +0000 | [diff] [blame] | 12009 | } |
| 12010 | |
| 12011 | // Issue new vector shifts for the smaller types |
| 12012 | V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1); |
| 12013 | V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2); |
| 12014 | |
| 12015 | // Concatenate the result back |
| 12016 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2); |
| 12017 | } |
| 12018 | |
| Nate Begeman | 5140921 | 2010-07-28 00:21:48 +0000 | [diff] [blame] | 12019 | return SDValue(); |
| Nate Begeman | bdcb5af | 2010-07-27 22:37:06 +0000 | [diff] [blame] | 12020 | } |
| Mon P Wang | af9b952 | 2008-12-18 21:42:19 +0000 | [diff] [blame] | 12021 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12022 | static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) { |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12023 | // Lower the "add/sub/mul with overflow" instruction into a regular ins plus |
| 12024 | // a "setcc" instruction that checks the overflow flag. The "brcond" lowering |
| Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 12025 | // looks for this combo and may remove the "setcc" instruction if the "setcc" |
| 12026 | // has only one use. |
| Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 12027 | SDNode *N = Op.getNode(); |
| Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 12028 | SDValue LHS = N->getOperand(0); |
| 12029 | SDValue RHS = N->getOperand(1); |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12030 | unsigned BaseOp = 0; |
| 12031 | unsigned Cond = 0; |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12032 | SDLoc DL(Op); |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12033 | switch (Op.getOpcode()) { |
| Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 12034 | default: llvm_unreachable("Unknown ovf instruction!"); |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12035 | case ISD::SADDO: |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 12036 | // A subtract of one will be selected as a INC. Note that INC doesn't |
| 12037 | // set CF, so we can't do this for UADDO. |
| Benjamin Kramer | c175a4b | 2011-03-08 15:20:20 +0000 | [diff] [blame] | 12038 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) |
| 12039 | if (C->isOne()) { |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 12040 | BaseOp = X86ISD::INC; |
| 12041 | Cond = X86::COND_O; |
| 12042 | break; |
| 12043 | } |
| Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 12044 | BaseOp = X86ISD::ADD; |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12045 | Cond = X86::COND_O; |
| 12046 | break; |
| 12047 | case ISD::UADDO: |
| Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 12048 | BaseOp = X86ISD::ADD; |
| Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 12049 | Cond = X86::COND_B; |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12050 | break; |
| 12051 | case ISD::SSUBO: |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 12052 | // A subtract of one will be selected as a DEC. Note that DEC doesn't |
| 12053 | // set CF, so we can't do this for USUBO. |
| Benjamin Kramer | c175a4b | 2011-03-08 15:20:20 +0000 | [diff] [blame] | 12054 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS)) |
| 12055 | if (C->isOne()) { |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 12056 | BaseOp = X86ISD::DEC; |
| 12057 | Cond = X86::COND_O; |
| 12058 | break; |
| 12059 | } |
| Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 12060 | BaseOp = X86ISD::SUB; |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12061 | Cond = X86::COND_O; |
| 12062 | break; |
| 12063 | case ISD::USUBO: |
| Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 12064 | BaseOp = X86ISD::SUB; |
| Dan Gohman | 653456c | 2009-01-07 00:15:08 +0000 | [diff] [blame] | 12065 | Cond = X86::COND_B; |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12066 | break; |
| 12067 | case ISD::SMULO: |
| Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 12068 | BaseOp = X86ISD::SMUL; |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12069 | Cond = X86::COND_O; |
| 12070 | break; |
| Chris Lattner | b20e0b1 | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 12071 | case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs |
| 12072 | SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0), |
| 12073 | MVT::i32); |
| 12074 | SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 12075 | |
| Chris Lattner | b20e0b1 | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 12076 | SDValue SetCC = |
| 12077 | DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 12078 | DAG.getConstant(X86::COND_O, MVT::i32), |
| 12079 | SDValue(Sum.getNode(), 2)); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 12080 | |
| Dan Gohman | 6e5fda2 | 2011-07-22 18:45:15 +0000 | [diff] [blame] | 12081 | return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC); |
| Chris Lattner | b20e0b1 | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 12082 | } |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12083 | } |
| Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 12084 | |
| Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 12085 | // Also sets EFLAGS. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12086 | SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32); |
| Chris Lattner | b20e0b1 | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 12087 | SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS); |
| Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 12088 | |
| Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 12089 | SDValue SetCC = |
| Chris Lattner | b20e0b1 | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 12090 | DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1), |
| 12091 | DAG.getConstant(Cond, MVT::i32), |
| 12092 | SDValue(Sum.getNode(), 1)); |
| Bill Wendling | 3fafd93 | 2008-11-26 22:37:40 +0000 | [diff] [blame] | 12093 | |
| Dan Gohman | 6e5fda2 | 2011-07-22 18:45:15 +0000 | [diff] [blame] | 12094 | return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC); |
| Bill Wendling | 41ea7e7 | 2008-11-24 19:21:46 +0000 | [diff] [blame] | 12095 | } |
| 12096 | |
| Chad Rosier | 30450e8 | 2011-12-22 22:35:21 +0000 | [diff] [blame] | 12097 | SDValue X86TargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op, |
| 12098 | SelectionDAG &DAG) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12099 | SDLoc dl(Op); |
| Craig Topper | a124f94 | 2011-11-21 01:12:36 +0000 | [diff] [blame] | 12100 | EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT(); |
| 12101 | EVT VT = Op.getValueType(); |
| 12102 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12103 | if (!Subtarget->hasSSE2() || !VT.isVector()) |
| 12104 | return SDValue(); |
| Nadav Rotem | d0f3ef8 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 12105 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12106 | unsigned BitsDiff = VT.getScalarType().getSizeInBits() - |
| 12107 | ExtraVT.getScalarType().getSizeInBits(); |
| 12108 | SDValue ShAmt = DAG.getConstant(BitsDiff, MVT::i32); |
| 12109 | |
| 12110 | switch (VT.getSimpleVT().SimpleTy) { |
| 12111 | default: return SDValue(); |
| 12112 | case MVT::v8i32: |
| 12113 | case MVT::v16i16: |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 12114 | if (!Subtarget->hasFp256()) |
| Nadav Rotem | d0f3ef8 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 12115 | return SDValue(); |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 12116 | if (!Subtarget->hasInt256()) { |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12117 | // needs to be split |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 12118 | unsigned NumElems = VT.getVectorNumElements(); |
| Craig Topper | a124f94 | 2011-11-21 01:12:36 +0000 | [diff] [blame] | 12119 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12120 | // Extract the LHS vectors |
| 12121 | SDValue LHS = Op.getOperand(0); |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 12122 | SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl); |
| 12123 | SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl); |
| Craig Topper | a124f94 | 2011-11-21 01:12:36 +0000 | [diff] [blame] | 12124 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12125 | MVT EltVT = VT.getVectorElementType().getSimpleVT(); |
| 12126 | EVT NewVT = MVT::getVectorVT(EltVT, NumElems/2); |
| Craig Topper | a124f94 | 2011-11-21 01:12:36 +0000 | [diff] [blame] | 12127 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12128 | EVT ExtraEltVT = ExtraVT.getVectorElementType(); |
| Craig Topper | b607264 | 2012-05-03 07:26:59 +0000 | [diff] [blame] | 12129 | unsigned ExtraNumElems = ExtraVT.getVectorNumElements(); |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12130 | ExtraVT = EVT::getVectorVT(*DAG.getContext(), ExtraEltVT, |
| 12131 | ExtraNumElems/2); |
| 12132 | SDValue Extra = DAG.getValueType(ExtraVT); |
| Craig Topper | a124f94 | 2011-11-21 01:12:36 +0000 | [diff] [blame] | 12133 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12134 | LHS1 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, Extra); |
| 12135 | LHS2 = DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, Extra); |
| Craig Topper | a124f94 | 2011-11-21 01:12:36 +0000 | [diff] [blame] | 12136 | |
| Dmitri Gribenko | 2de0572 | 2012-09-10 21:26:47 +0000 | [diff] [blame] | 12137 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, LHS1, LHS2); |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12138 | } |
| 12139 | // fall through |
| 12140 | case MVT::v4i32: |
| 12141 | case MVT::v8i16: { |
| Nadav Rotem | b05130e | 2013-03-19 18:38:27 +0000 | [diff] [blame] | 12142 | // (sext (vzext x)) -> (vsext x) |
| 12143 | SDValue Op0 = Op.getOperand(0); |
| 12144 | SDValue Op00 = Op0.getOperand(0); |
| 12145 | SDValue Tmp1; |
| 12146 | // Hopefully, this VECTOR_SHUFFLE is just a VZEXT. |
| 12147 | if (Op0.getOpcode() == ISD::BITCAST && |
| 12148 | Op00.getOpcode() == ISD::VECTOR_SHUFFLE) |
| 12149 | Tmp1 = LowerVectorIntExtend(Op00, DAG); |
| 12150 | if (Tmp1.getNode()) { |
| 12151 | SDValue Tmp1Op0 = Tmp1.getOperand(0); |
| 12152 | assert(Tmp1Op0.getOpcode() == X86ISD::VZEXT && |
| 12153 | "This optimization is invalid without a VZEXT."); |
| 12154 | return DAG.getNode(X86ISD::VSEXT, dl, VT, Tmp1Op0.getOperand(0)); |
| 12155 | } |
| 12156 | |
| 12157 | // If the above didn't work, then just use Shift-Left + Shift-Right. |
| 12158 | Tmp1 = getTargetVShiftNode(X86ISD::VSHLI, dl, VT, Op0, ShAmt, DAG); |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12159 | return getTargetVShiftNode(X86ISD::VSRAI, dl, VT, Tmp1, ShAmt, DAG); |
| Nadav Rotem | d0f3ef8 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 12160 | } |
| Nadav Rotem | d0f3ef8 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 12161 | } |
| Nadav Rotem | d0f3ef8 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 12162 | } |
| 12163 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12164 | static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget, |
| 12165 | SelectionDAG &DAG) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12166 | SDLoc dl(Op); |
| Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 12167 | AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>( |
| 12168 | cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()); |
| 12169 | SynchronizationScope FenceScope = static_cast<SynchronizationScope>( |
| 12170 | cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue()); |
| 12171 | |
| 12172 | // The only fence that needs an instruction is a sequentially-consistent |
| 12173 | // cross-thread fence. |
| 12174 | if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) { |
| 12175 | // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for |
| 12176 | // no-sse2). There isn't any reason to disable it if the target processor |
| 12177 | // supports it. |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 12178 | if (Subtarget->hasSSE2() || Subtarget->is64Bit()) |
| Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 12179 | return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0)); |
| 12180 | |
| 12181 | SDValue Chain = Op.getOperand(0); |
| 12182 | SDValue Zero = DAG.getConstant(0, MVT::i32); |
| 12183 | SDValue Ops[] = { |
| 12184 | DAG.getRegister(X86::ESP, MVT::i32), // Base |
| 12185 | DAG.getTargetConstant(1, MVT::i8), // Scale |
| 12186 | DAG.getRegister(0, MVT::i32), // Index |
| 12187 | DAG.getTargetConstant(0, MVT::i32), // Disp |
| 12188 | DAG.getRegister(0, MVT::i32), // Segment. |
| 12189 | Zero, |
| 12190 | Chain |
| 12191 | }; |
| Michael Liao | 2a8bea7 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 12192 | SDNode *Res = DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops); |
| Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 12193 | return SDValue(Res, 0); |
| 12194 | } |
| 12195 | |
| 12196 | // MEMBARRIER is a compiler barrier; it codegens to a no-op. |
| 12197 | return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0)); |
| 12198 | } |
| 12199 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12200 | static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget, |
| 12201 | SelectionDAG &DAG) { |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 12202 | EVT T = Op.getValueType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12203 | SDLoc DL(Op); |
| Andrew Lenharth | a76e2f0 | 2008-03-04 21:13:33 +0000 | [diff] [blame] | 12204 | unsigned Reg = 0; |
| 12205 | unsigned size = 0; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12206 | switch(T.getSimpleVT().SimpleTy) { |
| Craig Topper | abb94d0 | 2012-02-05 03:43:23 +0000 | [diff] [blame] | 12207 | default: llvm_unreachable("Invalid value type!"); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12208 | case MVT::i8: Reg = X86::AL; size = 1; break; |
| 12209 | case MVT::i16: Reg = X86::AX; size = 2; break; |
| 12210 | case MVT::i32: Reg = X86::EAX; size = 4; break; |
| 12211 | case MVT::i64: |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12212 | assert(Subtarget->is64Bit() && "Node not type legal!"); |
| 12213 | Reg = X86::RAX; size = 8; |
| Andrew Lenharth | d19189e | 2008-03-05 01:15:49 +0000 | [diff] [blame] | 12214 | break; |
| Bill Wendling | 61edeb5 | 2008-12-02 01:06:39 +0000 | [diff] [blame] | 12215 | } |
| Chris Lattner | 93c4a5b | 2010-09-21 23:59:42 +0000 | [diff] [blame] | 12216 | SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg, |
| Dale Johannesen | d18a462 | 2008-09-11 03:12:59 +0000 | [diff] [blame] | 12217 | Op.getOperand(2), SDValue()); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 12218 | SDValue Ops[] = { cpIn.getValue(0), |
| Evan Cheng | 8a186ae | 2008-09-24 23:26:36 +0000 | [diff] [blame] | 12219 | Op.getOperand(1), |
| 12220 | Op.getOperand(3), |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12221 | DAG.getTargetConstant(size, MVT::i8), |
| Evan Cheng | 8a186ae | 2008-09-24 23:26:36 +0000 | [diff] [blame] | 12222 | cpIn.getValue(1) }; |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 12223 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue); |
| Chris Lattner | 93c4a5b | 2010-09-21 23:59:42 +0000 | [diff] [blame] | 12224 | MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand(); |
| 12225 | SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys, |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 12226 | Ops, array_lengthof(Ops), T, MMO); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12227 | SDValue cpOut = |
| Chris Lattner | 93c4a5b | 2010-09-21 23:59:42 +0000 | [diff] [blame] | 12228 | DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1)); |
| Andrew Lenharth | 26ed869 | 2008-03-01 21:52:34 +0000 | [diff] [blame] | 12229 | return cpOut; |
| 12230 | } |
| 12231 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12232 | static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget, |
| 12233 | SelectionDAG &DAG) { |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12234 | assert(Subtarget->is64Bit() && "Result not type legalized?"); |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 12235 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12236 | SDValue TheChain = Op.getOperand(0); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12237 | SDLoc dl(Op); |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 12238 | SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12239 | SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1)); |
| 12240 | SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64, |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12241 | rax.getValue(2)); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12242 | SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx, |
| 12243 | DAG.getConstant(32, MVT::i8)); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12244 | SDValue Ops[] = { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12245 | DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp), |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12246 | rdx.getValue(1) |
| 12247 | }; |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 12248 | return DAG.getMergeValues(Ops, array_lengthof(Ops), dl); |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 12249 | } |
| 12250 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12251 | SDValue X86TargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const { |
| Dale Johannesen | 7d07b48 | 2010-05-21 00:52:33 +0000 | [diff] [blame] | 12252 | EVT SrcVT = Op.getOperand(0).getValueType(); |
| 12253 | EVT DstVT = Op.getValueType(); |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 12254 | assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() && |
| Chris Lattner | 2a786eb | 2010-12-19 20:19:20 +0000 | [diff] [blame] | 12255 | Subtarget->hasMMX() && "Unexpected custom BITCAST"); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 12256 | assert((DstVT == MVT::i64 || |
| Dale Johannesen | 7d07b48 | 2010-05-21 00:52:33 +0000 | [diff] [blame] | 12257 | (DstVT.isVector() && DstVT.getSizeInBits()==64)) && |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 12258 | "Unexpected custom BITCAST"); |
| Dale Johannesen | 7d07b48 | 2010-05-21 00:52:33 +0000 | [diff] [blame] | 12259 | // i64 <=> MMX conversions are Legal. |
| 12260 | if (SrcVT==MVT::i64 && DstVT.isVector()) |
| 12261 | return Op; |
| 12262 | if (DstVT==MVT::i64 && SrcVT.isVector()) |
| 12263 | return Op; |
| Dale Johannesen | e39859a | 2010-05-21 18:40:15 +0000 | [diff] [blame] | 12264 | // MMX <=> MMX conversions are Legal. |
| 12265 | if (SrcVT.isVector() && DstVT.isVector()) |
| 12266 | return Op; |
| Dale Johannesen | 7d07b48 | 2010-05-21 00:52:33 +0000 | [diff] [blame] | 12267 | // All other conversions need to be expanded. |
| 12268 | return SDValue(); |
| 12269 | } |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12270 | |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12271 | static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) { |
| Dale Johannesen | 71d1bf5 | 2008-09-29 22:25:26 +0000 | [diff] [blame] | 12272 | SDNode *Node = Op.getNode(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12273 | SDLoc dl(Node); |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 12274 | EVT T = Node->getValueType(0); |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 12275 | SDValue negOp = DAG.getNode(ISD::SUB, dl, T, |
| Evan Cheng | 242b38b | 2009-02-23 09:03:22 +0000 | [diff] [blame] | 12276 | DAG.getConstant(0, T), Node->getOperand(2)); |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 12277 | return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl, |
| Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 12278 | cast<AtomicSDNode>(Node)->getMemoryVT(), |
| Dale Johannesen | 71d1bf5 | 2008-09-29 22:25:26 +0000 | [diff] [blame] | 12279 | Node->getOperand(0), |
| 12280 | Node->getOperand(1), negOp, |
| 12281 | cast<AtomicSDNode>(Node)->getSrcValue(), |
| Eli Friedman | 55ba816 | 2011-07-29 03:05:32 +0000 | [diff] [blame] | 12282 | cast<AtomicSDNode>(Node)->getAlignment(), |
| 12283 | cast<AtomicSDNode>(Node)->getOrdering(), |
| 12284 | cast<AtomicSDNode>(Node)->getSynchScope()); |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 12285 | } |
| 12286 | |
| Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 12287 | static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) { |
| 12288 | SDNode *Node = Op.getNode(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12289 | SDLoc dl(Node); |
| Eli Friedman | f8f90f0 | 2011-08-24 22:33:28 +0000 | [diff] [blame] | 12290 | EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT(); |
| Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 12291 | |
| 12292 | // Convert seq_cst store -> xchg |
| Eli Friedman | f8f90f0 | 2011-08-24 22:33:28 +0000 | [diff] [blame] | 12293 | // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b) |
| 12294 | // FIXME: On 32-bit, store -> fist or movq would be more efficient |
| 12295 | // (The only way to get a 16-byte store is cmpxchg16b) |
| 12296 | // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment. |
| 12297 | if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent || |
| 12298 | !DAG.getTargetLoweringInfo().isTypeLegal(VT)) { |
| Eli Friedman | 4317fe1 | 2011-08-24 21:17:30 +0000 | [diff] [blame] | 12299 | SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl, |
| 12300 | cast<AtomicSDNode>(Node)->getMemoryVT(), |
| 12301 | Node->getOperand(0), |
| 12302 | Node->getOperand(1), Node->getOperand(2), |
| Eli Friedman | f8f90f0 | 2011-08-24 22:33:28 +0000 | [diff] [blame] | 12303 | cast<AtomicSDNode>(Node)->getMemOperand(), |
| Eli Friedman | 4317fe1 | 2011-08-24 21:17:30 +0000 | [diff] [blame] | 12304 | cast<AtomicSDNode>(Node)->getOrdering(), |
| 12305 | cast<AtomicSDNode>(Node)->getSynchScope()); |
| Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 12306 | return Swap.getValue(1); |
| 12307 | } |
| 12308 | // Other atomic stores have a simple pattern. |
| 12309 | return Op; |
| 12310 | } |
| 12311 | |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12312 | static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) { |
| 12313 | EVT VT = Op.getNode()->getValueType(0); |
| 12314 | |
| 12315 | // Let legalize expand this if it isn't a legal type yet. |
| 12316 | if (!DAG.getTargetLoweringInfo().isTypeLegal(VT)) |
| 12317 | return SDValue(); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 12318 | |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12319 | SDVTList VTs = DAG.getVTList(VT, MVT::i32); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 12320 | |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12321 | unsigned Opc; |
| 12322 | bool ExtraOp = false; |
| 12323 | switch (Op.getOpcode()) { |
| Craig Topper | abb94d0 | 2012-02-05 03:43:23 +0000 | [diff] [blame] | 12324 | default: llvm_unreachable("Invalid code"); |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12325 | case ISD::ADDC: Opc = X86ISD::ADD; break; |
| 12326 | case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break; |
| 12327 | case ISD::SUBC: Opc = X86ISD::SUB; break; |
| 12328 | case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break; |
| 12329 | } |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 12330 | |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12331 | if (!ExtraOp) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12332 | return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12333 | Op.getOperand(1)); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12334 | return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0), |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12335 | Op.getOperand(1), Op.getOperand(2)); |
| 12336 | } |
| 12337 | |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12338 | SDValue X86TargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const { |
| Evan Cheng | a66f40a | 2013-01-30 22:56:35 +0000 | [diff] [blame] | 12339 | assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit()); |
| Eric Christopher | e187e25 | 2013-01-31 00:50:48 +0000 | [diff] [blame] | 12340 | |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12341 | // For MacOSX, we want to call an alternative entry point: __sincos_stret, |
| Evan Cheng | 3a6b7d3 | 2013-04-10 01:26:07 +0000 | [diff] [blame] | 12342 | // which returns the values as { float, float } (in XMM0) or |
| 12343 | // { double, double } (which is returned in XMM0, XMM1). |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12344 | SDLoc dl(Op); |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12345 | SDValue Arg = Op.getOperand(0); |
| 12346 | EVT ArgVT = Arg.getValueType(); |
| 12347 | Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); |
| Eric Christopher | e187e25 | 2013-01-31 00:50:48 +0000 | [diff] [blame] | 12348 | |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12349 | ArgListTy Args; |
| 12350 | ArgListEntry Entry; |
| Eric Christopher | e187e25 | 2013-01-31 00:50:48 +0000 | [diff] [blame] | 12351 | |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12352 | Entry.Node = Arg; |
| 12353 | Entry.Ty = ArgTy; |
| 12354 | Entry.isSExt = false; |
| 12355 | Entry.isZExt = false; |
| 12356 | Args.push_back(Entry); |
| Evan Cheng | a66f40a | 2013-01-30 22:56:35 +0000 | [diff] [blame] | 12357 | |
| Evan Cheng | 3a6b7d3 | 2013-04-10 01:26:07 +0000 | [diff] [blame] | 12358 | bool isF64 = ArgVT == MVT::f64; |
| Evan Cheng | a66f40a | 2013-01-30 22:56:35 +0000 | [diff] [blame] | 12359 | // Only optimize x86_64 for now. i386 is a bit messy. For f32, |
| 12360 | // the small struct {f32, f32} is returned in (eax, edx). For f64, |
| 12361 | // the results are returned via SRet in memory. |
| Evan Cheng | 3a6b7d3 | 2013-04-10 01:26:07 +0000 | [diff] [blame] | 12362 | const char *LibcallName = isF64 ? "__sincos_stret" : "__sincosf_stret"; |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12363 | SDValue Callee = DAG.getExternalSymbol(LibcallName, getPointerTy()); |
| Evan Cheng | a66f40a | 2013-01-30 22:56:35 +0000 | [diff] [blame] | 12364 | |
| Evan Cheng | 3a6b7d3 | 2013-04-10 01:26:07 +0000 | [diff] [blame] | 12365 | Type *RetTy = isF64 |
| 12366 | ? (Type*)StructType::get(ArgTy, ArgTy, NULL) |
| 12367 | : (Type*)VectorType::get(ArgTy, 4); |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12368 | TargetLowering:: |
| Evan Cheng | a66f40a | 2013-01-30 22:56:35 +0000 | [diff] [blame] | 12369 | CallLoweringInfo CLI(DAG.getEntryNode(), RetTy, |
| 12370 | false, false, false, false, 0, |
| 12371 | CallingConv::C, /*isTaillCall=*/false, |
| 12372 | /*doesNotRet=*/false, /*isReturnValueUsed*/true, |
| 12373 | Callee, Args, DAG, dl); |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12374 | std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI); |
| Evan Cheng | 3a6b7d3 | 2013-04-10 01:26:07 +0000 | [diff] [blame] | 12375 | |
| 12376 | if (isF64) |
| 12377 | // Returned in xmm0 and xmm1. |
| 12378 | return CallResult.first; |
| 12379 | |
| 12380 | // Returned in bits 0:31 and 32:64 xmm0. |
| 12381 | SDValue SinVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT, |
| 12382 | CallResult.first, DAG.getIntPtrConstant(0)); |
| 12383 | SDValue CosVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT, |
| 12384 | CallResult.first, DAG.getIntPtrConstant(1)); |
| 12385 | SDVTList Tys = DAG.getVTList(ArgVT, ArgVT); |
| 12386 | return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, SinVal, CosVal); |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12387 | } |
| 12388 | |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12389 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 12390 | /// |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 12391 | SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12392 | switch (Op.getOpcode()) { |
| Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 12393 | default: llvm_unreachable("Should not custom lower this!"); |
| Nadav Rotem | d0f3ef8 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 12394 | case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op,DAG); |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12395 | case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG); |
| 12396 | case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op, Subtarget, DAG); |
| Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 12397 | case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG); |
| Eli Friedman | 327236c | 2011-08-24 20:50:09 +0000 | [diff] [blame] | 12398 | case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op,DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12399 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); |
| Mon P Wang | eb38ebf | 2010-01-24 00:05:03 +0000 | [diff] [blame] | 12400 | case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12401 | case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG); |
| 12402 | case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); |
| 12403 | case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12404 | case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG); |
| 12405 | case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12406 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG); |
| 12407 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); |
| 12408 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); |
| Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 12409 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); |
| Bill Wendling | 056292f | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 12410 | case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); |
| Dan Gohman | f705adb | 2009-10-30 01:28:02 +0000 | [diff] [blame] | 12411 | case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12412 | case ISD::SHL_PARTS: |
| 12413 | case ISD::SRA_PARTS: |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 12414 | case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12415 | case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); |
| Dale Johannesen | 1c15bf5 | 2008-10-21 20:50:01 +0000 | [diff] [blame] | 12416 | case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG); |
| Craig Topper | d713c0f | 2013-01-20 21:34:37 +0000 | [diff] [blame] | 12417 | case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG); |
| Nadav Rotem | 0509db2 | 2012-12-28 05:45:24 +0000 | [diff] [blame] | 12418 | case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG); |
| 12419 | case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG); |
| 12420 | case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12421 | case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 12422 | case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG); |
| Craig Topper | b84b423 | 2013-01-21 06:13:28 +0000 | [diff] [blame] | 12423 | case ISD::FP_EXTEND: return LowerFP_EXTEND(Op, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12424 | case ISD::FABS: return LowerFABS(Op, DAG); |
| 12425 | case ISD::FNEG: return LowerFNEG(Op, DAG); |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 12426 | case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); |
| Stuart Hastings | 4fd0dee | 2011-06-01 04:39:42 +0000 | [diff] [blame] | 12427 | case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG); |
| Evan Cheng | e5f6204 | 2007-09-29 00:00:36 +0000 | [diff] [blame] | 12428 | case ISD::SETCC: return LowerSETCC(Op, DAG); |
| 12429 | case ISD::SELECT: return LowerSELECT(Op, DAG); |
| 12430 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12431 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12432 | case ISD::VASTART: return LowerVASTART(Op, DAG); |
| Dan Gohman | 9018e83 | 2008-05-10 01:26:14 +0000 | [diff] [blame] | 12433 | case ISD::VAARG: return LowerVAARG(Op, DAG); |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12434 | case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12435 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 12436 | case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG); |
| Nate Begeman | bcc5f36 | 2007-01-29 22:58:52 +0000 | [diff] [blame] | 12437 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); |
| 12438 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 12439 | case ISD::FRAME_TO_ARGS_OFFSET: |
| 12440 | return LowerFRAME_TO_ARGS_OFFSET(Op, DAG); |
| Anton Korobeynikov | 57fc00d | 2007-04-17 09:20:00 +0000 | [diff] [blame] | 12441 | case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 12442 | case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 12443 | case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG); |
| 12444 | case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG); |
| Duncan Sands | 4a544a7 | 2011-09-06 13:37:06 +0000 | [diff] [blame] | 12445 | case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG); |
| 12446 | case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG); |
| Dan Gohman | 1a02486 | 2008-01-31 00:41:03 +0000 | [diff] [blame] | 12447 | case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); |
| Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 12448 | case ISD::CTLZ: return LowerCTLZ(Op, DAG); |
| Chandler Carruth | acc068e | 2011-12-24 10:55:54 +0000 | [diff] [blame] | 12449 | case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ_ZERO_UNDEF(Op, DAG); |
| Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 12450 | case ISD::CTTZ: return LowerCTTZ(Op, DAG); |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12451 | case ISD::MUL: return LowerMUL(Op, Subtarget, DAG); |
| Nadav Rotem | 4301222 | 2011-05-11 08:12:09 +0000 | [diff] [blame] | 12452 | case ISD::SRA: |
| 12453 | case ISD::SRL: |
| 12454 | case ISD::SHL: return LowerShift(Op, DAG); |
| Bill Wendling | 74c3765 | 2008-12-09 22:08:41 +0000 | [diff] [blame] | 12455 | case ISD::SADDO: |
| 12456 | case ISD::UADDO: |
| 12457 | case ISD::SSUBO: |
| 12458 | case ISD::USUBO: |
| 12459 | case ISD::SMULO: |
| 12460 | case ISD::UMULO: return LowerXALUO(Op, DAG); |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 12461 | case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG); |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 12462 | case ISD::BITCAST: return LowerBITCAST(Op, DAG); |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12463 | case ISD::ADDC: |
| 12464 | case ISD::ADDE: |
| 12465 | case ISD::SUBC: |
| 12466 | case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG); |
| Craig Topper | 13894fa | 2011-08-24 06:14:18 +0000 | [diff] [blame] | 12467 | case ISD::ADD: return LowerADD(Op, DAG); |
| 12468 | case ISD::SUB: return LowerSUB(Op, DAG); |
| Nadav Rotem | 13f8cf5 | 2013-01-09 05:14:33 +0000 | [diff] [blame] | 12469 | case ISD::SDIV: return LowerSDIV(Op, DAG); |
| Evan Cheng | 8688a58 | 2013-01-29 02:32:37 +0000 | [diff] [blame] | 12470 | case ISD::FSINCOS: return LowerFSINCOS(Op, DAG); |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12471 | } |
| Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 12472 | } |
| 12473 | |
| Eli Friedman | f8f90f0 | 2011-08-24 22:33:28 +0000 | [diff] [blame] | 12474 | static void ReplaceATOMIC_LOAD(SDNode *Node, |
| 12475 | SmallVectorImpl<SDValue> &Results, |
| 12476 | SelectionDAG &DAG) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12477 | SDLoc dl(Node); |
| Eli Friedman | f8f90f0 | 2011-08-24 22:33:28 +0000 | [diff] [blame] | 12478 | EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT(); |
| 12479 | |
| 12480 | // Convert wide load -> cmpxchg8b/cmpxchg16b |
| 12481 | // FIXME: On 32-bit, load -> fild or movq would be more efficient |
| 12482 | // (The only way to get a 16-byte load is cmpxchg16b) |
| 12483 | // FIXME: 16-byte ATOMIC_CMP_SWAP isn't actually hooked up at the moment. |
| Benjamin Kramer | 2753ae3 | 2011-08-27 17:36:14 +0000 | [diff] [blame] | 12484 | SDValue Zero = DAG.getConstant(0, VT); |
| 12485 | SDValue Swap = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, dl, VT, |
| Eli Friedman | f8f90f0 | 2011-08-24 22:33:28 +0000 | [diff] [blame] | 12486 | Node->getOperand(0), |
| 12487 | Node->getOperand(1), Zero, Zero, |
| 12488 | cast<AtomicSDNode>(Node)->getMemOperand(), |
| 12489 | cast<AtomicSDNode>(Node)->getOrdering(), |
| 12490 | cast<AtomicSDNode>(Node)->getSynchScope()); |
| 12491 | Results.push_back(Swap.getValue(0)); |
| 12492 | Results.push_back(Swap.getValue(1)); |
| 12493 | } |
| 12494 | |
| Craig Topper | c087870 | 2012-08-17 06:55:11 +0000 | [diff] [blame] | 12495 | static void |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12496 | ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results, |
| Craig Topper | c087870 | 2012-08-17 06:55:11 +0000 | [diff] [blame] | 12497 | SelectionDAG &DAG, unsigned NewOp) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12498 | SDLoc dl(Node); |
| Duncan Sands | 17001ce | 2011-10-18 12:44:00 +0000 | [diff] [blame] | 12499 | assert (Node->getValueType(0) == MVT::i64 && |
| 12500 | "Only know how to expand i64 atomics"); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12501 | |
| 12502 | SDValue Chain = Node->getOperand(0); |
| 12503 | SDValue In1 = Node->getOperand(1); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12504 | SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12505 | Node->getOperand(2), DAG.getIntPtrConstant(0)); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12506 | SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12507 | Node->getOperand(2), DAG.getIntPtrConstant(1)); |
| Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 12508 | SDValue Ops[] = { Chain, In1, In2L, In2H }; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12509 | SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other); |
| Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 12510 | SDValue Result = |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 12511 | DAG.getMemIntrinsicNode(NewOp, dl, Tys, Ops, array_lengthof(Ops), MVT::i64, |
| Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 12512 | cast<MemSDNode>(Node)->getMemOperand()); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12513 | SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)}; |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12514 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2)); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12515 | Results.push_back(Result.getValue(2)); |
| 12516 | } |
| 12517 | |
| Duncan Sands | 126d907 | 2008-07-04 11:47:58 +0000 | [diff] [blame] | 12518 | /// ReplaceNodeResults - Replace a node with an illegal result type |
| 12519 | /// with a new node built out of custom code. |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12520 | void X86TargetLowering::ReplaceNodeResults(SDNode *N, |
| 12521 | SmallVectorImpl<SDValue>&Results, |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 12522 | SelectionDAG &DAG) const { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 12523 | SDLoc dl(N); |
| Nadav Rotem | 0a1e914 | 2012-12-14 21:20:37 +0000 | [diff] [blame] | 12524 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 12525 | switch (N->getOpcode()) { |
| Duncan Sands | ed294c4 | 2008-10-20 15:56:33 +0000 | [diff] [blame] | 12526 | default: |
| Craig Topper | abb94d0 | 2012-02-05 03:43:23 +0000 | [diff] [blame] | 12527 | llvm_unreachable("Do not know how to custom type legalize this operation!"); |
| Nadav Rotem | d0f3ef8 | 2011-07-14 11:11:14 +0000 | [diff] [blame] | 12528 | case ISD::SIGN_EXTEND_INREG: |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12529 | case ISD::ADDC: |
| 12530 | case ISD::ADDE: |
| 12531 | case ISD::SUBC: |
| 12532 | case ISD::SUBE: |
| 12533 | // We don't want to expand or promote these. |
| 12534 | return; |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 12535 | case ISD::FP_TO_SINT: |
| 12536 | case ISD::FP_TO_UINT: { |
| 12537 | bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT; |
| 12538 | |
| 12539 | if (!IsSigned && !isIntegerTypeFTOL(SDValue(N, 0).getValueType())) |
| 12540 | return; |
| 12541 | |
| Eli Friedman | 948e95a | 2009-05-23 09:59:16 +0000 | [diff] [blame] | 12542 | std::pair<SDValue,SDValue> Vals = |
| NAKAMURA Takumi | 9a68fdc | 2012-02-25 03:37:25 +0000 | [diff] [blame] | 12543 | FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12544 | SDValue FIST = Vals.first, StackSlot = Vals.second; |
| 12545 | if (FIST.getNode() != 0) { |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 12546 | EVT VT = N->getValueType(0); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12547 | // Return a load from the stack slot. |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 12548 | if (StackSlot.getNode() != 0) |
| 12549 | Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, |
| 12550 | MachinePointerInfo(), |
| 12551 | false, false, false, 0)); |
| 12552 | else |
| 12553 | Results.push_back(FIST); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12554 | } |
| 12555 | return; |
| 12556 | } |
| Michael Liao | 991b6a2 | 2012-10-24 04:09:32 +0000 | [diff] [blame] | 12557 | case ISD::UINT_TO_FP: { |
| Michael Liao | 6f8c685 | 2013-03-14 06:57:42 +0000 | [diff] [blame] | 12558 | assert(Subtarget->hasSSE2() && "Requires at least SSE2!"); |
| 12559 | if (N->getOperand(0).getValueType() != MVT::v2i32 || |
| Michael Liao | 991b6a2 | 2012-10-24 04:09:32 +0000 | [diff] [blame] | 12560 | N->getValueType(0) != MVT::v2f32) |
| 12561 | return; |
| 12562 | SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64, |
| 12563 | N->getOperand(0)); |
| 12564 | SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), |
| 12565 | MVT::f64); |
| 12566 | SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias); |
| 12567 | SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn, |
| 12568 | DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, VBias)); |
| 12569 | Or = DAG.getNode(ISD::BITCAST, dl, MVT::v2f64, Or); |
| 12570 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias); |
| 12571 | Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub)); |
| 12572 | return; |
| 12573 | } |
| Michael Liao | 44c2d61 | 2012-10-10 16:53:28 +0000 | [diff] [blame] | 12574 | case ISD::FP_ROUND: { |
| Nadav Rotem | 0a1e914 | 2012-12-14 21:20:37 +0000 | [diff] [blame] | 12575 | if (!TLI.isTypeLegal(N->getOperand(0).getValueType())) |
| 12576 | return; |
| Michael Liao | 44c2d61 | 2012-10-10 16:53:28 +0000 | [diff] [blame] | 12577 | SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0)); |
| 12578 | Results.push_back(V); |
| 12579 | return; |
| 12580 | } |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12581 | case ISD::READCYCLECOUNTER: { |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 12582 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12583 | SDValue TheChain = N->getOperand(0); |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 12584 | SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12585 | SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32, |
| Dale Johannesen | dd64c41 | 2009-02-04 00:33:20 +0000 | [diff] [blame] | 12586 | rd.getValue(1)); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12587 | SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32, |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12588 | eax.getValue(2)); |
| 12589 | // Use a buildpair to merge the two 32-bit values into a 64-bit one. |
| 12590 | SDValue Ops[] = { eax, edx }; |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 12591 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, |
| 12592 | array_lengthof(Ops))); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12593 | Results.push_back(edx.getValue(1)); |
| 12594 | return; |
| 12595 | } |
| Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 12596 | case ISD::ATOMIC_CMP_SWAP: { |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 12597 | EVT T = N->getValueType(0); |
| Benjamin Kramer | 2753ae3 | 2011-08-27 17:36:14 +0000 | [diff] [blame] | 12598 | assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair"); |
| Eli Friedman | 43f51ae | 2011-08-26 21:21:21 +0000 | [diff] [blame] | 12599 | bool Regs64bit = T == MVT::i128; |
| 12600 | EVT HalfT = Regs64bit ? MVT::i64 : MVT::i32; |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12601 | SDValue cpInL, cpInH; |
| Eli Friedman | 43f51ae | 2011-08-26 21:21:21 +0000 | [diff] [blame] | 12602 | cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2), |
| 12603 | DAG.getConstant(0, HalfT)); |
| 12604 | cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2), |
| 12605 | DAG.getConstant(1, HalfT)); |
| 12606 | cpInL = DAG.getCopyToReg(N->getOperand(0), dl, |
| 12607 | Regs64bit ? X86::RAX : X86::EAX, |
| 12608 | cpInL, SDValue()); |
| 12609 | cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, |
| 12610 | Regs64bit ? X86::RDX : X86::EDX, |
| 12611 | cpInH, cpInL.getValue(1)); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12612 | SDValue swapInL, swapInH; |
| Eli Friedman | 43f51ae | 2011-08-26 21:21:21 +0000 | [diff] [blame] | 12613 | swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3), |
| 12614 | DAG.getConstant(0, HalfT)); |
| 12615 | swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3), |
| 12616 | DAG.getConstant(1, HalfT)); |
| 12617 | swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, |
| 12618 | Regs64bit ? X86::RBX : X86::EBX, |
| 12619 | swapInL, cpInH.getValue(1)); |
| 12620 | swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, |
| Chad Rosier | a20e1e7 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 12621 | Regs64bit ? X86::RCX : X86::ECX, |
| Eli Friedman | 43f51ae | 2011-08-26 21:21:21 +0000 | [diff] [blame] | 12622 | swapInH, swapInL.getValue(1)); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12623 | SDValue Ops[] = { swapInH.getValue(0), |
| 12624 | N->getOperand(1), |
| 12625 | swapInH.getValue(1) }; |
| Chris Lattner | f1b4eaf | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 12626 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue); |
| Andrew Trick | 1a2cf3b | 2010-10-11 19:02:04 +0000 | [diff] [blame] | 12627 | MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand(); |
| Eli Friedman | 43f51ae | 2011-08-26 21:21:21 +0000 | [diff] [blame] | 12628 | unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG : |
| 12629 | X86ISD::LCMPXCHG8_DAG; |
| 12630 | SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys, |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 12631 | Ops, array_lengthof(Ops), T, MMO); |
| Eli Friedman | 43f51ae | 2011-08-26 21:21:21 +0000 | [diff] [blame] | 12632 | SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, |
| 12633 | Regs64bit ? X86::RAX : X86::EAX, |
| 12634 | HalfT, Result.getValue(1)); |
| 12635 | SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, |
| 12636 | Regs64bit ? X86::RDX : X86::EDX, |
| 12637 | HalfT, cpOutL.getValue(2)); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12638 | SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)}; |
| Eli Friedman | 43f51ae | 2011-08-26 21:21:21 +0000 | [diff] [blame] | 12639 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF, 2)); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12640 | Results.push_back(cpOutH.getValue(1)); |
| 12641 | return; |
| 12642 | } |
| Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 12643 | case ISD::ATOMIC_LOAD_ADD: |
| Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 12644 | case ISD::ATOMIC_LOAD_AND: |
| Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 12645 | case ISD::ATOMIC_LOAD_NAND: |
| Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 12646 | case ISD::ATOMIC_LOAD_OR: |
| Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 12647 | case ISD::ATOMIC_LOAD_SUB: |
| Dan Gohman | 0b1d4a7 | 2008-12-23 21:37:04 +0000 | [diff] [blame] | 12648 | case ISD::ATOMIC_LOAD_XOR: |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 12649 | case ISD::ATOMIC_LOAD_MAX: |
| 12650 | case ISD::ATOMIC_LOAD_MIN: |
| 12651 | case ISD::ATOMIC_LOAD_UMAX: |
| 12652 | case ISD::ATOMIC_LOAD_UMIN: |
| Craig Topper | c087870 | 2012-08-17 06:55:11 +0000 | [diff] [blame] | 12653 | case ISD::ATOMIC_SWAP: { |
| 12654 | unsigned Opc; |
| 12655 | switch (N->getOpcode()) { |
| 12656 | default: llvm_unreachable("Unexpected opcode"); |
| 12657 | case ISD::ATOMIC_LOAD_ADD: |
| 12658 | Opc = X86ISD::ATOMADD64_DAG; |
| 12659 | break; |
| 12660 | case ISD::ATOMIC_LOAD_AND: |
| 12661 | Opc = X86ISD::ATOMAND64_DAG; |
| 12662 | break; |
| 12663 | case ISD::ATOMIC_LOAD_NAND: |
| 12664 | Opc = X86ISD::ATOMNAND64_DAG; |
| 12665 | break; |
| 12666 | case ISD::ATOMIC_LOAD_OR: |
| 12667 | Opc = X86ISD::ATOMOR64_DAG; |
| 12668 | break; |
| 12669 | case ISD::ATOMIC_LOAD_SUB: |
| 12670 | Opc = X86ISD::ATOMSUB64_DAG; |
| 12671 | break; |
| 12672 | case ISD::ATOMIC_LOAD_XOR: |
| 12673 | Opc = X86ISD::ATOMXOR64_DAG; |
| 12674 | break; |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 12675 | case ISD::ATOMIC_LOAD_MAX: |
| 12676 | Opc = X86ISD::ATOMMAX64_DAG; |
| 12677 | break; |
| 12678 | case ISD::ATOMIC_LOAD_MIN: |
| 12679 | Opc = X86ISD::ATOMMIN64_DAG; |
| 12680 | break; |
| 12681 | case ISD::ATOMIC_LOAD_UMAX: |
| 12682 | Opc = X86ISD::ATOMUMAX64_DAG; |
| 12683 | break; |
| 12684 | case ISD::ATOMIC_LOAD_UMIN: |
| 12685 | Opc = X86ISD::ATOMUMIN64_DAG; |
| 12686 | break; |
| Craig Topper | c087870 | 2012-08-17 06:55:11 +0000 | [diff] [blame] | 12687 | case ISD::ATOMIC_SWAP: |
| 12688 | Opc = X86ISD::ATOMSWAP64_DAG; |
| 12689 | break; |
| 12690 | } |
| 12691 | ReplaceATOMIC_BINARY_64(N, Results, DAG, Opc); |
| Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 12692 | return; |
| Craig Topper | c087870 | 2012-08-17 06:55:11 +0000 | [diff] [blame] | 12693 | } |
| Eli Friedman | f8f90f0 | 2011-08-24 22:33:28 +0000 | [diff] [blame] | 12694 | case ISD::ATOMIC_LOAD: |
| 12695 | ReplaceATOMIC_LOAD(N, Results, DAG); |
| Chris Lattner | 27a6c73 | 2007-11-24 07:07:01 +0000 | [diff] [blame] | 12696 | } |
| Evan Cheng | 0db9fe6 | 2006-04-25 20:13:52 +0000 | [diff] [blame] | 12697 | } |
| 12698 | |
| Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 12699 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 12700 | switch (Opcode) { |
| 12701 | default: return NULL; |
| Evan Cheng | 18efe26 | 2007-12-14 02:13:44 +0000 | [diff] [blame] | 12702 | case X86ISD::BSF: return "X86ISD::BSF"; |
| 12703 | case X86ISD::BSR: return "X86ISD::BSR"; |
| Evan Cheng | e341316 | 2006-01-09 18:33:28 +0000 | [diff] [blame] | 12704 | case X86ISD::SHLD: return "X86ISD::SHLD"; |
| 12705 | case X86ISD::SHRD: return "X86ISD::SHRD"; |
| Evan Cheng | ef6ffb1 | 2006-01-31 03:14:29 +0000 | [diff] [blame] | 12706 | case X86ISD::FAND: return "X86ISD::FAND"; |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 12707 | case X86ISD::FOR: return "X86ISD::FOR"; |
| Evan Cheng | 223547a | 2006-01-31 22:28:30 +0000 | [diff] [blame] | 12708 | case X86ISD::FXOR: return "X86ISD::FXOR"; |
| Evan Cheng | 68c47cb | 2007-01-05 07:55:56 +0000 | [diff] [blame] | 12709 | case X86ISD::FSRL: return "X86ISD::FSRL"; |
| Evan Cheng | a3195e8 | 2006-01-12 22:54:21 +0000 | [diff] [blame] | 12710 | case X86ISD::FILD: return "X86ISD::FILD"; |
| Evan Cheng | e3de85b | 2006-02-04 02:20:30 +0000 | [diff] [blame] | 12711 | case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG"; |
| Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 12712 | case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; |
| 12713 | case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; |
| 12714 | case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM"; |
| Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 12715 | case X86ISD::FLD: return "X86ISD::FLD"; |
| Evan Cheng | d90eb7f | 2006-01-05 00:27:02 +0000 | [diff] [blame] | 12716 | case X86ISD::FST: return "X86ISD::FST"; |
| Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 12717 | case X86ISD::CALL: return "X86ISD::CALL"; |
| Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 12718 | case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; |
| Dan Gohman | c7a37d4 | 2008-12-23 22:45:23 +0000 | [diff] [blame] | 12719 | case X86ISD::BT: return "X86ISD::BT"; |
| Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 12720 | case X86ISD::CMP: return "X86ISD::CMP"; |
| Evan Cheng | 6be2c58 | 2006-04-05 23:38:46 +0000 | [diff] [blame] | 12721 | case X86ISD::COMI: return "X86ISD::COMI"; |
| 12722 | case X86ISD::UCOMI: return "X86ISD::UCOMI"; |
| Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 12723 | case X86ISD::SETCC: return "X86ISD::SETCC"; |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 12724 | case X86ISD::SETCC_CARRY: return "X86ISD::SETCC_CARRY"; |
| Stuart Hastings | 865f093 | 2011-06-03 23:53:54 +0000 | [diff] [blame] | 12725 | case X86ISD::FSETCCsd: return "X86ISD::FSETCCsd"; |
| 12726 | case X86ISD::FSETCCss: return "X86ISD::FSETCCss"; |
| Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 12727 | case X86ISD::CMOV: return "X86ISD::CMOV"; |
| 12728 | case X86ISD::BRCOND: return "X86ISD::BRCOND"; |
| Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 12729 | case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; |
| Evan Cheng | 8df346b | 2006-03-04 01:12:00 +0000 | [diff] [blame] | 12730 | case X86ISD::REP_STOS: return "X86ISD::REP_STOS"; |
| 12731 | case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS"; |
| Evan Cheng | 7ccced6 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 12732 | case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg"; |
| Evan Cheng | 020d2e8 | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 12733 | case X86ISD::Wrapper: return "X86ISD::Wrapper"; |
| Chris Lattner | 18c5987 | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 12734 | case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP"; |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 12735 | case X86ISD::PEXTRB: return "X86ISD::PEXTRB"; |
| Evan Cheng | b067a1e | 2006-03-31 19:22:53 +0000 | [diff] [blame] | 12736 | case X86ISD::PEXTRW: return "X86ISD::PEXTRW"; |
| Nate Begeman | 14d12ca | 2008-02-11 04:19:36 +0000 | [diff] [blame] | 12737 | case X86ISD::INSERTPS: return "X86ISD::INSERTPS"; |
| 12738 | case X86ISD::PINSRB: return "X86ISD::PINSRB"; |
| Evan Cheng | 653159f | 2006-03-31 21:55:24 +0000 | [diff] [blame] | 12739 | case X86ISD::PINSRW: return "X86ISD::PINSRW"; |
| Nate Begeman | b9a47b8 | 2009-02-23 08:49:38 +0000 | [diff] [blame] | 12740 | case X86ISD::PSHUFB: return "X86ISD::PSHUFB"; |
| Bruno Cardoso Lopes | c1af477 | 2011-07-13 21:36:47 +0000 | [diff] [blame] | 12741 | case X86ISD::ANDNP: return "X86ISD::ANDNP"; |
| Craig Topper | 3113384 | 2011-11-19 07:33:10 +0000 | [diff] [blame] | 12742 | case X86ISD::PSIGN: return "X86ISD::PSIGN"; |
| Craig Topper | e6a6277 | 2011-11-13 17:31:07 +0000 | [diff] [blame] | 12743 | case X86ISD::BLENDV: return "X86ISD::BLENDV"; |
| Elena Demikhovsky | 226e0e6 | 2012-12-05 09:24:57 +0000 | [diff] [blame] | 12744 | case X86ISD::BLENDI: return "X86ISD::BLENDI"; |
| Benjamin Kramer | 388fc6a | 2012-12-15 16:47:44 +0000 | [diff] [blame] | 12745 | case X86ISD::SUBUS: return "X86ISD::SUBUS"; |
| Craig Topper | fe03315 | 2011-12-06 09:31:36 +0000 | [diff] [blame] | 12746 | case X86ISD::HADD: return "X86ISD::HADD"; |
| 12747 | case X86ISD::HSUB: return "X86ISD::HSUB"; |
| Craig Topper | e6a6277 | 2011-11-13 17:31:07 +0000 | [diff] [blame] | 12748 | case X86ISD::FHADD: return "X86ISD::FHADD"; |
| 12749 | case X86ISD::FHSUB: return "X86ISD::FHSUB"; |
| Benjamin Kramer | 739c7a8 | 2012-12-21 14:04:55 +0000 | [diff] [blame] | 12750 | case X86ISD::UMAX: return "X86ISD::UMAX"; |
| 12751 | case X86ISD::UMIN: return "X86ISD::UMIN"; |
| 12752 | case X86ISD::SMAX: return "X86ISD::SMAX"; |
| 12753 | case X86ISD::SMIN: return "X86ISD::SMIN"; |
| Evan Cheng | 8ca2932 | 2006-11-10 21:43:37 +0000 | [diff] [blame] | 12754 | case X86ISD::FMAX: return "X86ISD::FMAX"; |
| 12755 | case X86ISD::FMIN: return "X86ISD::FMIN"; |
| Nadav Rotem | d60cb11 | 2012-08-19 13:06:16 +0000 | [diff] [blame] | 12756 | case X86ISD::FMAXC: return "X86ISD::FMAXC"; |
| 12757 | case X86ISD::FMINC: return "X86ISD::FMINC"; |
| Dan Gohman | 2038252 | 2007-07-10 00:05:58 +0000 | [diff] [blame] | 12758 | case X86ISD::FRSQRT: return "X86ISD::FRSQRT"; |
| 12759 | case X86ISD::FRCP: return "X86ISD::FRCP"; |
| Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 12760 | case X86ISD::TLSADDR: return "X86ISD::TLSADDR"; |
| Hans Wennborg | f0234fc | 2012-06-01 16:27:21 +0000 | [diff] [blame] | 12761 | case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR"; |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 12762 | case X86ISD::TLSCALL: return "X86ISD::TLSCALL"; |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 12763 | case X86ISD::EH_SJLJ_SETJMP: return "X86ISD::EH_SJLJ_SETJMP"; |
| 12764 | case X86ISD::EH_SJLJ_LONGJMP: return "X86ISD::EH_SJLJ_LONGJMP"; |
| Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 12765 | case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN"; |
| Arnold Schwaighofer | c85e171 | 2007-10-11 19:40:01 +0000 | [diff] [blame] | 12766 | case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN"; |
| Anton Korobeynikov | 45b22fa | 2007-11-16 01:31:51 +0000 | [diff] [blame] | 12767 | case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m"; |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 12768 | case X86ISD::FNSTSW16r: return "X86ISD::FNSTSW16r"; |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 12769 | case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG"; |
| 12770 | case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG"; |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 12771 | case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG"; |
| 12772 | case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG"; |
| 12773 | case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG"; |
| 12774 | case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG"; |
| 12775 | case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG"; |
| 12776 | case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG"; |
| Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 12777 | case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL"; |
| Michael Liao | b7bf726 | 2012-08-14 22:53:17 +0000 | [diff] [blame] | 12778 | case X86ISD::VSEXT_MOVL: return "X86ISD::VSEXT_MOVL"; |
| Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 12779 | case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD"; |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 12780 | case X86ISD::VZEXT: return "X86ISD::VZEXT"; |
| 12781 | case X86ISD::VSEXT: return "X86ISD::VSEXT"; |
| Michael Liao | 7091b24 | 2012-08-14 21:24:47 +0000 | [diff] [blame] | 12782 | case X86ISD::VFPEXT: return "X86ISD::VFPEXT"; |
| Michael Liao | 44c2d61 | 2012-10-10 16:53:28 +0000 | [diff] [blame] | 12783 | case X86ISD::VFPROUND: return "X86ISD::VFPROUND"; |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12784 | case X86ISD::VSHLDQ: return "X86ISD::VSHLDQ"; |
| 12785 | case X86ISD::VSRLDQ: return "X86ISD::VSRLDQ"; |
| Evan Cheng | f26ffe9 | 2008-05-29 08:22:04 +0000 | [diff] [blame] | 12786 | case X86ISD::VSHL: return "X86ISD::VSHL"; |
| 12787 | case X86ISD::VSRL: return "X86ISD::VSRL"; |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 12788 | case X86ISD::VSRA: return "X86ISD::VSRA"; |
| 12789 | case X86ISD::VSHLI: return "X86ISD::VSHLI"; |
| 12790 | case X86ISD::VSRLI: return "X86ISD::VSRLI"; |
| 12791 | case X86ISD::VSRAI: return "X86ISD::VSRAI"; |
| Craig Topper | 1906d32 | 2012-01-22 23:36:02 +0000 | [diff] [blame] | 12792 | case X86ISD::CMPP: return "X86ISD::CMPP"; |
| Craig Topper | 67609fd | 2012-01-22 22:42:16 +0000 | [diff] [blame] | 12793 | case X86ISD::PCMPEQ: return "X86ISD::PCMPEQ"; |
| 12794 | case X86ISD::PCMPGT: return "X86ISD::PCMPGT"; |
| Bill Wendling | ab55ebd | 2008-12-12 00:56:36 +0000 | [diff] [blame] | 12795 | case X86ISD::ADD: return "X86ISD::ADD"; |
| 12796 | case X86ISD::SUB: return "X86ISD::SUB"; |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 12797 | case X86ISD::ADC: return "X86ISD::ADC"; |
| 12798 | case X86ISD::SBB: return "X86ISD::SBB"; |
| Bill Wendling | d350e02 | 2008-12-12 21:15:41 +0000 | [diff] [blame] | 12799 | case X86ISD::SMUL: return "X86ISD::SMUL"; |
| 12800 | case X86ISD::UMUL: return "X86ISD::UMUL"; |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 12801 | case X86ISD::INC: return "X86ISD::INC"; |
| 12802 | case X86ISD::DEC: return "X86ISD::DEC"; |
| Dan Gohman | e220c4b | 2009-09-18 19:59:53 +0000 | [diff] [blame] | 12803 | case X86ISD::OR: return "X86ISD::OR"; |
| 12804 | case X86ISD::XOR: return "X86ISD::XOR"; |
| 12805 | case X86ISD::AND: return "X86ISD::AND"; |
| Craig Topper | e6a6277 | 2011-11-13 17:31:07 +0000 | [diff] [blame] | 12806 | case X86ISD::BLSI: return "X86ISD::BLSI"; |
| 12807 | case X86ISD::BLSMSK: return "X86ISD::BLSMSK"; |
| 12808 | case X86ISD::BLSR: return "X86ISD::BLSR"; |
| Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 12809 | case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM"; |
| Eric Christopher | 71c6753 | 2009-07-29 00:28:05 +0000 | [diff] [blame] | 12810 | case X86ISD::PTEST: return "X86ISD::PTEST"; |
| Bruno Cardoso Lopes | 045573c | 2010-08-10 23:25:42 +0000 | [diff] [blame] | 12811 | case X86ISD::TESTP: return "X86ISD::TESTP"; |
| Craig Topper | 4aee1bb | 2013-01-28 06:48:25 +0000 | [diff] [blame] | 12812 | case X86ISD::PALIGNR: return "X86ISD::PALIGNR"; |
| Bruno Cardoso Lopes | 3157ef1 | 2010-08-20 22:55:05 +0000 | [diff] [blame] | 12813 | case X86ISD::PSHUFD: return "X86ISD::PSHUFD"; |
| 12814 | case X86ISD::PSHUFHW: return "X86ISD::PSHUFHW"; |
| Bruno Cardoso Lopes | 3157ef1 | 2010-08-20 22:55:05 +0000 | [diff] [blame] | 12815 | case X86ISD::PSHUFLW: return "X86ISD::PSHUFLW"; |
| Craig Topper | b3982da | 2011-12-31 23:50:21 +0000 | [diff] [blame] | 12816 | case X86ISD::SHUFP: return "X86ISD::SHUFP"; |
| Bruno Cardoso Lopes | 3157ef1 | 2010-08-20 22:55:05 +0000 | [diff] [blame] | 12817 | case X86ISD::MOVLHPS: return "X86ISD::MOVLHPS"; |
| Bruno Cardoso Lopes | 3157ef1 | 2010-08-20 22:55:05 +0000 | [diff] [blame] | 12818 | case X86ISD::MOVLHPD: return "X86ISD::MOVLHPD"; |
| Bruno Cardoso Lopes | f2db5b4 | 2010-08-31 21:15:21 +0000 | [diff] [blame] | 12819 | case X86ISD::MOVHLPS: return "X86ISD::MOVHLPS"; |
| Bruno Cardoso Lopes | 56098f5 | 2010-09-01 05:08:25 +0000 | [diff] [blame] | 12820 | case X86ISD::MOVLPS: return "X86ISD::MOVLPS"; |
| 12821 | case X86ISD::MOVLPD: return "X86ISD::MOVLPD"; |
| Bruno Cardoso Lopes | 3157ef1 | 2010-08-20 22:55:05 +0000 | [diff] [blame] | 12822 | case X86ISD::MOVDDUP: return "X86ISD::MOVDDUP"; |
| 12823 | case X86ISD::MOVSHDUP: return "X86ISD::MOVSHDUP"; |
| 12824 | case X86ISD::MOVSLDUP: return "X86ISD::MOVSLDUP"; |
| Bruno Cardoso Lopes | 3157ef1 | 2010-08-20 22:55:05 +0000 | [diff] [blame] | 12825 | case X86ISD::MOVSD: return "X86ISD::MOVSD"; |
| 12826 | case X86ISD::MOVSS: return "X86ISD::MOVSS"; |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 12827 | case X86ISD::UNPCKL: return "X86ISD::UNPCKL"; |
| 12828 | case X86ISD::UNPCKH: return "X86ISD::UNPCKH"; |
| Bruno Cardoso Lopes | 0e6d230 | 2011-08-17 02:29:19 +0000 | [diff] [blame] | 12829 | case X86ISD::VBROADCAST: return "X86ISD::VBROADCAST"; |
| Craig Topper | 316cd2a | 2011-11-30 06:25:25 +0000 | [diff] [blame] | 12830 | case X86ISD::VPERMILP: return "X86ISD::VPERMILP"; |
| Craig Topper | ec24e61 | 2011-11-30 07:47:51 +0000 | [diff] [blame] | 12831 | case X86ISD::VPERM2X128: return "X86ISD::VPERM2X128"; |
| Craig Topper | 8325c11 | 2012-04-16 00:41:45 +0000 | [diff] [blame] | 12832 | case X86ISD::VPERMV: return "X86ISD::VPERMV"; |
| 12833 | case X86ISD::VPERMI: return "X86ISD::VPERMI"; |
| Craig Topper | 5b209e8 | 2012-02-05 03:14:49 +0000 | [diff] [blame] | 12834 | case X86ISD::PMULUDQ: return "X86ISD::PMULUDQ"; |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 12835 | case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS"; |
| Dan Gohman | 320afb8 | 2010-10-12 18:00:49 +0000 | [diff] [blame] | 12836 | case X86ISD::VAARG_64: return "X86ISD::VAARG_64"; |
| Michael J. Spencer | e9c253e | 2010-10-21 01:41:01 +0000 | [diff] [blame] | 12837 | case X86ISD::WIN_ALLOCA: return "X86ISD::WIN_ALLOCA"; |
| Eli Friedman | 1464846 | 2011-07-27 22:21:52 +0000 | [diff] [blame] | 12838 | case X86ISD::MEMBARRIER: return "X86ISD::MEMBARRIER"; |
| Rafael Espindola | d07b7ec | 2011-08-30 19:43:21 +0000 | [diff] [blame] | 12839 | case X86ISD::SEG_ALLOCA: return "X86ISD::SEG_ALLOCA"; |
| Michael J. Spencer | 1a2d061 | 2012-02-24 19:01:22 +0000 | [diff] [blame] | 12840 | case X86ISD::WIN_FTOL: return "X86ISD::WIN_FTOL"; |
| Benjamin Kramer | 17c836c | 2012-04-27 12:07:43 +0000 | [diff] [blame] | 12841 | case X86ISD::SAHF: return "X86ISD::SAHF"; |
| Benjamin Kramer | b9bee04 | 2012-07-12 09:31:43 +0000 | [diff] [blame] | 12842 | case X86ISD::RDRAND: return "X86ISD::RDRAND"; |
| Michael Liao | c26392a | 2013-03-28 23:41:26 +0000 | [diff] [blame] | 12843 | case X86ISD::RDSEED: return "X86ISD::RDSEED"; |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 12844 | case X86ISD::FMADD: return "X86ISD::FMADD"; |
| 12845 | case X86ISD::FMSUB: return "X86ISD::FMSUB"; |
| 12846 | case X86ISD::FNMADD: return "X86ISD::FNMADD"; |
| 12847 | case X86ISD::FNMSUB: return "X86ISD::FNMSUB"; |
| 12848 | case X86ISD::FMADDSUB: return "X86ISD::FMADDSUB"; |
| 12849 | case X86ISD::FMSUBADD: return "X86ISD::FMSUBADD"; |
| Craig Topper | 9c7ae01 | 2012-11-10 01:23:36 +0000 | [diff] [blame] | 12850 | case X86ISD::PCMPESTRI: return "X86ISD::PCMPESTRI"; |
| 12851 | case X86ISD::PCMPISTRI: return "X86ISD::PCMPISTRI"; |
| Michael Liao | f8fd883 | 2013-03-26 22:47:01 +0000 | [diff] [blame] | 12852 | case X86ISD::XTEST: return "X86ISD::XTEST"; |
| Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 12853 | } |
| 12854 | } |
| Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 12855 | |
| Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12856 | // isLegalAddressingMode - Return true if the addressing mode represented |
| 12857 | // by AM is legal for this target, for a load/store of the specified type. |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12858 | bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM, |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 12859 | Type *Ty) const { |
| Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12860 | // X86 supports extremely general addressing modes. |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 12861 | CodeModel::Model M = getTargetMachine().getCodeModel(); |
| Dan Gohman | 92b651f | 2010-08-24 15:55:12 +0000 | [diff] [blame] | 12862 | Reloc::Model R = getTargetMachine().getRelocationModel(); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12863 | |
| Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12864 | // X86 allows a sign-extended 32-bit immediate field as a displacement. |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 12865 | if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != NULL)) |
| Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12866 | return false; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12867 | |
| Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12868 | if (AM.BaseGV) { |
| Chris Lattner | dfed413 | 2009-07-10 07:38:24 +0000 | [diff] [blame] | 12869 | unsigned GVFlags = |
| 12870 | Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine()); |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 12871 | |
| Chris Lattner | dfed413 | 2009-07-10 07:38:24 +0000 | [diff] [blame] | 12872 | // If a reference to this global requires an extra load, we can't fold it. |
| 12873 | if (isGlobalStubReference(GVFlags)) |
| Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12874 | return false; |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 12875 | |
| Chris Lattner | dfed413 | 2009-07-10 07:38:24 +0000 | [diff] [blame] | 12876 | // If BaseGV requires a register for the PIC base, we cannot also have a |
| 12877 | // BaseReg specified. |
| 12878 | if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags)) |
| Dale Johannesen | 203af58 | 2008-12-05 21:47:27 +0000 | [diff] [blame] | 12879 | return false; |
| Evan Cheng | 5278784 | 2007-08-01 23:46:47 +0000 | [diff] [blame] | 12880 | |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 12881 | // If lower 4G is not available, then we must use rip-relative addressing. |
| Dan Gohman | 92b651f | 2010-08-24 15:55:12 +0000 | [diff] [blame] | 12882 | if ((M != CodeModel::Small || R != Reloc::Static) && |
| 12883 | Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1)) |
| Anton Korobeynikov | b5e0172 | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 12884 | return false; |
| Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12885 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12886 | |
| Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12887 | switch (AM.Scale) { |
| 12888 | case 0: |
| 12889 | case 1: |
| 12890 | case 2: |
| 12891 | case 4: |
| 12892 | case 8: |
| 12893 | // These scales always work. |
| 12894 | break; |
| 12895 | case 3: |
| 12896 | case 5: |
| 12897 | case 9: |
| 12898 | // These scales are formed with basereg+scalereg. Only accept if there is |
| 12899 | // no basereg yet. |
| 12900 | if (AM.HasBaseReg) |
| 12901 | return false; |
| 12902 | break; |
| 12903 | default: // Other stuff never works. |
| 12904 | return false; |
| 12905 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 12906 | |
| Chris Lattner | c9addb7 | 2007-03-30 23:15:24 +0000 | [diff] [blame] | 12907 | return true; |
| 12908 | } |
| 12909 | |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 12910 | bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const { |
| Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 12911 | if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) |
| Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 12912 | return false; |
| Evan Cheng | e127a73 | 2007-10-29 07:57:50 +0000 | [diff] [blame] | 12913 | unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); |
| 12914 | unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); |
| Jakub Staszak | c20323a | 2012-12-29 15:57:26 +0000 | [diff] [blame] | 12915 | return NumBits1 > NumBits2; |
| Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 12916 | } |
| 12917 | |
| Evan Cheng | 70e10d3 | 2012-07-17 06:53:39 +0000 | [diff] [blame] | 12918 | bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const { |
| Jakub Staszak | c20323a | 2012-12-29 15:57:26 +0000 | [diff] [blame] | 12919 | return isInt<32>(Imm); |
| Evan Cheng | 70e10d3 | 2012-07-17 06:53:39 +0000 | [diff] [blame] | 12920 | } |
| 12921 | |
| 12922 | bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const { |
| Evan Cheng | a9e13ba | 2012-07-17 18:54:11 +0000 | [diff] [blame] | 12923 | // Can also use sub to handle negated immediates. |
| Jakub Staszak | c20323a | 2012-12-29 15:57:26 +0000 | [diff] [blame] | 12924 | return isInt<32>(Imm); |
| Evan Cheng | 70e10d3 | 2012-07-17 06:53:39 +0000 | [diff] [blame] | 12925 | } |
| 12926 | |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 12927 | bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const { |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 12928 | if (!VT1.isInteger() || !VT2.isInteger()) |
| Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 12929 | return false; |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 12930 | unsigned NumBits1 = VT1.getSizeInBits(); |
| 12931 | unsigned NumBits2 = VT2.getSizeInBits(); |
| Jakub Staszak | c20323a | 2012-12-29 15:57:26 +0000 | [diff] [blame] | 12932 | return NumBits1 > NumBits2; |
| Evan Cheng | 3c3ddb3 | 2007-10-29 19:58:20 +0000 | [diff] [blame] | 12933 | } |
| Evan Cheng | 2bd122c | 2007-10-26 01:56:11 +0000 | [diff] [blame] | 12934 | |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 12935 | bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const { |
| Dan Gohman | 349ba49 | 2009-04-09 02:06:09 +0000 | [diff] [blame] | 12936 | // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. |
| Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 12937 | return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit(); |
| Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 12938 | } |
| 12939 | |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 12940 | bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const { |
| Dan Gohman | 349ba49 | 2009-04-09 02:06:09 +0000 | [diff] [blame] | 12941 | // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12942 | return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit(); |
| Dan Gohman | 97121ba | 2009-04-08 00:15:30 +0000 | [diff] [blame] | 12943 | } |
| 12944 | |
| Evan Cheng | 2766a47 | 2012-12-06 19:13:27 +0000 | [diff] [blame] | 12945 | bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const { |
| 12946 | EVT VT1 = Val.getValueType(); |
| 12947 | if (isZExtFree(VT1, VT2)) |
| 12948 | return true; |
| 12949 | |
| 12950 | if (Val.getOpcode() != ISD::LOAD) |
| 12951 | return false; |
| 12952 | |
| 12953 | if (!VT1.isSimple() || !VT1.isInteger() || |
| 12954 | !VT2.isSimple() || !VT2.isInteger()) |
| 12955 | return false; |
| 12956 | |
| 12957 | switch (VT1.getSimpleVT().SimpleTy) { |
| 12958 | default: break; |
| 12959 | case MVT::i8: |
| 12960 | case MVT::i16: |
| 12961 | case MVT::i32: |
| 12962 | // X86 has 8, 16, and 32-bit zero-extending loads. |
| 12963 | return true; |
| 12964 | } |
| 12965 | |
| 12966 | return false; |
| 12967 | } |
| 12968 | |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 12969 | bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const { |
| Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 12970 | // i16 instructions are longer (0x66 prefix) and potentially slower. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 12971 | return !(VT1 == MVT::i32 && VT2 == MVT::i16); |
| Evan Cheng | 8b944d3 | 2009-05-28 00:35:15 +0000 | [diff] [blame] | 12972 | } |
| 12973 | |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 12974 | /// isShuffleMaskLegal - Targets can use this to indicate that they only |
| 12975 | /// support *some* VECTOR_SHUFFLE operations, those with specific masks. |
| 12976 | /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values |
| 12977 | /// are assumed to be legal. |
| 12978 | bool |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 12979 | X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M, |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 12980 | EVT VT) const { |
| Eric Christopher | cff6f85 | 2010-04-15 01:40:20 +0000 | [diff] [blame] | 12981 | // Very little shuffling can be done for 64-bit vectors right now. |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 12982 | if (VT.getSizeInBits() == 64) |
| Craig Topper | 1dc0fbc | 2011-12-05 07:27:14 +0000 | [diff] [blame] | 12983 | return false; |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 12984 | |
| Nate Begeman | a09008b | 2009-10-19 02:17:23 +0000 | [diff] [blame] | 12985 | // FIXME: pshufb, blends, shifts. |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 12986 | return (VT.getVectorNumElements() == 2 || |
| 12987 | ShuffleVectorSDNode::isSplatMask(&M[0], VT) || |
| 12988 | isMOVLMask(M, VT) || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 12989 | isSHUFPMask(M, VT, Subtarget->hasFp256()) || |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 12990 | isPSHUFDMask(M, VT) || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 12991 | isPSHUFHWMask(M, VT, Subtarget->hasInt256()) || |
| 12992 | isPSHUFLWMask(M, VT, Subtarget->hasInt256()) || |
| Craig Topper | 0e2037b | 2012-01-20 05:53:00 +0000 | [diff] [blame] | 12993 | isPALIGNRMask(M, VT, Subtarget) || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 12994 | isUNPCKLMask(M, VT, Subtarget->hasInt256()) || |
| 12995 | isUNPCKHMask(M, VT, Subtarget->hasInt256()) || |
| 12996 | isUNPCKL_v_undef_Mask(M, VT, Subtarget->hasInt256()) || |
| 12997 | isUNPCKH_v_undef_Mask(M, VT, Subtarget->hasInt256())); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 12998 | } |
| 12999 | |
| Dan Gohman | 7d8143f | 2008-04-09 20:09:42 +0000 | [diff] [blame] | 13000 | bool |
| Nate Begeman | 5a5ca15 | 2009-04-29 05:20:52 +0000 | [diff] [blame] | 13001 | X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask, |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 13002 | EVT VT) const { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 13003 | unsigned NumElts = VT.getVectorNumElements(); |
| 13004 | // FIXME: This collection of masks seems suspect. |
| 13005 | if (NumElts == 2) |
| 13006 | return true; |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 13007 | if (NumElts == 4 && VT.is128BitVector()) { |
| Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 13008 | return (isMOVLMask(Mask, VT) || |
| 13009 | isCommutedMOVLMask(Mask, VT, true) || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 13010 | isSHUFPMask(Mask, VT, Subtarget->hasFp256()) || |
| 13011 | isSHUFPMask(Mask, VT, Subtarget->hasFp256(), /* Commuted */ true)); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 13012 | } |
| 13013 | return false; |
| 13014 | } |
| 13015 | |
| 13016 | //===----------------------------------------------------------------------===// |
| 13017 | // X86 Scheduler Hooks |
| 13018 | //===----------------------------------------------------------------------===// |
| 13019 | |
| Michael Liao | be02a90 | 2012-11-08 07:28:54 +0000 | [diff] [blame] | 13020 | /// Utility function to emit xbegin specifying the start of an RTM region. |
| Craig Topper | 2da3691 | 2012-11-11 22:45:02 +0000 | [diff] [blame] | 13021 | static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB, |
| 13022 | const TargetInstrInfo *TII) { |
| Michael Liao | be02a90 | 2012-11-08 07:28:54 +0000 | [diff] [blame] | 13023 | DebugLoc DL = MI->getDebugLoc(); |
| Michael Liao | be02a90 | 2012-11-08 07:28:54 +0000 | [diff] [blame] | 13024 | |
| 13025 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 13026 | MachineFunction::iterator I = MBB; |
| 13027 | ++I; |
| 13028 | |
| 13029 | // For the v = xbegin(), we generate |
| 13030 | // |
| 13031 | // thisMBB: |
| 13032 | // xbegin sinkMBB |
| 13033 | // |
| 13034 | // mainMBB: |
| 13035 | // eax = -1 |
| 13036 | // |
| 13037 | // sinkMBB: |
| 13038 | // v = eax |
| 13039 | |
| 13040 | MachineBasicBlock *thisMBB = MBB; |
| 13041 | MachineFunction *MF = MBB->getParent(); |
| 13042 | MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); |
| 13043 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); |
| 13044 | MF->insert(I, mainMBB); |
| 13045 | MF->insert(I, sinkMBB); |
| 13046 | |
| 13047 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 13048 | sinkMBB->splice(sinkMBB->begin(), MBB, |
| 13049 | llvm::next(MachineBasicBlock::iterator(MI)), MBB->end()); |
| 13050 | sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 13051 | |
| 13052 | // thisMBB: |
| 13053 | // xbegin sinkMBB |
| 13054 | // # fallthrough to mainMBB |
| 13055 | // # abortion to sinkMBB |
| 13056 | BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB); |
| 13057 | thisMBB->addSuccessor(mainMBB); |
| 13058 | thisMBB->addSuccessor(sinkMBB); |
| 13059 | |
| 13060 | // mainMBB: |
| 13061 | // EAX = -1 |
| 13062 | BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1); |
| 13063 | mainMBB->addSuccessor(sinkMBB); |
| 13064 | |
| 13065 | // sinkMBB: |
| 13066 | // EAX is live into the sinkMBB |
| 13067 | sinkMBB->addLiveIn(X86::EAX); |
| 13068 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, |
| 13069 | TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg()) |
| 13070 | .addReg(X86::EAX); |
| 13071 | |
| 13072 | MI->eraseFromParent(); |
| 13073 | return sinkMBB; |
| 13074 | } |
| 13075 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13076 | // Get CMPXCHG opcode for the specified data type. |
| 13077 | static unsigned getCmpXChgOpcode(EVT VT) { |
| 13078 | switch (VT.getSimpleVT().SimpleTy) { |
| 13079 | case MVT::i8: return X86::LCMPXCHG8; |
| 13080 | case MVT::i16: return X86::LCMPXCHG16; |
| 13081 | case MVT::i32: return X86::LCMPXCHG32; |
| 13082 | case MVT::i64: return X86::LCMPXCHG64; |
| 13083 | default: |
| 13084 | break; |
| Richard Smith | 42fc29e | 2012-04-13 22:47:00 +0000 | [diff] [blame] | 13085 | } |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13086 | llvm_unreachable("Invalid operand size!"); |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 13087 | } |
| 13088 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13089 | // Get LOAD opcode for the specified data type. |
| 13090 | static unsigned getLoadOpcode(EVT VT) { |
| 13091 | switch (VT.getSimpleVT().SimpleTy) { |
| 13092 | case MVT::i8: return X86::MOV8rm; |
| 13093 | case MVT::i16: return X86::MOV16rm; |
| 13094 | case MVT::i32: return X86::MOV32rm; |
| 13095 | case MVT::i64: return X86::MOV64rm; |
| 13096 | default: |
| 13097 | break; |
| 13098 | } |
| 13099 | llvm_unreachable("Invalid operand size!"); |
| 13100 | } |
| 13101 | |
| 13102 | // Get opcode of the non-atomic one from the specified atomic instruction. |
| 13103 | static unsigned getNonAtomicOpcode(unsigned Opc) { |
| 13104 | switch (Opc) { |
| 13105 | case X86::ATOMAND8: return X86::AND8rr; |
| 13106 | case X86::ATOMAND16: return X86::AND16rr; |
| 13107 | case X86::ATOMAND32: return X86::AND32rr; |
| 13108 | case X86::ATOMAND64: return X86::AND64rr; |
| 13109 | case X86::ATOMOR8: return X86::OR8rr; |
| 13110 | case X86::ATOMOR16: return X86::OR16rr; |
| 13111 | case X86::ATOMOR32: return X86::OR32rr; |
| 13112 | case X86::ATOMOR64: return X86::OR64rr; |
| 13113 | case X86::ATOMXOR8: return X86::XOR8rr; |
| 13114 | case X86::ATOMXOR16: return X86::XOR16rr; |
| 13115 | case X86::ATOMXOR32: return X86::XOR32rr; |
| 13116 | case X86::ATOMXOR64: return X86::XOR64rr; |
| 13117 | } |
| 13118 | llvm_unreachable("Unhandled atomic-load-op opcode!"); |
| 13119 | } |
| 13120 | |
| 13121 | // Get opcode of the non-atomic one from the specified atomic instruction with |
| 13122 | // extra opcode. |
| 13123 | static unsigned getNonAtomicOpcodeWithExtraOpc(unsigned Opc, |
| 13124 | unsigned &ExtraOpc) { |
| 13125 | switch (Opc) { |
| 13126 | case X86::ATOMNAND8: ExtraOpc = X86::NOT8r; return X86::AND8rr; |
| 13127 | case X86::ATOMNAND16: ExtraOpc = X86::NOT16r; return X86::AND16rr; |
| 13128 | case X86::ATOMNAND32: ExtraOpc = X86::NOT32r; return X86::AND32rr; |
| 13129 | case X86::ATOMNAND64: ExtraOpc = X86::NOT64r; return X86::AND64rr; |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13130 | case X86::ATOMMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVL32rr; |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13131 | case X86::ATOMMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVL16rr; |
| 13132 | case X86::ATOMMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVL32rr; |
| 13133 | case X86::ATOMMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVL64rr; |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13134 | case X86::ATOMMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVG32rr; |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13135 | case X86::ATOMMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVG16rr; |
| 13136 | case X86::ATOMMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVG32rr; |
| 13137 | case X86::ATOMMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVG64rr; |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13138 | case X86::ATOMUMAX8: ExtraOpc = X86::CMP8rr; return X86::CMOVB32rr; |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13139 | case X86::ATOMUMAX16: ExtraOpc = X86::CMP16rr; return X86::CMOVB16rr; |
| 13140 | case X86::ATOMUMAX32: ExtraOpc = X86::CMP32rr; return X86::CMOVB32rr; |
| 13141 | case X86::ATOMUMAX64: ExtraOpc = X86::CMP64rr; return X86::CMOVB64rr; |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13142 | case X86::ATOMUMIN8: ExtraOpc = X86::CMP8rr; return X86::CMOVA32rr; |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13143 | case X86::ATOMUMIN16: ExtraOpc = X86::CMP16rr; return X86::CMOVA16rr; |
| 13144 | case X86::ATOMUMIN32: ExtraOpc = X86::CMP32rr; return X86::CMOVA32rr; |
| 13145 | case X86::ATOMUMIN64: ExtraOpc = X86::CMP64rr; return X86::CMOVA64rr; |
| 13146 | } |
| 13147 | llvm_unreachable("Unhandled atomic-load-op opcode!"); |
| 13148 | } |
| 13149 | |
| 13150 | // Get opcode of the non-atomic one from the specified atomic instruction for |
| 13151 | // 64-bit data type on 32-bit target. |
| 13152 | static unsigned getNonAtomic6432Opcode(unsigned Opc, unsigned &HiOpc) { |
| 13153 | switch (Opc) { |
| 13154 | case X86::ATOMAND6432: HiOpc = X86::AND32rr; return X86::AND32rr; |
| 13155 | case X86::ATOMOR6432: HiOpc = X86::OR32rr; return X86::OR32rr; |
| 13156 | case X86::ATOMXOR6432: HiOpc = X86::XOR32rr; return X86::XOR32rr; |
| 13157 | case X86::ATOMADD6432: HiOpc = X86::ADC32rr; return X86::ADD32rr; |
| 13158 | case X86::ATOMSUB6432: HiOpc = X86::SBB32rr; return X86::SUB32rr; |
| 13159 | case X86::ATOMSWAP6432: HiOpc = X86::MOV32rr; return X86::MOV32rr; |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 13160 | case X86::ATOMMAX6432: HiOpc = X86::SETLr; return X86::SETLr; |
| 13161 | case X86::ATOMMIN6432: HiOpc = X86::SETGr; return X86::SETGr; |
| 13162 | case X86::ATOMUMAX6432: HiOpc = X86::SETBr; return X86::SETBr; |
| 13163 | case X86::ATOMUMIN6432: HiOpc = X86::SETAr; return X86::SETAr; |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13164 | } |
| 13165 | llvm_unreachable("Unhandled atomic-load-op opcode!"); |
| 13166 | } |
| 13167 | |
| 13168 | // Get opcode of the non-atomic one from the specified atomic instruction for |
| 13169 | // 64-bit data type on 32-bit target with extra opcode. |
| 13170 | static unsigned getNonAtomic6432OpcodeWithExtraOpc(unsigned Opc, |
| 13171 | unsigned &HiOpc, |
| 13172 | unsigned &ExtraOpc) { |
| 13173 | switch (Opc) { |
| 13174 | case X86::ATOMNAND6432: |
| 13175 | ExtraOpc = X86::NOT32r; |
| 13176 | HiOpc = X86::AND32rr; |
| 13177 | return X86::AND32rr; |
| 13178 | } |
| 13179 | llvm_unreachable("Unhandled atomic-load-op opcode!"); |
| 13180 | } |
| 13181 | |
| 13182 | // Get pseudo CMOV opcode from the specified data type. |
| 13183 | static unsigned getPseudoCMOVOpc(EVT VT) { |
| 13184 | switch (VT.getSimpleVT().SimpleTy) { |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13185 | case MVT::i8: return X86::CMOV_GR8; |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13186 | case MVT::i16: return X86::CMOV_GR16; |
| 13187 | case MVT::i32: return X86::CMOV_GR32; |
| 13188 | default: |
| 13189 | break; |
| 13190 | } |
| 13191 | llvm_unreachable("Unknown CMOV opcode!"); |
| 13192 | } |
| 13193 | |
| 13194 | // EmitAtomicLoadArith - emit the code sequence for pseudo atomic instructions. |
| 13195 | // They will be translated into a spin-loop or compare-exchange loop from |
| 13196 | // |
| 13197 | // ... |
| 13198 | // dst = atomic-fetch-op MI.addr, MI.val |
| 13199 | // ... |
| 13200 | // |
| 13201 | // to |
| 13202 | // |
| 13203 | // ... |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13204 | // t1 = LOAD MI.addr |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13205 | // loop: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13206 | // t4 = phi(t1, t3 / loop) |
| 13207 | // t2 = OP MI.val, t4 |
| 13208 | // EAX = t4 |
| 13209 | // LCMPXCHG [MI.addr], t2, [EAX is implicitly used & defined] |
| 13210 | // t3 = EAX |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13211 | // JNE loop |
| 13212 | // sink: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13213 | // dst = t3 |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13214 | // ... |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 13215 | MachineBasicBlock * |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13216 | X86TargetLowering::EmitAtomicLoadArith(MachineInstr *MI, |
| 13217 | MachineBasicBlock *MBB) const { |
| 13218 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 13219 | DebugLoc DL = MI->getDebugLoc(); |
| 13220 | |
| 13221 | MachineFunction *MF = MBB->getParent(); |
| 13222 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 13223 | |
| 13224 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 13225 | MachineFunction::iterator I = MBB; |
| 13226 | ++I; |
| 13227 | |
| Michael Liao | 13d08bf | 2013-01-22 21:47:38 +0000 | [diff] [blame] | 13228 | assert(MI->getNumOperands() <= X86::AddrNumOperands + 4 && |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13229 | "Unexpected number of operands"); |
| 13230 | |
| 13231 | assert(MI->hasOneMemOperand() && |
| 13232 | "Expected atomic-load-op to have one memoperand"); |
| 13233 | |
| 13234 | // Memory Reference |
| 13235 | MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin(); |
| 13236 | MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end(); |
| 13237 | |
| 13238 | unsigned DstReg, SrcReg; |
| 13239 | unsigned MemOpndSlot; |
| 13240 | |
| 13241 | unsigned CurOp = 0; |
| 13242 | |
| 13243 | DstReg = MI->getOperand(CurOp++).getReg(); |
| 13244 | MemOpndSlot = CurOp; |
| 13245 | CurOp += X86::AddrNumOperands; |
| 13246 | SrcReg = MI->getOperand(CurOp++).getReg(); |
| 13247 | |
| 13248 | const TargetRegisterClass *RC = MRI.getRegClass(DstReg); |
| Craig Topper | f4d25a2 | 2012-09-30 19:49:56 +0000 | [diff] [blame] | 13249 | MVT::SimpleValueType VT = *RC->vt_begin(); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13250 | unsigned t1 = MRI.createVirtualRegister(RC); |
| 13251 | unsigned t2 = MRI.createVirtualRegister(RC); |
| 13252 | unsigned t3 = MRI.createVirtualRegister(RC); |
| 13253 | unsigned t4 = MRI.createVirtualRegister(RC); |
| 13254 | unsigned PhyReg = getX86SubSuperRegister(X86::EAX, VT); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13255 | |
| 13256 | unsigned LCMPXCHGOpc = getCmpXChgOpcode(VT); |
| 13257 | unsigned LOADOpc = getLoadOpcode(VT); |
| 13258 | |
| 13259 | // For the atomic load-arith operator, we generate |
| 13260 | // |
| 13261 | // thisMBB: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13262 | // t1 = LOAD [MI.addr] |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13263 | // mainMBB: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13264 | // t4 = phi(t1 / thisMBB, t3 / mainMBB) |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13265 | // t1 = OP MI.val, EAX |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13266 | // EAX = t4 |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13267 | // LCMPXCHG [MI.addr], t1, [EAX is implicitly used & defined] |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13268 | // t3 = EAX |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13269 | // JNE mainMBB |
| 13270 | // sinkMBB: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13271 | // dst = t3 |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13272 | |
| 13273 | MachineBasicBlock *thisMBB = MBB; |
| 13274 | MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); |
| 13275 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); |
| 13276 | MF->insert(I, mainMBB); |
| 13277 | MF->insert(I, sinkMBB); |
| 13278 | |
| 13279 | MachineInstrBuilder MIB; |
| 13280 | |
| 13281 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 13282 | sinkMBB->splice(sinkMBB->begin(), MBB, |
| 13283 | llvm::next(MachineBasicBlock::iterator(MI)), MBB->end()); |
| 13284 | sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 13285 | |
| 13286 | // thisMBB: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13287 | MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1); |
| 13288 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { |
| 13289 | MachineOperand NewMO = MI->getOperand(MemOpndSlot + i); |
| 13290 | if (NewMO.isReg()) |
| 13291 | NewMO.setIsKill(false); |
| 13292 | MIB.addOperand(NewMO); |
| 13293 | } |
| 13294 | for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) { |
| 13295 | unsigned flags = (*MMOI)->getFlags(); |
| 13296 | flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad; |
| 13297 | MachineMemOperand *MMO = |
| 13298 | MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags, |
| 13299 | (*MMOI)->getSize(), |
| 13300 | (*MMOI)->getBaseAlignment(), |
| 13301 | (*MMOI)->getTBAAInfo(), |
| 13302 | (*MMOI)->getRanges()); |
| 13303 | MIB.addMemOperand(MMO); |
| 13304 | } |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13305 | |
| 13306 | thisMBB->addSuccessor(mainMBB); |
| 13307 | |
| 13308 | // mainMBB: |
| 13309 | MachineBasicBlock *origMainMBB = mainMBB; |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13310 | |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13311 | // Add a PHI. |
| Michael Liao | fe9dbe0 | 2013-03-07 01:01:29 +0000 | [diff] [blame] | 13312 | MachineInstr *Phi = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4) |
| 13313 | .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13314 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13315 | unsigned Opc = MI->getOpcode(); |
| 13316 | switch (Opc) { |
| 13317 | default: |
| 13318 | llvm_unreachable("Unhandled atomic-load-op opcode!"); |
| 13319 | case X86::ATOMAND8: |
| 13320 | case X86::ATOMAND16: |
| 13321 | case X86::ATOMAND32: |
| 13322 | case X86::ATOMAND64: |
| 13323 | case X86::ATOMOR8: |
| 13324 | case X86::ATOMOR16: |
| 13325 | case X86::ATOMOR32: |
| 13326 | case X86::ATOMOR64: |
| 13327 | case X86::ATOMXOR8: |
| 13328 | case X86::ATOMXOR16: |
| 13329 | case X86::ATOMXOR32: |
| 13330 | case X86::ATOMXOR64: { |
| 13331 | unsigned ARITHOpc = getNonAtomicOpcode(Opc); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13332 | BuildMI(mainMBB, DL, TII->get(ARITHOpc), t2).addReg(SrcReg) |
| 13333 | .addReg(t4); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13334 | break; |
| 13335 | } |
| 13336 | case X86::ATOMNAND8: |
| 13337 | case X86::ATOMNAND16: |
| 13338 | case X86::ATOMNAND32: |
| 13339 | case X86::ATOMNAND64: { |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13340 | unsigned Tmp = MRI.createVirtualRegister(RC); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13341 | unsigned NOTOpc; |
| 13342 | unsigned ANDOpc = getNonAtomicOpcodeWithExtraOpc(Opc, NOTOpc); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13343 | BuildMI(mainMBB, DL, TII->get(ANDOpc), Tmp).addReg(SrcReg) |
| 13344 | .addReg(t4); |
| 13345 | BuildMI(mainMBB, DL, TII->get(NOTOpc), t2).addReg(Tmp); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13346 | break; |
| 13347 | } |
| Michael Liao | 0838249 | 2012-09-21 03:00:17 +0000 | [diff] [blame] | 13348 | case X86::ATOMMAX8: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13349 | case X86::ATOMMAX16: |
| 13350 | case X86::ATOMMAX32: |
| 13351 | case X86::ATOMMAX64: |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13352 | case X86::ATOMMIN8: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13353 | case X86::ATOMMIN16: |
| 13354 | case X86::ATOMMIN32: |
| 13355 | case X86::ATOMMIN64: |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13356 | case X86::ATOMUMAX8: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13357 | case X86::ATOMUMAX16: |
| 13358 | case X86::ATOMUMAX32: |
| 13359 | case X86::ATOMUMAX64: |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13360 | case X86::ATOMUMIN8: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13361 | case X86::ATOMUMIN16: |
| 13362 | case X86::ATOMUMIN32: |
| 13363 | case X86::ATOMUMIN64: { |
| 13364 | unsigned CMPOpc; |
| 13365 | unsigned CMOVOpc = getNonAtomicOpcodeWithExtraOpc(Opc, CMPOpc); |
| 13366 | |
| 13367 | BuildMI(mainMBB, DL, TII->get(CMPOpc)) |
| 13368 | .addReg(SrcReg) |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13369 | .addReg(t4); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13370 | |
| 13371 | if (Subtarget->hasCMov()) { |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13372 | if (VT != MVT::i8) { |
| 13373 | // Native support |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13374 | BuildMI(mainMBB, DL, TII->get(CMOVOpc), t2) |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13375 | .addReg(SrcReg) |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13376 | .addReg(t4); |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13377 | } else { |
| 13378 | // Promote i8 to i32 to use CMOV32 |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13379 | const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo(); |
| 13380 | const TargetRegisterClass *RC32 = |
| 13381 | TRI->getSubClassWithSubReg(getRegClassFor(MVT::i32), X86::sub_8bit); |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13382 | unsigned SrcReg32 = MRI.createVirtualRegister(RC32); |
| 13383 | unsigned AccReg32 = MRI.createVirtualRegister(RC32); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13384 | unsigned Tmp = MRI.createVirtualRegister(RC32); |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13385 | |
| 13386 | unsigned Undef = MRI.createVirtualRegister(RC32); |
| 13387 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Undef); |
| 13388 | |
| 13389 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), SrcReg32) |
| 13390 | .addReg(Undef) |
| 13391 | .addReg(SrcReg) |
| 13392 | .addImm(X86::sub_8bit); |
| 13393 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::INSERT_SUBREG), AccReg32) |
| 13394 | .addReg(Undef) |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13395 | .addReg(t4) |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13396 | .addImm(X86::sub_8bit); |
| 13397 | |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13398 | BuildMI(mainMBB, DL, TII->get(CMOVOpc), Tmp) |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13399 | .addReg(SrcReg32) |
| 13400 | .addReg(AccReg32); |
| 13401 | |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13402 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t2) |
| 13403 | .addReg(Tmp, 0, X86::sub_8bit); |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13404 | } |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13405 | } else { |
| 13406 | // Use pseudo select and lower them. |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 13407 | assert((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32) && |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13408 | "Invalid atomic-load-op transformation!"); |
| 13409 | unsigned SelOpc = getPseudoCMOVOpc(VT); |
| 13410 | X86::CondCode CC = X86::getCondFromCMovOpc(CMOVOpc); |
| 13411 | assert(CC != X86::COND_INVALID && "Invalid atomic-load-op transformation!"); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13412 | MIB = BuildMI(mainMBB, DL, TII->get(SelOpc), t2) |
| 13413 | .addReg(SrcReg).addReg(t4) |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13414 | .addImm(CC); |
| 13415 | mainMBB = EmitLoweredSelect(MIB, mainMBB); |
| Michael Liao | fe9dbe0 | 2013-03-07 01:01:29 +0000 | [diff] [blame] | 13416 | // Replace the original PHI node as mainMBB is changed after CMOV |
| 13417 | // lowering. |
| 13418 | BuildMI(*origMainMBB, Phi, DL, TII->get(X86::PHI), t4) |
| 13419 | .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(mainMBB); |
| 13420 | Phi->eraseFromParent(); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13421 | } |
| 13422 | break; |
| 13423 | } |
| 13424 | } |
| 13425 | |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13426 | // Copy PhyReg back from virtual register. |
| 13427 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), PhyReg) |
| 13428 | .addReg(t4); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13429 | |
| 13430 | MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc)); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13431 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { |
| 13432 | MachineOperand NewMO = MI->getOperand(MemOpndSlot + i); |
| 13433 | if (NewMO.isReg()) |
| 13434 | NewMO.setIsKill(false); |
| 13435 | MIB.addOperand(NewMO); |
| 13436 | } |
| 13437 | MIB.addReg(t2); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13438 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 13439 | |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13440 | // Copy PhyReg back to virtual register. |
| 13441 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3) |
| 13442 | .addReg(PhyReg); |
| 13443 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13444 | BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB); |
| 13445 | |
| 13446 | mainMBB->addSuccessor(origMainMBB); |
| 13447 | mainMBB->addSuccessor(sinkMBB); |
| 13448 | |
| 13449 | // sinkMBB: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13450 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, |
| 13451 | TII->get(TargetOpcode::COPY), DstReg) |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13452 | .addReg(t3); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13453 | |
| 13454 | MI->eraseFromParent(); |
| 13455 | return sinkMBB; |
| 13456 | } |
| 13457 | |
| 13458 | // EmitAtomicLoadArith6432 - emit the code sequence for pseudo atomic |
| 13459 | // instructions. They will be translated into a spin-loop or compare-exchange |
| 13460 | // loop from |
| 13461 | // |
| 13462 | // ... |
| 13463 | // dst = atomic-fetch-op MI.addr, MI.val |
| 13464 | // ... |
| 13465 | // |
| 13466 | // to |
| 13467 | // |
| 13468 | // ... |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13469 | // t1L = LOAD [MI.addr + 0] |
| 13470 | // t1H = LOAD [MI.addr + 4] |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13471 | // loop: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13472 | // t4L = phi(t1L, t3L / loop) |
| 13473 | // t4H = phi(t1H, t3H / loop) |
| 13474 | // t2L = OP MI.val.lo, t4L |
| 13475 | // t2H = OP MI.val.hi, t4H |
| 13476 | // EAX = t4L |
| 13477 | // EDX = t4H |
| 13478 | // EBX = t2L |
| 13479 | // ECX = t2H |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13480 | // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined] |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13481 | // t3L = EAX |
| 13482 | // t3H = EDX |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13483 | // JNE loop |
| 13484 | // sink: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13485 | // dstL = t3L |
| 13486 | // dstH = t3H |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13487 | // ... |
| 13488 | MachineBasicBlock * |
| 13489 | X86TargetLowering::EmitAtomicLoadArith6432(MachineInstr *MI, |
| 13490 | MachineBasicBlock *MBB) const { |
| 13491 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 13492 | DebugLoc DL = MI->getDebugLoc(); |
| 13493 | |
| 13494 | MachineFunction *MF = MBB->getParent(); |
| 13495 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 13496 | |
| 13497 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 13498 | MachineFunction::iterator I = MBB; |
| 13499 | ++I; |
| 13500 | |
| Michael Liao | 13d08bf | 2013-01-22 21:47:38 +0000 | [diff] [blame] | 13501 | assert(MI->getNumOperands() <= X86::AddrNumOperands + 7 && |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13502 | "Unexpected number of operands"); |
| 13503 | |
| 13504 | assert(MI->hasOneMemOperand() && |
| 13505 | "Expected atomic-load-op32 to have one memoperand"); |
| 13506 | |
| 13507 | // Memory Reference |
| 13508 | MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin(); |
| 13509 | MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end(); |
| 13510 | |
| 13511 | unsigned DstLoReg, DstHiReg; |
| 13512 | unsigned SrcLoReg, SrcHiReg; |
| 13513 | unsigned MemOpndSlot; |
| 13514 | |
| 13515 | unsigned CurOp = 0; |
| 13516 | |
| 13517 | DstLoReg = MI->getOperand(CurOp++).getReg(); |
| 13518 | DstHiReg = MI->getOperand(CurOp++).getReg(); |
| 13519 | MemOpndSlot = CurOp; |
| 13520 | CurOp += X86::AddrNumOperands; |
| 13521 | SrcLoReg = MI->getOperand(CurOp++).getReg(); |
| 13522 | SrcHiReg = MI->getOperand(CurOp++).getReg(); |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 13523 | |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 13524 | const TargetRegisterClass *RC = &X86::GR32RegClass; |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 13525 | const TargetRegisterClass *RC8 = &X86::GR8RegClass; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13526 | |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13527 | unsigned t1L = MRI.createVirtualRegister(RC); |
| 13528 | unsigned t1H = MRI.createVirtualRegister(RC); |
| 13529 | unsigned t2L = MRI.createVirtualRegister(RC); |
| 13530 | unsigned t2H = MRI.createVirtualRegister(RC); |
| 13531 | unsigned t3L = MRI.createVirtualRegister(RC); |
| 13532 | unsigned t3H = MRI.createVirtualRegister(RC); |
| 13533 | unsigned t4L = MRI.createVirtualRegister(RC); |
| 13534 | unsigned t4H = MRI.createVirtualRegister(RC); |
| 13535 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13536 | unsigned LCMPXCHGOpc = X86::LCMPXCHG8B; |
| 13537 | unsigned LOADOpc = X86::MOV32rm; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13538 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13539 | // For the atomic load-arith operator, we generate |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 13540 | // |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13541 | // thisMBB: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13542 | // t1L = LOAD [MI.addr + 0] |
| 13543 | // t1H = LOAD [MI.addr + 4] |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13544 | // mainMBB: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13545 | // t4L = phi(t1L / thisMBB, t3L / mainMBB) |
| 13546 | // t4H = phi(t1H / thisMBB, t3H / mainMBB) |
| 13547 | // t2L = OP MI.val.lo, t4L |
| 13548 | // t2H = OP MI.val.hi, t4H |
| 13549 | // EBX = t2L |
| 13550 | // ECX = t2H |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13551 | // LCMPXCHG8B [MI.addr], [ECX:EBX & EDX:EAX are implicitly used and EDX:EAX is implicitly defined] |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13552 | // t3L = EAX |
| 13553 | // t3H = EDX |
| 13554 | // JNE loop |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13555 | // sinkMBB: |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13556 | // dstL = t3L |
| 13557 | // dstH = t3H |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13558 | |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 13559 | MachineBasicBlock *thisMBB = MBB; |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13560 | MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); |
| 13561 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); |
| 13562 | MF->insert(I, mainMBB); |
| 13563 | MF->insert(I, sinkMBB); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13564 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13565 | MachineInstrBuilder MIB; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13566 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13567 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 13568 | sinkMBB->splice(sinkMBB->begin(), MBB, |
| 13569 | llvm::next(MachineBasicBlock::iterator(MI)), MBB->end()); |
| 13570 | sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13571 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13572 | // thisMBB: |
| 13573 | // Lo |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13574 | MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1L); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13575 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13576 | MachineOperand NewMO = MI->getOperand(MemOpndSlot + i); |
| 13577 | if (NewMO.isReg()) |
| 13578 | NewMO.setIsKill(false); |
| 13579 | MIB.addOperand(NewMO); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13580 | } |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13581 | for (MachineInstr::mmo_iterator MMOI = MMOBegin; MMOI != MMOEnd; ++MMOI) { |
| 13582 | unsigned flags = (*MMOI)->getFlags(); |
| 13583 | flags = (flags & ~MachineMemOperand::MOStore) | MachineMemOperand::MOLoad; |
| 13584 | MachineMemOperand *MMO = |
| 13585 | MF->getMachineMemOperand((*MMOI)->getPointerInfo(), flags, |
| 13586 | (*MMOI)->getSize(), |
| 13587 | (*MMOI)->getBaseAlignment(), |
| 13588 | (*MMOI)->getTBAAInfo(), |
| 13589 | (*MMOI)->getRanges()); |
| 13590 | MIB.addMemOperand(MMO); |
| 13591 | }; |
| 13592 | MachineInstr *LowMI = MIB; |
| 13593 | |
| 13594 | // Hi |
| 13595 | MIB = BuildMI(thisMBB, DL, TII->get(LOADOpc), t1H); |
| 13596 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { |
| 13597 | if (i == X86::AddrDisp) { |
| 13598 | MIB.addDisp(MI->getOperand(MemOpndSlot + i), 4); // 4 == sizeof(i32) |
| 13599 | } else { |
| 13600 | MachineOperand NewMO = MI->getOperand(MemOpndSlot + i); |
| 13601 | if (NewMO.isReg()) |
| 13602 | NewMO.setIsKill(false); |
| 13603 | MIB.addOperand(NewMO); |
| 13604 | } |
| 13605 | } |
| 13606 | MIB.setMemRefs(LowMI->memoperands_begin(), LowMI->memoperands_end()); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13607 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13608 | thisMBB->addSuccessor(mainMBB); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13609 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13610 | // mainMBB: |
| 13611 | MachineBasicBlock *origMainMBB = mainMBB; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13612 | |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13613 | // Add PHIs. |
| Michael Liao | fe9dbe0 | 2013-03-07 01:01:29 +0000 | [diff] [blame] | 13614 | MachineInstr *PhiL = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4L) |
| 13615 | .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB); |
| 13616 | MachineInstr *PhiH = BuildMI(mainMBB, DL, TII->get(X86::PHI), t4H) |
| 13617 | .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13618 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13619 | unsigned Opc = MI->getOpcode(); |
| 13620 | switch (Opc) { |
| 13621 | default: |
| 13622 | llvm_unreachable("Unhandled atomic-load-op6432 opcode!"); |
| 13623 | case X86::ATOMAND6432: |
| 13624 | case X86::ATOMOR6432: |
| 13625 | case X86::ATOMXOR6432: |
| 13626 | case X86::ATOMADD6432: |
| 13627 | case X86::ATOMSUB6432: { |
| 13628 | unsigned HiOpc; |
| 13629 | unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13630 | BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(t4L) |
| 13631 | .addReg(SrcLoReg); |
| 13632 | BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(t4H) |
| 13633 | .addReg(SrcHiReg); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13634 | break; |
| 13635 | } |
| 13636 | case X86::ATOMNAND6432: { |
| 13637 | unsigned HiOpc, NOTOpc; |
| 13638 | unsigned LoOpc = getNonAtomic6432OpcodeWithExtraOpc(Opc, HiOpc, NOTOpc); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13639 | unsigned TmpL = MRI.createVirtualRegister(RC); |
| 13640 | unsigned TmpH = MRI.createVirtualRegister(RC); |
| 13641 | BuildMI(mainMBB, DL, TII->get(LoOpc), TmpL).addReg(SrcLoReg) |
| 13642 | .addReg(t4L); |
| 13643 | BuildMI(mainMBB, DL, TII->get(HiOpc), TmpH).addReg(SrcHiReg) |
| 13644 | .addReg(t4H); |
| 13645 | BuildMI(mainMBB, DL, TII->get(NOTOpc), t2L).addReg(TmpL); |
| 13646 | BuildMI(mainMBB, DL, TII->get(NOTOpc), t2H).addReg(TmpH); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13647 | break; |
| 13648 | } |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 13649 | case X86::ATOMMAX6432: |
| 13650 | case X86::ATOMMIN6432: |
| 13651 | case X86::ATOMUMAX6432: |
| 13652 | case X86::ATOMUMIN6432: { |
| 13653 | unsigned HiOpc; |
| 13654 | unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc); |
| 13655 | unsigned cL = MRI.createVirtualRegister(RC8); |
| 13656 | unsigned cH = MRI.createVirtualRegister(RC8); |
| 13657 | unsigned cL32 = MRI.createVirtualRegister(RC); |
| 13658 | unsigned cH32 = MRI.createVirtualRegister(RC); |
| 13659 | unsigned cc = MRI.createVirtualRegister(RC); |
| 13660 | // cl := cmp src_lo, lo |
| 13661 | BuildMI(mainMBB, DL, TII->get(X86::CMP32rr)) |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13662 | .addReg(SrcLoReg).addReg(t4L); |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 13663 | BuildMI(mainMBB, DL, TII->get(LoOpc), cL); |
| 13664 | BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cL32).addReg(cL); |
| 13665 | // ch := cmp src_hi, hi |
| 13666 | BuildMI(mainMBB, DL, TII->get(X86::CMP32rr)) |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13667 | .addReg(SrcHiReg).addReg(t4H); |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 13668 | BuildMI(mainMBB, DL, TII->get(HiOpc), cH); |
| 13669 | BuildMI(mainMBB, DL, TII->get(X86::MOVZX32rr8), cH32).addReg(cH); |
| 13670 | // cc := if (src_hi == hi) ? cl : ch; |
| 13671 | if (Subtarget->hasCMov()) { |
| 13672 | BuildMI(mainMBB, DL, TII->get(X86::CMOVE32rr), cc) |
| 13673 | .addReg(cH32).addReg(cL32); |
| 13674 | } else { |
| 13675 | MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), cc) |
| 13676 | .addReg(cH32).addReg(cL32) |
| 13677 | .addImm(X86::COND_E); |
| 13678 | mainMBB = EmitLoweredSelect(MIB, mainMBB); |
| 13679 | } |
| 13680 | BuildMI(mainMBB, DL, TII->get(X86::TEST32rr)).addReg(cc).addReg(cc); |
| 13681 | if (Subtarget->hasCMov()) { |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13682 | BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2L) |
| 13683 | .addReg(SrcLoReg).addReg(t4L); |
| 13684 | BuildMI(mainMBB, DL, TII->get(X86::CMOVNE32rr), t2H) |
| 13685 | .addReg(SrcHiReg).addReg(t4H); |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 13686 | } else { |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13687 | MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2L) |
| 13688 | .addReg(SrcLoReg).addReg(t4L) |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 13689 | .addImm(X86::COND_NE); |
| 13690 | mainMBB = EmitLoweredSelect(MIB, mainMBB); |
| Michael Liao | fe9dbe0 | 2013-03-07 01:01:29 +0000 | [diff] [blame] | 13691 | // As the lowered CMOV won't clobber EFLAGS, we could reuse it for the |
| 13692 | // 2nd CMOV lowering. |
| 13693 | mainMBB->addLiveIn(X86::EFLAGS); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13694 | MIB = BuildMI(mainMBB, DL, TII->get(X86::CMOV_GR32), t2H) |
| 13695 | .addReg(SrcHiReg).addReg(t4H) |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 13696 | .addImm(X86::COND_NE); |
| 13697 | mainMBB = EmitLoweredSelect(MIB, mainMBB); |
| Michael Liao | fe9dbe0 | 2013-03-07 01:01:29 +0000 | [diff] [blame] | 13698 | // Replace the original PHI node as mainMBB is changed after CMOV |
| 13699 | // lowering. |
| 13700 | BuildMI(*origMainMBB, PhiL, DL, TII->get(X86::PHI), t4L) |
| 13701 | .addReg(t1L).addMBB(thisMBB).addReg(t3L).addMBB(mainMBB); |
| 13702 | BuildMI(*origMainMBB, PhiH, DL, TII->get(X86::PHI), t4H) |
| 13703 | .addReg(t1H).addMBB(thisMBB).addReg(t3H).addMBB(mainMBB); |
| 13704 | PhiL->eraseFromParent(); |
| 13705 | PhiH->eraseFromParent(); |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 13706 | } |
| 13707 | break; |
| 13708 | } |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13709 | case X86::ATOMSWAP6432: { |
| 13710 | unsigned HiOpc; |
| 13711 | unsigned LoOpc = getNonAtomic6432Opcode(Opc, HiOpc); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13712 | BuildMI(mainMBB, DL, TII->get(LoOpc), t2L).addReg(SrcLoReg); |
| 13713 | BuildMI(mainMBB, DL, TII->get(HiOpc), t2H).addReg(SrcHiReg); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13714 | break; |
| 13715 | } |
| 13716 | } |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 13717 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13718 | // Copy EDX:EAX back from HiReg:LoReg |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13719 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EAX).addReg(t4L); |
| 13720 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EDX).addReg(t4H); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13721 | // Copy ECX:EBX from t1H:t1L |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13722 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::EBX).addReg(t2L); |
| 13723 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), X86::ECX).addReg(t2H); |
| Mon P Wang | ab3e747 | 2008-05-05 22:56:23 +0000 | [diff] [blame] | 13724 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13725 | MIB = BuildMI(mainMBB, DL, TII->get(LCMPXCHGOpc)); |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13726 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { |
| 13727 | MachineOperand NewMO = MI->getOperand(MemOpndSlot + i); |
| 13728 | if (NewMO.isReg()) |
| 13729 | NewMO.setIsKill(false); |
| 13730 | MIB.addOperand(NewMO); |
| 13731 | } |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13732 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 13733 | |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13734 | // Copy EDX:EAX back to t3H:t3L |
| 13735 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3L).addReg(X86::EAX); |
| 13736 | BuildMI(mainMBB, DL, TII->get(TargetOpcode::COPY), t3H).addReg(X86::EDX); |
| 13737 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13738 | BuildMI(mainMBB, DL, TII->get(X86::JNE_4)).addMBB(origMainMBB); |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 13739 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13740 | mainMBB->addSuccessor(origMainMBB); |
| 13741 | mainMBB->addSuccessor(sinkMBB); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 13742 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13743 | // sinkMBB: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13744 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, |
| 13745 | TII->get(TargetOpcode::COPY), DstLoReg) |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13746 | .addReg(t3L); |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13747 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, |
| 13748 | TII->get(TargetOpcode::COPY), DstHiReg) |
| Michael Liao | c537f79 | 2013-03-06 00:17:04 +0000 | [diff] [blame] | 13749 | .addReg(t3H); |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 13750 | |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 13751 | MI->eraseFromParent(); |
| 13752 | return sinkMBB; |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 13753 | } |
| 13754 | |
| Eric Christopher | f83a5de | 2009-08-27 18:08:16 +0000 | [diff] [blame] | 13755 | // FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8 |
| Bruno Cardoso Lopes | 98f9856 | 2010-07-30 19:54:33 +0000 | [diff] [blame] | 13756 | // or XMM0_V32I8 in AVX all of this code can be replaced with that |
| 13757 | // in the .td file. |
| Craig Topper | 8cb8c81 | 2012-11-10 09:02:47 +0000 | [diff] [blame] | 13758 | static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB, |
| 13759 | const TargetInstrInfo *TII) { |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 13760 | unsigned Opc; |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 13761 | switch (MI->getOpcode()) { |
| 13762 | default: llvm_unreachable("illegal opcode!"); |
| 13763 | case X86::PCMPISTRM128REG: Opc = X86::PCMPISTRM128rr; break; |
| 13764 | case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break; |
| 13765 | case X86::PCMPISTRM128MEM: Opc = X86::PCMPISTRM128rm; break; |
| 13766 | case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break; |
| 13767 | case X86::PCMPESTRM128REG: Opc = X86::PCMPESTRM128rr; break; |
| 13768 | case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break; |
| 13769 | case X86::PCMPESTRM128MEM: Opc = X86::PCMPESTRM128rm; break; |
| 13770 | case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break; |
| Bruno Cardoso Lopes | 98f9856 | 2010-07-30 19:54:33 +0000 | [diff] [blame] | 13771 | } |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 13772 | |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 13773 | DebugLoc dl = MI->getDebugLoc(); |
| Eric Christopher | 41c902f | 2010-11-30 08:20:21 +0000 | [diff] [blame] | 13774 | MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc)); |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 13775 | |
| Craig Topper | 52ea245 | 2012-11-10 09:25:36 +0000 | [diff] [blame] | 13776 | unsigned NumArgs = MI->getNumOperands(); |
| 13777 | for (unsigned i = 1; i < NumArgs; ++i) { |
| 13778 | MachineOperand &Op = MI->getOperand(i); |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 13779 | if (!(Op.isReg() && Op.isImplicit())) |
| 13780 | MIB.addOperand(Op); |
| 13781 | } |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 13782 | if (MI->hasOneMemOperand()) |
| Craig Topper | 9c7ae01 | 2012-11-10 01:23:36 +0000 | [diff] [blame] | 13783 | MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end()); |
| 13784 | |
| Bruno Cardoso Lopes | 5affa51 | 2011-08-31 03:04:09 +0000 | [diff] [blame] | 13785 | BuildMI(*BB, MI, dl, |
| Craig Topper | 638aa68 | 2012-08-05 00:17:48 +0000 | [diff] [blame] | 13786 | TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg()) |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 13787 | .addReg(X86::XMM0); |
| 13788 | |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 13789 | MI->eraseFromParent(); |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 13790 | return BB; |
| 13791 | } |
| 13792 | |
| Craig Topper | 9c7ae01 | 2012-11-10 01:23:36 +0000 | [diff] [blame] | 13793 | // FIXME: Custom handling because TableGen doesn't support multiple implicit |
| 13794 | // defs in an instruction pattern |
| Craig Topper | 8cb8c81 | 2012-11-10 09:02:47 +0000 | [diff] [blame] | 13795 | static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB, |
| 13796 | const TargetInstrInfo *TII) { |
| Craig Topper | 9c7ae01 | 2012-11-10 01:23:36 +0000 | [diff] [blame] | 13797 | unsigned Opc; |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 13798 | switch (MI->getOpcode()) { |
| 13799 | default: llvm_unreachable("illegal opcode!"); |
| 13800 | case X86::PCMPISTRIREG: Opc = X86::PCMPISTRIrr; break; |
| 13801 | case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break; |
| 13802 | case X86::PCMPISTRIMEM: Opc = X86::PCMPISTRIrm; break; |
| 13803 | case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break; |
| 13804 | case X86::PCMPESTRIREG: Opc = X86::PCMPESTRIrr; break; |
| 13805 | case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break; |
| 13806 | case X86::PCMPESTRIMEM: Opc = X86::PCMPESTRIrm; break; |
| 13807 | case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break; |
| Craig Topper | 9c7ae01 | 2012-11-10 01:23:36 +0000 | [diff] [blame] | 13808 | } |
| 13809 | |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 13810 | DebugLoc dl = MI->getDebugLoc(); |
| Craig Topper | 9c7ae01 | 2012-11-10 01:23:36 +0000 | [diff] [blame] | 13811 | MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc)); |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 13812 | |
| Craig Topper | 52ea245 | 2012-11-10 09:25:36 +0000 | [diff] [blame] | 13813 | unsigned NumArgs = MI->getNumOperands(); // remove the results |
| 13814 | for (unsigned i = 1; i < NumArgs; ++i) { |
| 13815 | MachineOperand &Op = MI->getOperand(i); |
| Craig Topper | 9c7ae01 | 2012-11-10 01:23:36 +0000 | [diff] [blame] | 13816 | if (!(Op.isReg() && Op.isImplicit())) |
| 13817 | MIB.addOperand(Op); |
| 13818 | } |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 13819 | if (MI->hasOneMemOperand()) |
| Craig Topper | 9c7ae01 | 2012-11-10 01:23:36 +0000 | [diff] [blame] | 13820 | MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end()); |
| 13821 | |
| 13822 | BuildMI(*BB, MI, dl, |
| 13823 | TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg()) |
| 13824 | .addReg(X86::ECX); |
| 13825 | |
| 13826 | MI->eraseFromParent(); |
| 13827 | return BB; |
| 13828 | } |
| 13829 | |
| Craig Topper | 2da3691 | 2012-11-11 22:45:02 +0000 | [diff] [blame] | 13830 | static MachineBasicBlock * EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB, |
| 13831 | const TargetInstrInfo *TII, |
| 13832 | const X86Subtarget* Subtarget) { |
| Eric Christopher | 228232b | 2010-11-30 07:20:12 +0000 | [diff] [blame] | 13833 | DebugLoc dl = MI->getDebugLoc(); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 13834 | |
| Eric Christopher | 228232b | 2010-11-30 07:20:12 +0000 | [diff] [blame] | 13835 | // Address into RAX/EAX, other two args into ECX, EDX. |
| 13836 | unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r; |
| 13837 | unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX; |
| 13838 | MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg); |
| 13839 | for (int i = 0; i < X86::AddrNumOperands; ++i) |
| Eric Christopher | 82be220 | 2010-11-30 08:10:28 +0000 | [diff] [blame] | 13840 | MIB.addOperand(MI->getOperand(i)); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 13841 | |
| Eric Christopher | 228232b | 2010-11-30 07:20:12 +0000 | [diff] [blame] | 13842 | unsigned ValOps = X86::AddrNumOperands; |
| 13843 | BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX) |
| 13844 | .addReg(MI->getOperand(ValOps).getReg()); |
| 13845 | BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX) |
| 13846 | .addReg(MI->getOperand(ValOps+1).getReg()); |
| 13847 | |
| 13848 | // The instruction doesn't actually take any operands though. |
| 13849 | BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr)); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 13850 | |
| Eric Christopher | 228232b | 2010-11-30 07:20:12 +0000 | [diff] [blame] | 13851 | MI->eraseFromParent(); // The pseudo is gone now. |
| 13852 | return BB; |
| 13853 | } |
| 13854 | |
| 13855 | MachineBasicBlock * |
| Dan Gohman | 320afb8 | 2010-10-12 18:00:49 +0000 | [diff] [blame] | 13856 | X86TargetLowering::EmitVAARG64WithCustomInserter( |
| 13857 | MachineInstr *MI, |
| 13858 | MachineBasicBlock *MBB) const { |
| 13859 | // Emit va_arg instruction on X86-64. |
| 13860 | |
| 13861 | // Operands to this pseudo-instruction: |
| 13862 | // 0 ) Output : destination address (reg) |
| 13863 | // 1-5) Input : va_list address (addr, i64mem) |
| 13864 | // 6 ) ArgSize : Size (in bytes) of vararg type |
| 13865 | // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset |
| 13866 | // 8 ) Align : Alignment of type |
| 13867 | // 9 ) EFLAGS (implicit-def) |
| 13868 | |
| 13869 | assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!"); |
| 13870 | assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands"); |
| 13871 | |
| 13872 | unsigned DestReg = MI->getOperand(0).getReg(); |
| 13873 | MachineOperand &Base = MI->getOperand(1); |
| 13874 | MachineOperand &Scale = MI->getOperand(2); |
| 13875 | MachineOperand &Index = MI->getOperand(3); |
| 13876 | MachineOperand &Disp = MI->getOperand(4); |
| 13877 | MachineOperand &Segment = MI->getOperand(5); |
| 13878 | unsigned ArgSize = MI->getOperand(6).getImm(); |
| 13879 | unsigned ArgMode = MI->getOperand(7).getImm(); |
| 13880 | unsigned Align = MI->getOperand(8).getImm(); |
| 13881 | |
| 13882 | // Memory Reference |
| 13883 | assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand"); |
| 13884 | MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin(); |
| 13885 | MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end(); |
| 13886 | |
| 13887 | // Machine Information |
| 13888 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 13889 | MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); |
| 13890 | const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64); |
| 13891 | const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32); |
| 13892 | DebugLoc DL = MI->getDebugLoc(); |
| 13893 | |
| 13894 | // struct va_list { |
| 13895 | // i32 gp_offset |
| 13896 | // i32 fp_offset |
| 13897 | // i64 overflow_area (address) |
| 13898 | // i64 reg_save_area (address) |
| 13899 | // } |
| 13900 | // sizeof(va_list) = 24 |
| 13901 | // alignment(va_list) = 8 |
| 13902 | |
| 13903 | unsigned TotalNumIntRegs = 6; |
| 13904 | unsigned TotalNumXMMRegs = 8; |
| 13905 | bool UseGPOffset = (ArgMode == 1); |
| 13906 | bool UseFPOffset = (ArgMode == 2); |
| 13907 | unsigned MaxOffset = TotalNumIntRegs * 8 + |
| 13908 | (UseFPOffset ? TotalNumXMMRegs * 16 : 0); |
| 13909 | |
| 13910 | /* Align ArgSize to a multiple of 8 */ |
| 13911 | unsigned ArgSizeA8 = (ArgSize + 7) & ~7; |
| 13912 | bool NeedsAlign = (Align > 8); |
| 13913 | |
| 13914 | MachineBasicBlock *thisMBB = MBB; |
| 13915 | MachineBasicBlock *overflowMBB; |
| 13916 | MachineBasicBlock *offsetMBB; |
| 13917 | MachineBasicBlock *endMBB; |
| 13918 | |
| 13919 | unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB |
| 13920 | unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB |
| 13921 | unsigned OffsetReg = 0; |
| 13922 | |
| 13923 | if (!UseGPOffset && !UseFPOffset) { |
| 13924 | // If we only pull from the overflow region, we don't create a branch. |
| 13925 | // We don't need to alter control flow. |
| 13926 | OffsetDestReg = 0; // unused |
| 13927 | OverflowDestReg = DestReg; |
| 13928 | |
| 13929 | offsetMBB = NULL; |
| 13930 | overflowMBB = thisMBB; |
| 13931 | endMBB = thisMBB; |
| 13932 | } else { |
| 13933 | // First emit code to check if gp_offset (or fp_offset) is below the bound. |
| 13934 | // If so, pull the argument from reg_save_area. (branch to offsetMBB) |
| 13935 | // If not, pull from overflow_area. (branch to overflowMBB) |
| 13936 | // |
| 13937 | // thisMBB |
| 13938 | // | . |
| 13939 | // | . |
| 13940 | // offsetMBB overflowMBB |
| 13941 | // | . |
| 13942 | // | . |
| 13943 | // endMBB |
| 13944 | |
| 13945 | // Registers for the PHI in endMBB |
| 13946 | OffsetDestReg = MRI.createVirtualRegister(AddrRegClass); |
| 13947 | OverflowDestReg = MRI.createVirtualRegister(AddrRegClass); |
| 13948 | |
| 13949 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
| 13950 | MachineFunction *MF = MBB->getParent(); |
| 13951 | overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 13952 | offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 13953 | endMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 13954 | |
| 13955 | MachineFunction::iterator MBBIter = MBB; |
| 13956 | ++MBBIter; |
| 13957 | |
| 13958 | // Insert the new basic blocks |
| 13959 | MF->insert(MBBIter, offsetMBB); |
| 13960 | MF->insert(MBBIter, overflowMBB); |
| 13961 | MF->insert(MBBIter, endMBB); |
| 13962 | |
| 13963 | // Transfer the remainder of MBB and its successor edges to endMBB. |
| 13964 | endMBB->splice(endMBB->begin(), thisMBB, |
| 13965 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 13966 | thisMBB->end()); |
| 13967 | endMBB->transferSuccessorsAndUpdatePHIs(thisMBB); |
| 13968 | |
| 13969 | // Make offsetMBB and overflowMBB successors of thisMBB |
| 13970 | thisMBB->addSuccessor(offsetMBB); |
| 13971 | thisMBB->addSuccessor(overflowMBB); |
| 13972 | |
| 13973 | // endMBB is a successor of both offsetMBB and overflowMBB |
| 13974 | offsetMBB->addSuccessor(endMBB); |
| 13975 | overflowMBB->addSuccessor(endMBB); |
| 13976 | |
| 13977 | // Load the offset value into a register |
| 13978 | OffsetReg = MRI.createVirtualRegister(OffsetRegClass); |
| 13979 | BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg) |
| 13980 | .addOperand(Base) |
| 13981 | .addOperand(Scale) |
| 13982 | .addOperand(Index) |
| 13983 | .addDisp(Disp, UseFPOffset ? 4 : 0) |
| 13984 | .addOperand(Segment) |
| 13985 | .setMemRefs(MMOBegin, MMOEnd); |
| 13986 | |
| 13987 | // Check if there is enough room left to pull this argument. |
| 13988 | BuildMI(thisMBB, DL, TII->get(X86::CMP32ri)) |
| 13989 | .addReg(OffsetReg) |
| 13990 | .addImm(MaxOffset + 8 - ArgSizeA8); |
| 13991 | |
| 13992 | // Branch to "overflowMBB" if offset >= max |
| 13993 | // Fall through to "offsetMBB" otherwise |
| 13994 | BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE))) |
| 13995 | .addMBB(overflowMBB); |
| 13996 | } |
| 13997 | |
| 13998 | // In offsetMBB, emit code to use the reg_save_area. |
| 13999 | if (offsetMBB) { |
| 14000 | assert(OffsetReg != 0); |
| 14001 | |
| 14002 | // Read the reg_save_area address. |
| 14003 | unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass); |
| 14004 | BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg) |
| 14005 | .addOperand(Base) |
| 14006 | .addOperand(Scale) |
| 14007 | .addOperand(Index) |
| 14008 | .addDisp(Disp, 16) |
| 14009 | .addOperand(Segment) |
| 14010 | .setMemRefs(MMOBegin, MMOEnd); |
| 14011 | |
| 14012 | // Zero-extend the offset |
| 14013 | unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass); |
| 14014 | BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64) |
| 14015 | .addImm(0) |
| 14016 | .addReg(OffsetReg) |
| 14017 | .addImm(X86::sub_32bit); |
| 14018 | |
| 14019 | // Add the offset to the reg_save_area to get the final address. |
| 14020 | BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg) |
| 14021 | .addReg(OffsetReg64) |
| 14022 | .addReg(RegSaveReg); |
| 14023 | |
| 14024 | // Compute the offset for the next argument |
| 14025 | unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass); |
| 14026 | BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg) |
| 14027 | .addReg(OffsetReg) |
| 14028 | .addImm(UseFPOffset ? 16 : 8); |
| 14029 | |
| 14030 | // Store it back into the va_list. |
| 14031 | BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr)) |
| 14032 | .addOperand(Base) |
| 14033 | .addOperand(Scale) |
| 14034 | .addOperand(Index) |
| 14035 | .addDisp(Disp, UseFPOffset ? 4 : 0) |
| 14036 | .addOperand(Segment) |
| 14037 | .addReg(NextOffsetReg) |
| 14038 | .setMemRefs(MMOBegin, MMOEnd); |
| 14039 | |
| 14040 | // Jump to endMBB |
| 14041 | BuildMI(offsetMBB, DL, TII->get(X86::JMP_4)) |
| 14042 | .addMBB(endMBB); |
| 14043 | } |
| 14044 | |
| 14045 | // |
| 14046 | // Emit code to use overflow area |
| 14047 | // |
| 14048 | |
| 14049 | // Load the overflow_area address into a register. |
| 14050 | unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass); |
| 14051 | BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg) |
| 14052 | .addOperand(Base) |
| 14053 | .addOperand(Scale) |
| 14054 | .addOperand(Index) |
| 14055 | .addDisp(Disp, 8) |
| 14056 | .addOperand(Segment) |
| 14057 | .setMemRefs(MMOBegin, MMOEnd); |
| 14058 | |
| 14059 | // If we need to align it, do so. Otherwise, just copy the address |
| 14060 | // to OverflowDestReg. |
| 14061 | if (NeedsAlign) { |
| 14062 | // Align the overflow address |
| 14063 | assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2"); |
| 14064 | unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass); |
| 14065 | |
| 14066 | // aligned_addr = (addr + (align-1)) & ~(align-1) |
| 14067 | BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg) |
| 14068 | .addReg(OverflowAddrReg) |
| 14069 | .addImm(Align-1); |
| 14070 | |
| 14071 | BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg) |
| 14072 | .addReg(TmpReg) |
| 14073 | .addImm(~(uint64_t)(Align-1)); |
| 14074 | } else { |
| 14075 | BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg) |
| 14076 | .addReg(OverflowAddrReg); |
| 14077 | } |
| 14078 | |
| 14079 | // Compute the next overflow address after this argument. |
| 14080 | // (the overflow address should be kept 8-byte aligned) |
| 14081 | unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass); |
| 14082 | BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg) |
| 14083 | .addReg(OverflowDestReg) |
| 14084 | .addImm(ArgSizeA8); |
| 14085 | |
| 14086 | // Store the new overflow address. |
| 14087 | BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr)) |
| 14088 | .addOperand(Base) |
| 14089 | .addOperand(Scale) |
| 14090 | .addOperand(Index) |
| 14091 | .addDisp(Disp, 8) |
| 14092 | .addOperand(Segment) |
| 14093 | .addReg(NextAddrReg) |
| 14094 | .setMemRefs(MMOBegin, MMOEnd); |
| 14095 | |
| 14096 | // If we branched, emit the PHI to the front of endMBB. |
| 14097 | if (offsetMBB) { |
| 14098 | BuildMI(*endMBB, endMBB->begin(), DL, |
| 14099 | TII->get(X86::PHI), DestReg) |
| 14100 | .addReg(OffsetDestReg).addMBB(offsetMBB) |
| 14101 | .addReg(OverflowDestReg).addMBB(overflowMBB); |
| 14102 | } |
| 14103 | |
| 14104 | // Erase the pseudo instruction |
| 14105 | MI->eraseFromParent(); |
| 14106 | |
| 14107 | return endMBB; |
| 14108 | } |
| 14109 | |
| 14110 | MachineBasicBlock * |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 14111 | X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter( |
| 14112 | MachineInstr *MI, |
| 14113 | MachineBasicBlock *MBB) const { |
| 14114 | // Emit code to save XMM registers to the stack. The ABI says that the |
| 14115 | // number of registers to save is given in %al, so it's theoretically |
| 14116 | // possible to do an indirect jump trick to avoid saving all of them, |
| 14117 | // however this code takes a simpler approach and just executes all |
| 14118 | // of the stores if %al is non-zero. It's less code, and it's probably |
| 14119 | // easier on the hardware branch predictor, and stores aren't all that |
| 14120 | // expensive anyway. |
| 14121 | |
| 14122 | // Create the new basic blocks. One block contains all the XMM stores, |
| 14123 | // and one block is the final destination regardless of whether any |
| 14124 | // stores were performed. |
| 14125 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); |
| 14126 | MachineFunction *F = MBB->getParent(); |
| 14127 | MachineFunction::iterator MBBIter = MBB; |
| 14128 | ++MBBIter; |
| 14129 | MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 14130 | MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 14131 | F->insert(MBBIter, XMMSaveMBB); |
| 14132 | F->insert(MBBIter, EndMBB); |
| 14133 | |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14134 | // Transfer the remainder of MBB and its successor edges to EndMBB. |
| 14135 | EndMBB->splice(EndMBB->begin(), MBB, |
| 14136 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 14137 | MBB->end()); |
| 14138 | EndMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 14139 | |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 14140 | // The original block will now fall through to the XMM save block. |
| 14141 | MBB->addSuccessor(XMMSaveMBB); |
| 14142 | // The XMMSaveMBB will fall through to the end block. |
| 14143 | XMMSaveMBB->addSuccessor(EndMBB); |
| 14144 | |
| 14145 | // Now add the instructions. |
| 14146 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 14147 | DebugLoc DL = MI->getDebugLoc(); |
| 14148 | |
| 14149 | unsigned CountReg = MI->getOperand(0).getReg(); |
| 14150 | int64_t RegSaveFrameIndex = MI->getOperand(1).getImm(); |
| 14151 | int64_t VarArgsFPOffset = MI->getOperand(2).getImm(); |
| 14152 | |
| 14153 | if (!Subtarget->isTargetWin64()) { |
| 14154 | // If %al is 0, branch around the XMM save block. |
| 14155 | BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg); |
| Chris Lattner | bd13fb6 | 2010-02-11 19:25:55 +0000 | [diff] [blame] | 14156 | BuildMI(MBB, DL, TII->get(X86::JE_4)).addMBB(EndMBB); |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 14157 | MBB->addSuccessor(EndMBB); |
| 14158 | } |
| 14159 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 14160 | unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr; |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 14161 | // In the XMM save block, save all the XMM argument registers. |
| 14162 | for (int i = 3, e = MI->getNumOperands(); i != e; ++i) { |
| 14163 | int64_t Offset = (i - 3) * 16 + VarArgsFPOffset; |
| Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 14164 | MachineMemOperand *MMO = |
| Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 14165 | F->getMachineMemOperand( |
| Chris Lattner | e863903 | 2010-09-21 06:22:23 +0000 | [diff] [blame] | 14166 | MachinePointerInfo::getFixedStack(RegSaveFrameIndex, Offset), |
| Chris Lattner | 59db549 | 2010-09-21 04:39:43 +0000 | [diff] [blame] | 14167 | MachineMemOperand::MOStore, |
| Evan Cheng | ff89dcb | 2009-10-18 18:16:27 +0000 | [diff] [blame] | 14168 | /*Size=*/16, /*Align=*/16); |
| Bruno Cardoso Lopes | 5affa51 | 2011-08-31 03:04:09 +0000 | [diff] [blame] | 14169 | BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc)) |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 14170 | .addFrameIndex(RegSaveFrameIndex) |
| 14171 | .addImm(/*Scale=*/1) |
| 14172 | .addReg(/*IndexReg=*/0) |
| 14173 | .addImm(/*Disp=*/Offset) |
| 14174 | .addReg(/*Segment=*/0) |
| 14175 | .addReg(MI->getOperand(i).getReg()) |
| Dan Gohman | c76909a | 2009-09-25 20:36:54 +0000 | [diff] [blame] | 14176 | .addMemOperand(MMO); |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 14177 | } |
| 14178 | |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14179 | MI->eraseFromParent(); // The pseudo instruction is gone now. |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 14180 | |
| 14181 | return EndMBB; |
| 14182 | } |
| Mon P Wang | 63307c3 | 2008-05-05 19:05:59 +0000 | [diff] [blame] | 14183 | |
| Lang Hames | 6e3f7e4 | 2012-02-03 01:13:49 +0000 | [diff] [blame] | 14184 | // The EFLAGS operand of SelectItr might be missing a kill marker |
| 14185 | // because there were multiple uses of EFLAGS, and ISel didn't know |
| 14186 | // which to mark. Figure out whether SelectItr should have had a |
| 14187 | // kill marker, and set it if it should. Returns the correct kill |
| 14188 | // marker value. |
| 14189 | static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr, |
| 14190 | MachineBasicBlock* BB, |
| 14191 | const TargetRegisterInfo* TRI) { |
| 14192 | // Scan forward through BB for a use/def of EFLAGS. |
| 14193 | MachineBasicBlock::iterator miI(llvm::next(SelectItr)); |
| 14194 | for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) { |
| Lang Hames | 50a36f7 | 2012-02-02 07:48:37 +0000 | [diff] [blame] | 14195 | const MachineInstr& mi = *miI; |
| Lang Hames | 6e3f7e4 | 2012-02-03 01:13:49 +0000 | [diff] [blame] | 14196 | if (mi.readsRegister(X86::EFLAGS)) |
| Lang Hames | 50a36f7 | 2012-02-02 07:48:37 +0000 | [diff] [blame] | 14197 | return false; |
| Lang Hames | 6e3f7e4 | 2012-02-03 01:13:49 +0000 | [diff] [blame] | 14198 | if (mi.definesRegister(X86::EFLAGS)) |
| 14199 | break; // Should have kill-flag - update below. |
| 14200 | } |
| 14201 | |
| 14202 | // If we hit the end of the block, check whether EFLAGS is live into a |
| 14203 | // successor. |
| 14204 | if (miI == BB->end()) { |
| 14205 | for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(), |
| 14206 | sEnd = BB->succ_end(); |
| 14207 | sItr != sEnd; ++sItr) { |
| 14208 | MachineBasicBlock* succ = *sItr; |
| 14209 | if (succ->isLiveIn(X86::EFLAGS)) |
| 14210 | return false; |
| Lang Hames | 50a36f7 | 2012-02-02 07:48:37 +0000 | [diff] [blame] | 14211 | } |
| 14212 | } |
| 14213 | |
| Lang Hames | 6e3f7e4 | 2012-02-03 01:13:49 +0000 | [diff] [blame] | 14214 | // We found a def, or hit the end of the basic block and EFLAGS wasn't live |
| 14215 | // out. SelectMI should have a kill flag on EFLAGS. |
| 14216 | SelectItr->addRegisterKilled(X86::EFLAGS, TRI); |
| Lang Hames | 50a36f7 | 2012-02-02 07:48:37 +0000 | [diff] [blame] | 14217 | return true; |
| 14218 | } |
| 14219 | |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14220 | MachineBasicBlock * |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14221 | X86TargetLowering::EmitLoweredSelect(MachineInstr *MI, |
| Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 14222 | MachineBasicBlock *BB) const { |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14223 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 14224 | DebugLoc DL = MI->getDebugLoc(); |
| Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 14225 | |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14226 | // To "insert" a SELECT_CC instruction, we actually have to insert the |
| 14227 | // diamond control-flow pattern. The incoming instruction knows the |
| 14228 | // destination vreg to set, the condition code register to branch on, the |
| 14229 | // true/false values to select between, and a branch opcode to use. |
| 14230 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 14231 | MachineFunction::iterator It = BB; |
| 14232 | ++It; |
| Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 14233 | |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14234 | // thisMBB: |
| 14235 | // ... |
| 14236 | // TrueVal = ... |
| 14237 | // cmpTY ccX, r1, r2 |
| 14238 | // bCC copy1MBB |
| 14239 | // fallthrough --> copy0MBB |
| 14240 | MachineBasicBlock *thisMBB = BB; |
| 14241 | MachineFunction *F = BB->getParent(); |
| 14242 | MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); |
| 14243 | MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14244 | F->insert(It, copy0MBB); |
| 14245 | F->insert(It, sinkMBB); |
| Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 14246 | |
| Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 14247 | // If the EFLAGS register isn't dead in the terminator, then claim that it's |
| 14248 | // live into the sink and copy blocks. |
| Lang Hames | 6e3f7e4 | 2012-02-03 01:13:49 +0000 | [diff] [blame] | 14249 | const TargetRegisterInfo* TRI = getTargetMachine().getRegisterInfo(); |
| 14250 | if (!MI->killsRegister(X86::EFLAGS) && |
| 14251 | !checkAndUpdateEFLAGSKill(MI, BB, TRI)) { |
| 14252 | copy0MBB->addLiveIn(X86::EFLAGS); |
| 14253 | sinkMBB->addLiveIn(X86::EFLAGS); |
| Bill Wendling | 730c07e | 2010-06-25 20:48:10 +0000 | [diff] [blame] | 14254 | } |
| 14255 | |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14256 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 14257 | sinkMBB->splice(sinkMBB->begin(), BB, |
| 14258 | llvm::next(MachineBasicBlock::iterator(MI)), |
| 14259 | BB->end()); |
| 14260 | sinkMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 14261 | |
| 14262 | // Add the true and fallthrough blocks as its successors. |
| 14263 | BB->addSuccessor(copy0MBB); |
| 14264 | BB->addSuccessor(sinkMBB); |
| 14265 | |
| 14266 | // Create the conditional branch instruction. |
| 14267 | unsigned Opc = |
| 14268 | X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm()); |
| 14269 | BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB); |
| 14270 | |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14271 | // copy0MBB: |
| 14272 | // %FalseValue = ... |
| 14273 | // # fallthrough to sinkMBB |
| Dan Gohman | 3335a22 | 2010-04-30 20:14:26 +0000 | [diff] [blame] | 14274 | copy0MBB->addSuccessor(sinkMBB); |
| Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 14275 | |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14276 | // sinkMBB: |
| 14277 | // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] |
| 14278 | // ... |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14279 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, |
| 14280 | TII->get(X86::PHI), MI->getOperand(0).getReg()) |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14281 | .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB) |
| 14282 | .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB); |
| 14283 | |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14284 | MI->eraseFromParent(); // The pseudo instruction is gone now. |
| Dan Gohman | 3335a22 | 2010-04-30 20:14:26 +0000 | [diff] [blame] | 14285 | return sinkMBB; |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14286 | } |
| 14287 | |
| Anton Korobeynikov | 043f3c2 | 2010-03-06 19:32:29 +0000 | [diff] [blame] | 14288 | MachineBasicBlock * |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14289 | X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI, MachineBasicBlock *BB, |
| 14290 | bool Is64Bit) const { |
| 14291 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 14292 | DebugLoc DL = MI->getDebugLoc(); |
| 14293 | MachineFunction *MF = BB->getParent(); |
| 14294 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); |
| 14295 | |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 14296 | assert(getTargetMachine().Options.EnableSegmentedStacks); |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14297 | |
| 14298 | unsigned TlsReg = Is64Bit ? X86::FS : X86::GS; |
| 14299 | unsigned TlsOffset = Is64Bit ? 0x70 : 0x30; |
| 14300 | |
| 14301 | // BB: |
| 14302 | // ... [Till the alloca] |
| 14303 | // If stacklet is not large enough, jump to mallocMBB |
| 14304 | // |
| 14305 | // bumpMBB: |
| 14306 | // Allocate by subtracting from RSP |
| 14307 | // Jump to continueMBB |
| 14308 | // |
| 14309 | // mallocMBB: |
| 14310 | // Allocate by call to runtime |
| 14311 | // |
| 14312 | // continueMBB: |
| 14313 | // ... |
| 14314 | // [rest of original BB] |
| 14315 | // |
| 14316 | |
| 14317 | MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 14318 | MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 14319 | MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB); |
| 14320 | |
| 14321 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 14322 | const TargetRegisterClass *AddrRegClass = |
| 14323 | getRegClassFor(Is64Bit ? MVT::i64:MVT::i32); |
| 14324 | |
| 14325 | unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass), |
| 14326 | bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass), |
| 14327 | tmpSPVReg = MRI.createVirtualRegister(AddrRegClass), |
| Rafael Espindola | 66bf743 | 2011-10-26 21:16:41 +0000 | [diff] [blame] | 14328 | SPLimitVReg = MRI.createVirtualRegister(AddrRegClass), |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14329 | sizeVReg = MI->getOperand(1).getReg(), |
| 14330 | physSPReg = Is64Bit ? X86::RSP : X86::ESP; |
| 14331 | |
| 14332 | MachineFunction::iterator MBBIter = BB; |
| 14333 | ++MBBIter; |
| 14334 | |
| 14335 | MF->insert(MBBIter, bumpMBB); |
| 14336 | MF->insert(MBBIter, mallocMBB); |
| 14337 | MF->insert(MBBIter, continueMBB); |
| 14338 | |
| 14339 | continueMBB->splice(continueMBB->begin(), BB, llvm::next |
| 14340 | (MachineBasicBlock::iterator(MI)), BB->end()); |
| 14341 | continueMBB->transferSuccessorsAndUpdatePHIs(BB); |
| 14342 | |
| 14343 | // Add code to the main basic block to check if the stack limit has been hit, |
| 14344 | // and if so, jump to mallocMBB otherwise to bumpMBB. |
| 14345 | BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg); |
| Rafael Espindola | 66bf743 | 2011-10-26 21:16:41 +0000 | [diff] [blame] | 14346 | BuildMI(BB, DL, TII->get(Is64Bit ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg) |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14347 | .addReg(tmpSPVReg).addReg(sizeVReg); |
| 14348 | BuildMI(BB, DL, TII->get(Is64Bit ? X86::CMP64mr:X86::CMP32mr)) |
| Rafael Espindola | 014f7a3 | 2012-01-11 18:14:03 +0000 | [diff] [blame] | 14349 | .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg) |
| Rafael Espindola | 66bf743 | 2011-10-26 21:16:41 +0000 | [diff] [blame] | 14350 | .addReg(SPLimitVReg); |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14351 | BuildMI(BB, DL, TII->get(X86::JG_4)).addMBB(mallocMBB); |
| 14352 | |
| 14353 | // bumpMBB simply decreases the stack pointer, since we know the current |
| 14354 | // stacklet has enough space. |
| 14355 | BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg) |
| Rafael Espindola | 66bf743 | 2011-10-26 21:16:41 +0000 | [diff] [blame] | 14356 | .addReg(SPLimitVReg); |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14357 | BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg) |
| Rafael Espindola | 66bf743 | 2011-10-26 21:16:41 +0000 | [diff] [blame] | 14358 | .addReg(SPLimitVReg); |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14359 | BuildMI(bumpMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB); |
| 14360 | |
| 14361 | // Calls into a routine in libgcc to allocate more space from the heap. |
| Jakob Stoklund Olesen | 8bcde2a | 2012-02-16 00:02:50 +0000 | [diff] [blame] | 14362 | const uint32_t *RegMask = |
| 14363 | getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C); |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14364 | if (Is64Bit) { |
| 14365 | BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI) |
| 14366 | .addReg(sizeVReg); |
| 14367 | BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32)) |
| Jakob Stoklund Olesen | 85dccf1 | 2012-07-04 23:53:27 +0000 | [diff] [blame] | 14368 | .addExternalSymbol("__morestack_allocate_stack_space") |
| Jakob Stoklund Olesen | 8bcde2a | 2012-02-16 00:02:50 +0000 | [diff] [blame] | 14369 | .addRegMask(RegMask) |
| Jakob Stoklund Olesen | 85dccf1 | 2012-07-04 23:53:27 +0000 | [diff] [blame] | 14370 | .addReg(X86::RDI, RegState::Implicit) |
| Jakob Stoklund Olesen | 8bcde2a | 2012-02-16 00:02:50 +0000 | [diff] [blame] | 14371 | .addReg(X86::RAX, RegState::ImplicitDefine); |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14372 | } else { |
| 14373 | BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg) |
| 14374 | .addImm(12); |
| 14375 | BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg); |
| 14376 | BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32)) |
| Jakob Stoklund Olesen | 8bcde2a | 2012-02-16 00:02:50 +0000 | [diff] [blame] | 14377 | .addExternalSymbol("__morestack_allocate_stack_space") |
| 14378 | .addRegMask(RegMask) |
| 14379 | .addReg(X86::EAX, RegState::ImplicitDefine); |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14380 | } |
| 14381 | |
| 14382 | if (!Is64Bit) |
| 14383 | BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg) |
| 14384 | .addImm(16); |
| 14385 | |
| 14386 | BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg) |
| 14387 | .addReg(Is64Bit ? X86::RAX : X86::EAX); |
| 14388 | BuildMI(mallocMBB, DL, TII->get(X86::JMP_4)).addMBB(continueMBB); |
| 14389 | |
| 14390 | // Set up the CFG correctly. |
| 14391 | BB->addSuccessor(bumpMBB); |
| 14392 | BB->addSuccessor(mallocMBB); |
| 14393 | mallocMBB->addSuccessor(continueMBB); |
| 14394 | bumpMBB->addSuccessor(continueMBB); |
| 14395 | |
| 14396 | // Take care of the PHI nodes. |
| 14397 | BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI), |
| 14398 | MI->getOperand(0).getReg()) |
| 14399 | .addReg(mallocPtrVReg).addMBB(mallocMBB) |
| 14400 | .addReg(bumpSPPtrVReg).addMBB(bumpMBB); |
| 14401 | |
| 14402 | // Delete the original pseudo instruction. |
| 14403 | MI->eraseFromParent(); |
| 14404 | |
| 14405 | // And we're done. |
| 14406 | return continueMBB; |
| 14407 | } |
| 14408 | |
| 14409 | MachineBasicBlock * |
| Michael J. Spencer | e9c253e | 2010-10-21 01:41:01 +0000 | [diff] [blame] | 14410 | X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI, |
| Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 14411 | MachineBasicBlock *BB) const { |
| Anton Korobeynikov | 043f3c2 | 2010-03-06 19:32:29 +0000 | [diff] [blame] | 14412 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 14413 | DebugLoc DL = MI->getDebugLoc(); |
| Anton Korobeynikov | 043f3c2 | 2010-03-06 19:32:29 +0000 | [diff] [blame] | 14414 | |
| NAKAMURA Takumi | a2e0762 | 2011-03-24 07:07:00 +0000 | [diff] [blame] | 14415 | assert(!Subtarget->isTargetEnvMacho()); |
| 14416 | |
| Anton Korobeynikov | 043f3c2 | 2010-03-06 19:32:29 +0000 | [diff] [blame] | 14417 | // The lowering is pretty easy: we're just emitting the call to _alloca. The |
| 14418 | // non-trivial part is impdef of ESP. |
| Anton Korobeynikov | 043f3c2 | 2010-03-06 19:32:29 +0000 | [diff] [blame] | 14419 | |
| NAKAMURA Takumi | a2e0762 | 2011-03-24 07:07:00 +0000 | [diff] [blame] | 14420 | if (Subtarget->isTargetWin64()) { |
| 14421 | if (Subtarget->isTargetCygMing()) { |
| 14422 | // ___chkstk(Mingw64): |
| 14423 | // Clobbers R10, R11, RAX and EFLAGS. |
| 14424 | // Updates RSP. |
| 14425 | BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA)) |
| 14426 | .addExternalSymbol("___chkstk") |
| 14427 | .addReg(X86::RAX, RegState::Implicit) |
| 14428 | .addReg(X86::RSP, RegState::Implicit) |
| 14429 | .addReg(X86::RAX, RegState::Define | RegState::Implicit) |
| 14430 | .addReg(X86::RSP, RegState::Define | RegState::Implicit) |
| 14431 | .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit); |
| 14432 | } else { |
| 14433 | // __chkstk(MSVCRT): does not update stack pointer. |
| 14434 | // Clobbers R10, R11 and EFLAGS. |
| NAKAMURA Takumi | a2e0762 | 2011-03-24 07:07:00 +0000 | [diff] [blame] | 14435 | BuildMI(*BB, MI, DL, TII->get(X86::W64ALLOCA)) |
| 14436 | .addExternalSymbol("__chkstk") |
| 14437 | .addReg(X86::RAX, RegState::Implicit) |
| 14438 | .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit); |
| Nico Rieck | 4010110 | 2013-07-08 11:20:11 +0000 | [diff] [blame^] | 14439 | // RAX has the offset to be subtracted from RSP. |
| NAKAMURA Takumi | a2e0762 | 2011-03-24 07:07:00 +0000 | [diff] [blame] | 14440 | BuildMI(*BB, MI, DL, TII->get(X86::SUB64rr), X86::RSP) |
| 14441 | .addReg(X86::RSP) |
| 14442 | .addReg(X86::RAX); |
| 14443 | } |
| 14444 | } else { |
| 14445 | const char *StackProbeSymbol = |
| Michael J. Spencer | e9c253e | 2010-10-21 01:41:01 +0000 | [diff] [blame] | 14446 | Subtarget->isTargetWindows() ? "_chkstk" : "_alloca"; |
| 14447 | |
| NAKAMURA Takumi | a2e0762 | 2011-03-24 07:07:00 +0000 | [diff] [blame] | 14448 | BuildMI(*BB, MI, DL, TII->get(X86::CALLpcrel32)) |
| 14449 | .addExternalSymbol(StackProbeSymbol) |
| 14450 | .addReg(X86::EAX, RegState::Implicit) |
| 14451 | .addReg(X86::ESP, RegState::Implicit) |
| 14452 | .addReg(X86::EAX, RegState::Define | RegState::Implicit) |
| 14453 | .addReg(X86::ESP, RegState::Define | RegState::Implicit) |
| 14454 | .addReg(X86::EFLAGS, RegState::Define | RegState::Implicit); |
| 14455 | } |
| Anton Korobeynikov | 043f3c2 | 2010-03-06 19:32:29 +0000 | [diff] [blame] | 14456 | |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14457 | MI->eraseFromParent(); // The pseudo instruction is gone now. |
| Anton Korobeynikov | 043f3c2 | 2010-03-06 19:32:29 +0000 | [diff] [blame] | 14458 | return BB; |
| 14459 | } |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14460 | |
| 14461 | MachineBasicBlock * |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 14462 | X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI, |
| 14463 | MachineBasicBlock *BB) const { |
| 14464 | // This is pretty easy. We're taking the value that we received from |
| 14465 | // our load from the relocation, sticking it in either RDI (x86-64) |
| 14466 | // or EAX and doing an indirect call. The return value will then |
| 14467 | // be in the normal return register. |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 14468 | const X86InstrInfo *TII |
| Eric Christopher | 5441536 | 2010-06-08 22:04:25 +0000 | [diff] [blame] | 14469 | = static_cast<const X86InstrInfo*>(getTargetMachine().getInstrInfo()); |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 14470 | DebugLoc DL = MI->getDebugLoc(); |
| 14471 | MachineFunction *F = BB->getParent(); |
| Eric Christopher | 722d315 | 2010-09-27 06:01:51 +0000 | [diff] [blame] | 14472 | |
| 14473 | assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?"); |
| Eric Christopher | 5441536 | 2010-06-08 22:04:25 +0000 | [diff] [blame] | 14474 | assert(MI->getOperand(3).isGlobal() && "This should be a global"); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 14475 | |
| Jakob Stoklund Olesen | 8bcde2a | 2012-02-16 00:02:50 +0000 | [diff] [blame] | 14476 | // Get a register mask for the lowered call. |
| 14477 | // FIXME: The 32-bit calls have non-standard calling conventions. Use a |
| 14478 | // proper register mask. |
| 14479 | const uint32_t *RegMask = |
| 14480 | getTargetMachine().getRegisterInfo()->getCallPreservedMask(CallingConv::C); |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 14481 | if (Subtarget->is64Bit()) { |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14482 | MachineInstrBuilder MIB = BuildMI(*BB, MI, DL, |
| 14483 | TII->get(X86::MOV64rm), X86::RDI) |
| Eric Christopher | 5441536 | 2010-06-08 22:04:25 +0000 | [diff] [blame] | 14484 | .addReg(X86::RIP) |
| 14485 | .addImm(0).addReg(0) |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 14486 | .addGlobalAddress(MI->getOperand(3).getGlobal(), 0, |
| Eric Christopher | 5441536 | 2010-06-08 22:04:25 +0000 | [diff] [blame] | 14487 | MI->getOperand(3).getTargetFlags()) |
| 14488 | .addReg(0); |
| Eric Christopher | 722d315 | 2010-09-27 06:01:51 +0000 | [diff] [blame] | 14489 | MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m)); |
| Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 14490 | addDirectMem(MIB, X86::RDI); |
| Jakob Stoklund Olesen | 8bcde2a | 2012-02-16 00:02:50 +0000 | [diff] [blame] | 14491 | MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask); |
| Eric Christopher | 6102549 | 2010-06-15 23:08:42 +0000 | [diff] [blame] | 14492 | } else if (getTargetMachine().getRelocationModel() != Reloc::PIC_) { |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14493 | MachineInstrBuilder MIB = BuildMI(*BB, MI, DL, |
| 14494 | TII->get(X86::MOV32rm), X86::EAX) |
| Eric Christopher | 6102549 | 2010-06-15 23:08:42 +0000 | [diff] [blame] | 14495 | .addReg(0) |
| 14496 | .addImm(0).addReg(0) |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 14497 | .addGlobalAddress(MI->getOperand(3).getGlobal(), 0, |
| Eric Christopher | 6102549 | 2010-06-15 23:08:42 +0000 | [diff] [blame] | 14498 | MI->getOperand(3).getTargetFlags()) |
| 14499 | .addReg(0); |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14500 | MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m)); |
| Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 14501 | addDirectMem(MIB, X86::EAX); |
| Jakob Stoklund Olesen | 8bcde2a | 2012-02-16 00:02:50 +0000 | [diff] [blame] | 14502 | MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask); |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 14503 | } else { |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14504 | MachineInstrBuilder MIB = BuildMI(*BB, MI, DL, |
| 14505 | TII->get(X86::MOV32rm), X86::EAX) |
| Eric Christopher | 5441536 | 2010-06-08 22:04:25 +0000 | [diff] [blame] | 14506 | .addReg(TII->getGlobalBaseReg(F)) |
| 14507 | .addImm(0).addReg(0) |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 14508 | .addGlobalAddress(MI->getOperand(3).getGlobal(), 0, |
| Eric Christopher | 5441536 | 2010-06-08 22:04:25 +0000 | [diff] [blame] | 14509 | MI->getOperand(3).getTargetFlags()) |
| 14510 | .addReg(0); |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14511 | MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m)); |
| Chris Lattner | 599b531 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 14512 | addDirectMem(MIB, X86::EAX); |
| Jakob Stoklund Olesen | 8bcde2a | 2012-02-16 00:02:50 +0000 | [diff] [blame] | 14513 | MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask); |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 14514 | } |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 14515 | |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14516 | MI->eraseFromParent(); // The pseudo instruction is gone now. |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 14517 | return BB; |
| 14518 | } |
| 14519 | |
| 14520 | MachineBasicBlock * |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14521 | X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI, |
| 14522 | MachineBasicBlock *MBB) const { |
| 14523 | DebugLoc DL = MI->getDebugLoc(); |
| 14524 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 14525 | |
| 14526 | MachineFunction *MF = MBB->getParent(); |
| 14527 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 14528 | |
| 14529 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 14530 | MachineFunction::iterator I = MBB; |
| 14531 | ++I; |
| 14532 | |
| 14533 | // Memory Reference |
| 14534 | MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin(); |
| 14535 | MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end(); |
| 14536 | |
| 14537 | unsigned DstReg; |
| 14538 | unsigned MemOpndSlot = 0; |
| 14539 | |
| 14540 | unsigned CurOp = 0; |
| 14541 | |
| 14542 | DstReg = MI->getOperand(CurOp++).getReg(); |
| 14543 | const TargetRegisterClass *RC = MRI.getRegClass(DstReg); |
| 14544 | assert(RC->hasType(MVT::i32) && "Invalid destination!"); |
| 14545 | unsigned mainDstReg = MRI.createVirtualRegister(RC); |
| 14546 | unsigned restoreDstReg = MRI.createVirtualRegister(RC); |
| 14547 | |
| 14548 | MemOpndSlot = CurOp; |
| 14549 | |
| 14550 | MVT PVT = getPointerTy(); |
| 14551 | assert((PVT == MVT::i64 || PVT == MVT::i32) && |
| 14552 | "Invalid Pointer Size!"); |
| 14553 | |
| 14554 | // For v = setjmp(buf), we generate |
| 14555 | // |
| 14556 | // thisMBB: |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 14557 | // buf[LabelOffset] = restoreMBB |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14558 | // SjLjSetup restoreMBB |
| 14559 | // |
| 14560 | // mainMBB: |
| 14561 | // v_main = 0 |
| 14562 | // |
| 14563 | // sinkMBB: |
| 14564 | // v = phi(main, restore) |
| 14565 | // |
| 14566 | // restoreMBB: |
| 14567 | // v_restore = 1 |
| 14568 | |
| 14569 | MachineBasicBlock *thisMBB = MBB; |
| 14570 | MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); |
| 14571 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); |
| 14572 | MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB); |
| 14573 | MF->insert(I, mainMBB); |
| 14574 | MF->insert(I, sinkMBB); |
| 14575 | MF->push_back(restoreMBB); |
| 14576 | |
| 14577 | MachineInstrBuilder MIB; |
| 14578 | |
| 14579 | // Transfer the remainder of BB and its successor edges to sinkMBB. |
| 14580 | sinkMBB->splice(sinkMBB->begin(), MBB, |
| 14581 | llvm::next(MachineBasicBlock::iterator(MI)), MBB->end()); |
| 14582 | sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 14583 | |
| 14584 | // thisMBB: |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 14585 | unsigned PtrStoreOpc = 0; |
| 14586 | unsigned LabelReg = 0; |
| 14587 | const int64_t LabelOffset = 1 * PVT.getStoreSize(); |
| 14588 | Reloc::Model RM = getTargetMachine().getRelocationModel(); |
| 14589 | bool UseImmLabel = (getTargetMachine().getCodeModel() == CodeModel::Small) && |
| 14590 | (RM == Reloc::Static || RM == Reloc::DynamicNoPIC); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14591 | |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 14592 | // Prepare IP either in reg or imm. |
| 14593 | if (!UseImmLabel) { |
| 14594 | PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr; |
| 14595 | const TargetRegisterClass *PtrRC = getRegClassFor(PVT); |
| 14596 | LabelReg = MRI.createVirtualRegister(PtrRC); |
| 14597 | if (Subtarget->is64Bit()) { |
| 14598 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg) |
| 14599 | .addReg(X86::RIP) |
| 14600 | .addImm(0) |
| 14601 | .addReg(0) |
| 14602 | .addMBB(restoreMBB) |
| 14603 | .addReg(0); |
| 14604 | } else { |
| 14605 | const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII); |
| 14606 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg) |
| 14607 | .addReg(XII->getGlobalBaseReg(MF)) |
| 14608 | .addImm(0) |
| 14609 | .addReg(0) |
| 14610 | .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference()) |
| 14611 | .addReg(0); |
| 14612 | } |
| 14613 | } else |
| 14614 | PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi; |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14615 | // Store IP |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 14616 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc)); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14617 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { |
| 14618 | if (i == X86::AddrDisp) |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 14619 | MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14620 | else |
| 14621 | MIB.addOperand(MI->getOperand(MemOpndSlot + i)); |
| 14622 | } |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 14623 | if (!UseImmLabel) |
| 14624 | MIB.addReg(LabelReg); |
| 14625 | else |
| 14626 | MIB.addMBB(restoreMBB); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14627 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 14628 | // Setup |
| 14629 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup)) |
| 14630 | .addMBB(restoreMBB); |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 14631 | |
| 14632 | const X86RegisterInfo *RegInfo = |
| 14633 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14634 | MIB.addRegMask(RegInfo->getNoPreservedMask()); |
| 14635 | thisMBB->addSuccessor(mainMBB); |
| 14636 | thisMBB->addSuccessor(restoreMBB); |
| 14637 | |
| 14638 | // mainMBB: |
| 14639 | // EAX = 0 |
| 14640 | BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg); |
| 14641 | mainMBB->addSuccessor(sinkMBB); |
| 14642 | |
| 14643 | // sinkMBB: |
| 14644 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, |
| 14645 | TII->get(X86::PHI), DstReg) |
| 14646 | .addReg(mainDstReg).addMBB(mainMBB) |
| 14647 | .addReg(restoreDstReg).addMBB(restoreMBB); |
| 14648 | |
| 14649 | // restoreMBB: |
| 14650 | BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1); |
| 14651 | BuildMI(restoreMBB, DL, TII->get(X86::JMP_4)).addMBB(sinkMBB); |
| 14652 | restoreMBB->addSuccessor(sinkMBB); |
| 14653 | |
| 14654 | MI->eraseFromParent(); |
| 14655 | return sinkMBB; |
| 14656 | } |
| 14657 | |
| 14658 | MachineBasicBlock * |
| 14659 | X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI, |
| 14660 | MachineBasicBlock *MBB) const { |
| 14661 | DebugLoc DL = MI->getDebugLoc(); |
| 14662 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 14663 | |
| 14664 | MachineFunction *MF = MBB->getParent(); |
| 14665 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 14666 | |
| 14667 | // Memory Reference |
| 14668 | MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin(); |
| 14669 | MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end(); |
| 14670 | |
| 14671 | MVT PVT = getPointerTy(); |
| 14672 | assert((PVT == MVT::i64 || PVT == MVT::i32) && |
| 14673 | "Invalid Pointer Size!"); |
| 14674 | |
| 14675 | const TargetRegisterClass *RC = |
| 14676 | (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass; |
| 14677 | unsigned Tmp = MRI.createVirtualRegister(RC); |
| 14678 | // Since FP is only updated here but NOT referenced, it's treated as GPR. |
| Bill Wendling | a5e5ba6 | 2013-06-07 21:00:34 +0000 | [diff] [blame] | 14679 | const X86RegisterInfo *RegInfo = |
| 14680 | static_cast<const X86RegisterInfo*>(getTargetMachine().getRegisterInfo()); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14681 | unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP; |
| 14682 | unsigned SP = RegInfo->getStackRegister(); |
| 14683 | |
| 14684 | MachineInstrBuilder MIB; |
| 14685 | |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 14686 | const int64_t LabelOffset = 1 * PVT.getStoreSize(); |
| 14687 | const int64_t SPOffset = 2 * PVT.getStoreSize(); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14688 | |
| 14689 | unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm; |
| 14690 | unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r; |
| 14691 | |
| 14692 | // Reload FP |
| 14693 | MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP); |
| 14694 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) |
| 14695 | MIB.addOperand(MI->getOperand(i)); |
| 14696 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 14697 | // Reload IP |
| 14698 | MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp); |
| 14699 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { |
| 14700 | if (i == X86::AddrDisp) |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 14701 | MIB.addDisp(MI->getOperand(i), LabelOffset); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14702 | else |
| 14703 | MIB.addOperand(MI->getOperand(i)); |
| 14704 | } |
| 14705 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 14706 | // Reload SP |
| 14707 | MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP); |
| 14708 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { |
| 14709 | if (i == X86::AddrDisp) |
| Michael Liao | 281ae5a | 2012-10-17 02:22:27 +0000 | [diff] [blame] | 14710 | MIB.addDisp(MI->getOperand(i), SPOffset); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14711 | else |
| 14712 | MIB.addOperand(MI->getOperand(i)); |
| 14713 | } |
| 14714 | MIB.setMemRefs(MMOBegin, MMOEnd); |
| 14715 | // Jump |
| 14716 | BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp); |
| 14717 | |
| 14718 | MI->eraseFromParent(); |
| 14719 | return MBB; |
| 14720 | } |
| 14721 | |
| 14722 | MachineBasicBlock * |
| Evan Cheng | ff9b373 | 2008-01-30 18:18:23 +0000 | [diff] [blame] | 14723 | X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, |
| Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 14724 | MachineBasicBlock *BB) const { |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14725 | switch (MI->getOpcode()) { |
| Craig Topper | abb94d0 | 2012-02-05 03:43:23 +0000 | [diff] [blame] | 14726 | default: llvm_unreachable("Unexpected instr type to insert"); |
| NAKAMURA Takumi | 7754f85 | 2011-01-26 02:04:09 +0000 | [diff] [blame] | 14727 | case X86::TAILJMPd64: |
| 14728 | case X86::TAILJMPr64: |
| 14729 | case X86::TAILJMPm64: |
| Craig Topper | 6d1263a | 2012-02-05 05:38:58 +0000 | [diff] [blame] | 14730 | llvm_unreachable("TAILJMP64 would not be touched here."); |
| NAKAMURA Takumi | 7754f85 | 2011-01-26 02:04:09 +0000 | [diff] [blame] | 14731 | case X86::TCRETURNdi64: |
| 14732 | case X86::TCRETURNri64: |
| 14733 | case X86::TCRETURNmi64: |
| NAKAMURA Takumi | 7754f85 | 2011-01-26 02:04:09 +0000 | [diff] [blame] | 14734 | return BB; |
| Michael J. Spencer | e9c253e | 2010-10-21 01:41:01 +0000 | [diff] [blame] | 14735 | case X86::WIN_ALLOCA: |
| 14736 | return EmitLoweredWinAlloca(MI, BB); |
| Rafael Espindola | 151ab3e | 2011-08-30 19:47:04 +0000 | [diff] [blame] | 14737 | case X86::SEG_ALLOCA_32: |
| 14738 | return EmitLoweredSegAlloca(MI, BB, false); |
| 14739 | case X86::SEG_ALLOCA_64: |
| 14740 | return EmitLoweredSegAlloca(MI, BB, true); |
| Eric Christopher | 30ef0e5 | 2010-06-03 04:07:48 +0000 | [diff] [blame] | 14741 | case X86::TLSCall_32: |
| 14742 | case X86::TLSCall_64: |
| 14743 | return EmitLoweredTLSCall(MI, BB); |
| Dan Gohman | cbbea0f | 2009-08-27 00:14:12 +0000 | [diff] [blame] | 14744 | case X86::CMOV_GR8: |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14745 | case X86::CMOV_FR32: |
| 14746 | case X86::CMOV_FR64: |
| 14747 | case X86::CMOV_V4F32: |
| 14748 | case X86::CMOV_V2F64: |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14749 | case X86::CMOV_V2I64: |
| Bruno Cardoso Lopes | d40aa24 | 2011-08-09 23:27:13 +0000 | [diff] [blame] | 14750 | case X86::CMOV_V8F32: |
| 14751 | case X86::CMOV_V4F64: |
| 14752 | case X86::CMOV_V4I64: |
| Chris Lattner | 314a113 | 2010-03-14 18:31:44 +0000 | [diff] [blame] | 14753 | case X86::CMOV_GR16: |
| 14754 | case X86::CMOV_GR32: |
| 14755 | case X86::CMOV_RFP32: |
| 14756 | case X86::CMOV_RFP64: |
| 14757 | case X86::CMOV_RFP80: |
| Dan Gohman | af1d8ca | 2010-05-01 00:01:06 +0000 | [diff] [blame] | 14758 | return EmitLoweredSelect(MI, BB); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14759 | |
| Dale Johannesen | 849f214 | 2007-07-03 00:53:03 +0000 | [diff] [blame] | 14760 | case X86::FP32_TO_INT16_IN_MEM: |
| 14761 | case X86::FP32_TO_INT32_IN_MEM: |
| 14762 | case X86::FP32_TO_INT64_IN_MEM: |
| 14763 | case X86::FP64_TO_INT16_IN_MEM: |
| 14764 | case X86::FP64_TO_INT32_IN_MEM: |
| Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 14765 | case X86::FP64_TO_INT64_IN_MEM: |
| 14766 | case X86::FP80_TO_INT16_IN_MEM: |
| 14767 | case X86::FP80_TO_INT32_IN_MEM: |
| 14768 | case X86::FP80_TO_INT64_IN_MEM: { |
| Chris Lattner | 5260097 | 2009-09-02 05:57:00 +0000 | [diff] [blame] | 14769 | const TargetInstrInfo *TII = getTargetMachine().getInstrInfo(); |
| 14770 | DebugLoc DL = MI->getDebugLoc(); |
| 14771 | |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14772 | // Change the floating point control register to use "round towards zero" |
| 14773 | // mode when truncating to an integer value. |
| 14774 | MachineFunction *F = BB->getParent(); |
| David Greene | 3f2bf85 | 2009-11-12 20:49:22 +0000 | [diff] [blame] | 14775 | int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false); |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14776 | addFrameReference(BuildMI(*BB, MI, DL, |
| 14777 | TII->get(X86::FNSTCW16m)), CWFrameIdx); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14778 | |
| 14779 | // Load the old value of the high byte of the control word... |
| 14780 | unsigned OldCW = |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 14781 | F->getRegInfo().createVirtualRegister(&X86::GR16RegClass); |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14782 | addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW), |
| Dale Johannesen | e4d209d | 2009-02-03 20:21:25 +0000 | [diff] [blame] | 14783 | CWFrameIdx); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14784 | |
| 14785 | // Set the high part to be round to zero... |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14786 | addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx) |
| Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 14787 | .addImm(0xC7F); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14788 | |
| 14789 | // Reload the modified control word now... |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14790 | addFrameReference(BuildMI(*BB, MI, DL, |
| 14791 | TII->get(X86::FLDCW16m)), CWFrameIdx); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14792 | |
| 14793 | // Restore the memory image of control word to original value |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14794 | addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx) |
| Evan Cheng | c0f64ff | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 14795 | .addReg(OldCW); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14796 | |
| 14797 | // Get the X86 opcode to use. |
| 14798 | unsigned Opc; |
| 14799 | switch (MI->getOpcode()) { |
| Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 14800 | default: llvm_unreachable("illegal opcode!"); |
| Dale Johannesen | e377d4d | 2007-07-04 21:07:47 +0000 | [diff] [blame] | 14801 | case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break; |
| 14802 | case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break; |
| 14803 | case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break; |
| 14804 | case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break; |
| 14805 | case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break; |
| 14806 | case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break; |
| Dale Johannesen | a996d52 | 2007-08-07 01:17:37 +0000 | [diff] [blame] | 14807 | case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break; |
| 14808 | case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break; |
| 14809 | case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break; |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14810 | } |
| 14811 | |
| 14812 | X86AddressMode AM; |
| 14813 | MachineOperand &Op = MI->getOperand(0); |
| Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 14814 | if (Op.isReg()) { |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14815 | AM.BaseType = X86AddressMode::RegBase; |
| 14816 | AM.Base.Reg = Op.getReg(); |
| 14817 | } else { |
| 14818 | AM.BaseType = X86AddressMode::FrameIndexBase; |
| Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 14819 | AM.Base.FrameIndex = Op.getIndex(); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14820 | } |
| 14821 | Op = MI->getOperand(1); |
| Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 14822 | if (Op.isImm()) |
| Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 14823 | AM.Scale = Op.getImm(); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14824 | Op = MI->getOperand(2); |
| Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 14825 | if (Op.isImm()) |
| Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 14826 | AM.IndexReg = Op.getImm(); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14827 | Op = MI->getOperand(3); |
| Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 14828 | if (Op.isGlobal()) { |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14829 | AM.GV = Op.getGlobal(); |
| 14830 | } else { |
| Chris Lattner | 7fbe972 | 2006-10-20 17:42:20 +0000 | [diff] [blame] | 14831 | AM.Disp = Op.getImm(); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14832 | } |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14833 | addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM) |
| Chris Lattner | ac0ed5d | 2010-07-08 22:41:28 +0000 | [diff] [blame] | 14834 | .addReg(MI->getOperand(X86::AddrNumOperands).getReg()); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14835 | |
| 14836 | // Reload the original control word now. |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14837 | addFrameReference(BuildMI(*BB, MI, DL, |
| 14838 | TII->get(X86::FLDCW16m)), CWFrameIdx); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14839 | |
| Dan Gohman | 14152b4 | 2010-07-06 20:24:04 +0000 | [diff] [blame] | 14840 | MI->eraseFromParent(); // The pseudo instruction is gone now. |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14841 | return BB; |
| 14842 | } |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 14843 | // String/text processing lowering. |
| 14844 | case X86::PCMPISTRM128REG: |
| Bruno Cardoso Lopes | 98f9856 | 2010-07-30 19:54:33 +0000 | [diff] [blame] | 14845 | case X86::VPCMPISTRM128REG: |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 14846 | case X86::PCMPISTRM128MEM: |
| Bruno Cardoso Lopes | 98f9856 | 2010-07-30 19:54:33 +0000 | [diff] [blame] | 14847 | case X86::VPCMPISTRM128MEM: |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 14848 | case X86::PCMPESTRM128REG: |
| Bruno Cardoso Lopes | 98f9856 | 2010-07-30 19:54:33 +0000 | [diff] [blame] | 14849 | case X86::VPCMPESTRM128REG: |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 14850 | case X86::PCMPESTRM128MEM: |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 14851 | case X86::VPCMPESTRM128MEM: |
| 14852 | assert(Subtarget->hasSSE42() && |
| 14853 | "Target must have SSE4.2 or AVX features enabled"); |
| 14854 | return EmitPCMPSTRM(MI, BB, getTargetMachine().getInstrInfo()); |
| Craig Topper | 9c7ae01 | 2012-11-10 01:23:36 +0000 | [diff] [blame] | 14855 | |
| 14856 | // String/text processing lowering. |
| 14857 | case X86::PCMPISTRIREG: |
| 14858 | case X86::VPCMPISTRIREG: |
| 14859 | case X86::PCMPISTRIMEM: |
| 14860 | case X86::VPCMPISTRIMEM: |
| 14861 | case X86::PCMPESTRIREG: |
| 14862 | case X86::VPCMPESTRIREG: |
| 14863 | case X86::PCMPESTRIMEM: |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 14864 | case X86::VPCMPESTRIMEM: |
| 14865 | assert(Subtarget->hasSSE42() && |
| 14866 | "Target must have SSE4.2 or AVX features enabled"); |
| 14867 | return EmitPCMPSTRI(MI, BB, getTargetMachine().getInstrInfo()); |
| Eric Christopher | b120ab4 | 2009-08-18 22:50:32 +0000 | [diff] [blame] | 14868 | |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 14869 | // Thread synchronization. |
| Eric Christopher | 228232b | 2010-11-30 07:20:12 +0000 | [diff] [blame] | 14870 | case X86::MONITOR: |
| Craig Topper | 2da3691 | 2012-11-11 22:45:02 +0000 | [diff] [blame] | 14871 | return EmitMonitor(MI, BB, getTargetMachine().getInstrInfo(), Subtarget); |
| Eric Christopher | 228232b | 2010-11-30 07:20:12 +0000 | [diff] [blame] | 14872 | |
| Michael Liao | be02a90 | 2012-11-08 07:28:54 +0000 | [diff] [blame] | 14873 | // xbegin |
| 14874 | case X86::XBEGIN: |
| Craig Topper | 2da3691 | 2012-11-11 22:45:02 +0000 | [diff] [blame] | 14875 | return EmitXBegin(MI, BB, getTargetMachine().getInstrInfo()); |
| Michael Liao | be02a90 | 2012-11-08 07:28:54 +0000 | [diff] [blame] | 14876 | |
| Craig Topper | 8aae8dd | 2012-11-10 08:57:41 +0000 | [diff] [blame] | 14877 | // Atomic Lowering. |
| Dale Johannesen | 140be2d | 2008-08-19 18:47:28 +0000 | [diff] [blame] | 14878 | case X86::ATOMAND8: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 14879 | case X86::ATOMAND16: |
| 14880 | case X86::ATOMAND32: |
| Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 14881 | case X86::ATOMAND64: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 14882 | // Fall through |
| 14883 | case X86::ATOMOR8: |
| 14884 | case X86::ATOMOR16: |
| 14885 | case X86::ATOMOR32: |
| Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 14886 | case X86::ATOMOR64: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 14887 | // Fall through |
| 14888 | case X86::ATOMXOR16: |
| 14889 | case X86::ATOMXOR8: |
| 14890 | case X86::ATOMXOR32: |
| Dale Johannesen | a99e384 | 2008-08-20 00:48:50 +0000 | [diff] [blame] | 14891 | case X86::ATOMXOR64: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 14892 | // Fall through |
| 14893 | case X86::ATOMNAND8: |
| 14894 | case X86::ATOMNAND16: |
| 14895 | case X86::ATOMNAND32: |
| 14896 | case X86::ATOMNAND64: |
| 14897 | // Fall through |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 14898 | case X86::ATOMMAX8: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 14899 | case X86::ATOMMAX16: |
| 14900 | case X86::ATOMMAX32: |
| 14901 | case X86::ATOMMAX64: |
| 14902 | // Fall through |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 14903 | case X86::ATOMMIN8: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 14904 | case X86::ATOMMIN16: |
| 14905 | case X86::ATOMMIN32: |
| 14906 | case X86::ATOMMIN64: |
| 14907 | // Fall through |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 14908 | case X86::ATOMUMAX8: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 14909 | case X86::ATOMUMAX16: |
| 14910 | case X86::ATOMUMAX32: |
| 14911 | case X86::ATOMUMAX64: |
| 14912 | // Fall through |
| Michael Liao | fe87c30 | 2012-09-21 03:18:52 +0000 | [diff] [blame] | 14913 | case X86::ATOMUMIN8: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 14914 | case X86::ATOMUMIN16: |
| 14915 | case X86::ATOMUMIN32: |
| 14916 | case X86::ATOMUMIN64: |
| 14917 | return EmitAtomicLoadArith(MI, BB); |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 14918 | |
| 14919 | // This group does 64-bit operations on a 32-bit host. |
| 14920 | case X86::ATOMAND6432: |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 14921 | case X86::ATOMOR6432: |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 14922 | case X86::ATOMXOR6432: |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 14923 | case X86::ATOMNAND6432: |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 14924 | case X86::ATOMADD6432: |
| Dale Johannesen | 48c1bc2 | 2008-10-02 18:53:47 +0000 | [diff] [blame] | 14925 | case X86::ATOMSUB6432: |
| Michael Liao | e5e8f76 | 2012-09-25 18:08:13 +0000 | [diff] [blame] | 14926 | case X86::ATOMMAX6432: |
| 14927 | case X86::ATOMMIN6432: |
| 14928 | case X86::ATOMUMAX6432: |
| 14929 | case X86::ATOMUMIN6432: |
| Michael Liao | b118a07 | 2012-09-20 03:06:15 +0000 | [diff] [blame] | 14930 | case X86::ATOMSWAP6432: |
| 14931 | return EmitAtomicLoadArith6432(MI, BB); |
| Craig Topper | acaaa6f | 2012-08-18 06:39:34 +0000 | [diff] [blame] | 14932 | |
| Dan Gohman | d6708ea | 2009-08-15 01:38:56 +0000 | [diff] [blame] | 14933 | case X86::VASTART_SAVE_XMM_REGS: |
| 14934 | return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB); |
| Dan Gohman | 320afb8 | 2010-10-12 18:00:49 +0000 | [diff] [blame] | 14935 | |
| 14936 | case X86::VAARG_64: |
| 14937 | return EmitVAARG64WithCustomInserter(MI, BB); |
| Michael Liao | 6c0e04c | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 14938 | |
| 14939 | case X86::EH_SjLj_SetJmp32: |
| 14940 | case X86::EH_SjLj_SetJmp64: |
| 14941 | return emitEHSjLjSetJmp(MI, BB); |
| 14942 | |
| 14943 | case X86::EH_SjLj_LongJmp32: |
| 14944 | case X86::EH_SjLj_LongJmp64: |
| 14945 | return emitEHSjLjLongJmp(MI, BB); |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 14946 | } |
| 14947 | } |
| 14948 | |
| 14949 | //===----------------------------------------------------------------------===// |
| 14950 | // X86 Optimization Hooks |
| 14951 | //===----------------------------------------------------------------------===// |
| 14952 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 14953 | void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, |
| Dan Gohman | fd29e0e | 2008-02-13 00:35:47 +0000 | [diff] [blame] | 14954 | APInt &KnownZero, |
| 14955 | APInt &KnownOne, |
| Dan Gohman | ea859be | 2007-06-22 14:59:07 +0000 | [diff] [blame] | 14956 | const SelectionDAG &DAG, |
| Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 14957 | unsigned Depth) const { |
| Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 14958 | unsigned BitWidth = KnownZero.getBitWidth(); |
| Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 14959 | unsigned Opc = Op.getOpcode(); |
| Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 14960 | assert((Opc >= ISD::BUILTIN_OP_END || |
| 14961 | Opc == ISD::INTRINSIC_WO_CHAIN || |
| 14962 | Opc == ISD::INTRINSIC_W_CHAIN || |
| 14963 | Opc == ISD::INTRINSIC_VOID) && |
| 14964 | "Should use MaskedValueIsZero if you don't know whether Op" |
| 14965 | " is a target node!"); |
| Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 14966 | |
| Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 14967 | KnownZero = KnownOne = APInt(BitWidth, 0); // Don't know anything. |
| Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 14968 | switch (Opc) { |
| Evan Cheng | 865f060 | 2006-04-05 06:11:20 +0000 | [diff] [blame] | 14969 | default: break; |
| Evan Cheng | 97d0e0e | 2009-02-02 09:15:04 +0000 | [diff] [blame] | 14970 | case X86ISD::ADD: |
| 14971 | case X86ISD::SUB: |
| Chris Lattner | 5b85654 | 2010-12-20 00:59:46 +0000 | [diff] [blame] | 14972 | case X86ISD::ADC: |
| 14973 | case X86ISD::SBB: |
| Evan Cheng | 97d0e0e | 2009-02-02 09:15:04 +0000 | [diff] [blame] | 14974 | case X86ISD::SMUL: |
| 14975 | case X86ISD::UMUL: |
| Dan Gohman | 076aee3 | 2009-03-04 19:44:21 +0000 | [diff] [blame] | 14976 | case X86ISD::INC: |
| 14977 | case X86ISD::DEC: |
| Dan Gohman | e220c4b | 2009-09-18 19:59:53 +0000 | [diff] [blame] | 14978 | case X86ISD::OR: |
| 14979 | case X86ISD::XOR: |
| 14980 | case X86ISD::AND: |
| Evan Cheng | 97d0e0e | 2009-02-02 09:15:04 +0000 | [diff] [blame] | 14981 | // These nodes' second result is a boolean. |
| 14982 | if (Op.getResNo() == 0) |
| 14983 | break; |
| 14984 | // Fallthrough |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 14985 | case X86ISD::SETCC: |
| Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 14986 | KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1); |
| Nate Begeman | 368e18d | 2006-02-16 21:11:51 +0000 | [diff] [blame] | 14987 | break; |
| Evan Cheng | 7c1780c | 2011-10-07 17:21:44 +0000 | [diff] [blame] | 14988 | case ISD::INTRINSIC_WO_CHAIN: { |
| 14989 | unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 14990 | unsigned NumLoBits = 0; |
| 14991 | switch (IntId) { |
| 14992 | default: break; |
| 14993 | case Intrinsic::x86_sse_movmsk_ps: |
| 14994 | case Intrinsic::x86_avx_movmsk_ps_256: |
| 14995 | case Intrinsic::x86_sse2_movmsk_pd: |
| 14996 | case Intrinsic::x86_avx_movmsk_pd_256: |
| 14997 | case Intrinsic::x86_mmx_pmovmskb: |
| Craig Topper | 3738ccd | 2011-12-27 06:27:23 +0000 | [diff] [blame] | 14998 | case Intrinsic::x86_sse2_pmovmskb_128: |
| 14999 | case Intrinsic::x86_avx2_pmovmskb: { |
| Evan Cheng | 7c1780c | 2011-10-07 17:21:44 +0000 | [diff] [blame] | 15000 | // High bits of movmskp{s|d}, pmovmskb are known zero. |
| 15001 | switch (IntId) { |
| Craig Topper | abb94d0 | 2012-02-05 03:43:23 +0000 | [diff] [blame] | 15002 | default: llvm_unreachable("Impossible intrinsic"); // Can't reach here. |
| Evan Cheng | 7c1780c | 2011-10-07 17:21:44 +0000 | [diff] [blame] | 15003 | case Intrinsic::x86_sse_movmsk_ps: NumLoBits = 4; break; |
| 15004 | case Intrinsic::x86_avx_movmsk_ps_256: NumLoBits = 8; break; |
| 15005 | case Intrinsic::x86_sse2_movmsk_pd: NumLoBits = 2; break; |
| 15006 | case Intrinsic::x86_avx_movmsk_pd_256: NumLoBits = 4; break; |
| 15007 | case Intrinsic::x86_mmx_pmovmskb: NumLoBits = 8; break; |
| 15008 | case Intrinsic::x86_sse2_pmovmskb_128: NumLoBits = 16; break; |
| Craig Topper | 3738ccd | 2011-12-27 06:27:23 +0000 | [diff] [blame] | 15009 | case Intrinsic::x86_avx2_pmovmskb: NumLoBits = 32; break; |
| Evan Cheng | 7c1780c | 2011-10-07 17:21:44 +0000 | [diff] [blame] | 15010 | } |
| Rafael Espindola | 26c8dcc | 2012-04-04 12:51:34 +0000 | [diff] [blame] | 15011 | KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits); |
| Evan Cheng | 7c1780c | 2011-10-07 17:21:44 +0000 | [diff] [blame] | 15012 | break; |
| 15013 | } |
| 15014 | } |
| 15015 | break; |
| 15016 | } |
| Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 15017 | } |
| Evan Cheng | 3a03ebb | 2005-12-21 23:05:39 +0000 | [diff] [blame] | 15018 | } |
| Chris Lattner | 259e97c | 2006-01-31 19:43:35 +0000 | [diff] [blame] | 15019 | |
| Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 15020 | unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(SDValue Op, |
| 15021 | unsigned Depth) const { |
| 15022 | // SETCC_CARRY sets the dest to ~0 for true or 0 for false. |
| 15023 | if (Op.getOpcode() == X86ISD::SETCC_CARRY) |
| 15024 | return Op.getValueType().getScalarType().getSizeInBits(); |
| Michael J. Spencer | ec38de2 | 2010-10-10 22:04:20 +0000 | [diff] [blame] | 15025 | |
| Owen Anderson | bc146b0 | 2010-09-21 20:42:50 +0000 | [diff] [blame] | 15026 | // Fallback case. |
| 15027 | return 1; |
| 15028 | } |
| 15029 | |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 15030 | /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the |
| Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 15031 | /// node is a GlobalAddress + offset. |
| 15032 | bool X86TargetLowering::isGAPlusOffset(SDNode *N, |
| Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 15033 | const GlobalValue* &GA, |
| 15034 | int64_t &Offset) const { |
| Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 15035 | if (N->getOpcode() == X86ISD::Wrapper) { |
| 15036 | if (isa<GlobalAddressSDNode>(N->getOperand(0))) { |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 15037 | GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal(); |
| Dan Gohman | 6520e20 | 2008-10-18 02:06:02 +0000 | [diff] [blame] | 15038 | Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset(); |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 15039 | return true; |
| 15040 | } |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 15041 | } |
| Evan Cheng | ad4196b | 2008-05-12 19:56:52 +0000 | [diff] [blame] | 15042 | return TargetLowering::isGAPlusOffset(N, GA, Offset); |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 15043 | } |
| 15044 | |
| Bruno Cardoso Lopes | ef8d699 | 2011-08-11 21:50:44 +0000 | [diff] [blame] | 15045 | /// isShuffleHigh128VectorInsertLow - Checks whether the shuffle node is the |
| 15046 | /// same as extracting the high 128-bit part of 256-bit vector and then |
| 15047 | /// inserting the result into the low part of a new 256-bit vector |
| 15048 | static bool isShuffleHigh128VectorInsertLow(ShuffleVectorSDNode *SVOp) { |
| 15049 | EVT VT = SVOp->getValueType(0); |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 15050 | unsigned NumElems = VT.getVectorNumElements(); |
| Bruno Cardoso Lopes | ef8d699 | 2011-08-11 21:50:44 +0000 | [diff] [blame] | 15051 | |
| 15052 | // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u> |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 15053 | for (unsigned i = 0, j = NumElems/2; i != NumElems/2; ++i, ++j) |
| Bruno Cardoso Lopes | ef8d699 | 2011-08-11 21:50:44 +0000 | [diff] [blame] | 15054 | if (!isUndefOrEqual(SVOp->getMaskElt(i), j) || |
| 15055 | SVOp->getMaskElt(j) >= 0) |
| 15056 | return false; |
| 15057 | |
| 15058 | return true; |
| 15059 | } |
| 15060 | |
| 15061 | /// isShuffleLow128VectorInsertHigh - Checks whether the shuffle node is the |
| 15062 | /// same as extracting the low 128-bit part of 256-bit vector and then |
| 15063 | /// inserting the result into the high part of a new 256-bit vector |
| 15064 | static bool isShuffleLow128VectorInsertHigh(ShuffleVectorSDNode *SVOp) { |
| 15065 | EVT VT = SVOp->getValueType(0); |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 15066 | unsigned NumElems = VT.getVectorNumElements(); |
| Bruno Cardoso Lopes | ef8d699 | 2011-08-11 21:50:44 +0000 | [diff] [blame] | 15067 | |
| 15068 | // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1> |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 15069 | for (unsigned i = NumElems/2, j = 0; i != NumElems; ++i, ++j) |
| Bruno Cardoso Lopes | ef8d699 | 2011-08-11 21:50:44 +0000 | [diff] [blame] | 15070 | if (!isUndefOrEqual(SVOp->getMaskElt(i), j) || |
| 15071 | SVOp->getMaskElt(j) >= 0) |
| 15072 | return false; |
| 15073 | |
| 15074 | return true; |
| 15075 | } |
| 15076 | |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15077 | /// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors. |
| 15078 | static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG, |
| Craig Topper | 1221617 | 2012-01-13 08:12:35 +0000 | [diff] [blame] | 15079 | TargetLowering::DAGCombinerInfo &DCI, |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 15080 | const X86Subtarget* Subtarget) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 15081 | SDLoc dl(N); |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15082 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); |
| 15083 | SDValue V1 = SVOp->getOperand(0); |
| 15084 | SDValue V2 = SVOp->getOperand(1); |
| 15085 | EVT VT = SVOp->getValueType(0); |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 15086 | unsigned NumElems = VT.getVectorNumElements(); |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15087 | |
| 15088 | if (V1.getOpcode() == ISD::CONCAT_VECTORS && |
| 15089 | V2.getOpcode() == ISD::CONCAT_VECTORS) { |
| 15090 | // |
| 15091 | // 0,0,0,... |
| Benjamin Kramer | 558cc5a | 2011-07-22 01:02:57 +0000 | [diff] [blame] | 15092 | // | |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15093 | // V UNDEF BUILD_VECTOR UNDEF |
| 15094 | // \ / \ / |
| 15095 | // CONCAT_VECTOR CONCAT_VECTOR |
| 15096 | // \ / |
| 15097 | // \ / |
| 15098 | // RESULT: V + zero extended |
| 15099 | // |
| 15100 | if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR || |
| 15101 | V2.getOperand(1).getOpcode() != ISD::UNDEF || |
| 15102 | V1.getOperand(1).getOpcode() != ISD::UNDEF) |
| 15103 | return SDValue(); |
| 15104 | |
| 15105 | if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode())) |
| 15106 | return SDValue(); |
| 15107 | |
| 15108 | // To match the shuffle mask, the first half of the mask should |
| 15109 | // be exactly the first vector, and all the rest a splat with the |
| 15110 | // first element of the second one. |
| Craig Topper | 66ddd15 | 2012-04-27 22:54:43 +0000 | [diff] [blame] | 15111 | for (unsigned i = 0; i != NumElems/2; ++i) |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15112 | if (!isUndefOrEqual(SVOp->getMaskElt(i), i) || |
| 15113 | !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems)) |
| 15114 | return SDValue(); |
| 15115 | |
| Chad Rosier | 3d1161e | 2012-01-03 21:05:52 +0000 | [diff] [blame] | 15116 | // If V1 is coming from a vector load then just fold to a VZEXT_LOAD. |
| 15117 | if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) { |
| Chad Rosier | 4272683 | 2012-05-07 18:47:44 +0000 | [diff] [blame] | 15118 | if (Ld->hasNUsesOfValue(1, 0)) { |
| 15119 | SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other); |
| 15120 | SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() }; |
| 15121 | SDValue ResNode = |
| Michael Liao | 0ee1700 | 2013-04-19 04:03:37 +0000 | [diff] [blame] | 15122 | DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, |
| 15123 | array_lengthof(Ops), |
| Chad Rosier | 4272683 | 2012-05-07 18:47:44 +0000 | [diff] [blame] | 15124 | Ld->getMemoryVT(), |
| 15125 | Ld->getPointerInfo(), |
| 15126 | Ld->getAlignment(), |
| 15127 | false/*isVolatile*/, true/*ReadMem*/, |
| 15128 | false/*WriteMem*/); |
| Manman Ren | 2adc503 | 2012-11-13 19:13:05 +0000 | [diff] [blame] | 15129 | |
| 15130 | // Make sure the newly-created LOAD is in the same position as Ld in |
| 15131 | // terms of dependency. We create a TokenFactor for Ld and ResNode, |
| 15132 | // and update uses of Ld's output chain to use the TokenFactor. |
| 15133 | if (Ld->hasAnyUseOfValue(1)) { |
| 15134 | SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 15135 | SDValue(Ld, 1), SDValue(ResNode.getNode(), 1)); |
| 15136 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain); |
| 15137 | DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1), |
| 15138 | SDValue(ResNode.getNode(), 1)); |
| 15139 | } |
| 15140 | |
| Chad Rosier | 4272683 | 2012-05-07 18:47:44 +0000 | [diff] [blame] | 15141 | return DAG.getNode(ISD::BITCAST, dl, VT, ResNode); |
| 15142 | } |
| Chad Rosier | a20e1e7 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 15143 | } |
| Chad Rosier | 3d1161e | 2012-01-03 21:05:52 +0000 | [diff] [blame] | 15144 | |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15145 | // Emit a zeroed vector and insert the desired subvector on its |
| 15146 | // first half. |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 15147 | SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl); |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 15148 | SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl); |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15149 | return DCI.CombineTo(N, InsV); |
| 15150 | } |
| 15151 | |
| Bruno Cardoso Lopes | ef8d699 | 2011-08-11 21:50:44 +0000 | [diff] [blame] | 15152 | //===--------------------------------------------------------------------===// |
| 15153 | // Combine some shuffles into subvector extracts and inserts: |
| 15154 | // |
| 15155 | |
| 15156 | // vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u> |
| 15157 | if (isShuffleHigh128VectorInsertLow(SVOp)) { |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 15158 | SDValue V = Extract128BitVector(V1, NumElems/2, DAG, dl); |
| 15159 | SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, 0, DAG, dl); |
| Bruno Cardoso Lopes | ef8d699 | 2011-08-11 21:50:44 +0000 | [diff] [blame] | 15160 | return DCI.CombineTo(N, InsV); |
| 15161 | } |
| 15162 | |
| 15163 | // vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1> |
| 15164 | if (isShuffleLow128VectorInsertHigh(SVOp)) { |
| Craig Topper | b14940a | 2012-04-22 20:55:18 +0000 | [diff] [blame] | 15165 | SDValue V = Extract128BitVector(V1, 0, DAG, dl); |
| 15166 | SDValue InsV = Insert128BitVector(DAG.getUNDEF(VT), V, NumElems/2, DAG, dl); |
| Bruno Cardoso Lopes | ef8d699 | 2011-08-11 21:50:44 +0000 | [diff] [blame] | 15167 | return DCI.CombineTo(N, InsV); |
| 15168 | } |
| 15169 | |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15170 | return SDValue(); |
| 15171 | } |
| 15172 | |
| 15173 | /// PerformShuffleCombine - Performs several different shuffle combines. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 15174 | static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG, |
| Bruno Cardoso Lopes | 50b37c7 | 2011-08-15 21:45:54 +0000 | [diff] [blame] | 15175 | TargetLowering::DAGCombinerInfo &DCI, |
| 15176 | const X86Subtarget *Subtarget) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 15177 | SDLoc dl(N); |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 15178 | EVT VT = N->getValueType(0); |
| Mon P Wang | 1e95580 | 2009-04-03 02:43:30 +0000 | [diff] [blame] | 15179 | |
| Mon P Wang | a0fd0d5 | 2010-12-19 23:55:53 +0000 | [diff] [blame] | 15180 | // Don't create instructions with illegal types after legalize types has run. |
| 15181 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 15182 | if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType())) |
| 15183 | return SDValue(); |
| 15184 | |
| Bruno Cardoso Lopes | 50b37c7 | 2011-08-15 21:45:54 +0000 | [diff] [blame] | 15185 | // Combine 256-bit vector shuffles. This is only profitable when in AVX mode |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 15186 | if (Subtarget->hasFp256() && VT.is256BitVector() && |
| Bruno Cardoso Lopes | 50b37c7 | 2011-08-15 21:45:54 +0000 | [diff] [blame] | 15187 | N->getOpcode() == ISD::VECTOR_SHUFFLE) |
| Elena Demikhovsky | 0f1ead4 | 2012-02-02 09:20:18 +0000 | [diff] [blame] | 15188 | return PerformShuffleCombine256(N, DAG, DCI, Subtarget); |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15189 | |
| 15190 | // Only handle 128 wide vector from here on. |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 15191 | if (!VT.is128BitVector()) |
| Bruno Cardoso Lopes | 74dad55 | 2011-07-22 00:15:00 +0000 | [diff] [blame] | 15192 | return SDValue(); |
| 15193 | |
| 15194 | // Combine a vector_shuffle that is equal to build_vector load1, load2, load3, |
| 15195 | // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are |
| 15196 | // consecutive, non-overlapping, and in the right order. |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 15197 | SmallVector<SDValue, 16> Elts; |
| 15198 | for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 15199 | Elts.push_back(getShuffleScalarElt(N, i, DAG, 0)); |
| Bruno Cardoso Lopes | 27f1279 | 2010-08-28 02:46:39 +0000 | [diff] [blame] | 15200 | |
| Nate Begeman | fdea31a | 2010-03-24 20:49:50 +0000 | [diff] [blame] | 15201 | return EltsFromConsecutiveLoads(VT, Elts, dl, DAG); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 15202 | } |
| Evan Cheng | d880b97 | 2008-05-09 21:53:03 +0000 | [diff] [blame] | 15203 | |
| Nadav Rotem | e12bf18 | 2013-01-04 17:35:21 +0000 | [diff] [blame] | 15204 | /// PerformTruncateCombine - Converts truncate operation to |
| 15205 | /// a sequence of vector shuffle operations. |
| 15206 | /// It is possible when we truncate 256-bit vector to 128-bit vector |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 15207 | static SDValue PerformTruncateCombine(SDNode *N, SelectionDAG &DAG, |
| 15208 | TargetLowering::DAGCombinerInfo &DCI, |
| 15209 | const X86Subtarget *Subtarget) { |
| Elena Demikhovsky | 3ae9815 | 2012-02-01 07:56:44 +0000 | [diff] [blame] | 15210 | return SDValue(); |
| 15211 | } |
| 15212 | |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 15213 | /// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target |
| 15214 | /// specific shuffle of a load can be folded into a single element load. |
| 15215 | /// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but |
| 15216 | /// shuffles have been customed lowered so we need to handle those here. |
| 15217 | static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG, |
| 15218 | TargetLowering::DAGCombinerInfo &DCI) { |
| 15219 | if (DCI.isBeforeLegalizeOps()) |
| 15220 | return SDValue(); |
| 15221 | |
| 15222 | SDValue InVec = N->getOperand(0); |
| 15223 | SDValue EltNo = N->getOperand(1); |
| 15224 | |
| 15225 | if (!isa<ConstantSDNode>(EltNo)) |
| 15226 | return SDValue(); |
| 15227 | |
| 15228 | EVT VT = InVec.getValueType(); |
| 15229 | |
| 15230 | bool HasShuffleIntoBitcast = false; |
| 15231 | if (InVec.getOpcode() == ISD::BITCAST) { |
| 15232 | // Don't duplicate a load with other uses. |
| 15233 | if (!InVec.hasOneUse()) |
| 15234 | return SDValue(); |
| 15235 | EVT BCVT = InVec.getOperand(0).getValueType(); |
| 15236 | if (BCVT.getVectorNumElements() != VT.getVectorNumElements()) |
| 15237 | return SDValue(); |
| 15238 | InVec = InVec.getOperand(0); |
| 15239 | HasShuffleIntoBitcast = true; |
| 15240 | } |
| 15241 | |
| 15242 | if (!isTargetShuffle(InVec.getOpcode())) |
| 15243 | return SDValue(); |
| 15244 | |
| 15245 | // Don't duplicate a load with other uses. |
| 15246 | if (!InVec.hasOneUse()) |
| 15247 | return SDValue(); |
| 15248 | |
| 15249 | SmallVector<int, 16> ShuffleMask; |
| 15250 | bool UnaryShuffle; |
| Craig Topper | d978c54 | 2012-05-06 19:46:21 +0000 | [diff] [blame] | 15251 | if (!getTargetShuffleMask(InVec.getNode(), VT.getSimpleVT(), ShuffleMask, |
| 15252 | UnaryShuffle)) |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 15253 | return SDValue(); |
| 15254 | |
| 15255 | // Select the input vector, guarding against out of range extract vector. |
| 15256 | unsigned NumElems = VT.getVectorNumElements(); |
| 15257 | int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue(); |
| 15258 | int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt]; |
| 15259 | SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0) |
| 15260 | : InVec.getOperand(1); |
| 15261 | |
| 15262 | // If inputs to shuffle are the same for both ops, then allow 2 uses |
| 15263 | unsigned AllowedUses = InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1; |
| 15264 | |
| 15265 | if (LdNode.getOpcode() == ISD::BITCAST) { |
| 15266 | // Don't duplicate a load with other uses. |
| 15267 | if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0)) |
| 15268 | return SDValue(); |
| 15269 | |
| 15270 | AllowedUses = 1; // only allow 1 load use if we have a bitcast |
| 15271 | LdNode = LdNode.getOperand(0); |
| 15272 | } |
| 15273 | |
| 15274 | if (!ISD::isNormalLoad(LdNode.getNode())) |
| 15275 | return SDValue(); |
| 15276 | |
| 15277 | LoadSDNode *LN0 = cast<LoadSDNode>(LdNode); |
| 15278 | |
| 15279 | if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile()) |
| 15280 | return SDValue(); |
| 15281 | |
| 15282 | if (HasShuffleIntoBitcast) { |
| 15283 | // If there's a bitcast before the shuffle, check if the load type and |
| 15284 | // alignment is valid. |
| 15285 | unsigned Align = LN0->getAlignment(); |
| 15286 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| Micah Villmow | 3574eca | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 15287 | unsigned NewAlign = TLI.getDataLayout()-> |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 15288 | getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext())); |
| 15289 | |
| 15290 | if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, VT)) |
| 15291 | return SDValue(); |
| 15292 | } |
| 15293 | |
| 15294 | // All checks match so transform back to vector_shuffle so that DAG combiner |
| 15295 | // can finish the job |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 15296 | SDLoc dl(N); |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 15297 | |
| 15298 | // Create shuffle node taking into account the case that its a unary shuffle |
| 15299 | SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(VT) : InVec.getOperand(1); |
| 15300 | Shuffle = DAG.getVectorShuffle(InVec.getValueType(), dl, |
| 15301 | InVec.getOperand(0), Shuffle, |
| 15302 | &ShuffleMask[0]); |
| 15303 | Shuffle = DAG.getNode(ISD::BITCAST, dl, VT, Shuffle); |
| 15304 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle, |
| 15305 | EltNo); |
| 15306 | } |
| 15307 | |
| Bruno Cardoso Lopes | b3e0669 | 2010-09-03 19:55:05 +0000 | [diff] [blame] | 15308 | /// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index |
| 15309 | /// generation and convert it from being a bunch of shuffles and extracts |
| 15310 | /// to a simple store and scalar loads to extract the elements. |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 15311 | static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG, |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 15312 | TargetLowering::DAGCombinerInfo &DCI) { |
| 15313 | SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI); |
| 15314 | if (NewOp.getNode()) |
| 15315 | return NewOp; |
| 15316 | |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 15317 | SDValue InputVector = N->getOperand(0); |
| Manman Ren | 4c74a95 | 2012-10-30 22:15:38 +0000 | [diff] [blame] | 15318 | // Detect whether we are trying to convert from mmx to i32 and the bitcast |
| 15319 | // from mmx to v2i32 has a single usage. |
| 15320 | if (InputVector.getNode()->getOpcode() == llvm::ISD::BITCAST && |
| 15321 | InputVector.getNode()->getOperand(0).getValueType() == MVT::x86mmx && |
| 15322 | InputVector.hasOneUse() && N->getValueType(0) == MVT::i32) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 15323 | return DAG.getNode(X86ISD::MMX_MOVD2W, SDLoc(InputVector), |
| Manman Ren | 4c74a95 | 2012-10-30 22:15:38 +0000 | [diff] [blame] | 15324 | N->getValueType(0), |
| 15325 | InputVector.getNode()->getOperand(0)); |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 15326 | |
| 15327 | // Only operate on vectors of 4 elements, where the alternative shuffling |
| 15328 | // gets to be more expensive. |
| 15329 | if (InputVector.getValueType() != MVT::v4i32) |
| 15330 | return SDValue(); |
| 15331 | |
| 15332 | // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a |
| 15333 | // single use which is a sign-extend or zero-extend, and all elements are |
| 15334 | // used. |
| 15335 | SmallVector<SDNode *, 4> Uses; |
| 15336 | unsigned ExtractedElements = 0; |
| 15337 | for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(), |
| 15338 | UE = InputVector.getNode()->use_end(); UI != UE; ++UI) { |
| 15339 | if (UI.getUse().getResNo() != InputVector.getResNo()) |
| 15340 | return SDValue(); |
| 15341 | |
| 15342 | SDNode *Extract = *UI; |
| 15343 | if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT) |
| 15344 | return SDValue(); |
| 15345 | |
| 15346 | if (Extract->getValueType(0) != MVT::i32) |
| 15347 | return SDValue(); |
| 15348 | if (!Extract->hasOneUse()) |
| 15349 | return SDValue(); |
| 15350 | if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND && |
| 15351 | Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND) |
| 15352 | return SDValue(); |
| 15353 | if (!isa<ConstantSDNode>(Extract->getOperand(1))) |
| 15354 | return SDValue(); |
| 15355 | |
| 15356 | // Record which element was extracted. |
| 15357 | ExtractedElements |= |
| 15358 | 1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue(); |
| 15359 | |
| 15360 | Uses.push_back(Extract); |
| 15361 | } |
| 15362 | |
| 15363 | // If not all the elements were used, this may not be worthwhile. |
| 15364 | if (ExtractedElements != 15) |
| 15365 | return SDValue(); |
| 15366 | |
| 15367 | // Ok, we've now decided to do the transformation. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 15368 | SDLoc dl(InputVector); |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 15369 | |
| 15370 | // Store the value to a temporary stack slot. |
| 15371 | SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType()); |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 15372 | SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr, |
| 15373 | MachinePointerInfo(), false, false, 0); |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 15374 | |
| 15375 | // Replace each use (extract) with a load of the appropriate element. |
| 15376 | for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(), |
| 15377 | UE = Uses.end(); UI != UE; ++UI) { |
| 15378 | SDNode *Extract = *UI; |
| 15379 | |
| Nadav Rotem | 8669429 | 2011-05-17 08:31:57 +0000 | [diff] [blame] | 15380 | // cOMpute the element's address. |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 15381 | SDValue Idx = Extract->getOperand(1); |
| 15382 | unsigned EltSize = |
| 15383 | InputVector.getValueType().getVectorElementType().getSizeInBits()/8; |
| 15384 | uint64_t Offset = EltSize * cast<ConstantSDNode>(Idx)->getZExtValue(); |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 15385 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 15386 | SDValue OffsetVal = DAG.getConstant(Offset, TLI.getPointerTy()); |
| 15387 | |
| Nadav Rotem | 8669429 | 2011-05-17 08:31:57 +0000 | [diff] [blame] | 15388 | SDValue ScalarAddr = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(), |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 15389 | StackPtr, OffsetVal); |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 15390 | |
| 15391 | // Load the scalar. |
| Eric Christopher | 90eb402 | 2010-07-22 00:26:08 +0000 | [diff] [blame] | 15392 | SDValue LoadScalar = DAG.getLoad(Extract->getValueType(0), dl, Ch, |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 15393 | ScalarAddr, MachinePointerInfo(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 15394 | false, false, false, 0); |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 15395 | |
| 15396 | // Replace the exact with the load. |
| 15397 | DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), LoadScalar); |
| 15398 | } |
| 15399 | |
| 15400 | // The replacement was made in place; don't return anything. |
| 15401 | return SDValue(); |
| 15402 | } |
| 15403 | |
| Benjamin Kramer | 2556c6b | 2012-12-21 17:46:58 +0000 | [diff] [blame] | 15404 | /// \brief Matches a VSELECT onto min/max or return 0 if the node doesn't match. |
| 15405 | static unsigned matchIntegerMINMAX(SDValue Cond, EVT VT, SDValue LHS, |
| 15406 | SDValue RHS, SelectionDAG &DAG, |
| 15407 | const X86Subtarget *Subtarget) { |
| 15408 | if (!VT.isVector()) |
| 15409 | return 0; |
| 15410 | |
| 15411 | switch (VT.getSimpleVT().SimpleTy) { |
| 15412 | default: return 0; |
| 15413 | case MVT::v32i8: |
| 15414 | case MVT::v16i16: |
| 15415 | case MVT::v8i32: |
| 15416 | if (!Subtarget->hasAVX2()) |
| 15417 | return 0; |
| 15418 | case MVT::v16i8: |
| 15419 | case MVT::v8i16: |
| 15420 | case MVT::v4i32: |
| 15421 | if (!Subtarget->hasSSE2()) |
| 15422 | return 0; |
| 15423 | } |
| 15424 | |
| 15425 | // SSE2 has only a small subset of the operations. |
| 15426 | bool hasUnsigned = Subtarget->hasSSE41() || |
| 15427 | (Subtarget->hasSSE2() && VT == MVT::v16i8); |
| 15428 | bool hasSigned = Subtarget->hasSSE41() || |
| 15429 | (Subtarget->hasSSE2() && VT == MVT::v8i16); |
| 15430 | |
| 15431 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
| 15432 | |
| 15433 | // Check for x CC y ? x : y. |
| 15434 | if (DAG.isEqualTo(LHS, Cond.getOperand(0)) && |
| 15435 | DAG.isEqualTo(RHS, Cond.getOperand(1))) { |
| 15436 | switch (CC) { |
| 15437 | default: break; |
| 15438 | case ISD::SETULT: |
| 15439 | case ISD::SETULE: |
| 15440 | return hasUnsigned ? X86ISD::UMIN : 0; |
| 15441 | case ISD::SETUGT: |
| 15442 | case ISD::SETUGE: |
| 15443 | return hasUnsigned ? X86ISD::UMAX : 0; |
| 15444 | case ISD::SETLT: |
| 15445 | case ISD::SETLE: |
| 15446 | return hasSigned ? X86ISD::SMIN : 0; |
| 15447 | case ISD::SETGT: |
| 15448 | case ISD::SETGE: |
| 15449 | return hasSigned ? X86ISD::SMAX : 0; |
| 15450 | } |
| 15451 | // Check for x CC y ? y : x -- a min/max with reversed arms. |
| 15452 | } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) && |
| 15453 | DAG.isEqualTo(RHS, Cond.getOperand(0))) { |
| 15454 | switch (CC) { |
| 15455 | default: break; |
| 15456 | case ISD::SETULT: |
| 15457 | case ISD::SETULE: |
| 15458 | return hasUnsigned ? X86ISD::UMAX : 0; |
| 15459 | case ISD::SETUGT: |
| 15460 | case ISD::SETUGE: |
| 15461 | return hasUnsigned ? X86ISD::UMIN : 0; |
| 15462 | case ISD::SETLT: |
| 15463 | case ISD::SETLE: |
| 15464 | return hasSigned ? X86ISD::SMAX : 0; |
| 15465 | case ISD::SETGT: |
| 15466 | case ISD::SETGE: |
| 15467 | return hasSigned ? X86ISD::SMIN : 0; |
| 15468 | } |
| 15469 | } |
| 15470 | |
| 15471 | return 0; |
| 15472 | } |
| 15473 | |
| Duncan Sands | 6bcd219 | 2011-09-17 16:49:39 +0000 | [diff] [blame] | 15474 | /// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT |
| 15475 | /// nodes. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 15476 | static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG, |
| Nadav Rotem | cc61656 | 2012-01-15 19:27:55 +0000 | [diff] [blame] | 15477 | TargetLowering::DAGCombinerInfo &DCI, |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15478 | const X86Subtarget *Subtarget) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 15479 | SDLoc DL(N); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 15480 | SDValue Cond = N->getOperand(0); |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15481 | // Get the LHS/RHS of the select. |
| 15482 | SDValue LHS = N->getOperand(1); |
| 15483 | SDValue RHS = N->getOperand(2); |
| Bruno Cardoso Lopes | 149f29f | 2011-09-20 22:34:45 +0000 | [diff] [blame] | 15484 | EVT VT = LHS.getValueType(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15485 | |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15486 | // If we have SSE[12] support, try to form min/max nodes. SSE min/max |
| Dan Gohman | 8ce05da | 2010-02-22 04:03:39 +0000 | [diff] [blame] | 15487 | // instructions match the semantics of the common C idiom x<y?x:y but not |
| 15488 | // x<=y?x:y, because of how they handle negative zero (which can be |
| 15489 | // ignored in unsafe-math mode). |
| Benjamin Kramer | 2c2ccbf | 2011-09-22 03:27:22 +0000 | [diff] [blame] | 15490 | if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() && |
| 15491 | VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) && |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 15492 | (Subtarget->hasSSE2() || |
| 15493 | (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) { |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15494 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 15495 | |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15496 | unsigned Opcode = 0; |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15497 | // Check for x CC y ? x : y. |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15498 | if (DAG.isEqualTo(LHS, Cond.getOperand(0)) && |
| 15499 | DAG.isEqualTo(RHS, Cond.getOperand(1))) { |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15500 | switch (CC) { |
| 15501 | default: break; |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15502 | case ISD::SETULT: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15503 | // Converting this to a min would handle NaNs incorrectly, and swapping |
| 15504 | // the operands would cause it to handle comparisons between positive |
| 15505 | // and negative zero incorrectly. |
| Evan Cheng | 60108e9 | 2010-07-15 22:07:12 +0000 | [diff] [blame] | 15506 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) { |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 15507 | if (!DAG.getTarget().Options.UnsafeFPMath && |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15508 | !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) |
| 15509 | break; |
| 15510 | std::swap(LHS, RHS); |
| 15511 | } |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15512 | Opcode = X86ISD::FMIN; |
| 15513 | break; |
| 15514 | case ISD::SETOLE: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15515 | // Converting this to a min would handle comparisons between positive |
| 15516 | // and negative zero incorrectly. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 15517 | if (!DAG.getTarget().Options.UnsafeFPMath && |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15518 | !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) |
| 15519 | break; |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15520 | Opcode = X86ISD::FMIN; |
| 15521 | break; |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15522 | case ISD::SETULE: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15523 | // Converting this to a min would handle both negative zeros and NaNs |
| 15524 | // incorrectly, but we can swap the operands to fix both. |
| 15525 | std::swap(LHS, RHS); |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15526 | case ISD::SETOLT: |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15527 | case ISD::SETLT: |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15528 | case ISD::SETLE: |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15529 | Opcode = X86ISD::FMIN; |
| 15530 | break; |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 15531 | |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15532 | case ISD::SETOGE: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15533 | // Converting this to a max would handle comparisons between positive |
| 15534 | // and negative zero incorrectly. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 15535 | if (!DAG.getTarget().Options.UnsafeFPMath && |
| Evan Cheng | dd5663c | 2011-08-04 18:38:15 +0000 | [diff] [blame] | 15536 | !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15537 | break; |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15538 | Opcode = X86ISD::FMAX; |
| 15539 | break; |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15540 | case ISD::SETUGT: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15541 | // Converting this to a max would handle NaNs incorrectly, and swapping |
| 15542 | // the operands would cause it to handle comparisons between positive |
| 15543 | // and negative zero incorrectly. |
| Evan Cheng | 60108e9 | 2010-07-15 22:07:12 +0000 | [diff] [blame] | 15544 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) { |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 15545 | if (!DAG.getTarget().Options.UnsafeFPMath && |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15546 | !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) |
| 15547 | break; |
| 15548 | std::swap(LHS, RHS); |
| 15549 | } |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15550 | Opcode = X86ISD::FMAX; |
| 15551 | break; |
| 15552 | case ISD::SETUGE: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15553 | // Converting this to a max would handle both negative zeros and NaNs |
| 15554 | // incorrectly, but we can swap the operands to fix both. |
| 15555 | std::swap(LHS, RHS); |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15556 | case ISD::SETOGT: |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15557 | case ISD::SETGT: |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15558 | case ISD::SETGE: |
| 15559 | Opcode = X86ISD::FMAX; |
| 15560 | break; |
| Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 15561 | } |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15562 | // Check for x CC y ? y : x -- a min/max with reversed arms. |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15563 | } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) && |
| 15564 | DAG.isEqualTo(RHS, Cond.getOperand(0))) { |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15565 | switch (CC) { |
| 15566 | default: break; |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15567 | case ISD::SETOGE: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15568 | // Converting this to a min would handle comparisons between positive |
| 15569 | // and negative zero incorrectly, and swapping the operands would |
| 15570 | // cause it to handle NaNs incorrectly. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 15571 | if (!DAG.getTarget().Options.UnsafeFPMath && |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15572 | !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) { |
| Evan Cheng | 60108e9 | 2010-07-15 22:07:12 +0000 | [diff] [blame] | 15573 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15574 | break; |
| 15575 | std::swap(LHS, RHS); |
| 15576 | } |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15577 | Opcode = X86ISD::FMIN; |
| Dan Gohman | 8d44b28 | 2009-09-03 20:34:31 +0000 | [diff] [blame] | 15578 | break; |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15579 | case ISD::SETUGT: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15580 | // Converting this to a min would handle NaNs incorrectly. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 15581 | if (!DAG.getTarget().Options.UnsafeFPMath && |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15582 | (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))) |
| 15583 | break; |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15584 | Opcode = X86ISD::FMIN; |
| 15585 | break; |
| 15586 | case ISD::SETUGE: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15587 | // Converting this to a min would handle both negative zeros and NaNs |
| 15588 | // incorrectly, but we can swap the operands to fix both. |
| 15589 | std::swap(LHS, RHS); |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15590 | case ISD::SETOGT: |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15591 | case ISD::SETGT: |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15592 | case ISD::SETGE: |
| 15593 | Opcode = X86ISD::FMIN; |
| 15594 | break; |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 15595 | |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15596 | case ISD::SETULT: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15597 | // Converting this to a max would handle NaNs incorrectly. |
| Evan Cheng | 60108e9 | 2010-07-15 22:07:12 +0000 | [diff] [blame] | 15598 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15599 | break; |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15600 | Opcode = X86ISD::FMAX; |
| Dan Gohman | 8d44b28 | 2009-09-03 20:34:31 +0000 | [diff] [blame] | 15601 | break; |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15602 | case ISD::SETOLE: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15603 | // Converting this to a max would handle comparisons between positive |
| 15604 | // and negative zero incorrectly, and swapping the operands would |
| 15605 | // cause it to handle NaNs incorrectly. |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 15606 | if (!DAG.getTarget().Options.UnsafeFPMath && |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15607 | !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) { |
| Evan Cheng | 60108e9 | 2010-07-15 22:07:12 +0000 | [diff] [blame] | 15608 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15609 | break; |
| 15610 | std::swap(LHS, RHS); |
| 15611 | } |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15612 | Opcode = X86ISD::FMAX; |
| 15613 | break; |
| 15614 | case ISD::SETULE: |
| Dan Gohman | e832693 | 2010-02-24 06:52:40 +0000 | [diff] [blame] | 15615 | // Converting this to a max would handle both negative zeros and NaNs |
| 15616 | // incorrectly, but we can swap the operands to fix both. |
| 15617 | std::swap(LHS, RHS); |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15618 | case ISD::SETOLT: |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15619 | case ISD::SETLT: |
| Dan Gohman | 670e539 | 2009-09-21 18:03:22 +0000 | [diff] [blame] | 15620 | case ISD::SETLE: |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15621 | Opcode = X86ISD::FMAX; |
| 15622 | break; |
| 15623 | } |
| Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 15624 | } |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 15625 | |
| Chris Lattner | 47b4ce8 | 2009-03-11 05:48:52 +0000 | [diff] [blame] | 15626 | if (Opcode) |
| 15627 | return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS); |
| Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 15628 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15629 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15630 | // If this is a select between two integer constants, try to do some |
| 15631 | // optimizations. |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15632 | if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) { |
| 15633 | if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS)) |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15634 | // Don't do this for crazy integer types. |
| 15635 | if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) { |
| 15636 | // If this is efficiently invertible, canonicalize the LHSC/RHSC values |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15637 | // so that TrueC (the true value) is larger than FalseC. |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15638 | bool NeedsCondInvert = false; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15639 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15640 | if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) && |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15641 | // Efficiently invertible. |
| 15642 | (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible. |
| 15643 | (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible. |
| 15644 | isa<ConstantSDNode>(Cond.getOperand(1))))) { |
| 15645 | NeedsCondInvert = true; |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15646 | std::swap(TrueC, FalseC); |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15647 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15648 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15649 | // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0. |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15650 | if (FalseC->getAPIntValue() == 0 && |
| 15651 | TrueC->getAPIntValue().isPowerOf2()) { |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15652 | if (NeedsCondInvert) // Invert the condition if needed. |
| 15653 | Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, |
| 15654 | DAG.getConstant(1, Cond.getValueType())); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15655 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15656 | // Zero extend the condition if needed. |
| 15657 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15658 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15659 | unsigned ShAmt = TrueC->getAPIntValue().logBase2(); |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15660 | return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 15661 | DAG.getConstant(ShAmt, MVT::i8)); |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15662 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15663 | |
| Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 15664 | // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15665 | if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) { |
| Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 15666 | if (NeedsCondInvert) // Invert the condition if needed. |
| 15667 | Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, |
| 15668 | DAG.getConstant(1, Cond.getValueType())); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15669 | |
| Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 15670 | // Zero extend the condition if needed. |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15671 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, |
| 15672 | FalseC->getValueType(0), Cond); |
| Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 15673 | return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15674 | SDValue(FalseC, 0)); |
| Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 15675 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15676 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15677 | // Optimize cases that will turn into an LEA instruction. This requires |
| 15678 | // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9). |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 15679 | if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) { |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15680 | uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue(); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 15681 | if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15682 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15683 | bool isFastMultiplier = false; |
| 15684 | if (Diff < 10) { |
| 15685 | switch ((unsigned char)Diff) { |
| 15686 | default: break; |
| 15687 | case 1: // result = add base, cond |
| 15688 | case 2: // result = lea base( , cond*2) |
| 15689 | case 3: // result = lea base(cond, cond*2) |
| 15690 | case 4: // result = lea base( , cond*4) |
| 15691 | case 5: // result = lea base(cond, cond*4) |
| 15692 | case 8: // result = lea base( , cond*8) |
| 15693 | case 9: // result = lea base(cond, cond*8) |
| 15694 | isFastMultiplier = true; |
| 15695 | break; |
| 15696 | } |
| 15697 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15698 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15699 | if (isFastMultiplier) { |
| 15700 | APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue(); |
| 15701 | if (NeedsCondInvert) // Invert the condition if needed. |
| 15702 | Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond, |
| 15703 | DAG.getConstant(1, Cond.getValueType())); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15704 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15705 | // Zero extend the condition if needed. |
| 15706 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0), |
| 15707 | Cond); |
| 15708 | // Scale the condition by the difference. |
| 15709 | if (Diff != 1) |
| 15710 | Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond, |
| 15711 | DAG.getConstant(Diff, Cond.getValueType())); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15712 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 15713 | // Add the base if non-zero. |
| 15714 | if (FalseC->getAPIntValue() != 0) |
| 15715 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| 15716 | SDValue(FalseC, 0)); |
| 15717 | return Cond; |
| 15718 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15719 | } |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 15720 | } |
| 15721 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 15722 | |
| Evan Cheng | 56f582d | 2012-01-04 01:41:39 +0000 | [diff] [blame] | 15723 | // Canonicalize max and min: |
| 15724 | // (x > y) ? x : y -> (x >= y) ? x : y |
| 15725 | // (x < y) ? x : y -> (x <= y) ? x : y |
| 15726 | // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates |
| 15727 | // the need for an extra compare |
| 15728 | // against zero. e.g. |
| 15729 | // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0 |
| 15730 | // subl %esi, %edi |
| 15731 | // testl %edi, %edi |
| 15732 | // movl $0, %eax |
| 15733 | // cmovgl %edi, %eax |
| 15734 | // => |
| 15735 | // xorl %eax, %eax |
| 15736 | // subl %esi, $edi |
| 15737 | // cmovsl %eax, %edi |
| 15738 | if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC && |
| 15739 | DAG.isEqualTo(LHS, Cond.getOperand(0)) && |
| 15740 | DAG.isEqualTo(RHS, Cond.getOperand(1))) { |
| 15741 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
| 15742 | switch (CC) { |
| 15743 | default: break; |
| 15744 | case ISD::SETLT: |
| 15745 | case ISD::SETGT: { |
| 15746 | ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE; |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 15747 | Cond = DAG.getSetCC(SDLoc(Cond), Cond.getValueType(), |
| Evan Cheng | 56f582d | 2012-01-04 01:41:39 +0000 | [diff] [blame] | 15748 | Cond.getOperand(0), Cond.getOperand(1), NewCC); |
| 15749 | return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS); |
| 15750 | } |
| 15751 | } |
| 15752 | } |
| 15753 | |
| Benjamin Kramer | 388fc6a | 2012-12-15 16:47:44 +0000 | [diff] [blame] | 15754 | // Match VSELECTs into subs with unsigned saturation. |
| 15755 | if (!DCI.isBeforeLegalize() && |
| 15756 | N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC && |
| 15757 | // psubus is available in SSE2 and AVX2 for i8 and i16 vectors. |
| 15758 | ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) || |
| 15759 | (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) { |
| 15760 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); |
| 15761 | |
| 15762 | // Check if one of the arms of the VSELECT is a zero vector. If it's on the |
| 15763 | // left side invert the predicate to simplify logic below. |
| 15764 | SDValue Other; |
| 15765 | if (ISD::isBuildVectorAllZeros(LHS.getNode())) { |
| 15766 | Other = RHS; |
| 15767 | CC = ISD::getSetCCInverse(CC, true); |
| 15768 | } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) { |
| 15769 | Other = LHS; |
| 15770 | } |
| 15771 | |
| 15772 | if (Other.getNode() && Other->getNumOperands() == 2 && |
| 15773 | DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) { |
| 15774 | SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1); |
| 15775 | SDValue CondRHS = Cond->getOperand(1); |
| 15776 | |
| 15777 | // Look for a general sub with unsigned saturation first. |
| 15778 | // x >= y ? x-y : 0 --> subus x, y |
| 15779 | // x > y ? x-y : 0 --> subus x, y |
| 15780 | if ((CC == ISD::SETUGE || CC == ISD::SETUGT) && |
| 15781 | Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS)) |
| 15782 | return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS); |
| 15783 | |
| 15784 | // If the RHS is a constant we have to reverse the const canonicalization. |
| 15785 | // x > C-1 ? x+-C : 0 --> subus x, C |
| 15786 | if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD && |
| 15787 | isSplatVector(CondRHS.getNode()) && isSplatVector(OpRHS.getNode())) { |
| 15788 | APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue(); |
| Benjamin Kramer | 9fa9251 | 2013-02-04 15:19:25 +0000 | [diff] [blame] | 15789 | if (CondRHS.getConstantOperandVal(0) == -A-1) |
| Benjamin Kramer | 388fc6a | 2012-12-15 16:47:44 +0000 | [diff] [blame] | 15790 | return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, |
| Benjamin Kramer | 9fa9251 | 2013-02-04 15:19:25 +0000 | [diff] [blame] | 15791 | DAG.getConstant(-A, VT)); |
| Benjamin Kramer | 388fc6a | 2012-12-15 16:47:44 +0000 | [diff] [blame] | 15792 | } |
| 15793 | |
| 15794 | // Another special case: If C was a sign bit, the sub has been |
| 15795 | // canonicalized into a xor. |
| 15796 | // FIXME: Would it be better to use ComputeMaskedBits to determine whether |
| 15797 | // it's safe to decanonicalize the xor? |
| 15798 | // x s< 0 ? x^C : 0 --> subus x, C |
| 15799 | if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR && |
| 15800 | ISD::isBuildVectorAllZeros(CondRHS.getNode()) && |
| 15801 | isSplatVector(OpRHS.getNode())) { |
| 15802 | APInt A = cast<ConstantSDNode>(OpRHS.getOperand(0))->getAPIntValue(); |
| 15803 | if (A.isSignBit()) |
| 15804 | return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS); |
| 15805 | } |
| 15806 | } |
| 15807 | } |
| 15808 | |
| Benjamin Kramer | 2556c6b | 2012-12-21 17:46:58 +0000 | [diff] [blame] | 15809 | // Try to match a min/max vector operation. |
| 15810 | if (!DCI.isBeforeLegalize() && |
| 15811 | N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC) |
| 15812 | if (unsigned Op = matchIntegerMINMAX(Cond, VT, LHS, RHS, DAG, Subtarget)) |
| 15813 | return DAG.getNode(Op, DL, N->getValueType(0), LHS, RHS); |
| 15814 | |
| Michael Liao | bf53841 | 2013-04-11 05:15:54 +0000 | [diff] [blame] | 15815 | // Simplify vector selection if the selector will be produced by CMPP*/PCMP*. |
| 15816 | if (!DCI.isBeforeLegalize() && N->getOpcode() == ISD::VSELECT && |
| 15817 | Cond.getOpcode() == ISD::SETCC) { |
| 15818 | |
| 15819 | assert(Cond.getValueType().isVector() && |
| 15820 | "vector select expects a vector selector!"); |
| 15821 | |
| 15822 | EVT IntVT = Cond.getValueType(); |
| 15823 | bool TValIsAllOnes = ISD::isBuildVectorAllOnes(LHS.getNode()); |
| 15824 | bool FValIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode()); |
| 15825 | |
| 15826 | if (!TValIsAllOnes && !FValIsAllZeros) { |
| 15827 | // Try invert the condition if true value is not all 1s and false value |
| 15828 | // is not all 0s. |
| 15829 | bool TValIsAllZeros = ISD::isBuildVectorAllZeros(LHS.getNode()); |
| 15830 | bool FValIsAllOnes = ISD::isBuildVectorAllOnes(RHS.getNode()); |
| 15831 | |
| 15832 | if (TValIsAllZeros || FValIsAllOnes) { |
| 15833 | SDValue CC = Cond.getOperand(2); |
| 15834 | ISD::CondCode NewCC = |
| 15835 | ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(), |
| 15836 | Cond.getOperand(0).getValueType().isInteger()); |
| 15837 | Cond = DAG.getSetCC(DL, IntVT, Cond.getOperand(0), Cond.getOperand(1), NewCC); |
| 15838 | std::swap(LHS, RHS); |
| 15839 | TValIsAllOnes = FValIsAllOnes; |
| 15840 | FValIsAllZeros = TValIsAllZeros; |
| 15841 | } |
| 15842 | } |
| 15843 | |
| 15844 | if (TValIsAllOnes || FValIsAllZeros) { |
| 15845 | SDValue Ret; |
| 15846 | |
| 15847 | if (TValIsAllOnes && FValIsAllZeros) |
| 15848 | Ret = Cond; |
| 15849 | else if (TValIsAllOnes) |
| 15850 | Ret = DAG.getNode(ISD::OR, DL, IntVT, Cond, |
| 15851 | DAG.getNode(ISD::BITCAST, DL, IntVT, RHS)); |
| 15852 | else if (FValIsAllZeros) |
| 15853 | Ret = DAG.getNode(ISD::AND, DL, IntVT, Cond, |
| 15854 | DAG.getNode(ISD::BITCAST, DL, IntVT, LHS)); |
| 15855 | |
| 15856 | return DAG.getNode(ISD::BITCAST, DL, VT, Ret); |
| 15857 | } |
| 15858 | } |
| 15859 | |
| Nadav Rotem | cc61656 | 2012-01-15 19:27:55 +0000 | [diff] [blame] | 15860 | // If we know that this node is legal then we know that it is going to be |
| 15861 | // matched by one of the SSE/AVX BLEND instructions. These instructions only |
| 15862 | // depend on the highest bit in each word. Try to use SimplifyDemandedBits |
| 15863 | // to simplify previous instructions. |
| 15864 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 15865 | if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() && |
| Nadav Rotem | bdcae38 | 2012-06-07 20:53:48 +0000 | [diff] [blame] | 15866 | !DCI.isBeforeLegalize() && TLI.isOperationLegal(ISD::VSELECT, VT)) { |
| Nadav Rotem | cc61656 | 2012-01-15 19:27:55 +0000 | [diff] [blame] | 15867 | unsigned BitWidth = Cond.getValueType().getScalarType().getSizeInBits(); |
| Nadav Rotem | bdcae38 | 2012-06-07 20:53:48 +0000 | [diff] [blame] | 15868 | |
| 15869 | // Don't optimize vector selects that map to mask-registers. |
| 15870 | if (BitWidth == 1) |
| 15871 | return SDValue(); |
| 15872 | |
| Nadav Rotem | cc61656 | 2012-01-15 19:27:55 +0000 | [diff] [blame] | 15873 | assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size"); |
| 15874 | APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1); |
| 15875 | |
| 15876 | APInt KnownZero, KnownOne; |
| 15877 | TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(), |
| 15878 | DCI.isBeforeLegalizeOps()); |
| 15879 | if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) || |
| 15880 | TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne, TLO)) |
| 15881 | DCI.CommitTargetLoweringOpt(TLO); |
| 15882 | } |
| 15883 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 15884 | return SDValue(); |
| Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 15885 | } |
| 15886 | |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 15887 | // Check whether a boolean test is testing a boolean value generated by |
| 15888 | // X86ISD::SETCC. If so, return the operand of that SETCC and proper condition |
| 15889 | // code. |
| 15890 | // |
| 15891 | // Simplify the following patterns: |
| 15892 | // (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or |
| 15893 | // (Op (CMP (SETCC Cond EFLAGS) 0) NEQ) |
| 15894 | // to (Op EFLAGS Cond) |
| 15895 | // |
| 15896 | // (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or |
| 15897 | // (Op (CMP (SETCC Cond EFLAGS) 1) NEQ) |
| 15898 | // to (Op EFLAGS !Cond) |
| 15899 | // |
| 15900 | // where Op could be BRCOND or CMOV. |
| 15901 | // |
| Michael Liao | dbf8b5b | 2012-08-28 03:34:40 +0000 | [diff] [blame] | 15902 | static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) { |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 15903 | // Quit if not CMP and SUB with its value result used. |
| 15904 | if (Cmp.getOpcode() != X86ISD::CMP && |
| 15905 | (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0))) |
| 15906 | return SDValue(); |
| 15907 | |
| 15908 | // Quit if not used as a boolean value. |
| 15909 | if (CC != X86::COND_E && CC != X86::COND_NE) |
| 15910 | return SDValue(); |
| 15911 | |
| 15912 | // Check CMP operands. One of them should be 0 or 1 and the other should be |
| 15913 | // an SetCC or extended from it. |
| 15914 | SDValue Op1 = Cmp.getOperand(0); |
| 15915 | SDValue Op2 = Cmp.getOperand(1); |
| 15916 | |
| 15917 | SDValue SetCC; |
| 15918 | const ConstantSDNode* C = 0; |
| 15919 | bool needOppositeCond = (CC == X86::COND_E); |
| Michael Liao | 959ddbb | 2013-04-11 04:43:09 +0000 | [diff] [blame] | 15920 | bool checkAgainstTrue = false; // Is it a comparison against 1? |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 15921 | |
| 15922 | if ((C = dyn_cast<ConstantSDNode>(Op1))) |
| 15923 | SetCC = Op2; |
| 15924 | else if ((C = dyn_cast<ConstantSDNode>(Op2))) |
| 15925 | SetCC = Op1; |
| 15926 | else // Quit if all operands are not constants. |
| 15927 | return SDValue(); |
| 15928 | |
| Michael Liao | 959ddbb | 2013-04-11 04:43:09 +0000 | [diff] [blame] | 15929 | if (C->getZExtValue() == 1) { |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 15930 | needOppositeCond = !needOppositeCond; |
| Michael Liao | 959ddbb | 2013-04-11 04:43:09 +0000 | [diff] [blame] | 15931 | checkAgainstTrue = true; |
| 15932 | } else if (C->getZExtValue() != 0) |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 15933 | // Quit if the constant is neither 0 or 1. |
| 15934 | return SDValue(); |
| 15935 | |
| Michael Liao | 959ddbb | 2013-04-11 04:43:09 +0000 | [diff] [blame] | 15936 | bool truncatedToBoolWithAnd = false; |
| 15937 | // Skip (zext $x), (trunc $x), or (and $x, 1) node. |
| 15938 | while (SetCC.getOpcode() == ISD::ZERO_EXTEND || |
| 15939 | SetCC.getOpcode() == ISD::TRUNCATE || |
| 15940 | SetCC.getOpcode() == ISD::AND) { |
| 15941 | if (SetCC.getOpcode() == ISD::AND) { |
| 15942 | int OpIdx = -1; |
| 15943 | ConstantSDNode *CS; |
| 15944 | if ((CS = dyn_cast<ConstantSDNode>(SetCC.getOperand(0))) && |
| 15945 | CS->getZExtValue() == 1) |
| 15946 | OpIdx = 1; |
| 15947 | if ((CS = dyn_cast<ConstantSDNode>(SetCC.getOperand(1))) && |
| 15948 | CS->getZExtValue() == 1) |
| 15949 | OpIdx = 0; |
| 15950 | if (OpIdx == -1) |
| 15951 | break; |
| 15952 | SetCC = SetCC.getOperand(OpIdx); |
| 15953 | truncatedToBoolWithAnd = true; |
| 15954 | } else |
| 15955 | SetCC = SetCC.getOperand(0); |
| 15956 | } |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 15957 | |
| Michael Liao | 7fdc66b | 2012-09-10 16:36:16 +0000 | [diff] [blame] | 15958 | switch (SetCC.getOpcode()) { |
| Michael Liao | 959ddbb | 2013-04-11 04:43:09 +0000 | [diff] [blame] | 15959 | case X86ISD::SETCC_CARRY: |
| 15960 | // Since SETCC_CARRY gives output based on R = CF ? ~0 : 0, it's unsafe to |
| 15961 | // simplify it if the result of SETCC_CARRY is not canonicalized to 0 or 1, |
| 15962 | // i.e. it's a comparison against true but the result of SETCC_CARRY is not |
| 15963 | // truncated to i1 using 'and'. |
| 15964 | if (checkAgainstTrue && !truncatedToBoolWithAnd) |
| 15965 | break; |
| 15966 | assert(X86::CondCode(SetCC.getConstantOperandVal(0)) == X86::COND_B && |
| 15967 | "Invalid use of SETCC_CARRY!"); |
| 15968 | // FALL THROUGH |
| Michael Liao | 7fdc66b | 2012-09-10 16:36:16 +0000 | [diff] [blame] | 15969 | case X86ISD::SETCC: |
| 15970 | // Set the condition code or opposite one if necessary. |
| 15971 | CC = X86::CondCode(SetCC.getConstantOperandVal(0)); |
| 15972 | if (needOppositeCond) |
| 15973 | CC = X86::GetOppositeBranchCondition(CC); |
| 15974 | return SetCC.getOperand(1); |
| 15975 | case X86ISD::CMOV: { |
| 15976 | // Check whether false/true value has canonical one, i.e. 0 or 1. |
| 15977 | ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0)); |
| 15978 | ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1)); |
| 15979 | // Quit if true value is not a constant. |
| 15980 | if (!TVal) |
| 15981 | return SDValue(); |
| 15982 | // Quit if false value is not a constant. |
| 15983 | if (!FVal) { |
| Michael Liao | 7fdc66b | 2012-09-10 16:36:16 +0000 | [diff] [blame] | 15984 | SDValue Op = SetCC.getOperand(0); |
| Michael Liao | 258d9b7 | 2013-03-28 23:38:52 +0000 | [diff] [blame] | 15985 | // Skip 'zext' or 'trunc' node. |
| 15986 | if (Op.getOpcode() == ISD::ZERO_EXTEND || |
| 15987 | Op.getOpcode() == ISD::TRUNCATE) |
| 15988 | Op = Op.getOperand(0); |
| Michael Liao | c26392a | 2013-03-28 23:41:26 +0000 | [diff] [blame] | 15989 | // A special case for rdrand/rdseed, where 0 is set if false cond is |
| 15990 | // found. |
| 15991 | if ((Op.getOpcode() != X86ISD::RDRAND && |
| 15992 | Op.getOpcode() != X86ISD::RDSEED) || Op.getResNo() != 0) |
| Michael Liao | 7fdc66b | 2012-09-10 16:36:16 +0000 | [diff] [blame] | 15993 | return SDValue(); |
| 15994 | } |
| 15995 | // Quit if false value is not the constant 0 or 1. |
| 15996 | bool FValIsFalse = true; |
| 15997 | if (FVal && FVal->getZExtValue() != 0) { |
| 15998 | if (FVal->getZExtValue() != 1) |
| 15999 | return SDValue(); |
| 16000 | // If FVal is 1, opposite cond is needed. |
| 16001 | needOppositeCond = !needOppositeCond; |
| 16002 | FValIsFalse = false; |
| 16003 | } |
| 16004 | // Quit if TVal is not the constant opposite of FVal. |
| 16005 | if (FValIsFalse && TVal->getZExtValue() != 1) |
| 16006 | return SDValue(); |
| 16007 | if (!FValIsFalse && TVal->getZExtValue() != 0) |
| 16008 | return SDValue(); |
| 16009 | CC = X86::CondCode(SetCC.getConstantOperandVal(2)); |
| 16010 | if (needOppositeCond) |
| 16011 | CC = X86::GetOppositeBranchCondition(CC); |
| 16012 | return SetCC.getOperand(3); |
| 16013 | } |
| 16014 | } |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 16015 | |
| Michael Liao | 7fdc66b | 2012-09-10 16:36:16 +0000 | [diff] [blame] | 16016 | return SDValue(); |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 16017 | } |
| 16018 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16019 | /// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL] |
| 16020 | static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG, |
| Michael Liao | dbf8b5b | 2012-08-28 03:34:40 +0000 | [diff] [blame] | 16021 | TargetLowering::DAGCombinerInfo &DCI, |
| 16022 | const X86Subtarget *Subtarget) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16023 | SDLoc DL(N); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16024 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16025 | // If the flag operand isn't dead, don't touch this CMOV. |
| 16026 | if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty()) |
| 16027 | return SDValue(); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16028 | |
| Evan Cheng | b5a55d9 | 2011-05-24 01:48:22 +0000 | [diff] [blame] | 16029 | SDValue FalseOp = N->getOperand(0); |
| 16030 | SDValue TrueOp = N->getOperand(1); |
| 16031 | X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2); |
| 16032 | SDValue Cond = N->getOperand(3); |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 16033 | |
| Evan Cheng | b5a55d9 | 2011-05-24 01:48:22 +0000 | [diff] [blame] | 16034 | if (CC == X86::COND_E || CC == X86::COND_NE) { |
| 16035 | switch (Cond.getOpcode()) { |
| 16036 | default: break; |
| 16037 | case X86ISD::BSR: |
| 16038 | case X86ISD::BSF: |
| 16039 | // If operand of BSR / BSF are proven never zero, then ZF cannot be set. |
| 16040 | if (DAG.isKnownNeverZero(Cond.getOperand(0))) |
| 16041 | return (CC == X86::COND_E) ? FalseOp : TrueOp; |
| 16042 | } |
| 16043 | } |
| 16044 | |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 16045 | SDValue Flags; |
| 16046 | |
| Michael Liao | dbf8b5b | 2012-08-28 03:34:40 +0000 | [diff] [blame] | 16047 | Flags = checkBoolTestSetCCCombine(Cond, CC); |
| Michael Liao | 9eac20a | 2012-08-11 23:47:06 +0000 | [diff] [blame] | 16048 | if (Flags.getNode() && |
| 16049 | // Extra check as FCMOV only supports a subset of X86 cond. |
| Michael Liao | 7859f43 | 2012-09-06 07:11:22 +0000 | [diff] [blame] | 16050 | (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) { |
| Michael Liao | dbf8b5b | 2012-08-28 03:34:40 +0000 | [diff] [blame] | 16051 | SDValue Ops[] = { FalseOp, TrueOp, |
| 16052 | DAG.getConstant(CC, MVT::i8), Flags }; |
| 16053 | return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(), |
| 16054 | Ops, array_lengthof(Ops)); |
| 16055 | } |
| 16056 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16057 | // If this is a select between two integer constants, try to do some |
| 16058 | // optimizations. Note that the operands are ordered the opposite of SELECT |
| 16059 | // operands. |
| Evan Cheng | b5a55d9 | 2011-05-24 01:48:22 +0000 | [diff] [blame] | 16060 | if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) { |
| 16061 | if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) { |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16062 | // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is |
| 16063 | // larger than FalseC (the false value). |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16064 | if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) { |
| 16065 | CC = X86::GetOppositeBranchCondition(CC); |
| 16066 | std::swap(TrueC, FalseC); |
| NAKAMURA Takumi | e268745 | 2012-10-16 06:28:34 +0000 | [diff] [blame] | 16067 | std::swap(TrueOp, FalseOp); |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16068 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16069 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16070 | // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0. |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 16071 | // This is efficient for any integer data type (including i8/i16) and |
| 16072 | // shift amount. |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16073 | if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 16074 | Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 16075 | DAG.getConstant(CC, MVT::i8), Cond); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16076 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16077 | // Zero extend the condition if needed. |
| 16078 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16079 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16080 | unsigned ShAmt = TrueC->getAPIntValue().logBase2(); |
| 16081 | Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 16082 | DAG.getConstant(ShAmt, MVT::i8)); |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16083 | if (N->getNumValues() == 2) // Dead flag value? |
| 16084 | return DCI.CombineTo(N, Cond, SDValue()); |
| 16085 | return Cond; |
| 16086 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16087 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 16088 | // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient |
| 16089 | // for any integer data type, including i8/i16. |
| Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 16090 | if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 16091 | Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 16092 | DAG.getConstant(CC, MVT::i8), Cond); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16093 | |
| Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 16094 | // Zero extend the condition if needed. |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 16095 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, |
| 16096 | FalseC->getValueType(0), Cond); |
| Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 16097 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| 16098 | SDValue(FalseC, 0)); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16099 | |
| Chris Lattner | 97a29a5 | 2009-03-13 05:22:11 +0000 | [diff] [blame] | 16100 | if (N->getNumValues() == 2) // Dead flag value? |
| 16101 | return DCI.CombineTo(N, Cond, SDValue()); |
| 16102 | return Cond; |
| 16103 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16104 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 16105 | // Optimize cases that will turn into an LEA instruction. This requires |
| 16106 | // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9). |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 16107 | if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) { |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 16108 | uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue(); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 16109 | if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16110 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 16111 | bool isFastMultiplier = false; |
| 16112 | if (Diff < 10) { |
| 16113 | switch ((unsigned char)Diff) { |
| 16114 | default: break; |
| 16115 | case 1: // result = add base, cond |
| 16116 | case 2: // result = lea base( , cond*2) |
| 16117 | case 3: // result = lea base(cond, cond*2) |
| 16118 | case 4: // result = lea base( , cond*4) |
| 16119 | case 5: // result = lea base(cond, cond*4) |
| 16120 | case 8: // result = lea base( , cond*8) |
| 16121 | case 9: // result = lea base(cond, cond*8) |
| 16122 | isFastMultiplier = true; |
| 16123 | break; |
| 16124 | } |
| 16125 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16126 | |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 16127 | if (isFastMultiplier) { |
| 16128 | APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue(); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 16129 | Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8, |
| 16130 | DAG.getConstant(CC, MVT::i8), Cond); |
| Chris Lattner | cee56e7 | 2009-03-13 05:53:31 +0000 | [diff] [blame] | 16131 | // Zero extend the condition if needed. |
| 16132 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0), |
| 16133 | Cond); |
| 16134 | // Scale the condition by the difference. |
| 16135 | if (Diff != 1) |
| 16136 | Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond, |
| 16137 | DAG.getConstant(Diff, Cond.getValueType())); |
| 16138 | |
| 16139 | // Add the base if non-zero. |
| 16140 | if (FalseC->getAPIntValue() != 0) |
| 16141 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, |
| 16142 | SDValue(FalseC, 0)); |
| 16143 | if (N->getNumValues() == 2) // Dead flag value? |
| 16144 | return DCI.CombineTo(N, Cond, SDValue()); |
| 16145 | return Cond; |
| 16146 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16147 | } |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16148 | } |
| 16149 | } |
| NAKAMURA Takumi | e268745 | 2012-10-16 06:28:34 +0000 | [diff] [blame] | 16150 | |
| 16151 | // Handle these cases: |
| 16152 | // (select (x != c), e, c) -> select (x != c), e, x), |
| 16153 | // (select (x == c), c, e) -> select (x == c), x, e) |
| 16154 | // where the c is an integer constant, and the "select" is the combination |
| 16155 | // of CMOV and CMP. |
| 16156 | // |
| 16157 | // The rationale for this change is that the conditional-move from a constant |
| 16158 | // needs two instructions, however, conditional-move from a register needs |
| 16159 | // only one instruction. |
| 16160 | // |
| 16161 | // CAVEAT: By replacing a constant with a symbolic value, it may obscure |
| 16162 | // some instruction-combining opportunities. This opt needs to be |
| 16163 | // postponed as late as possible. |
| 16164 | // |
| 16165 | if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) { |
| 16166 | // the DCI.xxxx conditions are provided to postpone the optimization as |
| 16167 | // late as possible. |
| 16168 | |
| 16169 | ConstantSDNode *CmpAgainst = 0; |
| 16170 | if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) && |
| 16171 | (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) && |
| Jakub Staszak | 30fcfc3 | 2013-02-16 13:34:26 +0000 | [diff] [blame] | 16172 | !isa<ConstantSDNode>(Cond.getOperand(0))) { |
| NAKAMURA Takumi | e268745 | 2012-10-16 06:28:34 +0000 | [diff] [blame] | 16173 | |
| 16174 | if (CC == X86::COND_NE && |
| 16175 | CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) { |
| 16176 | CC = X86::GetOppositeBranchCondition(CC); |
| 16177 | std::swap(TrueOp, FalseOp); |
| 16178 | } |
| 16179 | |
| 16180 | if (CC == X86::COND_E && |
| 16181 | CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) { |
| 16182 | SDValue Ops[] = { FalseOp, Cond.getOperand(0), |
| 16183 | DAG.getConstant(CC, MVT::i8), Cond }; |
| 16184 | return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops, |
| 16185 | array_lengthof(Ops)); |
| 16186 | } |
| 16187 | } |
| 16188 | } |
| 16189 | |
| Chris Lattner | d1980a5 | 2009-03-12 06:52:53 +0000 | [diff] [blame] | 16190 | return SDValue(); |
| 16191 | } |
| 16192 | |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16193 | /// PerformMulCombine - Optimize a single multiply with constant into two |
| 16194 | /// in order to implement it with two cheaper instructions, e.g. |
| 16195 | /// LEA + SHL, LEA + LEA. |
| 16196 | static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG, |
| 16197 | TargetLowering::DAGCombinerInfo &DCI) { |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16198 | if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) |
| 16199 | return SDValue(); |
| 16200 | |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 16201 | EVT VT = N->getValueType(0); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 16202 | if (VT != MVT::i64) |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16203 | return SDValue(); |
| 16204 | |
| 16205 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); |
| 16206 | if (!C) |
| 16207 | return SDValue(); |
| 16208 | uint64_t MulAmt = C->getZExtValue(); |
| 16209 | if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9) |
| 16210 | return SDValue(); |
| 16211 | |
| 16212 | uint64_t MulAmt1 = 0; |
| 16213 | uint64_t MulAmt2 = 0; |
| 16214 | if ((MulAmt % 9) == 0) { |
| 16215 | MulAmt1 = 9; |
| 16216 | MulAmt2 = MulAmt / 9; |
| 16217 | } else if ((MulAmt % 5) == 0) { |
| 16218 | MulAmt1 = 5; |
| 16219 | MulAmt2 = MulAmt / 5; |
| 16220 | } else if ((MulAmt % 3) == 0) { |
| 16221 | MulAmt1 = 3; |
| 16222 | MulAmt2 = MulAmt / 3; |
| 16223 | } |
| 16224 | if (MulAmt2 && |
| 16225 | (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){ |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16226 | SDLoc DL(N); |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16227 | |
| 16228 | if (isPowerOf2_64(MulAmt2) && |
| 16229 | !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD)) |
| 16230 | // If second multiplifer is pow2, issue it first. We want the multiply by |
| 16231 | // 3, 5, or 9 to be folded into the addressing mode unless the lone use |
| 16232 | // is an add. |
| 16233 | std::swap(MulAmt1, MulAmt2); |
| 16234 | |
| 16235 | SDValue NewMul; |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16236 | if (isPowerOf2_64(MulAmt1)) |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16237 | NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 16238 | DAG.getConstant(Log2_64(MulAmt1), MVT::i8)); |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16239 | else |
| Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 16240 | NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0), |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16241 | DAG.getConstant(MulAmt1, VT)); |
| 16242 | |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16243 | if (isPowerOf2_64(MulAmt2)) |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16244 | NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul, |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 16245 | DAG.getConstant(Log2_64(MulAmt2), MVT::i8)); |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 16246 | else |
| Evan Cheng | 73f24c9 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 16247 | NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul, |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16248 | DAG.getConstant(MulAmt2, VT)); |
| 16249 | |
| 16250 | // Do not add new nodes to DAG combiner worklist. |
| 16251 | DCI.CombineTo(N, NewMul, false); |
| 16252 | } |
| 16253 | return SDValue(); |
| 16254 | } |
| 16255 | |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 16256 | static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) { |
| 16257 | SDValue N0 = N->getOperand(0); |
| 16258 | SDValue N1 = N->getOperand(1); |
| 16259 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); |
| 16260 | EVT VT = N0.getValueType(); |
| 16261 | |
| 16262 | // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2)) |
| 16263 | // since the result of setcc_c is all zero's or all ones. |
| Nadav Rotem | fb0dfbb | 2011-10-30 13:24:22 +0000 | [diff] [blame] | 16264 | if (VT.isInteger() && !VT.isVector() && |
| 16265 | N1C && N0.getOpcode() == ISD::AND && |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 16266 | N0.getOperand(1).getOpcode() == ISD::Constant) { |
| 16267 | SDValue N00 = N0.getOperand(0); |
| 16268 | if (N00.getOpcode() == X86ISD::SETCC_CARRY || |
| 16269 | ((N00.getOpcode() == ISD::ANY_EXTEND || |
| 16270 | N00.getOpcode() == ISD::ZERO_EXTEND) && |
| 16271 | N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY)) { |
| 16272 | APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue(); |
| 16273 | APInt ShAmt = N1C->getAPIntValue(); |
| 16274 | Mask = Mask.shl(ShAmt); |
| 16275 | if (Mask != 0) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16276 | return DAG.getNode(ISD::AND, SDLoc(N), VT, |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 16277 | N00, DAG.getConstant(Mask, VT)); |
| 16278 | } |
| 16279 | } |
| 16280 | |
| Nadav Rotem | fb0dfbb | 2011-10-30 13:24:22 +0000 | [diff] [blame] | 16281 | // Hardware support for vector shifts is sparse which makes us scalarize the |
| 16282 | // vector operations in many cases. Also, on sandybridge ADD is faster than |
| 16283 | // shl. |
| 16284 | // (shl V, 1) -> add V,V |
| 16285 | if (isSplatVector(N1.getNode())) { |
| 16286 | assert(N0.getValueType().isVector() && "Invalid vector shift type"); |
| 16287 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1->getOperand(0)); |
| 16288 | // We shift all of the values by one. In many cases we do not have |
| 16289 | // hardware support for this operation. This is better expressed as an ADD |
| 16290 | // of two values. |
| 16291 | if (N1C && (1 == N1C->getZExtValue())) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16292 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N0); |
| Nadav Rotem | fb0dfbb | 2011-10-30 13:24:22 +0000 | [diff] [blame] | 16293 | } |
| 16294 | } |
| 16295 | |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 16296 | return SDValue(); |
| 16297 | } |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 16298 | |
| Nadav Rotem | 0fb6523 | 2013-05-04 23:24:56 +0000 | [diff] [blame] | 16299 | /// PerformShiftCombine - Combine shifts. |
| Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 16300 | static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG, |
| Mon P Wang | 845b189 | 2012-02-01 22:15:20 +0000 | [diff] [blame] | 16301 | TargetLowering::DAGCombinerInfo &DCI, |
| Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 16302 | const X86Subtarget *Subtarget) { |
| Nadav Rotem | fb0dfbb | 2011-10-30 13:24:22 +0000 | [diff] [blame] | 16303 | if (N->getOpcode() == ISD::SHL) { |
| 16304 | SDValue V = PerformSHLCombine(N, DAG); |
| 16305 | if (V.getNode()) return V; |
| 16306 | } |
| Evan Cheng | ad9c0a3 | 2009-12-15 00:53:42 +0000 | [diff] [blame] | 16307 | |
| Michael Liao | 42317cc | 2013-03-20 02:33:21 +0000 | [diff] [blame] | 16308 | return SDValue(); |
| Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 16309 | } |
| 16310 | |
| Stuart Hastings | 865f093 | 2011-06-03 23:53:54 +0000 | [diff] [blame] | 16311 | // CMPEQCombine - Recognize the distinctive (AND (setcc ...) (setcc ..)) |
| 16312 | // where both setccs reference the same FP CMP, and rewrite for CMPEQSS |
| 16313 | // and friends. Likewise for OR -> CMPNEQSS. |
| 16314 | static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG, |
| 16315 | TargetLowering::DAGCombinerInfo &DCI, |
| 16316 | const X86Subtarget *Subtarget) { |
| 16317 | unsigned opcode; |
| 16318 | |
| 16319 | // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but |
| 16320 | // we're requiring SSE2 for both. |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 16321 | if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) { |
| Stuart Hastings | 865f093 | 2011-06-03 23:53:54 +0000 | [diff] [blame] | 16322 | SDValue N0 = N->getOperand(0); |
| 16323 | SDValue N1 = N->getOperand(1); |
| 16324 | SDValue CMP0 = N0->getOperand(1); |
| 16325 | SDValue CMP1 = N1->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16326 | SDLoc DL(N); |
| Stuart Hastings | 865f093 | 2011-06-03 23:53:54 +0000 | [diff] [blame] | 16327 | |
| 16328 | // The SETCCs should both refer to the same CMP. |
| 16329 | if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1) |
| 16330 | return SDValue(); |
| 16331 | |
| 16332 | SDValue CMP00 = CMP0->getOperand(0); |
| 16333 | SDValue CMP01 = CMP0->getOperand(1); |
| 16334 | EVT VT = CMP00.getValueType(); |
| 16335 | |
| 16336 | if (VT == MVT::f32 || VT == MVT::f64) { |
| 16337 | bool ExpectingFlags = false; |
| 16338 | // Check for any users that want flags: |
| Jakub Staszak | 30fcfc3 | 2013-02-16 13:34:26 +0000 | [diff] [blame] | 16339 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); |
| Stuart Hastings | 865f093 | 2011-06-03 23:53:54 +0000 | [diff] [blame] | 16340 | !ExpectingFlags && UI != UE; ++UI) |
| 16341 | switch (UI->getOpcode()) { |
| 16342 | default: |
| 16343 | case ISD::BR_CC: |
| 16344 | case ISD::BRCOND: |
| 16345 | case ISD::SELECT: |
| 16346 | ExpectingFlags = true; |
| 16347 | break; |
| 16348 | case ISD::CopyToReg: |
| 16349 | case ISD::SIGN_EXTEND: |
| 16350 | case ISD::ZERO_EXTEND: |
| 16351 | case ISD::ANY_EXTEND: |
| 16352 | break; |
| 16353 | } |
| 16354 | |
| 16355 | if (!ExpectingFlags) { |
| 16356 | enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0); |
| 16357 | enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0); |
| 16358 | |
| 16359 | if (cc1 == X86::COND_E || cc1 == X86::COND_NE) { |
| 16360 | X86::CondCode tmp = cc0; |
| 16361 | cc0 = cc1; |
| 16362 | cc1 = tmp; |
| 16363 | } |
| 16364 | |
| 16365 | if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) || |
| 16366 | (cc0 == X86::COND_NE && cc1 == X86::COND_P)) { |
| 16367 | bool is64BitFP = (CMP00.getValueType() == MVT::f64); |
| 16368 | X86ISD::NodeType NTOperator = is64BitFP ? |
| 16369 | X86ISD::FSETCCsd : X86ISD::FSETCCss; |
| 16370 | // FIXME: need symbolic constants for these magic numbers. |
| 16371 | // See X86ATTInstPrinter.cpp:printSSECC(). |
| 16372 | unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4; |
| 16373 | SDValue OnesOrZeroesF = DAG.getNode(NTOperator, DL, MVT::f32, CMP00, CMP01, |
| 16374 | DAG.getConstant(x86cc, MVT::i8)); |
| 16375 | SDValue OnesOrZeroesI = DAG.getNode(ISD::BITCAST, DL, MVT::i32, |
| 16376 | OnesOrZeroesF); |
| 16377 | SDValue ANDed = DAG.getNode(ISD::AND, DL, MVT::i32, OnesOrZeroesI, |
| 16378 | DAG.getConstant(1, MVT::i32)); |
| 16379 | SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, ANDed); |
| 16380 | return OneBitOfTruth; |
| 16381 | } |
| 16382 | } |
| 16383 | } |
| 16384 | } |
| 16385 | return SDValue(); |
| 16386 | } |
| 16387 | |
| Bruno Cardoso Lopes | 863bd9d | 2011-07-25 23:05:32 +0000 | [diff] [blame] | 16388 | /// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector |
| 16389 | /// so it can be folded inside ANDNP. |
| 16390 | static bool CanFoldXORWithAllOnes(const SDNode *N) { |
| 16391 | EVT VT = N->getValueType(0); |
| 16392 | |
| 16393 | // Match direct AllOnes for 128 and 256-bit vectors |
| 16394 | if (ISD::isBuildVectorAllOnes(N)) |
| 16395 | return true; |
| 16396 | |
| 16397 | // Look through a bit convert. |
| 16398 | if (N->getOpcode() == ISD::BITCAST) |
| 16399 | N = N->getOperand(0).getNode(); |
| 16400 | |
| 16401 | // Sometimes the operand may come from a insert_subvector building a 256-bit |
| 16402 | // allones vector |
| Craig Topper | 7a9a28b | 2012-08-12 02:23:29 +0000 | [diff] [blame] | 16403 | if (VT.is256BitVector() && |
| Bill Wendling | 456a925 | 2011-08-04 00:32:58 +0000 | [diff] [blame] | 16404 | N->getOpcode() == ISD::INSERT_SUBVECTOR) { |
| 16405 | SDValue V1 = N->getOperand(0); |
| 16406 | SDValue V2 = N->getOperand(1); |
| 16407 | |
| 16408 | if (V1.getOpcode() == ISD::INSERT_SUBVECTOR && |
| 16409 | V1.getOperand(0).getOpcode() == ISD::UNDEF && |
| 16410 | ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) && |
| 16411 | ISD::isBuildVectorAllOnes(V2.getNode())) |
| 16412 | return true; |
| 16413 | } |
| Bruno Cardoso Lopes | 863bd9d | 2011-07-25 23:05:32 +0000 | [diff] [blame] | 16414 | |
| 16415 | return false; |
| 16416 | } |
| 16417 | |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 16418 | // On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized |
| 16419 | // register. In most cases we actually compare or select YMM-sized registers |
| 16420 | // and mixing the two types creates horrible code. This method optimizes |
| 16421 | // some of the transition sequences. |
| 16422 | static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG, |
| 16423 | TargetLowering::DAGCombinerInfo &DCI, |
| 16424 | const X86Subtarget *Subtarget) { |
| 16425 | EVT VT = N->getValueType(0); |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 16426 | if (!VT.is256BitVector()) |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 16427 | return SDValue(); |
| 16428 | |
| 16429 | assert((N->getOpcode() == ISD::ANY_EXTEND || |
| 16430 | N->getOpcode() == ISD::ZERO_EXTEND || |
| 16431 | N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node"); |
| 16432 | |
| 16433 | SDValue Narrow = N->getOperand(0); |
| 16434 | EVT NarrowVT = Narrow->getValueType(0); |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 16435 | if (!NarrowVT.is128BitVector()) |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 16436 | return SDValue(); |
| 16437 | |
| 16438 | if (Narrow->getOpcode() != ISD::XOR && |
| 16439 | Narrow->getOpcode() != ISD::AND && |
| 16440 | Narrow->getOpcode() != ISD::OR) |
| 16441 | return SDValue(); |
| 16442 | |
| 16443 | SDValue N0 = Narrow->getOperand(0); |
| 16444 | SDValue N1 = Narrow->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16445 | SDLoc DL(Narrow); |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 16446 | |
| 16447 | // The Left side has to be a trunc. |
| 16448 | if (N0.getOpcode() != ISD::TRUNCATE) |
| 16449 | return SDValue(); |
| 16450 | |
| 16451 | // The type of the truncated inputs. |
| 16452 | EVT WideVT = N0->getOperand(0)->getValueType(0); |
| 16453 | if (WideVT != VT) |
| 16454 | return SDValue(); |
| 16455 | |
| 16456 | // The right side has to be a 'trunc' or a constant vector. |
| 16457 | bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE; |
| 16458 | bool RHSConst = (isSplatVector(N1.getNode()) && |
| 16459 | isa<ConstantSDNode>(N1->getOperand(0))); |
| 16460 | if (!RHSTrunc && !RHSConst) |
| 16461 | return SDValue(); |
| 16462 | |
| 16463 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 16464 | |
| 16465 | if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT)) |
| 16466 | return SDValue(); |
| 16467 | |
| 16468 | // Set N0 and N1 to hold the inputs to the new wide operation. |
| 16469 | N0 = N0->getOperand(0); |
| 16470 | if (RHSConst) { |
| 16471 | N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getScalarType(), |
| 16472 | N1->getOperand(0)); |
| 16473 | SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1); |
| 16474 | N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, &C[0], C.size()); |
| 16475 | } else if (RHSTrunc) { |
| 16476 | N1 = N1->getOperand(0); |
| 16477 | } |
| 16478 | |
| 16479 | // Generate the wide operation. |
| Nadav Rotem | e3b2489 | 2013-01-02 17:41:03 +0000 | [diff] [blame] | 16480 | SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1); |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 16481 | unsigned Opcode = N->getOpcode(); |
| 16482 | switch (Opcode) { |
| 16483 | case ISD::ANY_EXTEND: |
| 16484 | return Op; |
| 16485 | case ISD::ZERO_EXTEND: { |
| 16486 | unsigned InBits = NarrowVT.getScalarType().getSizeInBits(); |
| 16487 | APInt Mask = APInt::getAllOnesValue(InBits); |
| 16488 | Mask = Mask.zext(VT.getScalarType().getSizeInBits()); |
| 16489 | return DAG.getNode(ISD::AND, DL, VT, |
| 16490 | Op, DAG.getConstant(Mask, VT)); |
| 16491 | } |
| 16492 | case ISD::SIGN_EXTEND: |
| 16493 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, |
| 16494 | Op, DAG.getValueType(NarrowVT)); |
| 16495 | default: |
| 16496 | llvm_unreachable("Unexpected opcode"); |
| 16497 | } |
| 16498 | } |
| 16499 | |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16500 | static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG, |
| 16501 | TargetLowering::DAGCombinerInfo &DCI, |
| 16502 | const X86Subtarget *Subtarget) { |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 16503 | EVT VT = N->getValueType(0); |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16504 | if (DCI.isBeforeLegalizeOps()) |
| 16505 | return SDValue(); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16506 | |
| Stuart Hastings | 865f093 | 2011-06-03 23:53:54 +0000 | [diff] [blame] | 16507 | SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget); |
| 16508 | if (R.getNode()) |
| 16509 | return R; |
| 16510 | |
| Craig Topper | b926afc | 2012-12-17 05:12:30 +0000 | [diff] [blame] | 16511 | // Create BLSI, and BLSR instructions |
| Craig Topper | b4c9457 | 2011-10-21 06:55:01 +0000 | [diff] [blame] | 16512 | // BLSI is X & (-X) |
| 16513 | // BLSR is X & (X-1) |
| Craig Topper | 54a1117 | 2011-10-14 07:06:56 +0000 | [diff] [blame] | 16514 | if (Subtarget->hasBMI() && (VT == MVT::i32 || VT == MVT::i64)) { |
| 16515 | SDValue N0 = N->getOperand(0); |
| 16516 | SDValue N1 = N->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16517 | SDLoc DL(N); |
| Craig Topper | 54a1117 | 2011-10-14 07:06:56 +0000 | [diff] [blame] | 16518 | |
| Craig Topper | b4c9457 | 2011-10-21 06:55:01 +0000 | [diff] [blame] | 16519 | // Check LHS for neg |
| 16520 | if (N0.getOpcode() == ISD::SUB && N0.getOperand(1) == N1 && |
| 16521 | isZero(N0.getOperand(0))) |
| 16522 | return DAG.getNode(X86ISD::BLSI, DL, VT, N1); |
| 16523 | |
| 16524 | // Check RHS for neg |
| 16525 | if (N1.getOpcode() == ISD::SUB && N1.getOperand(1) == N0 && |
| 16526 | isZero(N1.getOperand(0))) |
| 16527 | return DAG.getNode(X86ISD::BLSI, DL, VT, N0); |
| 16528 | |
| 16529 | // Check LHS for X-1 |
| 16530 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 && |
| 16531 | isAllOnes(N0.getOperand(1))) |
| 16532 | return DAG.getNode(X86ISD::BLSR, DL, VT, N1); |
| 16533 | |
| 16534 | // Check RHS for X-1 |
| 16535 | if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 && |
| 16536 | isAllOnes(N1.getOperand(1))) |
| 16537 | return DAG.getNode(X86ISD::BLSR, DL, VT, N0); |
| 16538 | |
| Craig Topper | 54a1117 | 2011-10-14 07:06:56 +0000 | [diff] [blame] | 16539 | return SDValue(); |
| 16540 | } |
| 16541 | |
| Bruno Cardoso Lopes | 466b022 | 2011-07-13 21:36:51 +0000 | [diff] [blame] | 16542 | // Want to form ANDNP nodes: |
| 16543 | // 1) In the hopes of then easily combining them with OR and AND nodes |
| 16544 | // to form PBLEND/PSIGN. |
| 16545 | // 2) To match ANDN packed intrinsics |
| Bruno Cardoso Lopes | 466b022 | 2011-07-13 21:36:51 +0000 | [diff] [blame] | 16546 | if (VT != MVT::v2i64 && VT != MVT::v4i64) |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16547 | return SDValue(); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16548 | |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16549 | SDValue N0 = N->getOperand(0); |
| 16550 | SDValue N1 = N->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16551 | SDLoc DL(N); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16552 | |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16553 | // Check LHS for vnot |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16554 | if (N0.getOpcode() == ISD::XOR && |
| Bruno Cardoso Lopes | 863bd9d | 2011-07-25 23:05:32 +0000 | [diff] [blame] | 16555 | //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode())) |
| 16556 | CanFoldXORWithAllOnes(N0.getOperand(1).getNode())) |
| Bruno Cardoso Lopes | c1af477 | 2011-07-13 21:36:47 +0000 | [diff] [blame] | 16557 | return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1); |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16558 | |
| 16559 | // Check RHS for vnot |
| 16560 | if (N1.getOpcode() == ISD::XOR && |
| Bruno Cardoso Lopes | 863bd9d | 2011-07-25 23:05:32 +0000 | [diff] [blame] | 16561 | //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode())) |
| 16562 | CanFoldXORWithAllOnes(N1.getOperand(1).getNode())) |
| Bruno Cardoso Lopes | c1af477 | 2011-07-13 21:36:47 +0000 | [diff] [blame] | 16563 | return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16564 | |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16565 | return SDValue(); |
| 16566 | } |
| 16567 | |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16568 | static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG, |
| Evan Cheng | 8b1190a | 2010-04-28 01:18:01 +0000 | [diff] [blame] | 16569 | TargetLowering::DAGCombinerInfo &DCI, |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16570 | const X86Subtarget *Subtarget) { |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 16571 | EVT VT = N->getValueType(0); |
| Evan Cheng | 39cfeec | 2010-04-28 02:25:18 +0000 | [diff] [blame] | 16572 | if (DCI.isBeforeLegalizeOps()) |
| Evan Cheng | 8b1190a | 2010-04-28 01:18:01 +0000 | [diff] [blame] | 16573 | return SDValue(); |
| 16574 | |
| Stuart Hastings | 865f093 | 2011-06-03 23:53:54 +0000 | [diff] [blame] | 16575 | SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget); |
| 16576 | if (R.getNode()) |
| 16577 | return R; |
| 16578 | |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16579 | SDValue N0 = N->getOperand(0); |
| 16580 | SDValue N1 = N->getOperand(1); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16581 | |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16582 | // look for psign/blend |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16583 | if (VT == MVT::v2i64 || VT == MVT::v4i64) { |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 16584 | if (!Subtarget->hasSSSE3() || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 16585 | (VT == MVT::v4i64 && !Subtarget->hasInt256())) |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16586 | return SDValue(); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16587 | |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16588 | // Canonicalize pandn to RHS |
| 16589 | if (N0.getOpcode() == X86ISD::ANDNP) |
| 16590 | std::swap(N0, N1); |
| Lang Hames | 9ffaa6a | 2012-01-10 22:53:20 +0000 | [diff] [blame] | 16591 | // or (and (m, y), (pandn m, x)) |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16592 | if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) { |
| 16593 | SDValue Mask = N1.getOperand(0); |
| 16594 | SDValue X = N1.getOperand(1); |
| 16595 | SDValue Y; |
| 16596 | if (N0.getOperand(0) == Mask) |
| 16597 | Y = N0.getOperand(1); |
| 16598 | if (N0.getOperand(1) == Mask) |
| 16599 | Y = N0.getOperand(0); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16600 | |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16601 | // Check to see if the mask appeared in both the AND and ANDNP and |
| 16602 | if (!Y.getNode()) |
| 16603 | return SDValue(); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16604 | |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16605 | // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them. |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16606 | // Look through mask bitcast. |
| Nadav Rotem | 4ac9081 | 2012-04-01 19:31:22 +0000 | [diff] [blame] | 16607 | if (Mask.getOpcode() == ISD::BITCAST) |
| 16608 | Mask = Mask.getOperand(0); |
| 16609 | if (X.getOpcode() == ISD::BITCAST) |
| 16610 | X = X.getOperand(0); |
| 16611 | if (Y.getOpcode() == ISD::BITCAST) |
| 16612 | Y = Y.getOperand(0); |
| 16613 | |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16614 | EVT MaskVT = Mask.getValueType(); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16615 | |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 16616 | // Validate that the Mask operand is a vector sra node. |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16617 | // FIXME: what to do for bytes, since there is a psignb/pblendvb, but |
| 16618 | // there is no psrai.b |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16619 | unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits(); |
| Michael Liao | 42317cc | 2013-03-20 02:33:21 +0000 | [diff] [blame] | 16620 | unsigned SraAmt = ~0; |
| 16621 | if (Mask.getOpcode() == ISD::SRA) { |
| 16622 | SDValue Amt = Mask.getOperand(1); |
| 16623 | if (isSplatVector(Amt.getNode())) { |
| 16624 | SDValue SclrAmt = Amt->getOperand(0); |
| 16625 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(SclrAmt)) |
| 16626 | SraAmt = C->getZExtValue(); |
| 16627 | } |
| 16628 | } else if (Mask.getOpcode() == X86ISD::VSRAI) { |
| 16629 | SDValue SraC = Mask.getOperand(1); |
| 16630 | SraAmt = cast<ConstantSDNode>(SraC)->getZExtValue(); |
| 16631 | } |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16632 | if ((SraAmt + 1) != EltBits) |
| 16633 | return SDValue(); |
| 16634 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16635 | SDLoc DL(N); |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16636 | |
| 16637 | // Now we know we at least have a plendvb with the mask val. See if |
| 16638 | // we can form a psignb/w/d. |
| 16639 | // psign = x.type == y.type == mask.type && y = sub(0, x); |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16640 | if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X && |
| 16641 | ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) && |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 16642 | X.getValueType() == MaskVT && Y.getValueType() == MaskVT) { |
| 16643 | assert((EltBits == 8 || EltBits == 16 || EltBits == 32) && |
| 16644 | "Unsupported VT for PSIGN"); |
| Nadav Rotem | f8db447 | 2013-02-24 07:09:35 +0000 | [diff] [blame] | 16645 | Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0)); |
| Craig Topper | ed2e13d | 2012-01-22 19:15:14 +0000 | [diff] [blame] | 16646 | return DAG.getNode(ISD::BITCAST, DL, VT, Mask); |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16647 | } |
| 16648 | // PBLENDVB only available on SSE 4.1 |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 16649 | if (!Subtarget->hasSSE41()) |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16650 | return SDValue(); |
| 16651 | |
| 16652 | EVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8; |
| 16653 | |
| 16654 | X = DAG.getNode(ISD::BITCAST, DL, BlendVT, X); |
| 16655 | Y = DAG.getNode(ISD::BITCAST, DL, BlendVT, Y); |
| 16656 | Mask = DAG.getNode(ISD::BITCAST, DL, BlendVT, Mask); |
| Nadav Rotem | 18197d7 | 2011-11-30 10:13:37 +0000 | [diff] [blame] | 16657 | Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X); |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16658 | return DAG.getNode(ISD::BITCAST, DL, VT, Mask); |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16659 | } |
| 16660 | } |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16661 | |
| Craig Topper | 1666cb6 | 2011-11-19 07:07:26 +0000 | [diff] [blame] | 16662 | if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64) |
| 16663 | return SDValue(); |
| 16664 | |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 16665 | // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c) |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16666 | if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL) |
| 16667 | std::swap(N0, N1); |
| 16668 | if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL) |
| 16669 | return SDValue(); |
| Evan Cheng | 8b1190a | 2010-04-28 01:18:01 +0000 | [diff] [blame] | 16670 | if (!N0.hasOneUse() || !N1.hasOneUse()) |
| 16671 | return SDValue(); |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16672 | |
| 16673 | SDValue ShAmt0 = N0.getOperand(1); |
| 16674 | if (ShAmt0.getValueType() != MVT::i8) |
| 16675 | return SDValue(); |
| 16676 | SDValue ShAmt1 = N1.getOperand(1); |
| 16677 | if (ShAmt1.getValueType() != MVT::i8) |
| 16678 | return SDValue(); |
| 16679 | if (ShAmt0.getOpcode() == ISD::TRUNCATE) |
| 16680 | ShAmt0 = ShAmt0.getOperand(0); |
| 16681 | if (ShAmt1.getOpcode() == ISD::TRUNCATE) |
| 16682 | ShAmt1 = ShAmt1.getOperand(0); |
| 16683 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16684 | SDLoc DL(N); |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16685 | unsigned Opc = X86ISD::SHLD; |
| 16686 | SDValue Op0 = N0.getOperand(0); |
| 16687 | SDValue Op1 = N1.getOperand(0); |
| 16688 | if (ShAmt0.getOpcode() == ISD::SUB) { |
| 16689 | Opc = X86ISD::SHRD; |
| 16690 | std::swap(Op0, Op1); |
| 16691 | std::swap(ShAmt0, ShAmt1); |
| 16692 | } |
| 16693 | |
| Evan Cheng | 8b1190a | 2010-04-28 01:18:01 +0000 | [diff] [blame] | 16694 | unsigned Bits = VT.getSizeInBits(); |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16695 | if (ShAmt1.getOpcode() == ISD::SUB) { |
| 16696 | SDValue Sum = ShAmt1.getOperand(0); |
| 16697 | if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) { |
| Dan Gohman | 4e39e9d | 2010-06-24 14:30:44 +0000 | [diff] [blame] | 16698 | SDValue ShAmt1Op1 = ShAmt1.getOperand(1); |
| 16699 | if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE) |
| 16700 | ShAmt1Op1 = ShAmt1Op1.getOperand(0); |
| 16701 | if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0) |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16702 | return DAG.getNode(Opc, DL, VT, |
| 16703 | Op0, Op1, |
| 16704 | DAG.getNode(ISD::TRUNCATE, DL, |
| 16705 | MVT::i8, ShAmt0)); |
| 16706 | } |
| 16707 | } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) { |
| 16708 | ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0); |
| 16709 | if (ShAmt0C && |
| Evan Cheng | 8b1190a | 2010-04-28 01:18:01 +0000 | [diff] [blame] | 16710 | ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits) |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16711 | return DAG.getNode(Opc, DL, VT, |
| 16712 | N0.getOperand(0), N1.getOperand(0), |
| 16713 | DAG.getNode(ISD::TRUNCATE, DL, |
| 16714 | MVT::i8, ShAmt0)); |
| 16715 | } |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 16716 | |
| Evan Cheng | 760d194 | 2010-01-04 21:22:48 +0000 | [diff] [blame] | 16717 | return SDValue(); |
| 16718 | } |
| 16719 | |
| Manman Ren | 9236362 | 2012-06-07 22:39:10 +0000 | [diff] [blame] | 16720 | // Generate NEG and CMOV for integer abs. |
| 16721 | static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) { |
| 16722 | EVT VT = N->getValueType(0); |
| 16723 | |
| 16724 | // Since X86 does not have CMOV for 8-bit integer, we don't convert |
| 16725 | // 8-bit integer abs to NEG and CMOV. |
| 16726 | if (VT.isInteger() && VT.getSizeInBits() == 8) |
| 16727 | return SDValue(); |
| 16728 | |
| 16729 | SDValue N0 = N->getOperand(0); |
| 16730 | SDValue N1 = N->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16731 | SDLoc DL(N); |
| Manman Ren | 9236362 | 2012-06-07 22:39:10 +0000 | [diff] [blame] | 16732 | |
| 16733 | // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1) |
| 16734 | // and change it to SUB and CMOV. |
| 16735 | if (VT.isInteger() && N->getOpcode() == ISD::XOR && |
| 16736 | N0.getOpcode() == ISD::ADD && |
| 16737 | N0.getOperand(1) == N1 && |
| 16738 | N1.getOpcode() == ISD::SRA && |
| 16739 | N1.getOperand(0) == N0.getOperand(0)) |
| 16740 | if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1))) |
| 16741 | if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) { |
| 16742 | // Generate SUB & CMOV. |
| 16743 | SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32), |
| 16744 | DAG.getConstant(0, VT), N0.getOperand(0)); |
| 16745 | |
| 16746 | SDValue Ops[] = { N0.getOperand(0), Neg, |
| 16747 | DAG.getConstant(X86::COND_GE, MVT::i8), |
| 16748 | SDValue(Neg.getNode(), 1) }; |
| 16749 | return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue), |
| 16750 | Ops, array_lengthof(Ops)); |
| 16751 | } |
| 16752 | return SDValue(); |
| 16753 | } |
| 16754 | |
| Craig Topper | 3738ccd | 2011-12-27 06:27:23 +0000 | [diff] [blame] | 16755 | // PerformXorCombine - Attempts to turn XOR nodes into BLSMSK nodes |
| Craig Topper | b4c9457 | 2011-10-21 06:55:01 +0000 | [diff] [blame] | 16756 | static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG, |
| 16757 | TargetLowering::DAGCombinerInfo &DCI, |
| 16758 | const X86Subtarget *Subtarget) { |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 16759 | EVT VT = N->getValueType(0); |
| Craig Topper | b4c9457 | 2011-10-21 06:55:01 +0000 | [diff] [blame] | 16760 | if (DCI.isBeforeLegalizeOps()) |
| 16761 | return SDValue(); |
| 16762 | |
| Manman Ren | 45d53b8 | 2012-06-08 18:58:26 +0000 | [diff] [blame] | 16763 | if (Subtarget->hasCMov()) { |
| 16764 | SDValue RV = performIntegerAbsCombine(N, DAG); |
| 16765 | if (RV.getNode()) |
| 16766 | return RV; |
| 16767 | } |
| Manman Ren | 9236362 | 2012-06-07 22:39:10 +0000 | [diff] [blame] | 16768 | |
| 16769 | // Try forming BMI if it is available. |
| 16770 | if (!Subtarget->hasBMI()) |
| 16771 | return SDValue(); |
| 16772 | |
| Craig Topper | b4c9457 | 2011-10-21 06:55:01 +0000 | [diff] [blame] | 16773 | if (VT != MVT::i32 && VT != MVT::i64) |
| 16774 | return SDValue(); |
| 16775 | |
| Craig Topper | 3738ccd | 2011-12-27 06:27:23 +0000 | [diff] [blame] | 16776 | assert(Subtarget->hasBMI() && "Creating BLSMSK requires BMI instructions"); |
| 16777 | |
| Craig Topper | b4c9457 | 2011-10-21 06:55:01 +0000 | [diff] [blame] | 16778 | // Create BLSMSK instructions by finding X ^ (X-1) |
| 16779 | SDValue N0 = N->getOperand(0); |
| 16780 | SDValue N1 = N->getOperand(1); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16781 | SDLoc DL(N); |
| Craig Topper | b4c9457 | 2011-10-21 06:55:01 +0000 | [diff] [blame] | 16782 | |
| 16783 | if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1 && |
| 16784 | isAllOnes(N0.getOperand(1))) |
| 16785 | return DAG.getNode(X86ISD::BLSMSK, DL, VT, N1); |
| 16786 | |
| 16787 | if (N1.getOpcode() == ISD::ADD && N1.getOperand(0) == N0 && |
| 16788 | isAllOnes(N1.getOperand(1))) |
| 16789 | return DAG.getNode(X86ISD::BLSMSK, DL, VT, N0); |
| 16790 | |
| 16791 | return SDValue(); |
| 16792 | } |
| 16793 | |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16794 | /// PerformLOADCombine - Do target-specific dag combines on LOAD nodes. |
| 16795 | static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG, |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16796 | TargetLowering::DAGCombinerInfo &DCI, |
| 16797 | const X86Subtarget *Subtarget) { |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16798 | LoadSDNode *Ld = cast<LoadSDNode>(N); |
| 16799 | EVT RegVT = Ld->getValueType(0); |
| 16800 | EVT MemVT = Ld->getMemoryVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16801 | SDLoc dl(Ld); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16802 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| Nadav Rotem | 48177ac | 2013-01-18 23:10:30 +0000 | [diff] [blame] | 16803 | unsigned RegSz = RegVT.getSizeInBits(); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16804 | |
| Michael Liao | d4584c9 | 2013-03-25 23:50:10 +0000 | [diff] [blame] | 16805 | // On Sandybridge unaligned 256bit loads are inefficient. |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16806 | ISD::LoadExtType Ext = Ld->getExtensionType(); |
| Nadav Rotem | 48177ac | 2013-01-18 23:10:30 +0000 | [diff] [blame] | 16807 | unsigned Alignment = Ld->getAlignment(); |
| Michael Liao | d4584c9 | 2013-03-25 23:50:10 +0000 | [diff] [blame] | 16808 | bool IsAligned = Alignment == 0 || Alignment >= MemVT.getSizeInBits()/8; |
| Nadav Rotem | 48177ac | 2013-01-18 23:10:30 +0000 | [diff] [blame] | 16809 | if (RegVT.is256BitVector() && !Subtarget->hasInt256() && |
| Nadav Rotem | ba95865 | 2013-01-19 08:38:41 +0000 | [diff] [blame] | 16810 | !DCI.isBeforeLegalizeOps() && !IsAligned && Ext == ISD::NON_EXTLOAD) { |
| Nadav Rotem | 48177ac | 2013-01-18 23:10:30 +0000 | [diff] [blame] | 16811 | unsigned NumElems = RegVT.getVectorNumElements(); |
| Nadav Rotem | ba95865 | 2013-01-19 08:38:41 +0000 | [diff] [blame] | 16812 | if (NumElems < 2) |
| 16813 | return SDValue(); |
| 16814 | |
| Nadav Rotem | 48177ac | 2013-01-18 23:10:30 +0000 | [diff] [blame] | 16815 | SDValue Ptr = Ld->getBasePtr(); |
| 16816 | SDValue Increment = DAG.getConstant(16, TLI.getPointerTy()); |
| 16817 | |
| 16818 | EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(), |
| 16819 | NumElems/2); |
| 16820 | SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr, |
| 16821 | Ld->getPointerInfo(), Ld->isVolatile(), |
| 16822 | Ld->isNonTemporal(), Ld->isInvariant(), |
| 16823 | Alignment); |
| 16824 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); |
| 16825 | SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr, |
| 16826 | Ld->getPointerInfo(), Ld->isVolatile(), |
| 16827 | Ld->isNonTemporal(), Ld->isInvariant(), |
| Michael Liao | d4584c9 | 2013-03-25 23:50:10 +0000 | [diff] [blame] | 16828 | std::min(16U, Alignment)); |
| Nadav Rotem | 48177ac | 2013-01-18 23:10:30 +0000 | [diff] [blame] | 16829 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 16830 | Load1.getValue(1), |
| 16831 | Load2.getValue(1)); |
| 16832 | |
| 16833 | SDValue NewVec = DAG.getUNDEF(RegVT); |
| 16834 | NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl); |
| 16835 | NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl); |
| 16836 | return DCI.CombineTo(N, NewVec, TF, true); |
| 16837 | } |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16838 | |
| Nadav Rotem | ca6f296 | 2011-09-18 19:00:23 +0000 | [diff] [blame] | 16839 | // If this is a vector EXT Load then attempt to optimize it using a |
| Benjamin Kramer | 1734791 | 2012-12-22 11:34:28 +0000 | [diff] [blame] | 16840 | // shuffle. If SSSE3 is not available we may emit an illegal shuffle but the |
| 16841 | // expansion is still better than scalar code. |
| 16842 | // We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise we'll |
| 16843 | // emit a shuffle and a arithmetic shift. |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16844 | // TODO: It is possible to support ZExt by zeroing the undef values |
| 16845 | // during the shuffle phase or after the shuffle. |
| Benjamin Kramer | 1734791 | 2012-12-22 11:34:28 +0000 | [diff] [blame] | 16846 | if (RegVT.isVector() && RegVT.isInteger() && Subtarget->hasSSE2() && |
| 16847 | (Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)) { |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16848 | assert(MemVT != RegVT && "Cannot extend to the same type"); |
| 16849 | assert(MemVT.isVector() && "Must load a vector from memory"); |
| 16850 | |
| 16851 | unsigned NumElems = RegVT.getVectorNumElements(); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16852 | unsigned MemSz = MemVT.getSizeInBits(); |
| 16853 | assert(RegSz > MemSz && "Register size must be greater than the mem size"); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16854 | |
| Elena Demikhovsky | 4b97731 | 2012-12-19 07:50:20 +0000 | [diff] [blame] | 16855 | if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256()) |
| 16856 | return SDValue(); |
| 16857 | |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16858 | // All sizes must be a power of two. |
| 16859 | if (!isPowerOf2_32(RegSz * MemSz * NumElems)) |
| 16860 | return SDValue(); |
| 16861 | |
| 16862 | // Attempt to load the original value using scalar loads. |
| 16863 | // Find the largest scalar type that divides the total loaded size. |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16864 | MVT SclrLoadTy = MVT::i8; |
| 16865 | for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE; |
| 16866 | tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) { |
| 16867 | MVT Tp = (MVT::SimpleValueType)tp; |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16868 | if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) { |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16869 | SclrLoadTy = Tp; |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16870 | } |
| 16871 | } |
| 16872 | |
| Nadav Rotem | 5cd95e1 | 2012-07-11 13:27:05 +0000 | [diff] [blame] | 16873 | // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64. |
| 16874 | if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 && |
| 16875 | (64 <= MemSz)) |
| 16876 | SclrLoadTy = MVT::f64; |
| 16877 | |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16878 | // Calculate the number of scalar loads that we need to perform |
| 16879 | // in order to load our vector from memory. |
| 16880 | unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits(); |
| Elena Demikhovsky | 4b97731 | 2012-12-19 07:50:20 +0000 | [diff] [blame] | 16881 | if (Ext == ISD::SEXTLOAD && NumLoads > 1) |
| 16882 | return SDValue(); |
| 16883 | |
| 16884 | unsigned loadRegZize = RegSz; |
| 16885 | if (Ext == ISD::SEXTLOAD && RegSz == 256) |
| 16886 | loadRegZize /= 2; |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16887 | |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16888 | // Represent our vector as a sequence of elements which are the |
| 16889 | // largest scalar that we can load. |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16890 | EVT LoadUnitVecVT = EVT::getVectorVT(*DAG.getContext(), SclrLoadTy, |
| Elena Demikhovsky | 4b97731 | 2012-12-19 07:50:20 +0000 | [diff] [blame] | 16891 | loadRegZize/SclrLoadTy.getSizeInBits()); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16892 | |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16893 | // Represent the data using the same element type that is stored in |
| 16894 | // memory. In practice, we ''widen'' MemVT. |
| Eric Christopher | e187e25 | 2013-01-31 00:50:48 +0000 | [diff] [blame] | 16895 | EVT WideVecVT = |
| 16896 | EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(), |
| Elena Demikhovsky | 4b97731 | 2012-12-19 07:50:20 +0000 | [diff] [blame] | 16897 | loadRegZize/MemVT.getScalarType().getSizeInBits()); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16898 | |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16899 | assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() && |
| 16900 | "Invalid vector type"); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16901 | |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16902 | // We can't shuffle using an illegal type. |
| 16903 | if (!TLI.isTypeLegal(WideVecVT)) |
| 16904 | return SDValue(); |
| 16905 | |
| 16906 | SmallVector<SDValue, 8> Chains; |
| 16907 | SDValue Ptr = Ld->getBasePtr(); |
| 16908 | SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits()/8, |
| 16909 | TLI.getPointerTy()); |
| 16910 | SDValue Res = DAG.getUNDEF(LoadUnitVecVT); |
| 16911 | |
| 16912 | for (unsigned i = 0; i < NumLoads; ++i) { |
| 16913 | // Perform a single load. |
| 16914 | SDValue ScalarLoad = DAG.getLoad(SclrLoadTy, dl, Ld->getChain(), |
| 16915 | Ptr, Ld->getPointerInfo(), |
| 16916 | Ld->isVolatile(), Ld->isNonTemporal(), |
| 16917 | Ld->isInvariant(), Ld->getAlignment()); |
| 16918 | Chains.push_back(ScalarLoad.getValue(1)); |
| 16919 | // Create the first element type using SCALAR_TO_VECTOR in order to avoid |
| 16920 | // another round of DAGCombining. |
| 16921 | if (i == 0) |
| 16922 | Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad); |
| 16923 | else |
| 16924 | Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res, |
| 16925 | ScalarLoad, DAG.getIntPtrConstant(i)); |
| 16926 | |
| 16927 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); |
| 16928 | } |
| 16929 | |
| 16930 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], |
| 16931 | Chains.size()); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16932 | |
| 16933 | // Bitcast the loaded value to a vector of the original element type, in |
| 16934 | // the size of the target vector type. |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16935 | SDValue SlicedVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, Res); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16936 | unsigned SizeRatio = RegSz/MemSz; |
| 16937 | |
| Elena Demikhovsky | 4b97731 | 2012-12-19 07:50:20 +0000 | [diff] [blame] | 16938 | if (Ext == ISD::SEXTLOAD) { |
| Benjamin Kramer | 1734791 | 2012-12-22 11:34:28 +0000 | [diff] [blame] | 16939 | // If we have SSE4.1 we can directly emit a VSEXT node. |
| 16940 | if (Subtarget->hasSSE41()) { |
| 16941 | SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec); |
| 16942 | return DCI.CombineTo(N, Sext, TF, true); |
| 16943 | } |
| 16944 | |
| 16945 | // Otherwise we'll shuffle the small elements in the high bits of the |
| 16946 | // larger type and perform an arithmetic shift. If the shift is not legal |
| 16947 | // it's better to scalarize. |
| 16948 | if (!TLI.isOperationLegalOrCustom(ISD::SRA, RegVT)) |
| 16949 | return SDValue(); |
| 16950 | |
| 16951 | // Redistribute the loaded elements into the different locations. |
| 16952 | SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); |
| 16953 | for (unsigned i = 0; i != NumElems; ++i) |
| 16954 | ShuffleVec[i*SizeRatio + SizeRatio-1] = i; |
| 16955 | |
| 16956 | SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec, |
| 16957 | DAG.getUNDEF(WideVecVT), |
| 16958 | &ShuffleVec[0]); |
| 16959 | |
| 16960 | Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff); |
| 16961 | |
| 16962 | // Build the arithmetic shift. |
| 16963 | unsigned Amt = RegVT.getVectorElementType().getSizeInBits() - |
| 16964 | MemVT.getVectorElementType().getSizeInBits(); |
| Benjamin Kramer | 9fa9251 | 2013-02-04 15:19:25 +0000 | [diff] [blame] | 16965 | Shuff = DAG.getNode(ISD::SRA, dl, RegVT, Shuff, |
| 16966 | DAG.getConstant(Amt, RegVT)); |
| Benjamin Kramer | 1734791 | 2012-12-22 11:34:28 +0000 | [diff] [blame] | 16967 | |
| 16968 | return DCI.CombineTo(N, Shuff, TF, true); |
| Elena Demikhovsky | 4b97731 | 2012-12-19 07:50:20 +0000 | [diff] [blame] | 16969 | } |
| Benjamin Kramer | 1734791 | 2012-12-22 11:34:28 +0000 | [diff] [blame] | 16970 | |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16971 | // Redistribute the loaded elements into the different locations. |
| 16972 | SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); |
| Craig Topper | 31a207a | 2012-05-04 06:39:13 +0000 | [diff] [blame] | 16973 | for (unsigned i = 0; i != NumElems; ++i) |
| 16974 | ShuffleVec[i*SizeRatio] = i; |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16975 | |
| 16976 | SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec, |
| Craig Topper | df966f6 | 2012-04-22 19:17:57 +0000 | [diff] [blame] | 16977 | DAG.getUNDEF(WideVecVT), |
| 16978 | &ShuffleVec[0]); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16979 | |
| 16980 | // Bitcast to the requested type. |
| 16981 | Shuff = DAG.getNode(ISD::BITCAST, dl, RegVT, Shuff); |
| 16982 | // Replace the original load with the new sequence |
| 16983 | // and return the new chain. |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 16984 | return DCI.CombineTo(N, Shuff, TF, true); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 16985 | } |
| 16986 | |
| 16987 | return SDValue(); |
| 16988 | } |
| 16989 | |
| Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 16990 | /// PerformSTORECombine - Do target-specific dag combines on STORE nodes. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 16991 | static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG, |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 16992 | const X86Subtarget *Subtarget) { |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 16993 | StoreSDNode *St = cast<StoreSDNode>(N); |
| 16994 | EVT VT = St->getValue().getValueType(); |
| 16995 | EVT StVT = St->getMemoryVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 16996 | SDLoc dl(St); |
| Nadav Rotem | 5e742a3 | 2011-08-11 16:41:21 +0000 | [diff] [blame] | 16997 | SDValue StoredVal = St->getOperand(1); |
| 16998 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 16999 | |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 17000 | // If we are saving a concatenation of two XMM registers, perform two stores. |
| Nadav Rotem | 87d35e8 | 2012-05-19 20:30:08 +0000 | [diff] [blame] | 17001 | // On Sandy Bridge, 256-bit memory operations are executed by two |
| 17002 | // 128-bit ports. However, on Haswell it is better to issue a single 256-bit |
| 17003 | // memory operation. |
| Michael Liao | d4584c9 | 2013-03-25 23:50:10 +0000 | [diff] [blame] | 17004 | unsigned Alignment = St->getAlignment(); |
| 17005 | bool IsAligned = Alignment == 0 || Alignment >= VT.getSizeInBits()/8; |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 17006 | if (VT.is256BitVector() && !Subtarget->hasInt256() && |
| Nadav Rotem | ba95865 | 2013-01-19 08:38:41 +0000 | [diff] [blame] | 17007 | StVT == VT && !IsAligned) { |
| 17008 | unsigned NumElems = VT.getVectorNumElements(); |
| 17009 | if (NumElems < 2) |
| 17010 | return SDValue(); |
| 17011 | |
| 17012 | SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl); |
| 17013 | SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl); |
| Nadav Rotem | 5e742a3 | 2011-08-11 16:41:21 +0000 | [diff] [blame] | 17014 | |
| 17015 | SDValue Stride = DAG.getConstant(16, TLI.getPointerTy()); |
| 17016 | SDValue Ptr0 = St->getBasePtr(); |
| 17017 | SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride); |
| 17018 | |
| 17019 | SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0, |
| 17020 | St->getPointerInfo(), St->isVolatile(), |
| Nadav Rotem | ba95865 | 2013-01-19 08:38:41 +0000 | [diff] [blame] | 17021 | St->isNonTemporal(), Alignment); |
| Nadav Rotem | 5e742a3 | 2011-08-11 16:41:21 +0000 | [diff] [blame] | 17022 | SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1, |
| 17023 | St->getPointerInfo(), St->isVolatile(), |
| Nadav Rotem | ba95865 | 2013-01-19 08:38:41 +0000 | [diff] [blame] | 17024 | St->isNonTemporal(), |
| Michael Liao | d4584c9 | 2013-03-25 23:50:10 +0000 | [diff] [blame] | 17025 | std::min(16U, Alignment)); |
| Nadav Rotem | 5e742a3 | 2011-08-11 16:41:21 +0000 | [diff] [blame] | 17026 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1); |
| 17027 | } |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 17028 | |
| 17029 | // Optimize trunc store (of multiple scalars) to shuffle and store. |
| 17030 | // First, pack all of the elements in one place. Next, store to memory |
| 17031 | // in fewer chunks. |
| 17032 | if (St->isTruncatingStore() && VT.isVector()) { |
| 17033 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| 17034 | unsigned NumElems = VT.getVectorNumElements(); |
| 17035 | assert(StVT != VT && "Cannot truncate to the same type"); |
| 17036 | unsigned FromSz = VT.getVectorElementType().getSizeInBits(); |
| 17037 | unsigned ToSz = StVT.getVectorElementType().getSizeInBits(); |
| 17038 | |
| 17039 | // From, To sizes and ElemCount must be pow of two |
| 17040 | if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue(); |
| Nadav Rotem | 9c6cdf4 | 2011-09-21 08:45:10 +0000 | [diff] [blame] | 17041 | // We are going to use the original vector elt for storing. |
| Nadav Rotem | 64ac73b | 2011-09-21 17:14:40 +0000 | [diff] [blame] | 17042 | // Accumulated smaller vector elements must be a multiple of the store size. |
| Nadav Rotem | 9c6cdf4 | 2011-09-21 08:45:10 +0000 | [diff] [blame] | 17043 | if (0 != (NumElems * FromSz) % ToSz) return SDValue(); |
| Nadav Rotem | 91e43fd | 2011-09-18 10:39:32 +0000 | [diff] [blame] | 17044 | |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 17045 | unsigned SizeRatio = FromSz / ToSz; |
| 17046 | |
| 17047 | assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits()); |
| 17048 | |
| 17049 | // Create a type on which we perform the shuffle |
| 17050 | EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(), |
| 17051 | StVT.getScalarType(), NumElems*SizeRatio); |
| 17052 | |
| 17053 | assert(WideVecVT.getSizeInBits() == VT.getSizeInBits()); |
| 17054 | |
| 17055 | SDValue WideVec = DAG.getNode(ISD::BITCAST, dl, WideVecVT, St->getValue()); |
| 17056 | SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1); |
| Craig Topper | 31a207a | 2012-05-04 06:39:13 +0000 | [diff] [blame] | 17057 | for (unsigned i = 0; i != NumElems; ++i) |
| 17058 | ShuffleVec[i] = i * SizeRatio; |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 17059 | |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 17060 | // Can't shuffle using an illegal type. |
| 17061 | if (!TLI.isTypeLegal(WideVecVT)) |
| 17062 | return SDValue(); |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 17063 | |
| 17064 | SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec, |
| Craig Topper | df966f6 | 2012-04-22 19:17:57 +0000 | [diff] [blame] | 17065 | DAG.getUNDEF(WideVecVT), |
| 17066 | &ShuffleVec[0]); |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 17067 | // At this point all of the data is stored at the bottom of the |
| 17068 | // register. We now need to save it to mem. |
| 17069 | |
| 17070 | // Find the largest store unit |
| 17071 | MVT StoreType = MVT::i8; |
| 17072 | for (unsigned tp = MVT::FIRST_INTEGER_VALUETYPE; |
| 17073 | tp < MVT::LAST_INTEGER_VALUETYPE; ++tp) { |
| 17074 | MVT Tp = (MVT::SimpleValueType)tp; |
| Nadav Rotem | 5cd95e1 | 2012-07-11 13:27:05 +0000 | [diff] [blame] | 17075 | if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz) |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 17076 | StoreType = Tp; |
| 17077 | } |
| 17078 | |
| Nadav Rotem | 5cd95e1 | 2012-07-11 13:27:05 +0000 | [diff] [blame] | 17079 | // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64. |
| 17080 | if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 && |
| 17081 | (64 <= NumElems * ToSz)) |
| 17082 | StoreType = MVT::f64; |
| 17083 | |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 17084 | // Bitcast the original vector into a vector of store-size units |
| 17085 | EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(), |
| Nadav Rotem | 5cd95e1 | 2012-07-11 13:27:05 +0000 | [diff] [blame] | 17086 | StoreType, VT.getSizeInBits()/StoreType.getSizeInBits()); |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 17087 | assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits()); |
| 17088 | SDValue ShuffWide = DAG.getNode(ISD::BITCAST, dl, StoreVecVT, Shuff); |
| 17089 | SmallVector<SDValue, 8> Chains; |
| 17090 | SDValue Increment = DAG.getConstant(StoreType.getSizeInBits()/8, |
| 17091 | TLI.getPointerTy()); |
| 17092 | SDValue Ptr = St->getBasePtr(); |
| 17093 | |
| 17094 | // Perform one or more big stores into memory. |
| Craig Topper | 31a207a | 2012-05-04 06:39:13 +0000 | [diff] [blame] | 17095 | for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) { |
| Nadav Rotem | 614061b | 2011-08-10 19:30:14 +0000 | [diff] [blame] | 17096 | SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, |
| 17097 | StoreType, ShuffWide, |
| 17098 | DAG.getIntPtrConstant(i)); |
| 17099 | SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr, |
| 17100 | St->getPointerInfo(), St->isVolatile(), |
| 17101 | St->isNonTemporal(), St->getAlignment()); |
| 17102 | Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment); |
| 17103 | Chains.push_back(Ch); |
| 17104 | } |
| 17105 | |
| 17106 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], |
| 17107 | Chains.size()); |
| 17108 | } |
| 17109 | |
| Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 17110 | // Turn load->store of MMX types into GPR load/stores. This avoids clobbering |
| 17111 | // the FP state in cases where an emms may be missing. |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17112 | // A preferable solution to the general problem is to figure out the right |
| 17113 | // places to insert EMMS. This qualifies as a quick hack. |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17114 | |
| 17115 | // Similarly, turn load->store of i64 into double load/stores in 32-bit mode. |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17116 | if (VT.getSizeInBits() != 64) |
| 17117 | return SDValue(); |
| 17118 | |
| Devang Patel | 578efa9 | 2009-06-05 21:57:13 +0000 | [diff] [blame] | 17119 | const Function *F = DAG.getMachineFunction().getFunction(); |
| Bill Wendling | 831737d | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 17120 | bool NoImplicitFloatOps = F->getAttributes(). |
| 17121 | hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat); |
| Nick Lewycky | 8a8d479 | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 17122 | bool F64IsLegal = !DAG.getTarget().Options.UseSoftFloat && !NoImplicitFloatOps |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 17123 | && Subtarget->hasSSE2(); |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17124 | if ((VT.isVector() || |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 17125 | (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) && |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17126 | isa<LoadSDNode>(St->getValue()) && |
| 17127 | !cast<LoadSDNode>(St->getValue())->isVolatile() && |
| 17128 | St->getChain().hasOneUse() && !St->isVolatile()) { |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 17129 | SDNode* LdVal = St->getValue().getNode(); |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17130 | LoadSDNode *Ld = 0; |
| 17131 | int TokenFactorIndex = -1; |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 17132 | SmallVector<SDValue, 8> Ops; |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 17133 | SDNode* ChainVal = St->getChain().getNode(); |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17134 | // Must be a store of a load. We currently handle two cases: the load |
| 17135 | // is a direct child, and it's under an intervening TokenFactor. It is |
| 17136 | // possible to dig deeper under nested TokenFactors. |
| Dale Johannesen | 14e2ea9 | 2008-02-25 22:29:22 +0000 | [diff] [blame] | 17137 | if (ChainVal == LdVal) |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17138 | Ld = cast<LoadSDNode>(St->getChain()); |
| 17139 | else if (St->getValue().hasOneUse() && |
| 17140 | ChainVal->getOpcode() == ISD::TokenFactor) { |
| Chad Rosier | c2348d5 | 2012-02-01 18:45:51 +0000 | [diff] [blame] | 17141 | for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) { |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 17142 | if (ChainVal->getOperand(i).getNode() == LdVal) { |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17143 | TokenFactorIndex = i; |
| 17144 | Ld = cast<LoadSDNode>(St->getValue()); |
| 17145 | } else |
| 17146 | Ops.push_back(ChainVal->getOperand(i)); |
| 17147 | } |
| 17148 | } |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17149 | |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17150 | if (!Ld || !ISD::isNormalLoad(Ld)) |
| 17151 | return SDValue(); |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17152 | |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17153 | // If this is not the MMX case, i.e. we are just turning i64 load/store |
| 17154 | // into f64 load/store, avoid the transformation if there are multiple |
| 17155 | // uses of the loaded value. |
| 17156 | if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0)) |
| 17157 | return SDValue(); |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17158 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17159 | SDLoc LdDL(Ld); |
| 17160 | SDLoc StDL(N); |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17161 | // If we are a 64-bit capable x86, lower to a single movq load/store pair. |
| 17162 | // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store |
| 17163 | // pair instead. |
| 17164 | if (Subtarget->is64Bit() || F64IsLegal) { |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 17165 | EVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64; |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 17166 | SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(), |
| 17167 | Ld->getPointerInfo(), Ld->isVolatile(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 17168 | Ld->isNonTemporal(), Ld->isInvariant(), |
| 17169 | Ld->getAlignment()); |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17170 | SDValue NewChain = NewLd.getValue(1); |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17171 | if (TokenFactorIndex != -1) { |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17172 | Ops.push_back(NewChain); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 17173 | NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0], |
| Dale Johannesen | 079f2a6 | 2008-02-25 19:20:14 +0000 | [diff] [blame] | 17174 | Ops.size()); |
| 17175 | } |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17176 | return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(), |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 17177 | St->getPointerInfo(), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 17178 | St->isVolatile(), St->isNonTemporal(), |
| 17179 | St->getAlignment()); |
| Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 17180 | } |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17181 | |
| 17182 | // Otherwise, lower to two pairs of 32-bit loads / stores. |
| 17183 | SDValue LoAddr = Ld->getBasePtr(); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 17184 | SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr, |
| 17185 | DAG.getConstant(4, MVT::i32)); |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17186 | |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 17187 | SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr, |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 17188 | Ld->getPointerInfo(), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 17189 | Ld->isVolatile(), Ld->isNonTemporal(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 17190 | Ld->isInvariant(), Ld->getAlignment()); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 17191 | SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr, |
| Chris Lattner | 51abfe4 | 2010-09-21 06:02:19 +0000 | [diff] [blame] | 17192 | Ld->getPointerInfo().getWithOffset(4), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 17193 | Ld->isVolatile(), Ld->isNonTemporal(), |
| Pete Cooper | d752e0f | 2011-11-08 18:42:53 +0000 | [diff] [blame] | 17194 | Ld->isInvariant(), |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17195 | MinAlign(Ld->getAlignment(), 4)); |
| 17196 | |
| 17197 | SDValue NewChain = LoLd.getValue(1); |
| 17198 | if (TokenFactorIndex != -1) { |
| 17199 | Ops.push_back(LoLd); |
| 17200 | Ops.push_back(HiLd); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 17201 | NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0], |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17202 | Ops.size()); |
| 17203 | } |
| 17204 | |
| 17205 | LoAddr = St->getBasePtr(); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 17206 | HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr, |
| 17207 | DAG.getConstant(4, MVT::i32)); |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17208 | |
| 17209 | SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr, |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 17210 | St->getPointerInfo(), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 17211 | St->isVolatile(), St->isNonTemporal(), |
| 17212 | St->getAlignment()); |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17213 | SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr, |
| Chris Lattner | 8026a9d | 2010-09-21 17:50:43 +0000 | [diff] [blame] | 17214 | St->getPointerInfo().getWithOffset(4), |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17215 | St->isVolatile(), |
| David Greene | 67c9d42 | 2010-02-15 16:53:33 +0000 | [diff] [blame] | 17216 | St->isNonTemporal(), |
| Evan Cheng | 536e667 | 2009-03-12 05:59:15 +0000 | [diff] [blame] | 17217 | MinAlign(St->getAlignment(), 4)); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 17218 | return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt); |
| Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 17219 | } |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 17220 | return SDValue(); |
| Chris Lattner | 149a4e5 | 2008-02-22 02:09:43 +0000 | [diff] [blame] | 17221 | } |
| 17222 | |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17223 | /// isHorizontalBinOp - Return 'true' if this vector operation is "horizontal" |
| 17224 | /// and return the operands for the horizontal operation in LHS and RHS. A |
| 17225 | /// horizontal operation performs the binary operation on successive elements |
| 17226 | /// of its first operand, then on successive elements of its second operand, |
| 17227 | /// returning the resulting values in a vector. For example, if |
| 17228 | /// A = < float a0, float a1, float a2, float a3 > |
| 17229 | /// and |
| 17230 | /// B = < float b0, float b1, float b2, float b3 > |
| 17231 | /// then the result of doing a horizontal operation on A and B is |
| 17232 | /// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >. |
| 17233 | /// In short, LHS and RHS are inspected to see if LHS op RHS is of the form |
| 17234 | /// A horizontal-op B, for some already available A and B, and if so then LHS is |
| 17235 | /// set to A, RHS to B, and the routine returns 'true'. |
| 17236 | /// Note that the binary operation should have the property that if one of the |
| 17237 | /// operands is UNDEF then the result is UNDEF. |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 17238 | static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) { |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17239 | // Look for the following pattern: if |
| 17240 | // A = < float a0, float a1, float a2, float a3 > |
| 17241 | // B = < float b0, float b1, float b2, float b3 > |
| 17242 | // and |
| 17243 | // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6> |
| 17244 | // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7> |
| 17245 | // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 > |
| 17246 | // which is A horizontal-op B. |
| 17247 | |
| 17248 | // At least one of the operands should be a vector shuffle. |
| 17249 | if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE && |
| 17250 | RHS.getOpcode() != ISD::VECTOR_SHUFFLE) |
| 17251 | return false; |
| 17252 | |
| 17253 | EVT VT = LHS.getValueType(); |
| Craig Topper | f836330 | 2011-12-02 08:18:41 +0000 | [diff] [blame] | 17254 | |
| 17255 | assert((VT.is128BitVector() || VT.is256BitVector()) && |
| 17256 | "Unsupported vector type for horizontal add/sub"); |
| 17257 | |
| 17258 | // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to |
| 17259 | // operate independently on 128-bit lanes. |
| Craig Topper | b72039c | 2011-11-30 09:10:50 +0000 | [diff] [blame] | 17260 | unsigned NumElts = VT.getVectorNumElements(); |
| 17261 | unsigned NumLanes = VT.getSizeInBits()/128; |
| 17262 | unsigned NumLaneElts = NumElts / NumLanes; |
| Craig Topper | f836330 | 2011-12-02 08:18:41 +0000 | [diff] [blame] | 17263 | assert((NumLaneElts % 2 == 0) && |
| 17264 | "Vector type should have an even number of elements in each lane"); |
| 17265 | unsigned HalfLaneElts = NumLaneElts/2; |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17266 | |
| 17267 | // View LHS in the form |
| 17268 | // LHS = VECTOR_SHUFFLE A, B, LMask |
| 17269 | // If LHS is not a shuffle then pretend it is the shuffle |
| 17270 | // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1> |
| 17271 | // NOTE: in what follows a default initialized SDValue represents an UNDEF of |
| 17272 | // type VT. |
| 17273 | SDValue A, B; |
| Craig Topper | b72039c | 2011-11-30 09:10:50 +0000 | [diff] [blame] | 17274 | SmallVector<int, 16> LMask(NumElts); |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17275 | if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) { |
| 17276 | if (LHS.getOperand(0).getOpcode() != ISD::UNDEF) |
| 17277 | A = LHS.getOperand(0); |
| 17278 | if (LHS.getOperand(1).getOpcode() != ISD::UNDEF) |
| 17279 | B = LHS.getOperand(1); |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 17280 | ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask(); |
| 17281 | std::copy(Mask.begin(), Mask.end(), LMask.begin()); |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17282 | } else { |
| 17283 | if (LHS.getOpcode() != ISD::UNDEF) |
| 17284 | A = LHS; |
| Craig Topper | b72039c | 2011-11-30 09:10:50 +0000 | [diff] [blame] | 17285 | for (unsigned i = 0; i != NumElts; ++i) |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17286 | LMask[i] = i; |
| 17287 | } |
| 17288 | |
| 17289 | // Likewise, view RHS in the form |
| 17290 | // RHS = VECTOR_SHUFFLE C, D, RMask |
| 17291 | SDValue C, D; |
| Craig Topper | b72039c | 2011-11-30 09:10:50 +0000 | [diff] [blame] | 17292 | SmallVector<int, 16> RMask(NumElts); |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17293 | if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) { |
| 17294 | if (RHS.getOperand(0).getOpcode() != ISD::UNDEF) |
| 17295 | C = RHS.getOperand(0); |
| 17296 | if (RHS.getOperand(1).getOpcode() != ISD::UNDEF) |
| 17297 | D = RHS.getOperand(1); |
| Benjamin Kramer | ed4c8c6 | 2012-01-15 13:16:05 +0000 | [diff] [blame] | 17298 | ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask(); |
| 17299 | std::copy(Mask.begin(), Mask.end(), RMask.begin()); |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17300 | } else { |
| 17301 | if (RHS.getOpcode() != ISD::UNDEF) |
| 17302 | C = RHS; |
| Craig Topper | b72039c | 2011-11-30 09:10:50 +0000 | [diff] [blame] | 17303 | for (unsigned i = 0; i != NumElts; ++i) |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17304 | RMask[i] = i; |
| 17305 | } |
| 17306 | |
| 17307 | // Check that the shuffles are both shuffling the same vectors. |
| 17308 | if (!(A == C && B == D) && !(A == D && B == C)) |
| 17309 | return false; |
| 17310 | |
| 17311 | // If everything is UNDEF then bail out: it would be better to fold to UNDEF. |
| 17312 | if (!A.getNode() && !B.getNode()) |
| 17313 | return false; |
| 17314 | |
| 17315 | // If A and B occur in reverse order in RHS, then "swap" them (which means |
| 17316 | // rewriting the mask). |
| 17317 | if (A != C) |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 17318 | CommuteVectorShuffleMask(RMask, NumElts); |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17319 | |
| 17320 | // At this point LHS and RHS are equivalent to |
| 17321 | // LHS = VECTOR_SHUFFLE A, B, LMask |
| 17322 | // RHS = VECTOR_SHUFFLE A, B, RMask |
| 17323 | // Check that the masks correspond to performing a horizontal operation. |
| Craig Topper | f836330 | 2011-12-02 08:18:41 +0000 | [diff] [blame] | 17324 | for (unsigned i = 0; i != NumElts; ++i) { |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 17325 | int LIdx = LMask[i], RIdx = RMask[i]; |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17326 | |
| Craig Topper | f836330 | 2011-12-02 08:18:41 +0000 | [diff] [blame] | 17327 | // Ignore any UNDEF components. |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 17328 | if (LIdx < 0 || RIdx < 0 || |
| 17329 | (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) || |
| 17330 | (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts))) |
| Craig Topper | f836330 | 2011-12-02 08:18:41 +0000 | [diff] [blame] | 17331 | continue; |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17332 | |
| Craig Topper | f836330 | 2011-12-02 08:18:41 +0000 | [diff] [blame] | 17333 | // Check that successive elements are being operated on. If not, this is |
| 17334 | // not a horizontal operation. |
| 17335 | unsigned Src = (i/HalfLaneElts) % 2; // each lane is split between srcs |
| 17336 | unsigned LaneStart = (i/NumLaneElts) * NumLaneElts; |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 17337 | int Index = 2*(i%HalfLaneElts) + NumElts*Src + LaneStart; |
| Craig Topper | f836330 | 2011-12-02 08:18:41 +0000 | [diff] [blame] | 17338 | if (!(LIdx == Index && RIdx == Index + 1) && |
| Craig Topper | beabc6c | 2011-12-05 06:56:46 +0000 | [diff] [blame] | 17339 | !(IsCommutative && LIdx == Index + 1 && RIdx == Index)) |
| Craig Topper | f836330 | 2011-12-02 08:18:41 +0000 | [diff] [blame] | 17340 | return false; |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17341 | } |
| 17342 | |
| 17343 | LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it. |
| 17344 | RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it. |
| 17345 | return true; |
| 17346 | } |
| 17347 | |
| 17348 | /// PerformFADDCombine - Do target-specific dag combines on floating point adds. |
| 17349 | static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG, |
| 17350 | const X86Subtarget *Subtarget) { |
| 17351 | EVT VT = N->getValueType(0); |
| 17352 | SDValue LHS = N->getOperand(0); |
| 17353 | SDValue RHS = N->getOperand(1); |
| 17354 | |
| 17355 | // Try to synthesize horizontal adds from adds of shuffles. |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 17356 | if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 17357 | (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) && |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17358 | isHorizontalBinOp(LHS, RHS, true)) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17359 | return DAG.getNode(X86ISD::FHADD, SDLoc(N), VT, LHS, RHS); |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17360 | return SDValue(); |
| 17361 | } |
| 17362 | |
| 17363 | /// PerformFSUBCombine - Do target-specific dag combines on floating point subs. |
| 17364 | static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG, |
| 17365 | const X86Subtarget *Subtarget) { |
| 17366 | EVT VT = N->getValueType(0); |
| 17367 | SDValue LHS = N->getOperand(0); |
| 17368 | SDValue RHS = N->getOperand(1); |
| 17369 | |
| 17370 | // Try to synthesize horizontal subs from subs of shuffles. |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 17371 | if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 17372 | (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) && |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17373 | isHorizontalBinOp(LHS, RHS, false)) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17374 | return DAG.getNode(X86ISD::FHSUB, SDLoc(N), VT, LHS, RHS); |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17375 | return SDValue(); |
| 17376 | } |
| 17377 | |
| Chris Lattner | 6cf7326 | 2008-01-25 06:14:17 +0000 | [diff] [blame] | 17378 | /// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and |
| 17379 | /// X86ISD::FXOR nodes. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 17380 | static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) { |
| Chris Lattner | 6cf7326 | 2008-01-25 06:14:17 +0000 | [diff] [blame] | 17381 | assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR); |
| 17382 | // F[X]OR(0.0, x) -> x |
| 17383 | // F[X]OR(x, 0.0) -> x |
| Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 17384 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) |
| 17385 | if (C->getValueAPF().isPosZero()) |
| 17386 | return N->getOperand(1); |
| 17387 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1))) |
| 17388 | if (C->getValueAPF().isPosZero()) |
| 17389 | return N->getOperand(0); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 17390 | return SDValue(); |
| Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 17391 | } |
| 17392 | |
| Nadav Rotem | d60cb11 | 2012-08-19 13:06:16 +0000 | [diff] [blame] | 17393 | /// PerformFMinFMaxCombine - Do target-specific dag combines on X86ISD::FMIN and |
| 17394 | /// X86ISD::FMAX nodes. |
| 17395 | static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) { |
| 17396 | assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX); |
| 17397 | |
| 17398 | // Only perform optimizations if UnsafeMath is used. |
| 17399 | if (!DAG.getTarget().Options.UnsafeFPMath) |
| 17400 | return SDValue(); |
| 17401 | |
| 17402 | // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes |
| Craig Topper | 8365e9b | 2012-09-01 06:33:50 +0000 | [diff] [blame] | 17403 | // into FMINC and FMAXC, which are Commutative operations. |
| Nadav Rotem | d60cb11 | 2012-08-19 13:06:16 +0000 | [diff] [blame] | 17404 | unsigned NewOp = 0; |
| 17405 | switch (N->getOpcode()) { |
| 17406 | default: llvm_unreachable("unknown opcode"); |
| 17407 | case X86ISD::FMIN: NewOp = X86ISD::FMINC; break; |
| 17408 | case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break; |
| 17409 | } |
| 17410 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17411 | return DAG.getNode(NewOp, SDLoc(N), N->getValueType(0), |
| Nadav Rotem | d60cb11 | 2012-08-19 13:06:16 +0000 | [diff] [blame] | 17412 | N->getOperand(0), N->getOperand(1)); |
| 17413 | } |
| 17414 | |
| Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 17415 | /// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 17416 | static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) { |
| Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 17417 | // FAND(0.0, x) -> 0.0 |
| 17418 | // FAND(x, 0.0) -> 0.0 |
| 17419 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) |
| 17420 | if (C->getValueAPF().isPosZero()) |
| 17421 | return N->getOperand(0); |
| 17422 | if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1))) |
| 17423 | if (C->getValueAPF().isPosZero()) |
| 17424 | return N->getOperand(1); |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 17425 | return SDValue(); |
| Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 17426 | } |
| 17427 | |
| Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 17428 | static SDValue PerformBTCombine(SDNode *N, |
| 17429 | SelectionDAG &DAG, |
| 17430 | TargetLowering::DAGCombinerInfo &DCI) { |
| 17431 | // BT ignores high bits in the bit index operand. |
| 17432 | SDValue Op1 = N->getOperand(1); |
| 17433 | if (Op1.hasOneUse()) { |
| 17434 | unsigned BitWidth = Op1.getValueSizeInBits(); |
| 17435 | APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth)); |
| 17436 | APInt KnownZero, KnownOne; |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17437 | TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), |
| 17438 | !DCI.isBeforeLegalizeOps()); |
| Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 17439 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); |
| Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 17440 | if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) || |
| 17441 | TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO)) |
| 17442 | DCI.CommitTargetLoweringOpt(TLO); |
| 17443 | } |
| 17444 | return SDValue(); |
| 17445 | } |
| Chris Lattner | 83e6c99 | 2006-10-04 06:57:07 +0000 | [diff] [blame] | 17446 | |
| Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 17447 | static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) { |
| 17448 | SDValue Op = N->getOperand(0); |
| Wesley Peck | bf17cfa | 2010-11-23 03:31:01 +0000 | [diff] [blame] | 17449 | if (Op.getOpcode() == ISD::BITCAST) |
| Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 17450 | Op = Op.getOperand(0); |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 17451 | EVT VT = N->getValueType(0), OpVT = Op.getValueType(); |
| Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 17452 | if (Op.getOpcode() == X86ISD::VZEXT_LOAD && |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 17453 | VT.getVectorElementType().getSizeInBits() == |
| Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 17454 | OpVT.getVectorElementType().getSizeInBits()) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17455 | return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); |
| Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 17456 | } |
| 17457 | return SDValue(); |
| 17458 | } |
| 17459 | |
| Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 17460 | static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG, |
| Elena Demikhovsky | 52981c4 | 2013-02-20 12:42:54 +0000 | [diff] [blame] | 17461 | const X86Subtarget *Subtarget) { |
| 17462 | EVT VT = N->getValueType(0); |
| 17463 | if (!VT.isVector()) |
| 17464 | return SDValue(); |
| 17465 | |
| 17466 | SDValue N0 = N->getOperand(0); |
| 17467 | SDValue N1 = N->getOperand(1); |
| 17468 | EVT ExtraVT = cast<VTSDNode>(N1)->getVT(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17469 | SDLoc dl(N); |
| Elena Demikhovsky | 52981c4 | 2013-02-20 12:42:54 +0000 | [diff] [blame] | 17470 | |
| 17471 | // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the |
| 17472 | // both SSE and AVX2 since there is no sign-extended shift right |
| 17473 | // operation on a vector with 64-bit elements. |
| 17474 | //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) -> |
| 17475 | // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT))) |
| 17476 | if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND || |
| 17477 | N0.getOpcode() == ISD::SIGN_EXTEND)) { |
| 17478 | SDValue N00 = N0.getOperand(0); |
| 17479 | |
| Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 17480 | // EXTLOAD has a better solution on AVX2, |
| Elena Demikhovsky | 52981c4 | 2013-02-20 12:42:54 +0000 | [diff] [blame] | 17481 | // it may be replaced with X86ISD::VSEXT node. |
| 17482 | if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256()) |
| 17483 | if (!ISD::isNormalLoad(N00.getNode())) |
| 17484 | return SDValue(); |
| 17485 | |
| 17486 | if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) { |
| Matt Arsenault | 225ed70 | 2013-05-18 00:21:46 +0000 | [diff] [blame] | 17487 | SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, |
| Elena Demikhovsky | 52981c4 | 2013-02-20 12:42:54 +0000 | [diff] [blame] | 17488 | N00, N1); |
| 17489 | return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp); |
| 17490 | } |
| 17491 | } |
| 17492 | return SDValue(); |
| 17493 | } |
| 17494 | |
| Elena Demikhovsky | dcabc7b | 2012-02-02 09:10:43 +0000 | [diff] [blame] | 17495 | static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG, |
| 17496 | TargetLowering::DAGCombinerInfo &DCI, |
| 17497 | const X86Subtarget *Subtarget) { |
| 17498 | if (!DCI.isBeforeLegalizeOps()) |
| 17499 | return SDValue(); |
| 17500 | |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 17501 | if (!Subtarget->hasFp256()) |
| Elena Demikhovsky | f602040 | 2012-02-08 08:37:26 +0000 | [diff] [blame] | 17502 | return SDValue(); |
| Elena Demikhovsky | dcabc7b | 2012-02-02 09:10:43 +0000 | [diff] [blame] | 17503 | |
| Nadav Rotem | 0c8607b | 2013-01-20 08:35:56 +0000 | [diff] [blame] | 17504 | EVT VT = N->getValueType(0); |
| 17505 | if (VT.isVector() && VT.getSizeInBits() == 256) { |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 17506 | SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget); |
| 17507 | if (R.getNode()) |
| 17508 | return R; |
| 17509 | } |
| 17510 | |
| Elena Demikhovsky | dcabc7b | 2012-02-02 09:10:43 +0000 | [diff] [blame] | 17511 | return SDValue(); |
| 17512 | } |
| 17513 | |
| Michael Liao | f6c24ee | 2012-08-10 14:39:24 +0000 | [diff] [blame] | 17514 | static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG, |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 17515 | const X86Subtarget* Subtarget) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17516 | SDLoc dl(N); |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 17517 | EVT VT = N->getValueType(0); |
| 17518 | |
| Craig Topper | b1bdd7d | 2012-08-30 06:56:15 +0000 | [diff] [blame] | 17519 | // Let legalize expand this if it isn't a legal type yet. |
| 17520 | if (!DAG.getTargetLoweringInfo().isTypeLegal(VT)) |
| 17521 | return SDValue(); |
| 17522 | |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 17523 | EVT ScalarVT = VT.getScalarType(); |
| Craig Topper | bf40437 | 2012-08-31 15:40:30 +0000 | [diff] [blame] | 17524 | if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) || |
| 17525 | (!Subtarget->hasFMA() && !Subtarget->hasFMA4())) |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 17526 | return SDValue(); |
| 17527 | |
| 17528 | SDValue A = N->getOperand(0); |
| 17529 | SDValue B = N->getOperand(1); |
| 17530 | SDValue C = N->getOperand(2); |
| 17531 | |
| 17532 | bool NegA = (A.getOpcode() == ISD::FNEG); |
| 17533 | bool NegB = (B.getOpcode() == ISD::FNEG); |
| 17534 | bool NegC = (C.getOpcode() == ISD::FNEG); |
| 17535 | |
| Michael Liao | f6c24ee | 2012-08-10 14:39:24 +0000 | [diff] [blame] | 17536 | // Negative multiplication when NegA xor NegB |
| 17537 | bool NegMul = (NegA != NegB); |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 17538 | if (NegA) |
| 17539 | A = A.getOperand(0); |
| 17540 | if (NegB) |
| 17541 | B = B.getOperand(0); |
| 17542 | if (NegC) |
| 17543 | C = C.getOperand(0); |
| 17544 | |
| 17545 | unsigned Opcode; |
| 17546 | if (!NegMul) |
| Craig Topper | bf40437 | 2012-08-31 15:40:30 +0000 | [diff] [blame] | 17547 | Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB; |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 17548 | else |
| Craig Topper | bf40437 | 2012-08-31 15:40:30 +0000 | [diff] [blame] | 17549 | Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB; |
| 17550 | |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 17551 | return DAG.getNode(Opcode, dl, VT, A, B, C); |
| 17552 | } |
| 17553 | |
| Elena Demikhovsky | 28d7e71 | 2012-01-24 13:54:13 +0000 | [diff] [blame] | 17554 | static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG, |
| Craig Topper | c16f851 | 2012-04-25 06:39:39 +0000 | [diff] [blame] | 17555 | TargetLowering::DAGCombinerInfo &DCI, |
| Elena Demikhovsky | 28d7e71 | 2012-01-24 13:54:13 +0000 | [diff] [blame] | 17556 | const X86Subtarget *Subtarget) { |
| Evan Cheng | 2e489c4 | 2009-12-16 00:53:11 +0000 | [diff] [blame] | 17557 | // (i32 zext (and (i8 x86isd::setcc_carry), 1)) -> |
| 17558 | // (and (i32 x86isd::setcc_carry), 1) |
| 17559 | // This eliminates the zext. This transformation is necessary because |
| 17560 | // ISD::SETCC is always legalized to i8. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17561 | SDLoc dl(N); |
| Evan Cheng | 2e489c4 | 2009-12-16 00:53:11 +0000 | [diff] [blame] | 17562 | SDValue N0 = N->getOperand(0); |
| 17563 | EVT VT = N->getValueType(0); |
| Elena Demikhovsky | 28d7e71 | 2012-01-24 13:54:13 +0000 | [diff] [blame] | 17564 | |
| Evan Cheng | 2e489c4 | 2009-12-16 00:53:11 +0000 | [diff] [blame] | 17565 | if (N0.getOpcode() == ISD::AND && |
| 17566 | N0.hasOneUse() && |
| 17567 | N0.getOperand(0).hasOneUse()) { |
| 17568 | SDValue N00 = N0.getOperand(0); |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 17569 | if (N00.getOpcode() == X86ISD::SETCC_CARRY) { |
| 17570 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 17571 | if (!C || C->getZExtValue() != 1) |
| 17572 | return SDValue(); |
| 17573 | return DAG.getNode(ISD::AND, dl, VT, |
| 17574 | DAG.getNode(X86ISD::SETCC_CARRY, dl, VT, |
| 17575 | N00.getOperand(0), N00.getOperand(1)), |
| 17576 | DAG.getConstant(1, VT)); |
| 17577 | } |
| 17578 | } |
| 17579 | |
| Craig Topper | 5a529e4 | 2013-01-18 06:44:29 +0000 | [diff] [blame] | 17580 | if (VT.is256BitVector()) { |
| Nadav Rotem | d6fb53a | 2012-12-27 08:15:45 +0000 | [diff] [blame] | 17581 | SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget); |
| 17582 | if (R.getNode()) |
| 17583 | return R; |
| Evan Cheng | 2e489c4 | 2009-12-16 00:53:11 +0000 | [diff] [blame] | 17584 | } |
| Craig Topper | d0cf565 | 2012-04-21 18:13:35 +0000 | [diff] [blame] | 17585 | |
| Evan Cheng | 2e489c4 | 2009-12-16 00:53:11 +0000 | [diff] [blame] | 17586 | return SDValue(); |
| 17587 | } |
| 17588 | |
| Chad Rosier | a73b6fc | 2012-04-27 22:33:25 +0000 | [diff] [blame] | 17589 | // Optimize x == -y --> x+y == 0 |
| 17590 | // x != -y --> x+y != 0 |
| 17591 | static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG) { |
| 17592 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
| 17593 | SDValue LHS = N->getOperand(0); |
| Chad Rosier | a20e1e7 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 17594 | SDValue RHS = N->getOperand(1); |
| Chad Rosier | a73b6fc | 2012-04-27 22:33:25 +0000 | [diff] [blame] | 17595 | |
| 17596 | if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB) |
| 17597 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHS.getOperand(0))) |
| 17598 | if (C->getAPIntValue() == 0 && LHS.hasOneUse()) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17599 | SDValue addV = DAG.getNode(ISD::ADD, SDLoc(N), |
| Chad Rosier | a73b6fc | 2012-04-27 22:33:25 +0000 | [diff] [blame] | 17600 | LHS.getValueType(), RHS, LHS.getOperand(1)); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17601 | return DAG.getSetCC(SDLoc(N), N->getValueType(0), |
| Chad Rosier | a73b6fc | 2012-04-27 22:33:25 +0000 | [diff] [blame] | 17602 | addV, DAG.getConstant(0, addV.getValueType()), CC); |
| 17603 | } |
| 17604 | if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB) |
| 17605 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHS.getOperand(0))) |
| 17606 | if (C->getAPIntValue() == 0 && RHS.hasOneUse()) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17607 | SDValue addV = DAG.getNode(ISD::ADD, SDLoc(N), |
| Chad Rosier | a73b6fc | 2012-04-27 22:33:25 +0000 | [diff] [blame] | 17608 | RHS.getValueType(), LHS, RHS.getOperand(1)); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17609 | return DAG.getSetCC(SDLoc(N), N->getValueType(0), |
| Chad Rosier | a73b6fc | 2012-04-27 22:33:25 +0000 | [diff] [blame] | 17610 | addV, DAG.getConstant(0, addV.getValueType()), CC); |
| 17611 | } |
| 17612 | return SDValue(); |
| 17613 | } |
| 17614 | |
| Eric Christopher | e187e25 | 2013-01-31 00:50:48 +0000 | [diff] [blame] | 17615 | // Helper function of PerformSETCCCombine. It is to materialize "setb reg" |
| 17616 | // as "sbb reg,reg", since it can be extended without zext and produces |
| Shuxin Yang | a5526a9 | 2012-10-31 23:11:48 +0000 | [diff] [blame] | 17617 | // an all-ones bit which is more useful than 0/1 in some cases. |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17618 | static SDValue MaterializeSETB(SDLoc DL, SDValue EFLAGS, SelectionDAG &DAG) { |
| Shuxin Yang | a5526a9 | 2012-10-31 23:11:48 +0000 | [diff] [blame] | 17619 | return DAG.getNode(ISD::AND, DL, MVT::i8, |
| 17620 | DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8, |
| 17621 | DAG.getConstant(X86::COND_B, MVT::i8), EFLAGS), |
| 17622 | DAG.getConstant(1, MVT::i8)); |
| 17623 | } |
| 17624 | |
| Chris Lattner | c19d1c3 | 2010-12-19 22:08:31 +0000 | [diff] [blame] | 17625 | // Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT |
| Michael Liao | dbf8b5b | 2012-08-28 03:34:40 +0000 | [diff] [blame] | 17626 | static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG, |
| 17627 | TargetLowering::DAGCombinerInfo &DCI, |
| 17628 | const X86Subtarget *Subtarget) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17629 | SDLoc DL(N); |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 17630 | X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0)); |
| 17631 | SDValue EFLAGS = N->getOperand(1); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 17632 | |
| Shuxin Yang | a5526a9 | 2012-10-31 23:11:48 +0000 | [diff] [blame] | 17633 | if (CC == X86::COND_A) { |
| Eric Christopher | e187e25 | 2013-01-31 00:50:48 +0000 | [diff] [blame] | 17634 | // Try to convert COND_A into COND_B in an attempt to facilitate |
| Shuxin Yang | a5526a9 | 2012-10-31 23:11:48 +0000 | [diff] [blame] | 17635 | // materializing "setb reg". |
| 17636 | // |
| 17637 | // Do not flip "e > c", where "c" is a constant, because Cmp instruction |
| 17638 | // cannot take an immediate as its first operand. |
| 17639 | // |
| Eric Christopher | e187e25 | 2013-01-31 00:50:48 +0000 | [diff] [blame] | 17640 | if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() && |
| Shuxin Yang | a5526a9 | 2012-10-31 23:11:48 +0000 | [diff] [blame] | 17641 | EFLAGS.getValueType().isInteger() && |
| 17642 | !isa<ConstantSDNode>(EFLAGS.getOperand(1))) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17643 | SDValue NewSub = DAG.getNode(X86ISD::SUB, SDLoc(EFLAGS), |
| Shuxin Yang | a5526a9 | 2012-10-31 23:11:48 +0000 | [diff] [blame] | 17644 | EFLAGS.getNode()->getVTList(), |
| 17645 | EFLAGS.getOperand(1), EFLAGS.getOperand(0)); |
| 17646 | SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo()); |
| 17647 | return MaterializeSETB(DL, NewEFLAGS, DAG); |
| 17648 | } |
| 17649 | } |
| 17650 | |
| Chris Lattner | c19d1c3 | 2010-12-19 22:08:31 +0000 | [diff] [blame] | 17651 | // Materialize "setb reg" as "sbb reg,reg", since it can be extended without |
| 17652 | // a zext and produces an all-ones bit which is more useful than 0/1 in some |
| 17653 | // cases. |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 17654 | if (CC == X86::COND_B) |
| Shuxin Yang | a5526a9 | 2012-10-31 23:11:48 +0000 | [diff] [blame] | 17655 | return MaterializeSETB(DL, EFLAGS, DAG); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 17656 | |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 17657 | SDValue Flags; |
| 17658 | |
| Michael Liao | dbf8b5b | 2012-08-28 03:34:40 +0000 | [diff] [blame] | 17659 | Flags = checkBoolTestSetCCCombine(EFLAGS, CC); |
| 17660 | if (Flags.getNode()) { |
| 17661 | SDValue Cond = DAG.getConstant(CC, MVT::i8); |
| 17662 | return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags); |
| 17663 | } |
| 17664 | |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 17665 | return SDValue(); |
| 17666 | } |
| 17667 | |
| 17668 | // Optimize branch condition evaluation. |
| 17669 | // |
| 17670 | static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG, |
| 17671 | TargetLowering::DAGCombinerInfo &DCI, |
| 17672 | const X86Subtarget *Subtarget) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17673 | SDLoc DL(N); |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 17674 | SDValue Chain = N->getOperand(0); |
| 17675 | SDValue Dest = N->getOperand(1); |
| 17676 | SDValue EFLAGS = N->getOperand(3); |
| 17677 | X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2)); |
| 17678 | |
| 17679 | SDValue Flags; |
| 17680 | |
| Michael Liao | dbf8b5b | 2012-08-28 03:34:40 +0000 | [diff] [blame] | 17681 | Flags = checkBoolTestSetCCCombine(EFLAGS, CC); |
| 17682 | if (Flags.getNode()) { |
| 17683 | SDValue Cond = DAG.getConstant(CC, MVT::i8); |
| 17684 | return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond, |
| 17685 | Flags); |
| 17686 | } |
| 17687 | |
| Chris Lattner | c19d1c3 | 2010-12-19 22:08:31 +0000 | [diff] [blame] | 17688 | return SDValue(); |
| 17689 | } |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 17690 | |
| Benjamin Kramer | 1396c40 | 2011-06-18 11:09:41 +0000 | [diff] [blame] | 17691 | static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG, |
| 17692 | const X86TargetLowering *XTLI) { |
| Stuart Hastings | f99a4b8 | 2011-06-06 23:15:58 +0000 | [diff] [blame] | 17693 | SDValue Op0 = N->getOperand(0); |
| Nadav Rotem | a354077 | 2012-04-23 21:53:37 +0000 | [diff] [blame] | 17694 | EVT InVT = Op0->getValueType(0); |
| Nadav Rotem | a354077 | 2012-04-23 21:53:37 +0000 | [diff] [blame] | 17695 | |
| 17696 | // SINT_TO_FP(v4i8) -> SINT_TO_FP(SEXT(v4i8 to v4i32)) |
| Craig Topper | 7fd5e16 | 2012-04-24 06:02:29 +0000 | [diff] [blame] | 17697 | if (InVT == MVT::v8i8 || InVT == MVT::v4i8) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17698 | SDLoc dl(N); |
| Craig Topper | 7fd5e16 | 2012-04-24 06:02:29 +0000 | [diff] [blame] | 17699 | MVT DstVT = InVT == MVT::v4i8 ? MVT::v4i32 : MVT::v8i32; |
| Nadav Rotem | a354077 | 2012-04-23 21:53:37 +0000 | [diff] [blame] | 17700 | SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0); |
| 17701 | return DAG.getNode(ISD::SINT_TO_FP, dl, N->getValueType(0), P); |
| 17702 | } |
| 17703 | |
| Stuart Hastings | f99a4b8 | 2011-06-06 23:15:58 +0000 | [diff] [blame] | 17704 | // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have |
| 17705 | // a 32-bit target where SSE doesn't support i64->FP operations. |
| 17706 | if (Op0.getOpcode() == ISD::LOAD) { |
| 17707 | LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode()); |
| 17708 | EVT VT = Ld->getValueType(0); |
| 17709 | if (!Ld->isVolatile() && !N->getValueType(0).isVector() && |
| 17710 | ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() && |
| 17711 | !XTLI->getSubtarget()->is64Bit() && |
| 17712 | !DAG.getTargetLoweringInfo().isTypeLegal(VT)) { |
| Benjamin Kramer | 1396c40 | 2011-06-18 11:09:41 +0000 | [diff] [blame] | 17713 | SDValue FILDChain = XTLI->BuildFILD(SDValue(N, 0), Ld->getValueType(0), |
| 17714 | Ld->getChain(), Op0, DAG); |
| Stuart Hastings | f99a4b8 | 2011-06-06 23:15:58 +0000 | [diff] [blame] | 17715 | DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1)); |
| 17716 | return FILDChain; |
| 17717 | } |
| 17718 | } |
| 17719 | return SDValue(); |
| 17720 | } |
| 17721 | |
| Chris Lattner | 23a0199 | 2010-12-20 01:37:09 +0000 | [diff] [blame] | 17722 | // Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS |
| 17723 | static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG, |
| 17724 | X86TargetLowering::DAGCombinerInfo &DCI) { |
| 17725 | // If the LHS and RHS of the ADC node are zero, then it can't overflow and |
| 17726 | // the result is either zero or one (depending on the input carry bit). |
| 17727 | // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1. |
| 17728 | if (X86::isZeroNode(N->getOperand(0)) && |
| 17729 | X86::isZeroNode(N->getOperand(1)) && |
| 17730 | // We don't have a good way to replace an EFLAGS use, so only do this when |
| 17731 | // dead right now. |
| 17732 | SDValue(N, 1).use_empty()) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17733 | SDLoc DL(N); |
| Chris Lattner | 23a0199 | 2010-12-20 01:37:09 +0000 | [diff] [blame] | 17734 | EVT VT = N->getValueType(0); |
| 17735 | SDValue CarryOut = DAG.getConstant(0, N->getValueType(1)); |
| 17736 | SDValue Res1 = DAG.getNode(ISD::AND, DL, VT, |
| 17737 | DAG.getNode(X86ISD::SETCC_CARRY, DL, VT, |
| 17738 | DAG.getConstant(X86::COND_B,MVT::i8), |
| 17739 | N->getOperand(2)), |
| 17740 | DAG.getConstant(1, VT)); |
| 17741 | return DCI.CombineTo(N, Res1, CarryOut); |
| 17742 | } |
| 17743 | |
| 17744 | return SDValue(); |
| 17745 | } |
| 17746 | |
| Benjamin Kramer | 7d6fe13 | 2010-12-21 21:41:44 +0000 | [diff] [blame] | 17747 | // fold (add Y, (sete X, 0)) -> adc 0, Y |
| 17748 | // (add Y, (setne X, 0)) -> sbb -1, Y |
| 17749 | // (sub (sete X, 0), Y) -> sbb 0, Y |
| 17750 | // (sub (setne X, 0), Y) -> adc -1, Y |
| Benjamin Kramer | 162ee5c | 2011-07-26 22:42:13 +0000 | [diff] [blame] | 17751 | static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) { |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17752 | SDLoc DL(N); |
| NAKAMURA Takumi | e5fffe9 | 2011-01-26 02:03:37 +0000 | [diff] [blame] | 17753 | |
| Benjamin Kramer | 7d6fe13 | 2010-12-21 21:41:44 +0000 | [diff] [blame] | 17754 | // Look through ZExts. |
| 17755 | SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0); |
| 17756 | if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse()) |
| 17757 | return SDValue(); |
| 17758 | |
| 17759 | SDValue SetCC = Ext.getOperand(0); |
| 17760 | if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse()) |
| 17761 | return SDValue(); |
| 17762 | |
| 17763 | X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0); |
| 17764 | if (CC != X86::COND_E && CC != X86::COND_NE) |
| 17765 | return SDValue(); |
| 17766 | |
| 17767 | SDValue Cmp = SetCC.getOperand(1); |
| 17768 | if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() || |
| Chris Lattner | 9cd3da4 | 2011-01-16 02:56:53 +0000 | [diff] [blame] | 17769 | !X86::isZeroNode(Cmp.getOperand(1)) || |
| 17770 | !Cmp.getOperand(0).getValueType().isInteger()) |
| Benjamin Kramer | 7d6fe13 | 2010-12-21 21:41:44 +0000 | [diff] [blame] | 17771 | return SDValue(); |
| 17772 | |
| 17773 | SDValue CmpOp0 = Cmp.getOperand(0); |
| 17774 | SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0, |
| 17775 | DAG.getConstant(1, CmpOp0.getValueType())); |
| 17776 | |
| 17777 | SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1); |
| 17778 | if (CC == X86::COND_NE) |
| 17779 | return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB, |
| 17780 | DL, OtherVal.getValueType(), OtherVal, |
| 17781 | DAG.getConstant(-1ULL, OtherVal.getValueType()), NewCmp); |
| 17782 | return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC, |
| 17783 | DL, OtherVal.getValueType(), OtherVal, |
| 17784 | DAG.getConstant(0, OtherVal.getValueType()), NewCmp); |
| 17785 | } |
| Chris Lattner | c19d1c3 | 2010-12-19 22:08:31 +0000 | [diff] [blame] | 17786 | |
| Craig Topper | 54f952a | 2011-11-19 09:02:40 +0000 | [diff] [blame] | 17787 | /// PerformADDCombine - Do target-specific dag combines on integer adds. |
| 17788 | static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG, |
| 17789 | const X86Subtarget *Subtarget) { |
| 17790 | EVT VT = N->getValueType(0); |
| 17791 | SDValue Op0 = N->getOperand(0); |
| 17792 | SDValue Op1 = N->getOperand(1); |
| 17793 | |
| 17794 | // Try to synthesize horizontal adds from adds of shuffles. |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 17795 | if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 17796 | (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) && |
| Craig Topper | 54f952a | 2011-11-19 09:02:40 +0000 | [diff] [blame] | 17797 | isHorizontalBinOp(Op0, Op1, true)) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17798 | return DAG.getNode(X86ISD::HADD, SDLoc(N), VT, Op0, Op1); |
| Craig Topper | 54f952a | 2011-11-19 09:02:40 +0000 | [diff] [blame] | 17799 | |
| 17800 | return OptimizeConditionalInDecrement(N, DAG); |
| 17801 | } |
| 17802 | |
| 17803 | static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG, |
| 17804 | const X86Subtarget *Subtarget) { |
| Benjamin Kramer | 162ee5c | 2011-07-26 22:42:13 +0000 | [diff] [blame] | 17805 | SDValue Op0 = N->getOperand(0); |
| 17806 | SDValue Op1 = N->getOperand(1); |
| 17807 | |
| 17808 | // X86 can't encode an immediate LHS of a sub. See if we can push the |
| 17809 | // negation into a preceding instruction. |
| 17810 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) { |
| Benjamin Kramer | 162ee5c | 2011-07-26 22:42:13 +0000 | [diff] [blame] | 17811 | // If the RHS of the sub is a XOR with one use and a constant, invert the |
| 17812 | // immediate. Then add one to the LHS of the sub so we can turn |
| 17813 | // X-Y -> X+~Y+1, saving one register. |
| 17814 | if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR && |
| 17815 | isa<ConstantSDNode>(Op1.getOperand(1))) { |
| Nick Lewycky | 726ebd6 | 2011-08-23 19:01:24 +0000 | [diff] [blame] | 17816 | APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue(); |
| Benjamin Kramer | 162ee5c | 2011-07-26 22:42:13 +0000 | [diff] [blame] | 17817 | EVT VT = Op0.getValueType(); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17818 | SDValue NewXor = DAG.getNode(ISD::XOR, SDLoc(Op1), VT, |
| Benjamin Kramer | 162ee5c | 2011-07-26 22:42:13 +0000 | [diff] [blame] | 17819 | Op1.getOperand(0), |
| 17820 | DAG.getConstant(~XorC, VT)); |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17821 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, NewXor, |
| Nick Lewycky | 726ebd6 | 2011-08-23 19:01:24 +0000 | [diff] [blame] | 17822 | DAG.getConstant(C->getAPIntValue()+1, VT)); |
| Benjamin Kramer | 162ee5c | 2011-07-26 22:42:13 +0000 | [diff] [blame] | 17823 | } |
| 17824 | } |
| 17825 | |
| Craig Topper | 54f952a | 2011-11-19 09:02:40 +0000 | [diff] [blame] | 17826 | // Try to synthesize horizontal adds from adds of shuffles. |
| 17827 | EVT VT = N->getValueType(0); |
| Craig Topper | d0a3117 | 2012-01-10 06:37:29 +0000 | [diff] [blame] | 17828 | if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 17829 | (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) && |
| Craig Topper | b72039c | 2011-11-30 09:10:50 +0000 | [diff] [blame] | 17830 | isHorizontalBinOp(Op0, Op1, true)) |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17831 | return DAG.getNode(X86ISD::HSUB, SDLoc(N), VT, Op0, Op1); |
| Craig Topper | 54f952a | 2011-11-19 09:02:40 +0000 | [diff] [blame] | 17832 | |
| Benjamin Kramer | 162ee5c | 2011-07-26 22:42:13 +0000 | [diff] [blame] | 17833 | return OptimizeConditionalInDecrement(N, DAG); |
| 17834 | } |
| 17835 | |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 17836 | /// performVZEXTCombine - Performs build vector combines |
| 17837 | static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG, |
| 17838 | TargetLowering::DAGCombinerInfo &DCI, |
| 17839 | const X86Subtarget *Subtarget) { |
| 17840 | // (vzext (bitcast (vzext (x)) -> (vzext x) |
| 17841 | SDValue In = N->getOperand(0); |
| 17842 | while (In.getOpcode() == ISD::BITCAST) |
| 17843 | In = In.getOperand(0); |
| 17844 | |
| 17845 | if (In.getOpcode() != X86ISD::VZEXT) |
| 17846 | return SDValue(); |
| 17847 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 17848 | return DAG.getNode(X86ISD::VZEXT, SDLoc(N), N->getValueType(0), |
| Nadav Rotem | b39a552 | 2013-02-14 18:20:48 +0000 | [diff] [blame] | 17849 | In.getOperand(0)); |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 17850 | } |
| 17851 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 17852 | SDValue X86TargetLowering::PerformDAGCombine(SDNode *N, |
| Evan Cheng | 9dd93b3 | 2008-11-05 06:03:38 +0000 | [diff] [blame] | 17853 | DAGCombinerInfo &DCI) const { |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 17854 | SelectionDAG &DAG = DCI.DAG; |
| 17855 | switch (N->getOpcode()) { |
| 17856 | default: break; |
| Dan Gohman | 1bbf72b | 2010-03-15 23:23:03 +0000 | [diff] [blame] | 17857 | case ISD::EXTRACT_VECTOR_ELT: |
| Craig Topper | 89f4e66 | 2012-03-20 07:17:59 +0000 | [diff] [blame] | 17858 | return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI); |
| Duncan Sands | 6bcd219 | 2011-09-17 16:49:39 +0000 | [diff] [blame] | 17859 | case ISD::VSELECT: |
| Nadav Rotem | cc61656 | 2012-01-15 19:27:55 +0000 | [diff] [blame] | 17860 | case ISD::SELECT: return PerformSELECTCombine(N, DAG, DCI, Subtarget); |
| Michael Liao | dbf8b5b | 2012-08-28 03:34:40 +0000 | [diff] [blame] | 17861 | case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI, Subtarget); |
| Craig Topper | 54f952a | 2011-11-19 09:02:40 +0000 | [diff] [blame] | 17862 | case ISD::ADD: return PerformAddCombine(N, DAG, Subtarget); |
| 17863 | case ISD::SUB: return PerformSubCombine(N, DAG, Subtarget); |
| Chris Lattner | 23a0199 | 2010-12-20 01:37:09 +0000 | [diff] [blame] | 17864 | case X86ISD::ADC: return PerformADCCombine(N, DAG, DCI); |
| Evan Cheng | 0b0cd91 | 2009-03-28 05:57:29 +0000 | [diff] [blame] | 17865 | case ISD::MUL: return PerformMulCombine(N, DAG, DCI); |
| Nate Begeman | 740ab03 | 2009-01-26 00:52:55 +0000 | [diff] [blame] | 17866 | case ISD::SHL: |
| 17867 | case ISD::SRA: |
| Mon P Wang | 845b189 | 2012-02-01 22:15:20 +0000 | [diff] [blame] | 17868 | case ISD::SRL: return PerformShiftCombine(N, DAG, DCI, Subtarget); |
| Nate Begeman | b65c175 | 2010-12-17 22:55:37 +0000 | [diff] [blame] | 17869 | case ISD::AND: return PerformAndCombine(N, DAG, DCI, Subtarget); |
| Evan Cheng | 8b1190a | 2010-04-28 01:18:01 +0000 | [diff] [blame] | 17870 | case ISD::OR: return PerformOrCombine(N, DAG, DCI, Subtarget); |
| Craig Topper | b4c9457 | 2011-10-21 06:55:01 +0000 | [diff] [blame] | 17871 | case ISD::XOR: return PerformXorCombine(N, DAG, DCI, Subtarget); |
| Nadav Rotem | 2dd83eb | 2012-07-10 13:25:08 +0000 | [diff] [blame] | 17872 | case ISD::LOAD: return PerformLOADCombine(N, DAG, DCI, Subtarget); |
| Evan Cheng | 7e2ff77 | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 17873 | case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget); |
| Stuart Hastings | f99a4b8 | 2011-06-06 23:15:58 +0000 | [diff] [blame] | 17874 | case ISD::SINT_TO_FP: return PerformSINT_TO_FPCombine(N, DAG, this); |
| Duncan Sands | 17470be | 2011-09-22 20:15:48 +0000 | [diff] [blame] | 17875 | case ISD::FADD: return PerformFADDCombine(N, DAG, Subtarget); |
| 17876 | case ISD::FSUB: return PerformFSUBCombine(N, DAG, Subtarget); |
| Chris Lattner | 6cf7326 | 2008-01-25 06:14:17 +0000 | [diff] [blame] | 17877 | case X86ISD::FXOR: |
| Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 17878 | case X86ISD::FOR: return PerformFORCombine(N, DAG); |
| Nadav Rotem | d60cb11 | 2012-08-19 13:06:16 +0000 | [diff] [blame] | 17879 | case X86ISD::FMIN: |
| 17880 | case X86ISD::FMAX: return PerformFMinFMaxCombine(N, DAG); |
| Chris Lattner | af723b9 | 2008-01-25 05:46:26 +0000 | [diff] [blame] | 17881 | case X86ISD::FAND: return PerformFANDCombine(N, DAG); |
| Dan Gohman | e5af2d3 | 2009-01-29 01:59:02 +0000 | [diff] [blame] | 17882 | case X86ISD::BT: return PerformBTCombine(N, DAG, DCI); |
| Eli Friedman | 7a5e555 | 2009-06-07 06:52:44 +0000 | [diff] [blame] | 17883 | case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG); |
| Elena Demikhovsky | 1da5867 | 2012-04-22 09:39:03 +0000 | [diff] [blame] | 17884 | case ISD::ANY_EXTEND: |
| Craig Topper | c16f851 | 2012-04-25 06:39:39 +0000 | [diff] [blame] | 17885 | case ISD::ZERO_EXTEND: return PerformZExtCombine(N, DAG, DCI, Subtarget); |
| Elena Demikhovsky | dcabc7b | 2012-02-02 09:10:43 +0000 | [diff] [blame] | 17886 | case ISD::SIGN_EXTEND: return PerformSExtCombine(N, DAG, DCI, Subtarget); |
| Elena Demikhovsky | 52981c4 | 2013-02-20 12:42:54 +0000 | [diff] [blame] | 17887 | case ISD::SIGN_EXTEND_INREG: return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget); |
| Craig Topper | 55b2405 | 2012-09-11 06:15:32 +0000 | [diff] [blame] | 17888 | case ISD::TRUNCATE: return PerformTruncateCombine(N, DAG,DCI,Subtarget); |
| Chad Rosier | a73b6fc | 2012-04-27 22:33:25 +0000 | [diff] [blame] | 17889 | case ISD::SETCC: return PerformISDSETCCCombine(N, DAG); |
| Michael Liao | dbf8b5b | 2012-08-28 03:34:40 +0000 | [diff] [blame] | 17890 | case X86ISD::SETCC: return PerformSETCCCombine(N, DAG, DCI, Subtarget); |
| Michael Liao | 2a33cec | 2012-08-10 19:58:13 +0000 | [diff] [blame] | 17891 | case X86ISD::BRCOND: return PerformBrCondCombine(N, DAG, DCI, Subtarget); |
| Michael Liao | d9d0960 | 2012-10-23 17:34:00 +0000 | [diff] [blame] | 17892 | case X86ISD::VZEXT: return performVZEXTCombine(N, DAG, DCI, Subtarget); |
| Craig Topper | b3982da | 2011-12-31 23:50:21 +0000 | [diff] [blame] | 17893 | case X86ISD::SHUFP: // Handle all target specific shuffles |
| Craig Topper | 4aee1bb | 2013-01-28 06:48:25 +0000 | [diff] [blame] | 17894 | case X86ISD::PALIGNR: |
| Craig Topper | 34671b8 | 2011-12-06 08:21:25 +0000 | [diff] [blame] | 17895 | case X86ISD::UNPCKH: |
| 17896 | case X86ISD::UNPCKL: |
| Bruno Cardoso Lopes | e8f279c | 2010-09-03 22:09:41 +0000 | [diff] [blame] | 17897 | case X86ISD::MOVHLPS: |
| 17898 | case X86ISD::MOVLHPS: |
| 17899 | case X86ISD::PSHUFD: |
| 17900 | case X86ISD::PSHUFHW: |
| 17901 | case X86ISD::PSHUFLW: |
| 17902 | case X86ISD::MOVSS: |
| 17903 | case X86ISD::MOVSD: |
| Craig Topper | 316cd2a | 2011-11-30 06:25:25 +0000 | [diff] [blame] | 17904 | case X86ISD::VPERMILP: |
| Craig Topper | ec24e61 | 2011-11-30 07:47:51 +0000 | [diff] [blame] | 17905 | case X86ISD::VPERM2X128: |
| Bruno Cardoso Lopes | 50b37c7 | 2011-08-15 21:45:54 +0000 | [diff] [blame] | 17906 | case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget); |
| Elena Demikhovsky | 1503aba | 2012-08-01 12:06:00 +0000 | [diff] [blame] | 17907 | case ISD::FMA: return PerformFMACombine(N, DAG, Subtarget); |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 17908 | } |
| 17909 | |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 17910 | return SDValue(); |
| Evan Cheng | 206ee9d | 2006-07-07 08:33:52 +0000 | [diff] [blame] | 17911 | } |
| 17912 | |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17913 | /// isTypeDesirableForOp - Return true if the target has native support for |
| 17914 | /// the specified value type and it is 'desirable' to use the type for the |
| 17915 | /// given node type. e.g. On x86 i16 is legal, but undesirable since i16 |
| 17916 | /// instruction encodings are longer and some i16 instructions are slow. |
| 17917 | bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const { |
| 17918 | if (!isTypeLegal(VT)) |
| 17919 | return false; |
| Evan Cheng | 2bce5f4b | 2010-04-28 08:30:49 +0000 | [diff] [blame] | 17920 | if (VT != MVT::i16) |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17921 | return true; |
| 17922 | |
| 17923 | switch (Opc) { |
| 17924 | default: |
| 17925 | return true; |
| Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 17926 | case ISD::LOAD: |
| 17927 | case ISD::SIGN_EXTEND: |
| 17928 | case ISD::ZERO_EXTEND: |
| 17929 | case ISD::ANY_EXTEND: |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17930 | case ISD::SHL: |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17931 | case ISD::SRL: |
| 17932 | case ISD::SUB: |
| 17933 | case ISD::ADD: |
| 17934 | case ISD::MUL: |
| 17935 | case ISD::AND: |
| 17936 | case ISD::OR: |
| 17937 | case ISD::XOR: |
| 17938 | return false; |
| 17939 | } |
| 17940 | } |
| 17941 | |
| 17942 | /// IsDesirableToPromoteOp - This method query the target whether it is |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 17943 | /// beneficial for dag combiner to promote the specified node. If true, it |
| 17944 | /// should return the desired promotion type by reference. |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17945 | bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const { |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 17946 | EVT VT = Op.getValueType(); |
| 17947 | if (VT != MVT::i16) |
| 17948 | return false; |
| 17949 | |
| Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 17950 | bool Promote = false; |
| 17951 | bool Commute = false; |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 17952 | switch (Op.getOpcode()) { |
| Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 17953 | default: break; |
| 17954 | case ISD::LOAD: { |
| 17955 | LoadSDNode *LD = cast<LoadSDNode>(Op); |
| 17956 | // If the non-extending load has a single use and it's not live out, then it |
| 17957 | // might be folded. |
| Evan Cheng | 2bce5f4b | 2010-04-28 08:30:49 +0000 | [diff] [blame] | 17958 | if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&& |
| 17959 | Op.hasOneUse()*/) { |
| 17960 | for (SDNode::use_iterator UI = Op.getNode()->use_begin(), |
| 17961 | UE = Op.getNode()->use_end(); UI != UE; ++UI) { |
| 17962 | // The only case where we'd want to promote LOAD (rather then it being |
| 17963 | // promoted as an operand is when it's only use is liveout. |
| 17964 | if (UI->getOpcode() != ISD::CopyToReg) |
| 17965 | return false; |
| 17966 | } |
| 17967 | } |
| Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 17968 | Promote = true; |
| 17969 | break; |
| 17970 | } |
| 17971 | case ISD::SIGN_EXTEND: |
| 17972 | case ISD::ZERO_EXTEND: |
| 17973 | case ISD::ANY_EXTEND: |
| 17974 | Promote = true; |
| 17975 | break; |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17976 | case ISD::SHL: |
| Evan Cheng | 2bce5f4b | 2010-04-28 08:30:49 +0000 | [diff] [blame] | 17977 | case ISD::SRL: { |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17978 | SDValue N0 = Op.getOperand(0); |
| 17979 | // Look out for (store (shl (load), x)). |
| Evan Cheng | c82c20b | 2010-04-24 04:44:57 +0000 | [diff] [blame] | 17980 | if (MayFoldLoad(N0) && MayFoldIntoStore(Op)) |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17981 | return false; |
| Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 17982 | Promote = true; |
| Evan Cheng | e5b51ac | 2010-04-17 06:13:15 +0000 | [diff] [blame] | 17983 | break; |
| 17984 | } |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 17985 | case ISD::ADD: |
| 17986 | case ISD::MUL: |
| 17987 | case ISD::AND: |
| 17988 | case ISD::OR: |
| Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 17989 | case ISD::XOR: |
| 17990 | Commute = true; |
| 17991 | // fallthrough |
| 17992 | case ISD::SUB: { |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 17993 | SDValue N0 = Op.getOperand(0); |
| 17994 | SDValue N1 = Op.getOperand(1); |
| Evan Cheng | c82c20b | 2010-04-24 04:44:57 +0000 | [diff] [blame] | 17995 | if (!Commute && MayFoldLoad(N1)) |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 17996 | return false; |
| 17997 | // Avoid disabling potential load folding opportunities. |
| Evan Cheng | c82c20b | 2010-04-24 04:44:57 +0000 | [diff] [blame] | 17998 | if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op))) |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 17999 | return false; |
| Evan Cheng | c82c20b | 2010-04-24 04:44:57 +0000 | [diff] [blame] | 18000 | if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op))) |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 18001 | return false; |
| Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 18002 | Promote = true; |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 18003 | } |
| 18004 | } |
| 18005 | |
| 18006 | PVT = MVT::i32; |
| Evan Cheng | 4c26e93 | 2010-04-19 19:29:22 +0000 | [diff] [blame] | 18007 | return Promote; |
| Evan Cheng | 64b7bf7 | 2010-04-16 06:14:10 +0000 | [diff] [blame] | 18008 | } |
| 18009 | |
| Evan Cheng | 60c07e1 | 2006-07-05 22:17:51 +0000 | [diff] [blame] | 18010 | //===----------------------------------------------------------------------===// |
| 18011 | // X86 Inline Assembly Support |
| 18012 | //===----------------------------------------------------------------------===// |
| 18013 | |
| Benjamin Kramer | 2ea4cdb | 2011-12-18 19:59:20 +0000 | [diff] [blame] | 18014 | namespace { |
| 18015 | // Helper to match a string separated by whitespace. |
| Benjamin Kramer | 0581ed7 | 2011-12-18 20:51:31 +0000 | [diff] [blame] | 18016 | bool matchAsmImpl(StringRef s, ArrayRef<const StringRef *> args) { |
| Benjamin Kramer | 2ea4cdb | 2011-12-18 19:59:20 +0000 | [diff] [blame] | 18017 | s = s.substr(s.find_first_not_of(" \t")); // Skip leading whitespace. |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18018 | |
| Benjamin Kramer | 0581ed7 | 2011-12-18 20:51:31 +0000 | [diff] [blame] | 18019 | for (unsigned i = 0, e = args.size(); i != e; ++i) { |
| Benjamin Kramer | 2ea4cdb | 2011-12-18 19:59:20 +0000 | [diff] [blame] | 18020 | StringRef piece(*args[i]); |
| 18021 | if (!s.startswith(piece)) // Check if the piece matches. |
| 18022 | return false; |
| 18023 | |
| 18024 | s = s.substr(piece.size()); |
| 18025 | StringRef::size_type pos = s.find_first_not_of(" \t"); |
| 18026 | if (pos == 0) // We matched a prefix. |
| 18027 | return false; |
| 18028 | |
| 18029 | s = s.substr(pos); |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18030 | } |
| 18031 | |
| Benjamin Kramer | 2ea4cdb | 2011-12-18 19:59:20 +0000 | [diff] [blame] | 18032 | return s.empty(); |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18033 | } |
| Benjamin Kramer | 0581ed7 | 2011-12-18 20:51:31 +0000 | [diff] [blame] | 18034 | const VariadicFunction1<bool, StringRef, StringRef, matchAsmImpl> matchAsm={}; |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18035 | } |
| 18036 | |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18037 | bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const { |
| 18038 | InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue()); |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18039 | |
| 18040 | std::string AsmStr = IA->getAsmString(); |
| 18041 | |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18042 | IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); |
| 18043 | if (!Ty || Ty->getBitWidth() % 16 != 0) |
| 18044 | return false; |
| 18045 | |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18046 | // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a" |
| Benjamin Kramer | d4f1959 | 2010-01-11 18:03:24 +0000 | [diff] [blame] | 18047 | SmallVector<StringRef, 4> AsmPieces; |
| Peter Collingbourne | 9836118 | 2010-11-13 19:54:23 +0000 | [diff] [blame] | 18048 | SplitString(AsmStr, AsmPieces, ";\n"); |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18049 | |
| 18050 | switch (AsmPieces.size()) { |
| 18051 | default: return false; |
| 18052 | case 1: |
| Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 18053 | // FIXME: this should verify that we are targeting a 486 or better. If not, |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18054 | // we will turn this bswap into something that will be lowered to logical |
| 18055 | // ops instead of emitting the bswap asm. For now, we don't support 486 or |
| 18056 | // lower so don't worry about this. |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18057 | // bswap $0 |
| Benjamin Kramer | 2ea4cdb | 2011-12-18 19:59:20 +0000 | [diff] [blame] | 18058 | if (matchAsm(AsmPieces[0], "bswap", "$0") || |
| 18059 | matchAsm(AsmPieces[0], "bswapl", "$0") || |
| 18060 | matchAsm(AsmPieces[0], "bswapq", "$0") || |
| 18061 | matchAsm(AsmPieces[0], "bswap", "${0:q}") || |
| 18062 | matchAsm(AsmPieces[0], "bswapl", "${0:q}") || |
| 18063 | matchAsm(AsmPieces[0], "bswapq", "${0:q}")) { |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18064 | // No need to check constraints, nothing other than the equivalent of |
| 18065 | // "=r,0" would be valid here. |
| Evan Cheng | 55d4200 | 2011-01-08 01:24:27 +0000 | [diff] [blame] | 18066 | return IntrinsicLowering::LowerToByteSwap(CI); |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18067 | } |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18068 | |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18069 | // rorw $$8, ${0:w} --> llvm.bswap.i16 |
| Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 18070 | if (CI->getType()->isIntegerTy(16) && |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18071 | IA->getConstraintString().compare(0, 5, "=r,0,") == 0 && |
| Benjamin Kramer | 2ea4cdb | 2011-12-18 19:59:20 +0000 | [diff] [blame] | 18072 | (matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") || |
| 18073 | matchAsm(AsmPieces[0], "rolw", "$$8,", "${0:w}"))) { |
| Dan Gohman | 0ef701e | 2010-03-04 19:58:08 +0000 | [diff] [blame] | 18074 | AsmPieces.clear(); |
| Evan Cheng | 55d4200 | 2011-01-08 01:24:27 +0000 | [diff] [blame] | 18075 | const std::string &ConstraintsStr = IA->getConstraintString(); |
| 18076 | SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ","); |
| Jakub Staszak | 56f58ad | 2013-02-18 23:18:22 +0000 | [diff] [blame] | 18077 | array_pod_sort(AsmPieces.begin(), AsmPieces.end()); |
| Dan Gohman | 0ef701e | 2010-03-04 19:58:08 +0000 | [diff] [blame] | 18078 | if (AsmPieces.size() == 4 && |
| 18079 | AsmPieces[0] == "~{cc}" && |
| 18080 | AsmPieces[1] == "~{dirflag}" && |
| 18081 | AsmPieces[2] == "~{flags}" && |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18082 | AsmPieces[3] == "~{fpsr}") |
| 18083 | return IntrinsicLowering::LowerToByteSwap(CI); |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18084 | } |
| 18085 | break; |
| 18086 | case 3: |
| Peter Collingbourne | 948cf02 | 2010-11-13 19:54:30 +0000 | [diff] [blame] | 18087 | if (CI->getType()->isIntegerTy(32) && |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18088 | IA->getConstraintString().compare(0, 5, "=r,0,") == 0 && |
| Benjamin Kramer | 2ea4cdb | 2011-12-18 19:59:20 +0000 | [diff] [blame] | 18089 | matchAsm(AsmPieces[0], "rorw", "$$8,", "${0:w}") && |
| 18090 | matchAsm(AsmPieces[1], "rorl", "$$16,", "$0") && |
| 18091 | matchAsm(AsmPieces[2], "rorw", "$$8,", "${0:w}")) { |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18092 | AsmPieces.clear(); |
| 18093 | const std::string &ConstraintsStr = IA->getConstraintString(); |
| 18094 | SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ","); |
| Jakub Staszak | 56f58ad | 2013-02-18 23:18:22 +0000 | [diff] [blame] | 18095 | array_pod_sort(AsmPieces.begin(), AsmPieces.end()); |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18096 | if (AsmPieces.size() == 4 && |
| 18097 | AsmPieces[0] == "~{cc}" && |
| 18098 | AsmPieces[1] == "~{dirflag}" && |
| 18099 | AsmPieces[2] == "~{flags}" && |
| 18100 | AsmPieces[3] == "~{fpsr}") |
| 18101 | return IntrinsicLowering::LowerToByteSwap(CI); |
| Peter Collingbourne | 948cf02 | 2010-11-13 19:54:30 +0000 | [diff] [blame] | 18102 | } |
| Evan Cheng | 55d4200 | 2011-01-08 01:24:27 +0000 | [diff] [blame] | 18103 | |
| 18104 | if (CI->getType()->isIntegerTy(64)) { |
| 18105 | InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints(); |
| 18106 | if (Constraints.size() >= 2 && |
| 18107 | Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" && |
| 18108 | Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") { |
| 18109 | // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64 |
| Benjamin Kramer | 2ea4cdb | 2011-12-18 19:59:20 +0000 | [diff] [blame] | 18110 | if (matchAsm(AsmPieces[0], "bswap", "%eax") && |
| 18111 | matchAsm(AsmPieces[1], "bswap", "%edx") && |
| 18112 | matchAsm(AsmPieces[2], "xchgl", "%eax,", "%edx")) |
| Benjamin Kramer | e6cddb7 | 2011-12-17 14:36:05 +0000 | [diff] [blame] | 18113 | return IntrinsicLowering::LowerToByteSwap(CI); |
| Chris Lattner | b810565 | 2009-07-20 17:51:36 +0000 | [diff] [blame] | 18114 | } |
| 18115 | } |
| 18116 | break; |
| 18117 | } |
| 18118 | return false; |
| 18119 | } |
| 18120 | |
| Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 18121 | /// getConstraintType - Given a constraint letter, return the type of |
| 18122 | /// constraint it is for this target. |
| 18123 | X86TargetLowering::ConstraintType |
| Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 18124 | X86TargetLowering::getConstraintType(const std::string &Constraint) const { |
| 18125 | if (Constraint.size() == 1) { |
| 18126 | switch (Constraint[0]) { |
| Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 18127 | case 'R': |
| Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 18128 | case 'q': |
| 18129 | case 'Q': |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18130 | case 'f': |
| 18131 | case 't': |
| 18132 | case 'u': |
| Dale Johannesen | 2ffbcac | 2008-04-01 00:57:48 +0000 | [diff] [blame] | 18133 | case 'y': |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18134 | case 'x': |
| Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 18135 | case 'Y': |
| Eric Christopher | 31b5f00 | 2011-07-07 22:29:07 +0000 | [diff] [blame] | 18136 | case 'l': |
| Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 18137 | return C_RegisterClass; |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18138 | case 'a': |
| 18139 | case 'b': |
| 18140 | case 'c': |
| 18141 | case 'd': |
| 18142 | case 'S': |
| 18143 | case 'D': |
| 18144 | case 'A': |
| 18145 | return C_Register; |
| 18146 | case 'I': |
| 18147 | case 'J': |
| 18148 | case 'K': |
| 18149 | case 'L': |
| 18150 | case 'M': |
| 18151 | case 'N': |
| 18152 | case 'G': |
| 18153 | case 'C': |
| Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 18154 | case 'e': |
| 18155 | case 'Z': |
| 18156 | return C_Other; |
| Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 18157 | default: |
| 18158 | break; |
| 18159 | } |
| Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 18160 | } |
| Chris Lattner | 4234f57 | 2007-03-25 02:14:49 +0000 | [diff] [blame] | 18161 | return TargetLowering::getConstraintType(Constraint); |
| Chris Lattner | f4dff84 | 2006-07-11 02:54:03 +0000 | [diff] [blame] | 18162 | } |
| 18163 | |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18164 | /// Examine constraint type and operand type and determine a weight value. |
| John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 18165 | /// This object must already have been set up with the operand type |
| 18166 | /// and the current alternative constraint selected. |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18167 | TargetLowering::ConstraintWeight |
| 18168 | X86TargetLowering::getSingleConstraintMatchWeight( |
| John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 18169 | AsmOperandInfo &info, const char *constraint) const { |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18170 | ConstraintWeight weight = CW_Invalid; |
| John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 18171 | Value *CallOperandVal = info.CallOperandVal; |
| 18172 | // If we don't have a value, we can't do a match, |
| 18173 | // but allow it at the lowest weight. |
| 18174 | if (CallOperandVal == NULL) |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18175 | return CW_Default; |
| Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 18176 | Type *type = CallOperandVal->getType(); |
| John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 18177 | // Look at the constraint type. |
| 18178 | switch (*constraint) { |
| 18179 | default: |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18180 | weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); |
| 18181 | case 'R': |
| 18182 | case 'q': |
| 18183 | case 'Q': |
| 18184 | case 'a': |
| 18185 | case 'b': |
| 18186 | case 'c': |
| 18187 | case 'd': |
| 18188 | case 'S': |
| 18189 | case 'D': |
| 18190 | case 'A': |
| 18191 | if (CallOperandVal->getType()->isIntegerTy()) |
| 18192 | weight = CW_SpecificReg; |
| 18193 | break; |
| 18194 | case 'f': |
| 18195 | case 't': |
| 18196 | case 'u': |
| Jakub Staszak | c20323a | 2012-12-29 15:57:26 +0000 | [diff] [blame] | 18197 | if (type->isFloatingPointTy()) |
| 18198 | weight = CW_SpecificReg; |
| 18199 | break; |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18200 | case 'y': |
| Jakub Staszak | c20323a | 2012-12-29 15:57:26 +0000 | [diff] [blame] | 18201 | if (type->isX86_MMXTy() && Subtarget->hasMMX()) |
| 18202 | weight = CW_SpecificReg; |
| 18203 | break; |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18204 | case 'x': |
| 18205 | case 'Y': |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 18206 | if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) || |
| Elena Demikhovsky | 8564dc6 | 2012-11-29 12:44:59 +0000 | [diff] [blame] | 18207 | ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256())) |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18208 | weight = CW_Register; |
| John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 18209 | break; |
| 18210 | case 'I': |
| 18211 | if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) { |
| 18212 | if (C->getZExtValue() <= 31) |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18213 | weight = CW_Constant; |
| John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 18214 | } |
| 18215 | break; |
| John Thompson | 44ab89e | 2010-10-29 17:29:13 +0000 | [diff] [blame] | 18216 | case 'J': |
| 18217 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { |
| 18218 | if (C->getZExtValue() <= 63) |
| 18219 | weight = CW_Constant; |
| 18220 | } |
| 18221 | break; |
| 18222 | case 'K': |
| 18223 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { |
| 18224 | if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f)) |
| 18225 | weight = CW_Constant; |
| 18226 | } |
| 18227 | break; |
| 18228 | case 'L': |
| 18229 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { |
| 18230 | if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff)) |
| 18231 | weight = CW_Constant; |
| 18232 | } |
| 18233 | break; |
| 18234 | case 'M': |
| 18235 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { |
| 18236 | if (C->getZExtValue() <= 3) |
| 18237 | weight = CW_Constant; |
| 18238 | } |
| 18239 | break; |
| 18240 | case 'N': |
| 18241 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { |
| 18242 | if (C->getZExtValue() <= 0xff) |
| 18243 | weight = CW_Constant; |
| 18244 | } |
| 18245 | break; |
| 18246 | case 'G': |
| 18247 | case 'C': |
| 18248 | if (dyn_cast<ConstantFP>(CallOperandVal)) { |
| 18249 | weight = CW_Constant; |
| 18250 | } |
| 18251 | break; |
| 18252 | case 'e': |
| 18253 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { |
| 18254 | if ((C->getSExtValue() >= -0x80000000LL) && |
| 18255 | (C->getSExtValue() <= 0x7fffffffLL)) |
| 18256 | weight = CW_Constant; |
| 18257 | } |
| 18258 | break; |
| 18259 | case 'Z': |
| 18260 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { |
| 18261 | if (C->getZExtValue() <= 0xffffffff) |
| 18262 | weight = CW_Constant; |
| 18263 | } |
| 18264 | break; |
| John Thompson | eac6e1d | 2010-09-13 18:15:37 +0000 | [diff] [blame] | 18265 | } |
| 18266 | return weight; |
| 18267 | } |
| 18268 | |
| Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 18269 | /// LowerXConstraint - try to replace an X constraint, which matches anything, |
| 18270 | /// with another that has more specific requirements based on the type of the |
| 18271 | /// corresponding operand. |
| Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 18272 | const char *X86TargetLowering:: |
| Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 18273 | LowerXConstraint(EVT ConstraintVT) const { |
| Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 18274 | // FP X constraints get lowered to SSE1/2 registers if available, otherwise |
| 18275 | // 'f' like normal targets. |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 18276 | if (ConstraintVT.isFloatingPoint()) { |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 18277 | if (Subtarget->hasSSE2()) |
| Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 18278 | return "Y"; |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 18279 | if (Subtarget->hasSSE1()) |
| Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 18280 | return "x"; |
| 18281 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 18282 | |
| Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 18283 | return TargetLowering::LowerXConstraint(ConstraintVT); |
| Dale Johannesen | ba2a0b9 | 2008-01-29 02:21:21 +0000 | [diff] [blame] | 18284 | } |
| 18285 | |
| Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 18286 | /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops |
| 18287 | /// vector. If it is invalid, don't add anything to Ops. |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 18288 | void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op, |
| Eric Christopher | 100c833 | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 18289 | std::string &Constraint, |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 18290 | std::vector<SDValue>&Ops, |
| Chris Lattner | 5e76423 | 2008-04-26 23:02:14 +0000 | [diff] [blame] | 18291 | SelectionDAG &DAG) const { |
| Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 18292 | SDValue Result(0, 0); |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 18293 | |
| Eric Christopher | 100c833 | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 18294 | // Only support length 1 constraints for now. |
| 18295 | if (Constraint.length() > 1) return; |
| Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 18296 | |
| Eric Christopher | 100c833 | 2011-06-02 23:16:42 +0000 | [diff] [blame] | 18297 | char ConstraintLetter = Constraint[0]; |
| 18298 | switch (ConstraintLetter) { |
| Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 18299 | default: break; |
| Devang Patel | 84f7fd2 | 2007-03-17 00:13:28 +0000 | [diff] [blame] | 18300 | case 'I': |
| Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 18301 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 18302 | if (C->getZExtValue() <= 31) { |
| 18303 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 18304 | break; |
| 18305 | } |
| Devang Patel | 84f7fd2 | 2007-03-17 00:13:28 +0000 | [diff] [blame] | 18306 | } |
| Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 18307 | return; |
| Evan Cheng | 364091e | 2008-09-22 23:57:37 +0000 | [diff] [blame] | 18308 | case 'J': |
| 18309 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| Chris Lattner | 2e06dd2 | 2009-06-15 04:39:05 +0000 | [diff] [blame] | 18310 | if (C->getZExtValue() <= 63) { |
| Chris Lattner | e493515 | 2009-06-15 04:01:39 +0000 | [diff] [blame] | 18311 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| 18312 | break; |
| 18313 | } |
| 18314 | } |
| 18315 | return; |
| 18316 | case 'K': |
| 18317 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| Jakub Staszak | dccd7f9 | 2012-11-06 23:52:19 +0000 | [diff] [blame] | 18318 | if (isInt<8>(C->getSExtValue())) { |
| Evan Cheng | 364091e | 2008-09-22 23:57:37 +0000 | [diff] [blame] | 18319 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| 18320 | break; |
| 18321 | } |
| 18322 | } |
| 18323 | return; |
| Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 18324 | case 'N': |
| 18325 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 18326 | if (C->getZExtValue() <= 255) { |
| 18327 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 18328 | break; |
| 18329 | } |
| Chris Lattner | 188b9fe | 2007-03-25 01:57:35 +0000 | [diff] [blame] | 18330 | } |
| Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 18331 | return; |
| Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 18332 | case 'e': { |
| 18333 | // 32-bit signed value |
| 18334 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| Dan Gohman | 7720cb3 | 2010-06-18 14:01:07 +0000 | [diff] [blame] | 18335 | if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()), |
| 18336 | C->getSExtValue())) { |
| Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 18337 | // Widen to 64 bits here to get it sign extended. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 18338 | Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64); |
| Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 18339 | break; |
| 18340 | } |
| 18341 | // FIXME gcc accepts some relocatable values here too, but only in certain |
| 18342 | // memory models; it's complicated. |
| 18343 | } |
| 18344 | return; |
| 18345 | } |
| 18346 | case 'Z': { |
| 18347 | // 32-bit unsigned value |
| 18348 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { |
| Dan Gohman | 7720cb3 | 2010-06-18 14:01:07 +0000 | [diff] [blame] | 18349 | if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()), |
| 18350 | C->getZExtValue())) { |
| Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 18351 | Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType()); |
| 18352 | break; |
| 18353 | } |
| 18354 | } |
| 18355 | // FIXME gcc accepts some relocatable values here too, but only in certain |
| 18356 | // memory models; it's complicated. |
| 18357 | return; |
| 18358 | } |
| Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 18359 | case 'i': { |
| Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 18360 | // Literal immediates are always ok. |
| Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 18361 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) { |
| Dale Johannesen | 78e3e52 | 2009-02-12 20:58:09 +0000 | [diff] [blame] | 18362 | // Widen to 64 bits here to get it sign extended. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 18363 | Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64); |
| Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 18364 | break; |
| 18365 | } |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 18366 | |
| Dale Johannesen | e5ff9ef | 2010-06-24 20:14:51 +0000 | [diff] [blame] | 18367 | // In any sort of PIC mode addresses need to be computed at runtime by |
| 18368 | // adding in a register or some sort of table lookup. These can't |
| 18369 | // be used as immediates. |
| Dale Johannesen | e2b448c | 2010-07-06 23:27:00 +0000 | [diff] [blame] | 18370 | if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC()) |
| Dale Johannesen | e5ff9ef | 2010-06-24 20:14:51 +0000 | [diff] [blame] | 18371 | return; |
| 18372 | |
| Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 18373 | // If we are in non-pic codegen mode, we allow the address of a global (with |
| 18374 | // an optional displacement) to be used with 'i'. |
| Chris Lattner | 4992196 | 2009-05-08 18:23:14 +0000 | [diff] [blame] | 18375 | GlobalAddressSDNode *GA = 0; |
| Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 18376 | int64_t Offset = 0; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 18377 | |
| Chris Lattner | 4992196 | 2009-05-08 18:23:14 +0000 | [diff] [blame] | 18378 | // Match either (GA), (GA+C), (GA+C1+C2), etc. |
| 18379 | while (1) { |
| 18380 | if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) { |
| 18381 | Offset += GA->getOffset(); |
| 18382 | break; |
| 18383 | } else if (Op.getOpcode() == ISD::ADD) { |
| 18384 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 18385 | Offset += C->getZExtValue(); |
| 18386 | Op = Op.getOperand(0); |
| 18387 | continue; |
| 18388 | } |
| 18389 | } else if (Op.getOpcode() == ISD::SUB) { |
| 18390 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 18391 | Offset += -C->getZExtValue(); |
| 18392 | Op = Op.getOperand(0); |
| 18393 | continue; |
| 18394 | } |
| Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 18395 | } |
| Dale Johannesen | 76a1e2e | 2009-07-07 00:18:49 +0000 | [diff] [blame] | 18396 | |
| Chris Lattner | 4992196 | 2009-05-08 18:23:14 +0000 | [diff] [blame] | 18397 | // Otherwise, this isn't something we can handle, reject it. |
| 18398 | return; |
| Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 18399 | } |
| Eric Christopher | fd17929 | 2009-08-27 18:07:15 +0000 | [diff] [blame] | 18400 | |
| Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 18401 | const GlobalValue *GV = GA->getGlobal(); |
| Dale Johannesen | 76a1e2e | 2009-07-07 00:18:49 +0000 | [diff] [blame] | 18402 | // If we require an extra load to get this address, as in PIC mode, we |
| 18403 | // can't accept it. |
| Chris Lattner | 36c2501 | 2009-07-10 07:34:39 +0000 | [diff] [blame] | 18404 | if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV, |
| 18405 | getTargetMachine()))) |
| Dale Johannesen | 76a1e2e | 2009-07-07 00:18:49 +0000 | [diff] [blame] | 18406 | return; |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 18407 | |
| Andrew Trick | ac6d9be | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 18408 | Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op), |
| Devang Patel | 0d881da | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 18409 | GA->getValueType(0), Offset); |
| Chris Lattner | 4992196 | 2009-05-08 18:23:14 +0000 | [diff] [blame] | 18410 | break; |
| Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 18411 | } |
| Chris Lattner | dc43a88 | 2007-05-03 16:52:29 +0000 | [diff] [blame] | 18412 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 18413 | |
| Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 18414 | if (Result.getNode()) { |
| Chris Lattner | 48884cd | 2007-08-25 00:47:38 +0000 | [diff] [blame] | 18415 | Ops.push_back(Result); |
| 18416 | return; |
| 18417 | } |
| Dale Johannesen | 1784d16 | 2010-06-25 21:55:36 +0000 | [diff] [blame] | 18418 | return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); |
| Chris Lattner | 22aaf1d | 2006-10-31 20:13:11 +0000 | [diff] [blame] | 18419 | } |
| 18420 | |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 18421 | std::pair<unsigned, const TargetRegisterClass*> |
| Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 18422 | X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, |
| Chad Rosier | 5b3fca5 | 2013-06-22 18:37:38 +0000 | [diff] [blame] | 18423 | MVT VT) const { |
| Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 18424 | // First, see if this is a constraint that directly corresponds to an LLVM |
| 18425 | // register class. |
| 18426 | if (Constraint.size() == 1) { |
| 18427 | // GCC Constraint Letters |
| 18428 | switch (Constraint[0]) { |
| 18429 | default: break; |
| Eric Christopher | d176af8 | 2011-06-29 17:23:50 +0000 | [diff] [blame] | 18430 | // TODO: Slight differences here in allocation order and leaving |
| 18431 | // RIP in the class. Do they matter any more here than they do |
| 18432 | // in the normal allocation? |
| 18433 | case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode. |
| 18434 | if (Subtarget->is64Bit()) { |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18435 | if (VT == MVT::i32 || VT == MVT::f32) |
| 18436 | return std::make_pair(0U, &X86::GR32RegClass); |
| 18437 | if (VT == MVT::i16) |
| 18438 | return std::make_pair(0U, &X86::GR16RegClass); |
| 18439 | if (VT == MVT::i8 || VT == MVT::i1) |
| 18440 | return std::make_pair(0U, &X86::GR8RegClass); |
| 18441 | if (VT == MVT::i64 || VT == MVT::f64) |
| 18442 | return std::make_pair(0U, &X86::GR64RegClass); |
| 18443 | break; |
| Eric Christopher | d176af8 | 2011-06-29 17:23:50 +0000 | [diff] [blame] | 18444 | } |
| 18445 | // 32-bit fallthrough |
| 18446 | case 'Q': // Q_REGS |
| Nick Lewycky | 9bf45d0 | 2011-07-08 00:19:27 +0000 | [diff] [blame] | 18447 | if (VT == MVT::i32 || VT == MVT::f32) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18448 | return std::make_pair(0U, &X86::GR32_ABCDRegClass); |
| 18449 | if (VT == MVT::i16) |
| 18450 | return std::make_pair(0U, &X86::GR16_ABCDRegClass); |
| 18451 | if (VT == MVT::i8 || VT == MVT::i1) |
| 18452 | return std::make_pair(0U, &X86::GR8_ABCD_LRegClass); |
| 18453 | if (VT == MVT::i64) |
| 18454 | return std::make_pair(0U, &X86::GR64_ABCDRegClass); |
| Eric Christopher | d176af8 | 2011-06-29 17:23:50 +0000 | [diff] [blame] | 18455 | break; |
| Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 18456 | case 'r': // GENERAL_REGS |
| Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 18457 | case 'l': // INDEX_REGS |
| Eric Christopher | 5427ede | 2011-07-14 20:13:52 +0000 | [diff] [blame] | 18458 | if (VT == MVT::i8 || VT == MVT::i1) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18459 | return std::make_pair(0U, &X86::GR8RegClass); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 18460 | if (VT == MVT::i16) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18461 | return std::make_pair(0U, &X86::GR16RegClass); |
| Eric Christopher | 2bbecd8 | 2011-05-19 21:33:47 +0000 | [diff] [blame] | 18462 | if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit()) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18463 | return std::make_pair(0U, &X86::GR32RegClass); |
| 18464 | return std::make_pair(0U, &X86::GR64RegClass); |
| Dale Johannesen | 5f3663e | 2009-10-07 22:47:20 +0000 | [diff] [blame] | 18465 | case 'R': // LEGACY_REGS |
| Eric Christopher | 5427ede | 2011-07-14 20:13:52 +0000 | [diff] [blame] | 18466 | if (VT == MVT::i8 || VT == MVT::i1) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18467 | return std::make_pair(0U, &X86::GR8_NOREXRegClass); |
| Dale Johannesen | 5f3663e | 2009-10-07 22:47:20 +0000 | [diff] [blame] | 18468 | if (VT == MVT::i16) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18469 | return std::make_pair(0U, &X86::GR16_NOREXRegClass); |
| Dale Johannesen | 5f3663e | 2009-10-07 22:47:20 +0000 | [diff] [blame] | 18470 | if (VT == MVT::i32 || !Subtarget->is64Bit()) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18471 | return std::make_pair(0U, &X86::GR32_NOREXRegClass); |
| 18472 | return std::make_pair(0U, &X86::GR64_NOREXRegClass); |
| Chris Lattner | fce84ac | 2008-03-11 19:06:29 +0000 | [diff] [blame] | 18473 | case 'f': // FP Stack registers. |
| 18474 | // If SSE is enabled for this VT, use f80 to ensure the isel moves the |
| 18475 | // value to the correct fpstack register class. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 18476 | if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT)) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18477 | return std::make_pair(0U, &X86::RFP32RegClass); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 18478 | if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT)) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18479 | return std::make_pair(0U, &X86::RFP64RegClass); |
| 18480 | return std::make_pair(0U, &X86::RFP80RegClass); |
| Chris Lattner | 6c284d7 | 2007-04-12 04:14:49 +0000 | [diff] [blame] | 18481 | case 'y': // MMX_REGS if MMX allowed. |
| 18482 | if (!Subtarget->hasMMX()) break; |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18483 | return std::make_pair(0U, &X86::VR64RegClass); |
| Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 18484 | case 'Y': // SSE_REGS if SSE2 allowed |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 18485 | if (!Subtarget->hasSSE2()) break; |
| Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 18486 | // FALL THROUGH. |
| Eric Christopher | 5548755 | 2012-01-07 01:02:09 +0000 | [diff] [blame] | 18487 | case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed |
| Craig Topper | 1accb7e | 2012-01-10 06:54:16 +0000 | [diff] [blame] | 18488 | if (!Subtarget->hasSSE1()) break; |
| Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 18489 | |
| Chad Rosier | 5b3fca5 | 2013-06-22 18:37:38 +0000 | [diff] [blame] | 18490 | switch (VT.SimpleTy) { |
| Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 18491 | default: break; |
| 18492 | // Scalar SSE types. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 18493 | case MVT::f32: |
| 18494 | case MVT::i32: |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18495 | return std::make_pair(0U, &X86::FR32RegClass); |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 18496 | case MVT::f64: |
| 18497 | case MVT::i64: |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18498 | return std::make_pair(0U, &X86::FR64RegClass); |
| Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 18499 | // Vector types. |
| Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 18500 | case MVT::v16i8: |
| 18501 | case MVT::v8i16: |
| 18502 | case MVT::v4i32: |
| 18503 | case MVT::v2i64: |
| 18504 | case MVT::v4f32: |
| 18505 | case MVT::v2f64: |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18506 | return std::make_pair(0U, &X86::VR128RegClass); |
| Eric Christopher | 5548755 | 2012-01-07 01:02:09 +0000 | [diff] [blame] | 18507 | // AVX types. |
| 18508 | case MVT::v32i8: |
| 18509 | case MVT::v16i16: |
| 18510 | case MVT::v8i32: |
| 18511 | case MVT::v4i64: |
| 18512 | case MVT::v8f32: |
| 18513 | case MVT::v4f64: |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18514 | return std::make_pair(0U, &X86::VR256RegClass); |
| Chris Lattner | 0f65cad | 2007-04-09 05:49:22 +0000 | [diff] [blame] | 18515 | } |
| Chris Lattner | ad043e8 | 2007-04-09 05:11:28 +0000 | [diff] [blame] | 18516 | break; |
| 18517 | } |
| 18518 | } |
| Scott Michel | fdc40a0 | 2009-02-17 22:15:04 +0000 | [diff] [blame] | 18519 | |
| Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 18520 | // Use the default implementation in TargetLowering to convert the register |
| 18521 | // constraint into a member of a register class. |
| 18522 | std::pair<unsigned, const TargetRegisterClass*> Res; |
| 18523 | Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); |
| Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 18524 | |
| 18525 | // Not found as a standard register? |
| 18526 | if (Res.second == 0) { |
| Chris Lattner | 56d77c7 | 2009-09-13 22:41:48 +0000 | [diff] [blame] | 18527 | // Map st(0) -> st(7) -> ST0 |
| 18528 | if (Constraint.size() == 7 && Constraint[0] == '{' && |
| 18529 | tolower(Constraint[1]) == 's' && |
| 18530 | tolower(Constraint[2]) == 't' && |
| 18531 | Constraint[3] == '(' && |
| 18532 | (Constraint[4] >= '0' && Constraint[4] <= '7') && |
| 18533 | Constraint[5] == ')' && |
| 18534 | Constraint[6] == '}') { |
| Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 18535 | |
| Chris Lattner | 56d77c7 | 2009-09-13 22:41:48 +0000 | [diff] [blame] | 18536 | Res.first = X86::ST0+Constraint[4]-'0'; |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18537 | Res.second = &X86::RFP80RegClass; |
| Chris Lattner | 56d77c7 | 2009-09-13 22:41:48 +0000 | [diff] [blame] | 18538 | return Res; |
| 18539 | } |
| Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 18540 | |
| Chris Lattner | 56d77c7 | 2009-09-13 22:41:48 +0000 | [diff] [blame] | 18541 | // GCC allows "st(0)" to be called just plain "st". |
| Benjamin Kramer | 05872ea | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 18542 | if (StringRef("{st}").equals_lower(Constraint)) { |
| Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 18543 | Res.first = X86::ST0; |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18544 | Res.second = &X86::RFP80RegClass; |
| Chris Lattner | 56d77c7 | 2009-09-13 22:41:48 +0000 | [diff] [blame] | 18545 | return Res; |
| Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 18546 | } |
| Chris Lattner | 56d77c7 | 2009-09-13 22:41:48 +0000 | [diff] [blame] | 18547 | |
| 18548 | // flags -> EFLAGS |
| Benjamin Kramer | 05872ea | 2009-11-12 20:36:59 +0000 | [diff] [blame] | 18549 | if (StringRef("{flags}").equals_lower(Constraint)) { |
| Chris Lattner | 56d77c7 | 2009-09-13 22:41:48 +0000 | [diff] [blame] | 18550 | Res.first = X86::EFLAGS; |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18551 | Res.second = &X86::CCRRegClass; |
| Chris Lattner | 56d77c7 | 2009-09-13 22:41:48 +0000 | [diff] [blame] | 18552 | return Res; |
| 18553 | } |
| Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 18554 | |
| Dale Johannesen | 330169f | 2008-11-13 21:52:36 +0000 | [diff] [blame] | 18555 | // 'A' means EAX + EDX. |
| 18556 | if (Constraint == "A") { |
| 18557 | Res.first = X86::EAX; |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18558 | Res.second = &X86::GR32_ADRegClass; |
| Chris Lattner | 56d77c7 | 2009-09-13 22:41:48 +0000 | [diff] [blame] | 18559 | return Res; |
| Dale Johannesen | 330169f | 2008-11-13 21:52:36 +0000 | [diff] [blame] | 18560 | } |
| Chris Lattner | 1a60aa7 | 2006-10-31 19:42:44 +0000 | [diff] [blame] | 18561 | return Res; |
| 18562 | } |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 18563 | |
| Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 18564 | // Otherwise, check to see if this is a register class of the wrong value |
| 18565 | // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to |
| 18566 | // turn into {ax},{dx}. |
| 18567 | if (Res.second->hasType(VT)) |
| 18568 | return Res; // Correct type already, nothing to do. |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 18569 | |
| Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 18570 | // All of the single-register GCC register classes map their values onto |
| 18571 | // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we |
| 18572 | // really want an 8-bit or 32-bit register, map to the appropriate register |
| 18573 | // class and return the appropriate register. |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18574 | if (Res.second == &X86::GR16RegClass) { |
| Eric Christopher | 23571f4 | 2013-02-13 06:01:05 +0000 | [diff] [blame] | 18575 | if (VT == MVT::i8 || VT == MVT::i1) { |
| Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 18576 | unsigned DestReg = 0; |
| 18577 | switch (Res.first) { |
| 18578 | default: break; |
| 18579 | case X86::AX: DestReg = X86::AL; break; |
| 18580 | case X86::DX: DestReg = X86::DL; break; |
| 18581 | case X86::CX: DestReg = X86::CL; break; |
| 18582 | case X86::BX: DestReg = X86::BL; break; |
| 18583 | } |
| 18584 | if (DestReg) { |
| 18585 | Res.first = DestReg; |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18586 | Res.second = &X86::GR8RegClass; |
| Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 18587 | } |
| Eric Christopher | a9bd4b4 | 2013-01-31 00:50:46 +0000 | [diff] [blame] | 18588 | } else if (VT == MVT::i32 || VT == MVT::f32) { |
| Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 18589 | unsigned DestReg = 0; |
| 18590 | switch (Res.first) { |
| 18591 | default: break; |
| 18592 | case X86::AX: DestReg = X86::EAX; break; |
| 18593 | case X86::DX: DestReg = X86::EDX; break; |
| 18594 | case X86::CX: DestReg = X86::ECX; break; |
| 18595 | case X86::BX: DestReg = X86::EBX; break; |
| 18596 | case X86::SI: DestReg = X86::ESI; break; |
| 18597 | case X86::DI: DestReg = X86::EDI; break; |
| 18598 | case X86::BP: DestReg = X86::EBP; break; |
| 18599 | case X86::SP: DestReg = X86::ESP; break; |
| 18600 | } |
| 18601 | if (DestReg) { |
| 18602 | Res.first = DestReg; |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18603 | Res.second = &X86::GR32RegClass; |
| Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 18604 | } |
| Eric Christopher | a9bd4b4 | 2013-01-31 00:50:46 +0000 | [diff] [blame] | 18605 | } else if (VT == MVT::i64 || VT == MVT::f64) { |
| Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 18606 | unsigned DestReg = 0; |
| 18607 | switch (Res.first) { |
| 18608 | default: break; |
| 18609 | case X86::AX: DestReg = X86::RAX; break; |
| 18610 | case X86::DX: DestReg = X86::RDX; break; |
| 18611 | case X86::CX: DestReg = X86::RCX; break; |
| 18612 | case X86::BX: DestReg = X86::RBX; break; |
| 18613 | case X86::SI: DestReg = X86::RSI; break; |
| 18614 | case X86::DI: DestReg = X86::RDI; break; |
| 18615 | case X86::BP: DestReg = X86::RBP; break; |
| 18616 | case X86::SP: DestReg = X86::RSP; break; |
| 18617 | } |
| 18618 | if (DestReg) { |
| 18619 | Res.first = DestReg; |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18620 | Res.second = &X86::GR64RegClass; |
| Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 18621 | } |
| Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 18622 | } |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18623 | } else if (Res.second == &X86::FR32RegClass || |
| 18624 | Res.second == &X86::FR64RegClass || |
| 18625 | Res.second == &X86::VR128RegClass) { |
| Chris Lattner | 6ba50a9 | 2008-08-26 06:19:02 +0000 | [diff] [blame] | 18626 | // Handle references to XMM physical registers that got mapped into the |
| 18627 | // wrong class. This can happen with constraints like {xmm0} where the |
| 18628 | // target independent register mapper will just pick the first match it can |
| 18629 | // find, ignoring the required type. |
| Eli Friedman | 52d418d | 2012-06-25 23:42:33 +0000 | [diff] [blame] | 18630 | |
| 18631 | if (VT == MVT::f32 || VT == MVT::i32) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18632 | Res.second = &X86::FR32RegClass; |
| Eli Friedman | 52d418d | 2012-06-25 23:42:33 +0000 | [diff] [blame] | 18633 | else if (VT == MVT::f64 || VT == MVT::i64) |
| Craig Topper | c909950 | 2012-04-20 06:31:50 +0000 | [diff] [blame] | 18634 | Res.second = &X86::FR64RegClass; |
| 18635 | else if (X86::VR128RegClass.hasType(VT)) |
| 18636 | Res.second = &X86::VR128RegClass; |
| Eli Friedman | 52d418d | 2012-06-25 23:42:33 +0000 | [diff] [blame] | 18637 | else if (X86::VR256RegClass.hasType(VT)) |
| 18638 | Res.second = &X86::VR256RegClass; |
| Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 18639 | } |
| Anton Korobeynikov | 12c49af | 2006-11-21 00:01:06 +0000 | [diff] [blame] | 18640 | |
| Chris Lattner | f76d180 | 2006-07-31 23:26:50 +0000 | [diff] [blame] | 18641 | return Res; |
| 18642 | } |