[PATCH] D29845: [SelectionDAG] Remove redundant stores more aggressively.

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 17 15:12:47 PST 2017


efriedma updated this revision to Diff 88969.
efriedma added a comment.

Updated to address review comments.  Updated to check that Dest has only one use, so we don't accidentally allow some illegal reordering.


Repository:
  rL LLVM

https://reviews.llvm.org/D29845

Files:
  lib/CodeGen/SelectionDAG/SelectionDAG.cpp
  test/CodeGen/ARM/redundant-store.ll


Index: test/CodeGen/ARM/redundant-store.ll
===================================================================
--- /dev/null
+++ test/CodeGen/ARM/redundant-store.ll
@@ -0,0 +1,27 @@
+; RUN: llc -mtriple=thumbv7m-eabi %s -o - | FileCheck %s
+
+; CHECK-LABEL: test1:
+; CHECK: test1_entry
+; CHECK-NEXT: ldrb r0, [r1]
+; CHECK-NEXT: bx lr
+define i8 @test1(i8* %a, i8* %b) {
+test1_entry:
+  %aa = load i8, i8* %a
+  %bb = load i8, i8* %b
+  store i8 %aa, i8* %a
+  ret i8 %bb
+}
+
+; CHECK-LABEL: test2:
+; CHECK: test2_entry
+; CHECK-NEXT: ldrh r1, [r0]
+; CHECK-NEXT: orr r1, r1, #384
+; CHECK-NEXT: strh r1, [r0]
+; CHECK-NEXT: bx lr
+define void @test2(i24* %a) {
+test2_entry:
+  %aa = load i24, i24* %a
+  %b = or i24 %aa, 384
+  store i24 %b, i24* %a
+  ret void
+}
Index: lib/CodeGen/SelectionDAG/SelectionDAG.cpp
===================================================================
--- lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -7147,20 +7147,32 @@
 /// through token factors and non-volatile loads.  In order to remain efficient,
 /// this only looks a couple of nodes in, it does not do an exhaustive search.
 bool SDValue::reachesChainWithoutSideEffects(SDValue Dest,
-                                               unsigned Depth) const {
+                                             unsigned Depth) const {
   if (*this == Dest) return true;
 
   // Don't search too deeply, we just want to be able to see through
   // TokenFactor's etc.
   if (Depth == 0) return false;
 
-  // If this is a token factor, all inputs to the TF happen in parallel.  If any
-  // of the operands of the TF does not reach dest, then we cannot do the xform.
+  // If this is a token factor, all inputs to the TF happen in parallel.
   if (getOpcode() == ISD::TokenFactor) {
-    for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
-      if (!getOperand(i).reachesChainWithoutSideEffects(Dest, Depth-1))
-        return false;
-    return true;
+    // First, try a shallow search.
+    if (is_contained((*this)->ops(), Dest)) {
+      // We found the chain we want as an operand of this TokenFactor.
+      // Essentially, we reach the chain without side-effects if we could
+      // serialize the TokenFactor into a simple chain of operations with
+      // Dest as the last operation. This is automatically true if the
+      // chain has one use: there are no other ordering constraints.
+      // If the chain has more than one use, we give up: some other
+      // use of Dest might force a side-effect between Dest and the current
+      // node.
+      return Dest.hasOneUse();
+    }
+    // Next, try a deep search: check whether every operand of the TokenFactor
+    // reaches Dest.
+    return all_of((*this)->ops(), [=](SDValue Op) {
+      return Op.reachesChainWithoutSideEffects(Dest, Depth - 1);
+    });
   }
 
   // Loads don't have side effects, look through them.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29845.88969.patch
Type: text/x-patch
Size: 2933 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170217/67ff8f76/attachment.bin>


More information about the llvm-commits mailing list