[PATCH] D106589: [GlobalOpt] support ConstantExpr use of global address for OptimizeGlobalAddressOfMalloc

Eli Friedman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 23 14:07:14 PDT 2021


efriedma added inline comments.


================
Comment at: llvm/lib/Transforms/IPO/GlobalOpt.cpp:962
   // Loop over all uses of GV, processing them in turn.
-  while (!GV->use_empty()) {
-    if (StoreInst *SI = dyn_cast<StoreInst>(GV->user_back())) {
+  for (auto UI = GV->user_begin(), UE = GV->user_end(); UI != UE;) {
+    // We're likely changing the use list, so use a mutation-safe
----------------
scui wrote:
> efriedma wrote:
> > Not sure why you're changing the pattern used here.  It's a lot easier to show the `while (!GV->use_empty()) {` loop works correctly.
> I prefer to use for loops as we need add additional code to handle CE uses, but I can change back to use while loop, and add some code to remove those CE uses to ensure the loop runs correctly.
In general, I don't like the early-increment pattern on constants because it's easy to mess up if constant expressions are involved.


================
Comment at: llvm/lib/Transforms/IPO/GlobalOpt.cpp:1063
+        if (SI->getOperand(0) == V &&
+            SI->getOperand(1)->stripPointerCasts() != GV)
+          return false; // Storing the pointer itself... bad.
----------------
scui wrote:
> efriedma wrote:
> > `VUse.getOperandNo() == 0`?
> Thanks for the suggestion. This is the original code as it is, I can change it as you suggested - but looks to me no difference as SI is available.
The `SI->getOperand(1)->stripPointerCasts() != GV` seems very suspicious to me; I don't understand why we want to special-case storing the address of a global to itself.

For the `SI->getOperand(0) == V` part, I guess it doesn't really matter.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D106589/new/

https://reviews.llvm.org/D106589



More information about the llvm-commits mailing list