1*9880d681SAndroid Build Coastguard Worker //===-- SystemZInstrInfo.cpp - SystemZ instruction information ------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file contains the SystemZ implementation of the TargetInstrInfo class.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "SystemZInstrInfo.h"
15*9880d681SAndroid Build Coastguard Worker #include "SystemZInstrBuilder.h"
16*9880d681SAndroid Build Coastguard Worker #include "SystemZTargetMachine.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveVariables.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveIntervalAnalysis.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard Worker using namespace llvm;
22*9880d681SAndroid Build Coastguard Worker
23*9880d681SAndroid Build Coastguard Worker #define GET_INSTRINFO_CTOR_DTOR
24*9880d681SAndroid Build Coastguard Worker #define GET_INSTRMAP_INFO
25*9880d681SAndroid Build Coastguard Worker #include "SystemZGenInstrInfo.inc"
26*9880d681SAndroid Build Coastguard Worker
27*9880d681SAndroid Build Coastguard Worker // Return a mask with Count low bits set.
allOnes(unsigned int Count)28*9880d681SAndroid Build Coastguard Worker static uint64_t allOnes(unsigned int Count) {
29*9880d681SAndroid Build Coastguard Worker return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
30*9880d681SAndroid Build Coastguard Worker }
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker // Reg should be a 32-bit GPR. Return true if it is a high register rather
33*9880d681SAndroid Build Coastguard Worker // than a low register.
isHighReg(unsigned int Reg)34*9880d681SAndroid Build Coastguard Worker static bool isHighReg(unsigned int Reg) {
35*9880d681SAndroid Build Coastguard Worker if (SystemZ::GRH32BitRegClass.contains(Reg))
36*9880d681SAndroid Build Coastguard Worker return true;
37*9880d681SAndroid Build Coastguard Worker assert(SystemZ::GR32BitRegClass.contains(Reg) && "Invalid GRX32");
38*9880d681SAndroid Build Coastguard Worker return false;
39*9880d681SAndroid Build Coastguard Worker }
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker // Pin the vtable to this file.
anchor()42*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::anchor() {}
43*9880d681SAndroid Build Coastguard Worker
SystemZInstrInfo(SystemZSubtarget & sti)44*9880d681SAndroid Build Coastguard Worker SystemZInstrInfo::SystemZInstrInfo(SystemZSubtarget &sti)
45*9880d681SAndroid Build Coastguard Worker : SystemZGenInstrInfo(SystemZ::ADJCALLSTACKDOWN, SystemZ::ADJCALLSTACKUP),
46*9880d681SAndroid Build Coastguard Worker RI(), STI(sti) {
47*9880d681SAndroid Build Coastguard Worker }
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker // MI is a 128-bit load or store. Split it into two 64-bit loads or stores,
50*9880d681SAndroid Build Coastguard Worker // each having the opcode given by NewOpcode.
splitMove(MachineBasicBlock::iterator MI,unsigned NewOpcode) const51*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::splitMove(MachineBasicBlock::iterator MI,
52*9880d681SAndroid Build Coastguard Worker unsigned NewOpcode) const {
53*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MI->getParent();
54*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = *MBB->getParent();
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker // Get two load or store instructions. Use the original instruction for one
57*9880d681SAndroid Build Coastguard Worker // of them (arbitrarily the second here) and create a clone for the other.
58*9880d681SAndroid Build Coastguard Worker MachineInstr *EarlierMI = MF.CloneMachineInstr(&*MI);
59*9880d681SAndroid Build Coastguard Worker MBB->insert(MI, EarlierMI);
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker // Set up the two 64-bit registers.
62*9880d681SAndroid Build Coastguard Worker MachineOperand &HighRegOp = EarlierMI->getOperand(0);
63*9880d681SAndroid Build Coastguard Worker MachineOperand &LowRegOp = MI->getOperand(0);
64*9880d681SAndroid Build Coastguard Worker HighRegOp.setReg(RI.getSubReg(HighRegOp.getReg(), SystemZ::subreg_h64));
65*9880d681SAndroid Build Coastguard Worker LowRegOp.setReg(RI.getSubReg(LowRegOp.getReg(), SystemZ::subreg_l64));
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker // The address in the first (high) instruction is already correct.
68*9880d681SAndroid Build Coastguard Worker // Adjust the offset in the second (low) instruction.
69*9880d681SAndroid Build Coastguard Worker MachineOperand &HighOffsetOp = EarlierMI->getOperand(2);
70*9880d681SAndroid Build Coastguard Worker MachineOperand &LowOffsetOp = MI->getOperand(2);
71*9880d681SAndroid Build Coastguard Worker LowOffsetOp.setImm(LowOffsetOp.getImm() + 8);
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker // Clear the kill flags for the base and index registers in the first
74*9880d681SAndroid Build Coastguard Worker // instruction.
75*9880d681SAndroid Build Coastguard Worker EarlierMI->getOperand(1).setIsKill(false);
76*9880d681SAndroid Build Coastguard Worker EarlierMI->getOperand(3).setIsKill(false);
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Worker // Set the opcodes.
79*9880d681SAndroid Build Coastguard Worker unsigned HighOpcode = getOpcodeForOffset(NewOpcode, HighOffsetOp.getImm());
80*9880d681SAndroid Build Coastguard Worker unsigned LowOpcode = getOpcodeForOffset(NewOpcode, LowOffsetOp.getImm());
81*9880d681SAndroid Build Coastguard Worker assert(HighOpcode && LowOpcode && "Both offsets should be in range");
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker EarlierMI->setDesc(get(HighOpcode));
84*9880d681SAndroid Build Coastguard Worker MI->setDesc(get(LowOpcode));
85*9880d681SAndroid Build Coastguard Worker }
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker // Split ADJDYNALLOC instruction MI.
splitAdjDynAlloc(MachineBasicBlock::iterator MI) const88*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::splitAdjDynAlloc(MachineBasicBlock::iterator MI) const {
89*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MI->getParent();
90*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = *MBB->getParent();
91*9880d681SAndroid Build Coastguard Worker MachineFrameInfo *MFFrame = MF.getFrameInfo();
92*9880d681SAndroid Build Coastguard Worker MachineOperand &OffsetMO = MI->getOperand(2);
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard Worker uint64_t Offset = (MFFrame->getMaxCallFrameSize() +
95*9880d681SAndroid Build Coastguard Worker SystemZMC::CallFrameSize +
96*9880d681SAndroid Build Coastguard Worker OffsetMO.getImm());
97*9880d681SAndroid Build Coastguard Worker unsigned NewOpcode = getOpcodeForOffset(SystemZ::LA, Offset);
98*9880d681SAndroid Build Coastguard Worker assert(NewOpcode && "No support for huge argument lists yet");
99*9880d681SAndroid Build Coastguard Worker MI->setDesc(get(NewOpcode));
100*9880d681SAndroid Build Coastguard Worker OffsetMO.setImm(Offset);
101*9880d681SAndroid Build Coastguard Worker }
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker // MI is an RI-style pseudo instruction. Replace it with LowOpcode
104*9880d681SAndroid Build Coastguard Worker // if the first operand is a low GR32 and HighOpcode if the first operand
105*9880d681SAndroid Build Coastguard Worker // is a high GR32. ConvertHigh is true if LowOpcode takes a signed operand
106*9880d681SAndroid Build Coastguard Worker // and HighOpcode takes an unsigned 32-bit operand. In those cases,
107*9880d681SAndroid Build Coastguard Worker // MI has the same kind of operand as LowOpcode, so needs to be converted
108*9880d681SAndroid Build Coastguard Worker // if HighOpcode is used.
expandRIPseudo(MachineInstr & MI,unsigned LowOpcode,unsigned HighOpcode,bool ConvertHigh) const109*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::expandRIPseudo(MachineInstr &MI, unsigned LowOpcode,
110*9880d681SAndroid Build Coastguard Worker unsigned HighOpcode,
111*9880d681SAndroid Build Coastguard Worker bool ConvertHigh) const {
112*9880d681SAndroid Build Coastguard Worker unsigned Reg = MI.getOperand(0).getReg();
113*9880d681SAndroid Build Coastguard Worker bool IsHigh = isHighReg(Reg);
114*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(IsHigh ? HighOpcode : LowOpcode));
115*9880d681SAndroid Build Coastguard Worker if (IsHigh && ConvertHigh)
116*9880d681SAndroid Build Coastguard Worker MI.getOperand(1).setImm(uint32_t(MI.getOperand(1).getImm()));
117*9880d681SAndroid Build Coastguard Worker }
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker // MI is a three-operand RIE-style pseudo instruction. Replace it with
120*9880d681SAndroid Build Coastguard Worker // LowOpcodeK if the registers are both low GR32s, otherwise use a move
121*9880d681SAndroid Build Coastguard Worker // followed by HighOpcode or LowOpcode, depending on whether the target
122*9880d681SAndroid Build Coastguard Worker // is a high or low GR32.
expandRIEPseudo(MachineInstr & MI,unsigned LowOpcode,unsigned LowOpcodeK,unsigned HighOpcode) const123*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::expandRIEPseudo(MachineInstr &MI, unsigned LowOpcode,
124*9880d681SAndroid Build Coastguard Worker unsigned LowOpcodeK,
125*9880d681SAndroid Build Coastguard Worker unsigned HighOpcode) const {
126*9880d681SAndroid Build Coastguard Worker unsigned DestReg = MI.getOperand(0).getReg();
127*9880d681SAndroid Build Coastguard Worker unsigned SrcReg = MI.getOperand(1).getReg();
128*9880d681SAndroid Build Coastguard Worker bool DestIsHigh = isHighReg(DestReg);
129*9880d681SAndroid Build Coastguard Worker bool SrcIsHigh = isHighReg(SrcReg);
130*9880d681SAndroid Build Coastguard Worker if (!DestIsHigh && !SrcIsHigh)
131*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(LowOpcodeK));
132*9880d681SAndroid Build Coastguard Worker else {
133*9880d681SAndroid Build Coastguard Worker emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(), DestReg, SrcReg,
134*9880d681SAndroid Build Coastguard Worker SystemZ::LR, 32, MI.getOperand(1).isKill());
135*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(DestIsHigh ? HighOpcode : LowOpcode));
136*9880d681SAndroid Build Coastguard Worker MI.getOperand(1).setReg(DestReg);
137*9880d681SAndroid Build Coastguard Worker MI.tieOperands(0, 1);
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker }
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker // MI is an RXY-style pseudo instruction. Replace it with LowOpcode
142*9880d681SAndroid Build Coastguard Worker // if the first operand is a low GR32 and HighOpcode if the first operand
143*9880d681SAndroid Build Coastguard Worker // is a high GR32.
expandRXYPseudo(MachineInstr & MI,unsigned LowOpcode,unsigned HighOpcode) const144*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::expandRXYPseudo(MachineInstr &MI, unsigned LowOpcode,
145*9880d681SAndroid Build Coastguard Worker unsigned HighOpcode) const {
146*9880d681SAndroid Build Coastguard Worker unsigned Reg = MI.getOperand(0).getReg();
147*9880d681SAndroid Build Coastguard Worker unsigned Opcode = getOpcodeForOffset(isHighReg(Reg) ? HighOpcode : LowOpcode,
148*9880d681SAndroid Build Coastguard Worker MI.getOperand(2).getImm());
149*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(Opcode));
150*9880d681SAndroid Build Coastguard Worker }
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker // MI is an RR-style pseudo instruction that zero-extends the low Size bits
153*9880d681SAndroid Build Coastguard Worker // of one GRX32 into another. Replace it with LowOpcode if both operands
154*9880d681SAndroid Build Coastguard Worker // are low registers, otherwise use RISB[LH]G.
expandZExtPseudo(MachineInstr & MI,unsigned LowOpcode,unsigned Size) const155*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::expandZExtPseudo(MachineInstr &MI, unsigned LowOpcode,
156*9880d681SAndroid Build Coastguard Worker unsigned Size) const {
157*9880d681SAndroid Build Coastguard Worker emitGRX32Move(*MI.getParent(), MI, MI.getDebugLoc(),
158*9880d681SAndroid Build Coastguard Worker MI.getOperand(0).getReg(), MI.getOperand(1).getReg(), LowOpcode,
159*9880d681SAndroid Build Coastguard Worker Size, MI.getOperand(1).isKill());
160*9880d681SAndroid Build Coastguard Worker MI.eraseFromParent();
161*9880d681SAndroid Build Coastguard Worker }
162*9880d681SAndroid Build Coastguard Worker
expandLoadStackGuard(MachineInstr * MI) const163*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::expandLoadStackGuard(MachineInstr *MI) const {
164*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MI->getParent();
165*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = *MBB->getParent();
166*9880d681SAndroid Build Coastguard Worker const unsigned Reg = MI->getOperand(0).getReg();
167*9880d681SAndroid Build Coastguard Worker
168*9880d681SAndroid Build Coastguard Worker // Conveniently, all 4 instructions are cloned from LOAD_STACK_GUARD,
169*9880d681SAndroid Build Coastguard Worker // so they already have operand 0 set to reg.
170*9880d681SAndroid Build Coastguard Worker
171*9880d681SAndroid Build Coastguard Worker // ear <reg>, %a0
172*9880d681SAndroid Build Coastguard Worker MachineInstr *Ear1MI = MF.CloneMachineInstr(MI);
173*9880d681SAndroid Build Coastguard Worker MBB->insert(MI, Ear1MI);
174*9880d681SAndroid Build Coastguard Worker Ear1MI->setDesc(get(SystemZ::EAR));
175*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(MF, Ear1MI).addImm(0);
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker // sllg <reg>, <reg>, 32
178*9880d681SAndroid Build Coastguard Worker MachineInstr *SllgMI = MF.CloneMachineInstr(MI);
179*9880d681SAndroid Build Coastguard Worker MBB->insert(MI, SllgMI);
180*9880d681SAndroid Build Coastguard Worker SllgMI->setDesc(get(SystemZ::SLLG));
181*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(MF, SllgMI).addReg(Reg).addReg(0).addImm(32);
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker // ear <reg>, %a1
184*9880d681SAndroid Build Coastguard Worker MachineInstr *Ear2MI = MF.CloneMachineInstr(MI);
185*9880d681SAndroid Build Coastguard Worker MBB->insert(MI, Ear2MI);
186*9880d681SAndroid Build Coastguard Worker Ear2MI->setDesc(get(SystemZ::EAR));
187*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(MF, Ear2MI).addImm(1);
188*9880d681SAndroid Build Coastguard Worker
189*9880d681SAndroid Build Coastguard Worker // lg <reg>, 40(<reg>)
190*9880d681SAndroid Build Coastguard Worker MI->setDesc(get(SystemZ::LG));
191*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(MF, MI).addReg(Reg).addImm(40).addReg(0);
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker // Emit a zero-extending move from 32-bit GPR SrcReg to 32-bit GPR
195*9880d681SAndroid Build Coastguard Worker // DestReg before MBBI in MBB. Use LowLowOpcode when both DestReg and SrcReg
196*9880d681SAndroid Build Coastguard Worker // are low registers, otherwise use RISB[LH]G. Size is the number of bits
197*9880d681SAndroid Build Coastguard Worker // taken from the low end of SrcReg (8 for LLCR, 16 for LLHR and 32 for LR).
198*9880d681SAndroid Build Coastguard Worker // KillSrc is true if this move is the last use of SrcReg.
emitGRX32Move(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,unsigned DestReg,unsigned SrcReg,unsigned LowLowOpcode,unsigned Size,bool KillSrc) const199*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::emitGRX32Move(MachineBasicBlock &MBB,
200*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MBBI,
201*9880d681SAndroid Build Coastguard Worker const DebugLoc &DL, unsigned DestReg,
202*9880d681SAndroid Build Coastguard Worker unsigned SrcReg, unsigned LowLowOpcode,
203*9880d681SAndroid Build Coastguard Worker unsigned Size, bool KillSrc) const {
204*9880d681SAndroid Build Coastguard Worker unsigned Opcode;
205*9880d681SAndroid Build Coastguard Worker bool DestIsHigh = isHighReg(DestReg);
206*9880d681SAndroid Build Coastguard Worker bool SrcIsHigh = isHighReg(SrcReg);
207*9880d681SAndroid Build Coastguard Worker if (DestIsHigh && SrcIsHigh)
208*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::RISBHH;
209*9880d681SAndroid Build Coastguard Worker else if (DestIsHigh && !SrcIsHigh)
210*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::RISBHL;
211*9880d681SAndroid Build Coastguard Worker else if (!DestIsHigh && SrcIsHigh)
212*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::RISBLH;
213*9880d681SAndroid Build Coastguard Worker else {
214*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MBBI, DL, get(LowLowOpcode), DestReg)
215*9880d681SAndroid Build Coastguard Worker .addReg(SrcReg, getKillRegState(KillSrc));
216*9880d681SAndroid Build Coastguard Worker return;
217*9880d681SAndroid Build Coastguard Worker }
218*9880d681SAndroid Build Coastguard Worker unsigned Rotate = (DestIsHigh != SrcIsHigh ? 32 : 0);
219*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
220*9880d681SAndroid Build Coastguard Worker .addReg(DestReg, RegState::Undef)
221*9880d681SAndroid Build Coastguard Worker .addReg(SrcReg, getKillRegState(KillSrc))
222*9880d681SAndroid Build Coastguard Worker .addImm(32 - Size).addImm(128 + 31).addImm(Rotate);
223*9880d681SAndroid Build Coastguard Worker }
224*9880d681SAndroid Build Coastguard Worker
225*9880d681SAndroid Build Coastguard Worker // If MI is a simple load or store for a frame object, return the register
226*9880d681SAndroid Build Coastguard Worker // it loads or stores and set FrameIndex to the index of the frame object.
227*9880d681SAndroid Build Coastguard Worker // Return 0 otherwise.
228*9880d681SAndroid Build Coastguard Worker //
229*9880d681SAndroid Build Coastguard Worker // Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
isSimpleMove(const MachineInstr & MI,int & FrameIndex,unsigned Flag)230*9880d681SAndroid Build Coastguard Worker static int isSimpleMove(const MachineInstr &MI, int &FrameIndex,
231*9880d681SAndroid Build Coastguard Worker unsigned Flag) {
232*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID = MI.getDesc();
233*9880d681SAndroid Build Coastguard Worker if ((MCID.TSFlags & Flag) && MI.getOperand(1).isFI() &&
234*9880d681SAndroid Build Coastguard Worker MI.getOperand(2).getImm() == 0 && MI.getOperand(3).getReg() == 0) {
235*9880d681SAndroid Build Coastguard Worker FrameIndex = MI.getOperand(1).getIndex();
236*9880d681SAndroid Build Coastguard Worker return MI.getOperand(0).getReg();
237*9880d681SAndroid Build Coastguard Worker }
238*9880d681SAndroid Build Coastguard Worker return 0;
239*9880d681SAndroid Build Coastguard Worker }
240*9880d681SAndroid Build Coastguard Worker
isLoadFromStackSlot(const MachineInstr & MI,int & FrameIndex) const241*9880d681SAndroid Build Coastguard Worker unsigned SystemZInstrInfo::isLoadFromStackSlot(const MachineInstr &MI,
242*9880d681SAndroid Build Coastguard Worker int &FrameIndex) const {
243*9880d681SAndroid Build Coastguard Worker return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXLoad);
244*9880d681SAndroid Build Coastguard Worker }
245*9880d681SAndroid Build Coastguard Worker
isStoreToStackSlot(const MachineInstr & MI,int & FrameIndex) const246*9880d681SAndroid Build Coastguard Worker unsigned SystemZInstrInfo::isStoreToStackSlot(const MachineInstr &MI,
247*9880d681SAndroid Build Coastguard Worker int &FrameIndex) const {
248*9880d681SAndroid Build Coastguard Worker return isSimpleMove(MI, FrameIndex, SystemZII::SimpleBDXStore);
249*9880d681SAndroid Build Coastguard Worker }
250*9880d681SAndroid Build Coastguard Worker
isStackSlotCopy(const MachineInstr & MI,int & DestFrameIndex,int & SrcFrameIndex) const251*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::isStackSlotCopy(const MachineInstr &MI,
252*9880d681SAndroid Build Coastguard Worker int &DestFrameIndex,
253*9880d681SAndroid Build Coastguard Worker int &SrcFrameIndex) const {
254*9880d681SAndroid Build Coastguard Worker // Check for MVC 0(Length,FI1),0(FI2)
255*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo *MFI = MI.getParent()->getParent()->getFrameInfo();
256*9880d681SAndroid Build Coastguard Worker if (MI.getOpcode() != SystemZ::MVC || !MI.getOperand(0).isFI() ||
257*9880d681SAndroid Build Coastguard Worker MI.getOperand(1).getImm() != 0 || !MI.getOperand(3).isFI() ||
258*9880d681SAndroid Build Coastguard Worker MI.getOperand(4).getImm() != 0)
259*9880d681SAndroid Build Coastguard Worker return false;
260*9880d681SAndroid Build Coastguard Worker
261*9880d681SAndroid Build Coastguard Worker // Check that Length covers the full slots.
262*9880d681SAndroid Build Coastguard Worker int64_t Length = MI.getOperand(2).getImm();
263*9880d681SAndroid Build Coastguard Worker unsigned FI1 = MI.getOperand(0).getIndex();
264*9880d681SAndroid Build Coastguard Worker unsigned FI2 = MI.getOperand(3).getIndex();
265*9880d681SAndroid Build Coastguard Worker if (MFI->getObjectSize(FI1) != Length ||
266*9880d681SAndroid Build Coastguard Worker MFI->getObjectSize(FI2) != Length)
267*9880d681SAndroid Build Coastguard Worker return false;
268*9880d681SAndroid Build Coastguard Worker
269*9880d681SAndroid Build Coastguard Worker DestFrameIndex = FI1;
270*9880d681SAndroid Build Coastguard Worker SrcFrameIndex = FI2;
271*9880d681SAndroid Build Coastguard Worker return true;
272*9880d681SAndroid Build Coastguard Worker }
273*9880d681SAndroid Build Coastguard Worker
analyzeBranch(MachineBasicBlock & MBB,MachineBasicBlock * & TBB,MachineBasicBlock * & FBB,SmallVectorImpl<MachineOperand> & Cond,bool AllowModify) const274*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::analyzeBranch(MachineBasicBlock &MBB,
275*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *&TBB,
276*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *&FBB,
277*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<MachineOperand> &Cond,
278*9880d681SAndroid Build Coastguard Worker bool AllowModify) const {
279*9880d681SAndroid Build Coastguard Worker // Most of the code and comments here are boilerplate.
280*9880d681SAndroid Build Coastguard Worker
281*9880d681SAndroid Build Coastguard Worker // Start from the bottom of the block and work up, examining the
282*9880d681SAndroid Build Coastguard Worker // terminator instructions.
283*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I = MBB.end();
284*9880d681SAndroid Build Coastguard Worker while (I != MBB.begin()) {
285*9880d681SAndroid Build Coastguard Worker --I;
286*9880d681SAndroid Build Coastguard Worker if (I->isDebugValue())
287*9880d681SAndroid Build Coastguard Worker continue;
288*9880d681SAndroid Build Coastguard Worker
289*9880d681SAndroid Build Coastguard Worker // Working from the bottom, when we see a non-terminator instruction, we're
290*9880d681SAndroid Build Coastguard Worker // done.
291*9880d681SAndroid Build Coastguard Worker if (!isUnpredicatedTerminator(*I))
292*9880d681SAndroid Build Coastguard Worker break;
293*9880d681SAndroid Build Coastguard Worker
294*9880d681SAndroid Build Coastguard Worker // A terminator that isn't a branch can't easily be handled by this
295*9880d681SAndroid Build Coastguard Worker // analysis.
296*9880d681SAndroid Build Coastguard Worker if (!I->isBranch())
297*9880d681SAndroid Build Coastguard Worker return true;
298*9880d681SAndroid Build Coastguard Worker
299*9880d681SAndroid Build Coastguard Worker // Can't handle indirect branches.
300*9880d681SAndroid Build Coastguard Worker SystemZII::Branch Branch(getBranchInfo(*I));
301*9880d681SAndroid Build Coastguard Worker if (!Branch.Target->isMBB())
302*9880d681SAndroid Build Coastguard Worker return true;
303*9880d681SAndroid Build Coastguard Worker
304*9880d681SAndroid Build Coastguard Worker // Punt on compound branches.
305*9880d681SAndroid Build Coastguard Worker if (Branch.Type != SystemZII::BranchNormal)
306*9880d681SAndroid Build Coastguard Worker return true;
307*9880d681SAndroid Build Coastguard Worker
308*9880d681SAndroid Build Coastguard Worker if (Branch.CCMask == SystemZ::CCMASK_ANY) {
309*9880d681SAndroid Build Coastguard Worker // Handle unconditional branches.
310*9880d681SAndroid Build Coastguard Worker if (!AllowModify) {
311*9880d681SAndroid Build Coastguard Worker TBB = Branch.Target->getMBB();
312*9880d681SAndroid Build Coastguard Worker continue;
313*9880d681SAndroid Build Coastguard Worker }
314*9880d681SAndroid Build Coastguard Worker
315*9880d681SAndroid Build Coastguard Worker // If the block has any instructions after a JMP, delete them.
316*9880d681SAndroid Build Coastguard Worker while (std::next(I) != MBB.end())
317*9880d681SAndroid Build Coastguard Worker std::next(I)->eraseFromParent();
318*9880d681SAndroid Build Coastguard Worker
319*9880d681SAndroid Build Coastguard Worker Cond.clear();
320*9880d681SAndroid Build Coastguard Worker FBB = nullptr;
321*9880d681SAndroid Build Coastguard Worker
322*9880d681SAndroid Build Coastguard Worker // Delete the JMP if it's equivalent to a fall-through.
323*9880d681SAndroid Build Coastguard Worker if (MBB.isLayoutSuccessor(Branch.Target->getMBB())) {
324*9880d681SAndroid Build Coastguard Worker TBB = nullptr;
325*9880d681SAndroid Build Coastguard Worker I->eraseFromParent();
326*9880d681SAndroid Build Coastguard Worker I = MBB.end();
327*9880d681SAndroid Build Coastguard Worker continue;
328*9880d681SAndroid Build Coastguard Worker }
329*9880d681SAndroid Build Coastguard Worker
330*9880d681SAndroid Build Coastguard Worker // TBB is used to indicate the unconditinal destination.
331*9880d681SAndroid Build Coastguard Worker TBB = Branch.Target->getMBB();
332*9880d681SAndroid Build Coastguard Worker continue;
333*9880d681SAndroid Build Coastguard Worker }
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard Worker // Working from the bottom, handle the first conditional branch.
336*9880d681SAndroid Build Coastguard Worker if (Cond.empty()) {
337*9880d681SAndroid Build Coastguard Worker // FIXME: add X86-style branch swap
338*9880d681SAndroid Build Coastguard Worker FBB = TBB;
339*9880d681SAndroid Build Coastguard Worker TBB = Branch.Target->getMBB();
340*9880d681SAndroid Build Coastguard Worker Cond.push_back(MachineOperand::CreateImm(Branch.CCValid));
341*9880d681SAndroid Build Coastguard Worker Cond.push_back(MachineOperand::CreateImm(Branch.CCMask));
342*9880d681SAndroid Build Coastguard Worker continue;
343*9880d681SAndroid Build Coastguard Worker }
344*9880d681SAndroid Build Coastguard Worker
345*9880d681SAndroid Build Coastguard Worker // Handle subsequent conditional branches.
346*9880d681SAndroid Build Coastguard Worker assert(Cond.size() == 2 && TBB && "Should have seen a conditional branch");
347*9880d681SAndroid Build Coastguard Worker
348*9880d681SAndroid Build Coastguard Worker // Only handle the case where all conditional branches branch to the same
349*9880d681SAndroid Build Coastguard Worker // destination.
350*9880d681SAndroid Build Coastguard Worker if (TBB != Branch.Target->getMBB())
351*9880d681SAndroid Build Coastguard Worker return true;
352*9880d681SAndroid Build Coastguard Worker
353*9880d681SAndroid Build Coastguard Worker // If the conditions are the same, we can leave them alone.
354*9880d681SAndroid Build Coastguard Worker unsigned OldCCValid = Cond[0].getImm();
355*9880d681SAndroid Build Coastguard Worker unsigned OldCCMask = Cond[1].getImm();
356*9880d681SAndroid Build Coastguard Worker if (OldCCValid == Branch.CCValid && OldCCMask == Branch.CCMask)
357*9880d681SAndroid Build Coastguard Worker continue;
358*9880d681SAndroid Build Coastguard Worker
359*9880d681SAndroid Build Coastguard Worker // FIXME: Try combining conditions like X86 does. Should be easy on Z!
360*9880d681SAndroid Build Coastguard Worker return false;
361*9880d681SAndroid Build Coastguard Worker }
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard Worker return false;
364*9880d681SAndroid Build Coastguard Worker }
365*9880d681SAndroid Build Coastguard Worker
RemoveBranch(MachineBasicBlock & MBB) const366*9880d681SAndroid Build Coastguard Worker unsigned SystemZInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
367*9880d681SAndroid Build Coastguard Worker // Most of the code and comments here are boilerplate.
368*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator I = MBB.end();
369*9880d681SAndroid Build Coastguard Worker unsigned Count = 0;
370*9880d681SAndroid Build Coastguard Worker
371*9880d681SAndroid Build Coastguard Worker while (I != MBB.begin()) {
372*9880d681SAndroid Build Coastguard Worker --I;
373*9880d681SAndroid Build Coastguard Worker if (I->isDebugValue())
374*9880d681SAndroid Build Coastguard Worker continue;
375*9880d681SAndroid Build Coastguard Worker if (!I->isBranch())
376*9880d681SAndroid Build Coastguard Worker break;
377*9880d681SAndroid Build Coastguard Worker if (!getBranchInfo(*I).Target->isMBB())
378*9880d681SAndroid Build Coastguard Worker break;
379*9880d681SAndroid Build Coastguard Worker // Remove the branch.
380*9880d681SAndroid Build Coastguard Worker I->eraseFromParent();
381*9880d681SAndroid Build Coastguard Worker I = MBB.end();
382*9880d681SAndroid Build Coastguard Worker ++Count;
383*9880d681SAndroid Build Coastguard Worker }
384*9880d681SAndroid Build Coastguard Worker
385*9880d681SAndroid Build Coastguard Worker return Count;
386*9880d681SAndroid Build Coastguard Worker }
387*9880d681SAndroid Build Coastguard Worker
388*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::
ReverseBranchCondition(SmallVectorImpl<MachineOperand> & Cond) const389*9880d681SAndroid Build Coastguard Worker ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
390*9880d681SAndroid Build Coastguard Worker assert(Cond.size() == 2 && "Invalid condition");
391*9880d681SAndroid Build Coastguard Worker Cond[1].setImm(Cond[1].getImm() ^ Cond[0].getImm());
392*9880d681SAndroid Build Coastguard Worker return false;
393*9880d681SAndroid Build Coastguard Worker }
394*9880d681SAndroid Build Coastguard Worker
InsertBranch(MachineBasicBlock & MBB,MachineBasicBlock * TBB,MachineBasicBlock * FBB,ArrayRef<MachineOperand> Cond,const DebugLoc & DL) const395*9880d681SAndroid Build Coastguard Worker unsigned SystemZInstrInfo::InsertBranch(MachineBasicBlock &MBB,
396*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *TBB,
397*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *FBB,
398*9880d681SAndroid Build Coastguard Worker ArrayRef<MachineOperand> Cond,
399*9880d681SAndroid Build Coastguard Worker const DebugLoc &DL) const {
400*9880d681SAndroid Build Coastguard Worker // In this function we output 32-bit branches, which should always
401*9880d681SAndroid Build Coastguard Worker // have enough range. They can be shortened and relaxed by later code
402*9880d681SAndroid Build Coastguard Worker // in the pipeline, if desired.
403*9880d681SAndroid Build Coastguard Worker
404*9880d681SAndroid Build Coastguard Worker // Shouldn't be a fall through.
405*9880d681SAndroid Build Coastguard Worker assert(TBB && "InsertBranch must not be told to insert a fallthrough");
406*9880d681SAndroid Build Coastguard Worker assert((Cond.size() == 2 || Cond.size() == 0) &&
407*9880d681SAndroid Build Coastguard Worker "SystemZ branch conditions have one component!");
408*9880d681SAndroid Build Coastguard Worker
409*9880d681SAndroid Build Coastguard Worker if (Cond.empty()) {
410*9880d681SAndroid Build Coastguard Worker // Unconditional branch?
411*9880d681SAndroid Build Coastguard Worker assert(!FBB && "Unconditional branch with multiple successors!");
412*9880d681SAndroid Build Coastguard Worker BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(TBB);
413*9880d681SAndroid Build Coastguard Worker return 1;
414*9880d681SAndroid Build Coastguard Worker }
415*9880d681SAndroid Build Coastguard Worker
416*9880d681SAndroid Build Coastguard Worker // Conditional branch.
417*9880d681SAndroid Build Coastguard Worker unsigned Count = 0;
418*9880d681SAndroid Build Coastguard Worker unsigned CCValid = Cond[0].getImm();
419*9880d681SAndroid Build Coastguard Worker unsigned CCMask = Cond[1].getImm();
420*9880d681SAndroid Build Coastguard Worker BuildMI(&MBB, DL, get(SystemZ::BRC))
421*9880d681SAndroid Build Coastguard Worker .addImm(CCValid).addImm(CCMask).addMBB(TBB);
422*9880d681SAndroid Build Coastguard Worker ++Count;
423*9880d681SAndroid Build Coastguard Worker
424*9880d681SAndroid Build Coastguard Worker if (FBB) {
425*9880d681SAndroid Build Coastguard Worker // Two-way Conditional branch. Insert the second branch.
426*9880d681SAndroid Build Coastguard Worker BuildMI(&MBB, DL, get(SystemZ::J)).addMBB(FBB);
427*9880d681SAndroid Build Coastguard Worker ++Count;
428*9880d681SAndroid Build Coastguard Worker }
429*9880d681SAndroid Build Coastguard Worker return Count;
430*9880d681SAndroid Build Coastguard Worker }
431*9880d681SAndroid Build Coastguard Worker
analyzeCompare(const MachineInstr & MI,unsigned & SrcReg,unsigned & SrcReg2,int & Mask,int & Value) const432*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::analyzeCompare(const MachineInstr &MI, unsigned &SrcReg,
433*9880d681SAndroid Build Coastguard Worker unsigned &SrcReg2, int &Mask,
434*9880d681SAndroid Build Coastguard Worker int &Value) const {
435*9880d681SAndroid Build Coastguard Worker assert(MI.isCompare() && "Caller should have checked for a comparison");
436*9880d681SAndroid Build Coastguard Worker
437*9880d681SAndroid Build Coastguard Worker if (MI.getNumExplicitOperands() == 2 && MI.getOperand(0).isReg() &&
438*9880d681SAndroid Build Coastguard Worker MI.getOperand(1).isImm()) {
439*9880d681SAndroid Build Coastguard Worker SrcReg = MI.getOperand(0).getReg();
440*9880d681SAndroid Build Coastguard Worker SrcReg2 = 0;
441*9880d681SAndroid Build Coastguard Worker Value = MI.getOperand(1).getImm();
442*9880d681SAndroid Build Coastguard Worker Mask = ~0;
443*9880d681SAndroid Build Coastguard Worker return true;
444*9880d681SAndroid Build Coastguard Worker }
445*9880d681SAndroid Build Coastguard Worker
446*9880d681SAndroid Build Coastguard Worker return false;
447*9880d681SAndroid Build Coastguard Worker }
448*9880d681SAndroid Build Coastguard Worker
449*9880d681SAndroid Build Coastguard Worker // If Reg is a virtual register, return its definition, otherwise return null.
getDef(unsigned Reg,const MachineRegisterInfo * MRI)450*9880d681SAndroid Build Coastguard Worker static MachineInstr *getDef(unsigned Reg,
451*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo *MRI) {
452*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg))
453*9880d681SAndroid Build Coastguard Worker return nullptr;
454*9880d681SAndroid Build Coastguard Worker return MRI->getUniqueVRegDef(Reg);
455*9880d681SAndroid Build Coastguard Worker }
456*9880d681SAndroid Build Coastguard Worker
457*9880d681SAndroid Build Coastguard Worker // Return true if MI is a shift of type Opcode by Imm bits.
isShift(MachineInstr * MI,unsigned Opcode,int64_t Imm)458*9880d681SAndroid Build Coastguard Worker static bool isShift(MachineInstr *MI, unsigned Opcode, int64_t Imm) {
459*9880d681SAndroid Build Coastguard Worker return (MI->getOpcode() == Opcode &&
460*9880d681SAndroid Build Coastguard Worker !MI->getOperand(2).getReg() &&
461*9880d681SAndroid Build Coastguard Worker MI->getOperand(3).getImm() == Imm);
462*9880d681SAndroid Build Coastguard Worker }
463*9880d681SAndroid Build Coastguard Worker
464*9880d681SAndroid Build Coastguard Worker // If the destination of MI has no uses, delete it as dead.
eraseIfDead(MachineInstr * MI,const MachineRegisterInfo * MRI)465*9880d681SAndroid Build Coastguard Worker static void eraseIfDead(MachineInstr *MI, const MachineRegisterInfo *MRI) {
466*9880d681SAndroid Build Coastguard Worker if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
467*9880d681SAndroid Build Coastguard Worker MI->eraseFromParent();
468*9880d681SAndroid Build Coastguard Worker }
469*9880d681SAndroid Build Coastguard Worker
470*9880d681SAndroid Build Coastguard Worker // Compare compares SrcReg against zero. Check whether SrcReg contains
471*9880d681SAndroid Build Coastguard Worker // the result of an IPM sequence whose input CC survives until Compare,
472*9880d681SAndroid Build Coastguard Worker // and whether Compare is therefore redundant. Delete it and return
473*9880d681SAndroid Build Coastguard Worker // true if so.
removeIPMBasedCompare(MachineInstr & Compare,unsigned SrcReg,const MachineRegisterInfo * MRI,const TargetRegisterInfo * TRI)474*9880d681SAndroid Build Coastguard Worker static bool removeIPMBasedCompare(MachineInstr &Compare, unsigned SrcReg,
475*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo *MRI,
476*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) {
477*9880d681SAndroid Build Coastguard Worker MachineInstr *LGFR = nullptr;
478*9880d681SAndroid Build Coastguard Worker MachineInstr *RLL = getDef(SrcReg, MRI);
479*9880d681SAndroid Build Coastguard Worker if (RLL && RLL->getOpcode() == SystemZ::LGFR) {
480*9880d681SAndroid Build Coastguard Worker LGFR = RLL;
481*9880d681SAndroid Build Coastguard Worker RLL = getDef(LGFR->getOperand(1).getReg(), MRI);
482*9880d681SAndroid Build Coastguard Worker }
483*9880d681SAndroid Build Coastguard Worker if (!RLL || !isShift(RLL, SystemZ::RLL, 31))
484*9880d681SAndroid Build Coastguard Worker return false;
485*9880d681SAndroid Build Coastguard Worker
486*9880d681SAndroid Build Coastguard Worker MachineInstr *SRL = getDef(RLL->getOperand(1).getReg(), MRI);
487*9880d681SAndroid Build Coastguard Worker if (!SRL || !isShift(SRL, SystemZ::SRL, SystemZ::IPM_CC))
488*9880d681SAndroid Build Coastguard Worker return false;
489*9880d681SAndroid Build Coastguard Worker
490*9880d681SAndroid Build Coastguard Worker MachineInstr *IPM = getDef(SRL->getOperand(1).getReg(), MRI);
491*9880d681SAndroid Build Coastguard Worker if (!IPM || IPM->getOpcode() != SystemZ::IPM)
492*9880d681SAndroid Build Coastguard Worker return false;
493*9880d681SAndroid Build Coastguard Worker
494*9880d681SAndroid Build Coastguard Worker // Check that there are no assignments to CC between the IPM and Compare,
495*9880d681SAndroid Build Coastguard Worker if (IPM->getParent() != Compare.getParent())
496*9880d681SAndroid Build Coastguard Worker return false;
497*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MBBI = IPM, MBBE = Compare.getIterator();
498*9880d681SAndroid Build Coastguard Worker for (++MBBI; MBBI != MBBE; ++MBBI) {
499*9880d681SAndroid Build Coastguard Worker MachineInstr &MI = *MBBI;
500*9880d681SAndroid Build Coastguard Worker if (MI.modifiesRegister(SystemZ::CC, TRI))
501*9880d681SAndroid Build Coastguard Worker return false;
502*9880d681SAndroid Build Coastguard Worker }
503*9880d681SAndroid Build Coastguard Worker
504*9880d681SAndroid Build Coastguard Worker Compare.eraseFromParent();
505*9880d681SAndroid Build Coastguard Worker if (LGFR)
506*9880d681SAndroid Build Coastguard Worker eraseIfDead(LGFR, MRI);
507*9880d681SAndroid Build Coastguard Worker eraseIfDead(RLL, MRI);
508*9880d681SAndroid Build Coastguard Worker eraseIfDead(SRL, MRI);
509*9880d681SAndroid Build Coastguard Worker eraseIfDead(IPM, MRI);
510*9880d681SAndroid Build Coastguard Worker
511*9880d681SAndroid Build Coastguard Worker return true;
512*9880d681SAndroid Build Coastguard Worker }
513*9880d681SAndroid Build Coastguard Worker
optimizeCompareInstr(MachineInstr & Compare,unsigned SrcReg,unsigned SrcReg2,int Mask,int Value,const MachineRegisterInfo * MRI) const514*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::optimizeCompareInstr(
515*9880d681SAndroid Build Coastguard Worker MachineInstr &Compare, unsigned SrcReg, unsigned SrcReg2, int Mask,
516*9880d681SAndroid Build Coastguard Worker int Value, const MachineRegisterInfo *MRI) const {
517*9880d681SAndroid Build Coastguard Worker assert(!SrcReg2 && "Only optimizing constant comparisons so far");
518*9880d681SAndroid Build Coastguard Worker bool IsLogical = (Compare.getDesc().TSFlags & SystemZII::IsLogical) != 0;
519*9880d681SAndroid Build Coastguard Worker return Value == 0 && !IsLogical &&
520*9880d681SAndroid Build Coastguard Worker removeIPMBasedCompare(Compare, SrcReg, MRI, &RI);
521*9880d681SAndroid Build Coastguard Worker }
522*9880d681SAndroid Build Coastguard Worker
523*9880d681SAndroid Build Coastguard Worker // If Opcode is a move that has a conditional variant, return that variant,
524*9880d681SAndroid Build Coastguard Worker // otherwise return 0.
getConditionalMove(unsigned Opcode)525*9880d681SAndroid Build Coastguard Worker static unsigned getConditionalMove(unsigned Opcode) {
526*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
527*9880d681SAndroid Build Coastguard Worker case SystemZ::LR: return SystemZ::LOCR;
528*9880d681SAndroid Build Coastguard Worker case SystemZ::LGR: return SystemZ::LOCGR;
529*9880d681SAndroid Build Coastguard Worker default: return 0;
530*9880d681SAndroid Build Coastguard Worker }
531*9880d681SAndroid Build Coastguard Worker }
532*9880d681SAndroid Build Coastguard Worker
getConditionalLoadImmediate(unsigned Opcode)533*9880d681SAndroid Build Coastguard Worker static unsigned getConditionalLoadImmediate(unsigned Opcode) {
534*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
535*9880d681SAndroid Build Coastguard Worker case SystemZ::LHI: return SystemZ::LOCHI;
536*9880d681SAndroid Build Coastguard Worker case SystemZ::LGHI: return SystemZ::LOCGHI;
537*9880d681SAndroid Build Coastguard Worker default: return 0;
538*9880d681SAndroid Build Coastguard Worker }
539*9880d681SAndroid Build Coastguard Worker }
540*9880d681SAndroid Build Coastguard Worker
isPredicable(MachineInstr & MI) const541*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::isPredicable(MachineInstr &MI) const {
542*9880d681SAndroid Build Coastguard Worker unsigned Opcode = MI.getOpcode();
543*9880d681SAndroid Build Coastguard Worker if (STI.hasLoadStoreOnCond() && getConditionalMove(Opcode))
544*9880d681SAndroid Build Coastguard Worker return true;
545*9880d681SAndroid Build Coastguard Worker if (STI.hasLoadStoreOnCond2() && getConditionalLoadImmediate(Opcode))
546*9880d681SAndroid Build Coastguard Worker return true;
547*9880d681SAndroid Build Coastguard Worker if (Opcode == SystemZ::Return ||
548*9880d681SAndroid Build Coastguard Worker Opcode == SystemZ::Trap ||
549*9880d681SAndroid Build Coastguard Worker Opcode == SystemZ::CallJG ||
550*9880d681SAndroid Build Coastguard Worker Opcode == SystemZ::CallBR)
551*9880d681SAndroid Build Coastguard Worker return true;
552*9880d681SAndroid Build Coastguard Worker return false;
553*9880d681SAndroid Build Coastguard Worker }
554*9880d681SAndroid Build Coastguard Worker
555*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::
isProfitableToIfCvt(MachineBasicBlock & MBB,unsigned NumCycles,unsigned ExtraPredCycles,BranchProbability Probability) const556*9880d681SAndroid Build Coastguard Worker isProfitableToIfCvt(MachineBasicBlock &MBB,
557*9880d681SAndroid Build Coastguard Worker unsigned NumCycles, unsigned ExtraPredCycles,
558*9880d681SAndroid Build Coastguard Worker BranchProbability Probability) const {
559*9880d681SAndroid Build Coastguard Worker // Avoid using conditional returns at the end of a loop (since then
560*9880d681SAndroid Build Coastguard Worker // we'd need to emit an unconditional branch to the beginning anyway,
561*9880d681SAndroid Build Coastguard Worker // making the loop body longer). This doesn't apply for low-probability
562*9880d681SAndroid Build Coastguard Worker // loops (eg. compare-and-swap retry), so just decide based on branch
563*9880d681SAndroid Build Coastguard Worker // probability instead of looping structure.
564*9880d681SAndroid Build Coastguard Worker // However, since Compare and Trap instructions cost the same as a regular
565*9880d681SAndroid Build Coastguard Worker // Compare instruction, we should allow the if conversion to convert this
566*9880d681SAndroid Build Coastguard Worker // into a Conditional Compare regardless of the branch probability.
567*9880d681SAndroid Build Coastguard Worker if (MBB.getLastNonDebugInstr()->getOpcode() != SystemZ::Trap &&
568*9880d681SAndroid Build Coastguard Worker MBB.succ_empty() && Probability < BranchProbability(1, 8))
569*9880d681SAndroid Build Coastguard Worker return false;
570*9880d681SAndroid Build Coastguard Worker // For now only convert single instructions.
571*9880d681SAndroid Build Coastguard Worker return NumCycles == 1;
572*9880d681SAndroid Build Coastguard Worker }
573*9880d681SAndroid Build Coastguard Worker
574*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::
isProfitableToIfCvt(MachineBasicBlock & TMBB,unsigned NumCyclesT,unsigned ExtraPredCyclesT,MachineBasicBlock & FMBB,unsigned NumCyclesF,unsigned ExtraPredCyclesF,BranchProbability Probability) const575*9880d681SAndroid Build Coastguard Worker isProfitableToIfCvt(MachineBasicBlock &TMBB,
576*9880d681SAndroid Build Coastguard Worker unsigned NumCyclesT, unsigned ExtraPredCyclesT,
577*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &FMBB,
578*9880d681SAndroid Build Coastguard Worker unsigned NumCyclesF, unsigned ExtraPredCyclesF,
579*9880d681SAndroid Build Coastguard Worker BranchProbability Probability) const {
580*9880d681SAndroid Build Coastguard Worker // For now avoid converting mutually-exclusive cases.
581*9880d681SAndroid Build Coastguard Worker return false;
582*9880d681SAndroid Build Coastguard Worker }
583*9880d681SAndroid Build Coastguard Worker
584*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::
isProfitableToDupForIfCvt(MachineBasicBlock & MBB,unsigned NumCycles,BranchProbability Probability) const585*9880d681SAndroid Build Coastguard Worker isProfitableToDupForIfCvt(MachineBasicBlock &MBB, unsigned NumCycles,
586*9880d681SAndroid Build Coastguard Worker BranchProbability Probability) const {
587*9880d681SAndroid Build Coastguard Worker // For now only duplicate single instructions.
588*9880d681SAndroid Build Coastguard Worker return NumCycles == 1;
589*9880d681SAndroid Build Coastguard Worker }
590*9880d681SAndroid Build Coastguard Worker
PredicateInstruction(MachineInstr & MI,ArrayRef<MachineOperand> Pred) const591*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::PredicateInstruction(
592*9880d681SAndroid Build Coastguard Worker MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
593*9880d681SAndroid Build Coastguard Worker assert(Pred.size() == 2 && "Invalid condition");
594*9880d681SAndroid Build Coastguard Worker unsigned CCValid = Pred[0].getImm();
595*9880d681SAndroid Build Coastguard Worker unsigned CCMask = Pred[1].getImm();
596*9880d681SAndroid Build Coastguard Worker assert(CCMask > 0 && CCMask < 15 && "Invalid predicate");
597*9880d681SAndroid Build Coastguard Worker unsigned Opcode = MI.getOpcode();
598*9880d681SAndroid Build Coastguard Worker if (STI.hasLoadStoreOnCond()) {
599*9880d681SAndroid Build Coastguard Worker if (unsigned CondOpcode = getConditionalMove(Opcode)) {
600*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(CondOpcode));
601*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(*MI.getParent()->getParent(), MI)
602*9880d681SAndroid Build Coastguard Worker .addImm(CCValid)
603*9880d681SAndroid Build Coastguard Worker .addImm(CCMask)
604*9880d681SAndroid Build Coastguard Worker .addReg(SystemZ::CC, RegState::Implicit);
605*9880d681SAndroid Build Coastguard Worker return true;
606*9880d681SAndroid Build Coastguard Worker }
607*9880d681SAndroid Build Coastguard Worker }
608*9880d681SAndroid Build Coastguard Worker if (STI.hasLoadStoreOnCond2()) {
609*9880d681SAndroid Build Coastguard Worker if (unsigned CondOpcode = getConditionalLoadImmediate(Opcode)) {
610*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(CondOpcode));
611*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(*MI.getParent()->getParent(), MI)
612*9880d681SAndroid Build Coastguard Worker .addImm(CCValid)
613*9880d681SAndroid Build Coastguard Worker .addImm(CCMask)
614*9880d681SAndroid Build Coastguard Worker .addReg(SystemZ::CC, RegState::Implicit);
615*9880d681SAndroid Build Coastguard Worker return true;
616*9880d681SAndroid Build Coastguard Worker }
617*9880d681SAndroid Build Coastguard Worker }
618*9880d681SAndroid Build Coastguard Worker if (Opcode == SystemZ::Trap) {
619*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(SystemZ::CondTrap));
620*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(*MI.getParent()->getParent(), MI)
621*9880d681SAndroid Build Coastguard Worker .addImm(CCValid).addImm(CCMask)
622*9880d681SAndroid Build Coastguard Worker .addReg(SystemZ::CC, RegState::Implicit);
623*9880d681SAndroid Build Coastguard Worker return true;
624*9880d681SAndroid Build Coastguard Worker }
625*9880d681SAndroid Build Coastguard Worker if (Opcode == SystemZ::Return) {
626*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(SystemZ::CondReturn));
627*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(*MI.getParent()->getParent(), MI)
628*9880d681SAndroid Build Coastguard Worker .addImm(CCValid).addImm(CCMask)
629*9880d681SAndroid Build Coastguard Worker .addReg(SystemZ::CC, RegState::Implicit);
630*9880d681SAndroid Build Coastguard Worker return true;
631*9880d681SAndroid Build Coastguard Worker }
632*9880d681SAndroid Build Coastguard Worker if (Opcode == SystemZ::CallJG) {
633*9880d681SAndroid Build Coastguard Worker MachineOperand FirstOp = MI.getOperand(0);
634*9880d681SAndroid Build Coastguard Worker const uint32_t *RegMask = MI.getOperand(1).getRegMask();
635*9880d681SAndroid Build Coastguard Worker MI.RemoveOperand(1);
636*9880d681SAndroid Build Coastguard Worker MI.RemoveOperand(0);
637*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(SystemZ::CallBRCL));
638*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(*MI.getParent()->getParent(), MI)
639*9880d681SAndroid Build Coastguard Worker .addImm(CCValid).addImm(CCMask)
640*9880d681SAndroid Build Coastguard Worker .addOperand(FirstOp)
641*9880d681SAndroid Build Coastguard Worker .addRegMask(RegMask)
642*9880d681SAndroid Build Coastguard Worker .addReg(SystemZ::CC, RegState::Implicit);
643*9880d681SAndroid Build Coastguard Worker return true;
644*9880d681SAndroid Build Coastguard Worker }
645*9880d681SAndroid Build Coastguard Worker if (Opcode == SystemZ::CallBR) {
646*9880d681SAndroid Build Coastguard Worker const uint32_t *RegMask = MI.getOperand(0).getRegMask();
647*9880d681SAndroid Build Coastguard Worker MI.RemoveOperand(0);
648*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(SystemZ::CallBCR));
649*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder(*MI.getParent()->getParent(), MI)
650*9880d681SAndroid Build Coastguard Worker .addImm(CCValid).addImm(CCMask)
651*9880d681SAndroid Build Coastguard Worker .addRegMask(RegMask)
652*9880d681SAndroid Build Coastguard Worker .addReg(SystemZ::CC, RegState::Implicit);
653*9880d681SAndroid Build Coastguard Worker return true;
654*9880d681SAndroid Build Coastguard Worker }
655*9880d681SAndroid Build Coastguard Worker return false;
656*9880d681SAndroid Build Coastguard Worker }
657*9880d681SAndroid Build Coastguard Worker
copyPhysReg(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,const DebugLoc & DL,unsigned DestReg,unsigned SrcReg,bool KillSrc) const658*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
659*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MBBI,
660*9880d681SAndroid Build Coastguard Worker const DebugLoc &DL, unsigned DestReg,
661*9880d681SAndroid Build Coastguard Worker unsigned SrcReg, bool KillSrc) const {
662*9880d681SAndroid Build Coastguard Worker // Split 128-bit GPR moves into two 64-bit moves. This handles ADDR128 too.
663*9880d681SAndroid Build Coastguard Worker if (SystemZ::GR128BitRegClass.contains(DestReg, SrcReg)) {
664*9880d681SAndroid Build Coastguard Worker copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_h64),
665*9880d681SAndroid Build Coastguard Worker RI.getSubReg(SrcReg, SystemZ::subreg_h64), KillSrc);
666*9880d681SAndroid Build Coastguard Worker copyPhysReg(MBB, MBBI, DL, RI.getSubReg(DestReg, SystemZ::subreg_l64),
667*9880d681SAndroid Build Coastguard Worker RI.getSubReg(SrcReg, SystemZ::subreg_l64), KillSrc);
668*9880d681SAndroid Build Coastguard Worker return;
669*9880d681SAndroid Build Coastguard Worker }
670*9880d681SAndroid Build Coastguard Worker
671*9880d681SAndroid Build Coastguard Worker if (SystemZ::GRX32BitRegClass.contains(DestReg, SrcReg)) {
672*9880d681SAndroid Build Coastguard Worker emitGRX32Move(MBB, MBBI, DL, DestReg, SrcReg, SystemZ::LR, 32, KillSrc);
673*9880d681SAndroid Build Coastguard Worker return;
674*9880d681SAndroid Build Coastguard Worker }
675*9880d681SAndroid Build Coastguard Worker
676*9880d681SAndroid Build Coastguard Worker // Everything else needs only one instruction.
677*9880d681SAndroid Build Coastguard Worker unsigned Opcode;
678*9880d681SAndroid Build Coastguard Worker if (SystemZ::GR64BitRegClass.contains(DestReg, SrcReg))
679*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::LGR;
680*9880d681SAndroid Build Coastguard Worker else if (SystemZ::FP32BitRegClass.contains(DestReg, SrcReg))
681*9880d681SAndroid Build Coastguard Worker // For z13 we prefer LDR over LER to avoid partial register dependencies.
682*9880d681SAndroid Build Coastguard Worker Opcode = STI.hasVector() ? SystemZ::LDR32 : SystemZ::LER;
683*9880d681SAndroid Build Coastguard Worker else if (SystemZ::FP64BitRegClass.contains(DestReg, SrcReg))
684*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::LDR;
685*9880d681SAndroid Build Coastguard Worker else if (SystemZ::FP128BitRegClass.contains(DestReg, SrcReg))
686*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::LXR;
687*9880d681SAndroid Build Coastguard Worker else if (SystemZ::VR32BitRegClass.contains(DestReg, SrcReg))
688*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::VLR32;
689*9880d681SAndroid Build Coastguard Worker else if (SystemZ::VR64BitRegClass.contains(DestReg, SrcReg))
690*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::VLR64;
691*9880d681SAndroid Build Coastguard Worker else if (SystemZ::VR128BitRegClass.contains(DestReg, SrcReg))
692*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::VLR;
693*9880d681SAndroid Build Coastguard Worker else
694*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Impossible reg-to-reg copy");
695*9880d681SAndroid Build Coastguard Worker
696*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MBBI, DL, get(Opcode), DestReg)
697*9880d681SAndroid Build Coastguard Worker .addReg(SrcReg, getKillRegState(KillSrc));
698*9880d681SAndroid Build Coastguard Worker }
699*9880d681SAndroid Build Coastguard Worker
storeRegToStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,unsigned SrcReg,bool isKill,int FrameIdx,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI) const700*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::storeRegToStackSlot(
701*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned SrcReg,
702*9880d681SAndroid Build Coastguard Worker bool isKill, int FrameIdx, const TargetRegisterClass *RC,
703*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) const {
704*9880d681SAndroid Build Coastguard Worker DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
705*9880d681SAndroid Build Coastguard Worker
706*9880d681SAndroid Build Coastguard Worker // Callers may expect a single instruction, so keep 128-bit moves
707*9880d681SAndroid Build Coastguard Worker // together for now and lower them after register allocation.
708*9880d681SAndroid Build Coastguard Worker unsigned LoadOpcode, StoreOpcode;
709*9880d681SAndroid Build Coastguard Worker getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
710*9880d681SAndroid Build Coastguard Worker addFrameReference(BuildMI(MBB, MBBI, DL, get(StoreOpcode))
711*9880d681SAndroid Build Coastguard Worker .addReg(SrcReg, getKillRegState(isKill)),
712*9880d681SAndroid Build Coastguard Worker FrameIdx);
713*9880d681SAndroid Build Coastguard Worker }
714*9880d681SAndroid Build Coastguard Worker
loadRegFromStackSlot(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,unsigned DestReg,int FrameIdx,const TargetRegisterClass * RC,const TargetRegisterInfo * TRI) const715*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::loadRegFromStackSlot(
716*9880d681SAndroid Build Coastguard Worker MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI, unsigned DestReg,
717*9880d681SAndroid Build Coastguard Worker int FrameIdx, const TargetRegisterClass *RC,
718*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI) const {
719*9880d681SAndroid Build Coastguard Worker DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
720*9880d681SAndroid Build Coastguard Worker
721*9880d681SAndroid Build Coastguard Worker // Callers may expect a single instruction, so keep 128-bit moves
722*9880d681SAndroid Build Coastguard Worker // together for now and lower them after register allocation.
723*9880d681SAndroid Build Coastguard Worker unsigned LoadOpcode, StoreOpcode;
724*9880d681SAndroid Build Coastguard Worker getLoadStoreOpcodes(RC, LoadOpcode, StoreOpcode);
725*9880d681SAndroid Build Coastguard Worker addFrameReference(BuildMI(MBB, MBBI, DL, get(LoadOpcode), DestReg),
726*9880d681SAndroid Build Coastguard Worker FrameIdx);
727*9880d681SAndroid Build Coastguard Worker }
728*9880d681SAndroid Build Coastguard Worker
729*9880d681SAndroid Build Coastguard Worker // Return true if MI is a simple load or store with a 12-bit displacement
730*9880d681SAndroid Build Coastguard Worker // and no index. Flag is SimpleBDXLoad for loads and SimpleBDXStore for stores.
isSimpleBD12Move(const MachineInstr * MI,unsigned Flag)731*9880d681SAndroid Build Coastguard Worker static bool isSimpleBD12Move(const MachineInstr *MI, unsigned Flag) {
732*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID = MI->getDesc();
733*9880d681SAndroid Build Coastguard Worker return ((MCID.TSFlags & Flag) &&
734*9880d681SAndroid Build Coastguard Worker isUInt<12>(MI->getOperand(2).getImm()) &&
735*9880d681SAndroid Build Coastguard Worker MI->getOperand(3).getReg() == 0);
736*9880d681SAndroid Build Coastguard Worker }
737*9880d681SAndroid Build Coastguard Worker
738*9880d681SAndroid Build Coastguard Worker namespace {
739*9880d681SAndroid Build Coastguard Worker struct LogicOp {
LogicOp__anon4bf2ff270111::LogicOp740*9880d681SAndroid Build Coastguard Worker LogicOp() : RegSize(0), ImmLSB(0), ImmSize(0) {}
LogicOp__anon4bf2ff270111::LogicOp741*9880d681SAndroid Build Coastguard Worker LogicOp(unsigned regSize, unsigned immLSB, unsigned immSize)
742*9880d681SAndroid Build Coastguard Worker : RegSize(regSize), ImmLSB(immLSB), ImmSize(immSize) {}
743*9880d681SAndroid Build Coastguard Worker
operator bool__anon4bf2ff270111::LogicOp744*9880d681SAndroid Build Coastguard Worker explicit operator bool() const { return RegSize; }
745*9880d681SAndroid Build Coastguard Worker
746*9880d681SAndroid Build Coastguard Worker unsigned RegSize, ImmLSB, ImmSize;
747*9880d681SAndroid Build Coastguard Worker };
748*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
749*9880d681SAndroid Build Coastguard Worker
interpretAndImmediate(unsigned Opcode)750*9880d681SAndroid Build Coastguard Worker static LogicOp interpretAndImmediate(unsigned Opcode) {
751*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
752*9880d681SAndroid Build Coastguard Worker case SystemZ::NILMux: return LogicOp(32, 0, 16);
753*9880d681SAndroid Build Coastguard Worker case SystemZ::NIHMux: return LogicOp(32, 16, 16);
754*9880d681SAndroid Build Coastguard Worker case SystemZ::NILL64: return LogicOp(64, 0, 16);
755*9880d681SAndroid Build Coastguard Worker case SystemZ::NILH64: return LogicOp(64, 16, 16);
756*9880d681SAndroid Build Coastguard Worker case SystemZ::NIHL64: return LogicOp(64, 32, 16);
757*9880d681SAndroid Build Coastguard Worker case SystemZ::NIHH64: return LogicOp(64, 48, 16);
758*9880d681SAndroid Build Coastguard Worker case SystemZ::NIFMux: return LogicOp(32, 0, 32);
759*9880d681SAndroid Build Coastguard Worker case SystemZ::NILF64: return LogicOp(64, 0, 32);
760*9880d681SAndroid Build Coastguard Worker case SystemZ::NIHF64: return LogicOp(64, 32, 32);
761*9880d681SAndroid Build Coastguard Worker default: return LogicOp();
762*9880d681SAndroid Build Coastguard Worker }
763*9880d681SAndroid Build Coastguard Worker }
764*9880d681SAndroid Build Coastguard Worker
transferDeadCC(MachineInstr * OldMI,MachineInstr * NewMI)765*9880d681SAndroid Build Coastguard Worker static void transferDeadCC(MachineInstr *OldMI, MachineInstr *NewMI) {
766*9880d681SAndroid Build Coastguard Worker if (OldMI->registerDefIsDead(SystemZ::CC)) {
767*9880d681SAndroid Build Coastguard Worker MachineOperand *CCDef = NewMI->findRegisterDefOperand(SystemZ::CC);
768*9880d681SAndroid Build Coastguard Worker if (CCDef != nullptr)
769*9880d681SAndroid Build Coastguard Worker CCDef->setIsDead(true);
770*9880d681SAndroid Build Coastguard Worker }
771*9880d681SAndroid Build Coastguard Worker }
772*9880d681SAndroid Build Coastguard Worker
773*9880d681SAndroid Build Coastguard Worker // Used to return from convertToThreeAddress after replacing two-address
774*9880d681SAndroid Build Coastguard Worker // instruction OldMI with three-address instruction NewMI.
finishConvertToThreeAddress(MachineInstr * OldMI,MachineInstr * NewMI,LiveVariables * LV)775*9880d681SAndroid Build Coastguard Worker static MachineInstr *finishConvertToThreeAddress(MachineInstr *OldMI,
776*9880d681SAndroid Build Coastguard Worker MachineInstr *NewMI,
777*9880d681SAndroid Build Coastguard Worker LiveVariables *LV) {
778*9880d681SAndroid Build Coastguard Worker if (LV) {
779*9880d681SAndroid Build Coastguard Worker unsigned NumOps = OldMI->getNumOperands();
780*9880d681SAndroid Build Coastguard Worker for (unsigned I = 1; I < NumOps; ++I) {
781*9880d681SAndroid Build Coastguard Worker MachineOperand &Op = OldMI->getOperand(I);
782*9880d681SAndroid Build Coastguard Worker if (Op.isReg() && Op.isKill())
783*9880d681SAndroid Build Coastguard Worker LV->replaceKillInstruction(Op.getReg(), *OldMI, *NewMI);
784*9880d681SAndroid Build Coastguard Worker }
785*9880d681SAndroid Build Coastguard Worker }
786*9880d681SAndroid Build Coastguard Worker transferDeadCC(OldMI, NewMI);
787*9880d681SAndroid Build Coastguard Worker return NewMI;
788*9880d681SAndroid Build Coastguard Worker }
789*9880d681SAndroid Build Coastguard Worker
convertToThreeAddress(MachineFunction::iterator & MFI,MachineInstr & MI,LiveVariables * LV) const790*9880d681SAndroid Build Coastguard Worker MachineInstr *SystemZInstrInfo::convertToThreeAddress(
791*9880d681SAndroid Build Coastguard Worker MachineFunction::iterator &MFI, MachineInstr &MI, LiveVariables *LV) const {
792*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = MI.getParent();
793*9880d681SAndroid Build Coastguard Worker MachineFunction *MF = MBB->getParent();
794*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI = MF->getRegInfo();
795*9880d681SAndroid Build Coastguard Worker
796*9880d681SAndroid Build Coastguard Worker unsigned Opcode = MI.getOpcode();
797*9880d681SAndroid Build Coastguard Worker unsigned NumOps = MI.getNumOperands();
798*9880d681SAndroid Build Coastguard Worker
799*9880d681SAndroid Build Coastguard Worker // Try to convert something like SLL into SLLK, if supported.
800*9880d681SAndroid Build Coastguard Worker // We prefer to keep the two-operand form where possible both
801*9880d681SAndroid Build Coastguard Worker // because it tends to be shorter and because some instructions
802*9880d681SAndroid Build Coastguard Worker // have memory forms that can be used during spilling.
803*9880d681SAndroid Build Coastguard Worker if (STI.hasDistinctOps()) {
804*9880d681SAndroid Build Coastguard Worker MachineOperand &Dest = MI.getOperand(0);
805*9880d681SAndroid Build Coastguard Worker MachineOperand &Src = MI.getOperand(1);
806*9880d681SAndroid Build Coastguard Worker unsigned DestReg = Dest.getReg();
807*9880d681SAndroid Build Coastguard Worker unsigned SrcReg = Src.getReg();
808*9880d681SAndroid Build Coastguard Worker // AHIMux is only really a three-operand instruction when both operands
809*9880d681SAndroid Build Coastguard Worker // are low registers. Try to constrain both operands to be low if
810*9880d681SAndroid Build Coastguard Worker // possible.
811*9880d681SAndroid Build Coastguard Worker if (Opcode == SystemZ::AHIMux &&
812*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isVirtualRegister(DestReg) &&
813*9880d681SAndroid Build Coastguard Worker TargetRegisterInfo::isVirtualRegister(SrcReg) &&
814*9880d681SAndroid Build Coastguard Worker MRI.getRegClass(DestReg)->contains(SystemZ::R1L) &&
815*9880d681SAndroid Build Coastguard Worker MRI.getRegClass(SrcReg)->contains(SystemZ::R1L)) {
816*9880d681SAndroid Build Coastguard Worker MRI.constrainRegClass(DestReg, &SystemZ::GR32BitRegClass);
817*9880d681SAndroid Build Coastguard Worker MRI.constrainRegClass(SrcReg, &SystemZ::GR32BitRegClass);
818*9880d681SAndroid Build Coastguard Worker }
819*9880d681SAndroid Build Coastguard Worker int ThreeOperandOpcode = SystemZ::getThreeOperandOpcode(Opcode);
820*9880d681SAndroid Build Coastguard Worker if (ThreeOperandOpcode >= 0) {
821*9880d681SAndroid Build Coastguard Worker // Create three address instruction without adding the implicit
822*9880d681SAndroid Build Coastguard Worker // operands. Those will instead be copied over from the original
823*9880d681SAndroid Build Coastguard Worker // instruction by the loop below.
824*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder MIB(
825*9880d681SAndroid Build Coastguard Worker *MF, MF->CreateMachineInstr(get(ThreeOperandOpcode), MI.getDebugLoc(),
826*9880d681SAndroid Build Coastguard Worker /*NoImplicit=*/true));
827*9880d681SAndroid Build Coastguard Worker MIB.addOperand(Dest);
828*9880d681SAndroid Build Coastguard Worker // Keep the kill state, but drop the tied flag.
829*9880d681SAndroid Build Coastguard Worker MIB.addReg(Src.getReg(), getKillRegState(Src.isKill()), Src.getSubReg());
830*9880d681SAndroid Build Coastguard Worker // Keep the remaining operands as-is.
831*9880d681SAndroid Build Coastguard Worker for (unsigned I = 2; I < NumOps; ++I)
832*9880d681SAndroid Build Coastguard Worker MIB.addOperand(MI.getOperand(I));
833*9880d681SAndroid Build Coastguard Worker MBB->insert(MI, MIB);
834*9880d681SAndroid Build Coastguard Worker return finishConvertToThreeAddress(&MI, MIB, LV);
835*9880d681SAndroid Build Coastguard Worker }
836*9880d681SAndroid Build Coastguard Worker }
837*9880d681SAndroid Build Coastguard Worker
838*9880d681SAndroid Build Coastguard Worker // Try to convert an AND into an RISBG-type instruction.
839*9880d681SAndroid Build Coastguard Worker if (LogicOp And = interpretAndImmediate(Opcode)) {
840*9880d681SAndroid Build Coastguard Worker uint64_t Imm = MI.getOperand(2).getImm() << And.ImmLSB;
841*9880d681SAndroid Build Coastguard Worker // AND IMMEDIATE leaves the other bits of the register unchanged.
842*9880d681SAndroid Build Coastguard Worker Imm |= allOnes(And.RegSize) & ~(allOnes(And.ImmSize) << And.ImmLSB);
843*9880d681SAndroid Build Coastguard Worker unsigned Start, End;
844*9880d681SAndroid Build Coastguard Worker if (isRxSBGMask(Imm, And.RegSize, Start, End)) {
845*9880d681SAndroid Build Coastguard Worker unsigned NewOpcode;
846*9880d681SAndroid Build Coastguard Worker if (And.RegSize == 64) {
847*9880d681SAndroid Build Coastguard Worker NewOpcode = SystemZ::RISBG;
848*9880d681SAndroid Build Coastguard Worker // Prefer RISBGN if available, since it does not clobber CC.
849*9880d681SAndroid Build Coastguard Worker if (STI.hasMiscellaneousExtensions())
850*9880d681SAndroid Build Coastguard Worker NewOpcode = SystemZ::RISBGN;
851*9880d681SAndroid Build Coastguard Worker } else {
852*9880d681SAndroid Build Coastguard Worker NewOpcode = SystemZ::RISBMux;
853*9880d681SAndroid Build Coastguard Worker Start &= 31;
854*9880d681SAndroid Build Coastguard Worker End &= 31;
855*9880d681SAndroid Build Coastguard Worker }
856*9880d681SAndroid Build Coastguard Worker MachineOperand &Dest = MI.getOperand(0);
857*9880d681SAndroid Build Coastguard Worker MachineOperand &Src = MI.getOperand(1);
858*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder MIB =
859*9880d681SAndroid Build Coastguard Worker BuildMI(*MBB, MI, MI.getDebugLoc(), get(NewOpcode))
860*9880d681SAndroid Build Coastguard Worker .addOperand(Dest)
861*9880d681SAndroid Build Coastguard Worker .addReg(0)
862*9880d681SAndroid Build Coastguard Worker .addReg(Src.getReg(), getKillRegState(Src.isKill()),
863*9880d681SAndroid Build Coastguard Worker Src.getSubReg())
864*9880d681SAndroid Build Coastguard Worker .addImm(Start)
865*9880d681SAndroid Build Coastguard Worker .addImm(End + 128)
866*9880d681SAndroid Build Coastguard Worker .addImm(0);
867*9880d681SAndroid Build Coastguard Worker return finishConvertToThreeAddress(&MI, MIB, LV);
868*9880d681SAndroid Build Coastguard Worker }
869*9880d681SAndroid Build Coastguard Worker }
870*9880d681SAndroid Build Coastguard Worker return nullptr;
871*9880d681SAndroid Build Coastguard Worker }
872*9880d681SAndroid Build Coastguard Worker
foldMemoryOperandImpl(MachineFunction & MF,MachineInstr & MI,ArrayRef<unsigned> Ops,MachineBasicBlock::iterator InsertPt,int FrameIndex,LiveIntervals * LIS) const873*9880d681SAndroid Build Coastguard Worker MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
874*9880d681SAndroid Build Coastguard Worker MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
875*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator InsertPt, int FrameIndex,
876*9880d681SAndroid Build Coastguard Worker LiveIntervals *LIS) const {
877*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
878*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo *MFI = MF.getFrameInfo();
879*9880d681SAndroid Build Coastguard Worker unsigned Size = MFI->getObjectSize(FrameIndex);
880*9880d681SAndroid Build Coastguard Worker unsigned Opcode = MI.getOpcode();
881*9880d681SAndroid Build Coastguard Worker
882*9880d681SAndroid Build Coastguard Worker if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
883*9880d681SAndroid Build Coastguard Worker if (LIS != nullptr && (Opcode == SystemZ::LA || Opcode == SystemZ::LAY) &&
884*9880d681SAndroid Build Coastguard Worker isInt<8>(MI.getOperand(2).getImm()) && !MI.getOperand(3).getReg()) {
885*9880d681SAndroid Build Coastguard Worker
886*9880d681SAndroid Build Coastguard Worker // Check CC liveness, since new instruction introduces a dead
887*9880d681SAndroid Build Coastguard Worker // def of CC.
888*9880d681SAndroid Build Coastguard Worker MCRegUnitIterator CCUnit(SystemZ::CC, TRI);
889*9880d681SAndroid Build Coastguard Worker LiveRange &CCLiveRange = LIS->getRegUnit(*CCUnit);
890*9880d681SAndroid Build Coastguard Worker ++CCUnit;
891*9880d681SAndroid Build Coastguard Worker assert (!CCUnit.isValid() && "CC only has one reg unit.");
892*9880d681SAndroid Build Coastguard Worker SlotIndex MISlot =
893*9880d681SAndroid Build Coastguard Worker LIS->getSlotIndexes()->getInstructionIndex(MI).getRegSlot();
894*9880d681SAndroid Build Coastguard Worker if (!CCLiveRange.liveAt(MISlot)) {
895*9880d681SAndroid Build Coastguard Worker // LA(Y) %reg, CONST(%reg) -> AGSI %mem, CONST
896*9880d681SAndroid Build Coastguard Worker MachineInstr *BuiltMI = BuildMI(*InsertPt->getParent(), InsertPt,
897*9880d681SAndroid Build Coastguard Worker MI.getDebugLoc(), get(SystemZ::AGSI))
898*9880d681SAndroid Build Coastguard Worker .addFrameIndex(FrameIndex)
899*9880d681SAndroid Build Coastguard Worker .addImm(0)
900*9880d681SAndroid Build Coastguard Worker .addImm(MI.getOperand(2).getImm());
901*9880d681SAndroid Build Coastguard Worker BuiltMI->findRegisterDefOperand(SystemZ::CC)->setIsDead(true);
902*9880d681SAndroid Build Coastguard Worker CCLiveRange.createDeadDef(MISlot, LIS->getVNInfoAllocator());
903*9880d681SAndroid Build Coastguard Worker return BuiltMI;
904*9880d681SAndroid Build Coastguard Worker }
905*9880d681SAndroid Build Coastguard Worker }
906*9880d681SAndroid Build Coastguard Worker return nullptr;
907*9880d681SAndroid Build Coastguard Worker }
908*9880d681SAndroid Build Coastguard Worker
909*9880d681SAndroid Build Coastguard Worker // All other cases require a single operand.
910*9880d681SAndroid Build Coastguard Worker if (Ops.size() != 1)
911*9880d681SAndroid Build Coastguard Worker return nullptr;
912*9880d681SAndroid Build Coastguard Worker
913*9880d681SAndroid Build Coastguard Worker unsigned OpNum = Ops[0];
914*9880d681SAndroid Build Coastguard Worker assert(Size ==
915*9880d681SAndroid Build Coastguard Worker MF.getRegInfo()
916*9880d681SAndroid Build Coastguard Worker .getRegClass(MI.getOperand(OpNum).getReg())
917*9880d681SAndroid Build Coastguard Worker ->getSize() &&
918*9880d681SAndroid Build Coastguard Worker "Invalid size combination");
919*9880d681SAndroid Build Coastguard Worker
920*9880d681SAndroid Build Coastguard Worker if ((Opcode == SystemZ::AHI || Opcode == SystemZ::AGHI) && OpNum == 0 &&
921*9880d681SAndroid Build Coastguard Worker isInt<8>(MI.getOperand(2).getImm())) {
922*9880d681SAndroid Build Coastguard Worker // A(G)HI %reg, CONST -> A(G)SI %mem, CONST
923*9880d681SAndroid Build Coastguard Worker Opcode = (Opcode == SystemZ::AHI ? SystemZ::ASI : SystemZ::AGSI);
924*9880d681SAndroid Build Coastguard Worker MachineInstr *BuiltMI =
925*9880d681SAndroid Build Coastguard Worker BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(), get(Opcode))
926*9880d681SAndroid Build Coastguard Worker .addFrameIndex(FrameIndex)
927*9880d681SAndroid Build Coastguard Worker .addImm(0)
928*9880d681SAndroid Build Coastguard Worker .addImm(MI.getOperand(2).getImm());
929*9880d681SAndroid Build Coastguard Worker transferDeadCC(&MI, BuiltMI);
930*9880d681SAndroid Build Coastguard Worker return BuiltMI;
931*9880d681SAndroid Build Coastguard Worker }
932*9880d681SAndroid Build Coastguard Worker
933*9880d681SAndroid Build Coastguard Worker if (Opcode == SystemZ::LGDR || Opcode == SystemZ::LDGR) {
934*9880d681SAndroid Build Coastguard Worker bool Op0IsGPR = (Opcode == SystemZ::LGDR);
935*9880d681SAndroid Build Coastguard Worker bool Op1IsGPR = (Opcode == SystemZ::LDGR);
936*9880d681SAndroid Build Coastguard Worker // If we're spilling the destination of an LDGR or LGDR, store the
937*9880d681SAndroid Build Coastguard Worker // source register instead.
938*9880d681SAndroid Build Coastguard Worker if (OpNum == 0) {
939*9880d681SAndroid Build Coastguard Worker unsigned StoreOpcode = Op1IsGPR ? SystemZ::STG : SystemZ::STD;
940*9880d681SAndroid Build Coastguard Worker return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
941*9880d681SAndroid Build Coastguard Worker get(StoreOpcode))
942*9880d681SAndroid Build Coastguard Worker .addOperand(MI.getOperand(1))
943*9880d681SAndroid Build Coastguard Worker .addFrameIndex(FrameIndex)
944*9880d681SAndroid Build Coastguard Worker .addImm(0)
945*9880d681SAndroid Build Coastguard Worker .addReg(0);
946*9880d681SAndroid Build Coastguard Worker }
947*9880d681SAndroid Build Coastguard Worker // If we're spilling the source of an LDGR or LGDR, load the
948*9880d681SAndroid Build Coastguard Worker // destination register instead.
949*9880d681SAndroid Build Coastguard Worker if (OpNum == 1) {
950*9880d681SAndroid Build Coastguard Worker unsigned LoadOpcode = Op0IsGPR ? SystemZ::LG : SystemZ::LD;
951*9880d681SAndroid Build Coastguard Worker unsigned Dest = MI.getOperand(0).getReg();
952*9880d681SAndroid Build Coastguard Worker return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
953*9880d681SAndroid Build Coastguard Worker get(LoadOpcode), Dest)
954*9880d681SAndroid Build Coastguard Worker .addFrameIndex(FrameIndex)
955*9880d681SAndroid Build Coastguard Worker .addImm(0)
956*9880d681SAndroid Build Coastguard Worker .addReg(0);
957*9880d681SAndroid Build Coastguard Worker }
958*9880d681SAndroid Build Coastguard Worker }
959*9880d681SAndroid Build Coastguard Worker
960*9880d681SAndroid Build Coastguard Worker // Look for cases where the source of a simple store or the destination
961*9880d681SAndroid Build Coastguard Worker // of a simple load is being spilled. Try to use MVC instead.
962*9880d681SAndroid Build Coastguard Worker //
963*9880d681SAndroid Build Coastguard Worker // Although MVC is in practice a fast choice in these cases, it is still
964*9880d681SAndroid Build Coastguard Worker // logically a bytewise copy. This means that we cannot use it if the
965*9880d681SAndroid Build Coastguard Worker // load or store is volatile. We also wouldn't be able to use MVC if
966*9880d681SAndroid Build Coastguard Worker // the two memories partially overlap, but that case cannot occur here,
967*9880d681SAndroid Build Coastguard Worker // because we know that one of the memories is a full frame index.
968*9880d681SAndroid Build Coastguard Worker //
969*9880d681SAndroid Build Coastguard Worker // For performance reasons, we also want to avoid using MVC if the addresses
970*9880d681SAndroid Build Coastguard Worker // might be equal. We don't worry about that case here, because spill slot
971*9880d681SAndroid Build Coastguard Worker // coloring happens later, and because we have special code to remove
972*9880d681SAndroid Build Coastguard Worker // MVCs that turn out to be redundant.
973*9880d681SAndroid Build Coastguard Worker if (OpNum == 0 && MI.hasOneMemOperand()) {
974*9880d681SAndroid Build Coastguard Worker MachineMemOperand *MMO = *MI.memoperands_begin();
975*9880d681SAndroid Build Coastguard Worker if (MMO->getSize() == Size && !MMO->isVolatile()) {
976*9880d681SAndroid Build Coastguard Worker // Handle conversion of loads.
977*9880d681SAndroid Build Coastguard Worker if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXLoad)) {
978*9880d681SAndroid Build Coastguard Worker return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
979*9880d681SAndroid Build Coastguard Worker get(SystemZ::MVC))
980*9880d681SAndroid Build Coastguard Worker .addFrameIndex(FrameIndex)
981*9880d681SAndroid Build Coastguard Worker .addImm(0)
982*9880d681SAndroid Build Coastguard Worker .addImm(Size)
983*9880d681SAndroid Build Coastguard Worker .addOperand(MI.getOperand(1))
984*9880d681SAndroid Build Coastguard Worker .addImm(MI.getOperand(2).getImm())
985*9880d681SAndroid Build Coastguard Worker .addMemOperand(MMO);
986*9880d681SAndroid Build Coastguard Worker }
987*9880d681SAndroid Build Coastguard Worker // Handle conversion of stores.
988*9880d681SAndroid Build Coastguard Worker if (isSimpleBD12Move(&MI, SystemZII::SimpleBDXStore)) {
989*9880d681SAndroid Build Coastguard Worker return BuildMI(*InsertPt->getParent(), InsertPt, MI.getDebugLoc(),
990*9880d681SAndroid Build Coastguard Worker get(SystemZ::MVC))
991*9880d681SAndroid Build Coastguard Worker .addOperand(MI.getOperand(1))
992*9880d681SAndroid Build Coastguard Worker .addImm(MI.getOperand(2).getImm())
993*9880d681SAndroid Build Coastguard Worker .addImm(Size)
994*9880d681SAndroid Build Coastguard Worker .addFrameIndex(FrameIndex)
995*9880d681SAndroid Build Coastguard Worker .addImm(0)
996*9880d681SAndroid Build Coastguard Worker .addMemOperand(MMO);
997*9880d681SAndroid Build Coastguard Worker }
998*9880d681SAndroid Build Coastguard Worker }
999*9880d681SAndroid Build Coastguard Worker }
1000*9880d681SAndroid Build Coastguard Worker
1001*9880d681SAndroid Build Coastguard Worker // If the spilled operand is the final one, try to change <INSN>R
1002*9880d681SAndroid Build Coastguard Worker // into <INSN>.
1003*9880d681SAndroid Build Coastguard Worker int MemOpcode = SystemZ::getMemOpcode(Opcode);
1004*9880d681SAndroid Build Coastguard Worker if (MemOpcode >= 0) {
1005*9880d681SAndroid Build Coastguard Worker unsigned NumOps = MI.getNumExplicitOperands();
1006*9880d681SAndroid Build Coastguard Worker if (OpNum == NumOps - 1) {
1007*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MemDesc = get(MemOpcode);
1008*9880d681SAndroid Build Coastguard Worker uint64_t AccessBytes = SystemZII::getAccessSize(MemDesc.TSFlags);
1009*9880d681SAndroid Build Coastguard Worker assert(AccessBytes != 0 && "Size of access should be known");
1010*9880d681SAndroid Build Coastguard Worker assert(AccessBytes <= Size && "Access outside the frame index");
1011*9880d681SAndroid Build Coastguard Worker uint64_t Offset = Size - AccessBytes;
1012*9880d681SAndroid Build Coastguard Worker MachineInstrBuilder MIB = BuildMI(*InsertPt->getParent(), InsertPt,
1013*9880d681SAndroid Build Coastguard Worker MI.getDebugLoc(), get(MemOpcode));
1014*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0; I < OpNum; ++I)
1015*9880d681SAndroid Build Coastguard Worker MIB.addOperand(MI.getOperand(I));
1016*9880d681SAndroid Build Coastguard Worker MIB.addFrameIndex(FrameIndex).addImm(Offset);
1017*9880d681SAndroid Build Coastguard Worker if (MemDesc.TSFlags & SystemZII::HasIndex)
1018*9880d681SAndroid Build Coastguard Worker MIB.addReg(0);
1019*9880d681SAndroid Build Coastguard Worker transferDeadCC(&MI, MIB);
1020*9880d681SAndroid Build Coastguard Worker return MIB;
1021*9880d681SAndroid Build Coastguard Worker }
1022*9880d681SAndroid Build Coastguard Worker }
1023*9880d681SAndroid Build Coastguard Worker
1024*9880d681SAndroid Build Coastguard Worker return nullptr;
1025*9880d681SAndroid Build Coastguard Worker }
1026*9880d681SAndroid Build Coastguard Worker
foldMemoryOperandImpl(MachineFunction & MF,MachineInstr & MI,ArrayRef<unsigned> Ops,MachineBasicBlock::iterator InsertPt,MachineInstr & LoadMI,LiveIntervals * LIS) const1027*9880d681SAndroid Build Coastguard Worker MachineInstr *SystemZInstrInfo::foldMemoryOperandImpl(
1028*9880d681SAndroid Build Coastguard Worker MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
1029*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
1030*9880d681SAndroid Build Coastguard Worker LiveIntervals *LIS) const {
1031*9880d681SAndroid Build Coastguard Worker return nullptr;
1032*9880d681SAndroid Build Coastguard Worker }
1033*9880d681SAndroid Build Coastguard Worker
expandPostRAPseudo(MachineInstr & MI) const1034*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::expandPostRAPseudo(MachineInstr &MI) const {
1035*9880d681SAndroid Build Coastguard Worker switch (MI.getOpcode()) {
1036*9880d681SAndroid Build Coastguard Worker case SystemZ::L128:
1037*9880d681SAndroid Build Coastguard Worker splitMove(MI, SystemZ::LG);
1038*9880d681SAndroid Build Coastguard Worker return true;
1039*9880d681SAndroid Build Coastguard Worker
1040*9880d681SAndroid Build Coastguard Worker case SystemZ::ST128:
1041*9880d681SAndroid Build Coastguard Worker splitMove(MI, SystemZ::STG);
1042*9880d681SAndroid Build Coastguard Worker return true;
1043*9880d681SAndroid Build Coastguard Worker
1044*9880d681SAndroid Build Coastguard Worker case SystemZ::LX:
1045*9880d681SAndroid Build Coastguard Worker splitMove(MI, SystemZ::LD);
1046*9880d681SAndroid Build Coastguard Worker return true;
1047*9880d681SAndroid Build Coastguard Worker
1048*9880d681SAndroid Build Coastguard Worker case SystemZ::STX:
1049*9880d681SAndroid Build Coastguard Worker splitMove(MI, SystemZ::STD);
1050*9880d681SAndroid Build Coastguard Worker return true;
1051*9880d681SAndroid Build Coastguard Worker
1052*9880d681SAndroid Build Coastguard Worker case SystemZ::LBMux:
1053*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::LB, SystemZ::LBH);
1054*9880d681SAndroid Build Coastguard Worker return true;
1055*9880d681SAndroid Build Coastguard Worker
1056*9880d681SAndroid Build Coastguard Worker case SystemZ::LHMux:
1057*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::LH, SystemZ::LHH);
1058*9880d681SAndroid Build Coastguard Worker return true;
1059*9880d681SAndroid Build Coastguard Worker
1060*9880d681SAndroid Build Coastguard Worker case SystemZ::LLCRMux:
1061*9880d681SAndroid Build Coastguard Worker expandZExtPseudo(MI, SystemZ::LLCR, 8);
1062*9880d681SAndroid Build Coastguard Worker return true;
1063*9880d681SAndroid Build Coastguard Worker
1064*9880d681SAndroid Build Coastguard Worker case SystemZ::LLHRMux:
1065*9880d681SAndroid Build Coastguard Worker expandZExtPseudo(MI, SystemZ::LLHR, 16);
1066*9880d681SAndroid Build Coastguard Worker return true;
1067*9880d681SAndroid Build Coastguard Worker
1068*9880d681SAndroid Build Coastguard Worker case SystemZ::LLCMux:
1069*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::LLC, SystemZ::LLCH);
1070*9880d681SAndroid Build Coastguard Worker return true;
1071*9880d681SAndroid Build Coastguard Worker
1072*9880d681SAndroid Build Coastguard Worker case SystemZ::LLHMux:
1073*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::LLH, SystemZ::LLHH);
1074*9880d681SAndroid Build Coastguard Worker return true;
1075*9880d681SAndroid Build Coastguard Worker
1076*9880d681SAndroid Build Coastguard Worker case SystemZ::LMux:
1077*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::L, SystemZ::LFH);
1078*9880d681SAndroid Build Coastguard Worker return true;
1079*9880d681SAndroid Build Coastguard Worker
1080*9880d681SAndroid Build Coastguard Worker case SystemZ::STCMux:
1081*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::STC, SystemZ::STCH);
1082*9880d681SAndroid Build Coastguard Worker return true;
1083*9880d681SAndroid Build Coastguard Worker
1084*9880d681SAndroid Build Coastguard Worker case SystemZ::STHMux:
1085*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::STH, SystemZ::STHH);
1086*9880d681SAndroid Build Coastguard Worker return true;
1087*9880d681SAndroid Build Coastguard Worker
1088*9880d681SAndroid Build Coastguard Worker case SystemZ::STMux:
1089*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::ST, SystemZ::STFH);
1090*9880d681SAndroid Build Coastguard Worker return true;
1091*9880d681SAndroid Build Coastguard Worker
1092*9880d681SAndroid Build Coastguard Worker case SystemZ::LHIMux:
1093*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::LHI, SystemZ::IIHF, true);
1094*9880d681SAndroid Build Coastguard Worker return true;
1095*9880d681SAndroid Build Coastguard Worker
1096*9880d681SAndroid Build Coastguard Worker case SystemZ::IIFMux:
1097*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::IILF, SystemZ::IIHF, false);
1098*9880d681SAndroid Build Coastguard Worker return true;
1099*9880d681SAndroid Build Coastguard Worker
1100*9880d681SAndroid Build Coastguard Worker case SystemZ::IILMux:
1101*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::IILL, SystemZ::IIHL, false);
1102*9880d681SAndroid Build Coastguard Worker return true;
1103*9880d681SAndroid Build Coastguard Worker
1104*9880d681SAndroid Build Coastguard Worker case SystemZ::IIHMux:
1105*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::IILH, SystemZ::IIHH, false);
1106*9880d681SAndroid Build Coastguard Worker return true;
1107*9880d681SAndroid Build Coastguard Worker
1108*9880d681SAndroid Build Coastguard Worker case SystemZ::NIFMux:
1109*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::NILF, SystemZ::NIHF, false);
1110*9880d681SAndroid Build Coastguard Worker return true;
1111*9880d681SAndroid Build Coastguard Worker
1112*9880d681SAndroid Build Coastguard Worker case SystemZ::NILMux:
1113*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::NILL, SystemZ::NIHL, false);
1114*9880d681SAndroid Build Coastguard Worker return true;
1115*9880d681SAndroid Build Coastguard Worker
1116*9880d681SAndroid Build Coastguard Worker case SystemZ::NIHMux:
1117*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::NILH, SystemZ::NIHH, false);
1118*9880d681SAndroid Build Coastguard Worker return true;
1119*9880d681SAndroid Build Coastguard Worker
1120*9880d681SAndroid Build Coastguard Worker case SystemZ::OIFMux:
1121*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::OILF, SystemZ::OIHF, false);
1122*9880d681SAndroid Build Coastguard Worker return true;
1123*9880d681SAndroid Build Coastguard Worker
1124*9880d681SAndroid Build Coastguard Worker case SystemZ::OILMux:
1125*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::OILL, SystemZ::OIHL, false);
1126*9880d681SAndroid Build Coastguard Worker return true;
1127*9880d681SAndroid Build Coastguard Worker
1128*9880d681SAndroid Build Coastguard Worker case SystemZ::OIHMux:
1129*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::OILH, SystemZ::OIHH, false);
1130*9880d681SAndroid Build Coastguard Worker return true;
1131*9880d681SAndroid Build Coastguard Worker
1132*9880d681SAndroid Build Coastguard Worker case SystemZ::XIFMux:
1133*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::XILF, SystemZ::XIHF, false);
1134*9880d681SAndroid Build Coastguard Worker return true;
1135*9880d681SAndroid Build Coastguard Worker
1136*9880d681SAndroid Build Coastguard Worker case SystemZ::TMLMux:
1137*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::TMLL, SystemZ::TMHL, false);
1138*9880d681SAndroid Build Coastguard Worker return true;
1139*9880d681SAndroid Build Coastguard Worker
1140*9880d681SAndroid Build Coastguard Worker case SystemZ::TMHMux:
1141*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::TMLH, SystemZ::TMHH, false);
1142*9880d681SAndroid Build Coastguard Worker return true;
1143*9880d681SAndroid Build Coastguard Worker
1144*9880d681SAndroid Build Coastguard Worker case SystemZ::AHIMux:
1145*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::AHI, SystemZ::AIH, false);
1146*9880d681SAndroid Build Coastguard Worker return true;
1147*9880d681SAndroid Build Coastguard Worker
1148*9880d681SAndroid Build Coastguard Worker case SystemZ::AHIMuxK:
1149*9880d681SAndroid Build Coastguard Worker expandRIEPseudo(MI, SystemZ::AHI, SystemZ::AHIK, SystemZ::AIH);
1150*9880d681SAndroid Build Coastguard Worker return true;
1151*9880d681SAndroid Build Coastguard Worker
1152*9880d681SAndroid Build Coastguard Worker case SystemZ::AFIMux:
1153*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::AFI, SystemZ::AIH, false);
1154*9880d681SAndroid Build Coastguard Worker return true;
1155*9880d681SAndroid Build Coastguard Worker
1156*9880d681SAndroid Build Coastguard Worker case SystemZ::CFIMux:
1157*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::CFI, SystemZ::CIH, false);
1158*9880d681SAndroid Build Coastguard Worker return true;
1159*9880d681SAndroid Build Coastguard Worker
1160*9880d681SAndroid Build Coastguard Worker case SystemZ::CLFIMux:
1161*9880d681SAndroid Build Coastguard Worker expandRIPseudo(MI, SystemZ::CLFI, SystemZ::CLIH, false);
1162*9880d681SAndroid Build Coastguard Worker return true;
1163*9880d681SAndroid Build Coastguard Worker
1164*9880d681SAndroid Build Coastguard Worker case SystemZ::CMux:
1165*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::C, SystemZ::CHF);
1166*9880d681SAndroid Build Coastguard Worker return true;
1167*9880d681SAndroid Build Coastguard Worker
1168*9880d681SAndroid Build Coastguard Worker case SystemZ::CLMux:
1169*9880d681SAndroid Build Coastguard Worker expandRXYPseudo(MI, SystemZ::CL, SystemZ::CLHF);
1170*9880d681SAndroid Build Coastguard Worker return true;
1171*9880d681SAndroid Build Coastguard Worker
1172*9880d681SAndroid Build Coastguard Worker case SystemZ::RISBMux: {
1173*9880d681SAndroid Build Coastguard Worker bool DestIsHigh = isHighReg(MI.getOperand(0).getReg());
1174*9880d681SAndroid Build Coastguard Worker bool SrcIsHigh = isHighReg(MI.getOperand(2).getReg());
1175*9880d681SAndroid Build Coastguard Worker if (SrcIsHigh == DestIsHigh)
1176*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(DestIsHigh ? SystemZ::RISBHH : SystemZ::RISBLL));
1177*9880d681SAndroid Build Coastguard Worker else {
1178*9880d681SAndroid Build Coastguard Worker MI.setDesc(get(DestIsHigh ? SystemZ::RISBHL : SystemZ::RISBLH));
1179*9880d681SAndroid Build Coastguard Worker MI.getOperand(5).setImm(MI.getOperand(5).getImm() ^ 32);
1180*9880d681SAndroid Build Coastguard Worker }
1181*9880d681SAndroid Build Coastguard Worker return true;
1182*9880d681SAndroid Build Coastguard Worker }
1183*9880d681SAndroid Build Coastguard Worker
1184*9880d681SAndroid Build Coastguard Worker case SystemZ::ADJDYNALLOC:
1185*9880d681SAndroid Build Coastguard Worker splitAdjDynAlloc(MI);
1186*9880d681SAndroid Build Coastguard Worker return true;
1187*9880d681SAndroid Build Coastguard Worker
1188*9880d681SAndroid Build Coastguard Worker case TargetOpcode::LOAD_STACK_GUARD:
1189*9880d681SAndroid Build Coastguard Worker expandLoadStackGuard(&MI);
1190*9880d681SAndroid Build Coastguard Worker return true;
1191*9880d681SAndroid Build Coastguard Worker
1192*9880d681SAndroid Build Coastguard Worker default:
1193*9880d681SAndroid Build Coastguard Worker return false;
1194*9880d681SAndroid Build Coastguard Worker }
1195*9880d681SAndroid Build Coastguard Worker }
1196*9880d681SAndroid Build Coastguard Worker
getInstSizeInBytes(const MachineInstr & MI) const1197*9880d681SAndroid Build Coastguard Worker uint64_t SystemZInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
1198*9880d681SAndroid Build Coastguard Worker if (MI.getOpcode() == TargetOpcode::INLINEASM) {
1199*9880d681SAndroid Build Coastguard Worker const MachineFunction *MF = MI.getParent()->getParent();
1200*9880d681SAndroid Build Coastguard Worker const char *AsmStr = MI.getOperand(0).getSymbolName();
1201*9880d681SAndroid Build Coastguard Worker return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1202*9880d681SAndroid Build Coastguard Worker }
1203*9880d681SAndroid Build Coastguard Worker return MI.getDesc().getSize();
1204*9880d681SAndroid Build Coastguard Worker }
1205*9880d681SAndroid Build Coastguard Worker
1206*9880d681SAndroid Build Coastguard Worker SystemZII::Branch
getBranchInfo(const MachineInstr & MI) const1207*9880d681SAndroid Build Coastguard Worker SystemZInstrInfo::getBranchInfo(const MachineInstr &MI) const {
1208*9880d681SAndroid Build Coastguard Worker switch (MI.getOpcode()) {
1209*9880d681SAndroid Build Coastguard Worker case SystemZ::BR:
1210*9880d681SAndroid Build Coastguard Worker case SystemZ::J:
1211*9880d681SAndroid Build Coastguard Worker case SystemZ::JG:
1212*9880d681SAndroid Build Coastguard Worker return SystemZII::Branch(SystemZII::BranchNormal, SystemZ::CCMASK_ANY,
1213*9880d681SAndroid Build Coastguard Worker SystemZ::CCMASK_ANY, &MI.getOperand(0));
1214*9880d681SAndroid Build Coastguard Worker
1215*9880d681SAndroid Build Coastguard Worker case SystemZ::BRC:
1216*9880d681SAndroid Build Coastguard Worker case SystemZ::BRCL:
1217*9880d681SAndroid Build Coastguard Worker return SystemZII::Branch(SystemZII::BranchNormal, MI.getOperand(0).getImm(),
1218*9880d681SAndroid Build Coastguard Worker MI.getOperand(1).getImm(), &MI.getOperand(2));
1219*9880d681SAndroid Build Coastguard Worker
1220*9880d681SAndroid Build Coastguard Worker case SystemZ::BRCT:
1221*9880d681SAndroid Build Coastguard Worker return SystemZII::Branch(SystemZII::BranchCT, SystemZ::CCMASK_ICMP,
1222*9880d681SAndroid Build Coastguard Worker SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
1223*9880d681SAndroid Build Coastguard Worker
1224*9880d681SAndroid Build Coastguard Worker case SystemZ::BRCTG:
1225*9880d681SAndroid Build Coastguard Worker return SystemZII::Branch(SystemZII::BranchCTG, SystemZ::CCMASK_ICMP,
1226*9880d681SAndroid Build Coastguard Worker SystemZ::CCMASK_CMP_NE, &MI.getOperand(2));
1227*9880d681SAndroid Build Coastguard Worker
1228*9880d681SAndroid Build Coastguard Worker case SystemZ::CIJ:
1229*9880d681SAndroid Build Coastguard Worker case SystemZ::CRJ:
1230*9880d681SAndroid Build Coastguard Worker return SystemZII::Branch(SystemZII::BranchC, SystemZ::CCMASK_ICMP,
1231*9880d681SAndroid Build Coastguard Worker MI.getOperand(2).getImm(), &MI.getOperand(3));
1232*9880d681SAndroid Build Coastguard Worker
1233*9880d681SAndroid Build Coastguard Worker case SystemZ::CLIJ:
1234*9880d681SAndroid Build Coastguard Worker case SystemZ::CLRJ:
1235*9880d681SAndroid Build Coastguard Worker return SystemZII::Branch(SystemZII::BranchCL, SystemZ::CCMASK_ICMP,
1236*9880d681SAndroid Build Coastguard Worker MI.getOperand(2).getImm(), &MI.getOperand(3));
1237*9880d681SAndroid Build Coastguard Worker
1238*9880d681SAndroid Build Coastguard Worker case SystemZ::CGIJ:
1239*9880d681SAndroid Build Coastguard Worker case SystemZ::CGRJ:
1240*9880d681SAndroid Build Coastguard Worker return SystemZII::Branch(SystemZII::BranchCG, SystemZ::CCMASK_ICMP,
1241*9880d681SAndroid Build Coastguard Worker MI.getOperand(2).getImm(), &MI.getOperand(3));
1242*9880d681SAndroid Build Coastguard Worker
1243*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGIJ:
1244*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGRJ:
1245*9880d681SAndroid Build Coastguard Worker return SystemZII::Branch(SystemZII::BranchCLG, SystemZ::CCMASK_ICMP,
1246*9880d681SAndroid Build Coastguard Worker MI.getOperand(2).getImm(), &MI.getOperand(3));
1247*9880d681SAndroid Build Coastguard Worker
1248*9880d681SAndroid Build Coastguard Worker default:
1249*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unrecognized branch opcode");
1250*9880d681SAndroid Build Coastguard Worker }
1251*9880d681SAndroid Build Coastguard Worker }
1252*9880d681SAndroid Build Coastguard Worker
getLoadStoreOpcodes(const TargetRegisterClass * RC,unsigned & LoadOpcode,unsigned & StoreOpcode) const1253*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::getLoadStoreOpcodes(const TargetRegisterClass *RC,
1254*9880d681SAndroid Build Coastguard Worker unsigned &LoadOpcode,
1255*9880d681SAndroid Build Coastguard Worker unsigned &StoreOpcode) const {
1256*9880d681SAndroid Build Coastguard Worker if (RC == &SystemZ::GR32BitRegClass || RC == &SystemZ::ADDR32BitRegClass) {
1257*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::L;
1258*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::ST;
1259*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::GRH32BitRegClass) {
1260*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::LFH;
1261*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::STFH;
1262*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::GRX32BitRegClass) {
1263*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::LMux;
1264*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::STMux;
1265*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::GR64BitRegClass ||
1266*9880d681SAndroid Build Coastguard Worker RC == &SystemZ::ADDR64BitRegClass) {
1267*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::LG;
1268*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::STG;
1269*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::GR128BitRegClass ||
1270*9880d681SAndroid Build Coastguard Worker RC == &SystemZ::ADDR128BitRegClass) {
1271*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::L128;
1272*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::ST128;
1273*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::FP32BitRegClass) {
1274*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::LE;
1275*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::STE;
1276*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::FP64BitRegClass) {
1277*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::LD;
1278*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::STD;
1279*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::FP128BitRegClass) {
1280*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::LX;
1281*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::STX;
1282*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::VR32BitRegClass) {
1283*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::VL32;
1284*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::VST32;
1285*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::VR64BitRegClass) {
1286*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::VL64;
1287*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::VST64;
1288*9880d681SAndroid Build Coastguard Worker } else if (RC == &SystemZ::VF128BitRegClass ||
1289*9880d681SAndroid Build Coastguard Worker RC == &SystemZ::VR128BitRegClass) {
1290*9880d681SAndroid Build Coastguard Worker LoadOpcode = SystemZ::VL;
1291*9880d681SAndroid Build Coastguard Worker StoreOpcode = SystemZ::VST;
1292*9880d681SAndroid Build Coastguard Worker } else
1293*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unsupported regclass to load or store");
1294*9880d681SAndroid Build Coastguard Worker }
1295*9880d681SAndroid Build Coastguard Worker
getOpcodeForOffset(unsigned Opcode,int64_t Offset) const1296*9880d681SAndroid Build Coastguard Worker unsigned SystemZInstrInfo::getOpcodeForOffset(unsigned Opcode,
1297*9880d681SAndroid Build Coastguard Worker int64_t Offset) const {
1298*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID = get(Opcode);
1299*9880d681SAndroid Build Coastguard Worker int64_t Offset2 = (MCID.TSFlags & SystemZII::Is128Bit ? Offset + 8 : Offset);
1300*9880d681SAndroid Build Coastguard Worker if (isUInt<12>(Offset) && isUInt<12>(Offset2)) {
1301*9880d681SAndroid Build Coastguard Worker // Get the instruction to use for unsigned 12-bit displacements.
1302*9880d681SAndroid Build Coastguard Worker int Disp12Opcode = SystemZ::getDisp12Opcode(Opcode);
1303*9880d681SAndroid Build Coastguard Worker if (Disp12Opcode >= 0)
1304*9880d681SAndroid Build Coastguard Worker return Disp12Opcode;
1305*9880d681SAndroid Build Coastguard Worker
1306*9880d681SAndroid Build Coastguard Worker // All address-related instructions can use unsigned 12-bit
1307*9880d681SAndroid Build Coastguard Worker // displacements.
1308*9880d681SAndroid Build Coastguard Worker return Opcode;
1309*9880d681SAndroid Build Coastguard Worker }
1310*9880d681SAndroid Build Coastguard Worker if (isInt<20>(Offset) && isInt<20>(Offset2)) {
1311*9880d681SAndroid Build Coastguard Worker // Get the instruction to use for signed 20-bit displacements.
1312*9880d681SAndroid Build Coastguard Worker int Disp20Opcode = SystemZ::getDisp20Opcode(Opcode);
1313*9880d681SAndroid Build Coastguard Worker if (Disp20Opcode >= 0)
1314*9880d681SAndroid Build Coastguard Worker return Disp20Opcode;
1315*9880d681SAndroid Build Coastguard Worker
1316*9880d681SAndroid Build Coastguard Worker // Check whether Opcode allows signed 20-bit displacements.
1317*9880d681SAndroid Build Coastguard Worker if (MCID.TSFlags & SystemZII::Has20BitOffset)
1318*9880d681SAndroid Build Coastguard Worker return Opcode;
1319*9880d681SAndroid Build Coastguard Worker }
1320*9880d681SAndroid Build Coastguard Worker return 0;
1321*9880d681SAndroid Build Coastguard Worker }
1322*9880d681SAndroid Build Coastguard Worker
getLoadAndTest(unsigned Opcode) const1323*9880d681SAndroid Build Coastguard Worker unsigned SystemZInstrInfo::getLoadAndTest(unsigned Opcode) const {
1324*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
1325*9880d681SAndroid Build Coastguard Worker case SystemZ::L: return SystemZ::LT;
1326*9880d681SAndroid Build Coastguard Worker case SystemZ::LY: return SystemZ::LT;
1327*9880d681SAndroid Build Coastguard Worker case SystemZ::LG: return SystemZ::LTG;
1328*9880d681SAndroid Build Coastguard Worker case SystemZ::LGF: return SystemZ::LTGF;
1329*9880d681SAndroid Build Coastguard Worker case SystemZ::LR: return SystemZ::LTR;
1330*9880d681SAndroid Build Coastguard Worker case SystemZ::LGFR: return SystemZ::LTGFR;
1331*9880d681SAndroid Build Coastguard Worker case SystemZ::LGR: return SystemZ::LTGR;
1332*9880d681SAndroid Build Coastguard Worker case SystemZ::LER: return SystemZ::LTEBR;
1333*9880d681SAndroid Build Coastguard Worker case SystemZ::LDR: return SystemZ::LTDBR;
1334*9880d681SAndroid Build Coastguard Worker case SystemZ::LXR: return SystemZ::LTXBR;
1335*9880d681SAndroid Build Coastguard Worker case SystemZ::LCDFR: return SystemZ::LCDBR;
1336*9880d681SAndroid Build Coastguard Worker case SystemZ::LPDFR: return SystemZ::LPDBR;
1337*9880d681SAndroid Build Coastguard Worker case SystemZ::LNDFR: return SystemZ::LNDBR;
1338*9880d681SAndroid Build Coastguard Worker case SystemZ::LCDFR_32: return SystemZ::LCEBR;
1339*9880d681SAndroid Build Coastguard Worker case SystemZ::LPDFR_32: return SystemZ::LPEBR;
1340*9880d681SAndroid Build Coastguard Worker case SystemZ::LNDFR_32: return SystemZ::LNEBR;
1341*9880d681SAndroid Build Coastguard Worker // On zEC12 we prefer to use RISBGN. But if there is a chance to
1342*9880d681SAndroid Build Coastguard Worker // actually use the condition code, we may turn it back into RISGB.
1343*9880d681SAndroid Build Coastguard Worker // Note that RISBG is not really a "load-and-test" instruction,
1344*9880d681SAndroid Build Coastguard Worker // but sets the same condition code values, so is OK to use here.
1345*9880d681SAndroid Build Coastguard Worker case SystemZ::RISBGN: return SystemZ::RISBG;
1346*9880d681SAndroid Build Coastguard Worker default: return 0;
1347*9880d681SAndroid Build Coastguard Worker }
1348*9880d681SAndroid Build Coastguard Worker }
1349*9880d681SAndroid Build Coastguard Worker
1350*9880d681SAndroid Build Coastguard Worker // Return true if Mask matches the regexp 0*1+0*, given that zero masks
1351*9880d681SAndroid Build Coastguard Worker // have already been filtered out. Store the first set bit in LSB and
1352*9880d681SAndroid Build Coastguard Worker // the number of set bits in Length if so.
isStringOfOnes(uint64_t Mask,unsigned & LSB,unsigned & Length)1353*9880d681SAndroid Build Coastguard Worker static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
1354*9880d681SAndroid Build Coastguard Worker unsigned First = findFirstSet(Mask);
1355*9880d681SAndroid Build Coastguard Worker uint64_t Top = (Mask >> First) + 1;
1356*9880d681SAndroid Build Coastguard Worker if ((Top & -Top) == Top) {
1357*9880d681SAndroid Build Coastguard Worker LSB = First;
1358*9880d681SAndroid Build Coastguard Worker Length = findFirstSet(Top);
1359*9880d681SAndroid Build Coastguard Worker return true;
1360*9880d681SAndroid Build Coastguard Worker }
1361*9880d681SAndroid Build Coastguard Worker return false;
1362*9880d681SAndroid Build Coastguard Worker }
1363*9880d681SAndroid Build Coastguard Worker
isRxSBGMask(uint64_t Mask,unsigned BitSize,unsigned & Start,unsigned & End) const1364*9880d681SAndroid Build Coastguard Worker bool SystemZInstrInfo::isRxSBGMask(uint64_t Mask, unsigned BitSize,
1365*9880d681SAndroid Build Coastguard Worker unsigned &Start, unsigned &End) const {
1366*9880d681SAndroid Build Coastguard Worker // Reject trivial all-zero masks.
1367*9880d681SAndroid Build Coastguard Worker Mask &= allOnes(BitSize);
1368*9880d681SAndroid Build Coastguard Worker if (Mask == 0)
1369*9880d681SAndroid Build Coastguard Worker return false;
1370*9880d681SAndroid Build Coastguard Worker
1371*9880d681SAndroid Build Coastguard Worker // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
1372*9880d681SAndroid Build Coastguard Worker // the msb and End specifies the index of the lsb.
1373*9880d681SAndroid Build Coastguard Worker unsigned LSB, Length;
1374*9880d681SAndroid Build Coastguard Worker if (isStringOfOnes(Mask, LSB, Length)) {
1375*9880d681SAndroid Build Coastguard Worker Start = 63 - (LSB + Length - 1);
1376*9880d681SAndroid Build Coastguard Worker End = 63 - LSB;
1377*9880d681SAndroid Build Coastguard Worker return true;
1378*9880d681SAndroid Build Coastguard Worker }
1379*9880d681SAndroid Build Coastguard Worker
1380*9880d681SAndroid Build Coastguard Worker // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
1381*9880d681SAndroid Build Coastguard Worker // of the low 1s and End specifies the lsb of the high 1s.
1382*9880d681SAndroid Build Coastguard Worker if (isStringOfOnes(Mask ^ allOnes(BitSize), LSB, Length)) {
1383*9880d681SAndroid Build Coastguard Worker assert(LSB > 0 && "Bottom bit must be set");
1384*9880d681SAndroid Build Coastguard Worker assert(LSB + Length < BitSize && "Top bit must be set");
1385*9880d681SAndroid Build Coastguard Worker Start = 63 - (LSB - 1);
1386*9880d681SAndroid Build Coastguard Worker End = 63 - (LSB + Length);
1387*9880d681SAndroid Build Coastguard Worker return true;
1388*9880d681SAndroid Build Coastguard Worker }
1389*9880d681SAndroid Build Coastguard Worker
1390*9880d681SAndroid Build Coastguard Worker return false;
1391*9880d681SAndroid Build Coastguard Worker }
1392*9880d681SAndroid Build Coastguard Worker
getFusedCompare(unsigned Opcode,SystemZII::FusedCompareType Type,const MachineInstr * MI) const1393*9880d681SAndroid Build Coastguard Worker unsigned SystemZInstrInfo::getFusedCompare(unsigned Opcode,
1394*9880d681SAndroid Build Coastguard Worker SystemZII::FusedCompareType Type,
1395*9880d681SAndroid Build Coastguard Worker const MachineInstr *MI) const {
1396*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
1397*9880d681SAndroid Build Coastguard Worker case SystemZ::CHI:
1398*9880d681SAndroid Build Coastguard Worker case SystemZ::CGHI:
1399*9880d681SAndroid Build Coastguard Worker if (!(MI && isInt<8>(MI->getOperand(1).getImm())))
1400*9880d681SAndroid Build Coastguard Worker return 0;
1401*9880d681SAndroid Build Coastguard Worker break;
1402*9880d681SAndroid Build Coastguard Worker case SystemZ::CLFI:
1403*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGFI:
1404*9880d681SAndroid Build Coastguard Worker if (!(MI && isUInt<8>(MI->getOperand(1).getImm())))
1405*9880d681SAndroid Build Coastguard Worker return 0;
1406*9880d681SAndroid Build Coastguard Worker }
1407*9880d681SAndroid Build Coastguard Worker switch (Type) {
1408*9880d681SAndroid Build Coastguard Worker case SystemZII::CompareAndBranch:
1409*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
1410*9880d681SAndroid Build Coastguard Worker case SystemZ::CR:
1411*9880d681SAndroid Build Coastguard Worker return SystemZ::CRJ;
1412*9880d681SAndroid Build Coastguard Worker case SystemZ::CGR:
1413*9880d681SAndroid Build Coastguard Worker return SystemZ::CGRJ;
1414*9880d681SAndroid Build Coastguard Worker case SystemZ::CHI:
1415*9880d681SAndroid Build Coastguard Worker return SystemZ::CIJ;
1416*9880d681SAndroid Build Coastguard Worker case SystemZ::CGHI:
1417*9880d681SAndroid Build Coastguard Worker return SystemZ::CGIJ;
1418*9880d681SAndroid Build Coastguard Worker case SystemZ::CLR:
1419*9880d681SAndroid Build Coastguard Worker return SystemZ::CLRJ;
1420*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGR:
1421*9880d681SAndroid Build Coastguard Worker return SystemZ::CLGRJ;
1422*9880d681SAndroid Build Coastguard Worker case SystemZ::CLFI:
1423*9880d681SAndroid Build Coastguard Worker return SystemZ::CLIJ;
1424*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGFI:
1425*9880d681SAndroid Build Coastguard Worker return SystemZ::CLGIJ;
1426*9880d681SAndroid Build Coastguard Worker default:
1427*9880d681SAndroid Build Coastguard Worker return 0;
1428*9880d681SAndroid Build Coastguard Worker }
1429*9880d681SAndroid Build Coastguard Worker case SystemZII::CompareAndReturn:
1430*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
1431*9880d681SAndroid Build Coastguard Worker case SystemZ::CR:
1432*9880d681SAndroid Build Coastguard Worker return SystemZ::CRBReturn;
1433*9880d681SAndroid Build Coastguard Worker case SystemZ::CGR:
1434*9880d681SAndroid Build Coastguard Worker return SystemZ::CGRBReturn;
1435*9880d681SAndroid Build Coastguard Worker case SystemZ::CHI:
1436*9880d681SAndroid Build Coastguard Worker return SystemZ::CIBReturn;
1437*9880d681SAndroid Build Coastguard Worker case SystemZ::CGHI:
1438*9880d681SAndroid Build Coastguard Worker return SystemZ::CGIBReturn;
1439*9880d681SAndroid Build Coastguard Worker case SystemZ::CLR:
1440*9880d681SAndroid Build Coastguard Worker return SystemZ::CLRBReturn;
1441*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGR:
1442*9880d681SAndroid Build Coastguard Worker return SystemZ::CLGRBReturn;
1443*9880d681SAndroid Build Coastguard Worker case SystemZ::CLFI:
1444*9880d681SAndroid Build Coastguard Worker return SystemZ::CLIBReturn;
1445*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGFI:
1446*9880d681SAndroid Build Coastguard Worker return SystemZ::CLGIBReturn;
1447*9880d681SAndroid Build Coastguard Worker default:
1448*9880d681SAndroid Build Coastguard Worker return 0;
1449*9880d681SAndroid Build Coastguard Worker }
1450*9880d681SAndroid Build Coastguard Worker case SystemZII::CompareAndSibcall:
1451*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
1452*9880d681SAndroid Build Coastguard Worker case SystemZ::CR:
1453*9880d681SAndroid Build Coastguard Worker return SystemZ::CRBCall;
1454*9880d681SAndroid Build Coastguard Worker case SystemZ::CGR:
1455*9880d681SAndroid Build Coastguard Worker return SystemZ::CGRBCall;
1456*9880d681SAndroid Build Coastguard Worker case SystemZ::CHI:
1457*9880d681SAndroid Build Coastguard Worker return SystemZ::CIBCall;
1458*9880d681SAndroid Build Coastguard Worker case SystemZ::CGHI:
1459*9880d681SAndroid Build Coastguard Worker return SystemZ::CGIBCall;
1460*9880d681SAndroid Build Coastguard Worker case SystemZ::CLR:
1461*9880d681SAndroid Build Coastguard Worker return SystemZ::CLRBCall;
1462*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGR:
1463*9880d681SAndroid Build Coastguard Worker return SystemZ::CLGRBCall;
1464*9880d681SAndroid Build Coastguard Worker case SystemZ::CLFI:
1465*9880d681SAndroid Build Coastguard Worker return SystemZ::CLIBCall;
1466*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGFI:
1467*9880d681SAndroid Build Coastguard Worker return SystemZ::CLGIBCall;
1468*9880d681SAndroid Build Coastguard Worker default:
1469*9880d681SAndroid Build Coastguard Worker return 0;
1470*9880d681SAndroid Build Coastguard Worker }
1471*9880d681SAndroid Build Coastguard Worker case SystemZII::CompareAndTrap:
1472*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
1473*9880d681SAndroid Build Coastguard Worker case SystemZ::CR:
1474*9880d681SAndroid Build Coastguard Worker return SystemZ::CRT;
1475*9880d681SAndroid Build Coastguard Worker case SystemZ::CGR:
1476*9880d681SAndroid Build Coastguard Worker return SystemZ::CGRT;
1477*9880d681SAndroid Build Coastguard Worker case SystemZ::CHI:
1478*9880d681SAndroid Build Coastguard Worker return SystemZ::CIT;
1479*9880d681SAndroid Build Coastguard Worker case SystemZ::CGHI:
1480*9880d681SAndroid Build Coastguard Worker return SystemZ::CGIT;
1481*9880d681SAndroid Build Coastguard Worker case SystemZ::CLR:
1482*9880d681SAndroid Build Coastguard Worker return SystemZ::CLRT;
1483*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGR:
1484*9880d681SAndroid Build Coastguard Worker return SystemZ::CLGRT;
1485*9880d681SAndroid Build Coastguard Worker case SystemZ::CLFI:
1486*9880d681SAndroid Build Coastguard Worker return SystemZ::CLFIT;
1487*9880d681SAndroid Build Coastguard Worker case SystemZ::CLGFI:
1488*9880d681SAndroid Build Coastguard Worker return SystemZ::CLGIT;
1489*9880d681SAndroid Build Coastguard Worker default:
1490*9880d681SAndroid Build Coastguard Worker return 0;
1491*9880d681SAndroid Build Coastguard Worker }
1492*9880d681SAndroid Build Coastguard Worker }
1493*9880d681SAndroid Build Coastguard Worker return 0;
1494*9880d681SAndroid Build Coastguard Worker }
1495*9880d681SAndroid Build Coastguard Worker
loadImmediate(MachineBasicBlock & MBB,MachineBasicBlock::iterator MBBI,unsigned Reg,uint64_t Value) const1496*9880d681SAndroid Build Coastguard Worker void SystemZInstrInfo::loadImmediate(MachineBasicBlock &MBB,
1497*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::iterator MBBI,
1498*9880d681SAndroid Build Coastguard Worker unsigned Reg, uint64_t Value) const {
1499*9880d681SAndroid Build Coastguard Worker DebugLoc DL = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1500*9880d681SAndroid Build Coastguard Worker unsigned Opcode;
1501*9880d681SAndroid Build Coastguard Worker if (isInt<16>(Value))
1502*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::LGHI;
1503*9880d681SAndroid Build Coastguard Worker else if (SystemZ::isImmLL(Value))
1504*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::LLILL;
1505*9880d681SAndroid Build Coastguard Worker else if (SystemZ::isImmLH(Value)) {
1506*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::LLILH;
1507*9880d681SAndroid Build Coastguard Worker Value >>= 16;
1508*9880d681SAndroid Build Coastguard Worker } else {
1509*9880d681SAndroid Build Coastguard Worker assert(isInt<32>(Value) && "Huge values not handled yet");
1510*9880d681SAndroid Build Coastguard Worker Opcode = SystemZ::LGFI;
1511*9880d681SAndroid Build Coastguard Worker }
1512*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, MBBI, DL, get(Opcode), Reg).addImm(Value);
1513*9880d681SAndroid Build Coastguard Worker }
1514