Skip to content

Commit f9da56f

Browse files
committed
Add changes under a flag, disabled by default.
1 parent 60306d5 commit f9da56f

File tree

2 files changed

+52
-11
lines changed

2 files changed

+52
-11
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
5959
INITIALIZE_PASS_END(LazyValueInfoWrapperPass, "lazy-value-info",
6060
"Lazy Value Information Analysis", false, true)
6161

62+
static cl::opt<bool> PerPredRanges(
63+
"lvi-per-pred-ranges", cl::Hidden, cl::init(false),
64+
cl::desc("Enable tracking of ranges for a value in a block for"
65+
"each block predecessor (default = false)"));
66+
6267
namespace llvm {
6368
FunctionPass *createLazyValueInfoPass() {
6469
return new LazyValueInfoWrapperPass();
@@ -246,7 +251,8 @@ void LazyValueInfoCache::eraseValue(Value *V) {
246251
Pair.second->OverDefined.erase(V);
247252
if (Pair.second->NonNullPointers)
248253
Pair.second->NonNullPointers->erase(V);
249-
Pair.second->PredecessorLatticeElements.erase(V);
254+
if (PerPredRanges)
255+
Pair.second->PredecessorLatticeElements.erase(V);
250256
}
251257

252258
auto HandleIt = ValueHandles.find_as(V);
@@ -262,9 +268,9 @@ void LVIValueHandle::deleted() {
262268

263269
void LazyValueInfoCache::eraseBlock(BasicBlock *BB) {
264270
// Clear all when a BB is removed.
265-
for (auto &Pair : BlockCache) {
266-
Pair.second->PredecessorLatticeElements.clear();
267-
}
271+
if (PerPredRanges)
272+
for (auto &Pair : BlockCache)
273+
Pair.second->PredecessorLatticeElements.clear();
268274
BlockCache.erase(BB);
269275
}
270276

@@ -746,11 +752,12 @@ LazyValueInfoImpl::solveBlockValueNonLocal(Value *Val, BasicBlock *BB) {
746752
<< Pred->getName() << "' (non local).\n");
747753
return Result;
748754
}
749-
750-
PredLatticeElements.insert({Pred, *EdgeResult});
755+
if (PerPredRanges)
756+
PredLatticeElements.insert({Pred, *EdgeResult});
751757
}
752758

753-
TheCache.insertPredecessorResults(Val, BB, PredLatticeElements);
759+
if (PerPredRanges)
760+
TheCache.insertPredecessorResults(Val, BB, PredLatticeElements);
754761

755762
// Return the merged value, which is more precise than 'overdefined'.
756763
assert(!Result.isOverdefined());
@@ -788,10 +795,12 @@ LazyValueInfoImpl::solveBlockValuePHINode(PHINode *PN, BasicBlock *BB) {
788795
return Result;
789796
}
790797

791-
PredLatticeElements.insert({PhiBB, *EdgeResult});
798+
if (PerPredRanges)
799+
PredLatticeElements.insert({PhiBB, *EdgeResult});
792800
}
793801

794-
TheCache.insertPredecessorResults(PN, BB, PredLatticeElements);
802+
if (PerPredRanges)
803+
TheCache.insertPredecessorResults(PN, BB, PredLatticeElements);
795804

796805
// Return the merged value, which is more precise than 'overdefined'.
797806
assert(!Result.isOverdefined() && "Possible PHI in entry block?");
@@ -1051,6 +1060,9 @@ LazyValueInfoImpl::solveBlockValueBinaryOpImpl(
10511060
std::optional<ValueLatticeElement> MergedResult =
10521061
ValueLatticeElement::getRange(OpFn(LHSRange, RHSRange));
10531062

1063+
if (!PerPredRanges)
1064+
return MergedResult;
1065+
10541066
std::optional<BBLatticeElementMap> PredLHS =
10551067
TheCache.getCachedPredecessorInfo(LHS, BB);
10561068
if (!PredLHS)

llvm/test/Transforms/CorrelatedValuePropagation/track-predecessor-ranges.ll

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt < %s -passes="correlated-propagation" -S 2>&1 | FileCheck %s
2+
; RUN: opt < %s -passes="correlated-propagation" -S 2>&1 | FileCheck %s
3+
; RUN: opt < %s -passes="correlated-propagation" -lvi-per-pred-ranges -S 2>&1 | FileCheck %s -check-prefix=LVI-PRED-RANGES
34

45
@global = external local_unnamed_addr global [4338 x i32], align 16
56

@@ -17,7 +18,8 @@ define dso_local noundef zeroext i1 @bar(i64 noundef %arg, ptr noundef writeonly
1718
; CHECK-NEXT: [[PHI5:%.*]] = phi i64 [ 3, %[[BB]] ], [ 7, %[[BB2]] ]
1819
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw i64 [[PHI]], [[ARG]]
1920
; CHECK-NEXT: [[LSHR:%.*]] = lshr i64 [[ADD]], [[PHI5]]
20-
; CHECK-NEXT: br i1 true, label %[[BB8:.*]], label %[[BB7:.*]]
21+
; CHECK-NEXT: [[ICMP6:%.*]] = icmp samesign ult i64 [[LSHR]], 4338
22+
; CHECK-NEXT: br i1 [[ICMP6]], label %[[BB8:.*]], label %[[BB7:.*]]
2123
; CHECK: [[BB7]]:
2224
; CHECK-NEXT: tail call void @llvm.ubsantrap(i8 18)
2325
; CHECK-NEXT: unreachable
@@ -31,6 +33,33 @@ define dso_local noundef zeroext i1 @bar(i64 noundef %arg, ptr noundef writeonly
3133
; CHECK-NEXT: [[PHI10:%.*]] = phi i1 [ true, %[[BB8]] ], [ false, %[[BB2]] ]
3234
; CHECK-NEXT: ret i1 [[PHI10]]
3335
;
36+
; LVI-PRED-RANGES-LABEL: define dso_local noundef zeroext i1 @bar(
37+
; LVI-PRED-RANGES-SAME: i64 noundef [[ARG:%.*]], ptr noundef writeonly captures(none) [[ARG1:%.*]]) local_unnamed_addr {
38+
; LVI-PRED-RANGES-NEXT: [[BB:.*]]:
39+
; LVI-PRED-RANGES-NEXT: [[ICMP:%.*]] = icmp ult i64 [[ARG]], 1025
40+
; LVI-PRED-RANGES-NEXT: br i1 [[ICMP]], label %[[BB4:.*]], label %[[BB2:.*]]
41+
; LVI-PRED-RANGES: [[BB2]]:
42+
; LVI-PRED-RANGES-NEXT: [[ICMP3:%.*]] = icmp ult i64 [[ARG]], 262145
43+
; LVI-PRED-RANGES-NEXT: br i1 [[ICMP3]], label %[[BB4]], label %[[BB9:.*]]
44+
; LVI-PRED-RANGES: [[BB4]]:
45+
; LVI-PRED-RANGES-NEXT: [[PHI:%.*]] = phi i64 [ 7, %[[BB]] ], [ 15487, %[[BB2]] ]
46+
; LVI-PRED-RANGES-NEXT: [[PHI5:%.*]] = phi i64 [ 3, %[[BB]] ], [ 7, %[[BB2]] ]
47+
; LVI-PRED-RANGES-NEXT: [[ADD:%.*]] = add nuw nsw i64 [[PHI]], [[ARG]]
48+
; LVI-PRED-RANGES-NEXT: [[LSHR:%.*]] = lshr i64 [[ADD]], [[PHI5]]
49+
; LVI-PRED-RANGES-NEXT: br i1 true, label %[[BB8:.*]], label %[[BB7:.*]]
50+
; LVI-PRED-RANGES: [[BB7]]:
51+
; LVI-PRED-RANGES-NEXT: tail call void @llvm.ubsantrap(i8 18)
52+
; LVI-PRED-RANGES-NEXT: unreachable
53+
; LVI-PRED-RANGES: [[BB8]]:
54+
; LVI-PRED-RANGES-NEXT: [[GETELEMENTPTR:%.*]] = getelementptr inbounds nuw [4338 x i32], ptr @global, i64 0, i64 [[LSHR]]
55+
; LVI-PRED-RANGES-NEXT: [[LOAD:%.*]] = load i32, ptr [[GETELEMENTPTR]], align 4
56+
; LVI-PRED-RANGES-NEXT: [[SEXT:%.*]] = sext i32 [[LOAD]] to i64
57+
; LVI-PRED-RANGES-NEXT: store i64 [[SEXT]], ptr [[ARG1]], align 8
58+
; LVI-PRED-RANGES-NEXT: br label %[[BB9]]
59+
; LVI-PRED-RANGES: [[BB9]]:
60+
; LVI-PRED-RANGES-NEXT: [[PHI10:%.*]] = phi i1 [ true, %[[BB8]] ], [ false, %[[BB2]] ]
61+
; LVI-PRED-RANGES-NEXT: ret i1 [[PHI10]]
62+
;
3463
bb:
3564
%icmp = icmp ult i64 %arg, 1025
3665
br i1 %icmp, label %bb4, label %bb2

0 commit comments

Comments
 (0)