blob: cb963a10010ccd7a64d5d44f2ae116622b4ab652 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZInstrInfo.cpp - SystemZ instruction information ------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the SystemZ implementation of the TargetInstrInfo class.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth6bda14b2017-06-06 11:49:48 +000013#include "SystemZInstrInfo.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000014#include "MCTargetDesc/SystemZMCTargetDesc.h"
15#include "SystemZ.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000016#include "SystemZInstrBuilder.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000017#include "SystemZSubtarget.h"
Jonas Paulsson4b017e62017-11-10 08:46:26 +000018#include "llvm/ADT/Statistic.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000019#include "llvm/CodeGen/LiveInterval.h"
Matthias Braunf8422972017-12-13 02:51:04 +000020#include "llvm/CodeGen/LiveIntervals.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000021#include "llvm/CodeGen/LiveVariables.h"
22#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000026#include "llvm/CodeGen/MachineMemOperand.h"
27#include "llvm/CodeGen/MachineOperand.h"
Richard Sandifordf6bae1e2013-07-02 15:28:56 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000029#include "llvm/CodeGen/SlotIndexes.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000030#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000031#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000032#include "llvm/MC/MCInstrDesc.h"
33#include "llvm/MC/MCRegisterInfo.h"
34#include "llvm/Support/BranchProbability.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/MathExtras.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000037#include "llvm/Target/TargetMachine.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000038#include <cassert>
39#include <cstdint>
40#include <iterator>
Ulrich Weigand5f613df2013-05-06 16:15:19 +000041
Chandler Carruthd174b722014-04-22 02:03:14 +000042using namespace llvm;
43
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000044#define GET_INSTRINFO_CTOR_DTOR
Ulrich Weigand5f613df2013-05-06 16:15:19 +000045#define GET_INSTRMAP_INFO
46#include "SystemZGenInstrInfo.inc"
47
Jonas Paulsson4b017e62017-11-10 08:46:26 +000048#define DEBUG_TYPE "systemz-II"
49STATISTIC(LOCRMuxJumps, "Number of LOCRMux jump-sequences (lower is better)");
50
Richard Sandiford6a06ba32013-07-31 11:36:35 +000051// Return a mask with Count low bits set.
52static uint64_t allOnes(unsigned int Count) {
53 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
54}
55
Richard Sandiford0755c932013-10-01 11:26:28 +000056// Reg should be a 32-bit GPR. Return true if it is a high register rather
57// than a low register.
58static bool isHighReg(unsigned int Reg) {
59 if (SystemZ::GRH32BitRegClass.contains(Reg))
60 return true;
61 assert(SystemZ::GR32BitRegClass.contains(Reg) && "Invalid GRX32");
62 return false;
63}
64
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000065// Pin the vtable to this file.
66void SystemZInstrInfo::anchor() {}
67
Eric Christopher673b3af2014-06-27 07:01:17 +000068SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti)
Ulrich Weigand5f613df2013-05-06 16:15:19 +000069 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
Eric Christopher673b3af2014-06-27 07:01:17 +000070 RI(), STI(sti) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000071}
72
73// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
74// each having the opcode given by NewOpcode.
75void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
76 unsigned NewOpcode) const {
77 MachineBasicBlock *MBB = MI->getParent();
78 MachineFunction &MF = *MBB->getParent();
79
80 // Get two load or store instructions. Use the original instruction for one
Alp Tokercb402912014-01-24 17:20:08 +000081 // of them (arbitrarily the second here) and create a clone for the other.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000082 MachineInstr *EarlierMI = MF.CloneMachineInstr(&*MI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000083 MBB->insert(MI, EarlierMI);
84
Jonas Paulsson8a7bd242017-03-17 06:47:08 +000085 // Set up the two 64-bit registers and remember super reg and its flags.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000086 MachineOperand &HighRegOp = EarlierMI->getOperand(0);
87 MachineOperand &LowRegOp = MI->getOperand(0);
Jonas Paulsson8a7bd242017-03-17 06:47:08 +000088 unsigned Reg128 = LowRegOp.getReg();
89 unsigned Reg128Killed = getKillRegState(LowRegOp.isKill());
90 unsigned Reg128Undef = getUndefRegState(LowRegOp.isUndef());
Richard Sandiford87a44362013-09-30 10:28:35 +000091 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64));
92 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64));
Ulrich Weigand5f613df2013-05-06 16:15:19 +000093
Jonas Paulsson8a7bd242017-03-17 06:47:08 +000094 if (MI->mayStore()) {
95 // Add implicit uses of the super register in case one of the subregs is
96 // undefined. We could track liveness and skip storing an undefined
97 // subreg, but this is hopefully rare (discovered with llvm-stress).
98 // If Reg128 was killed, set kill flag on MI.
99 unsigned Reg128UndefImpl = (Reg128Undef | RegState::Implicit);
100 MachineInstrBuilder(MF, EarlierMI).addReg(Reg128, Reg128UndefImpl);
101 MachineInstrBuilder(MF, MI).addReg(Reg128, (Reg128UndefImpl | Reg128Killed));
102 }
103
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000104 // The address in the first (high) instruction is already correct.
105 // Adjust the offset in the second (low) instruction.
106 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
107 MachineOperand &LowOffsetOp = MI->getOperand(2);
108 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
109
Jonas Paulsson1e864852017-04-24 12:40:28 +0000110 // Clear the kill flags on the registers in the first instruction.
111 if (EarlierMI->getOperand(0).isReg() && EarlierMI->getOperand(0).isUse())
112 EarlierMI->getOperand(0).setIsKill(false);
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000113 EarlierMI->getOperand(1).setIsKill(false);
Jonas Paulsson7da38202015-10-26 15:03:41 +0000114 EarlierMI->getOperand(3).setIsKill(false);
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000115
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000116 // Set the opcodes.
117 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
118 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
119 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
120
121 EarlierMI->setDesc(get(HighOpcode));
122 MI->setDesc(get(LowOpcode));
123}
124
125// Split ADJDYNALLOC instruction MI.
126void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
127 MachineBasicBlock *MBB = MI->getParent();
128 MachineFunction &MF = *MBB->getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000129 MachineFrameInfo &MFFrame = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000130 MachineOperand &OffsetMO = MI->getOperand(2);
131
Matthias Braun941a7052016-07-28 18:40:00 +0000132 uint64_t Offset = (MFFrame.getMaxCallFrameSize() +
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133 SystemZMC::CallFrameSize +
134 OffsetMO.getImm());
135 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
136 assert(NewOpcode && "No support for huge argument lists yet");
137 MI->setDesc(get(NewOpcode));
138 OffsetMO.setImm(Offset);
139}
140
Richard Sandiford01240232013-10-01 13:02:28 +0000141// MI is an RI-style pseudo instruction. Replace it with LowOpcode
142// if the first operand is a low GR32 and HighOpcode if the first operand
143// is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
144// and HighOpcode takes an unsigned 32-bit operand. In those cases,
145// MI has the same kind of operand as LowOpcode, so needs to be converted
146// if HighOpcode is used.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000147void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford01240232013-10-01 13:02:28 +0000148 unsigned HighOpcode,
149 bool ConvertHigh) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000150 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford01240232013-10-01 13:02:28 +0000151 bool IsHigh = isHighReg(Reg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000152 MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode));
Richard Sandiford01240232013-10-01 13:02:28 +0000153 if (IsHigh && ConvertHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000154 MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm()));
Richard Sandiford01240232013-10-01 13:02:28 +0000155}
156
Richard Sandiford42a694f2013-10-01 14:53:46 +0000157// MI is a three-operand RIE-style pseudo instruction. Replace it with
Jonas Paulsson18d877f2015-10-09 07:19:16 +0000158// LowOpcodeK if the registers are both low GR32s, otherwise use a move
Richard Sandiford42a694f2013-10-01 14:53:46 +0000159// followed by HighOpcode or LowOpcode, depending on whether the target
160// is a high or low GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000161void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford42a694f2013-10-01 14:53:46 +0000162 unsigned LowOpcodeK,
163 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000164 unsigned DestReg = MI.getOperand(0).getReg();
165 unsigned SrcReg = MI.getOperand(1).getReg();
Richard Sandiford42a694f2013-10-01 14:53:46 +0000166 bool DestIsHigh = isHighReg(DestReg);
167 bool SrcIsHigh = isHighReg(SrcReg);
168 if (!DestIsHigh && !SrcIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000169 MI.setDesc(get(LowOpcodeK));
Richard Sandiford42a694f2013-10-01 14:53:46 +0000170 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000171 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg,
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000172 SystemZ::LR, 32, MI.getOperand(1).isKill(),
173 MI.getOperand(1).isUndef());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000174 MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
175 MI.getOperand(1).setReg(DestReg);
176 MI.tieOperands(0, 1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000177 }
178}
179
Richard Sandiford0755c932013-10-01 11:26:28 +0000180// MI is an RXY-style pseudo instruction. Replace it with LowOpcode
181// if the first operand is a low GR32 and HighOpcode if the first operand
182// is a high GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000183void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford0755c932013-10-01 11:26:28 +0000184 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000185 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000186 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000187 MI.getOperand(2).getImm());
188 MI.setDesc(get(Opcode));
Richard Sandiford0755c932013-10-01 11:26:28 +0000189}
190
Ulrich Weigand524f2762016-11-28 13:34:08 +0000191// MI is a load-on-condition pseudo instruction with a single register
192// (source or destination) operand. Replace it with LowOpcode if the
193// register is a low GR32 and HighOpcode if the register is a high GR32.
194void SystemZInstrInfo::expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode,
195 unsigned HighOpcode) const {
196 unsigned Reg = MI.getOperand(0).getReg();
197 unsigned Opcode = isHighReg(Reg) ? HighOpcode : LowOpcode;
198 MI.setDesc(get(Opcode));
199}
200
201// MI is a load-register-on-condition pseudo instruction. Replace it with
202// LowOpcode if source and destination are both low GR32s and HighOpcode if
203// source and destination are both high GR32s.
204void SystemZInstrInfo::expandLOCRPseudo(MachineInstr &MI, unsigned LowOpcode,
205 unsigned HighOpcode) const {
206 unsigned DestReg = MI.getOperand(0).getReg();
207 unsigned SrcReg = MI.getOperand(2).getReg();
208 bool DestIsHigh = isHighReg(DestReg);
209 bool SrcIsHigh = isHighReg(SrcReg);
210
211 if (!DestIsHigh && !SrcIsHigh)
212 MI.setDesc(get(LowOpcode));
213 else if (DestIsHigh && SrcIsHigh)
214 MI.setDesc(get(HighOpcode));
Jonas Paulsson4b017e62017-11-10 08:46:26 +0000215 else
216 LOCRMuxJumps++;
Ulrich Weigand524f2762016-11-28 13:34:08 +0000217
218 // If we were unable to implement the pseudo with a single instruction, we
219 // need to convert it back into a branch sequence. This cannot be done here
220 // since the caller of expandPostRAPseudo does not handle changes to the CFG
221 // correctly. This change is defered to the SystemZExpandPseudo pass.
222}
223
Richard Sandiford21235a22013-10-01 12:49:07 +0000224// MI is an RR-style pseudo instruction that zero-extends the low Size bits
225// of one GRX32 into another. Replace it with LowOpcode if both operands
226// are low registers, otherwise use RISB[LH]G.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000227void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford21235a22013-10-01 12:49:07 +0000228 unsigned Size) const {
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000229 MachineInstrBuilder MIB =
230 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(),
231 MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode,
232 Size, MI.getOperand(1).isKill(), MI.getOperand(1).isUndef());
233
234 // Keep the remaining operands as-is.
235 for (unsigned I = 2; I < MI.getNumOperands(); ++I)
236 MIB.add(MI.getOperand(I));
237
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000238 MI.eraseFromParent();
Richard Sandiford21235a22013-10-01 12:49:07 +0000239}
240
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000241void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const {
242 MachineBasicBlock *MBB = MI->getParent();
243 MachineFunction &MF = *MBB->getParent();
Jonas Paulsson081b5a12017-05-24 13:15:48 +0000244 const unsigned Reg64 = MI->getOperand(0).getReg();
245 const unsigned Reg32 = RI.getSubReg(Reg64, SystemZ::subreg_l32);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000246
Jonas Paulsson081b5a12017-05-24 13:15:48 +0000247 // EAR can only load the low subregister so us a shift for %a0 to produce
248 // the GR containing %a0 and %a1.
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000249
250 // ear <reg>, %a0
Jonas Paulsson081b5a12017-05-24 13:15:48 +0000251 BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::EAR), Reg32)
252 .addReg(SystemZ::A0)
253 .addReg(Reg64, RegState::ImplicitDefine);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000254
255 // sllg <reg>, <reg>, 32
Jonas Paulsson081b5a12017-05-24 13:15:48 +0000256 BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::SLLG), Reg64)
257 .addReg(Reg64)
258 .addReg(0)
259 .addImm(32);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000260
261 // ear <reg>, %a1
Jonas Paulsson081b5a12017-05-24 13:15:48 +0000262 BuildMI(*MBB, MI, MI->getDebugLoc(), get(SystemZ::EAR), Reg32)
263 .addReg(SystemZ::A1);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000264
265 // lg <reg>, 40(<reg>)
266 MI->setDesc(get(SystemZ::LG));
Jonas Paulsson081b5a12017-05-24 13:15:48 +0000267 MachineInstrBuilder(MF, MI).addReg(Reg64).addImm(40).addReg(0);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000268}
269
Richard Sandiford0755c932013-10-01 11:26:28 +0000270// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
271// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
272// are low registers, otherwise use RISB[LH]G. Size is the number of bits
273// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
274// KillSrc is true if this move is the last use of SrcReg.
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000275MachineInstrBuilder
276SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
277 MachineBasicBlock::iterator MBBI,
278 const DebugLoc &DL, unsigned DestReg,
279 unsigned SrcReg, unsigned LowLowOpcode,
280 unsigned Size, bool KillSrc,
281 bool UndefSrc) const {
Richard Sandiford0755c932013-10-01 11:26:28 +0000282 unsigned Opcode;
283 bool DestIsHigh = isHighReg(DestReg);
284 bool SrcIsHigh = isHighReg(SrcReg);
285 if (DestIsHigh && SrcIsHigh)
286 Opcode = SystemZ::RISBHH;
287 else if (DestIsHigh && !SrcIsHigh)
288 Opcode = SystemZ::RISBHL;
289 else if (!DestIsHigh && SrcIsHigh)
290 Opcode = SystemZ::RISBLH;
291 else {
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000292 return BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000293 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc));
Richard Sandiford0755c932013-10-01 11:26:28 +0000294 }
295 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000296 return BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
Richard Sandiford0755c932013-10-01 11:26:28 +0000297 .addReg(DestReg, RegState::Undef)
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000298 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc))
Richard Sandiford0755c932013-10-01 11:26:28 +0000299 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
300}
301
Ulrich Weigand524f2762016-11-28 13:34:08 +0000302MachineInstr *SystemZInstrInfo::commuteInstructionImpl(MachineInstr &MI,
303 bool NewMI,
304 unsigned OpIdx1,
305 unsigned OpIdx2) const {
306 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & {
307 if (NewMI)
308 return *MI.getParent()->getParent()->CloneMachineInstr(&MI);
309 return MI;
310 };
311
312 switch (MI.getOpcode()) {
313 case SystemZ::LOCRMux:
314 case SystemZ::LOCFHR:
315 case SystemZ::LOCR:
316 case SystemZ::LOCGR: {
317 auto &WorkingMI = cloneIfNew(MI);
318 // Invert condition.
319 unsigned CCValid = WorkingMI.getOperand(3).getImm();
320 unsigned CCMask = WorkingMI.getOperand(4).getImm();
321 WorkingMI.getOperand(4).setImm(CCMask ^ CCValid);
322 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
323 OpIdx1, OpIdx2);
324 }
325 default:
326 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
327 }
328}
329
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000330// If MI is a simple load or store for a frame object, return the register
331// it loads or stores and set FrameIndex to the index of the frame object.
332// Return 0 otherwise.
333//
334// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000335static int isSimpleMove(const MachineInstr &MI, int &FrameIndex,
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000336 unsigned Flag) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000337 const MCInstrDesc &MCID = MI.getDesc();
338 if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() &&
339 MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) {
340 FrameIndex = MI.getOperand(1).getIndex();
341 return MI.getOperand(0).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000342 }
343 return 0;
344}
345
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000346unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000347 int &FrameIndex) const {
348 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
349}
350
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000351unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000352 int &FrameIndex) const {
353 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
354}
355
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000356bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI,
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000357 int &DestFrameIndex,
358 int &SrcFrameIndex) const {
359 // Check for MVC 0(Length,FI1),0(FI2)
Matthias Braun941a7052016-07-28 18:40:00 +0000360 const MachineFrameInfo &MFI = MI.getParent()->getParent()->getFrameInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000361 if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() ||
362 MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() ||
363 MI.getOperand(4).getImm() != 0)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000364 return false;
365
366 // Check that Length covers the full slots.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000367 int64_t Length = MI.getOperand(2).getImm();
368 unsigned FI1 = MI.getOperand(0).getIndex();
369 unsigned FI2 = MI.getOperand(3).getIndex();
Matthias Braun941a7052016-07-28 18:40:00 +0000370 if (MFI.getObjectSize(FI1) != Length ||
371 MFI.getObjectSize(FI2) != Length)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000372 return false;
373
374 DestFrameIndex = FI1;
375 SrcFrameIndex = FI2;
376 return true;
377}
378
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000379bool SystemZInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000380 MachineBasicBlock *&TBB,
381 MachineBasicBlock *&FBB,
382 SmallVectorImpl<MachineOperand> &Cond,
383 bool AllowModify) const {
384 // Most of the code and comments here are boilerplate.
385
386 // Start from the bottom of the block and work up, examining the
387 // terminator instructions.
388 MachineBasicBlock::iterator I = MBB.end();
389 while (I != MBB.begin()) {
390 --I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000391 if (I->isDebugInstr())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000392 continue;
393
394 // Working from the bottom, when we see a non-terminator instruction, we're
395 // done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000396 if (!isUnpredicatedTerminator(*I))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000397 break;
398
399 // A terminator that isn't a branch can't easily be handled by this
400 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000401 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000402 return true;
403
404 // Can't handle indirect branches.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000405 SystemZII::Branch Branch(getBranchInfo(*I));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000406 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000407 return true;
408
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000409 // Punt on compound branches.
410 if (Branch.Type != SystemZII::BranchNormal)
411 return true;
412
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000413 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000414 // Handle unconditional branches.
415 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000416 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000417 continue;
418 }
419
420 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000421 while (std::next(I) != MBB.end())
422 std::next(I)->eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000423
424 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000425 FBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000426
427 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000428 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000429 TBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000430 I->eraseFromParent();
431 I = MBB.end();
432 continue;
433 }
434
435 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000436 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000437 continue;
438 }
439
440 // Working from the bottom, handle the first conditional branch.
441 if (Cond.empty()) {
442 // FIXME: add X86-style branch swap
443 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000444 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000445 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000446 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000447 continue;
448 }
449
450 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000451 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000452
453 // Only handle the case where all conditional branches branch to the same
454 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000455 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000456 return true;
457
458 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000459 unsigned OldCCValid = Cond[0].getImm();
460 unsigned OldCCMask = Cond[1].getImm();
461 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000462 continue;
463
464 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000465 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000466 }
467
468 return false;
469}
470
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000471unsigned SystemZInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000472 int *BytesRemoved) const {
473 assert(!BytesRemoved && "code size not handled");
474
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000475 // Most of the code and comments here are boilerplate.
476 MachineBasicBlock::iterator I = MBB.end();
477 unsigned Count = 0;
478
479 while (I != MBB.begin()) {
480 --I;
Shiva Chen801bf7e2018-05-09 02:42:00 +0000481 if (I->isDebugInstr())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000482 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000483 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000484 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000485 if (!getBranchInfo(*I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000486 break;
487 // Remove the branch.
488 I->eraseFromParent();
489 I = MBB.end();
490 ++Count;
491 }
492
493 return Count;
494}
495
Richard Sandiford3d768e32013-07-31 12:30:20 +0000496bool SystemZInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000497reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000498 assert(Cond.size() == 2 && "Invalid condition");
499 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
500 return false;
501}
502
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000503unsigned SystemZInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000504 MachineBasicBlock *TBB,
505 MachineBasicBlock *FBB,
506 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000507 const DebugLoc &DL,
508 int *BytesAdded) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000509 // In this function we output 32-bit branches, which should always
510 // have enough range. They can be shortened and relaxed by later code
511 // in the pipeline, if desired.
512
513 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000514 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000515 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000516 "SystemZ branch conditions have one component!");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000517 assert(!BytesAdded && "code size not handled");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000518
519 if (Cond.empty()) {
520 // Unconditional branch?
521 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000522 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000523 return 1;
524 }
525
526 // Conditional branch.
527 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000528 unsigned CCValid = Cond[0].getImm();
529 unsigned CCMask = Cond[1].getImm();
530 BuildMI(&MBB, DL, get(SystemZ::BRC))
531 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000532 ++Count;
533
534 if (FBB) {
535 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000536 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000537 ++Count;
538 }
539 return Count;
540}
541
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000542bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
543 unsigned &SrcReg2, int &Mask,
544 int &Value) const {
545 assert(MI.isCompare() && "Caller should have checked for a comparison");
Richard Sandiford564681c2013-08-12 10:28:10 +0000546
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000547 if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() &&
548 MI.getOperand(1).isImm()) {
549 SrcReg = MI.getOperand(0).getReg();
Richard Sandiford564681c2013-08-12 10:28:10 +0000550 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000551 Value = MI.getOperand(1).getImm();
Richard Sandiford564681c2013-08-12 10:28:10 +0000552 Mask = ~0;
553 return true;
554 }
555
556 return false;
557}
558
Ulrich Weigand524f2762016-11-28 13:34:08 +0000559bool SystemZInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
560 ArrayRef<MachineOperand> Pred,
561 unsigned TrueReg, unsigned FalseReg,
562 int &CondCycles, int &TrueCycles,
563 int &FalseCycles) const {
564 // Not all subtargets have LOCR instructions.
565 if (!STI.hasLoadStoreOnCond())
566 return false;
567 if (Pred.size() != 2)
568 return false;
569
570 // Check register classes.
571 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
572 const TargetRegisterClass *RC =
573 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
574 if (!RC)
575 return false;
576
577 // We have LOCR instructions for 32 and 64 bit general purpose registers.
578 if ((STI.hasLoadStoreOnCond2() &&
579 SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) ||
580 SystemZ::GR32BitRegClass.hasSubClassEq(RC) ||
581 SystemZ::GR64BitRegClass.hasSubClassEq(RC)) {
582 CondCycles = 2;
583 TrueCycles = 2;
584 FalseCycles = 2;
585 return true;
Richard Sandifordf2404162013-07-25 09:11:15 +0000586 }
Ulrich Weigand524f2762016-11-28 13:34:08 +0000587
588 // Can't do anything else.
589 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000590}
591
Ulrich Weigand524f2762016-11-28 13:34:08 +0000592void SystemZInstrInfo::insertSelect(MachineBasicBlock &MBB,
593 MachineBasicBlock::iterator I,
594 const DebugLoc &DL, unsigned DstReg,
595 ArrayRef<MachineOperand> Pred,
596 unsigned TrueReg,
597 unsigned FalseReg) const {
598 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
599 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
600
601 assert(Pred.size() == 2 && "Invalid condition");
602 unsigned CCValid = Pred[0].getImm();
603 unsigned CCMask = Pred[1].getImm();
604
605 unsigned Opc;
606 if (SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) {
607 if (STI.hasLoadStoreOnCond2())
608 Opc = SystemZ::LOCRMux;
609 else {
610 Opc = SystemZ::LOCR;
611 MRI.constrainRegClass(DstReg, &SystemZ::GR32BitRegClass);
Jonas Paulssonc7bb22e2017-03-31 14:06:59 +0000612 unsigned TReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
613 unsigned FReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
614 BuildMI(MBB, I, DL, get(TargetOpcode::COPY), TReg).addReg(TrueReg);
615 BuildMI(MBB, I, DL, get(TargetOpcode::COPY), FReg).addReg(FalseReg);
616 TrueReg = TReg;
617 FalseReg = FReg;
Ulrich Weigand524f2762016-11-28 13:34:08 +0000618 }
619 } else if (SystemZ::GR64BitRegClass.hasSubClassEq(RC))
620 Opc = SystemZ::LOCGR;
621 else
622 llvm_unreachable("Invalid register class");
623
624 BuildMI(MBB, I, DL, get(Opc), DstReg)
625 .addReg(FalseReg).addReg(TrueReg)
626 .addImm(CCValid).addImm(CCMask);
627}
628
629bool SystemZInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
630 unsigned Reg,
631 MachineRegisterInfo *MRI) const {
632 unsigned DefOpc = DefMI.getOpcode();
633 if (DefOpc != SystemZ::LHIMux && DefOpc != SystemZ::LHI &&
634 DefOpc != SystemZ::LGHI)
635 return false;
636 if (DefMI.getOperand(0).getReg() != Reg)
637 return false;
638 int32_t ImmVal = (int32_t)DefMI.getOperand(1).getImm();
639
640 unsigned UseOpc = UseMI.getOpcode();
641 unsigned NewUseOpc;
642 unsigned UseIdx;
643 int CommuteIdx = -1;
644 switch (UseOpc) {
645 case SystemZ::LOCRMux:
646 if (!STI.hasLoadStoreOnCond2())
647 return false;
648 NewUseOpc = SystemZ::LOCHIMux;
649 if (UseMI.getOperand(2).getReg() == Reg)
650 UseIdx = 2;
651 else if (UseMI.getOperand(1).getReg() == Reg)
652 UseIdx = 2, CommuteIdx = 1;
653 else
654 return false;
655 break;
656 case SystemZ::LOCGR:
657 if (!STI.hasLoadStoreOnCond2())
658 return false;
659 NewUseOpc = SystemZ::LOCGHI;
660 if (UseMI.getOperand(2).getReg() == Reg)
661 UseIdx = 2;
662 else if (UseMI.getOperand(1).getReg() == Reg)
663 UseIdx = 2, CommuteIdx = 1;
664 else
665 return false;
666 break;
667 default:
668 return false;
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000669 }
Ulrich Weigand524f2762016-11-28 13:34:08 +0000670
671 if (CommuteIdx != -1)
672 if (!commuteInstruction(UseMI, false, CommuteIdx, UseIdx))
673 return false;
674
675 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
676 UseMI.setDesc(get(NewUseOpc));
677 UseMI.getOperand(UseIdx).ChangeToImmediate(ImmVal);
678 if (DeleteDef)
679 DefMI.eraseFromParent();
680
681 return true;
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000682}
683
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000684bool SystemZInstrInfo::isPredicable(const MachineInstr &MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000685 unsigned Opcode = MI.getOpcode();
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000686 if (Opcode == SystemZ::Return ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000687 Opcode == SystemZ::Trap ||
Ulrich Weigand848a5132016-04-11 12:12:32 +0000688 Opcode == SystemZ::CallJG ||
689 Opcode == SystemZ::CallBR)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000690 return true;
691 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000692}
693
694bool SystemZInstrInfo::
695isProfitableToIfCvt(MachineBasicBlock &MBB,
696 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000697 BranchProbability Probability) const {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000698 // Avoid using conditional returns at the end of a loop (since then
699 // we'd need to emit an unconditional branch to the beginning anyway,
700 // making the loop body longer). This doesn't apply for low-probability
701 // loops (eg. compare-and-swap retry), so just decide based on branch
702 // probability instead of looping structure.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000703 // However, since Compare and Trap instructions cost the same as a regular
704 // Compare instruction, we should allow the if conversion to convert this
705 // into a Conditional Compare regardless of the branch probability.
706 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap &&
707 MBB.succ_empty() && Probability < BranchProbability(1, 8))
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000708 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000709 // For now only convert single instructions.
710 return NumCycles == 1;
711}
712
713bool SystemZInstrInfo::
714isProfitableToIfCvt(MachineBasicBlock &TMBB,
715 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
716 MachineBasicBlock &FMBB,
717 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000718 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000719 // For now avoid converting mutually-exclusive cases.
720 return false;
721}
722
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000723bool SystemZInstrInfo::
724isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
725 BranchProbability Probability) const {
726 // For now only duplicate single instructions.
727 return NumCycles == 1;
728}
729
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000730bool SystemZInstrInfo::PredicateInstruction(
731 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000732 assert(Pred.size() == 2 && "Invalid condition");
733 unsigned CCValid = Pred[0].getImm();
734 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000735 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000736 unsigned Opcode = MI.getOpcode();
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000737 if (Opcode == SystemZ::Trap) {
738 MI.setDesc(get(SystemZ::CondTrap));
739 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
740 .addImm(CCValid).addImm(CCMask)
741 .addReg(SystemZ::CC, RegState::Implicit);
742 return true;
743 }
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000744 if (Opcode == SystemZ::Return) {
745 MI.setDesc(get(SystemZ::CondReturn));
746 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
747 .addImm(CCValid).addImm(CCMask)
748 .addReg(SystemZ::CC, RegState::Implicit);
749 return true;
750 }
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000751 if (Opcode == SystemZ::CallJG) {
Zhan Jun Liaua5d60af2016-07-07 15:34:46 +0000752 MachineOperand FirstOp = MI.getOperand(0);
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000753 const uint32_t *RegMask = MI.getOperand(1).getRegMask();
754 MI.RemoveOperand(1);
755 MI.RemoveOperand(0);
756 MI.setDesc(get(SystemZ::CallBRCL));
757 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Diana Picus116bbab2017-01-13 09:58:52 +0000758 .addImm(CCValid)
759 .addImm(CCMask)
760 .add(FirstOp)
761 .addRegMask(RegMask)
762 .addReg(SystemZ::CC, RegState::Implicit);
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000763 return true;
764 }
Ulrich Weigand848a5132016-04-11 12:12:32 +0000765 if (Opcode == SystemZ::CallBR) {
766 const uint32_t *RegMask = MI.getOperand(0).getRegMask();
767 MI.RemoveOperand(0);
768 MI.setDesc(get(SystemZ::CallBCR));
769 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
770 .addImm(CCValid).addImm(CCMask)
771 .addRegMask(RegMask)
772 .addReg(SystemZ::CC, RegState::Implicit);
773 return true;
774 }
Richard Sandifordf2404162013-07-25 09:11:15 +0000775 return false;
776}
777
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000778void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
779 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000780 const DebugLoc &DL, unsigned DestReg,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000781 unsigned SrcReg, bool KillSrc) const {
Jonas Paulsson4fd15622017-05-04 13:33:30 +0000782 // Split 128-bit GPR moves into two 64-bit moves. Add implicit uses of the
783 // super register in case one of the subregs is undefined.
784 // This handles ADDR128 too.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000785 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000786 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
787 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
Jonas Paulsson4fd15622017-05-04 13:33:30 +0000788 MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI))
789 .addReg(SrcReg, RegState::Implicit);
Richard Sandiford87a44362013-09-30 10:28:35 +0000790 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
791 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Jonas Paulsson4fd15622017-05-04 13:33:30 +0000792 MachineInstrBuilder(*MBB.getParent(), std::prev(MBBI))
793 .addReg(SrcReg, (getKillRegState(KillSrc) | RegState::Implicit));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000794 return;
795 }
796
Richard Sandiford0755c932013-10-01 11:26:28 +0000797 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000798 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc,
799 false);
Richard Sandiford0755c932013-10-01 11:26:28 +0000800 return;
801 }
802
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000803 // Move 128-bit floating-point values between VR128 and FP128.
804 if (SystemZ::VR128BitRegClass.contains(DestReg) &&
805 SystemZ::FP128BitRegClass.contains(SrcReg)) {
806 unsigned SrcRegHi =
807 RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_h64),
Krzysztof Parzyszek2a119b92018-08-15 15:21:23 +0000808 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000809 unsigned SrcRegLo =
810 RI.getMatchingSuperReg(RI.getSubReg(SrcReg, SystemZ::subreg_l64),
Krzysztof Parzyszek2a119b92018-08-15 15:21:23 +0000811 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000812
813 BuildMI(MBB, MBBI, DL, get(SystemZ::VMRHG), DestReg)
814 .addReg(SrcRegHi, getKillRegState(KillSrc))
815 .addReg(SrcRegLo, getKillRegState(KillSrc));
816 return;
817 }
818 if (SystemZ::FP128BitRegClass.contains(DestReg) &&
819 SystemZ::VR128BitRegClass.contains(SrcReg)) {
820 unsigned DestRegHi =
821 RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_h64),
Krzysztof Parzyszek2a119b92018-08-15 15:21:23 +0000822 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000823 unsigned DestRegLo =
824 RI.getMatchingSuperReg(RI.getSubReg(DestReg, SystemZ::subreg_l64),
Krzysztof Parzyszek2a119b92018-08-15 15:21:23 +0000825 SystemZ::subreg_h64, &SystemZ::VR128BitRegClass);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000826
827 if (DestRegHi != SrcReg)
828 copyPhysReg(MBB, MBBI, DL, DestRegHi, SrcReg, false);
829 BuildMI(MBB, MBBI, DL, get(SystemZ::VREPG), DestRegLo)
830 .addReg(SrcReg, getKillRegState(KillSrc)).addImm(1);
831 return;
832 }
833
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000834 // Move CC value from/to a GR32.
835 if (SrcReg == SystemZ::CC) {
836 auto MIB = BuildMI(MBB, MBBI, DL, get(SystemZ::IPM), DestReg);
837 if (KillSrc) {
838 const MachineFunction *MF = MBB.getParent();
839 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
840 MIB->addRegisterKilled(SrcReg, TRI);
841 }
842 return;
843 }
844 if (DestReg == SystemZ::CC) {
845 BuildMI(MBB, MBBI, DL, get(SystemZ::TMLH))
846 .addReg(SrcReg, getKillRegState(KillSrc))
847 .addImm(3 << (SystemZ::IPM_CC - 16));
848 return;
849 }
850
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000851 // Everything else needs only one instruction.
852 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000853 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000854 Opcode = SystemZ::LGR;
855 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigandcdce0262016-03-14 13:50:03 +0000856 // For z13 we prefer LDR over LER to avoid partial register dependencies.
857 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000858 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
859 Opcode = SystemZ::LDR;
860 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
861 Opcode = SystemZ::LXR;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000862 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
863 Opcode = SystemZ::VLR32;
864 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
865 Opcode = SystemZ::VLR64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000866 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
867 Opcode = SystemZ::VLR;
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000868 else if (SystemZ::AR32BitRegClass.contains(DestReg, SrcReg))
869 Opcode = SystemZ::CPYA;
870 else if (SystemZ::AR32BitRegClass.contains(DestReg) &&
871 SystemZ::GR32BitRegClass.contains(SrcReg))
872 Opcode = SystemZ::SAR;
873 else if (SystemZ::GR32BitRegClass.contains(DestReg) &&
874 SystemZ::AR32BitRegClass.contains(SrcReg))
875 Opcode = SystemZ::EAR;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000876 else
877 llvm_unreachable("Impossible reg-to-reg copy");
878
879 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
880 .addReg(SrcReg, getKillRegState(KillSrc));
881}
882
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000883void SystemZInstrInfo::storeRegToStackSlot(
884 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
885 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
886 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000887 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
888
889 // Callers may expect a single instruction, so keep 128-bit moves
890 // together for now and lower them after register allocation.
891 unsigned LoadOpcode, StoreOpcode;
892 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
893 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000894 .addReg(SrcReg, getKillRegState(isKill)),
895 FrameIdx);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000896}
897
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000898void SystemZInstrInfo::loadRegFromStackSlot(
899 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
900 int FrameIdx, const TargetRegisterClass *RC,
901 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000902 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
903
904 // Callers may expect a single instruction, so keep 128-bit moves
905 // together for now and lower them after register allocation.
906 unsigned LoadOpcode, StoreOpcode;
907 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
908 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
909 FrameIdx);
910}
911
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000912// Return true if MI is a simple load or store with a 12-bit displacement
913// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
914static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
915 const MCInstrDesc &MCID = MI->getDesc();
916 return ((MCID.TSFlags & Flag) &&
917 isUInt<12>(MI->getOperand(2).getImm()) &&
918 MI->getOperand(3).getReg() == 0);
919}
920
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000921namespace {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000922
Richard Sandifordc2312692014-03-06 10:38:30 +0000923struct LogicOp {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000924 LogicOp() = default;
Richard Sandifordc2312692014-03-06 10:38:30 +0000925 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
926 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000927
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000928 explicit operator bool() const { return RegSize; }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000929
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000930 unsigned RegSize = 0;
931 unsigned ImmLSB = 0;
932 unsigned ImmSize = 0;
Richard Sandifordc2312692014-03-06 10:38:30 +0000933};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000934
Richard Sandifordc2312692014-03-06 10:38:30 +0000935} // end anonymous namespace
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000936
937static LogicOp interpretAndImmediate(unsigned Opcode) {
938 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000939 case SystemZ::NILMux: return LogicOp(32, 0, 16);
940 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000941 case SystemZ::NILL64: return LogicOp(64, 0, 16);
942 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000943 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
944 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
945 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000946 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000947 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000948 default: return LogicOp();
949 }
950}
951
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000952static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) {
953 if (OldMI->registerDefIsDead(SystemZ::CC)) {
954 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC);
955 if (CCDef != nullptr)
956 CCDef->setIsDead(true);
957 }
958}
959
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000960// Used to return from convertToThreeAddress after replacing two-address
961// instruction OldMI with three-address instruction NewMI.
962static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
963 MachineInstr *NewMI,
964 LiveVariables *LV) {
965 if (LV) {
966 unsigned NumOps = OldMI->getNumOperands();
967 for (unsigned I = 1; I < NumOps; ++I) {
968 MachineOperand &Op = OldMI->getOperand(I);
969 if (Op.isReg() && Op.isKill())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000970 LV->replaceKillInstruction(Op.getReg(), *OldMI, *NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000971 }
972 }
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000973 transferDeadCC(OldMI, NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000974 return NewMI;
975}
976
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000977MachineInstr *SystemZInstrInfo::convertToThreeAddress(
978 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
979 MachineBasicBlock *MBB = MI.getParent();
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000980 MachineFunction *MF = MBB->getParent();
981 MachineRegisterInfo &MRI = MF->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000982
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000983 unsigned Opcode = MI.getOpcode();
984 unsigned NumOps = MI.getNumOperands();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000985
986 // Try to convert something like SLL into SLLK, if supported.
987 // We prefer to keep the two-operand form where possible both
988 // because it tends to be shorter and because some instructions
989 // have memory forms that can be used during spilling.
Eric Christopher673b3af2014-06-27 07:01:17 +0000990 if (STI.hasDistinctOps()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000991 MachineOperand &Dest = MI.getOperand(0);
992 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000993 unsigned DestReg = Dest.getReg();
994 unsigned SrcReg = Src.getReg();
995 // AHIMux is only really a three-operand instruction when both operands
996 // are low registers. Try to constrain both operands to be low if
997 // possible.
998 if (Opcode == SystemZ::AHIMux &&
999 TargetRegisterInfo::isVirtualRegister(DestReg) &&
1000 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
1001 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
1002 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
1003 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
1004 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
1005 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001006 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
1007 if (ThreeOperandOpcode >= 0) {
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +00001008 // Create three address instruction without adding the implicit
1009 // operands. Those will instead be copied over from the original
1010 // instruction by the loop below.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001011 MachineInstrBuilder MIB(
1012 *MF, MF->CreateMachineInstr(get(ThreeOperandOpcode), MI.getDebugLoc(),
1013 /*NoImplicit=*/true));
Diana Picus116bbab2017-01-13 09:58:52 +00001014 MIB.add(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001015 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001016 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001017 // Keep the remaining operands as-is.
1018 for (unsigned I = 2; I < NumOps; ++I)
Diana Picus116bbab2017-01-13 09:58:52 +00001019 MIB.add(MI.getOperand(I));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +00001020 MBB->insert(MI, MIB);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001021 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001022 }
1023 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001024
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001025 // Try to convert an AND into an RISBG-type instruction.
1026 if (LogicOp And = interpretAndImmediate(Opcode)) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001027 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB;
Richard Sandiford70284282013-10-01 14:20:41 +00001028 // AND IMMEDIATE leaves the other bits of the register unchanged.
1029 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
1030 unsigned Start, End;
1031 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
1032 unsigned NewOpcode;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001033 if (And.RegSize == 64) {
Richard Sandiford70284282013-10-01 14:20:41 +00001034 NewOpcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001035 // Prefer RISBGN if available, since it does not clobber CC.
1036 if (STI.hasMiscellaneousExtensions())
1037 NewOpcode = SystemZ::RISBGN;
1038 } else {
Richard Sandiford70284282013-10-01 14:20:41 +00001039 NewOpcode = SystemZ::RISBMux;
1040 Start &= 31;
1041 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001042 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001043 MachineOperand &Dest = MI.getOperand(0);
1044 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford70284282013-10-01 14:20:41 +00001045 MachineInstrBuilder MIB =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001046 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00001047 .add(Dest)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001048 .addReg(0)
1049 .addReg(Src.getReg(), getKillRegState(Src.isKill()),
1050 Src.getSubReg())
1051 .addImm(Start)
1052 .addImm(End + 128)
1053 .addImm(0);
1054 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001055 }
1056 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001057 return nullptr;
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001058}
1059
Keno Fischere70b31f2015-06-08 20:09:58 +00001060MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001061 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001062 MachineBasicBlock::iterator InsertPt, int FrameIndex,
1063 LiveIntervals *LIS) const {
1064 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001065 const MachineFrameInfo &MFI = MF.getFrameInfo();
1066 unsigned Size = MFI.getObjectSize(FrameIndex);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001067 unsigned Opcode = MI.getOpcode();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001068
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001069 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001070 if (LIS != nullptr && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
1071 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) {
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001072
1073 // Check CC liveness, since new instruction introduces a dead
1074 // def of CC.
1075 MCRegUnitIterator CCUnit(SystemZ::CC, TRI);
1076 LiveRange &CCLiveRange = LIS->getRegUnit(*CCUnit);
1077 ++CCUnit;
Eugene Zelenko3943d2b2017-01-24 22:10:43 +00001078 assert(!CCUnit.isValid() && "CC only has one reg unit.");
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001079 SlotIndex MISlot =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001080 LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot();
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001081 if (!CCLiveRange.liveAt(MISlot)) {
1082 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001083 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt,
1084 MI.getDebugLoc(), get(SystemZ::AGSI))
1085 .addFrameIndex(FrameIndex)
1086 .addImm(0)
1087 .addImm(MI.getOperand(2).getImm());
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001088 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true);
1089 CCLiveRange.createDeadDef(MISlot, LIS->getVNInfoAllocator());
1090 return BuiltMI;
1091 }
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001092 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001093 return nullptr;
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001094 }
1095
1096 // All other cases require a single operand.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001097 if (Ops.size() != 1)
Craig Topper062a2ba2014-04-25 05:30:21 +00001098 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001099
1100 unsigned OpNum = Ops[0];
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +00001101 assert(Size * 8 ==
1102 TRI->getRegSizeInBits(*MF.getRegInfo()
1103 .getRegClass(MI.getOperand(OpNum).getReg())) &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +00001104 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001105
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001106 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 &&
1107 isInt<8>(MI.getOperand(2).getImm())) {
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001108 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
1109 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +00001110 MachineInstr *BuiltMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001111 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
1112 .addFrameIndex(FrameIndex)
1113 .addImm(0)
1114 .addImm(MI.getOperand(2).getImm());
1115 transferDeadCC(&MI, BuiltMI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +00001116 return BuiltMI;
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001117 }
1118
Ulrich Weigandc3ec80f2018-04-30 17:54:28 +00001119 if ((Opcode == SystemZ::ALFI && OpNum == 0 &&
1120 isInt<8>((int32_t)MI.getOperand(2).getImm())) ||
1121 (Opcode == SystemZ::ALGFI && OpNum == 0 &&
1122 isInt<8>((int64_t)MI.getOperand(2).getImm()))) {
1123 // AL(G)FI %reg, CONST -> AL(G)SI %mem, CONST
1124 Opcode = (Opcode == SystemZ::ALFI ? SystemZ::ALSI : SystemZ::ALGSI);
1125 MachineInstr *BuiltMI =
1126 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
1127 .addFrameIndex(FrameIndex)
1128 .addImm(0)
1129 .addImm((int8_t)MI.getOperand(2).getImm());
1130 transferDeadCC(&MI, BuiltMI);
1131 return BuiltMI;
1132 }
1133
1134 if ((Opcode == SystemZ::SLFI && OpNum == 0 &&
1135 isInt<8>((int32_t)-MI.getOperand(2).getImm())) ||
1136 (Opcode == SystemZ::SLGFI && OpNum == 0 &&
1137 isInt<8>((int64_t)-MI.getOperand(2).getImm()))) {
1138 // SL(G)FI %reg, CONST -> AL(G)SI %mem, -CONST
1139 Opcode = (Opcode == SystemZ::SLFI ? SystemZ::ALSI : SystemZ::ALGSI);
1140 MachineInstr *BuiltMI =
1141 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
1142 .addFrameIndex(FrameIndex)
1143 .addImm(0)
1144 .addImm((int8_t)-MI.getOperand(2).getImm());
1145 transferDeadCC(&MI, BuiltMI);
1146 return BuiltMI;
1147 }
1148
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001149 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
1150 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
1151 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
1152 // If we're spilling the destination of an LDGR or LGDR, store the
1153 // source register instead.
1154 if (OpNum == 0) {
1155 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001156 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001157 get(StoreOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00001158 .add(MI.getOperand(1))
Keno Fischere70b31f2015-06-08 20:09:58 +00001159 .addFrameIndex(FrameIndex)
1160 .addImm(0)
1161 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001162 }
1163 // If we're spilling the source of an LDGR or LGDR, load the
1164 // destination register instead.
1165 if (OpNum == 1) {
1166 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001167 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Jonas Paulssonbd654212017-03-21 05:49:40 +00001168 get(LoadOpcode))
1169 .add(MI.getOperand(0))
1170 .addFrameIndex(FrameIndex)
1171 .addImm(0)
1172 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001173 }
1174 }
1175
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001176 // Look for cases where the source of a simple store or the destination
1177 // of a simple load is being spilled. Try to use MVC instead.
1178 //
1179 // Although MVC is in practice a fast choice in these cases, it is still
1180 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +00001181 // load or store is volatile. We also wouldn't be able to use MVC if
1182 // the two memories partially overlap, but that case cannot occur here,
1183 // because we know that one of the memories is a full frame index.
1184 //
1185 // For performance reasons, we also want to avoid using MVC if the addresses
1186 // might be equal. We don't worry about that case here, because spill slot
1187 // coloring happens later, and because we have special code to remove
1188 // MVCs that turn out to be redundant.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001189 if (OpNum == 0 && MI.hasOneMemOperand()) {
1190 MachineMemOperand *MMO = *MI.memoperands_begin();
Philip Reames33d7e492019-02-24 00:45:09 +00001191 if (MMO->getSize() == Size && !MMO->isVolatile() && !MMO->isAtomic()) {
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001192 // Handle conversion of loads.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001193 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) {
1194 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001195 get(SystemZ::MVC))
1196 .addFrameIndex(FrameIndex)
1197 .addImm(0)
1198 .addImm(Size)
Diana Picus116bbab2017-01-13 09:58:52 +00001199 .add(MI.getOperand(1))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001200 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +00001201 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001202 }
1203 // Handle conversion of stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001204 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) {
1205 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001206 get(SystemZ::MVC))
Diana Picus116bbab2017-01-13 09:58:52 +00001207 .add(MI.getOperand(1))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001208 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +00001209 .addImm(Size)
1210 .addFrameIndex(FrameIndex)
1211 .addImm(0)
1212 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001213 }
1214 }
1215 }
1216
Richard Sandiforded1fab62013-07-03 10:10:02 +00001217 // If the spilled operand is the final one, try to change <INSN>R
1218 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001219 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001220 if (MemOpcode >= 0) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001221 unsigned NumOps = MI.getNumExplicitOperands();
Richard Sandiforded1fab62013-07-03 10:10:02 +00001222 if (OpNum == NumOps - 1) {
1223 const MCInstrDesc &MemDesc = get(MemOpcode);
1224 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
1225 assert(AccessBytes != 0 && "Size of access should be known");
1226 assert(AccessBytes <= Size && "Access outside the frame index");
1227 uint64_t Offset = Size - AccessBytes;
Keno Fischere70b31f2015-06-08 20:09:58 +00001228 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001229 MI.getDebugLoc(), get(MemOpcode));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001230 for (unsigned I = 0; I < OpNum; ++I)
Diana Picus116bbab2017-01-13 09:58:52 +00001231 MIB.add(MI.getOperand(I));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001232 MIB.addFrameIndex(FrameIndex).addImm(Offset);
1233 if (MemDesc.TSFlags & SystemZII::HasIndex)
1234 MIB.addReg(0);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001235 transferDeadCC(&MI, MIB);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001236 return MIB;
1237 }
1238 }
1239
Craig Topper062a2ba2014-04-25 05:30:21 +00001240 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001241}
1242
Keno Fischere70b31f2015-06-08 20:09:58 +00001243MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001244 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
1245 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001246 LiveIntervals *LIS) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001247 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001248}
1249
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001250bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1251 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001252 case SystemZ::L128:
1253 splitMove(MI, SystemZ::LG);
1254 return true;
1255
1256 case SystemZ::ST128:
1257 splitMove(MI, SystemZ::STG);
1258 return true;
1259
1260 case SystemZ::LX:
1261 splitMove(MI, SystemZ::LD);
1262 return true;
1263
1264 case SystemZ::STX:
1265 splitMove(MI, SystemZ::STD);
1266 return true;
1267
Richard Sandiford89e160d2013-10-01 12:11:47 +00001268 case SystemZ::LBMux:
1269 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
1270 return true;
1271
1272 case SystemZ::LHMux:
1273 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
1274 return true;
1275
Richard Sandiford21235a22013-10-01 12:49:07 +00001276 case SystemZ::LLCRMux:
1277 expandZExtPseudo(MI, SystemZ::LLCR, 8);
1278 return true;
1279
1280 case SystemZ::LLHRMux:
1281 expandZExtPseudo(MI, SystemZ::LLHR, 16);
1282 return true;
1283
Richard Sandiford0d46b1a2013-10-01 12:19:08 +00001284 case SystemZ::LLCMux:
1285 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
1286 return true;
1287
1288 case SystemZ::LLHMux:
1289 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
1290 return true;
1291
Richard Sandiford0755c932013-10-01 11:26:28 +00001292 case SystemZ::LMux:
1293 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
1294 return true;
1295
Ulrich Weigand524f2762016-11-28 13:34:08 +00001296 case SystemZ::LOCMux:
1297 expandLOCPseudo(MI, SystemZ::LOC, SystemZ::LOCFH);
1298 return true;
1299
1300 case SystemZ::LOCHIMux:
1301 expandLOCPseudo(MI, SystemZ::LOCHI, SystemZ::LOCHHI);
1302 return true;
1303
1304 case SystemZ::LOCRMux:
1305 expandLOCRPseudo(MI, SystemZ::LOCR, SystemZ::LOCFHR);
1306 return true;
1307
Richard Sandiford5469c392013-10-01 12:22:49 +00001308 case SystemZ::STCMux:
1309 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
1310 return true;
1311
1312 case SystemZ::STHMux:
1313 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
1314 return true;
1315
Richard Sandiford0755c932013-10-01 11:26:28 +00001316 case SystemZ::STMux:
1317 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
1318 return true;
1319
Ulrich Weigand524f2762016-11-28 13:34:08 +00001320 case SystemZ::STOCMux:
1321 expandLOCPseudo(MI, SystemZ::STOC, SystemZ::STOCFH);
1322 return true;
1323
Richard Sandiford01240232013-10-01 13:02:28 +00001324 case SystemZ::LHIMux:
1325 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
1326 return true;
1327
1328 case SystemZ::IIFMux:
1329 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
1330 return true;
1331
Richard Sandiford1a569312013-10-01 13:18:56 +00001332 case SystemZ::IILMux:
1333 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
1334 return true;
1335
1336 case SystemZ::IIHMux:
1337 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
1338 return true;
1339
Richard Sandiford70284282013-10-01 14:20:41 +00001340 case SystemZ::NIFMux:
1341 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1342 return true;
1343
1344 case SystemZ::NILMux:
1345 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1346 return true;
1347
1348 case SystemZ::NIHMux:
1349 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1350 return true;
1351
Richard Sandiford6e96ac62013-10-01 13:22:41 +00001352 case SystemZ::OIFMux:
1353 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1354 return true;
1355
1356 case SystemZ::OILMux:
1357 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1358 return true;
1359
1360 case SystemZ::OIHMux:
1361 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1362 return true;
1363
Richard Sandiford5718dac2013-10-01 14:08:44 +00001364 case SystemZ::XIFMux:
1365 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1366 return true;
1367
Richard Sandiford2cac7632013-10-01 14:41:52 +00001368 case SystemZ::TMLMux:
1369 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1370 return true;
1371
1372 case SystemZ::TMHMux:
1373 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1374 return true;
1375
Richard Sandiford42a694f2013-10-01 14:53:46 +00001376 case SystemZ::AHIMux:
1377 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1378 return true;
1379
1380 case SystemZ::AHIMuxK:
1381 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1382 return true;
1383
1384 case SystemZ::AFIMux:
1385 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1386 return true;
1387
Ulrich Weigand75839912016-11-28 13:40:08 +00001388 case SystemZ::CHIMux:
1389 expandRIPseudo(MI, SystemZ::CHI, SystemZ::CIH, false);
1390 return true;
1391
Richard Sandiforda9ac0e02013-10-01 14:56:23 +00001392 case SystemZ::CFIMux:
1393 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1394 return true;
1395
1396 case SystemZ::CLFIMux:
1397 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1398 return true;
1399
Richard Sandifordb63e3002013-10-01 15:00:44 +00001400 case SystemZ::CMux:
1401 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1402 return true;
1403
1404 case SystemZ::CLMux:
1405 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1406 return true;
1407
Richard Sandiford70284282013-10-01 14:20:41 +00001408 case SystemZ::RISBMux: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001409 bool DestIsHigh = isHighReg(MI.getOperand(0).getReg());
1410 bool SrcIsHigh = isHighReg(MI.getOperand(2).getReg());
Richard Sandiford70284282013-10-01 14:20:41 +00001411 if (SrcIsHigh == DestIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001412 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
Richard Sandiford70284282013-10-01 14:20:41 +00001413 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001414 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1415 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32);
Richard Sandiford70284282013-10-01 14:20:41 +00001416 }
1417 return true;
1418 }
1419
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001420 case SystemZ::ADJDYNALLOC:
1421 splitAdjDynAlloc(MI);
1422 return true;
1423
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001424 case TargetOpcode::LOAD_STACK_GUARD:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001425 expandLoadStackGuard(&MI);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001426 return true;
1427
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001428 default:
1429 return false;
1430 }
1431}
1432
Sjoerd Meijer0eb96ed2016-07-29 08:16:16 +00001433unsigned SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Craig Topperc45e39b2019-02-04 21:24:13 +00001434 if (MI.isInlineAsm()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001435 const MachineFunction *MF = MI.getParent()->getParent();
1436 const char *AsmStr = MI.getOperand(0).getSymbolName();
Richard Sandiford312425f2013-05-20 14:23:08 +00001437 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1438 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001439 return MI.getDesc().getSize();
Richard Sandiford312425f2013-05-20 14:23:08 +00001440}
1441
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001442SystemZII::Branch
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001443SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const {
1444 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001445 case SystemZ::BR:
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00001446 case SystemZ::BI:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001447 case SystemZ::J:
1448 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001449 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001450 SystemZ::CCMASK_ANY, &MI.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001451
1452 case SystemZ::BRC:
1453 case SystemZ::BRCL:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001454 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(),
1455 MI.getOperand(1).getImm(), &MI.getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001456
Richard Sandifordc2121252013-08-05 11:23:46 +00001457 case SystemZ::BRCT:
Ulrich Weigand75839912016-11-28 13:40:08 +00001458 case SystemZ::BRCTH:
Richard Sandifordc2121252013-08-05 11:23:46 +00001459 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001460 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001461
1462 case SystemZ::BRCTG:
1463 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001464 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001465
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001466 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001467 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001468 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001469 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001470
Richard Sandiford93183ee2013-09-18 09:56:40 +00001471 case SystemZ::CLIJ:
1472 case SystemZ::CLRJ:
1473 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001474 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001475
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001476 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001477 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001478 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001479 MI.getOperand(2).getImm(), &MI.getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001480
Richard Sandiford93183ee2013-09-18 09:56:40 +00001481 case SystemZ::CLGIJ:
1482 case SystemZ::CLGRJ:
1483 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001484 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001485
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001486 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001487 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001488 }
1489}
1490
1491void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1492 unsigned &LoadOpcode,
1493 unsigned &StoreOpcode) const {
1494 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1495 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001496 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001497 } else if (RC == &SystemZ::GRH32BitRegClass) {
1498 LoadOpcode = SystemZ::LFH;
1499 StoreOpcode = SystemZ::STFH;
1500 } else if (RC == &SystemZ::GRX32BitRegClass) {
1501 LoadOpcode = SystemZ::LMux;
1502 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001503 } else if (RC == &SystemZ::GR64BitRegClass ||
1504 RC == &SystemZ::ADDR64BitRegClass) {
1505 LoadOpcode = SystemZ::LG;
1506 StoreOpcode = SystemZ::STG;
1507 } else if (RC == &SystemZ::GR128BitRegClass ||
1508 RC == &SystemZ::ADDR128BitRegClass) {
1509 LoadOpcode = SystemZ::L128;
1510 StoreOpcode = SystemZ::ST128;
1511 } else if (RC == &SystemZ::FP32BitRegClass) {
1512 LoadOpcode = SystemZ::LE;
1513 StoreOpcode = SystemZ::STE;
1514 } else if (RC == &SystemZ::FP64BitRegClass) {
1515 LoadOpcode = SystemZ::LD;
1516 StoreOpcode = SystemZ::STD;
1517 } else if (RC == &SystemZ::FP128BitRegClass) {
1518 LoadOpcode = SystemZ::LX;
1519 StoreOpcode = SystemZ::STX;
Ulrich Weigand49506d72015-05-05 19:28:34 +00001520 } else if (RC == &SystemZ::VR32BitRegClass) {
1521 LoadOpcode = SystemZ::VL32;
1522 StoreOpcode = SystemZ::VST32;
1523 } else if (RC == &SystemZ::VR64BitRegClass) {
1524 LoadOpcode = SystemZ::VL64;
1525 StoreOpcode = SystemZ::VST64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001526 } else if (RC == &SystemZ::VF128BitRegClass ||
1527 RC == &SystemZ::VR128BitRegClass) {
1528 LoadOpcode = SystemZ::VL;
1529 StoreOpcode = SystemZ::VST;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001530 } else
1531 llvm_unreachable("Unsupported regclass to load or store");
1532}
1533
1534unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1535 int64_t Offset) const {
1536 const MCInstrDesc &MCID = get(Opcode);
1537 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1538 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1539 // Get the instruction to use for unsigned 12-bit displacements.
1540 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1541 if (Disp12Opcode >= 0)
1542 return Disp12Opcode;
1543
1544 // All address-related instructions can use unsigned 12-bit
1545 // displacements.
1546 return Opcode;
1547 }
1548 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1549 // Get the instruction to use for signed 20-bit displacements.
1550 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1551 if (Disp20Opcode >= 0)
1552 return Disp20Opcode;
1553
1554 // Check whether Opcode allows signed 20-bit displacements.
1555 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1556 return Opcode;
1557 }
1558 return 0;
1559}
1560
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001561unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1562 switch (Opcode) {
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001563 case SystemZ::L: return SystemZ::LT;
1564 case SystemZ::LY: return SystemZ::LT;
1565 case SystemZ::LG: return SystemZ::LTG;
1566 case SystemZ::LGF: return SystemZ::LTGF;
1567 case SystemZ::LR: return SystemZ::LTR;
1568 case SystemZ::LGFR: return SystemZ::LTGFR;
1569 case SystemZ::LGR: return SystemZ::LTGR;
1570 case SystemZ::LER: return SystemZ::LTEBR;
1571 case SystemZ::LDR: return SystemZ::LTDBR;
1572 case SystemZ::LXR: return SystemZ::LTXBR;
Jonas Paulsson12629322015-10-01 18:12:28 +00001573 case SystemZ::LCDFR: return SystemZ::LCDBR;
1574 case SystemZ::LPDFR: return SystemZ::LPDBR;
1575 case SystemZ::LNDFR: return SystemZ::LNDBR;
1576 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1577 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1578 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001579 // On zEC12 we prefer to use RISBGN. But if there is a chance to
1580 // actually use the condition code, we may turn it back into RISGB.
1581 // Note that RISBG is not really a "load-and-test" instruction,
1582 // but sets the same condition code values, so is OK to use here.
1583 case SystemZ::RISBGN: return SystemZ::RISBG;
1584 default: return 0;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001585 }
1586}
1587
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001588// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1589// have already been filtered out. Store the first set bit in LSB and
1590// the number of set bits in Length if so.
1591static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1592 unsigned First = findFirstSet(Mask);
1593 uint64_t Top = (Mask >> First) + 1;
1594 if ((Top & -Top) == Top) {
1595 LSB = First;
1596 Length = findFirstSet(Top);
1597 return true;
1598 }
1599 return false;
1600}
1601
1602bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1603 unsigned &Start, unsigned &End) const {
1604 // Reject trivial all-zero masks.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001605 Mask &= allOnes(BitSize);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001606 if (Mask == 0)
1607 return false;
1608
1609 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1610 // the msb and End specifies the index of the lsb.
1611 unsigned LSB, Length;
1612 if (isStringOfOnes(Mask, LSB, Length)) {
1613 Start = 63 - (LSB + Length - 1);
1614 End = 63 - LSB;
1615 return true;
1616 }
1617
1618 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1619 // of the low 1s and End specifies the lsb of the high 1s.
1620 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1621 assert(LSB > 0 && "Bottom bit must be set");
1622 assert(LSB + Length < BitSize && "Top bit must be set");
1623 Start = 63 - (LSB - 1);
1624 End = 63 - (LSB + Length);
1625 return true;
1626 }
1627
1628 return false;
1629}
1630
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001631unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode,
1632 SystemZII::FusedCompareType Type,
1633 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001634 switch (Opcode) {
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001635 case SystemZ::CHI:
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001636 case SystemZ::CGHI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001637 if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
1638 return 0;
1639 break;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001640 case SystemZ::CLFI:
Richard Sandiford93183ee2013-09-18 09:56:40 +00001641 case SystemZ::CLGFI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001642 if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
1643 return 0;
Ulrich Weiganda0e73252016-11-11 12:48:26 +00001644 break;
1645 case SystemZ::CL:
1646 case SystemZ::CLG:
1647 if (!STI.hasMiscellaneousExtensions())
1648 return 0;
1649 if (!(MI && MI->getOperand(3).getReg() == 0))
1650 return 0;
1651 break;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001652 }
1653 switch (Type) {
1654 case SystemZII::CompareAndBranch:
1655 switch (Opcode) {
1656 case SystemZ::CR:
1657 return SystemZ::CRJ;
1658 case SystemZ::CGR:
1659 return SystemZ::CGRJ;
1660 case SystemZ::CHI:
1661 return SystemZ::CIJ;
1662 case SystemZ::CGHI:
1663 return SystemZ::CGIJ;
1664 case SystemZ::CLR:
1665 return SystemZ::CLRJ;
1666 case SystemZ::CLGR:
1667 return SystemZ::CLGRJ;
1668 case SystemZ::CLFI:
1669 return SystemZ::CLIJ;
1670 case SystemZ::CLGFI:
1671 return SystemZ::CLGIJ;
1672 default:
1673 return 0;
1674 }
1675 case SystemZII::CompareAndReturn:
1676 switch (Opcode) {
1677 case SystemZ::CR:
1678 return SystemZ::CRBReturn;
1679 case SystemZ::CGR:
1680 return SystemZ::CGRBReturn;
1681 case SystemZ::CHI:
1682 return SystemZ::CIBReturn;
1683 case SystemZ::CGHI:
1684 return SystemZ::CGIBReturn;
1685 case SystemZ::CLR:
1686 return SystemZ::CLRBReturn;
1687 case SystemZ::CLGR:
1688 return SystemZ::CLGRBReturn;
1689 case SystemZ::CLFI:
1690 return SystemZ::CLIBReturn;
1691 case SystemZ::CLGFI:
1692 return SystemZ::CLGIBReturn;
1693 default:
1694 return 0;
1695 }
Ulrich Weigand848a5132016-04-11 12:12:32 +00001696 case SystemZII::CompareAndSibcall:
1697 switch (Opcode) {
1698 case SystemZ::CR:
1699 return SystemZ::CRBCall;
1700 case SystemZ::CGR:
1701 return SystemZ::CGRBCall;
1702 case SystemZ::CHI:
1703 return SystemZ::CIBCall;
1704 case SystemZ::CGHI:
1705 return SystemZ::CGIBCall;
1706 case SystemZ::CLR:
1707 return SystemZ::CLRBCall;
1708 case SystemZ::CLGR:
1709 return SystemZ::CLGRBCall;
1710 case SystemZ::CLFI:
1711 return SystemZ::CLIBCall;
1712 case SystemZ::CLGFI:
1713 return SystemZ::CLGIBCall;
1714 default:
1715 return 0;
1716 }
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001717 case SystemZII::CompareAndTrap:
1718 switch (Opcode) {
1719 case SystemZ::CR:
1720 return SystemZ::CRT;
1721 case SystemZ::CGR:
1722 return SystemZ::CGRT;
1723 case SystemZ::CHI:
1724 return SystemZ::CIT;
1725 case SystemZ::CGHI:
1726 return SystemZ::CGIT;
1727 case SystemZ::CLR:
1728 return SystemZ::CLRT;
1729 case SystemZ::CLGR:
1730 return SystemZ::CLGRT;
1731 case SystemZ::CLFI:
1732 return SystemZ::CLFIT;
1733 case SystemZ::CLGFI:
1734 return SystemZ::CLGIT;
Ulrich Weiganda0e73252016-11-11 12:48:26 +00001735 case SystemZ::CL:
1736 return SystemZ::CLT;
1737 case SystemZ::CLG:
1738 return SystemZ::CLGT;
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001739 default:
1740 return 0;
1741 }
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001742 }
Ulrich Weigand79391ee2016-04-07 16:33:25 +00001743 return 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001744}
1745
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +00001746unsigned SystemZInstrInfo::getLoadAndTrap(unsigned Opcode) const {
1747 if (!STI.hasLoadAndTrap())
1748 return 0;
1749 switch (Opcode) {
1750 case SystemZ::L:
1751 case SystemZ::LY:
1752 return SystemZ::LAT;
1753 case SystemZ::LG:
1754 return SystemZ::LGAT;
1755 case SystemZ::LFH:
1756 return SystemZ::LFHAT;
1757 case SystemZ::LLGF:
1758 return SystemZ::LLGFAT;
1759 case SystemZ::LLGT:
1760 return SystemZ::LLGTAT;
1761 }
1762 return 0;
1763}
1764
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001765void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1766 MachineBasicBlock::iterator MBBI,
1767 unsigned Reg, uint64_t Value) const {
1768 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1769 unsigned Opcode;
1770 if (isInt<16>(Value))
1771 Opcode = SystemZ::LGHI;
1772 else if (SystemZ::isImmLL(Value))
1773 Opcode = SystemZ::LLILL;
1774 else if (SystemZ::isImmLH(Value)) {
1775 Opcode = SystemZ::LLILH;
1776 Value >>= 16;
1777 } else {
1778 assert(isInt<32>(Value) && "Huge values not handled yet");
1779 Opcode = SystemZ::LGFI;
1780 }
1781 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1782}
Jonas Paulsson8010b632016-10-20 08:27:16 +00001783
1784bool SystemZInstrInfo::
1785areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb,
1786 AliasAnalysis *AA) const {
1787
1788 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand())
1789 return false;
1790
1791 // If mem-operands show that the same address Value is used by both
1792 // instructions, check for non-overlapping offsets and widths. Not
1793 // sure if a register based analysis would be an improvement...
1794
1795 MachineMemOperand *MMOa = *MIa.memoperands_begin();
1796 MachineMemOperand *MMOb = *MIb.memoperands_begin();
1797 const Value *VALa = MMOa->getValue();
1798 const Value *VALb = MMOb->getValue();
1799 bool SameVal = (VALa && VALb && (VALa == VALb));
1800 if (!SameVal) {
1801 const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
1802 const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
1803 if (PSVa && PSVb && (PSVa == PSVb))
1804 SameVal = true;
1805 }
1806 if (SameVal) {
1807 int OffsetA = MMOa->getOffset(), OffsetB = MMOb->getOffset();
1808 int WidthA = MMOa->getSize(), WidthB = MMOb->getSize();
1809 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
1810 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
1811 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
1812 if (LowOffset + LowWidth <= HighOffset)
1813 return true;
1814 }
1815
1816 return false;
1817}