diff --git a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp index 75585fcc80266..7d017095c88ce 100644 --- a/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/StraightLineStrengthReduce.cpp @@ -78,6 +78,7 @@ #include "llvm/InitializePasses.h" #include "llvm/Pass.h" #include "llvm/Support/Casting.h" +#include "llvm/Support/DebugCounter.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Utils/Local.h" @@ -93,6 +94,9 @@ using namespace PatternMatch; static const unsigned UnknownAddressSpace = std::numeric_limits::max(); +DEBUG_COUNTER(StraightLineStrengthReduceCounter, "slsr-counter", + "Controls whether rewriteCandidateWithBasis is executed."); + namespace { class StraightLineStrengthReduceLegacyPass : public FunctionPass { @@ -268,8 +272,8 @@ FunctionPass *llvm::createStraightLineStrengthReducePass() { bool StraightLineStrengthReduce::isBasisFor(const Candidate &Basis, const Candidate &C) { return (Basis.Ins != C.Ins && // skip the same instruction - // They must have the same type too. Basis.Base == C.Base doesn't - // guarantee their types are the same (PR23975). + // They must have the same type too. Basis.Base == C.Base + // doesn't guarantee their types are the same (PR23975). Basis.Ins->getType() == C.Ins->getType() && // Basis must dominate C in order to rewrite C with respect to Basis. DT->dominates(Basis.Ins->getParent(), C.Ins->getParent()) && @@ -610,6 +614,9 @@ Value *StraightLineStrengthReduce::emitBump(const Candidate &Basis, void StraightLineStrengthReduce::rewriteCandidateWithBasis( const Candidate &C, const Candidate &Basis) { + if (!DebugCounter::shouldExecute(StraightLineStrengthReduceCounter)) + return; + assert(C.CandidateKind == Basis.CandidateKind && C.Base == Basis.Base && C.Stride == Basis.Stride); // We run rewriteCandidateWithBasis on all candidates in a post-order, so the diff --git a/llvm/test/Other/debugcounter-slsr.ll b/llvm/test/Other/debugcounter-slsr.ll new file mode 100644 index 0000000000000..a9ca45222a5cc --- /dev/null +++ b/llvm/test/Other/debugcounter-slsr.ll @@ -0,0 +1,26 @@ +; REQUIRES: asserts +; RUN: opt -passes=slsr -S -debug-counter=slsr-counter=1 < %s | FileCheck %s + +; Test that, with debug counters on, we will skip the first slsr opportunity. + +define void @stride_is_2s(i32 %b, i32 %s) { +; CHECK-LABEL: @stride_is_2s( +; CHECK-NEXT: %s2 = shl i32 %s, 1 +; CHECK-NEXT: %t1 = add i32 %b, %s2 +; CHECK-NEXT: call void @foo(i32 %t1) +; CHECK-NEXT: %s4 = shl i32 %s, 2 +; CHECK-NEXT: %t2 = add i32 %b, %s4 +; CHECK-NEXT: call void @foo(i32 %t2) +; CHECK-NEXT: ret void +; + %s2 = shl i32 %s, 1 + %t1 = add i32 %b, %s2 + call void @foo(i32 %t1) + %s4 = shl i32 %s, 2 + %t2 = add i32 %b, %s4 + call void @foo(i32 %t2) + ret void +} + +declare void @foo(i32) +