[PATCH] D64884: [PHINode] Preserve use-list order when removing incoming values.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 17 14:26:55 PDT 2019


fhahn created this revision.
fhahn added reviewers: efriedma, reames, rnk, dblaikie.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

Currently we remove/add all ops we copy from their use lists. We can
just swap the op to remove with the last op, which preserves the
use-lists and needs fewer copies.

But it changes the order of the PHI operands. With this change, 14 unit
tests fail. If that's no concern, I'll go ahead and update them.
Otherwise we could also do the copy with preserving the use-list order I
think.

My motivation for this change is to fix a case where we end up with
a non-deterministic use-list order after simplifycfg, which I think is
caused by this trashing of the use-lists.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D64884

Files:
  llvm/lib/IR/Instructions.cpp


Index: llvm/lib/IR/Instructions.cpp
===================================================================
--- llvm/lib/IR/Instructions.cpp
+++ llvm/lib/IR/Instructions.cpp
@@ -122,13 +122,9 @@
 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
   Value *Removed = getIncomingValue(Idx);
 
-  // Move everything after this operand down.
-  //
-  // FIXME: we could just swap with the end of the list, then erase.  However,
-  // clients might not expect this to happen.  The code as it is thrashes the
-  // use/def lists, which is kinda lame.
-  std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
-  std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
+  // Swap ops to remove with last op.
+  *(op_begin() + Idx)->swap(*std::prev(op_end()));
+  std::iter_swap(block_begin() + Idx, std::prev(block_end()));
 
   // Nuke the last value.
   Op<-1>().set(nullptr);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D64884.210418.patch
Type: text/x-patch
Size: 913 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190717/b2836766/attachment.bin>


More information about the llvm-commits mailing list