Skip to content

Commit 1d43096

Browse files
authored
[ConstraintElim] Don't decompose values wider than 64 bits (llvm#68803)
Our coefficients are 64-bits, so adding/multiplying them can wrap in 64-bits even if there would be no wrapping the full bit width. The alternative would be to check for overflows during all adds/muls in decomposition. I assume that we don't particularly care about handling wide integers here, so I've opted to bail out. Fixes llvm#68751.
1 parent dad563e commit 1d43096

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,19 @@ static Decomposition decompose(Value *V,
451451
return ResA;
452452
};
453453

454+
Type *Ty = V->getType()->getScalarType();
455+
if (Ty->isPointerTy() && !IsSigned) {
456+
if (auto *GEP = dyn_cast<GEPOperator>(V))
457+
return decomposeGEP(*GEP, Preconditions, IsSigned, DL);
458+
return V;
459+
}
460+
461+
// Don't handle integers > 64 bit. Our coefficients are 64-bit large, so
462+
// coefficient add/mul may wrap, while the operation in the full bit width
463+
// would not.
464+
if (!Ty->isIntegerTy() || Ty->getIntegerBitWidth() > 64)
465+
return V;
466+
454467
// Decompose \p V used with a signed predicate.
455468
if (IsSigned) {
456469
if (auto *CI = dyn_cast<ConstantInt>(V)) {
@@ -478,9 +491,6 @@ static Decomposition decompose(Value *V,
478491
return int64_t(CI->getZExtValue());
479492
}
480493

481-
if (auto *GEP = dyn_cast<GEPOperator>(V))
482-
return decomposeGEP(*GEP, Preconditions, IsSigned, DL);
483-
484494
Value *Op0;
485495
bool IsKnownNonNegative = false;
486496
if (match(V, m_ZExt(m_Value(Op0)))) {

llvm/test/Transforms/ConstraintElimination/large-constant-ints.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ else:
9696
ret i1 false
9797
}
9898

99+
; TODO: This could be folded.
99100
define i1 @sub_decomp_i80(i80 %a) {
100101
; CHECK-LABEL: @sub_decomp_i80(
101102
; CHECK-NEXT: entry:
@@ -104,7 +105,8 @@ define i1 @sub_decomp_i80(i80 %a) {
104105
; CHECK-NEXT: br i1 [[C]], label [[THEN:%.*]], label [[ELSE:%.*]]
105106
; CHECK: then:
106107
; CHECK-NEXT: [[SUB_1:%.*]] = sub nuw i80 [[A]], 1973801615886922022913
107-
; CHECK-NEXT: ret i1 true
108+
; CHECK-NEXT: [[C_1:%.*]] = icmp ult i80 [[SUB_1]], 1346612317380797267967
109+
; CHECK-NEXT: ret i1 [[C_1]]
108110
; CHECK: else:
109111
; CHECK-NEXT: ret i1 false
110112
;
@@ -418,12 +420,12 @@ entry:
418420
ret i1 %res
419421
}
420422

421-
; FIXME: This is a miscompile.
422423
define i1 @pr68751(i128 %arg) {
423424
; CHECK-LABEL: @pr68751(
424425
; CHECK-NEXT: [[SHL1:%.*]] = shl nuw nsw i128 [[ARG:%.*]], 32
425426
; CHECK-NEXT: [[SHL2:%.*]] = shl nuw nsw i128 [[SHL1]], 32
426-
; CHECK-NEXT: ret i1 true
427+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i128 [[SHL2]], 0
428+
; CHECK-NEXT: ret i1 [[CMP]]
427429
;
428430
%shl1 = shl nuw nsw i128 %arg, 32
429431
%shl2 = shl nuw nsw i128 %shl1, 32

llvm/test/Transforms/ConstraintElimination/shl.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,8 @@ define i1 @shl_55() {
12771277
; CHECK-LABEL: @shl_55(
12781278
; CHECK-NEXT: entry:
12791279
; CHECK-NEXT: [[SHL_UB:%.*]] = shl nuw nsw i256 1, 55
1280-
; CHECK-NEXT: ret i1 true
1280+
; CHECK-NEXT: [[SHL_CMP:%.*]] = icmp uge i256 [[SHL_UB]], 1
1281+
; CHECK-NEXT: ret i1 [[SHL_CMP]]
12811282
;
12821283
entry:
12831284
%shl.ub = shl nuw nsw i256 1, 55

0 commit comments

Comments
 (0)