Skip to content

Commit eb8c079

Browse files
nikicyuxuanchen1997
authored andcommitted
[ValueLattice] Support constant vectors in mergeIn() (#99466)
Summary: This is a followup to vector support in LVI/CVP/SCCP. In mergeIn(), if one of the operands is a vector of integer constant, we should try to convert it into a constant range, in case that allows performing a range union to something better than overdefined. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250842
1 parent 3dcab32 commit eb8c079

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

llvm/include/llvm/Analysis/ValueLattice.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,18 +281,21 @@ class ValueLatticeElement {
281281
return std::nullopt;
282282
}
283283

284-
ConstantRange asConstantRange(Type *Ty, bool UndefAllowed = false) const {
285-
assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
284+
ConstantRange asConstantRange(unsigned BW, bool UndefAllowed = false) const {
286285
if (isConstantRange(UndefAllowed))
287286
return getConstantRange();
288287
if (isConstant())
289288
return getConstant()->toConstantRange();
290-
unsigned BW = Ty->getScalarSizeInBits();
291289
if (isUnknown())
292290
return ConstantRange::getEmpty(BW);
293291
return ConstantRange::getFull(BW);
294292
}
295293

294+
ConstantRange asConstantRange(Type *Ty, bool UndefAllowed = false) const {
295+
assert(Ty->isIntOrIntVectorTy() && "Must be integer type");
296+
return asConstantRange(Ty->getScalarSizeInBits(), UndefAllowed);
297+
}
298+
296299
bool markOverdefined() {
297300
if (isOverdefined())
298301
return false;
@@ -384,7 +387,9 @@ class ValueLatticeElement {
384387
return true;
385388
}
386389

387-
assert(isUnknown() || isUndef());
390+
assert(isUnknown() || isUndef() || isConstant());
391+
assert((!isConstant() || NewR.contains(getConstant()->toConstantRange())) &&
392+
"Constant must be subset of new range");
388393

389394
NumRangeExtensions = 0;
390395
Tag = NewTag;
@@ -426,6 +431,16 @@ class ValueLatticeElement {
426431
return false;
427432
if (RHS.isUndef())
428433
return false;
434+
// If the constant is a vector of integers, try to treat it as a range.
435+
if (getConstant()->getType()->isVectorTy() &&
436+
getConstant()->getType()->getScalarType()->isIntegerTy()) {
437+
ConstantRange L = getConstant()->toConstantRange();
438+
ConstantRange NewR = L.unionWith(
439+
RHS.asConstantRange(L.getBitWidth(), /*UndefAllowed=*/true));
440+
return markConstantRange(
441+
std::move(NewR),
442+
Opts.setMayIncludeUndef(RHS.isConstantRangeIncludingUndef()));
443+
}
429444
markOverdefined();
430445
return true;
431446
}
@@ -444,14 +459,9 @@ class ValueLatticeElement {
444459
return OldTag != Tag;
445460
}
446461

447-
if (!RHS.isConstantRange()) {
448-
// We can get here if we've encountered a constantexpr of integer type
449-
// and merge it with a constantrange.
450-
markOverdefined();
451-
return true;
452-
}
453-
454-
ConstantRange NewR = getConstantRange().unionWith(RHS.getConstantRange());
462+
const ConstantRange &L = getConstantRange();
463+
ConstantRange NewR = L.unionWith(
464+
RHS.asConstantRange(L.getBitWidth(), /*UndefAllowed=*/true));
455465
return markConstantRange(
456466
std::move(NewR),
457467
Opts.setMayIncludeUndef(RHS.isConstantRangeIncludingUndef()));

llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ define <2 x i16> @phi_merge1(i1 %c, <2 x i8> %a) {
286286
; CHECK-NEXT: br label %[[JOIN]]
287287
; CHECK: [[JOIN]]:
288288
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i16> [ [[ZEXT]], %[[ENTRY]] ], [ <i16 1, i16 2>, %[[IF]] ]
289-
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i16> [[PHI]], <i16 2, i16 3>
289+
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw <2 x i16> [[PHI]], <i16 2, i16 3>
290290
; CHECK-NEXT: ret <2 x i16> [[ADD]]
291291
;
292292
entry:
@@ -312,7 +312,7 @@ define <2 x i16> @phi_merge2(i1 %c, <2 x i8> %a) {
312312
; CHECK-NEXT: br label %[[JOIN]]
313313
; CHECK: [[JOIN]]:
314314
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i16> [ <i16 1, i16 2>, %[[ENTRY]] ], [ [[ZEXT]], %[[IF]] ]
315-
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i16> [[PHI]], <i16 2, i16 3>
315+
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw <2 x i16> [[PHI]], <i16 2, i16 3>
316316
; CHECK-NEXT: ret <2 x i16> [[ADD]]
317317
;
318318
entry:

llvm/test/Transforms/SCCP/phis.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ define <2 x i16> @phi_vector_merge1(i1 %c, <2 x i8> %a) {
109109
; CHECK-NEXT: br label %[[JOIN]]
110110
; CHECK: [[JOIN]]:
111111
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i16> [ [[ZEXT]], %[[ENTRY]] ], [ <i16 1, i16 2>, %[[IF]] ]
112-
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i16> [[PHI]], <i16 2, i16 3>
112+
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw <2 x i16> [[PHI]], <i16 2, i16 3>
113113
; CHECK-NEXT: ret <2 x i16> [[ADD]]
114114
;
115115
entry:
@@ -135,7 +135,7 @@ define <2 x i16> @phi_vector_merge2(i1 %c, <2 x i8> %a) {
135135
; CHECK-NEXT: br label %[[JOIN]]
136136
; CHECK: [[JOIN]]:
137137
; CHECK-NEXT: [[PHI:%.*]] = phi <2 x i16> [ <i16 1, i16 2>, %[[ENTRY]] ], [ [[ZEXT]], %[[IF]] ]
138-
; CHECK-NEXT: [[ADD:%.*]] = add <2 x i16> [[PHI]], <i16 2, i16 3>
138+
; CHECK-NEXT: [[ADD:%.*]] = add nuw nsw <2 x i16> [[PHI]], <i16 2, i16 3>
139139
; CHECK-NEXT: ret <2 x i16> [[ADD]]
140140
;
141141
entry:

0 commit comments

Comments
 (0)