[llvm] 074561a - [Mem2Reg] Check that load type matches alloca type

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 8 08:16:25 PST 2022


Author: Nikita Popov
Date: 2022-02-08T17:16:15+01:00
New Revision: 074561a4a22f610d756109170285d8626c4cc3bc

URL: https://github.com/llvm/llvm-project/commit/074561a4a22f610d756109170285d8626c4cc3bc
DIFF: https://github.com/llvm/llvm-project/commit/074561a4a22f610d756109170285d8626c4cc3bc.diff

LOG: [Mem2Reg] Check that load type matches alloca type

Alloca promotion can only deal with cases where the load/store
types match the alloca type (it explicitly does not support
bitcasted load/stores).

With opaque pointers this is no longer enforced through the pointer
type, so add an explicit check.

Added: 
    llvm/test/Transforms/Mem2Reg/opaque-ptr.ll

Modified: 
    llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
index 01b433b4782a9..8d0d4be8d28c0 100644
--- a/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ b/llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -68,7 +68,7 @@ bool llvm::isAllocaPromotable(const AllocaInst *AI) {
     if (const LoadInst *LI = dyn_cast<LoadInst>(U)) {
       // Note that atomic loads can be transformed; atomic semantics do
       // not have any meaning for a local alloca.
-      if (LI->isVolatile())
+      if (LI->isVolatile() || LI->getType() != AI->getAllocatedType())
         return false;
     } else if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
       if (SI->getValueOperand() == AI ||

diff  --git a/llvm/test/Transforms/Mem2Reg/opaque-ptr.ll b/llvm/test/Transforms/Mem2Reg/opaque-ptr.ll
new file mode 100644
index 0000000000000..0eec7b6f41747
--- /dev/null
+++ b/llvm/test/Transforms/Mem2Reg/opaque-ptr.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -mem2reg -opaque-pointers < %s | FileCheck %s
+
+define i32 @test_same_type() {
+; CHECK-LABEL: @test_same_type(
+; CHECK-NEXT:    ret i32 0
+;
+  %p = alloca i32
+  store i32 0, ptr %p
+  %v = load i32, ptr %p
+  ret i32 %v
+}
+
+define i16 @test_
diff erent_type() {
+; CHECK-LABEL: @test_
diff erent_type(
+; CHECK-NEXT:    [[P:%.*]] = alloca i32, align 4
+; CHECK-NEXT:    store i32 0, ptr [[P]], align 4
+; CHECK-NEXT:    [[V:%.*]] = load i16, ptr [[P]], align 2
+; CHECK-NEXT:    ret i16 [[V]]
+;
+  %p = alloca i32
+  store i32 0, ptr %p
+  %v = load i16, ptr %p
+  ret i16 %v
+}


        


More information about the llvm-commits mailing list