[llvm] [CGP] Undo constant propagation of pointers across calls (PR #102926)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 14 13:56:23 PDT 2024
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/102926
>From e8596367f0d913beddae143d7947d3cc94de634c Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Mon, 12 Aug 2024 16:57:03 +0200
Subject: [PATCH 1/2] [CGP] Undo constant propagation of pointers across calls
It may be profitable to undo SCCP propagation of C++ static values,
if such constants are pointers, in order to avoid redundant pointer
computation, since the method returning the constant is non-removable.
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 39 ++++++++++++++
.../remove-constant-on-calls.ll | 54 +++++++++++++++++++
2 files changed, 93 insertions(+)
create mode 100644 llvm/test/Transforms/CodeGenPrepare/remove-constant-on-calls.ll
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 1fb37fb8406ef4..55712c81a2fe6d 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -2555,6 +2555,45 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
return true;
}
+ // SCCP may have propagated C++ static variables across calls. If this happens
+ // to be the case, we may want to undo it in order to avoid redundant pointer
+ // computation of the constant, as the function method returning the constant
+ // needs to be executed anyways.
+ auto GetUniformReturnValue = [](const Function *F) -> Constant * {
+ Constant *UniformValue = nullptr;
+ for (auto &BB : llvm::reverse(*F)) {
+ if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
+ if (auto *Val = dyn_cast<GlobalVariable>(Ret->getReturnValue());
+ Val && Val->getType()->isPointerTy()) {
+ if (!UniformValue)
+ UniformValue = Val;
+ else if (Val != UniformValue)
+ return nullptr;
+ } else {
+ return nullptr;
+ }
+ }
+ }
+
+ return UniformValue;
+ };
+
+ if (Constant *RV = GetUniformReturnValue(CI->getCalledFunction())) {
+ bool MadeChange = false;
+ const auto &DT = getDT(*CI->getFunction());
+ for (Use &U : RV->uses()) {
+ auto *I = dyn_cast<Instruction>(U.getUser());
+ if (!I || I->getParent() != CI->getParent())
+ continue;
+ if (DT.dominates(CI, I)) {
+ U.set(CI);
+ MadeChange = true;
+ }
+ }
+
+ return MadeChange;
+ }
+
return false;
}
diff --git a/llvm/test/Transforms/CodeGenPrepare/remove-constant-on-calls.ll b/llvm/test/Transforms/CodeGenPrepare/remove-constant-on-calls.ll
new file mode 100644
index 00000000000000..e38a22a30df056
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/remove-constant-on-calls.ll
@@ -0,0 +1,54 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' -mtriple=aarch64-none-linux-gnu < %s | FileCheck %s
+
+%struct.S = type { i8 }
+
+ at g_getS = internal global %struct.S zeroinitializer, align 1
+ at guard = internal global i64 0, align 8
+
+define nonnull align 1 dereferenceable(1) ptr @getS() personality ptr @__gxx_personality_v0 {
+entry:
+ %guard = load atomic i8, ptr @guard acquire, align 8
+ %mask = and i8 %guard, 1
+ %cond = icmp eq i8 %mask, 0
+ br i1 %cond, label %to_be_init, label %return
+
+to_be_init: ; preds = %entry
+ %is_init = call i32 @__cxa_guard_acquire(ptr @guard)
+ %cond.2 = icmp ne i32 %is_init, 0
+ br i1 %cond.2, label %ctor, label %return
+
+ctor: ; preds = %to_be_init
+ invoke void @S_ctor(ptr nonnull align 1 dereferenceable(1) @g_getS)
+ to label %continue unwind label %landing_pad
+
+continue: ; preds = %ctor
+ call void @__cxa_guard_release(ptr @guard)
+ br label %return
+
+return: ; preds = %continue, %to_be_init, %entry
+ ret ptr @g_getS
+
+landing_pad: ; preds = %ctor
+ %lp = landingpad { ptr, i32 } cleanup
+ call void @__cxa_guard_abort(ptr @guard)
+ resume { ptr, i32 } %lp
+}
+
+define i32 @getI() {
+; CHECK-LABEL: @getI(
+; CHECK-NEXT: [[GETS_PTR:%.*]] = call nonnull align 1 dereferenceable(1) ptr @getS()
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr nonnull align 1 dereferenceable(1) [[GETS_PTR]])
+; CHECK-NEXT: ret i32 [[GETI]]
+;
+ %getS_ptr = call nonnull align 1 dereferenceable(1) ptr @getS()
+ %getI = call i32 @S_getI(ptr nonnull align 1 dereferenceable(1) @g_getS)
+ ret i32 %getI
+}
+
+declare i32 @__cxa_guard_acquire(ptr)
+declare void @S_ctor(ptr nonnull align 1 dereferenceable(1))
+declare i32 @S_getI(ptr nonnull align 1 dereferenceable(1))
+declare void @__cxa_guard_abort(ptr)
+declare void @__cxa_guard_release(ptr)
+declare i32 @__gxx_personality_v0(...)
>From 76b966636309614b64a1e71d006e381077651203 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Wed, 14 Aug 2024 22:56:01 +0200
Subject: [PATCH 2/2] !fixup fix check
---
llvm/lib/CodeGen/CodeGenPrepare.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 55712c81a2fe6d..434411765a1950 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -2563,7 +2563,7 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
Constant *UniformValue = nullptr;
for (auto &BB : llvm::reverse(*F)) {
if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
- if (auto *Val = dyn_cast<GlobalVariable>(Ret->getReturnValue());
+ if (auto *Val = dyn_cast_or_null<GlobalVariable>(Ret->getReturnValue());
Val && Val->getType()->isPointerTy()) {
if (!UniformValue)
UniformValue = Val;
More information about the llvm-commits
mailing list