[ConstantFold] Check uniform value in ConstantFoldLoadFromConst()
authorNikita Popov <npopov@redhat.com>
Thu, 13 Jan 2022 13:37:20 +0000 (14:37 +0100)
committerNikita Popov <npopov@redhat.com>
Thu, 13 Jan 2022 13:40:19 +0000 (14:40 +0100)
This case is automatically handled if ConstantFoldLoadFromConstPtr()
is used. Make sure that ConstantFoldLoadFromConst() also handles it.

llvm/lib/Analysis/ConstantFolding.cpp
llvm/test/Transforms/GlobalOpt/evaluate-load-uniform.ll [new file with mode: 0644]

index 103992d..497d0ef 100644 (file)
@@ -671,9 +671,12 @@ Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty,
 
   // Try hard to fold loads from bitcasted strange and non-type-safe things.
   if (Offset.getMinSignedBits() <= 64)
-    return FoldReinterpretLoadFromConst(C, Ty, Offset.getSExtValue(), DL);
+    if (Constant *Result =
+            FoldReinterpretLoadFromConst(C, Ty, Offset.getSExtValue(), DL))
+      return Result;
 
-  return nullptr;
+  // Try an offset-independent fold of a uniform value.
+  return ConstantFoldLoadFromUniformValue(C, Ty);
 }
 
 Constant *llvm::ConstantFoldLoadFromConst(Constant *C, Type *Ty,
diff --git a/llvm/test/Transforms/GlobalOpt/evaluate-load-uniform.ll b/llvm/test/Transforms/GlobalOpt/evaluate-load-uniform.ll
new file mode 100644 (file)
index 0000000..ba434a9
--- /dev/null
@@ -0,0 +1,20 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
+; RUN: opt -S -globalopt < %s | FileCheck %s
+
+; Make sure that the load from uniform value @g1 succeeds during evaluation.
+
+@g1 = global [2 x i64*] zeroinitializer
+@g2 = global i64* inttoptr(i64 1 to i64*)
+
+@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @ctor, i8* null }]
+
+;.
+; CHECK: @[[G1:[a-zA-Z0-9_$"\\.-]+]] = local_unnamed_addr global [2 x i64*] zeroinitializer
+; CHECK: @[[G2:[a-zA-Z0-9_$"\\.-]+]] = local_unnamed_addr global i64* null
+; CHECK: @[[LLVM_GLOBAL_CTORS:[a-zA-Z0-9_$"\\.-]+]] = appending global [0 x { i32, void ()*, i8* }] zeroinitializer
+;.
+define internal void @ctor() {
+  %v = load i64*, i64** getelementptr ([2 x i64*], [2 x i64*]* @g1, i64 0, i64 1)
+  store i64* %v, i64** @g2
+  ret void
+}