Skip to content

Commit c7f0e49

Browse files
committed
[InstCombine] Fix canAlwaysEvaluateInTy() with constant exprs
The m_ZExtOrSExt / m_Trunc in the following code can match constant expressions, which we don't want here. Make sure we bail out early for non-immediate constants.
1 parent 128b3b6 commit c7f0e49

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) {
213213
/// Constants and extensions/truncates from the destination type are always
214214
/// free to be evaluated in that type. This is a helper for canEvaluate*.
215215
static bool canAlwaysEvaluateInType(Value *V, Type *Ty) {
216-
if (match(V, m_ImmConstant()))
217-
return true;
216+
if (isa<Constant>(V))
217+
return match(V, m_ImmConstant());
218+
218219
Value *X;
219220
if ((match(V, m_ZExtOrSExt(m_Value(X))) || match(V, m_Trunc(m_Value(X)))) &&
220221
X->getType() == Ty)

llvm/test/Transforms/InstCombine/zext.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
33

4+
target datalayout = "n64"
5+
46
declare void @use1(i1)
57
declare void @use32(i32)
68
declare void @use_vec(<2 x i9>)
@@ -762,3 +764,16 @@ define i32 @zext_icmp_eq0_no_shift(ptr %ptr ) {
762764
%res = zext i1 %cmp to i32
763765
ret i32 %res
764766
}
767+
768+
@g = external global i8
769+
770+
define i64 @evaluate_zexted_const_expr(i1 %c) {
771+
; CHECK-LABEL: @evaluate_zexted_const_expr(
772+
; CHECK-NEXT: [[AND:%.*]] = select i1 [[C:%.*]], i7 trunc (i64 add (i64 ptrtoint (ptr @g to i64), i64 1) to i7), i7 trunc (i64 add (i64 ptrtoint (ptr @g to i64), i64 2) to i7)
773+
; CHECK-NEXT: [[EXT:%.*]] = zext i7 [[AND]] to i64
774+
; CHECK-NEXT: ret i64 [[EXT]]
775+
;
776+
%and = select i1 %c, i7 trunc (i64 add (i64 ptrtoint (ptr @g to i64), i64 1) to i7), i7 trunc (i64 add (i64 ptrtoint (ptr @g to i64), i64 2) to i7)
777+
%ext = zext i7 %and to i64
778+
ret i64 %ext
779+
}

0 commit comments

Comments
 (0)