Skip to content

Commit 48e9072

Browse files
committed
Merging r322319:
------------------------------------------------------------------------ r322319 | matze | 2018-01-11 14:30:43 -0800 (Thu, 11 Jan 2018) | 7 lines PeepholeOptimizer: Fix for vregs without defs The PeepholeOptimizer would fail for vregs without a definition. If this was caused by an undef operand abort to keep the code simple (so we don't need to add logic everywhere to replicate the undef flag). Differential Revision: https://reviews.llvm.org/D40763 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_60@329619 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent a7769cb commit 48e9072

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ class TargetInstrInfo : public MCInstrInfo {
421421
/// Build the equivalent inputs of a REG_SEQUENCE for the given \p MI
422422
/// and \p DefIdx.
423423
/// \p [out] InputRegs of the equivalent REG_SEQUENCE. Each element of
424-
/// the list is modeled as <Reg:SubReg, SubIdx>.
424+
/// the list is modeled as <Reg:SubReg, SubIdx>. Operands with the undef
425+
/// flag are not added to this list.
425426
/// E.g., REG_SEQUENCE %1:sub1, sub0, %2, sub1 would produce
426427
/// two elements:
427428
/// - %1:sub1, sub0
@@ -446,7 +447,8 @@ class TargetInstrInfo : public MCInstrInfo {
446447
/// - %1:sub1, sub0
447448
///
448449
/// \returns true if it is possible to build such an input sequence
449-
/// with the pair \p MI, \p DefIdx. False otherwise.
450+
/// with the pair \p MI, \p DefIdx and the operand has no undef flag set.
451+
/// False otherwise.
450452
///
451453
/// \pre MI.isExtractSubreg() or MI.isExtractSubregLike().
452454
///
@@ -465,7 +467,8 @@ class TargetInstrInfo : public MCInstrInfo {
465467
/// - InsertedReg: %1:sub1, sub3
466468
///
467469
/// \returns true if it is possible to build such an input sequence
468-
/// with the pair \p MI, \p DefIdx. False otherwise.
470+
/// with the pair \p MI, \p DefIdx and the operand has no undef flag set.
471+
/// False otherwise.
469472
///
470473
/// \pre MI.isInsertSubreg() or MI.isInsertSubregLike().
471474
///

lib/CodeGen/PeepholeOptimizer.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,8 @@ ValueTrackerResult ValueTracker::getNextSourceFromCopy() {
18821882
return ValueTrackerResult();
18831883
// Otherwise, we want the whole source.
18841884
const MachineOperand &Src = Def->getOperand(1);
1885+
if (Src.isUndef())
1886+
return ValueTrackerResult();
18851887
return ValueTrackerResult(Src.getReg(), Src.getSubReg());
18861888
}
18871889

@@ -1925,6 +1927,8 @@ ValueTrackerResult ValueTracker::getNextSourceFromBitcast() {
19251927
}
19261928

19271929
const MachineOperand &Src = Def->getOperand(SrcIdx);
1930+
if (Src.isUndef())
1931+
return ValueTrackerResult();
19281932
return ValueTrackerResult(Src.getReg(), Src.getSubReg());
19291933
}
19301934

@@ -2093,6 +2097,10 @@ ValueTrackerResult ValueTracker::getNextSourceFromPHI() {
20932097
for (unsigned i = 1, e = Def->getNumOperands(); i < e; i += 2) {
20942098
auto &MO = Def->getOperand(i);
20952099
assert(MO.isReg() && "Invalid PHI instruction");
2100+
// We have no code to deal with undef operands. They shouldn't happen in
2101+
// normal programs anyway.
2102+
if (MO.isUndef())
2103+
return ValueTrackerResult();
20962104
Res.addSource(MO.getReg(), MO.getSubReg());
20972105
}
20982106

@@ -2149,9 +2157,14 @@ ValueTrackerResult ValueTracker::getNextSource() {
21492157
// If we can still move up in the use-def chain, move to the next
21502158
// definition.
21512159
if (!TargetRegisterInfo::isPhysicalRegister(Reg) && OneRegSrc) {
2152-
Def = MRI.getVRegDef(Reg);
2153-
DefIdx = MRI.def_begin(Reg).getOperandNo();
2154-
DefSubReg = Res.getSrcSubReg(0);
2160+
MachineRegisterInfo::def_iterator DI = MRI.def_begin(Reg);
2161+
if (DI != MRI.def_end()) {
2162+
Def = DI->getParent();
2163+
DefIdx = DI.getOperandNo();
2164+
DefSubReg = Res.getSrcSubReg(0);
2165+
} else {
2166+
Def = nullptr;
2167+
}
21552168
return Res;
21562169
}
21572170
}

lib/CodeGen/TargetInstrInfo.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,8 @@ bool TargetInstrInfo::getRegSequenceInputs(
11511151
for (unsigned OpIdx = 1, EndOpIdx = MI.getNumOperands(); OpIdx != EndOpIdx;
11521152
OpIdx += 2) {
11531153
const MachineOperand &MOReg = MI.getOperand(OpIdx);
1154+
if (MOReg.isUndef())
1155+
continue;
11541156
const MachineOperand &MOSubIdx = MI.getOperand(OpIdx + 1);
11551157
assert(MOSubIdx.isImm() &&
11561158
"One of the subindex of the reg_sequence is not an immediate");
@@ -1174,6 +1176,8 @@ bool TargetInstrInfo::getExtractSubregInputs(
11741176
// Def = EXTRACT_SUBREG v0.sub1, sub0.
11751177
assert(DefIdx == 0 && "EXTRACT_SUBREG only has one def");
11761178
const MachineOperand &MOReg = MI.getOperand(1);
1179+
if (MOReg.isUndef())
1180+
return false;
11771181
const MachineOperand &MOSubIdx = MI.getOperand(2);
11781182
assert(MOSubIdx.isImm() &&
11791183
"The subindex of the extract_subreg is not an immediate");
@@ -1198,6 +1202,8 @@ bool TargetInstrInfo::getInsertSubregInputs(
11981202
assert(DefIdx == 0 && "INSERT_SUBREG only has one def");
11991203
const MachineOperand &MOBaseReg = MI.getOperand(1);
12001204
const MachineOperand &MOInsertedReg = MI.getOperand(2);
1205+
if (MOInsertedReg.isUndef())
1206+
return false;
12011207
const MachineOperand &MOSubIdx = MI.getOperand(3);
12021208
assert(MOSubIdx.isImm() &&
12031209
"One of the subindex of the reg_sequence is not an immediate");

lib/Target/ARM/ARMBaseInstrInfo.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4864,12 +4864,14 @@ bool ARMBaseInstrInfo::getRegSequenceLikeInputs(
48644864
// Populate the InputRegs accordingly.
48654865
// rY
48664866
const MachineOperand *MOReg = &MI.getOperand(1);
4867-
InputRegs.push_back(
4868-
RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_0));
4867+
if (!MOReg->isUndef())
4868+
InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
4869+
MOReg->getSubReg(), ARM::ssub_0));
48694870
// rZ
48704871
MOReg = &MI.getOperand(2);
4871-
InputRegs.push_back(
4872-
RegSubRegPairAndIdx(MOReg->getReg(), MOReg->getSubReg(), ARM::ssub_1));
4872+
if (!MOReg->isUndef())
4873+
InputRegs.push_back(RegSubRegPairAndIdx(MOReg->getReg(),
4874+
MOReg->getSubReg(), ARM::ssub_1));
48734875
return true;
48744876
}
48754877
llvm_unreachable("Target dependent opcode missing");
@@ -4888,6 +4890,8 @@ bool ARMBaseInstrInfo::getExtractSubregLikeInputs(
48884890
// rX = EXTRACT_SUBREG dZ, ssub_0
48894891
// rY = EXTRACT_SUBREG dZ, ssub_1
48904892
const MachineOperand &MOReg = MI.getOperand(2);
4893+
if (MOReg.isUndef())
4894+
return false;
48914895
InputReg.Reg = MOReg.getReg();
48924896
InputReg.SubReg = MOReg.getSubReg();
48934897
InputReg.SubIdx = DefIdx == 0 ? ARM::ssub_0 : ARM::ssub_1;
@@ -4907,6 +4911,8 @@ bool ARMBaseInstrInfo::getInsertSubregLikeInputs(
49074911
// dX = VSETLNi32 dY, rZ, imm
49084912
const MachineOperand &MOBaseReg = MI.getOperand(1);
49094913
const MachineOperand &MOInsertedReg = MI.getOperand(2);
4914+
if (MOInsertedReg.isUndef())
4915+
return false;
49104916
const MachineOperand &MOIndex = MI.getOperand(3);
49114917
BaseReg.Reg = MOBaseReg.getReg();
49124918
BaseReg.SubReg = MOBaseReg.getSubReg();

test/CodeGen/ARM/peephole-phi.mir

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,39 @@ body: |
6565
%4:gpr = PHI %0, %bb.1, %2, %bb.2
6666
%5:spr = VMOVSR %4, 14, %noreg
6767
...
68+
69+
# The current implementation doesn't perform any transformations if undef
70+
# operands are involved.
71+
# CHECK-LABEL: name: func-undefops
72+
# CHECK: body: |
73+
# CHECK: bb.0:
74+
# CHECK: Bcc %bb.2, 1, undef %cpsr
75+
#
76+
# CHECK: bb.1:
77+
# CHECK: %0:gpr = VMOVRS undef %1:spr, 14, %noreg
78+
# CHECK: B %bb.3
79+
#
80+
# CHECK: bb.2:
81+
# CHECK: %2:gpr = VMOVRS undef %3:spr, 14, %noreg
82+
#
83+
# CHECK: bb.3:
84+
# CHECK: %4:gpr = PHI %0, %bb.1, %2, %bb.2
85+
# CHECK: %5:spr = VMOVSR %4, 14, %noreg
86+
---
87+
name: func-undefops
88+
tracksRegLiveness: true
89+
body: |
90+
bb.0:
91+
Bcc %bb.2, 1, undef %cpsr
92+
93+
bb.1:
94+
%0:gpr = VMOVRS undef %1:spr, 14, %noreg
95+
B %bb.3
96+
97+
bb.2:
98+
%2:gpr = VMOVRS undef %3:spr, 14, %noreg
99+
100+
bb.3:
101+
%4:gpr = PHI %0, %bb.1, %2, %bb.2
102+
%5:spr = VMOVSR %4, 14, %noreg
103+
...

0 commit comments

Comments
 (0)