[PATCH] D130448: [Reassociate][NFC] Use an appropriate `dyn_cast` for `BinaryOperator`

Warren Ristow via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 24 18:42:34 PDT 2022


wristow created this revision.
wristow added reviewers: spatel, RKSimon.
Herald added a subscriber: hiraditya.
Herald added a project: All.
wristow requested review of this revision.
Herald added a project: LLVM.

In D129523 <https://reviews.llvm.org/D129523>, it was noted by @spatel that there is are some questionable naked
casts from `Instruction` to `BinaryOperator`.  Specifically, Sanjay noted
regarding `isReassociableOp`:

> Existing issue: this is awkward - we're dyn_casting to Instruction, but then we nakedly
> cast to BinaryOperator on the return. Could this cast to BO directly? There might be
> some subtlety with binop constant expressions that needs to be addressed.

Looking into this, it seems to me it's a guaranteed-safe change, and a small
improvement.  The result of the dyn_cast is checked for non-null, and when that
passes more elaborate aspects are checked before returning the desired
`BinaryOperator` on success (and `nullptr` is returned if any of the checks
fail).  Changing the cast directly to BO results in the dyn_cast returning
null in more cases, and so we skip the more elaborate checking which is
guaranteed to have either been pointless, or exposing a bug.  (The bug would
only manifest if an inappropriate `Opcode` were passed (which presumably doesn't
happen).  So I'd say the existing code isn't a problem, but this way is better.)


https://reviews.llvm.org/D130448

Files:
  llvm/lib/Transforms/Scalar/Reassociate.cpp


Index: llvm/lib/Transforms/Scalar/Reassociate.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -154,20 +154,20 @@
 /// Return true if V is an instruction of the specified opcode and if it
 /// only has one use.
 static BinaryOperator *isReassociableOp(Value *V, unsigned Opcode) {
-  auto *I = dyn_cast<Instruction>(V);
-  if (I && I->hasOneUse() && I->getOpcode() == Opcode)
-    if (!isa<FPMathOperator>(I) || hasFPAssociativeFlags(I))
-      return cast<BinaryOperator>(I);
+  BinaryOperator *BO = dyn_cast<BinaryOperator>(V);
+  if (BO && BO->hasOneUse() && BO->getOpcode() == Opcode)
+    if (!isa<FPMathOperator>(BO) || hasFPAssociativeFlags(BO))
+      return BO;
   return nullptr;
 }
 
 static BinaryOperator *isReassociableOp(Value *V, unsigned Opcode1,
                                         unsigned Opcode2) {
-  auto *I = dyn_cast<Instruction>(V);
-  if (I && I->hasOneUse() &&
-      (I->getOpcode() == Opcode1 || I->getOpcode() == Opcode2))
-    if (!isa<FPMathOperator>(I) || hasFPAssociativeFlags(I))
-      return cast<BinaryOperator>(I);
+  BinaryOperator *BO = dyn_cast<BinaryOperator>(V);
+  if (BO && BO->hasOneUse() &&
+      (BO->getOpcode() == Opcode1 || BO->getOpcode() == Opcode2))
+    if (!isa<FPMathOperator>(BO) || hasFPAssociativeFlags(BO))
+      return BO;
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D130448.447168.patch
Type: text/x-patch
Size: 1438 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220725/3216ca17/attachment.bin>


More information about the llvm-commits mailing list