blob: 58c4b14af9752b7b78be036b65ccd3bd17d0bed3 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- 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
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 "SystemZInstrInfo.h"
18#include "SystemZSubtarget.h"
19#include "llvm/CodeGen/LiveInterval.h"
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +000020#include "llvm/CodeGen/LiveIntervalAnalysis.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"
30#include "llvm/MC/MCInstrDesc.h"
31#include "llvm/MC/MCRegisterInfo.h"
32#include "llvm/Support/BranchProbability.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/MathExtras.h"
35#include "llvm/Target/TargetInstrInfo.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetSubtargetInfo.h"
38#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
Richard Sandiford6a06ba32013-07-31 11:36:35 +000048// Return a mask with Count low bits set.
49static uint64_t allOnes(unsigned int Count) {
50 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
51}
52
Richard Sandiford0755c932013-10-01 11:26:28 +000053// Reg should be a 32-bit GPR. Return true if it is a high register rather
54// than a low register.
55static bool isHighReg(unsigned int Reg) {
56 if (SystemZ::GRH32BitRegClass.contains(Reg))
57 return true;
58 assert(SystemZ::GR32BitRegClass.contains(Reg) && "Invalid GRX32");
59 return false;
60}
61
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000062// Pin the vtable to this file.
63void SystemZInstrInfo::anchor() {}
64
Eric Christopher673b3af2014-06-27 07:01:17 +000065SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti)
Ulrich Weigand5f613df2013-05-06 16:15:19 +000066 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
Eric Christopher673b3af2014-06-27 07:01:17 +000067 RI(), STI(sti) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000068}
69
70// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
71// each having the opcode given by NewOpcode.
72void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
73 unsigned NewOpcode) const {
74 MachineBasicBlock *MBB = MI->getParent();
75 MachineFunction &MF = *MBB->getParent();
76
77 // Get two load or store instructions. Use the original instruction for one
Alp Tokercb402912014-01-24 17:20:08 +000078 // of them (arbitrarily the second here) and create a clone for the other.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000079 MachineInstr *EarlierMI = MF.CloneMachineInstr(&*MI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000080 MBB->insert(MI, EarlierMI);
81
Jonas Paulsson8a7bd242017-03-17 06:47:08 +000082 // Set up the two 64-bit registers and remember super reg and its flags.
Ulrich Weigand5f613df2013-05-06 16:15:19 +000083 MachineOperand &HighRegOp = EarlierMI->getOperand(0);
84 MachineOperand &LowRegOp = MI->getOperand(0);
Jonas Paulsson8a7bd242017-03-17 06:47:08 +000085 unsigned Reg128 = LowRegOp.getReg();
86 unsigned Reg128Killed = getKillRegState(LowRegOp.isKill());
87 unsigned Reg128Undef = getUndefRegState(LowRegOp.isUndef());
Richard Sandiford87a44362013-09-30 10:28:35 +000088 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64));
89 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64));
Ulrich Weigand5f613df2013-05-06 16:15:19 +000090
Jonas Paulsson8a7bd242017-03-17 06:47:08 +000091 if (MI->mayStore()) {
92 // Add implicit uses of the super register in case one of the subregs is
93 // undefined. We could track liveness and skip storing an undefined
94 // subreg, but this is hopefully rare (discovered with llvm-stress).
95 // If Reg128 was killed, set kill flag on MI.
96 unsigned Reg128UndefImpl = (Reg128Undef | RegState::Implicit);
97 MachineInstrBuilder(MF, EarlierMI).addReg(Reg128, Reg128UndefImpl);
98 MachineInstrBuilder(MF, MI).addReg(Reg128, (Reg128UndefImpl | Reg128Killed));
99 }
100
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000101 // The address in the first (high) instruction is already correct.
102 // Adjust the offset in the second (low) instruction.
103 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
104 MachineOperand &LowOffsetOp = MI->getOperand(2);
105 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
106
Jonas Paulsson1e864852017-04-24 12:40:28 +0000107 // Clear the kill flags on the registers in the first instruction.
108 if (EarlierMI->getOperand(0).isReg() && EarlierMI->getOperand(0).isUse())
109 EarlierMI->getOperand(0).setIsKill(false);
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000110 EarlierMI->getOperand(1).setIsKill(false);
Jonas Paulsson7da38202015-10-26 15:03:41 +0000111 EarlierMI->getOperand(3).setIsKill(false);
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000112
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000113 // Set the opcodes.
114 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
115 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
116 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
117
118 EarlierMI->setDesc(get(HighOpcode));
119 MI->setDesc(get(LowOpcode));
120}
121
122// Split ADJDYNALLOC instruction MI.
123void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
124 MachineBasicBlock *MBB = MI->getParent();
125 MachineFunction &MF = *MBB->getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000126 MachineFrameInfo &MFFrame = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000127 MachineOperand &OffsetMO = MI->getOperand(2);
128
Matthias Braun941a7052016-07-28 18:40:00 +0000129 uint64_t Offset = (MFFrame.getMaxCallFrameSize() +
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000130 SystemZMC::CallFrameSize +
131 OffsetMO.getImm());
132 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
133 assert(NewOpcode && "No support for huge argument lists yet");
134 MI->setDesc(get(NewOpcode));
135 OffsetMO.setImm(Offset);
136}
137
Richard Sandiford01240232013-10-01 13:02:28 +0000138// MI is an RI-style pseudo instruction. Replace it with LowOpcode
139// if the first operand is a low GR32 and HighOpcode if the first operand
140// is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
141// and HighOpcode takes an unsigned 32-bit operand. In those cases,
142// MI has the same kind of operand as LowOpcode, so needs to be converted
143// if HighOpcode is used.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000144void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford01240232013-10-01 13:02:28 +0000145 unsigned HighOpcode,
146 bool ConvertHigh) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000147 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford01240232013-10-01 13:02:28 +0000148 bool IsHigh = isHighReg(Reg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000149 MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode));
Richard Sandiford01240232013-10-01 13:02:28 +0000150 if (IsHigh && ConvertHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000151 MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm()));
Richard Sandiford01240232013-10-01 13:02:28 +0000152}
153
Richard Sandiford42a694f2013-10-01 14:53:46 +0000154// MI is a three-operand RIE-style pseudo instruction. Replace it with
Jonas Paulsson18d877f2015-10-09 07:19:16 +0000155// LowOpcodeK if the registers are both low GR32s, otherwise use a move
Richard Sandiford42a694f2013-10-01 14:53:46 +0000156// followed by HighOpcode or LowOpcode, depending on whether the target
157// is a high or low GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000158void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford42a694f2013-10-01 14:53:46 +0000159 unsigned LowOpcodeK,
160 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000161 unsigned DestReg = MI.getOperand(0).getReg();
162 unsigned SrcReg = MI.getOperand(1).getReg();
Richard Sandiford42a694f2013-10-01 14:53:46 +0000163 bool DestIsHigh = isHighReg(DestReg);
164 bool SrcIsHigh = isHighReg(SrcReg);
165 if (!DestIsHigh && !SrcIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000166 MI.setDesc(get(LowOpcodeK));
Richard Sandiford42a694f2013-10-01 14:53:46 +0000167 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000168 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg,
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000169 SystemZ::LR, 32, MI.getOperand(1).isKill(),
170 MI.getOperand(1).isUndef());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000171 MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
172 MI.getOperand(1).setReg(DestReg);
173 MI.tieOperands(0, 1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000174 }
175}
176
Richard Sandiford0755c932013-10-01 11:26:28 +0000177// MI is an RXY-style pseudo instruction. Replace it with LowOpcode
178// if the first operand is a low GR32 and HighOpcode if the first operand
179// is a high GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000180void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford0755c932013-10-01 11:26:28 +0000181 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000182 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000183 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000184 MI.getOperand(2).getImm());
185 MI.setDesc(get(Opcode));
Richard Sandiford0755c932013-10-01 11:26:28 +0000186}
187
Ulrich Weigand524f2762016-11-28 13:34:08 +0000188// MI is a load-on-condition pseudo instruction with a single register
189// (source or destination) operand. Replace it with LowOpcode if the
190// register is a low GR32 and HighOpcode if the register is a high GR32.
191void SystemZInstrInfo::expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode,
192 unsigned HighOpcode) const {
193 unsigned Reg = MI.getOperand(0).getReg();
194 unsigned Opcode = isHighReg(Reg) ? HighOpcode : LowOpcode;
195 MI.setDesc(get(Opcode));
196}
197
198// MI is a load-register-on-condition pseudo instruction. Replace it with
199// LowOpcode if source and destination are both low GR32s and HighOpcode if
200// source and destination are both high GR32s.
201void SystemZInstrInfo::expandLOCRPseudo(MachineInstr &MI, unsigned LowOpcode,
202 unsigned HighOpcode) const {
203 unsigned DestReg = MI.getOperand(0).getReg();
204 unsigned SrcReg = MI.getOperand(2).getReg();
205 bool DestIsHigh = isHighReg(DestReg);
206 bool SrcIsHigh = isHighReg(SrcReg);
207
208 if (!DestIsHigh && !SrcIsHigh)
209 MI.setDesc(get(LowOpcode));
210 else if (DestIsHigh && SrcIsHigh)
211 MI.setDesc(get(HighOpcode));
212
213 // If we were unable to implement the pseudo with a single instruction, we
214 // need to convert it back into a branch sequence. This cannot be done here
215 // since the caller of expandPostRAPseudo does not handle changes to the CFG
216 // correctly. This change is defered to the SystemZExpandPseudo pass.
217}
218
Richard Sandiford21235a22013-10-01 12:49:07 +0000219// MI is an RR-style pseudo instruction that zero-extends the low Size bits
220// of one GRX32 into another. Replace it with LowOpcode if both operands
221// are low registers, otherwise use RISB[LH]G.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000222void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford21235a22013-10-01 12:49:07 +0000223 unsigned Size) const {
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000224 MachineInstrBuilder MIB =
225 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(),
226 MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode,
227 Size, MI.getOperand(1).isKill(), MI.getOperand(1).isUndef());
228
229 // Keep the remaining operands as-is.
230 for (unsigned I = 2; I < MI.getNumOperands(); ++I)
231 MIB.add(MI.getOperand(I));
232
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000233 MI.eraseFromParent();
Richard Sandiford21235a22013-10-01 12:49:07 +0000234}
235
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000236void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const {
237 MachineBasicBlock *MBB = MI->getParent();
238 MachineFunction &MF = *MBB->getParent();
239 const unsigned Reg = MI->getOperand(0).getReg();
240
241 // Conveniently, all 4 instructions are cloned from LOAD_STACK_GUARD,
242 // so they already have operand 0 set to reg.
243
244 // ear <reg>, %a0
245 MachineInstr *Ear1MI = MF.CloneMachineInstr(MI);
246 MBB->insert(MI, Ear1MI);
247 Ear1MI->setDesc(get(SystemZ::EAR));
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000248 MachineInstrBuilder(MF, Ear1MI).addReg(SystemZ::A0);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000249
250 // sllg <reg>, <reg>, 32
251 MachineInstr *SllgMI = MF.CloneMachineInstr(MI);
252 MBB->insert(MI, SllgMI);
253 SllgMI->setDesc(get(SystemZ::SLLG));
254 MachineInstrBuilder(MF, SllgMI).addReg(Reg).addReg(0).addImm(32);
255
256 // ear <reg>, %a1
257 MachineInstr *Ear2MI = MF.CloneMachineInstr(MI);
258 MBB->insert(MI, Ear2MI);
259 Ear2MI->setDesc(get(SystemZ::EAR));
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000260 MachineInstrBuilder(MF, Ear2MI).addReg(SystemZ::A1);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000261
262 // lg <reg>, 40(<reg>)
263 MI->setDesc(get(SystemZ::LG));
264 MachineInstrBuilder(MF, MI).addReg(Reg).addImm(40).addReg(0);
265}
266
Richard Sandiford0755c932013-10-01 11:26:28 +0000267// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
268// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
269// are low registers, otherwise use RISB[LH]G. Size is the number of bits
270// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
271// KillSrc is true if this move is the last use of SrcReg.
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000272MachineInstrBuilder
273SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
274 MachineBasicBlock::iterator MBBI,
275 const DebugLoc &DL, unsigned DestReg,
276 unsigned SrcReg, unsigned LowLowOpcode,
277 unsigned Size, bool KillSrc,
278 bool UndefSrc) const {
Richard Sandiford0755c932013-10-01 11:26:28 +0000279 unsigned Opcode;
280 bool DestIsHigh = isHighReg(DestReg);
281 bool SrcIsHigh = isHighReg(SrcReg);
282 if (DestIsHigh && SrcIsHigh)
283 Opcode = SystemZ::RISBHH;
284 else if (DestIsHigh && !SrcIsHigh)
285 Opcode = SystemZ::RISBHL;
286 else if (!DestIsHigh && SrcIsHigh)
287 Opcode = SystemZ::RISBLH;
288 else {
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000289 return BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000290 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc));
Richard Sandiford0755c932013-10-01 11:26:28 +0000291 }
292 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000293 return BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
Richard Sandiford0755c932013-10-01 11:26:28 +0000294 .addReg(DestReg, RegState::Undef)
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000295 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc))
Richard Sandiford0755c932013-10-01 11:26:28 +0000296 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
297}
298
Ulrich Weigand524f2762016-11-28 13:34:08 +0000299MachineInstr *SystemZInstrInfo::commuteInstructionImpl(MachineInstr &MI,
300 bool NewMI,
301 unsigned OpIdx1,
302 unsigned OpIdx2) const {
303 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & {
304 if (NewMI)
305 return *MI.getParent()->getParent()->CloneMachineInstr(&MI);
306 return MI;
307 };
308
309 switch (MI.getOpcode()) {
310 case SystemZ::LOCRMux:
311 case SystemZ::LOCFHR:
312 case SystemZ::LOCR:
313 case SystemZ::LOCGR: {
314 auto &WorkingMI = cloneIfNew(MI);
315 // Invert condition.
316 unsigned CCValid = WorkingMI.getOperand(3).getImm();
317 unsigned CCMask = WorkingMI.getOperand(4).getImm();
318 WorkingMI.getOperand(4).setImm(CCMask ^ CCValid);
319 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
320 OpIdx1, OpIdx2);
321 }
322 default:
323 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
324 }
325}
326
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000327// If MI is a simple load or store for a frame object, return the register
328// it loads or stores and set FrameIndex to the index of the frame object.
329// Return 0 otherwise.
330//
331// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000332static int isSimpleMove(const MachineInstr &MI, int &FrameIndex,
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000333 unsigned Flag) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000334 const MCInstrDesc &MCID = MI.getDesc();
335 if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() &&
336 MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) {
337 FrameIndex = MI.getOperand(1).getIndex();
338 return MI.getOperand(0).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000339 }
340 return 0;
341}
342
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000343unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000344 int &FrameIndex) const {
345 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
346}
347
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000348unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000349 int &FrameIndex) const {
350 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
351}
352
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000353bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI,
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000354 int &DestFrameIndex,
355 int &SrcFrameIndex) const {
356 // Check for MVC 0(Length,FI1),0(FI2)
Matthias Braun941a7052016-07-28 18:40:00 +0000357 const MachineFrameInfo &MFI = MI.getParent()->getParent()->getFrameInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000358 if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() ||
359 MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() ||
360 MI.getOperand(4).getImm() != 0)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000361 return false;
362
363 // Check that Length covers the full slots.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000364 int64_t Length = MI.getOperand(2).getImm();
365 unsigned FI1 = MI.getOperand(0).getIndex();
366 unsigned FI2 = MI.getOperand(3).getIndex();
Matthias Braun941a7052016-07-28 18:40:00 +0000367 if (MFI.getObjectSize(FI1) != Length ||
368 MFI.getObjectSize(FI2) != Length)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000369 return false;
370
371 DestFrameIndex = FI1;
372 SrcFrameIndex = FI2;
373 return true;
374}
375
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000376bool SystemZInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000377 MachineBasicBlock *&TBB,
378 MachineBasicBlock *&FBB,
379 SmallVectorImpl<MachineOperand> &Cond,
380 bool AllowModify) const {
381 // Most of the code and comments here are boilerplate.
382
383 // Start from the bottom of the block and work up, examining the
384 // terminator instructions.
385 MachineBasicBlock::iterator I = MBB.end();
386 while (I != MBB.begin()) {
387 --I;
388 if (I->isDebugValue())
389 continue;
390
391 // Working from the bottom, when we see a non-terminator instruction, we're
392 // done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000393 if (!isUnpredicatedTerminator(*I))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000394 break;
395
396 // A terminator that isn't a branch can't easily be handled by this
397 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000398 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000399 return true;
400
401 // Can't handle indirect branches.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000402 SystemZII::Branch Branch(getBranchInfo(*I));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000403 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000404 return true;
405
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000406 // Punt on compound branches.
407 if (Branch.Type != SystemZII::BranchNormal)
408 return true;
409
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000410 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000411 // Handle unconditional branches.
412 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000413 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000414 continue;
415 }
416
417 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000418 while (std::next(I) != MBB.end())
419 std::next(I)->eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000420
421 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000422 FBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000423
424 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000425 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000426 TBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000427 I->eraseFromParent();
428 I = MBB.end();
429 continue;
430 }
431
432 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000433 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000434 continue;
435 }
436
437 // Working from the bottom, handle the first conditional branch.
438 if (Cond.empty()) {
439 // FIXME: add X86-style branch swap
440 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000441 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000442 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000443 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000444 continue;
445 }
446
447 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000448 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000449
450 // Only handle the case where all conditional branches branch to the same
451 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000452 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000453 return true;
454
455 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000456 unsigned OldCCValid = Cond[0].getImm();
457 unsigned OldCCMask = Cond[1].getImm();
458 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000459 continue;
460
461 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000462 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000463 }
464
465 return false;
466}
467
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000468unsigned SystemZInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000469 int *BytesRemoved) const {
470 assert(!BytesRemoved && "code size not handled");
471
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000472 // Most of the code and comments here are boilerplate.
473 MachineBasicBlock::iterator I = MBB.end();
474 unsigned Count = 0;
475
476 while (I != MBB.begin()) {
477 --I;
478 if (I->isDebugValue())
479 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000480 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000481 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000482 if (!getBranchInfo(*I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000483 break;
484 // Remove the branch.
485 I->eraseFromParent();
486 I = MBB.end();
487 ++Count;
488 }
489
490 return Count;
491}
492
Richard Sandiford3d768e32013-07-31 12:30:20 +0000493bool SystemZInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000494reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000495 assert(Cond.size() == 2 && "Invalid condition");
496 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
497 return false;
498}
499
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000500unsigned SystemZInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000501 MachineBasicBlock *TBB,
502 MachineBasicBlock *FBB,
503 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000504 const DebugLoc &DL,
505 int *BytesAdded) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000506 // In this function we output 32-bit branches, which should always
507 // have enough range. They can be shortened and relaxed by later code
508 // in the pipeline, if desired.
509
510 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000511 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000512 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000513 "SystemZ branch conditions have one component!");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000514 assert(!BytesAdded && "code size not handled");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000515
516 if (Cond.empty()) {
517 // Unconditional branch?
518 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000519 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000520 return 1;
521 }
522
523 // Conditional branch.
524 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000525 unsigned CCValid = Cond[0].getImm();
526 unsigned CCMask = Cond[1].getImm();
527 BuildMI(&MBB, DL, get(SystemZ::BRC))
528 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000529 ++Count;
530
531 if (FBB) {
532 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000533 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000534 ++Count;
535 }
536 return Count;
537}
538
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000539bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
540 unsigned &SrcReg2, int &Mask,
541 int &Value) const {
542 assert(MI.isCompare() && "Caller should have checked for a comparison");
Richard Sandiford564681c2013-08-12 10:28:10 +0000543
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000544 if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() &&
545 MI.getOperand(1).isImm()) {
546 SrcReg = MI.getOperand(0).getReg();
Richard Sandiford564681c2013-08-12 10:28:10 +0000547 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000548 Value = MI.getOperand(1).getImm();
Richard Sandiford564681c2013-08-12 10:28:10 +0000549 Mask = ~0;
550 return true;
551 }
552
553 return false;
554}
555
Richard Sandiforda5901252013-08-16 10:22:54 +0000556// If Reg is a virtual register, return its definition, otherwise return null.
557static MachineInstr *getDef(unsigned Reg,
558 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000559 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +0000560 return nullptr;
Richard Sandiford564681c2013-08-12 10:28:10 +0000561 return MRI->getUniqueVRegDef(Reg);
562}
563
564// Return true if MI is a shift of type Opcode by Imm bits.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000565static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000566 return (MI->getOpcode() == Opcode &&
567 !MI->getOperand(2).getReg() &&
568 MI->getOperand(3).getImm() == Imm);
569}
570
Richard Sandiforda5901252013-08-16 10:22:54 +0000571// If the destination of MI has no uses, delete it as dead.
572static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
573 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
574 MI->eraseFromParent();
575}
576
Richard Sandiford564681c2013-08-12 10:28:10 +0000577// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000578// the result of an IPM sequence whose input CC survives until Compare,
579// and whether Compare is therefore redundant. Delete it and return
580// true if so.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000581static bool removeIPMBasedCompare(MachineInstr &Compare, unsigned SrcReg,
Richard Sandiforda5901252013-08-16 10:22:54 +0000582 const MachineRegisterInfo *MRI,
583 const TargetRegisterInfo *TRI) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000584 MachineInstr *LGFR = nullptr;
Richard Sandiforda5901252013-08-16 10:22:54 +0000585 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000586 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
587 LGFR = RLL;
588 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
589 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000590 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000591 return false;
592
Richard Sandiforda5901252013-08-16 10:22:54 +0000593 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000594 if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
Richard Sandiford564681c2013-08-12 10:28:10 +0000595 return false;
596
Richard Sandiforda5901252013-08-16 10:22:54 +0000597 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000598 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
599 return false;
600
601 // Check that there are no assignments to CC between the IPM and Compare,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000602 if (IPM->getParent() != Compare.getParent())
Richard Sandiford564681c2013-08-12 10:28:10 +0000603 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000604 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare.getIterator();
Richard Sandiford564681c2013-08-12 10:28:10 +0000605 for (++MBBI; MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000606 MachineInstr &MI = *MBBI;
607 if (MI.modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000608 return false;
609 }
610
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000611 Compare.eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000612 if (LGFR)
613 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000614 eraseIfDead(RLL, MRI);
615 eraseIfDead(SRL, MRI);
616 eraseIfDead(IPM, MRI);
617
Richard Sandiford564681c2013-08-12 10:28:10 +0000618 return true;
619}
620
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000621bool SystemZInstrInfo::optimizeCompareInstr(
622 MachineInstr &Compare, unsigned SrcReg, unsigned SrcReg2, int Mask,
623 int Value, const MachineRegisterInfo *MRI) const {
Richard Sandiford564681c2013-08-12 10:28:10 +0000624 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000625 bool IsLogical = (Compare.getDesc().TSFlags & SystemZII::IsLogical) != 0;
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000626 return Value == 0 && !IsLogical &&
627 removeIPMBasedCompare(Compare, SrcReg, MRI, &RI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000628}
629
Ulrich Weigand524f2762016-11-28 13:34:08 +0000630bool SystemZInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
631 ArrayRef<MachineOperand> Pred,
632 unsigned TrueReg, unsigned FalseReg,
633 int &CondCycles, int &TrueCycles,
634 int &FalseCycles) const {
635 // Not all subtargets have LOCR instructions.
636 if (!STI.hasLoadStoreOnCond())
637 return false;
638 if (Pred.size() != 2)
639 return false;
640
641 // Check register classes.
642 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
643 const TargetRegisterClass *RC =
644 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
645 if (!RC)
646 return false;
647
648 // We have LOCR instructions for 32 and 64 bit general purpose registers.
649 if ((STI.hasLoadStoreOnCond2() &&
650 SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) ||
651 SystemZ::GR32BitRegClass.hasSubClassEq(RC) ||
652 SystemZ::GR64BitRegClass.hasSubClassEq(RC)) {
653 CondCycles = 2;
654 TrueCycles = 2;
655 FalseCycles = 2;
656 return true;
Richard Sandifordf2404162013-07-25 09:11:15 +0000657 }
Ulrich Weigand524f2762016-11-28 13:34:08 +0000658
659 // Can't do anything else.
660 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000661}
662
Ulrich Weigand524f2762016-11-28 13:34:08 +0000663void SystemZInstrInfo::insertSelect(MachineBasicBlock &MBB,
664 MachineBasicBlock::iterator I,
665 const DebugLoc &DL, unsigned DstReg,
666 ArrayRef<MachineOperand> Pred,
667 unsigned TrueReg,
668 unsigned FalseReg) const {
669 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
670 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
671
672 assert(Pred.size() == 2 && "Invalid condition");
673 unsigned CCValid = Pred[0].getImm();
674 unsigned CCMask = Pred[1].getImm();
675
676 unsigned Opc;
677 if (SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) {
678 if (STI.hasLoadStoreOnCond2())
679 Opc = SystemZ::LOCRMux;
680 else {
681 Opc = SystemZ::LOCR;
682 MRI.constrainRegClass(DstReg, &SystemZ::GR32BitRegClass);
Jonas Paulssonc7bb22e2017-03-31 14:06:59 +0000683 unsigned TReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
684 unsigned FReg = MRI.createVirtualRegister(&SystemZ::GR32BitRegClass);
685 BuildMI(MBB, I, DL, get(TargetOpcode::COPY), TReg).addReg(TrueReg);
686 BuildMI(MBB, I, DL, get(TargetOpcode::COPY), FReg).addReg(FalseReg);
687 TrueReg = TReg;
688 FalseReg = FReg;
Ulrich Weigand524f2762016-11-28 13:34:08 +0000689 }
690 } else if (SystemZ::GR64BitRegClass.hasSubClassEq(RC))
691 Opc = SystemZ::LOCGR;
692 else
693 llvm_unreachable("Invalid register class");
694
695 BuildMI(MBB, I, DL, get(Opc), DstReg)
696 .addReg(FalseReg).addReg(TrueReg)
697 .addImm(CCValid).addImm(CCMask);
698}
699
700bool SystemZInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
701 unsigned Reg,
702 MachineRegisterInfo *MRI) const {
703 unsigned DefOpc = DefMI.getOpcode();
704 if (DefOpc != SystemZ::LHIMux && DefOpc != SystemZ::LHI &&
705 DefOpc != SystemZ::LGHI)
706 return false;
707 if (DefMI.getOperand(0).getReg() != Reg)
708 return false;
709 int32_t ImmVal = (int32_t)DefMI.getOperand(1).getImm();
710
711 unsigned UseOpc = UseMI.getOpcode();
712 unsigned NewUseOpc;
713 unsigned UseIdx;
714 int CommuteIdx = -1;
715 switch (UseOpc) {
716 case SystemZ::LOCRMux:
717 if (!STI.hasLoadStoreOnCond2())
718 return false;
719 NewUseOpc = SystemZ::LOCHIMux;
720 if (UseMI.getOperand(2).getReg() == Reg)
721 UseIdx = 2;
722 else if (UseMI.getOperand(1).getReg() == Reg)
723 UseIdx = 2, CommuteIdx = 1;
724 else
725 return false;
726 break;
727 case SystemZ::LOCGR:
728 if (!STI.hasLoadStoreOnCond2())
729 return false;
730 NewUseOpc = SystemZ::LOCGHI;
731 if (UseMI.getOperand(2).getReg() == Reg)
732 UseIdx = 2;
733 else if (UseMI.getOperand(1).getReg() == Reg)
734 UseIdx = 2, CommuteIdx = 1;
735 else
736 return false;
737 break;
738 default:
739 return false;
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000740 }
Ulrich Weigand524f2762016-11-28 13:34:08 +0000741
742 if (CommuteIdx != -1)
743 if (!commuteInstruction(UseMI, false, CommuteIdx, UseIdx))
744 return false;
745
746 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
747 UseMI.setDesc(get(NewUseOpc));
748 UseMI.getOperand(UseIdx).ChangeToImmediate(ImmVal);
749 if (DeleteDef)
750 DefMI.eraseFromParent();
751
752 return true;
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000753}
754
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000755bool SystemZInstrInfo::isPredicable(const MachineInstr &MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000756 unsigned Opcode = MI.getOpcode();
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000757 if (Opcode == SystemZ::Return ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000758 Opcode == SystemZ::Trap ||
Ulrich Weigand848a5132016-04-11 12:12:32 +0000759 Opcode == SystemZ::CallJG ||
760 Opcode == SystemZ::CallBR)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000761 return true;
762 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000763}
764
765bool SystemZInstrInfo::
766isProfitableToIfCvt(MachineBasicBlock &MBB,
767 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000768 BranchProbability Probability) const {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000769 // Avoid using conditional returns at the end of a loop (since then
770 // we'd need to emit an unconditional branch to the beginning anyway,
771 // making the loop body longer). This doesn't apply for low-probability
772 // loops (eg. compare-and-swap retry), so just decide based on branch
773 // probability instead of looping structure.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000774 // However, since Compare and Trap instructions cost the same as a regular
775 // Compare instruction, we should allow the if conversion to convert this
776 // into a Conditional Compare regardless of the branch probability.
777 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap &&
778 MBB.succ_empty() && Probability < BranchProbability(1, 8))
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000779 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000780 // For now only convert single instructions.
781 return NumCycles == 1;
782}
783
784bool SystemZInstrInfo::
785isProfitableToIfCvt(MachineBasicBlock &TMBB,
786 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
787 MachineBasicBlock &FMBB,
788 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000789 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000790 // For now avoid converting mutually-exclusive cases.
791 return false;
792}
793
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000794bool SystemZInstrInfo::
795isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
796 BranchProbability Probability) const {
797 // For now only duplicate single instructions.
798 return NumCycles == 1;
799}
800
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000801bool SystemZInstrInfo::PredicateInstruction(
802 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000803 assert(Pred.size() == 2 && "Invalid condition");
804 unsigned CCValid = Pred[0].getImm();
805 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000806 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000807 unsigned Opcode = MI.getOpcode();
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000808 if (Opcode == SystemZ::Trap) {
809 MI.setDesc(get(SystemZ::CondTrap));
810 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
811 .addImm(CCValid).addImm(CCMask)
812 .addReg(SystemZ::CC, RegState::Implicit);
813 return true;
814 }
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000815 if (Opcode == SystemZ::Return) {
816 MI.setDesc(get(SystemZ::CondReturn));
817 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
818 .addImm(CCValid).addImm(CCMask)
819 .addReg(SystemZ::CC, RegState::Implicit);
820 return true;
821 }
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000822 if (Opcode == SystemZ::CallJG) {
Zhan Jun Liaua5d60af2016-07-07 15:34:46 +0000823 MachineOperand FirstOp = MI.getOperand(0);
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000824 const uint32_t *RegMask = MI.getOperand(1).getRegMask();
825 MI.RemoveOperand(1);
826 MI.RemoveOperand(0);
827 MI.setDesc(get(SystemZ::CallBRCL));
828 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Diana Picus116bbab2017-01-13 09:58:52 +0000829 .addImm(CCValid)
830 .addImm(CCMask)
831 .add(FirstOp)
832 .addRegMask(RegMask)
833 .addReg(SystemZ::CC, RegState::Implicit);
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000834 return true;
835 }
Ulrich Weigand848a5132016-04-11 12:12:32 +0000836 if (Opcode == SystemZ::CallBR) {
837 const uint32_t *RegMask = MI.getOperand(0).getRegMask();
838 MI.RemoveOperand(0);
839 MI.setDesc(get(SystemZ::CallBCR));
840 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
841 .addImm(CCValid).addImm(CCMask)
842 .addRegMask(RegMask)
843 .addReg(SystemZ::CC, RegState::Implicit);
844 return true;
845 }
Richard Sandifordf2404162013-07-25 09:11:15 +0000846 return false;
847}
848
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000849void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
850 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000851 const DebugLoc &DL, unsigned DestReg,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000852 unsigned SrcReg, bool KillSrc) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000853 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
854 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000855 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
856 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
857 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
858 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000859 return;
860 }
861
Richard Sandiford0755c932013-10-01 11:26:28 +0000862 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000863 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc,
864 false);
Richard Sandiford0755c932013-10-01 11:26:28 +0000865 return;
866 }
867
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000868 // Everything else needs only one instruction.
869 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000870 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000871 Opcode = SystemZ::LGR;
872 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigandcdce0262016-03-14 13:50:03 +0000873 // For z13 we prefer LDR over LER to avoid partial register dependencies.
874 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000875 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
876 Opcode = SystemZ::LDR;
877 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
878 Opcode = SystemZ::LXR;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000879 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
880 Opcode = SystemZ::VLR32;
881 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
882 Opcode = SystemZ::VLR64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000883 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
884 Opcode = SystemZ::VLR;
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000885 else if (SystemZ::AR32BitRegClass.contains(DestReg, SrcReg))
886 Opcode = SystemZ::CPYA;
887 else if (SystemZ::AR32BitRegClass.contains(DestReg) &&
888 SystemZ::GR32BitRegClass.contains(SrcReg))
889 Opcode = SystemZ::SAR;
890 else if (SystemZ::GR32BitRegClass.contains(DestReg) &&
891 SystemZ::AR32BitRegClass.contains(SrcReg))
892 Opcode = SystemZ::EAR;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000893 else
894 llvm_unreachable("Impossible reg-to-reg copy");
895
896 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
897 .addReg(SrcReg, getKillRegState(KillSrc));
898}
899
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000900void SystemZInstrInfo::storeRegToStackSlot(
901 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
902 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
903 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000904 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
905
906 // Callers may expect a single instruction, so keep 128-bit moves
907 // together for now and lower them after register allocation.
908 unsigned LoadOpcode, StoreOpcode;
909 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
910 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000911 .addReg(SrcReg, getKillRegState(isKill)),
912 FrameIdx);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000913}
914
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000915void SystemZInstrInfo::loadRegFromStackSlot(
916 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
917 int FrameIdx, const TargetRegisterClass *RC,
918 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000919 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
920
921 // Callers may expect a single instruction, so keep 128-bit moves
922 // together for now and lower them after register allocation.
923 unsigned LoadOpcode, StoreOpcode;
924 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
925 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
926 FrameIdx);
927}
928
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000929// Return true if MI is a simple load or store with a 12-bit displacement
930// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
931static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
932 const MCInstrDesc &MCID = MI->getDesc();
933 return ((MCID.TSFlags & Flag) &&
934 isUInt<12>(MI->getOperand(2).getImm()) &&
935 MI->getOperand(3).getReg() == 0);
936}
937
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000938namespace {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000939
Richard Sandifordc2312692014-03-06 10:38:30 +0000940struct LogicOp {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000941 LogicOp() = default;
Richard Sandifordc2312692014-03-06 10:38:30 +0000942 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
943 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000944
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000945 explicit operator bool() const { return RegSize; }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000946
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000947 unsigned RegSize = 0;
948 unsigned ImmLSB = 0;
949 unsigned ImmSize = 0;
Richard Sandifordc2312692014-03-06 10:38:30 +0000950};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000951
Richard Sandifordc2312692014-03-06 10:38:30 +0000952} // end anonymous namespace
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000953
954static LogicOp interpretAndImmediate(unsigned Opcode) {
955 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000956 case SystemZ::NILMux: return LogicOp(32, 0, 16);
957 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000958 case SystemZ::NILL64: return LogicOp(64, 0, 16);
959 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000960 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
961 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
962 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000963 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000964 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000965 default: return LogicOp();
966 }
967}
968
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000969static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) {
970 if (OldMI->registerDefIsDead(SystemZ::CC)) {
971 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC);
972 if (CCDef != nullptr)
973 CCDef->setIsDead(true);
974 }
975}
976
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000977// Used to return from convertToThreeAddress after replacing two-address
978// instruction OldMI with three-address instruction NewMI.
979static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
980 MachineInstr *NewMI,
981 LiveVariables *LV) {
982 if (LV) {
983 unsigned NumOps = OldMI->getNumOperands();
984 for (unsigned I = 1; I < NumOps; ++I) {
985 MachineOperand &Op = OldMI->getOperand(I);
986 if (Op.isReg() && Op.isKill())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000987 LV->replaceKillInstruction(Op.getReg(), *OldMI, *NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000988 }
989 }
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000990 transferDeadCC(OldMI, NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000991 return NewMI;
992}
993
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000994MachineInstr *SystemZInstrInfo::convertToThreeAddress(
995 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
996 MachineBasicBlock *MBB = MI.getParent();
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000997 MachineFunction *MF = MBB->getParent();
998 MachineRegisterInfo &MRI = MF->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000999
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001000 unsigned Opcode = MI.getOpcode();
1001 unsigned NumOps = MI.getNumOperands();
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001002
1003 // Try to convert something like SLL into SLLK, if supported.
1004 // We prefer to keep the two-operand form where possible both
1005 // because it tends to be shorter and because some instructions
1006 // have memory forms that can be used during spilling.
Eric Christopher673b3af2014-06-27 07:01:17 +00001007 if (STI.hasDistinctOps()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001008 MachineOperand &Dest = MI.getOperand(0);
1009 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford42a694f2013-10-01 14:53:46 +00001010 unsigned DestReg = Dest.getReg();
1011 unsigned SrcReg = Src.getReg();
1012 // AHIMux is only really a three-operand instruction when both operands
1013 // are low registers. Try to constrain both operands to be low if
1014 // possible.
1015 if (Opcode == SystemZ::AHIMux &&
1016 TargetRegisterInfo::isVirtualRegister(DestReg) &&
1017 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
1018 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
1019 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
1020 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
1021 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
1022 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001023 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
1024 if (ThreeOperandOpcode >= 0) {
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +00001025 // Create three address instruction without adding the implicit
1026 // operands. Those will instead be copied over from the original
1027 // instruction by the loop below.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001028 MachineInstrBuilder MIB(
1029 *MF, MF->CreateMachineInstr(get(ThreeOperandOpcode), MI.getDebugLoc(),
1030 /*NoImplicit=*/true));
Diana Picus116bbab2017-01-13 09:58:52 +00001031 MIB.add(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001032 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001033 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001034 // Keep the remaining operands as-is.
1035 for (unsigned I = 2; I < NumOps; ++I)
Diana Picus116bbab2017-01-13 09:58:52 +00001036 MIB.add(MI.getOperand(I));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +00001037 MBB->insert(MI, MIB);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001038 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001039 }
1040 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001041
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001042 // Try to convert an AND into an RISBG-type instruction.
1043 if (LogicOp And = interpretAndImmediate(Opcode)) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001044 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB;
Richard Sandiford70284282013-10-01 14:20:41 +00001045 // AND IMMEDIATE leaves the other bits of the register unchanged.
1046 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
1047 unsigned Start, End;
1048 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
1049 unsigned NewOpcode;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001050 if (And.RegSize == 64) {
Richard Sandiford70284282013-10-01 14:20:41 +00001051 NewOpcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001052 // Prefer RISBGN if available, since it does not clobber CC.
1053 if (STI.hasMiscellaneousExtensions())
1054 NewOpcode = SystemZ::RISBGN;
1055 } else {
Richard Sandiford70284282013-10-01 14:20:41 +00001056 NewOpcode = SystemZ::RISBMux;
1057 Start &= 31;
1058 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001059 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001060 MachineOperand &Dest = MI.getOperand(0);
1061 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford70284282013-10-01 14:20:41 +00001062 MachineInstrBuilder MIB =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001063 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00001064 .add(Dest)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001065 .addReg(0)
1066 .addReg(Src.getReg(), getKillRegState(Src.isKill()),
1067 Src.getSubReg())
1068 .addImm(Start)
1069 .addImm(End + 128)
1070 .addImm(0);
1071 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001072 }
1073 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001074 return nullptr;
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001075}
1076
Keno Fischere70b31f2015-06-08 20:09:58 +00001077MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001078 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001079 MachineBasicBlock::iterator InsertPt, int FrameIndex,
1080 LiveIntervals *LIS) const {
1081 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001082 const MachineFrameInfo &MFI = MF.getFrameInfo();
1083 unsigned Size = MFI.getObjectSize(FrameIndex);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001084 unsigned Opcode = MI.getOpcode();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001085
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001086 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001087 if (LIS != nullptr && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
1088 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) {
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001089
1090 // Check CC liveness, since new instruction introduces a dead
1091 // def of CC.
1092 MCRegUnitIterator CCUnit(SystemZ::CC, TRI);
1093 LiveRange &CCLiveRange = LIS->getRegUnit(*CCUnit);
1094 ++CCUnit;
Eugene Zelenko3943d2b2017-01-24 22:10:43 +00001095 assert(!CCUnit.isValid() && "CC only has one reg unit.");
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001096 SlotIndex MISlot =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001097 LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot();
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001098 if (!CCLiveRange.liveAt(MISlot)) {
1099 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001100 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt,
1101 MI.getDebugLoc(), get(SystemZ::AGSI))
1102 .addFrameIndex(FrameIndex)
1103 .addImm(0)
1104 .addImm(MI.getOperand(2).getImm());
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001105 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true);
1106 CCLiveRange.createDeadDef(MISlot, LIS->getVNInfoAllocator());
1107 return BuiltMI;
1108 }
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001109 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001110 return nullptr;
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001111 }
1112
1113 // All other cases require a single operand.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001114 if (Ops.size() != 1)
Craig Topper062a2ba2014-04-25 05:30:21 +00001115 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001116
1117 unsigned OpNum = Ops[0];
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001118 assert(Size ==
1119 MF.getRegInfo()
1120 .getRegClass(MI.getOperand(OpNum).getReg())
1121 ->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +00001122 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001123
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001124 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 &&
1125 isInt<8>(MI.getOperand(2).getImm())) {
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001126 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
1127 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +00001128 MachineInstr *BuiltMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001129 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
1130 .addFrameIndex(FrameIndex)
1131 .addImm(0)
1132 .addImm(MI.getOperand(2).getImm());
1133 transferDeadCC(&MI, BuiltMI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +00001134 return BuiltMI;
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001135 }
1136
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001137 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
1138 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
1139 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
1140 // If we're spilling the destination of an LDGR or LGDR, store the
1141 // source register instead.
1142 if (OpNum == 0) {
1143 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001144 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001145 get(StoreOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00001146 .add(MI.getOperand(1))
Keno Fischere70b31f2015-06-08 20:09:58 +00001147 .addFrameIndex(FrameIndex)
1148 .addImm(0)
1149 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001150 }
1151 // If we're spilling the source of an LDGR or LGDR, load the
1152 // destination register instead.
1153 if (OpNum == 1) {
1154 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001155 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Jonas Paulssonbd654212017-03-21 05:49:40 +00001156 get(LoadOpcode))
1157 .add(MI.getOperand(0))
1158 .addFrameIndex(FrameIndex)
1159 .addImm(0)
1160 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001161 }
1162 }
1163
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001164 // Look for cases where the source of a simple store or the destination
1165 // of a simple load is being spilled. Try to use MVC instead.
1166 //
1167 // Although MVC is in practice a fast choice in these cases, it is still
1168 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +00001169 // load or store is volatile. We also wouldn't be able to use MVC if
1170 // the two memories partially overlap, but that case cannot occur here,
1171 // because we know that one of the memories is a full frame index.
1172 //
1173 // For performance reasons, we also want to avoid using MVC if the addresses
1174 // might be equal. We don't worry about that case here, because spill slot
1175 // coloring happens later, and because we have special code to remove
1176 // MVCs that turn out to be redundant.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001177 if (OpNum == 0 && MI.hasOneMemOperand()) {
1178 MachineMemOperand *MMO = *MI.memoperands_begin();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001179 if (MMO->getSize() == Size && !MMO->isVolatile()) {
1180 // Handle conversion of loads.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001181 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) {
1182 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001183 get(SystemZ::MVC))
1184 .addFrameIndex(FrameIndex)
1185 .addImm(0)
1186 .addImm(Size)
Diana Picus116bbab2017-01-13 09:58:52 +00001187 .add(MI.getOperand(1))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001188 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +00001189 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001190 }
1191 // Handle conversion of stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001192 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) {
1193 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001194 get(SystemZ::MVC))
Diana Picus116bbab2017-01-13 09:58:52 +00001195 .add(MI.getOperand(1))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001196 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +00001197 .addImm(Size)
1198 .addFrameIndex(FrameIndex)
1199 .addImm(0)
1200 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001201 }
1202 }
1203 }
1204
Richard Sandiforded1fab62013-07-03 10:10:02 +00001205 // If the spilled operand is the final one, try to change <INSN>R
1206 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001207 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001208 if (MemOpcode >= 0) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001209 unsigned NumOps = MI.getNumExplicitOperands();
Richard Sandiforded1fab62013-07-03 10:10:02 +00001210 if (OpNum == NumOps - 1) {
1211 const MCInstrDesc &MemDesc = get(MemOpcode);
1212 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
1213 assert(AccessBytes != 0 && "Size of access should be known");
1214 assert(AccessBytes <= Size && "Access outside the frame index");
1215 uint64_t Offset = Size - AccessBytes;
Keno Fischere70b31f2015-06-08 20:09:58 +00001216 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001217 MI.getDebugLoc(), get(MemOpcode));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001218 for (unsigned I = 0; I < OpNum; ++I)
Diana Picus116bbab2017-01-13 09:58:52 +00001219 MIB.add(MI.getOperand(I));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001220 MIB.addFrameIndex(FrameIndex).addImm(Offset);
1221 if (MemDesc.TSFlags & SystemZII::HasIndex)
1222 MIB.addReg(0);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001223 transferDeadCC(&MI, MIB);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001224 return MIB;
1225 }
1226 }
1227
Craig Topper062a2ba2014-04-25 05:30:21 +00001228 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001229}
1230
Keno Fischere70b31f2015-06-08 20:09:58 +00001231MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001232 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
1233 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001234 LiveIntervals *LIS) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001235 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001236}
1237
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001238bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1239 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001240 case SystemZ::L128:
1241 splitMove(MI, SystemZ::LG);
1242 return true;
1243
1244 case SystemZ::ST128:
1245 splitMove(MI, SystemZ::STG);
1246 return true;
1247
1248 case SystemZ::LX:
1249 splitMove(MI, SystemZ::LD);
1250 return true;
1251
1252 case SystemZ::STX:
1253 splitMove(MI, SystemZ::STD);
1254 return true;
1255
Richard Sandiford89e160d2013-10-01 12:11:47 +00001256 case SystemZ::LBMux:
1257 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
1258 return true;
1259
1260 case SystemZ::LHMux:
1261 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
1262 return true;
1263
Richard Sandiford21235a22013-10-01 12:49:07 +00001264 case SystemZ::LLCRMux:
1265 expandZExtPseudo(MI, SystemZ::LLCR, 8);
1266 return true;
1267
1268 case SystemZ::LLHRMux:
1269 expandZExtPseudo(MI, SystemZ::LLHR, 16);
1270 return true;
1271
Richard Sandiford0d46b1a2013-10-01 12:19:08 +00001272 case SystemZ::LLCMux:
1273 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
1274 return true;
1275
1276 case SystemZ::LLHMux:
1277 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
1278 return true;
1279
Richard Sandiford0755c932013-10-01 11:26:28 +00001280 case SystemZ::LMux:
1281 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
1282 return true;
1283
Ulrich Weigand524f2762016-11-28 13:34:08 +00001284 case SystemZ::LOCMux:
1285 expandLOCPseudo(MI, SystemZ::LOC, SystemZ::LOCFH);
1286 return true;
1287
1288 case SystemZ::LOCHIMux:
1289 expandLOCPseudo(MI, SystemZ::LOCHI, SystemZ::LOCHHI);
1290 return true;
1291
1292 case SystemZ::LOCRMux:
1293 expandLOCRPseudo(MI, SystemZ::LOCR, SystemZ::LOCFHR);
1294 return true;
1295
Richard Sandiford5469c392013-10-01 12:22:49 +00001296 case SystemZ::STCMux:
1297 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
1298 return true;
1299
1300 case SystemZ::STHMux:
1301 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
1302 return true;
1303
Richard Sandiford0755c932013-10-01 11:26:28 +00001304 case SystemZ::STMux:
1305 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
1306 return true;
1307
Ulrich Weigand524f2762016-11-28 13:34:08 +00001308 case SystemZ::STOCMux:
1309 expandLOCPseudo(MI, SystemZ::STOC, SystemZ::STOCFH);
1310 return true;
1311
Richard Sandiford01240232013-10-01 13:02:28 +00001312 case SystemZ::LHIMux:
1313 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
1314 return true;
1315
1316 case SystemZ::IIFMux:
1317 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
1318 return true;
1319
Richard Sandiford1a569312013-10-01 13:18:56 +00001320 case SystemZ::IILMux:
1321 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
1322 return true;
1323
1324 case SystemZ::IIHMux:
1325 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
1326 return true;
1327
Richard Sandiford70284282013-10-01 14:20:41 +00001328 case SystemZ::NIFMux:
1329 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1330 return true;
1331
1332 case SystemZ::NILMux:
1333 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1334 return true;
1335
1336 case SystemZ::NIHMux:
1337 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1338 return true;
1339
Richard Sandiford6e96ac62013-10-01 13:22:41 +00001340 case SystemZ::OIFMux:
1341 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1342 return true;
1343
1344 case SystemZ::OILMux:
1345 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1346 return true;
1347
1348 case SystemZ::OIHMux:
1349 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1350 return true;
1351
Richard Sandiford5718dac2013-10-01 14:08:44 +00001352 case SystemZ::XIFMux:
1353 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1354 return true;
1355
Richard Sandiford2cac7632013-10-01 14:41:52 +00001356 case SystemZ::TMLMux:
1357 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1358 return true;
1359
1360 case SystemZ::TMHMux:
1361 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1362 return true;
1363
Richard Sandiford42a694f2013-10-01 14:53:46 +00001364 case SystemZ::AHIMux:
1365 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1366 return true;
1367
1368 case SystemZ::AHIMuxK:
1369 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1370 return true;
1371
1372 case SystemZ::AFIMux:
1373 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1374 return true;
1375
Ulrich Weigand75839912016-11-28 13:40:08 +00001376 case SystemZ::CHIMux:
1377 expandRIPseudo(MI, SystemZ::CHI, SystemZ::CIH, false);
1378 return true;
1379
Richard Sandiforda9ac0e02013-10-01 14:56:23 +00001380 case SystemZ::CFIMux:
1381 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1382 return true;
1383
1384 case SystemZ::CLFIMux:
1385 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1386 return true;
1387
Richard Sandifordb63e3002013-10-01 15:00:44 +00001388 case SystemZ::CMux:
1389 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1390 return true;
1391
1392 case SystemZ::CLMux:
1393 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1394 return true;
1395
Richard Sandiford70284282013-10-01 14:20:41 +00001396 case SystemZ::RISBMux: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001397 bool DestIsHigh = isHighReg(MI.getOperand(0).getReg());
1398 bool SrcIsHigh = isHighReg(MI.getOperand(2).getReg());
Richard Sandiford70284282013-10-01 14:20:41 +00001399 if (SrcIsHigh == DestIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001400 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
Richard Sandiford70284282013-10-01 14:20:41 +00001401 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001402 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1403 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32);
Richard Sandiford70284282013-10-01 14:20:41 +00001404 }
1405 return true;
1406 }
1407
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001408 case SystemZ::ADJDYNALLOC:
1409 splitAdjDynAlloc(MI);
1410 return true;
1411
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001412 case TargetOpcode::LOAD_STACK_GUARD:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001413 expandLoadStackGuard(&MI);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001414 return true;
1415
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001416 default:
1417 return false;
1418 }
1419}
1420
Sjoerd Meijer0eb96ed2016-07-29 08:16:16 +00001421unsigned SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001422 if (MI.getOpcode() == TargetOpcode::INLINEASM) {
1423 const MachineFunction *MF = MI.getParent()->getParent();
1424 const char *AsmStr = MI.getOperand(0).getSymbolName();
Richard Sandiford312425f2013-05-20 14:23:08 +00001425 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1426 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001427 return MI.getDesc().getSize();
Richard Sandiford312425f2013-05-20 14:23:08 +00001428}
1429
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001430SystemZII::Branch
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001431SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const {
1432 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001433 case SystemZ::BR:
1434 case SystemZ::J:
1435 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001436 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001437 SystemZ::CCMASK_ANY, &MI.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001438
1439 case SystemZ::BRC:
1440 case SystemZ::BRCL:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001441 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(),
1442 MI.getOperand(1).getImm(), &MI.getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001443
Richard Sandifordc2121252013-08-05 11:23:46 +00001444 case SystemZ::BRCT:
Ulrich Weigand75839912016-11-28 13:40:08 +00001445 case SystemZ::BRCTH:
Richard Sandifordc2121252013-08-05 11:23:46 +00001446 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001447 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001448
1449 case SystemZ::BRCTG:
1450 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001451 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001452
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001453 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001454 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001455 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001456 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001457
Richard Sandiford93183ee2013-09-18 09:56:40 +00001458 case SystemZ::CLIJ:
1459 case SystemZ::CLRJ:
1460 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001461 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001462
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001463 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001464 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001465 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001466 MI.getOperand(2).getImm(), &MI.getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001467
Richard Sandiford93183ee2013-09-18 09:56:40 +00001468 case SystemZ::CLGIJ:
1469 case SystemZ::CLGRJ:
1470 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001471 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001472
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001473 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001474 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001475 }
1476}
1477
1478void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1479 unsigned &LoadOpcode,
1480 unsigned &StoreOpcode) const {
1481 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1482 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001483 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001484 } else if (RC == &SystemZ::GRH32BitRegClass) {
1485 LoadOpcode = SystemZ::LFH;
1486 StoreOpcode = SystemZ::STFH;
1487 } else if (RC == &SystemZ::GRX32BitRegClass) {
1488 LoadOpcode = SystemZ::LMux;
1489 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001490 } else if (RC == &SystemZ::GR64BitRegClass ||
1491 RC == &SystemZ::ADDR64BitRegClass) {
1492 LoadOpcode = SystemZ::LG;
1493 StoreOpcode = SystemZ::STG;
1494 } else if (RC == &SystemZ::GR128BitRegClass ||
1495 RC == &SystemZ::ADDR128BitRegClass) {
1496 LoadOpcode = SystemZ::L128;
1497 StoreOpcode = SystemZ::ST128;
1498 } else if (RC == &SystemZ::FP32BitRegClass) {
1499 LoadOpcode = SystemZ::LE;
1500 StoreOpcode = SystemZ::STE;
1501 } else if (RC == &SystemZ::FP64BitRegClass) {
1502 LoadOpcode = SystemZ::LD;
1503 StoreOpcode = SystemZ::STD;
1504 } else if (RC == &SystemZ::FP128BitRegClass) {
1505 LoadOpcode = SystemZ::LX;
1506 StoreOpcode = SystemZ::STX;
Ulrich Weigand49506d72015-05-05 19:28:34 +00001507 } else if (RC == &SystemZ::VR32BitRegClass) {
1508 LoadOpcode = SystemZ::VL32;
1509 StoreOpcode = SystemZ::VST32;
1510 } else if (RC == &SystemZ::VR64BitRegClass) {
1511 LoadOpcode = SystemZ::VL64;
1512 StoreOpcode = SystemZ::VST64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001513 } else if (RC == &SystemZ::VF128BitRegClass ||
1514 RC == &SystemZ::VR128BitRegClass) {
1515 LoadOpcode = SystemZ::VL;
1516 StoreOpcode = SystemZ::VST;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001517 } else
1518 llvm_unreachable("Unsupported regclass to load or store");
1519}
1520
1521unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1522 int64_t Offset) const {
1523 const MCInstrDesc &MCID = get(Opcode);
1524 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1525 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1526 // Get the instruction to use for unsigned 12-bit displacements.
1527 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1528 if (Disp12Opcode >= 0)
1529 return Disp12Opcode;
1530
1531 // All address-related instructions can use unsigned 12-bit
1532 // displacements.
1533 return Opcode;
1534 }
1535 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1536 // Get the instruction to use for signed 20-bit displacements.
1537 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1538 if (Disp20Opcode >= 0)
1539 return Disp20Opcode;
1540
1541 // Check whether Opcode allows signed 20-bit displacements.
1542 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1543 return Opcode;
1544 }
1545 return 0;
1546}
1547
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001548unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1549 switch (Opcode) {
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001550 case SystemZ::L: return SystemZ::LT;
1551 case SystemZ::LY: return SystemZ::LT;
1552 case SystemZ::LG: return SystemZ::LTG;
1553 case SystemZ::LGF: return SystemZ::LTGF;
1554 case SystemZ::LR: return SystemZ::LTR;
1555 case SystemZ::LGFR: return SystemZ::LTGFR;
1556 case SystemZ::LGR: return SystemZ::LTGR;
1557 case SystemZ::LER: return SystemZ::LTEBR;
1558 case SystemZ::LDR: return SystemZ::LTDBR;
1559 case SystemZ::LXR: return SystemZ::LTXBR;
Jonas Paulsson12629322015-10-01 18:12:28 +00001560 case SystemZ::LCDFR: return SystemZ::LCDBR;
1561 case SystemZ::LPDFR: return SystemZ::LPDBR;
1562 case SystemZ::LNDFR: return SystemZ::LNDBR;
1563 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1564 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1565 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001566 // On zEC12 we prefer to use RISBGN. But if there is a chance to
1567 // actually use the condition code, we may turn it back into RISGB.
1568 // Note that RISBG is not really a "load-and-test" instruction,
1569 // but sets the same condition code values, so is OK to use here.
1570 case SystemZ::RISBGN: return SystemZ::RISBG;
1571 default: return 0;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001572 }
1573}
1574
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001575// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1576// have already been filtered out. Store the first set bit in LSB and
1577// the number of set bits in Length if so.
1578static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1579 unsigned First = findFirstSet(Mask);
1580 uint64_t Top = (Mask >> First) + 1;
1581 if ((Top & -Top) == Top) {
1582 LSB = First;
1583 Length = findFirstSet(Top);
1584 return true;
1585 }
1586 return false;
1587}
1588
1589bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1590 unsigned &Start, unsigned &End) const {
1591 // Reject trivial all-zero masks.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001592 Mask &= allOnes(BitSize);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001593 if (Mask == 0)
1594 return false;
1595
1596 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1597 // the msb and End specifies the index of the lsb.
1598 unsigned LSB, Length;
1599 if (isStringOfOnes(Mask, LSB, Length)) {
1600 Start = 63 - (LSB + Length - 1);
1601 End = 63 - LSB;
1602 return true;
1603 }
1604
1605 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1606 // of the low 1s and End specifies the lsb of the high 1s.
1607 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1608 assert(LSB > 0 && "Bottom bit must be set");
1609 assert(LSB + Length < BitSize && "Top bit must be set");
1610 Start = 63 - (LSB - 1);
1611 End = 63 - (LSB + Length);
1612 return true;
1613 }
1614
1615 return false;
1616}
1617
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001618unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode,
1619 SystemZII::FusedCompareType Type,
1620 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001621 switch (Opcode) {
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001622 case SystemZ::CHI:
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001623 case SystemZ::CGHI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001624 if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
1625 return 0;
1626 break;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001627 case SystemZ::CLFI:
Richard Sandiford93183ee2013-09-18 09:56:40 +00001628 case SystemZ::CLGFI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001629 if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
1630 return 0;
Ulrich Weiganda0e73252016-11-11 12:48:26 +00001631 break;
1632 case SystemZ::CL:
1633 case SystemZ::CLG:
1634 if (!STI.hasMiscellaneousExtensions())
1635 return 0;
1636 if (!(MI && MI->getOperand(3).getReg() == 0))
1637 return 0;
1638 break;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001639 }
1640 switch (Type) {
1641 case SystemZII::CompareAndBranch:
1642 switch (Opcode) {
1643 case SystemZ::CR:
1644 return SystemZ::CRJ;
1645 case SystemZ::CGR:
1646 return SystemZ::CGRJ;
1647 case SystemZ::CHI:
1648 return SystemZ::CIJ;
1649 case SystemZ::CGHI:
1650 return SystemZ::CGIJ;
1651 case SystemZ::CLR:
1652 return SystemZ::CLRJ;
1653 case SystemZ::CLGR:
1654 return SystemZ::CLGRJ;
1655 case SystemZ::CLFI:
1656 return SystemZ::CLIJ;
1657 case SystemZ::CLGFI:
1658 return SystemZ::CLGIJ;
1659 default:
1660 return 0;
1661 }
1662 case SystemZII::CompareAndReturn:
1663 switch (Opcode) {
1664 case SystemZ::CR:
1665 return SystemZ::CRBReturn;
1666 case SystemZ::CGR:
1667 return SystemZ::CGRBReturn;
1668 case SystemZ::CHI:
1669 return SystemZ::CIBReturn;
1670 case SystemZ::CGHI:
1671 return SystemZ::CGIBReturn;
1672 case SystemZ::CLR:
1673 return SystemZ::CLRBReturn;
1674 case SystemZ::CLGR:
1675 return SystemZ::CLGRBReturn;
1676 case SystemZ::CLFI:
1677 return SystemZ::CLIBReturn;
1678 case SystemZ::CLGFI:
1679 return SystemZ::CLGIBReturn;
1680 default:
1681 return 0;
1682 }
Ulrich Weigand848a5132016-04-11 12:12:32 +00001683 case SystemZII::CompareAndSibcall:
1684 switch (Opcode) {
1685 case SystemZ::CR:
1686 return SystemZ::CRBCall;
1687 case SystemZ::CGR:
1688 return SystemZ::CGRBCall;
1689 case SystemZ::CHI:
1690 return SystemZ::CIBCall;
1691 case SystemZ::CGHI:
1692 return SystemZ::CGIBCall;
1693 case SystemZ::CLR:
1694 return SystemZ::CLRBCall;
1695 case SystemZ::CLGR:
1696 return SystemZ::CLGRBCall;
1697 case SystemZ::CLFI:
1698 return SystemZ::CLIBCall;
1699 case SystemZ::CLGFI:
1700 return SystemZ::CLGIBCall;
1701 default:
1702 return 0;
1703 }
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001704 case SystemZII::CompareAndTrap:
1705 switch (Opcode) {
1706 case SystemZ::CR:
1707 return SystemZ::CRT;
1708 case SystemZ::CGR:
1709 return SystemZ::CGRT;
1710 case SystemZ::CHI:
1711 return SystemZ::CIT;
1712 case SystemZ::CGHI:
1713 return SystemZ::CGIT;
1714 case SystemZ::CLR:
1715 return SystemZ::CLRT;
1716 case SystemZ::CLGR:
1717 return SystemZ::CLGRT;
1718 case SystemZ::CLFI:
1719 return SystemZ::CLFIT;
1720 case SystemZ::CLGFI:
1721 return SystemZ::CLGIT;
Ulrich Weiganda0e73252016-11-11 12:48:26 +00001722 case SystemZ::CL:
1723 return SystemZ::CLT;
1724 case SystemZ::CLG:
1725 return SystemZ::CLGT;
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001726 default:
1727 return 0;
1728 }
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001729 }
Ulrich Weigand79391ee2016-04-07 16:33:25 +00001730 return 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001731}
1732
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +00001733unsigned SystemZInstrInfo::getLoadAndTrap(unsigned Opcode) const {
1734 if (!STI.hasLoadAndTrap())
1735 return 0;
1736 switch (Opcode) {
1737 case SystemZ::L:
1738 case SystemZ::LY:
1739 return SystemZ::LAT;
1740 case SystemZ::LG:
1741 return SystemZ::LGAT;
1742 case SystemZ::LFH:
1743 return SystemZ::LFHAT;
1744 case SystemZ::LLGF:
1745 return SystemZ::LLGFAT;
1746 case SystemZ::LLGT:
1747 return SystemZ::LLGTAT;
1748 }
1749 return 0;
1750}
1751
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001752void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1753 MachineBasicBlock::iterator MBBI,
1754 unsigned Reg, uint64_t Value) const {
1755 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1756 unsigned Opcode;
1757 if (isInt<16>(Value))
1758 Opcode = SystemZ::LGHI;
1759 else if (SystemZ::isImmLL(Value))
1760 Opcode = SystemZ::LLILL;
1761 else if (SystemZ::isImmLH(Value)) {
1762 Opcode = SystemZ::LLILH;
1763 Value >>= 16;
1764 } else {
1765 assert(isInt<32>(Value) && "Huge values not handled yet");
1766 Opcode = SystemZ::LGFI;
1767 }
1768 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1769}
Jonas Paulsson8010b632016-10-20 08:27:16 +00001770
1771bool SystemZInstrInfo::
1772areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb,
1773 AliasAnalysis *AA) const {
1774
1775 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand())
1776 return false;
1777
1778 // If mem-operands show that the same address Value is used by both
1779 // instructions, check for non-overlapping offsets and widths. Not
1780 // sure if a register based analysis would be an improvement...
1781
1782 MachineMemOperand *MMOa = *MIa.memoperands_begin();
1783 MachineMemOperand *MMOb = *MIb.memoperands_begin();
1784 const Value *VALa = MMOa->getValue();
1785 const Value *VALb = MMOb->getValue();
1786 bool SameVal = (VALa && VALb && (VALa == VALb));
1787 if (!SameVal) {
1788 const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
1789 const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
1790 if (PSVa && PSVb && (PSVa == PSVb))
1791 SameVal = true;
1792 }
1793 if (SameVal) {
1794 int OffsetA = MMOa->getOffset(), OffsetB = MMOb->getOffset();
1795 int WidthA = MMOa->getSize(), WidthB = MMOb->getSize();
1796 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
1797 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
1798 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
1799 if (LowOffset + LowWidth <= HighOffset)
1800 return true;
1801 }
1802
1803 return false;
1804}