[llvm] [CGP] Undo constant propagation of pointers across calls (PR #102926)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 27 06:09:57 PDT 2024


================
@@ -2686,6 +2687,52 @@ 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) -> Constant * {
+    if (!F->getReturnType()->isPointerTy())
+      return nullptr;
+
+    Constant *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->isInterposable() && !Callee->hasDLLImportStorageClass()) {
+    if (Constant *RV = GetUniformReturnValue(Callee)) {
+      bool MadeChange = false;
+      const auto &DT = getDT(*CI->getFunction());
----------------
nikic wrote:

If we're in the same BB, you may as well skip the dominator tree and use comesBefore() instead.

https://github.com/llvm/llvm-project/pull/102926


More information about the llvm-commits mailing list