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 | |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 84 | SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &tm, |
| 85 | const SystemZSubtarget &STI) |
| 86 | : TargetLowering(tm), Subtarget(STI) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 87 | MVT PtrVT = getPointerTy(); |
| 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 | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 94 | addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass); |
| 95 | addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass); |
| 96 | addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass); |
| 97 | addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass); |
| 98 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 99 | if (Subtarget.hasVector()) { |
| 100 | addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass); |
| 101 | addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass); |
| 102 | addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass); |
| 103 | addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 104 | addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 105 | addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 108 | // Compute derived properties from the register classes |
Eric Christopher | 23a3a7c | 2015-02-26 00:00:24 +0000 | [diff] [blame] | 109 | computeRegisterProperties(Subtarget.getRegisterInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 110 | |
| 111 | // Set up special registers. |
| 112 | setExceptionPointerRegister(SystemZ::R6D); |
| 113 | setExceptionSelectorRegister(SystemZ::R7D); |
| 114 | setStackPointerRegisterToSaveRestore(SystemZ::R15D); |
| 115 | |
| 116 | // TODO: It may be better to default to latency-oriented scheduling, however |
| 117 | // LLVM's current latency-oriented scheduler can't handle physreg definitions |
Richard Sandiford | 14a4449 | 2013-05-22 13:38:45 +0000 | [diff] [blame] | 118 | // 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] | 119 | // scheduler, because it can. |
| 120 | setSchedulingPreference(Sched::RegPressure); |
| 121 | |
| 122 | setBooleanContents(ZeroOrOneBooleanContent); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 123 | setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 124 | |
| 125 | // Instructions are strings of 2-byte aligned 2-byte values. |
| 126 | setMinFunctionAlignment(2); |
| 127 | |
| 128 | // Handle operations that are handled in a similar way for all types. |
| 129 | for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; |
| 130 | I <= MVT::LAST_FP_VALUETYPE; |
| 131 | ++I) { |
| 132 | MVT VT = MVT::SimpleValueType(I); |
| 133 | if (isTypeLegal(VT)) { |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 134 | // Lower SET_CC into an IPM-based sequence. |
| 135 | setOperationAction(ISD::SETCC, VT, Custom); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 136 | |
| 137 | // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE). |
| 138 | setOperationAction(ISD::SELECT, VT, Expand); |
| 139 | |
| 140 | // Lower SELECT_CC and BR_CC into separate comparisons and branches. |
| 141 | setOperationAction(ISD::SELECT_CC, VT, Custom); |
| 142 | setOperationAction(ISD::BR_CC, VT, Custom); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // Expand jump table branches as address arithmetic followed by an |
| 147 | // indirect jump. |
| 148 | setOperationAction(ISD::BR_JT, MVT::Other, Expand); |
| 149 | |
| 150 | // Expand BRCOND into a BR_CC (see above). |
| 151 | setOperationAction(ISD::BRCOND, MVT::Other, Expand); |
| 152 | |
| 153 | // Handle integer types. |
| 154 | for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; |
| 155 | I <= MVT::LAST_INTEGER_VALUETYPE; |
| 156 | ++I) { |
| 157 | MVT VT = MVT::SimpleValueType(I); |
| 158 | if (isTypeLegal(VT)) { |
| 159 | // Expand individual DIV and REMs into DIVREMs. |
| 160 | setOperationAction(ISD::SDIV, VT, Expand); |
| 161 | setOperationAction(ISD::UDIV, VT, Expand); |
| 162 | setOperationAction(ISD::SREM, VT, Expand); |
| 163 | setOperationAction(ISD::UREM, VT, Expand); |
| 164 | setOperationAction(ISD::SDIVREM, VT, Custom); |
| 165 | setOperationAction(ISD::UDIVREM, VT, Custom); |
| 166 | |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 167 | // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and |
| 168 | // stores, putting a serialization instruction after the stores. |
| 169 | setOperationAction(ISD::ATOMIC_LOAD, VT, Custom); |
| 170 | setOperationAction(ISD::ATOMIC_STORE, VT, Custom); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 171 | |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 172 | // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are |
| 173 | // available, or if the operand is constant. |
| 174 | setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom); |
| 175 | |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 176 | // Use POPCNT on z196 and above. |
| 177 | if (Subtarget.hasPopulationCount()) |
| 178 | setOperationAction(ISD::CTPOP, VT, Custom); |
| 179 | else |
| 180 | setOperationAction(ISD::CTPOP, VT, Expand); |
| 181 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 182 | // No special instructions for these. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 183 | setOperationAction(ISD::CTTZ, VT, Expand); |
| 184 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand); |
| 185 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand); |
| 186 | setOperationAction(ISD::ROTR, VT, Expand); |
| 187 | |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 188 | // Use *MUL_LOHI where possible instead of MULH*. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 189 | setOperationAction(ISD::MULHS, VT, Expand); |
| 190 | setOperationAction(ISD::MULHU, VT, Expand); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 191 | setOperationAction(ISD::SMUL_LOHI, VT, Custom); |
| 192 | setOperationAction(ISD::UMUL_LOHI, VT, Custom); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 193 | |
Richard Sandiford | dc6c2c9 | 2014-03-21 10:56:30 +0000 | [diff] [blame] | 194 | // Only z196 and above have native support for conversions to unsigned. |
| 195 | if (!Subtarget.hasFPExtension()) |
| 196 | setOperationAction(ISD::FP_TO_UINT, VT, Expand); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | // Type legalization will convert 8- and 16-bit atomic operations into |
| 201 | // forms that operate on i32s (but still keeping the original memory VT). |
| 202 | // Lower them into full i32 operations. |
| 203 | setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom); |
| 204 | setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom); |
| 205 | setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom); |
| 206 | setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom); |
| 207 | setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom); |
| 208 | setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom); |
| 209 | setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom); |
| 210 | setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom); |
| 211 | setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom); |
| 212 | setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom); |
| 213 | setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom); |
| 214 | setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); |
| 215 | |
Richard Sandiford | dc6c2c9 | 2014-03-21 10:56:30 +0000 | [diff] [blame] | 216 | // z10 has instructions for signed but not unsigned FP conversion. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 217 | // Handle unsigned 32-bit types as signed 64-bit types. |
Richard Sandiford | dc6c2c9 | 2014-03-21 10:56:30 +0000 | [diff] [blame] | 218 | if (!Subtarget.hasFPExtension()) { |
| 219 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote); |
| 220 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); |
| 221 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 222 | |
| 223 | // We have native support for a 64-bit CTLZ, via FLOGR. |
| 224 | setOperationAction(ISD::CTLZ, MVT::i32, Promote); |
| 225 | setOperationAction(ISD::CTLZ, MVT::i64, Legal); |
| 226 | |
| 227 | // Give LowerOperation the chance to replace 64-bit ORs with subregs. |
| 228 | setOperationAction(ISD::OR, MVT::i64, Custom); |
| 229 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 230 | // FIXME: Can we support these natively? |
| 231 | setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand); |
| 232 | setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand); |
| 233 | setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand); |
| 234 | |
| 235 | // 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] | 236 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 237 | for (MVT VT : MVT::integer_valuetypes()) { |
| 238 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); |
| 239 | setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote); |
| 240 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote); |
| 241 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 242 | |
| 243 | // Handle the various types of symbolic address. |
| 244 | setOperationAction(ISD::ConstantPool, PtrVT, Custom); |
| 245 | setOperationAction(ISD::GlobalAddress, PtrVT, Custom); |
| 246 | setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom); |
| 247 | setOperationAction(ISD::BlockAddress, PtrVT, Custom); |
| 248 | setOperationAction(ISD::JumpTable, PtrVT, Custom); |
| 249 | |
| 250 | // We need to handle dynamic allocations specially because of the |
| 251 | // 160-byte area at the bottom of the stack. |
| 252 | setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom); |
| 253 | |
| 254 | // Use custom expanders so that we can force the function to use |
| 255 | // a frame pointer. |
| 256 | setOperationAction(ISD::STACKSAVE, MVT::Other, Custom); |
| 257 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom); |
| 258 | |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 259 | // Handle prefetches with PFD or PFDRL. |
| 260 | setOperationAction(ISD::PREFETCH, MVT::Other, Custom); |
| 261 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 262 | for (MVT VT : MVT::vector_valuetypes()) { |
| 263 | // Assume by default that all vector operations need to be expanded. |
| 264 | for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode) |
| 265 | if (getOperationAction(Opcode, VT) == Legal) |
| 266 | setOperationAction(Opcode, VT, Expand); |
| 267 | |
| 268 | // Likewise all truncating stores and extending loads. |
| 269 | for (MVT InnerVT : MVT::vector_valuetypes()) { |
| 270 | setTruncStoreAction(VT, InnerVT, Expand); |
| 271 | setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand); |
| 272 | setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand); |
| 273 | setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand); |
| 274 | } |
| 275 | |
| 276 | if (isTypeLegal(VT)) { |
| 277 | // These operations are legal for anything that can be stored in a |
| 278 | // vector register, even if there is no native support for the format |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 279 | // as such. In particular, we can do these for v4f32 even though there |
| 280 | // are no specific instructions for that format. |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 281 | setOperationAction(ISD::LOAD, VT, Legal); |
| 282 | setOperationAction(ISD::STORE, VT, Legal); |
| 283 | setOperationAction(ISD::VSELECT, VT, Legal); |
| 284 | setOperationAction(ISD::BITCAST, VT, Legal); |
| 285 | setOperationAction(ISD::UNDEF, VT, Legal); |
| 286 | |
| 287 | // Likewise, except that we need to replace the nodes with something |
| 288 | // more specific. |
| 289 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); |
| 290 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // Handle integer vector types. |
| 295 | for (MVT VT : MVT::integer_vector_valuetypes()) { |
| 296 | if (isTypeLegal(VT)) { |
| 297 | // These operations have direct equivalents. |
| 298 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal); |
| 299 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal); |
| 300 | setOperationAction(ISD::ADD, VT, Legal); |
| 301 | setOperationAction(ISD::SUB, VT, Legal); |
| 302 | if (VT != MVT::v2i64) |
| 303 | setOperationAction(ISD::MUL, VT, Legal); |
| 304 | setOperationAction(ISD::AND, VT, Legal); |
| 305 | setOperationAction(ISD::OR, VT, Legal); |
| 306 | setOperationAction(ISD::XOR, VT, Legal); |
| 307 | setOperationAction(ISD::CTPOP, VT, Custom); |
| 308 | setOperationAction(ISD::CTTZ, VT, Legal); |
| 309 | setOperationAction(ISD::CTLZ, VT, Legal); |
| 310 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Custom); |
| 311 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Custom); |
| 312 | |
| 313 | // Convert a GPR scalar to a vector by inserting it into element 0. |
| 314 | setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom); |
| 315 | |
| 316 | // Detect shifts by a scalar amount and convert them into |
| 317 | // V*_BY_SCALAR. |
| 318 | setOperationAction(ISD::SHL, VT, Custom); |
| 319 | setOperationAction(ISD::SRA, VT, Custom); |
| 320 | setOperationAction(ISD::SRL, VT, Custom); |
| 321 | |
| 322 | // At present ROTL isn't matched by DAGCombiner. ROTR should be |
| 323 | // converted into ROTL. |
| 324 | setOperationAction(ISD::ROTL, VT, Expand); |
| 325 | setOperationAction(ISD::ROTR, VT, Expand); |
| 326 | |
| 327 | // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands |
| 328 | // and inverting the result as necessary. |
| 329 | setOperationAction(ISD::SETCC, VT, Custom); |
| 330 | } |
| 331 | } |
| 332 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 333 | if (Subtarget.hasVector()) { |
| 334 | // There should be no need to check for float types other than v2f64 |
| 335 | // since <2 x f32> isn't a legal type. |
| 336 | setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal); |
| 337 | setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal); |
| 338 | setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal); |
| 339 | setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal); |
| 340 | } |
| 341 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 342 | // Handle floating-point types. |
| 343 | for (unsigned I = MVT::FIRST_FP_VALUETYPE; |
| 344 | I <= MVT::LAST_FP_VALUETYPE; |
| 345 | ++I) { |
| 346 | MVT VT = MVT::SimpleValueType(I); |
| 347 | if (isTypeLegal(VT)) { |
| 348 | // We can use FI for FRINT. |
| 349 | setOperationAction(ISD::FRINT, VT, Legal); |
| 350 | |
Richard Sandiford | af5f66a | 2013-08-21 09:04:20 +0000 | [diff] [blame] | 351 | // We can use the extended form of FI for other rounding operations. |
| 352 | if (Subtarget.hasFPExtension()) { |
| 353 | setOperationAction(ISD::FNEARBYINT, VT, Legal); |
| 354 | setOperationAction(ISD::FFLOOR, VT, Legal); |
| 355 | setOperationAction(ISD::FCEIL, VT, Legal); |
| 356 | setOperationAction(ISD::FTRUNC, VT, Legal); |
| 357 | setOperationAction(ISD::FROUND, VT, Legal); |
| 358 | } |
| 359 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 360 | // No special instructions for these. |
| 361 | setOperationAction(ISD::FSIN, VT, Expand); |
| 362 | setOperationAction(ISD::FCOS, VT, Expand); |
| 363 | setOperationAction(ISD::FREM, VT, Expand); |
| 364 | } |
| 365 | } |
| 366 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 367 | // Handle floating-point vector types. |
| 368 | if (Subtarget.hasVector()) { |
| 369 | // Scalar-to-vector conversion is just a subreg. |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 370 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 371 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal); |
| 372 | |
| 373 | // Some insertions and extractions can be done directly but others |
| 374 | // need to go via integers. |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 375 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 376 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 377 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 378 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom); |
| 379 | |
| 380 | // These operations have direct equivalents. |
| 381 | setOperationAction(ISD::FADD, MVT::v2f64, Legal); |
| 382 | setOperationAction(ISD::FNEG, MVT::v2f64, Legal); |
| 383 | setOperationAction(ISD::FSUB, MVT::v2f64, Legal); |
| 384 | setOperationAction(ISD::FMUL, MVT::v2f64, Legal); |
| 385 | setOperationAction(ISD::FMA, MVT::v2f64, Legal); |
| 386 | setOperationAction(ISD::FDIV, MVT::v2f64, Legal); |
| 387 | setOperationAction(ISD::FABS, MVT::v2f64, Legal); |
| 388 | setOperationAction(ISD::FSQRT, MVT::v2f64, Legal); |
| 389 | setOperationAction(ISD::FRINT, MVT::v2f64, Legal); |
| 390 | setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal); |
| 391 | setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal); |
| 392 | setOperationAction(ISD::FCEIL, MVT::v2f64, Legal); |
| 393 | setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal); |
| 394 | setOperationAction(ISD::FROUND, MVT::v2f64, Legal); |
| 395 | } |
| 396 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 397 | // We have fused multiply-addition for f32 and f64 but not f128. |
| 398 | setOperationAction(ISD::FMA, MVT::f32, Legal); |
| 399 | setOperationAction(ISD::FMA, MVT::f64, Legal); |
| 400 | setOperationAction(ISD::FMA, MVT::f128, Expand); |
| 401 | |
| 402 | // Needed so that we don't try to implement f128 constant loads using |
| 403 | // a load-and-extend of a f80 constant (in cases where the constant |
| 404 | // would fit in an f80). |
Ahmed Bougacha | 2b6917b | 2015-01-08 00:51:32 +0000 | [diff] [blame] | 405 | for (MVT VT : MVT::fp_valuetypes()) |
| 406 | setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 407 | |
| 408 | // Floating-point truncation and stores need to be done separately. |
| 409 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); |
| 410 | setTruncStoreAction(MVT::f128, MVT::f32, Expand); |
| 411 | setTruncStoreAction(MVT::f128, MVT::f64, Expand); |
| 412 | |
| 413 | // We have 64-bit FPR<->GPR moves, but need special handling for |
| 414 | // 32-bit forms. |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 415 | if (!Subtarget.hasVector()) { |
| 416 | setOperationAction(ISD::BITCAST, MVT::i32, Custom); |
| 417 | setOperationAction(ISD::BITCAST, MVT::f32, Custom); |
| 418 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 419 | |
| 420 | // VASTART and VACOPY need to deal with the SystemZ-specific varargs |
| 421 | // structure, but VAEND is a no-op. |
| 422 | setOperationAction(ISD::VASTART, MVT::Other, Custom); |
| 423 | setOperationAction(ISD::VACOPY, MVT::Other, Custom); |
| 424 | setOperationAction(ISD::VAEND, MVT::Other, Expand); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 425 | |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 426 | // Codes for which we want to perform some z-specific combinations. |
| 427 | setTargetDAGCombine(ISD::SIGN_EXTEND); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 428 | setTargetDAGCombine(ISD::STORE); |
| 429 | setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 430 | setTargetDAGCombine(ISD::FP_ROUND); |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 431 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 432 | // Handle intrinsics. |
| 433 | setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); |
| 434 | |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 435 | // We want to use MVC in preference to even a single load/store pair. |
| 436 | MaxStoresPerMemcpy = 0; |
| 437 | MaxStoresPerMemcpyOptSize = 0; |
Richard Sandiford | 47660c1 | 2013-07-09 09:32:42 +0000 | [diff] [blame] | 438 | |
| 439 | // The main memset sequence is a byte store followed by an MVC. |
| 440 | // Two STC or MV..I stores win over that, but the kind of fused stores |
| 441 | // generated by target-independent code don't when the byte value is |
| 442 | // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better |
| 443 | // than "STC;MVC". Handle the choice in target-specific code instead. |
| 444 | MaxStoresPerMemset = 0; |
| 445 | MaxStoresPerMemsetOptSize = 0; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Richard Sandiford | abc010b | 2013-11-06 12:16:02 +0000 | [diff] [blame] | 448 | EVT SystemZTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { |
| 449 | if (!VT.isVector()) |
| 450 | return MVT::i32; |
| 451 | return VT.changeVectorElementTypeToInteger(); |
| 452 | } |
| 453 | |
| 454 | bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { |
Stephen Lin | 73de7bf | 2013-07-09 18:16:56 +0000 | [diff] [blame] | 455 | VT = VT.getScalarType(); |
| 456 | |
| 457 | if (!VT.isSimple()) |
| 458 | return false; |
| 459 | |
| 460 | switch (VT.getSimpleVT().SimpleTy) { |
| 461 | case MVT::f32: |
| 462 | case MVT::f64: |
| 463 | return true; |
| 464 | case MVT::f128: |
| 465 | return false; |
| 466 | default: |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | return false; |
| 471 | } |
| 472 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 473 | bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { |
| 474 | // We can load zero using LZ?R and negative zero using LZ?R;LC?BR. |
| 475 | return Imm.isZero() || Imm.isNegZero(); |
| 476 | } |
| 477 | |
Ulrich Weigand | 1f6666a | 2015-03-31 12:52:27 +0000 | [diff] [blame] | 478 | bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const { |
| 479 | // We can use CGFI or CLGFI. |
| 480 | return isInt<32>(Imm) || isUInt<32>(Imm); |
| 481 | } |
| 482 | |
| 483 | bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const { |
| 484 | // We can use ALGFI or SLGFI. |
| 485 | return isUInt<32>(Imm) || isUInt<32>(-Imm); |
| 486 | } |
| 487 | |
Matt Arsenault | 6f2a526 | 2014-07-27 17:46:40 +0000 | [diff] [blame] | 488 | bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT, |
| 489 | unsigned, |
| 490 | unsigned, |
| 491 | bool *Fast) const { |
Richard Sandiford | 46af5a2 | 2013-05-30 09:45:42 +0000 | [diff] [blame] | 492 | // Unaligned accesses should never be slower than the expanded version. |
| 493 | // We check specifically for aligned accesses in the few cases where |
| 494 | // they are required. |
| 495 | if (Fast) |
| 496 | *Fast = true; |
| 497 | return true; |
| 498 | } |
| 499 | |
Richard Sandiford | 791bea4 | 2013-07-31 12:58:26 +0000 | [diff] [blame] | 500 | bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM, |
| 501 | Type *Ty) const { |
| 502 | // Punt on globals for now, although they can be used in limited |
| 503 | // RELATIVE LONG cases. |
| 504 | if (AM.BaseGV) |
| 505 | return false; |
| 506 | |
| 507 | // Require a 20-bit signed offset. |
| 508 | if (!isInt<20>(AM.BaseOffs)) |
| 509 | return false; |
| 510 | |
| 511 | // Indexing is OK but no scale factor can be applied. |
| 512 | return AM.Scale == 0 || AM.Scale == 1; |
| 513 | } |
| 514 | |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 515 | bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const { |
| 516 | if (!FromType->isIntegerTy() || !ToType->isIntegerTy()) |
| 517 | return false; |
| 518 | unsigned FromBits = FromType->getPrimitiveSizeInBits(); |
| 519 | unsigned ToBits = ToType->getPrimitiveSizeInBits(); |
| 520 | return FromBits > ToBits; |
| 521 | } |
| 522 | |
| 523 | bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const { |
| 524 | if (!FromVT.isInteger() || !ToVT.isInteger()) |
| 525 | return false; |
| 526 | unsigned FromBits = FromVT.getSizeInBits(); |
| 527 | unsigned ToBits = ToVT.getSizeInBits(); |
| 528 | return FromBits > ToBits; |
| 529 | } |
| 530 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 531 | //===----------------------------------------------------------------------===// |
| 532 | // Inline asm support |
| 533 | //===----------------------------------------------------------------------===// |
| 534 | |
| 535 | TargetLowering::ConstraintType |
| 536 | SystemZTargetLowering::getConstraintType(const std::string &Constraint) const { |
| 537 | if (Constraint.size() == 1) { |
| 538 | switch (Constraint[0]) { |
| 539 | case 'a': // Address register |
| 540 | case 'd': // Data register (equivalent to 'r') |
| 541 | case 'f': // Floating-point register |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 542 | case 'h': // High-part register |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 543 | case 'r': // General-purpose register |
| 544 | return C_RegisterClass; |
| 545 | |
| 546 | case 'Q': // Memory with base and unsigned 12-bit displacement |
| 547 | case 'R': // Likewise, plus an index |
| 548 | case 'S': // Memory with base and signed 20-bit displacement |
| 549 | case 'T': // Likewise, plus an index |
| 550 | case 'm': // Equivalent to 'T'. |
| 551 | return C_Memory; |
| 552 | |
| 553 | case 'I': // Unsigned 8-bit constant |
| 554 | case 'J': // Unsigned 12-bit constant |
| 555 | case 'K': // Signed 16-bit constant |
| 556 | case 'L': // Signed 20-bit displacement (on all targets we support) |
| 557 | case 'M': // 0x7fffffff |
| 558 | return C_Other; |
| 559 | |
| 560 | default: |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | return TargetLowering::getConstraintType(Constraint); |
| 565 | } |
| 566 | |
| 567 | TargetLowering::ConstraintWeight SystemZTargetLowering:: |
| 568 | getSingleConstraintMatchWeight(AsmOperandInfo &info, |
| 569 | const char *constraint) const { |
| 570 | ConstraintWeight weight = CW_Invalid; |
| 571 | Value *CallOperandVal = info.CallOperandVal; |
| 572 | // If we don't have a value, we can't do a match, |
| 573 | // but allow it at the lowest weight. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 574 | if (!CallOperandVal) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 575 | return CW_Default; |
| 576 | Type *type = CallOperandVal->getType(); |
| 577 | // Look at the constraint type. |
| 578 | switch (*constraint) { |
| 579 | default: |
| 580 | weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); |
| 581 | break; |
| 582 | |
| 583 | case 'a': // Address register |
| 584 | case 'd': // Data register (equivalent to 'r') |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 585 | case 'h': // High-part register |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 586 | case 'r': // General-purpose register |
| 587 | if (CallOperandVal->getType()->isIntegerTy()) |
| 588 | weight = CW_Register; |
| 589 | break; |
| 590 | |
| 591 | case 'f': // Floating-point register |
| 592 | if (type->isFloatingPointTy()) |
| 593 | weight = CW_Register; |
| 594 | break; |
| 595 | |
| 596 | case 'I': // Unsigned 8-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 597 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 598 | if (isUInt<8>(C->getZExtValue())) |
| 599 | weight = CW_Constant; |
| 600 | break; |
| 601 | |
| 602 | case 'J': // Unsigned 12-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 603 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 604 | if (isUInt<12>(C->getZExtValue())) |
| 605 | weight = CW_Constant; |
| 606 | break; |
| 607 | |
| 608 | case 'K': // Signed 16-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 609 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 610 | if (isInt<16>(C->getSExtValue())) |
| 611 | weight = CW_Constant; |
| 612 | break; |
| 613 | |
| 614 | case 'L': // Signed 20-bit displacement (on all targets we support) |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 615 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 616 | if (isInt<20>(C->getSExtValue())) |
| 617 | weight = CW_Constant; |
| 618 | break; |
| 619 | |
| 620 | case 'M': // 0x7fffffff |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 621 | if (auto *C = dyn_cast<ConstantInt>(CallOperandVal)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 622 | if (C->getZExtValue() == 0x7fffffff) |
| 623 | weight = CW_Constant; |
| 624 | break; |
| 625 | } |
| 626 | return weight; |
| 627 | } |
| 628 | |
Richard Sandiford | b820405 | 2013-07-12 09:08:12 +0000 | [diff] [blame] | 629 | // Parse a "{tNNN}" register constraint for which the register type "t" |
| 630 | // has already been verified. MC is the class associated with "t" and |
| 631 | // Map maps 0-based register numbers to LLVM register numbers. |
| 632 | static std::pair<unsigned, const TargetRegisterClass *> |
| 633 | parseRegisterNumber(const std::string &Constraint, |
| 634 | const TargetRegisterClass *RC, const unsigned *Map) { |
| 635 | assert(*(Constraint.end()-1) == '}' && "Missing '}'"); |
| 636 | if (isdigit(Constraint[2])) { |
| 637 | std::string Suffix(Constraint.data() + 2, Constraint.size() - 2); |
| 638 | unsigned Index = atoi(Suffix.c_str()); |
| 639 | if (Index < 16 && Map[Index]) |
| 640 | return std::make_pair(Map[Index], RC); |
| 641 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 642 | return std::make_pair(0U, nullptr); |
Richard Sandiford | b820405 | 2013-07-12 09:08:12 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 645 | std::pair<unsigned, const TargetRegisterClass *> |
| 646 | SystemZTargetLowering::getRegForInlineAsmConstraint( |
| 647 | const TargetRegisterInfo *TRI, const std::string &Constraint, |
| 648 | MVT VT) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 649 | if (Constraint.size() == 1) { |
| 650 | // GCC Constraint Letters |
| 651 | switch (Constraint[0]) { |
| 652 | default: break; |
| 653 | case 'd': // Data register (equivalent to 'r') |
| 654 | case 'r': // General-purpose register |
| 655 | if (VT == MVT::i64) |
| 656 | return std::make_pair(0U, &SystemZ::GR64BitRegClass); |
| 657 | else if (VT == MVT::i128) |
| 658 | return std::make_pair(0U, &SystemZ::GR128BitRegClass); |
| 659 | return std::make_pair(0U, &SystemZ::GR32BitRegClass); |
| 660 | |
| 661 | case 'a': // Address register |
| 662 | if (VT == MVT::i64) |
| 663 | return std::make_pair(0U, &SystemZ::ADDR64BitRegClass); |
| 664 | else if (VT == MVT::i128) |
| 665 | return std::make_pair(0U, &SystemZ::ADDR128BitRegClass); |
| 666 | return std::make_pair(0U, &SystemZ::ADDR32BitRegClass); |
| 667 | |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 668 | case 'h': // High-part register (an LLVM extension) |
| 669 | return std::make_pair(0U, &SystemZ::GRH32BitRegClass); |
| 670 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 671 | case 'f': // Floating-point register |
| 672 | if (VT == MVT::f64) |
| 673 | return std::make_pair(0U, &SystemZ::FP64BitRegClass); |
| 674 | else if (VT == MVT::f128) |
| 675 | return std::make_pair(0U, &SystemZ::FP128BitRegClass); |
| 676 | return std::make_pair(0U, &SystemZ::FP32BitRegClass); |
| 677 | } |
| 678 | } |
Richard Sandiford | b820405 | 2013-07-12 09:08:12 +0000 | [diff] [blame] | 679 | if (Constraint[0] == '{') { |
| 680 | // We need to override the default register parsing for GPRs and FPRs |
| 681 | // because the interpretation depends on VT. The internal names of |
| 682 | // the registers are also different from the external names |
| 683 | // (F0D and F0S instead of F0, etc.). |
| 684 | if (Constraint[1] == 'r') { |
| 685 | if (VT == MVT::i32) |
| 686 | return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass, |
| 687 | SystemZMC::GR32Regs); |
| 688 | if (VT == MVT::i128) |
| 689 | return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass, |
| 690 | SystemZMC::GR128Regs); |
| 691 | return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass, |
| 692 | SystemZMC::GR64Regs); |
| 693 | } |
| 694 | if (Constraint[1] == 'f') { |
| 695 | if (VT == MVT::f32) |
| 696 | return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass, |
| 697 | SystemZMC::FP32Regs); |
| 698 | if (VT == MVT::f128) |
| 699 | return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass, |
| 700 | SystemZMC::FP128Regs); |
| 701 | return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass, |
| 702 | SystemZMC::FP64Regs); |
| 703 | } |
| 704 | } |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 705 | return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | void SystemZTargetLowering:: |
| 709 | LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint, |
| 710 | std::vector<SDValue> &Ops, |
| 711 | SelectionDAG &DAG) const { |
| 712 | // Only support length 1 constraints for now. |
| 713 | if (Constraint.length() == 1) { |
| 714 | switch (Constraint[0]) { |
| 715 | case 'I': // Unsigned 8-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 716 | if (auto *C = dyn_cast<ConstantSDNode>(Op)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 717 | if (isUInt<8>(C->getZExtValue())) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 718 | Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 719 | Op.getValueType())); |
| 720 | return; |
| 721 | |
| 722 | case 'J': // Unsigned 12-bit constant |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 723 | if (auto *C = dyn_cast<ConstantSDNode>(Op)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 724 | if (isUInt<12>(C->getZExtValue())) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 725 | Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 726 | Op.getValueType())); |
| 727 | return; |
| 728 | |
| 729 | case 'K': // Signed 16-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 (isInt<16>(C->getSExtValue())) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 732 | Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 733 | Op.getValueType())); |
| 734 | return; |
| 735 | |
| 736 | case 'L': // Signed 20-bit displacement (on all targets we support) |
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 (isInt<20>(C->getSExtValue())) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 739 | Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 740 | Op.getValueType())); |
| 741 | return; |
| 742 | |
| 743 | case 'M': // 0x7fffffff |
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 (C->getZExtValue() == 0x7fffffff) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 746 | Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 747 | Op.getValueType())); |
| 748 | return; |
| 749 | } |
| 750 | } |
| 751 | TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); |
| 752 | } |
| 753 | |
| 754 | //===----------------------------------------------------------------------===// |
| 755 | // Calling conventions |
| 756 | //===----------------------------------------------------------------------===// |
| 757 | |
| 758 | #include "SystemZGenCallingConv.inc" |
| 759 | |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 760 | bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType, |
| 761 | Type *ToType) const { |
| 762 | return isTruncateFree(FromType, ToType); |
| 763 | } |
| 764 | |
| 765 | bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { |
| 766 | if (!CI->isTailCall()) |
| 767 | return false; |
| 768 | return true; |
| 769 | } |
| 770 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 771 | // Value is a value that has been passed to us in the location described by VA |
| 772 | // (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining |
| 773 | // any loads onto Chain. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 774 | static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 775 | CCValAssign &VA, SDValue Chain, |
| 776 | SDValue Value) { |
| 777 | // If the argument has been promoted from a smaller type, insert an |
| 778 | // assertion to capture this. |
| 779 | if (VA.getLocInfo() == CCValAssign::SExt) |
| 780 | Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value, |
| 781 | DAG.getValueType(VA.getValVT())); |
| 782 | else if (VA.getLocInfo() == CCValAssign::ZExt) |
| 783 | Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value, |
| 784 | DAG.getValueType(VA.getValVT())); |
| 785 | |
| 786 | if (VA.isExtInLoc()) |
| 787 | Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value); |
| 788 | else if (VA.getLocInfo() == CCValAssign::Indirect) |
| 789 | Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value, |
| 790 | MachinePointerInfo(), false, false, false, 0); |
| 791 | else |
| 792 | assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo"); |
| 793 | return Value; |
| 794 | } |
| 795 | |
| 796 | // Value is a value of type VA.getValVT() that we need to copy into |
| 797 | // the location described by VA. Return a copy of Value converted to |
| 798 | // VA.getValVT(). The caller is responsible for handling indirect values. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 799 | static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 800 | CCValAssign &VA, SDValue Value) { |
| 801 | switch (VA.getLocInfo()) { |
| 802 | case CCValAssign::SExt: |
| 803 | return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value); |
| 804 | case CCValAssign::ZExt: |
| 805 | return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value); |
| 806 | case CCValAssign::AExt: |
| 807 | return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value); |
| 808 | case CCValAssign::Full: |
| 809 | return Value; |
| 810 | default: |
| 811 | llvm_unreachable("Unhandled getLocInfo()"); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | SDValue SystemZTargetLowering:: |
| 816 | LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, |
| 817 | const SmallVectorImpl<ISD::InputArg> &Ins, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 818 | SDLoc DL, SelectionDAG &DAG, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 819 | SmallVectorImpl<SDValue> &InVals) const { |
| 820 | MachineFunction &MF = DAG.getMachineFunction(); |
| 821 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 822 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 823 | SystemZMachineFunctionInfo *FuncInfo = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 824 | MF.getInfo<SystemZMachineFunctionInfo>(); |
| 825 | auto *TFL = |
| 826 | static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 827 | |
| 828 | // Assign locations to all of the incoming arguments. |
| 829 | SmallVector<CCValAssign, 16> ArgLocs; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 830 | SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 831 | CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ); |
| 832 | |
| 833 | unsigned NumFixedGPRs = 0; |
| 834 | unsigned NumFixedFPRs = 0; |
| 835 | for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { |
| 836 | SDValue ArgValue; |
| 837 | CCValAssign &VA = ArgLocs[I]; |
| 838 | EVT LocVT = VA.getLocVT(); |
| 839 | if (VA.isRegLoc()) { |
| 840 | // Arguments passed in registers |
| 841 | const TargetRegisterClass *RC; |
| 842 | switch (LocVT.getSimpleVT().SimpleTy) { |
| 843 | default: |
| 844 | // Integers smaller than i64 should be promoted to i64. |
| 845 | llvm_unreachable("Unexpected argument type"); |
| 846 | case MVT::i32: |
| 847 | NumFixedGPRs += 1; |
| 848 | RC = &SystemZ::GR32BitRegClass; |
| 849 | break; |
| 850 | case MVT::i64: |
| 851 | NumFixedGPRs += 1; |
| 852 | RC = &SystemZ::GR64BitRegClass; |
| 853 | break; |
| 854 | case MVT::f32: |
| 855 | NumFixedFPRs += 1; |
| 856 | RC = &SystemZ::FP32BitRegClass; |
| 857 | break; |
| 858 | case MVT::f64: |
| 859 | NumFixedFPRs += 1; |
| 860 | RC = &SystemZ::FP64BitRegClass; |
| 861 | break; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 862 | case MVT::v16i8: |
| 863 | case MVT::v8i16: |
| 864 | case MVT::v4i32: |
| 865 | case MVT::v2i64: |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 866 | case MVT::v4f32: |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 867 | case MVT::v2f64: |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 868 | RC = &SystemZ::VR128BitRegClass; |
| 869 | break; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | unsigned VReg = MRI.createVirtualRegister(RC); |
| 873 | MRI.addLiveIn(VA.getLocReg(), VReg); |
| 874 | ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT); |
| 875 | } else { |
| 876 | assert(VA.isMemLoc() && "Argument not register or memory"); |
| 877 | |
| 878 | // Create the frame index object for this incoming parameter. |
| 879 | int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8, |
| 880 | VA.getLocMemOffset(), true); |
| 881 | |
| 882 | // Create the SelectionDAG nodes corresponding to a load |
| 883 | // from this parameter. Unpromoted ints and floats are |
| 884 | // passed as right-justified 8-byte values. |
| 885 | EVT PtrVT = getPointerTy(); |
| 886 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); |
| 887 | if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 888 | FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, |
| 889 | DAG.getIntPtrConstant(4, DL)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 890 | ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN, |
| 891 | MachinePointerInfo::getFixedStack(FI), |
| 892 | false, false, false, 0); |
| 893 | } |
| 894 | |
| 895 | // Convert the value of the argument register into the value that's |
| 896 | // being passed. |
| 897 | InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue)); |
| 898 | } |
| 899 | |
| 900 | if (IsVarArg) { |
| 901 | // Save the number of non-varargs registers for later use by va_start, etc. |
| 902 | FuncInfo->setVarArgsFirstGPR(NumFixedGPRs); |
| 903 | FuncInfo->setVarArgsFirstFPR(NumFixedFPRs); |
| 904 | |
| 905 | // Likewise the address (in the form of a frame index) of where the |
| 906 | // first stack vararg would be. The 1-byte size here is arbitrary. |
| 907 | int64_t StackSize = CCInfo.getNextStackOffset(); |
| 908 | FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true)); |
| 909 | |
| 910 | // ...and a similar frame index for the caller-allocated save area |
| 911 | // that will be used to store the incoming registers. |
| 912 | int64_t RegSaveOffset = TFL->getOffsetOfLocalArea(); |
| 913 | unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true); |
| 914 | FuncInfo->setRegSaveFrameIndex(RegSaveIndex); |
| 915 | |
| 916 | // Store the FPR varargs in the reserved frame slots. (We store the |
| 917 | // GPRs as part of the prologue.) |
| 918 | if (NumFixedFPRs < SystemZ::NumArgFPRs) { |
| 919 | SDValue MemOps[SystemZ::NumArgFPRs]; |
| 920 | for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) { |
| 921 | unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]); |
| 922 | int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true); |
| 923 | SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); |
| 924 | unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I], |
| 925 | &SystemZ::FP64BitRegClass); |
| 926 | SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64); |
| 927 | MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN, |
| 928 | MachinePointerInfo::getFixedStack(FI), |
| 929 | false, false, 0); |
| 930 | |
| 931 | } |
| 932 | // Join the stores, which are independent of one another. |
| 933 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, |
Craig Topper | 2d2aa0c | 2014-04-30 07:17:30 +0000 | [diff] [blame] | 934 | makeArrayRef(&MemOps[NumFixedFPRs], |
| 935 | SystemZ::NumArgFPRs-NumFixedFPRs)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
| 938 | |
| 939 | return Chain; |
| 940 | } |
| 941 | |
Benjamin Kramer | c6cc58e | 2014-10-04 16:55:56 +0000 | [diff] [blame] | 942 | static bool canUseSiblingCall(const CCState &ArgCCInfo, |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 943 | SmallVectorImpl<CCValAssign> &ArgLocs) { |
| 944 | // Punt if there are any indirect or stack arguments, or if the call |
| 945 | // needs the call-saved argument register R6. |
| 946 | for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { |
| 947 | CCValAssign &VA = ArgLocs[I]; |
| 948 | if (VA.getLocInfo() == CCValAssign::Indirect) |
| 949 | return false; |
| 950 | if (!VA.isRegLoc()) |
| 951 | return false; |
| 952 | unsigned Reg = VA.getLocReg(); |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 953 | if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D) |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 954 | return false; |
| 955 | } |
| 956 | return true; |
| 957 | } |
| 958 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 959 | SDValue |
| 960 | SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI, |
| 961 | SmallVectorImpl<SDValue> &InVals) const { |
| 962 | SelectionDAG &DAG = CLI.DAG; |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 963 | SDLoc &DL = CLI.DL; |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 964 | SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; |
| 965 | SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; |
| 966 | SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 967 | SDValue Chain = CLI.Chain; |
| 968 | SDValue Callee = CLI.Callee; |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 969 | bool &IsTailCall = CLI.IsTailCall; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 970 | CallingConv::ID CallConv = CLI.CallConv; |
| 971 | bool IsVarArg = CLI.IsVarArg; |
| 972 | MachineFunction &MF = DAG.getMachineFunction(); |
| 973 | EVT PtrVT = getPointerTy(); |
| 974 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 975 | // Analyze the operands of the call, assigning locations to each operand. |
| 976 | SmallVector<CCValAssign, 16> ArgLocs; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 977 | SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 978 | ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ); |
| 979 | |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 980 | // We don't support GuaranteedTailCallOpt, only automatically-detected |
| 981 | // sibling calls. |
| 982 | if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs)) |
| 983 | IsTailCall = false; |
| 984 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 985 | // Get a count of how many bytes are to be pushed on the stack. |
| 986 | unsigned NumBytes = ArgCCInfo.getNextStackOffset(); |
| 987 | |
| 988 | // Mark the start of the call. |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 989 | if (!IsTailCall) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 990 | Chain = DAG.getCALLSEQ_START(Chain, |
| 991 | DAG.getConstant(NumBytes, DL, PtrVT, true), |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 992 | DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 993 | |
| 994 | // Copy argument values to their designated locations. |
| 995 | SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass; |
| 996 | SmallVector<SDValue, 8> MemOpChains; |
| 997 | SDValue StackPtr; |
| 998 | for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { |
| 999 | CCValAssign &VA = ArgLocs[I]; |
| 1000 | SDValue ArgValue = OutVals[I]; |
| 1001 | |
| 1002 | if (VA.getLocInfo() == CCValAssign::Indirect) { |
| 1003 | // Store the argument in a stack slot and pass its address. |
| 1004 | SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT()); |
| 1005 | int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); |
| 1006 | MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot, |
| 1007 | MachinePointerInfo::getFixedStack(FI), |
| 1008 | false, false, 0)); |
| 1009 | ArgValue = SpillSlot; |
| 1010 | } else |
| 1011 | ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue); |
| 1012 | |
| 1013 | if (VA.isRegLoc()) |
| 1014 | // Queue up the argument copies and emit them at the end. |
| 1015 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue)); |
| 1016 | else { |
| 1017 | assert(VA.isMemLoc() && "Argument not register or memory"); |
| 1018 | |
| 1019 | // Work out the address of the stack slot. Unpromoted ints and |
| 1020 | // floats are passed as right-justified 8-byte values. |
| 1021 | if (!StackPtr.getNode()) |
| 1022 | StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT); |
| 1023 | unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset(); |
| 1024 | if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32) |
| 1025 | Offset += 4; |
| 1026 | SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1027 | DAG.getIntPtrConstant(Offset, DL)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1028 | |
| 1029 | // Emit the store. |
| 1030 | MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address, |
| 1031 | MachinePointerInfo(), |
| 1032 | false, false, 0)); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | // Join the stores, which are independent of one another. |
| 1037 | if (!MemOpChains.empty()) |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 1038 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1039 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1040 | // Accept direct calls by converting symbolic call addresses to the |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1041 | // associated Target* opcodes. Force %r1 to be used for indirect |
| 1042 | // tail calls. |
| 1043 | SDValue Glue; |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1044 | if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1045 | Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT); |
| 1046 | Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee); |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1047 | } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1048 | Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT); |
| 1049 | Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee); |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1050 | } else if (IsTailCall) { |
| 1051 | Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue); |
| 1052 | Glue = Chain.getValue(1); |
| 1053 | Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType()); |
| 1054 | } |
| 1055 | |
| 1056 | // Build a sequence of copy-to-reg nodes, chained and glued together. |
| 1057 | for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) { |
| 1058 | Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first, |
| 1059 | RegsToPass[I].second, Glue); |
| 1060 | Glue = Chain.getValue(1); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | // The first call operand is the chain and the second is the target address. |
| 1064 | SmallVector<SDValue, 8> Ops; |
| 1065 | Ops.push_back(Chain); |
| 1066 | Ops.push_back(Callee); |
| 1067 | |
| 1068 | // Add argument registers to the end of the list so that they are |
| 1069 | // known live into the call. |
| 1070 | for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) |
| 1071 | Ops.push_back(DAG.getRegister(RegsToPass[I].first, |
| 1072 | RegsToPass[I].second.getValueType())); |
| 1073 | |
Richard Sandiford | 02bb0ec | 2014-07-10 11:44:37 +0000 | [diff] [blame] | 1074 | // Add a register mask operand representing the call-preserved registers. |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 1075 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
Eric Christopher | 9deb75d | 2015-03-11 22:42:13 +0000 | [diff] [blame] | 1076 | const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv); |
Richard Sandiford | 02bb0ec | 2014-07-10 11:44:37 +0000 | [diff] [blame] | 1077 | assert(Mask && "Missing call preserved mask for calling convention"); |
| 1078 | Ops.push_back(DAG.getRegisterMask(Mask)); |
| 1079 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1080 | // Glue the call to the argument copies, if any. |
| 1081 | if (Glue.getNode()) |
| 1082 | Ops.push_back(Glue); |
| 1083 | |
| 1084 | // Emit the call. |
| 1085 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 1086 | if (IsTailCall) |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 1087 | return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops); |
| 1088 | Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1089 | Glue = Chain.getValue(1); |
| 1090 | |
| 1091 | // Mark the end of the call, which is glued to the call itself. |
| 1092 | Chain = DAG.getCALLSEQ_END(Chain, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1093 | DAG.getConstant(NumBytes, DL, PtrVT, true), |
| 1094 | DAG.getConstant(0, DL, PtrVT, true), |
Andrew Trick | ad6d08a | 2013-05-29 22:03:55 +0000 | [diff] [blame] | 1095 | Glue, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1096 | Glue = Chain.getValue(1); |
| 1097 | |
| 1098 | // Assign locations to each value returned by this call. |
| 1099 | SmallVector<CCValAssign, 16> RetLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 1100 | CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1101 | RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ); |
| 1102 | |
| 1103 | // Copy all of the result registers out of their specified physreg. |
| 1104 | for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) { |
| 1105 | CCValAssign &VA = RetLocs[I]; |
| 1106 | |
| 1107 | // Copy the value out, gluing the copy to the end of the call sequence. |
| 1108 | SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), |
| 1109 | VA.getLocVT(), Glue); |
| 1110 | Chain = RetValue.getValue(1); |
| 1111 | Glue = RetValue.getValue(2); |
| 1112 | |
| 1113 | // Convert the value of the return register into the value that's |
| 1114 | // being returned. |
| 1115 | InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue)); |
| 1116 | } |
| 1117 | |
| 1118 | return Chain; |
| 1119 | } |
| 1120 | |
| 1121 | SDValue |
| 1122 | SystemZTargetLowering::LowerReturn(SDValue Chain, |
| 1123 | CallingConv::ID CallConv, bool IsVarArg, |
| 1124 | const SmallVectorImpl<ISD::OutputArg> &Outs, |
| 1125 | const SmallVectorImpl<SDValue> &OutVals, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1126 | SDLoc DL, SelectionDAG &DAG) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1127 | MachineFunction &MF = DAG.getMachineFunction(); |
| 1128 | |
| 1129 | // Assign locations to each returned value. |
| 1130 | SmallVector<CCValAssign, 16> RetLocs; |
Eric Christopher | b521750 | 2014-08-06 18:45:26 +0000 | [diff] [blame] | 1131 | CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1132 | RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ); |
| 1133 | |
| 1134 | // Quick exit for void returns |
| 1135 | if (RetLocs.empty()) |
| 1136 | return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain); |
| 1137 | |
| 1138 | // Copy the result values into the output registers. |
| 1139 | SDValue Glue; |
| 1140 | SmallVector<SDValue, 4> RetOps; |
| 1141 | RetOps.push_back(Chain); |
| 1142 | for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) { |
| 1143 | CCValAssign &VA = RetLocs[I]; |
| 1144 | SDValue RetValue = OutVals[I]; |
| 1145 | |
| 1146 | // Make the return register live on exit. |
| 1147 | assert(VA.isRegLoc() && "Can only return in registers!"); |
| 1148 | |
| 1149 | // Promote the value as required. |
| 1150 | RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue); |
| 1151 | |
| 1152 | // Chain and glue the copies together. |
| 1153 | unsigned Reg = VA.getLocReg(); |
| 1154 | Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue); |
| 1155 | Glue = Chain.getValue(1); |
| 1156 | RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT())); |
| 1157 | } |
| 1158 | |
| 1159 | // Update chain and glue. |
| 1160 | RetOps[0] = Chain; |
| 1161 | if (Glue.getNode()) |
| 1162 | RetOps.push_back(Glue); |
| 1163 | |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 1164 | return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Richard Sandiford | 9afe613 | 2013-12-10 10:36:34 +0000 | [diff] [blame] | 1167 | SDValue SystemZTargetLowering:: |
| 1168 | prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const { |
| 1169 | return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain); |
| 1170 | } |
| 1171 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 1172 | // Return true if Op is an intrinsic node with chain that returns the CC value |
| 1173 | // as its only (other) argument. Provide the associated SystemZISD opcode and |
| 1174 | // the mask of valid CC values if so. |
| 1175 | static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode, |
| 1176 | unsigned &CCValid) { |
| 1177 | unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 1178 | switch (Id) { |
| 1179 | case Intrinsic::s390_tbegin: |
| 1180 | Opcode = SystemZISD::TBEGIN; |
| 1181 | CCValid = SystemZ::CCMASK_TBEGIN; |
| 1182 | return true; |
| 1183 | |
| 1184 | case Intrinsic::s390_tbegin_nofloat: |
| 1185 | Opcode = SystemZISD::TBEGIN_NOFLOAT; |
| 1186 | CCValid = SystemZ::CCMASK_TBEGIN; |
| 1187 | return true; |
| 1188 | |
| 1189 | case Intrinsic::s390_tend: |
| 1190 | Opcode = SystemZISD::TEND; |
| 1191 | CCValid = SystemZ::CCMASK_TEND; |
| 1192 | return true; |
| 1193 | |
| 1194 | default: |
| 1195 | return false; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | // Emit an intrinsic with chain with a glued value instead of its CC result. |
| 1200 | static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op, |
| 1201 | unsigned Opcode) { |
| 1202 | // Copy all operands except the intrinsic ID. |
| 1203 | unsigned NumOps = Op.getNumOperands(); |
| 1204 | SmallVector<SDValue, 6> Ops; |
| 1205 | Ops.reserve(NumOps - 1); |
| 1206 | Ops.push_back(Op.getOperand(0)); |
| 1207 | for (unsigned I = 2; I < NumOps; ++I) |
| 1208 | Ops.push_back(Op.getOperand(I)); |
| 1209 | |
| 1210 | assert(Op->getNumValues() == 2 && "Expected only CC result and chain"); |
| 1211 | SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue); |
| 1212 | SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops); |
| 1213 | SDValue OldChain = SDValue(Op.getNode(), 1); |
| 1214 | SDValue NewChain = SDValue(Intr.getNode(), 0); |
| 1215 | DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain); |
| 1216 | return Intr; |
| 1217 | } |
| 1218 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1219 | // CC is a comparison that will be implemented using an integer or |
| 1220 | // floating-point comparison. Return the condition code mask for |
| 1221 | // a branch on true. In the integer case, CCMASK_CMP_UO is set for |
| 1222 | // unsigned comparisons and clear for signed ones. In the floating-point |
| 1223 | // case, CCMASK_CMP_UO has its normal mask meaning (unordered). |
| 1224 | static unsigned CCMaskForCondCode(ISD::CondCode CC) { |
| 1225 | #define CONV(X) \ |
| 1226 | case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \ |
| 1227 | case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \ |
| 1228 | case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X |
| 1229 | |
| 1230 | switch (CC) { |
| 1231 | default: |
| 1232 | llvm_unreachable("Invalid integer condition!"); |
| 1233 | |
| 1234 | CONV(EQ); |
| 1235 | CONV(NE); |
| 1236 | CONV(GT); |
| 1237 | CONV(GE); |
| 1238 | CONV(LT); |
| 1239 | CONV(LE); |
| 1240 | |
| 1241 | case ISD::SETO: return SystemZ::CCMASK_CMP_O; |
| 1242 | case ISD::SETUO: return SystemZ::CCMASK_CMP_UO; |
| 1243 | } |
| 1244 | #undef CONV |
| 1245 | } |
| 1246 | |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 1247 | // Return a sequence for getting a 1 from an IPM result when CC has a |
| 1248 | // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask. |
| 1249 | // The handling of CC values outside CCValid doesn't matter. |
| 1250 | static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) { |
| 1251 | // Deal with cases where the result can be taken directly from a bit |
| 1252 | // of the IPM result. |
| 1253 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3))) |
| 1254 | return IPMConversion(0, 0, SystemZ::IPM_CC); |
| 1255 | if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3))) |
| 1256 | return IPMConversion(0, 0, SystemZ::IPM_CC + 1); |
| 1257 | |
| 1258 | // Deal with cases where we can add a value to force the sign bit |
| 1259 | // to contain the right value. Putting the bit in 31 means we can |
| 1260 | // use SRL rather than RISBG(L), and also makes it easier to get a |
| 1261 | // 0/-1 value, so it has priority over the other tests below. |
| 1262 | // |
| 1263 | // These sequences rely on the fact that the upper two bits of the |
| 1264 | // IPM result are zero. |
| 1265 | uint64_t TopBit = uint64_t(1) << 31; |
| 1266 | if (CCMask == (CCValid & SystemZ::CCMASK_0)) |
| 1267 | return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31); |
| 1268 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1))) |
| 1269 | return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31); |
| 1270 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1271 | | SystemZ::CCMASK_1 |
| 1272 | | SystemZ::CCMASK_2))) |
| 1273 | return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31); |
| 1274 | if (CCMask == (CCValid & SystemZ::CCMASK_3)) |
| 1275 | return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31); |
| 1276 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 |
| 1277 | | SystemZ::CCMASK_2 |
| 1278 | | SystemZ::CCMASK_3))) |
| 1279 | return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31); |
| 1280 | |
| 1281 | // Next try inverting the value and testing a bit. 0/1 could be |
| 1282 | // handled this way too, but we dealt with that case above. |
| 1283 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2))) |
| 1284 | return IPMConversion(-1, 0, SystemZ::IPM_CC); |
| 1285 | |
| 1286 | // Handle cases where adding a value forces a non-sign bit to contain |
| 1287 | // the right value. |
| 1288 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2))) |
| 1289 | return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1); |
| 1290 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3))) |
| 1291 | return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1); |
| 1292 | |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1293 | // 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] | 1294 | // can be done by inverting the low CC bit and applying one of the |
| 1295 | // sign-based extractions above. |
| 1296 | if (CCMask == (CCValid & SystemZ::CCMASK_1)) |
| 1297 | return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31); |
| 1298 | if (CCMask == (CCValid & SystemZ::CCMASK_2)) |
| 1299 | return IPMConversion(1 << SystemZ::IPM_CC, |
| 1300 | TopBit - (3 << SystemZ::IPM_CC), 31); |
| 1301 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1302 | | SystemZ::CCMASK_1 |
| 1303 | | SystemZ::CCMASK_3))) |
| 1304 | return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31); |
| 1305 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1306 | | SystemZ::CCMASK_2 |
| 1307 | | SystemZ::CCMASK_3))) |
| 1308 | return IPMConversion(1 << SystemZ::IPM_CC, |
| 1309 | TopBit - (1 << SystemZ::IPM_CC), 31); |
| 1310 | |
| 1311 | llvm_unreachable("Unexpected CC combination"); |
| 1312 | } |
| 1313 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1314 | // 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] | 1315 | // as necessary. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1316 | static void adjustZeroCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1317 | if (C.ICmpType == SystemZICMP::UnsignedOnly) |
Richard Sandiford | a075708 | 2013-08-01 10:29:45 +0000 | [diff] [blame] | 1318 | return; |
| 1319 | |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1320 | auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode()); |
Richard Sandiford | a075708 | 2013-08-01 10:29:45 +0000 | [diff] [blame] | 1321 | if (!ConstOp1) |
| 1322 | return; |
| 1323 | |
| 1324 | int64_t Value = ConstOp1->getSExtValue(); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1325 | if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) || |
| 1326 | (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) || |
| 1327 | (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) || |
| 1328 | (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) { |
| 1329 | C.CCMask ^= SystemZ::CCMASK_CMP_EQ; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1330 | C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType()); |
Richard Sandiford | a075708 | 2013-08-01 10:29:45 +0000 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1334 | // If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI, |
| 1335 | // adjust the operands as necessary. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1336 | static void adjustSubwordCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1337 | // For us to make any changes, it must a comparison between a single-use |
| 1338 | // load and a constant. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1339 | if (!C.Op0.hasOneUse() || |
| 1340 | C.Op0.getOpcode() != ISD::LOAD || |
| 1341 | C.Op1.getOpcode() != ISD::Constant) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1342 | return; |
| 1343 | |
| 1344 | // We must have an 8- or 16-bit load. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1345 | auto *Load = cast<LoadSDNode>(C.Op0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1346 | unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits(); |
| 1347 | if (NumBits != 8 && NumBits != 16) |
| 1348 | return; |
| 1349 | |
| 1350 | // The load must be an extending one and the constant must be within the |
| 1351 | // range of the unextended value. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1352 | auto *ConstOp1 = cast<ConstantSDNode>(C.Op1); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1353 | uint64_t Value = ConstOp1->getZExtValue(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1354 | uint64_t Mask = (1 << NumBits) - 1; |
| 1355 | if (Load->getExtensionType() == ISD::SEXTLOAD) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1356 | // Make sure that ConstOp1 is in range of C.Op0. |
| 1357 | int64_t SignedValue = ConstOp1->getSExtValue(); |
| 1358 | if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1359 | return; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1360 | if (C.ICmpType != SystemZICMP::SignedOnly) { |
| 1361 | // Unsigned comparison between two sign-extended values is equivalent |
| 1362 | // to unsigned comparison between two zero-extended values. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1363 | Value &= Mask; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1364 | } else if (NumBits == 8) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1365 | // Try to treat the comparison as unsigned, so that we can use CLI. |
| 1366 | // Adjust CCMask and Value as necessary. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1367 | if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1368 | // Test whether the high bit of the byte is set. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1369 | Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT; |
| 1370 | else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1371 | // Test whether the high bit of the byte is clear. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1372 | Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1373 | else |
| 1374 | // No instruction exists for this combination. |
| 1375 | return; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1376 | C.ICmpType = SystemZICMP::UnsignedOnly; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1377 | } |
| 1378 | } else if (Load->getExtensionType() == ISD::ZEXTLOAD) { |
| 1379 | if (Value > Mask) |
| 1380 | return; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1381 | assert(C.ICmpType == SystemZICMP::Any && |
| 1382 | "Signedness shouldn't matter here."); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1383 | } else |
| 1384 | return; |
| 1385 | |
| 1386 | // 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] | 1387 | ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ? |
| 1388 | ISD::SEXTLOAD : |
| 1389 | ISD::ZEXTLOAD); |
| 1390 | if (C.Op0.getValueType() != MVT::i32 || |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1391 | Load->getExtensionType() != ExtType) |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1392 | C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, |
| 1393 | Load->getChain(), Load->getBasePtr(), |
| 1394 | Load->getPointerInfo(), Load->getMemoryVT(), |
| 1395 | Load->isVolatile(), Load->isNonTemporal(), |
Louis Gerbarg | 67474e3 | 2014-07-31 21:45:05 +0000 | [diff] [blame] | 1396 | Load->isInvariant(), Load->getAlignment()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1397 | |
| 1398 | // 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] | 1399 | if (C.Op1.getValueType() != MVT::i32 || |
| 1400 | Value != ConstOp1->getZExtValue()) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1401 | C.Op1 = DAG.getConstant(Value, DL, MVT::i32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1404 | // Return true if Op is either an unextended load, or a load suitable |
| 1405 | // for integer register-memory comparisons of type ICmpType. |
| 1406 | static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1407 | auto *Load = dyn_cast<LoadSDNode>(Op.getNode()); |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1408 | if (Load) { |
| 1409 | // There are no instructions to compare a register with a memory byte. |
| 1410 | if (Load->getMemoryVT() == MVT::i8) |
| 1411 | return false; |
| 1412 | // Otherwise decide on extension type. |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1413 | switch (Load->getExtensionType()) { |
| 1414 | case ISD::NON_EXTLOAD: |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1415 | return true; |
| 1416 | case ISD::SEXTLOAD: |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1417 | return ICmpType != SystemZICMP::UnsignedOnly; |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1418 | case ISD::ZEXTLOAD: |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1419 | return ICmpType != SystemZICMP::SignedOnly; |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1420 | default: |
| 1421 | break; |
| 1422 | } |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1423 | } |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1424 | return false; |
| 1425 | } |
| 1426 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1427 | // Return true if it is better to swap the operands of C. |
| 1428 | static bool shouldSwapCmpOperands(const Comparison &C) { |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1429 | // Leave f128 comparisons alone, since they have no memory forms. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1430 | if (C.Op0.getValueType() == MVT::f128) |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1431 | return false; |
| 1432 | |
| 1433 | // Always keep a floating-point constant second, since comparisons with |
| 1434 | // zero can use LOAD TEST and comparisons with other constants make a |
| 1435 | // natural memory operand. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1436 | if (isa<ConstantFPSDNode>(C.Op1)) |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1437 | return false; |
| 1438 | |
| 1439 | // Never swap comparisons with zero since there are many ways to optimize |
| 1440 | // those later. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1441 | auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1442 | if (ConstOp1 && ConstOp1->getZExtValue() == 0) |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1443 | return false; |
| 1444 | |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1445 | // Also keep natural memory operands second if the loaded value is |
| 1446 | // only used here. Several comparisons have memory forms. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1447 | if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse()) |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1448 | return false; |
| 1449 | |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1450 | // Look for cases where Cmp0 is a single-use load and Cmp1 isn't. |
| 1451 | // In that case we generally prefer the memory to be second. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1452 | if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) { |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1453 | // The only exceptions are when the second operand is a constant and |
| 1454 | // we can use things like CHHSI. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1455 | if (!ConstOp1) |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1456 | return true; |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1457 | // The unsigned memory-immediate instructions can handle 16-bit |
| 1458 | // unsigned integers. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1459 | if (C.ICmpType != SystemZICMP::SignedOnly && |
| 1460 | isUInt<16>(ConstOp1->getZExtValue())) |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1461 | return false; |
| 1462 | // The signed memory-immediate instructions can handle 16-bit |
| 1463 | // signed integers. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1464 | if (C.ICmpType != SystemZICMP::UnsignedOnly && |
| 1465 | isInt<16>(ConstOp1->getSExtValue())) |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1466 | return false; |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1467 | return true; |
| 1468 | } |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1469 | |
| 1470 | // Try to promote the use of CGFR and CLGFR. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1471 | unsigned Opcode0 = C.Op0.getOpcode(); |
| 1472 | if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND) |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1473 | return true; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1474 | if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND) |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1475 | return true; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1476 | if (C.ICmpType != SystemZICMP::SignedOnly && |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1477 | Opcode0 == ISD::AND && |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1478 | C.Op0.getOperand(1).getOpcode() == ISD::Constant && |
| 1479 | cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff) |
Richard Sandiford | 7b4118a | 2013-12-06 09:56:50 +0000 | [diff] [blame] | 1480 | return true; |
| 1481 | |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1482 | return false; |
| 1483 | } |
| 1484 | |
Richard Sandiford | 73170f8 | 2013-12-11 11:45:08 +0000 | [diff] [blame] | 1485 | // Return a version of comparison CC mask CCMask in which the LT and GT |
| 1486 | // actions are swapped. |
| 1487 | static unsigned reverseCCMask(unsigned CCMask) { |
| 1488 | return ((CCMask & SystemZ::CCMASK_CMP_EQ) | |
| 1489 | (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) | |
| 1490 | (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) | |
| 1491 | (CCMask & SystemZ::CCMASK_CMP_UO)); |
| 1492 | } |
| 1493 | |
Richard Sandiford | 0847c45 | 2013-12-13 15:50:30 +0000 | [diff] [blame] | 1494 | // Check whether C tests for equality between X and Y and whether X - Y |
| 1495 | // or Y - X is also computed. In that case it's better to compare the |
| 1496 | // result of the subtraction against zero. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1497 | static void adjustForSubtraction(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Richard Sandiford | 0847c45 | 2013-12-13 15:50:30 +0000 | [diff] [blame] | 1498 | if (C.CCMask == SystemZ::CCMASK_CMP_EQ || |
| 1499 | C.CCMask == SystemZ::CCMASK_CMP_NE) { |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 1500 | 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] | 1501 | SDNode *N = *I; |
| 1502 | if (N->getOpcode() == ISD::SUB && |
| 1503 | ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) || |
| 1504 | (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) { |
| 1505 | C.Op0 = SDValue(N, 0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1506 | C.Op1 = DAG.getConstant(0, DL, N->getValueType(0)); |
Richard Sandiford | 0847c45 | 2013-12-13 15:50:30 +0000 | [diff] [blame] | 1507 | return; |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1513 | // Check whether C compares a floating-point value with zero and if that |
| 1514 | // floating-point value is also negated. In this case we can use the |
| 1515 | // negation to set CC, so avoiding separate LOAD AND TEST and |
| 1516 | // LOAD (NEGATIVE/COMPLEMENT) instructions. |
| 1517 | static void adjustForFNeg(Comparison &C) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1518 | auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1); |
Richard Sandiford | 73170f8 | 2013-12-11 11:45:08 +0000 | [diff] [blame] | 1519 | if (C1 && C1->isZero()) { |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 1520 | 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] | 1521 | SDNode *N = *I; |
| 1522 | if (N->getOpcode() == ISD::FNEG) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1523 | C.Op0 = SDValue(N, 0); |
| 1524 | C.CCMask = reverseCCMask(C.CCMask); |
Richard Sandiford | 73170f8 | 2013-12-11 11:45:08 +0000 | [diff] [blame] | 1525 | return; |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1531 | // 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] | 1532 | // also sign-extended. In that case it is better to test the result |
| 1533 | // of the sign extension using LTGFR. |
| 1534 | // |
| 1535 | // This case is important because InstCombine transforms a comparison |
| 1536 | // with (sext (trunc X)) into a comparison with (shl X, 32). |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1537 | static void adjustForLTGFR(Comparison &C) { |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1538 | // Check for a comparison between (shl X, 32) and 0. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1539 | if (C.Op0.getOpcode() == ISD::SHL && |
| 1540 | C.Op0.getValueType() == MVT::i64 && |
| 1541 | C.Op1.getOpcode() == ISD::Constant && |
| 1542 | cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1543 | auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1)); |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1544 | if (C1 && C1->getZExtValue() == 32) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1545 | SDValue ShlOp0 = C.Op0.getOperand(0); |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1546 | // See whether X has any SIGN_EXTEND_INREG uses. |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 1547 | 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] | 1548 | SDNode *N = *I; |
| 1549 | if (N->getOpcode() == ISD::SIGN_EXTEND_INREG && |
| 1550 | cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1551 | C.Op0 = SDValue(N, 0); |
Richard Sandiford | bd2f0e9 | 2013-12-13 15:07:39 +0000 | [diff] [blame] | 1552 | return; |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | } |
| 1558 | |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1559 | // If C compares the truncation of an extending load, try to compare |
| 1560 | // the untruncated value instead. This exposes more opportunities to |
| 1561 | // reuse CC. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1562 | static void adjustICmpTruncate(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1563 | if (C.Op0.getOpcode() == ISD::TRUNCATE && |
| 1564 | C.Op0.getOperand(0).getOpcode() == ISD::LOAD && |
| 1565 | C.Op1.getOpcode() == ISD::Constant && |
| 1566 | cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1567 | auto *L = cast<LoadSDNode>(C.Op0.getOperand(0)); |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1568 | if (L->getMemoryVT().getStoreSizeInBits() |
| 1569 | <= C.Op0.getValueType().getSizeInBits()) { |
| 1570 | unsigned Type = L->getExtensionType(); |
| 1571 | if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) || |
| 1572 | (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) { |
| 1573 | C.Op0 = C.Op0.getOperand(0); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1574 | C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType()); |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1575 | } |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1580 | // Return true if shift operation N has an in-range constant shift value. |
| 1581 | // Store it in ShiftVal if so. |
| 1582 | static bool isSimpleShift(SDValue N, unsigned &ShiftVal) { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1583 | auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1584 | if (!Shift) |
| 1585 | return false; |
| 1586 | |
| 1587 | uint64_t Amount = Shift->getZExtValue(); |
| 1588 | if (Amount >= N.getValueType().getSizeInBits()) |
| 1589 | return false; |
| 1590 | |
| 1591 | ShiftVal = Amount; |
| 1592 | return true; |
| 1593 | } |
| 1594 | |
| 1595 | // Check whether an AND with Mask is suitable for a TEST UNDER MASK |
| 1596 | // instruction and whether the CC value is descriptive enough to handle |
| 1597 | // a comparison of type Opcode between the AND result and CmpVal. |
| 1598 | // CCMask says which comparison result is being tested and BitSize is |
| 1599 | // the number of bits in the operands. If TEST UNDER MASK can be used, |
| 1600 | // return the corresponding CC mask, otherwise return 0. |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1601 | static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask, |
| 1602 | uint64_t Mask, uint64_t CmpVal, |
| 1603 | unsigned ICmpType) { |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1604 | assert(Mask != 0 && "ANDs with zero should have been removed by now"); |
| 1605 | |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1606 | // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL. |
| 1607 | if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) && |
| 1608 | !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask)) |
| 1609 | return 0; |
| 1610 | |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1611 | // Work out the masks for the lowest and highest bits. |
| 1612 | unsigned HighShift = 63 - countLeadingZeros(Mask); |
| 1613 | uint64_t High = uint64_t(1) << HighShift; |
| 1614 | uint64_t Low = uint64_t(1) << countTrailingZeros(Mask); |
| 1615 | |
| 1616 | // Signed ordered comparisons are effectively unsigned if the sign |
| 1617 | // bit is dropped. |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1618 | bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly); |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1619 | |
| 1620 | // Check for equality comparisons with 0, or the equivalent. |
| 1621 | if (CmpVal == 0) { |
| 1622 | if (CCMask == SystemZ::CCMASK_CMP_EQ) |
| 1623 | return SystemZ::CCMASK_TM_ALL_0; |
| 1624 | if (CCMask == SystemZ::CCMASK_CMP_NE) |
| 1625 | return SystemZ::CCMASK_TM_SOME_1; |
| 1626 | } |
| 1627 | if (EffectivelyUnsigned && CmpVal <= Low) { |
| 1628 | if (CCMask == SystemZ::CCMASK_CMP_LT) |
| 1629 | return SystemZ::CCMASK_TM_ALL_0; |
| 1630 | if (CCMask == SystemZ::CCMASK_CMP_GE) |
| 1631 | return SystemZ::CCMASK_TM_SOME_1; |
| 1632 | } |
| 1633 | if (EffectivelyUnsigned && CmpVal < Low) { |
| 1634 | if (CCMask == SystemZ::CCMASK_CMP_LE) |
| 1635 | return SystemZ::CCMASK_TM_ALL_0; |
| 1636 | if (CCMask == SystemZ::CCMASK_CMP_GT) |
| 1637 | return SystemZ::CCMASK_TM_SOME_1; |
| 1638 | } |
| 1639 | |
| 1640 | // Check for equality comparisons with the mask, or the equivalent. |
| 1641 | if (CmpVal == Mask) { |
| 1642 | if (CCMask == SystemZ::CCMASK_CMP_EQ) |
| 1643 | return SystemZ::CCMASK_TM_ALL_1; |
| 1644 | if (CCMask == SystemZ::CCMASK_CMP_NE) |
| 1645 | return SystemZ::CCMASK_TM_SOME_0; |
| 1646 | } |
| 1647 | if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) { |
| 1648 | if (CCMask == SystemZ::CCMASK_CMP_GT) |
| 1649 | return SystemZ::CCMASK_TM_ALL_1; |
| 1650 | if (CCMask == SystemZ::CCMASK_CMP_LE) |
| 1651 | return SystemZ::CCMASK_TM_SOME_0; |
| 1652 | } |
| 1653 | if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) { |
| 1654 | if (CCMask == SystemZ::CCMASK_CMP_GE) |
| 1655 | return SystemZ::CCMASK_TM_ALL_1; |
| 1656 | if (CCMask == SystemZ::CCMASK_CMP_LT) |
| 1657 | return SystemZ::CCMASK_TM_SOME_0; |
| 1658 | } |
| 1659 | |
| 1660 | // Check for ordered comparisons with the top bit. |
| 1661 | if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) { |
| 1662 | if (CCMask == SystemZ::CCMASK_CMP_LE) |
| 1663 | return SystemZ::CCMASK_TM_MSB_0; |
| 1664 | if (CCMask == SystemZ::CCMASK_CMP_GT) |
| 1665 | return SystemZ::CCMASK_TM_MSB_1; |
| 1666 | } |
| 1667 | if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) { |
| 1668 | if (CCMask == SystemZ::CCMASK_CMP_LT) |
| 1669 | return SystemZ::CCMASK_TM_MSB_0; |
| 1670 | if (CCMask == SystemZ::CCMASK_CMP_GE) |
| 1671 | return SystemZ::CCMASK_TM_MSB_1; |
| 1672 | } |
| 1673 | |
| 1674 | // If there are just two bits, we can do equality checks for Low and High |
| 1675 | // as well. |
| 1676 | if (Mask == Low + High) { |
| 1677 | if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low) |
| 1678 | return SystemZ::CCMASK_TM_MIXED_MSB_0; |
| 1679 | if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low) |
| 1680 | return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY; |
| 1681 | if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High) |
| 1682 | return SystemZ::CCMASK_TM_MIXED_MSB_1; |
| 1683 | if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High) |
| 1684 | return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY; |
| 1685 | } |
| 1686 | |
| 1687 | // Looks like we've exhausted our options. |
| 1688 | return 0; |
| 1689 | } |
| 1690 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1691 | // See whether C can be implemented as a TEST UNDER MASK instruction. |
| 1692 | // Update the arguments with the TM version if so. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1693 | static void adjustForTestUnderMask(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1694 | // Check that we have a comparison with a constant. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1695 | auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1696 | if (!ConstOp1) |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 1697 | return; |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1698 | uint64_t CmpVal = ConstOp1->getZExtValue(); |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 1699 | |
| 1700 | // Check whether the nonconstant input is an AND with a constant mask. |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1701 | Comparison NewC(C); |
| 1702 | uint64_t MaskVal; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1703 | ConstantSDNode *Mask = nullptr; |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1704 | if (C.Op0.getOpcode() == ISD::AND) { |
| 1705 | NewC.Op0 = C.Op0.getOperand(0); |
| 1706 | NewC.Op1 = C.Op0.getOperand(1); |
| 1707 | Mask = dyn_cast<ConstantSDNode>(NewC.Op1); |
| 1708 | if (!Mask) |
| 1709 | return; |
| 1710 | MaskVal = Mask->getZExtValue(); |
| 1711 | } else { |
| 1712 | // There is no instruction to compare with a 64-bit immediate |
| 1713 | // so use TMHH instead if possible. We need an unsigned ordered |
| 1714 | // comparison with an i64 immediate. |
| 1715 | if (NewC.Op0.getValueType() != MVT::i64 || |
| 1716 | NewC.CCMask == SystemZ::CCMASK_CMP_EQ || |
| 1717 | NewC.CCMask == SystemZ::CCMASK_CMP_NE || |
| 1718 | NewC.ICmpType == SystemZICMP::SignedOnly) |
| 1719 | return; |
| 1720 | // Convert LE and GT comparisons into LT and GE. |
| 1721 | if (NewC.CCMask == SystemZ::CCMASK_CMP_LE || |
| 1722 | NewC.CCMask == SystemZ::CCMASK_CMP_GT) { |
| 1723 | if (CmpVal == uint64_t(-1)) |
| 1724 | return; |
| 1725 | CmpVal += 1; |
| 1726 | NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ; |
| 1727 | } |
| 1728 | // If the low N bits of Op1 are zero than the low N bits of Op0 can |
| 1729 | // be masked off without changing the result. |
| 1730 | MaskVal = -(CmpVal & -CmpVal); |
| 1731 | NewC.ICmpType = SystemZICMP::UnsignedOnly; |
| 1732 | } |
Ulrich Weigand | b8d76fb | 2015-03-30 13:46:59 +0000 | [diff] [blame] | 1733 | if (!MaskVal) |
| 1734 | return; |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 1735 | |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1736 | // Check whether the combination of mask, comparison value and comparison |
| 1737 | // type are suitable. |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1738 | unsigned BitSize = NewC.Op0.getValueType().getSizeInBits(); |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1739 | unsigned NewCCMask, ShiftVal; |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1740 | if (NewC.ICmpType != SystemZICMP::SignedOnly && |
| 1741 | NewC.Op0.getOpcode() == ISD::SHL && |
| 1742 | isSimpleShift(NewC.Op0, ShiftVal) && |
| 1743 | (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, |
| 1744 | MaskVal >> ShiftVal, |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1745 | CmpVal >> ShiftVal, |
| 1746 | SystemZICMP::Any))) { |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1747 | NewC.Op0 = NewC.Op0.getOperand(0); |
| 1748 | MaskVal >>= ShiftVal; |
| 1749 | } else if (NewC.ICmpType != SystemZICMP::SignedOnly && |
| 1750 | NewC.Op0.getOpcode() == ISD::SRL && |
| 1751 | isSimpleShift(NewC.Op0, ShiftVal) && |
| 1752 | (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1753 | MaskVal << ShiftVal, |
| 1754 | CmpVal << ShiftVal, |
| 1755 | SystemZICMP::UnsignedOnly))) { |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1756 | NewC.Op0 = NewC.Op0.getOperand(0); |
| 1757 | MaskVal <<= ShiftVal; |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1758 | } else { |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1759 | NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal, |
| 1760 | NewC.ICmpType); |
Richard Sandiford | 030c165 | 2013-09-13 09:09:50 +0000 | [diff] [blame] | 1761 | if (!NewCCMask) |
| 1762 | return; |
| 1763 | } |
Richard Sandiford | 113c870 | 2013-09-03 15:38:35 +0000 | [diff] [blame] | 1764 | |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 1765 | // Go ahead and make the change. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1766 | C.Opcode = SystemZISD::TM; |
Richard Sandiford | c3dc447 | 2013-12-13 15:46:55 +0000 | [diff] [blame] | 1767 | C.Op0 = NewC.Op0; |
| 1768 | if (Mask && Mask->getZExtValue() == MaskVal) |
| 1769 | C.Op1 = SDValue(Mask, 0); |
| 1770 | else |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1771 | C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType()); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1772 | C.CCValid = SystemZ::CCMASK_TM; |
| 1773 | C.CCMask = NewCCMask; |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 1776 | // Return a Comparison that tests the condition-code result of intrinsic |
| 1777 | // node Call against constant integer CC using comparison code Cond. |
| 1778 | // Opcode is the opcode of the SystemZISD operation for the intrinsic |
| 1779 | // and CCValid is the set of possible condition-code results. |
| 1780 | static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode, |
| 1781 | SDValue Call, unsigned CCValid, uint64_t CC, |
| 1782 | ISD::CondCode Cond) { |
| 1783 | Comparison C(Call, SDValue()); |
| 1784 | C.Opcode = Opcode; |
| 1785 | C.CCValid = CCValid; |
| 1786 | if (Cond == ISD::SETEQ) |
| 1787 | // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3. |
| 1788 | C.CCMask = CC < 4 ? 1 << (3 - CC) : 0; |
| 1789 | else if (Cond == ISD::SETNE) |
| 1790 | // ...and the inverse of that. |
| 1791 | C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1; |
| 1792 | else if (Cond == ISD::SETLT || Cond == ISD::SETULT) |
| 1793 | // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3, |
| 1794 | // always true for CC>3. |
| 1795 | C.CCMask = CC < 4 ? -1 << (4 - CC) : -1; |
| 1796 | else if (Cond == ISD::SETGE || Cond == ISD::SETUGE) |
| 1797 | // ...and the inverse of that. |
| 1798 | C.CCMask = CC < 4 ? ~(-1 << (4 - CC)) : 0; |
| 1799 | else if (Cond == ISD::SETLE || Cond == ISD::SETULE) |
| 1800 | // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true), |
| 1801 | // always true for CC>3. |
| 1802 | C.CCMask = CC < 4 ? -1 << (3 - CC) : -1; |
| 1803 | else if (Cond == ISD::SETGT || Cond == ISD::SETUGT) |
| 1804 | // ...and the inverse of that. |
| 1805 | C.CCMask = CC < 4 ? ~(-1 << (3 - CC)) : 0; |
| 1806 | else |
| 1807 | llvm_unreachable("Unexpected integer comparison type"); |
| 1808 | C.CCMask &= CCValid; |
| 1809 | return C; |
| 1810 | } |
| 1811 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1812 | // Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1. |
| 1813 | static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1814 | ISD::CondCode Cond, SDLoc DL) { |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 1815 | if (CmpOp1.getOpcode() == ISD::Constant) { |
| 1816 | uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue(); |
| 1817 | unsigned Opcode, CCValid; |
| 1818 | if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN && |
| 1819 | CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) && |
| 1820 | isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid)) |
| 1821 | return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond); |
| 1822 | } |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1823 | Comparison C(CmpOp0, CmpOp1); |
| 1824 | C.CCMask = CCMaskForCondCode(Cond); |
| 1825 | if (C.Op0.getValueType().isFloatingPoint()) { |
| 1826 | C.CCValid = SystemZ::CCMASK_FCMP; |
| 1827 | C.Opcode = SystemZISD::FCMP; |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1828 | adjustForFNeg(C); |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1829 | } else { |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1830 | C.CCValid = SystemZ::CCMASK_ICMP; |
| 1831 | C.Opcode = SystemZISD::ICMP; |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1832 | // Choose the type of comparison. Equality and inequality tests can |
| 1833 | // use either signed or unsigned comparisons. The choice also doesn't |
| 1834 | // matter if both sign bits are known to be clear. In those cases we |
| 1835 | // want to give the main isel code the freedom to choose whichever |
| 1836 | // form fits best. |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1837 | if (C.CCMask == SystemZ::CCMASK_CMP_EQ || |
| 1838 | C.CCMask == SystemZ::CCMASK_CMP_NE || |
| 1839 | (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1))) |
| 1840 | C.ICmpType = SystemZICMP::Any; |
| 1841 | else if (C.CCMask & SystemZ::CCMASK_CMP_UO) |
| 1842 | C.ICmpType = SystemZICMP::UnsignedOnly; |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 1843 | else |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1844 | C.ICmpType = SystemZICMP::SignedOnly; |
| 1845 | C.CCMask &= ~SystemZ::CCMASK_CMP_UO; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1846 | adjustZeroCmp(DAG, DL, C); |
| 1847 | adjustSubwordCmp(DAG, DL, C); |
| 1848 | adjustForSubtraction(DAG, DL, C); |
Richard Sandiford | 83a0b6a | 2013-12-20 11:56:02 +0000 | [diff] [blame] | 1849 | adjustForLTGFR(C); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1850 | adjustICmpTruncate(DAG, DL, C); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1853 | if (shouldSwapCmpOperands(C)) { |
| 1854 | std::swap(C.Op0, C.Op1); |
| 1855 | C.CCMask = reverseCCMask(C.CCMask); |
Richard Sandiford | 24e597b | 2013-08-23 11:27:19 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1858 | adjustForTestUnderMask(DAG, DL, C); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1859 | return C; |
| 1860 | } |
| 1861 | |
| 1862 | // Emit the comparison instruction described by C. |
| 1863 | static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) { |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 1864 | if (!C.Op1.getNode()) { |
| 1865 | SDValue Op; |
| 1866 | switch (C.Op0.getOpcode()) { |
| 1867 | case ISD::INTRINSIC_W_CHAIN: |
| 1868 | Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode); |
| 1869 | break; |
| 1870 | default: |
| 1871 | llvm_unreachable("Invalid comparison operands"); |
| 1872 | } |
| 1873 | return SDValue(Op.getNode(), Op->getNumValues() - 1); |
| 1874 | } |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1875 | if (C.Opcode == SystemZISD::ICMP) |
| 1876 | return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1877 | DAG.getConstant(C.ICmpType, DL, MVT::i32)); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1878 | if (C.Opcode == SystemZISD::TM) { |
| 1879 | bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) != |
| 1880 | bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1)); |
| 1881 | return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1882 | DAG.getConstant(RegisterOnly, DL, MVT::i32)); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 1883 | } |
| 1884 | return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 1887 | // Implement a 32-bit *MUL_LOHI operation by extending both operands to |
| 1888 | // 64 bits. Extend is the extension type to use. Store the high part |
| 1889 | // in Hi and the low part in Lo. |
| 1890 | static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL, |
| 1891 | unsigned Extend, SDValue Op0, SDValue Op1, |
| 1892 | SDValue &Hi, SDValue &Lo) { |
| 1893 | Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0); |
| 1894 | Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1); |
| 1895 | SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1896 | Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, |
| 1897 | DAG.getConstant(32, DL, MVT::i64)); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 1898 | Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi); |
| 1899 | Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul); |
| 1900 | } |
| 1901 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1902 | // Lower a binary operation that produces two VT results, one in each |
| 1903 | // half of a GR128 pair. Op0 and Op1 are the VT operands to the operation, |
| 1904 | // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation |
| 1905 | // on the extended Op0 and (unextended) Op1. Store the even register result |
| 1906 | // in Even and the odd register result in Odd. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1907 | static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1908 | unsigned Extend, unsigned Opcode, |
| 1909 | SDValue Op0, SDValue Op1, |
| 1910 | SDValue &Even, SDValue &Odd) { |
| 1911 | SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0); |
| 1912 | SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, |
| 1913 | SDValue(In128, 0), Op1); |
| 1914 | bool Is32Bit = is32Bit(VT); |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 1915 | Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result); |
| 1916 | Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 1919 | // Return an i32 value that is 1 if the CC value produced by Glue is |
| 1920 | // in the mask CCMask and 0 otherwise. CC is known to have a value |
| 1921 | // in CCValid, so other values can be ignored. |
| 1922 | static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue, |
| 1923 | unsigned CCValid, unsigned CCMask) { |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 1924 | IPMConversion Conversion = getIPMConversion(CCValid, CCMask); |
| 1925 | SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue); |
| 1926 | |
| 1927 | if (Conversion.XORValue) |
| 1928 | Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1929 | DAG.getConstant(Conversion.XORValue, DL, MVT::i32)); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 1930 | |
| 1931 | if (Conversion.AddValue) |
| 1932 | Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1933 | DAG.getConstant(Conversion.AddValue, DL, MVT::i32)); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 1934 | |
| 1935 | // The SHR/AND sequence should get optimized to an RISBG. |
| 1936 | Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1937 | DAG.getConstant(Conversion.Bit, DL, MVT::i32)); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 1938 | if (Conversion.Bit != 31) |
| 1939 | Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1940 | DAG.getConstant(1, DL, MVT::i32)); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 1941 | return Result; |
| 1942 | } |
| 1943 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 1944 | // Return the SystemISD vector comparison operation for CC, or 0 if it cannot |
| 1945 | // be done directly. IsFP is true if CC is for a floating-point rather than |
| 1946 | // integer comparison. |
| 1947 | static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1948 | switch (CC) { |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 1949 | case ISD::SETOEQ: |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1950 | case ISD::SETEQ: |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 1951 | return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1952 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 1953 | case ISD::SETOGE: |
| 1954 | case ISD::SETGE: |
| 1955 | return IsFP ? SystemZISD::VFCMPHE : 0; |
| 1956 | |
| 1957 | case ISD::SETOGT: |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1958 | case ISD::SETGT: |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 1959 | return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1960 | |
| 1961 | case ISD::SETUGT: |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 1962 | return IsFP ? 0 : SystemZISD::VICMPHL; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1963 | |
| 1964 | default: |
| 1965 | return 0; |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | // Return the SystemZISD vector comparison operation for CC or its inverse, |
| 1970 | // 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] | 1971 | // result is for the inverse of CC. IsFP is true if CC is for a |
| 1972 | // floating-point rather than integer comparison. |
| 1973 | static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP, |
| 1974 | bool &Invert) { |
| 1975 | if (unsigned Opcode = getVectorComparison(CC, IsFP)) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1976 | Invert = false; |
| 1977 | return Opcode; |
| 1978 | } |
| 1979 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 1980 | CC = ISD::getSetCCInverse(CC, !IsFP); |
| 1981 | if (unsigned Opcode = getVectorComparison(CC, IsFP)) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1982 | Invert = true; |
| 1983 | return Opcode; |
| 1984 | } |
| 1985 | |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 1989 | // Return a v2f64 that contains the extended form of elements Start and Start+1 |
| 1990 | // of v4f32 value Op. |
| 1991 | static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, SDLoc DL, |
| 1992 | SDValue Op) { |
| 1993 | int Mask[] = { Start, -1, Start + 1, -1 }; |
| 1994 | Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask); |
| 1995 | return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op); |
| 1996 | } |
| 1997 | |
| 1998 | // Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode, |
| 1999 | // producing a result of type VT. |
| 2000 | static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, SDLoc DL, |
| 2001 | EVT VT, SDValue CmpOp0, SDValue CmpOp1) { |
| 2002 | // There is no hardware support for v4f32, so extend the vector into |
| 2003 | // two v2f64s and compare those. |
| 2004 | if (CmpOp0.getValueType() == MVT::v4f32) { |
| 2005 | SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0); |
| 2006 | SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0); |
| 2007 | SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1); |
| 2008 | SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1); |
| 2009 | SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1); |
| 2010 | SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1); |
| 2011 | return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes); |
| 2012 | } |
| 2013 | return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1); |
| 2014 | } |
| 2015 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2016 | // Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing |
| 2017 | // an integer mask of type VT. |
| 2018 | static SDValue lowerVectorSETCC(SelectionDAG &DAG, SDLoc DL, EVT VT, |
| 2019 | ISD::CondCode CC, SDValue CmpOp0, |
| 2020 | SDValue CmpOp1) { |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2021 | bool IsFP = CmpOp0.getValueType().isFloatingPoint(); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2022 | bool Invert = false; |
| 2023 | SDValue Cmp; |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2024 | switch (CC) { |
| 2025 | // Handle tests for order using (or (ogt y x) (oge x y)). |
| 2026 | case ISD::SETUO: |
| 2027 | Invert = true; |
| 2028 | case ISD::SETO: { |
| 2029 | assert(IsFP && "Unexpected integer comparison"); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 2030 | SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0); |
| 2031 | SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2032 | Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE); |
| 2033 | break; |
| 2034 | } |
| 2035 | |
| 2036 | // Handle <> tests using (or (ogt y x) (ogt x y)). |
| 2037 | case ISD::SETUEQ: |
| 2038 | Invert = true; |
| 2039 | case ISD::SETONE: { |
| 2040 | assert(IsFP && "Unexpected integer comparison"); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 2041 | SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0); |
| 2042 | SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2043 | Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT); |
| 2044 | break; |
| 2045 | } |
| 2046 | |
| 2047 | // Otherwise a single comparison is enough. It doesn't really |
| 2048 | // matter whether we try the inversion or the swap first, since |
| 2049 | // there are no cases where both work. |
| 2050 | default: |
| 2051 | if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert)) |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 2052 | Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2053 | else { |
| 2054 | CC = ISD::getSetCCSwappedOperands(CC); |
| 2055 | if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert)) |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 2056 | Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 2057 | else |
| 2058 | llvm_unreachable("Unhandled comparison"); |
| 2059 | } |
| 2060 | break; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2061 | } |
| 2062 | if (Invert) { |
| 2063 | SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, |
| 2064 | DAG.getConstant(65535, DL, MVT::i32)); |
| 2065 | Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask); |
| 2066 | Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask); |
| 2067 | } |
| 2068 | return Cmp; |
| 2069 | } |
| 2070 | |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2071 | SDValue SystemZTargetLowering::lowerSETCC(SDValue Op, |
| 2072 | SelectionDAG &DAG) const { |
| 2073 | SDValue CmpOp0 = Op.getOperand(0); |
| 2074 | SDValue CmpOp1 = Op.getOperand(1); |
| 2075 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); |
| 2076 | SDLoc DL(Op); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2077 | EVT VT = Op.getValueType(); |
| 2078 | if (VT.isVector()) |
| 2079 | return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2080 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2081 | Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL)); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2082 | SDValue Glue = emitCmp(DAG, DL, C); |
| 2083 | return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2086 | SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2087 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); |
| 2088 | SDValue CmpOp0 = Op.getOperand(2); |
| 2089 | SDValue CmpOp1 = Op.getOperand(3); |
| 2090 | SDValue Dest = Op.getOperand(4); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2091 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2092 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2093 | Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL)); |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2094 | SDValue Glue = emitCmp(DAG, DL, C); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2095 | return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2096 | Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32), |
| 2097 | DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Richard Sandiford | 5748547 | 2013-12-13 15:35:00 +0000 | [diff] [blame] | 2100 | // Return true if Pos is CmpOp and Neg is the negative of CmpOp, |
| 2101 | // allowing Pos and Neg to be wider than CmpOp. |
| 2102 | static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) { |
| 2103 | return (Neg.getOpcode() == ISD::SUB && |
| 2104 | Neg.getOperand(0).getOpcode() == ISD::Constant && |
| 2105 | cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 && |
| 2106 | Neg.getOperand(1) == Pos && |
| 2107 | (Pos == CmpOp || |
| 2108 | (Pos.getOpcode() == ISD::SIGN_EXTEND && |
| 2109 | Pos.getOperand(0) == CmpOp))); |
| 2110 | } |
| 2111 | |
| 2112 | // Return the absolute or negative absolute of Op; IsNegative decides which. |
| 2113 | static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op, |
| 2114 | bool IsNegative) { |
| 2115 | Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op); |
| 2116 | if (IsNegative) |
| 2117 | Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2118 | DAG.getConstant(0, DL, Op.getValueType()), Op); |
Richard Sandiford | 5748547 | 2013-12-13 15:35:00 +0000 | [diff] [blame] | 2119 | return Op; |
| 2120 | } |
| 2121 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2122 | SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op, |
| 2123 | SelectionDAG &DAG) const { |
| 2124 | SDValue CmpOp0 = Op.getOperand(0); |
| 2125 | SDValue CmpOp1 = Op.getOperand(1); |
| 2126 | SDValue TrueOp = Op.getOperand(2); |
| 2127 | SDValue FalseOp = Op.getOperand(3); |
| 2128 | ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2129 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2130 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2131 | Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL)); |
Richard Sandiford | 5748547 | 2013-12-13 15:35:00 +0000 | [diff] [blame] | 2132 | |
| 2133 | // Check for absolute and negative-absolute selections, including those |
| 2134 | // where the comparison value is sign-extended (for LPGFR and LNGFR). |
| 2135 | // This check supplements the one in DAGCombiner. |
| 2136 | if (C.Opcode == SystemZISD::ICMP && |
| 2137 | C.CCMask != SystemZ::CCMASK_CMP_EQ && |
| 2138 | C.CCMask != SystemZ::CCMASK_CMP_NE && |
| 2139 | C.Op1.getOpcode() == ISD::Constant && |
| 2140 | cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { |
| 2141 | if (isAbsolute(C.Op0, TrueOp, FalseOp)) |
| 2142 | return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT); |
| 2143 | if (isAbsolute(C.Op0, FalseOp, TrueOp)) |
| 2144 | return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT); |
| 2145 | } |
| 2146 | |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2147 | SDValue Glue = emitCmp(DAG, DL, C); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2148 | |
| 2149 | // Special case for handling -1/0 results. The shifts we use here |
| 2150 | // should get optimized with the IPM conversion sequence. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2151 | auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp); |
| 2152 | auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2153 | if (TrueC && FalseC) { |
| 2154 | int64_t TrueVal = TrueC->getSExtValue(); |
| 2155 | int64_t FalseVal = FalseC->getSExtValue(); |
| 2156 | if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) { |
| 2157 | // Invert the condition if we want -1 on false. |
| 2158 | if (TrueVal == 0) |
Richard Sandiford | d420f73 | 2013-12-13 15:28:45 +0000 | [diff] [blame] | 2159 | C.CCMask ^= C.CCValid; |
| 2160 | SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2161 | EVT VT = Op.getValueType(); |
| 2162 | // Extend the result to VT. Upper bits are ignored. |
| 2163 | if (!is32Bit(VT)) |
| 2164 | Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result); |
| 2165 | // Sign-extend from the low bit. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2166 | SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32); |
Richard Sandiford | 48ef6ab | 2013-12-06 09:53:09 +0000 | [diff] [blame] | 2167 | SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt); |
| 2168 | return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt); |
| 2169 | } |
| 2170 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2171 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2172 | SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32), |
| 2173 | DAG.getConstant(C.CCMask, DL, MVT::i32), Glue}; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2174 | |
| 2175 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 2176 | return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node, |
| 2180 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2181 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2182 | const GlobalValue *GV = Node->getGlobal(); |
| 2183 | int64_t Offset = Node->getOffset(); |
| 2184 | EVT PtrVT = getPointerTy(); |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 2185 | Reloc::Model RM = DAG.getTarget().getRelocationModel(); |
| 2186 | CodeModel::Model CM = DAG.getTarget().getCodeModel(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2187 | |
| 2188 | SDValue Result; |
| 2189 | if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) { |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 2190 | // Assign anchors at 1<<12 byte boundaries. |
| 2191 | uint64_t Anchor = Offset & ~uint64_t(0xfff); |
| 2192 | Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor); |
| 2193 | Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2194 | |
| 2195 | // The offset can be folded into the address if it is aligned to a halfword. |
| 2196 | Offset -= Anchor; |
| 2197 | if (Offset != 0 && (Offset & 1) == 0) { |
| 2198 | SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset); |
| 2199 | Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2200 | Offset = 0; |
| 2201 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2202 | } else { |
| 2203 | Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT); |
| 2204 | Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2205 | Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, |
| 2206 | MachinePointerInfo::getGOT(), false, false, false, 0); |
| 2207 | } |
| 2208 | |
| 2209 | // If there was a non-zero offset that we didn't fold, create an explicit |
| 2210 | // addition for it. |
| 2211 | if (Offset != 0) |
| 2212 | Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2213 | DAG.getConstant(Offset, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2214 | |
| 2215 | return Result; |
| 2216 | } |
| 2217 | |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2218 | SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node, |
| 2219 | SelectionDAG &DAG, |
| 2220 | unsigned Opcode, |
| 2221 | SDValue GOTOffset) const { |
| 2222 | SDLoc DL(Node); |
| 2223 | EVT PtrVT = getPointerTy(); |
| 2224 | SDValue Chain = DAG.getEntryNode(); |
| 2225 | SDValue Glue; |
| 2226 | |
| 2227 | // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12. |
| 2228 | SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT); |
| 2229 | Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue); |
| 2230 | Glue = Chain.getValue(1); |
| 2231 | Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue); |
| 2232 | Glue = Chain.getValue(1); |
| 2233 | |
| 2234 | // The first call operand is the chain and the second is the TLS symbol. |
| 2235 | SmallVector<SDValue, 8> Ops; |
| 2236 | Ops.push_back(Chain); |
| 2237 | Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL, |
| 2238 | Node->getValueType(0), |
| 2239 | 0, 0)); |
| 2240 | |
| 2241 | // Add argument registers to the end of the list so that they are |
| 2242 | // known live into the call. |
| 2243 | Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT)); |
| 2244 | Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT)); |
| 2245 | |
| 2246 | // Add a register mask operand representing the call-preserved registers. |
| 2247 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
Eric Christopher | 9deb75d | 2015-03-11 22:42:13 +0000 | [diff] [blame] | 2248 | const uint32_t *Mask = |
| 2249 | TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C); |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2250 | assert(Mask && "Missing call preserved mask for calling convention"); |
| 2251 | Ops.push_back(DAG.getRegisterMask(Mask)); |
| 2252 | |
| 2253 | // Glue the call to the argument copies. |
| 2254 | Ops.push_back(Glue); |
| 2255 | |
| 2256 | // Emit the call. |
| 2257 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); |
| 2258 | Chain = DAG.getNode(Opcode, DL, NodeTys, Ops); |
| 2259 | Glue = Chain.getValue(1); |
| 2260 | |
| 2261 | // Copy the return value from %r2. |
| 2262 | return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue); |
| 2263 | } |
| 2264 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2265 | SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node, |
| 2266 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2267 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2268 | const GlobalValue *GV = Node->getGlobal(); |
| 2269 | EVT PtrVT = getPointerTy(); |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 2270 | TLSModel::Model model = DAG.getTarget().getTLSModel(GV); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2271 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2272 | // The high part of the thread pointer is in access register 0. |
| 2273 | SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2274 | DAG.getConstant(0, DL, MVT::i32)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2275 | TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi); |
| 2276 | |
| 2277 | // The low part of the thread pointer is in access register 1. |
| 2278 | SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2279 | DAG.getConstant(1, DL, MVT::i32)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2280 | TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo); |
| 2281 | |
| 2282 | // Merge them into a single 64-bit address. |
| 2283 | SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2284 | DAG.getConstant(32, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2285 | SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo); |
| 2286 | |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2287 | // Get the offset of GA from the thread pointer, based on the TLS model. |
| 2288 | SDValue Offset; |
| 2289 | switch (model) { |
| 2290 | case TLSModel::GeneralDynamic: { |
| 2291 | // Load the GOT offset of the tls_index (module ID / per-symbol offset). |
| 2292 | SystemZConstantPoolValue *CPV = |
| 2293 | SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2294 | |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2295 | Offset = DAG.getConstantPool(CPV, PtrVT, 8); |
| 2296 | Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), |
| 2297 | Offset, MachinePointerInfo::getConstantPool(), |
| 2298 | false, false, false, 0); |
| 2299 | |
| 2300 | // Call __tls_get_offset to retrieve the offset. |
| 2301 | Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset); |
| 2302 | break; |
| 2303 | } |
| 2304 | |
| 2305 | case TLSModel::LocalDynamic: { |
| 2306 | // Load the GOT offset of the module ID. |
| 2307 | SystemZConstantPoolValue *CPV = |
| 2308 | SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM); |
| 2309 | |
| 2310 | Offset = DAG.getConstantPool(CPV, PtrVT, 8); |
| 2311 | Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), |
| 2312 | Offset, MachinePointerInfo::getConstantPool(), |
| 2313 | false, false, false, 0); |
| 2314 | |
| 2315 | // Call __tls_get_offset to retrieve the module base offset. |
| 2316 | Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset); |
| 2317 | |
| 2318 | // Note: The SystemZLDCleanupPass will remove redundant computations |
| 2319 | // of the module base offset. Count total number of local-dynamic |
| 2320 | // accesses to trigger execution of that pass. |
| 2321 | SystemZMachineFunctionInfo* MFI = |
| 2322 | DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>(); |
| 2323 | MFI->incNumLocalDynamicTLSAccesses(); |
| 2324 | |
| 2325 | // Add the per-symbol offset. |
| 2326 | CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF); |
| 2327 | |
| 2328 | SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8); |
| 2329 | DTPOffset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), |
| 2330 | DTPOffset, MachinePointerInfo::getConstantPool(), |
| 2331 | false, false, false, 0); |
| 2332 | |
| 2333 | Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset); |
| 2334 | break; |
| 2335 | } |
| 2336 | |
| 2337 | case TLSModel::InitialExec: { |
| 2338 | // Load the offset from the GOT. |
| 2339 | Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, |
| 2340 | SystemZII::MO_INDNTPOFF); |
| 2341 | Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset); |
| 2342 | Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), |
| 2343 | Offset, MachinePointerInfo::getGOT(), |
| 2344 | false, false, false, 0); |
| 2345 | break; |
| 2346 | } |
| 2347 | |
| 2348 | case TLSModel::LocalExec: { |
| 2349 | // Force the offset into the constant pool and load it from there. |
| 2350 | SystemZConstantPoolValue *CPV = |
| 2351 | SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF); |
| 2352 | |
| 2353 | Offset = DAG.getConstantPool(CPV, PtrVT, 8); |
| 2354 | Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), |
| 2355 | Offset, MachinePointerInfo::getConstantPool(), |
| 2356 | false, false, false, 0); |
| 2357 | break; |
Ulrich Weigand | b7e5909 | 2015-02-18 09:42:23 +0000 | [diff] [blame] | 2358 | } |
Ulrich Weigand | 7db6918 | 2015-02-18 09:13:27 +0000 | [diff] [blame] | 2359 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2360 | |
| 2361 | // Add the base and offset together. |
| 2362 | return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset); |
| 2363 | } |
| 2364 | |
| 2365 | SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node, |
| 2366 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2367 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2368 | const BlockAddress *BA = Node->getBlockAddress(); |
| 2369 | int64_t Offset = Node->getOffset(); |
| 2370 | EVT PtrVT = getPointerTy(); |
| 2371 | |
| 2372 | SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset); |
| 2373 | Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2374 | return Result; |
| 2375 | } |
| 2376 | |
| 2377 | SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT, |
| 2378 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2379 | SDLoc DL(JT); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2380 | EVT PtrVT = getPointerTy(); |
| 2381 | SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); |
| 2382 | |
| 2383 | // Use LARL to load the address of the table. |
| 2384 | return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2385 | } |
| 2386 | |
| 2387 | SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP, |
| 2388 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2389 | SDLoc DL(CP); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2390 | EVT PtrVT = getPointerTy(); |
| 2391 | |
| 2392 | SDValue Result; |
| 2393 | if (CP->isMachineConstantPoolEntry()) |
| 2394 | Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, |
| 2395 | CP->getAlignment()); |
| 2396 | else |
| 2397 | Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, |
| 2398 | CP->getAlignment(), CP->getOffset()); |
| 2399 | |
| 2400 | // Use LARL to load the address of the constant pool entry. |
| 2401 | return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); |
| 2402 | } |
| 2403 | |
| 2404 | SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op, |
| 2405 | SelectionDAG &DAG) const { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2406 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2407 | SDValue In = Op.getOperand(0); |
| 2408 | EVT InVT = In.getValueType(); |
| 2409 | EVT ResVT = Op.getValueType(); |
| 2410 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2411 | // Convert loads directly. This is normally done by DAGCombiner, |
| 2412 | // but we need this case for bitcasts that are created during lowering |
| 2413 | // and which are then lowered themselves. |
| 2414 | if (auto *LoadN = dyn_cast<LoadSDNode>(In)) |
| 2415 | return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(), |
| 2416 | LoadN->getMemOperand()); |
| 2417 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2418 | if (InVT == MVT::i32 && ResVT == MVT::f32) { |
Richard Sandiford | f6377fb | 2013-10-01 14:31:11 +0000 | [diff] [blame] | 2419 | SDValue In64; |
| 2420 | if (Subtarget.hasHighWord()) { |
| 2421 | SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, |
| 2422 | MVT::i64); |
| 2423 | In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL, |
| 2424 | MVT::i64, SDValue(U64, 0), In); |
| 2425 | } else { |
| 2426 | In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In); |
| 2427 | In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2428 | DAG.getConstant(32, DL, MVT::i64)); |
Richard Sandiford | f6377fb | 2013-10-01 14:31:11 +0000 | [diff] [blame] | 2429 | } |
| 2430 | SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64); |
Ulrich Weigand | 9ac2f9b | 2015-05-04 17:41:22 +0000 | [diff] [blame] | 2431 | return DAG.getTargetExtractSubreg(SystemZ::subreg_r32, |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 2432 | DL, MVT::f32, Out64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2433 | } |
| 2434 | if (InVT == MVT::f32 && ResVT == MVT::i32) { |
| 2435 | SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64); |
Ulrich Weigand | 9ac2f9b | 2015-05-04 17:41:22 +0000 | [diff] [blame] | 2436 | SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL, |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 2437 | MVT::f64, SDValue(U64, 0), In); |
| 2438 | SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64); |
Richard Sandiford | f6377fb | 2013-10-01 14:31:11 +0000 | [diff] [blame] | 2439 | if (Subtarget.hasHighWord()) |
| 2440 | return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL, |
| 2441 | MVT::i32, Out64); |
| 2442 | SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2443 | DAG.getConstant(32, DL, MVT::i64)); |
Richard Sandiford | f6377fb | 2013-10-01 14:31:11 +0000 | [diff] [blame] | 2444 | return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2445 | } |
| 2446 | llvm_unreachable("Unexpected bitcast combination"); |
| 2447 | } |
| 2448 | |
| 2449 | SDValue SystemZTargetLowering::lowerVASTART(SDValue Op, |
| 2450 | SelectionDAG &DAG) const { |
| 2451 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2452 | SystemZMachineFunctionInfo *FuncInfo = |
| 2453 | MF.getInfo<SystemZMachineFunctionInfo>(); |
| 2454 | EVT PtrVT = getPointerTy(); |
| 2455 | |
| 2456 | SDValue Chain = Op.getOperand(0); |
| 2457 | SDValue Addr = Op.getOperand(1); |
| 2458 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2459 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2460 | |
| 2461 | // The initial values of each field. |
| 2462 | const unsigned NumFields = 4; |
| 2463 | SDValue Fields[NumFields] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2464 | DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT), |
| 2465 | DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2466 | DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT), |
| 2467 | DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT) |
| 2468 | }; |
| 2469 | |
| 2470 | // Store each field into its respective slot. |
| 2471 | SDValue MemOps[NumFields]; |
| 2472 | unsigned Offset = 0; |
| 2473 | for (unsigned I = 0; I < NumFields; ++I) { |
| 2474 | SDValue FieldAddr = Addr; |
| 2475 | if (Offset != 0) |
| 2476 | FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2477 | DAG.getIntPtrConstant(Offset, DL)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2478 | MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr, |
| 2479 | MachinePointerInfo(SV, Offset), |
| 2480 | false, false, 0); |
| 2481 | Offset += 8; |
| 2482 | } |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 2483 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op, |
| 2487 | SelectionDAG &DAG) const { |
| 2488 | SDValue Chain = Op.getOperand(0); |
| 2489 | SDValue DstPtr = Op.getOperand(1); |
| 2490 | SDValue SrcPtr = Op.getOperand(2); |
| 2491 | const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); |
| 2492 | const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2493 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2494 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2495 | return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2496 | /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false, |
Krzysztof Parzyszek | a46c36b | 2015-04-13 17:16:45 +0000 | [diff] [blame] | 2497 | /*isTailCall*/false, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2498 | MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV)); |
| 2499 | } |
| 2500 | |
| 2501 | SDValue SystemZTargetLowering:: |
| 2502 | lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { |
| 2503 | SDValue Chain = Op.getOperand(0); |
| 2504 | SDValue Size = Op.getOperand(1); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2505 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2506 | |
| 2507 | unsigned SPReg = getStackPointerRegisterToSaveRestore(); |
| 2508 | |
| 2509 | // Get a reference to the stack pointer. |
| 2510 | SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64); |
| 2511 | |
| 2512 | // Get the new stack pointer value. |
| 2513 | SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size); |
| 2514 | |
| 2515 | // Copy the new stack pointer back. |
| 2516 | Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP); |
| 2517 | |
| 2518 | // The allocated data lives above the 160 bytes allocated for the standard |
| 2519 | // frame, plus any outgoing stack arguments. We don't know how much that |
| 2520 | // amounts to yet, so emit a special ADJDYNALLOC placeholder. |
| 2521 | SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64); |
| 2522 | SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust); |
| 2523 | |
| 2524 | SDValue Ops[2] = { Result, Chain }; |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2525 | return DAG.getMergeValues(Ops, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2528 | SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op, |
| 2529 | SelectionDAG &DAG) const { |
| 2530 | EVT VT = Op.getValueType(); |
| 2531 | SDLoc DL(Op); |
| 2532 | SDValue Ops[2]; |
| 2533 | if (is32Bit(VT)) |
| 2534 | // Just do a normal 64-bit multiplication and extract the results. |
| 2535 | // We define this so that it can be used for constant division. |
| 2536 | lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0), |
| 2537 | Op.getOperand(1), Ops[1], Ops[0]); |
| 2538 | else { |
| 2539 | // Do a full 128-bit multiplication based on UMUL_LOHI64: |
| 2540 | // |
| 2541 | // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64) |
| 2542 | // |
| 2543 | // but using the fact that the upper halves are either all zeros |
| 2544 | // or all ones: |
| 2545 | // |
| 2546 | // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64) |
| 2547 | // |
| 2548 | // and grouping the right terms together since they are quicker than the |
| 2549 | // multiplication: |
| 2550 | // |
| 2551 | // (ll * rl) - (((lh & rl) + (ll & rh)) << 64) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2552 | SDValue C63 = DAG.getConstant(63, DL, MVT::i64); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2553 | SDValue LL = Op.getOperand(0); |
| 2554 | SDValue RL = Op.getOperand(1); |
| 2555 | SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63); |
| 2556 | SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63); |
| 2557 | // UMUL_LOHI64 returns the low result in the odd register and the high |
| 2558 | // result in the even register. SMUL_LOHI is defined to return the |
| 2559 | // low half first, so the results are in reverse order. |
| 2560 | lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, |
| 2561 | LL, RL, Ops[1], Ops[0]); |
| 2562 | SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH); |
| 2563 | SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL); |
| 2564 | SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL); |
| 2565 | Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum); |
| 2566 | } |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2567 | return DAG.getMergeValues(Ops, DL); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2570 | SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op, |
| 2571 | SelectionDAG &DAG) const { |
| 2572 | EVT VT = Op.getValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2573 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2574 | SDValue Ops[2]; |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 2575 | if (is32Bit(VT)) |
| 2576 | // Just do a normal 64-bit multiplication and extract the results. |
| 2577 | // We define this so that it can be used for constant division. |
| 2578 | lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0), |
| 2579 | Op.getOperand(1), Ops[1], Ops[0]); |
| 2580 | else |
| 2581 | // UMUL_LOHI64 returns the low result in the odd register and the high |
| 2582 | // result in the even register. UMUL_LOHI is defined to return the |
| 2583 | // low half first, so the results are in reverse order. |
| 2584 | lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, |
| 2585 | Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2586 | return DAG.getMergeValues(Ops, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op, |
| 2590 | SelectionDAG &DAG) const { |
| 2591 | SDValue Op0 = Op.getOperand(0); |
| 2592 | SDValue Op1 = Op.getOperand(1); |
| 2593 | EVT VT = Op.getValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2594 | SDLoc DL(Op); |
Richard Sandiford | e6e7885 | 2013-07-02 15:40:22 +0000 | [diff] [blame] | 2595 | unsigned Opcode; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2596 | |
| 2597 | // We use DSGF for 32-bit division. |
| 2598 | if (is32Bit(VT)) { |
| 2599 | Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0); |
Richard Sandiford | e6e7885 | 2013-07-02 15:40:22 +0000 | [diff] [blame] | 2600 | Opcode = SystemZISD::SDIVREM32; |
| 2601 | } else if (DAG.ComputeNumSignBits(Op1) > 32) { |
| 2602 | Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1); |
| 2603 | Opcode = SystemZISD::SDIVREM32; |
| 2604 | } else |
| 2605 | Opcode = SystemZISD::SDIVREM64; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2606 | |
| 2607 | // DSG(F) takes a 64-bit dividend, so the even register in the GR128 |
| 2608 | // input is "don't care". The instruction returns the remainder in |
| 2609 | // the even register and the quotient in the odd register. |
| 2610 | SDValue Ops[2]; |
Richard Sandiford | e6e7885 | 2013-07-02 15:40:22 +0000 | [diff] [blame] | 2611 | lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2612 | Op0, Op1, Ops[1], Ops[0]); |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2613 | return DAG.getMergeValues(Ops, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op, |
| 2617 | SelectionDAG &DAG) const { |
| 2618 | EVT VT = Op.getValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2619 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2620 | |
| 2621 | // DL(G) uses a double-width dividend, so we need to clear the even |
| 2622 | // register in the GR128 input. The instruction returns the remainder |
| 2623 | // in the even register and the quotient in the odd register. |
| 2624 | SDValue Ops[2]; |
| 2625 | if (is32Bit(VT)) |
| 2626 | lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32, |
| 2627 | Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); |
| 2628 | else |
| 2629 | lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64, |
| 2630 | Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2631 | return DAG.getMergeValues(Ops, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const { |
| 2635 | assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation"); |
| 2636 | |
| 2637 | // Get the known-zero masks for each operand. |
| 2638 | SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) }; |
| 2639 | APInt KnownZero[2], KnownOne[2]; |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 2640 | DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]); |
| 2641 | DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2642 | |
| 2643 | // See if the upper 32 bits of one operand and the lower 32 bits of the |
| 2644 | // other are known zero. They are the low and high operands respectively. |
| 2645 | uint64_t Masks[] = { KnownZero[0].getZExtValue(), |
| 2646 | KnownZero[1].getZExtValue() }; |
| 2647 | unsigned High, Low; |
| 2648 | if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff) |
| 2649 | High = 1, Low = 0; |
| 2650 | else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff) |
| 2651 | High = 0, Low = 1; |
| 2652 | else |
| 2653 | return Op; |
| 2654 | |
| 2655 | SDValue LowOp = Ops[Low]; |
| 2656 | SDValue HighOp = Ops[High]; |
| 2657 | |
| 2658 | // If the high part is a constant, we're better off using IILH. |
| 2659 | if (HighOp.getOpcode() == ISD::Constant) |
| 2660 | return Op; |
| 2661 | |
| 2662 | // If the low part is a constant that is outside the range of LHI, |
| 2663 | // then we're better off using IILF. |
| 2664 | if (LowOp.getOpcode() == ISD::Constant) { |
| 2665 | int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue()); |
| 2666 | if (!isInt<16>(Value)) |
| 2667 | return Op; |
| 2668 | } |
| 2669 | |
| 2670 | // Check whether the high part is an AND that doesn't change the |
| 2671 | // high 32 bits and just masks out low bits. We can skip it if so. |
| 2672 | if (HighOp.getOpcode() == ISD::AND && |
| 2673 | HighOp.getOperand(1).getOpcode() == ISD::Constant) { |
Richard Sandiford | ccc2a7c | 2013-12-03 11:01:54 +0000 | [diff] [blame] | 2674 | SDValue HighOp0 = HighOp.getOperand(0); |
| 2675 | uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue(); |
| 2676 | if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff)))) |
| 2677 | HighOp = HighOp0; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | // Take advantage of the fact that all GR32 operations only change the |
| 2681 | // low 32 bits by truncating Low to an i32 and inserting it directly |
| 2682 | // using a subreg. The interesting cases are those where the truncation |
| 2683 | // can be folded. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2684 | SDLoc DL(Op); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2685 | SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp); |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 2686 | return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL, |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 2687 | MVT::i64, HighOp, Low32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2690 | SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op, |
| 2691 | SelectionDAG &DAG) const { |
| 2692 | EVT VT = Op.getValueType(); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2693 | SDLoc DL(Op); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2694 | Op = Op.getOperand(0); |
| 2695 | |
| 2696 | // Handle vector types via VPOPCT. |
| 2697 | if (VT.isVector()) { |
| 2698 | Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op); |
| 2699 | Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op); |
| 2700 | switch (VT.getVectorElementType().getSizeInBits()) { |
| 2701 | case 8: |
| 2702 | break; |
| 2703 | case 16: { |
| 2704 | Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 2705 | SDValue Shift = DAG.getConstant(8, DL, MVT::i32); |
| 2706 | SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift); |
| 2707 | Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); |
| 2708 | Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift); |
| 2709 | break; |
| 2710 | } |
| 2711 | case 32: { |
| 2712 | SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, |
| 2713 | DAG.getConstant(0, DL, MVT::i32)); |
| 2714 | Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); |
| 2715 | break; |
| 2716 | } |
| 2717 | case 64: { |
| 2718 | SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, |
| 2719 | DAG.getConstant(0, DL, MVT::i32)); |
| 2720 | Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp); |
| 2721 | Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp); |
| 2722 | break; |
| 2723 | } |
| 2724 | default: |
| 2725 | llvm_unreachable("Unexpected type"); |
| 2726 | } |
| 2727 | return Op; |
| 2728 | } |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Get the known-zero mask for the operand. |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2731 | APInt KnownZero, KnownOne; |
| 2732 | DAG.computeKnownBits(Op, KnownZero, KnownOne); |
Ulrich Weigand | 050527b | 2015-03-31 19:28:50 +0000 | [diff] [blame] | 2733 | unsigned NumSignificantBits = (~KnownZero).getActiveBits(); |
| 2734 | if (NumSignificantBits == 0) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2735 | return DAG.getConstant(0, DL, VT); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2736 | |
| 2737 | // Skip known-zero high parts of the operand. |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 2738 | int64_t OrigBitSize = VT.getSizeInBits(); |
Ulrich Weigand | 050527b | 2015-03-31 19:28:50 +0000 | [diff] [blame] | 2739 | int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits); |
| 2740 | BitSize = std::min(BitSize, OrigBitSize); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2741 | |
| 2742 | // The POPCNT instruction counts the number of bits in each byte. |
| 2743 | Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op); |
| 2744 | Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op); |
| 2745 | Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); |
| 2746 | |
| 2747 | // Add up per-byte counts in a binary tree. All bits of Op at |
| 2748 | // position larger than BitSize remain zero throughout. |
| 2749 | for (int64_t I = BitSize / 2; I >= 8; I = I / 2) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2750 | 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] | 2751 | if (BitSize != OrigBitSize) |
| 2752 | Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2753 | DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT)); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2754 | Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp); |
| 2755 | } |
| 2756 | |
| 2757 | // Extract overall result from high byte. |
| 2758 | if (BitSize > 8) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2759 | Op = DAG.getNode(ISD::SRL, DL, VT, Op, |
| 2760 | DAG.getConstant(BitSize - 8, DL, VT)); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 2761 | |
| 2762 | return Op; |
| 2763 | } |
| 2764 | |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 2765 | // Op is an atomic load. Lower it into a normal volatile load. |
| 2766 | SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op, |
| 2767 | SelectionDAG &DAG) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2768 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 2769 | return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(), |
| 2770 | Node->getChain(), Node->getBasePtr(), |
| 2771 | Node->getMemoryVT(), Node->getMemOperand()); |
| 2772 | } |
| 2773 | |
| 2774 | // Op is an atomic store. Lower it into a normal volatile store followed |
| 2775 | // by a serialization. |
| 2776 | SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op, |
| 2777 | SelectionDAG &DAG) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2778 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 2779 | SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(), |
| 2780 | Node->getBasePtr(), Node->getMemoryVT(), |
| 2781 | Node->getMemOperand()); |
| 2782 | return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other, |
| 2783 | Chain), 0); |
| 2784 | } |
| 2785 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2786 | // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first |
| 2787 | // two into the fullword ATOMIC_LOADW_* operation given by Opcode. |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 2788 | SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op, |
| 2789 | SelectionDAG &DAG, |
| 2790 | unsigned Opcode) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2791 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2792 | |
| 2793 | // 32-bit operations need no code outside the main loop. |
| 2794 | EVT NarrowVT = Node->getMemoryVT(); |
| 2795 | EVT WideVT = MVT::i32; |
| 2796 | if (NarrowVT == WideVT) |
| 2797 | return Op; |
| 2798 | |
| 2799 | int64_t BitSize = NarrowVT.getSizeInBits(); |
| 2800 | SDValue ChainIn = Node->getChain(); |
| 2801 | SDValue Addr = Node->getBasePtr(); |
| 2802 | SDValue Src2 = Node->getVal(); |
| 2803 | MachineMemOperand *MMO = Node->getMemOperand(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2804 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2805 | EVT PtrVT = Addr.getValueType(); |
| 2806 | |
| 2807 | // Convert atomic subtracts of constants into additions. |
| 2808 | if (Opcode == SystemZISD::ATOMIC_LOADW_SUB) |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2809 | if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2810 | Opcode = SystemZISD::ATOMIC_LOADW_ADD; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2811 | Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | // Get the address of the containing word. |
| 2815 | SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2816 | DAG.getConstant(-4, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2817 | |
| 2818 | // Get the number of bits that the word must be rotated left in order |
| 2819 | // to bring the field to the top bits of a GR32. |
| 2820 | SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2821 | DAG.getConstant(3, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2822 | BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); |
| 2823 | |
| 2824 | // Get the complementing shift amount, for rotating a field in the top |
| 2825 | // bits back to its proper position. |
| 2826 | SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2827 | DAG.getConstant(0, DL, WideVT), BitShift); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2828 | |
| 2829 | // Extend the source operand to 32 bits and prepare it for the inner loop. |
| 2830 | // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other |
| 2831 | // operations require the source to be shifted in advance. (This shift |
| 2832 | // can be folded if the source is constant.) For AND and NAND, the lower |
| 2833 | // bits must be set, while for other opcodes they should be left clear. |
| 2834 | if (Opcode != SystemZISD::ATOMIC_SWAPW) |
| 2835 | Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2836 | DAG.getConstant(32 - BitSize, DL, WideVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2837 | if (Opcode == SystemZISD::ATOMIC_LOADW_AND || |
| 2838 | Opcode == SystemZISD::ATOMIC_LOADW_NAND) |
| 2839 | Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2840 | DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2841 | |
| 2842 | // Construct the ATOMIC_LOADW_* node. |
| 2843 | SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); |
| 2844 | SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2845 | DAG.getConstant(BitSize, DL, WideVT) }; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2846 | SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2847 | NarrowVT, MMO); |
| 2848 | |
| 2849 | // Rotate the result of the final CS so that the field is in the lower |
| 2850 | // bits of a GR32, then truncate it. |
| 2851 | SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2852 | DAG.getConstant(BitSize, DL, WideVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2853 | SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift); |
| 2854 | |
| 2855 | SDValue RetOps[2] = { Result, AtomicOp.getValue(1) }; |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 2856 | return DAG.getMergeValues(RetOps, DL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 2859 | // 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] | 2860 | // 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] | 2861 | // operations into additions. |
| 2862 | SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op, |
| 2863 | SelectionDAG &DAG) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2864 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 2865 | EVT MemVT = Node->getMemoryVT(); |
| 2866 | if (MemVT == MVT::i32 || MemVT == MVT::i64) { |
| 2867 | // A full-width operation. |
| 2868 | assert(Op.getValueType() == MemVT && "Mismatched VTs"); |
| 2869 | SDValue Src2 = Node->getVal(); |
| 2870 | SDValue NegSrc2; |
| 2871 | SDLoc DL(Src2); |
| 2872 | |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2873 | if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) { |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 2874 | // Use an addition if the operand is constant and either LAA(G) is |
| 2875 | // available or the negative value is in the range of A(G)FHI. |
| 2876 | int64_t Value = (-Op2->getAPIntValue()).getSExtValue(); |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 2877 | if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1()) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2878 | NegSrc2 = DAG.getConstant(Value, DL, MemVT); |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 2879 | } else if (Subtarget.hasInterlockedAccess1()) |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 2880 | // Use LAA(G) if available. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2881 | NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT), |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 2882 | Src2); |
| 2883 | |
| 2884 | if (NegSrc2.getNode()) |
| 2885 | return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT, |
| 2886 | Node->getChain(), Node->getBasePtr(), NegSrc2, |
| 2887 | Node->getMemOperand(), Node->getOrdering(), |
| 2888 | Node->getSynchScope()); |
| 2889 | |
| 2890 | // Use the node as-is. |
| 2891 | return Op; |
| 2892 | } |
| 2893 | |
| 2894 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB); |
| 2895 | } |
| 2896 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2897 | // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two |
| 2898 | // into a fullword ATOMIC_CMP_SWAPW operation. |
| 2899 | SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op, |
| 2900 | SelectionDAG &DAG) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2901 | auto *Node = cast<AtomicSDNode>(Op.getNode()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2902 | |
| 2903 | // We have native support for 32-bit compare and swap. |
| 2904 | EVT NarrowVT = Node->getMemoryVT(); |
| 2905 | EVT WideVT = MVT::i32; |
| 2906 | if (NarrowVT == WideVT) |
| 2907 | return Op; |
| 2908 | |
| 2909 | int64_t BitSize = NarrowVT.getSizeInBits(); |
| 2910 | SDValue ChainIn = Node->getOperand(0); |
| 2911 | SDValue Addr = Node->getOperand(1); |
| 2912 | SDValue CmpVal = Node->getOperand(2); |
| 2913 | SDValue SwapVal = Node->getOperand(3); |
| 2914 | MachineMemOperand *MMO = Node->getMemOperand(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2915 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2916 | EVT PtrVT = Addr.getValueType(); |
| 2917 | |
| 2918 | // Get the address of the containing word. |
| 2919 | SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2920 | DAG.getConstant(-4, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2921 | |
| 2922 | // Get the number of bits that the word must be rotated left in order |
| 2923 | // to bring the field to the top bits of a GR32. |
| 2924 | SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2925 | DAG.getConstant(3, DL, PtrVT)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2926 | BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); |
| 2927 | |
| 2928 | // Get the complementing shift amount, for rotating a field in the top |
| 2929 | // bits back to its proper position. |
| 2930 | SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2931 | DAG.getConstant(0, DL, WideVT), BitShift); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2932 | |
| 2933 | // Construct the ATOMIC_CMP_SWAPW node. |
| 2934 | SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); |
| 2935 | SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2936 | NegBitShift, DAG.getConstant(BitSize, DL, WideVT) }; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2937 | SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL, |
Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 2938 | VTList, Ops, NarrowVT, MMO); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2939 | return AtomicOp; |
| 2940 | } |
| 2941 | |
| 2942 | SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op, |
| 2943 | SelectionDAG &DAG) const { |
| 2944 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2945 | MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2946 | return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2947 | SystemZ::R15D, Op.getValueType()); |
| 2948 | } |
| 2949 | |
| 2950 | SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op, |
| 2951 | SelectionDAG &DAG) const { |
| 2952 | MachineFunction &MF = DAG.getMachineFunction(); |
| 2953 | MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2954 | return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op), |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 2955 | SystemZ::R15D, Op.getOperand(1)); |
| 2956 | } |
| 2957 | |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 2958 | SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op, |
| 2959 | SelectionDAG &DAG) const { |
| 2960 | bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); |
| 2961 | if (!IsData) |
| 2962 | // Just preserve the chain. |
| 2963 | return Op.getOperand(0); |
| 2964 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2965 | SDLoc DL(Op); |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 2966 | bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); |
| 2967 | unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ; |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 2968 | auto *Node = cast<MemIntrinsicSDNode>(Op.getNode()); |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 2969 | SDValue Ops[] = { |
| 2970 | Op.getOperand(0), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2971 | DAG.getConstant(Code, DL, MVT::i32), |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 2972 | Op.getOperand(1) |
| 2973 | }; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2974 | return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL, |
Craig Topper | 206fcd4 | 2014-04-26 19:29:41 +0000 | [diff] [blame] | 2975 | Node->getVTList(), Ops, |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 2976 | Node->getMemoryVT(), Node->getMemOperand()); |
| 2977 | } |
| 2978 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2979 | // Return an i32 that contains the value of CC immediately after After, |
| 2980 | // whose final operand must be MVT::Glue. |
| 2981 | static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2982 | SDLoc DL(After); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2983 | SDValue Glue = SDValue(After, After->getNumValues() - 1); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2984 | SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue); |
| 2985 | return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM, |
| 2986 | DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32)); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | SDValue |
| 2990 | SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op, |
| 2991 | SelectionDAG &DAG) const { |
| 2992 | unsigned Opcode, CCValid; |
| 2993 | if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) { |
| 2994 | assert(Op->getNumValues() == 2 && "Expected only CC result and chain"); |
| 2995 | SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode); |
| 2996 | SDValue CC = getCCResult(DAG, Glued.getNode()); |
| 2997 | DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC); |
| 2998 | return SDValue(); |
| 2999 | } |
| 3000 | |
| 3001 | return SDValue(); |
| 3002 | } |
| 3003 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3004 | namespace { |
| 3005 | // Says that SystemZISD operation Opcode can be used to perform the equivalent |
| 3006 | // of a VPERM with permute vector Bytes. If Opcode takes three operands, |
| 3007 | // Operand is the constant third operand, otherwise it is the number of |
| 3008 | // bytes in each element of the result. |
| 3009 | struct Permute { |
| 3010 | unsigned Opcode; |
| 3011 | unsigned Operand; |
| 3012 | unsigned char Bytes[SystemZ::VectorBytes]; |
| 3013 | }; |
| 3014 | } |
| 3015 | |
| 3016 | static const Permute PermuteForms[] = { |
| 3017 | // VMRHG |
| 3018 | { SystemZISD::MERGE_HIGH, 8, |
| 3019 | { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } }, |
| 3020 | // VMRHF |
| 3021 | { SystemZISD::MERGE_HIGH, 4, |
| 3022 | { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } }, |
| 3023 | // VMRHH |
| 3024 | { SystemZISD::MERGE_HIGH, 2, |
| 3025 | { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } }, |
| 3026 | // VMRHB |
| 3027 | { SystemZISD::MERGE_HIGH, 1, |
| 3028 | { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } }, |
| 3029 | // VMRLG |
| 3030 | { SystemZISD::MERGE_LOW, 8, |
| 3031 | { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } }, |
| 3032 | // VMRLF |
| 3033 | { SystemZISD::MERGE_LOW, 4, |
| 3034 | { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } }, |
| 3035 | // VMRLH |
| 3036 | { SystemZISD::MERGE_LOW, 2, |
| 3037 | { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } }, |
| 3038 | // VMRLB |
| 3039 | { SystemZISD::MERGE_LOW, 1, |
| 3040 | { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } }, |
| 3041 | // VPKG |
| 3042 | { SystemZISD::PACK, 4, |
| 3043 | { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } }, |
| 3044 | // VPKF |
| 3045 | { SystemZISD::PACK, 2, |
| 3046 | { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } }, |
| 3047 | // VPKH |
| 3048 | { SystemZISD::PACK, 1, |
| 3049 | { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } }, |
| 3050 | // VPDI V1, V2, 4 (low half of V1, high half of V2) |
| 3051 | { SystemZISD::PERMUTE_DWORDS, 4, |
| 3052 | { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } }, |
| 3053 | // VPDI V1, V2, 1 (high half of V1, low half of V2) |
| 3054 | { SystemZISD::PERMUTE_DWORDS, 1, |
| 3055 | { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } } |
| 3056 | }; |
| 3057 | |
| 3058 | // Called after matching a vector shuffle against a particular pattern. |
| 3059 | // Both the original shuffle and the pattern have two vector operands. |
| 3060 | // OpNos[0] is the operand of the original shuffle that should be used for |
| 3061 | // operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything. |
| 3062 | // OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and |
| 3063 | // set OpNo0 and OpNo1 to the shuffle operands that should actually be used |
| 3064 | // for operands 0 and 1 of the pattern. |
| 3065 | static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) { |
| 3066 | if (OpNos[0] < 0) { |
| 3067 | if (OpNos[1] < 0) |
| 3068 | return false; |
| 3069 | OpNo0 = OpNo1 = OpNos[1]; |
| 3070 | } else if (OpNos[1] < 0) { |
| 3071 | OpNo0 = OpNo1 = OpNos[0]; |
| 3072 | } else { |
| 3073 | OpNo0 = OpNos[0]; |
| 3074 | OpNo1 = OpNos[1]; |
| 3075 | } |
| 3076 | return true; |
| 3077 | } |
| 3078 | |
| 3079 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3080 | // undefined bytes. Return true if the VPERM can be implemented using P. |
| 3081 | // When returning true set OpNo0 to the VPERM operand that should be |
| 3082 | // used for operand 0 of P and likewise OpNo1 for operand 1 of P. |
| 3083 | // |
| 3084 | // For example, if swapping the VPERM operands allows P to match, OpNo0 |
| 3085 | // will be 1 and OpNo1 will be 0. If instead Bytes only refers to one |
| 3086 | // operand, but rewriting it to use two duplicated operands allows it to |
| 3087 | // match P, then OpNo0 and OpNo1 will be the same. |
| 3088 | static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P, |
| 3089 | unsigned &OpNo0, unsigned &OpNo1) { |
| 3090 | int OpNos[] = { -1, -1 }; |
| 3091 | for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) { |
| 3092 | int Elt = Bytes[I]; |
| 3093 | if (Elt >= 0) { |
| 3094 | // Make sure that the two permute vectors use the same suboperand |
| 3095 | // byte number. Only the operand numbers (the high bits) are |
| 3096 | // allowed to differ. |
| 3097 | if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1)) |
| 3098 | return false; |
| 3099 | int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes; |
| 3100 | int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes; |
| 3101 | // Make sure that the operand mappings are consistent with previous |
| 3102 | // elements. |
| 3103 | if (OpNos[ModelOpNo] == 1 - RealOpNo) |
| 3104 | return false; |
| 3105 | OpNos[ModelOpNo] = RealOpNo; |
| 3106 | } |
| 3107 | } |
| 3108 | return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); |
| 3109 | } |
| 3110 | |
| 3111 | // As above, but search for a matching permute. |
| 3112 | static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes, |
| 3113 | unsigned &OpNo0, unsigned &OpNo1) { |
| 3114 | for (auto &P : PermuteForms) |
| 3115 | if (matchPermute(Bytes, P, OpNo0, OpNo1)) |
| 3116 | return &P; |
| 3117 | return nullptr; |
| 3118 | } |
| 3119 | |
| 3120 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3121 | // undefined bytes. This permute is an operand of an outer permute. |
| 3122 | // See whether redistributing the -1 bytes gives a shuffle that can be |
| 3123 | // implemented using P. If so, set Transform to a VPERM-like permute vector |
| 3124 | // that, when applied to the result of P, gives the original permute in Bytes. |
| 3125 | static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes, |
| 3126 | const Permute &P, |
| 3127 | SmallVectorImpl<int> &Transform) { |
| 3128 | unsigned To = 0; |
| 3129 | for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) { |
| 3130 | int Elt = Bytes[From]; |
| 3131 | if (Elt < 0) |
| 3132 | // Byte number From of the result is undefined. |
| 3133 | Transform[From] = -1; |
| 3134 | else { |
| 3135 | while (P.Bytes[To] != Elt) { |
| 3136 | To += 1; |
| 3137 | if (To == SystemZ::VectorBytes) |
| 3138 | return false; |
| 3139 | } |
| 3140 | Transform[From] = To; |
| 3141 | } |
| 3142 | } |
| 3143 | return true; |
| 3144 | } |
| 3145 | |
| 3146 | // As above, but search for a matching permute. |
| 3147 | static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes, |
| 3148 | SmallVectorImpl<int> &Transform) { |
| 3149 | for (auto &P : PermuteForms) |
| 3150 | if (matchDoublePermute(Bytes, P, Transform)) |
| 3151 | return &P; |
| 3152 | return nullptr; |
| 3153 | } |
| 3154 | |
| 3155 | // Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask, |
| 3156 | // as if it had type vNi8. |
| 3157 | static void getVPermMask(ShuffleVectorSDNode *VSN, |
| 3158 | SmallVectorImpl<int> &Bytes) { |
| 3159 | EVT VT = VSN->getValueType(0); |
| 3160 | unsigned NumElements = VT.getVectorNumElements(); |
| 3161 | unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); |
| 3162 | Bytes.resize(NumElements * BytesPerElement, -1); |
| 3163 | for (unsigned I = 0; I < NumElements; ++I) { |
| 3164 | int Index = VSN->getMaskElt(I); |
| 3165 | if (Index >= 0) |
| 3166 | for (unsigned J = 0; J < BytesPerElement; ++J) |
| 3167 | Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J; |
| 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3172 | // undefined bytes. See whether bytes [Start, Start + BytesPerElement) of |
| 3173 | // the result come from a contiguous sequence of bytes from one input. |
| 3174 | // Set Base to the selector for the first byte if so. |
| 3175 | static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start, |
| 3176 | unsigned BytesPerElement, int &Base) { |
| 3177 | Base = -1; |
| 3178 | for (unsigned I = 0; I < BytesPerElement; ++I) { |
| 3179 | if (Bytes[Start + I] >= 0) { |
| 3180 | unsigned Elem = Bytes[Start + I]; |
| 3181 | if (Base < 0) { |
| 3182 | Base = Elem - I; |
| 3183 | // Make sure the bytes would come from one input operand. |
| 3184 | if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size()) |
| 3185 | return false; |
| 3186 | } else if (unsigned(Base) != Elem - I) |
| 3187 | return false; |
| 3188 | } |
| 3189 | } |
| 3190 | return true; |
| 3191 | } |
| 3192 | |
| 3193 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3194 | // undefined bytes. Return true if it can be performed using VSLDI. |
| 3195 | // When returning true, set StartIndex to the shift amount and OpNo0 |
| 3196 | // and OpNo1 to the VPERM operands that should be used as the first |
| 3197 | // and second shift operand respectively. |
| 3198 | static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes, |
| 3199 | unsigned &StartIndex, unsigned &OpNo0, |
| 3200 | unsigned &OpNo1) { |
| 3201 | int OpNos[] = { -1, -1 }; |
| 3202 | int Shift = -1; |
| 3203 | for (unsigned I = 0; I < 16; ++I) { |
| 3204 | int Index = Bytes[I]; |
| 3205 | if (Index >= 0) { |
| 3206 | int ExpectedShift = (Index - I) % SystemZ::VectorBytes; |
| 3207 | int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes; |
| 3208 | int RealOpNo = unsigned(Index) / SystemZ::VectorBytes; |
| 3209 | if (Shift < 0) |
| 3210 | Shift = ExpectedShift; |
| 3211 | else if (Shift != ExpectedShift) |
| 3212 | return false; |
| 3213 | // Make sure that the operand mappings are consistent with previous |
| 3214 | // elements. |
| 3215 | if (OpNos[ModelOpNo] == 1 - RealOpNo) |
| 3216 | return false; |
| 3217 | OpNos[ModelOpNo] = RealOpNo; |
| 3218 | } |
| 3219 | } |
| 3220 | StartIndex = Shift; |
| 3221 | return chooseShuffleOpNos(OpNos, OpNo0, OpNo1); |
| 3222 | } |
| 3223 | |
| 3224 | // Create a node that performs P on operands Op0 and Op1, casting the |
| 3225 | // operands to the appropriate type. The type of the result is determined by P. |
| 3226 | static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL, |
| 3227 | const Permute &P, SDValue Op0, SDValue Op1) { |
| 3228 | // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input |
| 3229 | // elements of a PACK are twice as wide as the outputs. |
| 3230 | unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 : |
| 3231 | P.Opcode == SystemZISD::PACK ? P.Operand * 2 : |
| 3232 | P.Operand); |
| 3233 | // Cast both operands to the appropriate type. |
| 3234 | MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8), |
| 3235 | SystemZ::VectorBytes / InBytes); |
| 3236 | Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0); |
| 3237 | Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1); |
| 3238 | SDValue Op; |
| 3239 | if (P.Opcode == SystemZISD::PERMUTE_DWORDS) { |
| 3240 | SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32); |
| 3241 | Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2); |
| 3242 | } else if (P.Opcode == SystemZISD::PACK) { |
| 3243 | MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8), |
| 3244 | SystemZ::VectorBytes / P.Operand); |
| 3245 | Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1); |
| 3246 | } else { |
| 3247 | Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1); |
| 3248 | } |
| 3249 | return Op; |
| 3250 | } |
| 3251 | |
| 3252 | // Bytes is a VPERM-like permute vector, except that -1 is used for |
| 3253 | // undefined bytes. Implement it on operands Ops[0] and Ops[1] using |
| 3254 | // VSLDI or VPERM. |
| 3255 | static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops, |
| 3256 | const SmallVectorImpl<int> &Bytes) { |
| 3257 | for (unsigned I = 0; I < 2; ++I) |
| 3258 | Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]); |
| 3259 | |
| 3260 | // First see whether VSLDI can be used. |
| 3261 | unsigned StartIndex, OpNo0, OpNo1; |
| 3262 | if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1)) |
| 3263 | return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0], |
| 3264 | Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32)); |
| 3265 | |
| 3266 | // Fall back on VPERM. Construct an SDNode for the permute vector. |
| 3267 | SDValue IndexNodes[SystemZ::VectorBytes]; |
| 3268 | for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) |
| 3269 | if (Bytes[I] >= 0) |
| 3270 | IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32); |
| 3271 | else |
| 3272 | IndexNodes[I] = DAG.getUNDEF(MVT::i32); |
| 3273 | SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes); |
| 3274 | return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2); |
| 3275 | } |
| 3276 | |
| 3277 | namespace { |
| 3278 | // Describes a general N-operand vector shuffle. |
| 3279 | struct GeneralShuffle { |
| 3280 | GeneralShuffle(EVT vt) : VT(vt) {} |
| 3281 | void addUndef(); |
| 3282 | void add(SDValue, unsigned); |
| 3283 | SDValue getNode(SelectionDAG &, SDLoc); |
| 3284 | |
| 3285 | // The operands of the shuffle. |
| 3286 | SmallVector<SDValue, SystemZ::VectorBytes> Ops; |
| 3287 | |
| 3288 | // Index I is -1 if byte I of the result is undefined. Otherwise the |
| 3289 | // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand |
| 3290 | // Bytes[I] / SystemZ::VectorBytes. |
| 3291 | SmallVector<int, SystemZ::VectorBytes> Bytes; |
| 3292 | |
| 3293 | // The type of the shuffle result. |
| 3294 | EVT VT; |
| 3295 | }; |
| 3296 | } |
| 3297 | |
| 3298 | // Add an extra undefined element to the shuffle. |
| 3299 | void GeneralShuffle::addUndef() { |
| 3300 | unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); |
| 3301 | for (unsigned I = 0; I < BytesPerElement; ++I) |
| 3302 | Bytes.push_back(-1); |
| 3303 | } |
| 3304 | |
| 3305 | // Add an extra element to the shuffle, taking it from element Elem of Op. |
| 3306 | // A null Op indicates a vector input whose value will be calculated later; |
| 3307 | // there is at most one such input per shuffle and it always has the same |
| 3308 | // type as the result. |
| 3309 | void GeneralShuffle::add(SDValue Op, unsigned Elem) { |
| 3310 | unsigned BytesPerElement = VT.getVectorElementType().getStoreSize(); |
| 3311 | |
| 3312 | // The source vector can have wider elements than the result, |
| 3313 | // either through an explicit TRUNCATE or because of type legalization. |
| 3314 | // We want the least significant part. |
| 3315 | EVT FromVT = Op.getNode() ? Op.getValueType() : VT; |
| 3316 | unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize(); |
| 3317 | assert(FromBytesPerElement >= BytesPerElement && |
| 3318 | "Invalid EXTRACT_VECTOR_ELT"); |
| 3319 | unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes + |
| 3320 | (FromBytesPerElement - BytesPerElement)); |
| 3321 | |
| 3322 | // Look through things like shuffles and bitcasts. |
| 3323 | while (Op.getNode()) { |
| 3324 | if (Op.getOpcode() == ISD::BITCAST) |
| 3325 | Op = Op.getOperand(0); |
| 3326 | else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) { |
| 3327 | // See whether the bytes we need come from a contiguous part of one |
| 3328 | // operand. |
| 3329 | SmallVector<int, SystemZ::VectorBytes> OpBytes; |
| 3330 | getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes); |
| 3331 | int NewByte; |
| 3332 | if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte)) |
| 3333 | break; |
| 3334 | if (NewByte < 0) { |
| 3335 | addUndef(); |
| 3336 | return; |
| 3337 | } |
| 3338 | Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes); |
| 3339 | Byte = unsigned(NewByte) % SystemZ::VectorBytes; |
| 3340 | } else if (Op.getOpcode() == ISD::UNDEF) { |
| 3341 | addUndef(); |
| 3342 | return; |
| 3343 | } else |
| 3344 | break; |
| 3345 | } |
| 3346 | |
| 3347 | // Make sure that the source of the extraction is in Ops. |
| 3348 | unsigned OpNo = 0; |
| 3349 | for (; OpNo < Ops.size(); ++OpNo) |
| 3350 | if (Ops[OpNo] == Op) |
| 3351 | break; |
| 3352 | if (OpNo == Ops.size()) |
| 3353 | Ops.push_back(Op); |
| 3354 | |
| 3355 | // Add the element to Bytes. |
| 3356 | unsigned Base = OpNo * SystemZ::VectorBytes + Byte; |
| 3357 | for (unsigned I = 0; I < BytesPerElement; ++I) |
| 3358 | Bytes.push_back(Base + I); |
| 3359 | } |
| 3360 | |
| 3361 | // Return SDNodes for the completed shuffle. |
| 3362 | SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) { |
| 3363 | assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector"); |
| 3364 | |
| 3365 | if (Ops.size() == 0) |
| 3366 | return DAG.getUNDEF(VT); |
| 3367 | |
| 3368 | // Make sure that there are at least two shuffle operands. |
| 3369 | if (Ops.size() == 1) |
| 3370 | Ops.push_back(DAG.getUNDEF(MVT::v16i8)); |
| 3371 | |
| 3372 | // Create a tree of shuffles, deferring root node until after the loop. |
| 3373 | // Try to redistribute the undefined elements of non-root nodes so that |
| 3374 | // the non-root shuffles match something like a pack or merge, then adjust |
| 3375 | // the parent node's permute vector to compensate for the new order. |
| 3376 | // Among other things, this copes with vectors like <2 x i16> that were |
| 3377 | // padded with undefined elements during type legalization. |
| 3378 | // |
| 3379 | // In the best case this redistribution will lead to the whole tree |
| 3380 | // using packs and merges. It should rarely be a loss in other cases. |
| 3381 | unsigned Stride = 1; |
| 3382 | for (; Stride * 2 < Ops.size(); Stride *= 2) { |
| 3383 | for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) { |
| 3384 | SDValue SubOps[] = { Ops[I], Ops[I + Stride] }; |
| 3385 | |
| 3386 | // Create a mask for just these two operands. |
| 3387 | SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes); |
| 3388 | for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { |
| 3389 | unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes; |
| 3390 | unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes; |
| 3391 | if (OpNo == I) |
| 3392 | NewBytes[J] = Byte; |
| 3393 | else if (OpNo == I + Stride) |
| 3394 | NewBytes[J] = SystemZ::VectorBytes + Byte; |
| 3395 | else |
| 3396 | NewBytes[J] = -1; |
| 3397 | } |
| 3398 | // See if it would be better to reorganize NewMask to avoid using VPERM. |
| 3399 | SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes); |
| 3400 | if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) { |
| 3401 | Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]); |
| 3402 | // Applying NewBytesMap to Ops[I] gets back to NewBytes. |
| 3403 | for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) { |
| 3404 | if (NewBytes[J] >= 0) { |
| 3405 | assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes && |
| 3406 | "Invalid double permute"); |
| 3407 | Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J]; |
| 3408 | } else |
| 3409 | assert(NewBytesMap[J] < 0 && "Invalid double permute"); |
| 3410 | } |
| 3411 | } else { |
| 3412 | // Just use NewBytes on the operands. |
| 3413 | Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes); |
| 3414 | for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) |
| 3415 | if (NewBytes[J] >= 0) |
| 3416 | Bytes[J] = I * SystemZ::VectorBytes + J; |
| 3417 | } |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | // Now we just have 2 inputs. Put the second operand in Ops[1]. |
| 3422 | if (Stride > 1) { |
| 3423 | Ops[1] = Ops[Stride]; |
| 3424 | for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) |
| 3425 | if (Bytes[I] >= int(SystemZ::VectorBytes)) |
| 3426 | Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes; |
| 3427 | } |
| 3428 | |
| 3429 | // Look for an instruction that can do the permute without resorting |
| 3430 | // to VPERM. |
| 3431 | unsigned OpNo0, OpNo1; |
| 3432 | SDValue Op; |
| 3433 | if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1)) |
| 3434 | Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]); |
| 3435 | else |
| 3436 | Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes); |
| 3437 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 3438 | } |
| 3439 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 3440 | // Return true if the given BUILD_VECTOR is a scalar-to-vector conversion. |
| 3441 | static bool isScalarToVector(SDValue Op) { |
| 3442 | for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I) |
| 3443 | if (Op.getOperand(I).getOpcode() != ISD::UNDEF) |
| 3444 | return false; |
| 3445 | return true; |
| 3446 | } |
| 3447 | |
| 3448 | // Return a vector of type VT that contains Value in the first element. |
| 3449 | // The other elements don't matter. |
| 3450 | static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT, |
| 3451 | SDValue Value) { |
| 3452 | // If we have a constant, replicate it to all elements and let the |
| 3453 | // BUILD_VECTOR lowering take care of it. |
| 3454 | if (Value.getOpcode() == ISD::Constant || |
| 3455 | Value.getOpcode() == ISD::ConstantFP) { |
| 3456 | SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value); |
| 3457 | return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops); |
| 3458 | } |
| 3459 | if (Value.getOpcode() == ISD::UNDEF) |
| 3460 | return DAG.getUNDEF(VT); |
| 3461 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value); |
| 3462 | } |
| 3463 | |
| 3464 | // Return a vector of type VT in which Op0 is in element 0 and Op1 is in |
| 3465 | // element 1. Used for cases in which replication is cheap. |
| 3466 | static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT, |
| 3467 | SDValue Op0, SDValue Op1) { |
| 3468 | if (Op0.getOpcode() == ISD::UNDEF) { |
| 3469 | if (Op1.getOpcode() == ISD::UNDEF) |
| 3470 | return DAG.getUNDEF(VT); |
| 3471 | return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1); |
| 3472 | } |
| 3473 | if (Op1.getOpcode() == ISD::UNDEF) |
| 3474 | return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0); |
| 3475 | return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT, |
| 3476 | buildScalarToVector(DAG, DL, VT, Op0), |
| 3477 | buildScalarToVector(DAG, DL, VT, Op1)); |
| 3478 | } |
| 3479 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3480 | // Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64 |
| 3481 | // vector for them. |
| 3482 | static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0, |
| 3483 | SDValue Op1) { |
| 3484 | if (Op0.getOpcode() == ISD::UNDEF && Op1.getOpcode() == ISD::UNDEF) |
| 3485 | return DAG.getUNDEF(MVT::v2i64); |
| 3486 | // If one of the two inputs is undefined then replicate the other one, |
| 3487 | // in order to avoid using another register unnecessarily. |
| 3488 | if (Op0.getOpcode() == ISD::UNDEF) |
| 3489 | Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); |
| 3490 | else if (Op1.getOpcode() == ISD::UNDEF) |
| 3491 | Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); |
| 3492 | else { |
| 3493 | Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0); |
| 3494 | Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1); |
| 3495 | } |
| 3496 | return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1); |
| 3497 | } |
| 3498 | |
| 3499 | // Try to represent constant BUILD_VECTOR node BVN using a |
| 3500 | // SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask |
| 3501 | // on success. |
| 3502 | static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) { |
| 3503 | EVT ElemVT = BVN->getValueType(0).getVectorElementType(); |
| 3504 | unsigned BytesPerElement = ElemVT.getStoreSize(); |
| 3505 | for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) { |
| 3506 | SDValue Op = BVN->getOperand(I); |
| 3507 | if (Op.getOpcode() != ISD::UNDEF) { |
| 3508 | uint64_t Value; |
| 3509 | if (Op.getOpcode() == ISD::Constant) |
| 3510 | Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue(); |
| 3511 | else if (Op.getOpcode() == ISD::ConstantFP) |
| 3512 | Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt() |
| 3513 | .getZExtValue()); |
| 3514 | else |
| 3515 | return false; |
| 3516 | for (unsigned J = 0; J < BytesPerElement; ++J) { |
| 3517 | uint64_t Byte = (Value >> (J * 8)) & 0xff; |
| 3518 | if (Byte == 0xff) |
| 3519 | Mask |= 1 << ((E - I - 1) * BytesPerElement + J); |
| 3520 | else if (Byte != 0) |
| 3521 | return false; |
| 3522 | } |
| 3523 | } |
| 3524 | } |
| 3525 | return true; |
| 3526 | } |
| 3527 | |
| 3528 | // Try to load a vector constant in which BitsPerElement-bit value Value |
| 3529 | // is replicated to fill the vector. VT is the type of the resulting |
| 3530 | // constant, which may have elements of a different size from BitsPerElement. |
| 3531 | // Return the SDValue of the constant on success, otherwise return |
| 3532 | // an empty value. |
| 3533 | static SDValue tryBuildVectorReplicate(SelectionDAG &DAG, |
| 3534 | const SystemZInstrInfo *TII, |
| 3535 | SDLoc DL, EVT VT, uint64_t Value, |
| 3536 | unsigned BitsPerElement) { |
| 3537 | // Signed 16-bit values can be replicated using VREPI. |
| 3538 | int64_t SignedValue = SignExtend64(Value, BitsPerElement); |
| 3539 | if (isInt<16>(SignedValue)) { |
| 3540 | MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement), |
| 3541 | SystemZ::VectorBits / BitsPerElement); |
| 3542 | SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT, |
| 3543 | DAG.getConstant(SignedValue, DL, MVT::i32)); |
| 3544 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 3545 | } |
| 3546 | // See whether rotating the constant left some N places gives a value that |
| 3547 | // is one less than a power of 2 (i.e. all zeros followed by all ones). |
| 3548 | // If so we can use VGM. |
| 3549 | unsigned Start, End; |
| 3550 | if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) { |
| 3551 | // isRxSBGMask returns the bit numbers for a full 64-bit value, |
| 3552 | // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to |
| 3553 | // bit numbers for an BitsPerElement value, so that 0 denotes |
| 3554 | // 1 << (BitsPerElement-1). |
| 3555 | Start -= 64 - BitsPerElement; |
| 3556 | End -= 64 - BitsPerElement; |
| 3557 | MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement), |
| 3558 | SystemZ::VectorBits / BitsPerElement); |
| 3559 | SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT, |
| 3560 | DAG.getConstant(Start, DL, MVT::i32), |
| 3561 | DAG.getConstant(End, DL, MVT::i32)); |
| 3562 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 3563 | } |
| 3564 | return SDValue(); |
| 3565 | } |
| 3566 | |
| 3567 | // If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually |
| 3568 | // better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for |
| 3569 | // the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR |
| 3570 | // would benefit from this representation and return it if so. |
| 3571 | static SDValue tryBuildVectorShuffle(SelectionDAG &DAG, |
| 3572 | BuildVectorSDNode *BVN) { |
| 3573 | EVT VT = BVN->getValueType(0); |
| 3574 | unsigned NumElements = VT.getVectorNumElements(); |
| 3575 | |
| 3576 | // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation |
| 3577 | // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still |
| 3578 | // need a BUILD_VECTOR, add an additional placeholder operand for that |
| 3579 | // BUILD_VECTOR and store its operands in ResidueOps. |
| 3580 | GeneralShuffle GS(VT); |
| 3581 | SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps; |
| 3582 | bool FoundOne = false; |
| 3583 | for (unsigned I = 0; I < NumElements; ++I) { |
| 3584 | SDValue Op = BVN->getOperand(I); |
| 3585 | if (Op.getOpcode() == ISD::TRUNCATE) |
| 3586 | Op = Op.getOperand(0); |
| 3587 | if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 3588 | Op.getOperand(1).getOpcode() == ISD::Constant) { |
| 3589 | unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue(); |
| 3590 | GS.add(Op.getOperand(0), Elem); |
| 3591 | FoundOne = true; |
| 3592 | } else if (Op.getOpcode() == ISD::UNDEF) { |
| 3593 | GS.addUndef(); |
| 3594 | } else { |
| 3595 | GS.add(SDValue(), ResidueOps.size()); |
| 3596 | ResidueOps.push_back(Op); |
| 3597 | } |
| 3598 | } |
| 3599 | |
| 3600 | // Nothing to do if there are no EXTRACT_VECTOR_ELTs. |
| 3601 | if (!FoundOne) |
| 3602 | return SDValue(); |
| 3603 | |
| 3604 | // Create the BUILD_VECTOR for the remaining elements, if any. |
| 3605 | if (!ResidueOps.empty()) { |
| 3606 | while (ResidueOps.size() < NumElements) |
| 3607 | ResidueOps.push_back(DAG.getUNDEF(VT.getVectorElementType())); |
| 3608 | for (auto &Op : GS.Ops) { |
| 3609 | if (!Op.getNode()) { |
| 3610 | Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps); |
| 3611 | break; |
| 3612 | } |
| 3613 | } |
| 3614 | } |
| 3615 | return GS.getNode(DAG, SDLoc(BVN)); |
| 3616 | } |
| 3617 | |
| 3618 | // Combine GPR scalar values Elems into a vector of type VT. |
| 3619 | static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT, |
| 3620 | SmallVectorImpl<SDValue> &Elems) { |
| 3621 | // See whether there is a single replicated value. |
| 3622 | SDValue Single; |
| 3623 | unsigned int NumElements = Elems.size(); |
| 3624 | unsigned int Count = 0; |
| 3625 | for (auto Elem : Elems) { |
| 3626 | if (Elem.getOpcode() != ISD::UNDEF) { |
| 3627 | if (!Single.getNode()) |
| 3628 | Single = Elem; |
| 3629 | else if (Elem != Single) { |
| 3630 | Single = SDValue(); |
| 3631 | break; |
| 3632 | } |
| 3633 | Count += 1; |
| 3634 | } |
| 3635 | } |
| 3636 | // There are three cases here: |
| 3637 | // |
| 3638 | // - if the only defined element is a loaded one, the best sequence |
| 3639 | // is a replicating load. |
| 3640 | // |
| 3641 | // - otherwise, if the only defined element is an i64 value, we will |
| 3642 | // end up with the same VLVGP sequence regardless of whether we short-cut |
| 3643 | // for replication or fall through to the later code. |
| 3644 | // |
| 3645 | // - otherwise, if the only defined element is an i32 or smaller value, |
| 3646 | // we would need 2 instructions to replicate it: VLVGP followed by VREPx. |
| 3647 | // This is only a win if the single defined element is used more than once. |
| 3648 | // In other cases we're better off using a single VLVGx. |
| 3649 | if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD)) |
| 3650 | return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single); |
| 3651 | |
| 3652 | // The best way of building a v2i64 from two i64s is to use VLVGP. |
| 3653 | if (VT == MVT::v2i64) |
| 3654 | return joinDwords(DAG, DL, Elems[0], Elems[1]); |
| 3655 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 3656 | // Use a 64-bit merge high to combine two doubles. |
| 3657 | if (VT == MVT::v2f64) |
| 3658 | return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); |
| 3659 | |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 3660 | // Build v4f32 values directly from the FPRs: |
| 3661 | // |
| 3662 | // <Axxx> <Bxxx> <Cxxxx> <Dxxx> |
| 3663 | // V V VMRHF |
| 3664 | // <ABxx> <CDxx> |
| 3665 | // V VMRHG |
| 3666 | // <ABCD> |
| 3667 | if (VT == MVT::v4f32) { |
| 3668 | SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]); |
| 3669 | SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]); |
| 3670 | // Avoid unnecessary undefs by reusing the other operand. |
| 3671 | if (Op01.getOpcode() == ISD::UNDEF) |
| 3672 | Op01 = Op23; |
| 3673 | else if (Op23.getOpcode() == ISD::UNDEF) |
| 3674 | Op23 = Op01; |
| 3675 | // Merging identical replications is a no-op. |
| 3676 | if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23) |
| 3677 | return Op01; |
| 3678 | Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01); |
| 3679 | Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23); |
| 3680 | SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH, |
| 3681 | DL, MVT::v2i64, Op01, Op23); |
| 3682 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 3683 | } |
| 3684 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3685 | // Collect the constant terms. |
| 3686 | SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue()); |
| 3687 | SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false); |
| 3688 | |
| 3689 | unsigned NumConstants = 0; |
| 3690 | for (unsigned I = 0; I < NumElements; ++I) { |
| 3691 | SDValue Elem = Elems[I]; |
| 3692 | if (Elem.getOpcode() == ISD::Constant || |
| 3693 | Elem.getOpcode() == ISD::ConstantFP) { |
| 3694 | NumConstants += 1; |
| 3695 | Constants[I] = Elem; |
| 3696 | Done[I] = true; |
| 3697 | } |
| 3698 | } |
| 3699 | // If there was at least one constant, fill in the other elements of |
| 3700 | // Constants with undefs to get a full vector constant and use that |
| 3701 | // as the starting point. |
| 3702 | SDValue Result; |
| 3703 | if (NumConstants > 0) { |
| 3704 | for (unsigned I = 0; I < NumElements; ++I) |
| 3705 | if (!Constants[I].getNode()) |
| 3706 | Constants[I] = DAG.getUNDEF(Elems[I].getValueType()); |
| 3707 | Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants); |
| 3708 | } else { |
| 3709 | // Otherwise try to use VLVGP to start the sequence in order to |
| 3710 | // avoid a false dependency on any previous contents of the vector |
| 3711 | // register. This only makes sense if one of the associated elements |
| 3712 | // is defined. |
| 3713 | unsigned I1 = NumElements / 2 - 1; |
| 3714 | unsigned I2 = NumElements - 1; |
| 3715 | bool Def1 = (Elems[I1].getOpcode() != ISD::UNDEF); |
| 3716 | bool Def2 = (Elems[I2].getOpcode() != ISD::UNDEF); |
| 3717 | if (Def1 || Def2) { |
| 3718 | SDValue Elem1 = Elems[Def1 ? I1 : I2]; |
| 3719 | SDValue Elem2 = Elems[Def2 ? I2 : I1]; |
| 3720 | Result = DAG.getNode(ISD::BITCAST, DL, VT, |
| 3721 | joinDwords(DAG, DL, Elem1, Elem2)); |
| 3722 | Done[I1] = true; |
| 3723 | Done[I2] = true; |
| 3724 | } else |
| 3725 | Result = DAG.getUNDEF(VT); |
| 3726 | } |
| 3727 | |
| 3728 | // Use VLVGx to insert the other elements. |
| 3729 | for (unsigned I = 0; I < NumElements; ++I) |
| 3730 | if (!Done[I] && Elems[I].getOpcode() != ISD::UNDEF) |
| 3731 | Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I], |
| 3732 | DAG.getConstant(I, DL, MVT::i32)); |
| 3733 | return Result; |
| 3734 | } |
| 3735 | |
| 3736 | SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op, |
| 3737 | SelectionDAG &DAG) const { |
| 3738 | const SystemZInstrInfo *TII = |
| 3739 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
| 3740 | auto *BVN = cast<BuildVectorSDNode>(Op.getNode()); |
| 3741 | SDLoc DL(Op); |
| 3742 | EVT VT = Op.getValueType(); |
| 3743 | |
| 3744 | if (BVN->isConstant()) { |
| 3745 | // Try using VECTOR GENERATE BYTE MASK. This is the architecturally- |
| 3746 | // preferred way of creating all-zero and all-one vectors so give it |
| 3747 | // priority over other methods below. |
| 3748 | uint64_t Mask = 0; |
| 3749 | if (tryBuildVectorByteMask(BVN, Mask)) { |
| 3750 | SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8, |
| 3751 | DAG.getConstant(Mask, DL, MVT::i32)); |
| 3752 | return DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 3753 | } |
| 3754 | |
| 3755 | // Try using some form of replication. |
| 3756 | APInt SplatBits, SplatUndef; |
| 3757 | unsigned SplatBitSize; |
| 3758 | bool HasAnyUndefs; |
| 3759 | if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, |
| 3760 | 8, true) && |
| 3761 | SplatBitSize <= 64) { |
| 3762 | // First try assuming that any undefined bits above the highest set bit |
| 3763 | // and below the lowest set bit are 1s. This increases the likelihood of |
| 3764 | // being able to use a sign-extended element value in VECTOR REPLICATE |
| 3765 | // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK. |
| 3766 | uint64_t SplatBitsZ = SplatBits.getZExtValue(); |
| 3767 | uint64_t SplatUndefZ = SplatUndef.getZExtValue(); |
| 3768 | uint64_t Lower = (SplatUndefZ |
| 3769 | & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1)); |
| 3770 | uint64_t Upper = (SplatUndefZ |
| 3771 | & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1)); |
| 3772 | uint64_t Value = SplatBitsZ | Upper | Lower; |
| 3773 | SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, |
| 3774 | SplatBitSize); |
| 3775 | if (Op.getNode()) |
| 3776 | return Op; |
| 3777 | |
| 3778 | // Now try assuming that any undefined bits between the first and |
| 3779 | // last defined set bits are set. This increases the chances of |
| 3780 | // using a non-wraparound mask. |
| 3781 | uint64_t Middle = SplatUndefZ & ~Upper & ~Lower; |
| 3782 | Value = SplatBitsZ | Middle; |
| 3783 | Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize); |
| 3784 | if (Op.getNode()) |
| 3785 | return Op; |
| 3786 | } |
| 3787 | |
| 3788 | // Fall back to loading it from memory. |
| 3789 | return SDValue(); |
| 3790 | } |
| 3791 | |
| 3792 | // See if we should use shuffles to construct the vector from other vectors. |
| 3793 | SDValue Res = tryBuildVectorShuffle(DAG, BVN); |
| 3794 | if (Res.getNode()) |
| 3795 | return Res; |
| 3796 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 3797 | // Detect SCALAR_TO_VECTOR conversions. |
| 3798 | if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op)) |
| 3799 | return buildScalarToVector(DAG, DL, VT, Op.getOperand(0)); |
| 3800 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3801 | // Otherwise use buildVector to build the vector up from GPRs. |
| 3802 | unsigned NumElements = Op.getNumOperands(); |
| 3803 | SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements); |
| 3804 | for (unsigned I = 0; I < NumElements; ++I) |
| 3805 | Ops[I] = Op.getOperand(I); |
| 3806 | return buildVector(DAG, DL, VT, Ops); |
| 3807 | } |
| 3808 | |
| 3809 | SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op, |
| 3810 | SelectionDAG &DAG) const { |
| 3811 | auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode()); |
| 3812 | SDLoc DL(Op); |
| 3813 | EVT VT = Op.getValueType(); |
| 3814 | unsigned NumElements = VT.getVectorNumElements(); |
| 3815 | |
| 3816 | if (VSN->isSplat()) { |
| 3817 | SDValue Op0 = Op.getOperand(0); |
| 3818 | unsigned Index = VSN->getSplatIndex(); |
| 3819 | assert(Index < VT.getVectorNumElements() && |
| 3820 | "Splat index should be defined and in first operand"); |
| 3821 | // See whether the value we're splatting is directly available as a scalar. |
| 3822 | if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) || |
| 3823 | Op0.getOpcode() == ISD::BUILD_VECTOR) |
| 3824 | return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index)); |
| 3825 | // Otherwise keep it as a vector-to-vector operation. |
| 3826 | return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0), |
| 3827 | DAG.getConstant(Index, DL, MVT::i32)); |
| 3828 | } |
| 3829 | |
| 3830 | GeneralShuffle GS(VT); |
| 3831 | for (unsigned I = 0; I < NumElements; ++I) { |
| 3832 | int Elt = VSN->getMaskElt(I); |
| 3833 | if (Elt < 0) |
| 3834 | GS.addUndef(); |
| 3835 | else |
| 3836 | GS.add(Op.getOperand(unsigned(Elt) / NumElements), |
| 3837 | unsigned(Elt) % NumElements); |
| 3838 | } |
| 3839 | return GS.getNode(DAG, SDLoc(VSN)); |
| 3840 | } |
| 3841 | |
| 3842 | SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op, |
| 3843 | SelectionDAG &DAG) const { |
| 3844 | SDLoc DL(Op); |
| 3845 | // Just insert the scalar into element 0 of an undefined vector. |
| 3846 | return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, |
| 3847 | Op.getValueType(), DAG.getUNDEF(Op.getValueType()), |
| 3848 | Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32)); |
| 3849 | } |
| 3850 | |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 3851 | SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op, |
| 3852 | SelectionDAG &DAG) const { |
| 3853 | // Handle insertions of floating-point values. |
| 3854 | SDLoc DL(Op); |
| 3855 | SDValue Op0 = Op.getOperand(0); |
| 3856 | SDValue Op1 = Op.getOperand(1); |
| 3857 | SDValue Op2 = Op.getOperand(2); |
| 3858 | EVT VT = Op.getValueType(); |
| 3859 | |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 3860 | // Insertions into constant indices of a v2f64 can be done using VPDI. |
| 3861 | // However, if the inserted value is a bitcast or a constant then it's |
| 3862 | // better to use GPRs, as below. |
| 3863 | if (VT == MVT::v2f64 && |
| 3864 | Op1.getOpcode() != ISD::BITCAST && |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 3865 | Op1.getOpcode() != ISD::ConstantFP && |
| 3866 | Op2.getOpcode() == ISD::Constant) { |
| 3867 | uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue(); |
| 3868 | unsigned Mask = VT.getVectorNumElements() - 1; |
| 3869 | if (Index <= Mask) |
| 3870 | return Op; |
| 3871 | } |
| 3872 | |
| 3873 | // Otherwise bitcast to the equivalent integer form and insert via a GPR. |
| 3874 | MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits()); |
| 3875 | MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements()); |
| 3876 | SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT, |
| 3877 | DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), |
| 3878 | DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2); |
| 3879 | return DAG.getNode(ISD::BITCAST, DL, VT, Res); |
| 3880 | } |
| 3881 | |
| 3882 | SDValue |
| 3883 | SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op, |
| 3884 | SelectionDAG &DAG) const { |
| 3885 | // Handle extractions of floating-point values. |
| 3886 | SDLoc DL(Op); |
| 3887 | SDValue Op0 = Op.getOperand(0); |
| 3888 | SDValue Op1 = Op.getOperand(1); |
| 3889 | EVT VT = Op.getValueType(); |
| 3890 | EVT VecVT = Op0.getValueType(); |
| 3891 | |
| 3892 | // Extractions of constant indices can be done directly. |
| 3893 | if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) { |
| 3894 | uint64_t Index = CIndexN->getZExtValue(); |
| 3895 | unsigned Mask = VecVT.getVectorNumElements() - 1; |
| 3896 | if (Index <= Mask) |
| 3897 | return Op; |
| 3898 | } |
| 3899 | |
| 3900 | // Otherwise bitcast to the equivalent integer form and extract via a GPR. |
| 3901 | MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits()); |
| 3902 | MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements()); |
| 3903 | SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT, |
| 3904 | DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1); |
| 3905 | return DAG.getNode(ISD::BITCAST, DL, VT, Res); |
| 3906 | } |
| 3907 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 3908 | SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG, |
| 3909 | unsigned ByScalar) const { |
| 3910 | // Look for cases where a vector shift can use the *_BY_SCALAR form. |
| 3911 | SDValue Op0 = Op.getOperand(0); |
| 3912 | SDValue Op1 = Op.getOperand(1); |
| 3913 | SDLoc DL(Op); |
| 3914 | EVT VT = Op.getValueType(); |
| 3915 | unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits(); |
| 3916 | |
| 3917 | // See whether the shift vector is a splat represented as BUILD_VECTOR. |
| 3918 | if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) { |
| 3919 | APInt SplatBits, SplatUndef; |
| 3920 | unsigned SplatBitSize; |
| 3921 | bool HasAnyUndefs; |
| 3922 | // Check for constant splats. Use ElemBitSize as the minimum element |
| 3923 | // width and reject splats that need wider elements. |
| 3924 | if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, |
| 3925 | ElemBitSize, true) && |
| 3926 | SplatBitSize == ElemBitSize) { |
| 3927 | SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff, |
| 3928 | DL, MVT::i32); |
| 3929 | return DAG.getNode(ByScalar, DL, VT, Op0, Shift); |
| 3930 | } |
| 3931 | // Check for variable splats. |
| 3932 | BitVector UndefElements; |
| 3933 | SDValue Splat = BVN->getSplatValue(&UndefElements); |
| 3934 | if (Splat) { |
| 3935 | // Since i32 is the smallest legal type, we either need a no-op |
| 3936 | // or a truncation. |
| 3937 | SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat); |
| 3938 | return DAG.getNode(ByScalar, DL, VT, Op0, Shift); |
| 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | // See whether the shift vector is a splat represented as SHUFFLE_VECTOR, |
| 3943 | // and the shift amount is directly available in a GPR. |
| 3944 | if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) { |
| 3945 | if (VSN->isSplat()) { |
| 3946 | SDValue VSNOp0 = VSN->getOperand(0); |
| 3947 | unsigned Index = VSN->getSplatIndex(); |
| 3948 | assert(Index < VT.getVectorNumElements() && |
| 3949 | "Splat index should be defined and in first operand"); |
| 3950 | if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) || |
| 3951 | VSNOp0.getOpcode() == ISD::BUILD_VECTOR) { |
| 3952 | // Since i32 is the smallest legal type, we either need a no-op |
| 3953 | // or a truncation. |
| 3954 | SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, |
| 3955 | VSNOp0.getOperand(Index)); |
| 3956 | return DAG.getNode(ByScalar, DL, VT, Op0, Shift); |
| 3957 | } |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | // Otherwise just treat the current form as legal. |
| 3962 | return Op; |
| 3963 | } |
| 3964 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3965 | SDValue SystemZTargetLowering::LowerOperation(SDValue Op, |
| 3966 | SelectionDAG &DAG) const { |
| 3967 | switch (Op.getOpcode()) { |
| 3968 | case ISD::BR_CC: |
| 3969 | return lowerBR_CC(Op, DAG); |
| 3970 | case ISD::SELECT_CC: |
| 3971 | return lowerSELECT_CC(Op, DAG); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 3972 | case ISD::SETCC: |
| 3973 | return lowerSETCC(Op, DAG); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3974 | case ISD::GlobalAddress: |
| 3975 | return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG); |
| 3976 | case ISD::GlobalTLSAddress: |
| 3977 | return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG); |
| 3978 | case ISD::BlockAddress: |
| 3979 | return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG); |
| 3980 | case ISD::JumpTable: |
| 3981 | return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG); |
| 3982 | case ISD::ConstantPool: |
| 3983 | return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG); |
| 3984 | case ISD::BITCAST: |
| 3985 | return lowerBITCAST(Op, DAG); |
| 3986 | case ISD::VASTART: |
| 3987 | return lowerVASTART(Op, DAG); |
| 3988 | case ISD::VACOPY: |
| 3989 | return lowerVACOPY(Op, DAG); |
| 3990 | case ISD::DYNAMIC_STACKALLOC: |
| 3991 | return lowerDYNAMIC_STACKALLOC(Op, DAG); |
Richard Sandiford | 7d86e47 | 2013-08-21 09:34:56 +0000 | [diff] [blame] | 3992 | case ISD::SMUL_LOHI: |
| 3993 | return lowerSMUL_LOHI(Op, DAG); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 3994 | case ISD::UMUL_LOHI: |
| 3995 | return lowerUMUL_LOHI(Op, DAG); |
| 3996 | case ISD::SDIVREM: |
| 3997 | return lowerSDIVREM(Op, DAG); |
| 3998 | case ISD::UDIVREM: |
| 3999 | return lowerUDIVREM(Op, DAG); |
| 4000 | case ISD::OR: |
| 4001 | return lowerOR(Op, DAG); |
Ulrich Weigand | b401218 | 2015-03-31 12:56:33 +0000 | [diff] [blame] | 4002 | case ISD::CTPOP: |
| 4003 | return lowerCTPOP(Op, DAG); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4004 | case ISD::CTLZ_ZERO_UNDEF: |
| 4005 | return DAG.getNode(ISD::CTLZ, SDLoc(Op), |
| 4006 | Op.getValueType(), Op.getOperand(0)); |
| 4007 | case ISD::CTTZ_ZERO_UNDEF: |
| 4008 | return DAG.getNode(ISD::CTTZ, SDLoc(Op), |
| 4009 | Op.getValueType(), Op.getOperand(0)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4010 | case ISD::ATOMIC_SWAP: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4011 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW); |
| 4012 | case ISD::ATOMIC_STORE: |
| 4013 | return lowerATOMIC_STORE(Op, DAG); |
| 4014 | case ISD::ATOMIC_LOAD: |
| 4015 | return lowerATOMIC_LOAD(Op, DAG); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4016 | case ISD::ATOMIC_LOAD_ADD: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4017 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4018 | case ISD::ATOMIC_LOAD_SUB: |
Richard Sandiford | 41350a5 | 2013-12-24 15:18:04 +0000 | [diff] [blame] | 4019 | return lowerATOMIC_LOAD_SUB(Op, DAG); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4020 | case ISD::ATOMIC_LOAD_AND: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4021 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4022 | case ISD::ATOMIC_LOAD_OR: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4023 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4024 | case ISD::ATOMIC_LOAD_XOR: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4025 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4026 | case ISD::ATOMIC_LOAD_NAND: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4027 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4028 | case ISD::ATOMIC_LOAD_MIN: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4029 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4030 | case ISD::ATOMIC_LOAD_MAX: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4031 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4032 | case ISD::ATOMIC_LOAD_UMIN: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4033 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4034 | case ISD::ATOMIC_LOAD_UMAX: |
Richard Sandiford | bef3d7a | 2013-12-10 10:49:34 +0000 | [diff] [blame] | 4035 | return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4036 | case ISD::ATOMIC_CMP_SWAP: |
| 4037 | return lowerATOMIC_CMP_SWAP(Op, DAG); |
| 4038 | case ISD::STACKSAVE: |
| 4039 | return lowerSTACKSAVE(Op, DAG); |
| 4040 | case ISD::STACKRESTORE: |
| 4041 | return lowerSTACKRESTORE(Op, DAG); |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 4042 | case ISD::PREFETCH: |
| 4043 | return lowerPREFETCH(Op, DAG); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 4044 | case ISD::INTRINSIC_W_CHAIN: |
| 4045 | return lowerINTRINSIC_W_CHAIN(Op, DAG); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4046 | case ISD::BUILD_VECTOR: |
| 4047 | return lowerBUILD_VECTOR(Op, DAG); |
| 4048 | case ISD::VECTOR_SHUFFLE: |
| 4049 | return lowerVECTOR_SHUFFLE(Op, DAG); |
| 4050 | case ISD::SCALAR_TO_VECTOR: |
| 4051 | return lowerSCALAR_TO_VECTOR(Op, DAG); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 4052 | case ISD::INSERT_VECTOR_ELT: |
| 4053 | return lowerINSERT_VECTOR_ELT(Op, DAG); |
| 4054 | case ISD::EXTRACT_VECTOR_ELT: |
| 4055 | return lowerEXTRACT_VECTOR_ELT(Op, DAG); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4056 | case ISD::SHL: |
| 4057 | return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR); |
| 4058 | case ISD::SRL: |
| 4059 | return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR); |
| 4060 | case ISD::SRA: |
| 4061 | return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4062 | default: |
| 4063 | llvm_unreachable("Unexpected node to lower"); |
| 4064 | } |
| 4065 | } |
| 4066 | |
| 4067 | const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 4068 | #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME |
| 4069 | switch (Opcode) { |
| 4070 | OPCODE(RET_FLAG); |
| 4071 | OPCODE(CALL); |
Richard Sandiford | 709bda6 | 2013-08-19 12:42:31 +0000 | [diff] [blame] | 4072 | OPCODE(SIBCALL); |
Ulrich Weigand | 1c6f07d | 2015-05-04 17:39:40 +0000 | [diff] [blame] | 4073 | OPCODE(TLS_GDCALL); |
| 4074 | OPCODE(TLS_LDCALL); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4075 | OPCODE(PCREL_WRAPPER); |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 4076 | OPCODE(PCREL_OFFSET); |
Richard Sandiford | 5748547 | 2013-12-13 15:35:00 +0000 | [diff] [blame] | 4077 | OPCODE(IABS); |
Richard Sandiford | 5bc670b | 2013-09-06 11:51:39 +0000 | [diff] [blame] | 4078 | OPCODE(ICMP); |
| 4079 | OPCODE(FCMP); |
Richard Sandiford | 35b9be2 | 2013-08-28 10:31:43 +0000 | [diff] [blame] | 4080 | OPCODE(TM); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4081 | OPCODE(BR_CCMASK); |
| 4082 | OPCODE(SELECT_CCMASK); |
| 4083 | OPCODE(ADJDYNALLOC); |
| 4084 | OPCODE(EXTRACT_ACCESS); |
Ulrich Weigand | 1c6f07d | 2015-05-04 17:39:40 +0000 | [diff] [blame] | 4085 | OPCODE(POPCNT); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4086 | OPCODE(UMUL_LOHI64); |
Ulrich Weigand | 1c6f07d | 2015-05-04 17:39:40 +0000 | [diff] [blame] | 4087 | OPCODE(SDIVREM32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4088 | OPCODE(SDIVREM64); |
| 4089 | OPCODE(UDIVREM32); |
| 4090 | OPCODE(UDIVREM64); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 4091 | OPCODE(MVC); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4092 | OPCODE(MVC_LOOP); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 4093 | OPCODE(NC); |
| 4094 | OPCODE(NC_LOOP); |
| 4095 | OPCODE(OC); |
| 4096 | OPCODE(OC_LOOP); |
| 4097 | OPCODE(XC); |
| 4098 | OPCODE(XC_LOOP); |
Richard Sandiford | 761703a | 2013-08-12 10:17:33 +0000 | [diff] [blame] | 4099 | OPCODE(CLC); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4100 | OPCODE(CLC_LOOP); |
Richard Sandiford | bb83a50 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 4101 | OPCODE(STPCPY); |
Ulrich Weigand | 1c6f07d | 2015-05-04 17:39:40 +0000 | [diff] [blame] | 4102 | OPCODE(STRCMP); |
Richard Sandiford | 0dec06a | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 4103 | OPCODE(SEARCH_STRING); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 4104 | OPCODE(IPM); |
Richard Sandiford | 9afe613 | 2013-12-10 10:36:34 +0000 | [diff] [blame] | 4105 | OPCODE(SERIALIZE); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 4106 | OPCODE(TBEGIN); |
| 4107 | OPCODE(TBEGIN_NOFLOAT); |
| 4108 | OPCODE(TEND); |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4109 | OPCODE(BYTE_MASK); |
| 4110 | OPCODE(ROTATE_MASK); |
| 4111 | OPCODE(REPLICATE); |
| 4112 | OPCODE(JOIN_DWORDS); |
| 4113 | OPCODE(SPLAT); |
| 4114 | OPCODE(MERGE_HIGH); |
| 4115 | OPCODE(MERGE_LOW); |
| 4116 | OPCODE(SHL_DOUBLE); |
| 4117 | OPCODE(PERMUTE_DWORDS); |
| 4118 | OPCODE(PERMUTE); |
| 4119 | OPCODE(PACK); |
| 4120 | OPCODE(VSHL_BY_SCALAR); |
| 4121 | OPCODE(VSRL_BY_SCALAR); |
| 4122 | OPCODE(VSRA_BY_SCALAR); |
| 4123 | OPCODE(VSUM); |
| 4124 | OPCODE(VICMPE); |
| 4125 | OPCODE(VICMPH); |
| 4126 | OPCODE(VICMPHL); |
Ulrich Weigand | cd80823 | 2015-05-05 19:26:48 +0000 | [diff] [blame] | 4127 | OPCODE(VFCMPE); |
| 4128 | OPCODE(VFCMPH); |
| 4129 | OPCODE(VFCMPHE); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 4130 | OPCODE(VEXTEND); |
| 4131 | OPCODE(VROUND); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4132 | OPCODE(ATOMIC_SWAPW); |
| 4133 | OPCODE(ATOMIC_LOADW_ADD); |
| 4134 | OPCODE(ATOMIC_LOADW_SUB); |
| 4135 | OPCODE(ATOMIC_LOADW_AND); |
| 4136 | OPCODE(ATOMIC_LOADW_OR); |
| 4137 | OPCODE(ATOMIC_LOADW_XOR); |
| 4138 | OPCODE(ATOMIC_LOADW_NAND); |
| 4139 | OPCODE(ATOMIC_LOADW_MIN); |
| 4140 | OPCODE(ATOMIC_LOADW_MAX); |
| 4141 | OPCODE(ATOMIC_LOADW_UMIN); |
| 4142 | OPCODE(ATOMIC_LOADW_UMAX); |
| 4143 | OPCODE(ATOMIC_CMP_SWAPW); |
Richard Sandiford | 0348133 | 2013-08-23 11:36:42 +0000 | [diff] [blame] | 4144 | OPCODE(PREFETCH); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4145 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 4146 | return nullptr; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4147 | #undef OPCODE |
| 4148 | } |
| 4149 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4150 | // Return true if VT is a vector whose elements are a whole number of bytes |
| 4151 | // in width. |
| 4152 | static bool canTreatAsByteVector(EVT VT) { |
| 4153 | return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0; |
| 4154 | } |
| 4155 | |
| 4156 | // Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT |
| 4157 | // producing a result of type ResVT. Op is a possibly bitcast version |
| 4158 | // of the input vector and Index is the index (based on type VecVT) that |
| 4159 | // should be extracted. Return the new extraction if a simplification |
| 4160 | // was possible or if Force is true. |
| 4161 | SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT, |
| 4162 | SDValue Op, unsigned Index, |
| 4163 | DAGCombinerInfo &DCI, |
| 4164 | bool Force) const { |
| 4165 | SelectionDAG &DAG = DCI.DAG; |
| 4166 | |
| 4167 | // The number of bytes being extracted. |
| 4168 | unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); |
| 4169 | |
| 4170 | for (;;) { |
| 4171 | unsigned Opcode = Op.getOpcode(); |
| 4172 | if (Opcode == ISD::BITCAST) |
| 4173 | // Look through bitcasts. |
| 4174 | Op = Op.getOperand(0); |
| 4175 | else if (Opcode == ISD::VECTOR_SHUFFLE && |
| 4176 | canTreatAsByteVector(Op.getValueType())) { |
| 4177 | // Get a VPERM-like permute mask and see whether the bytes covered |
| 4178 | // by the extracted element are a contiguous sequence from one |
| 4179 | // source operand. |
| 4180 | SmallVector<int, SystemZ::VectorBytes> Bytes; |
| 4181 | getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes); |
| 4182 | int First; |
| 4183 | if (!getShuffleInput(Bytes, Index * BytesPerElement, |
| 4184 | BytesPerElement, First)) |
| 4185 | break; |
| 4186 | if (First < 0) |
| 4187 | return DAG.getUNDEF(ResVT); |
| 4188 | // Make sure the contiguous sequence starts at a multiple of the |
| 4189 | // original element size. |
| 4190 | unsigned Byte = unsigned(First) % Bytes.size(); |
| 4191 | if (Byte % BytesPerElement != 0) |
| 4192 | break; |
| 4193 | // We can get the extracted value directly from an input. |
| 4194 | Index = Byte / BytesPerElement; |
| 4195 | Op = Op.getOperand(unsigned(First) / Bytes.size()); |
| 4196 | Force = true; |
| 4197 | } else if (Opcode == ISD::BUILD_VECTOR && |
| 4198 | canTreatAsByteVector(Op.getValueType())) { |
| 4199 | // We can only optimize this case if the BUILD_VECTOR elements are |
| 4200 | // at least as wide as the extracted value. |
| 4201 | EVT OpVT = Op.getValueType(); |
| 4202 | unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); |
| 4203 | if (OpBytesPerElement < BytesPerElement) |
| 4204 | break; |
| 4205 | // Make sure that the least-significant bit of the extracted value |
| 4206 | // is the least significant bit of an input. |
| 4207 | unsigned End = (Index + 1) * BytesPerElement; |
| 4208 | if (End % OpBytesPerElement != 0) |
| 4209 | break; |
| 4210 | // We're extracting the low part of one operand of the BUILD_VECTOR. |
| 4211 | Op = Op.getOperand(End / OpBytesPerElement - 1); |
| 4212 | if (!Op.getValueType().isInteger()) { |
| 4213 | EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits()); |
| 4214 | Op = DAG.getNode(ISD::BITCAST, DL, VT, Op); |
| 4215 | DCI.AddToWorklist(Op.getNode()); |
| 4216 | } |
| 4217 | EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits()); |
| 4218 | Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); |
| 4219 | if (VT != ResVT) { |
| 4220 | DCI.AddToWorklist(Op.getNode()); |
| 4221 | Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op); |
| 4222 | } |
| 4223 | return Op; |
| 4224 | } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG || |
| 4225 | Opcode == ISD::ZERO_EXTEND_VECTOR_INREG || |
| 4226 | Opcode == ISD::ANY_EXTEND_VECTOR_INREG) && |
| 4227 | canTreatAsByteVector(Op.getValueType()) && |
| 4228 | canTreatAsByteVector(Op.getOperand(0).getValueType())) { |
| 4229 | // Make sure that only the unextended bits are significant. |
| 4230 | EVT ExtVT = Op.getValueType(); |
| 4231 | EVT OpVT = Op.getOperand(0).getValueType(); |
| 4232 | unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize(); |
| 4233 | unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize(); |
| 4234 | unsigned Byte = Index * BytesPerElement; |
| 4235 | unsigned SubByte = Byte % ExtBytesPerElement; |
| 4236 | unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement; |
| 4237 | if (SubByte < MinSubByte || |
| 4238 | SubByte + BytesPerElement > ExtBytesPerElement) |
| 4239 | break; |
| 4240 | // Get the byte offset of the unextended element |
| 4241 | Byte = Byte / ExtBytesPerElement * OpBytesPerElement; |
| 4242 | // ...then add the byte offset relative to that element. |
| 4243 | Byte += SubByte - MinSubByte; |
| 4244 | if (Byte % BytesPerElement != 0) |
| 4245 | break; |
| 4246 | Op = Op.getOperand(0); |
| 4247 | Index = Byte / BytesPerElement; |
| 4248 | Force = true; |
| 4249 | } else |
| 4250 | break; |
| 4251 | } |
| 4252 | if (Force) { |
| 4253 | if (Op.getValueType() != VecVT) { |
| 4254 | Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op); |
| 4255 | DCI.AddToWorklist(Op.getNode()); |
| 4256 | } |
| 4257 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op, |
| 4258 | DAG.getConstant(Index, DL, MVT::i32)); |
| 4259 | } |
| 4260 | return SDValue(); |
| 4261 | } |
| 4262 | |
| 4263 | // Optimize vector operations in scalar value Op on the basis that Op |
| 4264 | // is truncated to TruncVT. |
| 4265 | SDValue |
| 4266 | SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op, |
| 4267 | DAGCombinerInfo &DCI) const { |
| 4268 | // If we have (trunc (extract_vector_elt X, Y)), try to turn it into |
| 4269 | // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements |
| 4270 | // of type TruncVT. |
| 4271 | if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 4272 | TruncVT.getSizeInBits() % 8 == 0) { |
| 4273 | SDValue Vec = Op.getOperand(0); |
| 4274 | EVT VecVT = Vec.getValueType(); |
| 4275 | if (canTreatAsByteVector(VecVT)) { |
| 4276 | if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { |
| 4277 | unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize(); |
| 4278 | unsigned TruncBytes = TruncVT.getStoreSize(); |
| 4279 | if (BytesPerElement % TruncBytes == 0) { |
| 4280 | // Calculate the value of Y' in the above description. We are |
| 4281 | // splitting the original elements into Scale equal-sized pieces |
| 4282 | // and for truncation purposes want the last (least-significant) |
| 4283 | // of these pieces for IndexN. This is easiest to do by calculating |
| 4284 | // the start index of the following element and then subtracting 1. |
| 4285 | unsigned Scale = BytesPerElement / TruncBytes; |
| 4286 | unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1; |
| 4287 | |
| 4288 | // Defer the creation of the bitcast from X to combineExtract, |
| 4289 | // which might be able to optimize the extraction. |
| 4290 | VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8), |
| 4291 | VecVT.getStoreSize() / TruncBytes); |
| 4292 | EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT); |
| 4293 | return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true); |
| 4294 | } |
| 4295 | } |
| 4296 | } |
| 4297 | } |
| 4298 | return SDValue(); |
| 4299 | } |
| 4300 | |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 4301 | SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N, |
| 4302 | DAGCombinerInfo &DCI) const { |
| 4303 | SelectionDAG &DAG = DCI.DAG; |
| 4304 | unsigned Opcode = N->getOpcode(); |
| 4305 | if (Opcode == ISD::SIGN_EXTEND) { |
| 4306 | // Convert (sext (ashr (shl X, C1), C2)) to |
| 4307 | // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as |
| 4308 | // cheap as narrower ones. |
| 4309 | SDValue N0 = N->getOperand(0); |
| 4310 | EVT VT = N->getValueType(0); |
| 4311 | if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) { |
| 4312 | auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)); |
| 4313 | SDValue Inner = N0.getOperand(0); |
| 4314 | if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) { |
| 4315 | if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) { |
| 4316 | unsigned Extra = (VT.getSizeInBits() - |
| 4317 | N0.getValueType().getSizeInBits()); |
| 4318 | unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra; |
| 4319 | unsigned NewSraAmt = SraAmt->getZExtValue() + Extra; |
| 4320 | EVT ShiftVT = N0.getOperand(1).getValueType(); |
| 4321 | SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT, |
| 4322 | Inner.getOperand(0)); |
| 4323 | SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4324 | DAG.getConstant(NewShlAmt, SDLoc(Inner), |
| 4325 | ShiftVT)); |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 4326 | return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 4327 | DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT)); |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 4328 | } |
| 4329 | } |
| 4330 | } |
| 4331 | } |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 4332 | // (z_merge_high 0, 0) -> 0. This is mostly useful for using VLLEZF |
| 4333 | // for v4f32. |
| 4334 | if (Opcode == SystemZISD::MERGE_HIGH) { |
| 4335 | SDValue Op0 = N->getOperand(0); |
| 4336 | SDValue Op1 = N->getOperand(1); |
| 4337 | if (Op0 == Op1) { |
| 4338 | if (Op0.getOpcode() == ISD::BITCAST) |
| 4339 | Op0 = Op0.getOperand(0); |
| 4340 | if (Op0.getOpcode() == SystemZISD::BYTE_MASK && |
| 4341 | cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) |
| 4342 | return Op1; |
| 4343 | } |
| 4344 | } |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 4345 | // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better |
| 4346 | // for the extraction to be done on a vMiN value, so that we can use VSTE. |
| 4347 | // If X has wider elements then convert it to: |
| 4348 | // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z). |
| 4349 | if (Opcode == ISD::STORE) { |
| 4350 | auto *SN = cast<StoreSDNode>(N); |
| 4351 | EVT MemVT = SN->getMemoryVT(); |
| 4352 | if (MemVT.isInteger()) { |
| 4353 | SDValue Value = combineTruncateExtract(SDLoc(N), MemVT, |
| 4354 | SN->getValue(), DCI); |
| 4355 | if (Value.getNode()) { |
| 4356 | DCI.AddToWorklist(Value.getNode()); |
| 4357 | |
| 4358 | // Rewrite the store with the new form of stored value. |
| 4359 | return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value, |
| 4360 | SN->getBasePtr(), SN->getMemoryVT(), |
| 4361 | SN->getMemOperand()); |
| 4362 | } |
| 4363 | } |
| 4364 | } |
| 4365 | // Try to simplify a vector extraction. |
| 4366 | if (Opcode == ISD::EXTRACT_VECTOR_ELT) { |
| 4367 | if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) { |
| 4368 | SDValue Op0 = N->getOperand(0); |
| 4369 | EVT VecVT = Op0.getValueType(); |
| 4370 | return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0, |
| 4371 | IndexN->getZExtValue(), DCI, false); |
| 4372 | } |
| 4373 | } |
| 4374 | // (join_dwords X, X) == (replicate X) |
| 4375 | if (Opcode == SystemZISD::JOIN_DWORDS && |
| 4376 | N->getOperand(0) == N->getOperand(1)) |
| 4377 | return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0), |
| 4378 | N->getOperand(0)); |
Ulrich Weigand | 80b3af7 | 2015-05-05 19:27:45 +0000 | [diff] [blame^] | 4379 | // (fround (extract_vector_elt X 0)) |
| 4380 | // (fround (extract_vector_elt X 1)) -> |
| 4381 | // (extract_vector_elt (VROUND X) 0) |
| 4382 | // (extract_vector_elt (VROUND X) 1) |
| 4383 | // |
| 4384 | // This is a special case since the target doesn't really support v2f32s. |
| 4385 | if (Opcode == ISD::FP_ROUND) { |
| 4386 | SDValue Op0 = N->getOperand(0); |
| 4387 | if (N->getValueType(0) == MVT::f32 && |
| 4388 | Op0.hasOneUse() && |
| 4389 | Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 4390 | Op0.getOperand(0).getValueType() == MVT::v2f64 && |
| 4391 | Op0.getOperand(1).getOpcode() == ISD::Constant && |
| 4392 | cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) { |
| 4393 | SDValue Vec = Op0.getOperand(0); |
| 4394 | for (auto *U : Vec->uses()) { |
| 4395 | if (U != Op0.getNode() && |
| 4396 | U->hasOneUse() && |
| 4397 | U->getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 4398 | U->getOperand(0) == Vec && |
| 4399 | U->getOperand(1).getOpcode() == ISD::Constant && |
| 4400 | cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) { |
| 4401 | SDValue OtherRound = SDValue(*U->use_begin(), 0); |
| 4402 | if (OtherRound.getOpcode() == ISD::FP_ROUND && |
| 4403 | OtherRound.getOperand(0) == SDValue(U, 0) && |
| 4404 | OtherRound.getValueType() == MVT::f32) { |
| 4405 | SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N), |
| 4406 | MVT::v4f32, Vec); |
| 4407 | DCI.AddToWorklist(VRound.getNode()); |
| 4408 | SDValue Extract1 = |
| 4409 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32, |
| 4410 | VRound, DAG.getConstant(2, SDLoc(U), MVT::i32)); |
| 4411 | DCI.AddToWorklist(Extract1.getNode()); |
| 4412 | DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1); |
| 4413 | SDValue Extract0 = |
| 4414 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32, |
| 4415 | VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32)); |
| 4416 | return Extract0; |
| 4417 | } |
| 4418 | } |
| 4419 | } |
| 4420 | } |
| 4421 | } |
Richard Sandiford | 95bc5f9 | 2014-03-07 11:34:35 +0000 | [diff] [blame] | 4422 | return SDValue(); |
| 4423 | } |
| 4424 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4425 | //===----------------------------------------------------------------------===// |
| 4426 | // Custom insertion |
| 4427 | //===----------------------------------------------------------------------===// |
| 4428 | |
| 4429 | // Create a new basic block after MBB. |
| 4430 | static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) { |
| 4431 | MachineFunction &MF = *MBB->getParent(); |
| 4432 | MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock()); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 4433 | MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4434 | return NewMBB; |
| 4435 | } |
| 4436 | |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 4437 | // Split MBB after MI and return the new block (the one that contains |
| 4438 | // instructions after MI). |
| 4439 | static MachineBasicBlock *splitBlockAfter(MachineInstr *MI, |
| 4440 | MachineBasicBlock *MBB) { |
| 4441 | MachineBasicBlock *NewMBB = emitBlockAfter(MBB); |
| 4442 | NewMBB->splice(NewMBB->begin(), MBB, |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 4443 | std::next(MachineBasicBlock::iterator(MI)), MBB->end()); |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 4444 | NewMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 4445 | return NewMBB; |
| 4446 | } |
| 4447 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4448 | // Split MBB before MI and return the new block (the one that contains MI). |
| 4449 | static MachineBasicBlock *splitBlockBefore(MachineInstr *MI, |
| 4450 | MachineBasicBlock *MBB) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4451 | MachineBasicBlock *NewMBB = emitBlockAfter(MBB); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4452 | NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4453 | NewMBB->transferSuccessorsAndUpdatePHIs(MBB); |
| 4454 | return NewMBB; |
| 4455 | } |
| 4456 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4457 | // Force base value Base into a register before MI. Return the register. |
| 4458 | static unsigned forceReg(MachineInstr *MI, MachineOperand &Base, |
| 4459 | const SystemZInstrInfo *TII) { |
| 4460 | if (Base.isReg()) |
| 4461 | return Base.getReg(); |
| 4462 | |
| 4463 | MachineBasicBlock *MBB = MI->getParent(); |
| 4464 | MachineFunction &MF = *MBB->getParent(); |
| 4465 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 4466 | |
| 4467 | unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); |
| 4468 | BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg) |
| 4469 | .addOperand(Base).addImm(0).addReg(0); |
| 4470 | return Reg; |
| 4471 | } |
| 4472 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4473 | // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI. |
| 4474 | MachineBasicBlock * |
| 4475 | SystemZTargetLowering::emitSelect(MachineInstr *MI, |
| 4476 | MachineBasicBlock *MBB) const { |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4477 | const SystemZInstrInfo *TII = |
| 4478 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4479 | |
| 4480 | unsigned DestReg = MI->getOperand(0).getReg(); |
| 4481 | unsigned TrueReg = MI->getOperand(1).getReg(); |
| 4482 | unsigned FalseReg = MI->getOperand(2).getReg(); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4483 | unsigned CCValid = MI->getOperand(3).getImm(); |
| 4484 | unsigned CCMask = MI->getOperand(4).getImm(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4485 | DebugLoc DL = MI->getDebugLoc(); |
| 4486 | |
| 4487 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4488 | MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4489 | MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); |
| 4490 | |
| 4491 | // StartMBB: |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 4492 | // BRC CCMask, JoinMBB |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4493 | // # fallthrough to FalseMBB |
| 4494 | MBB = StartMBB; |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4495 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 4496 | .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4497 | MBB->addSuccessor(JoinMBB); |
| 4498 | MBB->addSuccessor(FalseMBB); |
| 4499 | |
| 4500 | // FalseMBB: |
| 4501 | // # fallthrough to JoinMBB |
| 4502 | MBB = FalseMBB; |
| 4503 | MBB->addSuccessor(JoinMBB); |
| 4504 | |
| 4505 | // JoinMBB: |
| 4506 | // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ] |
| 4507 | // ... |
| 4508 | MBB = JoinMBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4509 | BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4510 | .addReg(TrueReg).addMBB(StartMBB) |
| 4511 | .addReg(FalseReg).addMBB(FalseMBB); |
| 4512 | |
| 4513 | MI->eraseFromParent(); |
| 4514 | return JoinMBB; |
| 4515 | } |
| 4516 | |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4517 | // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI. |
| 4518 | // 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] | 4519 | // happen when the condition is false rather than true. If a STORE ON |
| 4520 | // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0. |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4521 | MachineBasicBlock * |
| 4522 | SystemZTargetLowering::emitCondStore(MachineInstr *MI, |
| 4523 | MachineBasicBlock *MBB, |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4524 | unsigned StoreOpcode, unsigned STOCOpcode, |
| 4525 | bool Invert) const { |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4526 | const SystemZInstrInfo *TII = |
| 4527 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4528 | |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4529 | unsigned SrcReg = MI->getOperand(0).getReg(); |
| 4530 | MachineOperand Base = MI->getOperand(1); |
| 4531 | int64_t Disp = MI->getOperand(2).getImm(); |
| 4532 | unsigned IndexReg = MI->getOperand(3).getReg(); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4533 | unsigned CCValid = MI->getOperand(4).getImm(); |
| 4534 | unsigned CCMask = MI->getOperand(5).getImm(); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4535 | DebugLoc DL = MI->getDebugLoc(); |
| 4536 | |
| 4537 | StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp); |
| 4538 | |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4539 | // Use STOCOpcode if possible. We could use different store patterns in |
| 4540 | // order to avoid matching the index register, but the performance trade-offs |
| 4541 | // might be more complicated in that case. |
Eric Christopher | 93bf97c | 2014-06-27 07:38:01 +0000 | [diff] [blame] | 4542 | if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) { |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4543 | if (Invert) |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4544 | CCMask ^= CCValid; |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4545 | BuildMI(*MBB, MI, DL, TII->get(STOCOpcode)) |
Richard Sandiford | fd7f4ae | 2013-08-01 10:39:40 +0000 | [diff] [blame] | 4546 | .addReg(SrcReg).addOperand(Base).addImm(Disp) |
| 4547 | .addImm(CCValid).addImm(CCMask); |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 4548 | MI->eraseFromParent(); |
| 4549 | return MBB; |
| 4550 | } |
| 4551 | |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4552 | // Get the condition needed to branch around the store. |
| 4553 | if (!Invert) |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4554 | CCMask ^= CCValid; |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4555 | |
| 4556 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4557 | MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4558 | MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); |
| 4559 | |
| 4560 | // StartMBB: |
| 4561 | // BRC CCMask, JoinMBB |
| 4562 | // # fallthrough to FalseMBB |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4563 | MBB = StartMBB; |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4564 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 4565 | .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 4566 | MBB->addSuccessor(JoinMBB); |
| 4567 | MBB->addSuccessor(FalseMBB); |
| 4568 | |
| 4569 | // FalseMBB: |
| 4570 | // store %SrcReg, %Disp(%Index,%Base) |
| 4571 | // # fallthrough to JoinMBB |
| 4572 | MBB = FalseMBB; |
| 4573 | BuildMI(MBB, DL, TII->get(StoreOpcode)) |
| 4574 | .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg); |
| 4575 | MBB->addSuccessor(JoinMBB); |
| 4576 | |
| 4577 | MI->eraseFromParent(); |
| 4578 | return JoinMBB; |
| 4579 | } |
| 4580 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4581 | // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_* |
| 4582 | // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that |
| 4583 | // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}. |
| 4584 | // BitSize is the width of the field in bits, or 0 if this is a partword |
| 4585 | // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize |
| 4586 | // is one of the operands. Invert says whether the field should be |
| 4587 | // inverted after performing BinOpcode (e.g. for NAND). |
| 4588 | MachineBasicBlock * |
| 4589 | SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI, |
| 4590 | MachineBasicBlock *MBB, |
| 4591 | unsigned BinOpcode, |
| 4592 | unsigned BitSize, |
| 4593 | bool Invert) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4594 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 4595 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4596 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4597 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4598 | bool IsSubWord = (BitSize < 32); |
| 4599 | |
| 4600 | // Extract the operands. Base can be a register or a frame index. |
| 4601 | // Src2 can be a register or immediate. |
| 4602 | unsigned Dest = MI->getOperand(0).getReg(); |
| 4603 | MachineOperand Base = earlyUseOperand(MI->getOperand(1)); |
| 4604 | int64_t Disp = MI->getOperand(2).getImm(); |
| 4605 | MachineOperand Src2 = earlyUseOperand(MI->getOperand(3)); |
| 4606 | unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); |
| 4607 | unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); |
| 4608 | DebugLoc DL = MI->getDebugLoc(); |
| 4609 | if (IsSubWord) |
| 4610 | BitSize = MI->getOperand(6).getImm(); |
| 4611 | |
| 4612 | // Subword operations use 32-bit registers. |
| 4613 | const TargetRegisterClass *RC = (BitSize <= 32 ? |
| 4614 | &SystemZ::GR32BitRegClass : |
| 4615 | &SystemZ::GR64BitRegClass); |
| 4616 | unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; |
| 4617 | unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; |
| 4618 | |
| 4619 | // Get the right opcodes for the displacement. |
| 4620 | LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); |
| 4621 | CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); |
| 4622 | assert(LOpcode && CSOpcode && "Displacement out of range"); |
| 4623 | |
| 4624 | // Create virtual registers for temporary results. |
| 4625 | unsigned OrigVal = MRI.createVirtualRegister(RC); |
| 4626 | unsigned OldVal = MRI.createVirtualRegister(RC); |
| 4627 | unsigned NewVal = (BinOpcode || IsSubWord ? |
| 4628 | MRI.createVirtualRegister(RC) : Src2.getReg()); |
| 4629 | unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); |
| 4630 | unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); |
| 4631 | |
| 4632 | // Insert a basic block for the main loop. |
| 4633 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4634 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4635 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
| 4636 | |
| 4637 | // StartMBB: |
| 4638 | // ... |
| 4639 | // %OrigVal = L Disp(%Base) |
| 4640 | // # fall through to LoopMMB |
| 4641 | MBB = StartMBB; |
| 4642 | BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) |
| 4643 | .addOperand(Base).addImm(Disp).addReg(0); |
| 4644 | MBB->addSuccessor(LoopMBB); |
| 4645 | |
| 4646 | // LoopMBB: |
| 4647 | // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ] |
| 4648 | // %RotatedOldVal = RLL %OldVal, 0(%BitShift) |
| 4649 | // %RotatedNewVal = OP %RotatedOldVal, %Src2 |
| 4650 | // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) |
| 4651 | // %Dest = CS %OldVal, %NewVal, Disp(%Base) |
| 4652 | // JNE LoopMBB |
| 4653 | // # fall through to DoneMMB |
| 4654 | MBB = LoopMBB; |
| 4655 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) |
| 4656 | .addReg(OrigVal).addMBB(StartMBB) |
| 4657 | .addReg(Dest).addMBB(LoopMBB); |
| 4658 | if (IsSubWord) |
| 4659 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) |
| 4660 | .addReg(OldVal).addReg(BitShift).addImm(0); |
| 4661 | if (Invert) { |
| 4662 | // Perform the operation normally and then invert every bit of the field. |
| 4663 | unsigned Tmp = MRI.createVirtualRegister(RC); |
| 4664 | BuildMI(MBB, DL, TII->get(BinOpcode), Tmp) |
| 4665 | .addReg(RotatedOldVal).addOperand(Src2); |
Alexey Samsonov | fffd56ec | 2014-08-20 21:56:43 +0000 | [diff] [blame] | 4666 | if (BitSize <= 32) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4667 | // XILF with the upper BitSize bits set. |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 4668 | BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal) |
Alexey Samsonov | fffd56ec | 2014-08-20 21:56:43 +0000 | [diff] [blame] | 4669 | .addReg(Tmp).addImm(-1U << (32 - BitSize)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4670 | else { |
| 4671 | // Use LCGR and add -1 to the result, which is more compact than |
| 4672 | // an XILF, XILH pair. |
| 4673 | unsigned Tmp2 = MRI.createVirtualRegister(RC); |
| 4674 | BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp); |
| 4675 | BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal) |
| 4676 | .addReg(Tmp2).addImm(-1); |
| 4677 | } |
| 4678 | } else if (BinOpcode) |
| 4679 | // A simply binary operation. |
| 4680 | BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal) |
| 4681 | .addReg(RotatedOldVal).addOperand(Src2); |
| 4682 | else if (IsSubWord) |
| 4683 | // Use RISBG to rotate Src2 into position and use it to replace the |
| 4684 | // field in RotatedOldVal. |
| 4685 | BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal) |
| 4686 | .addReg(RotatedOldVal).addReg(Src2.getReg()) |
| 4687 | .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize); |
| 4688 | if (IsSubWord) |
| 4689 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) |
| 4690 | .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); |
| 4691 | BuildMI(MBB, DL, TII->get(CSOpcode), Dest) |
| 4692 | .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4693 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 4694 | .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4695 | MBB->addSuccessor(LoopMBB); |
| 4696 | MBB->addSuccessor(DoneMBB); |
| 4697 | |
| 4698 | MI->eraseFromParent(); |
| 4699 | return DoneMBB; |
| 4700 | } |
| 4701 | |
| 4702 | // Implement EmitInstrWithCustomInserter for pseudo |
| 4703 | // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the |
| 4704 | // instruction that should be used to compare the current field with the |
| 4705 | // minimum or maximum value. KeepOldMask is the BRC condition-code mask |
| 4706 | // for when the current field should be kept. BitSize is the width of |
| 4707 | // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction. |
| 4708 | MachineBasicBlock * |
| 4709 | SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI, |
| 4710 | MachineBasicBlock *MBB, |
| 4711 | unsigned CompareOpcode, |
| 4712 | unsigned KeepOldMask, |
| 4713 | unsigned BitSize) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4714 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 4715 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4716 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4717 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4718 | bool IsSubWord = (BitSize < 32); |
| 4719 | |
| 4720 | // Extract the operands. Base can be a register or a frame index. |
| 4721 | unsigned Dest = MI->getOperand(0).getReg(); |
| 4722 | MachineOperand Base = earlyUseOperand(MI->getOperand(1)); |
| 4723 | int64_t Disp = MI->getOperand(2).getImm(); |
| 4724 | unsigned Src2 = MI->getOperand(3).getReg(); |
| 4725 | unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); |
| 4726 | unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); |
| 4727 | DebugLoc DL = MI->getDebugLoc(); |
| 4728 | if (IsSubWord) |
| 4729 | BitSize = MI->getOperand(6).getImm(); |
| 4730 | |
| 4731 | // Subword operations use 32-bit registers. |
| 4732 | const TargetRegisterClass *RC = (BitSize <= 32 ? |
| 4733 | &SystemZ::GR32BitRegClass : |
| 4734 | &SystemZ::GR64BitRegClass); |
| 4735 | unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; |
| 4736 | unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; |
| 4737 | |
| 4738 | // Get the right opcodes for the displacement. |
| 4739 | LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); |
| 4740 | CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); |
| 4741 | assert(LOpcode && CSOpcode && "Displacement out of range"); |
| 4742 | |
| 4743 | // Create virtual registers for temporary results. |
| 4744 | unsigned OrigVal = MRI.createVirtualRegister(RC); |
| 4745 | unsigned OldVal = MRI.createVirtualRegister(RC); |
| 4746 | unsigned NewVal = MRI.createVirtualRegister(RC); |
| 4747 | unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); |
| 4748 | unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2); |
| 4749 | unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); |
| 4750 | |
| 4751 | // Insert 3 basic blocks for the loop. |
| 4752 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4753 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4754 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
| 4755 | MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB); |
| 4756 | MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB); |
| 4757 | |
| 4758 | // StartMBB: |
| 4759 | // ... |
| 4760 | // %OrigVal = L Disp(%Base) |
| 4761 | // # fall through to LoopMMB |
| 4762 | MBB = StartMBB; |
| 4763 | BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) |
| 4764 | .addOperand(Base).addImm(Disp).addReg(0); |
| 4765 | MBB->addSuccessor(LoopMBB); |
| 4766 | |
| 4767 | // LoopMBB: |
| 4768 | // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ] |
| 4769 | // %RotatedOldVal = RLL %OldVal, 0(%BitShift) |
| 4770 | // CompareOpcode %RotatedOldVal, %Src2 |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 4771 | // BRC KeepOldMask, UpdateMBB |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4772 | MBB = LoopMBB; |
| 4773 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) |
| 4774 | .addReg(OrigVal).addMBB(StartMBB) |
| 4775 | .addReg(Dest).addMBB(UpdateMBB); |
| 4776 | if (IsSubWord) |
| 4777 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) |
| 4778 | .addReg(OldVal).addReg(BitShift).addImm(0); |
Richard Sandiford | 8a757bb | 2013-07-31 12:11:07 +0000 | [diff] [blame] | 4779 | BuildMI(MBB, DL, TII->get(CompareOpcode)) |
| 4780 | .addReg(RotatedOldVal).addReg(Src2); |
| 4781 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4782 | .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4783 | MBB->addSuccessor(UpdateMBB); |
| 4784 | MBB->addSuccessor(UseAltMBB); |
| 4785 | |
| 4786 | // UseAltMBB: |
| 4787 | // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0 |
| 4788 | // # fall through to UpdateMMB |
| 4789 | MBB = UseAltMBB; |
| 4790 | if (IsSubWord) |
| 4791 | BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal) |
| 4792 | .addReg(RotatedOldVal).addReg(Src2) |
| 4793 | .addImm(32).addImm(31 + BitSize).addImm(0); |
| 4794 | MBB->addSuccessor(UpdateMBB); |
| 4795 | |
| 4796 | // UpdateMBB: |
| 4797 | // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ], |
| 4798 | // [ %RotatedAltVal, UseAltMBB ] |
| 4799 | // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) |
| 4800 | // %Dest = CS %OldVal, %NewVal, Disp(%Base) |
| 4801 | // JNE LoopMBB |
| 4802 | // # fall through to DoneMMB |
| 4803 | MBB = UpdateMBB; |
| 4804 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal) |
| 4805 | .addReg(RotatedOldVal).addMBB(LoopMBB) |
| 4806 | .addReg(RotatedAltVal).addMBB(UseAltMBB); |
| 4807 | if (IsSubWord) |
| 4808 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) |
| 4809 | .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); |
| 4810 | BuildMI(MBB, DL, TII->get(CSOpcode), Dest) |
| 4811 | .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4812 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 4813 | .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4814 | MBB->addSuccessor(LoopMBB); |
| 4815 | MBB->addSuccessor(DoneMBB); |
| 4816 | |
| 4817 | MI->eraseFromParent(); |
| 4818 | return DoneMBB; |
| 4819 | } |
| 4820 | |
| 4821 | // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW |
| 4822 | // instruction MI. |
| 4823 | MachineBasicBlock * |
| 4824 | SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI, |
| 4825 | MachineBasicBlock *MBB) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4826 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 4827 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4828 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4829 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4830 | |
| 4831 | // Extract the operands. Base can be a register or a frame index. |
| 4832 | unsigned Dest = MI->getOperand(0).getReg(); |
| 4833 | MachineOperand Base = earlyUseOperand(MI->getOperand(1)); |
| 4834 | int64_t Disp = MI->getOperand(2).getImm(); |
| 4835 | unsigned OrigCmpVal = MI->getOperand(3).getReg(); |
| 4836 | unsigned OrigSwapVal = MI->getOperand(4).getReg(); |
| 4837 | unsigned BitShift = MI->getOperand(5).getReg(); |
| 4838 | unsigned NegBitShift = MI->getOperand(6).getReg(); |
| 4839 | int64_t BitSize = MI->getOperand(7).getImm(); |
| 4840 | DebugLoc DL = MI->getDebugLoc(); |
| 4841 | |
| 4842 | const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass; |
| 4843 | |
| 4844 | // Get the right opcodes for the displacement. |
| 4845 | unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp); |
| 4846 | unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp); |
| 4847 | assert(LOpcode && CSOpcode && "Displacement out of range"); |
| 4848 | |
| 4849 | // Create virtual registers for temporary results. |
| 4850 | unsigned OrigOldVal = MRI.createVirtualRegister(RC); |
| 4851 | unsigned OldVal = MRI.createVirtualRegister(RC); |
| 4852 | unsigned CmpVal = MRI.createVirtualRegister(RC); |
| 4853 | unsigned SwapVal = MRI.createVirtualRegister(RC); |
| 4854 | unsigned StoreVal = MRI.createVirtualRegister(RC); |
| 4855 | unsigned RetryOldVal = MRI.createVirtualRegister(RC); |
| 4856 | unsigned RetryCmpVal = MRI.createVirtualRegister(RC); |
| 4857 | unsigned RetrySwapVal = MRI.createVirtualRegister(RC); |
| 4858 | |
| 4859 | // Insert 2 basic blocks for the loop. |
| 4860 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4861 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4862 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
| 4863 | MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB); |
| 4864 | |
| 4865 | // StartMBB: |
| 4866 | // ... |
| 4867 | // %OrigOldVal = L Disp(%Base) |
| 4868 | // # fall through to LoopMMB |
| 4869 | MBB = StartMBB; |
| 4870 | BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal) |
| 4871 | .addOperand(Base).addImm(Disp).addReg(0); |
| 4872 | MBB->addSuccessor(LoopMBB); |
| 4873 | |
| 4874 | // LoopMBB: |
| 4875 | // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ] |
| 4876 | // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ] |
| 4877 | // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ] |
| 4878 | // %Dest = RLL %OldVal, BitSize(%BitShift) |
| 4879 | // ^^ The low BitSize bits contain the field |
| 4880 | // of interest. |
| 4881 | // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0 |
| 4882 | // ^^ Replace the upper 32-BitSize bits of the |
| 4883 | // comparison value with those that we loaded, |
| 4884 | // so that we can use a full word comparison. |
Richard Sandiford | 8a757bb | 2013-07-31 12:11:07 +0000 | [diff] [blame] | 4885 | // CR %Dest, %RetryCmpVal |
| 4886 | // JNE DoneMBB |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4887 | // # Fall through to SetMBB |
| 4888 | MBB = LoopMBB; |
| 4889 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) |
| 4890 | .addReg(OrigOldVal).addMBB(StartMBB) |
| 4891 | .addReg(RetryOldVal).addMBB(SetMBB); |
| 4892 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal) |
| 4893 | .addReg(OrigCmpVal).addMBB(StartMBB) |
| 4894 | .addReg(RetryCmpVal).addMBB(SetMBB); |
| 4895 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal) |
| 4896 | .addReg(OrigSwapVal).addMBB(StartMBB) |
| 4897 | .addReg(RetrySwapVal).addMBB(SetMBB); |
| 4898 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest) |
| 4899 | .addReg(OldVal).addReg(BitShift).addImm(BitSize); |
| 4900 | BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal) |
| 4901 | .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); |
Richard Sandiford | 8a757bb | 2013-07-31 12:11:07 +0000 | [diff] [blame] | 4902 | BuildMI(MBB, DL, TII->get(SystemZ::CR)) |
| 4903 | .addReg(Dest).addReg(RetryCmpVal); |
| 4904 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4905 | .addImm(SystemZ::CCMASK_ICMP) |
| 4906 | .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4907 | MBB->addSuccessor(DoneMBB); |
| 4908 | MBB->addSuccessor(SetMBB); |
| 4909 | |
| 4910 | // SetMBB: |
| 4911 | // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0 |
| 4912 | // ^^ Replace the upper 32-BitSize bits of the new |
| 4913 | // value with those that we loaded. |
| 4914 | // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift) |
| 4915 | // ^^ Rotate the new field to its proper position. |
| 4916 | // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base) |
| 4917 | // JNE LoopMBB |
| 4918 | // # fall through to ExitMMB |
| 4919 | MBB = SetMBB; |
| 4920 | BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal) |
| 4921 | .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); |
| 4922 | BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal) |
| 4923 | .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize); |
| 4924 | BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal) |
| 4925 | .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 4926 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 4927 | .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4928 | MBB->addSuccessor(LoopMBB); |
| 4929 | MBB->addSuccessor(DoneMBB); |
| 4930 | |
| 4931 | MI->eraseFromParent(); |
| 4932 | return DoneMBB; |
| 4933 | } |
| 4934 | |
| 4935 | // Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true |
| 4936 | // 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] | 4937 | // it's "don't care". SubReg is subreg_l32 when extending a GR32 |
| 4938 | // and subreg_l64 when extending a GR64. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4939 | MachineBasicBlock * |
| 4940 | SystemZTargetLowering::emitExt128(MachineInstr *MI, |
| 4941 | MachineBasicBlock *MBB, |
| 4942 | bool ClearEven, unsigned SubReg) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4943 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 4944 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4945 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4946 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 4947 | DebugLoc DL = MI->getDebugLoc(); |
| 4948 | |
| 4949 | unsigned Dest = MI->getOperand(0).getReg(); |
| 4950 | unsigned Src = MI->getOperand(1).getReg(); |
| 4951 | unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); |
| 4952 | |
| 4953 | BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128); |
| 4954 | if (ClearEven) { |
| 4955 | unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); |
| 4956 | unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); |
| 4957 | |
| 4958 | BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64) |
| 4959 | .addImm(0); |
| 4960 | BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128) |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 4961 | .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 4962 | In128 = NewIn128; |
| 4963 | } |
| 4964 | BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest) |
| 4965 | .addReg(In128).addReg(Src).addImm(SubReg); |
| 4966 | |
| 4967 | MI->eraseFromParent(); |
| 4968 | return MBB; |
| 4969 | } |
| 4970 | |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 4971 | MachineBasicBlock * |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 4972 | SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI, |
| 4973 | MachineBasicBlock *MBB, |
| 4974 | unsigned Opcode) const { |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4975 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 4976 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 4977 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4978 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 4979 | DebugLoc DL = MI->getDebugLoc(); |
| 4980 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4981 | MachineOperand DestBase = earlyUseOperand(MI->getOperand(0)); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 4982 | uint64_t DestDisp = MI->getOperand(1).getImm(); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4983 | MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2)); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 4984 | uint64_t SrcDisp = MI->getOperand(3).getImm(); |
| 4985 | uint64_t Length = MI->getOperand(4).getImm(); |
| 4986 | |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 4987 | // When generating more than one CLC, all but the last will need to |
| 4988 | // branch to the end when a difference is found. |
| 4989 | MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ? |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 4990 | splitBlockAfter(MI, MBB) : nullptr); |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 4991 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 4992 | // Check for the loop form, in which operand 5 is the trip count. |
| 4993 | if (MI->getNumExplicitOperands() > 5) { |
| 4994 | bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase); |
| 4995 | |
| 4996 | uint64_t StartCountReg = MI->getOperand(5).getReg(); |
| 4997 | uint64_t StartSrcReg = forceReg(MI, SrcBase, TII); |
| 4998 | uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg : |
| 4999 | forceReg(MI, DestBase, TII)); |
| 5000 | |
| 5001 | const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass; |
| 5002 | uint64_t ThisSrcReg = MRI.createVirtualRegister(RC); |
| 5003 | uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg : |
| 5004 | MRI.createVirtualRegister(RC)); |
| 5005 | uint64_t NextSrcReg = MRI.createVirtualRegister(RC); |
| 5006 | uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg : |
| 5007 | MRI.createVirtualRegister(RC)); |
| 5008 | |
| 5009 | RC = &SystemZ::GR64BitRegClass; |
| 5010 | uint64_t ThisCountReg = MRI.createVirtualRegister(RC); |
| 5011 | uint64_t NextCountReg = MRI.createVirtualRegister(RC); |
| 5012 | |
| 5013 | MachineBasicBlock *StartMBB = MBB; |
| 5014 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
| 5015 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5016 | MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5017 | |
| 5018 | // StartMBB: |
| 5019 | // # fall through to LoopMMB |
| 5020 | MBB->addSuccessor(LoopMBB); |
| 5021 | |
| 5022 | // LoopMBB: |
| 5023 | // %ThisDestReg = phi [ %StartDestReg, StartMBB ], |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5024 | // [ %NextDestReg, NextMBB ] |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5025 | // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ], |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5026 | // [ %NextSrcReg, NextMBB ] |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5027 | // %ThisCountReg = phi [ %StartCountReg, StartMBB ], |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5028 | // [ %NextCountReg, NextMBB ] |
| 5029 | // ( PFD 2, 768+DestDisp(%ThisDestReg) ) |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5030 | // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg) |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5031 | // ( JLH EndMBB ) |
| 5032 | // |
| 5033 | // The prefetch is used only for MVC. The JLH is used only for CLC. |
| 5034 | MBB = LoopMBB; |
| 5035 | |
| 5036 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg) |
| 5037 | .addReg(StartDestReg).addMBB(StartMBB) |
| 5038 | .addReg(NextDestReg).addMBB(NextMBB); |
| 5039 | if (!HaveSingleBase) |
| 5040 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg) |
| 5041 | .addReg(StartSrcReg).addMBB(StartMBB) |
| 5042 | .addReg(NextSrcReg).addMBB(NextMBB); |
| 5043 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg) |
| 5044 | .addReg(StartCountReg).addMBB(StartMBB) |
| 5045 | .addReg(NextCountReg).addMBB(NextMBB); |
| 5046 | if (Opcode == SystemZ::MVC) |
| 5047 | BuildMI(MBB, DL, TII->get(SystemZ::PFD)) |
| 5048 | .addImm(SystemZ::PFD_WRITE) |
| 5049 | .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0); |
| 5050 | BuildMI(MBB, DL, TII->get(Opcode)) |
| 5051 | .addReg(ThisDestReg).addImm(DestDisp).addImm(256) |
| 5052 | .addReg(ThisSrcReg).addImm(SrcDisp); |
| 5053 | if (EndMBB) { |
| 5054 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5055 | .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) |
| 5056 | .addMBB(EndMBB); |
| 5057 | MBB->addSuccessor(EndMBB); |
| 5058 | MBB->addSuccessor(NextMBB); |
| 5059 | } |
| 5060 | |
| 5061 | // NextMBB: |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5062 | // %NextDestReg = LA 256(%ThisDestReg) |
| 5063 | // %NextSrcReg = LA 256(%ThisSrcReg) |
| 5064 | // %NextCountReg = AGHI %ThisCountReg, -1 |
| 5065 | // CGHI %NextCountReg, 0 |
| 5066 | // JLH LoopMBB |
| 5067 | // # fall through to DoneMMB |
| 5068 | // |
| 5069 | // 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] | 5070 | MBB = NextMBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5071 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5072 | BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg) |
| 5073 | .addReg(ThisDestReg).addImm(256).addReg(0); |
| 5074 | if (!HaveSingleBase) |
| 5075 | BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg) |
| 5076 | .addReg(ThisSrcReg).addImm(256).addReg(0); |
| 5077 | BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg) |
| 5078 | .addReg(ThisCountReg).addImm(-1); |
| 5079 | BuildMI(MBB, DL, TII->get(SystemZ::CGHI)) |
| 5080 | .addReg(NextCountReg).addImm(0); |
| 5081 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5082 | .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) |
| 5083 | .addMBB(LoopMBB); |
| 5084 | MBB->addSuccessor(LoopMBB); |
| 5085 | MBB->addSuccessor(DoneMBB); |
| 5086 | |
| 5087 | DestBase = MachineOperand::CreateReg(NextDestReg, false); |
| 5088 | SrcBase = MachineOperand::CreateReg(NextSrcReg, false); |
| 5089 | Length &= 255; |
| 5090 | MBB = DoneMBB; |
| 5091 | } |
| 5092 | // Handle any remaining bytes with straight-line code. |
| 5093 | while (Length > 0) { |
| 5094 | uint64_t ThisLength = std::min(Length, uint64_t(256)); |
| 5095 | // The previous iteration might have created out-of-range displacements. |
| 5096 | // Apply them using LAY if so. |
| 5097 | if (!isUInt<12>(DestDisp)) { |
| 5098 | unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); |
| 5099 | BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) |
| 5100 | .addOperand(DestBase).addImm(DestDisp).addReg(0); |
| 5101 | DestBase = MachineOperand::CreateReg(Reg, false); |
| 5102 | DestDisp = 0; |
| 5103 | } |
| 5104 | if (!isUInt<12>(SrcDisp)) { |
| 5105 | unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); |
| 5106 | BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) |
| 5107 | .addOperand(SrcBase).addImm(SrcDisp).addReg(0); |
| 5108 | SrcBase = MachineOperand::CreateReg(Reg, false); |
| 5109 | SrcDisp = 0; |
| 5110 | } |
| 5111 | BuildMI(*MBB, MI, DL, TII->get(Opcode)) |
| 5112 | .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength) |
| 5113 | .addOperand(SrcBase).addImm(SrcDisp); |
| 5114 | DestDisp += ThisLength; |
| 5115 | SrcDisp += ThisLength; |
| 5116 | Length -= ThisLength; |
Richard Sandiford | be133a8 | 2013-08-28 09:01:51 +0000 | [diff] [blame] | 5117 | // If there's another CLC to go, branch to the end if a difference |
| 5118 | // was found. |
| 5119 | if (EndMBB && Length > 0) { |
| 5120 | MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB); |
| 5121 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5122 | .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) |
| 5123 | .addMBB(EndMBB); |
| 5124 | MBB->addSuccessor(EndMBB); |
| 5125 | MBB->addSuccessor(NextMBB); |
| 5126 | MBB = NextMBB; |
| 5127 | } |
| 5128 | } |
| 5129 | if (EndMBB) { |
| 5130 | MBB->addSuccessor(EndMBB); |
| 5131 | MBB = EndMBB; |
| 5132 | MBB->addLiveIn(SystemZ::CC); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5133 | } |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 5134 | |
| 5135 | MI->eraseFromParent(); |
| 5136 | return MBB; |
| 5137 | } |
| 5138 | |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5139 | // Decompose string pseudo-instruction MI into a loop that continually performs |
| 5140 | // Opcode until CC != 3. |
| 5141 | MachineBasicBlock * |
| 5142 | SystemZTargetLowering::emitStringWrapper(MachineInstr *MI, |
| 5143 | MachineBasicBlock *MBB, |
| 5144 | unsigned Opcode) const { |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5145 | MachineFunction &MF = *MBB->getParent(); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 5146 | const SystemZInstrInfo *TII = |
Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 5147 | static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo()); |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5148 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 5149 | DebugLoc DL = MI->getDebugLoc(); |
| 5150 | |
| 5151 | uint64_t End1Reg = MI->getOperand(0).getReg(); |
| 5152 | uint64_t Start1Reg = MI->getOperand(1).getReg(); |
| 5153 | uint64_t Start2Reg = MI->getOperand(2).getReg(); |
| 5154 | uint64_t CharReg = MI->getOperand(3).getReg(); |
| 5155 | |
| 5156 | const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass; |
| 5157 | uint64_t This1Reg = MRI.createVirtualRegister(RC); |
| 5158 | uint64_t This2Reg = MRI.createVirtualRegister(RC); |
| 5159 | uint64_t End2Reg = MRI.createVirtualRegister(RC); |
| 5160 | |
| 5161 | MachineBasicBlock *StartMBB = MBB; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5162 | MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5163 | MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); |
| 5164 | |
| 5165 | // StartMBB: |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5166 | // # fall through to LoopMMB |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5167 | MBB->addSuccessor(LoopMBB); |
| 5168 | |
| 5169 | // LoopMBB: |
| 5170 | // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ] |
| 5171 | // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ] |
Richard Sandiford | 7789b08 | 2013-09-30 08:48:38 +0000 | [diff] [blame] | 5172 | // R0L = %CharReg |
| 5173 | // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5174 | // JO LoopMBB |
| 5175 | // # fall through to DoneMMB |
Richard Sandiford | 6f6d551 | 2013-08-20 09:38:48 +0000 | [diff] [blame] | 5176 | // |
Richard Sandiford | 7789b08 | 2013-09-30 08:48:38 +0000 | [diff] [blame] | 5177 | // The load of R0L can be hoisted by post-RA LICM. |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5178 | MBB = LoopMBB; |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5179 | |
| 5180 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg) |
| 5181 | .addReg(Start1Reg).addMBB(StartMBB) |
| 5182 | .addReg(End1Reg).addMBB(LoopMBB); |
| 5183 | BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg) |
| 5184 | .addReg(Start2Reg).addMBB(StartMBB) |
| 5185 | .addReg(End2Reg).addMBB(LoopMBB); |
Richard Sandiford | 7789b08 | 2013-09-30 08:48:38 +0000 | [diff] [blame] | 5186 | BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg); |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5187 | BuildMI(MBB, DL, TII->get(Opcode)) |
| 5188 | .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define) |
| 5189 | .addReg(This1Reg).addReg(This2Reg); |
| 5190 | BuildMI(MBB, DL, TII->get(SystemZ::BRC)) |
| 5191 | .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB); |
| 5192 | MBB->addSuccessor(LoopMBB); |
| 5193 | MBB->addSuccessor(DoneMBB); |
| 5194 | |
| 5195 | DoneMBB->addLiveIn(SystemZ::CC); |
| 5196 | |
| 5197 | MI->eraseFromParent(); |
| 5198 | return DoneMBB; |
| 5199 | } |
| 5200 | |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 5201 | // Update TBEGIN instruction with final opcode and register clobbers. |
| 5202 | MachineBasicBlock * |
| 5203 | SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI, |
| 5204 | MachineBasicBlock *MBB, |
| 5205 | unsigned Opcode, |
| 5206 | bool NoFloat) const { |
| 5207 | MachineFunction &MF = *MBB->getParent(); |
| 5208 | const TargetFrameLowering *TFI = Subtarget.getFrameLowering(); |
| 5209 | const SystemZInstrInfo *TII = Subtarget.getInstrInfo(); |
| 5210 | |
| 5211 | // Update opcode. |
| 5212 | MI->setDesc(TII->get(Opcode)); |
| 5213 | |
| 5214 | // We cannot handle a TBEGIN that clobbers the stack or frame pointer. |
| 5215 | // Make sure to add the corresponding GRSM bits if they are missing. |
| 5216 | uint64_t Control = MI->getOperand(2).getImm(); |
| 5217 | static const unsigned GPRControlBit[16] = { |
| 5218 | 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000, |
| 5219 | 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100 |
| 5220 | }; |
| 5221 | Control |= GPRControlBit[15]; |
| 5222 | if (TFI->hasFP(MF)) |
| 5223 | Control |= GPRControlBit[11]; |
| 5224 | MI->getOperand(2).setImm(Control); |
| 5225 | |
| 5226 | // Add GPR clobbers. |
| 5227 | for (int I = 0; I < 16; I++) { |
| 5228 | if ((Control & GPRControlBit[I]) == 0) { |
| 5229 | unsigned Reg = SystemZMC::GR64Regs[I]; |
| 5230 | MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); |
| 5231 | } |
| 5232 | } |
| 5233 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 5234 | // Add FPR/VR clobbers. |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 5235 | if (!NoFloat && (Control & 4) != 0) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 5236 | if (Subtarget.hasVector()) { |
| 5237 | for (int I = 0; I < 32; I++) { |
| 5238 | unsigned Reg = SystemZMC::VR128Regs[I]; |
| 5239 | MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); |
| 5240 | } |
| 5241 | } else { |
| 5242 | for (int I = 0; I < 16; I++) { |
| 5243 | unsigned Reg = SystemZMC::FP64Regs[I]; |
| 5244 | MI->addOperand(MachineOperand::CreateReg(Reg, true, true)); |
| 5245 | } |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 5246 | } |
| 5247 | } |
| 5248 | |
| 5249 | return MBB; |
| 5250 | } |
| 5251 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5252 | MachineBasicBlock *SystemZTargetLowering:: |
| 5253 | EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const { |
| 5254 | switch (MI->getOpcode()) { |
Richard Sandiford | 7c5c0ea | 2013-10-01 13:10:16 +0000 | [diff] [blame] | 5255 | case SystemZ::Select32Mux: |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5256 | case SystemZ::Select32: |
| 5257 | case SystemZ::SelectF32: |
| 5258 | case SystemZ::Select64: |
| 5259 | case SystemZ::SelectF64: |
| 5260 | case SystemZ::SelectF128: |
| 5261 | return emitSelect(MI, MBB); |
| 5262 | |
Richard Sandiford | 2896d04 | 2013-10-01 14:33:55 +0000 | [diff] [blame] | 5263 | case SystemZ::CondStore8Mux: |
| 5264 | return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false); |
| 5265 | case SystemZ::CondStore8MuxInv: |
| 5266 | return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true); |
| 5267 | case SystemZ::CondStore16Mux: |
| 5268 | return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false); |
| 5269 | case SystemZ::CondStore16MuxInv: |
| 5270 | return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5271 | case SystemZ::CondStore8: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5272 | return emitCondStore(MI, MBB, SystemZ::STC, 0, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5273 | case SystemZ::CondStore8Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5274 | return emitCondStore(MI, MBB, SystemZ::STC, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5275 | case SystemZ::CondStore16: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5276 | return emitCondStore(MI, MBB, SystemZ::STH, 0, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5277 | case SystemZ::CondStore16Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5278 | return emitCondStore(MI, MBB, SystemZ::STH, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5279 | case SystemZ::CondStore32: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5280 | return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5281 | case SystemZ::CondStore32Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5282 | return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5283 | case SystemZ::CondStore64: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5284 | return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5285 | case SystemZ::CondStore64Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5286 | return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5287 | case SystemZ::CondStoreF32: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5288 | return emitCondStore(MI, MBB, SystemZ::STE, 0, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5289 | case SystemZ::CondStoreF32Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5290 | return emitCondStore(MI, MBB, SystemZ::STE, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5291 | case SystemZ::CondStoreF64: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5292 | return emitCondStore(MI, MBB, SystemZ::STD, 0, false); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5293 | case SystemZ::CondStoreF64Inv: |
Richard Sandiford | a68e6f5 | 2013-07-25 08:57:02 +0000 | [diff] [blame] | 5294 | return emitCondStore(MI, MBB, SystemZ::STD, 0, true); |
Richard Sandiford | b86a834 | 2013-06-27 09:27:40 +0000 | [diff] [blame] | 5295 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5296 | case SystemZ::AEXT128_64: |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 5297 | return emitExt128(MI, MBB, false, SystemZ::subreg_l64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5298 | case SystemZ::ZEXT128_32: |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 5299 | return emitExt128(MI, MBB, true, SystemZ::subreg_l32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5300 | case SystemZ::ZEXT128_64: |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 5301 | return emitExt128(MI, MBB, true, SystemZ::subreg_l64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5302 | |
| 5303 | case SystemZ::ATOMIC_SWAPW: |
| 5304 | return emitAtomicLoadBinary(MI, MBB, 0, 0); |
| 5305 | case SystemZ::ATOMIC_SWAP_32: |
| 5306 | return emitAtomicLoadBinary(MI, MBB, 0, 32); |
| 5307 | case SystemZ::ATOMIC_SWAP_64: |
| 5308 | return emitAtomicLoadBinary(MI, MBB, 0, 64); |
| 5309 | |
| 5310 | case SystemZ::ATOMIC_LOADW_AR: |
| 5311 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0); |
| 5312 | case SystemZ::ATOMIC_LOADW_AFI: |
| 5313 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0); |
| 5314 | case SystemZ::ATOMIC_LOAD_AR: |
| 5315 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32); |
| 5316 | case SystemZ::ATOMIC_LOAD_AHI: |
| 5317 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32); |
| 5318 | case SystemZ::ATOMIC_LOAD_AFI: |
| 5319 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32); |
| 5320 | case SystemZ::ATOMIC_LOAD_AGR: |
| 5321 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64); |
| 5322 | case SystemZ::ATOMIC_LOAD_AGHI: |
| 5323 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64); |
| 5324 | case SystemZ::ATOMIC_LOAD_AGFI: |
| 5325 | return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64); |
| 5326 | |
| 5327 | case SystemZ::ATOMIC_LOADW_SR: |
| 5328 | return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0); |
| 5329 | case SystemZ::ATOMIC_LOAD_SR: |
| 5330 | return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32); |
| 5331 | case SystemZ::ATOMIC_LOAD_SGR: |
| 5332 | return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64); |
| 5333 | |
| 5334 | case SystemZ::ATOMIC_LOADW_NR: |
| 5335 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0); |
| 5336 | case SystemZ::ATOMIC_LOADW_NILH: |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5337 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5338 | case SystemZ::ATOMIC_LOAD_NR: |
| 5339 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5340 | case SystemZ::ATOMIC_LOAD_NILL: |
| 5341 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32); |
| 5342 | case SystemZ::ATOMIC_LOAD_NILH: |
| 5343 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32); |
| 5344 | case SystemZ::ATOMIC_LOAD_NILF: |
| 5345 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5346 | case SystemZ::ATOMIC_LOAD_NGR: |
| 5347 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5348 | case SystemZ::ATOMIC_LOAD_NILL64: |
| 5349 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64); |
| 5350 | case SystemZ::ATOMIC_LOAD_NILH64: |
| 5351 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 5352 | case SystemZ::ATOMIC_LOAD_NIHL64: |
| 5353 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64); |
| 5354 | case SystemZ::ATOMIC_LOAD_NIHH64: |
| 5355 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5356 | case SystemZ::ATOMIC_LOAD_NILF64: |
| 5357 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 5358 | case SystemZ::ATOMIC_LOAD_NIHF64: |
| 5359 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5360 | |
| 5361 | case SystemZ::ATOMIC_LOADW_OR: |
| 5362 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0); |
| 5363 | case SystemZ::ATOMIC_LOADW_OILH: |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5364 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5365 | case SystemZ::ATOMIC_LOAD_OR: |
| 5366 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5367 | case SystemZ::ATOMIC_LOAD_OILL: |
| 5368 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32); |
| 5369 | case SystemZ::ATOMIC_LOAD_OILH: |
| 5370 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32); |
| 5371 | case SystemZ::ATOMIC_LOAD_OILF: |
| 5372 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5373 | case SystemZ::ATOMIC_LOAD_OGR: |
| 5374 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5375 | case SystemZ::ATOMIC_LOAD_OILL64: |
| 5376 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64); |
| 5377 | case SystemZ::ATOMIC_LOAD_OILH64: |
| 5378 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64); |
Richard Sandiford | 6e96ac6 | 2013-10-01 13:22:41 +0000 | [diff] [blame] | 5379 | case SystemZ::ATOMIC_LOAD_OIHL64: |
| 5380 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64); |
| 5381 | case SystemZ::ATOMIC_LOAD_OIHH64: |
| 5382 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5383 | case SystemZ::ATOMIC_LOAD_OILF64: |
| 5384 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64); |
Richard Sandiford | 6e96ac6 | 2013-10-01 13:22:41 +0000 | [diff] [blame] | 5385 | case SystemZ::ATOMIC_LOAD_OIHF64: |
| 5386 | return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5387 | |
| 5388 | case SystemZ::ATOMIC_LOADW_XR: |
| 5389 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0); |
| 5390 | case SystemZ::ATOMIC_LOADW_XILF: |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5391 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5392 | case SystemZ::ATOMIC_LOAD_XR: |
| 5393 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5394 | case SystemZ::ATOMIC_LOAD_XILF: |
| 5395 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5396 | case SystemZ::ATOMIC_LOAD_XGR: |
| 5397 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5398 | case SystemZ::ATOMIC_LOAD_XILF64: |
| 5399 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64); |
Richard Sandiford | 5718dac | 2013-10-01 14:08:44 +0000 | [diff] [blame] | 5400 | case SystemZ::ATOMIC_LOAD_XIHF64: |
| 5401 | return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5402 | |
| 5403 | case SystemZ::ATOMIC_LOADW_NRi: |
| 5404 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true); |
| 5405 | case SystemZ::ATOMIC_LOADW_NILHi: |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5406 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5407 | case SystemZ::ATOMIC_LOAD_NRi: |
| 5408 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5409 | case SystemZ::ATOMIC_LOAD_NILLi: |
| 5410 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true); |
| 5411 | case SystemZ::ATOMIC_LOAD_NILHi: |
| 5412 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true); |
| 5413 | case SystemZ::ATOMIC_LOAD_NILFi: |
| 5414 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5415 | case SystemZ::ATOMIC_LOAD_NGRi: |
| 5416 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5417 | case SystemZ::ATOMIC_LOAD_NILL64i: |
| 5418 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true); |
| 5419 | case SystemZ::ATOMIC_LOAD_NILH64i: |
| 5420 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 5421 | case SystemZ::ATOMIC_LOAD_NIHL64i: |
| 5422 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true); |
| 5423 | case SystemZ::ATOMIC_LOAD_NIHH64i: |
| 5424 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 5425 | case SystemZ::ATOMIC_LOAD_NILF64i: |
| 5426 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 5427 | case SystemZ::ATOMIC_LOAD_NIHF64i: |
| 5428 | return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5429 | |
| 5430 | case SystemZ::ATOMIC_LOADW_MIN: |
| 5431 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, |
| 5432 | SystemZ::CCMASK_CMP_LE, 0); |
| 5433 | case SystemZ::ATOMIC_LOAD_MIN_32: |
| 5434 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, |
| 5435 | SystemZ::CCMASK_CMP_LE, 32); |
| 5436 | case SystemZ::ATOMIC_LOAD_MIN_64: |
| 5437 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, |
| 5438 | SystemZ::CCMASK_CMP_LE, 64); |
| 5439 | |
| 5440 | case SystemZ::ATOMIC_LOADW_MAX: |
| 5441 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, |
| 5442 | SystemZ::CCMASK_CMP_GE, 0); |
| 5443 | case SystemZ::ATOMIC_LOAD_MAX_32: |
| 5444 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, |
| 5445 | SystemZ::CCMASK_CMP_GE, 32); |
| 5446 | case SystemZ::ATOMIC_LOAD_MAX_64: |
| 5447 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, |
| 5448 | SystemZ::CCMASK_CMP_GE, 64); |
| 5449 | |
| 5450 | case SystemZ::ATOMIC_LOADW_UMIN: |
| 5451 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, |
| 5452 | SystemZ::CCMASK_CMP_LE, 0); |
| 5453 | case SystemZ::ATOMIC_LOAD_UMIN_32: |
| 5454 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, |
| 5455 | SystemZ::CCMASK_CMP_LE, 32); |
| 5456 | case SystemZ::ATOMIC_LOAD_UMIN_64: |
| 5457 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, |
| 5458 | SystemZ::CCMASK_CMP_LE, 64); |
| 5459 | |
| 5460 | case SystemZ::ATOMIC_LOADW_UMAX: |
| 5461 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, |
| 5462 | SystemZ::CCMASK_CMP_GE, 0); |
| 5463 | case SystemZ::ATOMIC_LOAD_UMAX_32: |
| 5464 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, |
| 5465 | SystemZ::CCMASK_CMP_GE, 32); |
| 5466 | case SystemZ::ATOMIC_LOAD_UMAX_64: |
| 5467 | return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, |
| 5468 | SystemZ::CCMASK_CMP_GE, 64); |
| 5469 | |
| 5470 | case SystemZ::ATOMIC_CMP_SWAPW: |
| 5471 | return emitAtomicCmpSwapW(MI, MBB); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5472 | case SystemZ::MVCSequence: |
| 5473 | case SystemZ::MVCLoop: |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5474 | return emitMemMemWrapper(MI, MBB, SystemZ::MVC); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 5475 | case SystemZ::NCSequence: |
| 5476 | case SystemZ::NCLoop: |
| 5477 | return emitMemMemWrapper(MI, MBB, SystemZ::NC); |
| 5478 | case SystemZ::OCSequence: |
| 5479 | case SystemZ::OCLoop: |
| 5480 | return emitMemMemWrapper(MI, MBB, SystemZ::OC); |
| 5481 | case SystemZ::XCSequence: |
| 5482 | case SystemZ::XCLoop: |
| 5483 | return emitMemMemWrapper(MI, MBB, SystemZ::XC); |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 5484 | case SystemZ::CLCSequence: |
| 5485 | case SystemZ::CLCLoop: |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 5486 | return emitMemMemWrapper(MI, MBB, SystemZ::CLC); |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 5487 | case SystemZ::CLSTLoop: |
| 5488 | return emitStringWrapper(MI, MBB, SystemZ::CLST); |
Richard Sandiford | bb83a50 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 5489 | case SystemZ::MVSTLoop: |
| 5490 | return emitStringWrapper(MI, MBB, SystemZ::MVST); |
Richard Sandiford | 0dec06a | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 5491 | case SystemZ::SRSTLoop: |
| 5492 | return emitStringWrapper(MI, MBB, SystemZ::SRST); |
Ulrich Weigand | 57c85f5 | 2015-04-01 12:51:43 +0000 | [diff] [blame] | 5493 | case SystemZ::TBEGIN: |
| 5494 | return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false); |
| 5495 | case SystemZ::TBEGIN_nofloat: |
| 5496 | return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true); |
| 5497 | case SystemZ::TBEGINC: |
| 5498 | return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 5499 | default: |
| 5500 | llvm_unreachable("Unexpected instr type to insert"); |
| 5501 | } |
| 5502 | } |