Skip to content

Commit dd9f1a5

Browse files
authored
[InstSimplify] Correctly handle comparison with zero-size allocs (#115728)
InstSimplify currently folds alloc1 == alloc2 to false, even if one of them is a zero-size allocation. A zero-size allocation may have the same address as another allocation. This also disables the fold for the case where we're comparing a zero-size alloc with the middle of another allocation. It's possible that this case is legal to fold depending on our precise zero-size allocation semantics, but LangRef currently doesn't specify this either way, so we shouldn't make assumptions here.
1 parent 9b6b9d3 commit dd9f1a5

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,8 +2774,8 @@ static Constant *computePointerICmp(CmpInst::Predicate Pred, Value *LHS,
27742774
return nullptr;
27752775
}(LHS);
27762776
Opts.NullIsUnknownSize = F ? NullPointerIsDefined(F) : true;
2777-
if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) &&
2778-
getObjectSize(RHS, RHSSize, DL, TLI, Opts)) {
2777+
if (getObjectSize(LHS, LHSSize, DL, TLI, Opts) && LHSSize != 0 &&
2778+
getObjectSize(RHS, RHSSize, DL, TLI, Opts) && RHSSize != 0) {
27792779
APInt Dist = LHSOffset - RHSOffset;
27802780
if (Dist.isNonNegative() ? Dist.ult(LHSSize) : (-Dist).ult(RHSSize))
27812781
return ConstantInt::get(getCompareTy(LHS),

llvm/test/Transforms/InstSimplify/cmp-alloca-offsets.ll

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ define i1 @zst_alloca_start() {
234234
; CHECK-LABEL: @zst_alloca_start(
235235
; CHECK-NEXT: [[A:%.*]] = alloca i64, align 8
236236
; CHECK-NEXT: [[A2:%.*]] = alloca {}, align 1
237+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[A]], [[A2]]
237238
; CHECK-NEXT: call void @escape(ptr [[A]], ptr [[A2]])
238-
; CHECK-NEXT: ret i1 false
239+
; CHECK-NEXT: ret i1 [[CMP]]
239240
;
240241
%a = alloca i64
241242
%a2 = alloca {}, align 1
@@ -249,8 +250,10 @@ define i1 @zst_alloca_middle() {
249250
; CHECK-LABEL: @zst_alloca_middle(
250251
; CHECK-NEXT: [[A:%.*]] = alloca i64, align 8
251252
; CHECK-NEXT: [[A2:%.*]] = alloca {}, align 1
253+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[A]], i64 4
254+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[GEP]], [[A2]]
252255
; CHECK-NEXT: call void @escape(ptr [[A]], ptr [[A2]])
253-
; CHECK-NEXT: ret i1 false
256+
; CHECK-NEXT: ret i1 [[CMP]]
254257
;
255258
%a = alloca i64
256259
%a2 = alloca {}, align 1
@@ -282,8 +285,9 @@ define i1 @zst_alloca_end() {
282285
define i1 @zst_global_start() {
283286
; CHECK-LABEL: @zst_global_start(
284287
; CHECK-NEXT: [[A:%.*]] = alloca i64, align 8
288+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[A]], @gz
285289
; CHECK-NEXT: call void @escape(ptr [[A]], ptr @gz)
286-
; CHECK-NEXT: ret i1 false
290+
; CHECK-NEXT: ret i1 [[CMP]]
287291
;
288292
%a = alloca i64
289293
%gep = getelementptr i8, ptr %a, i64 0
@@ -295,8 +299,10 @@ define i1 @zst_global_start() {
295299
define i1 @zst_global_middle() {
296300
; CHECK-LABEL: @zst_global_middle(
297301
; CHECK-NEXT: [[A:%.*]] = alloca i64, align 8
302+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i8, ptr [[A]], i64 4
303+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[GEP]], @gz
298304
; CHECK-NEXT: call void @escape(ptr [[A]], ptr @gz)
299-
; CHECK-NEXT: ret i1 false
305+
; CHECK-NEXT: ret i1 [[CMP]]
300306
;
301307
%a = alloca i64
302308
%gep = getelementptr i8, ptr %a, i64 4

0 commit comments

Comments
 (0)