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 14 #define DEBUG_TYPE "systemz-lower" 15 16 #include "SystemZISelLowering.h" 17 #include "SystemZCallingConv.h" 18 #include "SystemZConstantPoolValue.h" 19 #include "SystemZMachineFunctionInfo.h" 20 #include "SystemZTargetMachine.h" 21 #include "llvm/CodeGen/CallingConvLower.h" 22 #include "llvm/CodeGen/MachineInstrBuilder.h" 23 #include "llvm/CodeGen/MachineRegisterInfo.h" 24 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 25 #include <cctype> 26 27 using namespace llvm; 28 29 namespace { 30 // Represents a sequence for extracting a 0/1 value from an IPM result: 31 // (((X ^ XORValue) + AddValue) >> Bit) 32 struct IPMConversion { 33 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit) 34 : XORValue(xorValue), AddValue(addValue), Bit(bit) {} 35 36 int64_t XORValue; 37 int64_t AddValue; 38 unsigned Bit; 39 }; 40 41 // Represents information about a comparison. 42 struct Comparison { 43 Comparison(SDValue Op0In, SDValue Op1In) 44 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {} 45 46 // The operands to the comparison. 47 SDValue Op0, Op1; 48 49 // The opcode that should be used to compare Op0 and Op1. 50 unsigned Opcode; 51 52 // A SystemZICMP value. Only used for integer comparisons. 53 unsigned ICmpType; 54 55 // The mask of CC values that Opcode can produce. 56 unsigned CCValid; 57 58 // The mask of CC values for which the original condition is true. 59 unsigned CCMask; 60 }; 61 } 62 63 // Classify VT as either 32 or 64 bit. 64 static bool is32Bit(EVT VT) { 65 switch (VT.getSimpleVT().SimpleTy) { 66 case MVT::i32: 67 return true; 68 case MVT::i64: 69 return false; 70 default: 71 llvm_unreachable("Unsupported type"); 72 } 73 } 74 75 // Return a version of MachineOperand that can be safely used before the 76 // final use. 77 static MachineOperand earlyUseOperand(MachineOperand Op) { 78 if (Op.isReg()) 79 Op.setIsKill(false); 80 return Op; 81 } 82 83 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm) 84 : TargetLowering(tm, new TargetLoweringObjectFileELF()), 85 Subtarget(*tm.getSubtargetImpl()), TM(tm) { 86 MVT PtrVT = getPointerTy(); 87 88 // Set up the register classes. 89 if (Subtarget.hasHighWord()) 90 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass); 91 else 92 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass); 93 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass); 94 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass); 95 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass); 96 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass); 97 98 // Compute derived properties from the register classes 99 computeRegisterProperties(); 100 101 // Set up special registers. 102 setExceptionPointerRegister(SystemZ::R6D); 103 setExceptionSelectorRegister(SystemZ::R7D); 104 setStackPointerRegisterToSaveRestore(SystemZ::R15D); 105 106 // TODO: It may be better to default to latency-oriented scheduling, however 107 // LLVM's current latency-oriented scheduler can't handle physreg definitions 108 // such as SystemZ has with CC, so set this to the register-pressure 109 // scheduler, because it can. 110 setSchedulingPreference(Sched::RegPressure); 111 112 setBooleanContents(ZeroOrOneBooleanContent); 113 setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct? 114 115 // Instructions are strings of 2-byte aligned 2-byte values. 116 setMinFunctionAlignment(2); 117 118 // Handle operations that are handled in a similar way for all types. 119 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; 120 I <= MVT::LAST_FP_VALUETYPE; 121 ++I) { 122 MVT VT = MVT::SimpleValueType(I); 123 if (isTypeLegal(VT)) { 124 // Lower SET_CC into an IPM-based sequence. 125 setOperationAction(ISD::SETCC, VT, Custom); 126 127 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE). 128 setOperationAction(ISD::SELECT, VT, Expand); 129 130 // Lower SELECT_CC and BR_CC into separate comparisons and branches. 131 setOperationAction(ISD::SELECT_CC, VT, Custom); 132 setOperationAction(ISD::BR_CC, VT, Custom); 133 } 134 } 135 136 // Expand jump table branches as address arithmetic followed by an 137 // indirect jump. 138 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 139 140 // Expand BRCOND into a BR_CC (see above). 141 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 142 143 // Handle integer types. 144 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE; 145 I <= MVT::LAST_INTEGER_VALUETYPE; 146 ++I) { 147 MVT VT = MVT::SimpleValueType(I); 148 if (isTypeLegal(VT)) { 149 // Expand individual DIV and REMs into DIVREMs. 150 setOperationAction(ISD::SDIV, VT, Expand); 151 setOperationAction(ISD::UDIV, VT, Expand); 152 setOperationAction(ISD::SREM, VT, Expand); 153 setOperationAction(ISD::UREM, VT, Expand); 154 setOperationAction(ISD::SDIVREM, VT, Custom); 155 setOperationAction(ISD::UDIVREM, VT, Custom); 156 157 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and 158 // stores, putting a serialization instruction after the stores. 159 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom); 160 setOperationAction(ISD::ATOMIC_STORE, VT, Custom); 161 162 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are 163 // available, or if the operand is constant. 164 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom); 165 166 // No special instructions for these. 167 setOperationAction(ISD::CTPOP, VT, Expand); 168 setOperationAction(ISD::CTTZ, VT, Expand); 169 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand); 170 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand); 171 setOperationAction(ISD::ROTR, VT, Expand); 172 173 // Use *MUL_LOHI where possible instead of MULH*. 174 setOperationAction(ISD::MULHS, VT, Expand); 175 setOperationAction(ISD::MULHU, VT, Expand); 176 setOperationAction(ISD::SMUL_LOHI, VT, Custom); 177 setOperationAction(ISD::UMUL_LOHI, VT, Custom); 178 179 // We have instructions for signed but not unsigned FP conversion. 180 setOperationAction(ISD::FP_TO_UINT, VT, Expand); 181 } 182 } 183 184 // Type legalization will convert 8- and 16-bit atomic operations into 185 // forms that operate on i32s (but still keeping the original memory VT). 186 // Lower them into full i32 operations. 187 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom); 188 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom); 189 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom); 190 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom); 191 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom); 192 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom); 193 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom); 194 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom); 195 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom); 196 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom); 197 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom); 198 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom); 199 200 // We have instructions for signed but not unsigned FP conversion. 201 // Handle unsigned 32-bit types as signed 64-bit types. 202 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote); 203 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand); 204 205 // We have native support for a 64-bit CTLZ, via FLOGR. 206 setOperationAction(ISD::CTLZ, MVT::i32, Promote); 207 setOperationAction(ISD::CTLZ, MVT::i64, Legal); 208 209 // Give LowerOperation the chance to replace 64-bit ORs with subregs. 210 setOperationAction(ISD::OR, MVT::i64, Custom); 211 212 // Give LowerOperation the chance to optimize SIGN_EXTEND sequences. 213 setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom); 214 215 // FIXME: Can we support these natively? 216 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand); 217 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand); 218 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand); 219 220 // We have native instructions for i8, i16 and i32 extensions, but not i1. 221 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 222 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote); 223 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote); 224 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); 225 226 // Handle the various types of symbolic address. 227 setOperationAction(ISD::ConstantPool, PtrVT, Custom); 228 setOperationAction(ISD::GlobalAddress, PtrVT, Custom); 229 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom); 230 setOperationAction(ISD::BlockAddress, PtrVT, Custom); 231 setOperationAction(ISD::JumpTable, PtrVT, Custom); 232 233 // We need to handle dynamic allocations specially because of the 234 // 160-byte area at the bottom of the stack. 235 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom); 236 237 // Use custom expanders so that we can force the function to use 238 // a frame pointer. 239 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom); 240 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom); 241 242 // Handle prefetches with PFD or PFDRL. 243 setOperationAction(ISD::PREFETCH, MVT::Other, Custom); 244 245 // Handle floating-point types. 246 for (unsigned I = MVT::FIRST_FP_VALUETYPE; 247 I <= MVT::LAST_FP_VALUETYPE; 248 ++I) { 249 MVT VT = MVT::SimpleValueType(I); 250 if (isTypeLegal(VT)) { 251 // We can use FI for FRINT. 252 setOperationAction(ISD::FRINT, VT, Legal); 253 254 // We can use the extended form of FI for other rounding operations. 255 if (Subtarget.hasFPExtension()) { 256 setOperationAction(ISD::FNEARBYINT, VT, Legal); 257 setOperationAction(ISD::FFLOOR, VT, Legal); 258 setOperationAction(ISD::FCEIL, VT, Legal); 259 setOperationAction(ISD::FTRUNC, VT, Legal); 260 setOperationAction(ISD::FROUND, VT, Legal); 261 } 262 263 // No special instructions for these. 264 setOperationAction(ISD::FSIN, VT, Expand); 265 setOperationAction(ISD::FCOS, VT, Expand); 266 setOperationAction(ISD::FREM, VT, Expand); 267 } 268 } 269 270 // We have fused multiply-addition for f32 and f64 but not f128. 271 setOperationAction(ISD::FMA, MVT::f32, Legal); 272 setOperationAction(ISD::FMA, MVT::f64, Legal); 273 setOperationAction(ISD::FMA, MVT::f128, Expand); 274 275 // Needed so that we don't try to implement f128 constant loads using 276 // a load-and-extend of a f80 constant (in cases where the constant 277 // would fit in an f80). 278 setLoadExtAction(ISD::EXTLOAD, MVT::f80, Expand); 279 280 // Floating-point truncation and stores need to be done separately. 281 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 282 setTruncStoreAction(MVT::f128, MVT::f32, Expand); 283 setTruncStoreAction(MVT::f128, MVT::f64, Expand); 284 285 // We have 64-bit FPR<->GPR moves, but need special handling for 286 // 32-bit forms. 287 setOperationAction(ISD::BITCAST, MVT::i32, Custom); 288 setOperationAction(ISD::BITCAST, MVT::f32, Custom); 289 290 // VASTART and VACOPY need to deal with the SystemZ-specific varargs 291 // structure, but VAEND is a no-op. 292 setOperationAction(ISD::VASTART, MVT::Other, Custom); 293 setOperationAction(ISD::VACOPY, MVT::Other, Custom); 294 setOperationAction(ISD::VAEND, MVT::Other, Expand); 295 296 // We want to use MVC in preference to even a single load/store pair. 297 MaxStoresPerMemcpy = 0; 298 MaxStoresPerMemcpyOptSize = 0; 299 300 // The main memset sequence is a byte store followed by an MVC. 301 // Two STC or MV..I stores win over that, but the kind of fused stores 302 // generated by target-independent code don't when the byte value is 303 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better 304 // than "STC;MVC". Handle the choice in target-specific code instead. 305 MaxStoresPerMemset = 0; 306 MaxStoresPerMemsetOptSize = 0; 307 } 308 309 EVT SystemZTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const { 310 if (!VT.isVector()) 311 return MVT::i32; 312 return VT.changeVectorElementTypeToInteger(); 313 } 314 315 bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const { 316 VT = VT.getScalarType(); 317 318 if (!VT.isSimple()) 319 return false; 320 321 switch (VT.getSimpleVT().SimpleTy) { 322 case MVT::f32: 323 case MVT::f64: 324 return true; 325 case MVT::f128: 326 return false; 327 default: 328 break; 329 } 330 331 return false; 332 } 333 334 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const { 335 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR. 336 return Imm.isZero() || Imm.isNegZero(); 337 } 338 339 bool SystemZTargetLowering::allowsUnalignedMemoryAccesses(EVT VT, 340 unsigned, 341 bool *Fast) const { 342 // Unaligned accesses should never be slower than the expanded version. 343 // We check specifically for aligned accesses in the few cases where 344 // they are required. 345 if (Fast) 346 *Fast = true; 347 return true; 348 } 349 350 bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM, 351 Type *Ty) const { 352 // Punt on globals for now, although they can be used in limited 353 // RELATIVE LONG cases. 354 if (AM.BaseGV) 355 return false; 356 357 // Require a 20-bit signed offset. 358 if (!isInt<20>(AM.BaseOffs)) 359 return false; 360 361 // Indexing is OK but no scale factor can be applied. 362 return AM.Scale == 0 || AM.Scale == 1; 363 } 364 365 bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const { 366 if (!FromType->isIntegerTy() || !ToType->isIntegerTy()) 367 return false; 368 unsigned FromBits = FromType->getPrimitiveSizeInBits(); 369 unsigned ToBits = ToType->getPrimitiveSizeInBits(); 370 return FromBits > ToBits; 371 } 372 373 bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const { 374 if (!FromVT.isInteger() || !ToVT.isInteger()) 375 return false; 376 unsigned FromBits = FromVT.getSizeInBits(); 377 unsigned ToBits = ToVT.getSizeInBits(); 378 return FromBits > ToBits; 379 } 380 381 //===----------------------------------------------------------------------===// 382 // Inline asm support 383 //===----------------------------------------------------------------------===// 384 385 TargetLowering::ConstraintType 386 SystemZTargetLowering::getConstraintType(const std::string &Constraint) const { 387 if (Constraint.size() == 1) { 388 switch (Constraint[0]) { 389 case 'a': // Address register 390 case 'd': // Data register (equivalent to 'r') 391 case 'f': // Floating-point register 392 case 'h': // High-part register 393 case 'r': // General-purpose register 394 return C_RegisterClass; 395 396 case 'Q': // Memory with base and unsigned 12-bit displacement 397 case 'R': // Likewise, plus an index 398 case 'S': // Memory with base and signed 20-bit displacement 399 case 'T': // Likewise, plus an index 400 case 'm': // Equivalent to 'T'. 401 return C_Memory; 402 403 case 'I': // Unsigned 8-bit constant 404 case 'J': // Unsigned 12-bit constant 405 case 'K': // Signed 16-bit constant 406 case 'L': // Signed 20-bit displacement (on all targets we support) 407 case 'M': // 0x7fffffff 408 return C_Other; 409 410 default: 411 break; 412 } 413 } 414 return TargetLowering::getConstraintType(Constraint); 415 } 416 417 TargetLowering::ConstraintWeight SystemZTargetLowering:: 418 getSingleConstraintMatchWeight(AsmOperandInfo &info, 419 const char *constraint) const { 420 ConstraintWeight weight = CW_Invalid; 421 Value *CallOperandVal = info.CallOperandVal; 422 // If we don't have a value, we can't do a match, 423 // but allow it at the lowest weight. 424 if (CallOperandVal == NULL) 425 return CW_Default; 426 Type *type = CallOperandVal->getType(); 427 // Look at the constraint type. 428 switch (*constraint) { 429 default: 430 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); 431 break; 432 433 case 'a': // Address register 434 case 'd': // Data register (equivalent to 'r') 435 case 'h': // High-part register 436 case 'r': // General-purpose register 437 if (CallOperandVal->getType()->isIntegerTy()) 438 weight = CW_Register; 439 break; 440 441 case 'f': // Floating-point register 442 if (type->isFloatingPointTy()) 443 weight = CW_Register; 444 break; 445 446 case 'I': // Unsigned 8-bit constant 447 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) 448 if (isUInt<8>(C->getZExtValue())) 449 weight = CW_Constant; 450 break; 451 452 case 'J': // Unsigned 12-bit constant 453 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) 454 if (isUInt<12>(C->getZExtValue())) 455 weight = CW_Constant; 456 break; 457 458 case 'K': // Signed 16-bit constant 459 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) 460 if (isInt<16>(C->getSExtValue())) 461 weight = CW_Constant; 462 break; 463 464 case 'L': // Signed 20-bit displacement (on all targets we support) 465 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) 466 if (isInt<20>(C->getSExtValue())) 467 weight = CW_Constant; 468 break; 469 470 case 'M': // 0x7fffffff 471 if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) 472 if (C->getZExtValue() == 0x7fffffff) 473 weight = CW_Constant; 474 break; 475 } 476 return weight; 477 } 478 479 // Parse a "{tNNN}" register constraint for which the register type "t" 480 // has already been verified. MC is the class associated with "t" and 481 // Map maps 0-based register numbers to LLVM register numbers. 482 static std::pair<unsigned, const TargetRegisterClass *> 483 parseRegisterNumber(const std::string &Constraint, 484 const TargetRegisterClass *RC, const unsigned *Map) { 485 assert(*(Constraint.end()-1) == '}' && "Missing '}'"); 486 if (isdigit(Constraint[2])) { 487 std::string Suffix(Constraint.data() + 2, Constraint.size() - 2); 488 unsigned Index = atoi(Suffix.c_str()); 489 if (Index < 16 && Map[Index]) 490 return std::make_pair(Map[Index], RC); 491 } 492 return std::make_pair(0u, static_cast<TargetRegisterClass*>(0)); 493 } 494 495 std::pair<unsigned, const TargetRegisterClass *> SystemZTargetLowering:: 496 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const { 497 if (Constraint.size() == 1) { 498 // GCC Constraint Letters 499 switch (Constraint[0]) { 500 default: break; 501 case 'd': // Data register (equivalent to 'r') 502 case 'r': // General-purpose register 503 if (VT == MVT::i64) 504 return std::make_pair(0U, &SystemZ::GR64BitRegClass); 505 else if (VT == MVT::i128) 506 return std::make_pair(0U, &SystemZ::GR128BitRegClass); 507 return std::make_pair(0U, &SystemZ::GR32BitRegClass); 508 509 case 'a': // Address register 510 if (VT == MVT::i64) 511 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass); 512 else if (VT == MVT::i128) 513 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass); 514 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass); 515 516 case 'h': // High-part register (an LLVM extension) 517 return std::make_pair(0U, &SystemZ::GRH32BitRegClass); 518 519 case 'f': // Floating-point register 520 if (VT == MVT::f64) 521 return std::make_pair(0U, &SystemZ::FP64BitRegClass); 522 else if (VT == MVT::f128) 523 return std::make_pair(0U, &SystemZ::FP128BitRegClass); 524 return std::make_pair(0U, &SystemZ::FP32BitRegClass); 525 } 526 } 527 if (Constraint[0] == '{') { 528 // We need to override the default register parsing for GPRs and FPRs 529 // because the interpretation depends on VT. The internal names of 530 // the registers are also different from the external names 531 // (F0D and F0S instead of F0, etc.). 532 if (Constraint[1] == 'r') { 533 if (VT == MVT::i32) 534 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass, 535 SystemZMC::GR32Regs); 536 if (VT == MVT::i128) 537 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass, 538 SystemZMC::GR128Regs); 539 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass, 540 SystemZMC::GR64Regs); 541 } 542 if (Constraint[1] == 'f') { 543 if (VT == MVT::f32) 544 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass, 545 SystemZMC::FP32Regs); 546 if (VT == MVT::f128) 547 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass, 548 SystemZMC::FP128Regs); 549 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass, 550 SystemZMC::FP64Regs); 551 } 552 } 553 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 554 } 555 556 void SystemZTargetLowering:: 557 LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint, 558 std::vector<SDValue> &Ops, 559 SelectionDAG &DAG) const { 560 // Only support length 1 constraints for now. 561 if (Constraint.length() == 1) { 562 switch (Constraint[0]) { 563 case 'I': // Unsigned 8-bit constant 564 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) 565 if (isUInt<8>(C->getZExtValue())) 566 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), 567 Op.getValueType())); 568 return; 569 570 case 'J': // Unsigned 12-bit constant 571 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) 572 if (isUInt<12>(C->getZExtValue())) 573 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), 574 Op.getValueType())); 575 return; 576 577 case 'K': // Signed 16-bit constant 578 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) 579 if (isInt<16>(C->getSExtValue())) 580 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), 581 Op.getValueType())); 582 return; 583 584 case 'L': // Signed 20-bit displacement (on all targets we support) 585 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) 586 if (isInt<20>(C->getSExtValue())) 587 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), 588 Op.getValueType())); 589 return; 590 591 case 'M': // 0x7fffffff 592 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) 593 if (C->getZExtValue() == 0x7fffffff) 594 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), 595 Op.getValueType())); 596 return; 597 } 598 } 599 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); 600 } 601 602 //===----------------------------------------------------------------------===// 603 // Calling conventions 604 //===----------------------------------------------------------------------===// 605 606 #include "SystemZGenCallingConv.inc" 607 608 bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType, 609 Type *ToType) const { 610 return isTruncateFree(FromType, ToType); 611 } 612 613 bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const { 614 if (!CI->isTailCall()) 615 return false; 616 return true; 617 } 618 619 // Value is a value that has been passed to us in the location described by VA 620 // (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining 621 // any loads onto Chain. 622 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL, 623 CCValAssign &VA, SDValue Chain, 624 SDValue Value) { 625 // If the argument has been promoted from a smaller type, insert an 626 // assertion to capture this. 627 if (VA.getLocInfo() == CCValAssign::SExt) 628 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value, 629 DAG.getValueType(VA.getValVT())); 630 else if (VA.getLocInfo() == CCValAssign::ZExt) 631 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value, 632 DAG.getValueType(VA.getValVT())); 633 634 if (VA.isExtInLoc()) 635 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value); 636 else if (VA.getLocInfo() == CCValAssign::Indirect) 637 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value, 638 MachinePointerInfo(), false, false, false, 0); 639 else 640 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo"); 641 return Value; 642 } 643 644 // Value is a value of type VA.getValVT() that we need to copy into 645 // the location described by VA. Return a copy of Value converted to 646 // VA.getValVT(). The caller is responsible for handling indirect values. 647 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL, 648 CCValAssign &VA, SDValue Value) { 649 switch (VA.getLocInfo()) { 650 case CCValAssign::SExt: 651 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value); 652 case CCValAssign::ZExt: 653 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value); 654 case CCValAssign::AExt: 655 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value); 656 case CCValAssign::Full: 657 return Value; 658 default: 659 llvm_unreachable("Unhandled getLocInfo()"); 660 } 661 } 662 663 SDValue SystemZTargetLowering:: 664 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 665 const SmallVectorImpl<ISD::InputArg> &Ins, 666 SDLoc DL, SelectionDAG &DAG, 667 SmallVectorImpl<SDValue> &InVals) const { 668 MachineFunction &MF = DAG.getMachineFunction(); 669 MachineFrameInfo *MFI = MF.getFrameInfo(); 670 MachineRegisterInfo &MRI = MF.getRegInfo(); 671 SystemZMachineFunctionInfo *FuncInfo = 672 MF.getInfo<SystemZMachineFunctionInfo>(); 673 const SystemZFrameLowering *TFL = 674 static_cast<const SystemZFrameLowering *>(TM.getFrameLowering()); 675 676 // Assign locations to all of the incoming arguments. 677 SmallVector<CCValAssign, 16> ArgLocs; 678 CCState CCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext()); 679 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ); 680 681 unsigned NumFixedGPRs = 0; 682 unsigned NumFixedFPRs = 0; 683 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 684 SDValue ArgValue; 685 CCValAssign &VA = ArgLocs[I]; 686 EVT LocVT = VA.getLocVT(); 687 if (VA.isRegLoc()) { 688 // Arguments passed in registers 689 const TargetRegisterClass *RC; 690 switch (LocVT.getSimpleVT().SimpleTy) { 691 default: 692 // Integers smaller than i64 should be promoted to i64. 693 llvm_unreachable("Unexpected argument type"); 694 case MVT::i32: 695 NumFixedGPRs += 1; 696 RC = &SystemZ::GR32BitRegClass; 697 break; 698 case MVT::i64: 699 NumFixedGPRs += 1; 700 RC = &SystemZ::GR64BitRegClass; 701 break; 702 case MVT::f32: 703 NumFixedFPRs += 1; 704 RC = &SystemZ::FP32BitRegClass; 705 break; 706 case MVT::f64: 707 NumFixedFPRs += 1; 708 RC = &SystemZ::FP64BitRegClass; 709 break; 710 } 711 712 unsigned VReg = MRI.createVirtualRegister(RC); 713 MRI.addLiveIn(VA.getLocReg(), VReg); 714 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT); 715 } else { 716 assert(VA.isMemLoc() && "Argument not register or memory"); 717 718 // Create the frame index object for this incoming parameter. 719 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8, 720 VA.getLocMemOffset(), true); 721 722 // Create the SelectionDAG nodes corresponding to a load 723 // from this parameter. Unpromoted ints and floats are 724 // passed as right-justified 8-byte values. 725 EVT PtrVT = getPointerTy(); 726 SDValue FIN = DAG.getFrameIndex(FI, PtrVT); 727 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32) 728 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4)); 729 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN, 730 MachinePointerInfo::getFixedStack(FI), 731 false, false, false, 0); 732 } 733 734 // Convert the value of the argument register into the value that's 735 // being passed. 736 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue)); 737 } 738 739 if (IsVarArg) { 740 // Save the number of non-varargs registers for later use by va_start, etc. 741 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs); 742 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs); 743 744 // Likewise the address (in the form of a frame index) of where the 745 // first stack vararg would be. The 1-byte size here is arbitrary. 746 int64_t StackSize = CCInfo.getNextStackOffset(); 747 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true)); 748 749 // ...and a similar frame index for the caller-allocated save area 750 // that will be used to store the incoming registers. 751 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea(); 752 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true); 753 FuncInfo->setRegSaveFrameIndex(RegSaveIndex); 754 755 // Store the FPR varargs in the reserved frame slots. (We store the 756 // GPRs as part of the prologue.) 757 if (NumFixedFPRs < SystemZ::NumArgFPRs) { 758 SDValue MemOps[SystemZ::NumArgFPRs]; 759 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) { 760 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]); 761 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true); 762 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy()); 763 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I], 764 &SystemZ::FP64BitRegClass); 765 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64); 766 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN, 767 MachinePointerInfo::getFixedStack(FI), 768 false, false, 0); 769 770 } 771 // Join the stores, which are independent of one another. 772 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, 773 &MemOps[NumFixedFPRs], 774 SystemZ::NumArgFPRs - NumFixedFPRs); 775 } 776 } 777 778 return Chain; 779 } 780 781 static bool canUseSiblingCall(CCState ArgCCInfo, 782 SmallVectorImpl<CCValAssign> &ArgLocs) { 783 // Punt if there are any indirect or stack arguments, or if the call 784 // needs the call-saved argument register R6. 785 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 786 CCValAssign &VA = ArgLocs[I]; 787 if (VA.getLocInfo() == CCValAssign::Indirect) 788 return false; 789 if (!VA.isRegLoc()) 790 return false; 791 unsigned Reg = VA.getLocReg(); 792 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D) 793 return false; 794 } 795 return true; 796 } 797 798 SDValue 799 SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI, 800 SmallVectorImpl<SDValue> &InVals) const { 801 SelectionDAG &DAG = CLI.DAG; 802 SDLoc &DL = CLI.DL; 803 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; 804 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; 805 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; 806 SDValue Chain = CLI.Chain; 807 SDValue Callee = CLI.Callee; 808 bool &IsTailCall = CLI.IsTailCall; 809 CallingConv::ID CallConv = CLI.CallConv; 810 bool IsVarArg = CLI.IsVarArg; 811 MachineFunction &MF = DAG.getMachineFunction(); 812 EVT PtrVT = getPointerTy(); 813 814 // Analyze the operands of the call, assigning locations to each operand. 815 SmallVector<CCValAssign, 16> ArgLocs; 816 CCState ArgCCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext()); 817 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ); 818 819 // We don't support GuaranteedTailCallOpt, only automatically-detected 820 // sibling calls. 821 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs)) 822 IsTailCall = false; 823 824 // Get a count of how many bytes are to be pushed on the stack. 825 unsigned NumBytes = ArgCCInfo.getNextStackOffset(); 826 827 // Mark the start of the call. 828 if (!IsTailCall) 829 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true), 830 DL); 831 832 // Copy argument values to their designated locations. 833 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass; 834 SmallVector<SDValue, 8> MemOpChains; 835 SDValue StackPtr; 836 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) { 837 CCValAssign &VA = ArgLocs[I]; 838 SDValue ArgValue = OutVals[I]; 839 840 if (VA.getLocInfo() == CCValAssign::Indirect) { 841 // Store the argument in a stack slot and pass its address. 842 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT()); 843 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); 844 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot, 845 MachinePointerInfo::getFixedStack(FI), 846 false, false, 0)); 847 ArgValue = SpillSlot; 848 } else 849 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue); 850 851 if (VA.isRegLoc()) 852 // Queue up the argument copies and emit them at the end. 853 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue)); 854 else { 855 assert(VA.isMemLoc() && "Argument not register or memory"); 856 857 // Work out the address of the stack slot. Unpromoted ints and 858 // floats are passed as right-justified 8-byte values. 859 if (!StackPtr.getNode()) 860 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT); 861 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset(); 862 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32) 863 Offset += 4; 864 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr, 865 DAG.getIntPtrConstant(Offset)); 866 867 // Emit the store. 868 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address, 869 MachinePointerInfo(), 870 false, false, 0)); 871 } 872 } 873 874 // Join the stores, which are independent of one another. 875 if (!MemOpChains.empty()) 876 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, 877 &MemOpChains[0], MemOpChains.size()); 878 879 // Accept direct calls by converting symbolic call addresses to the 880 // associated Target* opcodes. Force %r1 to be used for indirect 881 // tail calls. 882 SDValue Glue; 883 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 884 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT); 885 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee); 886 } else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) { 887 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT); 888 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee); 889 } else if (IsTailCall) { 890 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue); 891 Glue = Chain.getValue(1); 892 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType()); 893 } 894 895 // Build a sequence of copy-to-reg nodes, chained and glued together. 896 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) { 897 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first, 898 RegsToPass[I].second, Glue); 899 Glue = Chain.getValue(1); 900 } 901 902 // The first call operand is the chain and the second is the target address. 903 SmallVector<SDValue, 8> Ops; 904 Ops.push_back(Chain); 905 Ops.push_back(Callee); 906 907 // Add argument registers to the end of the list so that they are 908 // known live into the call. 909 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) 910 Ops.push_back(DAG.getRegister(RegsToPass[I].first, 911 RegsToPass[I].second.getValueType())); 912 913 // Glue the call to the argument copies, if any. 914 if (Glue.getNode()) 915 Ops.push_back(Glue); 916 917 // Emit the call. 918 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 919 if (IsTailCall) 920 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, &Ops[0], Ops.size()); 921 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, &Ops[0], Ops.size()); 922 Glue = Chain.getValue(1); 923 924 // Mark the end of the call, which is glued to the call itself. 925 Chain = DAG.getCALLSEQ_END(Chain, 926 DAG.getConstant(NumBytes, PtrVT, true), 927 DAG.getConstant(0, PtrVT, true), 928 Glue, DL); 929 Glue = Chain.getValue(1); 930 931 // Assign locations to each value returned by this call. 932 SmallVector<CCValAssign, 16> RetLocs; 933 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext()); 934 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ); 935 936 // Copy all of the result registers out of their specified physreg. 937 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) { 938 CCValAssign &VA = RetLocs[I]; 939 940 // Copy the value out, gluing the copy to the end of the call sequence. 941 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(), 942 VA.getLocVT(), Glue); 943 Chain = RetValue.getValue(1); 944 Glue = RetValue.getValue(2); 945 946 // Convert the value of the return register into the value that's 947 // being returned. 948 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue)); 949 } 950 951 return Chain; 952 } 953 954 SDValue 955 SystemZTargetLowering::LowerReturn(SDValue Chain, 956 CallingConv::ID CallConv, bool IsVarArg, 957 const SmallVectorImpl<ISD::OutputArg> &Outs, 958 const SmallVectorImpl<SDValue> &OutVals, 959 SDLoc DL, SelectionDAG &DAG) const { 960 MachineFunction &MF = DAG.getMachineFunction(); 961 962 // Assign locations to each returned value. 963 SmallVector<CCValAssign, 16> RetLocs; 964 CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext()); 965 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ); 966 967 // Quick exit for void returns 968 if (RetLocs.empty()) 969 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain); 970 971 // Copy the result values into the output registers. 972 SDValue Glue; 973 SmallVector<SDValue, 4> RetOps; 974 RetOps.push_back(Chain); 975 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) { 976 CCValAssign &VA = RetLocs[I]; 977 SDValue RetValue = OutVals[I]; 978 979 // Make the return register live on exit. 980 assert(VA.isRegLoc() && "Can only return in registers!"); 981 982 // Promote the value as required. 983 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue); 984 985 // Chain and glue the copies together. 986 unsigned Reg = VA.getLocReg(); 987 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue); 988 Glue = Chain.getValue(1); 989 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT())); 990 } 991 992 // Update chain and glue. 993 RetOps[0] = Chain; 994 if (Glue.getNode()) 995 RetOps.push_back(Glue); 996 997 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, 998 RetOps.data(), RetOps.size()); 999 } 1000 1001 SDValue SystemZTargetLowering:: 1002 prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const { 1003 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain); 1004 } 1005 1006 // CC is a comparison that will be implemented using an integer or 1007 // floating-point comparison. Return the condition code mask for 1008 // a branch on true. In the integer case, CCMASK_CMP_UO is set for 1009 // unsigned comparisons and clear for signed ones. In the floating-point 1010 // case, CCMASK_CMP_UO has its normal mask meaning (unordered). 1011 static unsigned CCMaskForCondCode(ISD::CondCode CC) { 1012 #define CONV(X) \ 1013 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \ 1014 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \ 1015 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X 1016 1017 switch (CC) { 1018 default: 1019 llvm_unreachable("Invalid integer condition!"); 1020 1021 CONV(EQ); 1022 CONV(NE); 1023 CONV(GT); 1024 CONV(GE); 1025 CONV(LT); 1026 CONV(LE); 1027 1028 case ISD::SETO: return SystemZ::CCMASK_CMP_O; 1029 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO; 1030 } 1031 #undef CONV 1032 } 1033 1034 // Return a sequence for getting a 1 from an IPM result when CC has a 1035 // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask. 1036 // The handling of CC values outside CCValid doesn't matter. 1037 static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) { 1038 // Deal with cases where the result can be taken directly from a bit 1039 // of the IPM result. 1040 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3))) 1041 return IPMConversion(0, 0, SystemZ::IPM_CC); 1042 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3))) 1043 return IPMConversion(0, 0, SystemZ::IPM_CC + 1); 1044 1045 // Deal with cases where we can add a value to force the sign bit 1046 // to contain the right value. Putting the bit in 31 means we can 1047 // use SRL rather than RISBG(L), and also makes it easier to get a 1048 // 0/-1 value, so it has priority over the other tests below. 1049 // 1050 // These sequences rely on the fact that the upper two bits of the 1051 // IPM result are zero. 1052 uint64_t TopBit = uint64_t(1) << 31; 1053 if (CCMask == (CCValid & SystemZ::CCMASK_0)) 1054 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31); 1055 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1))) 1056 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31); 1057 if (CCMask == (CCValid & (SystemZ::CCMASK_0 1058 | SystemZ::CCMASK_1 1059 | SystemZ::CCMASK_2))) 1060 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31); 1061 if (CCMask == (CCValid & SystemZ::CCMASK_3)) 1062 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31); 1063 if (CCMask == (CCValid & (SystemZ::CCMASK_1 1064 | SystemZ::CCMASK_2 1065 | SystemZ::CCMASK_3))) 1066 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31); 1067 1068 // Next try inverting the value and testing a bit. 0/1 could be 1069 // handled this way too, but we dealt with that case above. 1070 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2))) 1071 return IPMConversion(-1, 0, SystemZ::IPM_CC); 1072 1073 // Handle cases where adding a value forces a non-sign bit to contain 1074 // the right value. 1075 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2))) 1076 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1); 1077 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3))) 1078 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1); 1079 1080 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are 1081 // can be done by inverting the low CC bit and applying one of the 1082 // sign-based extractions above. 1083 if (CCMask == (CCValid & SystemZ::CCMASK_1)) 1084 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31); 1085 if (CCMask == (CCValid & SystemZ::CCMASK_2)) 1086 return IPMConversion(1 << SystemZ::IPM_CC, 1087 TopBit - (3 << SystemZ::IPM_CC), 31); 1088 if (CCMask == (CCValid & (SystemZ::CCMASK_0 1089 | SystemZ::CCMASK_1 1090 | SystemZ::CCMASK_3))) 1091 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31); 1092 if (CCMask == (CCValid & (SystemZ::CCMASK_0 1093 | SystemZ::CCMASK_2 1094 | SystemZ::CCMASK_3))) 1095 return IPMConversion(1 << SystemZ::IPM_CC, 1096 TopBit - (1 << SystemZ::IPM_CC), 31); 1097 1098 llvm_unreachable("Unexpected CC combination"); 1099 } 1100 1101 // If C can be converted to a comparison against zero, adjust the operands 1102 // as necessary. 1103 static void adjustZeroCmp(SelectionDAG &DAG, Comparison &C) { 1104 if (C.ICmpType == SystemZICMP::UnsignedOnly) 1105 return; 1106 1107 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode()); 1108 if (!ConstOp1) 1109 return; 1110 1111 int64_t Value = ConstOp1->getSExtValue(); 1112 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) || 1113 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) || 1114 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) || 1115 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) { 1116 C.CCMask ^= SystemZ::CCMASK_CMP_EQ; 1117 C.Op1 = DAG.getConstant(0, C.Op1.getValueType()); 1118 } 1119 } 1120 1121 // If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI, 1122 // adjust the operands as necessary. 1123 static void adjustSubwordCmp(SelectionDAG &DAG, Comparison &C) { 1124 // For us to make any changes, it must a comparison between a single-use 1125 // load and a constant. 1126 if (!C.Op0.hasOneUse() || 1127 C.Op0.getOpcode() != ISD::LOAD || 1128 C.Op1.getOpcode() != ISD::Constant) 1129 return; 1130 1131 // We must have an 8- or 16-bit load. 1132 LoadSDNode *Load = cast<LoadSDNode>(C.Op0); 1133 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits(); 1134 if (NumBits != 8 && NumBits != 16) 1135 return; 1136 1137 // The load must be an extending one and the constant must be within the 1138 // range of the unextended value. 1139 ConstantSDNode *ConstOp1 = cast<ConstantSDNode>(C.Op1); 1140 uint64_t Value = ConstOp1->getZExtValue(); 1141 uint64_t Mask = (1 << NumBits) - 1; 1142 if (Load->getExtensionType() == ISD::SEXTLOAD) { 1143 // Make sure that ConstOp1 is in range of C.Op0. 1144 int64_t SignedValue = ConstOp1->getSExtValue(); 1145 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask) 1146 return; 1147 if (C.ICmpType != SystemZICMP::SignedOnly) { 1148 // Unsigned comparison between two sign-extended values is equivalent 1149 // to unsigned comparison between two zero-extended values. 1150 Value &= Mask; 1151 } else if (NumBits == 8) { 1152 // Try to treat the comparison as unsigned, so that we can use CLI. 1153 // Adjust CCMask and Value as necessary. 1154 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT) 1155 // Test whether the high bit of the byte is set. 1156 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT; 1157 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE) 1158 // Test whether the high bit of the byte is clear. 1159 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT; 1160 else 1161 // No instruction exists for this combination. 1162 return; 1163 C.ICmpType = SystemZICMP::UnsignedOnly; 1164 } 1165 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) { 1166 if (Value > Mask) 1167 return; 1168 assert(C.ICmpType == SystemZICMP::Any && 1169 "Signedness shouldn't matter here."); 1170 } else 1171 return; 1172 1173 // Make sure that the first operand is an i32 of the right extension type. 1174 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ? 1175 ISD::SEXTLOAD : 1176 ISD::ZEXTLOAD); 1177 if (C.Op0.getValueType() != MVT::i32 || 1178 Load->getExtensionType() != ExtType) 1179 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, 1180 Load->getChain(), Load->getBasePtr(), 1181 Load->getPointerInfo(), Load->getMemoryVT(), 1182 Load->isVolatile(), Load->isNonTemporal(), 1183 Load->getAlignment()); 1184 1185 // Make sure that the second operand is an i32 with the right value. 1186 if (C.Op1.getValueType() != MVT::i32 || 1187 Value != ConstOp1->getZExtValue()) 1188 C.Op1 = DAG.getConstant(Value, MVT::i32); 1189 } 1190 1191 // Return true if Op is either an unextended load, or a load suitable 1192 // for integer register-memory comparisons of type ICmpType. 1193 static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) { 1194 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op.getNode()); 1195 if (Load) { 1196 // There are no instructions to compare a register with a memory byte. 1197 if (Load->getMemoryVT() == MVT::i8) 1198 return false; 1199 // Otherwise decide on extension type. 1200 switch (Load->getExtensionType()) { 1201 case ISD::NON_EXTLOAD: 1202 return true; 1203 case ISD::SEXTLOAD: 1204 return ICmpType != SystemZICMP::UnsignedOnly; 1205 case ISD::ZEXTLOAD: 1206 return ICmpType != SystemZICMP::SignedOnly; 1207 default: 1208 break; 1209 } 1210 } 1211 return false; 1212 } 1213 1214 // Return true if it is better to swap the operands of C. 1215 static bool shouldSwapCmpOperands(const Comparison &C) { 1216 // Leave f128 comparisons alone, since they have no memory forms. 1217 if (C.Op0.getValueType() == MVT::f128) 1218 return false; 1219 1220 // Always keep a floating-point constant second, since comparisons with 1221 // zero can use LOAD TEST and comparisons with other constants make a 1222 // natural memory operand. 1223 if (isa<ConstantFPSDNode>(C.Op1)) 1224 return false; 1225 1226 // Never swap comparisons with zero since there are many ways to optimize 1227 // those later. 1228 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1); 1229 if (ConstOp1 && ConstOp1->getZExtValue() == 0) 1230 return false; 1231 1232 // Also keep natural memory operands second if the loaded value is 1233 // only used here. Several comparisons have memory forms. 1234 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse()) 1235 return false; 1236 1237 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't. 1238 // In that case we generally prefer the memory to be second. 1239 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) { 1240 // The only exceptions are when the second operand is a constant and 1241 // we can use things like CHHSI. 1242 if (!ConstOp1) 1243 return true; 1244 // The unsigned memory-immediate instructions can handle 16-bit 1245 // unsigned integers. 1246 if (C.ICmpType != SystemZICMP::SignedOnly && 1247 isUInt<16>(ConstOp1->getZExtValue())) 1248 return false; 1249 // The signed memory-immediate instructions can handle 16-bit 1250 // signed integers. 1251 if (C.ICmpType != SystemZICMP::UnsignedOnly && 1252 isInt<16>(ConstOp1->getSExtValue())) 1253 return false; 1254 return true; 1255 } 1256 1257 // Try to promote the use of CGFR and CLGFR. 1258 unsigned Opcode0 = C.Op0.getOpcode(); 1259 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND) 1260 return true; 1261 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND) 1262 return true; 1263 if (C.ICmpType != SystemZICMP::SignedOnly && 1264 Opcode0 == ISD::AND && 1265 C.Op0.getOperand(1).getOpcode() == ISD::Constant && 1266 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff) 1267 return true; 1268 1269 return false; 1270 } 1271 1272 // Return a version of comparison CC mask CCMask in which the LT and GT 1273 // actions are swapped. 1274 static unsigned reverseCCMask(unsigned CCMask) { 1275 return ((CCMask & SystemZ::CCMASK_CMP_EQ) | 1276 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) | 1277 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) | 1278 (CCMask & SystemZ::CCMASK_CMP_UO)); 1279 } 1280 1281 // Check whether C tests for equality between X and Y and whether X - Y 1282 // or Y - X is also computed. In that case it's better to compare the 1283 // result of the subtraction against zero. 1284 static void adjustForSubtraction(SelectionDAG &DAG, Comparison &C) { 1285 if (C.CCMask == SystemZ::CCMASK_CMP_EQ || 1286 C.CCMask == SystemZ::CCMASK_CMP_NE) { 1287 for (SDNode::use_iterator I = C.Op0->use_begin(), E = C.Op0->use_end(); 1288 I != E; ++I) { 1289 SDNode *N = *I; 1290 if (N->getOpcode() == ISD::SUB && 1291 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) || 1292 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) { 1293 C.Op0 = SDValue(N, 0); 1294 C.Op1 = DAG.getConstant(0, N->getValueType(0)); 1295 return; 1296 } 1297 } 1298 } 1299 } 1300 1301 // Check whether C compares a floating-point value with zero and if that 1302 // floating-point value is also negated. In this case we can use the 1303 // negation to set CC, so avoiding separate LOAD AND TEST and 1304 // LOAD (NEGATIVE/COMPLEMENT) instructions. 1305 static void adjustForFNeg(Comparison &C) { 1306 ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(C.Op1); 1307 if (C1 && C1->isZero()) { 1308 for (SDNode::use_iterator I = C.Op0->use_begin(), E = C.Op0->use_end(); 1309 I != E; ++I) { 1310 SDNode *N = *I; 1311 if (N->getOpcode() == ISD::FNEG) { 1312 C.Op0 = SDValue(N, 0); 1313 C.CCMask = reverseCCMask(C.CCMask); 1314 return; 1315 } 1316 } 1317 } 1318 } 1319 1320 // Check whether C compares (shl X, 32) with 0 and whether X is 1321 // also sign-extended. In that case it is better to test the result 1322 // of the sign extension using LTGFR. 1323 // 1324 // This case is important because InstCombine transforms a comparison 1325 // with (sext (trunc X)) into a comparison with (shl X, 32). 1326 static void adjustForLTGFR(Comparison &C) { 1327 // Check for a comparison between (shl X, 32) and 0. 1328 if (C.Op0.getOpcode() == ISD::SHL && 1329 C.Op0.getValueType() == MVT::i64 && 1330 C.Op1.getOpcode() == ISD::Constant && 1331 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { 1332 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1)); 1333 if (C1 && C1->getZExtValue() == 32) { 1334 SDValue ShlOp0 = C.Op0.getOperand(0); 1335 // See whether X has any SIGN_EXTEND_INREG uses. 1336 for (SDNode::use_iterator I = ShlOp0->use_begin(), E = ShlOp0->use_end(); 1337 I != E; ++I) { 1338 SDNode *N = *I; 1339 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG && 1340 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) { 1341 C.Op0 = SDValue(N, 0); 1342 return; 1343 } 1344 } 1345 } 1346 } 1347 } 1348 1349 // If C compares the truncation of an extending load, try to compare 1350 // the untruncated value instead. This exposes more opportunities to 1351 // reuse CC. 1352 static void adjustICmpTruncate(SelectionDAG &DAG, Comparison &C) { 1353 if (C.Op0.getOpcode() == ISD::TRUNCATE && 1354 C.Op0.getOperand(0).getOpcode() == ISD::LOAD && 1355 C.Op1.getOpcode() == ISD::Constant && 1356 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { 1357 LoadSDNode *L = cast<LoadSDNode>(C.Op0.getOperand(0)); 1358 if (L->getMemoryVT().getStoreSizeInBits() 1359 <= C.Op0.getValueType().getSizeInBits()) { 1360 unsigned Type = L->getExtensionType(); 1361 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) || 1362 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) { 1363 C.Op0 = C.Op0.getOperand(0); 1364 C.Op1 = DAG.getConstant(0, C.Op0.getValueType()); 1365 } 1366 } 1367 } 1368 } 1369 1370 // Return true if shift operation N has an in-range constant shift value. 1371 // Store it in ShiftVal if so. 1372 static bool isSimpleShift(SDValue N, unsigned &ShiftVal) { 1373 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1)); 1374 if (!Shift) 1375 return false; 1376 1377 uint64_t Amount = Shift->getZExtValue(); 1378 if (Amount >= N.getValueType().getSizeInBits()) 1379 return false; 1380 1381 ShiftVal = Amount; 1382 return true; 1383 } 1384 1385 // Check whether an AND with Mask is suitable for a TEST UNDER MASK 1386 // instruction and whether the CC value is descriptive enough to handle 1387 // a comparison of type Opcode between the AND result and CmpVal. 1388 // CCMask says which comparison result is being tested and BitSize is 1389 // the number of bits in the operands. If TEST UNDER MASK can be used, 1390 // return the corresponding CC mask, otherwise return 0. 1391 static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask, 1392 uint64_t Mask, uint64_t CmpVal, 1393 unsigned ICmpType) { 1394 assert(Mask != 0 && "ANDs with zero should have been removed by now"); 1395 1396 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL. 1397 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) && 1398 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask)) 1399 return 0; 1400 1401 // Work out the masks for the lowest and highest bits. 1402 unsigned HighShift = 63 - countLeadingZeros(Mask); 1403 uint64_t High = uint64_t(1) << HighShift; 1404 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask); 1405 1406 // Signed ordered comparisons are effectively unsigned if the sign 1407 // bit is dropped. 1408 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly); 1409 1410 // Check for equality comparisons with 0, or the equivalent. 1411 if (CmpVal == 0) { 1412 if (CCMask == SystemZ::CCMASK_CMP_EQ) 1413 return SystemZ::CCMASK_TM_ALL_0; 1414 if (CCMask == SystemZ::CCMASK_CMP_NE) 1415 return SystemZ::CCMASK_TM_SOME_1; 1416 } 1417 if (EffectivelyUnsigned && CmpVal <= Low) { 1418 if (CCMask == SystemZ::CCMASK_CMP_LT) 1419 return SystemZ::CCMASK_TM_ALL_0; 1420 if (CCMask == SystemZ::CCMASK_CMP_GE) 1421 return SystemZ::CCMASK_TM_SOME_1; 1422 } 1423 if (EffectivelyUnsigned && CmpVal < Low) { 1424 if (CCMask == SystemZ::CCMASK_CMP_LE) 1425 return SystemZ::CCMASK_TM_ALL_0; 1426 if (CCMask == SystemZ::CCMASK_CMP_GT) 1427 return SystemZ::CCMASK_TM_SOME_1; 1428 } 1429 1430 // Check for equality comparisons with the mask, or the equivalent. 1431 if (CmpVal == Mask) { 1432 if (CCMask == SystemZ::CCMASK_CMP_EQ) 1433 return SystemZ::CCMASK_TM_ALL_1; 1434 if (CCMask == SystemZ::CCMASK_CMP_NE) 1435 return SystemZ::CCMASK_TM_SOME_0; 1436 } 1437 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) { 1438 if (CCMask == SystemZ::CCMASK_CMP_GT) 1439 return SystemZ::CCMASK_TM_ALL_1; 1440 if (CCMask == SystemZ::CCMASK_CMP_LE) 1441 return SystemZ::CCMASK_TM_SOME_0; 1442 } 1443 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) { 1444 if (CCMask == SystemZ::CCMASK_CMP_GE) 1445 return SystemZ::CCMASK_TM_ALL_1; 1446 if (CCMask == SystemZ::CCMASK_CMP_LT) 1447 return SystemZ::CCMASK_TM_SOME_0; 1448 } 1449 1450 // Check for ordered comparisons with the top bit. 1451 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) { 1452 if (CCMask == SystemZ::CCMASK_CMP_LE) 1453 return SystemZ::CCMASK_TM_MSB_0; 1454 if (CCMask == SystemZ::CCMASK_CMP_GT) 1455 return SystemZ::CCMASK_TM_MSB_1; 1456 } 1457 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) { 1458 if (CCMask == SystemZ::CCMASK_CMP_LT) 1459 return SystemZ::CCMASK_TM_MSB_0; 1460 if (CCMask == SystemZ::CCMASK_CMP_GE) 1461 return SystemZ::CCMASK_TM_MSB_1; 1462 } 1463 1464 // If there are just two bits, we can do equality checks for Low and High 1465 // as well. 1466 if (Mask == Low + High) { 1467 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low) 1468 return SystemZ::CCMASK_TM_MIXED_MSB_0; 1469 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low) 1470 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY; 1471 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High) 1472 return SystemZ::CCMASK_TM_MIXED_MSB_1; 1473 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High) 1474 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY; 1475 } 1476 1477 // Looks like we've exhausted our options. 1478 return 0; 1479 } 1480 1481 // See whether C can be implemented as a TEST UNDER MASK instruction. 1482 // Update the arguments with the TM version if so. 1483 static void adjustForTestUnderMask(SelectionDAG &DAG, Comparison &C) { 1484 // Check that we have a comparison with a constant. 1485 ConstantSDNode *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1); 1486 if (!ConstOp1) 1487 return; 1488 uint64_t CmpVal = ConstOp1->getZExtValue(); 1489 1490 // Check whether the nonconstant input is an AND with a constant mask. 1491 Comparison NewC(C); 1492 uint64_t MaskVal; 1493 ConstantSDNode *Mask = 0; 1494 if (C.Op0.getOpcode() == ISD::AND) { 1495 NewC.Op0 = C.Op0.getOperand(0); 1496 NewC.Op1 = C.Op0.getOperand(1); 1497 Mask = dyn_cast<ConstantSDNode>(NewC.Op1); 1498 if (!Mask) 1499 return; 1500 MaskVal = Mask->getZExtValue(); 1501 } else { 1502 // There is no instruction to compare with a 64-bit immediate 1503 // so use TMHH instead if possible. We need an unsigned ordered 1504 // comparison with an i64 immediate. 1505 if (NewC.Op0.getValueType() != MVT::i64 || 1506 NewC.CCMask == SystemZ::CCMASK_CMP_EQ || 1507 NewC.CCMask == SystemZ::CCMASK_CMP_NE || 1508 NewC.ICmpType == SystemZICMP::SignedOnly) 1509 return; 1510 // Convert LE and GT comparisons into LT and GE. 1511 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE || 1512 NewC.CCMask == SystemZ::CCMASK_CMP_GT) { 1513 if (CmpVal == uint64_t(-1)) 1514 return; 1515 CmpVal += 1; 1516 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ; 1517 } 1518 // If the low N bits of Op1 are zero than the low N bits of Op0 can 1519 // be masked off without changing the result. 1520 MaskVal = -(CmpVal & -CmpVal); 1521 NewC.ICmpType = SystemZICMP::UnsignedOnly; 1522 } 1523 1524 // Check whether the combination of mask, comparison value and comparison 1525 // type are suitable. 1526 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits(); 1527 unsigned NewCCMask, ShiftVal; 1528 if (NewC.ICmpType != SystemZICMP::SignedOnly && 1529 NewC.Op0.getOpcode() == ISD::SHL && 1530 isSimpleShift(NewC.Op0, ShiftVal) && 1531 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, 1532 MaskVal >> ShiftVal, 1533 CmpVal >> ShiftVal, 1534 SystemZICMP::Any))) { 1535 NewC.Op0 = NewC.Op0.getOperand(0); 1536 MaskVal >>= ShiftVal; 1537 } else if (NewC.ICmpType != SystemZICMP::SignedOnly && 1538 NewC.Op0.getOpcode() == ISD::SRL && 1539 isSimpleShift(NewC.Op0, ShiftVal) && 1540 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, 1541 MaskVal << ShiftVal, 1542 CmpVal << ShiftVal, 1543 SystemZICMP::UnsignedOnly))) { 1544 NewC.Op0 = NewC.Op0.getOperand(0); 1545 MaskVal <<= ShiftVal; 1546 } else { 1547 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal, 1548 NewC.ICmpType); 1549 if (!NewCCMask) 1550 return; 1551 } 1552 1553 // Go ahead and make the change. 1554 C.Opcode = SystemZISD::TM; 1555 C.Op0 = NewC.Op0; 1556 if (Mask && Mask->getZExtValue() == MaskVal) 1557 C.Op1 = SDValue(Mask, 0); 1558 else 1559 C.Op1 = DAG.getConstant(MaskVal, C.Op0.getValueType()); 1560 C.CCValid = SystemZ::CCMASK_TM; 1561 C.CCMask = NewCCMask; 1562 } 1563 1564 // Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1. 1565 static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1, 1566 ISD::CondCode Cond) { 1567 Comparison C(CmpOp0, CmpOp1); 1568 C.CCMask = CCMaskForCondCode(Cond); 1569 if (C.Op0.getValueType().isFloatingPoint()) { 1570 C.CCValid = SystemZ::CCMASK_FCMP; 1571 C.Opcode = SystemZISD::FCMP; 1572 adjustForFNeg(C); 1573 } else { 1574 C.CCValid = SystemZ::CCMASK_ICMP; 1575 C.Opcode = SystemZISD::ICMP; 1576 // Choose the type of comparison. Equality and inequality tests can 1577 // use either signed or unsigned comparisons. The choice also doesn't 1578 // matter if both sign bits are known to be clear. In those cases we 1579 // want to give the main isel code the freedom to choose whichever 1580 // form fits best. 1581 if (C.CCMask == SystemZ::CCMASK_CMP_EQ || 1582 C.CCMask == SystemZ::CCMASK_CMP_NE || 1583 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1))) 1584 C.ICmpType = SystemZICMP::Any; 1585 else if (C.CCMask & SystemZ::CCMASK_CMP_UO) 1586 C.ICmpType = SystemZICMP::UnsignedOnly; 1587 else 1588 C.ICmpType = SystemZICMP::SignedOnly; 1589 C.CCMask &= ~SystemZ::CCMASK_CMP_UO; 1590 adjustZeroCmp(DAG, C); 1591 adjustSubwordCmp(DAG, C); 1592 adjustForSubtraction(DAG, C); 1593 adjustForLTGFR(C); 1594 adjustICmpTruncate(DAG, C); 1595 } 1596 1597 if (shouldSwapCmpOperands(C)) { 1598 std::swap(C.Op0, C.Op1); 1599 C.CCMask = reverseCCMask(C.CCMask); 1600 } 1601 1602 adjustForTestUnderMask(DAG, C); 1603 return C; 1604 } 1605 1606 // Emit the comparison instruction described by C. 1607 static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) { 1608 if (C.Opcode == SystemZISD::ICMP) 1609 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1, 1610 DAG.getConstant(C.ICmpType, MVT::i32)); 1611 if (C.Opcode == SystemZISD::TM) { 1612 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) != 1613 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1)); 1614 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1, 1615 DAG.getConstant(RegisterOnly, MVT::i32)); 1616 } 1617 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1); 1618 } 1619 1620 // Implement a 32-bit *MUL_LOHI operation by extending both operands to 1621 // 64 bits. Extend is the extension type to use. Store the high part 1622 // in Hi and the low part in Lo. 1623 static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL, 1624 unsigned Extend, SDValue Op0, SDValue Op1, 1625 SDValue &Hi, SDValue &Lo) { 1626 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0); 1627 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1); 1628 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1); 1629 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, DAG.getConstant(32, MVT::i64)); 1630 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi); 1631 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul); 1632 } 1633 1634 // Lower a binary operation that produces two VT results, one in each 1635 // half of a GR128 pair. Op0 and Op1 are the VT operands to the operation, 1636 // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation 1637 // on the extended Op0 and (unextended) Op1. Store the even register result 1638 // in Even and the odd register result in Odd. 1639 static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT, 1640 unsigned Extend, unsigned Opcode, 1641 SDValue Op0, SDValue Op1, 1642 SDValue &Even, SDValue &Odd) { 1643 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0); 1644 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, 1645 SDValue(In128, 0), Op1); 1646 bool Is32Bit = is32Bit(VT); 1647 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result); 1648 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result); 1649 } 1650 1651 // Return an i32 value that is 1 if the CC value produced by Glue is 1652 // in the mask CCMask and 0 otherwise. CC is known to have a value 1653 // in CCValid, so other values can be ignored. 1654 static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue, 1655 unsigned CCValid, unsigned CCMask) { 1656 IPMConversion Conversion = getIPMConversion(CCValid, CCMask); 1657 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue); 1658 1659 if (Conversion.XORValue) 1660 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result, 1661 DAG.getConstant(Conversion.XORValue, MVT::i32)); 1662 1663 if (Conversion.AddValue) 1664 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result, 1665 DAG.getConstant(Conversion.AddValue, MVT::i32)); 1666 1667 // The SHR/AND sequence should get optimized to an RISBG. 1668 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result, 1669 DAG.getConstant(Conversion.Bit, MVT::i32)); 1670 if (Conversion.Bit != 31) 1671 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result, 1672 DAG.getConstant(1, MVT::i32)); 1673 return Result; 1674 } 1675 1676 SDValue SystemZTargetLowering::lowerSETCC(SDValue Op, 1677 SelectionDAG &DAG) const { 1678 SDValue CmpOp0 = Op.getOperand(0); 1679 SDValue CmpOp1 = Op.getOperand(1); 1680 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get(); 1681 SDLoc DL(Op); 1682 1683 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC)); 1684 SDValue Glue = emitCmp(DAG, DL, C); 1685 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask); 1686 } 1687 1688 SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const { 1689 SDValue Chain = Op.getOperand(0); 1690 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 1691 SDValue CmpOp0 = Op.getOperand(2); 1692 SDValue CmpOp1 = Op.getOperand(3); 1693 SDValue Dest = Op.getOperand(4); 1694 SDLoc DL(Op); 1695 1696 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC)); 1697 SDValue Glue = emitCmp(DAG, DL, C); 1698 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(), 1699 Chain, DAG.getConstant(C.CCValid, MVT::i32), 1700 DAG.getConstant(C.CCMask, MVT::i32), Dest, Glue); 1701 } 1702 1703 // Return true if Pos is CmpOp and Neg is the negative of CmpOp, 1704 // allowing Pos and Neg to be wider than CmpOp. 1705 static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) { 1706 return (Neg.getOpcode() == ISD::SUB && 1707 Neg.getOperand(0).getOpcode() == ISD::Constant && 1708 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 && 1709 Neg.getOperand(1) == Pos && 1710 (Pos == CmpOp || 1711 (Pos.getOpcode() == ISD::SIGN_EXTEND && 1712 Pos.getOperand(0) == CmpOp))); 1713 } 1714 1715 // Return the absolute or negative absolute of Op; IsNegative decides which. 1716 static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op, 1717 bool IsNegative) { 1718 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op); 1719 if (IsNegative) 1720 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(), 1721 DAG.getConstant(0, Op.getValueType()), Op); 1722 return Op; 1723 } 1724 1725 SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op, 1726 SelectionDAG &DAG) const { 1727 SDValue CmpOp0 = Op.getOperand(0); 1728 SDValue CmpOp1 = Op.getOperand(1); 1729 SDValue TrueOp = Op.getOperand(2); 1730 SDValue FalseOp = Op.getOperand(3); 1731 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 1732 SDLoc DL(Op); 1733 1734 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC)); 1735 1736 // Check for absolute and negative-absolute selections, including those 1737 // where the comparison value is sign-extended (for LPGFR and LNGFR). 1738 // This check supplements the one in DAGCombiner. 1739 if (C.Opcode == SystemZISD::ICMP && 1740 C.CCMask != SystemZ::CCMASK_CMP_EQ && 1741 C.CCMask != SystemZ::CCMASK_CMP_NE && 1742 C.Op1.getOpcode() == ISD::Constant && 1743 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) { 1744 if (isAbsolute(C.Op0, TrueOp, FalseOp)) 1745 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT); 1746 if (isAbsolute(C.Op0, FalseOp, TrueOp)) 1747 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT); 1748 } 1749 1750 SDValue Glue = emitCmp(DAG, DL, C); 1751 1752 // Special case for handling -1/0 results. The shifts we use here 1753 // should get optimized with the IPM conversion sequence. 1754 ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp); 1755 ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp); 1756 if (TrueC && FalseC) { 1757 int64_t TrueVal = TrueC->getSExtValue(); 1758 int64_t FalseVal = FalseC->getSExtValue(); 1759 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) { 1760 // Invert the condition if we want -1 on false. 1761 if (TrueVal == 0) 1762 C.CCMask ^= C.CCValid; 1763 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask); 1764 EVT VT = Op.getValueType(); 1765 // Extend the result to VT. Upper bits are ignored. 1766 if (!is32Bit(VT)) 1767 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result); 1768 // Sign-extend from the low bit. 1769 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, MVT::i32); 1770 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt); 1771 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt); 1772 } 1773 } 1774 1775 SmallVector<SDValue, 5> Ops; 1776 Ops.push_back(TrueOp); 1777 Ops.push_back(FalseOp); 1778 Ops.push_back(DAG.getConstant(C.CCValid, MVT::i32)); 1779 Ops.push_back(DAG.getConstant(C.CCMask, MVT::i32)); 1780 Ops.push_back(Glue); 1781 1782 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue); 1783 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, &Ops[0], Ops.size()); 1784 } 1785 1786 SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node, 1787 SelectionDAG &DAG) const { 1788 SDLoc DL(Node); 1789 const GlobalValue *GV = Node->getGlobal(); 1790 int64_t Offset = Node->getOffset(); 1791 EVT PtrVT = getPointerTy(); 1792 Reloc::Model RM = TM.getRelocationModel(); 1793 CodeModel::Model CM = TM.getCodeModel(); 1794 1795 SDValue Result; 1796 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) { 1797 // Assign anchors at 1<<12 byte boundaries. 1798 uint64_t Anchor = Offset & ~uint64_t(0xfff); 1799 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor); 1800 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 1801 1802 // The offset can be folded into the address if it is aligned to a halfword. 1803 Offset -= Anchor; 1804 if (Offset != 0 && (Offset & 1) == 0) { 1805 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset); 1806 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result); 1807 Offset = 0; 1808 } 1809 } else { 1810 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT); 1811 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 1812 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result, 1813 MachinePointerInfo::getGOT(), false, false, false, 0); 1814 } 1815 1816 // If there was a non-zero offset that we didn't fold, create an explicit 1817 // addition for it. 1818 if (Offset != 0) 1819 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result, 1820 DAG.getConstant(Offset, PtrVT)); 1821 1822 return Result; 1823 } 1824 1825 SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node, 1826 SelectionDAG &DAG) const { 1827 SDLoc DL(Node); 1828 const GlobalValue *GV = Node->getGlobal(); 1829 EVT PtrVT = getPointerTy(); 1830 TLSModel::Model model = TM.getTLSModel(GV); 1831 1832 if (model != TLSModel::LocalExec) 1833 llvm_unreachable("only local-exec TLS mode supported"); 1834 1835 // The high part of the thread pointer is in access register 0. 1836 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32, 1837 DAG.getConstant(0, MVT::i32)); 1838 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi); 1839 1840 // The low part of the thread pointer is in access register 1. 1841 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32, 1842 DAG.getConstant(1, MVT::i32)); 1843 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo); 1844 1845 // Merge them into a single 64-bit address. 1846 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi, 1847 DAG.getConstant(32, PtrVT)); 1848 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo); 1849 1850 // Get the offset of GA from the thread pointer. 1851 SystemZConstantPoolValue *CPV = 1852 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF); 1853 1854 // Force the offset into the constant pool and load it from there. 1855 SDValue CPAddr = DAG.getConstantPool(CPV, PtrVT, 8); 1856 SDValue Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), 1857 CPAddr, MachinePointerInfo::getConstantPool(), 1858 false, false, false, 0); 1859 1860 // Add the base and offset together. 1861 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset); 1862 } 1863 1864 SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node, 1865 SelectionDAG &DAG) const { 1866 SDLoc DL(Node); 1867 const BlockAddress *BA = Node->getBlockAddress(); 1868 int64_t Offset = Node->getOffset(); 1869 EVT PtrVT = getPointerTy(); 1870 1871 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset); 1872 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 1873 return Result; 1874 } 1875 1876 SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT, 1877 SelectionDAG &DAG) const { 1878 SDLoc DL(JT); 1879 EVT PtrVT = getPointerTy(); 1880 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT); 1881 1882 // Use LARL to load the address of the table. 1883 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 1884 } 1885 1886 SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP, 1887 SelectionDAG &DAG) const { 1888 SDLoc DL(CP); 1889 EVT PtrVT = getPointerTy(); 1890 1891 SDValue Result; 1892 if (CP->isMachineConstantPoolEntry()) 1893 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, 1894 CP->getAlignment()); 1895 else 1896 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, 1897 CP->getAlignment(), CP->getOffset()); 1898 1899 // Use LARL to load the address of the constant pool entry. 1900 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result); 1901 } 1902 1903 SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op, 1904 SelectionDAG &DAG) const { 1905 SDLoc DL(Op); 1906 SDValue In = Op.getOperand(0); 1907 EVT InVT = In.getValueType(); 1908 EVT ResVT = Op.getValueType(); 1909 1910 if (InVT == MVT::i32 && ResVT == MVT::f32) { 1911 SDValue In64; 1912 if (Subtarget.hasHighWord()) { 1913 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, 1914 MVT::i64); 1915 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL, 1916 MVT::i64, SDValue(U64, 0), In); 1917 } else { 1918 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In); 1919 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64, 1920 DAG.getConstant(32, MVT::i64)); 1921 } 1922 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64); 1923 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, 1924 DL, MVT::f32, Out64); 1925 } 1926 if (InVT == MVT::f32 && ResVT == MVT::i32) { 1927 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64); 1928 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL, 1929 MVT::f64, SDValue(U64, 0), In); 1930 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64); 1931 if (Subtarget.hasHighWord()) 1932 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL, 1933 MVT::i32, Out64); 1934 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64, 1935 DAG.getConstant(32, MVT::i64)); 1936 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift); 1937 } 1938 llvm_unreachable("Unexpected bitcast combination"); 1939 } 1940 1941 SDValue SystemZTargetLowering::lowerVASTART(SDValue Op, 1942 SelectionDAG &DAG) const { 1943 MachineFunction &MF = DAG.getMachineFunction(); 1944 SystemZMachineFunctionInfo *FuncInfo = 1945 MF.getInfo<SystemZMachineFunctionInfo>(); 1946 EVT PtrVT = getPointerTy(); 1947 1948 SDValue Chain = Op.getOperand(0); 1949 SDValue Addr = Op.getOperand(1); 1950 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 1951 SDLoc DL(Op); 1952 1953 // The initial values of each field. 1954 const unsigned NumFields = 4; 1955 SDValue Fields[NumFields] = { 1956 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT), 1957 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT), 1958 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT), 1959 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT) 1960 }; 1961 1962 // Store each field into its respective slot. 1963 SDValue MemOps[NumFields]; 1964 unsigned Offset = 0; 1965 for (unsigned I = 0; I < NumFields; ++I) { 1966 SDValue FieldAddr = Addr; 1967 if (Offset != 0) 1968 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr, 1969 DAG.getIntPtrConstant(Offset)); 1970 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr, 1971 MachinePointerInfo(SV, Offset), 1972 false, false, 0); 1973 Offset += 8; 1974 } 1975 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps, NumFields); 1976 } 1977 1978 SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op, 1979 SelectionDAG &DAG) const { 1980 SDValue Chain = Op.getOperand(0); 1981 SDValue DstPtr = Op.getOperand(1); 1982 SDValue SrcPtr = Op.getOperand(2); 1983 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); 1984 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); 1985 SDLoc DL(Op); 1986 1987 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32), 1988 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false, 1989 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV)); 1990 } 1991 1992 SDValue SystemZTargetLowering:: 1993 lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const { 1994 SDValue Chain = Op.getOperand(0); 1995 SDValue Size = Op.getOperand(1); 1996 SDLoc DL(Op); 1997 1998 unsigned SPReg = getStackPointerRegisterToSaveRestore(); 1999 2000 // Get a reference to the stack pointer. 2001 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64); 2002 2003 // Get the new stack pointer value. 2004 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size); 2005 2006 // Copy the new stack pointer back. 2007 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP); 2008 2009 // The allocated data lives above the 160 bytes allocated for the standard 2010 // frame, plus any outgoing stack arguments. We don't know how much that 2011 // amounts to yet, so emit a special ADJDYNALLOC placeholder. 2012 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64); 2013 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust); 2014 2015 SDValue Ops[2] = { Result, Chain }; 2016 return DAG.getMergeValues(Ops, 2, DL); 2017 } 2018 2019 SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op, 2020 SelectionDAG &DAG) const { 2021 EVT VT = Op.getValueType(); 2022 SDLoc DL(Op); 2023 SDValue Ops[2]; 2024 if (is32Bit(VT)) 2025 // Just do a normal 64-bit multiplication and extract the results. 2026 // We define this so that it can be used for constant division. 2027 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0), 2028 Op.getOperand(1), Ops[1], Ops[0]); 2029 else { 2030 // Do a full 128-bit multiplication based on UMUL_LOHI64: 2031 // 2032 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64) 2033 // 2034 // but using the fact that the upper halves are either all zeros 2035 // or all ones: 2036 // 2037 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64) 2038 // 2039 // and grouping the right terms together since they are quicker than the 2040 // multiplication: 2041 // 2042 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64) 2043 SDValue C63 = DAG.getConstant(63, MVT::i64); 2044 SDValue LL = Op.getOperand(0); 2045 SDValue RL = Op.getOperand(1); 2046 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63); 2047 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63); 2048 // UMUL_LOHI64 returns the low result in the odd register and the high 2049 // result in the even register. SMUL_LOHI is defined to return the 2050 // low half first, so the results are in reverse order. 2051 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, 2052 LL, RL, Ops[1], Ops[0]); 2053 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH); 2054 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL); 2055 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL); 2056 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum); 2057 } 2058 return DAG.getMergeValues(Ops, 2, DL); 2059 } 2060 2061 SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op, 2062 SelectionDAG &DAG) const { 2063 EVT VT = Op.getValueType(); 2064 SDLoc DL(Op); 2065 SDValue Ops[2]; 2066 if (is32Bit(VT)) 2067 // Just do a normal 64-bit multiplication and extract the results. 2068 // We define this so that it can be used for constant division. 2069 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0), 2070 Op.getOperand(1), Ops[1], Ops[0]); 2071 else 2072 // UMUL_LOHI64 returns the low result in the odd register and the high 2073 // result in the even register. UMUL_LOHI is defined to return the 2074 // low half first, so the results are in reverse order. 2075 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64, 2076 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); 2077 return DAG.getMergeValues(Ops, 2, DL); 2078 } 2079 2080 SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op, 2081 SelectionDAG &DAG) const { 2082 SDValue Op0 = Op.getOperand(0); 2083 SDValue Op1 = Op.getOperand(1); 2084 EVT VT = Op.getValueType(); 2085 SDLoc DL(Op); 2086 unsigned Opcode; 2087 2088 // We use DSGF for 32-bit division. 2089 if (is32Bit(VT)) { 2090 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0); 2091 Opcode = SystemZISD::SDIVREM32; 2092 } else if (DAG.ComputeNumSignBits(Op1) > 32) { 2093 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1); 2094 Opcode = SystemZISD::SDIVREM32; 2095 } else 2096 Opcode = SystemZISD::SDIVREM64; 2097 2098 // DSG(F) takes a 64-bit dividend, so the even register in the GR128 2099 // input is "don't care". The instruction returns the remainder in 2100 // the even register and the quotient in the odd register. 2101 SDValue Ops[2]; 2102 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode, 2103 Op0, Op1, Ops[1], Ops[0]); 2104 return DAG.getMergeValues(Ops, 2, DL); 2105 } 2106 2107 SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op, 2108 SelectionDAG &DAG) const { 2109 EVT VT = Op.getValueType(); 2110 SDLoc DL(Op); 2111 2112 // DL(G) uses a double-width dividend, so we need to clear the even 2113 // register in the GR128 input. The instruction returns the remainder 2114 // in the even register and the quotient in the odd register. 2115 SDValue Ops[2]; 2116 if (is32Bit(VT)) 2117 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32, 2118 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); 2119 else 2120 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64, 2121 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]); 2122 return DAG.getMergeValues(Ops, 2, DL); 2123 } 2124 2125 SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const { 2126 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation"); 2127 2128 // Get the known-zero masks for each operand. 2129 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) }; 2130 APInt KnownZero[2], KnownOne[2]; 2131 DAG.ComputeMaskedBits(Ops[0], KnownZero[0], KnownOne[0]); 2132 DAG.ComputeMaskedBits(Ops[1], KnownZero[1], KnownOne[1]); 2133 2134 // See if the upper 32 bits of one operand and the lower 32 bits of the 2135 // other are known zero. They are the low and high operands respectively. 2136 uint64_t Masks[] = { KnownZero[0].getZExtValue(), 2137 KnownZero[1].getZExtValue() }; 2138 unsigned High, Low; 2139 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff) 2140 High = 1, Low = 0; 2141 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff) 2142 High = 0, Low = 1; 2143 else 2144 return Op; 2145 2146 SDValue LowOp = Ops[Low]; 2147 SDValue HighOp = Ops[High]; 2148 2149 // If the high part is a constant, we're better off using IILH. 2150 if (HighOp.getOpcode() == ISD::Constant) 2151 return Op; 2152 2153 // If the low part is a constant that is outside the range of LHI, 2154 // then we're better off using IILF. 2155 if (LowOp.getOpcode() == ISD::Constant) { 2156 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue()); 2157 if (!isInt<16>(Value)) 2158 return Op; 2159 } 2160 2161 // Check whether the high part is an AND that doesn't change the 2162 // high 32 bits and just masks out low bits. We can skip it if so. 2163 if (HighOp.getOpcode() == ISD::AND && 2164 HighOp.getOperand(1).getOpcode() == ISD::Constant) { 2165 SDValue HighOp0 = HighOp.getOperand(0); 2166 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue(); 2167 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff)))) 2168 HighOp = HighOp0; 2169 } 2170 2171 // Take advantage of the fact that all GR32 operations only change the 2172 // low 32 bits by truncating Low to an i32 and inserting it directly 2173 // using a subreg. The interesting cases are those where the truncation 2174 // can be folded. 2175 SDLoc DL(Op); 2176 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp); 2177 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL, 2178 MVT::i64, HighOp, Low32); 2179 } 2180 2181 SDValue SystemZTargetLowering::lowerSIGN_EXTEND(SDValue Op, 2182 SelectionDAG &DAG) const { 2183 // Convert (sext (ashr (shl X, C1), C2)) to 2184 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as 2185 // cheap as narrower ones. 2186 SDValue N0 = Op.getOperand(0); 2187 EVT VT = Op.getValueType(); 2188 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) { 2189 ConstantSDNode *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)); 2190 SDValue Inner = N0.getOperand(0); 2191 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) { 2192 ConstantSDNode *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1)); 2193 if (ShlAmt) { 2194 unsigned Extra = (VT.getSizeInBits() - 2195 N0.getValueType().getSizeInBits()); 2196 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra; 2197 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra; 2198 EVT ShiftVT = N0.getOperand(1).getValueType(); 2199 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT, 2200 Inner.getOperand(0)); 2201 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext, 2202 DAG.getConstant(NewShlAmt, ShiftVT)); 2203 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, 2204 DAG.getConstant(NewSraAmt, ShiftVT)); 2205 } 2206 } 2207 } 2208 return SDValue(); 2209 } 2210 2211 // Op is an atomic load. Lower it into a normal volatile load. 2212 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op, 2213 SelectionDAG &DAG) const { 2214 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode()); 2215 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(), 2216 Node->getChain(), Node->getBasePtr(), 2217 Node->getMemoryVT(), Node->getMemOperand()); 2218 } 2219 2220 // Op is an atomic store. Lower it into a normal volatile store followed 2221 // by a serialization. 2222 SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op, 2223 SelectionDAG &DAG) const { 2224 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode()); 2225 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(), 2226 Node->getBasePtr(), Node->getMemoryVT(), 2227 Node->getMemOperand()); 2228 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other, 2229 Chain), 0); 2230 } 2231 2232 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first 2233 // two into the fullword ATOMIC_LOADW_* operation given by Opcode. 2234 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op, 2235 SelectionDAG &DAG, 2236 unsigned Opcode) const { 2237 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode()); 2238 2239 // 32-bit operations need no code outside the main loop. 2240 EVT NarrowVT = Node->getMemoryVT(); 2241 EVT WideVT = MVT::i32; 2242 if (NarrowVT == WideVT) 2243 return Op; 2244 2245 int64_t BitSize = NarrowVT.getSizeInBits(); 2246 SDValue ChainIn = Node->getChain(); 2247 SDValue Addr = Node->getBasePtr(); 2248 SDValue Src2 = Node->getVal(); 2249 MachineMemOperand *MMO = Node->getMemOperand(); 2250 SDLoc DL(Node); 2251 EVT PtrVT = Addr.getValueType(); 2252 2253 // Convert atomic subtracts of constants into additions. 2254 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB) 2255 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(Src2)) { 2256 Opcode = SystemZISD::ATOMIC_LOADW_ADD; 2257 Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType()); 2258 } 2259 2260 // Get the address of the containing word. 2261 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, 2262 DAG.getConstant(-4, PtrVT)); 2263 2264 // Get the number of bits that the word must be rotated left in order 2265 // to bring the field to the top bits of a GR32. 2266 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, 2267 DAG.getConstant(3, PtrVT)); 2268 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); 2269 2270 // Get the complementing shift amount, for rotating a field in the top 2271 // bits back to its proper position. 2272 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, 2273 DAG.getConstant(0, WideVT), BitShift); 2274 2275 // Extend the source operand to 32 bits and prepare it for the inner loop. 2276 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other 2277 // operations require the source to be shifted in advance. (This shift 2278 // can be folded if the source is constant.) For AND and NAND, the lower 2279 // bits must be set, while for other opcodes they should be left clear. 2280 if (Opcode != SystemZISD::ATOMIC_SWAPW) 2281 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2, 2282 DAG.getConstant(32 - BitSize, WideVT)); 2283 if (Opcode == SystemZISD::ATOMIC_LOADW_AND || 2284 Opcode == SystemZISD::ATOMIC_LOADW_NAND) 2285 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2, 2286 DAG.getConstant(uint32_t(-1) >> BitSize, WideVT)); 2287 2288 // Construct the ATOMIC_LOADW_* node. 2289 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); 2290 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift, 2291 DAG.getConstant(BitSize, WideVT) }; 2292 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops, 2293 array_lengthof(Ops), 2294 NarrowVT, MMO); 2295 2296 // Rotate the result of the final CS so that the field is in the lower 2297 // bits of a GR32, then truncate it. 2298 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift, 2299 DAG.getConstant(BitSize, WideVT)); 2300 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift); 2301 2302 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) }; 2303 return DAG.getMergeValues(RetOps, 2, DL); 2304 } 2305 2306 // Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations 2307 // into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit 2308 // operations into additions. 2309 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op, 2310 SelectionDAG &DAG) const { 2311 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode()); 2312 EVT MemVT = Node->getMemoryVT(); 2313 if (MemVT == MVT::i32 || MemVT == MVT::i64) { 2314 // A full-width operation. 2315 assert(Op.getValueType() == MemVT && "Mismatched VTs"); 2316 SDValue Src2 = Node->getVal(); 2317 SDValue NegSrc2; 2318 SDLoc DL(Src2); 2319 2320 if (ConstantSDNode *Op2 = dyn_cast<ConstantSDNode>(Src2)) { 2321 // Use an addition if the operand is constant and either LAA(G) is 2322 // available or the negative value is in the range of A(G)FHI. 2323 int64_t Value = (-Op2->getAPIntValue()).getSExtValue(); 2324 if (isInt<32>(Value) || TM.getSubtargetImpl()->hasInterlockedAccess1()) 2325 NegSrc2 = DAG.getConstant(Value, MemVT); 2326 } else if (TM.getSubtargetImpl()->hasInterlockedAccess1()) 2327 // Use LAA(G) if available. 2328 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, MemVT), 2329 Src2); 2330 2331 if (NegSrc2.getNode()) 2332 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT, 2333 Node->getChain(), Node->getBasePtr(), NegSrc2, 2334 Node->getMemOperand(), Node->getOrdering(), 2335 Node->getSynchScope()); 2336 2337 // Use the node as-is. 2338 return Op; 2339 } 2340 2341 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB); 2342 } 2343 2344 // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two 2345 // into a fullword ATOMIC_CMP_SWAPW operation. 2346 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op, 2347 SelectionDAG &DAG) const { 2348 AtomicSDNode *Node = cast<AtomicSDNode>(Op.getNode()); 2349 2350 // We have native support for 32-bit compare and swap. 2351 EVT NarrowVT = Node->getMemoryVT(); 2352 EVT WideVT = MVT::i32; 2353 if (NarrowVT == WideVT) 2354 return Op; 2355 2356 int64_t BitSize = NarrowVT.getSizeInBits(); 2357 SDValue ChainIn = Node->getOperand(0); 2358 SDValue Addr = Node->getOperand(1); 2359 SDValue CmpVal = Node->getOperand(2); 2360 SDValue SwapVal = Node->getOperand(3); 2361 MachineMemOperand *MMO = Node->getMemOperand(); 2362 SDLoc DL(Node); 2363 EVT PtrVT = Addr.getValueType(); 2364 2365 // Get the address of the containing word. 2366 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr, 2367 DAG.getConstant(-4, PtrVT)); 2368 2369 // Get the number of bits that the word must be rotated left in order 2370 // to bring the field to the top bits of a GR32. 2371 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr, 2372 DAG.getConstant(3, PtrVT)); 2373 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift); 2374 2375 // Get the complementing shift amount, for rotating a field in the top 2376 // bits back to its proper position. 2377 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT, 2378 DAG.getConstant(0, WideVT), BitShift); 2379 2380 // Construct the ATOMIC_CMP_SWAPW node. 2381 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other); 2382 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift, 2383 NegBitShift, DAG.getConstant(BitSize, WideVT) }; 2384 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL, 2385 VTList, Ops, array_lengthof(Ops), 2386 NarrowVT, MMO); 2387 return AtomicOp; 2388 } 2389 2390 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op, 2391 SelectionDAG &DAG) const { 2392 MachineFunction &MF = DAG.getMachineFunction(); 2393 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 2394 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op), 2395 SystemZ::R15D, Op.getValueType()); 2396 } 2397 2398 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op, 2399 SelectionDAG &DAG) const { 2400 MachineFunction &MF = DAG.getMachineFunction(); 2401 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true); 2402 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op), 2403 SystemZ::R15D, Op.getOperand(1)); 2404 } 2405 2406 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op, 2407 SelectionDAG &DAG) const { 2408 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue(); 2409 if (!IsData) 2410 // Just preserve the chain. 2411 return Op.getOperand(0); 2412 2413 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue(); 2414 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ; 2415 MemIntrinsicSDNode *Node = cast<MemIntrinsicSDNode>(Op.getNode()); 2416 SDValue Ops[] = { 2417 Op.getOperand(0), 2418 DAG.getConstant(Code, MVT::i32), 2419 Op.getOperand(1) 2420 }; 2421 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, SDLoc(Op), 2422 Node->getVTList(), Ops, array_lengthof(Ops), 2423 Node->getMemoryVT(), Node->getMemOperand()); 2424 } 2425 2426 SDValue SystemZTargetLowering::LowerOperation(SDValue Op, 2427 SelectionDAG &DAG) const { 2428 switch (Op.getOpcode()) { 2429 case ISD::BR_CC: 2430 return lowerBR_CC(Op, DAG); 2431 case ISD::SELECT_CC: 2432 return lowerSELECT_CC(Op, DAG); 2433 case ISD::SETCC: 2434 return lowerSETCC(Op, DAG); 2435 case ISD::GlobalAddress: 2436 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG); 2437 case ISD::GlobalTLSAddress: 2438 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG); 2439 case ISD::BlockAddress: 2440 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG); 2441 case ISD::JumpTable: 2442 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG); 2443 case ISD::ConstantPool: 2444 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG); 2445 case ISD::BITCAST: 2446 return lowerBITCAST(Op, DAG); 2447 case ISD::VASTART: 2448 return lowerVASTART(Op, DAG); 2449 case ISD::VACOPY: 2450 return lowerVACOPY(Op, DAG); 2451 case ISD::DYNAMIC_STACKALLOC: 2452 return lowerDYNAMIC_STACKALLOC(Op, DAG); 2453 case ISD::SMUL_LOHI: 2454 return lowerSMUL_LOHI(Op, DAG); 2455 case ISD::UMUL_LOHI: 2456 return lowerUMUL_LOHI(Op, DAG); 2457 case ISD::SDIVREM: 2458 return lowerSDIVREM(Op, DAG); 2459 case ISD::UDIVREM: 2460 return lowerUDIVREM(Op, DAG); 2461 case ISD::OR: 2462 return lowerOR(Op, DAG); 2463 case ISD::SIGN_EXTEND: 2464 return lowerSIGN_EXTEND(Op, DAG); 2465 case ISD::ATOMIC_SWAP: 2466 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW); 2467 case ISD::ATOMIC_STORE: 2468 return lowerATOMIC_STORE(Op, DAG); 2469 case ISD::ATOMIC_LOAD: 2470 return lowerATOMIC_LOAD(Op, DAG); 2471 case ISD::ATOMIC_LOAD_ADD: 2472 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD); 2473 case ISD::ATOMIC_LOAD_SUB: 2474 return lowerATOMIC_LOAD_SUB(Op, DAG); 2475 case ISD::ATOMIC_LOAD_AND: 2476 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND); 2477 case ISD::ATOMIC_LOAD_OR: 2478 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR); 2479 case ISD::ATOMIC_LOAD_XOR: 2480 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR); 2481 case ISD::ATOMIC_LOAD_NAND: 2482 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND); 2483 case ISD::ATOMIC_LOAD_MIN: 2484 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN); 2485 case ISD::ATOMIC_LOAD_MAX: 2486 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX); 2487 case ISD::ATOMIC_LOAD_UMIN: 2488 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN); 2489 case ISD::ATOMIC_LOAD_UMAX: 2490 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX); 2491 case ISD::ATOMIC_CMP_SWAP: 2492 return lowerATOMIC_CMP_SWAP(Op, DAG); 2493 case ISD::STACKSAVE: 2494 return lowerSTACKSAVE(Op, DAG); 2495 case ISD::STACKRESTORE: 2496 return lowerSTACKRESTORE(Op, DAG); 2497 case ISD::PREFETCH: 2498 return lowerPREFETCH(Op, DAG); 2499 default: 2500 llvm_unreachable("Unexpected node to lower"); 2501 } 2502 } 2503 2504 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const { 2505 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME 2506 switch (Opcode) { 2507 OPCODE(RET_FLAG); 2508 OPCODE(CALL); 2509 OPCODE(SIBCALL); 2510 OPCODE(PCREL_WRAPPER); 2511 OPCODE(PCREL_OFFSET); 2512 OPCODE(IABS); 2513 OPCODE(ICMP); 2514 OPCODE(FCMP); 2515 OPCODE(TM); 2516 OPCODE(BR_CCMASK); 2517 OPCODE(SELECT_CCMASK); 2518 OPCODE(ADJDYNALLOC); 2519 OPCODE(EXTRACT_ACCESS); 2520 OPCODE(UMUL_LOHI64); 2521 OPCODE(SDIVREM64); 2522 OPCODE(UDIVREM32); 2523 OPCODE(UDIVREM64); 2524 OPCODE(MVC); 2525 OPCODE(MVC_LOOP); 2526 OPCODE(NC); 2527 OPCODE(NC_LOOP); 2528 OPCODE(OC); 2529 OPCODE(OC_LOOP); 2530 OPCODE(XC); 2531 OPCODE(XC_LOOP); 2532 OPCODE(CLC); 2533 OPCODE(CLC_LOOP); 2534 OPCODE(STRCMP); 2535 OPCODE(STPCPY); 2536 OPCODE(SEARCH_STRING); 2537 OPCODE(IPM); 2538 OPCODE(SERIALIZE); 2539 OPCODE(ATOMIC_SWAPW); 2540 OPCODE(ATOMIC_LOADW_ADD); 2541 OPCODE(ATOMIC_LOADW_SUB); 2542 OPCODE(ATOMIC_LOADW_AND); 2543 OPCODE(ATOMIC_LOADW_OR); 2544 OPCODE(ATOMIC_LOADW_XOR); 2545 OPCODE(ATOMIC_LOADW_NAND); 2546 OPCODE(ATOMIC_LOADW_MIN); 2547 OPCODE(ATOMIC_LOADW_MAX); 2548 OPCODE(ATOMIC_LOADW_UMIN); 2549 OPCODE(ATOMIC_LOADW_UMAX); 2550 OPCODE(ATOMIC_CMP_SWAPW); 2551 OPCODE(PREFETCH); 2552 } 2553 return NULL; 2554 #undef OPCODE 2555 } 2556 2557 //===----------------------------------------------------------------------===// 2558 // Custom insertion 2559 //===----------------------------------------------------------------------===// 2560 2561 // Create a new basic block after MBB. 2562 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) { 2563 MachineFunction &MF = *MBB->getParent(); 2564 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock()); 2565 MF.insert(llvm::next(MachineFunction::iterator(MBB)), NewMBB); 2566 return NewMBB; 2567 } 2568 2569 // Split MBB after MI and return the new block (the one that contains 2570 // instructions after MI). 2571 static MachineBasicBlock *splitBlockAfter(MachineInstr *MI, 2572 MachineBasicBlock *MBB) { 2573 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 2574 NewMBB->splice(NewMBB->begin(), MBB, 2575 llvm::next(MachineBasicBlock::iterator(MI)), 2576 MBB->end()); 2577 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 2578 return NewMBB; 2579 } 2580 2581 // Split MBB before MI and return the new block (the one that contains MI). 2582 static MachineBasicBlock *splitBlockBefore(MachineInstr *MI, 2583 MachineBasicBlock *MBB) { 2584 MachineBasicBlock *NewMBB = emitBlockAfter(MBB); 2585 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end()); 2586 NewMBB->transferSuccessorsAndUpdatePHIs(MBB); 2587 return NewMBB; 2588 } 2589 2590 // Force base value Base into a register before MI. Return the register. 2591 static unsigned forceReg(MachineInstr *MI, MachineOperand &Base, 2592 const SystemZInstrInfo *TII) { 2593 if (Base.isReg()) 2594 return Base.getReg(); 2595 2596 MachineBasicBlock *MBB = MI->getParent(); 2597 MachineFunction &MF = *MBB->getParent(); 2598 MachineRegisterInfo &MRI = MF.getRegInfo(); 2599 2600 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 2601 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg) 2602 .addOperand(Base).addImm(0).addReg(0); 2603 return Reg; 2604 } 2605 2606 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI. 2607 MachineBasicBlock * 2608 SystemZTargetLowering::emitSelect(MachineInstr *MI, 2609 MachineBasicBlock *MBB) const { 2610 const SystemZInstrInfo *TII = TM.getInstrInfo(); 2611 2612 unsigned DestReg = MI->getOperand(0).getReg(); 2613 unsigned TrueReg = MI->getOperand(1).getReg(); 2614 unsigned FalseReg = MI->getOperand(2).getReg(); 2615 unsigned CCValid = MI->getOperand(3).getImm(); 2616 unsigned CCMask = MI->getOperand(4).getImm(); 2617 DebugLoc DL = MI->getDebugLoc(); 2618 2619 MachineBasicBlock *StartMBB = MBB; 2620 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); 2621 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); 2622 2623 // StartMBB: 2624 // BRC CCMask, JoinMBB 2625 // # fallthrough to FalseMBB 2626 MBB = StartMBB; 2627 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 2628 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 2629 MBB->addSuccessor(JoinMBB); 2630 MBB->addSuccessor(FalseMBB); 2631 2632 // FalseMBB: 2633 // # fallthrough to JoinMBB 2634 MBB = FalseMBB; 2635 MBB->addSuccessor(JoinMBB); 2636 2637 // JoinMBB: 2638 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ] 2639 // ... 2640 MBB = JoinMBB; 2641 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg) 2642 .addReg(TrueReg).addMBB(StartMBB) 2643 .addReg(FalseReg).addMBB(FalseMBB); 2644 2645 MI->eraseFromParent(); 2646 return JoinMBB; 2647 } 2648 2649 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI. 2650 // StoreOpcode is the store to use and Invert says whether the store should 2651 // happen when the condition is false rather than true. If a STORE ON 2652 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0. 2653 MachineBasicBlock * 2654 SystemZTargetLowering::emitCondStore(MachineInstr *MI, 2655 MachineBasicBlock *MBB, 2656 unsigned StoreOpcode, unsigned STOCOpcode, 2657 bool Invert) const { 2658 const SystemZInstrInfo *TII = TM.getInstrInfo(); 2659 2660 unsigned SrcReg = MI->getOperand(0).getReg(); 2661 MachineOperand Base = MI->getOperand(1); 2662 int64_t Disp = MI->getOperand(2).getImm(); 2663 unsigned IndexReg = MI->getOperand(3).getReg(); 2664 unsigned CCValid = MI->getOperand(4).getImm(); 2665 unsigned CCMask = MI->getOperand(5).getImm(); 2666 DebugLoc DL = MI->getDebugLoc(); 2667 2668 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp); 2669 2670 // Use STOCOpcode if possible. We could use different store patterns in 2671 // order to avoid matching the index register, but the performance trade-offs 2672 // might be more complicated in that case. 2673 if (STOCOpcode && !IndexReg && TM.getSubtargetImpl()->hasLoadStoreOnCond()) { 2674 if (Invert) 2675 CCMask ^= CCValid; 2676 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode)) 2677 .addReg(SrcReg).addOperand(Base).addImm(Disp) 2678 .addImm(CCValid).addImm(CCMask); 2679 MI->eraseFromParent(); 2680 return MBB; 2681 } 2682 2683 // Get the condition needed to branch around the store. 2684 if (!Invert) 2685 CCMask ^= CCValid; 2686 2687 MachineBasicBlock *StartMBB = MBB; 2688 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB); 2689 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB); 2690 2691 // StartMBB: 2692 // BRC CCMask, JoinMBB 2693 // # fallthrough to FalseMBB 2694 MBB = StartMBB; 2695 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 2696 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB); 2697 MBB->addSuccessor(JoinMBB); 2698 MBB->addSuccessor(FalseMBB); 2699 2700 // FalseMBB: 2701 // store %SrcReg, %Disp(%Index,%Base) 2702 // # fallthrough to JoinMBB 2703 MBB = FalseMBB; 2704 BuildMI(MBB, DL, TII->get(StoreOpcode)) 2705 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg); 2706 MBB->addSuccessor(JoinMBB); 2707 2708 MI->eraseFromParent(); 2709 return JoinMBB; 2710 } 2711 2712 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_* 2713 // or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that 2714 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}. 2715 // BitSize is the width of the field in bits, or 0 if this is a partword 2716 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize 2717 // is one of the operands. Invert says whether the field should be 2718 // inverted after performing BinOpcode (e.g. for NAND). 2719 MachineBasicBlock * 2720 SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI, 2721 MachineBasicBlock *MBB, 2722 unsigned BinOpcode, 2723 unsigned BitSize, 2724 bool Invert) const { 2725 const SystemZInstrInfo *TII = TM.getInstrInfo(); 2726 MachineFunction &MF = *MBB->getParent(); 2727 MachineRegisterInfo &MRI = MF.getRegInfo(); 2728 bool IsSubWord = (BitSize < 32); 2729 2730 // Extract the operands. Base can be a register or a frame index. 2731 // Src2 can be a register or immediate. 2732 unsigned Dest = MI->getOperand(0).getReg(); 2733 MachineOperand Base = earlyUseOperand(MI->getOperand(1)); 2734 int64_t Disp = MI->getOperand(2).getImm(); 2735 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3)); 2736 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); 2737 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); 2738 DebugLoc DL = MI->getDebugLoc(); 2739 if (IsSubWord) 2740 BitSize = MI->getOperand(6).getImm(); 2741 2742 // Subword operations use 32-bit registers. 2743 const TargetRegisterClass *RC = (BitSize <= 32 ? 2744 &SystemZ::GR32BitRegClass : 2745 &SystemZ::GR64BitRegClass); 2746 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 2747 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 2748 2749 // Get the right opcodes for the displacement. 2750 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 2751 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 2752 assert(LOpcode && CSOpcode && "Displacement out of range"); 2753 2754 // Create virtual registers for temporary results. 2755 unsigned OrigVal = MRI.createVirtualRegister(RC); 2756 unsigned OldVal = MRI.createVirtualRegister(RC); 2757 unsigned NewVal = (BinOpcode || IsSubWord ? 2758 MRI.createVirtualRegister(RC) : Src2.getReg()); 2759 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 2760 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 2761 2762 // Insert a basic block for the main loop. 2763 MachineBasicBlock *StartMBB = MBB; 2764 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 2765 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 2766 2767 // StartMBB: 2768 // ... 2769 // %OrigVal = L Disp(%Base) 2770 // # fall through to LoopMMB 2771 MBB = StartMBB; 2772 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) 2773 .addOperand(Base).addImm(Disp).addReg(0); 2774 MBB->addSuccessor(LoopMBB); 2775 2776 // LoopMBB: 2777 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ] 2778 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 2779 // %RotatedNewVal = OP %RotatedOldVal, %Src2 2780 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 2781 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 2782 // JNE LoopMBB 2783 // # fall through to DoneMMB 2784 MBB = LoopMBB; 2785 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 2786 .addReg(OrigVal).addMBB(StartMBB) 2787 .addReg(Dest).addMBB(LoopMBB); 2788 if (IsSubWord) 2789 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 2790 .addReg(OldVal).addReg(BitShift).addImm(0); 2791 if (Invert) { 2792 // Perform the operation normally and then invert every bit of the field. 2793 unsigned Tmp = MRI.createVirtualRegister(RC); 2794 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp) 2795 .addReg(RotatedOldVal).addOperand(Src2); 2796 if (BitSize < 32) 2797 // XILF with the upper BitSize bits set. 2798 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal) 2799 .addReg(Tmp).addImm(uint32_t(~0 << (32 - BitSize))); 2800 else if (BitSize == 32) 2801 // XILF with every bit set. 2802 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal) 2803 .addReg(Tmp).addImm(~uint32_t(0)); 2804 else { 2805 // Use LCGR and add -1 to the result, which is more compact than 2806 // an XILF, XILH pair. 2807 unsigned Tmp2 = MRI.createVirtualRegister(RC); 2808 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp); 2809 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal) 2810 .addReg(Tmp2).addImm(-1); 2811 } 2812 } else if (BinOpcode) 2813 // A simply binary operation. 2814 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal) 2815 .addReg(RotatedOldVal).addOperand(Src2); 2816 else if (IsSubWord) 2817 // Use RISBG to rotate Src2 into position and use it to replace the 2818 // field in RotatedOldVal. 2819 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal) 2820 .addReg(RotatedOldVal).addReg(Src2.getReg()) 2821 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize); 2822 if (IsSubWord) 2823 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 2824 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 2825 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 2826 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); 2827 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 2828 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 2829 MBB->addSuccessor(LoopMBB); 2830 MBB->addSuccessor(DoneMBB); 2831 2832 MI->eraseFromParent(); 2833 return DoneMBB; 2834 } 2835 2836 // Implement EmitInstrWithCustomInserter for pseudo 2837 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the 2838 // instruction that should be used to compare the current field with the 2839 // minimum or maximum value. KeepOldMask is the BRC condition-code mask 2840 // for when the current field should be kept. BitSize is the width of 2841 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction. 2842 MachineBasicBlock * 2843 SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI, 2844 MachineBasicBlock *MBB, 2845 unsigned CompareOpcode, 2846 unsigned KeepOldMask, 2847 unsigned BitSize) const { 2848 const SystemZInstrInfo *TII = TM.getInstrInfo(); 2849 MachineFunction &MF = *MBB->getParent(); 2850 MachineRegisterInfo &MRI = MF.getRegInfo(); 2851 bool IsSubWord = (BitSize < 32); 2852 2853 // Extract the operands. Base can be a register or a frame index. 2854 unsigned Dest = MI->getOperand(0).getReg(); 2855 MachineOperand Base = earlyUseOperand(MI->getOperand(1)); 2856 int64_t Disp = MI->getOperand(2).getImm(); 2857 unsigned Src2 = MI->getOperand(3).getReg(); 2858 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0); 2859 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0); 2860 DebugLoc DL = MI->getDebugLoc(); 2861 if (IsSubWord) 2862 BitSize = MI->getOperand(6).getImm(); 2863 2864 // Subword operations use 32-bit registers. 2865 const TargetRegisterClass *RC = (BitSize <= 32 ? 2866 &SystemZ::GR32BitRegClass : 2867 &SystemZ::GR64BitRegClass); 2868 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG; 2869 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG; 2870 2871 // Get the right opcodes for the displacement. 2872 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp); 2873 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp); 2874 assert(LOpcode && CSOpcode && "Displacement out of range"); 2875 2876 // Create virtual registers for temporary results. 2877 unsigned OrigVal = MRI.createVirtualRegister(RC); 2878 unsigned OldVal = MRI.createVirtualRegister(RC); 2879 unsigned NewVal = MRI.createVirtualRegister(RC); 2880 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal); 2881 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2); 2882 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal); 2883 2884 // Insert 3 basic blocks for the loop. 2885 MachineBasicBlock *StartMBB = MBB; 2886 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 2887 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 2888 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB); 2889 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB); 2890 2891 // StartMBB: 2892 // ... 2893 // %OrigVal = L Disp(%Base) 2894 // # fall through to LoopMMB 2895 MBB = StartMBB; 2896 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal) 2897 .addOperand(Base).addImm(Disp).addReg(0); 2898 MBB->addSuccessor(LoopMBB); 2899 2900 // LoopMBB: 2901 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ] 2902 // %RotatedOldVal = RLL %OldVal, 0(%BitShift) 2903 // CompareOpcode %RotatedOldVal, %Src2 2904 // BRC KeepOldMask, UpdateMBB 2905 MBB = LoopMBB; 2906 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 2907 .addReg(OrigVal).addMBB(StartMBB) 2908 .addReg(Dest).addMBB(UpdateMBB); 2909 if (IsSubWord) 2910 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal) 2911 .addReg(OldVal).addReg(BitShift).addImm(0); 2912 BuildMI(MBB, DL, TII->get(CompareOpcode)) 2913 .addReg(RotatedOldVal).addReg(Src2); 2914 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 2915 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB); 2916 MBB->addSuccessor(UpdateMBB); 2917 MBB->addSuccessor(UseAltMBB); 2918 2919 // UseAltMBB: 2920 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0 2921 // # fall through to UpdateMMB 2922 MBB = UseAltMBB; 2923 if (IsSubWord) 2924 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal) 2925 .addReg(RotatedOldVal).addReg(Src2) 2926 .addImm(32).addImm(31 + BitSize).addImm(0); 2927 MBB->addSuccessor(UpdateMBB); 2928 2929 // UpdateMBB: 2930 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ], 2931 // [ %RotatedAltVal, UseAltMBB ] 2932 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift) 2933 // %Dest = CS %OldVal, %NewVal, Disp(%Base) 2934 // JNE LoopMBB 2935 // # fall through to DoneMMB 2936 MBB = UpdateMBB; 2937 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal) 2938 .addReg(RotatedOldVal).addMBB(LoopMBB) 2939 .addReg(RotatedAltVal).addMBB(UseAltMBB); 2940 if (IsSubWord) 2941 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal) 2942 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0); 2943 BuildMI(MBB, DL, TII->get(CSOpcode), Dest) 2944 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp); 2945 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 2946 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 2947 MBB->addSuccessor(LoopMBB); 2948 MBB->addSuccessor(DoneMBB); 2949 2950 MI->eraseFromParent(); 2951 return DoneMBB; 2952 } 2953 2954 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW 2955 // instruction MI. 2956 MachineBasicBlock * 2957 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI, 2958 MachineBasicBlock *MBB) const { 2959 const SystemZInstrInfo *TII = TM.getInstrInfo(); 2960 MachineFunction &MF = *MBB->getParent(); 2961 MachineRegisterInfo &MRI = MF.getRegInfo(); 2962 2963 // Extract the operands. Base can be a register or a frame index. 2964 unsigned Dest = MI->getOperand(0).getReg(); 2965 MachineOperand Base = earlyUseOperand(MI->getOperand(1)); 2966 int64_t Disp = MI->getOperand(2).getImm(); 2967 unsigned OrigCmpVal = MI->getOperand(3).getReg(); 2968 unsigned OrigSwapVal = MI->getOperand(4).getReg(); 2969 unsigned BitShift = MI->getOperand(5).getReg(); 2970 unsigned NegBitShift = MI->getOperand(6).getReg(); 2971 int64_t BitSize = MI->getOperand(7).getImm(); 2972 DebugLoc DL = MI->getDebugLoc(); 2973 2974 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass; 2975 2976 // Get the right opcodes for the displacement. 2977 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp); 2978 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp); 2979 assert(LOpcode && CSOpcode && "Displacement out of range"); 2980 2981 // Create virtual registers for temporary results. 2982 unsigned OrigOldVal = MRI.createVirtualRegister(RC); 2983 unsigned OldVal = MRI.createVirtualRegister(RC); 2984 unsigned CmpVal = MRI.createVirtualRegister(RC); 2985 unsigned SwapVal = MRI.createVirtualRegister(RC); 2986 unsigned StoreVal = MRI.createVirtualRegister(RC); 2987 unsigned RetryOldVal = MRI.createVirtualRegister(RC); 2988 unsigned RetryCmpVal = MRI.createVirtualRegister(RC); 2989 unsigned RetrySwapVal = MRI.createVirtualRegister(RC); 2990 2991 // Insert 2 basic blocks for the loop. 2992 MachineBasicBlock *StartMBB = MBB; 2993 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 2994 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 2995 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB); 2996 2997 // StartMBB: 2998 // ... 2999 // %OrigOldVal = L Disp(%Base) 3000 // # fall through to LoopMMB 3001 MBB = StartMBB; 3002 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal) 3003 .addOperand(Base).addImm(Disp).addReg(0); 3004 MBB->addSuccessor(LoopMBB); 3005 3006 // LoopMBB: 3007 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ] 3008 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ] 3009 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ] 3010 // %Dest = RLL %OldVal, BitSize(%BitShift) 3011 // ^^ The low BitSize bits contain the field 3012 // of interest. 3013 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0 3014 // ^^ Replace the upper 32-BitSize bits of the 3015 // comparison value with those that we loaded, 3016 // so that we can use a full word comparison. 3017 // CR %Dest, %RetryCmpVal 3018 // JNE DoneMBB 3019 // # Fall through to SetMBB 3020 MBB = LoopMBB; 3021 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal) 3022 .addReg(OrigOldVal).addMBB(StartMBB) 3023 .addReg(RetryOldVal).addMBB(SetMBB); 3024 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal) 3025 .addReg(OrigCmpVal).addMBB(StartMBB) 3026 .addReg(RetryCmpVal).addMBB(SetMBB); 3027 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal) 3028 .addReg(OrigSwapVal).addMBB(StartMBB) 3029 .addReg(RetrySwapVal).addMBB(SetMBB); 3030 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest) 3031 .addReg(OldVal).addReg(BitShift).addImm(BitSize); 3032 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal) 3033 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 3034 BuildMI(MBB, DL, TII->get(SystemZ::CR)) 3035 .addReg(Dest).addReg(RetryCmpVal); 3036 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 3037 .addImm(SystemZ::CCMASK_ICMP) 3038 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB); 3039 MBB->addSuccessor(DoneMBB); 3040 MBB->addSuccessor(SetMBB); 3041 3042 // SetMBB: 3043 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0 3044 // ^^ Replace the upper 32-BitSize bits of the new 3045 // value with those that we loaded. 3046 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift) 3047 // ^^ Rotate the new field to its proper position. 3048 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base) 3049 // JNE LoopMBB 3050 // # fall through to ExitMMB 3051 MBB = SetMBB; 3052 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal) 3053 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0); 3054 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal) 3055 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize); 3056 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal) 3057 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp); 3058 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 3059 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB); 3060 MBB->addSuccessor(LoopMBB); 3061 MBB->addSuccessor(DoneMBB); 3062 3063 MI->eraseFromParent(); 3064 return DoneMBB; 3065 } 3066 3067 // Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true 3068 // if the high register of the GR128 value must be cleared or false if 3069 // it's "don't care". SubReg is subreg_l32 when extending a GR32 3070 // and subreg_l64 when extending a GR64. 3071 MachineBasicBlock * 3072 SystemZTargetLowering::emitExt128(MachineInstr *MI, 3073 MachineBasicBlock *MBB, 3074 bool ClearEven, unsigned SubReg) const { 3075 const SystemZInstrInfo *TII = TM.getInstrInfo(); 3076 MachineFunction &MF = *MBB->getParent(); 3077 MachineRegisterInfo &MRI = MF.getRegInfo(); 3078 DebugLoc DL = MI->getDebugLoc(); 3079 3080 unsigned Dest = MI->getOperand(0).getReg(); 3081 unsigned Src = MI->getOperand(1).getReg(); 3082 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 3083 3084 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128); 3085 if (ClearEven) { 3086 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass); 3087 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass); 3088 3089 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64) 3090 .addImm(0); 3091 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128) 3092 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64); 3093 In128 = NewIn128; 3094 } 3095 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest) 3096 .addReg(In128).addReg(Src).addImm(SubReg); 3097 3098 MI->eraseFromParent(); 3099 return MBB; 3100 } 3101 3102 MachineBasicBlock * 3103 SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI, 3104 MachineBasicBlock *MBB, 3105 unsigned Opcode) const { 3106 const SystemZInstrInfo *TII = TM.getInstrInfo(); 3107 MachineFunction &MF = *MBB->getParent(); 3108 MachineRegisterInfo &MRI = MF.getRegInfo(); 3109 DebugLoc DL = MI->getDebugLoc(); 3110 3111 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0)); 3112 uint64_t DestDisp = MI->getOperand(1).getImm(); 3113 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2)); 3114 uint64_t SrcDisp = MI->getOperand(3).getImm(); 3115 uint64_t Length = MI->getOperand(4).getImm(); 3116 3117 // When generating more than one CLC, all but the last will need to 3118 // branch to the end when a difference is found. 3119 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ? 3120 splitBlockAfter(MI, MBB) : 0); 3121 3122 // Check for the loop form, in which operand 5 is the trip count. 3123 if (MI->getNumExplicitOperands() > 5) { 3124 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase); 3125 3126 uint64_t StartCountReg = MI->getOperand(5).getReg(); 3127 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII); 3128 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg : 3129 forceReg(MI, DestBase, TII)); 3130 3131 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass; 3132 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC); 3133 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg : 3134 MRI.createVirtualRegister(RC)); 3135 uint64_t NextSrcReg = MRI.createVirtualRegister(RC); 3136 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg : 3137 MRI.createVirtualRegister(RC)); 3138 3139 RC = &SystemZ::GR64BitRegClass; 3140 uint64_t ThisCountReg = MRI.createVirtualRegister(RC); 3141 uint64_t NextCountReg = MRI.createVirtualRegister(RC); 3142 3143 MachineBasicBlock *StartMBB = MBB; 3144 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 3145 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 3146 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB); 3147 3148 // StartMBB: 3149 // # fall through to LoopMMB 3150 MBB->addSuccessor(LoopMBB); 3151 3152 // LoopMBB: 3153 // %ThisDestReg = phi [ %StartDestReg, StartMBB ], 3154 // [ %NextDestReg, NextMBB ] 3155 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ], 3156 // [ %NextSrcReg, NextMBB ] 3157 // %ThisCountReg = phi [ %StartCountReg, StartMBB ], 3158 // [ %NextCountReg, NextMBB ] 3159 // ( PFD 2, 768+DestDisp(%ThisDestReg) ) 3160 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg) 3161 // ( JLH EndMBB ) 3162 // 3163 // The prefetch is used only for MVC. The JLH is used only for CLC. 3164 MBB = LoopMBB; 3165 3166 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg) 3167 .addReg(StartDestReg).addMBB(StartMBB) 3168 .addReg(NextDestReg).addMBB(NextMBB); 3169 if (!HaveSingleBase) 3170 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg) 3171 .addReg(StartSrcReg).addMBB(StartMBB) 3172 .addReg(NextSrcReg).addMBB(NextMBB); 3173 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg) 3174 .addReg(StartCountReg).addMBB(StartMBB) 3175 .addReg(NextCountReg).addMBB(NextMBB); 3176 if (Opcode == SystemZ::MVC) 3177 BuildMI(MBB, DL, TII->get(SystemZ::PFD)) 3178 .addImm(SystemZ::PFD_WRITE) 3179 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0); 3180 BuildMI(MBB, DL, TII->get(Opcode)) 3181 .addReg(ThisDestReg).addImm(DestDisp).addImm(256) 3182 .addReg(ThisSrcReg).addImm(SrcDisp); 3183 if (EndMBB) { 3184 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 3185 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 3186 .addMBB(EndMBB); 3187 MBB->addSuccessor(EndMBB); 3188 MBB->addSuccessor(NextMBB); 3189 } 3190 3191 // NextMBB: 3192 // %NextDestReg = LA 256(%ThisDestReg) 3193 // %NextSrcReg = LA 256(%ThisSrcReg) 3194 // %NextCountReg = AGHI %ThisCountReg, -1 3195 // CGHI %NextCountReg, 0 3196 // JLH LoopMBB 3197 // # fall through to DoneMMB 3198 // 3199 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes. 3200 MBB = NextMBB; 3201 3202 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg) 3203 .addReg(ThisDestReg).addImm(256).addReg(0); 3204 if (!HaveSingleBase) 3205 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg) 3206 .addReg(ThisSrcReg).addImm(256).addReg(0); 3207 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg) 3208 .addReg(ThisCountReg).addImm(-1); 3209 BuildMI(MBB, DL, TII->get(SystemZ::CGHI)) 3210 .addReg(NextCountReg).addImm(0); 3211 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 3212 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 3213 .addMBB(LoopMBB); 3214 MBB->addSuccessor(LoopMBB); 3215 MBB->addSuccessor(DoneMBB); 3216 3217 DestBase = MachineOperand::CreateReg(NextDestReg, false); 3218 SrcBase = MachineOperand::CreateReg(NextSrcReg, false); 3219 Length &= 255; 3220 MBB = DoneMBB; 3221 } 3222 // Handle any remaining bytes with straight-line code. 3223 while (Length > 0) { 3224 uint64_t ThisLength = std::min(Length, uint64_t(256)); 3225 // The previous iteration might have created out-of-range displacements. 3226 // Apply them using LAY if so. 3227 if (!isUInt<12>(DestDisp)) { 3228 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 3229 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) 3230 .addOperand(DestBase).addImm(DestDisp).addReg(0); 3231 DestBase = MachineOperand::CreateReg(Reg, false); 3232 DestDisp = 0; 3233 } 3234 if (!isUInt<12>(SrcDisp)) { 3235 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass); 3236 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg) 3237 .addOperand(SrcBase).addImm(SrcDisp).addReg(0); 3238 SrcBase = MachineOperand::CreateReg(Reg, false); 3239 SrcDisp = 0; 3240 } 3241 BuildMI(*MBB, MI, DL, TII->get(Opcode)) 3242 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength) 3243 .addOperand(SrcBase).addImm(SrcDisp); 3244 DestDisp += ThisLength; 3245 SrcDisp += ThisLength; 3246 Length -= ThisLength; 3247 // If there's another CLC to go, branch to the end if a difference 3248 // was found. 3249 if (EndMBB && Length > 0) { 3250 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB); 3251 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 3252 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE) 3253 .addMBB(EndMBB); 3254 MBB->addSuccessor(EndMBB); 3255 MBB->addSuccessor(NextMBB); 3256 MBB = NextMBB; 3257 } 3258 } 3259 if (EndMBB) { 3260 MBB->addSuccessor(EndMBB); 3261 MBB = EndMBB; 3262 MBB->addLiveIn(SystemZ::CC); 3263 } 3264 3265 MI->eraseFromParent(); 3266 return MBB; 3267 } 3268 3269 // Decompose string pseudo-instruction MI into a loop that continually performs 3270 // Opcode until CC != 3. 3271 MachineBasicBlock * 3272 SystemZTargetLowering::emitStringWrapper(MachineInstr *MI, 3273 MachineBasicBlock *MBB, 3274 unsigned Opcode) const { 3275 const SystemZInstrInfo *TII = TM.getInstrInfo(); 3276 MachineFunction &MF = *MBB->getParent(); 3277 MachineRegisterInfo &MRI = MF.getRegInfo(); 3278 DebugLoc DL = MI->getDebugLoc(); 3279 3280 uint64_t End1Reg = MI->getOperand(0).getReg(); 3281 uint64_t Start1Reg = MI->getOperand(1).getReg(); 3282 uint64_t Start2Reg = MI->getOperand(2).getReg(); 3283 uint64_t CharReg = MI->getOperand(3).getReg(); 3284 3285 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass; 3286 uint64_t This1Reg = MRI.createVirtualRegister(RC); 3287 uint64_t This2Reg = MRI.createVirtualRegister(RC); 3288 uint64_t End2Reg = MRI.createVirtualRegister(RC); 3289 3290 MachineBasicBlock *StartMBB = MBB; 3291 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB); 3292 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB); 3293 3294 // StartMBB: 3295 // # fall through to LoopMMB 3296 MBB->addSuccessor(LoopMBB); 3297 3298 // LoopMBB: 3299 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ] 3300 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ] 3301 // R0L = %CharReg 3302 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L 3303 // JO LoopMBB 3304 // # fall through to DoneMMB 3305 // 3306 // The load of R0L can be hoisted by post-RA LICM. 3307 MBB = LoopMBB; 3308 3309 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg) 3310 .addReg(Start1Reg).addMBB(StartMBB) 3311 .addReg(End1Reg).addMBB(LoopMBB); 3312 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg) 3313 .addReg(Start2Reg).addMBB(StartMBB) 3314 .addReg(End2Reg).addMBB(LoopMBB); 3315 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg); 3316 BuildMI(MBB, DL, TII->get(Opcode)) 3317 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define) 3318 .addReg(This1Reg).addReg(This2Reg); 3319 BuildMI(MBB, DL, TII->get(SystemZ::BRC)) 3320 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB); 3321 MBB->addSuccessor(LoopMBB); 3322 MBB->addSuccessor(DoneMBB); 3323 3324 DoneMBB->addLiveIn(SystemZ::CC); 3325 3326 MI->eraseFromParent(); 3327 return DoneMBB; 3328 } 3329 3330 MachineBasicBlock *SystemZTargetLowering:: 3331 EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const { 3332 switch (MI->getOpcode()) { 3333 case SystemZ::Select32Mux: 3334 case SystemZ::Select32: 3335 case SystemZ::SelectF32: 3336 case SystemZ::Select64: 3337 case SystemZ::SelectF64: 3338 case SystemZ::SelectF128: 3339 return emitSelect(MI, MBB); 3340 3341 case SystemZ::CondStore8Mux: 3342 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false); 3343 case SystemZ::CondStore8MuxInv: 3344 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true); 3345 case SystemZ::CondStore16Mux: 3346 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false); 3347 case SystemZ::CondStore16MuxInv: 3348 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true); 3349 case SystemZ::CondStore8: 3350 return emitCondStore(MI, MBB, SystemZ::STC, 0, false); 3351 case SystemZ::CondStore8Inv: 3352 return emitCondStore(MI, MBB, SystemZ::STC, 0, true); 3353 case SystemZ::CondStore16: 3354 return emitCondStore(MI, MBB, SystemZ::STH, 0, false); 3355 case SystemZ::CondStore16Inv: 3356 return emitCondStore(MI, MBB, SystemZ::STH, 0, true); 3357 case SystemZ::CondStore32: 3358 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false); 3359 case SystemZ::CondStore32Inv: 3360 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true); 3361 case SystemZ::CondStore64: 3362 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false); 3363 case SystemZ::CondStore64Inv: 3364 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true); 3365 case SystemZ::CondStoreF32: 3366 return emitCondStore(MI, MBB, SystemZ::STE, 0, false); 3367 case SystemZ::CondStoreF32Inv: 3368 return emitCondStore(MI, MBB, SystemZ::STE, 0, true); 3369 case SystemZ::CondStoreF64: 3370 return emitCondStore(MI, MBB, SystemZ::STD, 0, false); 3371 case SystemZ::CondStoreF64Inv: 3372 return emitCondStore(MI, MBB, SystemZ::STD, 0, true); 3373 3374 case SystemZ::AEXT128_64: 3375 return emitExt128(MI, MBB, false, SystemZ::subreg_l64); 3376 case SystemZ::ZEXT128_32: 3377 return emitExt128(MI, MBB, true, SystemZ::subreg_l32); 3378 case SystemZ::ZEXT128_64: 3379 return emitExt128(MI, MBB, true, SystemZ::subreg_l64); 3380 3381 case SystemZ::ATOMIC_SWAPW: 3382 return emitAtomicLoadBinary(MI, MBB, 0, 0); 3383 case SystemZ::ATOMIC_SWAP_32: 3384 return emitAtomicLoadBinary(MI, MBB, 0, 32); 3385 case SystemZ::ATOMIC_SWAP_64: 3386 return emitAtomicLoadBinary(MI, MBB, 0, 64); 3387 3388 case SystemZ::ATOMIC_LOADW_AR: 3389 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0); 3390 case SystemZ::ATOMIC_LOADW_AFI: 3391 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0); 3392 case SystemZ::ATOMIC_LOAD_AR: 3393 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32); 3394 case SystemZ::ATOMIC_LOAD_AHI: 3395 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32); 3396 case SystemZ::ATOMIC_LOAD_AFI: 3397 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32); 3398 case SystemZ::ATOMIC_LOAD_AGR: 3399 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64); 3400 case SystemZ::ATOMIC_LOAD_AGHI: 3401 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64); 3402 case SystemZ::ATOMIC_LOAD_AGFI: 3403 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64); 3404 3405 case SystemZ::ATOMIC_LOADW_SR: 3406 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0); 3407 case SystemZ::ATOMIC_LOAD_SR: 3408 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32); 3409 case SystemZ::ATOMIC_LOAD_SGR: 3410 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64); 3411 3412 case SystemZ::ATOMIC_LOADW_NR: 3413 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0); 3414 case SystemZ::ATOMIC_LOADW_NILH: 3415 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0); 3416 case SystemZ::ATOMIC_LOAD_NR: 3417 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32); 3418 case SystemZ::ATOMIC_LOAD_NILL: 3419 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32); 3420 case SystemZ::ATOMIC_LOAD_NILH: 3421 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32); 3422 case SystemZ::ATOMIC_LOAD_NILF: 3423 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32); 3424 case SystemZ::ATOMIC_LOAD_NGR: 3425 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64); 3426 case SystemZ::ATOMIC_LOAD_NILL64: 3427 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64); 3428 case SystemZ::ATOMIC_LOAD_NILH64: 3429 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64); 3430 case SystemZ::ATOMIC_LOAD_NIHL64: 3431 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64); 3432 case SystemZ::ATOMIC_LOAD_NIHH64: 3433 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64); 3434 case SystemZ::ATOMIC_LOAD_NILF64: 3435 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64); 3436 case SystemZ::ATOMIC_LOAD_NIHF64: 3437 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64); 3438 3439 case SystemZ::ATOMIC_LOADW_OR: 3440 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0); 3441 case SystemZ::ATOMIC_LOADW_OILH: 3442 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0); 3443 case SystemZ::ATOMIC_LOAD_OR: 3444 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32); 3445 case SystemZ::ATOMIC_LOAD_OILL: 3446 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32); 3447 case SystemZ::ATOMIC_LOAD_OILH: 3448 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32); 3449 case SystemZ::ATOMIC_LOAD_OILF: 3450 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32); 3451 case SystemZ::ATOMIC_LOAD_OGR: 3452 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64); 3453 case SystemZ::ATOMIC_LOAD_OILL64: 3454 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64); 3455 case SystemZ::ATOMIC_LOAD_OILH64: 3456 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64); 3457 case SystemZ::ATOMIC_LOAD_OIHL64: 3458 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64); 3459 case SystemZ::ATOMIC_LOAD_OIHH64: 3460 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64); 3461 case SystemZ::ATOMIC_LOAD_OILF64: 3462 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64); 3463 case SystemZ::ATOMIC_LOAD_OIHF64: 3464 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64); 3465 3466 case SystemZ::ATOMIC_LOADW_XR: 3467 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0); 3468 case SystemZ::ATOMIC_LOADW_XILF: 3469 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0); 3470 case SystemZ::ATOMIC_LOAD_XR: 3471 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32); 3472 case SystemZ::ATOMIC_LOAD_XILF: 3473 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32); 3474 case SystemZ::ATOMIC_LOAD_XGR: 3475 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64); 3476 case SystemZ::ATOMIC_LOAD_XILF64: 3477 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64); 3478 case SystemZ::ATOMIC_LOAD_XIHF64: 3479 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64); 3480 3481 case SystemZ::ATOMIC_LOADW_NRi: 3482 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true); 3483 case SystemZ::ATOMIC_LOADW_NILHi: 3484 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true); 3485 case SystemZ::ATOMIC_LOAD_NRi: 3486 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true); 3487 case SystemZ::ATOMIC_LOAD_NILLi: 3488 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true); 3489 case SystemZ::ATOMIC_LOAD_NILHi: 3490 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true); 3491 case SystemZ::ATOMIC_LOAD_NILFi: 3492 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true); 3493 case SystemZ::ATOMIC_LOAD_NGRi: 3494 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true); 3495 case SystemZ::ATOMIC_LOAD_NILL64i: 3496 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true); 3497 case SystemZ::ATOMIC_LOAD_NILH64i: 3498 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true); 3499 case SystemZ::ATOMIC_LOAD_NIHL64i: 3500 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true); 3501 case SystemZ::ATOMIC_LOAD_NIHH64i: 3502 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true); 3503 case SystemZ::ATOMIC_LOAD_NILF64i: 3504 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true); 3505 case SystemZ::ATOMIC_LOAD_NIHF64i: 3506 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true); 3507 3508 case SystemZ::ATOMIC_LOADW_MIN: 3509 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 3510 SystemZ::CCMASK_CMP_LE, 0); 3511 case SystemZ::ATOMIC_LOAD_MIN_32: 3512 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 3513 SystemZ::CCMASK_CMP_LE, 32); 3514 case SystemZ::ATOMIC_LOAD_MIN_64: 3515 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 3516 SystemZ::CCMASK_CMP_LE, 64); 3517 3518 case SystemZ::ATOMIC_LOADW_MAX: 3519 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 3520 SystemZ::CCMASK_CMP_GE, 0); 3521 case SystemZ::ATOMIC_LOAD_MAX_32: 3522 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR, 3523 SystemZ::CCMASK_CMP_GE, 32); 3524 case SystemZ::ATOMIC_LOAD_MAX_64: 3525 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR, 3526 SystemZ::CCMASK_CMP_GE, 64); 3527 3528 case SystemZ::ATOMIC_LOADW_UMIN: 3529 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 3530 SystemZ::CCMASK_CMP_LE, 0); 3531 case SystemZ::ATOMIC_LOAD_UMIN_32: 3532 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 3533 SystemZ::CCMASK_CMP_LE, 32); 3534 case SystemZ::ATOMIC_LOAD_UMIN_64: 3535 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 3536 SystemZ::CCMASK_CMP_LE, 64); 3537 3538 case SystemZ::ATOMIC_LOADW_UMAX: 3539 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 3540 SystemZ::CCMASK_CMP_GE, 0); 3541 case SystemZ::ATOMIC_LOAD_UMAX_32: 3542 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR, 3543 SystemZ::CCMASK_CMP_GE, 32); 3544 case SystemZ::ATOMIC_LOAD_UMAX_64: 3545 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR, 3546 SystemZ::CCMASK_CMP_GE, 64); 3547 3548 case SystemZ::ATOMIC_CMP_SWAPW: 3549 return emitAtomicCmpSwapW(MI, MBB); 3550 case SystemZ::MVCSequence: 3551 case SystemZ::MVCLoop: 3552 return emitMemMemWrapper(MI, MBB, SystemZ::MVC); 3553 case SystemZ::NCSequence: 3554 case SystemZ::NCLoop: 3555 return emitMemMemWrapper(MI, MBB, SystemZ::NC); 3556 case SystemZ::OCSequence: 3557 case SystemZ::OCLoop: 3558 return emitMemMemWrapper(MI, MBB, SystemZ::OC); 3559 case SystemZ::XCSequence: 3560 case SystemZ::XCLoop: 3561 return emitMemMemWrapper(MI, MBB, SystemZ::XC); 3562 case SystemZ::CLCSequence: 3563 case SystemZ::CLCLoop: 3564 return emitMemMemWrapper(MI, MBB, SystemZ::CLC); 3565 case SystemZ::CLSTLoop: 3566 return emitStringWrapper(MI, MBB, SystemZ::CLST); 3567 case SystemZ::MVSTLoop: 3568 return emitStringWrapper(MI, MBB, SystemZ::MVST); 3569 case SystemZ::SRSTLoop: 3570 return emitStringWrapper(MI, MBB, SystemZ::SRST); 3571 default: 3572 llvm_unreachable("Unexpected instr type to insert"); 3573 } 3574 } 3575