blob: 3565d5f2c49cef10950d341922b10d7413d2a50e [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
14#include "SystemZInstrInfo.h"
15#include "SystemZInstrBuilder.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000016#include "SystemZTargetMachine.h"
Richard Sandifordff6c5a52013-07-19 16:12:08 +000017#include "llvm/CodeGen/LiveVariables.h"
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +000018#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Richard Sandifordf6bae1e2013-07-02 15:28:56 +000019#include "llvm/CodeGen/MachineRegisterInfo.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000020
Chandler Carruthd174b722014-04-22 02:03:14 +000021using namespace llvm;
22
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000023#define GET_INSTRINFO_CTOR_DTOR
Ulrich Weigand5f613df2013-05-06 16:15:19 +000024#define GET_INSTRMAP_INFO
25#include "SystemZGenInstrInfo.inc"
26
Richard Sandiford6a06ba32013-07-31 11:36:35 +000027// Return a mask with Count low bits set.
28static uint64_t allOnes(unsigned int Count) {
29 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
30}
31
Richard Sandiford0755c932013-10-01 11:26:28 +000032// Reg should be a 32-bit GPR. Return true if it is a high register rather
33// than a low register.
34static bool isHighReg(unsigned int Reg) {
35 if (SystemZ::GRH32BitRegClass.contains(Reg))
36 return true;
37 assert(SystemZ::GR32BitRegClass.contains(Reg) && "Invalid GRX32");
38 return false;
39}
40
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000041// Pin the vtable to this file.
42void SystemZInstrInfo::anchor() {}
43
Eric Christopher673b3af2014-06-27 07:01:17 +000044SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti)
Ulrich Weigand5f613df2013-05-06 16:15:19 +000045 : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
Eric Christopher673b3af2014-06-27 07:01:17 +000046 RI(), STI(sti) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +000047}
48
49// MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
50// each having the opcode given by NewOpcode.
51void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
52 unsigned NewOpcode) const {
53 MachineBasicBlock *MBB = MI->getParent();
54 MachineFunction &MF = *MBB->getParent();
55
56 // Get two load or store instructions. Use the original instruction for one
Alp Tokercb402912014-01-24 17:20:08 +000057 // of them (arbitrarily the second here) and create a clone for the other.
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +000058 MachineInstr *EarlierMI = MF.CloneMachineInstr(&*MI);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000059 MBB->insert(MI, EarlierMI);
60
61 // Set up the two 64-bit registers.
62 MachineOperand &HighRegOp = EarlierMI->getOperand(0);
63 MachineOperand &LowRegOp = MI->getOperand(0);
Richard Sandiford87a44362013-09-30 10:28:35 +000064 HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64));
65 LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64));
Ulrich Weigand5f613df2013-05-06 16:15:19 +000066
67 // The address in the first (high) instruction is already correct.
68 // Adjust the offset in the second (low) instruction.
69 MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
70 MachineOperand &LowOffsetOp = MI->getOperand(2);
71 LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
72
Jonas Paulsson2ba31522016-03-31 08:00:14 +000073 // Clear the kill flags for the base and index registers in the first
74 // instruction.
Jonas Paulsson63a2b682015-10-10 07:14:24 +000075 EarlierMI->getOperand(1).setIsKill(false);
Jonas Paulsson7da38202015-10-26 15:03:41 +000076 EarlierMI->getOperand(3).setIsKill(false);
Jonas Paulsson63a2b682015-10-10 07:14:24 +000077
Ulrich Weigand5f613df2013-05-06 16:15:19 +000078 // Set the opcodes.
79 unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
80 unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
81 assert(HighOpcode && LowOpcode && "Both offsets should be in range");
82
83 EarlierMI->setDesc(get(HighOpcode));
84 MI->setDesc(get(LowOpcode));
85}
86
87// Split ADJDYNALLOC instruction MI.
88void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
89 MachineBasicBlock *MBB = MI->getParent();
90 MachineFunction &MF = *MBB->getParent();
Matthias Braun941a7052016-07-28 18:40:00 +000091 MachineFrameInfo &MFFrame = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +000092 MachineOperand &OffsetMO = MI->getOperand(2);
93
Matthias Braun941a7052016-07-28 18:40:00 +000094 uint64_t Offset = (MFFrame.getMaxCallFrameSize() +
Ulrich Weigand5f613df2013-05-06 16:15:19 +000095 SystemZMC::CallFrameSize +
96 OffsetMO.getImm());
97 unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
98 assert(NewOpcode && "No support for huge argument lists yet");
99 MI->setDesc(get(NewOpcode));
100 OffsetMO.setImm(Offset);
101}
102
Richard Sandiford01240232013-10-01 13:02:28 +0000103// MI is an RI-style pseudo instruction. Replace it with LowOpcode
104// if the first operand is a low GR32 and HighOpcode if the first operand
105// is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
106// and HighOpcode takes an unsigned 32-bit operand. In those cases,
107// MI has the same kind of operand as LowOpcode, so needs to be converted
108// if HighOpcode is used.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000109void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford01240232013-10-01 13:02:28 +0000110 unsigned HighOpcode,
111 bool ConvertHigh) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000112 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford01240232013-10-01 13:02:28 +0000113 bool IsHigh = isHighReg(Reg);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000114 MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode));
Richard Sandiford01240232013-10-01 13:02:28 +0000115 if (IsHigh && ConvertHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000116 MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm()));
Richard Sandiford01240232013-10-01 13:02:28 +0000117}
118
Richard Sandiford42a694f2013-10-01 14:53:46 +0000119// MI is a three-operand RIE-style pseudo instruction. Replace it with
Jonas Paulsson18d877f2015-10-09 07:19:16 +0000120// LowOpcodeK if the registers are both low GR32s, otherwise use a move
Richard Sandiford42a694f2013-10-01 14:53:46 +0000121// followed by HighOpcode or LowOpcode, depending on whether the target
122// is a high or low GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000123void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford42a694f2013-10-01 14:53:46 +0000124 unsigned LowOpcodeK,
125 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000126 unsigned DestReg = MI.getOperand(0).getReg();
127 unsigned SrcReg = MI.getOperand(1).getReg();
Richard Sandiford42a694f2013-10-01 14:53:46 +0000128 bool DestIsHigh = isHighReg(DestReg);
129 bool SrcIsHigh = isHighReg(SrcReg);
130 if (!DestIsHigh && !SrcIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000131 MI.setDesc(get(LowOpcodeK));
Richard Sandiford42a694f2013-10-01 14:53:46 +0000132 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000133 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg,
134 SystemZ::LR, 32, MI.getOperand(1).isKill());
135 MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
136 MI.getOperand(1).setReg(DestReg);
137 MI.tieOperands(0, 1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000138 }
139}
140
Richard Sandiford0755c932013-10-01 11:26:28 +0000141// MI is an RXY-style pseudo instruction. Replace it with LowOpcode
142// if the first operand is a low GR32 and HighOpcode if the first operand
143// is a high GR32.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000144void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford0755c932013-10-01 11:26:28 +0000145 unsigned HighOpcode) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000146 unsigned Reg = MI.getOperand(0).getReg();
Richard Sandiford0755c932013-10-01 11:26:28 +0000147 unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000148 MI.getOperand(2).getImm());
149 MI.setDesc(get(Opcode));
Richard Sandiford0755c932013-10-01 11:26:28 +0000150}
151
Ulrich Weigand524f2762016-11-28 13:34:08 +0000152// MI is a load-on-condition pseudo instruction with a single register
153// (source or destination) operand. Replace it with LowOpcode if the
154// register is a low GR32 and HighOpcode if the register is a high GR32.
155void SystemZInstrInfo::expandLOCPseudo(MachineInstr &MI, unsigned LowOpcode,
156 unsigned HighOpcode) const {
157 unsigned Reg = MI.getOperand(0).getReg();
158 unsigned Opcode = isHighReg(Reg) ? HighOpcode : LowOpcode;
159 MI.setDesc(get(Opcode));
160}
161
162// MI is a load-register-on-condition pseudo instruction. Replace it with
163// LowOpcode if source and destination are both low GR32s and HighOpcode if
164// source and destination are both high GR32s.
165void SystemZInstrInfo::expandLOCRPseudo(MachineInstr &MI, unsigned LowOpcode,
166 unsigned HighOpcode) const {
167 unsigned DestReg = MI.getOperand(0).getReg();
168 unsigned SrcReg = MI.getOperand(2).getReg();
169 bool DestIsHigh = isHighReg(DestReg);
170 bool SrcIsHigh = isHighReg(SrcReg);
171
172 if (!DestIsHigh && !SrcIsHigh)
173 MI.setDesc(get(LowOpcode));
174 else if (DestIsHigh && SrcIsHigh)
175 MI.setDesc(get(HighOpcode));
176
177 // If we were unable to implement the pseudo with a single instruction, we
178 // need to convert it back into a branch sequence. This cannot be done here
179 // since the caller of expandPostRAPseudo does not handle changes to the CFG
180 // correctly. This change is defered to the SystemZExpandPseudo pass.
181}
182
Richard Sandiford21235a22013-10-01 12:49:07 +0000183// MI is an RR-style pseudo instruction that zero-extends the low Size bits
184// of one GRX32 into another. Replace it with LowOpcode if both operands
185// are low registers, otherwise use RISB[LH]G.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000186void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford21235a22013-10-01 12:49:07 +0000187 unsigned Size) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000188 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(),
189 MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode,
190 Size, MI.getOperand(1).isKill());
191 MI.eraseFromParent();
Richard Sandiford21235a22013-10-01 12:49:07 +0000192}
193
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000194void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const {
195 MachineBasicBlock *MBB = MI->getParent();
196 MachineFunction &MF = *MBB->getParent();
197 const unsigned Reg = MI->getOperand(0).getReg();
198
199 // Conveniently, all 4 instructions are cloned from LOAD_STACK_GUARD,
200 // so they already have operand 0 set to reg.
201
202 // ear <reg>, %a0
203 MachineInstr *Ear1MI = MF.CloneMachineInstr(MI);
204 MBB->insert(MI, Ear1MI);
205 Ear1MI->setDesc(get(SystemZ::EAR));
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000206 MachineInstrBuilder(MF, Ear1MI).addReg(SystemZ::A0);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000207
208 // sllg <reg>, <reg>, 32
209 MachineInstr *SllgMI = MF.CloneMachineInstr(MI);
210 MBB->insert(MI, SllgMI);
211 SllgMI->setDesc(get(SystemZ::SLLG));
212 MachineInstrBuilder(MF, SllgMI).addReg(Reg).addReg(0).addImm(32);
213
214 // ear <reg>, %a1
215 MachineInstr *Ear2MI = MF.CloneMachineInstr(MI);
216 MBB->insert(MI, Ear2MI);
217 Ear2MI->setDesc(get(SystemZ::EAR));
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000218 MachineInstrBuilder(MF, Ear2MI).addReg(SystemZ::A1);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000219
220 // lg <reg>, 40(<reg>)
221 MI->setDesc(get(SystemZ::LG));
222 MachineInstrBuilder(MF, MI).addReg(Reg).addImm(40).addReg(0);
223}
224
Richard Sandiford0755c932013-10-01 11:26:28 +0000225// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
226// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
227// are low registers, otherwise use RISB[LH]G. Size is the number of bits
228// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
229// KillSrc is true if this move is the last use of SrcReg.
230void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
231 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000232 const DebugLoc &DL, unsigned DestReg,
Richard Sandiford0755c932013-10-01 11:26:28 +0000233 unsigned SrcReg, unsigned LowLowOpcode,
234 unsigned Size, bool KillSrc) const {
235 unsigned Opcode;
236 bool DestIsHigh = isHighReg(DestReg);
237 bool SrcIsHigh = isHighReg(SrcReg);
238 if (DestIsHigh && SrcIsHigh)
239 Opcode = SystemZ::RISBHH;
240 else if (DestIsHigh && !SrcIsHigh)
241 Opcode = SystemZ::RISBHL;
242 else if (!DestIsHigh && SrcIsHigh)
243 Opcode = SystemZ::RISBLH;
244 else {
245 BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
246 .addReg(SrcReg, getKillRegState(KillSrc));
247 return;
248 }
249 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
250 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
251 .addReg(DestReg, RegState::Undef)
252 .addReg(SrcReg, getKillRegState(KillSrc))
253 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
254}
255
Ulrich Weigand524f2762016-11-28 13:34:08 +0000256
257MachineInstr *SystemZInstrInfo::commuteInstructionImpl(MachineInstr &MI,
258 bool NewMI,
259 unsigned OpIdx1,
260 unsigned OpIdx2) const {
261 auto cloneIfNew = [NewMI](MachineInstr &MI) -> MachineInstr & {
262 if (NewMI)
263 return *MI.getParent()->getParent()->CloneMachineInstr(&MI);
264 return MI;
265 };
266
267 switch (MI.getOpcode()) {
268 case SystemZ::LOCRMux:
269 case SystemZ::LOCFHR:
270 case SystemZ::LOCR:
271 case SystemZ::LOCGR: {
272 auto &WorkingMI = cloneIfNew(MI);
273 // Invert condition.
274 unsigned CCValid = WorkingMI.getOperand(3).getImm();
275 unsigned CCMask = WorkingMI.getOperand(4).getImm();
276 WorkingMI.getOperand(4).setImm(CCMask ^ CCValid);
277 return TargetInstrInfo::commuteInstructionImpl(WorkingMI, /*NewMI=*/false,
278 OpIdx1, OpIdx2);
279 }
280 default:
281 return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
282 }
283}
284
285
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000286// If MI is a simple load or store for a frame object, return the register
287// it loads or stores and set FrameIndex to the index of the frame object.
288// Return 0 otherwise.
289//
290// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000291static int isSimpleMove(const MachineInstr &MI, int &FrameIndex,
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000292 unsigned Flag) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000293 const MCInstrDesc &MCID = MI.getDesc();
294 if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() &&
295 MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) {
296 FrameIndex = MI.getOperand(1).getIndex();
297 return MI.getOperand(0).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000298 }
299 return 0;
300}
301
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000302unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000303 int &FrameIndex) const {
304 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
305}
306
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000307unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000308 int &FrameIndex) const {
309 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
310}
311
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000312bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI,
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000313 int &DestFrameIndex,
314 int &SrcFrameIndex) const {
315 // Check for MVC 0(Length,FI1),0(FI2)
Matthias Braun941a7052016-07-28 18:40:00 +0000316 const MachineFrameInfo &MFI = MI.getParent()->getParent()->getFrameInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000317 if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() ||
318 MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() ||
319 MI.getOperand(4).getImm() != 0)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000320 return false;
321
322 // Check that Length covers the full slots.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000323 int64_t Length = MI.getOperand(2).getImm();
324 unsigned FI1 = MI.getOperand(0).getIndex();
325 unsigned FI2 = MI.getOperand(3).getIndex();
Matthias Braun941a7052016-07-28 18:40:00 +0000326 if (MFI.getObjectSize(FI1) != Length ||
327 MFI.getObjectSize(FI2) != Length)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000328 return false;
329
330 DestFrameIndex = FI1;
331 SrcFrameIndex = FI2;
332 return true;
333}
334
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000335bool SystemZInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000336 MachineBasicBlock *&TBB,
337 MachineBasicBlock *&FBB,
338 SmallVectorImpl<MachineOperand> &Cond,
339 bool AllowModify) const {
340 // Most of the code and comments here are boilerplate.
341
342 // Start from the bottom of the block and work up, examining the
343 // terminator instructions.
344 MachineBasicBlock::iterator I = MBB.end();
345 while (I != MBB.begin()) {
346 --I;
347 if (I->isDebugValue())
348 continue;
349
350 // Working from the bottom, when we see a non-terminator instruction, we're
351 // done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000352 if (!isUnpredicatedTerminator(*I))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000353 break;
354
355 // A terminator that isn't a branch can't easily be handled by this
356 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000357 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000358 return true;
359
360 // Can't handle indirect branches.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000361 SystemZII::Branch Branch(getBranchInfo(*I));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000362 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000363 return true;
364
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000365 // Punt on compound branches.
366 if (Branch.Type != SystemZII::BranchNormal)
367 return true;
368
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000369 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000370 // Handle unconditional branches.
371 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000372 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000373 continue;
374 }
375
376 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000377 while (std::next(I) != MBB.end())
378 std::next(I)->eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000379
380 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000381 FBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000382
383 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000384 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000385 TBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000386 I->eraseFromParent();
387 I = MBB.end();
388 continue;
389 }
390
391 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000392 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000393 continue;
394 }
395
396 // Working from the bottom, handle the first conditional branch.
397 if (Cond.empty()) {
398 // FIXME: add X86-style branch swap
399 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000400 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000401 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000402 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000403 continue;
404 }
405
406 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000407 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000408
409 // Only handle the case where all conditional branches branch to the same
410 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000411 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000412 return true;
413
414 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000415 unsigned OldCCValid = Cond[0].getImm();
416 unsigned OldCCMask = Cond[1].getImm();
417 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000418 continue;
419
420 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000421 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000422 }
423
424 return false;
425}
426
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000427unsigned SystemZInstrInfo::removeBranch(MachineBasicBlock &MBB,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000428 int *BytesRemoved) const {
429 assert(!BytesRemoved && "code size not handled");
430
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000431 // Most of the code and comments here are boilerplate.
432 MachineBasicBlock::iterator I = MBB.end();
433 unsigned Count = 0;
434
435 while (I != MBB.begin()) {
436 --I;
437 if (I->isDebugValue())
438 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000439 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000440 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000441 if (!getBranchInfo(*I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000442 break;
443 // Remove the branch.
444 I->eraseFromParent();
445 I = MBB.end();
446 ++Count;
447 }
448
449 return Count;
450}
451
Richard Sandiford3d768e32013-07-31 12:30:20 +0000452bool SystemZInstrInfo::
Matt Arsenault1b9fc8e2016-09-14 20:43:16 +0000453reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000454 assert(Cond.size() == 2 && "Invalid condition");
455 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
456 return false;
457}
458
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000459unsigned SystemZInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000460 MachineBasicBlock *TBB,
461 MachineBasicBlock *FBB,
462 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000463 const DebugLoc &DL,
464 int *BytesAdded) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000465 // In this function we output 32-bit branches, which should always
466 // have enough range. They can be shortened and relaxed by later code
467 // in the pipeline, if desired.
468
469 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000470 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000471 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000472 "SystemZ branch conditions have one component!");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000473 assert(!BytesAdded && "code size not handled");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000474
475 if (Cond.empty()) {
476 // Unconditional branch?
477 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000478 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000479 return 1;
480 }
481
482 // Conditional branch.
483 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000484 unsigned CCValid = Cond[0].getImm();
485 unsigned CCMask = Cond[1].getImm();
486 BuildMI(&MBB, DL, get(SystemZ::BRC))
487 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000488 ++Count;
489
490 if (FBB) {
491 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000492 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000493 ++Count;
494 }
495 return Count;
496}
497
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000498bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
499 unsigned &SrcReg2, int &Mask,
500 int &Value) const {
501 assert(MI.isCompare() && "Caller should have checked for a comparison");
Richard Sandiford564681c2013-08-12 10:28:10 +0000502
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000503 if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() &&
504 MI.getOperand(1).isImm()) {
505 SrcReg = MI.getOperand(0).getReg();
Richard Sandiford564681c2013-08-12 10:28:10 +0000506 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000507 Value = MI.getOperand(1).getImm();
Richard Sandiford564681c2013-08-12 10:28:10 +0000508 Mask = ~0;
509 return true;
510 }
511
512 return false;
513}
514
Richard Sandiforda5901252013-08-16 10:22:54 +0000515// If Reg is a virtual register, return its definition, otherwise return null.
516static MachineInstr *getDef(unsigned Reg,
517 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000518 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +0000519 return nullptr;
Richard Sandiford564681c2013-08-12 10:28:10 +0000520 return MRI->getUniqueVRegDef(Reg);
521}
522
523// Return true if MI is a shift of type Opcode by Imm bits.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000524static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000525 return (MI->getOpcode() == Opcode &&
526 !MI->getOperand(2).getReg() &&
527 MI->getOperand(3).getImm() == Imm);
528}
529
Richard Sandiforda5901252013-08-16 10:22:54 +0000530// If the destination of MI has no uses, delete it as dead.
531static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
532 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
533 MI->eraseFromParent();
534}
535
Richard Sandiford564681c2013-08-12 10:28:10 +0000536// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000537// the result of an IPM sequence whose input CC survives until Compare,
538// and whether Compare is therefore redundant. Delete it and return
539// true if so.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000540static bool removeIPMBasedCompare(MachineInstr &Compare, unsigned SrcReg,
Richard Sandiforda5901252013-08-16 10:22:54 +0000541 const MachineRegisterInfo *MRI,
542 const TargetRegisterInfo *TRI) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000543 MachineInstr *LGFR = nullptr;
Richard Sandiforda5901252013-08-16 10:22:54 +0000544 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000545 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
546 LGFR = RLL;
547 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
548 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000549 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000550 return false;
551
Richard Sandiforda5901252013-08-16 10:22:54 +0000552 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000553 if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
Richard Sandiford564681c2013-08-12 10:28:10 +0000554 return false;
555
Richard Sandiforda5901252013-08-16 10:22:54 +0000556 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000557 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
558 return false;
559
560 // Check that there are no assignments to CC between the IPM and Compare,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000561 if (IPM->getParent() != Compare.getParent())
Richard Sandiford564681c2013-08-12 10:28:10 +0000562 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000563 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare.getIterator();
Richard Sandiford564681c2013-08-12 10:28:10 +0000564 for (++MBBI; MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000565 MachineInstr &MI = *MBBI;
566 if (MI.modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000567 return false;
568 }
569
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000570 Compare.eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000571 if (LGFR)
572 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000573 eraseIfDead(RLL, MRI);
574 eraseIfDead(SRL, MRI);
575 eraseIfDead(IPM, MRI);
576
Richard Sandiford564681c2013-08-12 10:28:10 +0000577 return true;
578}
579
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000580bool SystemZInstrInfo::optimizeCompareInstr(
581 MachineInstr &Compare, unsigned SrcReg, unsigned SrcReg2, int Mask,
582 int Value, const MachineRegisterInfo *MRI) const {
Richard Sandiford564681c2013-08-12 10:28:10 +0000583 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000584 bool IsLogical = (Compare.getDesc().TSFlags & SystemZII::IsLogical) != 0;
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000585 return Value == 0 && !IsLogical &&
586 removeIPMBasedCompare(Compare, SrcReg, MRI, &RI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000587}
588
Ulrich Weigand524f2762016-11-28 13:34:08 +0000589
590bool SystemZInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
591 ArrayRef<MachineOperand> Pred,
592 unsigned TrueReg, unsigned FalseReg,
593 int &CondCycles, int &TrueCycles,
594 int &FalseCycles) const {
595 // Not all subtargets have LOCR instructions.
596 if (!STI.hasLoadStoreOnCond())
597 return false;
598 if (Pred.size() != 2)
599 return false;
600
601 // Check register classes.
602 const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
603 const TargetRegisterClass *RC =
604 RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
605 if (!RC)
606 return false;
607
608 // We have LOCR instructions for 32 and 64 bit general purpose registers.
609 if ((STI.hasLoadStoreOnCond2() &&
610 SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) ||
611 SystemZ::GR32BitRegClass.hasSubClassEq(RC) ||
612 SystemZ::GR64BitRegClass.hasSubClassEq(RC)) {
613 CondCycles = 2;
614 TrueCycles = 2;
615 FalseCycles = 2;
616 return true;
Richard Sandifordf2404162013-07-25 09:11:15 +0000617 }
Ulrich Weigand524f2762016-11-28 13:34:08 +0000618
619 // Can't do anything else.
620 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000621}
622
Ulrich Weigand524f2762016-11-28 13:34:08 +0000623void SystemZInstrInfo::insertSelect(MachineBasicBlock &MBB,
624 MachineBasicBlock::iterator I,
625 const DebugLoc &DL, unsigned DstReg,
626 ArrayRef<MachineOperand> Pred,
627 unsigned TrueReg,
628 unsigned FalseReg) const {
629 MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
630 const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
631
632 assert(Pred.size() == 2 && "Invalid condition");
633 unsigned CCValid = Pred[0].getImm();
634 unsigned CCMask = Pred[1].getImm();
635
636 unsigned Opc;
637 if (SystemZ::GRX32BitRegClass.hasSubClassEq(RC)) {
638 if (STI.hasLoadStoreOnCond2())
639 Opc = SystemZ::LOCRMux;
640 else {
641 Opc = SystemZ::LOCR;
642 MRI.constrainRegClass(DstReg, &SystemZ::GR32BitRegClass);
643 }
644 } else if (SystemZ::GR64BitRegClass.hasSubClassEq(RC))
645 Opc = SystemZ::LOCGR;
646 else
647 llvm_unreachable("Invalid register class");
648
649 BuildMI(MBB, I, DL, get(Opc), DstReg)
650 .addReg(FalseReg).addReg(TrueReg)
651 .addImm(CCValid).addImm(CCMask);
652}
653
654bool SystemZInstrInfo::FoldImmediate(MachineInstr &UseMI, MachineInstr &DefMI,
655 unsigned Reg,
656 MachineRegisterInfo *MRI) const {
657 unsigned DefOpc = DefMI.getOpcode();
658 if (DefOpc != SystemZ::LHIMux && DefOpc != SystemZ::LHI &&
659 DefOpc != SystemZ::LGHI)
660 return false;
661 if (DefMI.getOperand(0).getReg() != Reg)
662 return false;
663 int32_t ImmVal = (int32_t)DefMI.getOperand(1).getImm();
664
665 unsigned UseOpc = UseMI.getOpcode();
666 unsigned NewUseOpc;
667 unsigned UseIdx;
668 int CommuteIdx = -1;
669 switch (UseOpc) {
670 case SystemZ::LOCRMux:
671 if (!STI.hasLoadStoreOnCond2())
672 return false;
673 NewUseOpc = SystemZ::LOCHIMux;
674 if (UseMI.getOperand(2).getReg() == Reg)
675 UseIdx = 2;
676 else if (UseMI.getOperand(1).getReg() == Reg)
677 UseIdx = 2, CommuteIdx = 1;
678 else
679 return false;
680 break;
681 case SystemZ::LOCGR:
682 if (!STI.hasLoadStoreOnCond2())
683 return false;
684 NewUseOpc = SystemZ::LOCGHI;
685 if (UseMI.getOperand(2).getReg() == Reg)
686 UseIdx = 2;
687 else if (UseMI.getOperand(1).getReg() == Reg)
688 UseIdx = 2, CommuteIdx = 1;
689 else
690 return false;
691 break;
692 default:
693 return false;
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000694 }
Ulrich Weigand524f2762016-11-28 13:34:08 +0000695
696 if (CommuteIdx != -1)
697 if (!commuteInstruction(UseMI, false, CommuteIdx, UseIdx))
698 return false;
699
700 bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
701 UseMI.setDesc(get(NewUseOpc));
702 UseMI.getOperand(UseIdx).ChangeToImmediate(ImmVal);
703 if (DeleteDef)
704 DefMI.eraseFromParent();
705
706 return true;
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000707}
708
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000709bool SystemZInstrInfo::isPredicable(MachineInstr &MI) const {
710 unsigned Opcode = MI.getOpcode();
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000711 if (Opcode == SystemZ::Return ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000712 Opcode == SystemZ::Trap ||
Ulrich Weigand848a5132016-04-11 12:12:32 +0000713 Opcode == SystemZ::CallJG ||
714 Opcode == SystemZ::CallBR)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000715 return true;
716 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000717}
718
719bool SystemZInstrInfo::
720isProfitableToIfCvt(MachineBasicBlock &MBB,
721 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000722 BranchProbability Probability) const {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000723 // Avoid using conditional returns at the end of a loop (since then
724 // we'd need to emit an unconditional branch to the beginning anyway,
725 // making the loop body longer). This doesn't apply for low-probability
726 // loops (eg. compare-and-swap retry), so just decide based on branch
727 // probability instead of looping structure.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000728 // However, since Compare and Trap instructions cost the same as a regular
729 // Compare instruction, we should allow the if conversion to convert this
730 // into a Conditional Compare regardless of the branch probability.
731 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap &&
732 MBB.succ_empty() && Probability < BranchProbability(1, 8))
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000733 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000734 // For now only convert single instructions.
735 return NumCycles == 1;
736}
737
738bool SystemZInstrInfo::
739isProfitableToIfCvt(MachineBasicBlock &TMBB,
740 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
741 MachineBasicBlock &FMBB,
742 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000743 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000744 // For now avoid converting mutually-exclusive cases.
745 return false;
746}
747
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000748bool SystemZInstrInfo::
749isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
750 BranchProbability Probability) const {
751 // For now only duplicate single instructions.
752 return NumCycles == 1;
753}
754
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000755bool SystemZInstrInfo::PredicateInstruction(
756 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000757 assert(Pred.size() == 2 && "Invalid condition");
758 unsigned CCValid = Pred[0].getImm();
759 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000760 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000761 unsigned Opcode = MI.getOpcode();
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000762 if (Opcode == SystemZ::Trap) {
763 MI.setDesc(get(SystemZ::CondTrap));
764 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
765 .addImm(CCValid).addImm(CCMask)
766 .addReg(SystemZ::CC, RegState::Implicit);
767 return true;
768 }
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000769 if (Opcode == SystemZ::Return) {
770 MI.setDesc(get(SystemZ::CondReturn));
771 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
772 .addImm(CCValid).addImm(CCMask)
773 .addReg(SystemZ::CC, RegState::Implicit);
774 return true;
775 }
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000776 if (Opcode == SystemZ::CallJG) {
Zhan Jun Liaua5d60af2016-07-07 15:34:46 +0000777 MachineOperand FirstOp = MI.getOperand(0);
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000778 const uint32_t *RegMask = MI.getOperand(1).getRegMask();
779 MI.RemoveOperand(1);
780 MI.RemoveOperand(0);
781 MI.setDesc(get(SystemZ::CallBRCL));
782 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
783 .addImm(CCValid).addImm(CCMask)
Zhan Jun Liaua5d60af2016-07-07 15:34:46 +0000784 .addOperand(FirstOp)
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000785 .addRegMask(RegMask)
786 .addReg(SystemZ::CC, RegState::Implicit);
787 return true;
788 }
Ulrich Weigand848a5132016-04-11 12:12:32 +0000789 if (Opcode == SystemZ::CallBR) {
790 const uint32_t *RegMask = MI.getOperand(0).getRegMask();
791 MI.RemoveOperand(0);
792 MI.setDesc(get(SystemZ::CallBCR));
793 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
794 .addImm(CCValid).addImm(CCMask)
795 .addRegMask(RegMask)
796 .addReg(SystemZ::CC, RegState::Implicit);
797 return true;
798 }
Richard Sandifordf2404162013-07-25 09:11:15 +0000799 return false;
800}
801
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000802void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
803 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000804 const DebugLoc &DL, unsigned DestReg,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000805 unsigned SrcReg, bool KillSrc) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000806 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
807 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000808 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
809 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
810 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
811 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000812 return;
813 }
814
Richard Sandiford0755c932013-10-01 11:26:28 +0000815 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
816 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
817 return;
818 }
819
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000820 // Everything else needs only one instruction.
821 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000822 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000823 Opcode = SystemZ::LGR;
824 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigandcdce0262016-03-14 13:50:03 +0000825 // For z13 we prefer LDR over LER to avoid partial register dependencies.
826 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000827 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
828 Opcode = SystemZ::LDR;
829 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
830 Opcode = SystemZ::LXR;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000831 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
832 Opcode = SystemZ::VLR32;
833 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
834 Opcode = SystemZ::VLR64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000835 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
836 Opcode = SystemZ::VLR;
Ulrich Weigandfffc7112016-11-08 20:15:26 +0000837 else if (SystemZ::AR32BitRegClass.contains(DestReg, SrcReg))
838 Opcode = SystemZ::CPYA;
839 else if (SystemZ::AR32BitRegClass.contains(DestReg) &&
840 SystemZ::GR32BitRegClass.contains(SrcReg))
841 Opcode = SystemZ::SAR;
842 else if (SystemZ::GR32BitRegClass.contains(DestReg) &&
843 SystemZ::AR32BitRegClass.contains(SrcReg))
844 Opcode = SystemZ::EAR;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000845 else
846 llvm_unreachable("Impossible reg-to-reg copy");
847
848 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
849 .addReg(SrcReg, getKillRegState(KillSrc));
850}
851
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000852void SystemZInstrInfo::storeRegToStackSlot(
853 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
854 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
855 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000856 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
857
858 // Callers may expect a single instruction, so keep 128-bit moves
859 // together for now and lower them after register allocation.
860 unsigned LoadOpcode, StoreOpcode;
861 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
862 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000863 .addReg(SrcReg, getKillRegState(isKill)),
864 FrameIdx);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000865}
866
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000867void SystemZInstrInfo::loadRegFromStackSlot(
868 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
869 int FrameIdx, const TargetRegisterClass *RC,
870 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000871 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
872
873 // Callers may expect a single instruction, so keep 128-bit moves
874 // together for now and lower them after register allocation.
875 unsigned LoadOpcode, StoreOpcode;
876 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
877 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
878 FrameIdx);
879}
880
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000881// Return true if MI is a simple load or store with a 12-bit displacement
882// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
883static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
884 const MCInstrDesc &MCID = MI->getDesc();
885 return ((MCID.TSFlags & Flag) &&
886 isUInt<12>(MI->getOperand(2).getImm()) &&
887 MI->getOperand(3).getReg() == 0);
888}
889
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000890namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +0000891struct LogicOp {
892 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
893 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
894 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000895
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000896 explicit operator bool() const { return RegSize; }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000897
Richard Sandifordc2312692014-03-06 10:38:30 +0000898 unsigned RegSize, ImmLSB, ImmSize;
899};
900} // end anonymous namespace
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000901
902static LogicOp interpretAndImmediate(unsigned Opcode) {
903 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000904 case SystemZ::NILMux: return LogicOp(32, 0, 16);
905 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000906 case SystemZ::NILL64: return LogicOp(64, 0, 16);
907 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000908 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
909 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
910 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000911 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000912 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000913 default: return LogicOp();
914 }
915}
916
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000917static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) {
918 if (OldMI->registerDefIsDead(SystemZ::CC)) {
919 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC);
920 if (CCDef != nullptr)
921 CCDef->setIsDead(true);
922 }
923}
924
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000925// Used to return from convertToThreeAddress after replacing two-address
926// instruction OldMI with three-address instruction NewMI.
927static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
928 MachineInstr *NewMI,
929 LiveVariables *LV) {
930 if (LV) {
931 unsigned NumOps = OldMI->getNumOperands();
932 for (unsigned I = 1; I < NumOps; ++I) {
933 MachineOperand &Op = OldMI->getOperand(I);
934 if (Op.isReg() && Op.isKill())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000935 LV->replaceKillInstruction(Op.getReg(), *OldMI, *NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000936 }
937 }
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000938 transferDeadCC(OldMI, NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000939 return NewMI;
940}
941
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000942MachineInstr *SystemZInstrInfo::convertToThreeAddress(
943 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
944 MachineBasicBlock *MBB = MI.getParent();
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000945 MachineFunction *MF = MBB->getParent();
946 MachineRegisterInfo &MRI = MF->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000947
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000948 unsigned Opcode = MI.getOpcode();
949 unsigned NumOps = MI.getNumOperands();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000950
951 // Try to convert something like SLL into SLLK, if supported.
952 // We prefer to keep the two-operand form where possible both
953 // because it tends to be shorter and because some instructions
954 // have memory forms that can be used during spilling.
Eric Christopher673b3af2014-06-27 07:01:17 +0000955 if (STI.hasDistinctOps()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000956 MachineOperand &Dest = MI.getOperand(0);
957 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000958 unsigned DestReg = Dest.getReg();
959 unsigned SrcReg = Src.getReg();
960 // AHIMux is only really a three-operand instruction when both operands
961 // are low registers. Try to constrain both operands to be low if
962 // possible.
963 if (Opcode == SystemZ::AHIMux &&
964 TargetRegisterInfo::isVirtualRegister(DestReg) &&
965 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
966 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
967 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
968 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
969 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
970 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000971 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
972 if (ThreeOperandOpcode >= 0) {
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000973 // Create three address instruction without adding the implicit
974 // operands. Those will instead be copied over from the original
975 // instruction by the loop below.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000976 MachineInstrBuilder MIB(
977 *MF, MF->CreateMachineInstr(get(ThreeOperandOpcode), MI.getDebugLoc(),
978 /*NoImplicit=*/true));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000979 MIB.addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000980 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000981 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000982 // Keep the remaining operands as-is.
983 for (unsigned I = 2; I < NumOps; ++I)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000984 MIB.addOperand(MI.getOperand(I));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000985 MBB->insert(MI, MIB);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000986 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000987 }
988 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000989
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000990 // Try to convert an AND into an RISBG-type instruction.
991 if (LogicOp And = interpretAndImmediate(Opcode)) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000992 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB;
Richard Sandiford70284282013-10-01 14:20:41 +0000993 // AND IMMEDIATE leaves the other bits of the register unchanged.
994 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
995 unsigned Start, End;
996 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
997 unsigned NewOpcode;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000998 if (And.RegSize == 64) {
Richard Sandiford70284282013-10-01 14:20:41 +0000999 NewOpcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001000 // Prefer RISBGN if available, since it does not clobber CC.
1001 if (STI.hasMiscellaneousExtensions())
1002 NewOpcode = SystemZ::RISBGN;
1003 } else {
Richard Sandiford70284282013-10-01 14:20:41 +00001004 NewOpcode = SystemZ::RISBMux;
1005 Start &= 31;
1006 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001007 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001008 MachineOperand &Dest = MI.getOperand(0);
1009 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford70284282013-10-01 14:20:41 +00001010 MachineInstrBuilder MIB =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001011 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode))
1012 .addOperand(Dest)
1013 .addReg(0)
1014 .addReg(Src.getReg(), getKillRegState(Src.isKill()),
1015 Src.getSubReg())
1016 .addImm(Start)
1017 .addImm(End + 128)
1018 .addImm(0);
1019 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001020 }
1021 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001022 return nullptr;
Richard Sandifordff6c5a52013-07-19 16:12:08 +00001023}
1024
Keno Fischere70b31f2015-06-08 20:09:58 +00001025MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001026 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001027 MachineBasicBlock::iterator InsertPt, int FrameIndex,
1028 LiveIntervals *LIS) const {
1029 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +00001030 const MachineFrameInfo &MFI = MF.getFrameInfo();
1031 unsigned Size = MFI.getObjectSize(FrameIndex);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001032 unsigned Opcode = MI.getOpcode();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001033
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001034 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001035 if (LIS != nullptr && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
1036 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) {
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001037
1038 // Check CC liveness, since new instruction introduces a dead
1039 // def of CC.
1040 MCRegUnitIterator CCUnit(SystemZ::CC, TRI);
1041 LiveRange &CCLiveRange = LIS->getRegUnit(*CCUnit);
1042 ++CCUnit;
1043 assert (!CCUnit.isValid() && "CC only has one reg unit.");
1044 SlotIndex MISlot =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001045 LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot();
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001046 if (!CCLiveRange.liveAt(MISlot)) {
1047 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001048 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt,
1049 MI.getDebugLoc(), get(SystemZ::AGSI))
1050 .addFrameIndex(FrameIndex)
1051 .addImm(0)
1052 .addImm(MI.getOperand(2).getImm());
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001053 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true);
1054 CCLiveRange.createDeadDef(MISlot, LIS->getVNInfoAllocator());
1055 return BuiltMI;
1056 }
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001057 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001058 return nullptr;
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001059 }
1060
1061 // All other cases require a single operand.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001062 if (Ops.size() != 1)
Craig Topper062a2ba2014-04-25 05:30:21 +00001063 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001064
1065 unsigned OpNum = Ops[0];
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001066 assert(Size ==
1067 MF.getRegInfo()
1068 .getRegClass(MI.getOperand(OpNum).getReg())
1069 ->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +00001070 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001071
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001072 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 &&
1073 isInt<8>(MI.getOperand(2).getImm())) {
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001074 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
1075 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +00001076 MachineInstr *BuiltMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001077 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
1078 .addFrameIndex(FrameIndex)
1079 .addImm(0)
1080 .addImm(MI.getOperand(2).getImm());
1081 transferDeadCC(&MI, BuiltMI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +00001082 return BuiltMI;
Richard Sandiford6af6ff12013-10-15 08:42:59 +00001083 }
1084
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001085 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
1086 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
1087 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
1088 // If we're spilling the destination of an LDGR or LGDR, store the
1089 // source register instead.
1090 if (OpNum == 0) {
1091 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001092 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001093 get(StoreOpcode))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001094 .addOperand(MI.getOperand(1))
Keno Fischere70b31f2015-06-08 20:09:58 +00001095 .addFrameIndex(FrameIndex)
1096 .addImm(0)
1097 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001098 }
1099 // If we're spilling the source of an LDGR or LGDR, load the
1100 // destination register instead.
1101 if (OpNum == 1) {
1102 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001103 unsigned Dest = MI.getOperand(0).getReg();
1104 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001105 get(LoadOpcode), Dest)
1106 .addFrameIndex(FrameIndex)
1107 .addImm(0)
1108 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001109 }
1110 }
1111
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001112 // Look for cases where the source of a simple store or the destination
1113 // of a simple load is being spilled. Try to use MVC instead.
1114 //
1115 // Although MVC is in practice a fast choice in these cases, it is still
1116 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +00001117 // load or store is volatile. We also wouldn't be able to use MVC if
1118 // the two memories partially overlap, but that case cannot occur here,
1119 // because we know that one of the memories is a full frame index.
1120 //
1121 // For performance reasons, we also want to avoid using MVC if the addresses
1122 // might be equal. We don't worry about that case here, because spill slot
1123 // coloring happens later, and because we have special code to remove
1124 // MVCs that turn out to be redundant.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001125 if (OpNum == 0 && MI.hasOneMemOperand()) {
1126 MachineMemOperand *MMO = *MI.memoperands_begin();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001127 if (MMO->getSize() == Size && !MMO->isVolatile()) {
1128 // Handle conversion of loads.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001129 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) {
1130 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001131 get(SystemZ::MVC))
1132 .addFrameIndex(FrameIndex)
1133 .addImm(0)
1134 .addImm(Size)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001135 .addOperand(MI.getOperand(1))
1136 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +00001137 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001138 }
1139 // Handle conversion of stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001140 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) {
1141 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +00001142 get(SystemZ::MVC))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001143 .addOperand(MI.getOperand(1))
1144 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +00001145 .addImm(Size)
1146 .addFrameIndex(FrameIndex)
1147 .addImm(0)
1148 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001149 }
1150 }
1151 }
1152
Richard Sandiforded1fab62013-07-03 10:10:02 +00001153 // If the spilled operand is the final one, try to change <INSN>R
1154 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001155 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001156 if (MemOpcode >= 0) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001157 unsigned NumOps = MI.getNumExplicitOperands();
Richard Sandiforded1fab62013-07-03 10:10:02 +00001158 if (OpNum == NumOps - 1) {
1159 const MCInstrDesc &MemDesc = get(MemOpcode);
1160 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
1161 assert(AccessBytes != 0 && "Size of access should be known");
1162 assert(AccessBytes <= Size && "Access outside the frame index");
1163 uint64_t Offset = Size - AccessBytes;
Keno Fischere70b31f2015-06-08 20:09:58 +00001164 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001165 MI.getDebugLoc(), get(MemOpcode));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001166 for (unsigned I = 0; I < OpNum; ++I)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001167 MIB.addOperand(MI.getOperand(I));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001168 MIB.addFrameIndex(FrameIndex).addImm(Offset);
1169 if (MemDesc.TSFlags & SystemZII::HasIndex)
1170 MIB.addReg(0);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001171 transferDeadCC(&MI, MIB);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001172 return MIB;
1173 }
1174 }
1175
Craig Topper062a2ba2014-04-25 05:30:21 +00001176 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001177}
1178
Keno Fischere70b31f2015-06-08 20:09:58 +00001179MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001180 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
1181 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001182 LiveIntervals *LIS) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001183 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001184}
1185
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001186bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1187 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001188 case SystemZ::L128:
1189 splitMove(MI, SystemZ::LG);
1190 return true;
1191
1192 case SystemZ::ST128:
1193 splitMove(MI, SystemZ::STG);
1194 return true;
1195
1196 case SystemZ::LX:
1197 splitMove(MI, SystemZ::LD);
1198 return true;
1199
1200 case SystemZ::STX:
1201 splitMove(MI, SystemZ::STD);
1202 return true;
1203
Richard Sandiford89e160d2013-10-01 12:11:47 +00001204 case SystemZ::LBMux:
1205 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
1206 return true;
1207
1208 case SystemZ::LHMux:
1209 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
1210 return true;
1211
Richard Sandiford21235a22013-10-01 12:49:07 +00001212 case SystemZ::LLCRMux:
1213 expandZExtPseudo(MI, SystemZ::LLCR, 8);
1214 return true;
1215
1216 case SystemZ::LLHRMux:
1217 expandZExtPseudo(MI, SystemZ::LLHR, 16);
1218 return true;
1219
Richard Sandiford0d46b1a2013-10-01 12:19:08 +00001220 case SystemZ::LLCMux:
1221 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
1222 return true;
1223
1224 case SystemZ::LLHMux:
1225 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
1226 return true;
1227
Richard Sandiford0755c932013-10-01 11:26:28 +00001228 case SystemZ::LMux:
1229 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
1230 return true;
1231
Ulrich Weigand524f2762016-11-28 13:34:08 +00001232 case SystemZ::LOCMux:
1233 expandLOCPseudo(MI, SystemZ::LOC, SystemZ::LOCFH);
1234 return true;
1235
1236 case SystemZ::LOCHIMux:
1237 expandLOCPseudo(MI, SystemZ::LOCHI, SystemZ::LOCHHI);
1238 return true;
1239
1240 case SystemZ::LOCRMux:
1241 expandLOCRPseudo(MI, SystemZ::LOCR, SystemZ::LOCFHR);
1242 return true;
1243
Richard Sandiford5469c392013-10-01 12:22:49 +00001244 case SystemZ::STCMux:
1245 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
1246 return true;
1247
1248 case SystemZ::STHMux:
1249 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
1250 return true;
1251
Richard Sandiford0755c932013-10-01 11:26:28 +00001252 case SystemZ::STMux:
1253 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
1254 return true;
1255
Ulrich Weigand524f2762016-11-28 13:34:08 +00001256 case SystemZ::STOCMux:
1257 expandLOCPseudo(MI, SystemZ::STOC, SystemZ::STOCFH);
1258 return true;
1259
Richard Sandiford01240232013-10-01 13:02:28 +00001260 case SystemZ::LHIMux:
1261 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
1262 return true;
1263
1264 case SystemZ::IIFMux:
1265 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
1266 return true;
1267
Richard Sandiford1a569312013-10-01 13:18:56 +00001268 case SystemZ::IILMux:
1269 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
1270 return true;
1271
1272 case SystemZ::IIHMux:
1273 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
1274 return true;
1275
Richard Sandiford70284282013-10-01 14:20:41 +00001276 case SystemZ::NIFMux:
1277 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1278 return true;
1279
1280 case SystemZ::NILMux:
1281 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1282 return true;
1283
1284 case SystemZ::NIHMux:
1285 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1286 return true;
1287
Richard Sandiford6e96ac62013-10-01 13:22:41 +00001288 case SystemZ::OIFMux:
1289 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1290 return true;
1291
1292 case SystemZ::OILMux:
1293 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1294 return true;
1295
1296 case SystemZ::OIHMux:
1297 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1298 return true;
1299
Richard Sandiford5718dac2013-10-01 14:08:44 +00001300 case SystemZ::XIFMux:
1301 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1302 return true;
1303
Richard Sandiford2cac7632013-10-01 14:41:52 +00001304 case SystemZ::TMLMux:
1305 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1306 return true;
1307
1308 case SystemZ::TMHMux:
1309 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1310 return true;
1311
Richard Sandiford42a694f2013-10-01 14:53:46 +00001312 case SystemZ::AHIMux:
1313 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1314 return true;
1315
1316 case SystemZ::AHIMuxK:
1317 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1318 return true;
1319
1320 case SystemZ::AFIMux:
1321 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1322 return true;
1323
Ulrich Weigand75839912016-11-28 13:40:08 +00001324 case SystemZ::CHIMux:
1325 expandRIPseudo(MI, SystemZ::CHI, SystemZ::CIH, false);
1326 return true;
1327
Richard Sandiforda9ac0e02013-10-01 14:56:23 +00001328 case SystemZ::CFIMux:
1329 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1330 return true;
1331
1332 case SystemZ::CLFIMux:
1333 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1334 return true;
1335
Richard Sandifordb63e3002013-10-01 15:00:44 +00001336 case SystemZ::CMux:
1337 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1338 return true;
1339
1340 case SystemZ::CLMux:
1341 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1342 return true;
1343
Richard Sandiford70284282013-10-01 14:20:41 +00001344 case SystemZ::RISBMux: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001345 bool DestIsHigh = isHighReg(MI.getOperand(0).getReg());
1346 bool SrcIsHigh = isHighReg(MI.getOperand(2).getReg());
Richard Sandiford70284282013-10-01 14:20:41 +00001347 if (SrcIsHigh == DestIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001348 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
Richard Sandiford70284282013-10-01 14:20:41 +00001349 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001350 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1351 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32);
Richard Sandiford70284282013-10-01 14:20:41 +00001352 }
1353 return true;
1354 }
1355
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001356 case SystemZ::ADJDYNALLOC:
1357 splitAdjDynAlloc(MI);
1358 return true;
1359
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001360 case TargetOpcode::LOAD_STACK_GUARD:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001361 expandLoadStackGuard(&MI);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001362 return true;
1363
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001364 default:
1365 return false;
1366 }
1367}
1368
Sjoerd Meijer0eb96ed2016-07-29 08:16:16 +00001369unsigned SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001370 if (MI.getOpcode() == TargetOpcode::INLINEASM) {
1371 const MachineFunction *MF = MI.getParent()->getParent();
1372 const char *AsmStr = MI.getOperand(0).getSymbolName();
Richard Sandiford312425f2013-05-20 14:23:08 +00001373 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1374 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001375 return MI.getDesc().getSize();
Richard Sandiford312425f2013-05-20 14:23:08 +00001376}
1377
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001378SystemZII::Branch
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001379SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const {
1380 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001381 case SystemZ::BR:
1382 case SystemZ::J:
1383 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001384 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001385 SystemZ::CCMASK_ANY, &MI.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001386
1387 case SystemZ::BRC:
1388 case SystemZ::BRCL:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001389 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(),
1390 MI.getOperand(1).getImm(), &MI.getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001391
Richard Sandifordc2121252013-08-05 11:23:46 +00001392 case SystemZ::BRCT:
Ulrich Weigand75839912016-11-28 13:40:08 +00001393 case SystemZ::BRCTH:
Richard Sandifordc2121252013-08-05 11:23:46 +00001394 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001395 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001396
1397 case SystemZ::BRCTG:
1398 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001399 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001400
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001401 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001402 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001403 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001404 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001405
Richard Sandiford93183ee2013-09-18 09:56:40 +00001406 case SystemZ::CLIJ:
1407 case SystemZ::CLRJ:
1408 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001409 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001410
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001411 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001412 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001413 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001414 MI.getOperand(2).getImm(), &MI.getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001415
Richard Sandiford93183ee2013-09-18 09:56:40 +00001416 case SystemZ::CLGIJ:
1417 case SystemZ::CLGRJ:
1418 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001419 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001420
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001421 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001422 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001423 }
1424}
1425
1426void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1427 unsigned &LoadOpcode,
1428 unsigned &StoreOpcode) const {
1429 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1430 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001431 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001432 } else if (RC == &SystemZ::GRH32BitRegClass) {
1433 LoadOpcode = SystemZ::LFH;
1434 StoreOpcode = SystemZ::STFH;
1435 } else if (RC == &SystemZ::GRX32BitRegClass) {
1436 LoadOpcode = SystemZ::LMux;
1437 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001438 } else if (RC == &SystemZ::GR64BitRegClass ||
1439 RC == &SystemZ::ADDR64BitRegClass) {
1440 LoadOpcode = SystemZ::LG;
1441 StoreOpcode = SystemZ::STG;
1442 } else if (RC == &SystemZ::GR128BitRegClass ||
1443 RC == &SystemZ::ADDR128BitRegClass) {
1444 LoadOpcode = SystemZ::L128;
1445 StoreOpcode = SystemZ::ST128;
1446 } else if (RC == &SystemZ::FP32BitRegClass) {
1447 LoadOpcode = SystemZ::LE;
1448 StoreOpcode = SystemZ::STE;
1449 } else if (RC == &SystemZ::FP64BitRegClass) {
1450 LoadOpcode = SystemZ::LD;
1451 StoreOpcode = SystemZ::STD;
1452 } else if (RC == &SystemZ::FP128BitRegClass) {
1453 LoadOpcode = SystemZ::LX;
1454 StoreOpcode = SystemZ::STX;
Ulrich Weigand49506d72015-05-05 19:28:34 +00001455 } else if (RC == &SystemZ::VR32BitRegClass) {
1456 LoadOpcode = SystemZ::VL32;
1457 StoreOpcode = SystemZ::VST32;
1458 } else if (RC == &SystemZ::VR64BitRegClass) {
1459 LoadOpcode = SystemZ::VL64;
1460 StoreOpcode = SystemZ::VST64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001461 } else if (RC == &SystemZ::VF128BitRegClass ||
1462 RC == &SystemZ::VR128BitRegClass) {
1463 LoadOpcode = SystemZ::VL;
1464 StoreOpcode = SystemZ::VST;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001465 } else
1466 llvm_unreachable("Unsupported regclass to load or store");
1467}
1468
1469unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1470 int64_t Offset) const {
1471 const MCInstrDesc &MCID = get(Opcode);
1472 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1473 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1474 // Get the instruction to use for unsigned 12-bit displacements.
1475 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1476 if (Disp12Opcode >= 0)
1477 return Disp12Opcode;
1478
1479 // All address-related instructions can use unsigned 12-bit
1480 // displacements.
1481 return Opcode;
1482 }
1483 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1484 // Get the instruction to use for signed 20-bit displacements.
1485 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1486 if (Disp20Opcode >= 0)
1487 return Disp20Opcode;
1488
1489 // Check whether Opcode allows signed 20-bit displacements.
1490 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1491 return Opcode;
1492 }
1493 return 0;
1494}
1495
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001496unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1497 switch (Opcode) {
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001498 case SystemZ::L: return SystemZ::LT;
1499 case SystemZ::LY: return SystemZ::LT;
1500 case SystemZ::LG: return SystemZ::LTG;
1501 case SystemZ::LGF: return SystemZ::LTGF;
1502 case SystemZ::LR: return SystemZ::LTR;
1503 case SystemZ::LGFR: return SystemZ::LTGFR;
1504 case SystemZ::LGR: return SystemZ::LTGR;
1505 case SystemZ::LER: return SystemZ::LTEBR;
1506 case SystemZ::LDR: return SystemZ::LTDBR;
1507 case SystemZ::LXR: return SystemZ::LTXBR;
Jonas Paulsson12629322015-10-01 18:12:28 +00001508 case SystemZ::LCDFR: return SystemZ::LCDBR;
1509 case SystemZ::LPDFR: return SystemZ::LPDBR;
1510 case SystemZ::LNDFR: return SystemZ::LNDBR;
1511 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1512 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1513 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001514 // On zEC12 we prefer to use RISBGN. But if there is a chance to
1515 // actually use the condition code, we may turn it back into RISGB.
1516 // Note that RISBG is not really a "load-and-test" instruction,
1517 // but sets the same condition code values, so is OK to use here.
1518 case SystemZ::RISBGN: return SystemZ::RISBG;
1519 default: return 0;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001520 }
1521}
1522
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001523// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1524// have already been filtered out. Store the first set bit in LSB and
1525// the number of set bits in Length if so.
1526static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1527 unsigned First = findFirstSet(Mask);
1528 uint64_t Top = (Mask >> First) + 1;
1529 if ((Top & -Top) == Top) {
1530 LSB = First;
1531 Length = findFirstSet(Top);
1532 return true;
1533 }
1534 return false;
1535}
1536
1537bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1538 unsigned &Start, unsigned &End) const {
1539 // Reject trivial all-zero masks.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001540 Mask &= allOnes(BitSize);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001541 if (Mask == 0)
1542 return false;
1543
1544 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1545 // the msb and End specifies the index of the lsb.
1546 unsigned LSB, Length;
1547 if (isStringOfOnes(Mask, LSB, Length)) {
1548 Start = 63 - (LSB + Length - 1);
1549 End = 63 - LSB;
1550 return true;
1551 }
1552
1553 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1554 // of the low 1s and End specifies the lsb of the high 1s.
1555 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1556 assert(LSB > 0 && "Bottom bit must be set");
1557 assert(LSB + Length < BitSize && "Top bit must be set");
1558 Start = 63 - (LSB - 1);
1559 End = 63 - (LSB + Length);
1560 return true;
1561 }
1562
1563 return false;
1564}
1565
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001566unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode,
1567 SystemZII::FusedCompareType Type,
1568 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001569 switch (Opcode) {
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001570 case SystemZ::CHI:
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001571 case SystemZ::CGHI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001572 if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
1573 return 0;
1574 break;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001575 case SystemZ::CLFI:
Richard Sandiford93183ee2013-09-18 09:56:40 +00001576 case SystemZ::CLGFI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001577 if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
1578 return 0;
Ulrich Weiganda0e73252016-11-11 12:48:26 +00001579 break;
1580 case SystemZ::CL:
1581 case SystemZ::CLG:
1582 if (!STI.hasMiscellaneousExtensions())
1583 return 0;
1584 if (!(MI && MI->getOperand(3).getReg() == 0))
1585 return 0;
1586 break;
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001587 }
1588 switch (Type) {
1589 case SystemZII::CompareAndBranch:
1590 switch (Opcode) {
1591 case SystemZ::CR:
1592 return SystemZ::CRJ;
1593 case SystemZ::CGR:
1594 return SystemZ::CGRJ;
1595 case SystemZ::CHI:
1596 return SystemZ::CIJ;
1597 case SystemZ::CGHI:
1598 return SystemZ::CGIJ;
1599 case SystemZ::CLR:
1600 return SystemZ::CLRJ;
1601 case SystemZ::CLGR:
1602 return SystemZ::CLGRJ;
1603 case SystemZ::CLFI:
1604 return SystemZ::CLIJ;
1605 case SystemZ::CLGFI:
1606 return SystemZ::CLGIJ;
1607 default:
1608 return 0;
1609 }
1610 case SystemZII::CompareAndReturn:
1611 switch (Opcode) {
1612 case SystemZ::CR:
1613 return SystemZ::CRBReturn;
1614 case SystemZ::CGR:
1615 return SystemZ::CGRBReturn;
1616 case SystemZ::CHI:
1617 return SystemZ::CIBReturn;
1618 case SystemZ::CGHI:
1619 return SystemZ::CGIBReturn;
1620 case SystemZ::CLR:
1621 return SystemZ::CLRBReturn;
1622 case SystemZ::CLGR:
1623 return SystemZ::CLGRBReturn;
1624 case SystemZ::CLFI:
1625 return SystemZ::CLIBReturn;
1626 case SystemZ::CLGFI:
1627 return SystemZ::CLGIBReturn;
1628 default:
1629 return 0;
1630 }
Ulrich Weigand848a5132016-04-11 12:12:32 +00001631 case SystemZII::CompareAndSibcall:
1632 switch (Opcode) {
1633 case SystemZ::CR:
1634 return SystemZ::CRBCall;
1635 case SystemZ::CGR:
1636 return SystemZ::CGRBCall;
1637 case SystemZ::CHI:
1638 return SystemZ::CIBCall;
1639 case SystemZ::CGHI:
1640 return SystemZ::CGIBCall;
1641 case SystemZ::CLR:
1642 return SystemZ::CLRBCall;
1643 case SystemZ::CLGR:
1644 return SystemZ::CLGRBCall;
1645 case SystemZ::CLFI:
1646 return SystemZ::CLIBCall;
1647 case SystemZ::CLGFI:
1648 return SystemZ::CLGIBCall;
1649 default:
1650 return 0;
1651 }
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001652 case SystemZII::CompareAndTrap:
1653 switch (Opcode) {
1654 case SystemZ::CR:
1655 return SystemZ::CRT;
1656 case SystemZ::CGR:
1657 return SystemZ::CGRT;
1658 case SystemZ::CHI:
1659 return SystemZ::CIT;
1660 case SystemZ::CGHI:
1661 return SystemZ::CGIT;
1662 case SystemZ::CLR:
1663 return SystemZ::CLRT;
1664 case SystemZ::CLGR:
1665 return SystemZ::CLGRT;
1666 case SystemZ::CLFI:
1667 return SystemZ::CLFIT;
1668 case SystemZ::CLGFI:
1669 return SystemZ::CLGIT;
Ulrich Weiganda0e73252016-11-11 12:48:26 +00001670 case SystemZ::CL:
1671 return SystemZ::CLT;
1672 case SystemZ::CLG:
1673 return SystemZ::CLGT;
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001674 default:
1675 return 0;
1676 }
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001677 }
Ulrich Weigand79391ee2016-04-07 16:33:25 +00001678 return 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001679}
1680
Ulrich Weigand2d9e3d92016-11-28 13:59:22 +00001681unsigned SystemZInstrInfo::getLoadAndTrap(unsigned Opcode) const {
1682 if (!STI.hasLoadAndTrap())
1683 return 0;
1684 switch (Opcode) {
1685 case SystemZ::L:
1686 case SystemZ::LY:
1687 return SystemZ::LAT;
1688 case SystemZ::LG:
1689 return SystemZ::LGAT;
1690 case SystemZ::LFH:
1691 return SystemZ::LFHAT;
1692 case SystemZ::LLGF:
1693 return SystemZ::LLGFAT;
1694 case SystemZ::LLGT:
1695 return SystemZ::LLGTAT;
1696 }
1697 return 0;
1698}
1699
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001700void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1701 MachineBasicBlock::iterator MBBI,
1702 unsigned Reg, uint64_t Value) const {
1703 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1704 unsigned Opcode;
1705 if (isInt<16>(Value))
1706 Opcode = SystemZ::LGHI;
1707 else if (SystemZ::isImmLL(Value))
1708 Opcode = SystemZ::LLILL;
1709 else if (SystemZ::isImmLH(Value)) {
1710 Opcode = SystemZ::LLILH;
1711 Value >>= 16;
1712 } else {
1713 assert(isInt<32>(Value) && "Huge values not handled yet");
1714 Opcode = SystemZ::LGFI;
1715 }
1716 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1717}
Jonas Paulsson8010b632016-10-20 08:27:16 +00001718
1719bool SystemZInstrInfo::
1720areMemAccessesTriviallyDisjoint(MachineInstr &MIa, MachineInstr &MIb,
1721 AliasAnalysis *AA) const {
1722
1723 if (!MIa.hasOneMemOperand() || !MIb.hasOneMemOperand())
1724 return false;
1725
1726 // If mem-operands show that the same address Value is used by both
1727 // instructions, check for non-overlapping offsets and widths. Not
1728 // sure if a register based analysis would be an improvement...
1729
1730 MachineMemOperand *MMOa = *MIa.memoperands_begin();
1731 MachineMemOperand *MMOb = *MIb.memoperands_begin();
1732 const Value *VALa = MMOa->getValue();
1733 const Value *VALb = MMOb->getValue();
1734 bool SameVal = (VALa && VALb && (VALa == VALb));
1735 if (!SameVal) {
1736 const PseudoSourceValue *PSVa = MMOa->getPseudoValue();
1737 const PseudoSourceValue *PSVb = MMOb->getPseudoValue();
1738 if (PSVa && PSVb && (PSVa == PSVb))
1739 SameVal = true;
1740 }
1741 if (SameVal) {
1742 int OffsetA = MMOa->getOffset(), OffsetB = MMOb->getOffset();
1743 int WidthA = MMOa->getSize(), WidthB = MMOb->getSize();
1744 int LowOffset = OffsetA < OffsetB ? OffsetA : OffsetB;
1745 int HighOffset = OffsetA < OffsetB ? OffsetB : OffsetA;
1746 int LowWidth = (LowOffset == OffsetA) ? WidthA : WidthB;
1747 if (LowOffset + LowWidth <= HighOffset)
1748 return true;
1749 }
1750
1751 return false;
1752}