Skip to content

[NFCI] [ValueTracking] Add pass to print ConstantRanges #140144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion llvm/include/llvm/Analysis/ValueTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/FMF.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/PassManager.h"
#include <cassert>
#include <cstdint>

Expand Down Expand Up @@ -1001,6 +1002,19 @@ std::optional<bool> isImpliedByDomCondition(CmpPredicate Pred, const Value *LHS,
void findValuesAffectedByCondition(Value *Cond, bool IsAssume,
function_ref<void(Value *)> InsertAffected);

/// Printer pass for \p computeConstantRange results.
class ConstantRangePrinterPass
: public PassInfoMixin<ConstantRangePrinterPass> {
raw_ostream &OS;

public:
explicit ConstantRangePrinterPass(raw_ostream &OS) : OS(OS) {}

PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);

static bool isRequired() { return true; }
};

} // end namespace llvm

#endif // LLVM_ANALYSIS_VALUETRACKING_H
17 changes: 17 additions & 0 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "llvm/IR/GlobalAlias.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
Expand Down Expand Up @@ -10451,3 +10452,19 @@ void llvm::findValuesAffectedByCondition(
}
}
}

PreservedAnalyses ConstantRangePrinterPass::run(Function &F,
FunctionAnalysisManager &AM) {
// For compatibility with opt's -analyze feature under legacy pass manager
// which was not ported to NPM. This keeps tests using
// update_analyze_test_checks.py working.
OS << "Printing analysis 'ConstantRange' for function '" << F.getName()
<< "':\n";
for (const Instruction &I : instructions(F)) {
if (!I.getType()->isIntOrIntVectorTy())
continue;
auto CR = computeConstantRange(&I, false);
OS << I << ": " << CR << "\n";
}
return PreservedAnalyses::all();
}
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
#include "llvm/Analysis/UniformityAnalysis.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/CodeGen/AssignmentTrackingAnalysis.h"
#include "llvm/CodeGen/AtomicExpand.h"
#include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/Passes/PassRegistry.def
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ FUNCTION_PASS("print<access-info>", LoopAccessInfoPrinterPass(errs()))
FUNCTION_PASS("print<assumptions>", AssumptionPrinterPass(errs()))
FUNCTION_PASS("print<block-freq>", BlockFrequencyPrinterPass(errs()))
FUNCTION_PASS("print<branch-prob>", BranchProbabilityPrinterPass(errs()))
FUNCTION_PASS("print<constant-range>", ConstantRangePrinterPass(errs()))
FUNCTION_PASS("print<cost-model>", CostModelPrinterPass(errs()))
FUNCTION_PASS("print<cycles>", CycleInfoPrinterPass(errs()))
FUNCTION_PASS("print<da>", DependenceAnalysisPrinterPass(errs()))
Expand Down
234 changes: 234 additions & 0 deletions llvm/test/Analysis/ValueTracking/print-constant-range.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 5
; RUN: opt -disable-output '-passes=print<constant-range>' < %s -S 2>&1 | FileCheck %s

define i1 @shl_C_X_ugt(i8 %x) {
; CHECK-LABEL: 'shl_C_X_ugt'
; CHECK-NEXT: %shl = shl i8 7, %x: [1,-31)
; CHECK-NEXT: %r = icmp ugt i8 %shl, -32: full-set
;
%shl = shl i8 7, %x
%r = icmp ugt i8 %shl, 224
ret i1 %r
}

define i1 @shl_C_X_ugt2(i8 %x) {
; CHECK-LABEL: 'shl_C_X_ugt2'
; CHECK-NEXT: %shl = shl i8 5, %x: [1,-63)
; CHECK-NEXT: %r = icmp ugt i8 %shl, -64: full-set
;
%shl = shl i8 5, %x
%r = icmp ugt i8 %shl, 192
ret i1 %r
}

define i1 @shl_C_X_ugt_fail(i8 %x) {
; CHECK-LABEL: 'shl_C_X_ugt_fail'
; CHECK-NEXT: %shl = shl i8 1, %x: [1,-127)
; CHECK-NEXT: %r = icmp ugt i8 %shl, 127: full-set
;
%shl = shl i8 1, %x
%r = icmp ugt i8 %shl, 127
ret i1 %r
}

define i1 @shl_C_X_ugt_fail2(i8 %x) {
; CHECK-LABEL: 'shl_C_X_ugt_fail2'
; CHECK-NEXT: %shl = shl i8 3, %x: [1,-63)
; CHECK-NEXT: %r = icmp ugt i8 %shl, -66: full-set
;
%shl = shl i8 3, %x
%r = icmp ugt i8 %shl, 190
ret i1 %r
}

define i1 @shl_C_X_ugt_fail3(i8 %x) {
; CHECK-LABEL: 'shl_C_X_ugt_fail3'
; CHECK-NEXT: %shl = shl i8 -1, %x: [1,0)
; CHECK-NEXT: %r = icmp ugt i8 %shl, -2: full-set
;
%shl = shl i8 -1, %x
%r = icmp ugt i8 %shl, 254
ret i1 %r
}

define i1 @shl_C_X_ugt_todo(i8 %x) {
; CHECK-LABEL: 'shl_C_X_ugt_todo'
; CHECK-NEXT: %shl = shl i8 -127, %x: [1,-63)
; CHECK-NEXT: %r = icmp ugt i8 %shl, -116: full-set
;
%shl = shl i8 129, %x
%r = icmp ugt i8 %shl, 140
ret i1 %r
}

define i1 @shl_X_C_ugt(i8 %x) {
; CHECK-LABEL: 'shl_X_C_ugt'
; CHECK-NEXT: %shl = shl i8 %x, 6: [0,-63)
; CHECK-NEXT: %r = icmp ugt i8 %shl, -64: full-set
;
%shl = shl i8 %x, 6
%r = icmp ugt i8 %shl, 192
ret i1 %r
}

define i1 @shl_X_C_ugt_fail(i8 %x) {
; CHECK-LABEL: 'shl_X_C_ugt_fail'
; CHECK-NEXT: %shl = shl i8 %x, 6: [0,-63)
; CHECK-NEXT: %r = icmp ugt i8 %shl, -65: full-set
;
%shl = shl i8 %x, 6
%r = icmp ugt i8 %shl, 191
ret i1 %r
}

define i1 @shl_X_C_ugt_fail2(i8 %x) {
; CHECK-LABEL: 'shl_X_C_ugt_fail2'
; CHECK-NEXT: %shl = shl i8 %x, 5: [0,-31)
; CHECK-NEXT: %r = icmp ugt i8 %shl, -64: full-set
;
%shl = shl i8 %x, 5
%r = icmp ugt i8 %shl, 192
ret i1 %r
}

define i1 @and_ugt(i8 %xx) {
; CHECK-LABEL: 'and_ugt'
; CHECK-NEXT: %x = mul i8 %xx, %xx: full-set
; CHECK-NEXT: %negx = sub i8 0, %x: full-set
; CHECK-NEXT: %x_p2 = and i8 %negx, %x: [0,-127)
; CHECK-NEXT: %r = icmp ugt i8 %x_p2, -128: full-set
;
%x = mul i8 %xx, %xx ; thwart complexity-based canonicalization
%negx = sub i8 0, %x
%x_p2 = and i8 %negx, %x
%r = icmp ugt i8 %x_p2, 128
ret i1 %r
}

define i1 @and_ugt2(i8 %xx) {
; CHECK-LABEL: 'and_ugt2'
; CHECK-NEXT: %x = mul i8 %xx, %xx: full-set
; CHECK-NEXT: %negx = sub i8 0, %x: full-set
; CHECK-NEXT: %x_p2 = and i8 %x, %negx: [0,-127)
; CHECK-NEXT: %r = icmp ugt i8 %x_p2, -128: full-set
;
%x = mul i8 %xx, %xx ; thwart complexity-based canonicalization
%negx = sub i8 0, %x
%x_p2 = and i8 %x, %negx
%r = icmp ugt i8 %x_p2, 128
ret i1 %r
}

define i1 @and_ugt_fail(i8 %xx) {
; CHECK-LABEL: 'and_ugt_fail'
; CHECK-NEXT: %x = mul i8 %xx, %xx: full-set
; CHECK-NEXT: %negx = sub i8 0, %x: full-set
; CHECK-NEXT: %x_p2 = and i8 %x, %negx: [0,-127)
; CHECK-NEXT: %r = icmp ugt i8 %x_p2, 127: full-set
;
%x = mul i8 %xx, %xx ; thwart complexity-based canonicalization
%negx = sub i8 0, %x
%x_p2 = and i8 %x, %negx
%r = icmp ugt i8 %x_p2, 127
ret i1 %r
}

define i1 @urem_okay(i8 %x) {
; CHECK-LABEL: 'urem_okay'
; CHECK-NEXT: %val = urem i8 34, %x: [0,35)
; CHECK-NEXT: %r = icmp ule i8 %val, 35: full-set
;
%val = urem i8 34, %x
%r = icmp ule i8 %val, 35
ret i1 %r
}

define i1 @urem_fail(i8 %x) {
; CHECK-LABEL: 'urem_fail'
; CHECK-NEXT: %val = urem i8 34, %x: [0,35)
; CHECK-NEXT: %r = icmp ule i8 %val, 33: full-set
;
%val = urem i8 34, %x
%r = icmp ule i8 %val, 33
ret i1 %r
}

define i1 @srem_posC_okay0(i8 %x) {
; CHECK-LABEL: 'srem_posC_okay0'
; CHECK-NEXT: %val = srem i8 34, %x: [0,35)
; CHECK-NEXT: %r = icmp sle i8 %val, 34: full-set
;
%val = srem i8 34, %x
%r = icmp sle i8 %val, 34
ret i1 %r
}

define i1 @srem_posC_okay1(i8 %x) {
; CHECK-LABEL: 'srem_posC_okay1'
; CHECK-NEXT: %val = srem i8 34, %x: [0,35)
; CHECK-NEXT: %r = icmp sge i8 %val, -3: full-set
;
%val = srem i8 34, %x
%r = icmp sge i8 %val, -3
ret i1 %r
}

define i1 @srem_negC_okay0(i8 %x) {
; CHECK-LABEL: 'srem_negC_okay0'
; CHECK-NEXT: %val = srem i8 -34, %x: [-34,1)
; CHECK-NEXT: %r = icmp sle i8 %val, 0: full-set
;
%val = srem i8 -34, %x
%r = icmp sle i8 %val, 0
ret i1 %r
}

define i1 @srem_negC_okay1(i8 %x) {
; CHECK-LABEL: 'srem_negC_okay1'
; CHECK-NEXT: %val = srem i8 -34, %x: [-34,1)
; CHECK-NEXT: %r = icmp sge i8 %val, -34: full-set
;
%val = srem i8 -34, %x
%r = icmp sge i8 %val, -34
ret i1 %r
}

define i1 @srem_posC_fail0(i8 %x) {
; CHECK-LABEL: 'srem_posC_fail0'
; CHECK-NEXT: %val = srem i8 34, %x: [0,35)
; CHECK-NEXT: %r = icmp sle i8 %val, 32: full-set
;
%val = srem i8 34, %x
%r = icmp sle i8 %val, 32
ret i1 %r
}

define i1 @srem_posC_fail1(i8 %x) {
; CHECK-LABEL: 'srem_posC_fail1'
; CHECK-NEXT: %val = srem i8 34, %x: [0,35)
; CHECK-NEXT: %r = icmp sge i8 %val, 1: full-set
;
%val = srem i8 34, %x
%r = icmp sge i8 %val, 1
ret i1 %r
}

define i1 @srem_negC_fail0(i8 %x) {
; CHECK-LABEL: 'srem_negC_fail0'
; CHECK-NEXT: %val = srem i8 -34, %x: [-34,1)
; CHECK-NEXT: %r = icmp sle i8 %val, -1: full-set
;
%val = srem i8 -34, %x
%r = icmp sle i8 %val, -1
ret i1 %r
}

define i1 @srem_negC_fail1(i8 %x) {
; CHECK-LABEL: 'srem_negC_fail1'
; CHECK-NEXT: %val = srem i8 -34, %x: [-34,1)
; CHECK-NEXT: %r = icmp sge i8 %val, -33: full-set
;
%val = srem i8 -34, %x
%r = icmp sge i8 %val, -33
ret i1 %r
}
1 change: 1 addition & 0 deletions llvm/utils/UpdateTestChecks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"Dependence Analysis",
"Loop Access Analysis",
"Scalar Evolution Analysis",
"ConstantRange",
}


Expand Down