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

Shimin Cui via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 30 08:27:16 PDT 2021


scui added inline comments.


================
Comment at: llvm/lib/Transforms/IPO/GlobalOpt.cpp:738
+      } else if (auto *CE = dyn_cast<ConstantExpr>(U)) {
+        if (CE->stripPointerCasts() != P)
+          return false;
----------------
efriedma wrote:
> I'm not sure constant folding ensures that `CE->stripPointerCasts()` looks through exactly one pointer cast?
Yes, Should be  if (CE->stripPointerCasts() != V).


================
Comment at: llvm/lib/Transforms/IPO/GlobalOpt.cpp:982
+      if (CE)
+        CE->destroyConstant();
       continue;
----------------
efriedma wrote:
> A constant can have more than one use?
Good point. Will rewrite the code to consider multiple uses of ConstantExpr.


================
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.
----------------
efriedma wrote:
> scui wrote:
> > efriedma wrote:
> > > 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.
> > I think this is to make sure the malloc site for GV is only for GV.
> In case it wasn't clear, I meant, "For the SI->getOperand(0) == V part, I guess it doesn't really matter how you write it.  SI->getOperand(0) == V is essentially equivalent to VUse.getOperandNo() == 0."  
> 
> I don't follow your explanation for the `SI->getOperand(1)->stripPointerCasts() != GV` part; can you describe it in a bit more detail?
V is from malloc. For stores, V can be used in the following 3 forms:
1. store x, V
2. store V, GV
3. store V, not-GV
Case 1 is ok, as it stores through it. Case 2 is the one we care about - store to GV - we are trying to optimize this kind of malloc calls.  This check is for case 3 - the malloc value is also stored to memory other than GV - simply return false, as there is no further code currently to handle this for safe transformation.


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

https://reviews.llvm.org/D106589



More information about the llvm-commits mailing list