Skip to content

Commit e0333cf

Browse files
committed
check canReplaceReg
1 parent 13a26c2 commit e0333cf

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,8 @@ class CombinerHelper {
996996
bool matchSuboCarryOut(const MachineInstr &MI, BuildFnTy &MatchInfo) const;
997997

998998
// (sext_inreg (sext_inreg x, K0), K1)
999-
void applyRedundantSextInReg(MachineInstr &Root, MachineInstr &Other) const;
999+
bool matchRedundantSextInReg(MachineInstr &Root, MachineInstr &Other,
1000+
BuildFnTy &MatchInfo) const;
10001001

10011002
private:
10021003
/// Checks for legality of an indexed variant of \p LdSt.

llvm/include/llvm/Target/GlobalISel/Combine.td

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,10 +1868,11 @@ def anyext_of_zext : ext_of_ext_opcodes<G_ANYEXT, G_ZEXT>;
18681868
def anyext_of_sext : ext_of_ext_opcodes<G_ANYEXT, G_SEXT>;
18691869

18701870
def sext_inreg_of_sext_inreg : GICombineRule<
1871-
(defs root:$dst),
1871+
(defs root:$dst, build_fn_matchinfo:$matchinfo),
18721872
(match (G_SEXT_INREG $x, $src, $a):$other,
1873-
(G_SEXT_INREG $dst, $x, $b):$root),
1874-
(apply [{ Helper.applyRedundantSextInReg(*${root}, *${other}); }])>;
1873+
(G_SEXT_INREG $dst, $x, $b):$root,
1874+
[{ return Helper.matchRedundantSextInReg(*${root}, *${other}, ${matchinfo}); }]),
1875+
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
18751876

18761877
// Push cast through build vector.
18771878
class buildvector_of_opcode<Instruction castOpcode> : GICombineRule <

llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,9 @@ bool CombinerHelper::matchCastOfInteger(const MachineInstr &CastMI,
379379
}
380380
}
381381

382-
void CombinerHelper::applyRedundantSextInReg(MachineInstr &Root,
383-
MachineInstr &Other) const {
382+
bool CombinerHelper::matchRedundantSextInReg(MachineInstr &Root,
383+
MachineInstr &Other,
384+
BuildFnTy &MatchInfo) const {
384385
assert(Root.getOpcode() == TargetOpcode::G_SEXT_INREG &&
385386
Other.getOpcode() == TargetOpcode::G_SEXT_INREG);
386387

@@ -394,14 +395,19 @@ void CombinerHelper::applyRedundantSextInReg(MachineInstr &Root,
394395
if (RootWidth >= OtherWidth) {
395396
// The root sext_inreg is entirely redundant because the other one
396397
// is narrower.
397-
Observer.changingAllUsesOfReg(MRI, Dst);
398-
MRI.replaceRegWith(Dst, OtherDst);
399-
Observer.finishedChangingAllUsesOfReg();
398+
if (!canReplaceReg(Dst, OtherDst, MRI))
399+
return false;
400+
401+
MatchInfo = [=](MachineIRBuilder &B) {
402+
Observer.changingAllUsesOfReg(MRI, Dst);
403+
MRI.replaceRegWith(Dst, OtherDst);
404+
Observer.finishedChangingAllUsesOfReg();
405+
};
400406
} else {
401407
// RootWidth < OtherWidth, rewrite this G_SEXT_INREG with the source of the
402408
// other G_SEXT_INREG.
403-
Builder.buildSExtInReg(Dst, Src, RootWidth);
409+
MatchInfo = [=](MachineIRBuilder &B) {
410+
B.buildSExtInReg(Dst, Src, RootWidth);
411+
};
404412
}
405-
406-
Root.eraseFromParent();
407413
}

0 commit comments

Comments
 (0)