Skip to content

Commit 5131037

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
1 parent 5fb3ae5 commit 5131037

File tree

6 files changed

+14
-11
lines changed

6 files changed

+14
-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
@@ -7031,6 +7031,7 @@ static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower,
70317031
ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo,
70327032
AssumptionCache *AC,
70337033
const Instruction *CtxI,
7034+
const DominatorTree *DT,
70347035
unsigned Depth) {
70357036
assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
70367037

@@ -7069,15 +7070,15 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool UseInstrInfo,
70697070
assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
70707071
"must be an assume intrinsic");
70717072

7072-
if (!isValidAssumeForContext(I, CtxI, nullptr))
7073+
if (!isValidAssumeForContext(I, CtxI, DT))
70737074
continue;
70747075
Value *Arg = I->getArgOperand(0);
70757076
ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
70767077
// Currently we just use information from comparisons.
70777078
if (!Cmp || Cmp->getOperand(0) != V)
70787079
continue;
70797080
ConstantRange RHS = computeConstantRange(Cmp->getOperand(1), UseInstrInfo,
7080-
AC, I, Depth + 1);
7081+
AC, I, DT, Depth + 1);
70817082
CR = CR.intersectWith(
70827083
ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
70837084
}

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,7 @@ TEST_F(ValueTrackingTest, ComputeConstantRange) {
21032103

21042104
// Check the depth cutoff results in a conservative result (full set) by
21052105
// passing Depth == MaxDepth == 6.
2106-
ConstantRange CR3 = computeConstantRange(X2, true, &AC, I, 6);
2106+
ConstantRange CR3 = computeConstantRange(X2, true, &AC, I, nullptr, 6);
21072107
EXPECT_TRUE(CR3.isFullSet());
21082108
}
21092109
{

0 commit comments

Comments
 (0)