Skip to content

Commit 5618e9b

Browse files
committed
[RDA][ARM] collectKilledOperands across multiple blocks
Use MIOperand in collectLocalKilledOperands to make the search global, as we already have to search for global uses too. This allows us to delete more dead code when tail predicating. Differential Revision: https://reviews.llvm.org/D75167
1 parent caf5a4d commit 5618e9b

File tree

7 files changed

+48
-69
lines changed

7 files changed

+48
-69
lines changed

llvm/include/llvm/CodeGen/ReachingDefAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class ReachingDefAnalysis : public MachineFunctionPass {
176176

177177
/// Assuming MI is dead, recursively search the incoming operands which are
178178
/// killed by MI and collect those that would become dead.
179-
void collectLocalKilledOperands(MachineInstr *MI, InstSet &Dead) const;
179+
void collectKilledOperands(MachineInstr *MI, InstSet &Dead) const;
180180

181181
/// Return whether removing this instruction will have no effect on the
182182
/// program, returning the redundant use-def chain.

llvm/lib/CodeGen/ReachingDefAnalysis.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ bool isValidRegUseOf(const MachineOperand &MO, int PhysReg) {
3333
return isValidRegUse(MO) && MO.getReg() == PhysReg;
3434
}
3535

36-
bool isKilledRegUse(const MachineOperand &MO) {
37-
return isValidRegUse(MO) && MO.isKill();
38-
}
39-
4036
bool isValidRegDef(const MachineOperand &MO) {
4137
return isValidReg(MO) && MO.isDef();
4238
}
@@ -353,7 +349,7 @@ MachineInstr *ReachingDefAnalysis::getUniqueReachingMIDef(MachineInstr *MI,
353349
int PhysReg) const {
354350
// If there's a local def before MI, return it.
355351
MachineInstr *LocalDef = getReachingLocalMIDef(MI, PhysReg);
356-
if (InstIds.lookup(LocalDef) < InstIds.lookup(MI))
352+
if (LocalDef && InstIds.lookup(LocalDef) < InstIds.lookup(MI))
357353
return LocalDef;
358354

359355
SmallPtrSet<MachineBasicBlock*, 4> VisitedBBs;
@@ -544,8 +540,8 @@ ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI, InstSet &Visited,
544540
return true;
545541
}
546542

547-
void ReachingDefAnalysis::collectLocalKilledOperands(MachineInstr *MI,
548-
InstSet &Dead) const {
543+
void ReachingDefAnalysis::collectKilledOperands(MachineInstr *MI,
544+
InstSet &Dead) const {
549545
Dead.insert(MI);
550546
auto IsDead = [this, &Dead](MachineInstr *Def, int PhysReg) {
551547
unsigned LiveDefs = 0;
@@ -568,11 +564,11 @@ void ReachingDefAnalysis::collectLocalKilledOperands(MachineInstr *MI,
568564
};
569565

570566
for (auto &MO : MI->operands()) {
571-
if (!isKilledRegUse(MO))
567+
if (!isValidRegUse(MO))
572568
continue;
573-
if (MachineInstr *Def = getReachingLocalMIDef(MI, MO.getReg()))
569+
if (MachineInstr *Def = getMIOperand(MI, MO))
574570
if (IsDead(Def, MO.getReg()))
575-
collectLocalKilledOperands(Def, Dead);
571+
collectKilledOperands(Def, Dead);
576572
}
577573
}
578574

llvm/lib/Target/ARM/ARMLowOverheadLoops.cpp

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -900,40 +900,53 @@ void ARMLowOverheadLoops::IterationCountDCE(LowOverheadLoop &LoLoop) {
900900
if (!LoLoop.IsTailPredicationLegal())
901901
return;
902902

903+
LLVM_DEBUG(dbgs() << "ARM Loops: Trying DCE on loop iteration count.\n");
904+
903905
MachineInstr *Def = RDA->getMIOperand(LoLoop.Start, 0);
904-
if (!Def)
906+
if (!Def) {
907+
LLVM_DEBUG(dbgs() << "ARM Loops: Couldn't find iteration count.\n");
908+
return;
909+
}
910+
911+
// Collect and remove the users of iteration count.
912+
SmallPtrSet<MachineInstr*, 4> Killed = { LoLoop.Start, LoLoop.Dec,
913+
LoLoop.End, LoLoop.InsertPt };
914+
SmallPtrSet<MachineInstr*, 2> Remove;
915+
if (RDA->isSafeToRemove(Def, Remove, Killed))
916+
LoLoop.ToRemove.insert(Remove.begin(), Remove.end());
917+
else {
918+
LLVM_DEBUG(dbgs() << "ARM Loops: Unsafe to remove loop iteration count.\n");
905919
return;
920+
}
906921

907-
// Collect IT blocks.
922+
// Collect the dead code and the MBBs in which they reside.
923+
RDA->collectKilledOperands(Def, Killed);
924+
SmallPtrSet<MachineBasicBlock*, 2> BasicBlocks;
925+
for (auto *MI : Killed)
926+
BasicBlocks.insert(MI->getParent());
927+
928+
// Collect IT blocks in all affected basic blocks.
908929
std::map<MachineInstr *, SmallPtrSet<MachineInstr *, 2>> ITBlocks;
909-
std::map<MachineInstr *, MachineInstr *> Predicates;
910-
MachineInstr *IT = nullptr;
911-
for (auto &MI : *Def->getParent()) {
912-
if (MI.getOpcode() == ARM::t2IT)
913-
IT = &MI;
914-
else if (TII->getPredicate(MI) != ARMCC::AL) {
915-
ITBlocks[IT].insert(&MI);
916-
Predicates[&MI] = IT;
930+
for (auto *MBB : BasicBlocks) {
931+
for (auto &MI : *MBB) {
932+
if (MI.getOpcode() != ARM::t2IT)
933+
continue;
934+
RDA->getReachingLocalUses(&MI, ARM::ITSTATE, ITBlocks[&MI]);
917935
}
918936
}
919937

920938
// If we're removing all of the instructions within an IT block, then
921939
// also remove the IT instruction.
922940
SmallPtrSet<MachineInstr*, 2> ModifiedITs;
923-
SmallPtrSet<MachineInstr*, 2> DeadITs;
924-
SmallPtrSet<MachineInstr*, 4> Killed;
925-
RDA->collectLocalKilledOperands(Def, Killed);
926941
for (auto *MI : Killed) {
927-
if (!Predicates.count(MI))
928-
continue;
929-
930-
MachineInstr *IT = Predicates[MI];
931-
auto &CurrentBlock = ITBlocks[IT];
932-
CurrentBlock.erase(MI);
933-
ModifiedITs.insert(IT);
934-
if (CurrentBlock.empty()) {
935-
DeadITs.insert(IT);
936-
ModifiedITs.erase(IT);
942+
if (MachineOperand *MO = MI->findRegisterUseOperand(ARM::ITSTATE)) {
943+
MachineInstr *IT = RDA->getMIOperand(MI, *MO);
944+
auto &CurrentBlock = ITBlocks[IT];
945+
CurrentBlock.erase(MI);
946+
if (CurrentBlock.empty())
947+
ModifiedITs.erase(IT);
948+
else
949+
ModifiedITs.insert(IT);
937950
}
938951
}
939952

@@ -945,14 +958,8 @@ void ARMLowOverheadLoops::IterationCountDCE(LowOverheadLoop &LoLoop) {
945958
for (auto *MI : Killed)
946959
dbgs() << " - " << *MI);
947960
LoLoop.ToRemove.insert(Killed.begin(), Killed.end());
948-
}
949-
950-
// Collect and remove the users of iteration count.
951-
SmallPtrSet<MachineInstr*, 4> Ignore = { LoLoop.Start, LoLoop.Dec,
952-
LoLoop.End, LoLoop.InsertPt };
953-
SmallPtrSet<MachineInstr*, 2> Remove;
954-
if (RDA->isSafeToRemove(Def, Remove, Ignore))
955-
LoLoop.ToRemove.insert(Remove.begin(), Remove.end());
961+
} else
962+
LLVM_DEBUG(dbgs() << "ARM Loops: Would need to modify IT block(s).\n");
956963
}
957964

958965
MachineInstr* ARMLowOverheadLoops::ExpandLoopStart(LowOverheadLoop &LoLoop) {
@@ -1076,8 +1083,8 @@ void ARMLowOverheadLoops::Expand(LowOverheadLoop &LoLoop) {
10761083
MIB.add(End->getOperand(0));
10771084
MIB.add(End->getOperand(1));
10781085
LLVM_DEBUG(dbgs() << "ARM Loops: Inserted LE: " << *MIB);
1079-
LoLoop.Dec->eraseFromParent();
1080-
End->eraseFromParent();
1086+
LoLoop.ToRemove.insert(LoLoop.Dec);
1087+
LoLoop.ToRemove.insert(End);
10811088
return &*MIB;
10821089
};
10831090

llvm/test/CodeGen/Thumb2/LowOverheadLoops/it-block-itercount.mir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ body: |
104104
; CHECK: frame-setup CFI_INSTRUCTION offset $lr, -4
105105
; CHECK: frame-setup CFI_INSTRUCTION offset $r7, -8
106106
; CHECK: renamable $r3, dead $cpsr = tLSLri killed renamable $r2, 1, 14 /* CC::al */, $noreg
107-
; CHECK: dead renamable $r12 = t2MOVi 4, 14 /* CC::al */, $noreg, $noreg
108-
; CHECK: tCMPi8 renamable $r3, 4, 14 /* CC::al */, $noreg, implicit-def dead $cpsr
109107
; CHECK: renamable $r2 = tLEApcrel %const.0, 14 /* CC::al */, $noreg
110108
; CHECK: renamable $q0 = MVE_VLDRWU32 killed renamable $r2, 0, 0, $noreg :: (load 16 from constant-pool)
111109
; CHECK: $lr = MVE_DLSTP_32 killed renamable $r3

llvm/test/CodeGen/Thumb2/LowOverheadLoops/it-block-mov.mir

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ body: |
4545
; CHECK: frame-setup CFI_INSTRUCTION def_cfa_offset 8
4646
; CHECK: frame-setup CFI_INSTRUCTION offset $lr, -4
4747
; CHECK: frame-setup CFI_INSTRUCTION offset $r4, -8
48-
; CHECK: tCMPi8 renamable $r1, 4, 14 /* CC::al */, $noreg, implicit-def $cpsr
49-
; CHECK: renamable $r3 = t2MOVi 4, 14 /* CC::al */, $noreg, $noreg
50-
; CHECK: t2IT 11, 8, implicit-def $itstate
51-
; CHECK: dead $r3 = tMOVr renamable $r1, 11 /* CC::lt */, killed $cpsr, implicit killed renamable $r3, implicit killed $itstate
5248
; CHECK: tCMPi8 renamable $r1, 2, 14 /* CC::al */, $noreg, implicit-def $cpsr
53-
; CHECK: renamable $r12 = t2MOVi 4, 14 /* CC::al */, $noreg, $noreg
5449
; CHECK: tBcc %bb.2, 2 /* CC::hs */, killed $cpsr
5550
; CHECK: bb.1:
5651
; CHECK: liveins: $r2
@@ -59,15 +54,8 @@ body: |
5954
; CHECK: tPOP_RET 14 /* CC::al */, $noreg, def $r4, def $pc
6055
; CHECK: bb.2:
6156
; CHECK: successors: %bb.3(0x80000000)
62-
; CHECK: liveins: $r0, $r1, $r2, $r12
63-
; CHECK: renamable $r4, dead $cpsr = tMOVi8 1, 14 /* CC::al */, $noreg
64-
; CHECK: tCMPi8 renamable $r1, 4, 14 /* CC::al */, $noreg, implicit-def $cpsr
65-
; CHECK: t2IT 11, 8, implicit-def $itstate
66-
; CHECK: $r12 = tMOVr renamable $r1, 11 /* CC::lt */, killed $cpsr, implicit killed renamable $r12, implicit killed $itstate
67-
; CHECK: renamable $r3 = t2SUBrr renamable $r1, killed renamable $r12, 14 /* CC::al */, $noreg, $noreg
68-
; CHECK: renamable $r3, dead $cpsr = tADDi8 killed renamable $r3, 3, 14 /* CC::al */, $noreg
57+
; CHECK: liveins: $r0, $r1, $r2
6958
; CHECK: $r12 = tMOVr $r1, 14 /* CC::al */, $noreg
70-
; CHECK: dead renamable $r4 = nuw nsw t2ADDrs killed renamable $r4, killed renamable $r3, 19, 14 /* CC::al */, $noreg, $noreg
7159
; CHECK: renamable $q0 = MVE_VMOVimmi32 0, 0, $noreg, undef renamable $q0
7260
; CHECK: $r3 = tMOVr $r0, 14 /* CC::al */, $noreg
7361
; CHECK: $lr = MVE_DLSTP_32 killed renamable $r12

llvm/test/CodeGen/Thumb2/LowOverheadLoops/multiple-do-loops.mir

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ body: |
563563
; CHECK: early-clobber $sp = frame-setup t2STR_PRE killed $r8, $sp, -4, 14 /* CC::al */, $noreg
564564
; CHECK: frame-setup CFI_INSTRUCTION offset $r8, -24
565565
; CHECK: renamable $r6, dead $cpsr = tMOVi8 0, 14 /* CC::al */, $noreg
566-
; CHECK: dead renamable $r12 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
567566
; CHECK: t2CMPrs killed renamable $r6, renamable $r3, 11, 14 /* CC::al */, $noreg, implicit-def $cpsr
568567
; CHECK: tBcc %bb.3, 0 /* CC::eq */, killed $cpsr
569568
; CHECK: bb.1.vector.ph:
@@ -801,7 +800,6 @@ body: |
801800
; CHECK: successors: %bb.6(0x30000000), %bb.4(0x50000000)
802801
; CHECK: liveins: $r0, $r1, $r2, $r3
803802
; CHECK: renamable $r6, dead $cpsr = tMOVi8 0, 14 /* CC::al */, $noreg
804-
; CHECK: dead renamable $r8 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
805803
; CHECK: t2CMPrs killed renamable $r6, renamable $r3, 11, 14 /* CC::al */, $noreg, implicit-def $cpsr
806804
; CHECK: tBcc %bb.6, 0 /* CC::eq */, killed $cpsr
807805
; CHECK: bb.4.vector.ph66:

llvm/test/CodeGen/Thumb2/mve-postinc-lsr.ll

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -576,15 +576,7 @@ define i32 @arm_nn_mat_mul_core_4x_s8(i32 %row_elements, i32 %offset, i8* %row_b
576576
; CHECK: @ %bb.0: @ %entry
577577
; CHECK-NEXT: .save {r4, r5, r6, r7, r8, r10, lr}
578578
; CHECK-NEXT: push.w {r4, r5, r6, r7, r8, r10, lr}
579-
; CHECK-NEXT: add.w r7, r0, #15
580579
; CHECK-NEXT: ldr.w r12, [sp, #32]
581-
; CHECK-NEXT: mov.w lr, #1
582-
; CHECK-NEXT: asrs r6, r7, #31
583-
; CHECK-NEXT: add.w r4, r7, r6, lsr #28
584-
; CHECK-NEXT: asrs r5, r4, #4
585-
; CHECK-NEXT: cmp r5, #1
586-
; CHECK-NEXT: it gt
587-
; CHECK-NEXT: asrgt.w lr, r4, #4
588580
; CHECK-NEXT: cmp r0, #1
589581
; CHECK-NEXT: blt .LBB4_3
590582
; CHECK-NEXT: @ %bb.1: @ %for.body.preheader

0 commit comments

Comments
 (0)