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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 12 11:18:43 PDT 2024


================
@@ -2555,6 +2555,37 @@ 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 MaybeFunctionReturnConstantPtr = [](const Function *F,
+                                           GlobalVariable *GV) {
+    SmallVector<const ReturnInst *, 4> Returns;
+    for (auto &BB : llvm::reverse(*F))
+      if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
+        Returns.emplace_back(Ret);
+
+    return !Returns.empty() && all_of(Returns, [&](auto *RI) {
+      return RI->getReturnValue() == GV;
+    });
+  };
+
+  auto *PrevCI = dyn_cast_or_null<CallInst>(CI->getPrevNonDebugInstruction());
----------------
dtcxzyw wrote:

This implementation is too specific :(

The following approach would be better:
```
if (Constant *V = getUniformReturnValue(CI->getCalledFunction())) {
   // replace all uses of V which are dominated by CI
}
```


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


More information about the llvm-commits mailing list