LLVM 23.0.0git
SystemZInstrInfo.cpp
Go to the documentation of this file.
1//===-- SystemZInstrInfo.cpp - SystemZ instruction information ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the SystemZ implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "SystemZInstrInfo.h"
15#include "SystemZ.h"
16#include "SystemZInstrBuilder.h"
17#include "SystemZSubtarget.h"
18#include "llvm/ADT/Statistic.h"
37#include "llvm/MC/MCInstrDesc.h"
43#include <cassert>
44#include <cstdint>
45#include <iterator>
46
47using namespace llvm;
48
49#define GET_INSTRINFO_CTOR_DTOR
50#define GET_INSTRMAP_INFO
51#include "SystemZGenInstrInfo.inc"
52
53#define DEBUG_TYPE "systemz-II"
54
55// Return a mask with Count low bits set.
56static uint64_t allOnes(unsigned int Count) {
57 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
58}
59
60// Pin the vtable to this file.
61void SystemZInstrInfo::anchor() {}
62
64 : SystemZGenInstrInfo(sti, RI, -1, -1),
65 RI(sti.getSpecialRegisters()->getReturnFunctionAddressRegister(),
66 sti.getHwMode()),
67 STI(sti) {}
68
69// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
70// each having the opcode given by NewOpcode.
71void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
72 unsigned NewOpcode) const {
73 MachineBasicBlock *MBB = MI->getParent();
74 MachineFunction &MF = *MBB->getParent();
75
76 // Get two load or store instructions. Use the original instruction for
77 // one of them and create a clone for the other.
78 MachineInstr *HighPartMI = MF.CloneMachineInstr(&*MI);
79 MachineInstr *LowPartMI = &*MI;
80 MBB->insert(LowPartMI, HighPartMI);
81
82 // Set up the two 64-bit registers and remember super reg and its flags.
83 MachineOperand &HighRegOp = HighPartMI->getOperand(0);
84 MachineOperand &LowRegOp = LowPartMI->getOperand(0);
85 Register Reg128 = LowRegOp.getReg();
86 RegState Reg128Killed = getKillRegState(LowRegOp.isKill());
87 RegState Reg128Undef = getUndefRegState(LowRegOp.isUndef());
88 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64));
89 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64));
90
91 // The address in the first (high) instruction is already correct.
92 // Adjust the offset in the second (low) instruction.
93 MachineOperand &HighOffsetOp = HighPartMI->getOperand(2);
94 MachineOperand &LowOffsetOp = LowPartMI->getOperand(2);
95 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
96
97 // Set the opcodes.
98 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
99 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
100 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
101 HighPartMI->setDesc(get(HighOpcode));
102 LowPartMI->setDesc(get(LowOpcode));
103
104 MachineInstr *FirstMI = HighPartMI;
105 if (MI->mayStore()) {
106 FirstMI->getOperand(0).setIsKill(false);
107 // Add implicit uses of the super register in case one of the subregs is
108 // undefined. We could track liveness and skip storing an undefined
109 // subreg, but this is hopefully rare (discovered with llvm-stress).
110 // If Reg128 was killed, set kill flag on MI.
111 RegState Reg128UndefImpl = (Reg128Undef | RegState::Implicit);
112 MachineInstrBuilder(MF, HighPartMI).addReg(Reg128, Reg128UndefImpl);
113 MachineInstrBuilder(MF, LowPartMI).addReg(Reg128, (Reg128UndefImpl | Reg128Killed));
114 } else {
115 // If HighPartMI clobbers any of the address registers, it needs to come
116 // after LowPartMI.
117 auto overlapsAddressReg = [&](Register Reg) -> bool {
118 return RI.regsOverlap(Reg, MI->getOperand(1).getReg()) ||
119 RI.regsOverlap(Reg, MI->getOperand(3).getReg());
120 };
121 if (overlapsAddressReg(HighRegOp.getReg())) {
122 assert(!overlapsAddressReg(LowRegOp.getReg()) &&
123 "Both loads clobber address!");
124 MBB->splice(HighPartMI, MBB, LowPartMI);
125 FirstMI = LowPartMI;
126 }
127 }
128
129 // Clear the kill flags on the address registers in the first instruction.
130 FirstMI->getOperand(1).setIsKill(false);
131 FirstMI->getOperand(3).setIsKill(false);
132}
133
134// Split ADJDYNALLOC instruction MI.
135void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
136 MachineBasicBlock *MBB = MI->getParent();
137 MachineFunction &MF = *MBB->getParent();
138 MachineFrameInfo &MFFrame = MF.getFrameInfo();
139 MachineOperand &OffsetMO = MI->getOperand(2);
140 SystemZCallingConventionRegisters *Regs = STI.getSpecialRegisters();
141
142 uint64_t Offset = (MFFrame.getMaxCallFrameSize() +
143 Regs->getCallFrameSize() +
144 Regs->getStackPointerBias() +
145 OffsetMO.getImm());
146 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
147 assert(NewOpcode && "No support for huge argument lists yet");
148 MI->setDesc(get(NewOpcode));
149 OffsetMO.setImm(Offset);
150}
151
152// MI is an RI-style pseudo instruction. Replace it with LowOpcode
153// if the first operand is a low GR32 and HighOpcode if the first operand
154// is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
155// and HighOpcode takes an unsigned 32-bit operand. In those cases,
156// MI has the same kind of operand as LowOpcode, so needs to be converted
157// if HighOpcode is used.
158void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode,
159 unsigned HighOpcode,
160 bool ConvertHigh) const {
161 Register Reg = MI.getOperand(0).getReg();
162 bool IsHigh = SystemZ::isHighReg(Reg);
163 MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode));
164 if (IsHigh && ConvertHigh)
165 MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm()));
166}
167
168// MI is a three-operand RIE-style pseudo instruction. Replace it with
169// LowOpcodeK if the registers are both low GR32s, otherwise use a move
170// followed by HighOpcode or LowOpcode, depending on whether the target
171// is a high or low GR32.
172void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
173 unsigned LowOpcodeK,
174 unsigned HighOpcode) const {
175 Register DestReg = MI.getOperand(0).getReg();
176 Register SrcReg = MI.getOperand(1).getReg();
177 bool DestIsHigh = SystemZ::isHighReg(DestReg);
178 bool SrcIsHigh = SystemZ::isHighReg(SrcReg);
179 if (!DestIsHigh && !SrcIsHigh)
180 MI.setDesc(get(LowOpcodeK));
181 else {
182 if (DestReg != SrcReg) {
183 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg,
184 SystemZ::LR, 32, MI.getOperand(1).isKill(),
185 MI.getOperand(1).isUndef());
186 MI.getOperand(1).setReg(DestReg);
187 }
188 MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
189 MI.tieOperands(0, 1);
190 }
191}
192
193// MI is an RXY-style pseudo instruction. Replace it with LowOpcode
194// if the first operand is a low GR32 and HighOpcode if the first operand
195// is a high GR32.
196void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
197 unsigned HighOpcode) const {
198 Register Reg = MI.getOperand(0).getReg();
199 unsigned Opcode = getOpcodeForOffset(
200 SystemZ::isHighReg(Reg) ? HighOpcode : LowOpcode,
201 MI.getOperand(2).getImm());
202 MI.setDesc(get(Opcode));
203}
204
205// MI is a load-on-condition pseudo instruction with a single register
206// (source or destination) operand. Replace it with LowOpcode if the
207// register is a low GR32 and HighOpcode if the register is a high GR32.
208void SystemZInstrInfo::expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode,
209 unsigned HighOpcode) const {
210 Register Reg = MI.getOperand(0).getReg();
211 unsigned Opcode = SystemZ::isHighReg(Reg) ? HighOpcode : LowOpcode;
212 MI.setDesc(get(Opcode));
213}
214
215// MI is an RR-style pseudo instruction that zero-extends the low Size bits
216// of one GRX32 into another. Replace it with LowOpcode if both operands
217// are low registers, otherwise use RISB[LH]G.
218void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
219 unsigned Size) const {
220 MachineInstrBuilder MIB =
221 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(),
222 MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode,
223 Size, MI.getOperand(1).isKill(), MI.getOperand(1).isUndef());
224
225 // Keep the remaining operands as-is.
226 for (const MachineOperand &MO : llvm::drop_begin(MI.operands(), 2))
227 MIB.add(MO);
228
229 MI.eraseFromParent();
230}
231
232void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const {
233 MachineBasicBlock *MBB = MI->getParent();
234 MachineFunction &MF = *MBB->getParent();
235 const Register Reg64 = MI->getOperand(0).getReg();
236 const Register Reg32 = RI.getSubReg(Reg64, SystemZ::subreg_l32);
237
238 // EAR can only load the low subregister so us a shift for %a0 to produce
239 // the GR containing %a0 and %a1.
240
241 // ear <reg>, %a0
242 BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::EAR), Reg32)
243 .addReg(SystemZ::A0)
245
246 // sllg <reg>, <reg>, 32
247 BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::SLLG), Reg64)
248 .addReg(Reg64)
249 .addReg(0)
250 .addImm(32);
251
252 // ear <reg>, %a1
253 BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::EAR), Reg32)
254 .addReg(SystemZ::A1);
255
256 // lg <reg>, 40(<reg>)
257 MI->setDesc(get(SystemZ::LG));
258 MachineInstrBuilder(MF, MI).addReg(Reg64).addImm(40).addReg(0);
259}
260
261// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
262// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
263// are low registers, otherwise use RISB[LH]G. Size is the number of bits
264// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
265// KillSrc is true if this move is the last use of SrcReg.
267SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
269 const DebugLoc &DL, unsigned DestReg,
270 unsigned SrcReg, unsigned LowLowOpcode,
271 unsigned Size, bool KillSrc,
272 bool UndefSrc) const {
273 unsigned Opcode;
274 bool DestIsHigh = SystemZ::isHighReg(DestReg);
275 bool SrcIsHigh = SystemZ::isHighReg(SrcReg);
276 if (DestIsHigh && SrcIsHigh)
277 Opcode = SystemZ::RISBHH;
278 else if (DestIsHigh && !SrcIsHigh)
279 Opcode = SystemZ::RISBHL;
280 else if (!DestIsHigh && SrcIsHigh)
281 Opcode = SystemZ::RISBLH;
282 else {
283 return BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
284 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc));
285 }
286 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
287 return BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
288 .addReg(DestReg, RegState::Undef)
289 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc))
290 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
291}
292
294 bool NewMI,
295 unsigned OpIdx1,
296 unsigned OpIdx2) const {
297 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & {
298 if (NewMI)
299 return *MI.getParent()->getParent()->CloneMachineInstr(&MI);
300 return MI;
301 };
302
303 switch (MI.getOpcode()) {
304 case SystemZ::SELRMux:
305 case SystemZ::SELFHR:
306 case SystemZ::SELR:
307 case SystemZ::SELGR:
308 case SystemZ::LOCRMux:
309 case SystemZ::LOCFHR:
310 case SystemZ::LOCR:
311 case SystemZ::LOCGR: {
312 auto &WorkingMI = cloneIfNew(MI);
313 // Invert condition.
314 unsigned CCValid = WorkingMI.getOperand(3).getImm();
315 unsigned CCMask = WorkingMI.getOperand(4).getImm();
316 WorkingMI.getOperand(4).setImm(CCMask ^ CCValid);
317 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
318 OpIdx1, OpIdx2);
319 }
320 default:
321 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
322 }
323}
324
325// If MI is a simple load or store for a frame object, return the register
326// it loads or stores and set FrameIndex to the index of the frame object.
327// Return 0 otherwise.
328//
329// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
330static int isSimpleMove(const MachineInstr &MI, int &FrameIndex,
331 unsigned Flag) {
332 const MCInstrDesc &MCID = MI.getDesc();
333 if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() &&
334 MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) {
335 FrameIndex = MI.getOperand(1).getIndex();
336 return MI.getOperand(0).getReg();
337 }
338 return 0;
339}
340
342 int &FrameIndex) const {
343 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
344}
345
347 int &FrameIndex) const {
348 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
349}
350
352 int &FrameIndex) const {
353 // if this is not a simple load from memory, it's not a load from stack slot
354 // either.
355 const MCInstrDesc &MCID = MI.getDesc();
356 if (!(MCID.TSFlags & SystemZII::SimpleBDXLoad))
357 return 0;
358
359 // This version of isLoadFromStackSlot should only be used post frame-index
360 // elimination.
361 assert(!MI.getOperand(1).isFI());
362
363 // Now attempt to derive frame index from MachineMemOperands.
365 if (hasLoadFromStackSlot(MI, Accesses)) {
366 FrameIndex =
367 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
368 ->getFrameIndex();
369 return MI.getOperand(0).getReg();
370 }
371 return 0;
372}
373
375 int &FrameIndex) const {
376 // if this is not a simple store to memory, it's not a store to stack slot
377 // either.
378 const MCInstrDesc &MCID = MI.getDesc();
379 if (!(MCID.TSFlags & SystemZII::SimpleBDXStore))
380 return 0;
381
382 // This version of isStoreToStackSlot should only be used post frame-index
383 // elimination.
384 assert(!MI.getOperand(1).isFI());
385
386 // Now attempt to derive frame index from MachineMemOperands.
388 if (hasStoreToStackSlot(MI, Accesses)) {
389 FrameIndex =
390 cast<FixedStackPseudoSourceValue>(Accesses.front()->getPseudoValue())
391 ->getFrameIndex();
392 return MI.getOperand(0).getReg();
393 }
394 return 0;
395}
396
398 int &DestFrameIndex,
399 int &SrcFrameIndex) const {
400 // Check for MVC 0(Length,FI1),0(FI2)
401 const MachineFrameInfo &MFI = MI.getParent()->getParent()->getFrameInfo();
402 if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() ||
403 MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() ||
404 MI.getOperand(4).getImm() != 0)
405 return false;
406
407 // Check that Length covers the full slots.
408 int64_t Length = MI.getOperand(2).getImm();
409 unsigned FI1 = MI.getOperand(0).getIndex();
410 unsigned FI2 = MI.getOperand(3).getIndex();
411 if (MFI.getObjectSize(FI1) != Length ||
412 MFI.getObjectSize(FI2) != Length)
413 return false;
414
415 DestFrameIndex = FI1;
416 SrcFrameIndex = FI2;
417 return true;
418}
419
422 MachineBasicBlock *&FBB,
424 bool AllowModify) const {
425 // Most of the code and comments here are boilerplate.
426
427 // Start from the bottom of the block and work up, examining the
428 // terminator instructions.
430 while (I != MBB.begin()) {
431 --I;
432 if (I->isDebugInstr())
433 continue;
434
435 // Working from the bottom, when we see a non-terminator instruction, we're
436 // done.
437 if (!isUnpredicatedTerminator(*I))
438 break;
439
440 // A terminator that isn't a branch can't easily be handled by this
441 // analysis.
442 if (!I->isBranch())
443 return true;
444
445 // Can't handle indirect branches.
447 if (!Branch.hasMBBTarget())
448 return true;
449
450 // Punt on compound branches.
451 if (Branch.Type != SystemZII::BranchNormal)
452 return true;
453
454 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
455 // Handle unconditional branches.
456 if (!AllowModify) {
457 TBB = Branch.getMBBTarget();
458 continue;
459 }
460
461 // If the block has any instructions after a JMP, delete them.
462 MBB.erase(std::next(I), MBB.end());
463
464 Cond.clear();
465 FBB = nullptr;
466
467 // Delete the JMP if it's equivalent to a fall-through.
468 if (MBB.isLayoutSuccessor(Branch.getMBBTarget())) {
469 TBB = nullptr;
470 I->eraseFromParent();
471 I = MBB.end();
472 continue;
473 }
474
475 // TBB is used to indicate the unconditinal destination.
476 TBB = Branch.getMBBTarget();
477 continue;
478 }
479
480 // Working from the bottom, handle the first conditional branch.
481 if (Cond.empty()) {
482 // FIXME: add X86-style branch swap
483 FBB = TBB;
484 TBB = Branch.getMBBTarget();
485 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
486 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
487 continue;
488 }
489
490 // Handle subsequent conditional branches.
491 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
492
493 // Only handle the case where all conditional branches branch to the same
494 // destination.
495 if (TBB != Branch.getMBBTarget())
496 return true;
497
498 // If the conditions are the same, we can leave them alone.
499 unsigned OldCCValid = Cond[0].getImm();
500 unsigned OldCCMask = Cond[1].getImm();
501 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
502 continue;
503
504 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
505 return false;
506 }
507
508 return false;
509}
510
512 int *BytesRemoved) const {
513 assert(!BytesRemoved && "code size not handled");
514
515 // Most of the code and comments here are boilerplate.
517 unsigned Count = 0;
518
519 while (I != MBB.begin()) {
520 --I;
521 if (I->isDebugInstr())
522 continue;
523 if (!I->isBranch())
524 break;
525 if (!getBranchInfo(*I).hasMBBTarget())
526 break;
527 // Remove the branch.
528 I->eraseFromParent();
529 I = MBB.end();
530 ++Count;
531 }
532
533 return Count;
534}
535
538 assert(Cond.size() == 2 && "Invalid condition");
539 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
540 return false;
541}
542
547 const DebugLoc &DL,
548 int *BytesAdded) const {
549 // In this function we output 32-bit branches, which should always
550 // have enough range. They can be shortened and relaxed by later code
551 // in the pipeline, if desired.
552
553 // Shouldn't be a fall through.
554 assert(TBB && "insertBranch must not be told to insert a fallthrough");
555 assert((Cond.size() == 2 || Cond.size() == 0) &&
556 "SystemZ branch conditions have one component!");
557 assert(!BytesAdded && "code size not handled");
558
559 if (Cond.empty()) {
560 // Unconditional branch?
561 assert(!FBB && "Unconditional branch with multiple successors!");
562 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
563 return 1;
564 }
565
566 // Conditional branch.
567 unsigned Count = 0;
568 unsigned CCValid = Cond[0].getImm();
569 unsigned CCMask = Cond[1].getImm();
570 BuildMI(&MBB, DL, get(SystemZ::BRC))
571 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
572 ++Count;
573
574 if (FBB) {
575 // Two-way Conditional branch. Insert the second branch.
576 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
577 ++Count;
578 }
579 return Count;
580}
581
583 Register &SrcReg2, int64_t &Mask,
584 int64_t &Value) const {
585 assert(MI.isCompare() && "Caller should have checked for a comparison");
586
587 if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() &&
588 MI.getOperand(1).isImm()) {
589 SrcReg = MI.getOperand(0).getReg();
590 SrcReg2 = 0;
591 Value = MI.getOperand(1).getImm();
592 Mask = ~0;
593 return true;
594 }
595
596 return false;
597}
598
601 Register DstReg, Register TrueReg,
602 Register FalseReg, int &CondCycles,
603 int &TrueCycles,
604 int &FalseCycles) const {
605 // Not all subtargets have LOCR instructions.
606 if (!STI.hasLoadStoreOnCond())
607 return false;
608 if (Pred.size() != 2)
609 return false;
610
611 // Check register classes.
612 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
613 const TargetRegisterClass *RC =
614 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
615 if (!RC)
616 return false;
617
618 // We have LOCR instructions for 32 and 64 bit general purpose registers.
619 if ((STI.hasLoadStoreOnCond2() &&
620 SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) ||
621 SystemZ::GR32BitRegClass.hasSubClassEq(RC) ||
622 SystemZ::GR64BitRegClass.hasSubClassEq(RC)) {
623 CondCycles = 2;
624 TrueCycles = 2;
625 FalseCycles = 2;
626 return true;
627 }
628
629 // Can't do anything else.
630 return false;
631}
632
635 const DebugLoc &DL, Register DstReg,
637 Register TrueReg,
638 Register FalseReg) const {
639 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
640 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
641
642 assert(Pred.size() == 2 && "Invalid condition");
643 unsigned CCValid = Pred[0].getImm();
644 unsigned CCMask = Pred[1].getImm();
645
646 unsigned Opc;
647 if (SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) {
648 if (STI.hasMiscellaneousExtensions3())
649 Opc = SystemZ::SELRMux;
650 else if (STI.hasLoadStoreOnCond2())
651 Opc = SystemZ::LOCRMux;
652 else {
653 Opc = SystemZ::LOCR;
654 MRI.constrainRegClass(DstReg, &SystemZ::GR32BitRegClass);
655 Register TReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
656 Register FReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
657 BuildMI(MBB, I, DL, get(TargetOpcode::COPY), TReg).addReg(TrueReg);
658 BuildMI(MBB, I, DL, get(TargetOpcode::COPY), FReg).addReg(FalseReg);
659 TrueReg = TReg;
660 FalseReg = FReg;
661 }
662 } else if (SystemZ::GR64BitRegClass.hasSubClassEq(RC)) {
663 if (STI.hasMiscellaneousExtensions3())
664 Opc = SystemZ::SELGR;
665 else
666 Opc = SystemZ::LOCGR;
667 } else
668 llvm_unreachable("Invalid register class");
669
670 BuildMI(MBB, I, DL, get(Opc), DstReg)
671 .addReg(FalseReg).addReg(TrueReg)
672 .addImm(CCValid).addImm(CCMask);
673}
674
676 Register Reg,
677 MachineRegisterInfo *MRI) const {
678 unsigned DefOpc = DefMI.getOpcode();
679
680 if (DefOpc == SystemZ::VGBM) {
681 int64_t ImmVal = DefMI.getOperand(1).getImm();
682 if (ImmVal != 0) // TODO: Handle other values
683 return false;
684
685 // Fold gr128 = COPY (vr128 VGBM imm)
686 //
687 // %tmp:gr64 = LGHI 0
688 // to gr128 = REG_SEQUENCE %tmp, %tmp
689 assert(DefMI.getOperand(0).getReg() == Reg);
690
691 if (!UseMI.isCopy())
692 return false;
693
694 Register CopyDstReg = UseMI.getOperand(0).getReg();
695 if (CopyDstReg.isVirtual() &&
696 MRI->getRegClass(CopyDstReg) == &SystemZ::GR128BitRegClass &&
697 MRI->hasOneNonDBGUse(Reg)) {
698 // TODO: Handle physical registers
699 // TODO: Handle gr64 uses with subregister indexes
700 // TODO: Should this multi-use cases?
701 Register TmpReg = MRI->createVirtualRegister(&SystemZ::GR64BitRegClass);
702 MachineBasicBlock &MBB = *UseMI.getParent();
703
704 loadImmediate(MBB, UseMI.getIterator(), TmpReg, ImmVal);
705
706 UseMI.setDesc(get(SystemZ::REG_SEQUENCE));
707 UseMI.getOperand(1).setReg(TmpReg);
708 MachineInstrBuilder(*MBB.getParent(), &UseMI)
709 .addImm(SystemZ::subreg_h64)
710 .addReg(TmpReg)
711 .addImm(SystemZ::subreg_l64);
712
713 if (MRI->use_nodbg_empty(Reg))
714 DefMI.eraseFromParent();
715 return true;
716 }
717
718 return false;
719 }
720
721 if (DefOpc != SystemZ::LHIMux && DefOpc != SystemZ::LHI &&
722 DefOpc != SystemZ::LGHI)
723 return false;
724 if (DefMI.getOperand(0).getReg() != Reg)
725 return false;
726 int32_t ImmVal = (int32_t)DefMI.getOperand(1).getImm();
727
728 unsigned UseOpc = UseMI.getOpcode();
729 unsigned NewUseOpc;
730 unsigned UseIdx;
731 int CommuteIdx = -1;
732 bool TieOps = false;
733 switch (UseOpc) {
734 case SystemZ::SELRMux:
735 TieOps = true;
736 [[fallthrough]];
737 case SystemZ::LOCRMux:
738 if (!STI.hasLoadStoreOnCond2())
739 return false;
740 NewUseOpc = SystemZ::LOCHIMux;
741 if (UseMI.getOperand(2).getReg() == Reg)
742 UseIdx = 2;
743 else if (UseMI.getOperand(1).getReg() == Reg)
744 UseIdx = 2, CommuteIdx = 1;
745 else
746 return false;
747 break;
748 case SystemZ::SELGR:
749 TieOps = true;
750 [[fallthrough]];
751 case SystemZ::LOCGR:
752 if (!STI.hasLoadStoreOnCond2())
753 return false;
754 NewUseOpc = SystemZ::LOCGHI;
755 if (UseMI.getOperand(2).getReg() == Reg)
756 UseIdx = 2;
757 else if (UseMI.getOperand(1).getReg() == Reg)
758 UseIdx = 2, CommuteIdx = 1;
759 else
760 return false;
761 break;
762 default:
763 return false;
764 }
765
766 if (CommuteIdx != -1)
767 if (!commuteInstruction(UseMI, false, CommuteIdx, UseIdx))
768 return false;
769
770 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
771 UseMI.setDesc(get(NewUseOpc));
772 if (TieOps)
773 UseMI.tieOperands(0, 1);
774 UseMI.getOperand(UseIdx).ChangeToImmediate(ImmVal);
775 if (DeleteDef)
776 DefMI.eraseFromParent();
777
778 return true;
779}
780
782 unsigned Opcode = MI.getOpcode();
783 if (Opcode == SystemZ::Return ||
784 Opcode == SystemZ::Return_XPLINK ||
785 Opcode == SystemZ::Trap ||
786 Opcode == SystemZ::CallJG ||
787 Opcode == SystemZ::CallBR)
788 return true;
789 return false;
790}
791
794 unsigned NumCycles, unsigned ExtraPredCycles,
795 BranchProbability Probability) const {
796 // Avoid using conditional returns at the end of a loop (since then
797 // we'd need to emit an unconditional branch to the beginning anyway,
798 // making the loop body longer). This doesn't apply for low-probability
799 // loops (eg. compare-and-swap retry), so just decide based on branch
800 // probability instead of looping structure.
801 // However, since Compare and Trap instructions cost the same as a regular
802 // Compare instruction, we should allow the if conversion to convert this
803 // into a Conditional Compare regardless of the branch probability.
804 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap &&
805 MBB.succ_empty() && Probability < BranchProbability(1, 8))
806 return false;
807 // For now only convert single instructions.
808 return NumCycles == 1;
809}
810
813 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
814 MachineBasicBlock &FMBB,
815 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
816 BranchProbability Probability) const {
817 // For now avoid converting mutually-exclusive cases.
818 return false;
819}
820
823 BranchProbability Probability) const {
824 // For now only duplicate single instructions.
825 return NumCycles == 1;
826}
827
830 assert(Pred.size() == 2 && "Invalid condition");
831 unsigned CCValid = Pred[0].getImm();
832 unsigned CCMask = Pred[1].getImm();
833 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
834 unsigned Opcode = MI.getOpcode();
835 if (Opcode == SystemZ::Trap) {
836 MI.setDesc(get(SystemZ::CondTrap));
837 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
838 .addImm(CCValid).addImm(CCMask)
839 .addReg(SystemZ::CC, RegState::Implicit);
840 return true;
841 }
842 if (Opcode == SystemZ::Return || Opcode == SystemZ::Return_XPLINK) {
843 MI.setDesc(get(Opcode == SystemZ::Return ? SystemZ::CondReturn
844 : SystemZ::CondReturn_XPLINK));
845 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
846 .addImm(CCValid)
847 .addImm(CCMask)
848 .addReg(SystemZ::CC, RegState::Implicit);
849 return true;
850 }
851 if (Opcode == SystemZ::CallJG) {
852 MachineOperand FirstOp = MI.getOperand(0);
853 const uint32_t *RegMask = MI.getOperand(1).getRegMask();
854 MI.removeOperand(1);
855 MI.removeOperand(0);
856 MI.setDesc(get(SystemZ::CallBRCL));
857 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
858 .addImm(CCValid)
859 .addImm(CCMask)
860 .add(FirstOp)
861 .addRegMask(RegMask)
862 .addReg(SystemZ::CC, RegState::Implicit);
863 return true;
864 }
865 if (Opcode == SystemZ::CallBR) {
866 MachineOperand Target = MI.getOperand(0);
867 const uint32_t *RegMask = MI.getOperand(1).getRegMask();
868 MI.removeOperand(1);
869 MI.removeOperand(0);
870 MI.setDesc(get(SystemZ::CallBCR));
871 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
872 .addImm(CCValid).addImm(CCMask)
873 .add(Target)
874 .addRegMask(RegMask)
875 .addReg(SystemZ::CC, RegState::Implicit);
876 return true;
877 }
878 return false;
879}
880
883 const DebugLoc &DL, Register DestReg,
884 Register SrcReg, bool KillSrc,
885 bool RenamableDest,
886 bool RenamableSrc) const {
887 // Split 128-bit GPR moves into two 64-bit moves. Add implicit uses of the
888 // super register in case one of the subregs is undefined.
889 // This handles ADDR128 too.
890 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
891 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
892 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
893 MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI))
894 .addReg(SrcReg, RegState::Implicit);
895 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
896 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
897 MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI))
898 .addReg(SrcReg, (getKillRegState(KillSrc) | RegState::Implicit));
899 return;
900 }
901
902 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
903 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc,
904 false);
905 return;
906 }
907
908 // Move 128-bit floating-point values between VR128 and FP128.
909 if (SystemZ::VR128BitRegClass.contains(DestReg) &&
910 SystemZ::FP128BitRegClass.contains(SrcReg)) {
911 MCRegister SrcRegHi =
912 RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_h64),
913 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass);
914 MCRegister SrcRegLo =
915 RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_l64),
916 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass);
917
918 BuildMI(MBB, MBBI, DL, get(SystemZ::VMRHG), DestReg)
919 .addReg(SrcRegHi, getKillRegState(KillSrc))
920 .addReg(SrcRegLo, getKillRegState(KillSrc));
921 return;
922 }
923 if (SystemZ::FP128BitRegClass.contains(DestReg) &&
924 SystemZ::VR128BitRegClass.contains(SrcReg)) {
925 MCRegister DestRegHi =
926 RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_h64),
927 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass);
928 MCRegister DestRegLo =
929 RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_l64),
930 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass);
931
932 if (DestRegHi != SrcReg.asMCReg())
933 copyPhysReg(MBB, MBBI, DL, DestRegHi, SrcReg, false);
934 BuildMI(MBB, MBBI, DL, get(SystemZ::VREPG), DestRegLo)
935 .addReg(SrcReg, getKillRegState(KillSrc)).addImm(1);
936 return;
937 }
938
939 if (SystemZ::FP128BitRegClass.contains(DestReg) &&
940 SystemZ::GR128BitRegClass.contains(SrcReg)) {
941 MCRegister DestRegHi = RI.getSubReg(DestReg, SystemZ::subreg_h64);
942 MCRegister DestRegLo = RI.getSubReg(DestReg, SystemZ::subreg_l64);
943 MCRegister SrcRegHi = RI.getSubReg(SrcReg, SystemZ::subreg_h64);
944 MCRegister SrcRegLo = RI.getSubReg(SrcReg, SystemZ::subreg_l64);
945
946 BuildMI(MBB, MBBI, DL, get(SystemZ::LDGR), DestRegHi)
947 .addReg(SrcRegHi)
949
950 BuildMI(MBB, MBBI, DL, get(SystemZ::LDGR), DestRegLo)
951 .addReg(SrcRegLo, getKillRegState(KillSrc));
952 return;
953 }
954
955 // Move CC value from a GR32.
956 if (DestReg == SystemZ::CC) {
957 unsigned Opcode =
958 SystemZ::GR32BitRegClass.contains(SrcReg) ? SystemZ::TMLH : SystemZ::TMHH;
959 BuildMI(MBB, MBBI, DL, get(Opcode))
960 .addReg(SrcReg, getKillRegState(KillSrc))
961 .addImm(3 << (SystemZ::IPM_CC - 16));
962 return;
963 }
964
965 if (SystemZ::GR128BitRegClass.contains(DestReg) &&
966 SystemZ::VR128BitRegClass.contains(SrcReg)) {
967 MCRegister DestH64 = RI.getSubReg(DestReg, SystemZ::subreg_h64);
968 MCRegister DestL64 = RI.getSubReg(DestReg, SystemZ::subreg_l64);
969
970 BuildMI(MBB, MBBI, DL, get(SystemZ::VLGVG), DestH64)
971 .addReg(SrcReg)
972 .addReg(SystemZ::NoRegister)
973 .addImm(0)
974 .addDef(DestReg, RegState::Implicit);
975 BuildMI(MBB, MBBI, DL, get(SystemZ::VLGVG), DestL64)
976 .addReg(SrcReg, getKillRegState(KillSrc))
977 .addReg(SystemZ::NoRegister)
978 .addImm(1);
979 return;
980 }
981
982 if (SystemZ::VR128BitRegClass.contains(DestReg) &&
983 SystemZ::GR128BitRegClass.contains(SrcReg)) {
984 BuildMI(MBB, MBBI, DL, get(SystemZ::VLVGP), DestReg)
985 .addReg(RI.getSubReg(SrcReg, SystemZ::subreg_h64))
986 .addReg(RI.getSubReg(SrcReg, SystemZ::subreg_l64));
987 return;
988 }
989
990 // Everything else needs only one instruction.
991 unsigned Opcode;
992 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
993 Opcode = SystemZ::LGR;
994 else if (SystemZ::FP16BitRegClass.contains(DestReg, SrcReg))
995 Opcode = STI.hasVector() ? SystemZ::LDR16 : SystemZ::LER16;
996 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
997 // For z13 we prefer LDR over LER to avoid partial register dependencies.
998 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
999 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
1000 Opcode = SystemZ::LDR;
1001 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
1002 Opcode = SystemZ::LXR;
1003 else if (SystemZ::VR16BitRegClass.contains(DestReg, SrcReg))
1004 Opcode = SystemZ::VLR16;
1005 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
1006 Opcode = SystemZ::VLR32;
1007 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
1008 Opcode = SystemZ::VLR64;
1009 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
1010 Opcode = SystemZ::VLR;
1011 else if (SystemZ::AR32BitRegClass.contains(DestReg, SrcReg))
1012 Opcode = SystemZ::CPYA;
1013 else if (SystemZ::GR64BitRegClass.contains(DestReg) &&
1014 SystemZ::FP64BitRegClass.contains(SrcReg))
1015 Opcode = SystemZ::LGDR;
1016 else if (SystemZ::FP64BitRegClass.contains(DestReg) &&
1017 SystemZ::GR64BitRegClass.contains(SrcReg))
1018 Opcode = SystemZ::LDGR;
1019 else
1020 llvm_unreachable("Impossible reg-to-reg copy");
1021
1022 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
1023 .addReg(SrcReg, getKillRegState(KillSrc));
1024}
1025
1028 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
1029
1030 Register VReg, MachineInstr::MIFlag Flags) const {
1031 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1032
1033 // Callers may expect a single instruction, so keep 128-bit moves
1034 // together for now and lower them after register allocation.
1035 unsigned LoadOpcode, StoreOpcode;
1036 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
1037 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
1038 .addReg(SrcReg, getKillRegState(isKill)),
1039 FrameIdx);
1040}
1041
1044 Register DestReg, int FrameIdx,
1045 const TargetRegisterClass *RC,
1046 Register VReg, unsigned SubReg,
1047 MachineInstr::MIFlag Flags) const {
1048 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1049
1050 // Callers may expect a single instruction, so keep 128-bit moves
1051 // together for now and lower them after register allocation.
1052 unsigned LoadOpcode, StoreOpcode;
1053 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
1054 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
1055 FrameIdx);
1056}
1057
1058// Return true if MI is a simple load or store with a 12-bit displacement
1059// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
1060static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
1061 const MCInstrDesc &MCID = MI->getDesc();
1062 return ((MCID.TSFlags & Flag) &&
1063 isUInt<12>(MI->getOperand(2).getImm()) &&
1064 MI->getOperand(3).getReg() == 0);
1065}
1066
1067namespace {
1068
1069struct LogicOp {
1070 LogicOp() = default;
1071 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
1072 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
1073
1074 explicit operator bool() const { return RegSize; }
1075
1076 unsigned RegSize = 0;
1077 unsigned ImmLSB = 0;
1078 unsigned ImmSize = 0;
1079};
1080
1081} // end anonymous namespace
1082
1083static LogicOp interpretAndImmediate(unsigned Opcode) {
1084 switch (Opcode) {
1085 case SystemZ::NILMux: return LogicOp(32, 0, 16);
1086 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
1087 case SystemZ::NILL64: return LogicOp(64, 0, 16);
1088 case SystemZ::NILH64: return LogicOp(64, 16, 16);
1089 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
1090 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
1091 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
1092 case SystemZ::NILF64: return LogicOp(64, 0, 32);
1093 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
1094 default: return LogicOp();
1095 }
1096}
1097
1098static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) {
1099 if (OldMI->registerDefIsDead(SystemZ::CC, /*TRI=*/nullptr)) {
1100 MachineOperand *CCDef =
1101 NewMI->findRegisterDefOperand(SystemZ::CC, /*TRI=*/nullptr);
1102 if (CCDef != nullptr)
1103 CCDef->setIsDead(true);
1104 }
1105}
1106
1107static void transferMIFlag(MachineInstr *OldMI, MachineInstr *NewMI,
1108 MachineInstr::MIFlag Flag) {
1109 if (OldMI->getFlag(Flag))
1110 NewMI->setFlag(Flag);
1111}
1112
1115 LiveIntervals *LIS) const {
1116 MachineBasicBlock *MBB = MI.getParent();
1117
1118 // Try to convert an AND into an RISBG-type instruction.
1119 // TODO: It might be beneficial to select RISBG and shorten to AND instead.
1120 if (LogicOp And = interpretAndImmediate(MI.getOpcode())) {
1121 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB;
1122 // AND IMMEDIATE leaves the other bits of the register unchanged.
1123 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
1124 unsigned Start, End;
1125 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
1126 unsigned NewOpcode;
1127 if (And.RegSize == 64) {
1128 NewOpcode = SystemZ::RISBG;
1129 // Prefer RISBGN if available, since it does not clobber CC.
1130 if (STI.hasMiscellaneousExtensions())
1131 NewOpcode = SystemZ::RISBGN;
1132 } else {
1133 NewOpcode = SystemZ::RISBMux;
1134 Start &= 31;
1135 End &= 31;
1136 }
1137 MachineOperand &Dest = MI.getOperand(0);
1138 MachineOperand &Src = MI.getOperand(1);
1140 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode))
1141 .add(Dest)
1142 .addReg(0)
1143 .addReg(Src.getReg(), getKillRegState(Src.isKill()),
1144 Src.getSubReg())
1145 .addImm(Start)
1146 .addImm(End + 128)
1147 .addImm(0);
1148 if (LV) {
1149 unsigned NumOps = MI.getNumOperands();
1150 for (unsigned I = 1; I < NumOps; ++I) {
1151 MachineOperand &Op = MI.getOperand(I);
1152 if (Op.isReg() && Op.isKill())
1153 LV->replaceKillInstruction(Op.getReg(), MI, *MIB);
1154 }
1155 }
1156 if (LIS)
1157 LIS->ReplaceMachineInstrInMaps(MI, *MIB);
1158 transferDeadCC(&MI, MIB);
1159 return MIB;
1160 }
1161 }
1162 return nullptr;
1163}
1164
1166 bool Invert) const {
1167 unsigned Opc = Inst.getOpcode();
1168 if (Invert) {
1169 auto InverseOpcode = getInverseOpcode(Opc);
1170 if (!InverseOpcode)
1171 return false;
1172 Opc = *InverseOpcode;
1173 }
1174
1175 switch (Opc) {
1176 default:
1177 break;
1178 // Adds and multiplications.
1179 case SystemZ::WFADB:
1180 case SystemZ::WFASB:
1181 case SystemZ::WFAXB:
1182 case SystemZ::VFADB:
1183 case SystemZ::VFASB:
1184 case SystemZ::WFMDB:
1185 case SystemZ::WFMSB:
1186 case SystemZ::WFMXB:
1187 case SystemZ::VFMDB:
1188 case SystemZ::VFMSB:
1191 }
1192
1193 return false;
1194}
1195
1196std::optional<unsigned>
1198 // fadd => fsub
1199 switch (Opcode) {
1200 case SystemZ::WFADB:
1201 return SystemZ::WFSDB;
1202 case SystemZ::WFASB:
1203 return SystemZ::WFSSB;
1204 case SystemZ::WFAXB:
1205 return SystemZ::WFSXB;
1206 case SystemZ::VFADB:
1207 return SystemZ::VFSDB;
1208 case SystemZ::VFASB:
1209 return SystemZ::VFSSB;
1210 // fsub => fadd
1211 case SystemZ::WFSDB:
1212 return SystemZ::WFADB;
1213 case SystemZ::WFSSB:
1214 return SystemZ::WFASB;
1215 case SystemZ::WFSXB:
1216 return SystemZ::WFAXB;
1217 case SystemZ::VFSDB:
1218 return SystemZ::VFADB;
1219 case SystemZ::VFSSB:
1220 return SystemZ::VFASB;
1221 default:
1222 return std::nullopt;
1223 }
1224}
1225
1228 int FrameIndex, MachineInstr *&CopyMI, LiveIntervals *LIS,
1229 VirtRegMap *VRM) const {
1232 MachineRegisterInfo &MRI = MF.getRegInfo();
1233 const MachineFrameInfo &MFI = MF.getFrameInfo();
1234 unsigned Size = MFI.getObjectSize(FrameIndex);
1235 unsigned Opcode = MI.getOpcode();
1236
1237 // Check CC liveness if new instruction introduces a dead def of CC.
1238 SlotIndex MISlot = SlotIndex();
1239 LiveRange *CCLiveRange = nullptr;
1240 bool CCLiveAtMI = true;
1241 if (LIS) {
1242 MISlot = LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot();
1243 auto CCUnits = TRI->regunits(MCRegister::from(SystemZ::CC));
1244 assert(range_size(CCUnits) == 1 && "CC only has one reg unit.");
1245 CCLiveRange = &LIS->getRegUnit(*CCUnits.begin());
1246 CCLiveAtMI = CCLiveRange->liveAt(MISlot);
1247 }
1248
1249 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
1250 if (!CCLiveAtMI && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
1251 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) {
1252 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
1253 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt,
1254 MI.getDebugLoc(), get(SystemZ::AGSI))
1255 .addFrameIndex(FrameIndex)
1256 .addImm(0)
1257 .addImm(MI.getOperand(2).getImm());
1258 BuiltMI->findRegisterDefOperand(SystemZ::CC, /*TRI=*/nullptr)
1259 ->setIsDead(true);
1260 CCLiveRange->createDeadDef(MISlot, LIS->getVNInfoAllocator());
1261 return BuiltMI;
1262 }
1263 return nullptr;
1264 }
1265
1266 // All other cases require a single operand.
1267 if (Ops.size() != 1)
1268 return nullptr;
1269
1270 unsigned OpNum = Ops[0];
1271 const TargetRegisterClass *RC =
1272 MF.getRegInfo().getRegClass(MI.getOperand(OpNum).getReg());
1273 assert((Size * 8 == TRI->getRegSizeInBits(*RC) ||
1274 (RC == &SystemZ::FP16BitRegClass && Size == 4 && !STI.hasVector())) &&
1275 "Invalid size combination");
1276 (void)RC;
1277
1278 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 &&
1279 isInt<8>(MI.getOperand(2).getImm())) {
1280 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
1281 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
1282 MachineInstr *BuiltMI =
1283 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
1284 .addFrameIndex(FrameIndex)
1285 .addImm(0)
1286 .addImm(MI.getOperand(2).getImm());
1287 transferDeadCC(&MI, BuiltMI);
1289 return BuiltMI;
1290 }
1291
1292 if ((Opcode == SystemZ::ALFI && OpNum == 0 &&
1293 isInt<8>((int32_t)MI.getOperand(2).getImm())) ||
1294 (Opcode == SystemZ::ALGFI && OpNum == 0 &&
1295 isInt<8>(MI.getOperand(2).getImm()))) {
1296 // AL(G)FI %reg, CONST -> AL(G)SI %mem, CONST
1297 Opcode = (Opcode == SystemZ::ALFI ? SystemZ::ALSI : SystemZ::ALGSI);
1298 MachineInstr *BuiltMI =
1299 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
1300 .addFrameIndex(FrameIndex)
1301 .addImm(0)
1302 .addImm((int8_t)MI.getOperand(2).getImm());
1303 transferDeadCC(&MI, BuiltMI);
1304 return BuiltMI;
1305 }
1306
1307 if ((Opcode == SystemZ::SLFI && OpNum == 0 &&
1308 isInt<8>((int32_t)-MI.getOperand(2).getImm())) ||
1309 (Opcode == SystemZ::SLGFI && OpNum == 0 &&
1310 isInt<8>((-MI.getOperand(2).getImm())))) {
1311 // SL(G)FI %reg, CONST -> AL(G)SI %mem, -CONST
1312 Opcode = (Opcode == SystemZ::SLFI ? SystemZ::ALSI : SystemZ::ALGSI);
1313 MachineInstr *BuiltMI =
1314 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
1315 .addFrameIndex(FrameIndex)
1316 .addImm(0)
1317 .addImm((int8_t)-MI.getOperand(2).getImm());
1318 transferDeadCC(&MI, BuiltMI);
1319 return BuiltMI;
1320 }
1321
1322 unsigned MemImmOpc = 0;
1323 switch (Opcode) {
1324 case SystemZ::LHIMux:
1325 case SystemZ::LHI: MemImmOpc = SystemZ::MVHI; break;
1326 case SystemZ::LGHI: MemImmOpc = SystemZ::MVGHI; break;
1327 case SystemZ::CHIMux:
1328 case SystemZ::CHI: MemImmOpc = SystemZ::CHSI; break;
1329 case SystemZ::CGHI: MemImmOpc = SystemZ::CGHSI; break;
1330 case SystemZ::CLFIMux:
1331 case SystemZ::CLFI:
1332 if (isUInt<16>(MI.getOperand(1).getImm()))
1333 MemImmOpc = SystemZ::CLFHSI;
1334 break;
1335 case SystemZ::CLGFI:
1336 if (isUInt<16>(MI.getOperand(1).getImm()))
1337 MemImmOpc = SystemZ::CLGHSI;
1338 break;
1339 default: break;
1340 }
1341 if (MemImmOpc)
1342 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
1343 get(MemImmOpc))
1344 .addFrameIndex(FrameIndex)
1345 .addImm(0)
1346 .addImm(MI.getOperand(1).getImm());
1347
1348 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
1349 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
1350 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
1351 // If we're spilling the destination of an LDGR or LGDR, store the
1352 // source register instead.
1353 if (OpNum == 0) {
1354 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
1355 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
1356 get(StoreOpcode))
1357 .add(MI.getOperand(1))
1358 .addFrameIndex(FrameIndex)
1359 .addImm(0)
1360 .addReg(0);
1361 }
1362 // If we're spilling the source of an LDGR or LGDR, load the
1363 // destination register instead.
1364 if (OpNum == 1) {
1365 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
1366 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
1367 get(LoadOpcode))
1368 .add(MI.getOperand(0))
1369 .addFrameIndex(FrameIndex)
1370 .addImm(0)
1371 .addReg(0);
1372 }
1373 }
1374
1375 // Look for cases where the source of a simple store or the destination
1376 // of a simple load is being spilled. Try to use MVC instead.
1377 //
1378 // Although MVC is in practice a fast choice in these cases, it is still
1379 // logically a bytewise copy. This means that we cannot use it if the
1380 // load or store is volatile. We also wouldn't be able to use MVC if
1381 // the two memories partially overlap, but that case cannot occur here,
1382 // because we know that one of the memories is a full frame index.
1383 //
1384 // For performance reasons, we also want to avoid using MVC if the addresses
1385 // might be equal. We don't worry about that case here, because spill slot
1386 // coloring happens later, and because we have special code to remove
1387 // MVCs that turn out to be redundant.
1388 if (OpNum == 0 && MI.hasOneMemOperand()) {
1389 MachineMemOperand *MMO = *MI.memoperands_begin();
1390 if (MMO->getSize() == Size && !MMO->isVolatile() && !MMO->isAtomic()) {
1391 // Handle conversion of loads.
1393 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
1394 get(SystemZ::MVC))
1395 .addFrameIndex(FrameIndex)
1396 .addImm(0)
1397 .addImm(Size)
1398 .add(MI.getOperand(1))
1399 .addImm(MI.getOperand(2).getImm())
1400 .addMemOperand(MMO);
1401 }
1402 // Handle conversion of stores.
1404 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
1405 get(SystemZ::MVC))
1406 .add(MI.getOperand(1))
1407 .addImm(MI.getOperand(2).getImm())
1408 .addImm(Size)
1409 .addFrameIndex(FrameIndex)
1410 .addImm(0)
1411 .addMemOperand(MMO);
1412 }
1413 }
1414 }
1415
1416 // If the spilled operand is the final one or the instruction is
1417 // commutable, try to change <INSN>R into <INSN>. Don't introduce a def of
1418 // CC if it is live and MI does not define it.
1419 unsigned NumOps = MI.getNumExplicitOperands();
1420 int MemOpcode = SystemZ::getMemOpcode(Opcode);
1421 if (MemOpcode == -1 ||
1422 (CCLiveAtMI && !MI.definesRegister(SystemZ::CC, /*TRI=*/nullptr) &&
1423 get(MemOpcode).hasImplicitDefOfPhysReg(SystemZ::CC)))
1424 return nullptr;
1425
1426 // Check if all other vregs have a usable allocation in the case of vector
1427 // to FP conversion.
1428 const MCInstrDesc &MCID = MI.getDesc();
1429 for (unsigned I = 0, E = MCID.getNumOperands(); I != E; ++I) {
1430 const MCOperandInfo &MCOI = MCID.operands()[I];
1431 if (MCOI.OperandType != MCOI::OPERAND_REGISTER || I == OpNum)
1432 continue;
1433 const TargetRegisterClass *RC = TRI->getRegClass(MCOI.RegClass);
1434 if (RC == &SystemZ::VR32BitRegClass || RC == &SystemZ::VR64BitRegClass) {
1435 Register Reg = MI.getOperand(I).getReg();
1436 Register PhysReg = Reg.isVirtual()
1437 ? (VRM ? Register(VRM->getPhys(Reg)) : Register())
1438 : Reg;
1439 if (!PhysReg ||
1440 !(SystemZ::FP32BitRegClass.contains(PhysReg) ||
1441 SystemZ::FP64BitRegClass.contains(PhysReg) ||
1442 SystemZ::VF128BitRegClass.contains(PhysReg)))
1443 return nullptr;
1444 }
1445 }
1446 // Fused multiply and add/sub need to have the same dst and accumulator reg.
1447 bool FusedFPOp = (Opcode == SystemZ::WFMADB || Opcode == SystemZ::WFMASB ||
1448 Opcode == SystemZ::WFMSDB || Opcode == SystemZ::WFMSSB);
1449 if (FusedFPOp) {
1450 Register DstReg = VRM->getPhys(MI.getOperand(0).getReg());
1451 Register AccReg = VRM->getPhys(MI.getOperand(3).getReg());
1452 if (OpNum == 0 || OpNum == 3 || DstReg != AccReg)
1453 return nullptr;
1454 }
1455
1456 // Try to swap compare operands if possible.
1457 bool NeedsCommute = false;
1458 if ((MI.getOpcode() == SystemZ::CR || MI.getOpcode() == SystemZ::CGR ||
1459 MI.getOpcode() == SystemZ::CLR || MI.getOpcode() == SystemZ::CLGR ||
1460 MI.getOpcode() == SystemZ::WFCDB || MI.getOpcode() == SystemZ::WFCSB ||
1461 MI.getOpcode() == SystemZ::WFKDB || MI.getOpcode() == SystemZ::WFKSB) &&
1462 OpNum == 0 && prepareCompareSwapOperands(MI))
1463 NeedsCommute = true;
1464
1465 bool CCOperands = false;
1466 if (MI.getOpcode() == SystemZ::LOCRMux || MI.getOpcode() == SystemZ::LOCGR ||
1467 MI.getOpcode() == SystemZ::SELRMux || MI.getOpcode() == SystemZ::SELGR) {
1468 assert(MI.getNumOperands() == 6 && NumOps == 5 &&
1469 "LOCR/SELR instruction operands corrupt?");
1470 NumOps -= 2;
1471 CCOperands = true;
1472 }
1473
1474 // See if this is a 3-address instruction that is convertible to 2-address
1475 // and suitable for folding below. Only try this with virtual registers
1476 // and a provided VRM (during regalloc).
1477 if (NumOps == 3 && SystemZ::getTargetMemOpcode(MemOpcode) != -1) {
1478 if (VRM == nullptr)
1479 return nullptr;
1480 else {
1481 Register DstReg = MI.getOperand(0).getReg();
1482 Register DstPhys =
1483 (DstReg.isVirtual() ? Register(VRM->getPhys(DstReg)) : DstReg);
1484 Register SrcReg = (OpNum == 2 ? MI.getOperand(1).getReg()
1485 : ((OpNum == 1 && MI.isCommutable())
1486 ? MI.getOperand(2).getReg()
1487 : Register()));
1488 if (DstPhys && !SystemZ::GRH32BitRegClass.contains(DstPhys) && SrcReg &&
1489 SrcReg.isVirtual() && DstPhys == VRM->getPhys(SrcReg))
1490 NeedsCommute = (OpNum == 1);
1491 else
1492 return nullptr;
1493 }
1494 }
1495
1496 if ((OpNum == NumOps - 1) || NeedsCommute || FusedFPOp) {
1497 const MCInstrDesc &MemDesc = get(MemOpcode);
1498 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
1499 assert(AccessBytes != 0 && "Size of access should be known");
1500 assert(AccessBytes <= Size && "Access outside the frame index");
1501 uint64_t Offset = Size - AccessBytes;
1502 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
1503 MI.getDebugLoc(), get(MemOpcode));
1504 if (MI.isCompare()) {
1505 assert(NumOps == 2 && "Expected 2 register operands for a compare.");
1506 MIB.add(MI.getOperand(NeedsCommute ? 1 : 0));
1507 }
1508 else if (FusedFPOp) {
1509 MIB.add(MI.getOperand(0));
1510 MIB.add(MI.getOperand(3));
1511 MIB.add(MI.getOperand(OpNum == 1 ? 2 : 1));
1512 }
1513 else {
1514 MIB.add(MI.getOperand(0));
1515 if (NeedsCommute)
1516 MIB.add(MI.getOperand(2));
1517 else
1518 for (unsigned I = 1; I < OpNum; ++I)
1519 MIB.add(MI.getOperand(I));
1520 }
1521 MIB.addFrameIndex(FrameIndex).addImm(Offset);
1522 if (MemDesc.TSFlags & SystemZII::HasIndex)
1523 MIB.addReg(0);
1524 if (CCOperands) {
1525 unsigned CCValid = MI.getOperand(NumOps).getImm();
1526 unsigned CCMask = MI.getOperand(NumOps + 1).getImm();
1527 MIB.addImm(CCValid);
1528 MIB.addImm(NeedsCommute ? CCMask ^ CCValid : CCMask);
1529 }
1530 if (MIB->definesRegister(SystemZ::CC, /*TRI=*/nullptr) &&
1531 (!MI.definesRegister(SystemZ::CC, /*TRI=*/nullptr) ||
1532 MI.registerDefIsDead(SystemZ::CC, /*TRI=*/nullptr))) {
1533 MIB->addRegisterDead(SystemZ::CC, TRI);
1534 if (CCLiveRange)
1535 CCLiveRange->createDeadDef(MISlot, LIS->getVNInfoAllocator());
1536 }
1537 // Constrain the register classes if converted from a vector opcode. The
1538 // allocated regs are in an FP reg-class per previous check above.
1539 for (const MachineOperand &MO : MIB->operands())
1540 if (MO.isReg() && MO.getReg().isVirtual()) {
1541 Register Reg = MO.getReg();
1542 if (MRI.getRegClass(Reg) == &SystemZ::VR32BitRegClass)
1543 MRI.setRegClass(Reg, &SystemZ::FP32BitRegClass);
1544 else if (MRI.getRegClass(Reg) == &SystemZ::VR64BitRegClass)
1545 MRI.setRegClass(Reg, &SystemZ::FP64BitRegClass);
1546 else if (MRI.getRegClass(Reg) == &SystemZ::VR128BitRegClass)
1547 MRI.setRegClass(Reg, &SystemZ::VF128BitRegClass);
1548 }
1549
1550 transferDeadCC(&MI, MIB);
1553 return MIB;
1554 }
1555
1556 return nullptr;
1557}
1558
1561 MachineInstr &LoadMI, MachineInstr *&CopyMI, LiveIntervals *LIS) const {
1563 MachineRegisterInfo *MRI = &MF.getRegInfo();
1564 MachineBasicBlock *MBB = MI.getParent();
1565
1566 // For reassociable FP operations, any loads have been purposefully left
1567 // unfolded so that MachineCombiner can do its work on reg/reg
1568 // opcodes. After that, as many loads as possible are now folded.
1569 // TODO: This may be beneficial with other opcodes as well as machine-sink
1570 // can move loads close to their user in a different MBB, which the isel
1571 // matcher did not see.
1572 unsigned LoadOpc = 0;
1573 unsigned RegMemOpcode = 0;
1574 const TargetRegisterClass *FPRC = nullptr;
1575 RegMemOpcode = MI.getOpcode() == SystemZ::WFADB ? SystemZ::ADB
1576 : MI.getOpcode() == SystemZ::WFSDB ? SystemZ::SDB
1577 : MI.getOpcode() == SystemZ::WFMDB ? SystemZ::MDB
1578 : 0;
1579 if (RegMemOpcode) {
1580 LoadOpc = SystemZ::VL64;
1581 FPRC = &SystemZ::FP64BitRegClass;
1582 } else {
1583 RegMemOpcode = MI.getOpcode() == SystemZ::WFASB ? SystemZ::AEB
1584 : MI.getOpcode() == SystemZ::WFSSB ? SystemZ::SEB
1585 : MI.getOpcode() == SystemZ::WFMSB ? SystemZ::MEEB
1586 : 0;
1587 if (RegMemOpcode) {
1588 LoadOpc = SystemZ::VL32;
1589 FPRC = &SystemZ::FP32BitRegClass;
1590 }
1591 }
1592 if (!RegMemOpcode || LoadMI.getOpcode() != LoadOpc)
1593 return nullptr;
1594
1595 // If RegMemOpcode clobbers CC, first make sure CC is not live at this point.
1596 if (get(RegMemOpcode).hasImplicitDefOfPhysReg(SystemZ::CC)) {
1597 for (MachineBasicBlock::iterator MII = InsertPt;;) {
1598 if (MII == MBB->begin()) {
1599 if (MBB->isLiveIn(SystemZ::CC))
1600 return nullptr;
1601 break;
1602 }
1603 --MII;
1604 if (MII->definesRegister(SystemZ::CC, /*TRI=*/nullptr)) {
1605 if (!MII->registerDefIsDead(SystemZ::CC, /*TRI=*/nullptr))
1606 return nullptr;
1607 break;
1608 }
1609 }
1610 }
1611
1612 Register FoldAsLoadDefReg = LoadMI.getOperand(0).getReg();
1613 if (Ops.size() != 1 || FoldAsLoadDefReg != MI.getOperand(Ops[0]).getReg())
1614 return nullptr;
1615 Register DstReg = MI.getOperand(0).getReg();
1616 MachineOperand LHS = MI.getOperand(1);
1617 MachineOperand RHS = MI.getOperand(2);
1618 MachineOperand &RegMO = RHS.getReg() == FoldAsLoadDefReg ? LHS : RHS;
1619 if ((RegMemOpcode == SystemZ::SDB || RegMemOpcode == SystemZ::SEB) &&
1620 FoldAsLoadDefReg != RHS.getReg())
1621 return nullptr;
1622
1623 MachineOperand &Base = LoadMI.getOperand(1);
1624 MachineOperand &Disp = LoadMI.getOperand(2);
1625 MachineOperand &Indx = LoadMI.getOperand(3);
1627 BuildMI(*MI.getParent(), InsertPt, MI.getDebugLoc(), get(RegMemOpcode), DstReg)
1628 .add(RegMO)
1629 .add(Base)
1630 .add(Disp)
1631 .add(Indx);
1632 MIB->addRegisterDead(SystemZ::CC, &RI);
1633 MRI->setRegClass(DstReg, FPRC);
1634 MRI->setRegClass(RegMO.getReg(), FPRC);
1636
1637 return MIB;
1638}
1639
1641 switch (MI.getOpcode()) {
1642 case SystemZ::L128:
1643 splitMove(MI, SystemZ::LG);
1644 return true;
1645
1646 case SystemZ::ST128:
1647 splitMove(MI, SystemZ::STG);
1648 return true;
1649
1650 case SystemZ::LX:
1651 splitMove(MI, SystemZ::LD);
1652 return true;
1653
1654 case SystemZ::STX:
1655 splitMove(MI, SystemZ::STD);
1656 return true;
1657
1658 case SystemZ::LBMux:
1659 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
1660 return true;
1661
1662 case SystemZ::LHMux:
1663 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
1664 return true;
1665
1666 case SystemZ::LLCRMux:
1667 expandZExtPseudo(MI, SystemZ::LLCR, 8);
1668 return true;
1669
1670 case SystemZ::LLHRMux:
1671 expandZExtPseudo(MI, SystemZ::LLHR, 16);
1672 return true;
1673
1674 case SystemZ::LLCMux:
1675 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
1676 return true;
1677
1678 case SystemZ::LLHMux:
1679 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
1680 return true;
1681
1682 case SystemZ::LMux:
1683 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
1684 return true;
1685
1686 case SystemZ::LOCMux:
1687 expandLOCPseudo(MI, SystemZ::LOC, SystemZ::LOCFH);
1688 return true;
1689
1690 case SystemZ::LOCHIMux:
1691 expandLOCPseudo(MI, SystemZ::LOCHI, SystemZ::LOCHHI);
1692 return true;
1693
1694 case SystemZ::STCMux:
1695 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
1696 return true;
1697
1698 case SystemZ::STHMux:
1699 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
1700 return true;
1701
1702 case SystemZ::STMux:
1703 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
1704 return true;
1705
1706 case SystemZ::STOCMux:
1707 expandLOCPseudo(MI, SystemZ::STOC, SystemZ::STOCFH);
1708 return true;
1709
1710 case SystemZ::LHIMux:
1711 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
1712 return true;
1713
1714 case SystemZ::IIFMux:
1715 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
1716 return true;
1717
1718 case SystemZ::IILMux:
1719 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
1720 return true;
1721
1722 case SystemZ::IIHMux:
1723 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
1724 return true;
1725
1726 case SystemZ::NIFMux:
1727 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1728 return true;
1729
1730 case SystemZ::NILMux:
1731 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1732 return true;
1733
1734 case SystemZ::NIHMux:
1735 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1736 return true;
1737
1738 case SystemZ::OIFMux:
1739 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1740 return true;
1741
1742 case SystemZ::OILMux:
1743 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1744 return true;
1745
1746 case SystemZ::OIHMux:
1747 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1748 return true;
1749
1750 case SystemZ::XIFMux:
1751 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1752 return true;
1753
1754 case SystemZ::TMLMux:
1755 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1756 return true;
1757
1758 case SystemZ::TMHMux:
1759 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1760 return true;
1761
1762 case SystemZ::AHIMux:
1763 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1764 return true;
1765
1766 case SystemZ::AHIMuxK:
1767 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1768 return true;
1769
1770 case SystemZ::AFIMux:
1771 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1772 return true;
1773
1774 case SystemZ::CHIMux:
1775 expandRIPseudo(MI, SystemZ::CHI, SystemZ::CIH, false);
1776 return true;
1777
1778 case SystemZ::CFIMux:
1779 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1780 return true;
1781
1782 case SystemZ::CLFIMux:
1783 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1784 return true;
1785
1786 case SystemZ::CMux:
1787 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1788 return true;
1789
1790 case SystemZ::CLMux:
1791 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1792 return true;
1793
1794 case SystemZ::RISBMux: {
1795 bool DestIsHigh = SystemZ::isHighReg(MI.getOperand(0).getReg());
1796 bool SrcIsHigh = SystemZ::isHighReg(MI.getOperand(2).getReg());
1797 if (SrcIsHigh == DestIsHigh)
1798 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
1799 else {
1800 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1801 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32);
1802 }
1803 return true;
1804 }
1805
1806 case SystemZ::ADJDYNALLOC:
1807 splitAdjDynAlloc(MI);
1808 return true;
1809
1810 case TargetOpcode::LOAD_STACK_GUARD:
1811 expandLoadStackGuard(&MI);
1812 return true;
1813
1814 default:
1815 return false;
1816 }
1817}
1818
1820 if (MI.isInlineAsm()) {
1821 const MachineFunction *MF = MI.getParent()->getParent();
1822 const char *AsmStr = MI.getOperand(0).getSymbolName();
1823 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1824 }
1825 else if (MI.getOpcode() == SystemZ::PATCHPOINT)
1827 else if (MI.getOpcode() == SystemZ::STACKMAP)
1828 return MI.getOperand(1).getImm();
1829 else if (MI.getOpcode() == SystemZ::FENTRY_CALL)
1830 return 6;
1831 if (MI.getOpcode() == TargetOpcode::PATCHABLE_FUNCTION_ENTER)
1832 return 18;
1833 if (MI.getOpcode() == TargetOpcode::PATCHABLE_RET)
1834 return 18 + (MI.getOperand(0).getImm() == SystemZ::CondReturn ? 4 : 0);
1835 if (MI.getOpcode() == TargetOpcode::BUNDLE)
1836 return getInstBundleSize(MI);
1837
1838 return MI.getDesc().getSize();
1839}
1840
1843 switch (MI.getOpcode()) {
1844 case SystemZ::BR:
1845 case SystemZ::BI:
1846 case SystemZ::J:
1847 case SystemZ::JG:
1849 SystemZ::CCMASK_ANY, &MI.getOperand(0));
1850
1851 case SystemZ::BRC:
1852 case SystemZ::BRCL:
1853 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(),
1854 MI.getOperand(1).getImm(), &MI.getOperand(2));
1855
1856 case SystemZ::BRCT:
1857 case SystemZ::BRCTH:
1859 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
1860
1861 case SystemZ::BRCTG:
1863 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
1864
1865 case SystemZ::CIJ:
1866 case SystemZ::CRJ:
1868 MI.getOperand(2).getImm(), &MI.getOperand(3));
1869
1870 case SystemZ::CLIJ:
1871 case SystemZ::CLRJ:
1873 MI.getOperand(2).getImm(), &MI.getOperand(3));
1874
1875 case SystemZ::CGIJ:
1876 case SystemZ::CGRJ:
1878 MI.getOperand(2).getImm(), &MI.getOperand(3));
1879
1880 case SystemZ::CLGIJ:
1881 case SystemZ::CLGRJ:
1883 MI.getOperand(2).getImm(), &MI.getOperand(3));
1884
1885 case SystemZ::INLINEASM_BR:
1886 // Don't try to analyze asm goto, so pass nullptr as branch target argument.
1887 return SystemZII::Branch(SystemZII::AsmGoto, 0, 0, nullptr);
1888
1889 default:
1890 llvm_unreachable("Unrecognized branch opcode");
1891 }
1892}
1893
1895 unsigned &LoadOpcode,
1896 unsigned &StoreOpcode) const {
1897 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1898 LoadOpcode = SystemZ::L;
1899 StoreOpcode = SystemZ::ST;
1900 } else if (RC == &SystemZ::GRH32BitRegClass) {
1901 LoadOpcode = SystemZ::LFH;
1902 StoreOpcode = SystemZ::STFH;
1903 } else if (RC == &SystemZ::GRX32BitRegClass) {
1904 LoadOpcode = SystemZ::LMux;
1905 StoreOpcode = SystemZ::STMux;
1906 } else if (RC == &SystemZ::GR64BitRegClass ||
1907 RC == &SystemZ::ADDR64BitRegClass) {
1908 LoadOpcode = SystemZ::LG;
1909 StoreOpcode = SystemZ::STG;
1910 } else if (RC == &SystemZ::GR128BitRegClass ||
1911 RC == &SystemZ::ADDR128BitRegClass) {
1912 LoadOpcode = SystemZ::L128;
1913 StoreOpcode = SystemZ::ST128;
1914 } else if (RC == &SystemZ::FP16BitRegClass && !STI.hasVector()) {
1915 LoadOpcode = SystemZ::LE16;
1916 StoreOpcode = SystemZ::STE16;
1917 } else if (RC == &SystemZ::FP32BitRegClass) {
1918 LoadOpcode = SystemZ::LE;
1919 StoreOpcode = SystemZ::STE;
1920 } else if (RC == &SystemZ::FP64BitRegClass) {
1921 LoadOpcode = SystemZ::LD;
1922 StoreOpcode = SystemZ::STD;
1923 } else if (RC == &SystemZ::FP128BitRegClass) {
1924 LoadOpcode = SystemZ::LX;
1925 StoreOpcode = SystemZ::STX;
1926 } else if (RC == &SystemZ::FP16BitRegClass ||
1927 RC == &SystemZ::VR16BitRegClass) {
1928 LoadOpcode = SystemZ::VL16;
1929 StoreOpcode = SystemZ::VST16;
1930 } else if (RC == &SystemZ::VR32BitRegClass) {
1931 LoadOpcode = SystemZ::VL32;
1932 StoreOpcode = SystemZ::VST32;
1933 } else if (RC == &SystemZ::VR64BitRegClass) {
1934 LoadOpcode = SystemZ::VL64;
1935 StoreOpcode = SystemZ::VST64;
1936 } else if (RC == &SystemZ::VF128BitRegClass ||
1937 RC == &SystemZ::VR128BitRegClass) {
1938 LoadOpcode = SystemZ::VL;
1939 StoreOpcode = SystemZ::VST;
1940 } else
1941 llvm_unreachable("Unsupported regclass to load or store");
1942}
1943
1945 int64_t Offset,
1946 const MachineInstr *MI) const {
1947 const MCInstrDesc &MCID = get(Opcode);
1948 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1949 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1950 // Get the instruction to use for unsigned 12-bit displacements.
1951 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1952 if (Disp12Opcode >= 0)
1953 return Disp12Opcode;
1954
1955 // All address-related instructions can use unsigned 12-bit
1956 // displacements.
1957 return Opcode;
1958 }
1959 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1960 // Get the instruction to use for signed 20-bit displacements.
1961 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1962 if (Disp20Opcode >= 0)
1963 return Disp20Opcode;
1964
1965 // Check whether Opcode allows signed 20-bit displacements.
1966 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1967 return Opcode;
1968
1969 // If a VR32/VR64 reg ended up in an FP register, use the FP opcode.
1970 if (MI && MI->getOperand(0).isReg()) {
1971 Register Reg = MI->getOperand(0).getReg();
1972 if (Reg.isPhysical() && SystemZMC::getFirstReg(Reg) < 16) {
1973 switch (Opcode) {
1974 case SystemZ::VL32:
1975 return SystemZ::LEY;
1976 case SystemZ::VST32:
1977 return SystemZ::STEY;
1978 case SystemZ::VL64:
1979 return SystemZ::LDY;
1980 case SystemZ::VST64:
1981 return SystemZ::STDY;
1982 default: break;
1983 }
1984 }
1985 }
1986 }
1987 return 0;
1988}
1989
1991 const MCInstrDesc &MCID = get(Opcode);
1992 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1993 return SystemZ::getDisp12Opcode(Opcode) >= 0;
1994 return SystemZ::getDisp20Opcode(Opcode) >= 0;
1995}
1996
1997unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1998 switch (Opcode) {
1999 case SystemZ::L: return SystemZ::LT;
2000 case SystemZ::LY: return SystemZ::LT;
2001 case SystemZ::LG: return SystemZ::LTG;
2002 case SystemZ::LGF: return SystemZ::LTGF;
2003 case SystemZ::LR: return SystemZ::LTR;
2004 case SystemZ::LGFR: return SystemZ::LTGFR;
2005 case SystemZ::LGR: return SystemZ::LTGR;
2006 case SystemZ::LCDFR: return SystemZ::LCDBR;
2007 case SystemZ::LPDFR: return SystemZ::LPDBR;
2008 case SystemZ::LNDFR: return SystemZ::LNDBR;
2009 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
2010 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
2011 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
2012 // On zEC12 we prefer to use RISBGN. But if there is a chance to
2013 // actually use the condition code, we may turn it back into RISGB.
2014 // Note that RISBG is not really a "load-and-test" instruction,
2015 // but sets the same condition code values, so is OK to use here.
2016 case SystemZ::RISBGN: return SystemZ::RISBG;
2017 default: return 0;
2018 }
2019}
2020
2021bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
2022 unsigned &Start, unsigned &End) const {
2023 // Reject trivial all-zero masks.
2024 Mask &= allOnes(BitSize);
2025 if (Mask == 0)
2026 return false;
2027
2028 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
2029 // the msb and End specifies the index of the lsb.
2030 unsigned LSB, Length;
2031 if (isShiftedMask_64(Mask, LSB, Length)) {
2032 Start = 63 - (LSB + Length - 1);
2033 End = 63 - LSB;
2034 return true;
2035 }
2036
2037 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
2038 // of the low 1s and End specifies the lsb of the high 1s.
2039 if (isShiftedMask_64(Mask ^ allOnes(BitSize), LSB, Length)) {
2040 assert(LSB > 0 && "Bottom bit must be set");
2041 assert(LSB + Length < BitSize && "Top bit must be set");
2042 Start = 63 - (LSB - 1);
2043 End = 63 - (LSB + Length);
2044 return true;
2045 }
2046
2047 return false;
2048}
2049
2050unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode,
2052 const MachineInstr *MI) const {
2053 switch (Opcode) {
2054 case SystemZ::CHI:
2055 case SystemZ::CGHI:
2056 if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
2057 return 0;
2058 break;
2059 case SystemZ::CLFI:
2060 case SystemZ::CLGFI:
2061 if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
2062 return 0;
2063 break;
2064 case SystemZ::CL:
2065 case SystemZ::CLG:
2066 if (!STI.hasMiscellaneousExtensions())
2067 return 0;
2068 if (!(MI && MI->getOperand(3).getReg() == 0))
2069 return 0;
2070 break;
2071 }
2072 switch (Type) {
2074 switch (Opcode) {
2075 case SystemZ::CR:
2076 return SystemZ::CRJ;
2077 case SystemZ::CGR:
2078 return SystemZ::CGRJ;
2079 case SystemZ::CHI:
2080 return SystemZ::CIJ;
2081 case SystemZ::CGHI:
2082 return SystemZ::CGIJ;
2083 case SystemZ::CLR:
2084 return SystemZ::CLRJ;
2085 case SystemZ::CLGR:
2086 return SystemZ::CLGRJ;
2087 case SystemZ::CLFI:
2088 return SystemZ::CLIJ;
2089 case SystemZ::CLGFI:
2090 return SystemZ::CLGIJ;
2091 default:
2092 return 0;
2093 }
2095 switch (Opcode) {
2096 case SystemZ::CR:
2097 return SystemZ::CRBReturn;
2098 case SystemZ::CGR:
2099 return SystemZ::CGRBReturn;
2100 case SystemZ::CHI:
2101 return SystemZ::CIBReturn;
2102 case SystemZ::CGHI:
2103 return SystemZ::CGIBReturn;
2104 case SystemZ::CLR:
2105 return SystemZ::CLRBReturn;
2106 case SystemZ::CLGR:
2107 return SystemZ::CLGRBReturn;
2108 case SystemZ::CLFI:
2109 return SystemZ::CLIBReturn;
2110 case SystemZ::CLGFI:
2111 return SystemZ::CLGIBReturn;
2112 default:
2113 return 0;
2114 }
2116 switch (Opcode) {
2117 case SystemZ::CR:
2118 return SystemZ::CRBCall;
2119 case SystemZ::CGR:
2120 return SystemZ::CGRBCall;
2121 case SystemZ::CHI:
2122 return SystemZ::CIBCall;
2123 case SystemZ::CGHI:
2124 return SystemZ::CGIBCall;
2125 case SystemZ::CLR:
2126 return SystemZ::CLRBCall;
2127 case SystemZ::CLGR:
2128 return SystemZ::CLGRBCall;
2129 case SystemZ::CLFI:
2130 return SystemZ::CLIBCall;
2131 case SystemZ::CLGFI:
2132 return SystemZ::CLGIBCall;
2133 default:
2134 return 0;
2135 }
2137 switch (Opcode) {
2138 case SystemZ::CR:
2139 return SystemZ::CRT;
2140 case SystemZ::CGR:
2141 return SystemZ::CGRT;
2142 case SystemZ::CHI:
2143 return SystemZ::CIT;
2144 case SystemZ::CGHI:
2145 return SystemZ::CGIT;
2146 case SystemZ::CLR:
2147 return SystemZ::CLRT;
2148 case SystemZ::CLGR:
2149 return SystemZ::CLGRT;
2150 case SystemZ::CLFI:
2151 return SystemZ::CLFIT;
2152 case SystemZ::CLGFI:
2153 return SystemZ::CLGIT;
2154 case SystemZ::CL:
2155 return SystemZ::CLT;
2156 case SystemZ::CLG:
2157 return SystemZ::CLGT;
2158 default:
2159 return 0;
2160 }
2161 }
2162 return 0;
2163}
2164
2166 // If we during isel used a load-and-test as a compare with 0, the
2167 // def operand is dead.
2168 return (MI.getOpcode() == SystemZ::LTEBR ||
2169 MI.getOpcode() == SystemZ::LTDBR ||
2170 MI.getOpcode() == SystemZ::LTXBR) &&
2171 MI.getOperand(0).isDead();
2172}
2173
2175 if (isLoadAndTestAsCmp(Compare))
2176 return true;
2177 return Compare.isCompare() && Compare.getNumExplicitOperands() == 2 &&
2178 Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0;
2179}
2180
2183 assert(isCompareZero(Compare) && "Expected a compare with 0.");
2184 return Compare.getOperand(isLoadAndTestAsCmp(Compare) ? 1 : 0).getReg();
2185}
2186
2189 assert(MBBI->isCompare() && MBBI->getOperand(0).isReg() &&
2190 MBBI->getOperand(1).isReg() && !MBBI->mayLoad() &&
2191 "Not a compare reg/reg.");
2192
2193 MachineBasicBlock *MBB = MBBI->getParent();
2194 bool CCLive = true;
2196 for (MachineInstr &MI : llvm::make_range(std::next(MBBI), MBB->end())) {
2197 if (MI.readsRegister(SystemZ::CC, /*TRI=*/nullptr)) {
2198 unsigned Flags = MI.getDesc().TSFlags;
2199 if ((Flags & SystemZII::CCMaskFirst) || (Flags & SystemZII::CCMaskLast))
2200 CCUsers.push_back(&MI);
2201 else
2202 return false;
2203 }
2204 if (MI.definesRegister(SystemZ::CC, /*TRI=*/nullptr)) {
2205 CCLive = false;
2206 break;
2207 }
2208 }
2209 if (CCLive) {
2210 LiveRegUnits LiveRegs(*MBB->getParent()->getSubtarget().getRegisterInfo());
2211 LiveRegs.addLiveOuts(*MBB);
2212 if (!LiveRegs.available(SystemZ::CC))
2213 return false;
2214 }
2215
2216 // Update all CC users.
2217 for (unsigned Idx = 0; Idx < CCUsers.size(); ++Idx) {
2218 unsigned Flags = CCUsers[Idx]->getDesc().TSFlags;
2219 unsigned FirstOpNum = ((Flags & SystemZII::CCMaskFirst) ?
2220 0 : CCUsers[Idx]->getNumExplicitOperands() - 2);
2221 MachineOperand &CCMaskMO = CCUsers[Idx]->getOperand(FirstOpNum + 1);
2222 unsigned NewCCMask = SystemZ::reverseCCMask(CCMaskMO.getImm());
2223 CCMaskMO.setImm(NewCCMask);
2224 }
2225
2226 return true;
2227}
2228
2229unsigned SystemZ::reverseCCMask(unsigned CCMask) {
2230 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
2233 (CCMask & SystemZ::CCMASK_CMP_UO));
2234}
2235
2237 MachineFunction &MF = *MBB->getParent();
2238 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
2239 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
2240 return NewMBB;
2241}
2242
2251
2259
2260unsigned SystemZInstrInfo::getLoadAndTrap(unsigned Opcode) const {
2261 if (!STI.hasLoadAndTrap())
2262 return 0;
2263 switch (Opcode) {
2264 case SystemZ::L:
2265 case SystemZ::LY:
2266 return SystemZ::LAT;
2267 case SystemZ::LG:
2268 return SystemZ::LGAT;
2269 case SystemZ::LFH:
2270 return SystemZ::LFHAT;
2271 case SystemZ::LLGF:
2272 return SystemZ::LLGFAT;
2273 case SystemZ::LLGT:
2274 return SystemZ::LLGTAT;
2275 }
2276 return 0;
2277}
2278
2281 unsigned Reg, uint64_t Value) const {
2282 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
2283 unsigned Opcode = 0;
2284 if (isInt<16>(Value))
2285 Opcode = SystemZ::LGHI;
2286 else if (SystemZ::isImmLL(Value))
2287 Opcode = SystemZ::LLILL;
2288 else if (SystemZ::isImmLH(Value)) {
2289 Opcode = SystemZ::LLILH;
2290 Value >>= 16;
2291 }
2292 else if (isInt<32>(Value))
2293 Opcode = SystemZ::LGFI;
2294 if (Opcode) {
2295 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
2296 return;
2297 }
2298
2299 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
2300 assert (MRI.isSSA() && "Huge values only handled before reg-alloc .");
2301 Register Reg0 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
2302 Register Reg1 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
2303 BuildMI(MBB, MBBI, DL, get(SystemZ::IMPLICIT_DEF), Reg0);
2304 BuildMI(MBB, MBBI, DL, get(SystemZ::IIHF64), Reg1)
2305 .addReg(Reg0).addImm(Value >> 32);
2306 BuildMI(MBB, MBBI, DL, get(SystemZ::IILF64), Reg)
2307 .addReg(Reg1).addImm(Value & ((uint64_t(1) << 32) - 1));
2308}
2309
2311 StringRef &ErrInfo) const {
2312 const MCInstrDesc &MCID = MI.getDesc();
2313 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
2314 if (I >= MCID.getNumOperands())
2315 break;
2316 const MachineOperand &Op = MI.getOperand(I);
2317 const MCOperandInfo &MCOI = MCID.operands()[I];
2318 // Addressing modes have register and immediate operands. Op should be a
2319 // register (or frame index) operand if MCOI.RegClass contains a valid
2320 // register class, or an immediate otherwise.
2321 if (MCOI.OperandType == MCOI::OPERAND_MEMORY &&
2322 ((MCOI.RegClass != -1 && !Op.isReg() && !Op.isFI()) ||
2323 (MCOI.RegClass == -1 && !Op.isImm()))) {
2324 ErrInfo = "Addressing mode operands corrupt!";
2325 return false;
2326 }
2327 }
2328
2329 return true;
2330}
2331
2334 const MachineInstr &MIb) const {
2335
2336 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand())
2337 return false;
2338
2339 // If mem-operands show that the same address Value is used by both
2340 // instructions, check for non-overlapping offsets and widths. Not
2341 // sure if a register based analysis would be an improvement...
2342
2343 MachineMemOperand *MMOa = *MIa.memoperands_begin();
2344 MachineMemOperand *MMOb = *MIb.memoperands_begin();
2345 const Value *VALa = MMOa->getValue();
2346 const Value *VALb = MMOb->getValue();
2347 bool SameVal = (VALa && VALb && (VALa == VALb));
2348 if (!SameVal) {
2349 const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
2350 const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
2351 if (PSVa && PSVb && (PSVa == PSVb))
2352 SameVal = true;
2353 }
2354 if (SameVal) {
2355 int OffsetA = MMOa->getOffset(), OffsetB = MMOb->getOffset();
2356 LocationSize WidthA = MMOa->getSize(), WidthB = MMOb->getSize();
2357 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
2358 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
2359 LocationSize LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
2360 if (LowWidth.hasValue() &&
2361 LowOffset + (int)LowWidth.getValue() <= HighOffset)
2362 return true;
2363 }
2364
2365 return false;
2366}
2367
2369 const Register Reg,
2370 int64_t &ImmVal) const {
2371
2372 if (MI.getOpcode() == SystemZ::VGBM && Reg == MI.getOperand(0).getReg()) {
2373 ImmVal = MI.getOperand(1).getImm();
2374 // TODO: Handle non-0 values
2375 return ImmVal == 0;
2376 }
2377
2378 return false;
2379}
2380
2381std::optional<DestSourcePair>
2383 // if MI is a simple single-register copy operation, return operand pair
2384 if (MI.isMoveReg())
2385 return DestSourcePair(MI.getOperand(0), MI.getOperand(1));
2386
2387 return std::nullopt;
2388}
2389
2390std::pair<unsigned, unsigned>
2392 return std::make_pair(TF, 0u);
2393}
2394
2397 using namespace SystemZII;
2398
2399 static const std::pair<unsigned, const char *> TargetFlags[] = {
2400 {MO_ADA_DATA_SYMBOL_ADDR, "systemz-ada-datasymboladdr"},
2401 {MO_ADA_INDIRECT_FUNC_DESC, "systemz-ada-indirectfuncdesc"},
2402 {MO_ADA_DIRECT_FUNC_DESC, "systemz-ada-directfuncdesc"}};
2403 return ArrayRef(TargetFlags);
2404}
2405
2407 return MCInstBuilder(SystemZ::NOPR).addReg(0);
2408}
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
DXIL Forward Handle Accesses
IRTranslator LLVM IR MI
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
A set of register units.
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:483
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag)
static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI)
static void transferMIFlag(MachineInstr *OldMI, MachineInstr *NewMI, MachineInstr::MIFlag Flag)
static int isSimpleMove(const MachineInstr &MI, int &FrameIndex, unsigned Flag)
static LogicOp interpretAndImmediate(unsigned Opcode)
static uint64_t allOnes(unsigned int Count)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A debug info location.
Definition DebugLoc.h:123
SlotIndexes * getSlotIndexes() const
VNInfo::Allocator & getVNInfoAllocator()
LiveRange & getRegUnit(MCRegUnit Unit)
Return the live range for register unit Unit.
SlotIndex ReplaceMachineInstrInMaps(MachineInstr &MI, MachineInstr &NewMI)
This class represents the liveness of a register, stack slot, etc.
bool liveAt(SlotIndex index) const
LLVM_ABI VNInfo * createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc)
createDeadDef - Make sure the range has a value defined at Def.
A set of register units used to track register liveness.
LLVM_ABI void replaceKillInstruction(Register Reg, MachineInstr &OldMI, MachineInstr &NewMI)
replaceKillInstruction - Update register kill info by replacing a kill instruction with a new one.
bool hasValue() const
TypeSize getValue() const
MCInstBuilder & addReg(MCRegister Reg)
Add a new register operand.
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Describe properties that are true of each instruction in the target description file.
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition MCInstrDesc.h:86
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
static MCRegister from(unsigned Val)
Check the provided unsigned value is a valid MCRegister.
Definition MCRegister.h:77
LLVM_ABI void transferSuccessorsAndUpdatePHIs(MachineBasicBlock *FromMBB)
Transfers all the successors, as in transferSuccessors, and update PHI operands in the successor bloc...
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineInstrBundleIterator< MachineInstr > iterator
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
uint64_t getMaxCallFrameSize() const
Return the maximum size of a call frame that must be allocated for an outgoing function call.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
BasicBlockListType::iterator iterator
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *BB=nullptr, std::optional< UniqueBBID > BBID=std::nullopt)
CreateMachineInstr - Allocate a new MachineInstr.
void insert(iterator MBBI, MachineBasicBlock *MBB)
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addFrameIndex(int Idx) const
const MachineInstrBuilder & addRegMask(const uint32_t *Mask) const
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
const MachineInstrBuilder & addMemOperand(MachineMemOperand *MMO) const
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
mop_range operands()
bool registerDefIsDead(Register Reg, const TargetRegisterInfo *TRI) const
Returns true if the register is dead in this machine instruction.
bool definesRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr fully defines the specified register.
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
void setFlag(MIFlag Flag)
Set a MI flag.
const MachineOperand & getOperand(unsigned i) const
MachineOperand * findRegisterDefOperand(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false)
Wrapper for findRegisterDefOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
LLVM_ABI bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI defined a register without a use.
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of the memory reference.
const PseudoSourceValue * getPseudoValue() const
bool isAtomic() const
Returns true if this operation has an atomic ordering requirement of unordered or higher,...
const Value * getValue() const
Return the base address of the memory access.
int64_t getOffset() const
For normal values, this is a byte offset added to the base address.
MachineOperand class - Representation of each machine instruction operand.
void setImm(int64_t immVal)
int64_t getImm() const
void setIsDead(bool Val=true)
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
void setIsKill(bool Val=true)
static MachineOperand CreateImm(int64_t Val)
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI bool hasOneNonDBGUse(Register RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
bool use_nodbg_empty(Register RegNo) const
use_nodbg_empty - Return true if there are no non-Debug instructions using the specified register.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI void setRegClass(Register Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
LLVM_ABI const TargetRegisterClass * constrainRegClass(Register Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
MI-level patchpoint operands.
Definition StackMaps.h:77
uint32_t getNumPatchBytes() const
Return the number of patchable bytes the given patchpoint should emit.
Definition StackMaps.h:105
Special value supplied for machine level alias analysis.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
SlotIndex getInstructionIndex(const MachineInstr &MI, bool IgnoreBundle=false) const
Returns the base index for the given instruction.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
unsigned getLoadAndTrap(unsigned Opcode) const
void storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register SrcReg, bool isKill, int FrameIndex, const TargetRegisterClass *RC, Register VReg, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
Register isLoadFromStackSlotPostFE(const MachineInstr &MI, int &FrameIndex) const override
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
bool isProfitableToIfCvt(MachineBasicBlock &MBB, unsigned NumCycles, unsigned ExtraPredCycles, BranchProbability Probability) const override
unsigned getLoadAndTest(unsigned Opcode) const
MCInst getNop() const override
bool isPredicable(const MachineInstr &MI) const override
Register isStoreToStackSlotPostFE(const MachineInstr &MI, int &FrameIndex) const override
bool isStackSlotCopy(const MachineInstr &MI, int &DestFrameIndex, int &SrcFrameIndex) const override
MachineInstr * convertToThreeAddress(MachineInstr &MI, LiveVariables *LV, LiveIntervals *LIS) const override
unsigned getOpcodeForOffset(unsigned Opcode, int64_t Offset, const MachineInstr *MI=nullptr) const
unsigned getInstSizeInBytes(const MachineInstr &MI) const override
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
Register isStoreToStackSlot(const MachineInstr &MI, int &FrameIndex) const override
bool getConstValDefinedInReg(const MachineInstr &MI, const Register Reg, int64_t &ImmVal) const override
bool isAssociativeAndCommutative(const MachineInstr &Inst, bool Invert) const override
MachineInstr * foldMemoryOperandImpl(MachineFunction &MF, MachineInstr &MI, ArrayRef< unsigned > Ops, int FrameIndex, MachineInstr *&CopyMI, LiveIntervals *LIS=nullptr, VirtRegMap *VRM=nullptr) const override
bool hasDisplacementPairInsn(unsigned Opcode) const
MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned CommuteOpIdx1, unsigned CommuteOpIdx2) const override
Commutes the operands in the given instruction by changing the operands order and/or changing the ins...
void loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, Register DestReg, int FrameIdx, const TargetRegisterClass *RC, Register VReg, unsigned SubReg=0, MachineInstr::MIFlag Flags=MachineInstr::NoFlags) const override
ArrayRef< std::pair< unsigned, const char * > > getSerializableDirectMachineOperandTargetFlags() const override
void insertSelect(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL, Register DstReg, ArrayRef< MachineOperand > Cond, Register TrueReg, Register FalseReg) const override
std::optional< unsigned > getInverseOpcode(unsigned Opcode) const override
bool isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles, BranchProbability Probability) const override
bool isCompareZero(const MachineInstr &Compare) const
SystemZII::Branch getBranchInfo(const MachineInstr &MI) const
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, const DebugLoc &DL, Register DestReg, Register SrcReg, bool KillSrc, bool RenamableDest=false, bool RenamableSrc=false) const override
bool areMemAccessesTriviallyDisjoint(const MachineInstr &MIa, const MachineInstr &MIb) const override
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
bool isLoadAndTestAsCmp(const MachineInstr &MI) const
unsigned getFusedCompare(unsigned Opcode, SystemZII::FusedCompareType Type, const MachineInstr *MI=nullptr) const
bool expandPostRAPseudo(MachineInstr &MBBI) const override
bool analyzeCompare(const MachineInstr &MI, Register &SrcReg, Register &SrcReg2, int64_t &Mask, int64_t &Value) const override
bool verifyInstruction(const MachineInstr &MI, StringRef &ErrInfo) const override
void getLoadStoreOpcodes(const TargetRegisterClass *RC, unsigned &LoadOpcode, unsigned &StoreOpcode) const
bool isRxSBGMask(uint64_t Mask, unsigned BitSize, unsigned &Start, unsigned &End) const
bool foldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, Register Reg, MachineRegisterInfo *MRI) const override
bool canInsertSelect(const MachineBasicBlock &, ArrayRef< MachineOperand > Cond, Register, Register, Register, int &, int &, int &) const override
std::optional< DestSourcePair > isCopyInstrImpl(const MachineInstr &MI) const override
Register getCompareSourceReg(const MachineInstr &Compare) const
std::pair< unsigned, unsigned > decomposeMachineOperandsTargetFlags(unsigned TF) const override
bool prepareCompareSwapOperands(MachineBasicBlock::iterator MBBI) const
Register isLoadFromStackSlot(const MachineInstr &MI, int &FrameIndex) const override
SystemZInstrInfo(const SystemZSubtarget &STI)
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
void loadImmediate(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned Reg, uint64_t Value) const
bool PredicateInstruction(MachineInstr &MI, ArrayRef< MachineOperand > Pred) const override
virtual MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const
This method commutes the operands of the given machine instruction MI.
const MCAsmInfo * getMCAsmInfo() const
Return target specific asm information.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Target - Wrapper for Target specific information.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:91
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static unsigned getAccessSize(unsigned int Flags)
unsigned getFirstReg(unsigned Reg)
MachineBasicBlock * splitBlockBefore(MachineBasicBlock::iterator MI, MachineBasicBlock *MBB)
const unsigned CCMASK_CMP_GT
Definition SystemZ.h:38
const unsigned CCMASK_ANY
Definition SystemZ.h:32
static bool isImmLL(uint64_t Val)
Definition SystemZ.h:162
static bool isImmLH(uint64_t Val)
Definition SystemZ.h:167
MachineBasicBlock * emitBlockAfter(MachineBasicBlock *MBB)
unsigned reverseCCMask(unsigned CCMask)
const unsigned IPM_CC
Definition SystemZ.h:113
const unsigned CCMASK_CMP_EQ
Definition SystemZ.h:36
const unsigned CCMASK_ICMP
Definition SystemZ.h:48
MachineBasicBlock * splitBlockAfter(MachineBasicBlock::iterator MI, MachineBasicBlock *MBB)
int32_t getTargetMemOpcode(uint32_t Opcode)
const unsigned CCMASK_CMP_LT
Definition SystemZ.h:37
const unsigned CCMASK_CMP_NE
Definition SystemZ.h:39
bool isHighReg(unsigned int Reg)
const unsigned CCMASK_CMP_UO
Definition SystemZ.h:44
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
RegState
Flags to represent properties of register accesses.
@ Implicit
Not emitted register (e.g. carry, or temporary result).
@ Undef
Value of the register doesn't matter.
constexpr RegState getKillRegState(bool B)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static const MachineInstrBuilder & addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset=0, bool mem=true)
addFrameReference - This function is used to add a reference to the base of an abstract object on the...
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition MathExtras.h:273
constexpr size_t range_size(R &&Range)
Returns the size of the Range, i.e., the number of elements.
Definition STLExtras.h:1694
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr RegState getUndefRegState(bool B)
Matching combinators.