Skip to content

Commit 0e11d81

Browse files
committed
[ValueTracking,VectorCombine] Allow passing DT to computeConstantRange.
isValidAssumeForContext can provide better results with access to the dominator tree in some cases. This patch adjusts computeConstantRange to allow passing through a dominator tree. The use VectorCombine is updated to pass through the DT to enable additional scalarization. Note that similar APIs like computeKnownBits already accept optional dominator tree arguments. Reviewed By: lebedev.ri Differential Revision: https://reviews.llvm.org/D110175 (cherry-picked from 5131037)
1 parent 16e244e commit 0e11d81

File tree

6 files changed

+15
-11
lines changed

6 files changed

+15
-11
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ constexpr unsigned MaxAnalysisRecursionDepth = 6;
549549
ConstantRange computeConstantRange(const Value *V, bool UseInstrInfo = true,
550550
AssumptionCache *AC = nullptr,
551551
const Instruction *CtxI = nullptr,
552+
const DominatorTree *DT = nullptr,
552553
unsigned Depth = 0);
553554

554555
/// Return true if this function can prove that the instruction I will

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6989,6 +6989,7 @@ static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower,
69896989
ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo,
69906990
AssumptionCache *AC,
69916991
const Instruction *CtxI,
6992+
const DominatorTree *DT,
69926993
unsigned Depth) {
69936994
assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
69946995

@@ -7027,15 +7028,15 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo,
70277028
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
70287029
"must be an assume intrinsic");
70297030

7030-
if (!isValidAssumeForContext(I, CtxI, nullptr))
7031+
if (!isValidAssumeForContext(I, CtxI, DT))
70317032
continue;
70327033
Value *Arg = I->getArgOperand(0);
70337034
ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
70347035
// Currently we just use information from comparisons.
70357036
if (!Cmp || Cmp->getOperand(0) != V)
70367037
continue;
70377038
ConstantRange RHS = computeConstantRange(Cmp->getOperand(1), UseInstrInfo,
7038-
AC, I, Depth + 1);
7039+
AC, I, DT, Depth + 1);
70397040
CR = CR.intersectWith(
70407041
ConstantRange::makeSatisfyingICmpRegion(Cmp->getPredicate(), RHS));
70417042
}

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,8 @@ class ScalarizationResult {
827827
/// Idx. \p Idx must access a valid vector element.
828828
static ScalarizationResult canScalarizeAccess(FixedVectorType *VecTy,
829829
Value *Idx, Instruction *CtxI,
830-
AssumptionCache &AC) {
830+
AssumptionCache &AC,
831+
const DominatorTree &DT) {
831832
if (auto *C = dyn_cast<ConstantInt>(Idx)) {
832833
if (C->getValue().ult(VecTy->getNumElements()))
833834
return ScalarizationResult::safe();
@@ -841,7 +842,7 @@ static ScalarizationResult canScalarizeAccess(FixedVectorType *VecTy,
841842
ConstantRange IdxRange(IntWidth, true);
842843

843844
if (isGuaranteedNotToBePoison(Idx, &AC)) {
844-
if (ValidIndices.contains(computeConstantRange(Idx, true, &AC, CtxI, 0)))
845+
if (ValidIndices.contains(computeConstantRange(Idx, true, &AC, CtxI, &DT)))
845846
return ScalarizationResult::safe();
846847
return ScalarizationResult::unsafe();
847848
}
@@ -909,7 +910,7 @@ bool VectorCombine::foldSingleElementStore(Instruction &I) {
909910
SrcAddr != SI->getPointerOperand()->stripPointerCasts())
910911
return false;
911912

912-
auto ScalarizableIdx = canScalarizeAccess(VecTy, Idx, Load, AC);
913+
auto ScalarizableIdx = canScalarizeAccess(VecTy, Idx, Load, AC, DT);
913914
if (ScalarizableIdx.isUnsafe() ||
914915
isMemModifiedBetween(Load->getIterator(), SI->getIterator(),
915916
MemoryLocation::get(SI), AA))
@@ -987,7 +988,7 @@ bool VectorCombine::scalarizeLoadExtract(Instruction &I) {
987988
else if (LastCheckedInst->comesBefore(UI))
988989
LastCheckedInst = UI;
989990

990-
auto ScalarIdx = canScalarizeAccess(FixedVT, UI->getOperand(1), &I, AC);
991+
auto ScalarIdx = canScalarizeAccess(FixedVT, UI->getOperand(1), &I, AC, DT);
991992
if (!ScalarIdx.isSafe()) {
992993
// TODO: Freeze index if it is safe to do so.
993994
return false;

llvm/test/Transforms/VectorCombine/AArch64/load-extract-insert-store-scalarization.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ define void @load_extract_insert_store_var_idx_assume_valid_in_dominating_block(
7474
; CHECK-NEXT: [[MUL:%.*]] = fmul double 2.000000e+01, [[EXT_0]]
7575
; CHECK-NEXT: [[EXT_1:%.*]] = extractelement <225 x double> [[LV]], i64 [[IDX_2]]
7676
; CHECK-NEXT: [[SUB:%.*]] = fsub double [[EXT_1]], [[MUL]]
77-
; CHECK-NEXT: [[INS:%.*]] = insertelement <225 x double> [[LV]], double [[SUB]], i64 [[IDX_1]]
78-
; CHECK-NEXT: store <225 x double> [[INS]], <225 x double>* [[A]], align 8
77+
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds <225 x double>, <225 x double>* [[A]], i64 0, i64 [[IDX_1]]
78+
; CHECK-NEXT: store double [[SUB]], double* [[TMP0]], align 8
7979
; CHECK-NEXT: [[C_2:%.*]] = call i1 @cond()
8080
; CHECK-NEXT: br i1 [[C_2]], label [[LOOP]], label [[EXIT]]
8181
; CHECK: exit:

llvm/test/Transforms/VectorCombine/AArch64/load-extractelement-scalarization.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ define i32 @load_extract_idx_var_i64_known_valid_by_assume_in_dominating_block(<
114114
; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
115115
; CHECK-NEXT: br i1 [[C_1:%.*]], label [[LOOP:%.*]], label [[EXIT:%.*]]
116116
; CHECK: loop:
117-
; CHECK-NEXT: [[LV:%.*]] = load <4 x i32>, <4 x i32>* [[X:%.*]], align 16
118117
; CHECK-NEXT: call void @maythrow()
119-
; CHECK-NEXT: [[R:%.*]] = extractelement <4 x i32> [[LV]], i64 [[IDX]]
118+
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds <4 x i32>, <4 x i32>* [[X:%.*]], i32 0, i64 [[IDX]]
119+
; CHECK-NEXT: [[R:%.*]] = load i32, i32* [[TMP0]], align 4
120120
; CHECK-NEXT: [[C_2:%.*]] = call i1 @cond()
121121
; CHECK-NEXT: br i1 [[C_2]], label [[LOOP]], label [[EXIT]]
122122
; CHECK: exit:

llvm/unittests/Analysis/ValueTrackingTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2097,8 +2097,9 @@ TEST_F(ValueTrackingTest, ComputeConstantRange) {
20972097

20982098
// Check the depth cutoff results in a conservative result (full set) by
20992099
// passing Depth == MaxDepth == 6.
2100-
ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, 6);
2100+
ConstantRange CR2 = computeConstantRange(X2, true, &AC, I, nullptr, 6);
21012101
EXPECT_TRUE(CR2.isFullSet());
2102+
21022103
}
21032104
}
21042105

0 commit comments

Comments
 (0)