blob: f3eed05e88e65595de7608d3dd1dd3aafeea847a [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 Paulsson2ba31522016-03-31 08:00:14 +0000107 // Clear the kill flags for the base and index registers in the first
108 // instruction.
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000109 EarlierMI->getOperand(1).setIsKill(false);
Jonas Paulsson7da38202015-10-26 15:03:41 +0000110 EarlierMI->getOperand(3).setIsKill(false);
Jonas Paulsson63a2b682015-10-10 07:14:24 +0000111
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000112 // Set the opcodes.
113 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
114 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
115 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
116
117 EarlierMI->setDesc(get(HighOpcode));
118 MI->setDesc(get(LowOpcode));
119}
120
121// Split ADJDYNALLOC instruction MI.
122void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
123 MachineBasicBlock *MBB = MI->getParent();
124 MachineFunction &MF = *MBB->getParent();
Matthias Braun941a7052016-07-28 18:40:00 +0000125 MachineFrameInfo &MFFrame = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000126 MachineOperand &OffsetMO = MI->getOperand(2);
127
Matthias Braun941a7052016-07-28 18:40:00 +0000128 uint64_t Offset = (MFFrame.getMaxCallFrameSize() +
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000129 SystemZMC::CallFrameSize +
130 OffsetMO.getImm());
131 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
132 assert(NewOpcode && "No support for huge argument lists yet");
133 MI->setDesc(get(NewOpcode));
134 OffsetMO.setImm(Offset);
135}
136
Richard Sandiford01240232013-10-01 13:02:28 +0000137// MI is an RI-style pseudo instruction. Replace it with LowOpcode
138// if the first operand is a low GR32 and HighOpcode if the first operand
139// is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
140// and HighOpcode takes an unsigned 32-bit operand. In those cases,
141// MI has the same kind of operand as LowOpcode, so needs to be converted
142// if HighOpcode is used.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000143void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford01240232013-10-01 13:02:28 +0000144 unsigned HighOpcode,
145 bool ConvertHigh) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000146 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford01240232013-10-01 13:02:28 +0000147 bool IsHigh = isHighReg(Reg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000148 MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode));
Richard Sandiford01240232013-10-01 13:02:28 +0000149 if (IsHigh && ConvertHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000150 MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm()));
Richard Sandiford01240232013-10-01 13:02:28 +0000151}
152
Richard Sandiford42a694f2013-10-01 14:53:46 +0000153// MI is a three-operand RIE-style pseudo instruction. Replace it with
Jonas Paulsson18d877f2015-10-09 07:19:16 +0000154// LowOpcodeK if the registers are both low GR32s, otherwise use a move
Richard Sandiford42a694f2013-10-01 14:53:46 +0000155// followed by HighOpcode or LowOpcode, depending on whether the target
156// is a high or low GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000157void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford42a694f2013-10-01 14:53:46 +0000158 unsigned LowOpcodeK,
159 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000160 unsigned DestReg = MI.getOperand(0).getReg();
161 unsigned SrcReg = MI.getOperand(1).getReg();
Richard Sandiford42a694f2013-10-01 14:53:46 +0000162 bool DestIsHigh = isHighReg(DestReg);
163 bool SrcIsHigh = isHighReg(SrcReg);
164 if (!DestIsHigh && !SrcIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000165 MI.setDesc(get(LowOpcodeK));
Richard Sandiford42a694f2013-10-01 14:53:46 +0000166 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000167 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg,
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000168 SystemZ::LR, 32, MI.getOperand(1).isKill(),
169 MI.getOperand(1).isUndef());
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000170 MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
171 MI.getOperand(1).setReg(DestReg);
172 MI.tieOperands(0, 1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000173 }
174}
175
Richard Sandiford0755c932013-10-01 11:26:28 +0000176// MI is an RXY-style pseudo instruction. Replace it with LowOpcode
177// if the first operand is a low GR32 and HighOpcode if the first operand
178// is a high GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000179void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford0755c932013-10-01 11:26:28 +0000180 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000181 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000182 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000183 MI.getOperand(2).getImm());
184 MI.setDesc(get(Opcode));
Richard Sandiford0755c932013-10-01 11:26:28 +0000185}
186
Ulrich Weigand524f2762016-11-28 13:34:08 +0000187// MI is a load-on-condition pseudo instruction with a single register
188// (source or destination) operand. Replace it with LowOpcode if the
189// register is a low GR32 and HighOpcode if the register is a high GR32.
190void SystemZInstrInfo::expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode,
191 unsigned HighOpcode) const {
192 unsigned Reg = MI.getOperand(0).getReg();
193 unsigned Opcode = isHighReg(Reg) ? HighOpcode : LowOpcode;
194 MI.setDesc(get(Opcode));
195}
196
197// MI is a load-register-on-condition pseudo instruction. Replace it with
198// LowOpcode if source and destination are both low GR32s and HighOpcode if
199// source and destination are both high GR32s.
200void SystemZInstrInfo::expandLOCRPseudo(MachineInstr &MI, unsigned LowOpcode,
201 unsigned HighOpcode) const {
202 unsigned DestReg = MI.getOperand(0).getReg();
203 unsigned SrcReg = MI.getOperand(2).getReg();
204 bool DestIsHigh = isHighReg(DestReg);
205 bool SrcIsHigh = isHighReg(SrcReg);
206
207 if (!DestIsHigh && !SrcIsHigh)
208 MI.setDesc(get(LowOpcode));
209 else if (DestIsHigh && SrcIsHigh)
210 MI.setDesc(get(HighOpcode));
211
212 // If we were unable to implement the pseudo with a single instruction, we
213 // need to convert it back into a branch sequence. This cannot be done here
214 // since the caller of expandPostRAPseudo does not handle changes to the CFG
215 // correctly. This change is defered to the SystemZExpandPseudo pass.
216}
217
Richard Sandiford21235a22013-10-01 12:49:07 +0000218// MI is an RR-style pseudo instruction that zero-extends the low Size bits
219// of one GRX32 into another. Replace it with LowOpcode if both operands
220// are low registers, otherwise use RISB[LH]G.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000221void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford21235a22013-10-01 12:49:07 +0000222 unsigned Size) const {
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000223 MachineInstrBuilder MIB =
224 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(),
225 MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode,
226 Size, MI.getOperand(1).isKill(), MI.getOperand(1).isUndef());
227
228 // Keep the remaining operands as-is.
229 for (unsigned I = 2; I < MI.getNumOperands(); ++I)
230 MIB.add(MI.getOperand(I));
231
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000232 MI.eraseFromParent();
Richard Sandiford21235a22013-10-01 12:49:07 +0000233}
234
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000235void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const {
236 MachineBasicBlock *MBB = MI->getParent();
237 MachineFunction &MF = *MBB->getParent();
238 const unsigned Reg = MI->getOperand(0).getReg();
239
240 // Conveniently, all 4 instructions are cloned from LOAD_STACK_GUARD,
241 // so they already have operand 0 set to reg.
242
243 // ear <reg>, %a0
244 MachineInstr *Ear1MI = MF.CloneMachineInstr(MI);
245 MBB->insert(MI, Ear1MI);
246 Ear1MI->setDesc(get(SystemZ::EAR));
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000247 MachineInstrBuilder(MF, Ear1MI).addReg(SystemZ::A0);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000248
249 // sllg <reg>, <reg>, 32
250 MachineInstr *SllgMI = MF.CloneMachineInstr(MI);
251 MBB->insert(MI, SllgMI);
252 SllgMI->setDesc(get(SystemZ::SLLG));
253 MachineInstrBuilder(MF, SllgMI).addReg(Reg).addReg(0).addImm(32);
254
255 // ear <reg>, %a1
256 MachineInstr *Ear2MI = MF.CloneMachineInstr(MI);
257 MBB->insert(MI, Ear2MI);
258 Ear2MI->setDesc(get(SystemZ::EAR));
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000259 MachineInstrBuilder(MF, Ear2MI).addReg(SystemZ::A1);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000260
261 // lg <reg>, 40(<reg>)
262 MI->setDesc(get(SystemZ::LG));
263 MachineInstrBuilder(MF, MI).addReg(Reg).addImm(40).addReg(0);
264}
265
Richard Sandiford0755c932013-10-01 11:26:28 +0000266// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
267// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
268// are low registers, otherwise use RISB[LH]G. Size is the number of bits
269// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
270// KillSrc is true if this move is the last use of SrcReg.
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000271MachineInstrBuilder
272SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
273 MachineBasicBlock::iterator MBBI,
274 const DebugLoc &DL, unsigned DestReg,
275 unsigned SrcReg, unsigned LowLowOpcode,
276 unsigned Size, bool KillSrc,
277 bool UndefSrc) const {
Richard Sandiford0755c932013-10-01 11:26:28 +0000278 unsigned Opcode;
279 bool DestIsHigh = isHighReg(DestReg);
280 bool SrcIsHigh = isHighReg(SrcReg);
281 if (DestIsHigh && SrcIsHigh)
282 Opcode = SystemZ::RISBHH;
283 else if (DestIsHigh && !SrcIsHigh)
284 Opcode = SystemZ::RISBHL;
285 else if (!DestIsHigh && SrcIsHigh)
286 Opcode = SystemZ::RISBLH;
287 else {
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000288 return BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000289 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc));
Richard Sandiford0755c932013-10-01 11:26:28 +0000290 }
291 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
Jonas Paulsson808c89f2017-03-22 06:03:32 +0000292 return BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
Richard Sandiford0755c932013-10-01 11:26:28 +0000293 .addReg(DestReg, RegState::Undef)
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000294 .addReg(SrcReg, getKillRegState(KillSrc) | getUndefRegState(UndefSrc))
Richard Sandiford0755c932013-10-01 11:26:28 +0000295 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
296}
297
Ulrich Weigand524f2762016-11-28 13:34:08 +0000298MachineInstr *SystemZInstrInfo::commuteInstructionImpl(MachineInstr &MI,
299 bool NewMI,
300 unsigned OpIdx1,
301 unsigned OpIdx2) const {
302 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & {
303 if (NewMI)
304 return *MI.getParent()->getParent()->CloneMachineInstr(&MI);
305 return MI;
306 };
307
308 switch (MI.getOpcode()) {
309 case SystemZ::LOCRMux:
310 case SystemZ::LOCFHR:
311 case SystemZ::LOCR:
312 case SystemZ::LOCGR: {
313 auto &WorkingMI = cloneIfNew(MI);
314 // Invert condition.
315 unsigned CCValid = WorkingMI.getOperand(3).getImm();
316 unsigned CCMask = WorkingMI.getOperand(4).getImm();
317 WorkingMI.getOperand(4).setImm(CCMask ^ CCValid);
318 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
319 OpIdx1, OpIdx2);
320 }
321 default:
322 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
323 }
324}
325
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000326// If MI is a simple load or store for a frame object, return the register
327// it loads or stores and set FrameIndex to the index of the frame object.
328// Return 0 otherwise.
329//
330// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000331static int isSimpleMove(const MachineInstr &MI, int &FrameIndex,
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000332 unsigned Flag) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000333 const MCInstrDesc &MCID = MI.getDesc();
334 if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() &&
335 MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) {
336 FrameIndex = MI.getOperand(1).getIndex();
337 return MI.getOperand(0).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000338 }
339 return 0;
340}
341
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000342unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000343 int &FrameIndex) const {
344 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
345}
346
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000347unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000348 int &FrameIndex) const {
349 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
350}
351
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000352bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI,
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000353 int &DestFrameIndex,
354 int &SrcFrameIndex) const {
355 // Check for MVC 0(Length,FI1),0(FI2)
Matthias Braun941a7052016-07-28 18:40:00 +0000356 const MachineFrameInfo &MFI = MI.getParent()->getParent()->getFrameInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000357 if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() ||
358 MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() ||
359 MI.getOperand(4).getImm() != 0)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000360 return false;
361
362 // Check that Length covers the full slots.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000363 int64_t Length = MI.getOperand(2).getImm();
364 unsigned FI1 = MI.getOperand(0).getIndex();
365 unsigned FI2 = MI.getOperand(3).getIndex();
Matthias Braun941a7052016-07-28 18:40:00 +0000366 if (MFI.getObjectSize(FI1) != Length ||
367 MFI.getObjectSize(FI2) != Length)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000368 return false;
369
370 DestFrameIndex = FI1;
371 SrcFrameIndex = FI2;
372 return true;
373}
374
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000375bool SystemZInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000376 MachineBasicBlock *&TBB,
377 MachineBasicBlock *&FBB,
378 SmallVectorImpl<MachineOperand> &Cond,
379 bool AllowModify) const {
380 // Most of the code and comments here are boilerplate.
381
382 // Start from the bottom of the block and work up, examining the
383 // terminator instructions.
384 MachineBasicBlock::iterator I = MBB.end();
385 while (I != MBB.begin()) {
386 --I;
387 if (I->isDebugValue())
388 continue;
389
390 // Working from the bottom, when we see a non-terminator instruction, we're
391 // done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000392 if (!isUnpredicatedTerminator(*I))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000393 break;
394
395 // A terminator that isn't a branch can't easily be handled by this
396 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000397 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000398 return true;
399
400 // Can't handle indirect branches.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000401 SystemZII::Branch Branch(getBranchInfo(*I));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000402 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000403 return true;
404
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000405 // Punt on compound branches.
406 if (Branch.Type != SystemZII::BranchNormal)
407 return true;
408
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000409 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000410 // Handle unconditional branches.
411 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000412 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000413 continue;
414 }
415
416 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000417 while (std::next(I) != MBB.end())
418 std::next(I)->eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000419
420 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000421 FBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000422
423 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000424 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000425 TBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000426 I->eraseFromParent();
427 I = MBB.end();
428 continue;
429 }
430
431 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000432 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000433 continue;
434 }
435
436 // Working from the bottom, handle the first conditional branch.
437 if (Cond.empty()) {
438 // FIXME: add X86-style branch swap
439 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000440 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000441 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000442 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000443 continue;
444 }
445
446 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000447 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000448
449 // Only handle the case where all conditional branches branch to the same
450 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000451 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000452 return true;
453
454 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000455 unsigned OldCCValid = Cond[0].getImm();
456 unsigned OldCCMask = Cond[1].getImm();
457 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000458 continue;
459
460 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000461 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000462 }
463
464 return false;
465}
466
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000467unsigned SystemZInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000468 int *BytesRemoved) const {
469 assert(!BytesRemoved && "code size not handled");
470
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000471 // Most of the code and comments here are boilerplate.
472 MachineBasicBlock::iterator I = MBB.end();
473 unsigned Count = 0;
474
475 while (I != MBB.begin()) {
476 --I;
477 if (I->isDebugValue())
478 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000479 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000480 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000481 if (!getBranchInfo(*I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000482 break;
483 // Remove the branch.
484 I->eraseFromParent();
485 I = MBB.end();
486 ++Count;
487 }
488
489 return Count;
490}
491
Richard Sandiford3d768e32013-07-31 12:30:20 +0000492bool SystemZInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000493reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000494 assert(Cond.size() == 2 && "Invalid condition");
495 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
496 return false;
497}
498
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000499unsigned SystemZInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000500 MachineBasicBlock *TBB,
501 MachineBasicBlock *FBB,
502 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000503 const DebugLoc &DL,
504 int *BytesAdded) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000505 // In this function we output 32-bit branches, which should always
506 // have enough range. They can be shortened and relaxed by later code
507 // in the pipeline, if desired.
508
509 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000510 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000511 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000512 "SystemZ branch conditions have one component!");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000513 assert(!BytesAdded && "code size not handled");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000514
515 if (Cond.empty()) {
516 // Unconditional branch?
517 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000518 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000519 return 1;
520 }
521
522 // Conditional branch.
523 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000524 unsigned CCValid = Cond[0].getImm();
525 unsigned CCMask = Cond[1].getImm();
526 BuildMI(&MBB, DL, get(SystemZ::BRC))
527 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000528 ++Count;
529
530 if (FBB) {
531 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000532 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000533 ++Count;
534 }
535 return Count;
536}
537
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000538bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
539 unsigned &SrcReg2, int &Mask,
540 int &Value) const {
541 assert(MI.isCompare() && "Caller should have checked for a comparison");
Richard Sandiford564681c2013-08-12 10:28:10 +0000542
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000543 if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() &&
544 MI.getOperand(1).isImm()) {
545 SrcReg = MI.getOperand(0).getReg();
Richard Sandiford564681c2013-08-12 10:28:10 +0000546 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000547 Value = MI.getOperand(1).getImm();
Richard Sandiford564681c2013-08-12 10:28:10 +0000548 Mask = ~0;
549 return true;
550 }
551
552 return false;
553}
554
Richard Sandiforda5901252013-08-16 10:22:54 +0000555// If Reg is a virtual register, return its definition, otherwise return null.
556static MachineInstr *getDef(unsigned Reg,
557 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000558 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +0000559 return nullptr;
Richard Sandiford564681c2013-08-12 10:28:10 +0000560 return MRI->getUniqueVRegDef(Reg);
561}
562
563// Return true if MI is a shift of type Opcode by Imm bits.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000564static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000565 return (MI->getOpcode() == Opcode &&
566 !MI->getOperand(2).getReg() &&
567 MI->getOperand(3).getImm() == Imm);
568}
569
Richard Sandiforda5901252013-08-16 10:22:54 +0000570// If the destination of MI has no uses, delete it as dead.
571static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
572 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
573 MI->eraseFromParent();
574}
575
Richard Sandiford564681c2013-08-12 10:28:10 +0000576// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000577// the result of an IPM sequence whose input CC survives until Compare,
578// and whether Compare is therefore redundant. Delete it and return
579// true if so.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000580static bool removeIPMBasedCompare(MachineInstr &Compare, unsigned SrcReg,
Richard Sandiforda5901252013-08-16 10:22:54 +0000581 const MachineRegisterInfo *MRI,
582 const TargetRegisterInfo *TRI) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000583 MachineInstr *LGFR = nullptr;
Richard Sandiforda5901252013-08-16 10:22:54 +0000584 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000585 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
586 LGFR = RLL;
587 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
588 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000589 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000590 return false;
591
Richard Sandiforda5901252013-08-16 10:22:54 +0000592 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000593 if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
Richard Sandiford564681c2013-08-12 10:28:10 +0000594 return false;
595
Richard Sandiforda5901252013-08-16 10:22:54 +0000596 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000597 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
598 return false;
599
600 // Check that there are no assignments to CC between the IPM and Compare,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000601 if (IPM->getParent() != Compare.getParent())
Richard Sandiford564681c2013-08-12 10:28:10 +0000602 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000603 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare.getIterator();
Richard Sandiford564681c2013-08-12 10:28:10 +0000604 for (++MBBI; MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000605 MachineInstr &MI = *MBBI;
606 if (MI.modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000607 return false;
608 }
609
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000610 Compare.eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000611 if (LGFR)
612 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000613 eraseIfDead(RLL, MRI);
614 eraseIfDead(SRL, MRI);
615 eraseIfDead(IPM, MRI);
616
Richard Sandiford564681c2013-08-12 10:28:10 +0000617 return true;
618}
619
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000620bool SystemZInstrInfo::optimizeCompareInstr(
621 MachineInstr &Compare, unsigned SrcReg, unsigned SrcReg2, int Mask,
622 int Value, const MachineRegisterInfo *MRI) const {
Richard Sandiford564681c2013-08-12 10:28:10 +0000623 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000624 bool IsLogical = (Compare.getDesc().TSFlags & SystemZII::IsLogical) != 0;
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000625 return Value == 0 && !IsLogical &&
626 removeIPMBasedCompare(Compare, SrcReg, MRI, &RI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000627}
628
Ulrich Weigand524f2762016-11-28 13:34:08 +0000629bool SystemZInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
630 ArrayRef<MachineOperand> Pred,
631 unsigned TrueReg, unsigned FalseReg,
632 int &CondCycles, int &TrueCycles,
633 int &FalseCycles) const {
634 // Not all subtargets have LOCR instructions.
635 if (!STI.hasLoadStoreOnCond())
636 return false;
637 if (Pred.size() != 2)
638 return false;
639
640 // Check register classes.
641 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
642 const TargetRegisterClass *RC =
643 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
644 if (!RC)
645 return false;
646
647 // We have LOCR instructions for 32 and 64 bit general purpose registers.
648 if ((STI.hasLoadStoreOnCond2() &&
649 SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) ||
650 SystemZ::GR32BitRegClass.hasSubClassEq(RC) ||
651 SystemZ::GR64BitRegClass.hasSubClassEq(RC)) {
652 CondCycles = 2;
653 TrueCycles = 2;
654 FalseCycles = 2;
655 return true;
Richard Sandifordf2404162013-07-25 09:11:15 +0000656 }
Ulrich Weigand524f2762016-11-28 13:34:08 +0000657
658 // Can't do anything else.
659 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000660}
661
Ulrich Weigand524f2762016-11-28 13:34:08 +0000662void SystemZInstrInfo::insertSelect(MachineBasicBlock &MBB,
663 MachineBasicBlock::iterator I,
664 const DebugLoc &DL, unsigned DstReg,
665 ArrayRef<MachineOperand> Pred,
666 unsigned TrueReg,
667 unsigned FalseReg) const {
668 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
669 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
670
671 assert(Pred.size() == 2 && "Invalid condition");
672 unsigned CCValid = Pred[0].getImm();
673 unsigned CCMask = Pred[1].getImm();
674
675 unsigned Opc;
676 if (SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) {
677 if (STI.hasLoadStoreOnCond2())
678 Opc = SystemZ::LOCRMux;
679 else {
680 Opc = SystemZ::LOCR;
681 MRI.constrainRegClass(DstReg, &SystemZ::GR32BitRegClass);
682 }
683 } else if (SystemZ::GR64BitRegClass.hasSubClassEq(RC))
684 Opc = SystemZ::LOCGR;
685 else
686 llvm_unreachable("Invalid register class");
687
688 BuildMI(MBB, I, DL, get(Opc), DstReg)
689 .addReg(FalseReg).addReg(TrueReg)
690 .addImm(CCValid).addImm(CCMask);
691}
692
693bool SystemZInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
694 unsigned Reg,
695 MachineRegisterInfo *MRI) const {
696 unsigned DefOpc = DefMI.getOpcode();
697 if (DefOpc != SystemZ::LHIMux && DefOpc != SystemZ::LHI &&
698 DefOpc != SystemZ::LGHI)
699 return false;
700 if (DefMI.getOperand(0).getReg() != Reg)
701 return false;
702 int32_t ImmVal = (int32_t)DefMI.getOperand(1).getImm();
703
704 unsigned UseOpc = UseMI.getOpcode();
705 unsigned NewUseOpc;
706 unsigned UseIdx;
707 int CommuteIdx = -1;
708 switch (UseOpc) {
709 case SystemZ::LOCRMux:
710 if (!STI.hasLoadStoreOnCond2())
711 return false;
712 NewUseOpc = SystemZ::LOCHIMux;
713 if (UseMI.getOperand(2).getReg() == Reg)
714 UseIdx = 2;
715 else if (UseMI.getOperand(1).getReg() == Reg)
716 UseIdx = 2, CommuteIdx = 1;
717 else
718 return false;
719 break;
720 case SystemZ::LOCGR:
721 if (!STI.hasLoadStoreOnCond2())
722 return false;
723 NewUseOpc = SystemZ::LOCGHI;
724 if (UseMI.getOperand(2).getReg() == Reg)
725 UseIdx = 2;
726 else if (UseMI.getOperand(1).getReg() == Reg)
727 UseIdx = 2, CommuteIdx = 1;
728 else
729 return false;
730 break;
731 default:
732 return false;
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000733 }
Ulrich Weigand524f2762016-11-28 13:34:08 +0000734
735 if (CommuteIdx != -1)
736 if (!commuteInstruction(UseMI, false, CommuteIdx, UseIdx))
737 return false;
738
739 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
740 UseMI.setDesc(get(NewUseOpc));
741 UseMI.getOperand(UseIdx).ChangeToImmediate(ImmVal);
742 if (DeleteDef)
743 DefMI.eraseFromParent();
744
745 return true;
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000746}
747
Krzysztof Parzyszekcc318712017-03-03 18:30:54 +0000748bool SystemZInstrInfo::isPredicable(const MachineInstr &MI) const {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000749 unsigned Opcode = MI.getOpcode();
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000750 if (Opcode == SystemZ::Return ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000751 Opcode == SystemZ::Trap ||
Ulrich Weigand848a5132016-04-11 12:12:32 +0000752 Opcode == SystemZ::CallJG ||
753 Opcode == SystemZ::CallBR)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000754 return true;
755 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000756}
757
758bool SystemZInstrInfo::
759isProfitableToIfCvt(MachineBasicBlock &MBB,
760 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000761 BranchProbability Probability) const {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000762 // Avoid using conditional returns at the end of a loop (since then
763 // we'd need to emit an unconditional branch to the beginning anyway,
764 // making the loop body longer). This doesn't apply for low-probability
765 // loops (eg. compare-and-swap retry), so just decide based on branch
766 // probability instead of looping structure.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000767 // However, since Compare and Trap instructions cost the same as a regular
768 // Compare instruction, we should allow the if conversion to convert this
769 // into a Conditional Compare regardless of the branch probability.
770 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap &&
771 MBB.succ_empty() && Probability < BranchProbability(1, 8))
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000772 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000773 // For now only convert single instructions.
774 return NumCycles == 1;
775}
776
777bool SystemZInstrInfo::
778isProfitableToIfCvt(MachineBasicBlock &TMBB,
779 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
780 MachineBasicBlock &FMBB,
781 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000782 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000783 // For now avoid converting mutually-exclusive cases.
784 return false;
785}
786
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000787bool SystemZInstrInfo::
788isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
789 BranchProbability Probability) const {
790 // For now only duplicate single instructions.
791 return NumCycles == 1;
792}
793
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000794bool SystemZInstrInfo::PredicateInstruction(
795 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000796 assert(Pred.size() == 2 && "Invalid condition");
797 unsigned CCValid = Pred[0].getImm();
798 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000799 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000800 unsigned Opcode = MI.getOpcode();
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000801 if (Opcode == SystemZ::Trap) {
802 MI.setDesc(get(SystemZ::CondTrap));
803 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
804 .addImm(CCValid).addImm(CCMask)
805 .addReg(SystemZ::CC, RegState::Implicit);
806 return true;
807 }
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000808 if (Opcode == SystemZ::Return) {
809 MI.setDesc(get(SystemZ::CondReturn));
810 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
811 .addImm(CCValid).addImm(CCMask)
812 .addReg(SystemZ::CC, RegState::Implicit);
813 return true;
814 }
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000815 if (Opcode == SystemZ::CallJG) {
Zhan Jun Liaua5d60af2016-07-07 15:34:46 +0000816 MachineOperand FirstOp = MI.getOperand(0);
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000817 const uint32_t *RegMask = MI.getOperand(1).getRegMask();
818 MI.RemoveOperand(1);
819 MI.RemoveOperand(0);
820 MI.setDesc(get(SystemZ::CallBRCL));
821 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
Diana Picus116bbab2017-01-13 09:58:52 +0000822 .addImm(CCValid)
823 .addImm(CCMask)
824 .add(FirstOp)
825 .addRegMask(RegMask)
826 .addReg(SystemZ::CC, RegState::Implicit);
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000827 return true;
828 }
Ulrich Weigand848a5132016-04-11 12:12:32 +0000829 if (Opcode == SystemZ::CallBR) {
830 const uint32_t *RegMask = MI.getOperand(0).getRegMask();
831 MI.RemoveOperand(0);
832 MI.setDesc(get(SystemZ::CallBCR));
833 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
834 .addImm(CCValid).addImm(CCMask)
835 .addRegMask(RegMask)
836 .addReg(SystemZ::CC, RegState::Implicit);
837 return true;
838 }
Richard Sandifordf2404162013-07-25 09:11:15 +0000839 return false;
840}
841
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000842void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
843 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000844 const DebugLoc &DL, unsigned DestReg,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000845 unsigned SrcReg, bool KillSrc) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000846 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
847 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000848 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
849 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
850 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
851 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000852 return;
853 }
854
Richard Sandiford0755c932013-10-01 11:26:28 +0000855 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
Jonas Paulssona9bb00d2017-01-18 08:32:54 +0000856 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc,
857 false);
Richard Sandiford0755c932013-10-01 11:26:28 +0000858 return;
859 }
860
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000861 // Everything else needs only one instruction.
862 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000863 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000864 Opcode = SystemZ::LGR;
865 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigandcdce0262016-03-14 13:50:03 +0000866 // For z13 we prefer LDR over LER to avoid partial register dependencies.
867 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000868 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
869 Opcode = SystemZ::LDR;
870 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
871 Opcode = SystemZ::LXR;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000872 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
873 Opcode = SystemZ::VLR32;
874 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
875 Opcode = SystemZ::VLR64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000876 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
877 Opcode = SystemZ::VLR;
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000878 else if (SystemZ::AR32BitRegClass.contains(DestReg, SrcReg))
879 Opcode = SystemZ::CPYA;
880 else if (SystemZ::AR32BitRegClass.contains(DestReg) &&
881 SystemZ::GR32BitRegClass.contains(SrcReg))
882 Opcode = SystemZ::SAR;
883 else if (SystemZ::GR32BitRegClass.contains(DestReg) &&
884 SystemZ::AR32BitRegClass.contains(SrcReg))
885 Opcode = SystemZ::EAR;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000886 else
887 llvm_unreachable("Impossible reg-to-reg copy");
888
889 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
890 .addReg(SrcReg, getKillRegState(KillSrc));
891}
892
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000893void SystemZInstrInfo::storeRegToStackSlot(
894 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
895 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
896 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000897 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
898
899 // Callers may expect a single instruction, so keep 128-bit moves
900 // together for now and lower them after register allocation.
901 unsigned LoadOpcode, StoreOpcode;
902 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
903 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000904 .addReg(SrcReg, getKillRegState(isKill)),
905 FrameIdx);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000906}
907
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000908void SystemZInstrInfo::loadRegFromStackSlot(
909 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
910 int FrameIdx, const TargetRegisterClass *RC,
911 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000912 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
913
914 // Callers may expect a single instruction, so keep 128-bit moves
915 // together for now and lower them after register allocation.
916 unsigned LoadOpcode, StoreOpcode;
917 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
918 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
919 FrameIdx);
920}
921
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000922// Return true if MI is a simple load or store with a 12-bit displacement
923// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
924static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
925 const MCInstrDesc &MCID = MI->getDesc();
926 return ((MCID.TSFlags & Flag) &&
927 isUInt<12>(MI->getOperand(2).getImm()) &&
928 MI->getOperand(3).getReg() == 0);
929}
930
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000931namespace {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000932
Richard Sandifordc2312692014-03-06 10:38:30 +0000933struct LogicOp {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000934 LogicOp() = default;
Richard Sandifordc2312692014-03-06 10:38:30 +0000935 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
936 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000937
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000938 explicit operator bool() const { return RegSize; }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000939
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000940 unsigned RegSize = 0;
941 unsigned ImmLSB = 0;
942 unsigned ImmSize = 0;
Richard Sandifordc2312692014-03-06 10:38:30 +0000943};
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000944
Richard Sandifordc2312692014-03-06 10:38:30 +0000945} // end anonymous namespace
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000946
947static LogicOp interpretAndImmediate(unsigned Opcode) {
948 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000949 case SystemZ::NILMux: return LogicOp(32, 0, 16);
950 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000951 case SystemZ::NILL64: return LogicOp(64, 0, 16);
952 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000953 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
954 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
955 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000956 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000957 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000958 default: return LogicOp();
959 }
960}
961
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000962static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) {
963 if (OldMI->registerDefIsDead(SystemZ::CC)) {
964 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC);
965 if (CCDef != nullptr)
966 CCDef->setIsDead(true);
967 }
968}
969
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000970// Used to return from convertToThreeAddress after replacing two-address
971// instruction OldMI with three-address instruction NewMI.
972static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
973 MachineInstr *NewMI,
974 LiveVariables *LV) {
975 if (LV) {
976 unsigned NumOps = OldMI->getNumOperands();
977 for (unsigned I = 1; I < NumOps; ++I) {
978 MachineOperand &Op = OldMI->getOperand(I);
979 if (Op.isReg() && Op.isKill())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000980 LV->replaceKillInstruction(Op.getReg(), *OldMI, *NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000981 }
982 }
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000983 transferDeadCC(OldMI, NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000984 return NewMI;
985}
986
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000987MachineInstr *SystemZInstrInfo::convertToThreeAddress(
988 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
989 MachineBasicBlock *MBB = MI.getParent();
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000990 MachineFunction *MF = MBB->getParent();
991 MachineRegisterInfo &MRI = MF->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000992
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000993 unsigned Opcode = MI.getOpcode();
994 unsigned NumOps = MI.getNumOperands();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000995
996 // Try to convert something like SLL into SLLK, if supported.
997 // We prefer to keep the two-operand form where possible both
998 // because it tends to be shorter and because some instructions
999 // have memory forms that can be used during spilling.
Eric Christopher673b3af2014-06-27 07:01:17 +00001000 if (STI.hasDistinctOps()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001001 MachineOperand &Dest = MI.getOperand(0);
1002 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford42a694f2013-10-01 14:53:46 +00001003 unsigned DestReg = Dest.getReg();
1004 unsigned SrcReg = Src.getReg();
1005 // AHIMux is only really a three-operand instruction when both operands
1006 // are low registers. Try to constrain both operands to be low if
1007 // possible.
1008 if (Opcode == SystemZ::AHIMux &&
1009 TargetRegisterInfo::isVirtualRegister(DestReg) &&
1010 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
1011 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
1012 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
1013 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
1014 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
1015 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001016 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
1017 if (ThreeOperandOpcode >= 0) {
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +00001018 // Create three address instruction without adding the implicit
1019 // operands. Those will instead be copied over from the original
1020 // instruction by the loop below.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001021 MachineInstrBuilder MIB(
1022 *MF, MF->CreateMachineInstr(get(ThreeOperandOpcode), MI.getDebugLoc(),
1023 /*NoImplicit=*/true));
Diana Picus116bbab2017-01-13 09:58:52 +00001024 MIB.add(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001025 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001026 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001027 // Keep the remaining operands as-is.
1028 for (unsigned I = 2; I < NumOps; ++I)
Diana Picus116bbab2017-01-13 09:58:52 +00001029 MIB.add(MI.getOperand(I));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +00001030 MBB->insert(MI, MIB);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001031 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001032 }
1033 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001034
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001035 // Try to convert an AND into an RISBG-type instruction.
1036 if (LogicOp And = interpretAndImmediate(Opcode)) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001037 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB;
Richard Sandiford70284282013-10-01 14:20:41 +00001038 // AND IMMEDIATE leaves the other bits of the register unchanged.
1039 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
1040 unsigned Start, End;
1041 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
1042 unsigned NewOpcode;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001043 if (And.RegSize == 64) {
Richard Sandiford70284282013-10-01 14:20:41 +00001044 NewOpcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001045 // Prefer RISBGN if available, since it does not clobber CC.
1046 if (STI.hasMiscellaneousExtensions())
1047 NewOpcode = SystemZ::RISBGN;
1048 } else {
Richard Sandiford70284282013-10-01 14:20:41 +00001049 NewOpcode = SystemZ::RISBMux;
1050 Start &= 31;
1051 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001052 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001053 MachineOperand &Dest = MI.getOperand(0);
1054 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford70284282013-10-01 14:20:41 +00001055 MachineInstrBuilder MIB =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001056 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00001057 .add(Dest)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001058 .addReg(0)
1059 .addReg(Src.getReg(), getKillRegState(Src.isKill()),
1060 Src.getSubReg())
1061 .addImm(Start)
1062 .addImm(End + 128)
1063 .addImm(0);
1064 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001065 }
1066 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001067 return nullptr;
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001068}
1069
Keno Fischere70b31f2015-06-08 20:09:58 +00001070MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001071 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001072 MachineBasicBlock::iterator InsertPt, int FrameIndex,
1073 LiveIntervals *LIS) const {
1074 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001075 const MachineFrameInfo &MFI = MF.getFrameInfo();
1076 unsigned Size = MFI.getObjectSize(FrameIndex);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001077 unsigned Opcode = MI.getOpcode();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001078
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001079 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001080 if (LIS != nullptr && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
1081 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) {
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001082
1083 // Check CC liveness, since new instruction introduces a dead
1084 // def of CC.
1085 MCRegUnitIterator CCUnit(SystemZ::CC, TRI);
1086 LiveRange &CCLiveRange = LIS->getRegUnit(*CCUnit);
1087 ++CCUnit;
Eugene Zelenko3943d2b2017-01-24 22:10:43 +00001088 assert(!CCUnit.isValid() && "CC only has one reg unit.");
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001089 SlotIndex MISlot =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001090 LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot();
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001091 if (!CCLiveRange.liveAt(MISlot)) {
1092 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001093 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt,
1094 MI.getDebugLoc(), get(SystemZ::AGSI))
1095 .addFrameIndex(FrameIndex)
1096 .addImm(0)
1097 .addImm(MI.getOperand(2).getImm());
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001098 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true);
1099 CCLiveRange.createDeadDef(MISlot, LIS->getVNInfoAllocator());
1100 return BuiltMI;
1101 }
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001102 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001103 return nullptr;
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001104 }
1105
1106 // All other cases require a single operand.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001107 if (Ops.size() != 1)
Craig Topper062a2ba2014-04-25 05:30:21 +00001108 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001109
1110 unsigned OpNum = Ops[0];
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001111 assert(Size ==
1112 MF.getRegInfo()
1113 .getRegClass(MI.getOperand(OpNum).getReg())
1114 ->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +00001115 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001116
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001117 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 &&
1118 isInt<8>(MI.getOperand(2).getImm())) {
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001119 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
1120 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +00001121 MachineInstr *BuiltMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001122 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
1123 .addFrameIndex(FrameIndex)
1124 .addImm(0)
1125 .addImm(MI.getOperand(2).getImm());
1126 transferDeadCC(&MI, BuiltMI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +00001127 return BuiltMI;
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001128 }
1129
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001130 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
1131 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
1132 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
1133 // If we're spilling the destination of an LDGR or LGDR, store the
1134 // source register instead.
1135 if (OpNum == 0) {
1136 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001137 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001138 get(StoreOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00001139 .add(MI.getOperand(1))
Keno Fischere70b31f2015-06-08 20:09:58 +00001140 .addFrameIndex(FrameIndex)
1141 .addImm(0)
1142 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001143 }
1144 // If we're spilling the source of an LDGR or LGDR, load the
1145 // destination register instead.
1146 if (OpNum == 1) {
1147 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001148 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Jonas Paulssonbd654212017-03-21 05:49:40 +00001149 get(LoadOpcode))
1150 .add(MI.getOperand(0))
1151 .addFrameIndex(FrameIndex)
1152 .addImm(0)
1153 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001154 }
1155 }
1156
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001157 // Look for cases where the source of a simple store or the destination
1158 // of a simple load is being spilled. Try to use MVC instead.
1159 //
1160 // Although MVC is in practice a fast choice in these cases, it is still
1161 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +00001162 // load or store is volatile. We also wouldn't be able to use MVC if
1163 // the two memories partially overlap, but that case cannot occur here,
1164 // because we know that one of the memories is a full frame index.
1165 //
1166 // For performance reasons, we also want to avoid using MVC if the addresses
1167 // might be equal. We don't worry about that case here, because spill slot
1168 // coloring happens later, and because we have special code to remove
1169 // MVCs that turn out to be redundant.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001170 if (OpNum == 0 && MI.hasOneMemOperand()) {
1171 MachineMemOperand *MMO = *MI.memoperands_begin();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001172 if (MMO->getSize() == Size && !MMO->isVolatile()) {
1173 // Handle conversion of loads.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001174 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) {
1175 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001176 get(SystemZ::MVC))
1177 .addFrameIndex(FrameIndex)
1178 .addImm(0)
1179 .addImm(Size)
Diana Picus116bbab2017-01-13 09:58:52 +00001180 .add(MI.getOperand(1))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001181 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +00001182 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001183 }
1184 // Handle conversion of stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001185 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) {
1186 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001187 get(SystemZ::MVC))
Diana Picus116bbab2017-01-13 09:58:52 +00001188 .add(MI.getOperand(1))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001189 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +00001190 .addImm(Size)
1191 .addFrameIndex(FrameIndex)
1192 .addImm(0)
1193 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001194 }
1195 }
1196 }
1197
Richard Sandiforded1fab62013-07-03 10:10:02 +00001198 // If the spilled operand is the final one, try to change <INSN>R
1199 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001200 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001201 if (MemOpcode >= 0) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001202 unsigned NumOps = MI.getNumExplicitOperands();
Richard Sandiforded1fab62013-07-03 10:10:02 +00001203 if (OpNum == NumOps - 1) {
1204 const MCInstrDesc &MemDesc = get(MemOpcode);
1205 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
1206 assert(AccessBytes != 0 && "Size of access should be known");
1207 assert(AccessBytes <= Size && "Access outside the frame index");
1208 uint64_t Offset = Size - AccessBytes;
Keno Fischere70b31f2015-06-08 20:09:58 +00001209 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001210 MI.getDebugLoc(), get(MemOpcode));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001211 for (unsigned I = 0; I < OpNum; ++I)
Diana Picus116bbab2017-01-13 09:58:52 +00001212 MIB.add(MI.getOperand(I));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001213 MIB.addFrameIndex(FrameIndex).addImm(Offset);
1214 if (MemDesc.TSFlags & SystemZII::HasIndex)
1215 MIB.addReg(0);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001216 transferDeadCC(&MI, MIB);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001217 return MIB;
1218 }
1219 }
1220
Craig Topper062a2ba2014-04-25 05:30:21 +00001221 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001222}
1223
Keno Fischere70b31f2015-06-08 20:09:58 +00001224MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001225 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
1226 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001227 LiveIntervals *LIS) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001228 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001229}
1230
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001231bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1232 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001233 case SystemZ::L128:
1234 splitMove(MI, SystemZ::LG);
1235 return true;
1236
1237 case SystemZ::ST128:
1238 splitMove(MI, SystemZ::STG);
1239 return true;
1240
1241 case SystemZ::LX:
1242 splitMove(MI, SystemZ::LD);
1243 return true;
1244
1245 case SystemZ::STX:
1246 splitMove(MI, SystemZ::STD);
1247 return true;
1248
Richard Sandiford89e160d2013-10-01 12:11:47 +00001249 case SystemZ::LBMux:
1250 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
1251 return true;
1252
1253 case SystemZ::LHMux:
1254 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
1255 return true;
1256
Richard Sandiford21235a22013-10-01 12:49:07 +00001257 case SystemZ::LLCRMux:
1258 expandZExtPseudo(MI, SystemZ::LLCR, 8);
1259 return true;
1260
1261 case SystemZ::LLHRMux:
1262 expandZExtPseudo(MI, SystemZ::LLHR, 16);
1263 return true;
1264
Richard Sandiford0d46b1a2013-10-01 12:19:08 +00001265 case SystemZ::LLCMux:
1266 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
1267 return true;
1268
1269 case SystemZ::LLHMux:
1270 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
1271 return true;
1272
Richard Sandiford0755c932013-10-01 11:26:28 +00001273 case SystemZ::LMux:
1274 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
1275 return true;
1276
Ulrich Weigand524f2762016-11-28 13:34:08 +00001277 case SystemZ::LOCMux:
1278 expandLOCPseudo(MI, SystemZ::LOC, SystemZ::LOCFH);
1279 return true;
1280
1281 case SystemZ::LOCHIMux:
1282 expandLOCPseudo(MI, SystemZ::LOCHI, SystemZ::LOCHHI);
1283 return true;
1284
1285 case SystemZ::LOCRMux:
1286 expandLOCRPseudo(MI, SystemZ::LOCR, SystemZ::LOCFHR);
1287 return true;
1288
Richard Sandiford5469c392013-10-01 12:22:49 +00001289 case SystemZ::STCMux:
1290 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
1291 return true;
1292
1293 case SystemZ::STHMux:
1294 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
1295 return true;
1296
Richard Sandiford0755c932013-10-01 11:26:28 +00001297 case SystemZ::STMux:
1298 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
1299 return true;
1300
Ulrich Weigand524f2762016-11-28 13:34:08 +00001301 case SystemZ::STOCMux:
1302 expandLOCPseudo(MI, SystemZ::STOC, SystemZ::STOCFH);
1303 return true;
1304
Richard Sandiford01240232013-10-01 13:02:28 +00001305 case SystemZ::LHIMux:
1306 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
1307 return true;
1308
1309 case SystemZ::IIFMux:
1310 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
1311 return true;
1312
Richard Sandiford1a569312013-10-01 13:18:56 +00001313 case SystemZ::IILMux:
1314 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
1315 return true;
1316
1317 case SystemZ::IIHMux:
1318 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
1319 return true;
1320
Richard Sandiford70284282013-10-01 14:20:41 +00001321 case SystemZ::NIFMux:
1322 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1323 return true;
1324
1325 case SystemZ::NILMux:
1326 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1327 return true;
1328
1329 case SystemZ::NIHMux:
1330 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1331 return true;
1332
Richard Sandiford6e96ac62013-10-01 13:22:41 +00001333 case SystemZ::OIFMux:
1334 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1335 return true;
1336
1337 case SystemZ::OILMux:
1338 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1339 return true;
1340
1341 case SystemZ::OIHMux:
1342 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1343 return true;
1344
Richard Sandiford5718dac2013-10-01 14:08:44 +00001345 case SystemZ::XIFMux:
1346 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1347 return true;
1348
Richard Sandiford2cac7632013-10-01 14:41:52 +00001349 case SystemZ::TMLMux:
1350 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1351 return true;
1352
1353 case SystemZ::TMHMux:
1354 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1355 return true;
1356
Richard Sandiford42a694f2013-10-01 14:53:46 +00001357 case SystemZ::AHIMux:
1358 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1359 return true;
1360
1361 case SystemZ::AHIMuxK:
1362 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1363 return true;
1364
1365 case SystemZ::AFIMux:
1366 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1367 return true;
1368
Ulrich Weigand75839912016-11-28 13:40:08 +00001369 case SystemZ::CHIMux:
1370 expandRIPseudo(MI, SystemZ::CHI, SystemZ::CIH, false);
1371 return true;
1372
Richard Sandiforda9ac0e02013-10-01 14:56:23 +00001373 case SystemZ::CFIMux:
1374 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1375 return true;
1376
1377 case SystemZ::CLFIMux:
1378 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1379 return true;
1380
Richard Sandifordb63e3002013-10-01 15:00:44 +00001381 case SystemZ::CMux:
1382 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1383 return true;
1384
1385 case SystemZ::CLMux:
1386 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1387 return true;
1388
Richard Sandiford70284282013-10-01 14:20:41 +00001389 case SystemZ::RISBMux: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001390 bool DestIsHigh = isHighReg(MI.getOperand(0).getReg());
1391 bool SrcIsHigh = isHighReg(MI.getOperand(2).getReg());
Richard Sandiford70284282013-10-01 14:20:41 +00001392 if (SrcIsHigh == DestIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001393 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
Richard Sandiford70284282013-10-01 14:20:41 +00001394 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001395 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1396 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32);
Richard Sandiford70284282013-10-01 14:20:41 +00001397 }
1398 return true;
1399 }
1400
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001401 case SystemZ::ADJDYNALLOC:
1402 splitAdjDynAlloc(MI);
1403 return true;
1404
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001405 case TargetOpcode::LOAD_STACK_GUARD:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001406 expandLoadStackGuard(&MI);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001407 return true;
1408
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001409 default:
1410 return false;
1411 }
1412}
1413
Sjoerd Meijer0eb96ed2016-07-29 08:16:16 +00001414unsigned SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001415 if (MI.getOpcode() == TargetOpcode::INLINEASM) {
1416 const MachineFunction *MF = MI.getParent()->getParent();
1417 const char *AsmStr = MI.getOperand(0).getSymbolName();
Richard Sandiford312425f2013-05-20 14:23:08 +00001418 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1419 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001420 return MI.getDesc().getSize();
Richard Sandiford312425f2013-05-20 14:23:08 +00001421}
1422
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001423SystemZII::Branch
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001424SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const {
1425 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001426 case SystemZ::BR:
1427 case SystemZ::J:
1428 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001429 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001430 SystemZ::CCMASK_ANY, &MI.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001431
1432 case SystemZ::BRC:
1433 case SystemZ::BRCL:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001434 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(),
1435 MI.getOperand(1).getImm(), &MI.getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001436
Richard Sandifordc2121252013-08-05 11:23:46 +00001437 case SystemZ::BRCT:
Ulrich Weigand75839912016-11-28 13:40:08 +00001438 case SystemZ::BRCTH:
Richard Sandifordc2121252013-08-05 11:23:46 +00001439 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001440 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001441
1442 case SystemZ::BRCTG:
1443 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001444 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001445
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001446 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001447 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001448 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001449 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001450
Richard Sandiford93183ee2013-09-18 09:56:40 +00001451 case SystemZ::CLIJ:
1452 case SystemZ::CLRJ:
1453 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001454 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001455
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001456 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001457 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001458 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001459 MI.getOperand(2).getImm(), &MI.getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001460
Richard Sandiford93183ee2013-09-18 09:56:40 +00001461 case SystemZ::CLGIJ:
1462 case SystemZ::CLGRJ:
1463 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001464 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001465
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001466 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001467 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001468 }
1469}
1470
1471void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1472 unsigned &LoadOpcode,
1473 unsigned &StoreOpcode) const {
1474 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1475 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001476 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001477 } else if (RC == &SystemZ::GRH32BitRegClass) {
1478 LoadOpcode = SystemZ::LFH;
1479 StoreOpcode = SystemZ::STFH;
1480 } else if (RC == &SystemZ::GRX32BitRegClass) {
1481 LoadOpcode = SystemZ::LMux;
1482 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001483 } else if (RC == &SystemZ::GR64BitRegClass ||
1484 RC == &SystemZ::ADDR64BitRegClass) {
1485 LoadOpcode = SystemZ::LG;
1486 StoreOpcode = SystemZ::STG;
1487 } else if (RC == &SystemZ::GR128BitRegClass ||
1488 RC == &SystemZ::ADDR128BitRegClass) {
1489 LoadOpcode = SystemZ::L128;
1490 StoreOpcode = SystemZ::ST128;
1491 } else if (RC == &SystemZ::FP32BitRegClass) {
1492 LoadOpcode = SystemZ::LE;
1493 StoreOpcode = SystemZ::STE;
1494 } else if (RC == &SystemZ::FP64BitRegClass) {
1495 LoadOpcode = SystemZ::LD;
1496 StoreOpcode = SystemZ::STD;
1497 } else if (RC == &SystemZ::FP128BitRegClass) {
1498 LoadOpcode = SystemZ::LX;
1499 StoreOpcode = SystemZ::STX;
Ulrich Weigand49506d72015-05-05 19:28:34 +00001500 } else if (RC == &SystemZ::VR32BitRegClass) {
1501 LoadOpcode = SystemZ::VL32;
1502 StoreOpcode = SystemZ::VST32;
1503 } else if (RC == &SystemZ::VR64BitRegClass) {
1504 LoadOpcode = SystemZ::VL64;
1505 StoreOpcode = SystemZ::VST64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001506 } else if (RC == &SystemZ::VF128BitRegClass ||
1507 RC == &SystemZ::VR128BitRegClass) {
1508 LoadOpcode = SystemZ::VL;
1509 StoreOpcode = SystemZ::VST;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001510 } else
1511 llvm_unreachable("Unsupported regclass to load or store");
1512}
1513
1514unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1515 int64_t Offset) const {
1516 const MCInstrDesc &MCID = get(Opcode);
1517 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1518 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1519 // Get the instruction to use for unsigned 12-bit displacements.
1520 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1521 if (Disp12Opcode >= 0)
1522 return Disp12Opcode;
1523
1524 // All address-related instructions can use unsigned 12-bit
1525 // displacements.
1526 return Opcode;
1527 }
1528 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1529 // Get the instruction to use for signed 20-bit displacements.
1530 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1531 if (Disp20Opcode >= 0)
1532 return Disp20Opcode;
1533
1534 // Check whether Opcode allows signed 20-bit displacements.
1535 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1536 return Opcode;
1537 }
1538 return 0;
1539}
1540
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001541unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1542 switch (Opcode) {
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001543 case SystemZ::L: return SystemZ::LT;
1544 case SystemZ::LY: return SystemZ::LT;
1545 case SystemZ::LG: return SystemZ::LTG;
1546 case SystemZ::LGF: return SystemZ::LTGF;
1547 case SystemZ::LR: return SystemZ::LTR;
1548 case SystemZ::LGFR: return SystemZ::LTGFR;
1549 case SystemZ::LGR: return SystemZ::LTGR;
1550 case SystemZ::LER: return SystemZ::LTEBR;
1551 case SystemZ::LDR: return SystemZ::LTDBR;
1552 case SystemZ::LXR: return SystemZ::LTXBR;
Jonas Paulsson12629322015-10-01 18:12:28 +00001553 case SystemZ::LCDFR: return SystemZ::LCDBR;
1554 case SystemZ::LPDFR: return SystemZ::LPDBR;
1555 case SystemZ::LNDFR: return SystemZ::LNDBR;
1556 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1557 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1558 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001559 // On zEC12 we prefer to use RISBGN. But if there is a chance to
1560 // actually use the condition code, we may turn it back into RISGB.
1561 // Note that RISBG is not really a "load-and-test" instruction,
1562 // but sets the same condition code values, so is OK to use here.
1563 case SystemZ::RISBGN: return SystemZ::RISBG;
1564 default: return 0;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001565 }
1566}
1567
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001568// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1569// have already been filtered out. Store the first set bit in LSB and
1570// the number of set bits in Length if so.
1571static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1572 unsigned First = findFirstSet(Mask);
1573 uint64_t Top = (Mask >> First) + 1;
1574 if ((Top & -Top) == Top) {
1575 LSB = First;
1576 Length = findFirstSet(Top);
1577 return true;
1578 }
1579 return false;
1580}
1581
1582bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1583 unsigned &Start, unsigned &End) const {
1584 // Reject trivial all-zero masks.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001585 Mask &= allOnes(BitSize);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001586 if (Mask == 0)
1587 return false;
1588
1589 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1590 // the msb and End specifies the index of the lsb.
1591 unsigned LSB, Length;
1592 if (isStringOfOnes(Mask, LSB, Length)) {
1593 Start = 63 - (LSB + Length - 1);
1594 End = 63 - LSB;
1595 return true;
1596 }
1597
1598 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1599 // of the low 1s and End specifies the lsb of the high 1s.
1600 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1601 assert(LSB > 0 && "Bottom bit must be set");
1602 assert(LSB + Length < BitSize && "Top bit must be set");
1603 Start = 63 - (LSB - 1);
1604 End = 63 - (LSB + Length);
1605 return true;
1606 }
1607
1608 return false;
1609}
1610
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001611unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode,
1612 SystemZII::FusedCompareType Type,
1613 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001614 switch (Opcode) {
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001615 case SystemZ::CHI:
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001616 case SystemZ::CGHI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001617 if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
1618 return 0;
1619 break;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001620 case SystemZ::CLFI:
Richard Sandiford93183ee2013-09-18 09:56:40 +00001621 case SystemZ::CLGFI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001622 if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
1623 return 0;
Ulrich Weiganda0e73252016-11-11 12:48:26 +00001624 break;
1625 case SystemZ::CL:
1626 case SystemZ::CLG:
1627 if (!STI.hasMiscellaneousExtensions())
1628 return 0;
1629 if (!(MI && MI->getOperand(3).getReg() == 0))
1630 return 0;
1631 break;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001632 }
1633 switch (Type) {
1634 case SystemZII::CompareAndBranch:
1635 switch (Opcode) {
1636 case SystemZ::CR:
1637 return SystemZ::CRJ;
1638 case SystemZ::CGR:
1639 return SystemZ::CGRJ;
1640 case SystemZ::CHI:
1641 return SystemZ::CIJ;
1642 case SystemZ::CGHI:
1643 return SystemZ::CGIJ;
1644 case SystemZ::CLR:
1645 return SystemZ::CLRJ;
1646 case SystemZ::CLGR:
1647 return SystemZ::CLGRJ;
1648 case SystemZ::CLFI:
1649 return SystemZ::CLIJ;
1650 case SystemZ::CLGFI:
1651 return SystemZ::CLGIJ;
1652 default:
1653 return 0;
1654 }
1655 case SystemZII::CompareAndReturn:
1656 switch (Opcode) {
1657 case SystemZ::CR:
1658 return SystemZ::CRBReturn;
1659 case SystemZ::CGR:
1660 return SystemZ::CGRBReturn;
1661 case SystemZ::CHI:
1662 return SystemZ::CIBReturn;
1663 case SystemZ::CGHI:
1664 return SystemZ::CGIBReturn;
1665 case SystemZ::CLR:
1666 return SystemZ::CLRBReturn;
1667 case SystemZ::CLGR:
1668 return SystemZ::CLGRBReturn;
1669 case SystemZ::CLFI:
1670 return SystemZ::CLIBReturn;
1671 case SystemZ::CLGFI:
1672 return SystemZ::CLGIBReturn;
1673 default:
1674 return 0;
1675 }
Ulrich Weigand848a5132016-04-11 12:12:32 +00001676 case SystemZII::CompareAndSibcall:
1677 switch (Opcode) {
1678 case SystemZ::CR:
1679 return SystemZ::CRBCall;
1680 case SystemZ::CGR:
1681 return SystemZ::CGRBCall;
1682 case SystemZ::CHI:
1683 return SystemZ::CIBCall;
1684 case SystemZ::CGHI:
1685 return SystemZ::CGIBCall;
1686 case SystemZ::CLR:
1687 return SystemZ::CLRBCall;
1688 case SystemZ::CLGR:
1689 return SystemZ::CLGRBCall;
1690 case SystemZ::CLFI:
1691 return SystemZ::CLIBCall;
1692 case SystemZ::CLGFI:
1693 return SystemZ::CLGIBCall;
1694 default:
1695 return 0;
1696 }
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001697 case SystemZII::CompareAndTrap:
1698 switch (Opcode) {
1699 case SystemZ::CR:
1700 return SystemZ::CRT;
1701 case SystemZ::CGR:
1702 return SystemZ::CGRT;
1703 case SystemZ::CHI:
1704 return SystemZ::CIT;
1705 case SystemZ::CGHI:
1706 return SystemZ::CGIT;
1707 case SystemZ::CLR:
1708 return SystemZ::CLRT;
1709 case SystemZ::CLGR:
1710 return SystemZ::CLGRT;
1711 case SystemZ::CLFI:
1712 return SystemZ::CLFIT;
1713 case SystemZ::CLGFI:
1714 return SystemZ::CLGIT;
Ulrich Weiganda0e73252016-11-11 12:48:26 +00001715 case SystemZ::CL:
1716 return SystemZ::CLT;
1717 case SystemZ::CLG:
1718 return SystemZ::CLGT;
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001719 default:
1720 return 0;
1721 }
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001722 }
Ulrich Weigand79391ee2016-04-07 16:33:25 +00001723 return 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001724}
1725
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +00001726unsigned SystemZInstrInfo::getLoadAndTrap(unsigned Opcode) const {
1727 if (!STI.hasLoadAndTrap())
1728 return 0;
1729 switch (Opcode) {
1730 case SystemZ::L:
1731 case SystemZ::LY:
1732 return SystemZ::LAT;
1733 case SystemZ::LG:
1734 return SystemZ::LGAT;
1735 case SystemZ::LFH:
1736 return SystemZ::LFHAT;
1737 case SystemZ::LLGF:
1738 return SystemZ::LLGFAT;
1739 case SystemZ::LLGT:
1740 return SystemZ::LLGTAT;
1741 }
1742 return 0;
1743}
1744
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001745void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1746 MachineBasicBlock::iterator MBBI,
1747 unsigned Reg, uint64_t Value) const {
1748 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1749 unsigned Opcode;
1750 if (isInt<16>(Value))
1751 Opcode = SystemZ::LGHI;
1752 else if (SystemZ::isImmLL(Value))
1753 Opcode = SystemZ::LLILL;
1754 else if (SystemZ::isImmLH(Value)) {
1755 Opcode = SystemZ::LLILH;
1756 Value >>= 16;
1757 } else {
1758 assert(isInt<32>(Value) && "Huge values not handled yet");
1759 Opcode = SystemZ::LGFI;
1760 }
1761 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1762}
Jonas Paulsson8010b632016-10-20 08:27:16 +00001763
1764bool SystemZInstrInfo::
1765areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb,
1766 AliasAnalysis *AA) const {
1767
1768 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand())
1769 return false;
1770
1771 // If mem-operands show that the same address Value is used by both
1772 // instructions, check for non-overlapping offsets and widths. Not
1773 // sure if a register based analysis would be an improvement...
1774
1775 MachineMemOperand *MMOa = *MIa.memoperands_begin();
1776 MachineMemOperand *MMOb = *MIb.memoperands_begin();
1777 const Value *VALa = MMOa->getValue();
1778 const Value *VALb = MMOb->getValue();
1779 bool SameVal = (VALa && VALb && (VALa == VALb));
1780 if (!SameVal) {
1781 const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
1782 const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
1783 if (PSVa && PSVb && (PSVa == PSVb))
1784 SameVal = true;
1785 }
1786 if (SameVal) {
1787 int OffsetA = MMOa->getOffset(), OffsetB = MMOb->getOffset();
1788 int WidthA = MMOa->getSize(), WidthB = MMOb->getSize();
1789 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
1790 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
1791 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
1792 if (LowOffset + LowWidth <= HighOffset)
1793 return true;
1794 }
1795
1796 return false;
1797}