Skip to content

Commit 8d2ccd1

Browse files
committed
Reland: [TII] Use optional destination and source pair as a return value; NFC
Refactor usage of isCopyInstrImpl, isCopyInstr and isAddImmediate methods to return optional machine operand pair of destination and source registers. Patch by Nikola Prica Differential Revision: https://reviews.llvm.org/D69622
1 parent 5a1bac4 commit 8d2ccd1

13 files changed

+104
-131
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

+28-25
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ template <class T> class SmallVectorImpl;
6464

6565
using ParamLoadedValue = std::pair<MachineOperand, DIExpression*>;
6666

67+
struct DestSourcePair {
68+
const MachineOperand *Destination;
69+
const MachineOperand *Source;
70+
71+
DestSourcePair(const MachineOperand &Dest, const MachineOperand &Src)
72+
: Destination(&Dest), Source(&Src) {}
73+
};
74+
6775
//---------------------------------------------------------------------------
6876
///
6977
/// TargetInstrInfo - Interface to description of machine instruction set
@@ -918,41 +926,36 @@ class TargetInstrInfo : public MCInstrInfo {
918926
}
919927

920928
protected:
921-
/// Target-dependent implemenation for IsCopyInstr.
929+
/// Target-dependent implementation for IsCopyInstr.
922930
/// If the specific machine instruction is a instruction that moves/copies
923-
/// value from one register to another register return true along with
924-
/// @Source machine operand and @Destination machine operand.
925-
virtual bool isCopyInstrImpl(const MachineInstr &MI,
926-
const MachineOperand *&Source,
927-
const MachineOperand *&Destination) const {
928-
return false;
931+
/// value from one register to another register return destination and source
932+
/// registers as machine operands.
933+
virtual Optional<DestSourcePair>
934+
isCopyInstrImpl(const MachineInstr &MI) const {
935+
return None;
929936
}
930937

931938
public:
932939
/// If the specific machine instruction is a instruction that moves/copies
933-
/// value from one register to another register return true along with
934-
/// @Source machine operand and @Destination machine operand.
935-
/// For COPY-instruction the method naturally returns true, for all other
936-
/// instructions the method calls target-dependent implementation.
937-
bool isCopyInstr(const MachineInstr &MI, const MachineOperand *&Source,
938-
const MachineOperand *&Destination) const {
940+
/// value from one register to another register return destination and source
941+
/// registers as machine operands.
942+
/// For COPY-instruction the method naturally returns destination and source
943+
/// registers as machine operands, for all other instructions the method calls
944+
/// target-dependent implementation.
945+
Optional<DestSourcePair> isCopyInstr(const MachineInstr &MI) const {
939946
if (MI.isCopy()) {
940-
Destination = &MI.getOperand(0);
941-
Source = &MI.getOperand(1);
942-
return true;
947+
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
943948
}
944-
return isCopyInstrImpl(MI, Source, Destination);
949+
return isCopyInstrImpl(MI);
945950
}
946951

947952
/// If the specific machine instruction is an instruction that adds an
948-
/// immediate value to its \c Source operand and stores it in \c Destination,
949-
/// return true along with \c Destination and \c Source machine operand to
950-
/// which \c Offset has been added.
951-
virtual bool isAddImmediate(const MachineInstr &MI,
952-
const MachineOperand *&Destination,
953-
const MachineOperand *&Source,
954-
int64_t &Offset) const {
955-
return false;
953+
/// immediate value to its source operand and stores it in destination,
954+
/// return destination and source registers as machine operands along with
955+
/// \c Offset which has been added.
956+
virtual Optional<DestSourcePair> isAddImmediate(const MachineInstr &MI,
957+
int64_t &Offset) const {
958+
return None;
956959
}
957960

958961
/// Store the specified register of the given register class to the specified

llvm/lib/CodeGen/LiveDebugValues.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -997,10 +997,14 @@ void LiveDebugValues::transferRegisterCopy(MachineInstr &MI,
997997
OpenRangesSet &OpenRanges,
998998
VarLocMap &VarLocIDs,
999999
TransferMap &Transfers) {
1000-
const MachineOperand *SrcRegOp, *DestRegOp;
10011000

1002-
if (!TII->isCopyInstr(MI, SrcRegOp, DestRegOp) || !SrcRegOp->isKill() ||
1003-
!DestRegOp->isDef())
1001+
auto DestSrc = TII->isCopyInstr(MI);
1002+
if (!DestSrc)
1003+
return;
1004+
1005+
const MachineOperand *DestRegOp = DestSrc->Destination;
1006+
const MachineOperand *SrcRegOp = DestSrc->Source;
1007+
if (!SrcRegOp->isKill() || !DestRegOp->isDef())
10041008
return;
10051009

10061010
auto isCalleeSavedReg = [&](unsigned Reg) {

llvm/lib/CodeGen/TargetInstrInfo.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -1124,14 +1124,13 @@ Optional<ParamLoadedValue>
11241124
TargetInstrInfo::describeLoadedValue(const MachineInstr &MI) const {
11251125
const MachineFunction *MF = MI.getMF();
11261126
DIExpression *Expr = DIExpression::get(MF->getFunction().getContext(), {});
1127-
const MachineOperand *SrcRegOp, *DestRegOp;
11281127
int64_t Offset;
11291128

1130-
if (isCopyInstr(MI, SrcRegOp, DestRegOp)) {
1131-
return ParamLoadedValue(*SrcRegOp, Expr);
1132-
} else if (isAddImmediate(MI, DestRegOp, SrcRegOp, Offset)) {
1129+
if (auto DestSrc = isCopyInstr(MI)) {
1130+
return ParamLoadedValue(*DestSrc->Source, Expr);
1131+
} else if (auto DestSrc = isAddImmediate(MI, Offset)) {
11331132
Expr = DIExpression::prepend(Expr, DIExpression::ApplyOffset, Offset);
1134-
return ParamLoadedValue(*SrcRegOp, Expr);
1133+
return ParamLoadedValue(*DestSrc->Source, Expr);
11351134
}
11361135

11371136
return None;

llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

+11-19
Original file line numberDiff line numberDiff line change
@@ -5702,39 +5702,33 @@ bool AArch64InstrInfo::shouldOutlineFromFunctionByDefault(
57025702
return MF.getFunction().hasMinSize();
57035703
}
57045704

5705-
bool AArch64InstrInfo::isCopyInstrImpl(
5706-
const MachineInstr &MI, const MachineOperand *&Source,
5707-
const MachineOperand *&Destination) const {
5705+
Optional<DestSourcePair>
5706+
AArch64InstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
57085707

57095708
// AArch64::ORRWrs and AArch64::ORRXrs with WZR/XZR reg
57105709
// and zero immediate operands used as an alias for mov instruction.
57115710
if (MI.getOpcode() == AArch64::ORRWrs &&
57125711
MI.getOperand(1).getReg() == AArch64::WZR &&
57135712
MI.getOperand(3).getImm() == 0x0) {
5714-
Destination = &MI.getOperand(0);
5715-
Source = &MI.getOperand(2);
5716-
return true;
5713+
return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
57175714
}
57185715

57195716
if (MI.getOpcode() == AArch64::ORRXrs &&
57205717
MI.getOperand(1).getReg() == AArch64::XZR &&
57215718
MI.getOperand(3).getImm() == 0x0) {
5722-
Destination = &MI.getOperand(0);
5723-
Source = &MI.getOperand(2);
5724-
return true;
5719+
return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
57255720
}
57265721

5727-
return false;
5722+
return None;
57285723
}
57295724

5730-
bool AArch64InstrInfo::isAddImmediate(const MachineInstr &MI,
5731-
const MachineOperand *&Destination,
5732-
const MachineOperand *&Source,
5733-
int64_t &Offset) const {
5725+
Optional<DestSourcePair>
5726+
AArch64InstrInfo::isAddImmediate(const MachineInstr &MI,
5727+
int64_t &Offset) const {
57345728
int Sign = 1;
57355729
switch (MI.getOpcode()) {
57365730
default:
5737-
return false;
5731+
return None;
57385732
case AArch64::SUBWri:
57395733
case AArch64::SUBXri:
57405734
case AArch64::SUBSWri:
@@ -5748,16 +5742,14 @@ bool AArch64InstrInfo::isAddImmediate(const MachineInstr &MI,
57485742
// TODO: Third operand can be global address (usually some string).
57495743
if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg() ||
57505744
!MI.getOperand(2).isImm())
5751-
return false;
5752-
Source = &MI.getOperand(1);
5745+
return None;
57535746
Offset = MI.getOperand(2).getImm() * Sign;
57545747
int Shift = MI.getOperand(3).getImm();
57555748
assert((Shift == 0 || Shift == 12) && "Shift can be either 0 or 12");
57565749
Offset = Offset << Shift;
57575750
}
57585751
}
5759-
Destination = &MI.getOperand(0);
5760-
return true;
5752+
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
57615753
}
57625754

57635755
Optional<ParamLoadedValue>

llvm/lib/Target/AArch64/AArch64InstrInfo.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,8 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
265265
/// on Windows.
266266
static bool isSEHInstruction(const MachineInstr &MI);
267267

268-
bool isAddImmediate(const MachineInstr &MI,
269-
const MachineOperand *&Destination,
270-
const MachineOperand *&Source,
271-
int64_t &Offset) const override;
268+
Optional<DestSourcePair> isAddImmediate(const MachineInstr &MI,
269+
int64_t &Offset) const override;
272270

273271
Optional<ParamLoadedValue>
274272
describeLoadedValue(const MachineInstr &MI) const override;
@@ -277,11 +275,11 @@ class AArch64InstrInfo final : public AArch64GenInstrInfo {
277275
#include "AArch64GenInstrInfo.inc"
278276

279277
protected:
280-
/// If the specific machine instruction is a instruction that moves/copies
281-
/// value from one register to another register return true along with
282-
/// @Source machine operand and @Destination machine operand.
283-
bool isCopyInstrImpl(const MachineInstr &MI, const MachineOperand *&Source,
284-
const MachineOperand *&Destination) const override;
278+
/// If the specific machine instruction is an instruction that moves/copies
279+
/// value from one register to another register return destination and source
280+
/// registers as machine operands.
281+
Optional<DestSourcePair>
282+
isCopyInstrImpl(const MachineInstr &MI) const override;
285283

286284
private:
287285
/// Sets the offsets on outlined instructions in \p MBB which use SP

llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp

+10-16
Original file line numberDiff line numberDiff line change
@@ -993,9 +993,8 @@ void ARMBaseInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
993993
Mov->addRegisterKilled(SrcReg, TRI);
994994
}
995995

996-
bool ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr &MI,
997-
const MachineOperand *&Src,
998-
const MachineOperand *&Dest) const {
996+
Optional<DestSourcePair>
997+
ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
999998
// VMOVRRD is also a copy instruction but it requires
1000999
// special way of handling. It is more complex copy version
10011000
// and since that we are not considering it. For recognition
@@ -1006,10 +1005,8 @@ bool ARMBaseInstrInfo::isCopyInstrImpl(const MachineInstr &MI,
10061005
if (!MI.isMoveReg() ||
10071006
(MI.getOpcode() == ARM::VORRq &&
10081007
MI.getOperand(1).getReg() != MI.getOperand(2).getReg()))
1009-
return false;
1010-
Dest = &MI.getOperand(0);
1011-
Src = &MI.getOperand(1);
1012-
return true;
1008+
return None;
1009+
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
10131010
}
10141011

10151012
const MachineInstrBuilder &
@@ -5350,30 +5347,27 @@ ARMBaseInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
53505347
return makeArrayRef(TargetFlags);
53515348
}
53525349

5353-
bool ARMBaseInstrInfo::isAddImmediate(const MachineInstr &MI,
5354-
const MachineOperand *&Destination,
5355-
const MachineOperand *&Source,
5356-
int64_t &Offset) const {
5350+
Optional<DestSourcePair>
5351+
ARMBaseInstrInfo::isAddImmediate(const MachineInstr &MI,
5352+
int64_t &Offset) const {
53575353
int Sign = 1;
53585354
unsigned Opcode = MI.getOpcode();
53595355

53605356
// We describe SUBri or ADDri instructions.
53615357
if (Opcode == ARM::SUBri)
53625358
Sign = -1;
53635359
else if (Opcode != ARM::ADDri)
5364-
return false;
5360+
return None;
53655361

53665362
// TODO: Third operand can be global address (usually some string). Since
53675363
// strings can be relocated we cannot calculate their offsets for
53685364
// now.
53695365
if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg() ||
53705366
!MI.getOperand(2).isImm())
5371-
return false;
5367+
return None;
53725368

5373-
Destination = &MI.getOperand(0);
5374-
Source = &MI.getOperand(1);
53755369
Offset = MI.getOperand(2).getImm() * Sign;
5376-
return true;
5370+
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
53775371
}
53785372

53795373
bool llvm::registerDefinedBetween(unsigned Reg,

llvm/lib/Target/ARM/ARMBaseInstrInfo.h

+7-10
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,11 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
9999
MachineInstr *commuteInstructionImpl(MachineInstr &MI, bool NewMI,
100100
unsigned OpIdx1,
101101
unsigned OpIdx2) const override;
102-
103-
/// If the specific machine instruction is a instruction that moves/copies
104-
/// value from one register to another register return true along with
105-
/// @Source machine operand and @Destination machine operand.
106-
bool isCopyInstrImpl(const MachineInstr &MI, const MachineOperand *&Source,
107-
const MachineOperand *&Destination) const override;
102+
/// If the specific machine instruction is an instruction that moves/copies
103+
/// value from one register to another register return destination and source
104+
/// registers as machine operands.
105+
Optional<DestSourcePair>
106+
isCopyInstrImpl(const MachineInstr &MI) const override;
108107

109108
public:
110109
// Return whether the target has an explicit NOP encoding.
@@ -456,10 +455,8 @@ class ARMBaseInstrInfo : public ARMGenInstrInfo {
456455
return MI.getOperand(3).getReg();
457456
}
458457

459-
bool isAddImmediate(const MachineInstr &MI,
460-
const MachineOperand *&Destination,
461-
const MachineOperand *&Source,
462-
int64_t &Offset) const override;
458+
Optional<DestSourcePair> isAddImmediate(const MachineInstr &MI,
459+
int64_t &Offset) const override;
463460
};
464461

465462
/// Get the operands corresponding to the given \p Pred value. By default, the

llvm/lib/Target/Mips/Mips16InstrInfo.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,11 @@ void Mips16InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
9696
MIB.addReg(SrcReg, getKillRegState(KillSrc));
9797
}
9898

99-
bool Mips16InstrInfo::isCopyInstrImpl(const MachineInstr &MI,
100-
const MachineOperand *&Src,
101-
const MachineOperand *&Dest) const {
102-
if (MI.isMoveReg()) {
103-
Dest = &MI.getOperand(0);
104-
Src = &MI.getOperand(1);
105-
return true;
106-
}
107-
return false;
99+
Optional<DestSourcePair>
100+
Mips16InstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
101+
if (MI.isMoveReg())
102+
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
103+
return None;
108104
}
109105

110106
void Mips16InstrInfo::storeRegToStack(MachineBasicBlock &MBB,

llvm/lib/Target/Mips/Mips16InstrInfo.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ class Mips16InstrInfo : public MipsInstrInfo {
104104

105105
protected:
106106
/// If the specific machine instruction is a instruction that moves/copies
107-
/// value from one register to another register return true along with
108-
/// @Source machine operand and @Destination machine operand.
109-
bool isCopyInstrImpl(const MachineInstr &MI, const MachineOperand *&Source,
110-
const MachineOperand *&Destination) const override;
107+
/// value from one register to another register return destination and source
108+
/// registers as machine operands.
109+
Optional<DestSourcePair> isCopyInstrImpl(const MachineInstr &MI) const override;
111110

112111
private:
113112
unsigned getAnalyzableBrOpc(unsigned Opc) const override;

llvm/lib/Target/Mips/MipsSEInstrInfo.cpp

+9-14
Original file line numberDiff line numberDiff line change
@@ -221,29 +221,24 @@ static bool isReadOrWriteToDSPReg(const MachineInstr &MI, bool &isWrite) {
221221
/// We check for the common case of 'or', as it's MIPS' preferred instruction
222222
/// for GPRs but we have to check the operands to ensure that is the case.
223223
/// Other move instructions for MIPS are directly identifiable.
224-
bool MipsSEInstrInfo::isCopyInstrImpl(const MachineInstr &MI,
225-
const MachineOperand *&Src,
226-
const MachineOperand *&Dest) const {
224+
Optional<DestSourcePair>
225+
MipsSEInstrInfo::isCopyInstrImpl(const MachineInstr &MI) const {
227226
bool isDSPControlWrite = false;
228227
// Condition is made to match the creation of WRDSP/RDDSP copy instruction
229228
// from copyPhysReg function.
230229
if (isReadOrWriteToDSPReg(MI, isDSPControlWrite)) {
231-
if (!MI.getOperand(1).isImm() || MI.getOperand(1).getImm() != (1<<4))
232-
return false;
230+
if (!MI.getOperand(1).isImm() || MI.getOperand(1).getImm() != (1 << 4))
231+
return None;
233232
else if (isDSPControlWrite) {
234-
Src = &MI.getOperand(0);
235-
Dest = &MI.getOperand(2);
233+
return DestSourcePair{MI.getOperand(2), MI.getOperand(0)};
234+
236235
} else {
237-
Dest = &MI.getOperand(0);
238-
Src = &MI.getOperand(2);
236+
return DestSourcePair{MI.getOperand(0), MI.getOperand(2)};
239237
}
240-
return true;
241238
} else if (MI.isMoveReg() || isORCopyInst(MI)) {
242-
Dest = &MI.getOperand(0);
243-
Src = &MI.getOperand(1);
244-
return true;
239+
return DestSourcePair{MI.getOperand(0), MI.getOperand(1)};
245240
}
246-
return false;
241+
return None;
247242
}
248243

249244
void MipsSEInstrInfo::

llvm/lib/Target/Mips/MipsSEInstrInfo.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ class MipsSEInstrInfo : public MipsInstrInfo {
7777

7878
protected:
7979
/// If the specific machine instruction is a instruction that moves/copies
80-
/// value from one register to another register return true along with
81-
/// @Source machine operand and @Destination machine operand.
82-
bool isCopyInstrImpl(const MachineInstr &MI, const MachineOperand *&Source,
83-
const MachineOperand *&Destination) const override;
80+
/// value from one register to another register return destination and source
81+
/// registers as machine operands.
82+
Optional<DestSourcePair>
83+
isCopyInstrImpl(const MachineInstr &MI) const override;
8484

8585
private:
8686
unsigned getAnalyzableBrOpc(unsigned Opc) const override;

0 commit comments

Comments
 (0)