blob: c2b38116d932855c7bbca4938f5cc80c4d5f59d3 [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
Richard Sandiford21235a22013-10-01 12:49:07 +0000152// MI is an RR-style pseudo instruction that zero-extends the low Size bits
153// of one GRX32 into another. Replace it with LowOpcode if both operands
154// are low registers, otherwise use RISB[LH]G.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000155void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
Richard Sandiford21235a22013-10-01 12:49:07 +0000156 unsigned Size) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000157 emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(),
158 MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode,
159 Size, MI.getOperand(1).isKill());
160 MI.eraseFromParent();
Richard Sandiford21235a22013-10-01 12:49:07 +0000161}
162
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +0000163void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const {
164 MachineBasicBlock *MBB = MI->getParent();
165 MachineFunction &MF = *MBB->getParent();
166 const unsigned Reg = MI->getOperand(0).getReg();
167
168 // Conveniently, all 4 instructions are cloned from LOAD_STACK_GUARD,
169 // so they already have operand 0 set to reg.
170
171 // ear <reg>, %a0
172 MachineInstr *Ear1MI = MF.CloneMachineInstr(MI);
173 MBB->insert(MI, Ear1MI);
174 Ear1MI->setDesc(get(SystemZ::EAR));
175 MachineInstrBuilder(MF, Ear1MI).addImm(0);
176
177 // sllg <reg>, <reg>, 32
178 MachineInstr *SllgMI = MF.CloneMachineInstr(MI);
179 MBB->insert(MI, SllgMI);
180 SllgMI->setDesc(get(SystemZ::SLLG));
181 MachineInstrBuilder(MF, SllgMI).addReg(Reg).addReg(0).addImm(32);
182
183 // ear <reg>, %a1
184 MachineInstr *Ear2MI = MF.CloneMachineInstr(MI);
185 MBB->insert(MI, Ear2MI);
186 Ear2MI->setDesc(get(SystemZ::EAR));
187 MachineInstrBuilder(MF, Ear2MI).addImm(1);
188
189 // lg <reg>, 40(<reg>)
190 MI->setDesc(get(SystemZ::LG));
191 MachineInstrBuilder(MF, MI).addReg(Reg).addImm(40).addReg(0);
192}
193
Richard Sandiford0755c932013-10-01 11:26:28 +0000194// Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
195// DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
196// are low registers, otherwise use RISB[LH]G. Size is the number of bits
197// taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
198// KillSrc is true if this move is the last use of SrcReg.
199void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
200 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000201 const DebugLoc &DL, unsigned DestReg,
Richard Sandiford0755c932013-10-01 11:26:28 +0000202 unsigned SrcReg, unsigned LowLowOpcode,
203 unsigned Size, bool KillSrc) const {
204 unsigned Opcode;
205 bool DestIsHigh = isHighReg(DestReg);
206 bool SrcIsHigh = isHighReg(SrcReg);
207 if (DestIsHigh && SrcIsHigh)
208 Opcode = SystemZ::RISBHH;
209 else if (DestIsHigh && !SrcIsHigh)
210 Opcode = SystemZ::RISBHL;
211 else if (!DestIsHigh && SrcIsHigh)
212 Opcode = SystemZ::RISBLH;
213 else {
214 BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
215 .addReg(SrcReg, getKillRegState(KillSrc));
216 return;
217 }
218 unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
219 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
220 .addReg(DestReg, RegState::Undef)
221 .addReg(SrcReg, getKillRegState(KillSrc))
222 .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
223}
224
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000225// If MI is a simple load or store for a frame object, return the register
226// it loads or stores and set FrameIndex to the index of the frame object.
227// Return 0 otherwise.
228//
229// Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000230static int isSimpleMove(const MachineInstr &MI, int &FrameIndex,
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000231 unsigned Flag) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000232 const MCInstrDesc &MCID = MI.getDesc();
233 if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() &&
234 MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) {
235 FrameIndex = MI.getOperand(1).getIndex();
236 return MI.getOperand(0).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000237 }
238 return 0;
239}
240
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000241unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000242 int &FrameIndex) const {
243 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
244}
245
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000246unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000247 int &FrameIndex) const {
248 return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
249}
250
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000251bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI,
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000252 int &DestFrameIndex,
253 int &SrcFrameIndex) const {
254 // Check for MVC 0(Length,FI1),0(FI2)
Matthias Braun941a7052016-07-28 18:40:00 +0000255 const MachineFrameInfo &MFI = MI.getParent()->getParent()->getFrameInfo();
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000256 if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() ||
257 MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() ||
258 MI.getOperand(4).getImm() != 0)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000259 return false;
260
261 // Check that Length covers the full slots.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000262 int64_t Length = MI.getOperand(2).getImm();
263 unsigned FI1 = MI.getOperand(0).getIndex();
264 unsigned FI2 = MI.getOperand(3).getIndex();
Matthias Braun941a7052016-07-28 18:40:00 +0000265 if (MFI.getObjectSize(FI1) != Length ||
266 MFI.getObjectSize(FI2) != Length)
Richard Sandifordc40f27b2013-07-05 14:38:48 +0000267 return false;
268
269 DestFrameIndex = FI1;
270 SrcFrameIndex = FI2;
271 return true;
272}
273
Jacques Pienaar71c30a12016-07-15 14:41:04 +0000274bool SystemZInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000275 MachineBasicBlock *&TBB,
276 MachineBasicBlock *&FBB,
277 SmallVectorImpl<MachineOperand> &Cond,
278 bool AllowModify) const {
279 // Most of the code and comments here are boilerplate.
280
281 // Start from the bottom of the block and work up, examining the
282 // terminator instructions.
283 MachineBasicBlock::iterator I = MBB.end();
284 while (I != MBB.begin()) {
285 --I;
286 if (I->isDebugValue())
287 continue;
288
289 // Working from the bottom, when we see a non-terminator instruction, we're
290 // done.
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000291 if (!isUnpredicatedTerminator(*I))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000292 break;
293
294 // A terminator that isn't a branch can't easily be handled by this
295 // analysis.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000296 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000297 return true;
298
299 // Can't handle indirect branches.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000300 SystemZII::Branch Branch(getBranchInfo(*I));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000301 if (!Branch.Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000302 return true;
303
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000304 // Punt on compound branches.
305 if (Branch.Type != SystemZII::BranchNormal)
306 return true;
307
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000308 if (Branch.CCMask == SystemZ::CCMASK_ANY) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000309 // Handle unconditional branches.
310 if (!AllowModify) {
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000311 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000312 continue;
313 }
314
315 // If the block has any instructions after a JMP, delete them.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000316 while (std::next(I) != MBB.end())
317 std::next(I)->eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000318
319 Cond.clear();
Craig Topper062a2ba2014-04-25 05:30:21 +0000320 FBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000321
322 // Delete the JMP if it's equivalent to a fall-through.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000323 if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000324 TBB = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000325 I->eraseFromParent();
326 I = MBB.end();
327 continue;
328 }
329
330 // TBB is used to indicate the unconditinal destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000331 TBB = Branch.Target->getMBB();
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000332 continue;
333 }
334
335 // Working from the bottom, handle the first conditional branch.
336 if (Cond.empty()) {
337 // FIXME: add X86-style branch swap
338 FBB = TBB;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000339 TBB = Branch.Target->getMBB();
Richard Sandiford3d768e32013-07-31 12:30:20 +0000340 Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000341 Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000342 continue;
343 }
344
345 // Handle subsequent conditional branches.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000346 assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000347
348 // Only handle the case where all conditional branches branch to the same
349 // destination.
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000350 if (TBB != Branch.Target->getMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000351 return true;
352
353 // If the conditions are the same, we can leave them alone.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000354 unsigned OldCCValid = Cond[0].getImm();
355 unsigned OldCCMask = Cond[1].getImm();
356 if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000357 continue;
358
359 // FIXME: Try combining conditions like X86 does. Should be easy on Z!
Richard Sandiford3d768e32013-07-31 12:30:20 +0000360 return false;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000361 }
362
363 return false;
364}
365
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000366unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB,
367 int *BytesRemoved) const {
368 assert(!BytesRemoved && "code size not handled");
369
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000370 // Most of the code and comments here are boilerplate.
371 MachineBasicBlock::iterator I = MBB.end();
372 unsigned Count = 0;
373
374 while (I != MBB.begin()) {
375 --I;
376 if (I->isDebugValue())
377 continue;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000378 if (!I->isBranch())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000379 break;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000380 if (!getBranchInfo(*I).Target->isMBB())
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000381 break;
382 // Remove the branch.
383 I->eraseFromParent();
384 I = MBB.end();
385 ++Count;
386 }
387
388 return Count;
389}
390
Richard Sandiford3d768e32013-07-31 12:30:20 +0000391bool SystemZInstrInfo::
392ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
393 assert(Cond.size() == 2 && "Invalid condition");
394 Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
395 return false;
396}
397
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000398unsigned SystemZInstrInfo::insertBranch(MachineBasicBlock &MBB,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000399 MachineBasicBlock *TBB,
400 MachineBasicBlock *FBB,
401 ArrayRef<MachineOperand> Cond,
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000402 const DebugLoc &DL,
403 int *BytesAdded) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000404 // In this function we output 32-bit branches, which should always
405 // have enough range. They can be shortened and relaxed by later code
406 // in the pipeline, if desired.
407
408 // Shouldn't be a fall through.
Matt Arsenaulte8e0f5c2016-09-14 17:24:15 +0000409 assert(TBB && "insertBranch must not be told to insert a fallthrough");
Richard Sandiford3d768e32013-07-31 12:30:20 +0000410 assert((Cond.size() == 2 || Cond.size() == 0) &&
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000411 "SystemZ branch conditions have one component!");
Matt Arsenaulta2b036e2016-09-14 17:23:48 +0000412 assert(!BytesAdded && "code size not handled");
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000413
414 if (Cond.empty()) {
415 // Unconditional branch?
416 assert(!FBB && "Unconditional branch with multiple successors!");
Richard Sandiford312425f2013-05-20 14:23:08 +0000417 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000418 return 1;
419 }
420
421 // Conditional branch.
422 unsigned Count = 0;
Richard Sandiford3d768e32013-07-31 12:30:20 +0000423 unsigned CCValid = Cond[0].getImm();
424 unsigned CCMask = Cond[1].getImm();
425 BuildMI(&MBB, DL, get(SystemZ::BRC))
426 .addImm(CCValid).addImm(CCMask).addMBB(TBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000427 ++Count;
428
429 if (FBB) {
430 // Two-way Conditional branch. Insert the second branch.
Richard Sandiford312425f2013-05-20 14:23:08 +0000431 BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000432 ++Count;
433 }
434 return Count;
435}
436
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000437bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
438 unsigned &SrcReg2, int &Mask,
439 int &Value) const {
440 assert(MI.isCompare() && "Caller should have checked for a comparison");
Richard Sandiford564681c2013-08-12 10:28:10 +0000441
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000442 if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() &&
443 MI.getOperand(1).isImm()) {
444 SrcReg = MI.getOperand(0).getReg();
Richard Sandiford564681c2013-08-12 10:28:10 +0000445 SrcReg2 = 0;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000446 Value = MI.getOperand(1).getImm();
Richard Sandiford564681c2013-08-12 10:28:10 +0000447 Mask = ~0;
448 return true;
449 }
450
451 return false;
452}
453
Richard Sandiforda5901252013-08-16 10:22:54 +0000454// If Reg is a virtual register, return its definition, otherwise return null.
455static MachineInstr *getDef(unsigned Reg,
456 const MachineRegisterInfo *MRI) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000457 if (TargetRegisterInfo::isPhysicalRegister(Reg))
Craig Topper062a2ba2014-04-25 05:30:21 +0000458 return nullptr;
Richard Sandiford564681c2013-08-12 10:28:10 +0000459 return MRI->getUniqueVRegDef(Reg);
460}
461
462// Return true if MI is a shift of type Opcode by Imm bits.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000463static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000464 return (MI->getOpcode() == Opcode &&
465 !MI->getOperand(2).getReg() &&
466 MI->getOperand(3).getImm() == Imm);
467}
468
Richard Sandiforda5901252013-08-16 10:22:54 +0000469// If the destination of MI has no uses, delete it as dead.
470static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
471 if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
472 MI->eraseFromParent();
473}
474
Richard Sandiford564681c2013-08-12 10:28:10 +0000475// Compare compares SrcReg against zero. Check whether SrcReg contains
Richard Sandiforda5901252013-08-16 10:22:54 +0000476// the result of an IPM sequence whose input CC survives until Compare,
477// and whether Compare is therefore redundant. Delete it and return
478// true if so.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000479static bool removeIPMBasedCompare(MachineInstr &Compare, unsigned SrcReg,
Richard Sandiforda5901252013-08-16 10:22:54 +0000480 const MachineRegisterInfo *MRI,
481 const TargetRegisterInfo *TRI) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000482 MachineInstr *LGFR = nullptr;
Richard Sandiforda5901252013-08-16 10:22:54 +0000483 MachineInstr *RLL = getDef(SrcReg, MRI);
Richard Sandiforde3827752013-08-16 10:55:47 +0000484 if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
485 LGFR = RLL;
486 RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
487 }
Richard Sandiforda5901252013-08-16 10:22:54 +0000488 if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
Richard Sandiford564681c2013-08-12 10:28:10 +0000489 return false;
490
Richard Sandiforda5901252013-08-16 10:22:54 +0000491 MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000492 if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
Richard Sandiford564681c2013-08-12 10:28:10 +0000493 return false;
494
Richard Sandiforda5901252013-08-16 10:22:54 +0000495 MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000496 if (!IPM || IPM->getOpcode() != SystemZ::IPM)
497 return false;
498
499 // Check that there are no assignments to CC between the IPM and Compare,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000500 if (IPM->getParent() != Compare.getParent())
Richard Sandiford564681c2013-08-12 10:28:10 +0000501 return false;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000502 MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare.getIterator();
Richard Sandiford564681c2013-08-12 10:28:10 +0000503 for (++MBBI; MBBI != MBBE; ++MBBI) {
Duncan P. N. Exon Smith4565ec02016-07-12 01:39:01 +0000504 MachineInstr &MI = *MBBI;
505 if (MI.modifiesRegister(SystemZ::CC, TRI))
Richard Sandiford564681c2013-08-12 10:28:10 +0000506 return false;
507 }
508
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000509 Compare.eraseFromParent();
Richard Sandiforde3827752013-08-16 10:55:47 +0000510 if (LGFR)
511 eraseIfDead(LGFR, MRI);
Richard Sandiforda5901252013-08-16 10:22:54 +0000512 eraseIfDead(RLL, MRI);
513 eraseIfDead(SRL, MRI);
514 eraseIfDead(IPM, MRI);
515
Richard Sandiford564681c2013-08-12 10:28:10 +0000516 return true;
517}
518
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000519bool SystemZInstrInfo::optimizeCompareInstr(
520 MachineInstr &Compare, unsigned SrcReg, unsigned SrcReg2, int Mask,
521 int Value, const MachineRegisterInfo *MRI) const {
Richard Sandiford564681c2013-08-12 10:28:10 +0000522 assert(!SrcReg2 && "Only optimizing constant comparisons so far");
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000523 bool IsLogical = (Compare.getDesc().TSFlags & SystemZII::IsLogical) != 0;
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000524 return Value == 0 && !IsLogical &&
525 removeIPMBasedCompare(Compare, SrcReg, MRI, &RI);
Richard Sandiford564681c2013-08-12 10:28:10 +0000526}
527
Richard Sandifordf2404162013-07-25 09:11:15 +0000528// If Opcode is a move that has a conditional variant, return that variant,
529// otherwise return 0.
530static unsigned getConditionalMove(unsigned Opcode) {
531 switch (Opcode) {
532 case SystemZ::LR: return SystemZ::LOCR;
533 case SystemZ::LGR: return SystemZ::LOCGR;
534 default: return 0;
535 }
536}
537
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000538static unsigned getConditionalLoadImmediate(unsigned Opcode) {
539 switch (Opcode) {
540 case SystemZ::LHI: return SystemZ::LOCHI;
541 case SystemZ::LGHI: return SystemZ::LOCGHI;
542 default: return 0;
543 }
544}
545
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000546bool SystemZInstrInfo::isPredicable(MachineInstr &MI) const {
547 unsigned Opcode = MI.getOpcode();
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000548 if (STI.hasLoadStoreOnCond() && getConditionalMove(Opcode))
549 return true;
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000550 if (STI.hasLoadStoreOnCond2() && getConditionalLoadImmediate(Opcode))
551 return true;
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000552 if (Opcode == SystemZ::Return ||
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000553 Opcode == SystemZ::Trap ||
Ulrich Weigand848a5132016-04-11 12:12:32 +0000554 Opcode == SystemZ::CallJG ||
555 Opcode == SystemZ::CallBR)
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000556 return true;
557 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000558}
559
560bool SystemZInstrInfo::
561isProfitableToIfCvt(MachineBasicBlock &MBB,
562 unsigned NumCycles, unsigned ExtraPredCycles,
Cong Houc536bd92015-09-10 23:10:42 +0000563 BranchProbability Probability) const {
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000564 // Avoid using conditional returns at the end of a loop (since then
565 // we'd need to emit an unconditional branch to the beginning anyway,
566 // making the loop body longer). This doesn't apply for low-probability
567 // loops (eg. compare-and-swap retry), so just decide based on branch
568 // probability instead of looping structure.
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000569 // However, since Compare and Trap instructions cost the same as a regular
570 // Compare instruction, we should allow the if conversion to convert this
571 // into a Conditional Compare regardless of the branch probability.
572 if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap &&
573 MBB.succ_empty() && Probability < BranchProbability(1, 8))
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000574 return false;
Richard Sandifordf2404162013-07-25 09:11:15 +0000575 // For now only convert single instructions.
576 return NumCycles == 1;
577}
578
579bool SystemZInstrInfo::
580isProfitableToIfCvt(MachineBasicBlock &TMBB,
581 unsigned NumCyclesT, unsigned ExtraPredCyclesT,
582 MachineBasicBlock &FMBB,
583 unsigned NumCyclesF, unsigned ExtraPredCyclesF,
Cong Houc536bd92015-09-10 23:10:42 +0000584 BranchProbability Probability) const {
Richard Sandifordf2404162013-07-25 09:11:15 +0000585 // For now avoid converting mutually-exclusive cases.
586 return false;
587}
588
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000589bool SystemZInstrInfo::
590isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
591 BranchProbability Probability) const {
592 // For now only duplicate single instructions.
593 return NumCycles == 1;
594}
595
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000596bool SystemZInstrInfo::PredicateInstruction(
597 MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
Richard Sandiford3d768e32013-07-31 12:30:20 +0000598 assert(Pred.size() == 2 && "Invalid condition");
599 unsigned CCValid = Pred[0].getImm();
600 unsigned CCMask = Pred[1].getImm();
Richard Sandifordf2404162013-07-25 09:11:15 +0000601 assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000602 unsigned Opcode = MI.getOpcode();
Eric Christopher673b3af2014-06-27 07:01:17 +0000603 if (STI.hasLoadStoreOnCond()) {
Richard Sandifordf2404162013-07-25 09:11:15 +0000604 if (unsigned CondOpcode = getConditionalMove(Opcode)) {
Duncan P. N. Exon Smith6307eb52016-02-23 02:46:52 +0000605 MI.setDesc(get(CondOpcode));
606 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
607 .addImm(CCValid)
608 .addImm(CCMask)
609 .addReg(SystemZ::CC, RegState::Implicit);
Richard Sandifordf2404162013-07-25 09:11:15 +0000610 return true;
611 }
612 }
Zhan Jun Liaudef708a2016-07-11 18:45:03 +0000613 if (STI.hasLoadStoreOnCond2()) {
614 if (unsigned CondOpcode = getConditionalLoadImmediate(Opcode)) {
615 MI.setDesc(get(CondOpcode));
616 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
617 .addImm(CCValid)
618 .addImm(CCMask)
619 .addReg(SystemZ::CC, RegState::Implicit);
620 return true;
621 }
622 }
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000623 if (Opcode == SystemZ::Trap) {
624 MI.setDesc(get(SystemZ::CondTrap));
625 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
626 .addImm(CCValid).addImm(CCMask)
627 .addReg(SystemZ::CC, RegState::Implicit);
628 return true;
629 }
Ulrich Weigand2eb027d2016-04-07 16:11:44 +0000630 if (Opcode == SystemZ::Return) {
631 MI.setDesc(get(SystemZ::CondReturn));
632 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
633 .addImm(CCValid).addImm(CCMask)
634 .addReg(SystemZ::CC, RegState::Implicit);
635 return true;
636 }
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000637 if (Opcode == SystemZ::CallJG) {
Zhan Jun Liaua5d60af2016-07-07 15:34:46 +0000638 MachineOperand FirstOp = MI.getOperand(0);
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000639 const uint32_t *RegMask = MI.getOperand(1).getRegMask();
640 MI.RemoveOperand(1);
641 MI.RemoveOperand(0);
642 MI.setDesc(get(SystemZ::CallBRCL));
643 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
644 .addImm(CCValid).addImm(CCMask)
Zhan Jun Liaua5d60af2016-07-07 15:34:46 +0000645 .addOperand(FirstOp)
Ulrich Weigandfa2dffb2016-04-08 17:22:19 +0000646 .addRegMask(RegMask)
647 .addReg(SystemZ::CC, RegState::Implicit);
648 return true;
649 }
Ulrich Weigand848a5132016-04-11 12:12:32 +0000650 if (Opcode == SystemZ::CallBR) {
651 const uint32_t *RegMask = MI.getOperand(0).getRegMask();
652 MI.RemoveOperand(0);
653 MI.setDesc(get(SystemZ::CallBCR));
654 MachineInstrBuilder(*MI.getParent()->getParent(), MI)
655 .addImm(CCValid).addImm(CCMask)
656 .addRegMask(RegMask)
657 .addReg(SystemZ::CC, RegState::Implicit);
658 return true;
659 }
Richard Sandifordf2404162013-07-25 09:11:15 +0000660 return false;
661}
662
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000663void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
664 MachineBasicBlock::iterator MBBI,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000665 const DebugLoc &DL, unsigned DestReg,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000666 unsigned SrcReg, bool KillSrc) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000667 // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
668 if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
Richard Sandiford87a44362013-09-30 10:28:35 +0000669 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
670 RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
671 copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
672 RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000673 return;
674 }
675
Richard Sandiford0755c932013-10-01 11:26:28 +0000676 if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
677 emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
678 return;
679 }
680
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000681 // Everything else needs only one instruction.
682 unsigned Opcode;
Richard Sandiford0755c932013-10-01 11:26:28 +0000683 if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000684 Opcode = SystemZ::LGR;
685 else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
Ulrich Weigandcdce0262016-03-14 13:50:03 +0000686 // For z13 we prefer LDR over LER to avoid partial register dependencies.
687 Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000688 else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
689 Opcode = SystemZ::LDR;
690 else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
691 Opcode = SystemZ::LXR;
Ulrich Weigand49506d72015-05-05 19:28:34 +0000692 else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
693 Opcode = SystemZ::VLR32;
694 else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
695 Opcode = SystemZ::VLR64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000696 else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
697 Opcode = SystemZ::VLR;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000698 else
699 llvm_unreachable("Impossible reg-to-reg copy");
700
701 BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
702 .addReg(SrcReg, getKillRegState(KillSrc));
703}
704
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000705void SystemZInstrInfo::storeRegToStackSlot(
706 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
707 bool isKill, int FrameIdx, const TargetRegisterClass *RC,
708 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000709 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
710
711 // Callers may expect a single instruction, so keep 128-bit moves
712 // together for now and lower them after register allocation.
713 unsigned LoadOpcode, StoreOpcode;
714 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
715 addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000716 .addReg(SrcReg, getKillRegState(isKill)),
717 FrameIdx);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000718}
719
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000720void SystemZInstrInfo::loadRegFromStackSlot(
721 MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
722 int FrameIdx, const TargetRegisterClass *RC,
723 const TargetRegisterInfo *TRI) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000724 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
725
726 // Callers may expect a single instruction, so keep 128-bit moves
727 // together for now and lower them after register allocation.
728 unsigned LoadOpcode, StoreOpcode;
729 getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
730 addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
731 FrameIdx);
732}
733
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000734// Return true if MI is a simple load or store with a 12-bit displacement
735// and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
736static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
737 const MCInstrDesc &MCID = MI->getDesc();
738 return ((MCID.TSFlags & Flag) &&
739 isUInt<12>(MI->getOperand(2).getImm()) &&
740 MI->getOperand(3).getReg() == 0);
741}
742
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000743namespace {
Richard Sandifordc2312692014-03-06 10:38:30 +0000744struct LogicOp {
745 LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
746 LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
747 : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000748
Aaron Ballmanb46962f2015-02-15 22:00:20 +0000749 explicit operator bool() const { return RegSize; }
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000750
Richard Sandifordc2312692014-03-06 10:38:30 +0000751 unsigned RegSize, ImmLSB, ImmSize;
752};
753} // end anonymous namespace
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000754
755static LogicOp interpretAndImmediate(unsigned Opcode) {
756 switch (Opcode) {
Richard Sandiford70284282013-10-01 14:20:41 +0000757 case SystemZ::NILMux: return LogicOp(32, 0, 16);
758 case SystemZ::NIHMux: return LogicOp(32, 16, 16);
Richard Sandiford652784e2013-09-25 11:11:53 +0000759 case SystemZ::NILL64: return LogicOp(64, 0, 16);
760 case SystemZ::NILH64: return LogicOp(64, 16, 16);
Richard Sandiford70284282013-10-01 14:20:41 +0000761 case SystemZ::NIHL64: return LogicOp(64, 32, 16);
762 case SystemZ::NIHH64: return LogicOp(64, 48, 16);
763 case SystemZ::NIFMux: return LogicOp(32, 0, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +0000764 case SystemZ::NILF64: return LogicOp(64, 0, 32);
Richard Sandiford70284282013-10-01 14:20:41 +0000765 case SystemZ::NIHF64: return LogicOp(64, 32, 32);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000766 default: return LogicOp();
767 }
768}
769
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000770static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) {
771 if (OldMI->registerDefIsDead(SystemZ::CC)) {
772 MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC);
773 if (CCDef != nullptr)
774 CCDef->setIsDead(true);
775 }
776}
777
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000778// Used to return from convertToThreeAddress after replacing two-address
779// instruction OldMI with three-address instruction NewMI.
780static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
781 MachineInstr *NewMI,
782 LiveVariables *LV) {
783 if (LV) {
784 unsigned NumOps = OldMI->getNumOperands();
785 for (unsigned I = 1; I < NumOps; ++I) {
786 MachineOperand &Op = OldMI->getOperand(I);
787 if (Op.isReg() && Op.isKill())
Duncan P. N. Exon Smithd26fdc82016-07-01 01:51:32 +0000788 LV->replaceKillInstruction(Op.getReg(), *OldMI, *NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000789 }
790 }
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000791 transferDeadCC(OldMI, NewMI);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000792 return NewMI;
793}
794
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000795MachineInstr *SystemZInstrInfo::convertToThreeAddress(
796 MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
797 MachineBasicBlock *MBB = MI.getParent();
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000798 MachineFunction *MF = MBB->getParent();
799 MachineRegisterInfo &MRI = MF->getRegInfo();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000800
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000801 unsigned Opcode = MI.getOpcode();
802 unsigned NumOps = MI.getNumOperands();
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000803
804 // Try to convert something like SLL into SLLK, if supported.
805 // We prefer to keep the two-operand form where possible both
806 // because it tends to be shorter and because some instructions
807 // have memory forms that can be used during spilling.
Eric Christopher673b3af2014-06-27 07:01:17 +0000808 if (STI.hasDistinctOps()) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000809 MachineOperand &Dest = MI.getOperand(0);
810 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford42a694f2013-10-01 14:53:46 +0000811 unsigned DestReg = Dest.getReg();
812 unsigned SrcReg = Src.getReg();
813 // AHIMux is only really a three-operand instruction when both operands
814 // are low registers. Try to constrain both operands to be low if
815 // possible.
816 if (Opcode == SystemZ::AHIMux &&
817 TargetRegisterInfo::isVirtualRegister(DestReg) &&
818 TargetRegisterInfo::isVirtualRegister(SrcReg) &&
819 MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
820 MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
821 MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
822 MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
823 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000824 int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
825 if (ThreeOperandOpcode >= 0) {
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000826 // Create three address instruction without adding the implicit
827 // operands. Those will instead be copied over from the original
828 // instruction by the loop below.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000829 MachineInstrBuilder MIB(
830 *MF, MF->CreateMachineInstr(get(ThreeOperandOpcode), MI.getDebugLoc(),
831 /*NoImplicit=*/true));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000832 MIB.addOperand(Dest);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000833 // Keep the kill state, but drop the tied flag.
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000834 MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000835 // Keep the remaining operands as-is.
836 for (unsigned I = 2; I < NumOps; ++I)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000837 MIB.addOperand(MI.getOperand(I));
Jonas Paulsson7fa69cd2015-12-04 12:48:51 +0000838 MBB->insert(MI, MIB);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000839 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000840 }
841 }
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000842
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000843 // Try to convert an AND into an RISBG-type instruction.
844 if (LogicOp And = interpretAndImmediate(Opcode)) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000845 uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB;
Richard Sandiford70284282013-10-01 14:20:41 +0000846 // AND IMMEDIATE leaves the other bits of the register unchanged.
847 Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
848 unsigned Start, End;
849 if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
850 unsigned NewOpcode;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000851 if (And.RegSize == 64) {
Richard Sandiford70284282013-10-01 14:20:41 +0000852 NewOpcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000853 // Prefer RISBGN if available, since it does not clobber CC.
854 if (STI.hasMiscellaneousExtensions())
855 NewOpcode = SystemZ::RISBGN;
856 } else {
Richard Sandiford70284282013-10-01 14:20:41 +0000857 NewOpcode = SystemZ::RISBMux;
858 Start &= 31;
859 End &= 31;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000860 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000861 MachineOperand &Dest = MI.getOperand(0);
862 MachineOperand &Src = MI.getOperand(1);
Richard Sandiford70284282013-10-01 14:20:41 +0000863 MachineInstrBuilder MIB =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000864 BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode))
865 .addOperand(Dest)
866 .addReg(0)
867 .addReg(Src.getReg(), getKillRegState(Src.isKill()),
868 Src.getSubReg())
869 .addImm(Start)
870 .addImm(End + 128)
871 .addImm(0);
872 return finishConvertToThreeAddress(&MI, MIB, LV);
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000873 }
874 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000875 return nullptr;
Richard Sandifordff6c5a52013-07-19 16:12:08 +0000876}
877
Keno Fischere70b31f2015-06-08 20:09:58 +0000878MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000879 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000880 MachineBasicBlock::iterator InsertPt, int FrameIndex,
881 LiveIntervals *LIS) const {
882 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
Matthias Braun941a7052016-07-28 18:40:00 +0000883 const MachineFrameInfo &MFI = MF.getFrameInfo();
884 unsigned Size = MFI.getObjectSize(FrameIndex);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000885 unsigned Opcode = MI.getOpcode();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000886
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000887 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000888 if (LIS != nullptr && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
889 isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) {
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000890
891 // Check CC liveness, since new instruction introduces a dead
892 // def of CC.
893 MCRegUnitIterator CCUnit(SystemZ::CC, TRI);
894 LiveRange &CCLiveRange = LIS->getRegUnit(*CCUnit);
895 ++CCUnit;
896 assert (!CCUnit.isValid() && "CC only has one reg unit.");
897 SlotIndex MISlot =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000898 LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot();
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000899 if (!CCLiveRange.liveAt(MISlot)) {
900 // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000901 MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt,
902 MI.getDebugLoc(), get(SystemZ::AGSI))
903 .addFrameIndex(FrameIndex)
904 .addImm(0)
905 .addImm(MI.getOperand(2).getImm());
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +0000906 BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true);
907 CCLiveRange.createDeadDef(MISlot, LIS->getVNInfoAllocator());
908 return BuiltMI;
909 }
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000910 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000911 return nullptr;
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000912 }
913
914 // All other cases require a single operand.
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000915 if (Ops.size() != 1)
Craig Topper062a2ba2014-04-25 05:30:21 +0000916 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000917
918 unsigned OpNum = Ops[0];
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000919 assert(Size ==
920 MF.getRegInfo()
921 .getRegClass(MI.getOperand(OpNum).getReg())
922 ->getSize() &&
Benjamin Kramer421c8fb2013-07-02 21:17:31 +0000923 "Invalid size combination");
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000924
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000925 if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 &&
926 isInt<8>(MI.getOperand(2).getImm())) {
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000927 // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
928 Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000929 MachineInstr *BuiltMI =
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000930 BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
931 .addFrameIndex(FrameIndex)
932 .addImm(0)
933 .addImm(MI.getOperand(2).getImm());
934 transferDeadCC(&MI, BuiltMI);
Jonas Paulsson9028acf2016-05-02 09:37:40 +0000935 return BuiltMI;
Richard Sandiford6af6ff12013-10-15 08:42:59 +0000936 }
937
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000938 if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
939 bool Op0IsGPR = (Opcode == SystemZ::LGDR);
940 bool Op1IsGPR = (Opcode == SystemZ::LDGR);
941 // If we're spilling the destination of an LDGR or LGDR, store the
942 // source register instead.
943 if (OpNum == 0) {
944 unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000945 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000946 get(StoreOpcode))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000947 .addOperand(MI.getOperand(1))
Keno Fischere70b31f2015-06-08 20:09:58 +0000948 .addFrameIndex(FrameIndex)
949 .addImm(0)
950 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000951 }
952 // If we're spilling the source of an LDGR or LGDR, load the
953 // destination register instead.
954 if (OpNum == 1) {
955 unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000956 unsigned Dest = MI.getOperand(0).getReg();
957 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000958 get(LoadOpcode), Dest)
959 .addFrameIndex(FrameIndex)
960 .addImm(0)
961 .addReg(0);
Richard Sandiford3f0edc22013-07-12 08:37:17 +0000962 }
963 }
964
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000965 // Look for cases where the source of a simple store or the destination
966 // of a simple load is being spilled. Try to use MVC instead.
967 //
968 // Although MVC is in practice a fast choice in these cases, it is still
969 // logically a bytewise copy. This means that we cannot use it if the
Richard Sandiford067817e2013-09-27 15:29:20 +0000970 // load or store is volatile. We also wouldn't be able to use MVC if
971 // the two memories partially overlap, but that case cannot occur here,
972 // because we know that one of the memories is a full frame index.
973 //
974 // For performance reasons, we also want to avoid using MVC if the addresses
975 // might be equal. We don't worry about that case here, because spill slot
976 // coloring happens later, and because we have special code to remove
977 // MVCs that turn out to be redundant.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000978 if (OpNum == 0 && MI.hasOneMemOperand()) {
979 MachineMemOperand *MMO = *MI.memoperands_begin();
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000980 if (MMO->getSize() == Size && !MMO->isVolatile()) {
981 // Handle conversion of loads.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000982 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) {
983 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000984 get(SystemZ::MVC))
985 .addFrameIndex(FrameIndex)
986 .addImm(0)
987 .addImm(Size)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000988 .addOperand(MI.getOperand(1))
989 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +0000990 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +0000991 }
992 // Handle conversion of stores.
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000993 if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) {
994 return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
Keno Fischere70b31f2015-06-08 20:09:58 +0000995 get(SystemZ::MVC))
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000996 .addOperand(MI.getOperand(1))
997 .addImm(MI.getOperand(2).getImm())
Keno Fischere70b31f2015-06-08 20:09:58 +0000998 .addImm(Size)
999 .addFrameIndex(FrameIndex)
1000 .addImm(0)
1001 .addMemOperand(MMO);
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001002 }
1003 }
1004 }
1005
Richard Sandiforded1fab62013-07-03 10:10:02 +00001006 // If the spilled operand is the final one, try to change <INSN>R
1007 // into <INSN>.
Richard Sandiford3f0edc22013-07-12 08:37:17 +00001008 int MemOpcode = SystemZ::getMemOpcode(Opcode);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001009 if (MemOpcode >= 0) {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001010 unsigned NumOps = MI.getNumExplicitOperands();
Richard Sandiforded1fab62013-07-03 10:10:02 +00001011 if (OpNum == NumOps - 1) {
1012 const MCInstrDesc &MemDesc = get(MemOpcode);
1013 uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
1014 assert(AccessBytes != 0 && "Size of access should be known");
1015 assert(AccessBytes <= Size && "Access outside the frame index");
1016 uint64_t Offset = Size - AccessBytes;
Keno Fischere70b31f2015-06-08 20:09:58 +00001017 MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001018 MI.getDebugLoc(), get(MemOpcode));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001019 for (unsigned I = 0; I < OpNum; ++I)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001020 MIB.addOperand(MI.getOperand(I));
Richard Sandiforded1fab62013-07-03 10:10:02 +00001021 MIB.addFrameIndex(FrameIndex).addImm(Offset);
1022 if (MemDesc.TSFlags & SystemZII::HasIndex)
1023 MIB.addReg(0);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001024 transferDeadCC(&MI, MIB);
Richard Sandiforded1fab62013-07-03 10:10:02 +00001025 return MIB;
1026 }
1027 }
1028
Craig Topper062a2ba2014-04-25 05:30:21 +00001029 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001030}
1031
Keno Fischere70b31f2015-06-08 20:09:58 +00001032MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001033 MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
1034 MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
Jonas Paulsson8e5b0c62016-05-10 08:09:37 +00001035 LiveIntervals *LIS) const {
Craig Topper062a2ba2014-04-25 05:30:21 +00001036 return nullptr;
Richard Sandifordf6bae1e2013-07-02 15:28:56 +00001037}
1038
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001039bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1040 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001041 case SystemZ::L128:
1042 splitMove(MI, SystemZ::LG);
1043 return true;
1044
1045 case SystemZ::ST128:
1046 splitMove(MI, SystemZ::STG);
1047 return true;
1048
1049 case SystemZ::LX:
1050 splitMove(MI, SystemZ::LD);
1051 return true;
1052
1053 case SystemZ::STX:
1054 splitMove(MI, SystemZ::STD);
1055 return true;
1056
Richard Sandiford89e160d2013-10-01 12:11:47 +00001057 case SystemZ::LBMux:
1058 expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
1059 return true;
1060
1061 case SystemZ::LHMux:
1062 expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
1063 return true;
1064
Richard Sandiford21235a22013-10-01 12:49:07 +00001065 case SystemZ::LLCRMux:
1066 expandZExtPseudo(MI, SystemZ::LLCR, 8);
1067 return true;
1068
1069 case SystemZ::LLHRMux:
1070 expandZExtPseudo(MI, SystemZ::LLHR, 16);
1071 return true;
1072
Richard Sandiford0d46b1a2013-10-01 12:19:08 +00001073 case SystemZ::LLCMux:
1074 expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
1075 return true;
1076
1077 case SystemZ::LLHMux:
1078 expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
1079 return true;
1080
Richard Sandiford0755c932013-10-01 11:26:28 +00001081 case SystemZ::LMux:
1082 expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
1083 return true;
1084
Richard Sandiford5469c392013-10-01 12:22:49 +00001085 case SystemZ::STCMux:
1086 expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
1087 return true;
1088
1089 case SystemZ::STHMux:
1090 expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
1091 return true;
1092
Richard Sandiford0755c932013-10-01 11:26:28 +00001093 case SystemZ::STMux:
1094 expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
1095 return true;
1096
Richard Sandiford01240232013-10-01 13:02:28 +00001097 case SystemZ::LHIMux:
1098 expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
1099 return true;
1100
1101 case SystemZ::IIFMux:
1102 expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
1103 return true;
1104
Richard Sandiford1a569312013-10-01 13:18:56 +00001105 case SystemZ::IILMux:
1106 expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
1107 return true;
1108
1109 case SystemZ::IIHMux:
1110 expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
1111 return true;
1112
Richard Sandiford70284282013-10-01 14:20:41 +00001113 case SystemZ::NIFMux:
1114 expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1115 return true;
1116
1117 case SystemZ::NILMux:
1118 expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1119 return true;
1120
1121 case SystemZ::NIHMux:
1122 expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1123 return true;
1124
Richard Sandiford6e96ac62013-10-01 13:22:41 +00001125 case SystemZ::OIFMux:
1126 expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1127 return true;
1128
1129 case SystemZ::OILMux:
1130 expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1131 return true;
1132
1133 case SystemZ::OIHMux:
1134 expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1135 return true;
1136
Richard Sandiford5718dac2013-10-01 14:08:44 +00001137 case SystemZ::XIFMux:
1138 expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1139 return true;
1140
Richard Sandiford2cac7632013-10-01 14:41:52 +00001141 case SystemZ::TMLMux:
1142 expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1143 return true;
1144
1145 case SystemZ::TMHMux:
1146 expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1147 return true;
1148
Richard Sandiford42a694f2013-10-01 14:53:46 +00001149 case SystemZ::AHIMux:
1150 expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1151 return true;
1152
1153 case SystemZ::AHIMuxK:
1154 expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1155 return true;
1156
1157 case SystemZ::AFIMux:
1158 expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1159 return true;
1160
Richard Sandiforda9ac0e02013-10-01 14:56:23 +00001161 case SystemZ::CFIMux:
1162 expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1163 return true;
1164
1165 case SystemZ::CLFIMux:
1166 expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1167 return true;
1168
Richard Sandifordb63e3002013-10-01 15:00:44 +00001169 case SystemZ::CMux:
1170 expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1171 return true;
1172
1173 case SystemZ::CLMux:
1174 expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1175 return true;
1176
Richard Sandiford70284282013-10-01 14:20:41 +00001177 case SystemZ::RISBMux: {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001178 bool DestIsHigh = isHighReg(MI.getOperand(0).getReg());
1179 bool SrcIsHigh = isHighReg(MI.getOperand(2).getReg());
Richard Sandiford70284282013-10-01 14:20:41 +00001180 if (SrcIsHigh == DestIsHigh)
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001181 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
Richard Sandiford70284282013-10-01 14:20:41 +00001182 else {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001183 MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1184 MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32);
Richard Sandiford70284282013-10-01 14:20:41 +00001185 }
1186 return true;
1187 }
1188
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001189 case SystemZ::ADJDYNALLOC:
1190 splitAdjDynAlloc(MI);
1191 return true;
1192
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001193 case TargetOpcode::LOAD_STACK_GUARD:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001194 expandLoadStackGuard(&MI);
Marcin Koscielnickiaef3b5b2016-04-24 13:57:49 +00001195 return true;
1196
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001197 default:
1198 return false;
1199 }
1200}
1201
Sjoerd Meijer0eb96ed2016-07-29 08:16:16 +00001202unsigned SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001203 if (MI.getOpcode() == TargetOpcode::INLINEASM) {
1204 const MachineFunction *MF = MI.getParent()->getParent();
1205 const char *AsmStr = MI.getOperand(0).getSymbolName();
Richard Sandiford312425f2013-05-20 14:23:08 +00001206 return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1207 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001208 return MI.getDesc().getSize();
Richard Sandiford312425f2013-05-20 14:23:08 +00001209}
1210
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001211SystemZII::Branch
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001212SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const {
1213 switch (MI.getOpcode()) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001214 case SystemZ::BR:
1215 case SystemZ::J:
1216 case SystemZ::JG:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001217 return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001218 SystemZ::CCMASK_ANY, &MI.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001219
1220 case SystemZ::BRC:
1221 case SystemZ::BRCL:
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001222 return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(),
1223 MI.getOperand(1).getImm(), &MI.getOperand(2));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001224
Richard Sandifordc2121252013-08-05 11:23:46 +00001225 case SystemZ::BRCT:
1226 return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001227 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001228
1229 case SystemZ::BRCTG:
1230 return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001231 SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +00001232
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001233 case SystemZ::CIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001234 case SystemZ::CRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001235 return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001236 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001237
Richard Sandiford93183ee2013-09-18 09:56:40 +00001238 case SystemZ::CLIJ:
1239 case SystemZ::CLRJ:
1240 return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001241 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001242
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001243 case SystemZ::CGIJ:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001244 case SystemZ::CGRJ:
Richard Sandiford3d768e32013-07-31 12:30:20 +00001245 return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001246 MI.getOperand(2).getImm(), &MI.getOperand(3));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001247
Richard Sandiford93183ee2013-09-18 09:56:40 +00001248 case SystemZ::CLGIJ:
1249 case SystemZ::CLGRJ:
1250 return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +00001251 MI.getOperand(2).getImm(), &MI.getOperand(3));
Richard Sandiford93183ee2013-09-18 09:56:40 +00001252
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001253 default:
Richard Sandiford53c9efd2013-05-28 10:13:54 +00001254 llvm_unreachable("Unrecognized branch opcode");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001255 }
1256}
1257
1258void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1259 unsigned &LoadOpcode,
1260 unsigned &StoreOpcode) const {
1261 if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1262 LoadOpcode = SystemZ::L;
Richard Sandiford6cbd7f02013-09-25 10:29:47 +00001263 StoreOpcode = SystemZ::ST;
Richard Sandiford0755c932013-10-01 11:26:28 +00001264 } else if (RC == &SystemZ::GRH32BitRegClass) {
1265 LoadOpcode = SystemZ::LFH;
1266 StoreOpcode = SystemZ::STFH;
1267 } else if (RC == &SystemZ::GRX32BitRegClass) {
1268 LoadOpcode = SystemZ::LMux;
1269 StoreOpcode = SystemZ::STMux;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001270 } else if (RC == &SystemZ::GR64BitRegClass ||
1271 RC == &SystemZ::ADDR64BitRegClass) {
1272 LoadOpcode = SystemZ::LG;
1273 StoreOpcode = SystemZ::STG;
1274 } else if (RC == &SystemZ::GR128BitRegClass ||
1275 RC == &SystemZ::ADDR128BitRegClass) {
1276 LoadOpcode = SystemZ::L128;
1277 StoreOpcode = SystemZ::ST128;
1278 } else if (RC == &SystemZ::FP32BitRegClass) {
1279 LoadOpcode = SystemZ::LE;
1280 StoreOpcode = SystemZ::STE;
1281 } else if (RC == &SystemZ::FP64BitRegClass) {
1282 LoadOpcode = SystemZ::LD;
1283 StoreOpcode = SystemZ::STD;
1284 } else if (RC == &SystemZ::FP128BitRegClass) {
1285 LoadOpcode = SystemZ::LX;
1286 StoreOpcode = SystemZ::STX;
Ulrich Weigand49506d72015-05-05 19:28:34 +00001287 } else if (RC == &SystemZ::VR32BitRegClass) {
1288 LoadOpcode = SystemZ::VL32;
1289 StoreOpcode = SystemZ::VST32;
1290 } else if (RC == &SystemZ::VR64BitRegClass) {
1291 LoadOpcode = SystemZ::VL64;
1292 StoreOpcode = SystemZ::VST64;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001293 } else if (RC == &SystemZ::VF128BitRegClass ||
1294 RC == &SystemZ::VR128BitRegClass) {
1295 LoadOpcode = SystemZ::VL;
1296 StoreOpcode = SystemZ::VST;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001297 } else
1298 llvm_unreachable("Unsupported regclass to load or store");
1299}
1300
1301unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1302 int64_t Offset) const {
1303 const MCInstrDesc &MCID = get(Opcode);
1304 int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1305 if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1306 // Get the instruction to use for unsigned 12-bit displacements.
1307 int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1308 if (Disp12Opcode >= 0)
1309 return Disp12Opcode;
1310
1311 // All address-related instructions can use unsigned 12-bit
1312 // displacements.
1313 return Opcode;
1314 }
1315 if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1316 // Get the instruction to use for signed 20-bit displacements.
1317 int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1318 if (Disp20Opcode >= 0)
1319 return Disp20Opcode;
1320
1321 // Check whether Opcode allows signed 20-bit displacements.
1322 if (MCID.TSFlags & SystemZII::Has20BitOffset)
1323 return Opcode;
1324 }
1325 return 0;
1326}
1327
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001328unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1329 switch (Opcode) {
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001330 case SystemZ::L: return SystemZ::LT;
1331 case SystemZ::LY: return SystemZ::LT;
1332 case SystemZ::LG: return SystemZ::LTG;
1333 case SystemZ::LGF: return SystemZ::LTGF;
1334 case SystemZ::LR: return SystemZ::LTR;
1335 case SystemZ::LGFR: return SystemZ::LTGFR;
1336 case SystemZ::LGR: return SystemZ::LTGR;
1337 case SystemZ::LER: return SystemZ::LTEBR;
1338 case SystemZ::LDR: return SystemZ::LTDBR;
1339 case SystemZ::LXR: return SystemZ::LTXBR;
Jonas Paulsson12629322015-10-01 18:12:28 +00001340 case SystemZ::LCDFR: return SystemZ::LCDBR;
1341 case SystemZ::LPDFR: return SystemZ::LPDBR;
1342 case SystemZ::LNDFR: return SystemZ::LNDBR;
1343 case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1344 case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1345 case SystemZ::LNDFR_32: return SystemZ::LNEBR;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001346 // On zEC12 we prefer to use RISBGN. But if there is a chance to
1347 // actually use the condition code, we may turn it back into RISGB.
1348 // Note that RISBG is not really a "load-and-test" instruction,
1349 // but sets the same condition code values, so is OK to use here.
1350 case SystemZ::RISBGN: return SystemZ::RISBG;
1351 default: return 0;
Richard Sandifordb49a3ab2013-08-05 11:03:20 +00001352 }
1353}
1354
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001355// Return true if Mask matches the regexp 0*1+0*, given that zero masks
1356// have already been filtered out. Store the first set bit in LSB and
1357// the number of set bits in Length if so.
1358static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1359 unsigned First = findFirstSet(Mask);
1360 uint64_t Top = (Mask >> First) + 1;
1361 if ((Top & -Top) == Top) {
1362 LSB = First;
1363 Length = findFirstSet(Top);
1364 return true;
1365 }
1366 return false;
1367}
1368
1369bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1370 unsigned &Start, unsigned &End) const {
1371 // Reject trivial all-zero masks.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001372 Mask &= allOnes(BitSize);
Richard Sandiford6a06ba32013-07-31 11:36:35 +00001373 if (Mask == 0)
1374 return false;
1375
1376 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1377 // the msb and End specifies the index of the lsb.
1378 unsigned LSB, Length;
1379 if (isStringOfOnes(Mask, LSB, Length)) {
1380 Start = 63 - (LSB + Length - 1);
1381 End = 63 - LSB;
1382 return true;
1383 }
1384
1385 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1386 // of the low 1s and End specifies the lsb of the high 1s.
1387 if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1388 assert(LSB > 0 && "Bottom bit must be set");
1389 assert(LSB + Length < BitSize && "Top bit must be set");
1390 Start = 63 - (LSB - 1);
1391 End = 63 - (LSB + Length);
1392 return true;
1393 }
1394
1395 return false;
1396}
1397
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001398unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode,
1399 SystemZII::FusedCompareType Type,
1400 const MachineInstr *MI) const {
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001401 switch (Opcode) {
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001402 case SystemZ::CHI:
Richard Sandiforde1d9f002013-05-29 11:58:52 +00001403 case SystemZ::CGHI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001404 if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
1405 return 0;
1406 break;
Richard Sandiford93183ee2013-09-18 09:56:40 +00001407 case SystemZ::CLFI:
Richard Sandiford93183ee2013-09-18 09:56:40 +00001408 case SystemZ::CLGFI:
Ulrich Weigand2eb027d2016-04-07 16:11:44 +00001409 if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
1410 return 0;
1411 }
1412 switch (Type) {
1413 case SystemZII::CompareAndBranch:
1414 switch (Opcode) {
1415 case SystemZ::CR:
1416 return SystemZ::CRJ;
1417 case SystemZ::CGR:
1418 return SystemZ::CGRJ;
1419 case SystemZ::CHI:
1420 return SystemZ::CIJ;
1421 case SystemZ::CGHI:
1422 return SystemZ::CGIJ;
1423 case SystemZ::CLR:
1424 return SystemZ::CLRJ;
1425 case SystemZ::CLGR:
1426 return SystemZ::CLGRJ;
1427 case SystemZ::CLFI:
1428 return SystemZ::CLIJ;
1429 case SystemZ::CLGFI:
1430 return SystemZ::CLGIJ;
1431 default:
1432 return 0;
1433 }
1434 case SystemZII::CompareAndReturn:
1435 switch (Opcode) {
1436 case SystemZ::CR:
1437 return SystemZ::CRBReturn;
1438 case SystemZ::CGR:
1439 return SystemZ::CGRBReturn;
1440 case SystemZ::CHI:
1441 return SystemZ::CIBReturn;
1442 case SystemZ::CGHI:
1443 return SystemZ::CGIBReturn;
1444 case SystemZ::CLR:
1445 return SystemZ::CLRBReturn;
1446 case SystemZ::CLGR:
1447 return SystemZ::CLGRBReturn;
1448 case SystemZ::CLFI:
1449 return SystemZ::CLIBReturn;
1450 case SystemZ::CLGFI:
1451 return SystemZ::CLGIBReturn;
1452 default:
1453 return 0;
1454 }
Ulrich Weigand848a5132016-04-11 12:12:32 +00001455 case SystemZII::CompareAndSibcall:
1456 switch (Opcode) {
1457 case SystemZ::CR:
1458 return SystemZ::CRBCall;
1459 case SystemZ::CGR:
1460 return SystemZ::CGRBCall;
1461 case SystemZ::CHI:
1462 return SystemZ::CIBCall;
1463 case SystemZ::CGHI:
1464 return SystemZ::CGIBCall;
1465 case SystemZ::CLR:
1466 return SystemZ::CLRBCall;
1467 case SystemZ::CLGR:
1468 return SystemZ::CLGRBCall;
1469 case SystemZ::CLFI:
1470 return SystemZ::CLIBCall;
1471 case SystemZ::CLGFI:
1472 return SystemZ::CLGIBCall;
1473 default:
1474 return 0;
1475 }
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +00001476 case SystemZII::CompareAndTrap:
1477 switch (Opcode) {
1478 case SystemZ::CR:
1479 return SystemZ::CRT;
1480 case SystemZ::CGR:
1481 return SystemZ::CGRT;
1482 case SystemZ::CHI:
1483 return SystemZ::CIT;
1484 case SystemZ::CGHI:
1485 return SystemZ::CGIT;
1486 case SystemZ::CLR:
1487 return SystemZ::CLRT;
1488 case SystemZ::CLGR:
1489 return SystemZ::CLGRT;
1490 case SystemZ::CLFI:
1491 return SystemZ::CLFIT;
1492 case SystemZ::CLGFI:
1493 return SystemZ::CLGIT;
1494 default:
1495 return 0;
1496 }
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001497 }
Ulrich Weigand79391ee2016-04-07 16:33:25 +00001498 return 0;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00001499}
1500
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001501void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1502 MachineBasicBlock::iterator MBBI,
1503 unsigned Reg, uint64_t Value) const {
1504 DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1505 unsigned Opcode;
1506 if (isInt<16>(Value))
1507 Opcode = SystemZ::LGHI;
1508 else if (SystemZ::isImmLL(Value))
1509 Opcode = SystemZ::LLILL;
1510 else if (SystemZ::isImmLH(Value)) {
1511 Opcode = SystemZ::LLILH;
1512 Value >>= 16;
1513 } else {
1514 assert(isInt<32>(Value) && "Huge values not handled yet");
1515 Opcode = SystemZ::LGFI;
1516 }
1517 BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1518}