Skip to content

Commit 7f790f9

Browse files
nikictru
authored andcommitted
[ConstraintElim] Don't decompose values wider than 64 bits (#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 #68751. (cherry picked from commit 1d43096)
1 parent a1c67ff commit 7f790f9

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,19 @@ static Decomposition decompose(Value *V,
412412
return ResA;
413413
};
414414

415+
Type *Ty = V->getType()->getScalarType();
416+
if (Ty->isPointerTy() && !IsSigned) {
417+
if (auto *GEP = dyn_cast<GEPOperator>(V))
418+
return decomposeGEP(*GEP, Preconditions, IsSigned, DL);
419+
return V;
420+
}
421+
422+
// Don't handle integers > 64 bit. Our coefficients are 64-bit large, so
423+
// coefficient add/mul may wrap, while the operation in the full bit width
424+
// would not.
425+
if (!Ty->isIntegerTy() || Ty->getIntegerBitWidth() > 64)
426+
return V;
427+
415428
// Decompose \p V used with a signed predicate.
416429
if (IsSigned) {
417430
if (auto *CI = dyn_cast<ConstantInt>(V)) {
@@ -439,9 +452,6 @@ static Decomposition decompose(Value *V,
439452
return int64_t(CI->getZExtValue());
440453
}
441454

442-
if (auto *GEP = dyn_cast<GEPOperator>(V))
443-
return decomposeGEP(*GEP, Preconditions, IsSigned, DL);
444-
445455
Value *Op0;
446456
bool IsKnownNonNegative = false;
447457
if (match(V, m_ZExt(m_Value(Op0)))) {

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

Lines changed: 4 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:
@@ -105,7 +106,7 @@ define i1 @sub_decomp_i80(i80 %a) {
105106
; CHECK: then:
106107
; CHECK-NEXT: [[SUB_1:%.*]] = sub nuw i80 [[A]], 1973801615886922022913
107108
; CHECK-NEXT: [[C_1:%.*]] = icmp ult i80 [[SUB_1]], 1346612317380797267967
108-
; CHECK-NEXT: ret i1 true
109+
; CHECK-NEXT: ret i1 [[C_1]]
109110
; CHECK: else:
110111
; CHECK-NEXT: ret i1 false
111112
;
@@ -423,12 +424,12 @@ entry:
423424
ret i1 %res
424425
}
425426

426-
; FIXME: This is a miscompile.
427427
define i1 @pr68751(i128 %arg) {
428428
; CHECK-LABEL: @pr68751(
429429
; CHECK-NEXT: [[SHL1:%.*]] = shl nuw nsw i128 [[ARG:%.*]], 32
430430
; CHECK-NEXT: [[SHL2:%.*]] = shl nuw nsw i128 [[SHL1]], 32
431-
; CHECK-NEXT: ret i1 true
431+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i128 [[SHL2]], 0
432+
; CHECK-NEXT: ret i1 [[CMP]]
432433
;
433434
%shl1 = shl nuw nsw i128 %arg, 32
434435
%shl2 = shl nuw nsw i128 %shl1, 32

llvm/test/Transforms/ConstraintElimination/shl.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ define i1 @shl_55() {
12811281
; CHECK-NEXT: entry:
12821282
; CHECK-NEXT: [[SHL_UB:%.*]] = shl nuw nsw i256 1, 55
12831283
; CHECK-NEXT: [[SHL_CMP:%.*]] = icmp uge i256 [[SHL_UB]], 1
1284-
; CHECK-NEXT: ret i1 true
1284+
; CHECK-NEXT: ret i1 [[SHL_CMP]]
12851285
;
12861286
entry:
12871287
%shl.ub = shl nuw nsw i256 1, 55

0 commit comments

Comments
 (0)