Skip to content

Commit 202d590

Browse files
committed
[LVI] Consistently intersect assumes
Integrate intersection with assumes into getBlockValue(), to ensure that it is consistently performed. We were doing it in nearly all places, but for example missed it for select inputs.
1 parent f9f865d commit 202d590

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,8 @@ class LazyValueInfoImpl {
395395
/// if it exists in the module.
396396
Function *GuardDecl;
397397

398-
Optional<ValueLatticeElement> getBlockValue(Value *Val, BasicBlock *BB);
398+
Optional<ValueLatticeElement> getBlockValue(Value *Val, BasicBlock *BB,
399+
Instruction *CxtI);
399400
Optional<ValueLatticeElement> getEdgeValue(Value *V, BasicBlock *F,
400401
BasicBlock *T, Instruction *CxtI = nullptr);
401402

@@ -533,15 +534,17 @@ void LazyValueInfoImpl::solve() {
533534
}
534535
}
535536

536-
Optional<ValueLatticeElement> LazyValueInfoImpl::getBlockValue(Value *Val,
537-
BasicBlock *BB) {
537+
Optional<ValueLatticeElement> LazyValueInfoImpl::getBlockValue(
538+
Value *Val, BasicBlock *BB, Instruction *CxtI) {
538539
// If already a constant, there is nothing to compute.
539540
if (Constant *VC = dyn_cast<Constant>(Val))
540541
return ValueLatticeElement::get(VC);
541542

542543
if (Optional<ValueLatticeElement> OptLatticeVal =
543-
TheCache.getCachedValueInfo(Val, BB))
544+
TheCache.getCachedValueInfo(Val, BB)) {
545+
intersectAssumeOrGuardBlockValueConstantRange(Val, *OptLatticeVal, CxtI);
544546
return OptLatticeVal;
547+
}
545548

546549
// We have hit a cycle, assume overdefined.
547550
if (!pushBlockValue({ BB, Val }))
@@ -796,13 +799,13 @@ Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueSelect(
796799
SelectInst *SI, BasicBlock *BB) {
797800
// Recurse on our inputs if needed
798801
Optional<ValueLatticeElement> OptTrueVal =
799-
getBlockValue(SI->getTrueValue(), BB);
802+
getBlockValue(SI->getTrueValue(), BB, SI);
800803
if (!OptTrueVal)
801804
return None;
802805
ValueLatticeElement &TrueVal = *OptTrueVal;
803806

804807
Optional<ValueLatticeElement> OptFalseVal =
805-
getBlockValue(SI->getFalseValue(), BB);
808+
getBlockValue(SI->getFalseValue(), BB, SI);
806809
if (!OptFalseVal)
807810
return None;
808811
ValueLatticeElement &FalseVal = *OptFalseVal;
@@ -873,12 +876,11 @@ Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueSelect(
873876
Optional<ConstantRange> LazyValueInfoImpl::getRangeFor(Value *V,
874877
Instruction *CxtI,
875878
BasicBlock *BB) {
876-
Optional<ValueLatticeElement> OptVal = getBlockValue(V, BB);
879+
Optional<ValueLatticeElement> OptVal = getBlockValue(V, BB, CxtI);
877880
if (!OptVal)
878881
return None;
879882

880883
ValueLatticeElement &Val = *OptVal;
881-
intersectAssumeOrGuardBlockValueConstantRange(V, Val, CxtI);
882884
if (Val.isConstantRange())
883885
return Val.getConstantRange();
884886

@@ -1017,7 +1019,7 @@ Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueExtractValue(
10171019
if (Value *V = SimplifyExtractValueInst(
10181020
EVI->getAggregateOperand(), EVI->getIndices(),
10191021
EVI->getModule()->getDataLayout()))
1020-
return getBlockValue(V, BB);
1022+
return getBlockValue(V, BB, EVI);
10211023

10221024
LLVM_DEBUG(dbgs() << " compute BB '" << BB->getName()
10231025
<< "' - overdefined (unknown extractvalue).\n");
@@ -1430,14 +1432,12 @@ Optional<ValueLatticeElement> LazyValueInfoImpl::getEdgeValue(
14301432
// Can't get any more precise here
14311433
return LocalResult;
14321434

1433-
Optional<ValueLatticeElement> OptInBlock = getBlockValue(Val, BBFrom);
1435+
Optional<ValueLatticeElement> OptInBlock =
1436+
getBlockValue(Val, BBFrom, BBFrom->getTerminator());
14341437
if (!OptInBlock)
14351438
return None;
14361439
ValueLatticeElement &InBlock = *OptInBlock;
14371440

1438-
// Try to intersect ranges of the BB and the constraint on the edge.
1439-
intersectAssumeOrGuardBlockValueConstantRange(Val, InBlock,
1440-
BBFrom->getTerminator());
14411441
// We can use the context instruction (generically the ultimate instruction
14421442
// the calling pass is trying to simplify) here, even though the result of
14431443
// this function is generally cached when called from the solve* functions
@@ -1457,15 +1457,14 @@ ValueLatticeElement LazyValueInfoImpl::getValueInBlock(Value *V, BasicBlock *BB,
14571457
<< BB->getName() << "'\n");
14581458

14591459
assert(BlockValueStack.empty() && BlockValueSet.empty());
1460-
Optional<ValueLatticeElement> OptResult = getBlockValue(V, BB);
1460+
Optional<ValueLatticeElement> OptResult = getBlockValue(V, BB, CxtI);
14611461
if (!OptResult) {
14621462
solve();
1463-
OptResult = getBlockValue(V, BB);
1463+
OptResult = getBlockValue(V, BB, CxtI);
14641464
assert(OptResult && "Value not available after solving");
14651465
}
1466-
ValueLatticeElement Result = *OptResult;
1467-
intersectAssumeOrGuardBlockValueConstantRange(V, Result, CxtI);
14681466

1467+
ValueLatticeElement Result = *OptResult;
14691468
LLVM_DEBUG(dbgs() << " Result = " << Result << "\n");
14701469
return Result;
14711470
}

llvm/test/Transforms/CorrelatedValuePropagation/basic.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,8 +1653,7 @@ define void @select_assume(i32 %a, i32 %b, i1 %c, i1* %p) {
16531653
; CHECK-NEXT: [[S:%.*]] = select i1 [[C:%.*]], i32 [[A]], i32 [[B]]
16541654
; CHECK-NEXT: [[C3:%.*]] = icmp ult i32 [[S]], 19
16551655
; CHECK-NEXT: store i1 [[C3]], i1* [[P:%.*]], align 1
1656-
; CHECK-NEXT: [[C4:%.*]] = icmp ult i32 [[S]], 20
1657-
; CHECK-NEXT: store i1 [[C4]], i1* [[P]], align 1
1656+
; CHECK-NEXT: store i1 true, i1* [[P]], align 1
16581657
; CHECK-NEXT: ret void
16591658
;
16601659
%c1 = icmp ult i32 %a, 10

0 commit comments

Comments
 (0)