[llvm] 787f86e - [GlobalOpt][Evaluator] Don't create bitcast for same type (PR52994)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 5 00:17:13 PST 2022


Author: Nikita Popov
Date: 2022-01-05T09:17:07+01:00
New Revision: 787f86e68cbd137077ebf206a5422787aa7d18c5

URL: https://github.com/llvm/llvm-project/commit/787f86e68cbd137077ebf206a5422787aa7d18c5
DIFF: https://github.com/llvm/llvm-project/commit/787f86e68cbd137077ebf206a5422787aa7d18c5.diff

LOG: [GlobalOpt][Evaluator] Don't create bitcast for same type (PR52994)

isBitOrNoopPointerCastable() returns true if the types are the
same, but it's not actually possible to create a bitcast for all
such types. The assumption seems to be that the user will omit
creating the cast in that case, as it is unnecessary.

Fixes https://github.com/llvm/llvm-project/issues/52994.

Added: 
    llvm/test/Transforms/GlobalOpt/pr52994.ll

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/Evaluator.cpp b/llvm/lib/Transforms/Utils/Evaluator.cpp
index b1d2747d80452..6725e160cd58b 100644
--- a/llvm/lib/Transforms/Utils/Evaluator.cpp
+++ b/llvm/lib/Transforms/Utils/Evaluator.cpp
@@ -192,8 +192,10 @@ bool Evaluator::MutableValue::write(Constant *V, APInt Offset,
     MV->Val = ConstantExpr::getIntToPtr(V, MVType);
   else if (Ty->isPointerTy() && MVType->isIntegerTy())
     MV->Val = ConstantExpr::getPtrToInt(V, MVType);
-  else
+  else if (Ty != MVType)
     MV->Val = ConstantExpr::getBitCast(V, MVType);
+  else
+    MV->Val = V;
   return true;
 }
 

diff  --git a/llvm/test/Transforms/GlobalOpt/pr52994.ll b/llvm/test/Transforms/GlobalOpt/pr52994.ll
new file mode 100644
index 0000000000000..079d1d1ce3e7a
--- /dev/null
+++ b/llvm/test/Transforms/GlobalOpt/pr52994.ll
@@ -0,0 +1,17 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
+; RUN: opt -S -globalopt < %s | FileCheck %s
+
+; Check that aggregate stores work correctly.
+
+ at llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @ctor, i8* null }]
+
+ at g = global { i64, i64 } poison
+
+;.
+; CHECK: @[[LLVM_GLOBAL_CTORS:[a-zA-Z0-9_$"\\.-]+]] = appending global [0 x { i32, void ()*, i8* }] zeroinitializer
+; CHECK: @[[G:[a-zA-Z0-9_$"\\.-]+]] = local_unnamed_addr global { i64, i64 } { i64 1, i64 2 }
+;.
+define internal void @ctor() {
+  store { i64, i64 } { i64 1, i64 2 }, { i64, i64 }* @g
+  ret void
+}


        


More information about the llvm-commits mailing list