Skip to content

Commit f7ba2bd

Browse files
[LLVM][SLSR] Add a debug counter (llvm#119981)
Added debug counter and test for SLSR. Fixes: llvm#119770
1 parent e072cff commit f7ba2bd

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#include "llvm/InitializePasses.h"
7979
#include "llvm/Pass.h"
8080
#include "llvm/Support/Casting.h"
81+
#include "llvm/Support/DebugCounter.h"
8182
#include "llvm/Support/ErrorHandling.h"
8283
#include "llvm/Transforms/Scalar.h"
8384
#include "llvm/Transforms/Utils/Local.h"
@@ -93,6 +94,9 @@ using namespace PatternMatch;
9394
static const unsigned UnknownAddressSpace =
9495
std::numeric_limits<unsigned>::max();
9596

97+
DEBUG_COUNTER(StraightLineStrengthReduceCounter, "slsr-counter",
98+
"Controls whether rewriteCandidateWithBasis is executed.");
99+
96100
namespace {
97101

98102
class StraightLineStrengthReduceLegacyPass : public FunctionPass {
@@ -268,8 +272,8 @@ FunctionPass *llvm::createStraightLineStrengthReducePass() {
268272
bool StraightLineStrengthReduce::isBasisFor(const Candidate &Basis,
269273
const Candidate &C) {
270274
return (Basis.Ins != C.Ins && // skip the same instruction
271-
// They must have the same type too. Basis.Base == C.Base doesn't
272-
// guarantee their types are the same (PR23975).
275+
// They must have the same type too. Basis.Base == C.Base
276+
// doesn't guarantee their types are the same (PR23975).
273277
Basis.Ins->getType() == C.Ins->getType() &&
274278
// Basis must dominate C in order to rewrite C with respect to Basis.
275279
DT->dominates(Basis.Ins->getParent(), C.Ins->getParent()) &&
@@ -610,6 +614,9 @@ Value *StraightLineStrengthReduce::emitBump(const Candidate &Basis,
610614

611615
void StraightLineStrengthReduce::rewriteCandidateWithBasis(
612616
const Candidate &C, const Candidate &Basis) {
617+
if (!DebugCounter::shouldExecute(StraightLineStrengthReduceCounter))
618+
return;
619+
613620
assert(C.CandidateKind == Basis.CandidateKind && C.Base == Basis.Base &&
614621
C.Stride == Basis.Stride);
615622
// We run rewriteCandidateWithBasis on all candidates in a post-order, so the

llvm/test/Other/debugcounter-slsr.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; REQUIRES: asserts
2+
; RUN: opt -passes=slsr -S -debug-counter=slsr-counter=1 < %s | FileCheck %s
3+
4+
; Test that, with debug counters on, we will skip the first slsr opportunity.
5+
6+
define void @stride_is_2s(i32 %b, i32 %s) {
7+
; CHECK-LABEL: @stride_is_2s(
8+
; CHECK-NEXT: %s2 = shl i32 %s, 1
9+
; CHECK-NEXT: %t1 = add i32 %b, %s2
10+
; CHECK-NEXT: call void @foo(i32 %t1)
11+
; CHECK-NEXT: %s4 = shl i32 %s, 2
12+
; CHECK-NEXT: %t2 = add i32 %b, %s4
13+
; CHECK-NEXT: call void @foo(i32 %t2)
14+
; CHECK-NEXT: ret void
15+
;
16+
%s2 = shl i32 %s, 1
17+
%t1 = add i32 %b, %s2
18+
call void @foo(i32 %t1)
19+
%s4 = shl i32 %s, 2
20+
%t2 = add i32 %b, %s4
21+
call void @foo(i32 %t2)
22+
ret void
23+
}
24+
25+
declare void @foo(i32)
26+

0 commit comments

Comments
 (0)