Skip to content

Commit c949f2c

Browse files
committed
check canReplaceReg
1 parent 3f3c679 commit c949f2c

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
@@ -995,7 +995,8 @@ class CombinerHelper {
995995
bool matchSuboCarryOut(const MachineInstr &MI, BuildFnTy &MatchInfo) const;
996996

997997
// (sext_inreg (sext_inreg x, K0), K1)
998-
void applyRedundantSextInReg(MachineInstr &Root, MachineInstr &Other) const;
998+
bool matchRedundantSextInReg(MachineInstr &Root, MachineInstr &Other,
999+
BuildFnTy &MatchInfo) const;
9991000

10001001
private:
10011002
/// 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
@@ -1850,10 +1850,11 @@ def anyext_of_zext : ext_of_ext_opcodes<G_ANYEXT, G_ZEXT>;
18501850
def anyext_of_sext : ext_of_ext_opcodes<G_ANYEXT, G_SEXT>;
18511851

18521852
def sext_inreg_of_sext_inreg : GICombineRule<
1853-
(defs root:$dst),
1853+
(defs root:$dst, build_fn_matchinfo:$matchinfo),
18541854
(match (G_SEXT_INREG $x, $src, $a):$other,
1855-
(G_SEXT_INREG $dst, $x, $b):$root),
1856-
(apply [{ Helper.applyRedundantSextInReg(*${root}, *${other}); }])>;
1855+
(G_SEXT_INREG $dst, $x, $b):$root,
1856+
[{ return Helper.matchRedundantSextInReg(*${root}, *${other}, ${matchinfo}); }]),
1857+
(apply [{ Helper.applyBuildFn(*${root}, ${matchinfo}); }])>;
18571858

18581859
// Push cast through build vector.
18591860
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)