Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1 | //===-- SystemZInstrInfo.cpp - SystemZ instruction information ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the SystemZ implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "SystemZInstrInfo.h" |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/SystemZMCTargetDesc.h" |
| 16 | #include "SystemZ.h" |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 17 | #include "SystemZInstrBuilder.h" |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 18 | #include "SystemZSubtarget.h" |
Jonas Paulsson | 4b017e6 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LiveInterval.h" |
Matthias Braun | f842297 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveIntervals.h" |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/LiveVariables.h" |
| 23 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 24 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 25 | #include "llvm/CodeGen/MachineFunction.h" |
| 26 | #include "llvm/CodeGen/MachineInstr.h" |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 28 | #include "llvm/CodeGen/MachineOperand.h" |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/SlotIndexes.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCInstrDesc.h" |
| 34 | #include "llvm/MC/MCRegisterInfo.h" |
| 35 | #include "llvm/Support/BranchProbability.h" |
| 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 39 | #include <cassert> |
| 40 | #include <cstdint> |
| 41 | #include <iterator> |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 42 | |
Chandler Carruth | d174b72 | 2014-04-22 02:03:14 +0000 | [diff] [blame] | 43 | using namespace llvm; |
| 44 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 45 | #define GET_INSTRINFO_CTOR_DTOR |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 46 | #define GET_INSTRMAP_INFO |
| 47 | #include "SystemZGenInstrInfo.inc" |
| 48 | |
Jonas Paulsson | 4b017e6 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 49 | #define DEBUG_TYPE "systemz-II" |
| 50 | STATISTIC(LOCRMuxJumps, "Number of LOCRMux jump-sequences (lower is better)"); |
| 51 | |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 52 | // Return a mask with Count low bits set. |
| 53 | static uint64_t allOnes(unsigned int Count) { |
| 54 | return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1; |
| 55 | } |
| 56 | |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 57 | // Reg should be a 32-bit GPR. Return true if it is a high register rather |
| 58 | // than a low register. |
| 59 | static bool isHighReg(unsigned int Reg) { |
| 60 | if (SystemZ::GRH32BitRegClass.contains(Reg)) |
| 61 | return true; |
| 62 | assert(SystemZ::GR32BitRegClass.contains(Reg) && "Invalid GRX32"); |
| 63 | return false; |
| 64 | } |
| 65 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 66 | // Pin the vtable to this file. |
| 67 | void SystemZInstrInfo::anchor() {} |
| 68 | |
Eric Christopher | 673b3af | 2014-06-27 07:01:17 +0000 | [diff] [blame] | 69 | SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 70 | : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP), |
Eric Christopher | 673b3af | 2014-06-27 07:01:17 +0000 | [diff] [blame] | 71 | RI(), STI(sti) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | // MI is a 128-bit load or store. Split it into two 64-bit loads or stores, |
| 75 | // each having the opcode given by NewOpcode. |
| 76 | void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI, |
| 77 | unsigned NewOpcode) const { |
| 78 | MachineBasicBlock *MBB = MI->getParent(); |
| 79 | MachineFunction &MF = *MBB->getParent(); |
| 80 | |
| 81 | // Get two load or store instructions. Use the original instruction for one |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 82 | // of them (arbitrarily the second here) and create a clone for the other. |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 83 | MachineInstr *EarlierMI = MF.CloneMachineInstr(&*MI); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 84 | MBB->insert(MI, EarlierMI); |
| 85 | |
Jonas Paulsson | 8a7bd24 | 2017-03-17 06:47:08 +0000 | [diff] [blame] | 86 | // Set up the two 64-bit registers and remember super reg and its flags. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 87 | MachineOperand &HighRegOp = EarlierMI->getOperand(0); |
| 88 | MachineOperand &LowRegOp = MI->getOperand(0); |
Jonas Paulsson | 8a7bd24 | 2017-03-17 06:47:08 +0000 | [diff] [blame] | 89 | unsigned Reg128 = LowRegOp.getReg(); |
| 90 | unsigned Reg128Killed = getKillRegState(LowRegOp.isKill()); |
| 91 | unsigned Reg128Undef = getUndefRegState(LowRegOp.isUndef()); |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 92 | HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64)); |
| 93 | LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 94 | |
Jonas Paulsson | 8a7bd24 | 2017-03-17 06:47:08 +0000 | [diff] [blame] | 95 | if (MI->mayStore()) { |
| 96 | // Add implicit uses of the super register in case one of the subregs is |
| 97 | // undefined. We could track liveness and skip storing an undefined |
| 98 | // subreg, but this is hopefully rare (discovered with llvm-stress). |
| 99 | // If Reg128 was killed, set kill flag on MI. |
| 100 | unsigned Reg128UndefImpl = (Reg128Undef | RegState::Implicit); |
| 101 | MachineInstrBuilder(MF, EarlierMI).addReg(Reg128, Reg128UndefImpl); |
| 102 | MachineInstrBuilder(MF, MI).addReg(Reg128, (Reg128UndefImpl | Reg128Killed)); |
| 103 | } |
| 104 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 105 | // The address in the first (high) instruction is already correct. |
| 106 | // Adjust the offset in the second (low) instruction. |
| 107 | MachineOperand &HighOffsetOp = EarlierMI->getOperand(2); |
| 108 | MachineOperand &LowOffsetOp = MI->getOperand(2); |
| 109 | LowOffsetOp.setImm(LowOffsetOp.getImm() + 8); |
| 110 | |
Jonas Paulsson | 1e86485 | 2017-04-24 12:40:28 +0000 | [diff] [blame] | 111 | // Clear the kill flags on the registers in the first instruction. |
| 112 | if (EarlierMI->getOperand(0).isReg() && EarlierMI->getOperand(0).isUse()) |
| 113 | EarlierMI->getOperand(0).setIsKill(false); |
Jonas Paulsson | 63a2b68 | 2015-10-10 07:14:24 +0000 | [diff] [blame] | 114 | EarlierMI->getOperand(1).setIsKill(false); |
Jonas Paulsson | 7da3820 | 2015-10-26 15:03:41 +0000 | [diff] [blame] | 115 | EarlierMI->getOperand(3).setIsKill(false); |
Jonas Paulsson | 63a2b68 | 2015-10-10 07:14:24 +0000 | [diff] [blame] | 116 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 117 | // Set the opcodes. |
| 118 | unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm()); |
| 119 | unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm()); |
| 120 | assert(HighOpcode && LowOpcode && "Both offsets should be in range"); |
| 121 | |
| 122 | EarlierMI->setDesc(get(HighOpcode)); |
| 123 | MI->setDesc(get(LowOpcode)); |
| 124 | } |
| 125 | |
| 126 | // Split ADJDYNALLOC instruction MI. |
| 127 | void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const { |
| 128 | MachineBasicBlock *MBB = MI->getParent(); |
| 129 | MachineFunction &MF = *MBB->getParent(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 130 | MachineFrameInfo &MFFrame = MF.getFrameInfo(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 131 | MachineOperand &OffsetMO = MI->getOperand(2); |
| 132 | |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 133 | uint64_t Offset = (MFFrame.getMaxCallFrameSize() + |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 134 | SystemZMC::CallFrameSize + |
| 135 | OffsetMO.getImm()); |
| 136 | unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset); |
| 137 | assert(NewOpcode && "No support for huge argument lists yet"); |
| 138 | MI->setDesc(get(NewOpcode)); |
| 139 | OffsetMO.setImm(Offset); |
| 140 | } |
| 141 | |
Richard Sandiford | 0124023 | 2013-10-01 13:02:28 +0000 | [diff] [blame] | 142 | // MI is an RI-style pseudo instruction. Replace it with LowOpcode |
| 143 | // if the first operand is a low GR32 and HighOpcode if the first operand |
| 144 | // is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand |
| 145 | // and HighOpcode takes an unsigned 32-bit operand. In those cases, |
| 146 | // MI has the same kind of operand as LowOpcode, so needs to be converted |
| 147 | // if HighOpcode is used. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 148 | void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode, |
Richard Sandiford | 0124023 | 2013-10-01 13:02:28 +0000 | [diff] [blame] | 149 | unsigned HighOpcode, |
| 150 | bool ConvertHigh) const { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 151 | unsigned Reg = MI.getOperand(0).getReg(); |
Richard Sandiford | 0124023 | 2013-10-01 13:02:28 +0000 | [diff] [blame] | 152 | bool IsHigh = isHighReg(Reg); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 153 | MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode)); |
Richard Sandiford | 0124023 | 2013-10-01 13:02:28 +0000 | [diff] [blame] | 154 | if (IsHigh && ConvertHigh) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 155 | MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm())); |
Richard Sandiford | 0124023 | 2013-10-01 13:02:28 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Richard Sandiford | 42a694f | 2013-10-01 14:53:46 +0000 | [diff] [blame] | 158 | // MI is a three-operand RIE-style pseudo instruction. Replace it with |
Jonas Paulsson | 18d877f | 2015-10-09 07:19:16 +0000 | [diff] [blame] | 159 | // LowOpcodeK if the registers are both low GR32s, otherwise use a move |
Richard Sandiford | 42a694f | 2013-10-01 14:53:46 +0000 | [diff] [blame] | 160 | // followed by HighOpcode or LowOpcode, depending on whether the target |
| 161 | // is a high or low GR32. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 162 | void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode, |
Richard Sandiford | 42a694f | 2013-10-01 14:53:46 +0000 | [diff] [blame] | 163 | unsigned LowOpcodeK, |
| 164 | unsigned HighOpcode) const { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 165 | unsigned DestReg = MI.getOperand(0).getReg(); |
| 166 | unsigned SrcReg = MI.getOperand(1).getReg(); |
Richard Sandiford | 42a694f | 2013-10-01 14:53:46 +0000 | [diff] [blame] | 167 | bool DestIsHigh = isHighReg(DestReg); |
| 168 | bool SrcIsHigh = isHighReg(SrcReg); |
| 169 | if (!DestIsHigh && !SrcIsHigh) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 170 | MI.setDesc(get(LowOpcodeK)); |
Richard Sandiford | 42a694f | 2013-10-01 14:53:46 +0000 | [diff] [blame] | 171 | else { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 172 | emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg, |
Jonas Paulsson | a9bb00d | 2017-01-18 08:32:54 +0000 | [diff] [blame] | 173 | SystemZ::LR, 32, MI.getOperand(1).isKill(), |
| 174 | MI.getOperand(1).isUndef()); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 175 | MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode)); |
| 176 | MI.getOperand(1).setReg(DestReg); |
| 177 | MI.tieOperands(0, 1); |
Richard Sandiford | 42a694f | 2013-10-01 14:53:46 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 181 | // MI is an RXY-style pseudo instruction. Replace it with LowOpcode |
| 182 | // if the first operand is a low GR32 and HighOpcode if the first operand |
| 183 | // is a high GR32. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 184 | void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode, |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 185 | unsigned HighOpcode) const { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 186 | unsigned Reg = MI.getOperand(0).getReg(); |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 187 | unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 188 | MI.getOperand(2).getImm()); |
| 189 | MI.setDesc(get(Opcode)); |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 192 | // MI is a load-on-condition pseudo instruction with a single register |
| 193 | // (source or destination) operand. Replace it with LowOpcode if the |
| 194 | // register is a low GR32 and HighOpcode if the register is a high GR32. |
| 195 | void SystemZInstrInfo::expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode, |
| 196 | unsigned HighOpcode) const { |
| 197 | unsigned Reg = MI.getOperand(0).getReg(); |
| 198 | unsigned Opcode = isHighReg(Reg) ? HighOpcode : LowOpcode; |
| 199 | MI.setDesc(get(Opcode)); |
| 200 | } |
| 201 | |
| 202 | // MI is a load-register-on-condition pseudo instruction. Replace it with |
| 203 | // LowOpcode if source and destination are both low GR32s and HighOpcode if |
| 204 | // source and destination are both high GR32s. |
| 205 | void SystemZInstrInfo::expandLOCRPseudo(MachineInstr &MI, unsigned LowOpcode, |
| 206 | unsigned HighOpcode) const { |
| 207 | unsigned DestReg = MI.getOperand(0).getReg(); |
| 208 | unsigned SrcReg = MI.getOperand(2).getReg(); |
| 209 | bool DestIsHigh = isHighReg(DestReg); |
| 210 | bool SrcIsHigh = isHighReg(SrcReg); |
| 211 | |
| 212 | if (!DestIsHigh && !SrcIsHigh) |
| 213 | MI.setDesc(get(LowOpcode)); |
| 214 | else if (DestIsHigh && SrcIsHigh) |
| 215 | MI.setDesc(get(HighOpcode)); |
Jonas Paulsson | 4b017e6 | 2017-11-10 08:46:26 +0000 | [diff] [blame] | 216 | else |
| 217 | LOCRMuxJumps++; |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 218 | |
| 219 | // If we were unable to implement the pseudo with a single instruction, we |
| 220 | // need to convert it back into a branch sequence. This cannot be done here |
| 221 | // since the caller of expandPostRAPseudo does not handle changes to the CFG |
| 222 | // correctly. This change is defered to the SystemZExpandPseudo pass. |
| 223 | } |
| 224 | |
Richard Sandiford | 21235a2 | 2013-10-01 12:49:07 +0000 | [diff] [blame] | 225 | // MI is an RR-style pseudo instruction that zero-extends the low Size bits |
| 226 | // of one GRX32 into another. Replace it with LowOpcode if both operands |
| 227 | // are low registers, otherwise use RISB[LH]G. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 228 | void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode, |
Richard Sandiford | 21235a2 | 2013-10-01 12:49:07 +0000 | [diff] [blame] | 229 | unsigned Size) const { |
Jonas Paulsson | 808c89f | 2017-03-22 06:03:32 +0000 | [diff] [blame] | 230 | MachineInstrBuilder MIB = |
| 231 | emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), |
| 232 | MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode, |
| 233 | Size, MI.getOperand(1).isKill(), MI.getOperand(1).isUndef()); |
| 234 | |
| 235 | // Keep the remaining operands as-is. |
| 236 | for (unsigned I = 2; I < MI.getNumOperands(); ++I) |
| 237 | MIB.add(MI.getOperand(I)); |
| 238 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 239 | MI.eraseFromParent(); |
Richard Sandiford | 21235a2 | 2013-10-01 12:49:07 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Marcin Koscielnicki | aef3b5b | 2016-04-24 13:57:49 +0000 | [diff] [blame] | 242 | void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const { |
| 243 | MachineBasicBlock *MBB = MI->getParent(); |
| 244 | MachineFunction &MF = *MBB->getParent(); |
Jonas Paulsson | 081b5a1 | 2017-05-24 13:15:48 +0000 | [diff] [blame] | 245 | const unsigned Reg64 = MI->getOperand(0).getReg(); |
| 246 | const unsigned Reg32 = RI.getSubReg(Reg64, SystemZ::subreg_l32); |
Marcin Koscielnicki | aef3b5b | 2016-04-24 13:57:49 +0000 | [diff] [blame] | 247 | |
Jonas Paulsson | 081b5a1 | 2017-05-24 13:15:48 +0000 | [diff] [blame] | 248 | // EAR can only load the low subregister so us a shift for %a0 to produce |
| 249 | // the GR containing %a0 and %a1. |
Marcin Koscielnicki | aef3b5b | 2016-04-24 13:57:49 +0000 | [diff] [blame] | 250 | |
| 251 | // ear <reg>, %a0 |
Jonas Paulsson | 081b5a1 | 2017-05-24 13:15:48 +0000 | [diff] [blame] | 252 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::EAR), Reg32) |
| 253 | .addReg(SystemZ::A0) |
| 254 | .addReg(Reg64, RegState::ImplicitDefine); |
Marcin Koscielnicki | aef3b5b | 2016-04-24 13:57:49 +0000 | [diff] [blame] | 255 | |
| 256 | // sllg <reg>, <reg>, 32 |
Jonas Paulsson | 081b5a1 | 2017-05-24 13:15:48 +0000 | [diff] [blame] | 257 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::SLLG), Reg64) |
| 258 | .addReg(Reg64) |
| 259 | .addReg(0) |
| 260 | .addImm(32); |
Marcin Koscielnicki | aef3b5b | 2016-04-24 13:57:49 +0000 | [diff] [blame] | 261 | |
| 262 | // ear <reg>, %a1 |
Jonas Paulsson | 081b5a1 | 2017-05-24 13:15:48 +0000 | [diff] [blame] | 263 | BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::EAR), Reg32) |
| 264 | .addReg(SystemZ::A1); |
Marcin Koscielnicki | aef3b5b | 2016-04-24 13:57:49 +0000 | [diff] [blame] | 265 | |
| 266 | // lg <reg>, 40(<reg>) |
| 267 | MI->setDesc(get(SystemZ::LG)); |
Jonas Paulsson | 081b5a1 | 2017-05-24 13:15:48 +0000 | [diff] [blame] | 268 | MachineInstrBuilder(MF, MI).addReg(Reg64).addImm(40).addReg(0); |
Marcin Koscielnicki | aef3b5b | 2016-04-24 13:57:49 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 271 | // Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR |
| 272 | // DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg |
| 273 | // are low registers, otherwise use RISB[LH]G. Size is the number of bits |
| 274 | // taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR). |
| 275 | // KillSrc is true if this move is the last use of SrcReg. |
Jonas Paulsson | 808c89f | 2017-03-22 06:03:32 +0000 | [diff] [blame] | 276 | MachineInstrBuilder |
| 277 | SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB, |
| 278 | MachineBasicBlock::iterator MBBI, |
| 279 | const DebugLoc &DL, unsigned DestReg, |
| 280 | unsigned SrcReg, unsigned LowLowOpcode, |
| 281 | unsigned Size, bool KillSrc, |
| 282 | bool UndefSrc) const { |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 283 | unsigned Opcode; |
| 284 | bool DestIsHigh = isHighReg(DestReg); |
| 285 | bool SrcIsHigh = isHighReg(SrcReg); |
| 286 | if (DestIsHigh && SrcIsHigh) |
| 287 | Opcode = SystemZ::RISBHH; |
| 288 | else if (DestIsHigh && !SrcIsHigh) |
| 289 | Opcode = SystemZ::RISBHL; |
| 290 | else if (!DestIsHigh && SrcIsHigh) |
| 291 | Opcode = SystemZ::RISBLH; |
| 292 | else { |
Jonas Paulsson | 808c89f | 2017-03-22 06:03:32 +0000 | [diff] [blame] | 293 | return BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg) |
Jonas Paulsson | a9bb00d | 2017-01-18 08:32:54 +0000 | [diff] [blame] | 294 | .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc)); |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 295 | } |
| 296 | unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0); |
Jonas Paulsson | 808c89f | 2017-03-22 06:03:32 +0000 | [diff] [blame] | 297 | return BuildMI(MBB, MBBI, DL, get(Opcode), DestReg) |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 298 | .addReg(DestReg, RegState::Undef) |
Jonas Paulsson | a9bb00d | 2017-01-18 08:32:54 +0000 | [diff] [blame] | 299 | .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc)) |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 300 | .addImm(32 - Size).addImm(128 + 31).addImm(Rotate); |
| 301 | } |
| 302 | |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 303 | MachineInstr *SystemZInstrInfo::commuteInstructionImpl(MachineInstr &MI, |
| 304 | bool NewMI, |
| 305 | unsigned OpIdx1, |
| 306 | unsigned OpIdx2) const { |
| 307 | auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & { |
| 308 | if (NewMI) |
| 309 | return *MI.getParent()->getParent()->CloneMachineInstr(&MI); |
| 310 | return MI; |
| 311 | }; |
| 312 | |
| 313 | switch (MI.getOpcode()) { |
| 314 | case SystemZ::LOCRMux: |
| 315 | case SystemZ::LOCFHR: |
| 316 | case SystemZ::LOCR: |
| 317 | case SystemZ::LOCGR: { |
| 318 | auto &WorkingMI = cloneIfNew(MI); |
| 319 | // Invert condition. |
| 320 | unsigned CCValid = WorkingMI.getOperand(3).getImm(); |
| 321 | unsigned CCMask = WorkingMI.getOperand(4).getImm(); |
| 322 | WorkingMI.getOperand(4).setImm(CCMask ^ CCValid); |
| 323 | return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false, |
| 324 | OpIdx1, OpIdx2); |
| 325 | } |
| 326 | default: |
| 327 | return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); |
| 328 | } |
| 329 | } |
| 330 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 331 | // If MI is a simple load or store for a frame object, return the register |
| 332 | // it loads or stores and set FrameIndex to the index of the frame object. |
| 333 | // Return 0 otherwise. |
| 334 | // |
| 335 | // Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 336 | static int isSimpleMove(const MachineInstr &MI, int &FrameIndex, |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 337 | unsigned Flag) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 338 | const MCInstrDesc &MCID = MI.getDesc(); |
| 339 | if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() && |
| 340 | MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) { |
| 341 | FrameIndex = MI.getOperand(1).getIndex(); |
| 342 | return MI.getOperand(0).getReg(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 343 | } |
| 344 | return 0; |
| 345 | } |
| 346 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 347 | unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 348 | int &FrameIndex) const { |
| 349 | return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad); |
| 350 | } |
| 351 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 352 | unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 353 | int &FrameIndex) const { |
| 354 | return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore); |
| 355 | } |
| 356 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 357 | bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI, |
Richard Sandiford | c40f27b | 2013-07-05 14:38:48 +0000 | [diff] [blame] | 358 | int &DestFrameIndex, |
| 359 | int &SrcFrameIndex) const { |
| 360 | // Check for MVC 0(Length,FI1),0(FI2) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 361 | const MachineFrameInfo &MFI = MI.getParent()->getParent()->getFrameInfo(); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 362 | if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() || |
| 363 | MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() || |
| 364 | MI.getOperand(4).getImm() != 0) |
Richard Sandiford | c40f27b | 2013-07-05 14:38:48 +0000 | [diff] [blame] | 365 | return false; |
| 366 | |
| 367 | // Check that Length covers the full slots. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 368 | int64_t Length = MI.getOperand(2).getImm(); |
| 369 | unsigned FI1 = MI.getOperand(0).getIndex(); |
| 370 | unsigned FI2 = MI.getOperand(3).getIndex(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 371 | if (MFI.getObjectSize(FI1) != Length || |
| 372 | MFI.getObjectSize(FI2) != Length) |
Richard Sandiford | c40f27b | 2013-07-05 14:38:48 +0000 | [diff] [blame] | 373 | return false; |
| 374 | |
| 375 | DestFrameIndex = FI1; |
| 376 | SrcFrameIndex = FI2; |
| 377 | return true; |
| 378 | } |
| 379 | |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 380 | bool SystemZInstrInfo::analyzeBranch(MachineBasicBlock &MBB, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 381 | MachineBasicBlock *&TBB, |
| 382 | MachineBasicBlock *&FBB, |
| 383 | SmallVectorImpl<MachineOperand> &Cond, |
| 384 | bool AllowModify) const { |
| 385 | // Most of the code and comments here are boilerplate. |
| 386 | |
| 387 | // Start from the bottom of the block and work up, examining the |
| 388 | // terminator instructions. |
| 389 | MachineBasicBlock::iterator I = MBB.end(); |
| 390 | while (I != MBB.begin()) { |
| 391 | --I; |
| 392 | if (I->isDebugValue()) |
| 393 | continue; |
| 394 | |
| 395 | // Working from the bottom, when we see a non-terminator instruction, we're |
| 396 | // done. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 397 | if (!isUnpredicatedTerminator(*I)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 398 | break; |
| 399 | |
| 400 | // A terminator that isn't a branch can't easily be handled by this |
| 401 | // analysis. |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 402 | if (!I->isBranch()) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 403 | return true; |
| 404 | |
| 405 | // Can't handle indirect branches. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 406 | SystemZII::Branch Branch(getBranchInfo(*I)); |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 407 | if (!Branch.Target->isMBB()) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 408 | return true; |
| 409 | |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 410 | // Punt on compound branches. |
| 411 | if (Branch.Type != SystemZII::BranchNormal) |
| 412 | return true; |
| 413 | |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 414 | if (Branch.CCMask == SystemZ::CCMASK_ANY) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 415 | // Handle unconditional branches. |
| 416 | if (!AllowModify) { |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 417 | TBB = Branch.Target->getMBB(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 418 | continue; |
| 419 | } |
| 420 | |
| 421 | // If the block has any instructions after a JMP, delete them. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 422 | while (std::next(I) != MBB.end()) |
| 423 | std::next(I)->eraseFromParent(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 424 | |
| 425 | Cond.clear(); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 426 | FBB = nullptr; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 427 | |
| 428 | // Delete the JMP if it's equivalent to a fall-through. |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 429 | if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 430 | TBB = nullptr; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 431 | I->eraseFromParent(); |
| 432 | I = MBB.end(); |
| 433 | continue; |
| 434 | } |
| 435 | |
| 436 | // TBB is used to indicate the unconditinal destination. |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 437 | TBB = Branch.Target->getMBB(); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 438 | continue; |
| 439 | } |
| 440 | |
| 441 | // Working from the bottom, handle the first conditional branch. |
| 442 | if (Cond.empty()) { |
| 443 | // FIXME: add X86-style branch swap |
| 444 | FBB = TBB; |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 445 | TBB = Branch.Target->getMBB(); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 446 | Cond.push_back(MachineOperand::CreateImm(Branch.CCValid)); |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 447 | Cond.push_back(MachineOperand::CreateImm(Branch.CCMask)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 448 | continue; |
| 449 | } |
| 450 | |
| 451 | // Handle subsequent conditional branches. |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 452 | assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 453 | |
| 454 | // Only handle the case where all conditional branches branch to the same |
| 455 | // destination. |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 456 | if (TBB != Branch.Target->getMBB()) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 457 | return true; |
| 458 | |
| 459 | // If the conditions are the same, we can leave them alone. |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 460 | unsigned OldCCValid = Cond[0].getImm(); |
| 461 | unsigned OldCCMask = Cond[1].getImm(); |
| 462 | if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 463 | continue; |
| 464 | |
| 465 | // FIXME: Try combining conditions like X86 does. Should be easy on Z! |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 466 | return false; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | return false; |
| 470 | } |
| 471 | |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 472 | unsigned SystemZInstrInfo::removeBranch(MachineBasicBlock &MBB, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 473 | int *BytesRemoved) const { |
| 474 | assert(!BytesRemoved && "code size not handled"); |
| 475 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 476 | // Most of the code and comments here are boilerplate. |
| 477 | MachineBasicBlock::iterator I = MBB.end(); |
| 478 | unsigned Count = 0; |
| 479 | |
| 480 | while (I != MBB.begin()) { |
| 481 | --I; |
| 482 | if (I->isDebugValue()) |
| 483 | continue; |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 484 | if (!I->isBranch()) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 485 | break; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 486 | if (!getBranchInfo(*I).Target->isMBB()) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 487 | break; |
| 488 | // Remove the branch. |
| 489 | I->eraseFromParent(); |
| 490 | I = MBB.end(); |
| 491 | ++Count; |
| 492 | } |
| 493 | |
| 494 | return Count; |
| 495 | } |
| 496 | |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 497 | bool SystemZInstrInfo:: |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 498 | reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 499 | assert(Cond.size() == 2 && "Invalid condition"); |
| 500 | Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm()); |
| 501 | return false; |
| 502 | } |
| 503 | |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 504 | unsigned SystemZInstrInfo::insertBranch(MachineBasicBlock &MBB, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 505 | MachineBasicBlock *TBB, |
| 506 | MachineBasicBlock *FBB, |
| 507 | ArrayRef<MachineOperand> Cond, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 508 | const DebugLoc &DL, |
| 509 | int *BytesAdded) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 510 | // In this function we output 32-bit branches, which should always |
| 511 | // have enough range. They can be shortened and relaxed by later code |
| 512 | // in the pipeline, if desired. |
| 513 | |
| 514 | // Shouldn't be a fall through. |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 515 | assert(TBB && "insertBranch must not be told to insert a fallthrough"); |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 516 | assert((Cond.size() == 2 || Cond.size() == 0) && |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 517 | "SystemZ branch conditions have one component!"); |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 518 | assert(!BytesAdded && "code size not handled"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 519 | |
| 520 | if (Cond.empty()) { |
| 521 | // Unconditional branch? |
| 522 | assert(!FBB && "Unconditional branch with multiple successors!"); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 523 | BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 524 | return 1; |
| 525 | } |
| 526 | |
| 527 | // Conditional branch. |
| 528 | unsigned Count = 0; |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 529 | unsigned CCValid = Cond[0].getImm(); |
| 530 | unsigned CCMask = Cond[1].getImm(); |
| 531 | BuildMI(&MBB, DL, get(SystemZ::BRC)) |
| 532 | .addImm(CCValid).addImm(CCMask).addMBB(TBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 533 | ++Count; |
| 534 | |
| 535 | if (FBB) { |
| 536 | // Two-way Conditional branch. Insert the second branch. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 537 | BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 538 | ++Count; |
| 539 | } |
| 540 | return Count; |
| 541 | } |
| 542 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 543 | bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg, |
| 544 | unsigned &SrcReg2, int &Mask, |
| 545 | int &Value) const { |
| 546 | assert(MI.isCompare() && "Caller should have checked for a comparison"); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 547 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 548 | if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() && |
| 549 | MI.getOperand(1).isImm()) { |
| 550 | SrcReg = MI.getOperand(0).getReg(); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 551 | SrcReg2 = 0; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 552 | Value = MI.getOperand(1).getImm(); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 553 | Mask = ~0; |
| 554 | return true; |
| 555 | } |
| 556 | |
| 557 | return false; |
| 558 | } |
| 559 | |
Richard Sandiford | a590125 | 2013-08-16 10:22:54 +0000 | [diff] [blame] | 560 | // If Reg is a virtual register, return its definition, otherwise return null. |
| 561 | static MachineInstr *getDef(unsigned Reg, |
| 562 | const MachineRegisterInfo *MRI) { |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 563 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 564 | return nullptr; |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 565 | return MRI->getUniqueVRegDef(Reg); |
| 566 | } |
| 567 | |
| 568 | // Return true if MI is a shift of type Opcode by Imm bits. |
Matthias Braun | fa3872e | 2015-05-18 20:27:55 +0000 | [diff] [blame] | 569 | static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) { |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 570 | return (MI->getOpcode() == Opcode && |
| 571 | !MI->getOperand(2).getReg() && |
| 572 | MI->getOperand(3).getImm() == Imm); |
| 573 | } |
| 574 | |
Richard Sandiford | a590125 | 2013-08-16 10:22:54 +0000 | [diff] [blame] | 575 | // If the destination of MI has no uses, delete it as dead. |
| 576 | static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) { |
| 577 | if (MRI->use_nodbg_empty(MI->getOperand(0).getReg())) |
| 578 | MI->eraseFromParent(); |
| 579 | } |
| 580 | |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 581 | // Compare compares SrcReg against zero. Check whether SrcReg contains |
Richard Sandiford | a590125 | 2013-08-16 10:22:54 +0000 | [diff] [blame] | 582 | // the result of an IPM sequence whose input CC survives until Compare, |
| 583 | // and whether Compare is therefore redundant. Delete it and return |
| 584 | // true if so. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 585 | static bool removeIPMBasedCompare(MachineInstr &Compare, unsigned SrcReg, |
Richard Sandiford | a590125 | 2013-08-16 10:22:54 +0000 | [diff] [blame] | 586 | const MachineRegisterInfo *MRI, |
| 587 | const TargetRegisterInfo *TRI) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 588 | MachineInstr *LGFR = nullptr; |
Richard Sandiford | a590125 | 2013-08-16 10:22:54 +0000 | [diff] [blame] | 589 | MachineInstr *RLL = getDef(SrcReg, MRI); |
Richard Sandiford | e382775 | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 590 | if (RLL && RLL->getOpcode() == SystemZ::LGFR) { |
| 591 | LGFR = RLL; |
| 592 | RLL = getDef(LGFR->getOperand(1).getReg(), MRI); |
| 593 | } |
Richard Sandiford | a590125 | 2013-08-16 10:22:54 +0000 | [diff] [blame] | 594 | if (!RLL || !isShift(RLL, SystemZ::RLL, 31)) |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 595 | return false; |
| 596 | |
Richard Sandiford | a590125 | 2013-08-16 10:22:54 +0000 | [diff] [blame] | 597 | MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI); |
Richard Sandiford | f722a8e30 | 2013-10-16 11:10:55 +0000 | [diff] [blame] | 598 | if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC)) |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 599 | return false; |
| 600 | |
Richard Sandiford | a590125 | 2013-08-16 10:22:54 +0000 | [diff] [blame] | 601 | MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 602 | if (!IPM || IPM->getOpcode() != SystemZ::IPM) |
| 603 | return false; |
| 604 | |
| 605 | // Check that there are no assignments to CC between the IPM and Compare, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 606 | if (IPM->getParent() != Compare.getParent()) |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 607 | return false; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 608 | MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare.getIterator(); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 609 | for (++MBBI; MBBI != MBBE; ++MBBI) { |
Duncan P. N. Exon Smith | 4565ec0 | 2016-07-12 01:39:01 +0000 | [diff] [blame] | 610 | MachineInstr &MI = *MBBI; |
| 611 | if (MI.modifiesRegister(SystemZ::CC, TRI)) |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 612 | return false; |
| 613 | } |
| 614 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 615 | Compare.eraseFromParent(); |
Richard Sandiford | e382775 | 2013-08-16 10:55:47 +0000 | [diff] [blame] | 616 | if (LGFR) |
| 617 | eraseIfDead(LGFR, MRI); |
Richard Sandiford | a590125 | 2013-08-16 10:22:54 +0000 | [diff] [blame] | 618 | eraseIfDead(RLL, MRI); |
| 619 | eraseIfDead(SRL, MRI); |
| 620 | eraseIfDead(IPM, MRI); |
| 621 | |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 622 | return true; |
| 623 | } |
| 624 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 625 | bool SystemZInstrInfo::optimizeCompareInstr( |
| 626 | MachineInstr &Compare, unsigned SrcReg, unsigned SrcReg2, int Mask, |
| 627 | int Value, const MachineRegisterInfo *MRI) const { |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 628 | assert(!SrcReg2 && "Only optimizing constant comparisons so far"); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 629 | bool IsLogical = (Compare.getDesc().TSFlags & SystemZII::IsLogical) != 0; |
Ulrich Weigand | 19d24d2 | 2015-11-13 13:00:27 +0000 | [diff] [blame] | 630 | return Value == 0 && !IsLogical && |
| 631 | removeIPMBasedCompare(Compare, SrcReg, MRI, &RI); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 634 | bool SystemZInstrInfo::canInsertSelect(const MachineBasicBlock &MBB, |
| 635 | ArrayRef<MachineOperand> Pred, |
| 636 | unsigned TrueReg, unsigned FalseReg, |
| 637 | int &CondCycles, int &TrueCycles, |
| 638 | int &FalseCycles) const { |
| 639 | // Not all subtargets have LOCR instructions. |
| 640 | if (!STI.hasLoadStoreOnCond()) |
| 641 | return false; |
| 642 | if (Pred.size() != 2) |
| 643 | return false; |
| 644 | |
| 645 | // Check register classes. |
| 646 | const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 647 | const TargetRegisterClass *RC = |
| 648 | RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg)); |
| 649 | if (!RC) |
| 650 | return false; |
| 651 | |
| 652 | // We have LOCR instructions for 32 and 64 bit general purpose registers. |
| 653 | if ((STI.hasLoadStoreOnCond2() && |
| 654 | SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) || |
| 655 | SystemZ::GR32BitRegClass.hasSubClassEq(RC) || |
| 656 | SystemZ::GR64BitRegClass.hasSubClassEq(RC)) { |
| 657 | CondCycles = 2; |
| 658 | TrueCycles = 2; |
| 659 | FalseCycles = 2; |
| 660 | return true; |
Richard Sandiford | f240416 | 2013-07-25 09:11:15 +0000 | [diff] [blame] | 661 | } |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 662 | |
| 663 | // Can't do anything else. |
| 664 | return false; |
Richard Sandiford | f240416 | 2013-07-25 09:11:15 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 667 | void SystemZInstrInfo::insertSelect(MachineBasicBlock &MBB, |
| 668 | MachineBasicBlock::iterator I, |
| 669 | const DebugLoc &DL, unsigned DstReg, |
| 670 | ArrayRef<MachineOperand> Pred, |
| 671 | unsigned TrueReg, |
| 672 | unsigned FalseReg) const { |
| 673 | MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo(); |
| 674 | const TargetRegisterClass *RC = MRI.getRegClass(DstReg); |
| 675 | |
| 676 | assert(Pred.size() == 2 && "Invalid condition"); |
| 677 | unsigned CCValid = Pred[0].getImm(); |
| 678 | unsigned CCMask = Pred[1].getImm(); |
| 679 | |
| 680 | unsigned Opc; |
| 681 | if (SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) { |
| 682 | if (STI.hasLoadStoreOnCond2()) |
| 683 | Opc = SystemZ::LOCRMux; |
| 684 | else { |
| 685 | Opc = SystemZ::LOCR; |
| 686 | MRI.constrainRegClass(DstReg, &SystemZ::GR32BitRegClass); |
Jonas Paulsson | c7bb22e | 2017-03-31 14:06:59 +0000 | [diff] [blame] | 687 | unsigned TReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass); |
| 688 | unsigned FReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass); |
| 689 | BuildMI(MBB, I, DL, get(TargetOpcode::COPY), TReg).addReg(TrueReg); |
| 690 | BuildMI(MBB, I, DL, get(TargetOpcode::COPY), FReg).addReg(FalseReg); |
| 691 | TrueReg = TReg; |
| 692 | FalseReg = FReg; |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 693 | } |
| 694 | } else if (SystemZ::GR64BitRegClass.hasSubClassEq(RC)) |
| 695 | Opc = SystemZ::LOCGR; |
| 696 | else |
| 697 | llvm_unreachable("Invalid register class"); |
| 698 | |
| 699 | BuildMI(MBB, I, DL, get(Opc), DstReg) |
| 700 | .addReg(FalseReg).addReg(TrueReg) |
| 701 | .addImm(CCValid).addImm(CCMask); |
| 702 | } |
| 703 | |
| 704 | bool SystemZInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI, |
| 705 | unsigned Reg, |
| 706 | MachineRegisterInfo *MRI) const { |
| 707 | unsigned DefOpc = DefMI.getOpcode(); |
| 708 | if (DefOpc != SystemZ::LHIMux && DefOpc != SystemZ::LHI && |
| 709 | DefOpc != SystemZ::LGHI) |
| 710 | return false; |
| 711 | if (DefMI.getOperand(0).getReg() != Reg) |
| 712 | return false; |
| 713 | int32_t ImmVal = (int32_t)DefMI.getOperand(1).getImm(); |
| 714 | |
| 715 | unsigned UseOpc = UseMI.getOpcode(); |
| 716 | unsigned NewUseOpc; |
| 717 | unsigned UseIdx; |
| 718 | int CommuteIdx = -1; |
| 719 | switch (UseOpc) { |
| 720 | case SystemZ::LOCRMux: |
| 721 | if (!STI.hasLoadStoreOnCond2()) |
| 722 | return false; |
| 723 | NewUseOpc = SystemZ::LOCHIMux; |
| 724 | if (UseMI.getOperand(2).getReg() == Reg) |
| 725 | UseIdx = 2; |
| 726 | else if (UseMI.getOperand(1).getReg() == Reg) |
| 727 | UseIdx = 2, CommuteIdx = 1; |
| 728 | else |
| 729 | return false; |
| 730 | break; |
| 731 | case SystemZ::LOCGR: |
| 732 | if (!STI.hasLoadStoreOnCond2()) |
| 733 | return false; |
| 734 | NewUseOpc = SystemZ::LOCGHI; |
| 735 | if (UseMI.getOperand(2).getReg() == Reg) |
| 736 | UseIdx = 2; |
| 737 | else if (UseMI.getOperand(1).getReg() == Reg) |
| 738 | UseIdx = 2, CommuteIdx = 1; |
| 739 | else |
| 740 | return false; |
| 741 | break; |
| 742 | default: |
| 743 | return false; |
Zhan Jun Liau | def708a | 2016-07-11 18:45:03 +0000 | [diff] [blame] | 744 | } |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 745 | |
| 746 | if (CommuteIdx != -1) |
| 747 | if (!commuteInstruction(UseMI, false, CommuteIdx, UseIdx)) |
| 748 | return false; |
| 749 | |
| 750 | bool DeleteDef = MRI->hasOneNonDBGUse(Reg); |
| 751 | UseMI.setDesc(get(NewUseOpc)); |
| 752 | UseMI.getOperand(UseIdx).ChangeToImmediate(ImmVal); |
| 753 | if (DeleteDef) |
| 754 | DefMI.eraseFromParent(); |
| 755 | |
| 756 | return true; |
Zhan Jun Liau | def708a | 2016-07-11 18:45:03 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Krzysztof Parzyszek | cc31871 | 2017-03-03 18:30:54 +0000 | [diff] [blame] | 759 | bool SystemZInstrInfo::isPredicable(const MachineInstr &MI) const { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 760 | unsigned Opcode = MI.getOpcode(); |
Ulrich Weigand | fa2dffb | 2016-04-08 17:22:19 +0000 | [diff] [blame] | 761 | if (Opcode == SystemZ::Return || |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 762 | Opcode == SystemZ::Trap || |
Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 763 | Opcode == SystemZ::CallJG || |
| 764 | Opcode == SystemZ::CallBR) |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 765 | return true; |
| 766 | return false; |
Richard Sandiford | f240416 | 2013-07-25 09:11:15 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | bool SystemZInstrInfo:: |
| 770 | isProfitableToIfCvt(MachineBasicBlock &MBB, |
| 771 | unsigned NumCycles, unsigned ExtraPredCycles, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 772 | BranchProbability Probability) const { |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 773 | // Avoid using conditional returns at the end of a loop (since then |
| 774 | // we'd need to emit an unconditional branch to the beginning anyway, |
| 775 | // making the loop body longer). This doesn't apply for low-probability |
| 776 | // loops (eg. compare-and-swap retry), so just decide based on branch |
| 777 | // probability instead of looping structure. |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 778 | // However, since Compare and Trap instructions cost the same as a regular |
| 779 | // Compare instruction, we should allow the if conversion to convert this |
| 780 | // into a Conditional Compare regardless of the branch probability. |
| 781 | if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap && |
| 782 | MBB.succ_empty() && Probability < BranchProbability(1, 8)) |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 783 | return false; |
Richard Sandiford | f240416 | 2013-07-25 09:11:15 +0000 | [diff] [blame] | 784 | // For now only convert single instructions. |
| 785 | return NumCycles == 1; |
| 786 | } |
| 787 | |
| 788 | bool SystemZInstrInfo:: |
| 789 | isProfitableToIfCvt(MachineBasicBlock &TMBB, |
| 790 | unsigned NumCyclesT, unsigned ExtraPredCyclesT, |
| 791 | MachineBasicBlock &FMBB, |
| 792 | unsigned NumCyclesF, unsigned ExtraPredCyclesF, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 793 | BranchProbability Probability) const { |
Richard Sandiford | f240416 | 2013-07-25 09:11:15 +0000 | [diff] [blame] | 794 | // For now avoid converting mutually-exclusive cases. |
| 795 | return false; |
| 796 | } |
| 797 | |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 798 | bool SystemZInstrInfo:: |
| 799 | isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles, |
| 800 | BranchProbability Probability) const { |
| 801 | // For now only duplicate single instructions. |
| 802 | return NumCycles == 1; |
| 803 | } |
| 804 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 805 | bool SystemZInstrInfo::PredicateInstruction( |
| 806 | MachineInstr &MI, ArrayRef<MachineOperand> Pred) const { |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 807 | assert(Pred.size() == 2 && "Invalid condition"); |
| 808 | unsigned CCValid = Pred[0].getImm(); |
| 809 | unsigned CCMask = Pred[1].getImm(); |
Richard Sandiford | f240416 | 2013-07-25 09:11:15 +0000 | [diff] [blame] | 810 | assert(CCMask > 0 && CCMask < 15 && "Invalid predicate"); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 811 | unsigned Opcode = MI.getOpcode(); |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 812 | if (Opcode == SystemZ::Trap) { |
| 813 | MI.setDesc(get(SystemZ::CondTrap)); |
| 814 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 815 | .addImm(CCValid).addImm(CCMask) |
| 816 | .addReg(SystemZ::CC, RegState::Implicit); |
| 817 | return true; |
| 818 | } |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 819 | if (Opcode == SystemZ::Return) { |
| 820 | MI.setDesc(get(SystemZ::CondReturn)); |
| 821 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 822 | .addImm(CCValid).addImm(CCMask) |
| 823 | .addReg(SystemZ::CC, RegState::Implicit); |
| 824 | return true; |
| 825 | } |
Ulrich Weigand | fa2dffb | 2016-04-08 17:22:19 +0000 | [diff] [blame] | 826 | if (Opcode == SystemZ::CallJG) { |
Zhan Jun Liau | a5d60af | 2016-07-07 15:34:46 +0000 | [diff] [blame] | 827 | MachineOperand FirstOp = MI.getOperand(0); |
Ulrich Weigand | fa2dffb | 2016-04-08 17:22:19 +0000 | [diff] [blame] | 828 | const uint32_t *RegMask = MI.getOperand(1).getRegMask(); |
| 829 | MI.RemoveOperand(1); |
| 830 | MI.RemoveOperand(0); |
| 831 | MI.setDesc(get(SystemZ::CallBRCL)); |
| 832 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 833 | .addImm(CCValid) |
| 834 | .addImm(CCMask) |
| 835 | .add(FirstOp) |
| 836 | .addRegMask(RegMask) |
| 837 | .addReg(SystemZ::CC, RegState::Implicit); |
Ulrich Weigand | fa2dffb | 2016-04-08 17:22:19 +0000 | [diff] [blame] | 838 | return true; |
| 839 | } |
Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 840 | if (Opcode == SystemZ::CallBR) { |
| 841 | const uint32_t *RegMask = MI.getOperand(0).getRegMask(); |
| 842 | MI.RemoveOperand(0); |
| 843 | MI.setDesc(get(SystemZ::CallBCR)); |
| 844 | MachineInstrBuilder(*MI.getParent()->getParent(), MI) |
| 845 | .addImm(CCValid).addImm(CCMask) |
| 846 | .addRegMask(RegMask) |
| 847 | .addReg(SystemZ::CC, RegState::Implicit); |
| 848 | return true; |
| 849 | } |
Richard Sandiford | f240416 | 2013-07-25 09:11:15 +0000 | [diff] [blame] | 850 | return false; |
| 851 | } |
| 852 | |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 853 | void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 854 | MachineBasicBlock::iterator MBBI, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 855 | const DebugLoc &DL, unsigned DestReg, |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 856 | unsigned SrcReg, bool KillSrc) const { |
Jonas Paulsson | 4fd1562 | 2017-05-04 13:33:30 +0000 | [diff] [blame] | 857 | // Split 128-bit GPR moves into two 64-bit moves. Add implicit uses of the |
| 858 | // super register in case one of the subregs is undefined. |
| 859 | // This handles ADDR128 too. |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 860 | if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) { |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 861 | copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64), |
| 862 | RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc); |
Jonas Paulsson | 4fd1562 | 2017-05-04 13:33:30 +0000 | [diff] [blame] | 863 | MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI)) |
| 864 | .addReg(SrcReg, RegState::Implicit); |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 865 | copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64), |
| 866 | RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc); |
Jonas Paulsson | 4fd1562 | 2017-05-04 13:33:30 +0000 | [diff] [blame] | 867 | MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI)) |
| 868 | .addReg(SrcReg, (getKillRegState(KillSrc) | RegState::Implicit)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 869 | return; |
| 870 | } |
| 871 | |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 872 | if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) { |
Jonas Paulsson | a9bb00d | 2017-01-18 08:32:54 +0000 | [diff] [blame] | 873 | emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc, |
| 874 | false); |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 875 | return; |
| 876 | } |
| 877 | |
Ulrich Weigand | f2968d5 | 2017-07-17 17:44:20 +0000 | [diff] [blame] | 878 | // Move 128-bit floating-point values between VR128 and FP128. |
| 879 | if (SystemZ::VR128BitRegClass.contains(DestReg) && |
| 880 | SystemZ::FP128BitRegClass.contains(SrcReg)) { |
| 881 | unsigned SrcRegHi = |
| 882 | RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_h64), |
| 883 | SystemZ::subreg_r64, &SystemZ::VR128BitRegClass); |
| 884 | unsigned SrcRegLo = |
| 885 | RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_l64), |
| 886 | SystemZ::subreg_r64, &SystemZ::VR128BitRegClass); |
| 887 | |
| 888 | BuildMI(MBB, MBBI, DL, get(SystemZ::VMRHG), DestReg) |
| 889 | .addReg(SrcRegHi, getKillRegState(KillSrc)) |
| 890 | .addReg(SrcRegLo, getKillRegState(KillSrc)); |
| 891 | return; |
| 892 | } |
| 893 | if (SystemZ::FP128BitRegClass.contains(DestReg) && |
| 894 | SystemZ::VR128BitRegClass.contains(SrcReg)) { |
| 895 | unsigned DestRegHi = |
| 896 | RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_h64), |
| 897 | SystemZ::subreg_r64, &SystemZ::VR128BitRegClass); |
| 898 | unsigned DestRegLo = |
| 899 | RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_l64), |
| 900 | SystemZ::subreg_r64, &SystemZ::VR128BitRegClass); |
| 901 | |
| 902 | if (DestRegHi != SrcReg) |
| 903 | copyPhysReg(MBB, MBBI, DL, DestRegHi, SrcReg, false); |
| 904 | BuildMI(MBB, MBBI, DL, get(SystemZ::VREPG), DestRegLo) |
| 905 | .addReg(SrcReg, getKillRegState(KillSrc)).addImm(1); |
| 906 | return; |
| 907 | } |
| 908 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 909 | // Everything else needs only one instruction. |
| 910 | unsigned Opcode; |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 911 | if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg)) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 912 | Opcode = SystemZ::LGR; |
| 913 | else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg)) |
Ulrich Weigand | cdce026 | 2016-03-14 13:50:03 +0000 | [diff] [blame] | 914 | // For z13 we prefer LDR over LER to avoid partial register dependencies. |
| 915 | Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 916 | else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg)) |
| 917 | Opcode = SystemZ::LDR; |
| 918 | else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg)) |
| 919 | Opcode = SystemZ::LXR; |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 920 | else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg)) |
| 921 | Opcode = SystemZ::VLR32; |
| 922 | else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg)) |
| 923 | Opcode = SystemZ::VLR64; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 924 | else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg)) |
| 925 | Opcode = SystemZ::VLR; |
Ulrich Weigand | fffc711 | 2016-11-08 20:15:26 +0000 | [diff] [blame] | 926 | else if (SystemZ::AR32BitRegClass.contains(DestReg, SrcReg)) |
| 927 | Opcode = SystemZ::CPYA; |
| 928 | else if (SystemZ::AR32BitRegClass.contains(DestReg) && |
| 929 | SystemZ::GR32BitRegClass.contains(SrcReg)) |
| 930 | Opcode = SystemZ::SAR; |
| 931 | else if (SystemZ::GR32BitRegClass.contains(DestReg) && |
| 932 | SystemZ::AR32BitRegClass.contains(SrcReg)) |
| 933 | Opcode = SystemZ::EAR; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 934 | else |
| 935 | llvm_unreachable("Impossible reg-to-reg copy"); |
| 936 | |
| 937 | BuildMI(MBB, MBBI, DL, get(Opcode), DestReg) |
| 938 | .addReg(SrcReg, getKillRegState(KillSrc)); |
| 939 | } |
| 940 | |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 941 | void SystemZInstrInfo::storeRegToStackSlot( |
| 942 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg, |
| 943 | bool isKill, int FrameIdx, const TargetRegisterClass *RC, |
| 944 | const TargetRegisterInfo *TRI) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 945 | DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 946 | |
| 947 | // Callers may expect a single instruction, so keep 128-bit moves |
| 948 | // together for now and lower them after register allocation. |
| 949 | unsigned LoadOpcode, StoreOpcode; |
| 950 | getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode); |
| 951 | addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode)) |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 952 | .addReg(SrcReg, getKillRegState(isKill)), |
| 953 | FrameIdx); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 954 | } |
| 955 | |
NAKAMURA Takumi | 0a7d0ad | 2015-09-22 11:15:07 +0000 | [diff] [blame] | 956 | void SystemZInstrInfo::loadRegFromStackSlot( |
| 957 | MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg, |
| 958 | int FrameIdx, const TargetRegisterClass *RC, |
| 959 | const TargetRegisterInfo *TRI) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 960 | DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 961 | |
| 962 | // Callers may expect a single instruction, so keep 128-bit moves |
| 963 | // together for now and lower them after register allocation. |
| 964 | unsigned LoadOpcode, StoreOpcode; |
| 965 | getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode); |
| 966 | addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg), |
| 967 | FrameIdx); |
| 968 | } |
| 969 | |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 970 | // Return true if MI is a simple load or store with a 12-bit displacement |
| 971 | // and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores. |
| 972 | static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) { |
| 973 | const MCInstrDesc &MCID = MI->getDesc(); |
| 974 | return ((MCID.TSFlags & Flag) && |
| 975 | isUInt<12>(MI->getOperand(2).getImm()) && |
| 976 | MI->getOperand(3).getReg() == 0); |
| 977 | } |
| 978 | |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 979 | namespace { |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 980 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 981 | struct LogicOp { |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 982 | LogicOp() = default; |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 983 | LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize) |
| 984 | : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {} |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 985 | |
Aaron Ballman | b46962f | 2015-02-15 22:00:20 +0000 | [diff] [blame] | 986 | explicit operator bool() const { return RegSize; } |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 987 | |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 988 | unsigned RegSize = 0; |
| 989 | unsigned ImmLSB = 0; |
| 990 | unsigned ImmSize = 0; |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 991 | }; |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 992 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 993 | } // end anonymous namespace |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 994 | |
| 995 | static LogicOp interpretAndImmediate(unsigned Opcode) { |
| 996 | switch (Opcode) { |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 997 | case SystemZ::NILMux: return LogicOp(32, 0, 16); |
| 998 | case SystemZ::NIHMux: return LogicOp(32, 16, 16); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 999 | case SystemZ::NILL64: return LogicOp(64, 0, 16); |
| 1000 | case SystemZ::NILH64: return LogicOp(64, 16, 16); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1001 | case SystemZ::NIHL64: return LogicOp(64, 32, 16); |
| 1002 | case SystemZ::NIHH64: return LogicOp(64, 48, 16); |
| 1003 | case SystemZ::NIFMux: return LogicOp(32, 0, 32); |
Richard Sandiford | 652784e | 2013-09-25 11:11:53 +0000 | [diff] [blame] | 1004 | case SystemZ::NILF64: return LogicOp(64, 0, 32); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1005 | case SystemZ::NIHF64: return LogicOp(64, 32, 32); |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1006 | default: return LogicOp(); |
| 1007 | } |
| 1008 | } |
| 1009 | |
Jonas Paulsson | 9028acf | 2016-05-02 09:37:40 +0000 | [diff] [blame] | 1010 | static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) { |
| 1011 | if (OldMI->registerDefIsDead(SystemZ::CC)) { |
| 1012 | MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC); |
| 1013 | if (CCDef != nullptr) |
| 1014 | CCDef->setIsDead(true); |
| 1015 | } |
| 1016 | } |
| 1017 | |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1018 | // Used to return from convertToThreeAddress after replacing two-address |
| 1019 | // instruction OldMI with three-address instruction NewMI. |
| 1020 | static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI, |
| 1021 | MachineInstr *NewMI, |
| 1022 | LiveVariables *LV) { |
| 1023 | if (LV) { |
| 1024 | unsigned NumOps = OldMI->getNumOperands(); |
| 1025 | for (unsigned I = 1; I < NumOps; ++I) { |
| 1026 | MachineOperand &Op = OldMI->getOperand(I); |
| 1027 | if (Op.isReg() && Op.isKill()) |
Duncan P. N. Exon Smith | d26fdc8 | 2016-07-01 01:51:32 +0000 | [diff] [blame] | 1028 | LV->replaceKillInstruction(Op.getReg(), *OldMI, *NewMI); |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1029 | } |
| 1030 | } |
Jonas Paulsson | 9028acf | 2016-05-02 09:37:40 +0000 | [diff] [blame] | 1031 | transferDeadCC(OldMI, NewMI); |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1032 | return NewMI; |
| 1033 | } |
| 1034 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1035 | MachineInstr *SystemZInstrInfo::convertToThreeAddress( |
| 1036 | MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const { |
| 1037 | MachineBasicBlock *MBB = MI.getParent(); |
Jonas Paulsson | 7fa69cd | 2015-12-04 12:48:51 +0000 | [diff] [blame] | 1038 | MachineFunction *MF = MBB->getParent(); |
| 1039 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
Richard Sandiford | ff6c5a5 | 2013-07-19 16:12:08 +0000 | [diff] [blame] | 1040 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1041 | unsigned Opcode = MI.getOpcode(); |
| 1042 | unsigned NumOps = MI.getNumOperands(); |
Richard Sandiford | ff6c5a5 | 2013-07-19 16:12:08 +0000 | [diff] [blame] | 1043 | |
| 1044 | // Try to convert something like SLL into SLLK, if supported. |
| 1045 | // We prefer to keep the two-operand form where possible both |
| 1046 | // because it tends to be shorter and because some instructions |
| 1047 | // have memory forms that can be used during spilling. |
Eric Christopher | 673b3af | 2014-06-27 07:01:17 +0000 | [diff] [blame] | 1048 | if (STI.hasDistinctOps()) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1049 | MachineOperand &Dest = MI.getOperand(0); |
| 1050 | MachineOperand &Src = MI.getOperand(1); |
Richard Sandiford | 42a694f | 2013-10-01 14:53:46 +0000 | [diff] [blame] | 1051 | unsigned DestReg = Dest.getReg(); |
| 1052 | unsigned SrcReg = Src.getReg(); |
| 1053 | // AHIMux is only really a three-operand instruction when both operands |
| 1054 | // are low registers. Try to constrain both operands to be low if |
| 1055 | // possible. |
| 1056 | if (Opcode == SystemZ::AHIMux && |
| 1057 | TargetRegisterInfo::isVirtualRegister(DestReg) && |
| 1058 | TargetRegisterInfo::isVirtualRegister(SrcReg) && |
| 1059 | MRI.getRegClass(DestReg)->contains(SystemZ::R1L) && |
| 1060 | MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) { |
| 1061 | MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass); |
| 1062 | MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass); |
| 1063 | } |
Richard Sandiford | ff6c5a5 | 2013-07-19 16:12:08 +0000 | [diff] [blame] | 1064 | int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode); |
| 1065 | if (ThreeOperandOpcode >= 0) { |
Jonas Paulsson | 7fa69cd | 2015-12-04 12:48:51 +0000 | [diff] [blame] | 1066 | // Create three address instruction without adding the implicit |
| 1067 | // operands. Those will instead be copied over from the original |
| 1068 | // instruction by the loop below. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1069 | MachineInstrBuilder MIB( |
| 1070 | *MF, MF->CreateMachineInstr(get(ThreeOperandOpcode), MI.getDebugLoc(), |
| 1071 | /*NoImplicit=*/true)); |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 1072 | MIB.add(Dest); |
Richard Sandiford | ff6c5a5 | 2013-07-19 16:12:08 +0000 | [diff] [blame] | 1073 | // Keep the kill state, but drop the tied flag. |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1074 | MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg()); |
Richard Sandiford | ff6c5a5 | 2013-07-19 16:12:08 +0000 | [diff] [blame] | 1075 | // Keep the remaining operands as-is. |
| 1076 | for (unsigned I = 2; I < NumOps; ++I) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 1077 | MIB.add(MI.getOperand(I)); |
Jonas Paulsson | 7fa69cd | 2015-12-04 12:48:51 +0000 | [diff] [blame] | 1078 | MBB->insert(MI, MIB); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1079 | return finishConvertToThreeAddress(&MI, MIB, LV); |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1080 | } |
| 1081 | } |
Richard Sandiford | ff6c5a5 | 2013-07-19 16:12:08 +0000 | [diff] [blame] | 1082 | |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1083 | // Try to convert an AND into an RISBG-type instruction. |
| 1084 | if (LogicOp And = interpretAndImmediate(Opcode)) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1085 | uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB; |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1086 | // AND IMMEDIATE leaves the other bits of the register unchanged. |
| 1087 | Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB); |
| 1088 | unsigned Start, End; |
| 1089 | if (isRxSBGMask(Imm, And.RegSize, Start, End)) { |
| 1090 | unsigned NewOpcode; |
Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1091 | if (And.RegSize == 64) { |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1092 | NewOpcode = SystemZ::RISBG; |
Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1093 | // Prefer RISBGN if available, since it does not clobber CC. |
| 1094 | if (STI.hasMiscellaneousExtensions()) |
| 1095 | NewOpcode = SystemZ::RISBGN; |
| 1096 | } else { |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1097 | NewOpcode = SystemZ::RISBMux; |
| 1098 | Start &= 31; |
| 1099 | End &= 31; |
Richard Sandiford | ff6c5a5 | 2013-07-19 16:12:08 +0000 | [diff] [blame] | 1100 | } |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1101 | MachineOperand &Dest = MI.getOperand(0); |
| 1102 | MachineOperand &Src = MI.getOperand(1); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1103 | MachineInstrBuilder MIB = |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1104 | BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode)) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 1105 | .add(Dest) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1106 | .addReg(0) |
| 1107 | .addReg(Src.getReg(), getKillRegState(Src.isKill()), |
| 1108 | Src.getSubReg()) |
| 1109 | .addImm(Start) |
| 1110 | .addImm(End + 128) |
| 1111 | .addImm(0); |
| 1112 | return finishConvertToThreeAddress(&MI, MIB, LV); |
Richard Sandiford | ff6c5a5 | 2013-07-19 16:12:08 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1115 | return nullptr; |
Richard Sandiford | ff6c5a5 | 2013-07-19 16:12:08 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 1118 | MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl( |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1119 | MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, |
Jonas Paulsson | 8e5b0c6 | 2016-05-10 08:09:37 +0000 | [diff] [blame] | 1120 | MachineBasicBlock::iterator InsertPt, int FrameIndex, |
| 1121 | LiveIntervals *LIS) const { |
| 1122 | const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 1123 | const MachineFrameInfo &MFI = MF.getFrameInfo(); |
| 1124 | unsigned Size = MFI.getObjectSize(FrameIndex); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1125 | unsigned Opcode = MI.getOpcode(); |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1126 | |
Richard Sandiford | 6af6ff1 | 2013-10-15 08:42:59 +0000 | [diff] [blame] | 1127 | if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1128 | if (LIS != nullptr && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) && |
| 1129 | isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) { |
Jonas Paulsson | 8e5b0c6 | 2016-05-10 08:09:37 +0000 | [diff] [blame] | 1130 | |
| 1131 | // Check CC liveness, since new instruction introduces a dead |
| 1132 | // def of CC. |
| 1133 | MCRegUnitIterator CCUnit(SystemZ::CC, TRI); |
| 1134 | LiveRange &CCLiveRange = LIS->getRegUnit(*CCUnit); |
| 1135 | ++CCUnit; |
Eugene Zelenko | 3943d2b | 2017-01-24 22:10:43 +0000 | [diff] [blame] | 1136 | assert(!CCUnit.isValid() && "CC only has one reg unit."); |
Jonas Paulsson | 8e5b0c6 | 2016-05-10 08:09:37 +0000 | [diff] [blame] | 1137 | SlotIndex MISlot = |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1138 | LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot(); |
Jonas Paulsson | 8e5b0c6 | 2016-05-10 08:09:37 +0000 | [diff] [blame] | 1139 | if (!CCLiveRange.liveAt(MISlot)) { |
| 1140 | // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1141 | MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt, |
| 1142 | MI.getDebugLoc(), get(SystemZ::AGSI)) |
| 1143 | .addFrameIndex(FrameIndex) |
| 1144 | .addImm(0) |
| 1145 | .addImm(MI.getOperand(2).getImm()); |
Jonas Paulsson | 8e5b0c6 | 2016-05-10 08:09:37 +0000 | [diff] [blame] | 1146 | BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true); |
| 1147 | CCLiveRange.createDeadDef(MISlot, LIS->getVNInfoAllocator()); |
| 1148 | return BuiltMI; |
| 1149 | } |
Richard Sandiford | 6af6ff1 | 2013-10-15 08:42:59 +0000 | [diff] [blame] | 1150 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1151 | return nullptr; |
Richard Sandiford | 6af6ff1 | 2013-10-15 08:42:59 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | // All other cases require a single operand. |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1155 | if (Ops.size() != 1) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1156 | return nullptr; |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1157 | |
| 1158 | unsigned OpNum = Ops[0]; |
Krzysztof Parzyszek | 44e25f3 | 2017-04-24 18:55:33 +0000 | [diff] [blame] | 1159 | assert(Size * 8 == |
| 1160 | TRI->getRegSizeInBits(*MF.getRegInfo() |
| 1161 | .getRegClass(MI.getOperand(OpNum).getReg())) && |
Benjamin Kramer | 421c8fb | 2013-07-02 21:17:31 +0000 | [diff] [blame] | 1162 | "Invalid size combination"); |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1163 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1164 | if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 && |
| 1165 | isInt<8>(MI.getOperand(2).getImm())) { |
Richard Sandiford | 6af6ff1 | 2013-10-15 08:42:59 +0000 | [diff] [blame] | 1166 | // A(G)HI %reg, CONST -> A(G)SI %mem, CONST |
| 1167 | Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI); |
Jonas Paulsson | 9028acf | 2016-05-02 09:37:40 +0000 | [diff] [blame] | 1168 | MachineInstr *BuiltMI = |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1169 | BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode)) |
| 1170 | .addFrameIndex(FrameIndex) |
| 1171 | .addImm(0) |
| 1172 | .addImm(MI.getOperand(2).getImm()); |
| 1173 | transferDeadCC(&MI, BuiltMI); |
Jonas Paulsson | 9028acf | 2016-05-02 09:37:40 +0000 | [diff] [blame] | 1174 | return BuiltMI; |
Richard Sandiford | 6af6ff1 | 2013-10-15 08:42:59 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Richard Sandiford | 3f0edc2 | 2013-07-12 08:37:17 +0000 | [diff] [blame] | 1177 | if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) { |
| 1178 | bool Op0IsGPR = (Opcode == SystemZ::LGDR); |
| 1179 | bool Op1IsGPR = (Opcode == SystemZ::LDGR); |
| 1180 | // If we're spilling the destination of an LDGR or LGDR, store the |
| 1181 | // source register instead. |
| 1182 | if (OpNum == 0) { |
| 1183 | unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1184 | return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 1185 | get(StoreOpcode)) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 1186 | .add(MI.getOperand(1)) |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 1187 | .addFrameIndex(FrameIndex) |
| 1188 | .addImm(0) |
| 1189 | .addReg(0); |
Richard Sandiford | 3f0edc2 | 2013-07-12 08:37:17 +0000 | [diff] [blame] | 1190 | } |
| 1191 | // If we're spilling the source of an LDGR or LGDR, load the |
| 1192 | // destination register instead. |
| 1193 | if (OpNum == 1) { |
| 1194 | unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1195 | return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), |
Jonas Paulsson | bd65421 | 2017-03-21 05:49:40 +0000 | [diff] [blame] | 1196 | get(LoadOpcode)) |
| 1197 | .add(MI.getOperand(0)) |
| 1198 | .addFrameIndex(FrameIndex) |
| 1199 | .addImm(0) |
| 1200 | .addReg(0); |
Richard Sandiford | 3f0edc2 | 2013-07-12 08:37:17 +0000 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1204 | // Look for cases where the source of a simple store or the destination |
| 1205 | // of a simple load is being spilled. Try to use MVC instead. |
| 1206 | // |
| 1207 | // Although MVC is in practice a fast choice in these cases, it is still |
| 1208 | // logically a bytewise copy. This means that we cannot use it if the |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1209 | // load or store is volatile. We also wouldn't be able to use MVC if |
| 1210 | // the two memories partially overlap, but that case cannot occur here, |
| 1211 | // because we know that one of the memories is a full frame index. |
| 1212 | // |
| 1213 | // For performance reasons, we also want to avoid using MVC if the addresses |
| 1214 | // might be equal. We don't worry about that case here, because spill slot |
| 1215 | // coloring happens later, and because we have special code to remove |
| 1216 | // MVCs that turn out to be redundant. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1217 | if (OpNum == 0 && MI.hasOneMemOperand()) { |
| 1218 | MachineMemOperand *MMO = *MI.memoperands_begin(); |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1219 | if (MMO->getSize() == Size && !MMO->isVolatile()) { |
| 1220 | // Handle conversion of loads. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1221 | if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) { |
| 1222 | return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 1223 | get(SystemZ::MVC)) |
| 1224 | .addFrameIndex(FrameIndex) |
| 1225 | .addImm(0) |
| 1226 | .addImm(Size) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 1227 | .add(MI.getOperand(1)) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1228 | .addImm(MI.getOperand(2).getImm()) |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 1229 | .addMemOperand(MMO); |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1230 | } |
| 1231 | // Handle conversion of stores. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1232 | if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) { |
| 1233 | return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 1234 | get(SystemZ::MVC)) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 1235 | .add(MI.getOperand(1)) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1236 | .addImm(MI.getOperand(2).getImm()) |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 1237 | .addImm(Size) |
| 1238 | .addFrameIndex(FrameIndex) |
| 1239 | .addImm(0) |
| 1240 | .addMemOperand(MMO); |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | |
Richard Sandiford | ed1fab6 | 2013-07-03 10:10:02 +0000 | [diff] [blame] | 1245 | // If the spilled operand is the final one, try to change <INSN>R |
| 1246 | // into <INSN>. |
Richard Sandiford | 3f0edc2 | 2013-07-12 08:37:17 +0000 | [diff] [blame] | 1247 | int MemOpcode = SystemZ::getMemOpcode(Opcode); |
Richard Sandiford | ed1fab6 | 2013-07-03 10:10:02 +0000 | [diff] [blame] | 1248 | if (MemOpcode >= 0) { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1249 | unsigned NumOps = MI.getNumExplicitOperands(); |
Richard Sandiford | ed1fab6 | 2013-07-03 10:10:02 +0000 | [diff] [blame] | 1250 | if (OpNum == NumOps - 1) { |
| 1251 | const MCInstrDesc &MemDesc = get(MemOpcode); |
| 1252 | uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags); |
| 1253 | assert(AccessBytes != 0 && "Size of access should be known"); |
| 1254 | assert(AccessBytes <= Size && "Access outside the frame index"); |
| 1255 | uint64_t Offset = Size - AccessBytes; |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 1256 | MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1257 | MI.getDebugLoc(), get(MemOpcode)); |
Richard Sandiford | ed1fab6 | 2013-07-03 10:10:02 +0000 | [diff] [blame] | 1258 | for (unsigned I = 0; I < OpNum; ++I) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 1259 | MIB.add(MI.getOperand(I)); |
Richard Sandiford | ed1fab6 | 2013-07-03 10:10:02 +0000 | [diff] [blame] | 1260 | MIB.addFrameIndex(FrameIndex).addImm(Offset); |
| 1261 | if (MemDesc.TSFlags & SystemZII::HasIndex) |
| 1262 | MIB.addReg(0); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1263 | transferDeadCC(&MI, MIB); |
Richard Sandiford | ed1fab6 | 2013-07-03 10:10:02 +0000 | [diff] [blame] | 1264 | return MIB; |
| 1265 | } |
| 1266 | } |
| 1267 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1268 | return nullptr; |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Keno Fischer | e70b31f | 2015-06-08 20:09:58 +0000 | [diff] [blame] | 1271 | MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl( |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1272 | MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops, |
| 1273 | MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI, |
Jonas Paulsson | 8e5b0c6 | 2016-05-10 08:09:37 +0000 | [diff] [blame] | 1274 | LiveIntervals *LIS) const { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1275 | return nullptr; |
Richard Sandiford | f6bae1e | 2013-07-02 15:28:56 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1278 | bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const { |
| 1279 | switch (MI.getOpcode()) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1280 | case SystemZ::L128: |
| 1281 | splitMove(MI, SystemZ::LG); |
| 1282 | return true; |
| 1283 | |
| 1284 | case SystemZ::ST128: |
| 1285 | splitMove(MI, SystemZ::STG); |
| 1286 | return true; |
| 1287 | |
| 1288 | case SystemZ::LX: |
| 1289 | splitMove(MI, SystemZ::LD); |
| 1290 | return true; |
| 1291 | |
| 1292 | case SystemZ::STX: |
| 1293 | splitMove(MI, SystemZ::STD); |
| 1294 | return true; |
| 1295 | |
Richard Sandiford | 89e160d | 2013-10-01 12:11:47 +0000 | [diff] [blame] | 1296 | case SystemZ::LBMux: |
| 1297 | expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH); |
| 1298 | return true; |
| 1299 | |
| 1300 | case SystemZ::LHMux: |
| 1301 | expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH); |
| 1302 | return true; |
| 1303 | |
Richard Sandiford | 21235a2 | 2013-10-01 12:49:07 +0000 | [diff] [blame] | 1304 | case SystemZ::LLCRMux: |
| 1305 | expandZExtPseudo(MI, SystemZ::LLCR, 8); |
| 1306 | return true; |
| 1307 | |
| 1308 | case SystemZ::LLHRMux: |
| 1309 | expandZExtPseudo(MI, SystemZ::LLHR, 16); |
| 1310 | return true; |
| 1311 | |
Richard Sandiford | 0d46b1a | 2013-10-01 12:19:08 +0000 | [diff] [blame] | 1312 | case SystemZ::LLCMux: |
| 1313 | expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH); |
| 1314 | return true; |
| 1315 | |
| 1316 | case SystemZ::LLHMux: |
| 1317 | expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH); |
| 1318 | return true; |
| 1319 | |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 1320 | case SystemZ::LMux: |
| 1321 | expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH); |
| 1322 | return true; |
| 1323 | |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 1324 | case SystemZ::LOCMux: |
| 1325 | expandLOCPseudo(MI, SystemZ::LOC, SystemZ::LOCFH); |
| 1326 | return true; |
| 1327 | |
| 1328 | case SystemZ::LOCHIMux: |
| 1329 | expandLOCPseudo(MI, SystemZ::LOCHI, SystemZ::LOCHHI); |
| 1330 | return true; |
| 1331 | |
| 1332 | case SystemZ::LOCRMux: |
| 1333 | expandLOCRPseudo(MI, SystemZ::LOCR, SystemZ::LOCFHR); |
| 1334 | return true; |
| 1335 | |
Richard Sandiford | 5469c39 | 2013-10-01 12:22:49 +0000 | [diff] [blame] | 1336 | case SystemZ::STCMux: |
| 1337 | expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH); |
| 1338 | return true; |
| 1339 | |
| 1340 | case SystemZ::STHMux: |
| 1341 | expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH); |
| 1342 | return true; |
| 1343 | |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 1344 | case SystemZ::STMux: |
| 1345 | expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH); |
| 1346 | return true; |
| 1347 | |
Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 1348 | case SystemZ::STOCMux: |
| 1349 | expandLOCPseudo(MI, SystemZ::STOC, SystemZ::STOCFH); |
| 1350 | return true; |
| 1351 | |
Richard Sandiford | 0124023 | 2013-10-01 13:02:28 +0000 | [diff] [blame] | 1352 | case SystemZ::LHIMux: |
| 1353 | expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true); |
| 1354 | return true; |
| 1355 | |
| 1356 | case SystemZ::IIFMux: |
| 1357 | expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false); |
| 1358 | return true; |
| 1359 | |
Richard Sandiford | 1a56931 | 2013-10-01 13:18:56 +0000 | [diff] [blame] | 1360 | case SystemZ::IILMux: |
| 1361 | expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false); |
| 1362 | return true; |
| 1363 | |
| 1364 | case SystemZ::IIHMux: |
| 1365 | expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false); |
| 1366 | return true; |
| 1367 | |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1368 | case SystemZ::NIFMux: |
| 1369 | expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false); |
| 1370 | return true; |
| 1371 | |
| 1372 | case SystemZ::NILMux: |
| 1373 | expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false); |
| 1374 | return true; |
| 1375 | |
| 1376 | case SystemZ::NIHMux: |
| 1377 | expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false); |
| 1378 | return true; |
| 1379 | |
Richard Sandiford | 6e96ac6 | 2013-10-01 13:22:41 +0000 | [diff] [blame] | 1380 | case SystemZ::OIFMux: |
| 1381 | expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false); |
| 1382 | return true; |
| 1383 | |
| 1384 | case SystemZ::OILMux: |
| 1385 | expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false); |
| 1386 | return true; |
| 1387 | |
| 1388 | case SystemZ::OIHMux: |
| 1389 | expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false); |
| 1390 | return true; |
| 1391 | |
Richard Sandiford | 5718dac | 2013-10-01 14:08:44 +0000 | [diff] [blame] | 1392 | case SystemZ::XIFMux: |
| 1393 | expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false); |
| 1394 | return true; |
| 1395 | |
Richard Sandiford | 2cac763 | 2013-10-01 14:41:52 +0000 | [diff] [blame] | 1396 | case SystemZ::TMLMux: |
| 1397 | expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false); |
| 1398 | return true; |
| 1399 | |
| 1400 | case SystemZ::TMHMux: |
| 1401 | expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false); |
| 1402 | return true; |
| 1403 | |
Richard Sandiford | 42a694f | 2013-10-01 14:53:46 +0000 | [diff] [blame] | 1404 | case SystemZ::AHIMux: |
| 1405 | expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false); |
| 1406 | return true; |
| 1407 | |
| 1408 | case SystemZ::AHIMuxK: |
| 1409 | expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH); |
| 1410 | return true; |
| 1411 | |
| 1412 | case SystemZ::AFIMux: |
| 1413 | expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false); |
| 1414 | return true; |
| 1415 | |
Ulrich Weigand | 7583991 | 2016-11-28 13:40:08 +0000 | [diff] [blame] | 1416 | case SystemZ::CHIMux: |
| 1417 | expandRIPseudo(MI, SystemZ::CHI, SystemZ::CIH, false); |
| 1418 | return true; |
| 1419 | |
Richard Sandiford | a9ac0e0 | 2013-10-01 14:56:23 +0000 | [diff] [blame] | 1420 | case SystemZ::CFIMux: |
| 1421 | expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false); |
| 1422 | return true; |
| 1423 | |
| 1424 | case SystemZ::CLFIMux: |
| 1425 | expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false); |
| 1426 | return true; |
| 1427 | |
Richard Sandiford | b63e300 | 2013-10-01 15:00:44 +0000 | [diff] [blame] | 1428 | case SystemZ::CMux: |
| 1429 | expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF); |
| 1430 | return true; |
| 1431 | |
| 1432 | case SystemZ::CLMux: |
| 1433 | expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF); |
| 1434 | return true; |
| 1435 | |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1436 | case SystemZ::RISBMux: { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1437 | bool DestIsHigh = isHighReg(MI.getOperand(0).getReg()); |
| 1438 | bool SrcIsHigh = isHighReg(MI.getOperand(2).getReg()); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1439 | if (SrcIsHigh == DestIsHigh) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1440 | MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL)); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1441 | else { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1442 | MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH)); |
| 1443 | MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32); |
Richard Sandiford | 7028428 | 2013-10-01 14:20:41 +0000 | [diff] [blame] | 1444 | } |
| 1445 | return true; |
| 1446 | } |
| 1447 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1448 | case SystemZ::ADJDYNALLOC: |
| 1449 | splitAdjDynAlloc(MI); |
| 1450 | return true; |
| 1451 | |
Marcin Koscielnicki | aef3b5b | 2016-04-24 13:57:49 +0000 | [diff] [blame] | 1452 | case TargetOpcode::LOAD_STACK_GUARD: |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1453 | expandLoadStackGuard(&MI); |
Marcin Koscielnicki | aef3b5b | 2016-04-24 13:57:49 +0000 | [diff] [blame] | 1454 | return true; |
| 1455 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1456 | default: |
| 1457 | return false; |
| 1458 | } |
| 1459 | } |
| 1460 | |
Sjoerd Meijer | 0eb96ed | 2016-07-29 08:16:16 +0000 | [diff] [blame] | 1461 | unsigned SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const { |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1462 | if (MI.getOpcode() == TargetOpcode::INLINEASM) { |
| 1463 | const MachineFunction *MF = MI.getParent()->getParent(); |
| 1464 | const char *AsmStr = MI.getOperand(0).getSymbolName(); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 1465 | return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo()); |
| 1466 | } |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1467 | return MI.getDesc().getSize(); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 1470 | SystemZII::Branch |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1471 | SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const { |
| 1472 | switch (MI.getOpcode()) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1473 | case SystemZ::BR: |
Ulrich Weigand | 2b3482f | 2017-07-17 17:41:11 +0000 | [diff] [blame] | 1474 | case SystemZ::BI: |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1475 | case SystemZ::J: |
| 1476 | case SystemZ::JG: |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 1477 | return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1478 | SystemZ::CCMASK_ANY, &MI.getOperand(0)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1479 | |
| 1480 | case SystemZ::BRC: |
| 1481 | case SystemZ::BRCL: |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1482 | return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(), |
| 1483 | MI.getOperand(1).getImm(), &MI.getOperand(2)); |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 1484 | |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 1485 | case SystemZ::BRCT: |
Ulrich Weigand | 7583991 | 2016-11-28 13:40:08 +0000 | [diff] [blame] | 1486 | case SystemZ::BRCTH: |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 1487 | return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1488 | SystemZ::CCMASK_CMP_NE, &MI.getOperand(2)); |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 1489 | |
| 1490 | case SystemZ::BRCTG: |
| 1491 | return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1492 | SystemZ::CCMASK_CMP_NE, &MI.getOperand(2)); |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 1493 | |
Richard Sandiford | e1d9f00 | 2013-05-29 11:58:52 +0000 | [diff] [blame] | 1494 | case SystemZ::CIJ: |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 1495 | case SystemZ::CRJ: |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 1496 | return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1497 | MI.getOperand(2).getImm(), &MI.getOperand(3)); |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 1498 | |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 1499 | case SystemZ::CLIJ: |
| 1500 | case SystemZ::CLRJ: |
| 1501 | return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1502 | MI.getOperand(2).getImm(), &MI.getOperand(3)); |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 1503 | |
Richard Sandiford | e1d9f00 | 2013-05-29 11:58:52 +0000 | [diff] [blame] | 1504 | case SystemZ::CGIJ: |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 1505 | case SystemZ::CGRJ: |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 1506 | return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1507 | MI.getOperand(2).getImm(), &MI.getOperand(3)); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1508 | |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 1509 | case SystemZ::CLGIJ: |
| 1510 | case SystemZ::CLGRJ: |
| 1511 | return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP, |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 1512 | MI.getOperand(2).getImm(), &MI.getOperand(3)); |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 1513 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1514 | default: |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 1515 | llvm_unreachable("Unrecognized branch opcode"); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | |
| 1519 | void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC, |
| 1520 | unsigned &LoadOpcode, |
| 1521 | unsigned &StoreOpcode) const { |
| 1522 | if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) { |
| 1523 | LoadOpcode = SystemZ::L; |
Richard Sandiford | 6cbd7f0 | 2013-09-25 10:29:47 +0000 | [diff] [blame] | 1524 | StoreOpcode = SystemZ::ST; |
Richard Sandiford | 0755c93 | 2013-10-01 11:26:28 +0000 | [diff] [blame] | 1525 | } else if (RC == &SystemZ::GRH32BitRegClass) { |
| 1526 | LoadOpcode = SystemZ::LFH; |
| 1527 | StoreOpcode = SystemZ::STFH; |
| 1528 | } else if (RC == &SystemZ::GRX32BitRegClass) { |
| 1529 | LoadOpcode = SystemZ::LMux; |
| 1530 | StoreOpcode = SystemZ::STMux; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1531 | } else if (RC == &SystemZ::GR64BitRegClass || |
| 1532 | RC == &SystemZ::ADDR64BitRegClass) { |
| 1533 | LoadOpcode = SystemZ::LG; |
| 1534 | StoreOpcode = SystemZ::STG; |
| 1535 | } else if (RC == &SystemZ::GR128BitRegClass || |
| 1536 | RC == &SystemZ::ADDR128BitRegClass) { |
| 1537 | LoadOpcode = SystemZ::L128; |
| 1538 | StoreOpcode = SystemZ::ST128; |
| 1539 | } else if (RC == &SystemZ::FP32BitRegClass) { |
| 1540 | LoadOpcode = SystemZ::LE; |
| 1541 | StoreOpcode = SystemZ::STE; |
| 1542 | } else if (RC == &SystemZ::FP64BitRegClass) { |
| 1543 | LoadOpcode = SystemZ::LD; |
| 1544 | StoreOpcode = SystemZ::STD; |
| 1545 | } else if (RC == &SystemZ::FP128BitRegClass) { |
| 1546 | LoadOpcode = SystemZ::LX; |
| 1547 | StoreOpcode = SystemZ::STX; |
Ulrich Weigand | 49506d7 | 2015-05-05 19:28:34 +0000 | [diff] [blame] | 1548 | } else if (RC == &SystemZ::VR32BitRegClass) { |
| 1549 | LoadOpcode = SystemZ::VL32; |
| 1550 | StoreOpcode = SystemZ::VST32; |
| 1551 | } else if (RC == &SystemZ::VR64BitRegClass) { |
| 1552 | LoadOpcode = SystemZ::VL64; |
| 1553 | StoreOpcode = SystemZ::VST64; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1554 | } else if (RC == &SystemZ::VF128BitRegClass || |
| 1555 | RC == &SystemZ::VR128BitRegClass) { |
| 1556 | LoadOpcode = SystemZ::VL; |
| 1557 | StoreOpcode = SystemZ::VST; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1558 | } else |
| 1559 | llvm_unreachable("Unsupported regclass to load or store"); |
| 1560 | } |
| 1561 | |
| 1562 | unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode, |
| 1563 | int64_t Offset) const { |
| 1564 | const MCInstrDesc &MCID = get(Opcode); |
| 1565 | int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset); |
| 1566 | if (isUInt<12>(Offset) && isUInt<12>(Offset2)) { |
| 1567 | // Get the instruction to use for unsigned 12-bit displacements. |
| 1568 | int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode); |
| 1569 | if (Disp12Opcode >= 0) |
| 1570 | return Disp12Opcode; |
| 1571 | |
| 1572 | // All address-related instructions can use unsigned 12-bit |
| 1573 | // displacements. |
| 1574 | return Opcode; |
| 1575 | } |
| 1576 | if (isInt<20>(Offset) && isInt<20>(Offset2)) { |
| 1577 | // Get the instruction to use for signed 20-bit displacements. |
| 1578 | int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode); |
| 1579 | if (Disp20Opcode >= 0) |
| 1580 | return Disp20Opcode; |
| 1581 | |
| 1582 | // Check whether Opcode allows signed 20-bit displacements. |
| 1583 | if (MCID.TSFlags & SystemZII::Has20BitOffset) |
| 1584 | return Opcode; |
| 1585 | } |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 1589 | unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const { |
| 1590 | switch (Opcode) { |
Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1591 | case SystemZ::L: return SystemZ::LT; |
| 1592 | case SystemZ::LY: return SystemZ::LT; |
| 1593 | case SystemZ::LG: return SystemZ::LTG; |
| 1594 | case SystemZ::LGF: return SystemZ::LTGF; |
| 1595 | case SystemZ::LR: return SystemZ::LTR; |
| 1596 | case SystemZ::LGFR: return SystemZ::LTGFR; |
| 1597 | case SystemZ::LGR: return SystemZ::LTGR; |
| 1598 | case SystemZ::LER: return SystemZ::LTEBR; |
| 1599 | case SystemZ::LDR: return SystemZ::LTDBR; |
| 1600 | case SystemZ::LXR: return SystemZ::LTXBR; |
Jonas Paulsson | 1262932 | 2015-10-01 18:12:28 +0000 | [diff] [blame] | 1601 | case SystemZ::LCDFR: return SystemZ::LCDBR; |
| 1602 | case SystemZ::LPDFR: return SystemZ::LPDBR; |
| 1603 | case SystemZ::LNDFR: return SystemZ::LNDBR; |
| 1604 | case SystemZ::LCDFR_32: return SystemZ::LCEBR; |
| 1605 | case SystemZ::LPDFR_32: return SystemZ::LPEBR; |
| 1606 | case SystemZ::LNDFR_32: return SystemZ::LNEBR; |
Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1607 | // On zEC12 we prefer to use RISBGN. But if there is a chance to |
| 1608 | // actually use the condition code, we may turn it back into RISGB. |
| 1609 | // Note that RISBG is not really a "load-and-test" instruction, |
| 1610 | // but sets the same condition code values, so is OK to use here. |
| 1611 | case SystemZ::RISBGN: return SystemZ::RISBG; |
| 1612 | default: return 0; |
Richard Sandiford | b49a3ab | 2013-08-05 11:03:20 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1616 | // Return true if Mask matches the regexp 0*1+0*, given that zero masks |
| 1617 | // have already been filtered out. Store the first set bit in LSB and |
| 1618 | // the number of set bits in Length if so. |
| 1619 | static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) { |
| 1620 | unsigned First = findFirstSet(Mask); |
| 1621 | uint64_t Top = (Mask >> First) + 1; |
| 1622 | if ((Top & -Top) == Top) { |
| 1623 | LSB = First; |
| 1624 | Length = findFirstSet(Top); |
| 1625 | return true; |
| 1626 | } |
| 1627 | return false; |
| 1628 | } |
| 1629 | |
| 1630 | bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize, |
| 1631 | unsigned &Start, unsigned &End) const { |
| 1632 | // Reject trivial all-zero masks. |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1633 | Mask &= allOnes(BitSize); |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1634 | if (Mask == 0) |
| 1635 | return false; |
| 1636 | |
| 1637 | // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of |
| 1638 | // the msb and End specifies the index of the lsb. |
| 1639 | unsigned LSB, Length; |
| 1640 | if (isStringOfOnes(Mask, LSB, Length)) { |
| 1641 | Start = 63 - (LSB + Length - 1); |
| 1642 | End = 63 - LSB; |
| 1643 | return true; |
| 1644 | } |
| 1645 | |
| 1646 | // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb |
| 1647 | // of the low 1s and End specifies the lsb of the high 1s. |
| 1648 | if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) { |
| 1649 | assert(LSB > 0 && "Bottom bit must be set"); |
| 1650 | assert(LSB + Length < BitSize && "Top bit must be set"); |
| 1651 | Start = 63 - (LSB - 1); |
| 1652 | End = 63 - (LSB + Length); |
| 1653 | return true; |
| 1654 | } |
| 1655 | |
| 1656 | return false; |
| 1657 | } |
| 1658 | |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 1659 | unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode, |
| 1660 | SystemZII::FusedCompareType Type, |
| 1661 | const MachineInstr *MI) const { |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 1662 | switch (Opcode) { |
Richard Sandiford | e1d9f00 | 2013-05-29 11:58:52 +0000 | [diff] [blame] | 1663 | case SystemZ::CHI: |
Richard Sandiford | e1d9f00 | 2013-05-29 11:58:52 +0000 | [diff] [blame] | 1664 | case SystemZ::CGHI: |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 1665 | if (!(MI && isInt<8>(MI->getOperand(1).getImm()))) |
| 1666 | return 0; |
| 1667 | break; |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 1668 | case SystemZ::CLFI: |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 1669 | case SystemZ::CLGFI: |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 1670 | if (!(MI && isUInt<8>(MI->getOperand(1).getImm()))) |
| 1671 | return 0; |
Ulrich Weigand | a0e7325 | 2016-11-11 12:48:26 +0000 | [diff] [blame] | 1672 | break; |
| 1673 | case SystemZ::CL: |
| 1674 | case SystemZ::CLG: |
| 1675 | if (!STI.hasMiscellaneousExtensions()) |
| 1676 | return 0; |
| 1677 | if (!(MI && MI->getOperand(3).getReg() == 0)) |
| 1678 | return 0; |
| 1679 | break; |
Ulrich Weigand | 2eb027d | 2016-04-07 16:11:44 +0000 | [diff] [blame] | 1680 | } |
| 1681 | switch (Type) { |
| 1682 | case SystemZII::CompareAndBranch: |
| 1683 | switch (Opcode) { |
| 1684 | case SystemZ::CR: |
| 1685 | return SystemZ::CRJ; |
| 1686 | case SystemZ::CGR: |
| 1687 | return SystemZ::CGRJ; |
| 1688 | case SystemZ::CHI: |
| 1689 | return SystemZ::CIJ; |
| 1690 | case SystemZ::CGHI: |
| 1691 | return SystemZ::CGIJ; |
| 1692 | case SystemZ::CLR: |
| 1693 | return SystemZ::CLRJ; |
| 1694 | case SystemZ::CLGR: |
| 1695 | return SystemZ::CLGRJ; |
| 1696 | case SystemZ::CLFI: |
| 1697 | return SystemZ::CLIJ; |
| 1698 | case SystemZ::CLGFI: |
| 1699 | return SystemZ::CLGIJ; |
| 1700 | default: |
| 1701 | return 0; |
| 1702 | } |
| 1703 | case SystemZII::CompareAndReturn: |
| 1704 | switch (Opcode) { |
| 1705 | case SystemZ::CR: |
| 1706 | return SystemZ::CRBReturn; |
| 1707 | case SystemZ::CGR: |
| 1708 | return SystemZ::CGRBReturn; |
| 1709 | case SystemZ::CHI: |
| 1710 | return SystemZ::CIBReturn; |
| 1711 | case SystemZ::CGHI: |
| 1712 | return SystemZ::CGIBReturn; |
| 1713 | case SystemZ::CLR: |
| 1714 | return SystemZ::CLRBReturn; |
| 1715 | case SystemZ::CLGR: |
| 1716 | return SystemZ::CLGRBReturn; |
| 1717 | case SystemZ::CLFI: |
| 1718 | return SystemZ::CLIBReturn; |
| 1719 | case SystemZ::CLGFI: |
| 1720 | return SystemZ::CLGIBReturn; |
| 1721 | default: |
| 1722 | return 0; |
| 1723 | } |
Ulrich Weigand | 848a513 | 2016-04-11 12:12:32 +0000 | [diff] [blame] | 1724 | case SystemZII::CompareAndSibcall: |
| 1725 | switch (Opcode) { |
| 1726 | case SystemZ::CR: |
| 1727 | return SystemZ::CRBCall; |
| 1728 | case SystemZ::CGR: |
| 1729 | return SystemZ::CGRBCall; |
| 1730 | case SystemZ::CHI: |
| 1731 | return SystemZ::CIBCall; |
| 1732 | case SystemZ::CGHI: |
| 1733 | return SystemZ::CGIBCall; |
| 1734 | case SystemZ::CLR: |
| 1735 | return SystemZ::CLRBCall; |
| 1736 | case SystemZ::CLGR: |
| 1737 | return SystemZ::CLGRBCall; |
| 1738 | case SystemZ::CLFI: |
| 1739 | return SystemZ::CLIBCall; |
| 1740 | case SystemZ::CLGFI: |
| 1741 | return SystemZ::CLGIBCall; |
| 1742 | default: |
| 1743 | return 0; |
| 1744 | } |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 1745 | case SystemZII::CompareAndTrap: |
| 1746 | switch (Opcode) { |
| 1747 | case SystemZ::CR: |
| 1748 | return SystemZ::CRT; |
| 1749 | case SystemZ::CGR: |
| 1750 | return SystemZ::CGRT; |
| 1751 | case SystemZ::CHI: |
| 1752 | return SystemZ::CIT; |
| 1753 | case SystemZ::CGHI: |
| 1754 | return SystemZ::CGIT; |
| 1755 | case SystemZ::CLR: |
| 1756 | return SystemZ::CLRT; |
| 1757 | case SystemZ::CLGR: |
| 1758 | return SystemZ::CLGRT; |
| 1759 | case SystemZ::CLFI: |
| 1760 | return SystemZ::CLFIT; |
| 1761 | case SystemZ::CLGFI: |
| 1762 | return SystemZ::CLGIT; |
Ulrich Weigand | a0e7325 | 2016-11-11 12:48:26 +0000 | [diff] [blame] | 1763 | case SystemZ::CL: |
| 1764 | return SystemZ::CLT; |
| 1765 | case SystemZ::CLG: |
| 1766 | return SystemZ::CLGT; |
Zhan Jun Liau | ab42cbc | 2016-06-10 19:58:10 +0000 | [diff] [blame] | 1767 | default: |
| 1768 | return 0; |
| 1769 | } |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 1770 | } |
Ulrich Weigand | 79391ee | 2016-04-07 16:33:25 +0000 | [diff] [blame] | 1771 | return 0; |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Ulrich Weigand | 2d9e3d9 | 2016-11-28 13:59:22 +0000 | [diff] [blame] | 1774 | unsigned SystemZInstrInfo::getLoadAndTrap(unsigned Opcode) const { |
| 1775 | if (!STI.hasLoadAndTrap()) |
| 1776 | return 0; |
| 1777 | switch (Opcode) { |
| 1778 | case SystemZ::L: |
| 1779 | case SystemZ::LY: |
| 1780 | return SystemZ::LAT; |
| 1781 | case SystemZ::LG: |
| 1782 | return SystemZ::LGAT; |
| 1783 | case SystemZ::LFH: |
| 1784 | return SystemZ::LFHAT; |
| 1785 | case SystemZ::LLGF: |
| 1786 | return SystemZ::LLGFAT; |
| 1787 | case SystemZ::LLGT: |
| 1788 | return SystemZ::LLGTAT; |
| 1789 | } |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1793 | void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB, |
| 1794 | MachineBasicBlock::iterator MBBI, |
| 1795 | unsigned Reg, uint64_t Value) const { |
| 1796 | DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc(); |
| 1797 | unsigned Opcode; |
| 1798 | if (isInt<16>(Value)) |
| 1799 | Opcode = SystemZ::LGHI; |
| 1800 | else if (SystemZ::isImmLL(Value)) |
| 1801 | Opcode = SystemZ::LLILL; |
| 1802 | else if (SystemZ::isImmLH(Value)) { |
| 1803 | Opcode = SystemZ::LLILH; |
| 1804 | Value >>= 16; |
| 1805 | } else { |
| 1806 | assert(isInt<32>(Value) && "Huge values not handled yet"); |
| 1807 | Opcode = SystemZ::LGFI; |
| 1808 | } |
| 1809 | BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value); |
| 1810 | } |
Jonas Paulsson | 8010b63 | 2016-10-20 08:27:16 +0000 | [diff] [blame] | 1811 | |
| 1812 | bool SystemZInstrInfo:: |
| 1813 | areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb, |
| 1814 | AliasAnalysis *AA) const { |
| 1815 | |
| 1816 | if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand()) |
| 1817 | return false; |
| 1818 | |
| 1819 | // If mem-operands show that the same address Value is used by both |
| 1820 | // instructions, check for non-overlapping offsets and widths. Not |
| 1821 | // sure if a register based analysis would be an improvement... |
| 1822 | |
| 1823 | MachineMemOperand *MMOa = *MIa.memoperands_begin(); |
| 1824 | MachineMemOperand *MMOb = *MIb.memoperands_begin(); |
| 1825 | const Value *VALa = MMOa->getValue(); |
| 1826 | const Value *VALb = MMOb->getValue(); |
| 1827 | bool SameVal = (VALa && VALb && (VALa == VALb)); |
| 1828 | if (!SameVal) { |
| 1829 | const PseudoSourceValue *PSVa = MMOa->getPseudoValue(); |
| 1830 | const PseudoSourceValue *PSVb = MMOb->getPseudoValue(); |
| 1831 | if (PSVa && PSVb && (PSVa == PSVb)) |
| 1832 | SameVal = true; |
| 1833 | } |
| 1834 | if (SameVal) { |
| 1835 | int OffsetA = MMOa->getOffset(), OffsetB = MMOb->getOffset(); |
| 1836 | int WidthA = MMOa->getSize(), WidthB = MMOb->getSize(); |
| 1837 | int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB; |
| 1838 | int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA; |
| 1839 | int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB; |
| 1840 | if (LowOffset + LowWidth <= HighOffset) |
| 1841 | return true; |
| 1842 | } |
| 1843 | |
| 1844 | return false; |
| 1845 | } |