[llvm] [EarlyCSE] Add support for writeonly call CSE (PR #145474)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 24 01:15:48 PDT 2025


================
@@ -1626,14 +1626,17 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
         !(MemInst.isValid() && !MemInst.mayReadFromMemory()))
       LastStore = nullptr;
 
-    // If this is a read-only call, process it.
-    if (CallValue::canHandle(&Inst)) {
+    // If this is a read-only or write-only call, process it. Skip store
+    // MemInsts, as they will be more precisely handled lateron.
+    if (CallValue::canHandle(&Inst) &&
+        (!MemInst.isValid() || !MemInst.isStore())) {
       // If we have an available version of this call, and if it is the right
       // generation, replace this instruction.
       std::pair<Instruction *, unsigned> InVal = AvailableCalls.lookup(&Inst);
       if (InVal.first != nullptr &&
           isSameMemGeneration(InVal.second, CurrentGeneration, InVal.first,
-                              &Inst)) {
+                              &Inst) &&
+          InVal.first->mayReadFromMemory() == Inst.mayReadFromMemory()) {
----------------
nikic wrote:

I've added this to handle weird cases like the readonly_and_writeonly test, though I don't think it's strictly necessary...

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


More information about the llvm-commits mailing list