[llvm] [GVN] Look through select/phi when determining underlying object (PR #99509)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 22 02:16:18 PDT 2024


================
@@ -6609,6 +6609,46 @@ void llvm::getUnderlyingObjects(const Value *V,
   } while (!Worklist.empty());
 }
 
+const Value *llvm::getUnderlyingObjectAggressive(const Value *V) {
+  const unsigned MaxVisited = 8;
+
+  SmallPtrSet<const Value *, 8> Visited;
+  SmallVector<const Value *, 8> Worklist;
+  Worklist.push_back(V);
+  const Value *Object = nullptr, *FirstObject = nullptr;
+  do {
+    const Value *P = Worklist.pop_back_val();
+    P = getUnderlyingObject(P);
+
+    if (!FirstObject)
+      FirstObject = P;
----------------
goldsteinn wrote:

Yeah, the capturing lambda isn't nice.
I definetly do not prefer https://github.com/llvm/llvm-project/commit/f160e9ca0887ac18e60890848a9909b62e54ffa2, that really obfuscates the whole "cant be null" aspect.

Maybe just moving the flag? I.e
```
// Cant be null
FirstObject = getUnderlying(V);
// Instead of a seperate `First` bool, you could also give `P` scope outside of the loop and do `P == nullptr` as `First`.
bool First= true;
do {
  P = First ? FirstObject : getUnderlying(Worklist.pop_back_val());
  First = false;
}
```

Its basically same special case as you had but moved around to make the non-null condition clearer.

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


More information about the llvm-commits mailing list