Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1 | //===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the SystemZTargetLowering class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 14 | #include "SystemZISelLowering.h" |
| 15 | #include "SystemZCallingConv.h" |
| 16 | #include "SystemZConstantPoolValue.h" |
| 17 | #include "SystemZMachineFunctionInfo.h" |
| 18 | #include "SystemZTargetMachine.h" |
| 19 | #include "llvm/CodeGen/CallingConvLower.h" |
| 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 22 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Intrinsics.h" |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 24 | #include <cctype> |
| 25 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 28 | #define DEBUG_TYPE "systemz-lower" |
| 29 | |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | // Represents a sequence for extracting a 0/1 value from an IPM result: |
| 32 | // (((X ^ XORValue) + AddValue) >> Bit) |
| 33 | struct IPMConversion { |
| 34 | IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit) |
| 35 | : XORValue(xorValue), AddValue(addValue), Bit(bit) {} |
| 36 | |
| 37 | int64_t XORValue; |
| 38 | int64_t AddValue; |
| 39 | unsigned Bit; |
| 40 | }; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 41 | |
| 42 | // Represents information about a comparison. |
| 43 | struct Comparison { |
| 44 | Comparison(SDValue Op0In, SDValue Op1In) |
| 45 | : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {} |
| 46 | |
| 47 | // The operands to the comparison. |
| 48 | SDValue Op0, Op1; |
| 49 | |
| 50 | // The opcode that should be used to compare Op0 and Op1. |
| 51 | unsigned Opcode; |
| 52 | |
| 53 | // A SystemZICMP value. Only used for integer comparisons. |
| 54 | unsigned ICmpType; |
| 55 | |
| 56 | // The mask of CC values that Opcode can produce. |
| 57 | unsigned CCValid; |
| 58 | |
| 59 | // The mask of CC values for which the original condition is true. |
| 60 | unsigned CCMask; |
| 61 | }; |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 62 | } // end anonymous namespace |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 63 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 64 | // Classify VT as either 32 or 64 bit. |
| 65 | static bool is32Bit(EVT VT) { |
| 66 | switch (VT.getSimpleVT().SimpleTy) { |
| 67 | case MVT::i32: |
| 68 | return true; |
| 69 | case MVT::i64: |
| 70 | return false; |
| 71 | default: |
| 72 | llvm_unreachable("Unsupported type"); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Return a version of MachineOperand that can be safely used before the |
| 77 | // final use. |
| 78 | static MachineOperand earlyUseOperand(MachineOperand Op) { |
| 79 | if (Op.isReg()) |
| 80 | Op.setIsKill(false); |
| 81 | return Op; |
| 82 | } |
| 83 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 84 | SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM, |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 85 | const SystemZSubtarget &STI) |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 86 | : TargetLowering(TM), Subtarget(STI) { |
Mehdi Amini | 26d4813 | 2015-07-24 16:04:22 +0000 | [diff] [blame] | 87 | MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 88 | |
| 89 | // Set up the register classes. |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 90 | if (Subtarget.hasHighWord()) |
| 91 | addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass); |
| 92 | else |
| 93 | addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass); |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 94 | addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass); |
| 95 | if (Subtarget.hasVector()) { |
| 96 | addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass); |
| 97 | addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass); |
| 98 | } else { |
| 99 | addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass); |
| 100 | addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass); |
| 101 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 102 | addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass); |
| 103 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 104 | if (Subtarget.hasVector()) { |
| 105 | addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass); |
| 106 | addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass); |
| 107 | addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass); |
| 108 | addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 109 | addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 110 | addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 113 | // Compute derived properties from the register classes |
Eric Christopher | 23a3a7c | 2015-02-26 00:00:24 +0000 | [diff] [blame] | 114 | computeRegisterProperties(Subtarget.getRegisterInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 115 | |
| 116 | // Set up special registers. |
| 117 | setExceptionPointerRegister(SystemZ::R6D); |
| 118 | setExceptionSelectorRegister(SystemZ::R7D); |
| 119 | setStackPointerRegisterToSaveRestore(SystemZ::R15D); |
| 120 | |
| 121 | // TODO: It may be better to default to latency-oriented scheduling, however |
| 122 | // LLVM's current latency-oriented scheduler can't handle physreg definitions |
Richard Sandiford | 14a4449 | 2013-05-22 13:38:45 +0000 | [diff] [blame] | 123 | // such as SystemZ has with CC, so set this to the register-pressure |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 124 | // scheduler, because it can. |
| 125 | setSchedulingPreference(Sched::RegPressure); |
| 126 | |
| 127 | setBooleanContents(ZeroOrOneBooleanContent); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 128 | setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 129 | |
| 130 | // Instructions are strings of 2-byte aligned 2-byte values. |
| 131 | setMinFunctionAlignment(2); |
| 132 | |
| 133 | // Handle operations that are handled in a similar way for all types. |
| 134 | for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; |
| 135 | I <= MVT::LAST_FP_VALUETYPE; |
| 136 | ++I) { |
| 137 | MVT VT = MVT::SimpleValueType(I); |
| 138 | if (isTypeLegal(VT)) { |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 139 | // Lower SET_CC into an IPM-based sequence. |
| 140 | setOperationAction(ISD::SETCC, VT, Custom); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 141 | |
| 142 | // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE). |
| 143 | setOperationAction(ISD::SELECT, VT, Expand); |
| 144 | |
| 145 | // Lower SELECT_CC and BR_CC into separate comparisons and branches. |
| 146 | setOperationAction(ISD::SELECT_CC, VT, Custom); |
| 147 | setOperationAction(ISD::BR_CC, VT, Custom); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Expand jump table branches as address arithmetic followed by an |
| 152 | // indirect jump. |
| 153 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
| 154 | |
| 155 | // Expand BRCOND into a BR_CC (see above). |
| 156 | setOperationAction(ISD::BRCOND, MVT::Other, Expand); |
| 157 | |
| 158 | // Handle integer types. |
| 159 | for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; |
| 160 | I <= MVT::LAST_INTEGER_VALUETYPE; |
| 161 | ++I) { |
| 162 | MVT VT = MVT::SimpleValueType(I); |
| 163 | if (isTypeLegal(VT)) { |
| 164 | // Expand individual DIV and REMs into DIVREMs. |
| 165 | setOperationAction(ISD::SDIV, VT, Expand); |
| 166 | setOperationAction(ISD::UDIV, VT, Expand); |
| 167 | setOperationAction(ISD::SREM, VT, Expand); |
| 168 | setOperationAction(ISD::UREM, VT, Expand); |
| 169 | setOperationAction(ISD::SDIVREM, VT, Custom); |
| 170 | setOperationAction(ISD::UDIVREM, VT, Custom); |
| 171 | |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 172 | // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and |
| 173 | // stores, putting a serialization instruction after the stores. |
| 174 | setOperationAction(ISD::ATOMIC_LOAD, VT, Custom); |
| 175 | setOperationAction(ISD::ATOMIC_STORE, VT, Custom); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 176 | |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 177 | // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are |
| 178 | // available, or if the operand is constant. |
| 179 | setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom); |
| 180 | |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 181 | // Use POPCNT on z196 and above. |
| 182 | if (Subtarget.hasPopulationCount()) |
| 183 | setOperationAction(ISD::CTPOP, VT, Custom); |
| 184 | else |
| 185 | setOperationAction(ISD::CTPOP, VT, Expand); |
| 186 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 187 | // No special instructions for these. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 188 | setOperationAction(ISD::CTTZ, VT, Expand); |
| 189 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand); |
| 190 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand); |
| 191 | setOperationAction(ISD::ROTR, VT, Expand); |
| 192 | |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 193 | // Use *MUL_LOHI where possible instead of MULH*. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 194 | setOperationAction(ISD::MULHS, VT, Expand); |
| 195 | setOperationAction(ISD::MULHU, VT, Expand); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 196 | setOperationAction(ISD::SMUL_LOHI, VT, Custom); |
| 197 | setOperationAction(ISD::UMUL_LOHI, VT, Custom); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 198 | |
Richard Sandiford | dc6c2c9 | 2014-03-21 10:56:30 +0000 | [diff] [blame] | 199 | // Only z196 and above have native support for conversions to unsigned. |
| 200 | if (!Subtarget.hasFPExtension()) |
| 201 | setOperationAction(ISD::FP_TO_UINT, VT, Expand); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | // Type legalization will convert 8- and 16-bit atomic operations into |
| 206 | // forms that operate on i32s (but still keeping the original memory VT). |
| 207 | // Lower them into full i32 operations. |
| 208 | setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom); |
| 209 | setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom); |
| 210 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom); |
| 211 | setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom); |
| 212 | setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom); |
| 213 | setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom); |
| 214 | setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom); |
| 215 | setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom); |
| 216 | setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom); |
| 217 | setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom); |
| 218 | setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom); |
| 219 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); |
| 220 | |
Richard Sandiford | dc6c2c9 | 2014-03-21 10:56:30 +0000 | [diff] [blame] | 221 | // z10 has instructions for signed but not unsigned FP conversion. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 222 | // Handle unsigned 32-bit types as signed 64-bit types. |
Richard Sandiford | dc6c2c9 | 2014-03-21 10:56:30 +0000 | [diff] [blame] | 223 | if (!Subtarget.hasFPExtension()) { |
| 224 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote); |
| 225 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); |
| 226 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 227 | |
| 228 | // We have native support for a 64-bit CTLZ, via FLOGR. |
| 229 | setOperationAction(ISD::CTLZ, MVT::i32, Promote); |
| 230 | setOperationAction(ISD::CTLZ, MVT::i64, Legal); |
| 231 | |
| 232 | // Give LowerOperation the chance to replace 64-bit ORs with subregs. |
| 233 | setOperationAction(ISD::OR, MVT::i64, Custom); |
| 234 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 235 | // FIXME: Can we support these natively? |
| 236 | setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand); |
| 237 | setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand); |
| 238 | setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand); |
| 239 | |
| 240 | // We have native instructions for i8, i16 and i32 extensions, but not i1. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 241 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 242 | for (MVT VT : MVT::integer_valuetypes()) { |
| 243 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); |
| 244 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); |
| 245 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); |
| 246 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 247 | |
| 248 | // Handle the various types of symbolic address. |
| 249 | setOperationAction(ISD::ConstantPool, PtrVT, Custom); |
| 250 | setOperationAction(ISD::GlobalAddress, PtrVT, Custom); |
| 251 | setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom); |
| 252 | setOperationAction(ISD::BlockAddress, PtrVT, Custom); |
| 253 | setOperationAction(ISD::JumpTable, PtrVT, Custom); |
| 254 | |
| 255 | // We need to handle dynamic allocations specially because of the |
| 256 | // 160-byte area at the bottom of the stack. |
| 257 | setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom); |
| 258 | |
| 259 | // Use custom expanders so that we can force the function to use |
| 260 | // a frame pointer. |
| 261 | setOperationAction(ISD::STACKSAVE, MVT::Other, Custom); |
| 262 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom); |
| 263 | |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 264 | // Handle prefetches with PFD or PFDRL. |
| 265 | setOperationAction(ISD::PREFETCH, MVT::Other, Custom); |
| 266 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 267 | for (MVT VT : MVT::vector_valuetypes()) { |
| 268 | // Assume by default that all vector operations need to be expanded. |
| 269 | for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode) |
| 270 | if (getOperationAction(Opcode, VT) == Legal) |
| 271 | setOperationAction(Opcode, VT, Expand); |
| 272 | |
| 273 | // Likewise all truncating stores and extending loads. |
| 274 | for (MVT InnerVT : MVT::vector_valuetypes()) { |
| 275 | setTruncStoreAction(VT, InnerVT, Expand); |
| 276 | setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); |
| 277 | setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); |
| 278 | setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); |
| 279 | } |
| 280 | |
| 281 | if (isTypeLegal(VT)) { |
| 282 | // These operations are legal for anything that can be stored in a |
| 283 | // vector register, even if there is no native support for the format |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 284 | // as such. In particular, we can do these for v4f32 even though there |
| 285 | // are no specific instructions for that format. |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 286 | setOperationAction(ISD::LOAD, VT, Legal); |
| 287 | setOperationAction(ISD::STORE, VT, Legal); |
| 288 | setOperationAction(ISD::VSELECT, VT, Legal); |
| 289 | setOperationAction(ISD::BITCAST, VT, Legal); |
| 290 | setOperationAction(ISD::UNDEF, VT, Legal); |
| 291 | |
| 292 | // Likewise, except that we need to replace the nodes with something |
| 293 | // more specific. |
| 294 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); |
| 295 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Handle integer vector types. |
| 300 | for (MVT VT : MVT::integer_vector_valuetypes()) { |
| 301 | if (isTypeLegal(VT)) { |
| 302 | // These operations have direct equivalents. |
| 303 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal); |
| 304 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal); |
| 305 | setOperationAction(ISD::ADD, VT, Legal); |
| 306 | setOperationAction(ISD::SUB, VT, Legal); |
| 307 | if (VT != MVT::v2i64) |
| 308 | setOperationAction(ISD::MUL, VT, Legal); |
| 309 | setOperationAction(ISD::AND, VT, Legal); |
| 310 | setOperationAction(ISD::OR, VT, Legal); |
| 311 | setOperationAction(ISD::XOR, VT, Legal); |
| 312 | setOperationAction(ISD::CTPOP, VT, Custom); |
| 313 | setOperationAction(ISD::CTTZ, VT, Legal); |
| 314 | setOperationAction(ISD::CTLZ, VT, Legal); |
| 315 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Custom); |
| 316 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Custom); |
| 317 | |
| 318 | // Convert a GPR scalar to a vector by inserting it into element 0. |
| 319 | setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom); |
| 320 | |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 321 | // Use a series of unpacks for extensions. |
| 322 | setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom); |
| 323 | setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom); |
| 324 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 325 | // Detect shifts by a scalar amount and convert them into |
| 326 | // V*_BY_SCALAR. |
| 327 | setOperationAction(ISD::SHL, VT, Custom); |
| 328 | setOperationAction(ISD::SRA, VT, Custom); |
| 329 | setOperationAction(ISD::SRL, VT, Custom); |
| 330 | |
| 331 | // At present ROTL isn't matched by DAGCombiner. ROTR should be |
| 332 | // converted into ROTL. |
| 333 | setOperationAction(ISD::ROTL, VT, Expand); |
| 334 | setOperationAction(ISD::ROTR, VT, Expand); |
| 335 | |
| 336 | // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands |
| 337 | // and inverting the result as necessary. |
| 338 | setOperationAction(ISD::SETCC, VT, Custom); |
| 339 | } |
| 340 | } |
| 341 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 342 | if (Subtarget.hasVector()) { |
| 343 | // There should be no need to check for float types other than v2f64 |
| 344 | // since <2 x f32> isn't a legal type. |
| 345 | setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal); |
| 346 | setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal); |
| 347 | setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal); |
| 348 | setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal); |
| 349 | } |
| 350 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 351 | // Handle floating-point types. |
| 352 | for (unsigned I = MVT::FIRST_FP_VALUETYPE; |
| 353 | I <= MVT::LAST_FP_VALUETYPE; |
| 354 | ++I) { |
| 355 | MVT VT = MVT::SimpleValueType(I); |
| 356 | if (isTypeLegal(VT)) { |
| 357 | // We can use FI for FRINT. |
| 358 | setOperationAction(ISD::FRINT, VT, Legal); |
| 359 | |
Richard Sandiford | af5f66a | 2013-08-21 09:04:20 +0000 | [diff] [blame] | 360 | // We can use the extended form of FI for other rounding operations. |
| 361 | if (Subtarget.hasFPExtension()) { |
| 362 | setOperationAction(ISD::FNEARBYINT, VT, Legal); |
| 363 | setOperationAction(ISD::FFLOOR, VT, Legal); |
| 364 | setOperationAction(ISD::FCEIL, VT, Legal); |
| 365 | setOperationAction(ISD::FTRUNC, VT, Legal); |
| 366 | setOperationAction(ISD::FROUND, VT, Legal); |
| 367 | } |
| 368 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 369 | // No special instructions for these. |
| 370 | setOperationAction(ISD::FSIN, VT, Expand); |
| 371 | setOperationAction(ISD::FCOS, VT, Expand); |
Ulrich Weigand | 126caeb | 2015-09-21 17:35:45 +0000 | [diff] [blame] | 372 | setOperationAction(ISD::FSINCOS, VT, Expand); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 373 | setOperationAction(ISD::FREM, VT, Expand); |
Ulrich Weigand | 126caeb | 2015-09-21 17:35:45 +0000 | [diff] [blame] | 374 | setOperationAction(ISD::FPOW, VT, Expand); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 378 | // Handle floating-point vector types. |
| 379 | if (Subtarget.hasVector()) { |
| 380 | // Scalar-to-vector conversion is just a subreg. |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 381 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 382 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal); |
| 383 | |
| 384 | // Some insertions and extractions can be done directly but others |
| 385 | // need to go via integers. |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 386 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 387 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 388 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 389 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); |
| 390 | |
| 391 | // These operations have direct equivalents. |
| 392 | setOperationAction(ISD::FADD, MVT::v2f64, Legal); |
| 393 | setOperationAction(ISD::FNEG, MVT::v2f64, Legal); |
| 394 | setOperationAction(ISD::FSUB, MVT::v2f64, Legal); |
| 395 | setOperationAction(ISD::FMUL, MVT::v2f64, Legal); |
| 396 | setOperationAction(ISD::FMA, MVT::v2f64, Legal); |
| 397 | setOperationAction(ISD::FDIV, MVT::v2f64, Legal); |
| 398 | setOperationAction(ISD::FABS, MVT::v2f64, Legal); |
| 399 | setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); |
| 400 | setOperationAction(ISD::FRINT, MVT::v2f64, Legal); |
| 401 | setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal); |
| 402 | setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal); |
| 403 | setOperationAction(ISD::FCEIL, MVT::v2f64, Legal); |
| 404 | setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal); |
| 405 | setOperationAction(ISD::FROUND, MVT::v2f64, Legal); |
| 406 | } |
| 407 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 408 | // We have fused multiply-addition for f32 and f64 but not f128. |
| 409 | setOperationAction(ISD::FMA, MVT::f32, Legal); |
| 410 | setOperationAction(ISD::FMA, MVT::f64, Legal); |
| 411 | setOperationAction(ISD::FMA, MVT::f128, Expand); |
| 412 | |
| 413 | // Needed so that we don't try to implement f128 constant loads using |
| 414 | // a load-and-extend of a f80 constant (in cases where the constant |
| 415 | // would fit in an f80). |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 416 | for (MVT VT : MVT::fp_valuetypes()) |
| 417 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 418 | |
| 419 | // Floating-point truncation and stores need to be done separately. |
| 420 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 421 | setTruncStoreAction(MVT::f128, MVT::f32, Expand); |
| 422 | setTruncStoreAction(MVT::f128, MVT::f64, Expand); |
| 423 | |
| 424 | // We have 64-bit FPR<->GPR moves, but need special handling for |
| 425 | // 32-bit forms. |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 426 | if (!Subtarget.hasVector()) { |
| 427 | setOperationAction(ISD::BITCAST, MVT::i32, Custom); |
| 428 | setOperationAction(ISD::BITCAST, MVT::f32, Custom); |
| 429 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 430 | |
| 431 | // VASTART and VACOPY need to deal with the SystemZ-specific varargs |
| 432 | // structure, but VAEND is a no-op. |
| 433 | setOperationAction(ISD::VASTART, MVT::Other, Custom); |
| 434 | setOperationAction(ISD::VACOPY, MVT::Other, Custom); |
| 435 | setOperationAction(ISD::VAEND, MVT::Other, Expand); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 436 | |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 437 | // Codes for which we want to perform some z-specific combinations. |
| 438 | setTargetDAGCombine(ISD::SIGN_EXTEND); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 439 | setTargetDAGCombine(ISD::STORE); |
| 440 | setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 441 | setTargetDAGCombine(ISD::FP_ROUND); |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 442 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 443 | // Handle intrinsics. |
| 444 | setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 445 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 446 | |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 447 | // We want to use MVC in preference to even a single load/store pair. |
| 448 | MaxStoresPerMemcpy = 0; |
| 449 | MaxStoresPerMemcpyOptSize = 0; |
Richard Sandiford | 47660c1 | 2013-07-09 09:32:42 +0000 | [diff] [blame] | 450 | |
| 451 | // The main memset sequence is a byte store followed by an MVC. |
| 452 | // Two STC or MV..I stores win over that, but the kind of fused stores |
| 453 | // generated by target-independent code don't when the byte value is |
| 454 | // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better |
| 455 | // than "STC;MVC". Handle the choice in target-specific code instead. |
| 456 | MaxStoresPerMemset = 0; |
| 457 | MaxStoresPerMemsetOptSize = 0; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 460 | EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL, |
| 461 | LLVMContext &, EVT VT) const { |
Richard Sandiford | abc010b | 2013-11-06 12:16:02 +0000 | [diff] [blame] | 462 | if (!VT.isVector()) |
| 463 | return MVT::i32; |
| 464 | return VT.changeVectorElementTypeToInteger(); |
| 465 | } |
| 466 | |
| 467 | bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { |
Stephen Lin | 73de7bf | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 468 | VT = VT.getScalarType(); |
| 469 | |
| 470 | if (!VT.isSimple()) |
| 471 | return false; |
| 472 | |
| 473 | switch (VT.getSimpleVT().SimpleTy) { |
| 474 | case MVT::f32: |
| 475 | case MVT::f64: |
| 476 | return true; |
| 477 | case MVT::f128: |
| 478 | return false; |
| 479 | default: |
| 480 | break; |
| 481 | } |
| 482 | |
| 483 | return false; |
| 484 | } |
| 485 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 486 | bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { |
| 487 | // We can load zero using LZ?R and negative zero using LZ?R;LC?BR. |
| 488 | return Imm.isZero() || Imm.isNegZero(); |
| 489 | } |
| 490 | |
Ulrich Weigand | 1f6666a | 2015-03-31 12:52:27 +0000 | [diff] [blame] | 491 | bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const { |
| 492 | // We can use CGFI or CLGFI. |
| 493 | return isInt<32>(Imm) || isUInt<32>(Imm); |
| 494 | } |
| 495 | |
| 496 | bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const { |
| 497 | // We can use ALGFI or SLGFI. |
| 498 | return isUInt<32>(Imm) || isUInt<32>(-Imm); |
| 499 | } |
| 500 | |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 501 | bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, |
| 502 | unsigned, |
| 503 | unsigned, |
| 504 | bool *Fast) const { |
Richard Sandiford | 46af5a2 | 2013-05-30 09:45:42 +0000 | [diff] [blame] | 505 | // Unaligned accesses should never be slower than the expanded version. |
| 506 | // We check specifically for aligned accesses in the few cases where |
| 507 | // they are required. |
| 508 | if (Fast) |
| 509 | *Fast = true; |
| 510 | return true; |
| 511 | } |
Matt Arsenault | bd7d80a | 2015-06-01 05:31:59 +0000 | [diff] [blame] | 512 | |
Mehdi Amini | 0cdec1e | 2015-07-09 02:09:40 +0000 | [diff] [blame] | 513 | bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL, |
| 514 | const AddrMode &AM, Type *Ty, |
Matt Arsenault | bd7d80a | 2015-06-01 05:31:59 +0000 | [diff] [blame] | 515 | unsigned AS) const { |
Richard Sandiford | 791bea4 | 2013-07-31 12:58:26 +0000 | [diff] [blame] | 516 | // Punt on globals for now, although they can be used in limited |
| 517 | // RELATIVE LONG cases. |
| 518 | if (AM.BaseGV) |
| 519 | return false; |
| 520 | |
| 521 | // Require a 20-bit signed offset. |
| 522 | if (!isInt<20>(AM.BaseOffs)) |
| 523 | return false; |
| 524 | |
| 525 | // Indexing is OK but no scale factor can be applied. |
| 526 | return AM.Scale == 0 || AM.Scale == 1; |
| 527 | } |
| 528 | |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 529 | bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const { |
| 530 | if (!FromType->isIntegerTy() || !ToType->isIntegerTy()) |
| 531 | return false; |
| 532 | unsigned FromBits = FromType->getPrimitiveSizeInBits(); |
| 533 | unsigned ToBits = ToType->getPrimitiveSizeInBits(); |
| 534 | return FromBits > ToBits; |
| 535 | } |
| 536 | |
| 537 | bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const { |
| 538 | if (!FromVT.isInteger() || !ToVT.isInteger()) |
| 539 | return false; |
| 540 | unsigned FromBits = FromVT.getSizeInBits(); |
| 541 | unsigned ToBits = ToVT.getSizeInBits(); |
| 542 | return FromBits > ToBits; |
| 543 | } |
| 544 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 545 | //===----------------------------------------------------------------------===// |
| 546 | // Inline asm support |
| 547 | //===----------------------------------------------------------------------===// |
| 548 | |
| 549 | TargetLowering::ConstraintType |
Benjamin Kramer | 9bfb627 | 2015-07-05 19:29:18 +0000 | [diff] [blame] | 550 | SystemZTargetLowering::getConstraintType(StringRef Constraint) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 551 | if (Constraint.size() == 1) { |
| 552 | switch (Constraint[0]) { |
| 553 | case 'a': // Address register |
| 554 | case 'd': // Data register (equivalent to 'r') |
| 555 | case 'f': // Floating-point register |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 556 | case 'h': // High-part register |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 557 | case 'r': // General-purpose register |
| 558 | return C_RegisterClass; |
| 559 | |
| 560 | case 'Q': // Memory with base and unsigned 12-bit displacement |
| 561 | case 'R': // Likewise, plus an index |
| 562 | case 'S': // Memory with base and signed 20-bit displacement |
| 563 | case 'T': // Likewise, plus an index |
| 564 | case 'm': // Equivalent to 'T'. |
| 565 | return C_Memory; |
| 566 | |
| 567 | case 'I': // Unsigned 8-bit constant |
| 568 | case 'J': // Unsigned 12-bit constant |
| 569 | case 'K': // Signed 16-bit constant |
| 570 | case 'L': // Signed 20-bit displacement (on all targets we support) |
| 571 | case 'M': // 0x7fffffff |
| 572 | return C_Other; |
| 573 | |
| 574 | default: |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | return TargetLowering::getConstraintType(Constraint); |
| 579 | } |
| 580 | |
| 581 | TargetLowering::ConstraintWeight SystemZTargetLowering:: |
| 582 | getSingleConstraintMatchWeight(AsmOperandInfo &info, |
| 583 | const char *constraint) const { |
| 584 | ConstraintWeight weight = CW_Invalid; |
| 585 | Value *CallOperandVal = info.CallOperandVal; |
| 586 | // If we don't have a value, we can't do a match, |
| 587 | // but allow it at the lowest weight. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 588 | if (!CallOperandVal) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 589 | return CW_Default; |
| 590 | Type *type = CallOperandVal->getType(); |
| 591 | // Look at the constraint type. |
| 592 | switch (*constraint) { |
| 593 | default: |
| 594 | weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); |
| 595 | break; |
| 596 | |
| 597 | case 'a': // Address register |
| 598 | case 'd': // Data register (equivalent to 'r') |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 599 | case 'h': // High-part register |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 600 | case 'r': // General-purpose register |
| 601 | if (CallOperandVal->getType()->isIntegerTy()) |
| 602 | weight = CW_Register; |
| 603 | break; |
| 604 | |
| 605 | case 'f': // Floating-point register |
| 606 | if (type->isFloatingPointTy()) |
| 607 | weight = CW_Register; |
| 608 | break; |
| 609 | |
| 610 | case 'I': // Unsigned 8-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 611 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 612 | if (isUInt<8>(C->getZExtValue())) |
| 613 | weight = CW_Constant; |
| 614 | break; |
| 615 | |
| 616 | case 'J': // Unsigned 12-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 617 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 618 | if (isUInt<12>(C->getZExtValue())) |
| 619 | weight = CW_Constant; |
| 620 | break; |
| 621 | |
| 622 | case 'K': // Signed 16-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 623 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 624 | if (isInt<16>(C->getSExtValue())) |
| 625 | weight = CW_Constant; |
| 626 | break; |
| 627 | |
| 628 | case 'L': // Signed 20-bit displacement (on all targets we support) |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 629 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 630 | if (isInt<20>(C->getSExtValue())) |
| 631 | weight = CW_Constant; |
| 632 | break; |
| 633 | |
| 634 | case 'M': // 0x7fffffff |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 635 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 636 | if (C->getZExtValue() == 0x7fffffff) |
| 637 | weight = CW_Constant; |
| 638 | break; |
| 639 | } |
| 640 | return weight; |
| 641 | } |
| 642 | |
Richard Sandiford | b820405 | 2013-07-12 09:08:12 +0000 | [diff] [blame] | 643 | // Parse a "{tNNN}" register constraint for which the register type "t" |
| 644 | // has already been verified. MC is the class associated with "t" and |
| 645 | // Map maps 0-based register numbers to LLVM register numbers. |
| 646 | static std::pair<unsigned, const TargetRegisterClass *> |
Benjamin Kramer | 9bfb627 | 2015-07-05 19:29:18 +0000 | [diff] [blame] | 647 | parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC, |
| 648 | const unsigned *Map) { |
Richard Sandiford | b820405 | 2013-07-12 09:08:12 +0000 | [diff] [blame] | 649 | assert(*(Constraint.end()-1) == '}' && "Missing '}'"); |
| 650 | if (isdigit(Constraint[2])) { |
Benjamin Kramer | 9bfb627 | 2015-07-05 19:29:18 +0000 | [diff] [blame] | 651 | unsigned Index; |
| 652 | bool Failed = |
| 653 | Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index); |
| 654 | if (!Failed && Index < 16 && Map[Index]) |
Richard Sandiford | b820405 | 2013-07-12 09:08:12 +0000 | [diff] [blame] | 655 | return std::make_pair(Map[Index], RC); |
| 656 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 657 | return std::make_pair(0U, nullptr); |
Richard Sandiford | b820405 | 2013-07-12 09:08:12 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 660 | std::pair<unsigned, const TargetRegisterClass *> |
| 661 | SystemZTargetLowering::getRegForInlineAsmConstraint( |
Benjamin Kramer | 9bfb627 | 2015-07-05 19:29:18 +0000 | [diff] [blame] | 662 | const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 663 | if (Constraint.size() == 1) { |
| 664 | // GCC Constraint Letters |
| 665 | switch (Constraint[0]) { |
| 666 | default: break; |
| 667 | case 'd': // Data register (equivalent to 'r') |
| 668 | case 'r': // General-purpose register |
| 669 | if (VT == MVT::i64) |
| 670 | return std::make_pair(0U, &SystemZ::GR64BitRegClass); |
| 671 | else if (VT == MVT::i128) |
| 672 | return std::make_pair(0U, &SystemZ::GR128BitRegClass); |
| 673 | return std::make_pair(0U, &SystemZ::GR32BitRegClass); |
| 674 | |
| 675 | case 'a': // Address register |
| 676 | if (VT == MVT::i64) |
| 677 | return std::make_pair(0U, &SystemZ::ADDR64BitRegClass); |
| 678 | else if (VT == MVT::i128) |
| 679 | return std::make_pair(0U, &SystemZ::ADDR128BitRegClass); |
| 680 | return std::make_pair(0U, &SystemZ::ADDR32BitRegClass); |
| 681 | |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 682 | case 'h': // High-part register (an LLVM extension) |
| 683 | return std::make_pair(0U, &SystemZ::GRH32BitRegClass); |
| 684 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 685 | case 'f': // Floating-point register |
| 686 | if (VT == MVT::f64) |
| 687 | return std::make_pair(0U, &SystemZ::FP64BitRegClass); |
| 688 | else if (VT == MVT::f128) |
| 689 | return std::make_pair(0U, &SystemZ::FP128BitRegClass); |
| 690 | return std::make_pair(0U, &SystemZ::FP32BitRegClass); |
| 691 | } |
| 692 | } |
Benjamin Kramer | 9bfb627 | 2015-07-05 19:29:18 +0000 | [diff] [blame] | 693 | if (Constraint.size() > 0 && Constraint[0] == '{') { |
Richard Sandiford | b820405 | 2013-07-12 09:08:12 +0000 | [diff] [blame] | 694 | // We need to override the default register parsing for GPRs and FPRs |
| 695 | // because the interpretation depends on VT. The internal names of |
| 696 | // the registers are also different from the external names |
| 697 | // (F0D and F0S instead of F0, etc.). |
| 698 | if (Constraint[1] == 'r') { |
| 699 | if (VT == MVT::i32) |
| 700 | return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass, |
| 701 | SystemZMC::GR32Regs); |
| 702 | if (VT == MVT::i128) |
| 703 | return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass, |
| 704 | SystemZMC::GR128Regs); |
| 705 | return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass, |
| 706 | SystemZMC::GR64Regs); |
| 707 | } |
| 708 | if (Constraint[1] == 'f') { |
| 709 | if (VT == MVT::f32) |
| 710 | return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass, |
| 711 | SystemZMC::FP32Regs); |
| 712 | if (VT == MVT::f128) |
| 713 | return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass, |
| 714 | SystemZMC::FP128Regs); |
| 715 | return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass, |
| 716 | SystemZMC::FP64Regs); |
| 717 | } |
| 718 | } |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 719 | return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | void SystemZTargetLowering:: |
| 723 | LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint, |
| 724 | std::vector<SDValue> &Ops, |
| 725 | SelectionDAG &DAG) const { |
| 726 | // Only support length 1 constraints for now. |
| 727 | if (Constraint.length() == 1) { |
| 728 | switch (Constraint[0]) { |
| 729 | case 'I': // Unsigned 8-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 730 | if (auto *C = dyn_cast<ConstantSDNode>(Op)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 731 | if (isUInt<8>(C->getZExtValue())) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 732 | Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 733 | Op.getValueType())); |
| 734 | return; |
| 735 | |
| 736 | case 'J': // Unsigned 12-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 737 | if (auto *C = dyn_cast<ConstantSDNode>(Op)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 738 | if (isUInt<12>(C->getZExtValue())) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 739 | Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 740 | Op.getValueType())); |
| 741 | return; |
| 742 | |
| 743 | case 'K': // Signed 16-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 744 | if (auto *C = dyn_cast<ConstantSDNode>(Op)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 745 | if (isInt<16>(C->getSExtValue())) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 746 | Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 747 | Op.getValueType())); |
| 748 | return; |
| 749 | |
| 750 | case 'L': // Signed 20-bit displacement (on all targets we support) |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 751 | if (auto *C = dyn_cast<ConstantSDNode>(Op)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 752 | if (isInt<20>(C->getSExtValue())) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 753 | Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 754 | Op.getValueType())); |
| 755 | return; |
| 756 | |
| 757 | case 'M': // 0x7fffffff |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 758 | if (auto *C = dyn_cast<ConstantSDNode>(Op)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 759 | if (C->getZExtValue() == 0x7fffffff) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 760 | Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 761 | Op.getValueType())); |
| 762 | return; |
| 763 | } |
| 764 | } |
| 765 | TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); |
| 766 | } |
| 767 | |
| 768 | //===----------------------------------------------------------------------===// |
| 769 | // Calling conventions |
| 770 | //===----------------------------------------------------------------------===// |
| 771 | |
| 772 | #include "SystemZGenCallingConv.inc" |
| 773 | |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 774 | bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType, |
| 775 | Type *ToType) const { |
| 776 | return isTruncateFree(FromType, ToType); |
| 777 | } |
| 778 | |
| 779 | bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { |
| 780 | if (!CI->isTailCall()) |
| 781 | return false; |
| 782 | return true; |
| 783 | } |
| 784 | |
Ulrich Weigand | 5211f9f | 2015-05-05 19:30:05 +0000 | [diff] [blame] | 785 | // We do not yet support 128-bit single-element vector types. If the user |
| 786 | // attempts to use such types as function argument or return type, prefer |
| 787 | // to error out instead of emitting code violating the ABI. |
| 788 | static void VerifyVectorType(MVT VT, EVT ArgVT) { |
| 789 | if (ArgVT.isVector() && !VT.isVector()) |
| 790 | report_fatal_error("Unsupported vector argument or return type"); |
| 791 | } |
| 792 | |
| 793 | static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) { |
| 794 | for (unsigned i = 0; i < Ins.size(); ++i) |
| 795 | VerifyVectorType(Ins[i].VT, Ins[i].ArgVT); |
| 796 | } |
| 797 | |
| 798 | static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) { |
| 799 | for (unsigned i = 0; i < Outs.size(); ++i) |
| 800 | VerifyVectorType(Outs[i].VT, Outs[i].ArgVT); |
| 801 | } |
| 802 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 803 | // Value is a value that has been passed to us in the location described by VA |
| 804 | // (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining |
| 805 | // any loads onto Chain. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 806 | static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 807 | CCValAssign &VA, SDValue Chain, |
| 808 | SDValue Value) { |
| 809 | // If the argument has been promoted from a smaller type, insert an |
| 810 | // assertion to capture this. |
| 811 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 812 | Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value, |
| 813 | DAG.getValueType(VA.getValVT())); |
| 814 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 815 | Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value, |
| 816 | DAG.getValueType(VA.getValVT())); |
| 817 | |
| 818 | if (VA.isExtInLoc()) |
| 819 | Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value); |
| 820 | else if (VA.getLocInfo() == CCValAssign::Indirect) |
| 821 | Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value, |
| 822 | MachinePointerInfo(), false, false, false, 0); |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 823 | else if (VA.getLocInfo() == CCValAssign::BCvt) { |
| 824 | // If this is a short vector argument loaded from the stack, |
| 825 | // extend from i64 to full vector size and then bitcast. |
| 826 | assert(VA.getLocVT() == MVT::i64); |
| 827 | assert(VA.getValVT().isVector()); |
| 828 | Value = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i64, |
| 829 | Value, DAG.getUNDEF(MVT::i64)); |
| 830 | Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value); |
| 831 | } else |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 832 | assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo"); |
| 833 | return Value; |
| 834 | } |
| 835 | |
| 836 | // Value is a value of type VA.getValVT() that we need to copy into |
| 837 | // the location described by VA. Return a copy of Value converted to |
| 838 | // VA.getValVT(). The caller is responsible for handling indirect values. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 839 | static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 840 | CCValAssign &VA, SDValue Value) { |
| 841 | switch (VA.getLocInfo()) { |
| 842 | case CCValAssign::SExt: |
| 843 | return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value); |
| 844 | case CCValAssign::ZExt: |
| 845 | return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value); |
| 846 | case CCValAssign::AExt: |
| 847 | return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value); |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 848 | case CCValAssign::BCvt: |
| 849 | // If this is a short vector argument to be stored to the stack, |
| 850 | // bitcast to v2i64 and then extract first element. |
| 851 | assert(VA.getLocVT() == MVT::i64); |
| 852 | assert(VA.getValVT().isVector()); |
| 853 | Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value); |
| 854 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value, |
| 855 | DAG.getConstant(0, DL, MVT::i32)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 856 | case CCValAssign::Full: |
| 857 | return Value; |
| 858 | default: |
| 859 | llvm_unreachable("Unhandled getLocInfo()"); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | SDValue SystemZTargetLowering:: |
| 864 | LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, |
| 865 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 866 | SDLoc DL, SelectionDAG &DAG, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 867 | SmallVectorImpl<SDValue> &InVals) const { |
| 868 | MachineFunction &MF = DAG.getMachineFunction(); |
| 869 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 870 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 871 | SystemZMachineFunctionInfo *FuncInfo = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 872 | MF.getInfo<SystemZMachineFunctionInfo>(); |
| 873 | auto *TFL = |
| 874 | static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 875 | |
Ulrich Weigand | 5211f9f | 2015-05-05 19:30:05 +0000 | [diff] [blame] | 876 | // Detect unsupported vector argument types. |
| 877 | if (Subtarget.hasVector()) |
| 878 | VerifyVectorTypes(Ins); |
| 879 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 880 | // Assign locations to all of the incoming arguments. |
| 881 | SmallVector<CCValAssign, 16> ArgLocs; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 882 | SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 883 | CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ); |
| 884 | |
| 885 | unsigned NumFixedGPRs = 0; |
| 886 | unsigned NumFixedFPRs = 0; |
| 887 | for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { |
| 888 | SDValue ArgValue; |
| 889 | CCValAssign &VA = ArgLocs[I]; |
| 890 | EVT LocVT = VA.getLocVT(); |
| 891 | if (VA.isRegLoc()) { |
| 892 | // Arguments passed in registers |
| 893 | const TargetRegisterClass *RC; |
| 894 | switch (LocVT.getSimpleVT().SimpleTy) { |
| 895 | default: |
| 896 | // Integers smaller than i64 should be promoted to i64. |
| 897 | llvm_unreachable("Unexpected argument type"); |
| 898 | case MVT::i32: |
| 899 | NumFixedGPRs += 1; |
| 900 | RC = &SystemZ::GR32BitRegClass; |
| 901 | break; |
| 902 | case MVT::i64: |
| 903 | NumFixedGPRs += 1; |
| 904 | RC = &SystemZ::GR64BitRegClass; |
| 905 | break; |
| 906 | case MVT::f32: |
| 907 | NumFixedFPRs += 1; |
| 908 | RC = &SystemZ::FP32BitRegClass; |
| 909 | break; |
| 910 | case MVT::f64: |
| 911 | NumFixedFPRs += 1; |
| 912 | RC = &SystemZ::FP64BitRegClass; |
| 913 | break; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 914 | case MVT::v16i8: |
| 915 | case MVT::v8i16: |
| 916 | case MVT::v4i32: |
| 917 | case MVT::v2i64: |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 918 | case MVT::v4f32: |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 919 | case MVT::v2f64: |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 920 | RC = &SystemZ::VR128BitRegClass; |
| 921 | break; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | unsigned VReg = MRI.createVirtualRegister(RC); |
| 925 | MRI.addLiveIn(VA.getLocReg(), VReg); |
| 926 | ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT); |
| 927 | } else { |
| 928 | assert(VA.isMemLoc() && "Argument not register or memory"); |
| 929 | |
| 930 | // Create the frame index object for this incoming parameter. |
| 931 | int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8, |
| 932 | VA.getLocMemOffset(), true); |
| 933 | |
| 934 | // Create the SelectionDAG nodes corresponding to a load |
| 935 | // from this parameter. Unpromoted ints and floats are |
| 936 | // passed as right-justified 8-byte values. |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 937 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 938 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 939 | if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 940 | FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, |
| 941 | DAG.getIntPtrConstant(4, DL)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 942 | ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN, |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 943 | MachinePointerInfo::getFixedStack(MF, FI), false, |
| 944 | false, false, 0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | // Convert the value of the argument register into the value that's |
| 948 | // being passed. |
| 949 | InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue)); |
| 950 | } |
| 951 | |
| 952 | if (IsVarArg) { |
| 953 | // Save the number of non-varargs registers for later use by va_start, etc. |
| 954 | FuncInfo->setVarArgsFirstGPR(NumFixedGPRs); |
| 955 | FuncInfo->setVarArgsFirstFPR(NumFixedFPRs); |
| 956 | |
| 957 | // Likewise the address (in the form of a frame index) of where the |
| 958 | // first stack vararg would be. The 1-byte size here is arbitrary. |
| 959 | int64_t StackSize = CCInfo.getNextStackOffset(); |
| 960 | FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true)); |
| 961 | |
| 962 | // ...and a similar frame index for the caller-allocated save area |
| 963 | // that will be used to store the incoming registers. |
| 964 | int64_t RegSaveOffset = TFL->getOffsetOfLocalArea(); |
| 965 | unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true); |
| 966 | FuncInfo->setRegSaveFrameIndex(RegSaveIndex); |
| 967 | |
| 968 | // Store the FPR varargs in the reserved frame slots. (We store the |
| 969 | // GPRs as part of the prologue.) |
| 970 | if (NumFixedFPRs < SystemZ::NumArgFPRs) { |
| 971 | SDValue MemOps[SystemZ::NumArgFPRs]; |
| 972 | for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) { |
| 973 | unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]); |
| 974 | int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 975 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 976 | unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I], |
| 977 | &SystemZ::FP64BitRegClass); |
| 978 | SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64); |
| 979 | MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN, |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 980 | MachinePointerInfo::getFixedStack(MF, FI), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 981 | false, false, 0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 982 | } |
| 983 | // Join the stores, which are independent of one another. |
| 984 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, |
Craig Topper | 2d2aa0c | 2014-04-30 07:17:30 +0000 | [diff] [blame] | 985 | makeArrayRef(&MemOps[NumFixedFPRs], |
| 986 | SystemZ::NumArgFPRs-NumFixedFPRs)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 987 | } |
| 988 | } |
| 989 | |
| 990 | return Chain; |
| 991 | } |
| 992 | |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 993 | static bool canUseSiblingCall(const CCState &ArgCCInfo, |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 994 | SmallVectorImpl<CCValAssign> &ArgLocs) { |
| 995 | // Punt if there are any indirect or stack arguments, or if the call |
| 996 | // needs the call-saved argument register R6. |
| 997 | for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { |
| 998 | CCValAssign &VA = ArgLocs[I]; |
| 999 | if (VA.getLocInfo() == CCValAssign::Indirect) |
| 1000 | return false; |
| 1001 | if (!VA.isRegLoc()) |
| 1002 | return false; |
| 1003 | unsigned Reg = VA.getLocReg(); |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 1004 | if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D) |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1005 | return false; |
| 1006 | } |
| 1007 | return true; |
| 1008 | } |
| 1009 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1010 | SDValue |
| 1011 | SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI, |
| 1012 | SmallVectorImpl<SDValue> &InVals) const { |
| 1013 | SelectionDAG &DAG = CLI.DAG; |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1014 | SDLoc &DL = CLI.DL; |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 1015 | SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; |
| 1016 | SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; |
| 1017 | SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1018 | SDValue Chain = CLI.Chain; |
| 1019 | SDValue Callee = CLI.Callee; |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1020 | bool &IsTailCall = CLI.IsTailCall; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1021 | CallingConv::ID CallConv = CLI.CallConv; |
| 1022 | bool IsVarArg = CLI.IsVarArg; |
| 1023 | MachineFunction &MF = DAG.getMachineFunction(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1024 | EVT PtrVT = getPointerTy(MF.getDataLayout()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1025 | |
Ulrich Weigand | 5211f9f | 2015-05-05 19:30:05 +0000 | [diff] [blame] | 1026 | // Detect unsupported vector argument and return types. |
| 1027 | if (Subtarget.hasVector()) { |
| 1028 | VerifyVectorTypes(Outs); |
| 1029 | VerifyVectorTypes(Ins); |
| 1030 | } |
| 1031 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1032 | // Analyze the operands of the call, assigning locations to each operand. |
| 1033 | SmallVector<CCValAssign, 16> ArgLocs; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1034 | SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1035 | ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ); |
| 1036 | |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1037 | // We don't support GuaranteedTailCallOpt, only automatically-detected |
| 1038 | // sibling calls. |
| 1039 | if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs)) |
| 1040 | IsTailCall = false; |
| 1041 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1042 | // Get a count of how many bytes are to be pushed on the stack. |
| 1043 | unsigned NumBytes = ArgCCInfo.getNextStackOffset(); |
| 1044 | |
| 1045 | // Mark the start of the call. |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1046 | if (!IsTailCall) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1047 | Chain = DAG.getCALLSEQ_START(Chain, |
| 1048 | DAG.getConstant(NumBytes, DL, PtrVT, true), |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1049 | DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1050 | |
| 1051 | // Copy argument values to their designated locations. |
| 1052 | SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass; |
| 1053 | SmallVector<SDValue, 8> MemOpChains; |
| 1054 | SDValue StackPtr; |
| 1055 | for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { |
| 1056 | CCValAssign &VA = ArgLocs[I]; |
| 1057 | SDValue ArgValue = OutVals[I]; |
| 1058 | |
| 1059 | if (VA.getLocInfo() == CCValAssign::Indirect) { |
| 1060 | // Store the argument in a stack slot and pass its address. |
| 1061 | SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT()); |
| 1062 | int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 1063 | MemOpChains.push_back(DAG.getStore( |
| 1064 | Chain, DL, ArgValue, SpillSlot, |
| 1065 | MachinePointerInfo::getFixedStack(MF, FI), false, false, 0)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1066 | ArgValue = SpillSlot; |
| 1067 | } else |
| 1068 | ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue); |
| 1069 | |
| 1070 | if (VA.isRegLoc()) |
| 1071 | // Queue up the argument copies and emit them at the end. |
| 1072 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue)); |
| 1073 | else { |
| 1074 | assert(VA.isMemLoc() && "Argument not register or memory"); |
| 1075 | |
| 1076 | // Work out the address of the stack slot. Unpromoted ints and |
| 1077 | // floats are passed as right-justified 8-byte values. |
| 1078 | if (!StackPtr.getNode()) |
| 1079 | StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT); |
| 1080 | unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset(); |
| 1081 | if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32) |
| 1082 | Offset += 4; |
| 1083 | SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1084 | DAG.getIntPtrConstant(Offset, DL)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1085 | |
| 1086 | // Emit the store. |
| 1087 | MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address, |
| 1088 | MachinePointerInfo(), |
| 1089 | false, false, 0)); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | // Join the stores, which are independent of one another. |
| 1094 | if (!MemOpChains.empty()) |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 1095 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1096 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1097 | // Accept direct calls by converting symbolic call addresses to the |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1098 | // associated Target* opcodes. Force %r1 to be used for indirect |
| 1099 | // tail calls. |
| 1100 | SDValue Glue; |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1101 | if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1102 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT); |
| 1103 | Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee); |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1104 | } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1105 | Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT); |
| 1106 | Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee); |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1107 | } else if (IsTailCall) { |
| 1108 | Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue); |
| 1109 | Glue = Chain.getValue(1); |
| 1110 | Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType()); |
| 1111 | } |
| 1112 | |
| 1113 | // Build a sequence of copy-to-reg nodes, chained and glued together. |
| 1114 | for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) { |
| 1115 | Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first, |
| 1116 | RegsToPass[I].second, Glue); |
| 1117 | Glue = Chain.getValue(1); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | // The first call operand is the chain and the second is the target address. |
| 1121 | SmallVector<SDValue, 8> Ops; |
| 1122 | Ops.push_back(Chain); |
| 1123 | Ops.push_back(Callee); |
| 1124 | |
| 1125 | // Add argument registers to the end of the list so that they are |
| 1126 | // known live into the call. |
| 1127 | for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) |
| 1128 | Ops.push_back(DAG.getRegister(RegsToPass[I].first, |
| 1129 | RegsToPass[I].second.getValueType())); |
| 1130 | |
Richard Sandiford | 02bb0ec | 2014-07-10 11:44:37 +0000 | [diff] [blame] | 1131 | // Add a register mask operand representing the call-preserved registers. |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 1132 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
Eric Christopher | 9deb75d | 2015-03-11 22:42:13 +0000 | [diff] [blame] | 1133 | const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv); |
Richard Sandiford | 02bb0ec | 2014-07-10 11:44:37 +0000 | [diff] [blame] | 1134 | assert(Mask && "Missing call preserved mask for calling convention"); |
| 1135 | Ops.push_back(DAG.getRegisterMask(Mask)); |
| 1136 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1137 | // Glue the call to the argument copies, if any. |
| 1138 | if (Glue.getNode()) |
| 1139 | Ops.push_back(Glue); |
| 1140 | |
| 1141 | // Emit the call. |
| 1142 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1143 | if (IsTailCall) |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 1144 | return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops); |
| 1145 | Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1146 | Glue = Chain.getValue(1); |
| 1147 | |
| 1148 | // Mark the end of the call, which is glued to the call itself. |
| 1149 | Chain = DAG.getCALLSEQ_END(Chain, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1150 | DAG.getConstant(NumBytes, DL, PtrVT, true), |
| 1151 | DAG.getConstant(0, DL, PtrVT, true), |
Andrew Trick | ad6d08a | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 1152 | Glue, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1153 | Glue = Chain.getValue(1); |
| 1154 | |
| 1155 | // Assign locations to each value returned by this call. |
| 1156 | SmallVector<CCValAssign, 16> RetLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 1157 | CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1158 | RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ); |
| 1159 | |
| 1160 | // Copy all of the result registers out of their specified physreg. |
| 1161 | for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) { |
| 1162 | CCValAssign &VA = RetLocs[I]; |
| 1163 | |
| 1164 | // Copy the value out, gluing the copy to the end of the call sequence. |
| 1165 | SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), |
| 1166 | VA.getLocVT(), Glue); |
| 1167 | Chain = RetValue.getValue(1); |
| 1168 | Glue = RetValue.getValue(2); |
| 1169 | |
| 1170 | // Convert the value of the return register into the value that's |
| 1171 | // being returned. |
| 1172 | InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue)); |
| 1173 | } |
| 1174 | |
| 1175 | return Chain; |
| 1176 | } |
| 1177 | |
Ulrich Weigand | a887f06 | 2015-08-13 13:37:06 +0000 | [diff] [blame] | 1178 | bool SystemZTargetLowering:: |
| 1179 | CanLowerReturn(CallingConv::ID CallConv, |
| 1180 | MachineFunction &MF, bool isVarArg, |
| 1181 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 1182 | LLVMContext &Context) const { |
| 1183 | // Detect unsupported vector return types. |
| 1184 | if (Subtarget.hasVector()) |
| 1185 | VerifyVectorTypes(Outs); |
| 1186 | |
| 1187 | SmallVector<CCValAssign, 16> RetLocs; |
| 1188 | CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context); |
| 1189 | return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ); |
| 1190 | } |
| 1191 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1192 | SDValue |
| 1193 | SystemZTargetLowering::LowerReturn(SDValue Chain, |
| 1194 | CallingConv::ID CallConv, bool IsVarArg, |
| 1195 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 1196 | const SmallVectorImpl<SDValue> &OutVals, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1197 | SDLoc DL, SelectionDAG &DAG) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1198 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1199 | |
Ulrich Weigand | 5211f9f | 2015-05-05 19:30:05 +0000 | [diff] [blame] | 1200 | // Detect unsupported vector return types. |
| 1201 | if (Subtarget.hasVector()) |
| 1202 | VerifyVectorTypes(Outs); |
| 1203 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1204 | // Assign locations to each returned value. |
| 1205 | SmallVector<CCValAssign, 16> RetLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 1206 | CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1207 | RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ); |
| 1208 | |
| 1209 | // Quick exit for void returns |
| 1210 | if (RetLocs.empty()) |
| 1211 | return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain); |
| 1212 | |
| 1213 | // Copy the result values into the output registers. |
| 1214 | SDValue Glue; |
| 1215 | SmallVector<SDValue, 4> RetOps; |
| 1216 | RetOps.push_back(Chain); |
| 1217 | for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) { |
| 1218 | CCValAssign &VA = RetLocs[I]; |
| 1219 | SDValue RetValue = OutVals[I]; |
| 1220 | |
| 1221 | // Make the return register live on exit. |
| 1222 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 1223 | |
| 1224 | // Promote the value as required. |
| 1225 | RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue); |
| 1226 | |
| 1227 | // Chain and glue the copies together. |
| 1228 | unsigned Reg = VA.getLocReg(); |
| 1229 | Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue); |
| 1230 | Glue = Chain.getValue(1); |
| 1231 | RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT())); |
| 1232 | } |
| 1233 | |
| 1234 | // Update chain and glue. |
| 1235 | RetOps[0] = Chain; |
| 1236 | if (Glue.getNode()) |
| 1237 | RetOps.push_back(Glue); |
| 1238 | |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 1239 | return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Richard Sandiford | 9afe613 | 2013-12-10 10:36:34 +0000 | [diff] [blame] | 1242 | SDValue SystemZTargetLowering:: |
| 1243 | prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const { |
| 1244 | return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain); |
| 1245 | } |
| 1246 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 1247 | // Return true if Op is an intrinsic node with chain that returns the CC value |
| 1248 | // as its only (other) argument. Provide the associated SystemZISD opcode and |
| 1249 | // the mask of valid CC values if so. |
| 1250 | static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode, |
| 1251 | unsigned &CCValid) { |
| 1252 | unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 1253 | switch (Id) { |
| 1254 | case Intrinsic::s390_tbegin: |
| 1255 | Opcode = SystemZISD::TBEGIN; |
| 1256 | CCValid = SystemZ::CCMASK_TBEGIN; |
| 1257 | return true; |
| 1258 | |
| 1259 | case Intrinsic::s390_tbegin_nofloat: |
| 1260 | Opcode = SystemZISD::TBEGIN_NOFLOAT; |
| 1261 | CCValid = SystemZ::CCMASK_TBEGIN; |
| 1262 | return true; |
| 1263 | |
| 1264 | case Intrinsic::s390_tend: |
| 1265 | Opcode = SystemZISD::TEND; |
| 1266 | CCValid = SystemZ::CCMASK_TEND; |
| 1267 | return true; |
| 1268 | |
| 1269 | default: |
| 1270 | return false; |
| 1271 | } |
| 1272 | } |
| 1273 | |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 1274 | // Return true if Op is an intrinsic node without chain that returns the |
| 1275 | // CC value as its final argument. Provide the associated SystemZISD |
| 1276 | // opcode and the mask of valid CC values if so. |
| 1277 | static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) { |
| 1278 | unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 1279 | switch (Id) { |
| 1280 | case Intrinsic::s390_vpkshs: |
| 1281 | case Intrinsic::s390_vpksfs: |
| 1282 | case Intrinsic::s390_vpksgs: |
| 1283 | Opcode = SystemZISD::PACKS_CC; |
| 1284 | CCValid = SystemZ::CCMASK_VCMP; |
| 1285 | return true; |
| 1286 | |
| 1287 | case Intrinsic::s390_vpklshs: |
| 1288 | case Intrinsic::s390_vpklsfs: |
| 1289 | case Intrinsic::s390_vpklsgs: |
| 1290 | Opcode = SystemZISD::PACKLS_CC; |
| 1291 | CCValid = SystemZ::CCMASK_VCMP; |
| 1292 | return true; |
| 1293 | |
| 1294 | case Intrinsic::s390_vceqbs: |
| 1295 | case Intrinsic::s390_vceqhs: |
| 1296 | case Intrinsic::s390_vceqfs: |
| 1297 | case Intrinsic::s390_vceqgs: |
| 1298 | Opcode = SystemZISD::VICMPES; |
| 1299 | CCValid = SystemZ::CCMASK_VCMP; |
| 1300 | return true; |
| 1301 | |
| 1302 | case Intrinsic::s390_vchbs: |
| 1303 | case Intrinsic::s390_vchhs: |
| 1304 | case Intrinsic::s390_vchfs: |
| 1305 | case Intrinsic::s390_vchgs: |
| 1306 | Opcode = SystemZISD::VICMPHS; |
| 1307 | CCValid = SystemZ::CCMASK_VCMP; |
| 1308 | return true; |
| 1309 | |
| 1310 | case Intrinsic::s390_vchlbs: |
| 1311 | case Intrinsic::s390_vchlhs: |
| 1312 | case Intrinsic::s390_vchlfs: |
| 1313 | case Intrinsic::s390_vchlgs: |
| 1314 | Opcode = SystemZISD::VICMPHLS; |
| 1315 | CCValid = SystemZ::CCMASK_VCMP; |
| 1316 | return true; |
| 1317 | |
| 1318 | case Intrinsic::s390_vtm: |
| 1319 | Opcode = SystemZISD::VTM; |
| 1320 | CCValid = SystemZ::CCMASK_VCMP; |
| 1321 | return true; |
| 1322 | |
| 1323 | case Intrinsic::s390_vfaebs: |
| 1324 | case Intrinsic::s390_vfaehs: |
| 1325 | case Intrinsic::s390_vfaefs: |
| 1326 | Opcode = SystemZISD::VFAE_CC; |
| 1327 | CCValid = SystemZ::CCMASK_ANY; |
| 1328 | return true; |
| 1329 | |
| 1330 | case Intrinsic::s390_vfaezbs: |
| 1331 | case Intrinsic::s390_vfaezhs: |
| 1332 | case Intrinsic::s390_vfaezfs: |
| 1333 | Opcode = SystemZISD::VFAEZ_CC; |
| 1334 | CCValid = SystemZ::CCMASK_ANY; |
| 1335 | return true; |
| 1336 | |
| 1337 | case Intrinsic::s390_vfeebs: |
| 1338 | case Intrinsic::s390_vfeehs: |
| 1339 | case Intrinsic::s390_vfeefs: |
| 1340 | Opcode = SystemZISD::VFEE_CC; |
| 1341 | CCValid = SystemZ::CCMASK_ANY; |
| 1342 | return true; |
| 1343 | |
| 1344 | case Intrinsic::s390_vfeezbs: |
| 1345 | case Intrinsic::s390_vfeezhs: |
| 1346 | case Intrinsic::s390_vfeezfs: |
| 1347 | Opcode = SystemZISD::VFEEZ_CC; |
| 1348 | CCValid = SystemZ::CCMASK_ANY; |
| 1349 | return true; |
| 1350 | |
| 1351 | case Intrinsic::s390_vfenebs: |
| 1352 | case Intrinsic::s390_vfenehs: |
| 1353 | case Intrinsic::s390_vfenefs: |
| 1354 | Opcode = SystemZISD::VFENE_CC; |
| 1355 | CCValid = SystemZ::CCMASK_ANY; |
| 1356 | return true; |
| 1357 | |
| 1358 | case Intrinsic::s390_vfenezbs: |
| 1359 | case Intrinsic::s390_vfenezhs: |
| 1360 | case Intrinsic::s390_vfenezfs: |
| 1361 | Opcode = SystemZISD::VFENEZ_CC; |
| 1362 | CCValid = SystemZ::CCMASK_ANY; |
| 1363 | return true; |
| 1364 | |
| 1365 | case Intrinsic::s390_vistrbs: |
| 1366 | case Intrinsic::s390_vistrhs: |
| 1367 | case Intrinsic::s390_vistrfs: |
| 1368 | Opcode = SystemZISD::VISTR_CC; |
| 1369 | CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3; |
| 1370 | return true; |
| 1371 | |
| 1372 | case Intrinsic::s390_vstrcbs: |
| 1373 | case Intrinsic::s390_vstrchs: |
| 1374 | case Intrinsic::s390_vstrcfs: |
| 1375 | Opcode = SystemZISD::VSTRC_CC; |
| 1376 | CCValid = SystemZ::CCMASK_ANY; |
| 1377 | return true; |
| 1378 | |
| 1379 | case Intrinsic::s390_vstrczbs: |
| 1380 | case Intrinsic::s390_vstrczhs: |
| 1381 | case Intrinsic::s390_vstrczfs: |
| 1382 | Opcode = SystemZISD::VSTRCZ_CC; |
| 1383 | CCValid = SystemZ::CCMASK_ANY; |
| 1384 | return true; |
| 1385 | |
| 1386 | case Intrinsic::s390_vfcedbs: |
| 1387 | Opcode = SystemZISD::VFCMPES; |
| 1388 | CCValid = SystemZ::CCMASK_VCMP; |
| 1389 | return true; |
| 1390 | |
| 1391 | case Intrinsic::s390_vfchdbs: |
| 1392 | Opcode = SystemZISD::VFCMPHS; |
| 1393 | CCValid = SystemZ::CCMASK_VCMP; |
| 1394 | return true; |
| 1395 | |
| 1396 | case Intrinsic::s390_vfchedbs: |
| 1397 | Opcode = SystemZISD::VFCMPHES; |
| 1398 | CCValid = SystemZ::CCMASK_VCMP; |
| 1399 | return true; |
| 1400 | |
| 1401 | case Intrinsic::s390_vftcidb: |
| 1402 | Opcode = SystemZISD::VFTCI; |
| 1403 | CCValid = SystemZ::CCMASK_VCMP; |
| 1404 | return true; |
| 1405 | |
| 1406 | default: |
| 1407 | return false; |
| 1408 | } |
| 1409 | } |
| 1410 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 1411 | // Emit an intrinsic with chain with a glued value instead of its CC result. |
| 1412 | static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op, |
| 1413 | unsigned Opcode) { |
| 1414 | // Copy all operands except the intrinsic ID. |
| 1415 | unsigned NumOps = Op.getNumOperands(); |
| 1416 | SmallVector<SDValue, 6> Ops; |
| 1417 | Ops.reserve(NumOps - 1); |
| 1418 | Ops.push_back(Op.getOperand(0)); |
| 1419 | for (unsigned I = 2; I < NumOps; ++I) |
| 1420 | Ops.push_back(Op.getOperand(I)); |
| 1421 | |
| 1422 | assert(Op->getNumValues() == 2 && "Expected only CC result and chain"); |
| 1423 | SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue); |
| 1424 | SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops); |
| 1425 | SDValue OldChain = SDValue(Op.getNode(), 1); |
| 1426 | SDValue NewChain = SDValue(Intr.getNode(), 0); |
| 1427 | DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain); |
| 1428 | return Intr; |
| 1429 | } |
| 1430 | |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 1431 | // Emit an intrinsic with a glued value instead of its CC result. |
| 1432 | static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op, |
| 1433 | unsigned Opcode) { |
| 1434 | // Copy all operands except the intrinsic ID. |
| 1435 | unsigned NumOps = Op.getNumOperands(); |
| 1436 | SmallVector<SDValue, 6> Ops; |
| 1437 | Ops.reserve(NumOps - 1); |
| 1438 | for (unsigned I = 1; I < NumOps; ++I) |
| 1439 | Ops.push_back(Op.getOperand(I)); |
| 1440 | |
| 1441 | if (Op->getNumValues() == 1) |
| 1442 | return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops); |
| 1443 | assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result"); |
| 1444 | SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue); |
| 1445 | return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops); |
| 1446 | } |
| 1447 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1448 | // CC is a comparison that will be implemented using an integer or |
| 1449 | // floating-point comparison. Return the condition code mask for |
| 1450 | // a branch on true. In the integer case, CCMASK_CMP_UO is set for |
| 1451 | // unsigned comparisons and clear for signed ones. In the floating-point |
| 1452 | // case, CCMASK_CMP_UO has its normal mask meaning (unordered). |
| 1453 | static unsigned CCMaskForCondCode(ISD::CondCode CC) { |
| 1454 | #define CONV(X) \ |
| 1455 | case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \ |
| 1456 | case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \ |
| 1457 | case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X |
| 1458 | |
| 1459 | switch (CC) { |
| 1460 | default: |
| 1461 | llvm_unreachable("Invalid integer condition!"); |
| 1462 | |
| 1463 | CONV(EQ); |
| 1464 | CONV(NE); |
| 1465 | CONV(GT); |
| 1466 | CONV(GE); |
| 1467 | CONV(LT); |
| 1468 | CONV(LE); |
| 1469 | |
| 1470 | case ISD::SETO: return SystemZ::CCMASK_CMP_O; |
| 1471 | case ISD::SETUO: return SystemZ::CCMASK_CMP_UO; |
| 1472 | } |
| 1473 | #undef CONV |
| 1474 | } |
| 1475 | |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 1476 | // Return a sequence for getting a 1 from an IPM result when CC has a |
| 1477 | // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask. |
| 1478 | // The handling of CC values outside CCValid doesn't matter. |
| 1479 | static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) { |
| 1480 | // Deal with cases where the result can be taken directly from a bit |
| 1481 | // of the IPM result. |
| 1482 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3))) |
| 1483 | return IPMConversion(0, 0, SystemZ::IPM_CC); |
| 1484 | if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3))) |
| 1485 | return IPMConversion(0, 0, SystemZ::IPM_CC + 1); |
| 1486 | |
| 1487 | // Deal with cases where we can add a value to force the sign bit |
| 1488 | // to contain the right value. Putting the bit in 31 means we can |
| 1489 | // use SRL rather than RISBG(L), and also makes it easier to get a |
| 1490 | // 0/-1 value, so it has priority over the other tests below. |
| 1491 | // |
| 1492 | // These sequences rely on the fact that the upper two bits of the |
| 1493 | // IPM result are zero. |
| 1494 | uint64_t TopBit = uint64_t(1) << 31; |
| 1495 | if (CCMask == (CCValid & SystemZ::CCMASK_0)) |
| 1496 | return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31); |
| 1497 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1))) |
| 1498 | return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31); |
| 1499 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1500 | | SystemZ::CCMASK_1 |
| 1501 | | SystemZ::CCMASK_2))) |
| 1502 | return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31); |
| 1503 | if (CCMask == (CCValid & SystemZ::CCMASK_3)) |
| 1504 | return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31); |
| 1505 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 |
| 1506 | | SystemZ::CCMASK_2 |
| 1507 | | SystemZ::CCMASK_3))) |
| 1508 | return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31); |
| 1509 | |
| 1510 | // Next try inverting the value and testing a bit. 0/1 could be |
| 1511 | // handled this way too, but we dealt with that case above. |
| 1512 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2))) |
| 1513 | return IPMConversion(-1, 0, SystemZ::IPM_CC); |
| 1514 | |
| 1515 | // Handle cases where adding a value forces a non-sign bit to contain |
| 1516 | // the right value. |
| 1517 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2))) |
| 1518 | return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1); |
| 1519 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3))) |
| 1520 | return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1); |
| 1521 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1522 | // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 1523 | // can be done by inverting the low CC bit and applying one of the |
| 1524 | // sign-based extractions above. |
| 1525 | if (CCMask == (CCValid & SystemZ::CCMASK_1)) |
| 1526 | return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31); |
| 1527 | if (CCMask == (CCValid & SystemZ::CCMASK_2)) |
| 1528 | return IPMConversion(1 << SystemZ::IPM_CC, |
| 1529 | TopBit - (3 << SystemZ::IPM_CC), 31); |
| 1530 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1531 | | SystemZ::CCMASK_1 |
| 1532 | | SystemZ::CCMASK_3))) |
| 1533 | return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31); |
| 1534 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1535 | | SystemZ::CCMASK_2 |
| 1536 | | SystemZ::CCMASK_3))) |
| 1537 | return IPMConversion(1 << SystemZ::IPM_CC, |
| 1538 | TopBit - (1 << SystemZ::IPM_CC), 31); |
| 1539 | |
| 1540 | llvm_unreachable("Unexpected CC combination"); |
| 1541 | } |
| 1542 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1543 | // If C can be converted to a comparison against zero, adjust the operands |
Richard Sandiford | a075708 | 2013-08-01 10:29:45 +0000 | [diff] [blame] | 1544 | // as necessary. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1545 | static void adjustZeroCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1546 | if (C.ICmpType == SystemZICMP::UnsignedOnly) |
Richard Sandiford | a075708 | 2013-08-01 10:29:45 +0000 | [diff] [blame] | 1547 | return; |
| 1548 | |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1549 | auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode()); |
Richard Sandiford | a075708 | 2013-08-01 10:29:45 +0000 | [diff] [blame] | 1550 | if (!ConstOp1) |
| 1551 | return; |
| 1552 | |
| 1553 | int64_t Value = ConstOp1->getSExtValue(); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1554 | if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) || |
| 1555 | (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) || |
| 1556 | (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) || |
| 1557 | (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) { |
| 1558 | C.CCMask ^= SystemZ::CCMASK_CMP_EQ; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1559 | C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType()); |
Richard Sandiford | a075708 | 2013-08-01 10:29:45 +0000 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1563 | // If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI, |
| 1564 | // adjust the operands as necessary. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1565 | static void adjustSubwordCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1566 | // For us to make any changes, it must a comparison between a single-use |
| 1567 | // load and a constant. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1568 | if (!C.Op0.hasOneUse() || |
| 1569 | C.Op0.getOpcode() != ISD::LOAD || |
| 1570 | C.Op1.getOpcode() != ISD::Constant) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | |
| 1573 | // We must have an 8- or 16-bit load. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1574 | auto *Load = cast<LoadSDNode>(C.Op0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1575 | unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits(); |
| 1576 | if (NumBits != 8 && NumBits != 16) |
| 1577 | return; |
| 1578 | |
| 1579 | // The load must be an extending one and the constant must be within the |
| 1580 | // range of the unextended value. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1581 | auto *ConstOp1 = cast<ConstantSDNode>(C.Op1); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1582 | uint64_t Value = ConstOp1->getZExtValue(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1583 | uint64_t Mask = (1 << NumBits) - 1; |
| 1584 | if (Load->getExtensionType() == ISD::SEXTLOAD) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1585 | // Make sure that ConstOp1 is in range of C.Op0. |
| 1586 | int64_t SignedValue = ConstOp1->getSExtValue(); |
| 1587 | if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1588 | return; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1589 | if (C.ICmpType != SystemZICMP::SignedOnly) { |
| 1590 | // Unsigned comparison between two sign-extended values is equivalent |
| 1591 | // to unsigned comparison between two zero-extended values. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1592 | Value &= Mask; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1593 | } else if (NumBits == 8) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1594 | // Try to treat the comparison as unsigned, so that we can use CLI. |
| 1595 | // Adjust CCMask and Value as necessary. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1596 | if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1597 | // Test whether the high bit of the byte is set. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1598 | Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT; |
| 1599 | else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1600 | // Test whether the high bit of the byte is clear. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1601 | Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1602 | else |
| 1603 | // No instruction exists for this combination. |
| 1604 | return; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1605 | C.ICmpType = SystemZICMP::UnsignedOnly; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1606 | } |
| 1607 | } else if (Load->getExtensionType() == ISD::ZEXTLOAD) { |
| 1608 | if (Value > Mask) |
| 1609 | return; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1610 | assert(C.ICmpType == SystemZICMP::Any && |
| 1611 | "Signedness shouldn't matter here."); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1612 | } else |
| 1613 | return; |
| 1614 | |
| 1615 | // Make sure that the first operand is an i32 of the right extension type. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1616 | ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ? |
| 1617 | ISD::SEXTLOAD : |
| 1618 | ISD::ZEXTLOAD); |
| 1619 | if (C.Op0.getValueType() != MVT::i32 || |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1620 | Load->getExtensionType() != ExtType) |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1621 | C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, |
| 1622 | Load->getChain(), Load->getBasePtr(), |
| 1623 | Load->getPointerInfo(), Load->getMemoryVT(), |
| 1624 | Load->isVolatile(), Load->isNonTemporal(), |
Louis Gerbarg | 67474e3 | 2014-07-31 21:45:05 +0000 | [diff] [blame] | 1625 | Load->isInvariant(), Load->getAlignment()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1626 | |
| 1627 | // Make sure that the second operand is an i32 with the right value. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1628 | if (C.Op1.getValueType() != MVT::i32 || |
| 1629 | Value != ConstOp1->getZExtValue()) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1630 | C.Op1 = DAG.getConstant(Value, DL, MVT::i32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1633 | // Return true if Op is either an unextended load, or a load suitable |
| 1634 | // for integer register-memory comparisons of type ICmpType. |
| 1635 | static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1636 | auto *Load = dyn_cast<LoadSDNode>(Op.getNode()); |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1637 | if (Load) { |
| 1638 | // There are no instructions to compare a register with a memory byte. |
| 1639 | if (Load->getMemoryVT() == MVT::i8) |
| 1640 | return false; |
| 1641 | // Otherwise decide on extension type. |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1642 | switch (Load->getExtensionType()) { |
| 1643 | case ISD::NON_EXTLOAD: |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1644 | return true; |
| 1645 | case ISD::SEXTLOAD: |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1646 | return ICmpType != SystemZICMP::UnsignedOnly; |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1647 | case ISD::ZEXTLOAD: |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1648 | return ICmpType != SystemZICMP::SignedOnly; |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1649 | default: |
| 1650 | break; |
| 1651 | } |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1652 | } |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1653 | return false; |
| 1654 | } |
| 1655 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1656 | // Return true if it is better to swap the operands of C. |
| 1657 | static bool shouldSwapCmpOperands(const Comparison &C) { |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1658 | // Leave f128 comparisons alone, since they have no memory forms. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1659 | if (C.Op0.getValueType() == MVT::f128) |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1660 | return false; |
| 1661 | |
| 1662 | // Always keep a floating-point constant second, since comparisons with |
| 1663 | // zero can use LOAD TEST and comparisons with other constants make a |
| 1664 | // natural memory operand. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1665 | if (isa<ConstantFPSDNode>(C.Op1)) |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1666 | return false; |
| 1667 | |
| 1668 | // Never swap comparisons with zero since there are many ways to optimize |
| 1669 | // those later. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1670 | auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1671 | if (ConstOp1 && ConstOp1->getZExtValue() == 0) |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1672 | return false; |
| 1673 | |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1674 | // Also keep natural memory operands second if the loaded value is |
| 1675 | // only used here. Several comparisons have memory forms. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1676 | if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse()) |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1677 | return false; |
| 1678 | |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1679 | // Look for cases where Cmp0 is a single-use load and Cmp1 isn't. |
| 1680 | // In that case we generally prefer the memory to be second. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1681 | if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) { |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1682 | // The only exceptions are when the second operand is a constant and |
| 1683 | // we can use things like CHHSI. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1684 | if (!ConstOp1) |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1685 | return true; |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1686 | // The unsigned memory-immediate instructions can handle 16-bit |
| 1687 | // unsigned integers. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1688 | if (C.ICmpType != SystemZICMP::SignedOnly && |
| 1689 | isUInt<16>(ConstOp1->getZExtValue())) |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1690 | return false; |
| 1691 | // The signed memory-immediate instructions can handle 16-bit |
| 1692 | // signed integers. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1693 | if (C.ICmpType != SystemZICMP::UnsignedOnly && |
| 1694 | isInt<16>(ConstOp1->getSExtValue())) |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1695 | return false; |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1696 | return true; |
| 1697 | } |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1698 | |
| 1699 | // Try to promote the use of CGFR and CLGFR. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1700 | unsigned Opcode0 = C.Op0.getOpcode(); |
| 1701 | if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND) |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1702 | return true; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1703 | if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND) |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1704 | return true; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1705 | if (C.ICmpType != SystemZICMP::SignedOnly && |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1706 | Opcode0 == ISD::AND && |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1707 | C.Op0.getOperand(1).getOpcode() == ISD::Constant && |
| 1708 | cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff) |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1709 | return true; |
| 1710 | |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1711 | return false; |
| 1712 | } |
| 1713 | |
Richard Sandiford | 73170f8 | 2013-12-11 11:45:08 +0000 | [diff] [blame] | 1714 | // Return a version of comparison CC mask CCMask in which the LT and GT |
| 1715 | // actions are swapped. |
| 1716 | static unsigned reverseCCMask(unsigned CCMask) { |
| 1717 | return ((CCMask & SystemZ::CCMASK_CMP_EQ) | |
| 1718 | (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) | |
| 1719 | (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) | |
| 1720 | (CCMask & SystemZ::CCMASK_CMP_UO)); |
| 1721 | } |
| 1722 | |
Richard Sandiford | 0847c45 | 2013-12-13 15:50:30 +0000 | [diff] [blame] | 1723 | // Check whether C tests for equality between X and Y and whether X - Y |
| 1724 | // or Y - X is also computed. In that case it's better to compare the |
| 1725 | // result of the subtraction against zero. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1726 | static void adjustForSubtraction(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Richard Sandiford | 0847c45 | 2013-12-13 15:50:30 +0000 | [diff] [blame] | 1727 | if (C.CCMask == SystemZ::CCMASK_CMP_EQ || |
| 1728 | C.CCMask == SystemZ::CCMASK_CMP_NE) { |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 1729 | for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) { |
Richard Sandiford | 0847c45 | 2013-12-13 15:50:30 +0000 | [diff] [blame] | 1730 | SDNode *N = *I; |
| 1731 | if (N->getOpcode() == ISD::SUB && |
| 1732 | ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) || |
| 1733 | (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) { |
| 1734 | C.Op0 = SDValue(N, 0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1735 | C.Op1 = DAG.getConstant(0, DL, N->getValueType(0)); |
Richard Sandiford | 0847c45 | 2013-12-13 15:50:30 +0000 | [diff] [blame] | 1736 | return; |
| 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | } |
| 1741 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1742 | // Check whether C compares a floating-point value with zero and if that |
| 1743 | // floating-point value is also negated. In this case we can use the |
| 1744 | // negation to set CC, so avoiding separate LOAD AND TEST and |
| 1745 | // LOAD (NEGATIVE/COMPLEMENT) instructions. |
| 1746 | static void adjustForFNeg(Comparison &C) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1747 | auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1); |
Richard Sandiford | 73170f8 | 2013-12-11 11:45:08 +0000 | [diff] [blame] | 1748 | if (C1 && C1->isZero()) { |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 1749 | for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) { |
Richard Sandiford | 73170f8 | 2013-12-11 11:45:08 +0000 | [diff] [blame] | 1750 | SDNode *N = *I; |
| 1751 | if (N->getOpcode() == ISD::FNEG) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1752 | C.Op0 = SDValue(N, 0); |
| 1753 | C.CCMask = reverseCCMask(C.CCMask); |
Richard Sandiford | 73170f8 | 2013-12-11 11:45:08 +0000 | [diff] [blame] | 1754 | return; |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1760 | // Check whether C compares (shl X, 32) with 0 and whether X is |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1761 | // also sign-extended. In that case it is better to test the result |
| 1762 | // of the sign extension using LTGFR. |
| 1763 | // |
| 1764 | // This case is important because InstCombine transforms a comparison |
| 1765 | // with (sext (trunc X)) into a comparison with (shl X, 32). |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1766 | static void adjustForLTGFR(Comparison &C) { |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1767 | // Check for a comparison between (shl X, 32) and 0. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1768 | if (C.Op0.getOpcode() == ISD::SHL && |
| 1769 | C.Op0.getValueType() == MVT::i64 && |
| 1770 | C.Op1.getOpcode() == ISD::Constant && |
| 1771 | cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1772 | auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1)); |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1773 | if (C1 && C1->getZExtValue() == 32) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1774 | SDValue ShlOp0 = C.Op0.getOperand(0); |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1775 | // See whether X has any SIGN_EXTEND_INREG uses. |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 1776 | for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) { |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1777 | SDNode *N = *I; |
| 1778 | if (N->getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 1779 | cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1780 | C.Op0 = SDValue(N, 0); |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1781 | return; |
| 1782 | } |
| 1783 | } |
| 1784 | } |
| 1785 | } |
| 1786 | } |
| 1787 | |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1788 | // If C compares the truncation of an extending load, try to compare |
| 1789 | // the untruncated value instead. This exposes more opportunities to |
| 1790 | // reuse CC. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1791 | static void adjustICmpTruncate(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1792 | if (C.Op0.getOpcode() == ISD::TRUNCATE && |
| 1793 | C.Op0.getOperand(0).getOpcode() == ISD::LOAD && |
| 1794 | C.Op1.getOpcode() == ISD::Constant && |
| 1795 | cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1796 | auto *L = cast<LoadSDNode>(C.Op0.getOperand(0)); |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1797 | if (L->getMemoryVT().getStoreSizeInBits() |
| 1798 | <= C.Op0.getValueType().getSizeInBits()) { |
| 1799 | unsigned Type = L->getExtensionType(); |
| 1800 | if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) || |
| 1801 | (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) { |
| 1802 | C.Op0 = C.Op0.getOperand(0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1803 | C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType()); |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1809 | // Return true if shift operation N has an in-range constant shift value. |
| 1810 | // Store it in ShiftVal if so. |
| 1811 | static bool isSimpleShift(SDValue N, unsigned &ShiftVal) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1812 | auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1813 | if (!Shift) |
| 1814 | return false; |
| 1815 | |
| 1816 | uint64_t Amount = Shift->getZExtValue(); |
| 1817 | if (Amount >= N.getValueType().getSizeInBits()) |
| 1818 | return false; |
| 1819 | |
| 1820 | ShiftVal = Amount; |
| 1821 | return true; |
| 1822 | } |
| 1823 | |
| 1824 | // Check whether an AND with Mask is suitable for a TEST UNDER MASK |
| 1825 | // instruction and whether the CC value is descriptive enough to handle |
| 1826 | // a comparison of type Opcode between the AND result and CmpVal. |
| 1827 | // CCMask says which comparison result is being tested and BitSize is |
| 1828 | // the number of bits in the operands. If TEST UNDER MASK can be used, |
| 1829 | // return the corresponding CC mask, otherwise return 0. |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1830 | static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask, |
| 1831 | uint64_t Mask, uint64_t CmpVal, |
| 1832 | unsigned ICmpType) { |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1833 | assert(Mask != 0 && "ANDs with zero should have been removed by now"); |
| 1834 | |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1835 | // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL. |
| 1836 | if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) && |
| 1837 | !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask)) |
| 1838 | return 0; |
| 1839 | |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1840 | // Work out the masks for the lowest and highest bits. |
| 1841 | unsigned HighShift = 63 - countLeadingZeros(Mask); |
| 1842 | uint64_t High = uint64_t(1) << HighShift; |
| 1843 | uint64_t Low = uint64_t(1) << countTrailingZeros(Mask); |
| 1844 | |
| 1845 | // Signed ordered comparisons are effectively unsigned if the sign |
| 1846 | // bit is dropped. |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1847 | bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly); |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1848 | |
| 1849 | // Check for equality comparisons with 0, or the equivalent. |
| 1850 | if (CmpVal == 0) { |
| 1851 | if (CCMask == SystemZ::CCMASK_CMP_EQ) |
| 1852 | return SystemZ::CCMASK_TM_ALL_0; |
| 1853 | if (CCMask == SystemZ::CCMASK_CMP_NE) |
| 1854 | return SystemZ::CCMASK_TM_SOME_1; |
| 1855 | } |
| 1856 | if (EffectivelyUnsigned && CmpVal <= Low) { |
| 1857 | if (CCMask == SystemZ::CCMASK_CMP_LT) |
| 1858 | return SystemZ::CCMASK_TM_ALL_0; |
| 1859 | if (CCMask == SystemZ::CCMASK_CMP_GE) |
| 1860 | return SystemZ::CCMASK_TM_SOME_1; |
| 1861 | } |
| 1862 | if (EffectivelyUnsigned && CmpVal < Low) { |
| 1863 | if (CCMask == SystemZ::CCMASK_CMP_LE) |
| 1864 | return SystemZ::CCMASK_TM_ALL_0; |
| 1865 | if (CCMask == SystemZ::CCMASK_CMP_GT) |
| 1866 | return SystemZ::CCMASK_TM_SOME_1; |
| 1867 | } |
| 1868 | |
| 1869 | // Check for equality comparisons with the mask, or the equivalent. |
| 1870 | if (CmpVal == Mask) { |
| 1871 | if (CCMask == SystemZ::CCMASK_CMP_EQ) |
| 1872 | return SystemZ::CCMASK_TM_ALL_1; |
| 1873 | if (CCMask == SystemZ::CCMASK_CMP_NE) |
| 1874 | return SystemZ::CCMASK_TM_SOME_0; |
| 1875 | } |
| 1876 | if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) { |
| 1877 | if (CCMask == SystemZ::CCMASK_CMP_GT) |
| 1878 | return SystemZ::CCMASK_TM_ALL_1; |
| 1879 | if (CCMask == SystemZ::CCMASK_CMP_LE) |
| 1880 | return SystemZ::CCMASK_TM_SOME_0; |
| 1881 | } |
| 1882 | if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) { |
| 1883 | if (CCMask == SystemZ::CCMASK_CMP_GE) |
| 1884 | return SystemZ::CCMASK_TM_ALL_1; |
| 1885 | if (CCMask == SystemZ::CCMASK_CMP_LT) |
| 1886 | return SystemZ::CCMASK_TM_SOME_0; |
| 1887 | } |
| 1888 | |
| 1889 | // Check for ordered comparisons with the top bit. |
| 1890 | if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) { |
| 1891 | if (CCMask == SystemZ::CCMASK_CMP_LE) |
| 1892 | return SystemZ::CCMASK_TM_MSB_0; |
| 1893 | if (CCMask == SystemZ::CCMASK_CMP_GT) |
| 1894 | return SystemZ::CCMASK_TM_MSB_1; |
| 1895 | } |
| 1896 | if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) { |
| 1897 | if (CCMask == SystemZ::CCMASK_CMP_LT) |
| 1898 | return SystemZ::CCMASK_TM_MSB_0; |
| 1899 | if (CCMask == SystemZ::CCMASK_CMP_GE) |
| 1900 | return SystemZ::CCMASK_TM_MSB_1; |
| 1901 | } |
| 1902 | |
| 1903 | // If there are just two bits, we can do equality checks for Low and High |
| 1904 | // as well. |
| 1905 | if (Mask == Low + High) { |
| 1906 | if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low) |
| 1907 | return SystemZ::CCMASK_TM_MIXED_MSB_0; |
| 1908 | if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low) |
| 1909 | return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY; |
| 1910 | if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High) |
| 1911 | return SystemZ::CCMASK_TM_MIXED_MSB_1; |
| 1912 | if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High) |
| 1913 | return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY; |
| 1914 | } |
| 1915 | |
| 1916 | // Looks like we've exhausted our options. |
| 1917 | return 0; |
| 1918 | } |
| 1919 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1920 | // See whether C can be implemented as a TEST UNDER MASK instruction. |
| 1921 | // Update the arguments with the TM version if so. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1922 | static void adjustForTestUnderMask(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1923 | // Check that we have a comparison with a constant. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1924 | auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1925 | if (!ConstOp1) |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 1926 | return; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1927 | uint64_t CmpVal = ConstOp1->getZExtValue(); |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 1928 | |
| 1929 | // Check whether the nonconstant input is an AND with a constant mask. |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1930 | Comparison NewC(C); |
| 1931 | uint64_t MaskVal; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1932 | ConstantSDNode *Mask = nullptr; |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1933 | if (C.Op0.getOpcode() == ISD::AND) { |
| 1934 | NewC.Op0 = C.Op0.getOperand(0); |
| 1935 | NewC.Op1 = C.Op0.getOperand(1); |
| 1936 | Mask = dyn_cast<ConstantSDNode>(NewC.Op1); |
| 1937 | if (!Mask) |
| 1938 | return; |
| 1939 | MaskVal = Mask->getZExtValue(); |
| 1940 | } else { |
| 1941 | // There is no instruction to compare with a 64-bit immediate |
| 1942 | // so use TMHH instead if possible. We need an unsigned ordered |
| 1943 | // comparison with an i64 immediate. |
| 1944 | if (NewC.Op0.getValueType() != MVT::i64 || |
| 1945 | NewC.CCMask == SystemZ::CCMASK_CMP_EQ || |
| 1946 | NewC.CCMask == SystemZ::CCMASK_CMP_NE || |
| 1947 | NewC.ICmpType == SystemZICMP::SignedOnly) |
| 1948 | return; |
| 1949 | // Convert LE and GT comparisons into LT and GE. |
| 1950 | if (NewC.CCMask == SystemZ::CCMASK_CMP_LE || |
| 1951 | NewC.CCMask == SystemZ::CCMASK_CMP_GT) { |
| 1952 | if (CmpVal == uint64_t(-1)) |
| 1953 | return; |
| 1954 | CmpVal += 1; |
| 1955 | NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ; |
| 1956 | } |
| 1957 | // If the low N bits of Op1 are zero than the low N bits of Op0 can |
| 1958 | // be masked off without changing the result. |
| 1959 | MaskVal = -(CmpVal & -CmpVal); |
| 1960 | NewC.ICmpType = SystemZICMP::UnsignedOnly; |
| 1961 | } |
Ulrich Weigand | b8d76fb | 2015-03-30 13:46:59 +0000 | [diff] [blame] | 1962 | if (!MaskVal) |
| 1963 | return; |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 1964 | |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1965 | // Check whether the combination of mask, comparison value and comparison |
| 1966 | // type are suitable. |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1967 | unsigned BitSize = NewC.Op0.getValueType().getSizeInBits(); |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1968 | unsigned NewCCMask, ShiftVal; |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1969 | if (NewC.ICmpType != SystemZICMP::SignedOnly && |
| 1970 | NewC.Op0.getOpcode() == ISD::SHL && |
| 1971 | isSimpleShift(NewC.Op0, ShiftVal) && |
| 1972 | (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, |
| 1973 | MaskVal >> ShiftVal, |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1974 | CmpVal >> ShiftVal, |
| 1975 | SystemZICMP::Any))) { |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1976 | NewC.Op0 = NewC.Op0.getOperand(0); |
| 1977 | MaskVal >>= ShiftVal; |
| 1978 | } else if (NewC.ICmpType != SystemZICMP::SignedOnly && |
| 1979 | NewC.Op0.getOpcode() == ISD::SRL && |
| 1980 | isSimpleShift(NewC.Op0, ShiftVal) && |
| 1981 | (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1982 | MaskVal << ShiftVal, |
| 1983 | CmpVal << ShiftVal, |
| 1984 | SystemZICMP::UnsignedOnly))) { |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1985 | NewC.Op0 = NewC.Op0.getOperand(0); |
| 1986 | MaskVal <<= ShiftVal; |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1987 | } else { |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1988 | NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal, |
| 1989 | NewC.ICmpType); |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1990 | if (!NewCCMask) |
| 1991 | return; |
| 1992 | } |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1993 | |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 1994 | // Go ahead and make the change. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1995 | C.Opcode = SystemZISD::TM; |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1996 | C.Op0 = NewC.Op0; |
| 1997 | if (Mask && Mask->getZExtValue() == MaskVal) |
| 1998 | C.Op1 = SDValue(Mask, 0); |
| 1999 | else |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2000 | C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType()); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2001 | C.CCValid = SystemZ::CCMASK_TM; |
| 2002 | C.CCMask = NewCCMask; |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2005 | // Return a Comparison that tests the condition-code result of intrinsic |
| 2006 | // node Call against constant integer CC using comparison code Cond. |
| 2007 | // Opcode is the opcode of the SystemZISD operation for the intrinsic |
| 2008 | // and CCValid is the set of possible condition-code results. |
| 2009 | static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode, |
| 2010 | SDValue Call, unsigned CCValid, uint64_t CC, |
| 2011 | ISD::CondCode Cond) { |
| 2012 | Comparison C(Call, SDValue()); |
| 2013 | C.Opcode = Opcode; |
| 2014 | C.CCValid = CCValid; |
| 2015 | if (Cond == ISD::SETEQ) |
| 2016 | // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3. |
| 2017 | C.CCMask = CC < 4 ? 1 << (3 - CC) : 0; |
| 2018 | else if (Cond == ISD::SETNE) |
| 2019 | // ...and the inverse of that. |
| 2020 | C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1; |
| 2021 | else if (Cond == ISD::SETLT || Cond == ISD::SETULT) |
| 2022 | // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3, |
| 2023 | // always true for CC>3. |
Justin Bogner | a6d3836 | 2015-06-23 15:38:24 +0000 | [diff] [blame] | 2024 | C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1; |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2025 | else if (Cond == ISD::SETGE || Cond == ISD::SETUGE) |
| 2026 | // ...and the inverse of that. |
Justin Bogner | a6d3836 | 2015-06-23 15:38:24 +0000 | [diff] [blame] | 2027 | C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0; |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2028 | else if (Cond == ISD::SETLE || Cond == ISD::SETULE) |
| 2029 | // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true), |
| 2030 | // always true for CC>3. |
Justin Bogner | a6d3836 | 2015-06-23 15:38:24 +0000 | [diff] [blame] | 2031 | C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1; |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2032 | else if (Cond == ISD::SETGT || Cond == ISD::SETUGT) |
| 2033 | // ...and the inverse of that. |
Justin Bogner | a6d3836 | 2015-06-23 15:38:24 +0000 | [diff] [blame] | 2034 | C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0; |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2035 | else |
| 2036 | llvm_unreachable("Unexpected integer comparison type"); |
| 2037 | C.CCMask &= CCValid; |
| 2038 | return C; |
| 2039 | } |
| 2040 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2041 | // Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1. |
| 2042 | static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2043 | ISD::CondCode Cond, SDLoc DL) { |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2044 | if (CmpOp1.getOpcode() == ISD::Constant) { |
| 2045 | uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue(); |
| 2046 | unsigned Opcode, CCValid; |
| 2047 | if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN && |
| 2048 | CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) && |
| 2049 | isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid)) |
| 2050 | return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond); |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 2051 | if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN && |
| 2052 | CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 && |
| 2053 | isIntrinsicWithCC(CmpOp0, Opcode, CCValid)) |
| 2054 | return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2055 | } |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2056 | Comparison C(CmpOp0, CmpOp1); |
| 2057 | C.CCMask = CCMaskForCondCode(Cond); |
| 2058 | if (C.Op0.getValueType().isFloatingPoint()) { |
| 2059 | C.CCValid = SystemZ::CCMASK_FCMP; |
| 2060 | C.Opcode = SystemZISD::FCMP; |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 2061 | adjustForFNeg(C); |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 2062 | } else { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2063 | C.CCValid = SystemZ::CCMASK_ICMP; |
| 2064 | C.Opcode = SystemZISD::ICMP; |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 2065 | // Choose the type of comparison. Equality and inequality tests can |
| 2066 | // use either signed or unsigned comparisons. The choice also doesn't |
| 2067 | // matter if both sign bits are known to be clear. In those cases we |
| 2068 | // want to give the main isel code the freedom to choose whichever |
| 2069 | // form fits best. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2070 | if (C.CCMask == SystemZ::CCMASK_CMP_EQ || |
| 2071 | C.CCMask == SystemZ::CCMASK_CMP_NE || |
| 2072 | (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1))) |
| 2073 | C.ICmpType = SystemZICMP::Any; |
| 2074 | else if (C.CCMask & SystemZ::CCMASK_CMP_UO) |
| 2075 | C.ICmpType = SystemZICMP::UnsignedOnly; |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 2076 | else |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2077 | C.ICmpType = SystemZICMP::SignedOnly; |
| 2078 | C.CCMask &= ~SystemZ::CCMASK_CMP_UO; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2079 | adjustZeroCmp(DAG, DL, C); |
| 2080 | adjustSubwordCmp(DAG, DL, C); |
| 2081 | adjustForSubtraction(DAG, DL, C); |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 2082 | adjustForLTGFR(C); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2083 | adjustICmpTruncate(DAG, DL, C); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2086 | if (shouldSwapCmpOperands(C)) { |
| 2087 | std::swap(C.Op0, C.Op1); |
| 2088 | C.CCMask = reverseCCMask(C.CCMask); |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2091 | adjustForTestUnderMask(DAG, DL, C); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2092 | return C; |
| 2093 | } |
| 2094 | |
| 2095 | // Emit the comparison instruction described by C. |
| 2096 | static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2097 | if (!C.Op1.getNode()) { |
| 2098 | SDValue Op; |
| 2099 | switch (C.Op0.getOpcode()) { |
| 2100 | case ISD::INTRINSIC_W_CHAIN: |
| 2101 | Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode); |
| 2102 | break; |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 2103 | case ISD::INTRINSIC_WO_CHAIN: |
| 2104 | Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode); |
| 2105 | break; |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2106 | default: |
| 2107 | llvm_unreachable("Invalid comparison operands"); |
| 2108 | } |
| 2109 | return SDValue(Op.getNode(), Op->getNumValues() - 1); |
| 2110 | } |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2111 | if (C.Opcode == SystemZISD::ICMP) |
| 2112 | return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2113 | DAG.getConstant(C.ICmpType, DL, MVT::i32)); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2114 | if (C.Opcode == SystemZISD::TM) { |
| 2115 | bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) != |
| 2116 | bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1)); |
| 2117 | return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2118 | DAG.getConstant(RegisterOnly, DL, MVT::i32)); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2119 | } |
| 2120 | return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2123 | // Implement a 32-bit *MUL_LOHI operation by extending both operands to |
| 2124 | // 64 bits. Extend is the extension type to use. Store the high part |
| 2125 | // in Hi and the low part in Lo. |
| 2126 | static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL, |
| 2127 | unsigned Extend, SDValue Op0, SDValue Op1, |
| 2128 | SDValue &Hi, SDValue &Lo) { |
| 2129 | Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0); |
| 2130 | Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1); |
| 2131 | SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2132 | Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, |
| 2133 | DAG.getConstant(32, DL, MVT::i64)); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2134 | Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi); |
| 2135 | Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul); |
| 2136 | } |
| 2137 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2138 | // Lower a binary operation that produces two VT results, one in each |
| 2139 | // half of a GR128 pair. Op0 and Op1 are the VT operands to the operation, |
| 2140 | // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation |
| 2141 | // on the extended Op0 and (unextended) Op1. Store the even register result |
| 2142 | // in Even and the odd register result in Odd. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2143 | static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2144 | unsigned Extend, unsigned Opcode, |
| 2145 | SDValue Op0, SDValue Op1, |
| 2146 | SDValue &Even, SDValue &Odd) { |
| 2147 | SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0); |
| 2148 | SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, |
| 2149 | SDValue(In128, 0), Op1); |
| 2150 | bool Is32Bit = is32Bit(VT); |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 2151 | Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result); |
| 2152 | Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2155 | // Return an i32 value that is 1 if the CC value produced by Glue is |
| 2156 | // in the mask CCMask and 0 otherwise. CC is known to have a value |
| 2157 | // in CCValid, so other values can be ignored. |
| 2158 | static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue, |
| 2159 | unsigned CCValid, unsigned CCMask) { |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 2160 | IPMConversion Conversion = getIPMConversion(CCValid, CCMask); |
| 2161 | SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue); |
| 2162 | |
| 2163 | if (Conversion.XORValue) |
| 2164 | Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2165 | DAG.getConstant(Conversion.XORValue, DL, MVT::i32)); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 2166 | |
| 2167 | if (Conversion.AddValue) |
| 2168 | Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2169 | DAG.getConstant(Conversion.AddValue, DL, MVT::i32)); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 2170 | |
| 2171 | // The SHR/AND sequence should get optimized to an RISBG. |
| 2172 | Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2173 | DAG.getConstant(Conversion.Bit, DL, MVT::i32)); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 2174 | if (Conversion.Bit != 31) |
| 2175 | Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2176 | DAG.getConstant(1, DL, MVT::i32)); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 2177 | return Result; |
| 2178 | } |
| 2179 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2180 | // Return the SystemISD vector comparison operation for CC, or 0 if it cannot |
| 2181 | // be done directly. IsFP is true if CC is for a floating-point rather than |
| 2182 | // integer comparison. |
| 2183 | static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2184 | switch (CC) { |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2185 | case ISD::SETOEQ: |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2186 | case ISD::SETEQ: |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2187 | return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2188 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2189 | case ISD::SETOGE: |
| 2190 | case ISD::SETGE: |
Saleem Abdulrasool | ee33c49 | 2015-05-10 00:53:41 +0000 | [diff] [blame] | 2191 | return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2192 | |
| 2193 | case ISD::SETOGT: |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2194 | case ISD::SETGT: |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2195 | return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2196 | |
| 2197 | case ISD::SETUGT: |
Saleem Abdulrasool | ee33c49 | 2015-05-10 00:53:41 +0000 | [diff] [blame] | 2198 | return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2199 | |
| 2200 | default: |
| 2201 | return 0; |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | // Return the SystemZISD vector comparison operation for CC or its inverse, |
| 2206 | // or 0 if neither can be done directly. Indicate in Invert whether the |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2207 | // result is for the inverse of CC. IsFP is true if CC is for a |
| 2208 | // floating-point rather than integer comparison. |
| 2209 | static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP, |
| 2210 | bool &Invert) { |
| 2211 | if (unsigned Opcode = getVectorComparison(CC, IsFP)) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2212 | Invert = false; |
| 2213 | return Opcode; |
| 2214 | } |
| 2215 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2216 | CC = ISD::getSetCCInverse(CC, !IsFP); |
| 2217 | if (unsigned Opcode = getVectorComparison(CC, IsFP)) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2218 | Invert = true; |
| 2219 | return Opcode; |
| 2220 | } |
| 2221 | |
| 2222 | return 0; |
| 2223 | } |
| 2224 | |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 2225 | // Return a v2f64 that contains the extended form of elements Start and Start+1 |
| 2226 | // of v4f32 value Op. |
| 2227 | static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, SDLoc DL, |
| 2228 | SDValue Op) { |
| 2229 | int Mask[] = { Start, -1, Start + 1, -1 }; |
| 2230 | Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask); |
| 2231 | return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op); |
| 2232 | } |
| 2233 | |
| 2234 | // Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode, |
| 2235 | // producing a result of type VT. |
| 2236 | static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, SDLoc DL, |
| 2237 | EVT VT, SDValue CmpOp0, SDValue CmpOp1) { |
| 2238 | // There is no hardware support for v4f32, so extend the vector into |
| 2239 | // two v2f64s and compare those. |
| 2240 | if (CmpOp0.getValueType() == MVT::v4f32) { |
| 2241 | SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0); |
| 2242 | SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0); |
| 2243 | SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1); |
| 2244 | SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1); |
| 2245 | SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1); |
| 2246 | SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1); |
| 2247 | return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes); |
| 2248 | } |
| 2249 | return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1); |
| 2250 | } |
| 2251 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2252 | // Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing |
| 2253 | // an integer mask of type VT. |
| 2254 | static SDValue lowerVectorSETCC(SelectionDAG &DAG, SDLoc DL, EVT VT, |
| 2255 | ISD::CondCode CC, SDValue CmpOp0, |
| 2256 | SDValue CmpOp1) { |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2257 | bool IsFP = CmpOp0.getValueType().isFloatingPoint(); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2258 | bool Invert = false; |
| 2259 | SDValue Cmp; |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2260 | switch (CC) { |
| 2261 | // Handle tests for order using (or (ogt y x) (oge x y)). |
| 2262 | case ISD::SETUO: |
| 2263 | Invert = true; |
| 2264 | case ISD::SETO: { |
| 2265 | assert(IsFP && "Unexpected integer comparison"); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 2266 | SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0); |
| 2267 | SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2268 | Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE); |
| 2269 | break; |
| 2270 | } |
| 2271 | |
| 2272 | // Handle <> tests using (or (ogt y x) (ogt x y)). |
| 2273 | case ISD::SETUEQ: |
| 2274 | Invert = true; |
| 2275 | case ISD::SETONE: { |
| 2276 | assert(IsFP && "Unexpected integer comparison"); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 2277 | SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0); |
| 2278 | SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2279 | Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT); |
| 2280 | break; |
| 2281 | } |
| 2282 | |
| 2283 | // Otherwise a single comparison is enough. It doesn't really |
| 2284 | // matter whether we try the inversion or the swap first, since |
| 2285 | // there are no cases where both work. |
| 2286 | default: |
| 2287 | if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert)) |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 2288 | Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2289 | else { |
| 2290 | CC = ISD::getSetCCSwappedOperands(CC); |
| 2291 | if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert)) |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 2292 | Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2293 | else |
| 2294 | llvm_unreachable("Unhandled comparison"); |
| 2295 | } |
| 2296 | break; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2297 | } |
| 2298 | if (Invert) { |
| 2299 | SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, |
| 2300 | DAG.getConstant(65535, DL, MVT::i32)); |
| 2301 | Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask); |
| 2302 | Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask); |
| 2303 | } |
| 2304 | return Cmp; |
| 2305 | } |
| 2306 | |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2307 | SDValue SystemZTargetLowering::lowerSETCC(SDValue Op, |
| 2308 | SelectionDAG &DAG) const { |
| 2309 | SDValue CmpOp0 = Op.getOperand(0); |
| 2310 | SDValue CmpOp1 = Op.getOperand(1); |
| 2311 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
| 2312 | SDLoc DL(Op); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2313 | EVT VT = Op.getValueType(); |
| 2314 | if (VT.isVector()) |
| 2315 | return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2316 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2317 | Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL)); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2318 | SDValue Glue = emitCmp(DAG, DL, C); |
| 2319 | return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2322 | SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2323 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); |
| 2324 | SDValue CmpOp0 = Op.getOperand(2); |
| 2325 | SDValue CmpOp1 = Op.getOperand(3); |
| 2326 | SDValue Dest = Op.getOperand(4); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2327 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2328 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2329 | Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL)); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2330 | SDValue Glue = emitCmp(DAG, DL, C); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2331 | return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2332 | Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32), |
| 2333 | DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
Richard Sandiford | 5748547 | 2013-12-13 15:35:00 +0000 | [diff] [blame] | 2336 | // Return true if Pos is CmpOp and Neg is the negative of CmpOp, |
| 2337 | // allowing Pos and Neg to be wider than CmpOp. |
| 2338 | static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) { |
| 2339 | return (Neg.getOpcode() == ISD::SUB && |
| 2340 | Neg.getOperand(0).getOpcode() == ISD::Constant && |
| 2341 | cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 && |
| 2342 | Neg.getOperand(1) == Pos && |
| 2343 | (Pos == CmpOp || |
| 2344 | (Pos.getOpcode() == ISD::SIGN_EXTEND && |
| 2345 | Pos.getOperand(0) == CmpOp))); |
| 2346 | } |
| 2347 | |
| 2348 | // Return the absolute or negative absolute of Op; IsNegative decides which. |
| 2349 | static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op, |
| 2350 | bool IsNegative) { |
| 2351 | Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op); |
| 2352 | if (IsNegative) |
| 2353 | Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2354 | DAG.getConstant(0, DL, Op.getValueType()), Op); |
Richard Sandiford | 5748547 | 2013-12-13 15:35:00 +0000 | [diff] [blame] | 2355 | return Op; |
| 2356 | } |
| 2357 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2358 | SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op, |
| 2359 | SelectionDAG &DAG) const { |
| 2360 | SDValue CmpOp0 = Op.getOperand(0); |
| 2361 | SDValue CmpOp1 = Op.getOperand(1); |
| 2362 | SDValue TrueOp = Op.getOperand(2); |
| 2363 | SDValue FalseOp = Op.getOperand(3); |
| 2364 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2365 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2366 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2367 | Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL)); |
Richard Sandiford | 5748547 | 2013-12-13 15:35:00 +0000 | [diff] [blame] | 2368 | |
| 2369 | // Check for absolute and negative-absolute selections, including those |
| 2370 | // where the comparison value is sign-extended (for LPGFR and LNGFR). |
| 2371 | // This check supplements the one in DAGCombiner. |
| 2372 | if (C.Opcode == SystemZISD::ICMP && |
| 2373 | C.CCMask != SystemZ::CCMASK_CMP_EQ && |
| 2374 | C.CCMask != SystemZ::CCMASK_CMP_NE && |
| 2375 | C.Op1.getOpcode() == ISD::Constant && |
| 2376 | cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { |
| 2377 | if (isAbsolute(C.Op0, TrueOp, FalseOp)) |
| 2378 | return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT); |
| 2379 | if (isAbsolute(C.Op0, FalseOp, TrueOp)) |
| 2380 | return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT); |
| 2381 | } |
| 2382 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2383 | SDValue Glue = emitCmp(DAG, DL, C); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2384 | |
| 2385 | // Special case for handling -1/0 results. The shifts we use here |
| 2386 | // should get optimized with the IPM conversion sequence. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2387 | auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp); |
| 2388 | auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2389 | if (TrueC && FalseC) { |
| 2390 | int64_t TrueVal = TrueC->getSExtValue(); |
| 2391 | int64_t FalseVal = FalseC->getSExtValue(); |
| 2392 | if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) { |
| 2393 | // Invert the condition if we want -1 on false. |
| 2394 | if (TrueVal == 0) |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2395 | C.CCMask ^= C.CCValid; |
| 2396 | SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2397 | EVT VT = Op.getValueType(); |
| 2398 | // Extend the result to VT. Upper bits are ignored. |
| 2399 | if (!is32Bit(VT)) |
| 2400 | Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result); |
| 2401 | // Sign-extend from the low bit. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2402 | SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2403 | SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt); |
| 2404 | return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt); |
| 2405 | } |
| 2406 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2407 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2408 | SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32), |
| 2409 | DAG.getConstant(C.CCMask, DL, MVT::i32), Glue}; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2410 | |
| 2411 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 2412 | return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node, |
| 2416 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2417 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2418 | const GlobalValue *GV = Node->getGlobal(); |
| 2419 | int64_t Offset = Node->getOffset(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2420 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 2421 | Reloc::Model RM = DAG.getTarget().getRelocationModel(); |
| 2422 | CodeModel::Model CM = DAG.getTarget().getCodeModel(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2423 | |
| 2424 | SDValue Result; |
| 2425 | if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) { |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 2426 | // Assign anchors at 1<<12 byte boundaries. |
| 2427 | uint64_t Anchor = Offset & ~uint64_t(0xfff); |
| 2428 | Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor); |
| 2429 | Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2430 | |
| 2431 | // The offset can be folded into the address if it is aligned to a halfword. |
| 2432 | Offset -= Anchor; |
| 2433 | if (Offset != 0 && (Offset & 1) == 0) { |
| 2434 | SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset); |
| 2435 | Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2436 | Offset = 0; |
| 2437 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2438 | } else { |
| 2439 | Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT); |
| 2440 | Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2441 | Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 2442 | MachinePointerInfo::getGOT(DAG.getMachineFunction()), |
| 2443 | false, false, false, 0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | // If there was a non-zero offset that we didn't fold, create an explicit |
| 2447 | // addition for it. |
| 2448 | if (Offset != 0) |
| 2449 | Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2450 | DAG.getConstant(Offset, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2451 | |
| 2452 | return Result; |
| 2453 | } |
| 2454 | |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2455 | SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node, |
| 2456 | SelectionDAG &DAG, |
| 2457 | unsigned Opcode, |
| 2458 | SDValue GOTOffset) const { |
| 2459 | SDLoc DL(Node); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2460 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2461 | SDValue Chain = DAG.getEntryNode(); |
| 2462 | SDValue Glue; |
| 2463 | |
| 2464 | // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12. |
| 2465 | SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT); |
| 2466 | Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue); |
| 2467 | Glue = Chain.getValue(1); |
| 2468 | Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue); |
| 2469 | Glue = Chain.getValue(1); |
| 2470 | |
| 2471 | // The first call operand is the chain and the second is the TLS symbol. |
| 2472 | SmallVector<SDValue, 8> Ops; |
| 2473 | Ops.push_back(Chain); |
| 2474 | Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL, |
| 2475 | Node->getValueType(0), |
| 2476 | 0, 0)); |
| 2477 | |
| 2478 | // Add argument registers to the end of the list so that they are |
| 2479 | // known live into the call. |
| 2480 | Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT)); |
| 2481 | Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT)); |
| 2482 | |
| 2483 | // Add a register mask operand representing the call-preserved registers. |
| 2484 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
Eric Christopher | 9deb75d | 2015-03-11 22:42:13 +0000 | [diff] [blame] | 2485 | const uint32_t *Mask = |
| 2486 | TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C); |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2487 | assert(Mask && "Missing call preserved mask for calling convention"); |
| 2488 | Ops.push_back(DAG.getRegisterMask(Mask)); |
| 2489 | |
| 2490 | // Glue the call to the argument copies. |
| 2491 | Ops.push_back(Glue); |
| 2492 | |
| 2493 | // Emit the call. |
| 2494 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
| 2495 | Chain = DAG.getNode(Opcode, DL, NodeTys, Ops); |
| 2496 | Glue = Chain.getValue(1); |
| 2497 | |
| 2498 | // Copy the return value from %r2. |
| 2499 | return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue); |
| 2500 | } |
| 2501 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2502 | SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node, |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 2503 | SelectionDAG &DAG) const { |
Chih-Hung Hsieh | 1e85958 | 2015-07-28 16:24:05 +0000 | [diff] [blame] | 2504 | if (DAG.getTarget().Options.EmulatedTLS) |
| 2505 | return LowerToTLSEmulatedModel(Node, DAG); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2506 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2507 | const GlobalValue *GV = Node->getGlobal(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2508 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 2509 | TLSModel::Model model = DAG.getTarget().getTLSModel(GV); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2510 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2511 | // The high part of the thread pointer is in access register 0. |
| 2512 | SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2513 | DAG.getConstant(0, DL, MVT::i32)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2514 | TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi); |
| 2515 | |
| 2516 | // The low part of the thread pointer is in access register 1. |
| 2517 | SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2518 | DAG.getConstant(1, DL, MVT::i32)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2519 | TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo); |
| 2520 | |
| 2521 | // Merge them into a single 64-bit address. |
| 2522 | SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2523 | DAG.getConstant(32, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2524 | SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo); |
| 2525 | |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2526 | // Get the offset of GA from the thread pointer, based on the TLS model. |
| 2527 | SDValue Offset; |
| 2528 | switch (model) { |
| 2529 | case TLSModel::GeneralDynamic: { |
| 2530 | // Load the GOT offset of the tls_index (module ID / per-symbol offset). |
| 2531 | SystemZConstantPoolValue *CPV = |
| 2532 | SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2533 | |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2534 | Offset = DAG.getConstantPool(CPV, PtrVT, 8); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 2535 | Offset = DAG.getLoad( |
| 2536 | PtrVT, DL, DAG.getEntryNode(), Offset, |
| 2537 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, |
| 2538 | false, false, 0); |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2539 | |
| 2540 | // Call __tls_get_offset to retrieve the offset. |
| 2541 | Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset); |
| 2542 | break; |
| 2543 | } |
| 2544 | |
| 2545 | case TLSModel::LocalDynamic: { |
| 2546 | // Load the GOT offset of the module ID. |
| 2547 | SystemZConstantPoolValue *CPV = |
| 2548 | SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM); |
| 2549 | |
| 2550 | Offset = DAG.getConstantPool(CPV, PtrVT, 8); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 2551 | Offset = DAG.getLoad( |
| 2552 | PtrVT, DL, DAG.getEntryNode(), Offset, |
| 2553 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, |
| 2554 | false, false, 0); |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2555 | |
| 2556 | // Call __tls_get_offset to retrieve the module base offset. |
| 2557 | Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset); |
| 2558 | |
| 2559 | // Note: The SystemZLDCleanupPass will remove redundant computations |
| 2560 | // of the module base offset. Count total number of local-dynamic |
| 2561 | // accesses to trigger execution of that pass. |
| 2562 | SystemZMachineFunctionInfo* MFI = |
| 2563 | DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>(); |
| 2564 | MFI->incNumLocalDynamicTLSAccesses(); |
| 2565 | |
| 2566 | // Add the per-symbol offset. |
| 2567 | CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF); |
| 2568 | |
| 2569 | SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 2570 | DTPOffset = DAG.getLoad( |
| 2571 | PtrVT, DL, DAG.getEntryNode(), DTPOffset, |
| 2572 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, |
| 2573 | false, false, 0); |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2574 | |
| 2575 | Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset); |
| 2576 | break; |
| 2577 | } |
| 2578 | |
| 2579 | case TLSModel::InitialExec: { |
| 2580 | // Load the offset from the GOT. |
| 2581 | Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, |
| 2582 | SystemZII::MO_INDNTPOFF); |
| 2583 | Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 2584 | Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset, |
| 2585 | MachinePointerInfo::getGOT(DAG.getMachineFunction()), |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2586 | false, false, false, 0); |
| 2587 | break; |
| 2588 | } |
| 2589 | |
| 2590 | case TLSModel::LocalExec: { |
| 2591 | // Force the offset into the constant pool and load it from there. |
| 2592 | SystemZConstantPoolValue *CPV = |
| 2593 | SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF); |
| 2594 | |
| 2595 | Offset = DAG.getConstantPool(CPV, PtrVT, 8); |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 2596 | Offset = DAG.getLoad( |
| 2597 | PtrVT, DL, DAG.getEntryNode(), Offset, |
| 2598 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false, |
| 2599 | false, false, 0); |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2600 | break; |
Ulrich Weigand | b7e5909 | 2015-02-18 09:42:23 +0000 | [diff] [blame] | 2601 | } |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2602 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2603 | |
| 2604 | // Add the base and offset together. |
| 2605 | return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset); |
| 2606 | } |
| 2607 | |
| 2608 | SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node, |
| 2609 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2610 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2611 | const BlockAddress *BA = Node->getBlockAddress(); |
| 2612 | int64_t Offset = Node->getOffset(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2613 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2614 | |
| 2615 | SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset); |
| 2616 | Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2617 | return Result; |
| 2618 | } |
| 2619 | |
| 2620 | SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT, |
| 2621 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2622 | SDLoc DL(JT); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2623 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2624 | SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); |
| 2625 | |
| 2626 | // Use LARL to load the address of the table. |
| 2627 | return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2628 | } |
| 2629 | |
| 2630 | SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP, |
| 2631 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2632 | SDLoc DL(CP); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2633 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2634 | |
| 2635 | SDValue Result; |
| 2636 | if (CP->isMachineConstantPoolEntry()) |
| 2637 | Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 2638 | CP->getAlignment()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2639 | else |
| 2640 | Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 2641 | CP->getAlignment(), CP->getOffset()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2642 | |
| 2643 | // Use LARL to load the address of the constant pool entry. |
| 2644 | return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2645 | } |
| 2646 | |
| 2647 | SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op, |
| 2648 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2649 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2650 | SDValue In = Op.getOperand(0); |
| 2651 | EVT InVT = In.getValueType(); |
| 2652 | EVT ResVT = Op.getValueType(); |
| 2653 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2654 | // Convert loads directly. This is normally done by DAGCombiner, |
| 2655 | // but we need this case for bitcasts that are created during lowering |
| 2656 | // and which are then lowered themselves. |
| 2657 | if (auto *LoadN = dyn_cast<LoadSDNode>(In)) |
| 2658 | return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(), |
| 2659 | LoadN->getMemOperand()); |
| 2660 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2661 | if (InVT == MVT::i32 && ResVT == MVT::f32) { |
Richard Sandiford | f6377fb | 2013-10-01 14:31:11 +0000 | [diff] [blame] | 2662 | SDValue In64; |
| 2663 | if (Subtarget.hasHighWord()) { |
| 2664 | SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, |
| 2665 | MVT::i64); |
| 2666 | In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL, |
| 2667 | MVT::i64, SDValue(U64, 0), In); |
| 2668 | } else { |
| 2669 | In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In); |
| 2670 | In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2671 | DAG.getConstant(32, DL, MVT::i64)); |
Richard Sandiford | f6377fb | 2013-10-01 14:31:11 +0000 | [diff] [blame] | 2672 | } |
| 2673 | SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64); |
Ulrich Weigand | 9ac2f9b | 2015-05-04 17:41:22 +0000 | [diff] [blame] | 2674 | return DAG.getTargetExtractSubreg(SystemZ::subreg_r32, |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 2675 | DL, MVT::f32, Out64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2676 | } |
| 2677 | if (InVT == MVT::f32 && ResVT == MVT::i32) { |
| 2678 | SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64); |
Ulrich Weigand | 9ac2f9b | 2015-05-04 17:41:22 +0000 | [diff] [blame] | 2679 | SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL, |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 2680 | MVT::f64, SDValue(U64, 0), In); |
| 2681 | SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64); |
Richard Sandiford | f6377fb | 2013-10-01 14:31:11 +0000 | [diff] [blame] | 2682 | if (Subtarget.hasHighWord()) |
| 2683 | return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL, |
| 2684 | MVT::i32, Out64); |
| 2685 | SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2686 | DAG.getConstant(32, DL, MVT::i64)); |
Richard Sandiford | f6377fb | 2013-10-01 14:31:11 +0000 | [diff] [blame] | 2687 | return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2688 | } |
| 2689 | llvm_unreachable("Unexpected bitcast combination"); |
| 2690 | } |
| 2691 | |
| 2692 | SDValue SystemZTargetLowering::lowerVASTART(SDValue Op, |
| 2693 | SelectionDAG &DAG) const { |
| 2694 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2695 | SystemZMachineFunctionInfo *FuncInfo = |
| 2696 | MF.getInfo<SystemZMachineFunctionInfo>(); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 2697 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2698 | |
| 2699 | SDValue Chain = Op.getOperand(0); |
| 2700 | SDValue Addr = Op.getOperand(1); |
| 2701 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2702 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2703 | |
| 2704 | // The initial values of each field. |
| 2705 | const unsigned NumFields = 4; |
| 2706 | SDValue Fields[NumFields] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2707 | DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT), |
| 2708 | DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2709 | DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT), |
| 2710 | DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT) |
| 2711 | }; |
| 2712 | |
| 2713 | // Store each field into its respective slot. |
| 2714 | SDValue MemOps[NumFields]; |
| 2715 | unsigned Offset = 0; |
| 2716 | for (unsigned I = 0; I < NumFields; ++I) { |
| 2717 | SDValue FieldAddr = Addr; |
| 2718 | if (Offset != 0) |
| 2719 | FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2720 | DAG.getIntPtrConstant(Offset, DL)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2721 | MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr, |
| 2722 | MachinePointerInfo(SV, Offset), |
| 2723 | false, false, 0); |
| 2724 | Offset += 8; |
| 2725 | } |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 2726 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op, |
| 2730 | SelectionDAG &DAG) const { |
| 2731 | SDValue Chain = Op.getOperand(0); |
| 2732 | SDValue DstPtr = Op.getOperand(1); |
| 2733 | SDValue SrcPtr = Op.getOperand(2); |
| 2734 | const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); |
| 2735 | const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2736 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2737 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2738 | return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2739 | /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false, |
Krzysztof Parzyszek | a46c36b | 2015-04-13 17:16:45 +0000 | [diff] [blame] | 2740 | /*isTailCall*/false, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2741 | MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV)); |
| 2742 | } |
| 2743 | |
| 2744 | SDValue SystemZTargetLowering:: |
| 2745 | lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { |
| 2746 | SDValue Chain = Op.getOperand(0); |
| 2747 | SDValue Size = Op.getOperand(1); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2748 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2749 | |
| 2750 | unsigned SPReg = getStackPointerRegisterToSaveRestore(); |
| 2751 | |
| 2752 | // Get a reference to the stack pointer. |
| 2753 | SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64); |
| 2754 | |
| 2755 | // Get the new stack pointer value. |
| 2756 | SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size); |
| 2757 | |
| 2758 | // Copy the new stack pointer back. |
| 2759 | Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP); |
| 2760 | |
| 2761 | // The allocated data lives above the 160 bytes allocated for the standard |
| 2762 | // frame, plus any outgoing stack arguments. We don't know how much that |
| 2763 | // amounts to yet, so emit a special ADJDYNALLOC placeholder. |
| 2764 | SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64); |
| 2765 | SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust); |
| 2766 | |
| 2767 | SDValue Ops[2] = { Result, Chain }; |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2768 | return DAG.getMergeValues(Ops, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2771 | SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op, |
| 2772 | SelectionDAG &DAG) const { |
| 2773 | EVT VT = Op.getValueType(); |
| 2774 | SDLoc DL(Op); |
| 2775 | SDValue Ops[2]; |
| 2776 | if (is32Bit(VT)) |
| 2777 | // Just do a normal 64-bit multiplication and extract the results. |
| 2778 | // We define this so that it can be used for constant division. |
| 2779 | lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0), |
| 2780 | Op.getOperand(1), Ops[1], Ops[0]); |
| 2781 | else { |
| 2782 | // Do a full 128-bit multiplication based on UMUL_LOHI64: |
| 2783 | // |
| 2784 | // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64) |
| 2785 | // |
| 2786 | // but using the fact that the upper halves are either all zeros |
| 2787 | // or all ones: |
| 2788 | // |
| 2789 | // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64) |
| 2790 | // |
| 2791 | // and grouping the right terms together since they are quicker than the |
| 2792 | // multiplication: |
| 2793 | // |
| 2794 | // (ll * rl) - (((lh & rl) + (ll & rh)) << 64) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2795 | SDValue C63 = DAG.getConstant(63, DL, MVT::i64); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2796 | SDValue LL = Op.getOperand(0); |
| 2797 | SDValue RL = Op.getOperand(1); |
| 2798 | SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63); |
| 2799 | SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63); |
| 2800 | // UMUL_LOHI64 returns the low result in the odd register and the high |
| 2801 | // result in the even register. SMUL_LOHI is defined to return the |
| 2802 | // low half first, so the results are in reverse order. |
| 2803 | lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, |
| 2804 | LL, RL, Ops[1], Ops[0]); |
| 2805 | SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH); |
| 2806 | SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL); |
| 2807 | SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL); |
| 2808 | Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum); |
| 2809 | } |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2810 | return DAG.getMergeValues(Ops, DL); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2813 | SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op, |
| 2814 | SelectionDAG &DAG) const { |
| 2815 | EVT VT = Op.getValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2816 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2817 | SDValue Ops[2]; |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2818 | if (is32Bit(VT)) |
| 2819 | // Just do a normal 64-bit multiplication and extract the results. |
| 2820 | // We define this so that it can be used for constant division. |
| 2821 | lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0), |
| 2822 | Op.getOperand(1), Ops[1], Ops[0]); |
| 2823 | else |
| 2824 | // UMUL_LOHI64 returns the low result in the odd register and the high |
| 2825 | // result in the even register. UMUL_LOHI is defined to return the |
| 2826 | // low half first, so the results are in reverse order. |
| 2827 | lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, |
| 2828 | Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2829 | return DAG.getMergeValues(Ops, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2830 | } |
| 2831 | |
| 2832 | SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op, |
| 2833 | SelectionDAG &DAG) const { |
| 2834 | SDValue Op0 = Op.getOperand(0); |
| 2835 | SDValue Op1 = Op.getOperand(1); |
| 2836 | EVT VT = Op.getValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2837 | SDLoc DL(Op); |
Richard Sandiford | e6e7885 | 2013-07-02 15:40:22 +0000 | [diff] [blame] | 2838 | unsigned Opcode; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2839 | |
| 2840 | // We use DSGF for 32-bit division. |
| 2841 | if (is32Bit(VT)) { |
| 2842 | Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0); |
Richard Sandiford | e6e7885 | 2013-07-02 15:40:22 +0000 | [diff] [blame] | 2843 | Opcode = SystemZISD::SDIVREM32; |
| 2844 | } else if (DAG.ComputeNumSignBits(Op1) > 32) { |
| 2845 | Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1); |
| 2846 | Opcode = SystemZISD::SDIVREM32; |
NAKAMURA Takumi | 10c80e7 | 2015-09-22 11:19:03 +0000 | [diff] [blame^] | 2847 | } else |
Richard Sandiford | e6e7885 | 2013-07-02 15:40:22 +0000 | [diff] [blame] | 2848 | Opcode = SystemZISD::SDIVREM64; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2849 | |
| 2850 | // DSG(F) takes a 64-bit dividend, so the even register in the GR128 |
| 2851 | // input is "don't care". The instruction returns the remainder in |
| 2852 | // the even register and the quotient in the odd register. |
| 2853 | SDValue Ops[2]; |
Richard Sandiford | e6e7885 | 2013-07-02 15:40:22 +0000 | [diff] [blame] | 2854 | lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2855 | Op0, Op1, Ops[1], Ops[0]); |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2856 | return DAG.getMergeValues(Ops, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op, |
| 2860 | SelectionDAG &DAG) const { |
| 2861 | EVT VT = Op.getValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2862 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2863 | |
| 2864 | // DL(G) uses a double-width dividend, so we need to clear the even |
| 2865 | // register in the GR128 input. The instruction returns the remainder |
| 2866 | // in the even register and the quotient in the odd register. |
| 2867 | SDValue Ops[2]; |
| 2868 | if (is32Bit(VT)) |
| 2869 | lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32, |
| 2870 | Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); |
| 2871 | else |
| 2872 | lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64, |
| 2873 | Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2874 | return DAG.getMergeValues(Ops, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const { |
| 2878 | assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation"); |
| 2879 | |
| 2880 | // Get the known-zero masks for each operand. |
| 2881 | SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) }; |
| 2882 | APInt KnownZero[2], KnownOne[2]; |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 2883 | DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]); |
| 2884 | DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2885 | |
| 2886 | // See if the upper 32 bits of one operand and the lower 32 bits of the |
| 2887 | // other are known zero. They are the low and high operands respectively. |
| 2888 | uint64_t Masks[] = { KnownZero[0].getZExtValue(), |
| 2889 | KnownZero[1].getZExtValue() }; |
| 2890 | unsigned High, Low; |
| 2891 | if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff) |
| 2892 | High = 1, Low = 0; |
| 2893 | else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff) |
| 2894 | High = 0, Low = 1; |
| 2895 | else |
| 2896 | return Op; |
| 2897 | |
| 2898 | SDValue LowOp = Ops[Low]; |
| 2899 | SDValue HighOp = Ops[High]; |
| 2900 | |
| 2901 | // If the high part is a constant, we're better off using IILH. |
| 2902 | if (HighOp.getOpcode() == ISD::Constant) |
| 2903 | return Op; |
| 2904 | |
| 2905 | // If the low part is a constant that is outside the range of LHI, |
| 2906 | // then we're better off using IILF. |
| 2907 | if (LowOp.getOpcode() == ISD::Constant) { |
| 2908 | int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue()); |
| 2909 | if (!isInt<16>(Value)) |
| 2910 | return Op; |
| 2911 | } |
| 2912 | |
| 2913 | // Check whether the high part is an AND that doesn't change the |
| 2914 | // high 32 bits and just masks out low bits. We can skip it if so. |
| 2915 | if (HighOp.getOpcode() == ISD::AND && |
| 2916 | HighOp.getOperand(1).getOpcode() == ISD::Constant) { |
Richard Sandiford | ccc2a7c | 2013-12-03 11:01:54 +0000 | [diff] [blame] | 2917 | SDValue HighOp0 = HighOp.getOperand(0); |
| 2918 | uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue(); |
| 2919 | if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff)))) |
| 2920 | HighOp = HighOp0; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | // Take advantage of the fact that all GR32 operations only change the |
| 2924 | // low 32 bits by truncating Low to an i32 and inserting it directly |
| 2925 | // using a subreg. The interesting cases are those where the truncation |
| 2926 | // can be folded. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2927 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2928 | SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp); |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 2929 | return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL, |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 2930 | MVT::i64, HighOp, Low32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2933 | SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op, |
| 2934 | SelectionDAG &DAG) const { |
| 2935 | EVT VT = Op.getValueType(); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2936 | SDLoc DL(Op); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2937 | Op = Op.getOperand(0); |
| 2938 | |
| 2939 | // Handle vector types via VPOPCT. |
| 2940 | if (VT.isVector()) { |
| 2941 | Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op); |
| 2942 | Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op); |
| 2943 | switch (VT.getVectorElementType().getSizeInBits()) { |
| 2944 | case 8: |
| 2945 | break; |
| 2946 | case 16: { |
| 2947 | Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 2948 | SDValue Shift = DAG.getConstant(8, DL, MVT::i32); |
| 2949 | SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift); |
| 2950 | Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); |
| 2951 | Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift); |
| 2952 | break; |
| 2953 | } |
| 2954 | case 32: { |
| 2955 | SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, |
| 2956 | DAG.getConstant(0, DL, MVT::i32)); |
| 2957 | Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); |
| 2958 | break; |
| 2959 | } |
| 2960 | case 64: { |
| 2961 | SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, |
| 2962 | DAG.getConstant(0, DL, MVT::i32)); |
| 2963 | Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp); |
| 2964 | Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); |
| 2965 | break; |
| 2966 | } |
| 2967 | default: |
| 2968 | llvm_unreachable("Unexpected type"); |
| 2969 | } |
| 2970 | return Op; |
| 2971 | } |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2972 | |
| 2973 | // Get the known-zero mask for the operand. |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2974 | APInt KnownZero, KnownOne; |
| 2975 | DAG.computeKnownBits(Op, KnownZero, KnownOne); |
Ulrich Weigand | 050527b | 2015-03-31 19:28:50 +0000 | [diff] [blame] | 2976 | unsigned NumSignificantBits = (~KnownZero).getActiveBits(); |
| 2977 | if (NumSignificantBits == 0) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2978 | return DAG.getConstant(0, DL, VT); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2979 | |
| 2980 | // Skip known-zero high parts of the operand. |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2981 | int64_t OrigBitSize = VT.getSizeInBits(); |
Ulrich Weigand | 050527b | 2015-03-31 19:28:50 +0000 | [diff] [blame] | 2982 | int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits); |
| 2983 | BitSize = std::min(BitSize, OrigBitSize); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2984 | |
| 2985 | // The POPCNT instruction counts the number of bits in each byte. |
| 2986 | Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op); |
| 2987 | Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op); |
| 2988 | Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); |
| 2989 | |
| 2990 | // Add up per-byte counts in a binary tree. All bits of Op at |
| 2991 | // position larger than BitSize remain zero throughout. |
| 2992 | for (int64_t I = BitSize / 2; I >= 8; I = I / 2) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2993 | SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT)); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2994 | if (BitSize != OrigBitSize) |
| 2995 | Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2996 | DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT)); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2997 | Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); |
| 2998 | } |
| 2999 | |
| 3000 | // Extract overall result from high byte. |
| 3001 | if (BitSize > 8) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3002 | Op = DAG.getNode(ISD::SRL, DL, VT, Op, |
| 3003 | DAG.getConstant(BitSize - 8, DL, VT)); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 3004 | |
| 3005 | return Op; |
| 3006 | } |
| 3007 | |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 3008 | // Op is an atomic load. Lower it into a normal volatile load. |
| 3009 | SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op, |
| 3010 | SelectionDAG &DAG) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 3011 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 3012 | return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(), |
| 3013 | Node->getChain(), Node->getBasePtr(), |
| 3014 | Node->getMemoryVT(), Node->getMemOperand()); |
| 3015 | } |
| 3016 | |
| 3017 | // Op is an atomic store. Lower it into a normal volatile store followed |
| 3018 | // by a serialization. |
| 3019 | SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op, |
| 3020 | SelectionDAG &DAG) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 3021 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 3022 | SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(), |
| 3023 | Node->getBasePtr(), Node->getMemoryVT(), |
| 3024 | Node->getMemOperand()); |
| 3025 | return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other, |
| 3026 | Chain), 0); |
| 3027 | } |
| 3028 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3029 | // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first |
| 3030 | // two into the fullword ATOMIC_LOADW_* operation given by Opcode. |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 3031 | SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op, |
| 3032 | SelectionDAG &DAG, |
| 3033 | unsigned Opcode) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 3034 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3035 | |
| 3036 | // 32-bit operations need no code outside the main loop. |
| 3037 | EVT NarrowVT = Node->getMemoryVT(); |
| 3038 | EVT WideVT = MVT::i32; |
| 3039 | if (NarrowVT == WideVT) |
| 3040 | return Op; |
| 3041 | |
| 3042 | int64_t BitSize = NarrowVT.getSizeInBits(); |
| 3043 | SDValue ChainIn = Node->getChain(); |
| 3044 | SDValue Addr = Node->getBasePtr(); |
| 3045 | SDValue Src2 = Node->getVal(); |
| 3046 | MachineMemOperand *MMO = Node->getMemOperand(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3047 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3048 | EVT PtrVT = Addr.getValueType(); |
| 3049 | |
| 3050 | // Convert atomic subtracts of constants into additions. |
| 3051 | if (Opcode == SystemZISD::ATOMIC_LOADW_SUB) |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 3052 | if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3053 | Opcode = SystemZISD::ATOMIC_LOADW_ADD; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3054 | Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
| 3057 | // Get the address of the containing word. |
| 3058 | SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3059 | DAG.getConstant(-4, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3060 | |
| 3061 | // Get the number of bits that the word must be rotated left in order |
| 3062 | // to bring the field to the top bits of a GR32. |
| 3063 | SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3064 | DAG.getConstant(3, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3065 | BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); |
| 3066 | |
| 3067 | // Get the complementing shift amount, for rotating a field in the top |
| 3068 | // bits back to its proper position. |
| 3069 | SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3070 | DAG.getConstant(0, DL, WideVT), BitShift); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3071 | |
| 3072 | // Extend the source operand to 32 bits and prepare it for the inner loop. |
| 3073 | // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other |
| 3074 | // operations require the source to be shifted in advance. (This shift |
| 3075 | // can be folded if the source is constant.) For AND and NAND, the lower |
| 3076 | // bits must be set, while for other opcodes they should be left clear. |
| 3077 | if (Opcode != SystemZISD::ATOMIC_SWAPW) |
| 3078 | Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3079 | DAG.getConstant(32 - BitSize, DL, WideVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3080 | if (Opcode == SystemZISD::ATOMIC_LOADW_AND || |
| 3081 | Opcode == SystemZISD::ATOMIC_LOADW_NAND) |
| 3082 | Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3083 | DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3084 | |
| 3085 | // Construct the ATOMIC_LOADW_* node. |
| 3086 | SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); |
| 3087 | SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3088 | DAG.getConstant(BitSize, DL, WideVT) }; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3089 | SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3090 | NarrowVT, MMO); |
| 3091 | |
| 3092 | // Rotate the result of the final CS so that the field is in the lower |
| 3093 | // bits of a GR32, then truncate it. |
| 3094 | SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3095 | DAG.getConstant(BitSize, DL, WideVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3096 | SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift); |
| 3097 | |
| 3098 | SDValue RetOps[2] = { Result, AtomicOp.getValue(1) }; |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 3099 | return DAG.getMergeValues(RetOps, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 3102 | // Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations |
Richard Sandiford | 002019a | 2013-12-24 15:22:39 +0000 | [diff] [blame] | 3103 | // into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 3104 | // operations into additions. |
| 3105 | SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op, |
| 3106 | SelectionDAG &DAG) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 3107 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 3108 | EVT MemVT = Node->getMemoryVT(); |
| 3109 | if (MemVT == MVT::i32 || MemVT == MVT::i64) { |
| 3110 | // A full-width operation. |
| 3111 | assert(Op.getValueType() == MemVT && "Mismatched VTs"); |
| 3112 | SDValue Src2 = Node->getVal(); |
| 3113 | SDValue NegSrc2; |
| 3114 | SDLoc DL(Src2); |
| 3115 | |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 3116 | if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) { |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 3117 | // Use an addition if the operand is constant and either LAA(G) is |
| 3118 | // available or the negative value is in the range of A(G)FHI. |
| 3119 | int64_t Value = (-Op2->getAPIntValue()).getSExtValue(); |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 3120 | if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1()) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3121 | NegSrc2 = DAG.getConstant(Value, DL, MemVT); |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 3122 | } else if (Subtarget.hasInterlockedAccess1()) |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 3123 | // Use LAA(G) if available. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3124 | NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT), |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 3125 | Src2); |
| 3126 | |
| 3127 | if (NegSrc2.getNode()) |
| 3128 | return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT, |
| 3129 | Node->getChain(), Node->getBasePtr(), NegSrc2, |
| 3130 | Node->getMemOperand(), Node->getOrdering(), |
| 3131 | Node->getSynchScope()); |
| 3132 | |
| 3133 | // Use the node as-is. |
| 3134 | return Op; |
| 3135 | } |
| 3136 | |
| 3137 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB); |
| 3138 | } |
| 3139 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3140 | // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two |
| 3141 | // into a fullword ATOMIC_CMP_SWAPW operation. |
| 3142 | SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op, |
| 3143 | SelectionDAG &DAG) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 3144 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3145 | |
| 3146 | // We have native support for 32-bit compare and swap. |
| 3147 | EVT NarrowVT = Node->getMemoryVT(); |
| 3148 | EVT WideVT = MVT::i32; |
| 3149 | if (NarrowVT == WideVT) |
| 3150 | return Op; |
| 3151 | |
| 3152 | int64_t BitSize = NarrowVT.getSizeInBits(); |
| 3153 | SDValue ChainIn = Node->getOperand(0); |
| 3154 | SDValue Addr = Node->getOperand(1); |
| 3155 | SDValue CmpVal = Node->getOperand(2); |
| 3156 | SDValue SwapVal = Node->getOperand(3); |
| 3157 | MachineMemOperand *MMO = Node->getMemOperand(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3158 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3159 | EVT PtrVT = Addr.getValueType(); |
| 3160 | |
| 3161 | // Get the address of the containing word. |
| 3162 | SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3163 | DAG.getConstant(-4, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3164 | |
| 3165 | // Get the number of bits that the word must be rotated left in order |
| 3166 | // to bring the field to the top bits of a GR32. |
| 3167 | SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3168 | DAG.getConstant(3, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3169 | BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); |
| 3170 | |
| 3171 | // Get the complementing shift amount, for rotating a field in the top |
| 3172 | // bits back to its proper position. |
| 3173 | SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3174 | DAG.getConstant(0, DL, WideVT), BitShift); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3175 | |
| 3176 | // Construct the ATOMIC_CMP_SWAPW node. |
| 3177 | SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); |
| 3178 | SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3179 | NegBitShift, DAG.getConstant(BitSize, DL, WideVT) }; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3180 | SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL, |
Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 3181 | VTList, Ops, NarrowVT, MMO); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3182 | return AtomicOp; |
| 3183 | } |
| 3184 | |
| 3185 | SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op, |
| 3186 | SelectionDAG &DAG) const { |
| 3187 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3188 | MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3189 | return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3190 | SystemZ::R15D, Op.getValueType()); |
| 3191 | } |
| 3192 | |
| 3193 | SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op, |
| 3194 | SelectionDAG &DAG) const { |
| 3195 | MachineFunction &MF = DAG.getMachineFunction(); |
| 3196 | MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 3197 | return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3198 | SystemZ::R15D, Op.getOperand(1)); |
| 3199 | } |
| 3200 | |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 3201 | SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op, |
| 3202 | SelectionDAG &DAG) const { |
| 3203 | bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); |
| 3204 | if (!IsData) |
| 3205 | // Just preserve the chain. |
| 3206 | return Op.getOperand(0); |
| 3207 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3208 | SDLoc DL(Op); |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 3209 | bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); |
| 3210 | unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ; |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 3211 | auto *Node = cast<MemIntrinsicSDNode>(Op.getNode()); |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 3212 | SDValue Ops[] = { |
| 3213 | Op.getOperand(0), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3214 | DAG.getConstant(Code, DL, MVT::i32), |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 3215 | Op.getOperand(1) |
| 3216 | }; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3217 | return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL, |
Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 3218 | Node->getVTList(), Ops, |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 3219 | Node->getMemoryVT(), Node->getMemOperand()); |
| 3220 | } |
| 3221 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 3222 | // Return an i32 that contains the value of CC immediately after After, |
| 3223 | // whose final operand must be MVT::Glue. |
| 3224 | static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3225 | SDLoc DL(After); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 3226 | SDValue Glue = SDValue(After, After->getNumValues() - 1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 3227 | SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue); |
| 3228 | return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM, |
| 3229 | DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32)); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | SDValue |
| 3233 | SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op, |
| 3234 | SelectionDAG &DAG) const { |
| 3235 | unsigned Opcode, CCValid; |
| 3236 | if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) { |
| 3237 | assert(Op->getNumValues() == 2 && "Expected only CC result and chain"); |
| 3238 | SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode); |
| 3239 | SDValue CC = getCCResult(DAG, Glued.getNode()); |
| 3240 | DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC); |
| 3241 | return SDValue(); |
| 3242 | } |
| 3243 | |
| 3244 | return SDValue(); |
| 3245 | } |
| 3246 | |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 3247 | SDValue |
| 3248 | SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op, |
| 3249 | SelectionDAG &DAG) const { |
| 3250 | unsigned Opcode, CCValid; |
| 3251 | if (isIntrinsicWithCC(Op, Opcode, CCValid)) { |
| 3252 | SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode); |
| 3253 | SDValue CC = getCCResult(DAG, Glued.getNode()); |
| 3254 | if (Op->getNumValues() == 1) |
| 3255 | return CC; |
| 3256 | assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result"); |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 3257 | return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued, |
| 3258 | CC); |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue(); |
| 3262 | switch (Id) { |
| 3263 | case Intrinsic::s390_vpdi: |
| 3264 | return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(), |
| 3265 | Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); |
| 3266 | |
| 3267 | case Intrinsic::s390_vperm: |
| 3268 | return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(), |
| 3269 | Op.getOperand(1), Op.getOperand(2), Op.getOperand(3)); |
| 3270 | |
| 3271 | case Intrinsic::s390_vuphb: |
| 3272 | case Intrinsic::s390_vuphh: |
| 3273 | case Intrinsic::s390_vuphf: |
| 3274 | return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(), |
| 3275 | Op.getOperand(1)); |
| 3276 | |
| 3277 | case Intrinsic::s390_vuplhb: |
| 3278 | case Intrinsic::s390_vuplhh: |
| 3279 | case Intrinsic::s390_vuplhf: |
| 3280 | return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(), |
| 3281 | Op.getOperand(1)); |
| 3282 | |
| 3283 | case Intrinsic::s390_vuplb: |
| 3284 | case Intrinsic::s390_vuplhw: |
| 3285 | case Intrinsic::s390_vuplf: |
| 3286 | return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(), |
| 3287 | Op.getOperand(1)); |
| 3288 | |
| 3289 | case Intrinsic::s390_vupllb: |
| 3290 | case Intrinsic::s390_vupllh: |
| 3291 | case Intrinsic::s390_vupllf: |
| 3292 | return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(), |
| 3293 | Op.getOperand(1)); |
| 3294 | |
| 3295 | case Intrinsic::s390_vsumb: |
| 3296 | case Intrinsic::s390_vsumh: |
| 3297 | case Intrinsic::s390_vsumgh: |
| 3298 | case Intrinsic::s390_vsumgf: |
| 3299 | case Intrinsic::s390_vsumqf: |
| 3300 | case Intrinsic::s390_vsumqg: |
| 3301 | return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(), |
| 3302 | Op.getOperand(1), Op.getOperand(2)); |
| 3303 | } |
| 3304 | |
| 3305 | return SDValue(); |
| 3306 | } |
| 3307 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3308 | namespace { |
| 3309 | // Says that SystemZISD operation Opcode can be used to perform the equivalent |
| 3310 | // of a VPERM with permute vector Bytes. If Opcode takes three operands, |
| 3311 | // Operand is the constant third operand, otherwise it is the number of |
| 3312 | // bytes in each element of the result. |
| 3313 | struct Permute { |
| 3314 | unsigned Opcode; |
| 3315 | unsigned Operand; |
| 3316 | unsigned char Bytes[SystemZ::VectorBytes]; |
| 3317 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 3318 | } |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3319 | |
| 3320 | static const Permute PermuteForms[] = { |
| 3321 | // VMRHG |
| 3322 | { SystemZISD::MERGE_HIGH, 8, |
| 3323 | { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } }, |
| 3324 | // VMRHF |
| 3325 | { SystemZISD::MERGE_HIGH, 4, |
| 3326 | { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } }, |
| 3327 | // VMRHH |
| 3328 | { SystemZISD::MERGE_HIGH, 2, |
| 3329 | { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } }, |
| 3330 | // VMRHB |
| 3331 | { SystemZISD::MERGE_HIGH, 1, |
| 3332 | { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } }, |
| 3333 | // VMRLG |
| 3334 | { SystemZISD::MERGE_LOW, 8, |
| 3335 | { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } }, |
| 3336 | // VMRLF |
| 3337 | { SystemZISD::MERGE_LOW, 4, |
| 3338 | { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }, |
| 3339 | // VMRLH |
| 3340 | { SystemZISD::MERGE_LOW, 2, |
| 3341 | { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } }, |
| 3342 | // VMRLB |
| 3343 | { SystemZISD::MERGE_LOW, 1, |
| 3344 | { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } }, |
| 3345 | // VPKG |
| 3346 | { SystemZISD::PACK, 4, |
| 3347 | { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } }, |
| 3348 | // VPKF |
| 3349 | { SystemZISD::PACK, 2, |
| 3350 | { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } }, |
| 3351 | // VPKH |
| 3352 | { SystemZISD::PACK, 1, |
| 3353 | { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } }, |
| 3354 | // VPDI V1, V2, 4 (low half of V1, high half of V2) |
| 3355 | { SystemZISD::PERMUTE_DWORDS, 4, |
| 3356 | { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } }, |
| 3357 | // VPDI V1, V2, 1 (high half of V1, low half of V2) |
| 3358 | { SystemZISD::PERMUTE_DWORDS, 1, |
| 3359 | { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } } |
| 3360 | }; |
| 3361 | |
| 3362 | // Called after matching a vector shuffle against a particular pattern. |
| 3363 | // Both the original shuffle and the pattern have two vector operands. |
| 3364 | // OpNos[0] is the operand of the original shuffle that should be used for |
| 3365 | // operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything. |
| 3366 | // OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and |
| 3367 | // set OpNo0 and OpNo1 to the shuffle operands that should actually be used |
| 3368 | // for operands 0 and 1 of the pattern. |
| 3369 | static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) { |
| 3370 | if (OpNos[0] < 0) { |
| 3371 | if (OpNos[1] < 0) |
| 3372 | return false; |
| 3373 | OpNo0 = OpNo1 = OpNos[1]; |
| 3374 | } else if (OpNos[1] < 0) { |
| 3375 | OpNo0 = OpNo1 = OpNos[0]; |
| 3376 | } else { |
| 3377 | OpNo0 = OpNos[0]; |
| 3378 | OpNo1 = OpNos[1]; |
| 3379 | } |
| 3380 | return true; |
| 3381 | } |
| 3382 | |
| 3383 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3384 | // undefined bytes. Return true if the VPERM can be implemented using P. |
| 3385 | // When returning true set OpNo0 to the VPERM operand that should be |
| 3386 | // used for operand 0 of P and likewise OpNo1 for operand 1 of P. |
| 3387 | // |
| 3388 | // For example, if swapping the VPERM operands allows P to match, OpNo0 |
| 3389 | // will be 1 and OpNo1 will be 0. If instead Bytes only refers to one |
| 3390 | // operand, but rewriting it to use two duplicated operands allows it to |
| 3391 | // match P, then OpNo0 and OpNo1 will be the same. |
| 3392 | static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P, |
| 3393 | unsigned &OpNo0, unsigned &OpNo1) { |
| 3394 | int OpNos[] = { -1, -1 }; |
| 3395 | for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) { |
| 3396 | int Elt = Bytes[I]; |
| 3397 | if (Elt >= 0) { |
| 3398 | // Make sure that the two permute vectors use the same suboperand |
| 3399 | // byte number. Only the operand numbers (the high bits) are |
| 3400 | // allowed to differ. |
| 3401 | if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1)) |
| 3402 | return false; |
| 3403 | int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes; |
| 3404 | int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes; |
| 3405 | // Make sure that the operand mappings are consistent with previous |
| 3406 | // elements. |
| 3407 | if (OpNos[ModelOpNo] == 1 - RealOpNo) |
| 3408 | return false; |
| 3409 | OpNos[ModelOpNo] = RealOpNo; |
| 3410 | } |
| 3411 | } |
| 3412 | return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); |
| 3413 | } |
| 3414 | |
| 3415 | // As above, but search for a matching permute. |
| 3416 | static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes, |
| 3417 | unsigned &OpNo0, unsigned &OpNo1) { |
| 3418 | for (auto &P : PermuteForms) |
| 3419 | if (matchPermute(Bytes, P, OpNo0, OpNo1)) |
| 3420 | return &P; |
| 3421 | return nullptr; |
| 3422 | } |
| 3423 | |
| 3424 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3425 | // undefined bytes. This permute is an operand of an outer permute. |
| 3426 | // See whether redistributing the -1 bytes gives a shuffle that can be |
| 3427 | // implemented using P. If so, set Transform to a VPERM-like permute vector |
| 3428 | // that, when applied to the result of P, gives the original permute in Bytes. |
| 3429 | static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes, |
| 3430 | const Permute &P, |
| 3431 | SmallVectorImpl<int> &Transform) { |
| 3432 | unsigned To = 0; |
| 3433 | for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) { |
| 3434 | int Elt = Bytes[From]; |
| 3435 | if (Elt < 0) |
| 3436 | // Byte number From of the result is undefined. |
| 3437 | Transform[From] = -1; |
| 3438 | else { |
| 3439 | while (P.Bytes[To] != Elt) { |
| 3440 | To += 1; |
| 3441 | if (To == SystemZ::VectorBytes) |
| 3442 | return false; |
| 3443 | } |
| 3444 | Transform[From] = To; |
| 3445 | } |
| 3446 | } |
| 3447 | return true; |
| 3448 | } |
| 3449 | |
| 3450 | // As above, but search for a matching permute. |
| 3451 | static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes, |
| 3452 | SmallVectorImpl<int> &Transform) { |
| 3453 | for (auto &P : PermuteForms) |
| 3454 | if (matchDoublePermute(Bytes, P, Transform)) |
| 3455 | return &P; |
| 3456 | return nullptr; |
| 3457 | } |
| 3458 | |
| 3459 | // Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask, |
| 3460 | // as if it had type vNi8. |
| 3461 | static void getVPermMask(ShuffleVectorSDNode *VSN, |
| 3462 | SmallVectorImpl<int> &Bytes) { |
| 3463 | EVT VT = VSN->getValueType(0); |
| 3464 | unsigned NumElements = VT.getVectorNumElements(); |
| 3465 | unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); |
| 3466 | Bytes.resize(NumElements * BytesPerElement, -1); |
| 3467 | for (unsigned I = 0; I < NumElements; ++I) { |
| 3468 | int Index = VSN->getMaskElt(I); |
| 3469 | if (Index >= 0) |
| 3470 | for (unsigned J = 0; J < BytesPerElement; ++J) |
| 3471 | Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J; |
| 3472 | } |
| 3473 | } |
| 3474 | |
| 3475 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3476 | // undefined bytes. See whether bytes [Start, Start + BytesPerElement) of |
| 3477 | // the result come from a contiguous sequence of bytes from one input. |
| 3478 | // Set Base to the selector for the first byte if so. |
| 3479 | static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start, |
| 3480 | unsigned BytesPerElement, int &Base) { |
| 3481 | Base = -1; |
| 3482 | for (unsigned I = 0; I < BytesPerElement; ++I) { |
| 3483 | if (Bytes[Start + I] >= 0) { |
| 3484 | unsigned Elem = Bytes[Start + I]; |
| 3485 | if (Base < 0) { |
| 3486 | Base = Elem - I; |
| 3487 | // Make sure the bytes would come from one input operand. |
| 3488 | if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size()) |
| 3489 | return false; |
| 3490 | } else if (unsigned(Base) != Elem - I) |
| 3491 | return false; |
| 3492 | } |
| 3493 | } |
| 3494 | return true; |
| 3495 | } |
| 3496 | |
| 3497 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3498 | // undefined bytes. Return true if it can be performed using VSLDI. |
| 3499 | // When returning true, set StartIndex to the shift amount and OpNo0 |
| 3500 | // and OpNo1 to the VPERM operands that should be used as the first |
| 3501 | // and second shift operand respectively. |
| 3502 | static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes, |
| 3503 | unsigned &StartIndex, unsigned &OpNo0, |
| 3504 | unsigned &OpNo1) { |
| 3505 | int OpNos[] = { -1, -1 }; |
| 3506 | int Shift = -1; |
| 3507 | for (unsigned I = 0; I < 16; ++I) { |
| 3508 | int Index = Bytes[I]; |
| 3509 | if (Index >= 0) { |
| 3510 | int ExpectedShift = (Index - I) % SystemZ::VectorBytes; |
| 3511 | int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes; |
| 3512 | int RealOpNo = unsigned(Index) / SystemZ::VectorBytes; |
| 3513 | if (Shift < 0) |
| 3514 | Shift = ExpectedShift; |
| 3515 | else if (Shift != ExpectedShift) |
| 3516 | return false; |
| 3517 | // Make sure that the operand mappings are consistent with previous |
| 3518 | // elements. |
| 3519 | if (OpNos[ModelOpNo] == 1 - RealOpNo) |
| 3520 | return false; |
| 3521 | OpNos[ModelOpNo] = RealOpNo; |
| 3522 | } |
| 3523 | } |
| 3524 | StartIndex = Shift; |
| 3525 | return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); |
| 3526 | } |
| 3527 | |
| 3528 | // Create a node that performs P on operands Op0 and Op1, casting the |
| 3529 | // operands to the appropriate type. The type of the result is determined by P. |
| 3530 | static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL, |
| 3531 | const Permute &P, SDValue Op0, SDValue Op1) { |
| 3532 | // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input |
| 3533 | // elements of a PACK are twice as wide as the outputs. |
| 3534 | unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 : |
| 3535 | P.Opcode == SystemZISD::PACK ? P.Operand * 2 : |
| 3536 | P.Operand); |
| 3537 | // Cast both operands to the appropriate type. |
| 3538 | MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8), |
| 3539 | SystemZ::VectorBytes / InBytes); |
| 3540 | Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0); |
| 3541 | Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1); |
| 3542 | SDValue Op; |
| 3543 | if (P.Opcode == SystemZISD::PERMUTE_DWORDS) { |
| 3544 | SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32); |
| 3545 | Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2); |
| 3546 | } else if (P.Opcode == SystemZISD::PACK) { |
| 3547 | MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8), |
| 3548 | SystemZ::VectorBytes / P.Operand); |
| 3549 | Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1); |
| 3550 | } else { |
| 3551 | Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1); |
| 3552 | } |
| 3553 | return Op; |
| 3554 | } |
| 3555 | |
| 3556 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3557 | // undefined bytes. Implement it on operands Ops[0] and Ops[1] using |
| 3558 | // VSLDI or VPERM. |
| 3559 | static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops, |
| 3560 | const SmallVectorImpl<int> &Bytes) { |
| 3561 | for (unsigned I = 0; I < 2; ++I) |
| 3562 | Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]); |
| 3563 | |
| 3564 | // First see whether VSLDI can be used. |
| 3565 | unsigned StartIndex, OpNo0, OpNo1; |
| 3566 | if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1)) |
| 3567 | return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0], |
| 3568 | Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32)); |
| 3569 | |
| 3570 | // Fall back on VPERM. Construct an SDNode for the permute vector. |
| 3571 | SDValue IndexNodes[SystemZ::VectorBytes]; |
| 3572 | for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) |
| 3573 | if (Bytes[I] >= 0) |
| 3574 | IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32); |
| 3575 | else |
| 3576 | IndexNodes[I] = DAG.getUNDEF(MVT::i32); |
| 3577 | SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes); |
| 3578 | return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2); |
| 3579 | } |
| 3580 | |
| 3581 | namespace { |
| 3582 | // Describes a general N-operand vector shuffle. |
| 3583 | struct GeneralShuffle { |
| 3584 | GeneralShuffle(EVT vt) : VT(vt) {} |
| 3585 | void addUndef(); |
| 3586 | void add(SDValue, unsigned); |
| 3587 | SDValue getNode(SelectionDAG &, SDLoc); |
| 3588 | |
| 3589 | // The operands of the shuffle. |
| 3590 | SmallVector<SDValue, SystemZ::VectorBytes> Ops; |
| 3591 | |
| 3592 | // Index I is -1 if byte I of the result is undefined. Otherwise the |
| 3593 | // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand |
| 3594 | // Bytes[I] / SystemZ::VectorBytes. |
| 3595 | SmallVector<int, SystemZ::VectorBytes> Bytes; |
| 3596 | |
| 3597 | // The type of the shuffle result. |
| 3598 | EVT VT; |
| 3599 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 3600 | } |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3601 | |
| 3602 | // Add an extra undefined element to the shuffle. |
| 3603 | void GeneralShuffle::addUndef() { |
| 3604 | unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); |
| 3605 | for (unsigned I = 0; I < BytesPerElement; ++I) |
| 3606 | Bytes.push_back(-1); |
| 3607 | } |
| 3608 | |
| 3609 | // Add an extra element to the shuffle, taking it from element Elem of Op. |
| 3610 | // A null Op indicates a vector input whose value will be calculated later; |
| 3611 | // there is at most one such input per shuffle and it always has the same |
| 3612 | // type as the result. |
| 3613 | void GeneralShuffle::add(SDValue Op, unsigned Elem) { |
| 3614 | unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); |
| 3615 | |
| 3616 | // The source vector can have wider elements than the result, |
| 3617 | // either through an explicit TRUNCATE or because of type legalization. |
| 3618 | // We want the least significant part. |
| 3619 | EVT FromVT = Op.getNode() ? Op.getValueType() : VT; |
| 3620 | unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize(); |
| 3621 | assert(FromBytesPerElement >= BytesPerElement && |
| 3622 | "Invalid EXTRACT_VECTOR_ELT"); |
| 3623 | unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes + |
| 3624 | (FromBytesPerElement - BytesPerElement)); |
| 3625 | |
| 3626 | // Look through things like shuffles and bitcasts. |
| 3627 | while (Op.getNode()) { |
| 3628 | if (Op.getOpcode() == ISD::BITCAST) |
| 3629 | Op = Op.getOperand(0); |
| 3630 | else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) { |
| 3631 | // See whether the bytes we need come from a contiguous part of one |
| 3632 | // operand. |
| 3633 | SmallVector<int, SystemZ::VectorBytes> OpBytes; |
| 3634 | getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes); |
| 3635 | int NewByte; |
| 3636 | if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte)) |
| 3637 | break; |
| 3638 | if (NewByte < 0) { |
| 3639 | addUndef(); |
| 3640 | return; |
| 3641 | } |
| 3642 | Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes); |
| 3643 | Byte = unsigned(NewByte) % SystemZ::VectorBytes; |
| 3644 | } else if (Op.getOpcode() == ISD::UNDEF) { |
| 3645 | addUndef(); |
| 3646 | return; |
| 3647 | } else |
| 3648 | break; |
| 3649 | } |
| 3650 | |
| 3651 | // Make sure that the source of the extraction is in Ops. |
| 3652 | unsigned OpNo = 0; |
| 3653 | for (; OpNo < Ops.size(); ++OpNo) |
| 3654 | if (Ops[OpNo] == Op) |
| 3655 | break; |
| 3656 | if (OpNo == Ops.size()) |
| 3657 | Ops.push_back(Op); |
| 3658 | |
| 3659 | // Add the element to Bytes. |
| 3660 | unsigned Base = OpNo * SystemZ::VectorBytes + Byte; |
| 3661 | for (unsigned I = 0; I < BytesPerElement; ++I) |
| 3662 | Bytes.push_back(Base + I); |
| 3663 | } |
| 3664 | |
| 3665 | // Return SDNodes for the completed shuffle. |
| 3666 | SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) { |
| 3667 | assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector"); |
| 3668 | |
| 3669 | if (Ops.size() == 0) |
| 3670 | return DAG.getUNDEF(VT); |
| 3671 | |
| 3672 | // Make sure that there are at least two shuffle operands. |
| 3673 | if (Ops.size() == 1) |
| 3674 | Ops.push_back(DAG.getUNDEF(MVT::v16i8)); |
| 3675 | |
| 3676 | // Create a tree of shuffles, deferring root node until after the loop. |
| 3677 | // Try to redistribute the undefined elements of non-root nodes so that |
| 3678 | // the non-root shuffles match something like a pack or merge, then adjust |
| 3679 | // the parent node's permute vector to compensate for the new order. |
| 3680 | // Among other things, this copes with vectors like <2 x i16> that were |
| 3681 | // padded with undefined elements during type legalization. |
| 3682 | // |
| 3683 | // In the best case this redistribution will lead to the whole tree |
| 3684 | // using packs and merges. It should rarely be a loss in other cases. |
| 3685 | unsigned Stride = 1; |
| 3686 | for (; Stride * 2 < Ops.size(); Stride *= 2) { |
| 3687 | for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) { |
| 3688 | SDValue SubOps[] = { Ops[I], Ops[I + Stride] }; |
| 3689 | |
| 3690 | // Create a mask for just these two operands. |
| 3691 | SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes); |
| 3692 | for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { |
| 3693 | unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes; |
| 3694 | unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes; |
| 3695 | if (OpNo == I) |
| 3696 | NewBytes[J] = Byte; |
| 3697 | else if (OpNo == I + Stride) |
| 3698 | NewBytes[J] = SystemZ::VectorBytes + Byte; |
| 3699 | else |
| 3700 | NewBytes[J] = -1; |
| 3701 | } |
| 3702 | // See if it would be better to reorganize NewMask to avoid using VPERM. |
| 3703 | SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes); |
| 3704 | if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) { |
| 3705 | Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]); |
| 3706 | // Applying NewBytesMap to Ops[I] gets back to NewBytes. |
| 3707 | for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { |
| 3708 | if (NewBytes[J] >= 0) { |
| 3709 | assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes && |
| 3710 | "Invalid double permute"); |
| 3711 | Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J]; |
| 3712 | } else |
| 3713 | assert(NewBytesMap[J] < 0 && "Invalid double permute"); |
| 3714 | } |
| 3715 | } else { |
| 3716 | // Just use NewBytes on the operands. |
| 3717 | Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes); |
| 3718 | for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) |
| 3719 | if (NewBytes[J] >= 0) |
| 3720 | Bytes[J] = I * SystemZ::VectorBytes + J; |
| 3721 | } |
| 3722 | } |
| 3723 | } |
| 3724 | |
| 3725 | // Now we just have 2 inputs. Put the second operand in Ops[1]. |
| 3726 | if (Stride > 1) { |
| 3727 | Ops[1] = Ops[Stride]; |
| 3728 | for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) |
| 3729 | if (Bytes[I] >= int(SystemZ::VectorBytes)) |
| 3730 | Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes; |
| 3731 | } |
| 3732 | |
| 3733 | // Look for an instruction that can do the permute without resorting |
| 3734 | // to VPERM. |
| 3735 | unsigned OpNo0, OpNo1; |
| 3736 | SDValue Op; |
| 3737 | if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1)) |
| 3738 | Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]); |
| 3739 | else |
| 3740 | Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes); |
| 3741 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 3742 | } |
| 3743 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 3744 | // Return true if the given BUILD_VECTOR is a scalar-to-vector conversion. |
| 3745 | static bool isScalarToVector(SDValue Op) { |
| 3746 | for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I) |
| 3747 | if (Op.getOperand(I).getOpcode() != ISD::UNDEF) |
| 3748 | return false; |
| 3749 | return true; |
| 3750 | } |
| 3751 | |
| 3752 | // Return a vector of type VT that contains Value in the first element. |
| 3753 | // The other elements don't matter. |
| 3754 | static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT, |
| 3755 | SDValue Value) { |
| 3756 | // If we have a constant, replicate it to all elements and let the |
| 3757 | // BUILD_VECTOR lowering take care of it. |
| 3758 | if (Value.getOpcode() == ISD::Constant || |
| 3759 | Value.getOpcode() == ISD::ConstantFP) { |
| 3760 | SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value); |
| 3761 | return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops); |
| 3762 | } |
| 3763 | if (Value.getOpcode() == ISD::UNDEF) |
| 3764 | return DAG.getUNDEF(VT); |
| 3765 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value); |
| 3766 | } |
| 3767 | |
| 3768 | // Return a vector of type VT in which Op0 is in element 0 and Op1 is in |
| 3769 | // element 1. Used for cases in which replication is cheap. |
| 3770 | static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT, |
| 3771 | SDValue Op0, SDValue Op1) { |
| 3772 | if (Op0.getOpcode() == ISD::UNDEF) { |
| 3773 | if (Op1.getOpcode() == ISD::UNDEF) |
| 3774 | return DAG.getUNDEF(VT); |
| 3775 | return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1); |
| 3776 | } |
| 3777 | if (Op1.getOpcode() == ISD::UNDEF) |
| 3778 | return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0); |
| 3779 | return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT, |
| 3780 | buildScalarToVector(DAG, DL, VT, Op0), |
| 3781 | buildScalarToVector(DAG, DL, VT, Op1)); |
| 3782 | } |
| 3783 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3784 | // Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64 |
| 3785 | // vector for them. |
| 3786 | static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0, |
| 3787 | SDValue Op1) { |
| 3788 | if (Op0.getOpcode() == ISD::UNDEF && Op1.getOpcode() == ISD::UNDEF) |
| 3789 | return DAG.getUNDEF(MVT::v2i64); |
| 3790 | // If one of the two inputs is undefined then replicate the other one, |
| 3791 | // in order to avoid using another register unnecessarily. |
| 3792 | if (Op0.getOpcode() == ISD::UNDEF) |
| 3793 | Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); |
| 3794 | else if (Op1.getOpcode() == ISD::UNDEF) |
| 3795 | Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); |
| 3796 | else { |
| 3797 | Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); |
| 3798 | Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); |
| 3799 | } |
| 3800 | return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1); |
| 3801 | } |
| 3802 | |
| 3803 | // Try to represent constant BUILD_VECTOR node BVN using a |
| 3804 | // SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask |
| 3805 | // on success. |
| 3806 | static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) { |
| 3807 | EVT ElemVT = BVN->getValueType(0).getVectorElementType(); |
| 3808 | unsigned BytesPerElement = ElemVT.getStoreSize(); |
| 3809 | for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) { |
| 3810 | SDValue Op = BVN->getOperand(I); |
| 3811 | if (Op.getOpcode() != ISD::UNDEF) { |
| 3812 | uint64_t Value; |
| 3813 | if (Op.getOpcode() == ISD::Constant) |
| 3814 | Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue(); |
| 3815 | else if (Op.getOpcode() == ISD::ConstantFP) |
| 3816 | Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt() |
| 3817 | .getZExtValue()); |
| 3818 | else |
| 3819 | return false; |
| 3820 | for (unsigned J = 0; J < BytesPerElement; ++J) { |
| 3821 | uint64_t Byte = (Value >> (J * 8)) & 0xff; |
| 3822 | if (Byte == 0xff) |
Aaron Ballman | 2a3aa1f24 | 2015-05-11 12:45:53 +0000 | [diff] [blame] | 3823 | Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3824 | else if (Byte != 0) |
| 3825 | return false; |
| 3826 | } |
| 3827 | } |
| 3828 | } |
| 3829 | return true; |
| 3830 | } |
| 3831 | |
| 3832 | // Try to load a vector constant in which BitsPerElement-bit value Value |
| 3833 | // is replicated to fill the vector. VT is the type of the resulting |
| 3834 | // constant, which may have elements of a different size from BitsPerElement. |
| 3835 | // Return the SDValue of the constant on success, otherwise return |
| 3836 | // an empty value. |
| 3837 | static SDValue tryBuildVectorReplicate(SelectionDAG &DAG, |
| 3838 | const SystemZInstrInfo *TII, |
| 3839 | SDLoc DL, EVT VT, uint64_t Value, |
| 3840 | unsigned BitsPerElement) { |
| 3841 | // Signed 16-bit values can be replicated using VREPI. |
| 3842 | int64_t SignedValue = SignExtend64(Value, BitsPerElement); |
| 3843 | if (isInt<16>(SignedValue)) { |
| 3844 | MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement), |
| 3845 | SystemZ::VectorBits / BitsPerElement); |
| 3846 | SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT, |
| 3847 | DAG.getConstant(SignedValue, DL, MVT::i32)); |
| 3848 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 3849 | } |
| 3850 | // See whether rotating the constant left some N places gives a value that |
| 3851 | // is one less than a power of 2 (i.e. all zeros followed by all ones). |
| 3852 | // If so we can use VGM. |
| 3853 | unsigned Start, End; |
| 3854 | if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) { |
| 3855 | // isRxSBGMask returns the bit numbers for a full 64-bit value, |
| 3856 | // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to |
| 3857 | // bit numbers for an BitsPerElement value, so that 0 denotes |
| 3858 | // 1 << (BitsPerElement-1). |
| 3859 | Start -= 64 - BitsPerElement; |
| 3860 | End -= 64 - BitsPerElement; |
| 3861 | MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement), |
| 3862 | SystemZ::VectorBits / BitsPerElement); |
| 3863 | SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT, |
| 3864 | DAG.getConstant(Start, DL, MVT::i32), |
| 3865 | DAG.getConstant(End, DL, MVT::i32)); |
| 3866 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 3867 | } |
| 3868 | return SDValue(); |
| 3869 | } |
| 3870 | |
| 3871 | // If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually |
| 3872 | // better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for |
| 3873 | // the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR |
| 3874 | // would benefit from this representation and return it if so. |
| 3875 | static SDValue tryBuildVectorShuffle(SelectionDAG &DAG, |
| 3876 | BuildVectorSDNode *BVN) { |
| 3877 | EVT VT = BVN->getValueType(0); |
| 3878 | unsigned NumElements = VT.getVectorNumElements(); |
| 3879 | |
| 3880 | // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation |
| 3881 | // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still |
| 3882 | // need a BUILD_VECTOR, add an additional placeholder operand for that |
| 3883 | // BUILD_VECTOR and store its operands in ResidueOps. |
| 3884 | GeneralShuffle GS(VT); |
| 3885 | SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps; |
| 3886 | bool FoundOne = false; |
| 3887 | for (unsigned I = 0; I < NumElements; ++I) { |
| 3888 | SDValue Op = BVN->getOperand(I); |
| 3889 | if (Op.getOpcode() == ISD::TRUNCATE) |
| 3890 | Op = Op.getOperand(0); |
| 3891 | if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 3892 | Op.getOperand(1).getOpcode() == ISD::Constant) { |
| 3893 | unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 3894 | GS.add(Op.getOperand(0), Elem); |
| 3895 | FoundOne = true; |
| 3896 | } else if (Op.getOpcode() == ISD::UNDEF) { |
| 3897 | GS.addUndef(); |
| 3898 | } else { |
| 3899 | GS.add(SDValue(), ResidueOps.size()); |
Ulrich Weigand | e861e64 | 2015-09-15 14:27:46 +0000 | [diff] [blame] | 3900 | ResidueOps.push_back(BVN->getOperand(I)); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | // Nothing to do if there are no EXTRACT_VECTOR_ELTs. |
| 3905 | if (!FoundOne) |
| 3906 | return SDValue(); |
| 3907 | |
| 3908 | // Create the BUILD_VECTOR for the remaining elements, if any. |
| 3909 | if (!ResidueOps.empty()) { |
| 3910 | while (ResidueOps.size() < NumElements) |
| 3911 | ResidueOps.push_back(DAG.getUNDEF(VT.getVectorElementType())); |
| 3912 | for (auto &Op : GS.Ops) { |
| 3913 | if (!Op.getNode()) { |
| 3914 | Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps); |
| 3915 | break; |
| 3916 | } |
| 3917 | } |
| 3918 | } |
| 3919 | return GS.getNode(DAG, SDLoc(BVN)); |
| 3920 | } |
| 3921 | |
| 3922 | // Combine GPR scalar values Elems into a vector of type VT. |
| 3923 | static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT, |
| 3924 | SmallVectorImpl<SDValue> &Elems) { |
| 3925 | // See whether there is a single replicated value. |
| 3926 | SDValue Single; |
| 3927 | unsigned int NumElements = Elems.size(); |
| 3928 | unsigned int Count = 0; |
| 3929 | for (auto Elem : Elems) { |
| 3930 | if (Elem.getOpcode() != ISD::UNDEF) { |
| 3931 | if (!Single.getNode()) |
| 3932 | Single = Elem; |
| 3933 | else if (Elem != Single) { |
| 3934 | Single = SDValue(); |
| 3935 | break; |
| 3936 | } |
| 3937 | Count += 1; |
| 3938 | } |
| 3939 | } |
| 3940 | // There are three cases here: |
| 3941 | // |
| 3942 | // - if the only defined element is a loaded one, the best sequence |
| 3943 | // is a replicating load. |
| 3944 | // |
| 3945 | // - otherwise, if the only defined element is an i64 value, we will |
| 3946 | // end up with the same VLVGP sequence regardless of whether we short-cut |
| 3947 | // for replication or fall through to the later code. |
| 3948 | // |
| 3949 | // - otherwise, if the only defined element is an i32 or smaller value, |
| 3950 | // we would need 2 instructions to replicate it: VLVGP followed by VREPx. |
| 3951 | // This is only a win if the single defined element is used more than once. |
| 3952 | // In other cases we're better off using a single VLVGx. |
| 3953 | if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD)) |
| 3954 | return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single); |
| 3955 | |
| 3956 | // The best way of building a v2i64 from two i64s is to use VLVGP. |
| 3957 | if (VT == MVT::v2i64) |
| 3958 | return joinDwords(DAG, DL, Elems[0], Elems[1]); |
| 3959 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 3960 | // Use a 64-bit merge high to combine two doubles. |
| 3961 | if (VT == MVT::v2f64) |
| 3962 | return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); |
| 3963 | |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 3964 | // Build v4f32 values directly from the FPRs: |
| 3965 | // |
| 3966 | // <Axxx> <Bxxx> <Cxxxx> <Dxxx> |
| 3967 | // V V VMRHF |
| 3968 | // <ABxx> <CDxx> |
| 3969 | // V VMRHG |
| 3970 | // <ABCD> |
| 3971 | if (VT == MVT::v4f32) { |
| 3972 | SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); |
| 3973 | SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]); |
| 3974 | // Avoid unnecessary undefs by reusing the other operand. |
| 3975 | if (Op01.getOpcode() == ISD::UNDEF) |
| 3976 | Op01 = Op23; |
| 3977 | else if (Op23.getOpcode() == ISD::UNDEF) |
| 3978 | Op23 = Op01; |
| 3979 | // Merging identical replications is a no-op. |
| 3980 | if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23) |
| 3981 | return Op01; |
| 3982 | Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01); |
| 3983 | Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23); |
| 3984 | SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH, |
| 3985 | DL, MVT::v2i64, Op01, Op23); |
| 3986 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 3987 | } |
| 3988 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3989 | // Collect the constant terms. |
| 3990 | SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue()); |
| 3991 | SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false); |
| 3992 | |
| 3993 | unsigned NumConstants = 0; |
| 3994 | for (unsigned I = 0; I < NumElements; ++I) { |
| 3995 | SDValue Elem = Elems[I]; |
| 3996 | if (Elem.getOpcode() == ISD::Constant || |
| 3997 | Elem.getOpcode() == ISD::ConstantFP) { |
| 3998 | NumConstants += 1; |
| 3999 | Constants[I] = Elem; |
| 4000 | Done[I] = true; |
| 4001 | } |
| 4002 | } |
| 4003 | // If there was at least one constant, fill in the other elements of |
| 4004 | // Constants with undefs to get a full vector constant and use that |
| 4005 | // as the starting point. |
| 4006 | SDValue Result; |
| 4007 | if (NumConstants > 0) { |
| 4008 | for (unsigned I = 0; I < NumElements; ++I) |
| 4009 | if (!Constants[I].getNode()) |
| 4010 | Constants[I] = DAG.getUNDEF(Elems[I].getValueType()); |
| 4011 | Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants); |
| 4012 | } else { |
| 4013 | // Otherwise try to use VLVGP to start the sequence in order to |
| 4014 | // avoid a false dependency on any previous contents of the vector |
| 4015 | // register. This only makes sense if one of the associated elements |
| 4016 | // is defined. |
| 4017 | unsigned I1 = NumElements / 2 - 1; |
| 4018 | unsigned I2 = NumElements - 1; |
| 4019 | bool Def1 = (Elems[I1].getOpcode() != ISD::UNDEF); |
| 4020 | bool Def2 = (Elems[I2].getOpcode() != ISD::UNDEF); |
| 4021 | if (Def1 || Def2) { |
| 4022 | SDValue Elem1 = Elems[Def1 ? I1 : I2]; |
| 4023 | SDValue Elem2 = Elems[Def2 ? I2 : I1]; |
| 4024 | Result = DAG.getNode(ISD::BITCAST, DL, VT, |
| 4025 | joinDwords(DAG, DL, Elem1, Elem2)); |
| 4026 | Done[I1] = true; |
| 4027 | Done[I2] = true; |
| 4028 | } else |
| 4029 | Result = DAG.getUNDEF(VT); |
| 4030 | } |
| 4031 | |
| 4032 | // Use VLVGx to insert the other elements. |
| 4033 | for (unsigned I = 0; I < NumElements; ++I) |
| 4034 | if (!Done[I] && Elems[I].getOpcode() != ISD::UNDEF) |
| 4035 | Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I], |
| 4036 | DAG.getConstant(I, DL, MVT::i32)); |
| 4037 | return Result; |
| 4038 | } |
| 4039 | |
| 4040 | SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op, |
| 4041 | SelectionDAG &DAG) const { |
| 4042 | const SystemZInstrInfo *TII = |
| 4043 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
| 4044 | auto *BVN = cast<BuildVectorSDNode>(Op.getNode()); |
| 4045 | SDLoc DL(Op); |
| 4046 | EVT VT = Op.getValueType(); |
| 4047 | |
| 4048 | if (BVN->isConstant()) { |
| 4049 | // Try using VECTOR GENERATE BYTE MASK. This is the architecturally- |
| 4050 | // preferred way of creating all-zero and all-one vectors so give it |
| 4051 | // priority over other methods below. |
| 4052 | uint64_t Mask = 0; |
| 4053 | if (tryBuildVectorByteMask(BVN, Mask)) { |
| 4054 | SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, |
| 4055 | DAG.getConstant(Mask, DL, MVT::i32)); |
| 4056 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 4057 | } |
| 4058 | |
| 4059 | // Try using some form of replication. |
| 4060 | APInt SplatBits, SplatUndef; |
| 4061 | unsigned SplatBitSize; |
| 4062 | bool HasAnyUndefs; |
| 4063 | if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, |
| 4064 | 8, true) && |
| 4065 | SplatBitSize <= 64) { |
| 4066 | // First try assuming that any undefined bits above the highest set bit |
| 4067 | // and below the lowest set bit are 1s. This increases the likelihood of |
| 4068 | // being able to use a sign-extended element value in VECTOR REPLICATE |
| 4069 | // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK. |
| 4070 | uint64_t SplatBitsZ = SplatBits.getZExtValue(); |
| 4071 | uint64_t SplatUndefZ = SplatUndef.getZExtValue(); |
| 4072 | uint64_t Lower = (SplatUndefZ |
| 4073 | & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1)); |
| 4074 | uint64_t Upper = (SplatUndefZ |
| 4075 | & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1)); |
| 4076 | uint64_t Value = SplatBitsZ | Upper | Lower; |
| 4077 | SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, |
| 4078 | SplatBitSize); |
| 4079 | if (Op.getNode()) |
| 4080 | return Op; |
| 4081 | |
| 4082 | // Now try assuming that any undefined bits between the first and |
| 4083 | // last defined set bits are set. This increases the chances of |
| 4084 | // using a non-wraparound mask. |
| 4085 | uint64_t Middle = SplatUndefZ & ~Upper & ~Lower; |
| 4086 | Value = SplatBitsZ | Middle; |
| 4087 | Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize); |
| 4088 | if (Op.getNode()) |
| 4089 | return Op; |
| 4090 | } |
| 4091 | |
| 4092 | // Fall back to loading it from memory. |
| 4093 | return SDValue(); |
| 4094 | } |
| 4095 | |
| 4096 | // See if we should use shuffles to construct the vector from other vectors. |
| 4097 | SDValue Res = tryBuildVectorShuffle(DAG, BVN); |
| 4098 | if (Res.getNode()) |
| 4099 | return Res; |
| 4100 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 4101 | // Detect SCALAR_TO_VECTOR conversions. |
| 4102 | if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op)) |
| 4103 | return buildScalarToVector(DAG, DL, VT, Op.getOperand(0)); |
| 4104 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4105 | // Otherwise use buildVector to build the vector up from GPRs. |
| 4106 | unsigned NumElements = Op.getNumOperands(); |
| 4107 | SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements); |
| 4108 | for (unsigned I = 0; I < NumElements; ++I) |
| 4109 | Ops[I] = Op.getOperand(I); |
| 4110 | return buildVector(DAG, DL, VT, Ops); |
| 4111 | } |
| 4112 | |
| 4113 | SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op, |
| 4114 | SelectionDAG &DAG) const { |
| 4115 | auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode()); |
| 4116 | SDLoc DL(Op); |
| 4117 | EVT VT = Op.getValueType(); |
| 4118 | unsigned NumElements = VT.getVectorNumElements(); |
| 4119 | |
| 4120 | if (VSN->isSplat()) { |
| 4121 | SDValue Op0 = Op.getOperand(0); |
| 4122 | unsigned Index = VSN->getSplatIndex(); |
| 4123 | assert(Index < VT.getVectorNumElements() && |
| 4124 | "Splat index should be defined and in first operand"); |
| 4125 | // See whether the value we're splatting is directly available as a scalar. |
| 4126 | if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) || |
| 4127 | Op0.getOpcode() == ISD::BUILD_VECTOR) |
| 4128 | return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index)); |
| 4129 | // Otherwise keep it as a vector-to-vector operation. |
| 4130 | return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0), |
| 4131 | DAG.getConstant(Index, DL, MVT::i32)); |
| 4132 | } |
| 4133 | |
| 4134 | GeneralShuffle GS(VT); |
| 4135 | for (unsigned I = 0; I < NumElements; ++I) { |
| 4136 | int Elt = VSN->getMaskElt(I); |
| 4137 | if (Elt < 0) |
| 4138 | GS.addUndef(); |
| 4139 | else |
| 4140 | GS.add(Op.getOperand(unsigned(Elt) / NumElements), |
| 4141 | unsigned(Elt) % NumElements); |
| 4142 | } |
| 4143 | return GS.getNode(DAG, SDLoc(VSN)); |
| 4144 | } |
| 4145 | |
| 4146 | SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op, |
| 4147 | SelectionDAG &DAG) const { |
| 4148 | SDLoc DL(Op); |
| 4149 | // Just insert the scalar into element 0 of an undefined vector. |
| 4150 | return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, |
| 4151 | Op.getValueType(), DAG.getUNDEF(Op.getValueType()), |
| 4152 | Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32)); |
| 4153 | } |
| 4154 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 4155 | SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op, |
| 4156 | SelectionDAG &DAG) const { |
| 4157 | // Handle insertions of floating-point values. |
| 4158 | SDLoc DL(Op); |
| 4159 | SDValue Op0 = Op.getOperand(0); |
| 4160 | SDValue Op1 = Op.getOperand(1); |
| 4161 | SDValue Op2 = Op.getOperand(2); |
| 4162 | EVT VT = Op.getValueType(); |
| 4163 | |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 4164 | // Insertions into constant indices of a v2f64 can be done using VPDI. |
| 4165 | // However, if the inserted value is a bitcast or a constant then it's |
| 4166 | // better to use GPRs, as below. |
| 4167 | if (VT == MVT::v2f64 && |
| 4168 | Op1.getOpcode() != ISD::BITCAST && |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 4169 | Op1.getOpcode() != ISD::ConstantFP && |
| 4170 | Op2.getOpcode() == ISD::Constant) { |
| 4171 | uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue(); |
| 4172 | unsigned Mask = VT.getVectorNumElements() - 1; |
| 4173 | if (Index <= Mask) |
| 4174 | return Op; |
| 4175 | } |
| 4176 | |
| 4177 | // Otherwise bitcast to the equivalent integer form and insert via a GPR. |
| 4178 | MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits()); |
| 4179 | MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements()); |
| 4180 | SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT, |
| 4181 | DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), |
| 4182 | DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2); |
| 4183 | return DAG.getNode(ISD::BITCAST, DL, VT, Res); |
| 4184 | } |
| 4185 | |
| 4186 | SDValue |
| 4187 | SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op, |
| 4188 | SelectionDAG &DAG) const { |
| 4189 | // Handle extractions of floating-point values. |
| 4190 | SDLoc DL(Op); |
| 4191 | SDValue Op0 = Op.getOperand(0); |
| 4192 | SDValue Op1 = Op.getOperand(1); |
| 4193 | EVT VT = Op.getValueType(); |
| 4194 | EVT VecVT = Op0.getValueType(); |
| 4195 | |
| 4196 | // Extractions of constant indices can be done directly. |
| 4197 | if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) { |
| 4198 | uint64_t Index = CIndexN->getZExtValue(); |
| 4199 | unsigned Mask = VecVT.getVectorNumElements() - 1; |
| 4200 | if (Index <= Mask) |
| 4201 | return Op; |
| 4202 | } |
| 4203 | |
| 4204 | // Otherwise bitcast to the equivalent integer form and extract via a GPR. |
| 4205 | MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits()); |
| 4206 | MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements()); |
| 4207 | SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT, |
| 4208 | DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1); |
| 4209 | return DAG.getNode(ISD::BITCAST, DL, VT, Res); |
| 4210 | } |
| 4211 | |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 4212 | SDValue |
| 4213 | SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG, |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 4214 | unsigned UnpackHigh) const { |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 4215 | SDValue PackedOp = Op.getOperand(0); |
| 4216 | EVT OutVT = Op.getValueType(); |
| 4217 | EVT InVT = PackedOp.getValueType(); |
| 4218 | unsigned ToBits = OutVT.getVectorElementType().getSizeInBits(); |
| 4219 | unsigned FromBits = InVT.getVectorElementType().getSizeInBits(); |
| 4220 | do { |
| 4221 | FromBits *= 2; |
| 4222 | EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits), |
| 4223 | SystemZ::VectorBits / FromBits); |
| 4224 | PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp); |
| 4225 | } while (FromBits != ToBits); |
| 4226 | return PackedOp; |
| 4227 | } |
| 4228 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4229 | SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG, |
| 4230 | unsigned ByScalar) const { |
| 4231 | // Look for cases where a vector shift can use the *_BY_SCALAR form. |
| 4232 | SDValue Op0 = Op.getOperand(0); |
| 4233 | SDValue Op1 = Op.getOperand(1); |
| 4234 | SDLoc DL(Op); |
| 4235 | EVT VT = Op.getValueType(); |
| 4236 | unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits(); |
| 4237 | |
| 4238 | // See whether the shift vector is a splat represented as BUILD_VECTOR. |
| 4239 | if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) { |
| 4240 | APInt SplatBits, SplatUndef; |
| 4241 | unsigned SplatBitSize; |
| 4242 | bool HasAnyUndefs; |
| 4243 | // Check for constant splats. Use ElemBitSize as the minimum element |
| 4244 | // width and reject splats that need wider elements. |
| 4245 | if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, |
| 4246 | ElemBitSize, true) && |
| 4247 | SplatBitSize == ElemBitSize) { |
| 4248 | SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff, |
| 4249 | DL, MVT::i32); |
| 4250 | return DAG.getNode(ByScalar, DL, VT, Op0, Shift); |
| 4251 | } |
| 4252 | // Check for variable splats. |
| 4253 | BitVector UndefElements; |
| 4254 | SDValue Splat = BVN->getSplatValue(&UndefElements); |
| 4255 | if (Splat) { |
| 4256 | // Since i32 is the smallest legal type, we either need a no-op |
| 4257 | // or a truncation. |
| 4258 | SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat); |
| 4259 | return DAG.getNode(ByScalar, DL, VT, Op0, Shift); |
| 4260 | } |
| 4261 | } |
| 4262 | |
| 4263 | // See whether the shift vector is a splat represented as SHUFFLE_VECTOR, |
| 4264 | // and the shift amount is directly available in a GPR. |
| 4265 | if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) { |
| 4266 | if (VSN->isSplat()) { |
| 4267 | SDValue VSNOp0 = VSN->getOperand(0); |
| 4268 | unsigned Index = VSN->getSplatIndex(); |
| 4269 | assert(Index < VT.getVectorNumElements() && |
| 4270 | "Splat index should be defined and in first operand"); |
| 4271 | if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) || |
| 4272 | VSNOp0.getOpcode() == ISD::BUILD_VECTOR) { |
| 4273 | // Since i32 is the smallest legal type, we either need a no-op |
| 4274 | // or a truncation. |
| 4275 | SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, |
| 4276 | VSNOp0.getOperand(Index)); |
| 4277 | return DAG.getNode(ByScalar, DL, VT, Op0, Shift); |
| 4278 | } |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 4282 | // Otherwise just treat the current form as legal. |
| 4283 | return Op; |
| 4284 | } |
| 4285 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4286 | SDValue SystemZTargetLowering::LowerOperation(SDValue Op, |
| 4287 | SelectionDAG &DAG) const { |
| 4288 | switch (Op.getOpcode()) { |
| 4289 | case ISD::BR_CC: |
| 4290 | return lowerBR_CC(Op, DAG); |
| 4291 | case ISD::SELECT_CC: |
| 4292 | return lowerSELECT_CC(Op, DAG); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 4293 | case ISD::SETCC: |
| 4294 | return lowerSETCC(Op, DAG); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4295 | case ISD::GlobalAddress: |
| 4296 | return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG); |
| 4297 | case ISD::GlobalTLSAddress: |
| 4298 | return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG); |
| 4299 | case ISD::BlockAddress: |
| 4300 | return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG); |
| 4301 | case ISD::JumpTable: |
| 4302 | return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG); |
| 4303 | case ISD::ConstantPool: |
| 4304 | return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG); |
| 4305 | case ISD::BITCAST: |
| 4306 | return lowerBITCAST(Op, DAG); |
| 4307 | case ISD::VASTART: |
| 4308 | return lowerVASTART(Op, DAG); |
| 4309 | case ISD::VACOPY: |
| 4310 | return lowerVACOPY(Op, DAG); |
| 4311 | case ISD::DYNAMIC_STACKALLOC: |
| 4312 | return lowerDYNAMIC_STACKALLOC(Op, DAG); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 4313 | case ISD::SMUL_LOHI: |
| 4314 | return lowerSMUL_LOHI(Op, DAG); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4315 | case ISD::UMUL_LOHI: |
| 4316 | return lowerUMUL_LOHI(Op, DAG); |
| 4317 | case ISD::SDIVREM: |
| 4318 | return lowerSDIVREM(Op, DAG); |
| 4319 | case ISD::UDIVREM: |
| 4320 | return lowerUDIVREM(Op, DAG); |
| 4321 | case ISD::OR: |
| 4322 | return lowerOR(Op, DAG); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 4323 | case ISD::CTPOP: |
| 4324 | return lowerCTPOP(Op, DAG); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4325 | case ISD::CTLZ_ZERO_UNDEF: |
| 4326 | return DAG.getNode(ISD::CTLZ, SDLoc(Op), |
| 4327 | Op.getValueType(), Op.getOperand(0)); |
| 4328 | case ISD::CTTZ_ZERO_UNDEF: |
| 4329 | return DAG.getNode(ISD::CTTZ, SDLoc(Op), |
| 4330 | Op.getValueType(), Op.getOperand(0)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4331 | case ISD::ATOMIC_SWAP: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4332 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW); |
| 4333 | case ISD::ATOMIC_STORE: |
| 4334 | return lowerATOMIC_STORE(Op, DAG); |
| 4335 | case ISD::ATOMIC_LOAD: |
| 4336 | return lowerATOMIC_LOAD(Op, DAG); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4337 | case ISD::ATOMIC_LOAD_ADD: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4338 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4339 | case ISD::ATOMIC_LOAD_SUB: |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 4340 | return lowerATOMIC_LOAD_SUB(Op, DAG); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4341 | case ISD::ATOMIC_LOAD_AND: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4342 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4343 | case ISD::ATOMIC_LOAD_OR: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4344 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4345 | case ISD::ATOMIC_LOAD_XOR: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4346 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4347 | case ISD::ATOMIC_LOAD_NAND: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4348 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4349 | case ISD::ATOMIC_LOAD_MIN: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4350 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4351 | case ISD::ATOMIC_LOAD_MAX: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4352 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4353 | case ISD::ATOMIC_LOAD_UMIN: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4354 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4355 | case ISD::ATOMIC_LOAD_UMAX: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4356 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4357 | case ISD::ATOMIC_CMP_SWAP: |
| 4358 | return lowerATOMIC_CMP_SWAP(Op, DAG); |
| 4359 | case ISD::STACKSAVE: |
| 4360 | return lowerSTACKSAVE(Op, DAG); |
| 4361 | case ISD::STACKRESTORE: |
| 4362 | return lowerSTACKRESTORE(Op, DAG); |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 4363 | case ISD::PREFETCH: |
| 4364 | return lowerPREFETCH(Op, DAG); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 4365 | case ISD::INTRINSIC_W_CHAIN: |
| 4366 | return lowerINTRINSIC_W_CHAIN(Op, DAG); |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 4367 | case ISD::INTRINSIC_WO_CHAIN: |
| 4368 | return lowerINTRINSIC_WO_CHAIN(Op, DAG); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4369 | case ISD::BUILD_VECTOR: |
| 4370 | return lowerBUILD_VECTOR(Op, DAG); |
| 4371 | case ISD::VECTOR_SHUFFLE: |
| 4372 | return lowerVECTOR_SHUFFLE(Op, DAG); |
| 4373 | case ISD::SCALAR_TO_VECTOR: |
| 4374 | return lowerSCALAR_TO_VECTOR(Op, DAG); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 4375 | case ISD::INSERT_VECTOR_ELT: |
| 4376 | return lowerINSERT_VECTOR_ELT(Op, DAG); |
| 4377 | case ISD::EXTRACT_VECTOR_ELT: |
| 4378 | return lowerEXTRACT_VECTOR_ELT(Op, DAG); |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 4379 | case ISD::SIGN_EXTEND_VECTOR_INREG: |
| 4380 | return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH); |
| 4381 | case ISD::ZERO_EXTEND_VECTOR_INREG: |
| 4382 | return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4383 | case ISD::SHL: |
| 4384 | return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR); |
| 4385 | case ISD::SRL: |
| 4386 | return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR); |
| 4387 | case ISD::SRA: |
| 4388 | return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4389 | default: |
| 4390 | llvm_unreachable("Unexpected node to lower"); |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 4395 | #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME |
Matthias Braun | d04893f | 2015-05-07 21:33:59 +0000 | [diff] [blame] | 4396 | switch ((SystemZISD::NodeType)Opcode) { |
| 4397 | case SystemZISD::FIRST_NUMBER: break; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4398 | OPCODE(RET_FLAG); |
| 4399 | OPCODE(CALL); |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 4400 | OPCODE(SIBCALL); |
Ulrich Weigand | 1c6f07d | 2015-05-04 17:39:40 +0000 | [diff] [blame] | 4401 | OPCODE(TLS_GDCALL); |
| 4402 | OPCODE(TLS_LDCALL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4403 | OPCODE(PCREL_WRAPPER); |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 4404 | OPCODE(PCREL_OFFSET); |
Richard Sandiford | 5748547 | 2013-12-13 15:35:00 +0000 | [diff] [blame] | 4405 | OPCODE(IABS); |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 4406 | OPCODE(ICMP); |
| 4407 | OPCODE(FCMP); |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 4408 | OPCODE(TM); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4409 | OPCODE(BR_CCMASK); |
| 4410 | OPCODE(SELECT_CCMASK); |
| 4411 | OPCODE(ADJDYNALLOC); |
| 4412 | OPCODE(EXTRACT_ACCESS); |
Ulrich Weigand | 1c6f07d | 2015-05-04 17:39:40 +0000 | [diff] [blame] | 4413 | OPCODE(POPCNT); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4414 | OPCODE(UMUL_LOHI64); |
Ulrich Weigand | 1c6f07d | 2015-05-04 17:39:40 +0000 | [diff] [blame] | 4415 | OPCODE(SDIVREM32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4416 | OPCODE(SDIVREM64); |
| 4417 | OPCODE(UDIVREM32); |
| 4418 | OPCODE(UDIVREM64); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 4419 | OPCODE(MVC); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4420 | OPCODE(MVC_LOOP); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 4421 | OPCODE(NC); |
| 4422 | OPCODE(NC_LOOP); |
| 4423 | OPCODE(OC); |
| 4424 | OPCODE(OC_LOOP); |
| 4425 | OPCODE(XC); |
| 4426 | OPCODE(XC_LOOP); |
Richard Sandiford | 761703a | 2013-08-12 10:17:33 +0000 | [diff] [blame] | 4427 | OPCODE(CLC); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4428 | OPCODE(CLC_LOOP); |
Richard Sandiford | bb83a50 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 4429 | OPCODE(STPCPY); |
Ulrich Weigand | 1c6f07d | 2015-05-04 17:39:40 +0000 | [diff] [blame] | 4430 | OPCODE(STRCMP); |
Richard Sandiford | 0dec06a | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 4431 | OPCODE(SEARCH_STRING); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 4432 | OPCODE(IPM); |
Richard Sandiford | 9afe613 | 2013-12-10 10:36:34 +0000 | [diff] [blame] | 4433 | OPCODE(SERIALIZE); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 4434 | OPCODE(TBEGIN); |
| 4435 | OPCODE(TBEGIN_NOFLOAT); |
| 4436 | OPCODE(TEND); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4437 | OPCODE(BYTE_MASK); |
| 4438 | OPCODE(ROTATE_MASK); |
| 4439 | OPCODE(REPLICATE); |
| 4440 | OPCODE(JOIN_DWORDS); |
| 4441 | OPCODE(SPLAT); |
| 4442 | OPCODE(MERGE_HIGH); |
| 4443 | OPCODE(MERGE_LOW); |
| 4444 | OPCODE(SHL_DOUBLE); |
| 4445 | OPCODE(PERMUTE_DWORDS); |
| 4446 | OPCODE(PERMUTE); |
| 4447 | OPCODE(PACK); |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 4448 | OPCODE(PACKS_CC); |
| 4449 | OPCODE(PACKLS_CC); |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 4450 | OPCODE(UNPACK_HIGH); |
| 4451 | OPCODE(UNPACKL_HIGH); |
| 4452 | OPCODE(UNPACK_LOW); |
| 4453 | OPCODE(UNPACKL_LOW); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4454 | OPCODE(VSHL_BY_SCALAR); |
| 4455 | OPCODE(VSRL_BY_SCALAR); |
| 4456 | OPCODE(VSRA_BY_SCALAR); |
| 4457 | OPCODE(VSUM); |
| 4458 | OPCODE(VICMPE); |
| 4459 | OPCODE(VICMPH); |
| 4460 | OPCODE(VICMPHL); |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 4461 | OPCODE(VICMPES); |
| 4462 | OPCODE(VICMPHS); |
| 4463 | OPCODE(VICMPHLS); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 4464 | OPCODE(VFCMPE); |
| 4465 | OPCODE(VFCMPH); |
| 4466 | OPCODE(VFCMPHE); |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 4467 | OPCODE(VFCMPES); |
| 4468 | OPCODE(VFCMPHS); |
| 4469 | OPCODE(VFCMPHES); |
| 4470 | OPCODE(VFTCI); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 4471 | OPCODE(VEXTEND); |
| 4472 | OPCODE(VROUND); |
Ulrich Weigand | c1708b2 | 2015-05-05 19:31:09 +0000 | [diff] [blame] | 4473 | OPCODE(VTM); |
| 4474 | OPCODE(VFAE_CC); |
| 4475 | OPCODE(VFAEZ_CC); |
| 4476 | OPCODE(VFEE_CC); |
| 4477 | OPCODE(VFEEZ_CC); |
| 4478 | OPCODE(VFENE_CC); |
| 4479 | OPCODE(VFENEZ_CC); |
| 4480 | OPCODE(VISTR_CC); |
| 4481 | OPCODE(VSTRC_CC); |
| 4482 | OPCODE(VSTRCZ_CC); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4483 | OPCODE(ATOMIC_SWAPW); |
| 4484 | OPCODE(ATOMIC_LOADW_ADD); |
| 4485 | OPCODE(ATOMIC_LOADW_SUB); |
| 4486 | OPCODE(ATOMIC_LOADW_AND); |
| 4487 | OPCODE(ATOMIC_LOADW_OR); |
| 4488 | OPCODE(ATOMIC_LOADW_XOR); |
| 4489 | OPCODE(ATOMIC_LOADW_NAND); |
| 4490 | OPCODE(ATOMIC_LOADW_MIN); |
| 4491 | OPCODE(ATOMIC_LOADW_MAX); |
| 4492 | OPCODE(ATOMIC_LOADW_UMIN); |
| 4493 | OPCODE(ATOMIC_LOADW_UMAX); |
| 4494 | OPCODE(ATOMIC_CMP_SWAPW); |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 4495 | OPCODE(PREFETCH); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4496 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 4497 | return nullptr; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4498 | #undef OPCODE |
| 4499 | } |
| 4500 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4501 | // Return true if VT is a vector whose elements are a whole number of bytes |
| 4502 | // in width. |
| 4503 | static bool canTreatAsByteVector(EVT VT) { |
| 4504 | return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0; |
| 4505 | } |
| 4506 | |
| 4507 | // Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT |
| 4508 | // producing a result of type ResVT. Op is a possibly bitcast version |
| 4509 | // of the input vector and Index is the index (based on type VecVT) that |
| 4510 | // should be extracted. Return the new extraction if a simplification |
| 4511 | // was possible or if Force is true. |
| 4512 | SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT, |
| 4513 | SDValue Op, unsigned Index, |
| 4514 | DAGCombinerInfo &DCI, |
| 4515 | bool Force) const { |
| 4516 | SelectionDAG &DAG = DCI.DAG; |
| 4517 | |
| 4518 | // The number of bytes being extracted. |
| 4519 | unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); |
| 4520 | |
| 4521 | for (;;) { |
| 4522 | unsigned Opcode = Op.getOpcode(); |
| 4523 | if (Opcode == ISD::BITCAST) |
| 4524 | // Look through bitcasts. |
| 4525 | Op = Op.getOperand(0); |
| 4526 | else if (Opcode == ISD::VECTOR_SHUFFLE && |
| 4527 | canTreatAsByteVector(Op.getValueType())) { |
| 4528 | // Get a VPERM-like permute mask and see whether the bytes covered |
| 4529 | // by the extracted element are a contiguous sequence from one |
| 4530 | // source operand. |
| 4531 | SmallVector<int, SystemZ::VectorBytes> Bytes; |
| 4532 | getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes); |
| 4533 | int First; |
| 4534 | if (!getShuffleInput(Bytes, Index * BytesPerElement, |
| 4535 | BytesPerElement, First)) |
| 4536 | break; |
| 4537 | if (First < 0) |
| 4538 | return DAG.getUNDEF(ResVT); |
| 4539 | // Make sure the contiguous sequence starts at a multiple of the |
| 4540 | // original element size. |
| 4541 | unsigned Byte = unsigned(First) % Bytes.size(); |
| 4542 | if (Byte % BytesPerElement != 0) |
| 4543 | break; |
| 4544 | // We can get the extracted value directly from an input. |
| 4545 | Index = Byte / BytesPerElement; |
| 4546 | Op = Op.getOperand(unsigned(First) / Bytes.size()); |
| 4547 | Force = true; |
| 4548 | } else if (Opcode == ISD::BUILD_VECTOR && |
| 4549 | canTreatAsByteVector(Op.getValueType())) { |
| 4550 | // We can only optimize this case if the BUILD_VECTOR elements are |
| 4551 | // at least as wide as the extracted value. |
| 4552 | EVT OpVT = Op.getValueType(); |
| 4553 | unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); |
| 4554 | if (OpBytesPerElement < BytesPerElement) |
| 4555 | break; |
| 4556 | // Make sure that the least-significant bit of the extracted value |
| 4557 | // is the least significant bit of an input. |
| 4558 | unsigned End = (Index + 1) * BytesPerElement; |
| 4559 | if (End % OpBytesPerElement != 0) |
| 4560 | break; |
| 4561 | // We're extracting the low part of one operand of the BUILD_VECTOR. |
| 4562 | Op = Op.getOperand(End / OpBytesPerElement - 1); |
| 4563 | if (!Op.getValueType().isInteger()) { |
| 4564 | EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits()); |
| 4565 | Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 4566 | DCI.AddToWorklist(Op.getNode()); |
| 4567 | } |
| 4568 | EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits()); |
| 4569 | Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); |
| 4570 | if (VT != ResVT) { |
| 4571 | DCI.AddToWorklist(Op.getNode()); |
| 4572 | Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op); |
| 4573 | } |
| 4574 | return Op; |
| 4575 | } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 4576 | Opcode == ISD::ZERO_EXTEND_VECTOR_INREG || |
| 4577 | Opcode == ISD::ANY_EXTEND_VECTOR_INREG) && |
| 4578 | canTreatAsByteVector(Op.getValueType()) && |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4579 | canTreatAsByteVector(Op.getOperand(0).getValueType())) { |
| 4580 | // Make sure that only the unextended bits are significant. |
| 4581 | EVT ExtVT = Op.getValueType(); |
| 4582 | EVT OpVT = Op.getOperand(0).getValueType(); |
| 4583 | unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize(); |
| 4584 | unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); |
| 4585 | unsigned Byte = Index * BytesPerElement; |
| 4586 | unsigned SubByte = Byte % ExtBytesPerElement; |
| 4587 | unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement; |
| 4588 | if (SubByte < MinSubByte || |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 4589 | SubByte + BytesPerElement > ExtBytesPerElement) |
| 4590 | break; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4591 | // Get the byte offset of the unextended element |
| 4592 | Byte = Byte / ExtBytesPerElement * OpBytesPerElement; |
| 4593 | // ...then add the byte offset relative to that element. |
| 4594 | Byte += SubByte - MinSubByte; |
| 4595 | if (Byte % BytesPerElement != 0) |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 4596 | break; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4597 | Op = Op.getOperand(0); |
| 4598 | Index = Byte / BytesPerElement; |
| 4599 | Force = true; |
| 4600 | } else |
| 4601 | break; |
| 4602 | } |
| 4603 | if (Force) { |
| 4604 | if (Op.getValueType() != VecVT) { |
| 4605 | Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op); |
| 4606 | DCI.AddToWorklist(Op.getNode()); |
| 4607 | } |
| 4608 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op, |
| 4609 | DAG.getConstant(Index, DL, MVT::i32)); |
| 4610 | } |
| 4611 | return SDValue(); |
| 4612 | } |
| 4613 | |
| 4614 | // Optimize vector operations in scalar value Op on the basis that Op |
| 4615 | // is truncated to TruncVT. |
| 4616 | SDValue |
| 4617 | SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op, |
| 4618 | DAGCombinerInfo &DCI) const { |
| 4619 | // If we have (trunc (extract_vector_elt X, Y)), try to turn it into |
| 4620 | // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements |
| 4621 | // of type TruncVT. |
| 4622 | if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 4623 | TruncVT.getSizeInBits() % 8 == 0) { |
| 4624 | SDValue Vec = Op.getOperand(0); |
| 4625 | EVT VecVT = Vec.getValueType(); |
| 4626 | if (canTreatAsByteVector(VecVT)) { |
| 4627 | if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 4628 | unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); |
| 4629 | unsigned TruncBytes = TruncVT.getStoreSize(); |
| 4630 | if (BytesPerElement % TruncBytes == 0) { |
| 4631 | // Calculate the value of Y' in the above description. We are |
| 4632 | // splitting the original elements into Scale equal-sized pieces |
| 4633 | // and for truncation purposes want the last (least-significant) |
| 4634 | // of these pieces for IndexN. This is easiest to do by calculating |
| 4635 | // the start index of the following element and then subtracting 1. |
| 4636 | unsigned Scale = BytesPerElement / TruncBytes; |
| 4637 | unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1; |
| 4638 | |
| 4639 | // Defer the creation of the bitcast from X to combineExtract, |
| 4640 | // which might be able to optimize the extraction. |
| 4641 | VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8), |
| 4642 | VecVT.getStoreSize() / TruncBytes); |
| 4643 | EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT); |
| 4644 | return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true); |
| 4645 | } |
| 4646 | } |
| 4647 | } |
| 4648 | } |
| 4649 | return SDValue(); |
| 4650 | } |
| 4651 | |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 4652 | SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N, |
| 4653 | DAGCombinerInfo &DCI) const { |
| 4654 | SelectionDAG &DAG = DCI.DAG; |
| 4655 | unsigned Opcode = N->getOpcode(); |
| 4656 | if (Opcode == ISD::SIGN_EXTEND) { |
| 4657 | // Convert (sext (ashr (shl X, C1), C2)) to |
| 4658 | // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as |
| 4659 | // cheap as narrower ones. |
| 4660 | SDValue N0 = N->getOperand(0); |
| 4661 | EVT VT = N->getValueType(0); |
| 4662 | if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) { |
| 4663 | auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 4664 | SDValue Inner = N0.getOperand(0); |
| 4665 | if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) { |
| 4666 | if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) { |
| 4667 | unsigned Extra = (VT.getSizeInBits() - |
| 4668 | N0.getValueType().getSizeInBits()); |
| 4669 | unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra; |
| 4670 | unsigned NewSraAmt = SraAmt->getZExtValue() + Extra; |
| 4671 | EVT ShiftVT = N0.getOperand(1).getValueType(); |
| 4672 | SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT, |
| 4673 | Inner.getOperand(0)); |
| 4674 | SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4675 | DAG.getConstant(NewShlAmt, SDLoc(Inner), |
| 4676 | ShiftVT)); |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 4677 | return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4678 | DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT)); |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 4679 | } |
| 4680 | } |
| 4681 | } |
| 4682 | } |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 4683 | if (Opcode == SystemZISD::MERGE_HIGH || |
| 4684 | Opcode == SystemZISD::MERGE_LOW) { |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 4685 | SDValue Op0 = N->getOperand(0); |
| 4686 | SDValue Op1 = N->getOperand(1); |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 4687 | if (Op0.getOpcode() == ISD::BITCAST) |
| 4688 | Op0 = Op0.getOperand(0); |
| 4689 | if (Op0.getOpcode() == SystemZISD::BYTE_MASK && |
| 4690 | cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) { |
| 4691 | // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF |
| 4692 | // for v4f32. |
| 4693 | if (Op1 == N->getOperand(0)) |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 4694 | return Op1; |
Ulrich Weigand | cd2a1b5 | 2015-05-05 19:29:21 +0000 | [diff] [blame] | 4695 | // (z_merge_? 0, X) -> (z_unpackl_? 0, X). |
| 4696 | EVT VT = Op1.getValueType(); |
| 4697 | unsigned ElemBytes = VT.getVectorElementType().getStoreSize(); |
| 4698 | if (ElemBytes <= 4) { |
| 4699 | Opcode = (Opcode == SystemZISD::MERGE_HIGH ? |
| 4700 | SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW); |
| 4701 | EVT InVT = VT.changeVectorElementTypeToInteger(); |
| 4702 | EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16), |
| 4703 | SystemZ::VectorBytes / ElemBytes / 2); |
| 4704 | if (VT != InVT) { |
| 4705 | Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1); |
| 4706 | DCI.AddToWorklist(Op1.getNode()); |
| 4707 | } |
| 4708 | SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1); |
| 4709 | DCI.AddToWorklist(Op.getNode()); |
| 4710 | return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op); |
| 4711 | } |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 4712 | } |
| 4713 | } |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4714 | // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better |
| 4715 | // for the extraction to be done on a vMiN value, so that we can use VSTE. |
| 4716 | // If X has wider elements then convert it to: |
| 4717 | // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z). |
| 4718 | if (Opcode == ISD::STORE) { |
| 4719 | auto *SN = cast<StoreSDNode>(N); |
| 4720 | EVT MemVT = SN->getMemoryVT(); |
| 4721 | if (MemVT.isInteger()) { |
| 4722 | SDValue Value = combineTruncateExtract(SDLoc(N), MemVT, |
| 4723 | SN->getValue(), DCI); |
| 4724 | if (Value.getNode()) { |
| 4725 | DCI.AddToWorklist(Value.getNode()); |
| 4726 | |
| 4727 | // Rewrite the store with the new form of stored value. |
| 4728 | return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value, |
| 4729 | SN->getBasePtr(), SN->getMemoryVT(), |
| 4730 | SN->getMemOperand()); |
| 4731 | } |
| 4732 | } |
| 4733 | } |
| 4734 | // Try to simplify a vector extraction. |
| 4735 | if (Opcode == ISD::EXTRACT_VECTOR_ELT) { |
| 4736 | if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) { |
| 4737 | SDValue Op0 = N->getOperand(0); |
| 4738 | EVT VecVT = Op0.getValueType(); |
| 4739 | return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0, |
| 4740 | IndexN->getZExtValue(), DCI, false); |
| 4741 | } |
| 4742 | } |
| 4743 | // (join_dwords X, X) == (replicate X) |
| 4744 | if (Opcode == SystemZISD::JOIN_DWORDS && |
| 4745 | N->getOperand(0) == N->getOperand(1)) |
| 4746 | return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0), |
| 4747 | N->getOperand(0)); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame] | 4748 | // (fround (extract_vector_elt X 0)) |
| 4749 | // (fround (extract_vector_elt X 1)) -> |
| 4750 | // (extract_vector_elt (VROUND X) 0) |
| 4751 | // (extract_vector_elt (VROUND X) 1) |
| 4752 | // |
| 4753 | // This is a special case since the target doesn't really support v2f32s. |
| 4754 | if (Opcode == ISD::FP_ROUND) { |
| 4755 | SDValue Op0 = N->getOperand(0); |
| 4756 | if (N->getValueType(0) == MVT::f32 && |
| 4757 | Op0.hasOneUse() && |
| 4758 | Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 4759 | Op0.getOperand(0).getValueType() == MVT::v2f64 && |
| 4760 | Op0.getOperand(1).getOpcode() == ISD::Constant && |
| 4761 | cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) { |
| 4762 | SDValue Vec = Op0.getOperand(0); |
| 4763 | for (auto *U : Vec->uses()) { |
| 4764 | if (U != Op0.getNode() && |
| 4765 | U->hasOneUse() && |
| 4766 | U->getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 4767 | U->getOperand(0) == Vec && |
| 4768 | U->getOperand(1).getOpcode() == ISD::Constant && |
| 4769 | cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) { |
| 4770 | SDValue OtherRound = SDValue(*U->use_begin(), 0); |
| 4771 | if (OtherRound.getOpcode() == ISD::FP_ROUND && |
| 4772 | OtherRound.getOperand(0) == SDValue(U, 0) && |
| 4773 | OtherRound.getValueType() == MVT::f32) { |
| 4774 | SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N), |
| 4775 | MVT::v4f32, Vec); |
| 4776 | DCI.AddToWorklist(VRound.getNode()); |
| 4777 | SDValue Extract1 = |
| 4778 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32, |
| 4779 | VRound, DAG.getConstant(2, SDLoc(U), MVT::i32)); |
| 4780 | DCI.AddToWorklist(Extract1.getNode()); |
| 4781 | DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1); |
| 4782 | SDValue Extract0 = |
| 4783 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32, |
| 4784 | VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32)); |
| 4785 | return Extract0; |
| 4786 | } |
| 4787 | } |
| 4788 | } |
| 4789 | } |
| 4790 | } |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 4791 | return SDValue(); |
| 4792 | } |
| 4793 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4794 | //===----------------------------------------------------------------------===// |
| 4795 | // Custom insertion |
| 4796 | //===----------------------------------------------------------------------===// |
| 4797 | |
| 4798 | // Create a new basic block after MBB. |
| 4799 | static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) { |
| 4800 | MachineFunction &MF = *MBB->getParent(); |
| 4801 | MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock()); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 4802 | MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4803 | return NewMBB; |
| 4804 | } |
| 4805 | |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 4806 | // Split MBB after MI and return the new block (the one that contains |
| 4807 | // instructions after MI). |
| 4808 | static MachineBasicBlock *splitBlockAfter(MachineInstr *MI, |
| 4809 | MachineBasicBlock *MBB) { |
| 4810 | MachineBasicBlock *NewMBB = emitBlockAfter(MBB); |
| 4811 | NewMBB->splice(NewMBB->begin(), MBB, |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 4812 | std::next(MachineBasicBlock::iterator(MI)), MBB->end()); |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 4813 | NewMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 4814 | return NewMBB; |
| 4815 | } |
| 4816 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4817 | // Split MBB before MI and return the new block (the one that contains MI). |
| 4818 | static MachineBasicBlock *splitBlockBefore(MachineInstr *MI, |
| 4819 | MachineBasicBlock *MBB) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4820 | MachineBasicBlock *NewMBB = emitBlockAfter(MBB); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4821 | NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4822 | NewMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 4823 | return NewMBB; |
| 4824 | } |
| 4825 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4826 | // Force base value Base into a register before MI. Return the register. |
| 4827 | static unsigned forceReg(MachineInstr *MI, MachineOperand &Base, |
| 4828 | const SystemZInstrInfo *TII) { |
| 4829 | if (Base.isReg()) |
| 4830 | return Base.getReg(); |
| 4831 | |
| 4832 | MachineBasicBlock *MBB = MI->getParent(); |
| 4833 | MachineFunction &MF = *MBB->getParent(); |
| 4834 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 4835 | |
| 4836 | unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); |
| 4837 | BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg) |
| 4838 | .addOperand(Base).addImm(0).addReg(0); |
| 4839 | return Reg; |
| 4840 | } |
| 4841 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4842 | // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI. |
| 4843 | MachineBasicBlock * |
| 4844 | SystemZTargetLowering::emitSelect(MachineInstr *MI, |
| 4845 | MachineBasicBlock *MBB) const { |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4846 | const SystemZInstrInfo *TII = |
| 4847 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4848 | |
| 4849 | unsigned DestReg = MI->getOperand(0).getReg(); |
| 4850 | unsigned TrueReg = MI->getOperand(1).getReg(); |
| 4851 | unsigned FalseReg = MI->getOperand(2).getReg(); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4852 | unsigned CCValid = MI->getOperand(3).getImm(); |
| 4853 | unsigned CCMask = MI->getOperand(4).getImm(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4854 | DebugLoc DL = MI->getDebugLoc(); |
| 4855 | |
| 4856 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4857 | MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4858 | MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); |
| 4859 | |
| 4860 | // StartMBB: |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 4861 | // BRC CCMask, JoinMBB |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4862 | // # fallthrough to FalseMBB |
| 4863 | MBB = StartMBB; |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4864 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 4865 | .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4866 | MBB->addSuccessor(JoinMBB); |
| 4867 | MBB->addSuccessor(FalseMBB); |
| 4868 | |
| 4869 | // FalseMBB: |
| 4870 | // # fallthrough to JoinMBB |
| 4871 | MBB = FalseMBB; |
| 4872 | MBB->addSuccessor(JoinMBB); |
| 4873 | |
| 4874 | // JoinMBB: |
| 4875 | // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ] |
| 4876 | // ... |
| 4877 | MBB = JoinMBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4878 | BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4879 | .addReg(TrueReg).addMBB(StartMBB) |
| 4880 | .addReg(FalseReg).addMBB(FalseMBB); |
| 4881 | |
| 4882 | MI->eraseFromParent(); |
| 4883 | return JoinMBB; |
| 4884 | } |
| 4885 | |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4886 | // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI. |
| 4887 | // StoreOpcode is the store to use and Invert says whether the store should |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4888 | // happen when the condition is false rather than true. If a STORE ON |
| 4889 | // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0. |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4890 | MachineBasicBlock * |
| 4891 | SystemZTargetLowering::emitCondStore(MachineInstr *MI, |
| 4892 | MachineBasicBlock *MBB, |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4893 | unsigned StoreOpcode, unsigned STOCOpcode, |
| 4894 | bool Invert) const { |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4895 | const SystemZInstrInfo *TII = |
| 4896 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4897 | |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4898 | unsigned SrcReg = MI->getOperand(0).getReg(); |
| 4899 | MachineOperand Base = MI->getOperand(1); |
| 4900 | int64_t Disp = MI->getOperand(2).getImm(); |
| 4901 | unsigned IndexReg = MI->getOperand(3).getReg(); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4902 | unsigned CCValid = MI->getOperand(4).getImm(); |
| 4903 | unsigned CCMask = MI->getOperand(5).getImm(); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4904 | DebugLoc DL = MI->getDebugLoc(); |
| 4905 | |
| 4906 | StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp); |
| 4907 | |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4908 | // Use STOCOpcode if possible. We could use different store patterns in |
| 4909 | // order to avoid matching the index register, but the performance trade-offs |
| 4910 | // might be more complicated in that case. |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 4911 | if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) { |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4912 | if (Invert) |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4913 | CCMask ^= CCValid; |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4914 | BuildMI(*MBB, MI, DL, TII->get(STOCOpcode)) |
Richard Sandiford | fd7f4ae | 2013-08-01 10:39:40 +0000 | [diff] [blame] | 4915 | .addReg(SrcReg).addOperand(Base).addImm(Disp) |
| 4916 | .addImm(CCValid).addImm(CCMask); |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4917 | MI->eraseFromParent(); |
| 4918 | return MBB; |
| 4919 | } |
| 4920 | |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4921 | // Get the condition needed to branch around the store. |
| 4922 | if (!Invert) |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4923 | CCMask ^= CCValid; |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4924 | |
| 4925 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4926 | MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4927 | MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); |
| 4928 | |
| 4929 | // StartMBB: |
| 4930 | // BRC CCMask, JoinMBB |
| 4931 | // # fallthrough to FalseMBB |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4932 | MBB = StartMBB; |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4933 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 4934 | .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4935 | MBB->addSuccessor(JoinMBB); |
| 4936 | MBB->addSuccessor(FalseMBB); |
| 4937 | |
| 4938 | // FalseMBB: |
| 4939 | // store %SrcReg, %Disp(%Index,%Base) |
| 4940 | // # fallthrough to JoinMBB |
| 4941 | MBB = FalseMBB; |
| 4942 | BuildMI(MBB, DL, TII->get(StoreOpcode)) |
| 4943 | .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg); |
| 4944 | MBB->addSuccessor(JoinMBB); |
| 4945 | |
| 4946 | MI->eraseFromParent(); |
| 4947 | return JoinMBB; |
| 4948 | } |
| 4949 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4950 | // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_* |
| 4951 | // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that |
| 4952 | // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}. |
| 4953 | // BitSize is the width of the field in bits, or 0 if this is a partword |
| 4954 | // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize |
| 4955 | // is one of the operands. Invert says whether the field should be |
| 4956 | // inverted after performing BinOpcode (e.g. for NAND). |
| 4957 | MachineBasicBlock * |
| 4958 | SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI, |
| 4959 | MachineBasicBlock *MBB, |
| 4960 | unsigned BinOpcode, |
| 4961 | unsigned BitSize, |
| 4962 | bool Invert) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4963 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 4964 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4965 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4966 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4967 | bool IsSubWord = (BitSize < 32); |
| 4968 | |
| 4969 | // Extract the operands. Base can be a register or a frame index. |
| 4970 | // Src2 can be a register or immediate. |
| 4971 | unsigned Dest = MI->getOperand(0).getReg(); |
| 4972 | MachineOperand Base = earlyUseOperand(MI->getOperand(1)); |
| 4973 | int64_t Disp = MI->getOperand(2).getImm(); |
| 4974 | MachineOperand Src2 = earlyUseOperand(MI->getOperand(3)); |
| 4975 | unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); |
| 4976 | unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); |
| 4977 | DebugLoc DL = MI->getDebugLoc(); |
| 4978 | if (IsSubWord) |
| 4979 | BitSize = MI->getOperand(6).getImm(); |
| 4980 | |
| 4981 | // Subword operations use 32-bit registers. |
| 4982 | const TargetRegisterClass *RC = (BitSize <= 32 ? |
| 4983 | &SystemZ::GR32BitRegClass : |
| 4984 | &SystemZ::GR64BitRegClass); |
| 4985 | unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; |
| 4986 | unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; |
| 4987 | |
| 4988 | // Get the right opcodes for the displacement. |
| 4989 | LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); |
| 4990 | CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); |
| 4991 | assert(LOpcode && CSOpcode && "Displacement out of range"); |
| 4992 | |
| 4993 | // Create virtual registers for temporary results. |
| 4994 | unsigned OrigVal = MRI.createVirtualRegister(RC); |
| 4995 | unsigned OldVal = MRI.createVirtualRegister(RC); |
| 4996 | unsigned NewVal = (BinOpcode || IsSubWord ? |
| 4997 | MRI.createVirtualRegister(RC) : Src2.getReg()); |
| 4998 | unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); |
| 4999 | unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); |
| 5000 | |
| 5001 | // Insert a basic block for the main loop. |
| 5002 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5003 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5004 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
| 5005 | |
| 5006 | // StartMBB: |
| 5007 | // ... |
| 5008 | // %OrigVal = L Disp(%Base) |
| 5009 | // # fall through to LoopMMB |
| 5010 | MBB = StartMBB; |
| 5011 | BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) |
| 5012 | .addOperand(Base).addImm(Disp).addReg(0); |
| 5013 | MBB->addSuccessor(LoopMBB); |
| 5014 | |
| 5015 | // LoopMBB: |
| 5016 | // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ] |
| 5017 | // %RotatedOldVal = RLL %OldVal, 0(%BitShift) |
| 5018 | // %RotatedNewVal = OP %RotatedOldVal, %Src2 |
| 5019 | // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) |
| 5020 | // %Dest = CS %OldVal, %NewVal, Disp(%Base) |
| 5021 | // JNE LoopMBB |
| 5022 | // # fall through to DoneMMB |
| 5023 | MBB = LoopMBB; |
| 5024 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) |
| 5025 | .addReg(OrigVal).addMBB(StartMBB) |
| 5026 | .addReg(Dest).addMBB(LoopMBB); |
| 5027 | if (IsSubWord) |
| 5028 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) |
| 5029 | .addReg(OldVal).addReg(BitShift).addImm(0); |
| 5030 | if (Invert) { |
| 5031 | // Perform the operation normally and then invert every bit of the field. |
| 5032 | unsigned Tmp = MRI.createVirtualRegister(RC); |
| 5033 | BuildMI(MBB, DL, TII->get(BinOpcode), Tmp) |
| 5034 | .addReg(RotatedOldVal).addOperand(Src2); |
Alexey Samsonov | fffd56ec | 2014-08-20 21:56:43 +0000 | [diff] [blame] | 5035 | if (BitSize <= 32) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5036 | // XILF with the upper BitSize bits set. |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5037 | BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal) |
Alexey Samsonov | fffd56ec | 2014-08-20 21:56:43 +0000 | [diff] [blame] | 5038 | .addReg(Tmp).addImm(-1U << (32 - BitSize)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5039 | else { |
| 5040 | // Use LCGR and add -1 to the result, which is more compact than |
| 5041 | // an XILF, XILH pair. |
| 5042 | unsigned Tmp2 = MRI.createVirtualRegister(RC); |
| 5043 | BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp); |
| 5044 | BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal) |
| 5045 | .addReg(Tmp2).addImm(-1); |
| 5046 | } |
| 5047 | } else if (BinOpcode) |
| 5048 | // A simply binary operation. |
| 5049 | BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal) |
| 5050 | .addReg(RotatedOldVal).addOperand(Src2); |
| 5051 | else if (IsSubWord) |
| 5052 | // Use RISBG to rotate Src2 into position and use it to replace the |
| 5053 | // field in RotatedOldVal. |
| 5054 | BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal) |
| 5055 | .addReg(RotatedOldVal).addReg(Src2.getReg()) |
| 5056 | .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize); |
| 5057 | if (IsSubWord) |
| 5058 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) |
| 5059 | .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); |
| 5060 | BuildMI(MBB, DL, TII->get(CSOpcode), Dest) |
| 5061 | .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 5062 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5063 | .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5064 | MBB->addSuccessor(LoopMBB); |
| 5065 | MBB->addSuccessor(DoneMBB); |
| 5066 | |
| 5067 | MI->eraseFromParent(); |
| 5068 | return DoneMBB; |
| 5069 | } |
| 5070 | |
| 5071 | // Implement EmitInstrWithCustomInserter for pseudo |
| 5072 | // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the |
| 5073 | // instruction that should be used to compare the current field with the |
| 5074 | // minimum or maximum value. KeepOldMask is the BRC condition-code mask |
| 5075 | // for when the current field should be kept. BitSize is the width of |
| 5076 | // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction. |
| 5077 | MachineBasicBlock * |
| 5078 | SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI, |
| 5079 | MachineBasicBlock *MBB, |
| 5080 | unsigned CompareOpcode, |
| 5081 | unsigned KeepOldMask, |
| 5082 | unsigned BitSize) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5083 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 5084 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 5085 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5086 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5087 | bool IsSubWord = (BitSize < 32); |
| 5088 | |
| 5089 | // Extract the operands. Base can be a register or a frame index. |
| 5090 | unsigned Dest = MI->getOperand(0).getReg(); |
| 5091 | MachineOperand Base = earlyUseOperand(MI->getOperand(1)); |
| 5092 | int64_t Disp = MI->getOperand(2).getImm(); |
| 5093 | unsigned Src2 = MI->getOperand(3).getReg(); |
| 5094 | unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); |
| 5095 | unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); |
| 5096 | DebugLoc DL = MI->getDebugLoc(); |
| 5097 | if (IsSubWord) |
| 5098 | BitSize = MI->getOperand(6).getImm(); |
| 5099 | |
| 5100 | // Subword operations use 32-bit registers. |
| 5101 | const TargetRegisterClass *RC = (BitSize <= 32 ? |
| 5102 | &SystemZ::GR32BitRegClass : |
| 5103 | &SystemZ::GR64BitRegClass); |
| 5104 | unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; |
| 5105 | unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; |
| 5106 | |
| 5107 | // Get the right opcodes for the displacement. |
| 5108 | LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); |
| 5109 | CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); |
| 5110 | assert(LOpcode && CSOpcode && "Displacement out of range"); |
| 5111 | |
| 5112 | // Create virtual registers for temporary results. |
| 5113 | unsigned OrigVal = MRI.createVirtualRegister(RC); |
| 5114 | unsigned OldVal = MRI.createVirtualRegister(RC); |
| 5115 | unsigned NewVal = MRI.createVirtualRegister(RC); |
| 5116 | unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); |
| 5117 | unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2); |
| 5118 | unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); |
| 5119 | |
| 5120 | // Insert 3 basic blocks for the loop. |
| 5121 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5122 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5123 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
| 5124 | MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB); |
| 5125 | MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB); |
| 5126 | |
| 5127 | // StartMBB: |
| 5128 | // ... |
| 5129 | // %OrigVal = L Disp(%Base) |
| 5130 | // # fall through to LoopMMB |
| 5131 | MBB = StartMBB; |
| 5132 | BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) |
| 5133 | .addOperand(Base).addImm(Disp).addReg(0); |
| 5134 | MBB->addSuccessor(LoopMBB); |
| 5135 | |
| 5136 | // LoopMBB: |
| 5137 | // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ] |
| 5138 | // %RotatedOldVal = RLL %OldVal, 0(%BitShift) |
| 5139 | // CompareOpcode %RotatedOldVal, %Src2 |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 5140 | // BRC KeepOldMask, UpdateMBB |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5141 | MBB = LoopMBB; |
| 5142 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) |
| 5143 | .addReg(OrigVal).addMBB(StartMBB) |
| 5144 | .addReg(Dest).addMBB(UpdateMBB); |
| 5145 | if (IsSubWord) |
| 5146 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) |
| 5147 | .addReg(OldVal).addReg(BitShift).addImm(0); |
Richard Sandiford | 8a757bb | 2013-07-31 12:11:07 +0000 | [diff] [blame] | 5148 | BuildMI(MBB, DL, TII->get(CompareOpcode)) |
| 5149 | .addReg(RotatedOldVal).addReg(Src2); |
| 5150 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 5151 | .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5152 | MBB->addSuccessor(UpdateMBB); |
| 5153 | MBB->addSuccessor(UseAltMBB); |
| 5154 | |
| 5155 | // UseAltMBB: |
| 5156 | // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0 |
| 5157 | // # fall through to UpdateMMB |
| 5158 | MBB = UseAltMBB; |
| 5159 | if (IsSubWord) |
| 5160 | BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal) |
| 5161 | .addReg(RotatedOldVal).addReg(Src2) |
| 5162 | .addImm(32).addImm(31 + BitSize).addImm(0); |
| 5163 | MBB->addSuccessor(UpdateMBB); |
| 5164 | |
| 5165 | // UpdateMBB: |
| 5166 | // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ], |
| 5167 | // [ %RotatedAltVal, UseAltMBB ] |
| 5168 | // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) |
| 5169 | // %Dest = CS %OldVal, %NewVal, Disp(%Base) |
| 5170 | // JNE LoopMBB |
| 5171 | // # fall through to DoneMMB |
| 5172 | MBB = UpdateMBB; |
| 5173 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal) |
| 5174 | .addReg(RotatedOldVal).addMBB(LoopMBB) |
| 5175 | .addReg(RotatedAltVal).addMBB(UseAltMBB); |
| 5176 | if (IsSubWord) |
| 5177 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) |
| 5178 | .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); |
| 5179 | BuildMI(MBB, DL, TII->get(CSOpcode), Dest) |
| 5180 | .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 5181 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5182 | .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5183 | MBB->addSuccessor(LoopMBB); |
| 5184 | MBB->addSuccessor(DoneMBB); |
| 5185 | |
| 5186 | MI->eraseFromParent(); |
| 5187 | return DoneMBB; |
| 5188 | } |
| 5189 | |
| 5190 | // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW |
| 5191 | // instruction MI. |
| 5192 | MachineBasicBlock * |
| 5193 | SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI, |
| 5194 | MachineBasicBlock *MBB) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5195 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 5196 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 5197 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5198 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5199 | |
| 5200 | // Extract the operands. Base can be a register or a frame index. |
| 5201 | unsigned Dest = MI->getOperand(0).getReg(); |
| 5202 | MachineOperand Base = earlyUseOperand(MI->getOperand(1)); |
| 5203 | int64_t Disp = MI->getOperand(2).getImm(); |
| 5204 | unsigned OrigCmpVal = MI->getOperand(3).getReg(); |
| 5205 | unsigned OrigSwapVal = MI->getOperand(4).getReg(); |
| 5206 | unsigned BitShift = MI->getOperand(5).getReg(); |
| 5207 | unsigned NegBitShift = MI->getOperand(6).getReg(); |
| 5208 | int64_t BitSize = MI->getOperand(7).getImm(); |
| 5209 | DebugLoc DL = MI->getDebugLoc(); |
| 5210 | |
| 5211 | const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass; |
| 5212 | |
| 5213 | // Get the right opcodes for the displacement. |
| 5214 | unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp); |
| 5215 | unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp); |
| 5216 | assert(LOpcode && CSOpcode && "Displacement out of range"); |
| 5217 | |
| 5218 | // Create virtual registers for temporary results. |
| 5219 | unsigned OrigOldVal = MRI.createVirtualRegister(RC); |
| 5220 | unsigned OldVal = MRI.createVirtualRegister(RC); |
| 5221 | unsigned CmpVal = MRI.createVirtualRegister(RC); |
| 5222 | unsigned SwapVal = MRI.createVirtualRegister(RC); |
| 5223 | unsigned StoreVal = MRI.createVirtualRegister(RC); |
| 5224 | unsigned RetryOldVal = MRI.createVirtualRegister(RC); |
| 5225 | unsigned RetryCmpVal = MRI.createVirtualRegister(RC); |
| 5226 | unsigned RetrySwapVal = MRI.createVirtualRegister(RC); |
| 5227 | |
| 5228 | // Insert 2 basic blocks for the loop. |
| 5229 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5230 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5231 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
| 5232 | MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB); |
| 5233 | |
| 5234 | // StartMBB: |
| 5235 | // ... |
| 5236 | // %OrigOldVal = L Disp(%Base) |
| 5237 | // # fall through to LoopMMB |
| 5238 | MBB = StartMBB; |
| 5239 | BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal) |
| 5240 | .addOperand(Base).addImm(Disp).addReg(0); |
| 5241 | MBB->addSuccessor(LoopMBB); |
| 5242 | |
| 5243 | // LoopMBB: |
| 5244 | // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ] |
| 5245 | // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ] |
| 5246 | // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ] |
| 5247 | // %Dest = RLL %OldVal, BitSize(%BitShift) |
| 5248 | // ^^ The low BitSize bits contain the field |
| 5249 | // of interest. |
| 5250 | // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0 |
| 5251 | // ^^ Replace the upper 32-BitSize bits of the |
| 5252 | // comparison value with those that we loaded, |
| 5253 | // so that we can use a full word comparison. |
Richard Sandiford | 8a757bb | 2013-07-31 12:11:07 +0000 | [diff] [blame] | 5254 | // CR %Dest, %RetryCmpVal |
| 5255 | // JNE DoneMBB |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5256 | // # Fall through to SetMBB |
| 5257 | MBB = LoopMBB; |
| 5258 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) |
| 5259 | .addReg(OrigOldVal).addMBB(StartMBB) |
| 5260 | .addReg(RetryOldVal).addMBB(SetMBB); |
| 5261 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal) |
| 5262 | .addReg(OrigCmpVal).addMBB(StartMBB) |
| 5263 | .addReg(RetryCmpVal).addMBB(SetMBB); |
| 5264 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal) |
| 5265 | .addReg(OrigSwapVal).addMBB(StartMBB) |
| 5266 | .addReg(RetrySwapVal).addMBB(SetMBB); |
| 5267 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest) |
| 5268 | .addReg(OldVal).addReg(BitShift).addImm(BitSize); |
| 5269 | BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal) |
| 5270 | .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); |
Richard Sandiford | 8a757bb | 2013-07-31 12:11:07 +0000 | [diff] [blame] | 5271 | BuildMI(MBB, DL, TII->get(SystemZ::CR)) |
| 5272 | .addReg(Dest).addReg(RetryCmpVal); |
| 5273 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 5274 | .addImm(SystemZ::CCMASK_ICMP) |
| 5275 | .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5276 | MBB->addSuccessor(DoneMBB); |
| 5277 | MBB->addSuccessor(SetMBB); |
| 5278 | |
| 5279 | // SetMBB: |
| 5280 | // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0 |
| 5281 | // ^^ Replace the upper 32-BitSize bits of the new |
| 5282 | // value with those that we loaded. |
| 5283 | // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift) |
| 5284 | // ^^ Rotate the new field to its proper position. |
| 5285 | // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base) |
| 5286 | // JNE LoopMBB |
| 5287 | // # fall through to ExitMMB |
| 5288 | MBB = SetMBB; |
| 5289 | BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal) |
| 5290 | .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); |
| 5291 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal) |
| 5292 | .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize); |
| 5293 | BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal) |
| 5294 | .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 5295 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5296 | .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5297 | MBB->addSuccessor(LoopMBB); |
| 5298 | MBB->addSuccessor(DoneMBB); |
| 5299 | |
| 5300 | MI->eraseFromParent(); |
| 5301 | return DoneMBB; |
| 5302 | } |
| 5303 | |
| 5304 | // Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true |
| 5305 | // if the high register of the GR128 value must be cleared or false if |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 5306 | // it's "don't care". SubReg is subreg_l32 when extending a GR32 |
| 5307 | // and subreg_l64 when extending a GR64. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5308 | MachineBasicBlock * |
| 5309 | SystemZTargetLowering::emitExt128(MachineInstr *MI, |
| 5310 | MachineBasicBlock *MBB, |
| 5311 | bool ClearEven, unsigned SubReg) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5312 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 5313 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 5314 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5315 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 5316 | DebugLoc DL = MI->getDebugLoc(); |
| 5317 | |
| 5318 | unsigned Dest = MI->getOperand(0).getReg(); |
| 5319 | unsigned Src = MI->getOperand(1).getReg(); |
| 5320 | unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); |
| 5321 | |
| 5322 | BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128); |
| 5323 | if (ClearEven) { |
| 5324 | unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); |
| 5325 | unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); |
| 5326 | |
| 5327 | BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64) |
| 5328 | .addImm(0); |
| 5329 | BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128) |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 5330 | .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5331 | In128 = NewIn128; |
| 5332 | } |
| 5333 | BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest) |
| 5334 | .addReg(In128).addReg(Src).addImm(SubReg); |
| 5335 | |
| 5336 | MI->eraseFromParent(); |
| 5337 | return MBB; |
| 5338 | } |
| 5339 | |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 5340 | MachineBasicBlock * |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5341 | SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI, |
| 5342 | MachineBasicBlock *MBB, |
| 5343 | unsigned Opcode) const { |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5344 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 5345 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 5346 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5347 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 5348 | DebugLoc DL = MI->getDebugLoc(); |
| 5349 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5350 | MachineOperand DestBase = earlyUseOperand(MI->getOperand(0)); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 5351 | uint64_t DestDisp = MI->getOperand(1).getImm(); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5352 | MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2)); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 5353 | uint64_t SrcDisp = MI->getOperand(3).getImm(); |
| 5354 | uint64_t Length = MI->getOperand(4).getImm(); |
| 5355 | |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5356 | // When generating more than one CLC, all but the last will need to |
| 5357 | // branch to the end when a difference is found. |
| 5358 | MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ? |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 5359 | splitBlockAfter(MI, MBB) : nullptr); |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5360 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5361 | // Check for the loop form, in which operand 5 is the trip count. |
| 5362 | if (MI->getNumExplicitOperands() > 5) { |
| 5363 | bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase); |
| 5364 | |
| 5365 | uint64_t StartCountReg = MI->getOperand(5).getReg(); |
| 5366 | uint64_t StartSrcReg = forceReg(MI, SrcBase, TII); |
| 5367 | uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg : |
| 5368 | forceReg(MI, DestBase, TII)); |
| 5369 | |
| 5370 | const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass; |
| 5371 | uint64_t ThisSrcReg = MRI.createVirtualRegister(RC); |
| 5372 | uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg : |
| 5373 | MRI.createVirtualRegister(RC)); |
| 5374 | uint64_t NextSrcReg = MRI.createVirtualRegister(RC); |
| 5375 | uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg : |
| 5376 | MRI.createVirtualRegister(RC)); |
| 5377 | |
| 5378 | RC = &SystemZ::GR64BitRegClass; |
| 5379 | uint64_t ThisCountReg = MRI.createVirtualRegister(RC); |
| 5380 | uint64_t NextCountReg = MRI.createVirtualRegister(RC); |
| 5381 | |
| 5382 | MachineBasicBlock *StartMBB = MBB; |
| 5383 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
| 5384 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5385 | MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5386 | |
| 5387 | // StartMBB: |
| 5388 | // # fall through to LoopMMB |
| 5389 | MBB->addSuccessor(LoopMBB); |
| 5390 | |
| 5391 | // LoopMBB: |
| 5392 | // %ThisDestReg = phi [ %StartDestReg, StartMBB ], |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5393 | // [ %NextDestReg, NextMBB ] |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5394 | // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ], |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5395 | // [ %NextSrcReg, NextMBB ] |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5396 | // %ThisCountReg = phi [ %StartCountReg, StartMBB ], |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5397 | // [ %NextCountReg, NextMBB ] |
| 5398 | // ( PFD 2, 768+DestDisp(%ThisDestReg) ) |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5399 | // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg) |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5400 | // ( JLH EndMBB ) |
| 5401 | // |
| 5402 | // The prefetch is used only for MVC. The JLH is used only for CLC. |
| 5403 | MBB = LoopMBB; |
| 5404 | |
| 5405 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg) |
| 5406 | .addReg(StartDestReg).addMBB(StartMBB) |
| 5407 | .addReg(NextDestReg).addMBB(NextMBB); |
| 5408 | if (!HaveSingleBase) |
| 5409 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg) |
| 5410 | .addReg(StartSrcReg).addMBB(StartMBB) |
| 5411 | .addReg(NextSrcReg).addMBB(NextMBB); |
| 5412 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg) |
| 5413 | .addReg(StartCountReg).addMBB(StartMBB) |
| 5414 | .addReg(NextCountReg).addMBB(NextMBB); |
| 5415 | if (Opcode == SystemZ::MVC) |
| 5416 | BuildMI(MBB, DL, TII->get(SystemZ::PFD)) |
| 5417 | .addImm(SystemZ::PFD_WRITE) |
| 5418 | .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0); |
| 5419 | BuildMI(MBB, DL, TII->get(Opcode)) |
| 5420 | .addReg(ThisDestReg).addImm(DestDisp).addImm(256) |
| 5421 | .addReg(ThisSrcReg).addImm(SrcDisp); |
| 5422 | if (EndMBB) { |
| 5423 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5424 | .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) |
| 5425 | .addMBB(EndMBB); |
| 5426 | MBB->addSuccessor(EndMBB); |
| 5427 | MBB->addSuccessor(NextMBB); |
| 5428 | } |
| 5429 | |
| 5430 | // NextMBB: |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5431 | // %NextDestReg = LA 256(%ThisDestReg) |
| 5432 | // %NextSrcReg = LA 256(%ThisSrcReg) |
| 5433 | // %NextCountReg = AGHI %ThisCountReg, -1 |
| 5434 | // CGHI %NextCountReg, 0 |
| 5435 | // JLH LoopMBB |
| 5436 | // # fall through to DoneMMB |
| 5437 | // |
| 5438 | // The AGHI, CGHI and JLH should be converted to BRCTG by later passes. |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5439 | MBB = NextMBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5440 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5441 | BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg) |
| 5442 | .addReg(ThisDestReg).addImm(256).addReg(0); |
| 5443 | if (!HaveSingleBase) |
| 5444 | BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg) |
| 5445 | .addReg(ThisSrcReg).addImm(256).addReg(0); |
| 5446 | BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg) |
| 5447 | .addReg(ThisCountReg).addImm(-1); |
| 5448 | BuildMI(MBB, DL, TII->get(SystemZ::CGHI)) |
| 5449 | .addReg(NextCountReg).addImm(0); |
| 5450 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5451 | .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) |
| 5452 | .addMBB(LoopMBB); |
| 5453 | MBB->addSuccessor(LoopMBB); |
| 5454 | MBB->addSuccessor(DoneMBB); |
| 5455 | |
| 5456 | DestBase = MachineOperand::CreateReg(NextDestReg, false); |
| 5457 | SrcBase = MachineOperand::CreateReg(NextSrcReg, false); |
| 5458 | Length &= 255; |
| 5459 | MBB = DoneMBB; |
| 5460 | } |
| 5461 | // Handle any remaining bytes with straight-line code. |
| 5462 | while (Length > 0) { |
| 5463 | uint64_t ThisLength = std::min(Length, uint64_t(256)); |
| 5464 | // The previous iteration might have created out-of-range displacements. |
| 5465 | // Apply them using LAY if so. |
| 5466 | if (!isUInt<12>(DestDisp)) { |
| 5467 | unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); |
| 5468 | BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) |
| 5469 | .addOperand(DestBase).addImm(DestDisp).addReg(0); |
| 5470 | DestBase = MachineOperand::CreateReg(Reg, false); |
| 5471 | DestDisp = 0; |
| 5472 | } |
| 5473 | if (!isUInt<12>(SrcDisp)) { |
| 5474 | unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); |
| 5475 | BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) |
| 5476 | .addOperand(SrcBase).addImm(SrcDisp).addReg(0); |
| 5477 | SrcBase = MachineOperand::CreateReg(Reg, false); |
| 5478 | SrcDisp = 0; |
| 5479 | } |
| 5480 | BuildMI(*MBB, MI, DL, TII->get(Opcode)) |
| 5481 | .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength) |
| 5482 | .addOperand(SrcBase).addImm(SrcDisp); |
| 5483 | DestDisp += ThisLength; |
| 5484 | SrcDisp += ThisLength; |
| 5485 | Length -= ThisLength; |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5486 | // If there's another CLC to go, branch to the end if a difference |
| 5487 | // was found. |
| 5488 | if (EndMBB && Length > 0) { |
| 5489 | MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB); |
| 5490 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5491 | .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) |
| 5492 | .addMBB(EndMBB); |
| 5493 | MBB->addSuccessor(EndMBB); |
| 5494 | MBB->addSuccessor(NextMBB); |
| 5495 | MBB = NextMBB; |
| 5496 | } |
| 5497 | } |
| 5498 | if (EndMBB) { |
| 5499 | MBB->addSuccessor(EndMBB); |
| 5500 | MBB = EndMBB; |
| 5501 | MBB->addLiveIn(SystemZ::CC); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5502 | } |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 5503 | |
| 5504 | MI->eraseFromParent(); |
| 5505 | return MBB; |
| 5506 | } |
| 5507 | |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5508 | // Decompose string pseudo-instruction MI into a loop that continually performs |
| 5509 | // Opcode until CC != 3. |
| 5510 | MachineBasicBlock * |
| 5511 | SystemZTargetLowering::emitStringWrapper(MachineInstr *MI, |
| 5512 | MachineBasicBlock *MBB, |
| 5513 | unsigned Opcode) const { |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5514 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 5515 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 5516 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5517 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 5518 | DebugLoc DL = MI->getDebugLoc(); |
| 5519 | |
| 5520 | uint64_t End1Reg = MI->getOperand(0).getReg(); |
| 5521 | uint64_t Start1Reg = MI->getOperand(1).getReg(); |
| 5522 | uint64_t Start2Reg = MI->getOperand(2).getReg(); |
| 5523 | uint64_t CharReg = MI->getOperand(3).getReg(); |
| 5524 | |
| 5525 | const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass; |
| 5526 | uint64_t This1Reg = MRI.createVirtualRegister(RC); |
| 5527 | uint64_t This2Reg = MRI.createVirtualRegister(RC); |
| 5528 | uint64_t End2Reg = MRI.createVirtualRegister(RC); |
| 5529 | |
| 5530 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5531 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5532 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
| 5533 | |
| 5534 | // StartMBB: |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5535 | // # fall through to LoopMMB |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5536 | MBB->addSuccessor(LoopMBB); |
| 5537 | |
| 5538 | // LoopMBB: |
| 5539 | // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ] |
| 5540 | // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ] |
Richard Sandiford | 7789b08 | 2013-09-30 08:48:38 +0000 | [diff] [blame] | 5541 | // R0L = %CharReg |
| 5542 | // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5543 | // JO LoopMBB |
| 5544 | // # fall through to DoneMMB |
Richard Sandiford | 6f6d551 | 2013-08-20 09:38:48 +0000 | [diff] [blame] | 5545 | // |
Richard Sandiford | 7789b08 | 2013-09-30 08:48:38 +0000 | [diff] [blame] | 5546 | // The load of R0L can be hoisted by post-RA LICM. |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5547 | MBB = LoopMBB; |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5548 | |
| 5549 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg) |
| 5550 | .addReg(Start1Reg).addMBB(StartMBB) |
| 5551 | .addReg(End1Reg).addMBB(LoopMBB); |
| 5552 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg) |
| 5553 | .addReg(Start2Reg).addMBB(StartMBB) |
| 5554 | .addReg(End2Reg).addMBB(LoopMBB); |
Richard Sandiford | 7789b08 | 2013-09-30 08:48:38 +0000 | [diff] [blame] | 5555 | BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg); |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5556 | BuildMI(MBB, DL, TII->get(Opcode)) |
| 5557 | .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define) |
| 5558 | .addReg(This1Reg).addReg(This2Reg); |
| 5559 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5560 | .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB); |
| 5561 | MBB->addSuccessor(LoopMBB); |
| 5562 | MBB->addSuccessor(DoneMBB); |
| 5563 | |
| 5564 | DoneMBB->addLiveIn(SystemZ::CC); |
| 5565 | |
| 5566 | MI->eraseFromParent(); |
| 5567 | return DoneMBB; |
| 5568 | } |
| 5569 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 5570 | // Update TBEGIN instruction with final opcode and register clobbers. |
| 5571 | MachineBasicBlock * |
| 5572 | SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI, |
| 5573 | MachineBasicBlock *MBB, |
| 5574 | unsigned Opcode, |
| 5575 | bool NoFloat) const { |
| 5576 | MachineFunction &MF = *MBB->getParent(); |
| 5577 | const TargetFrameLowering *TFI = Subtarget.getFrameLowering(); |
| 5578 | const SystemZInstrInfo *TII = Subtarget.getInstrInfo(); |
| 5579 | |
| 5580 | // Update opcode. |
| 5581 | MI->setDesc(TII->get(Opcode)); |
| 5582 | |
| 5583 | // We cannot handle a TBEGIN that clobbers the stack or frame pointer. |
| 5584 | // Make sure to add the corresponding GRSM bits if they are missing. |
| 5585 | uint64_t Control = MI->getOperand(2).getImm(); |
| 5586 | static const unsigned GPRControlBit[16] = { |
| 5587 | 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000, |
| 5588 | 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100 |
| 5589 | }; |
| 5590 | Control |= GPRControlBit[15]; |
| 5591 | if (TFI->hasFP(MF)) |
| 5592 | Control |= GPRControlBit[11]; |
| 5593 | MI->getOperand(2).setImm(Control); |
| 5594 | |
| 5595 | // Add GPR clobbers. |
| 5596 | for (int I = 0; I < 16; I++) { |
| 5597 | if ((Control & GPRControlBit[I]) == 0) { |
| 5598 | unsigned Reg = SystemZMC::GR64Regs[I]; |
| 5599 | MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); |
| 5600 | } |
| 5601 | } |
| 5602 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 5603 | // Add FPR/VR clobbers. |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 5604 | if (!NoFloat && (Control & 4) != 0) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 5605 | if (Subtarget.hasVector()) { |
| 5606 | for (int I = 0; I < 32; I++) { |
| 5607 | unsigned Reg = SystemZMC::VR128Regs[I]; |
| 5608 | MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); |
| 5609 | } |
| 5610 | } else { |
| 5611 | for (int I = 0; I < 16; I++) { |
| 5612 | unsigned Reg = SystemZMC::FP64Regs[I]; |
| 5613 | MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); |
| 5614 | } |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 5615 | } |
| 5616 | } |
| 5617 | |
| 5618 | return MBB; |
| 5619 | } |
| 5620 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5621 | MachineBasicBlock *SystemZTargetLowering:: |
| 5622 | EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const { |
| 5623 | switch (MI->getOpcode()) { |
Richard Sandiford | 7c5c0ea | 2013-10-01 13:10:16 +0000 | [diff] [blame] | 5624 | case SystemZ::Select32Mux: |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5625 | case SystemZ::Select32: |
| 5626 | case SystemZ::SelectF32: |
| 5627 | case SystemZ::Select64: |
| 5628 | case SystemZ::SelectF64: |
| 5629 | case SystemZ::SelectF128: |
| 5630 | return emitSelect(MI, MBB); |
| 5631 | |
Richard Sandiford | 2896d04 | 2013-10-01 14:33:55 +0000 | [diff] [blame] | 5632 | case SystemZ::CondStore8Mux: |
| 5633 | return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false); |
| 5634 | case SystemZ::CondStore8MuxInv: |
| 5635 | return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true); |
| 5636 | case SystemZ::CondStore16Mux: |
| 5637 | return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false); |
| 5638 | case SystemZ::CondStore16MuxInv: |
| 5639 | return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5640 | case SystemZ::CondStore8: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5641 | return emitCondStore(MI, MBB, SystemZ::STC, 0, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5642 | case SystemZ::CondStore8Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5643 | return emitCondStore(MI, MBB, SystemZ::STC, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5644 | case SystemZ::CondStore16: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5645 | return emitCondStore(MI, MBB, SystemZ::STH, 0, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5646 | case SystemZ::CondStore16Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5647 | return emitCondStore(MI, MBB, SystemZ::STH, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5648 | case SystemZ::CondStore32: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5649 | return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5650 | case SystemZ::CondStore32Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5651 | return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5652 | case SystemZ::CondStore64: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5653 | return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5654 | case SystemZ::CondStore64Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5655 | return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5656 | case SystemZ::CondStoreF32: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5657 | return emitCondStore(MI, MBB, SystemZ::STE, 0, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5658 | case SystemZ::CondStoreF32Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5659 | return emitCondStore(MI, MBB, SystemZ::STE, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5660 | case SystemZ::CondStoreF64: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5661 | return emitCondStore(MI, MBB, SystemZ::STD, 0, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5662 | case SystemZ::CondStoreF64Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5663 | return emitCondStore(MI, MBB, SystemZ::STD, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5664 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5665 | case SystemZ::AEXT128_64: |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 5666 | return emitExt128(MI, MBB, false, SystemZ::subreg_l64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5667 | case SystemZ::ZEXT128_32: |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 5668 | return emitExt128(MI, MBB, true, SystemZ::subreg_l32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5669 | case SystemZ::ZEXT128_64: |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 5670 | return emitExt128(MI, MBB, true, SystemZ::subreg_l64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5671 | |
| 5672 | case SystemZ::ATOMIC_SWAPW: |
| 5673 | return emitAtomicLoadBinary(MI, MBB, 0, 0); |
| 5674 | case SystemZ::ATOMIC_SWAP_32: |
| 5675 | return emitAtomicLoadBinary(MI, MBB, 0, 32); |
| 5676 | case SystemZ::ATOMIC_SWAP_64: |
| 5677 | return emitAtomicLoadBinary(MI, MBB, 0, 64); |
| 5678 | |
| 5679 | case SystemZ::ATOMIC_LOADW_AR: |
| 5680 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0); |
| 5681 | case SystemZ::ATOMIC_LOADW_AFI: |
| 5682 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0); |
| 5683 | case SystemZ::ATOMIC_LOAD_AR: |
| 5684 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32); |
| 5685 | case SystemZ::ATOMIC_LOAD_AHI: |
| 5686 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32); |
| 5687 | case SystemZ::ATOMIC_LOAD_AFI: |
| 5688 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32); |
| 5689 | case SystemZ::ATOMIC_LOAD_AGR: |
| 5690 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64); |
| 5691 | case SystemZ::ATOMIC_LOAD_AGHI: |
| 5692 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64); |
| 5693 | case SystemZ::ATOMIC_LOAD_AGFI: |
| 5694 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64); |
| 5695 | |
| 5696 | case SystemZ::ATOMIC_LOADW_SR: |
| 5697 | return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0); |
| 5698 | case SystemZ::ATOMIC_LOAD_SR: |
| 5699 | return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32); |
| 5700 | case SystemZ::ATOMIC_LOAD_SGR: |
| 5701 | return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64); |
| 5702 | |
| 5703 | case SystemZ::ATOMIC_LOADW_NR: |
| 5704 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0); |
| 5705 | case SystemZ::ATOMIC_LOADW_NILH: |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5706 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5707 | case SystemZ::ATOMIC_LOAD_NR: |
| 5708 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5709 | case SystemZ::ATOMIC_LOAD_NILL: |
| 5710 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32); |
| 5711 | case SystemZ::ATOMIC_LOAD_NILH: |
| 5712 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32); |
| 5713 | case SystemZ::ATOMIC_LOAD_NILF: |
| 5714 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5715 | case SystemZ::ATOMIC_LOAD_NGR: |
| 5716 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5717 | case SystemZ::ATOMIC_LOAD_NILL64: |
| 5718 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64); |
| 5719 | case SystemZ::ATOMIC_LOAD_NILH64: |
| 5720 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 5721 | case SystemZ::ATOMIC_LOAD_NIHL64: |
| 5722 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64); |
| 5723 | case SystemZ::ATOMIC_LOAD_NIHH64: |
| 5724 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5725 | case SystemZ::ATOMIC_LOAD_NILF64: |
| 5726 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 5727 | case SystemZ::ATOMIC_LOAD_NIHF64: |
| 5728 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5729 | |
| 5730 | case SystemZ::ATOMIC_LOADW_OR: |
| 5731 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0); |
| 5732 | case SystemZ::ATOMIC_LOADW_OILH: |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5733 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5734 | case SystemZ::ATOMIC_LOAD_OR: |
| 5735 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5736 | case SystemZ::ATOMIC_LOAD_OILL: |
| 5737 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32); |
| 5738 | case SystemZ::ATOMIC_LOAD_OILH: |
| 5739 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32); |
| 5740 | case SystemZ::ATOMIC_LOAD_OILF: |
| 5741 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5742 | case SystemZ::ATOMIC_LOAD_OGR: |
| 5743 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5744 | case SystemZ::ATOMIC_LOAD_OILL64: |
| 5745 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64); |
| 5746 | case SystemZ::ATOMIC_LOAD_OILH64: |
| 5747 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64); |
Richard Sandiford | 6e96ac6 | 2013-10-01 13:22:41 +0000 | [diff] [blame] | 5748 | case SystemZ::ATOMIC_LOAD_OIHL64: |
| 5749 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64); |
| 5750 | case SystemZ::ATOMIC_LOAD_OIHH64: |
| 5751 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5752 | case SystemZ::ATOMIC_LOAD_OILF64: |
| 5753 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64); |
Richard Sandiford | 6e96ac6 | 2013-10-01 13:22:41 +0000 | [diff] [blame] | 5754 | case SystemZ::ATOMIC_LOAD_OIHF64: |
| 5755 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5756 | |
| 5757 | case SystemZ::ATOMIC_LOADW_XR: |
| 5758 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0); |
| 5759 | case SystemZ::ATOMIC_LOADW_XILF: |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5760 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5761 | case SystemZ::ATOMIC_LOAD_XR: |
| 5762 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5763 | case SystemZ::ATOMIC_LOAD_XILF: |
| 5764 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5765 | case SystemZ::ATOMIC_LOAD_XGR: |
| 5766 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5767 | case SystemZ::ATOMIC_LOAD_XILF64: |
| 5768 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64); |
Richard Sandiford | 5718dac | 2013-10-01 14:08:44 +0000 | [diff] [blame] | 5769 | case SystemZ::ATOMIC_LOAD_XIHF64: |
| 5770 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5771 | |
| 5772 | case SystemZ::ATOMIC_LOADW_NRi: |
| 5773 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true); |
| 5774 | case SystemZ::ATOMIC_LOADW_NILHi: |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5775 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5776 | case SystemZ::ATOMIC_LOAD_NRi: |
| 5777 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5778 | case SystemZ::ATOMIC_LOAD_NILLi: |
| 5779 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true); |
| 5780 | case SystemZ::ATOMIC_LOAD_NILHi: |
| 5781 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true); |
| 5782 | case SystemZ::ATOMIC_LOAD_NILFi: |
| 5783 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5784 | case SystemZ::ATOMIC_LOAD_NGRi: |
| 5785 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5786 | case SystemZ::ATOMIC_LOAD_NILL64i: |
| 5787 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true); |
| 5788 | case SystemZ::ATOMIC_LOAD_NILH64i: |
| 5789 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 5790 | case SystemZ::ATOMIC_LOAD_NIHL64i: |
| 5791 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true); |
| 5792 | case SystemZ::ATOMIC_LOAD_NIHH64i: |
| 5793 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5794 | case SystemZ::ATOMIC_LOAD_NILF64i: |
| 5795 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 5796 | case SystemZ::ATOMIC_LOAD_NIHF64i: |
| 5797 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5798 | |
| 5799 | case SystemZ::ATOMIC_LOADW_MIN: |
| 5800 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, |
| 5801 | SystemZ::CCMASK_CMP_LE, 0); |
| 5802 | case SystemZ::ATOMIC_LOAD_MIN_32: |
| 5803 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, |
| 5804 | SystemZ::CCMASK_CMP_LE, 32); |
| 5805 | case SystemZ::ATOMIC_LOAD_MIN_64: |
| 5806 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, |
| 5807 | SystemZ::CCMASK_CMP_LE, 64); |
| 5808 | |
| 5809 | case SystemZ::ATOMIC_LOADW_MAX: |
| 5810 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, |
| 5811 | SystemZ::CCMASK_CMP_GE, 0); |
| 5812 | case SystemZ::ATOMIC_LOAD_MAX_32: |
| 5813 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, |
| 5814 | SystemZ::CCMASK_CMP_GE, 32); |
| 5815 | case SystemZ::ATOMIC_LOAD_MAX_64: |
| 5816 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, |
| 5817 | SystemZ::CCMASK_CMP_GE, 64); |
| 5818 | |
| 5819 | case SystemZ::ATOMIC_LOADW_UMIN: |
| 5820 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, |
| 5821 | SystemZ::CCMASK_CMP_LE, 0); |
| 5822 | case SystemZ::ATOMIC_LOAD_UMIN_32: |
| 5823 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, |
| 5824 | SystemZ::CCMASK_CMP_LE, 32); |
| 5825 | case SystemZ::ATOMIC_LOAD_UMIN_64: |
| 5826 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, |
| 5827 | SystemZ::CCMASK_CMP_LE, 64); |
| 5828 | |
| 5829 | case SystemZ::ATOMIC_LOADW_UMAX: |
| 5830 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, |
| 5831 | SystemZ::CCMASK_CMP_GE, 0); |
| 5832 | case SystemZ::ATOMIC_LOAD_UMAX_32: |
| 5833 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, |
| 5834 | SystemZ::CCMASK_CMP_GE, 32); |
| 5835 | case SystemZ::ATOMIC_LOAD_UMAX_64: |
| 5836 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, |
| 5837 | SystemZ::CCMASK_CMP_GE, 64); |
| 5838 | |
| 5839 | case SystemZ::ATOMIC_CMP_SWAPW: |
| 5840 | return emitAtomicCmpSwapW(MI, MBB); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5841 | case SystemZ::MVCSequence: |
| 5842 | case SystemZ::MVCLoop: |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5843 | return emitMemMemWrapper(MI, MBB, SystemZ::MVC); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 5844 | case SystemZ::NCSequence: |
| 5845 | case SystemZ::NCLoop: |
| 5846 | return emitMemMemWrapper(MI, MBB, SystemZ::NC); |
| 5847 | case SystemZ::OCSequence: |
| 5848 | case SystemZ::OCLoop: |
| 5849 | return emitMemMemWrapper(MI, MBB, SystemZ::OC); |
| 5850 | case SystemZ::XCSequence: |
| 5851 | case SystemZ::XCLoop: |
| 5852 | return emitMemMemWrapper(MI, MBB, SystemZ::XC); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5853 | case SystemZ::CLCSequence: |
| 5854 | case SystemZ::CLCLoop: |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5855 | return emitMemMemWrapper(MI, MBB, SystemZ::CLC); |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5856 | case SystemZ::CLSTLoop: |
| 5857 | return emitStringWrapper(MI, MBB, SystemZ::CLST); |
Richard Sandiford | bb83a50 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 5858 | case SystemZ::MVSTLoop: |
| 5859 | return emitStringWrapper(MI, MBB, SystemZ::MVST); |
Richard Sandiford | 0dec06a | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 5860 | case SystemZ::SRSTLoop: |
| 5861 | return emitStringWrapper(MI, MBB, SystemZ::SRST); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 5862 | case SystemZ::TBEGIN: |
| 5863 | return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false); |
| 5864 | case SystemZ::TBEGIN_nofloat: |
| 5865 | return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true); |
| 5866 | case SystemZ::TBEGINC: |
| 5867 | return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5868 | default: |
| 5869 | llvm_unreachable("Unexpected instr type to insert"); |
| 5870 | } |
| 5871 | } |