Skip to content

Commit 899fe2c

Browse files
authored
[CVP][LVI] Fix incorrect scalar type when getting constant folded vec (llvm#97682)
Fixes llvm#97674 After llvm#97428 added support for vectors, our constant ranges can now be from splat vectors so when they reduce to a singe constant value, we need to return the original type as opposed to just an int.
1 parent c67653f commit 899fe2c

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,7 @@ LazyValueInfoImpl::getEdgeValueLocal(Value *Val, BasicBlock *BBFrom,
13701370

13711371
// If V is the condition of the branch itself, then we know exactly what
13721372
// it is.
1373+
// NB: The condition on a `br` can't be a vector type.
13731374
if (Condition == Val)
13741375
return ValueLatticeElement::get(ConstantInt::get(
13751376
Type::getInt1Ty(Val->getContext()), isTrueDest));
@@ -1723,7 +1724,7 @@ Constant *LazyValueInfo::getConstant(Value *V, Instruction *CxtI) {
17231724
if (Result.isConstantRange()) {
17241725
const ConstantRange &CR = Result.getConstantRange();
17251726
if (const APInt *SingleVal = CR.getSingleElement())
1726-
return ConstantInt::get(V->getContext(), *SingleVal);
1727+
return ConstantInt::get(V->getType(), *SingleVal);
17271728
}
17281729
return nullptr;
17291730
}
@@ -1758,7 +1759,7 @@ Constant *LazyValueInfo::getConstantOnEdge(Value *V, BasicBlock *FromBB,
17581759
if (Result.isConstantRange()) {
17591760
const ConstantRange &CR = Result.getConstantRange();
17601761
if (const APInt *SingleVal = CR.getSingleElement())
1761-
return ConstantInt::get(V->getContext(), *SingleVal);
1762+
return ConstantInt::get(V->getType(), *SingleVal);
17621763
}
17631764
return nullptr;
17641765
}

llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,24 @@ define <2 x i16> @and_with_poison(<2 x i8> %a) {
220220
%res = and <2 x i16> %zext, <i16 u0xff, i16 poison>
221221
ret <2 x i16> %res
222222
}
223+
224+
225+
226+
define <4 x i64> @issue_97674_getConstantOnEdge(i1 %cond) {
227+
entry:
228+
br i1 %cond, label %if.then, label %if.end
229+
230+
if.then:
231+
%folds = add <4 x i64> zeroinitializer, <i64 1, i64 1, i64 1, i64 1>
232+
br label %if.end
233+
234+
if.end:
235+
%r = phi <4 x i64> [ %folds, %if.then ], [ zeroinitializer, %entry ]
236+
ret <4 x i64> %r
237+
}
238+
239+
define <4 x i64> @issue_97674_getConstant() {
240+
entry:
241+
%folds = add <4 x i64> zeroinitializer, zeroinitializer
242+
ret <4 x i64> %folds
243+
}

0 commit comments

Comments
 (0)