[llvm] [GVN] Drop Clobber dependency if store may overwrite only the same value (PR #68322)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 9 05:59:34 PDT 2023


================
@@ -0,0 +1,91 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
+; RUN: opt < %s -passes=gvn -S | FileCheck %s
+
+define void @test_i8(ptr %a, ptr %b, ptr %c) {
+; CHECK-LABEL: define void @test_i8(
+; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]], ptr [[C:%.*]]) {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[TMP0:%.*]] = load i8, ptr [[A]], align 1
+; CHECK-NEXT:    store i8 [[TMP0]], ptr [[B]], align 1
+; CHECK-NEXT:    store i8 [[TMP0]], ptr [[C]], align 1
+; CHECK-NEXT:    ret void
+;
+entry:
+  %0 = load i8, ptr %a, align 1
+  store i8 %0, ptr %b, align 1
+  %1 = load i8, ptr %a, align 1
+  store i8 %1, ptr %c, align 1
+  ret void
+}
+
+define void @test_i32(ptr %a, ptr %b, ptr %c) {
+; CHECK-LABEL: define void @test_i32(
+; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]], ptr [[C:%.*]]) {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[TMP0:%.*]] = load i32, ptr [[A]], align 4
+; CHECK-NEXT:    store i32 [[TMP0]], ptr [[B]], align 4
+; CHECK-NEXT:    store i32 [[TMP0]], ptr [[C]], align 4
+; CHECK-NEXT:    ret void
+;
+entry:
+  %0 = load i32, ptr %a, align 4
+  store i32 %0, ptr %b, align 4
+  %1 = load i32, ptr %a, align 4
+  store i32 %1, ptr %c, align 4
+  ret void
+}
+
+define void @test_float(ptr %a, ptr %b, ptr %c) {
+; CHECK-LABEL: define void @test_float(
+; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]], ptr [[C:%.*]]) {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[TMP0:%.*]] = load float, ptr [[A]], align 4
+; CHECK-NEXT:    store float [[TMP0]], ptr [[B]], align 4
+; CHECK-NEXT:    store float [[TMP0]], ptr [[C]], align 4
+; CHECK-NEXT:    ret void
+;
+entry:
+  %0 = load float, ptr %a, align 4
+  store float %0, ptr %b, align 4
+  %1 = load float, ptr %a, align 4
+  store float %1, ptr %c, align 4
+  ret void
+}
----------------
nikic wrote:

The problematic case I had in mind here is this one:
```
define i8 @test_i1(ptr %a, ptr %b, ptr %c) {
entry:
  %0 = load i1, ptr %a, align 1
  store i1 %0, ptr %b, align 1
  %1 = load i8, ptr %a, align 1
  ret i8 %1
}
```
`store i1 %0` is going to write the same value to the low bit, but it will also write the 7 high bits (typically to zero, though this is unspecified), so it's not actually a no-op.

Though given that LangRef says

> When loading a value of a type like i20 with a size that is not an integral number of bytes, the result is undefined if the value was not originally written using a store of the same type.

this would fall under undefined behavior, so I guess it's fine.

https://github.com/llvm/llvm-project/pull/68322


More information about the llvm-commits mailing list