[llvm] e4e0dfb - [CGP] Undo constant propagation of pointers across calls
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 2 00:35:54 PDT 2024
Author: Antonio Frighetto
Date: 2024-09-02T09:33:23+02:00
New Revision: e4e0dfb0c24c9bcd4bef835bd6a162967f097584
URL: https://github.com/llvm/llvm-project/commit/e4e0dfb0c24c9bcd4bef835bd6a162967f097584
DIFF: https://github.com/llvm/llvm-project/commit/e4e0dfb0c24c9bcd4bef835bd6a162967f097584.diff
LOG: [CGP] Undo constant propagation of pointers across calls
It may be profitable to revert 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.
Added:
Modified:
llvm/lib/CodeGen/CodeGenPrepare.cpp
llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 271a047fc6a7b8..631cc26d6022fe 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -2677,7 +2677,8 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
}
// From here on out we're working with named functions.
- if (!CI->getCalledFunction())
+ auto *Callee = CI->getCalledFunction();
+ if (!Callee)
return false;
// Lower all default uses of _chk calls. This is very similar
@@ -2692,6 +2693,51 @@ bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
return true;
}
+ // SCCP may have propagated, among other things, 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) -> GlobalVariable * {
+ if (!F->getReturnType()->isPointerTy())
+ return nullptr;
+
+ GlobalVariable *UniformValue = nullptr;
+ for (auto &BB : *F) {
+ if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator())) {
+ if (auto *V = dyn_cast<GlobalVariable>(RI->getReturnValue())) {
+ if (!UniformValue)
+ UniformValue = V;
+ else if (V != UniformValue)
+ return nullptr;
+ } else {
+ return nullptr;
+ }
+ }
+ }
+
+ return UniformValue;
+ };
+
+ if (Callee->hasExactDefinition()) {
+ if (GlobalVariable *RV = GetUniformReturnValue(Callee)) {
+ bool MadeChange = false;
+ for (Use &U : make_early_inc_range(RV->uses())) {
+ auto *I = dyn_cast<Instruction>(U.getUser());
+ if (!I || I->getParent() != CI->getParent()) {
+ // Limit to the same basic block to avoid extending the call-site live
+ // range, which otherwise could increase register pressure.
+ continue;
+ }
+ if (CI->comesBefore(I)) {
+ U.set(CI);
+ MadeChange = true;
+ }
+ }
+
+ return MadeChange;
+ }
+ }
+
return false;
}
diff --git a/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll b/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
index 0e5bc79054d53b..51f1283a20ab27 100644
--- a/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/revert-constant-ptr-propagation-on-calls.ll
@@ -60,7 +60,7 @@ entry:
define i32 @caller_1() {
; CHECK-LABEL: @caller_1(
; CHECK-NEXT: [[GETS_PTR:%.*]] = call ptr @getS()
-; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr @g_getS)
+; CHECK-NEXT: [[GETI:%.*]] = call i32 @S_getI(ptr [[GETS_PTR]])
; CHECK-NEXT: ret i32 [[GETI]]
;
%getS_ptr = call ptr @getS()
More information about the llvm-commits
mailing list