Skip to content

Commit

Permalink
[NFC][InstCombine][SROA][Asan] Precommit test affected by #100773
Browse files Browse the repository at this point in the history
Some optimizations need to be undone with
sanitizers by #100773.

Pull Request: #100844
  • Loading branch information
vitalybuka committed Jul 27, 2024
1 parent 47aea61 commit 3eb7a8e
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 1 deletion.
10 changes: 10 additions & 0 deletions llvm/test/Transforms/InstCombine/load.ll
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ define i32 @test5(i1 %C) {
ret i32 %Z
}

define i32 @test5_asan(i1 %C) sanitize_address {
; CHECK-LABEL: @test5_asan(
; CHECK-NEXT: [[Z:%.*]] = select i1 [[C:%.*]], i32 42, i32 47
; CHECK-NEXT: ret i32 [[Z]]
;
%Y = select i1 %C, ptr @X, ptr @X2 ; <ptr> [#uses=1]
%Z = load i32, ptr %Y ; <i32> [#uses=1]
ret i32 %Z
}

define i32 @load_gep_null_inbounds(i64 %X) {
; CHECK-LABEL: @load_gep_null_inbounds(
; CHECK-NEXT: store i1 true, ptr poison, align 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,27 @@ define void @PR35618(ptr %st1, ptr %st2) {
ret void
}

define void @PR35618_asan(ptr %st1, ptr %st2) sanitize_address {
; CHECK-LABEL: @PR35618_asan(
; CHECK-NEXT: [[Y1:%.*]] = alloca double, align 8
; CHECK-NEXT: [[Z1:%.*]] = alloca double, align 8
; CHECK-NEXT: [[LD1:%.*]] = load double, ptr [[Y1]], align 8
; CHECK-NEXT: [[LD2:%.*]] = load double, ptr [[Z1]], align 8
; CHECK-NEXT: [[TMP:%.*]] = fcmp olt double [[LD1]], [[LD2]]
; CHECK-NEXT: [[TMP12_V:%.*]] = select i1 [[TMP]], double [[LD1]], double [[LD2]]
; CHECK-NEXT: store double [[TMP12_V]], ptr [[ST1:%.*]], align 8
; CHECK-NEXT: store double [[TMP12_V]], ptr [[ST2:%.*]], align 8
; CHECK-NEXT: ret void
;
%y1 = alloca double
%z1 = alloca double
%ld1 = load double, ptr %y1
%ld2 = load double, ptr %z1
%tmp = fcmp olt double %ld1, %ld2
%sel = select i1 %tmp, ptr %y1, ptr %z1
%tmp12 = load i64, ptr %sel
store i64 %tmp12, ptr %st1
store i64 %tmp12, ptr %st2
ret void
}

15 changes: 15 additions & 0 deletions llvm/test/Transforms/InstCombine/ptr-replace-alloca.ll
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,21 @@ entry:
ret i8 %load
}

define i8 @select_diff_addrspace_remove_alloca_asan(i1 %cond, ptr %p) sanitize_address {
; CHECK-LABEL: @select_diff_addrspace_remove_alloca_asan(
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i8 0
;
entry:
%alloca = alloca [32 x i8]
call void @llvm.memcpy.p0.p1.i64(ptr %alloca, ptr addrspace(1) @g2, i64 32, i1 false)
%gep = getelementptr inbounds [32 x i8], ptr %alloca, i32 0, i32 2
%sel = select i1 %cond, ptr %alloca, ptr %gep
%gep2 = getelementptr inbounds i8, ptr %sel, i64 4
%load = load i8, ptr %gep2
ret i8 %load
}

declare i8 @readonly_callee(ptr readonly nocapture)

; FIXME: This should be able to fold to call i8 @readonly_callee(ptr nonnull @g1)
Expand Down
104 changes: 104 additions & 0 deletions llvm/test/Transforms/InstCombine/select-load.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -passes=instcombine -S < %s | FileCheck %s

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-grtev4-linux-gnu"

define i32 @test_plain(i1 %f) {
; CHECK-LABEL: @test_plain(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[A_VAL:%.*]] = load i32, ptr [[A]], align 8
; CHECK-NEXT: [[B_VAL:%.*]] = load i32, ptr [[B]], align 8
; CHECK-NEXT: [[L:%.*]] = select i1 [[F:%.*]], i32 [[A_VAL]], i32 [[B_VAL]]
; CHECK-NEXT: ret i32 [[L]]
;
entry:
%a = alloca i32, align 8
%b = alloca i32, align 8
%sel = select i1 %f, ptr %a, ptr %b
%l = load i32, ptr %sel, align 8
ret i32 %l
}

; Don't speculate as the condition may control which memory is valid from
; sanitizer perspective.
define i32 @test_asan(i1 %f) sanitize_address {
; CHECK-LABEL: @test_asan(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[A_VAL:%.*]] = load i32, ptr [[A]], align 8
; CHECK-NEXT: [[B_VAL:%.*]] = load i32, ptr [[B]], align 8
; CHECK-NEXT: [[L:%.*]] = select i1 [[F:%.*]], i32 [[A_VAL]], i32 [[B_VAL]]
; CHECK-NEXT: ret i32 [[L]]
;
entry:
%a = alloca i32, align 8
%b = alloca i32, align 8
%sel = select i1 %f, ptr %a, ptr %b
%l = load i32, ptr %sel, align 8
ret i32 %l
}


; Don't speculate as the condition may control which memory is valid from
; sanitizer perspective.
define i32 @test_hwasan(i1 %f) sanitize_hwaddress {
; CHECK-LABEL: @test_hwasan(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[A_VAL:%.*]] = load i32, ptr [[A]], align 8
; CHECK-NEXT: [[B_VAL:%.*]] = load i32, ptr [[B]], align 8
; CHECK-NEXT: [[L:%.*]] = select i1 [[F:%.*]], i32 [[A_VAL]], i32 [[B_VAL]]
; CHECK-NEXT: ret i32 [[L]]
;
entry:
%a = alloca i32, align 8
%b = alloca i32, align 8
%sel = select i1 %f, ptr %a, ptr %b
%l = load i32, ptr %sel, align 8
ret i32 %l
}

; Don't speculate as the condition may control which memory is valid from
; sanitizer perspective.
define i32 @test_tsan(i1 %f) sanitize_thread {
; CHECK-LABEL: @test_tsan(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[A_VAL:%.*]] = load i32, ptr [[A]], align 8
; CHECK-NEXT: [[B_VAL:%.*]] = load i32, ptr [[B]], align 8
; CHECK-NEXT: [[L:%.*]] = select i1 [[F:%.*]], i32 [[A_VAL]], i32 [[B_VAL]]
; CHECK-NEXT: ret i32 [[L]]
;
entry:
%a = alloca i32, align 8
%b = alloca i32, align 8
%sel = select i1 %f, ptr %a, ptr %b
%l = load i32, ptr %sel, align 8
ret i32 %l
}

; Msan just propagates shadow, even if speculated load accesses uninitialized
; value, instrumentation will select shadow of the desired value anyway.
define i32 @test_msan(i1 %f) sanitize_memory {
; CHECK-LABEL: @test_msan(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 8
; CHECK-NEXT: [[A_VAL:%.*]] = load i32, ptr [[A]], align 8
; CHECK-NEXT: [[B_VAL:%.*]] = load i32, ptr [[B]], align 8
; CHECK-NEXT: [[L:%.*]] = select i1 [[F:%.*]], i32 [[A_VAL]], i32 [[B_VAL]]
; CHECK-NEXT: ret i32 [[L]]
;
entry:
%a = alloca i32, align 8
%b = alloca i32, align 8
%sel = select i1 %f, ptr %a, ptr %b
%l = load i32, ptr %sel, align 8
ret i32 %l
}
10 changes: 10 additions & 0 deletions llvm/test/Transforms/InstCombine/strnlen-2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ define i64 @fold_strnlen_s3_s5_1(i1 %C) {
ret i64 %len
}

define i64 @fold_strnlen_s3_s5_1_asan(i1 %C) sanitize_address {
; CHECK-LABEL: @fold_strnlen_s3_s5_1_asan(
; CHECK-NEXT: ret i64 1
;
%ptr = select i1 %C, ptr @s3, ptr @s6

%len = call i64 @strnlen(ptr %ptr, i64 1)
ret i64 %len
}


; Fold strnlen (C ? s3 : s5, 3) to 3.

Expand Down
21 changes: 21 additions & 0 deletions llvm/test/Transforms/SROA/phi-and-select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,27 @@ entry:
ret i32 %loaded
}

; We should not unconditionally load with sanitizers.
define i32 @test9_asan(i32 %b, ptr %ptr) sanitize_address {
; Same as @test8 but for a select rather than a PHI node.
;
; CHECK-LABEL: @test9_asan(
; CHECK-NEXT: entry:
; CHECK-NEXT: store i32 0, ptr [[PTR:%.*]], align 4
; CHECK-NEXT: [[TEST:%.*]] = icmp ne i32 [[B:%.*]], 0
; CHECK-NEXT: [[LOADED_SROA_SPECULATE_LOAD_FALSE:%.*]] = load i32, ptr [[PTR]], align 4
; CHECK-NEXT: [[LOADED_SROA_SPECULATED:%.*]] = select i1 [[TEST]], i32 undef, i32 [[LOADED_SROA_SPECULATE_LOAD_FALSE]]
; CHECK-NEXT: ret i32 [[LOADED_SROA_SPECULATED]]
;
entry:
%f = alloca float
store i32 0, ptr %ptr
%test = icmp ne i32 %b, 0
%select = select i1 %test, ptr %f, ptr %ptr
%loaded = load i32, ptr %select, align 4
ret i32 %loaded
}

define float @test10(i32 %b, ptr %ptr) {
; Don't try to promote allocas which are not elligible for it even after
; rewriting due to the necessity of inserting bitcasts when speculating a PHI
Expand Down
45 changes: 45 additions & 0 deletions llvm/test/Transforms/SROA/phi-with-duplicate-pred.ll
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,51 @@ cleanup7: ; preds = %cleanup
ret void
}

define void @f2_hwasan(i1 %c1) sanitize_hwaddress {
; CHECK-LABEL: @f2_hwasan(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[C1:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: br label [[CLEANUP:%.*]]
; CHECK: cleanup:
; CHECK-NEXT: [[G_0_SROA_SPECULATE_LOAD_CLEANUP:%.*]] = load i16, ptr @a, align 1
; CHECK-NEXT: switch i32 2, label [[CLEANUP7:%.*]] [
; CHECK-NEXT: i32 0, label [[LBL1:%.*]]
; CHECK-NEXT: i32 2, label [[LBL1]]
; CHECK-NEXT: ]
; CHECK: if.else:
; CHECK-NEXT: br label [[LBL1]]
; CHECK: lbl1:
; CHECK-NEXT: [[G_0_SROA_SPECULATED:%.*]] = phi i16 [ [[G_0_SROA_SPECULATE_LOAD_CLEANUP]], [[CLEANUP]] ], [ [[G_0_SROA_SPECULATE_LOAD_CLEANUP]], [[CLEANUP]] ], [ undef, [[IF_ELSE]] ]
; CHECK-NEXT: unreachable
; CHECK: cleanup7:
; CHECK-NEXT: ret void
;
entry:
%e = alloca i16, align 1
br i1 %c1, label %if.then, label %if.else

if.then: ; preds = %entry
br label %cleanup

cleanup: ; preds = %if.then
switch i32 2, label %cleanup7 [
i32 0, label %lbl1
i32 2, label %lbl1
]

if.else: ; preds = %entry
br label %lbl1

lbl1: ; preds = %if.else, %cleanup, %cleanup
%g.0 = phi ptr [ @a, %cleanup ], [ @a, %cleanup ], [ %e, %if.else ]
%0 = load i16, ptr %g.0, align 1
unreachable

cleanup7: ; preds = %cleanup
ret void
}

define void @f3(i1 %c1) {
; CHECK-LABEL: @f3(
; CHECK-NEXT: entry:
Expand Down
24 changes: 23 additions & 1 deletion llvm/test/Transforms/SROA/select-load.ll
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ entry:
%st.args = type { i32, ptr }

; A bitcasted load and a direct load of select.
define void @test_multiple_loads_select(i1 %cmp){
define void @test_multiple_loads_select(i1 %cmp) {
; CHECK-LABEL: @test_multiple_loads_select(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ADDR_I8_SROA_SPECULATED:%.*]] = select i1 [[CMP:%.*]], ptr undef, ptr undef
Expand All @@ -57,6 +57,28 @@ entry:
ret void
}

; Sanitizer will break optimization.
define void @test_multiple_loads_select_asan(i1 %cmp) sanitize_address {
; CHECK-LABEL: @test_multiple_loads_select_asan(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ADDR_I8_SROA_SPECULATED:%.*]] = select i1 [[CMP:%.*]], ptr undef, ptr undef
; CHECK-NEXT: call void @foo_i8(ptr [[ADDR_I8_SROA_SPECULATED]])
; CHECK-NEXT: [[ADDR_I32_SROA_SPECULATED:%.*]] = select i1 [[CMP]], ptr undef, ptr undef
; CHECK-NEXT: call void @foo_i32(ptr [[ADDR_I32_SROA_SPECULATED]])
; CHECK-NEXT: ret void
;
entry:
%args = alloca [2 x %st.args], align 16
%arr1 = getelementptr inbounds [2 x %st.args], ptr %args, i64 0, i64 1
%sel = select i1 %cmp, ptr %arr1, ptr %args
%addr = getelementptr inbounds %st.args, ptr %sel, i64 0, i32 1
%addr.i8 = load ptr, ptr %addr, align 8
call void @foo_i8(ptr %addr.i8)
%addr.i32 = load ptr, ptr %addr, align 8
call void @foo_i32 (ptr %addr.i32)
ret void
}

declare void @foo_i8(ptr)
declare void @foo_i32(ptr)

Expand Down

0 comments on commit 3eb7a8e

Please sign in to comment.