[PATCH] D143167: [NFCI] Cleanup processing of casts in PHITransAddr

Sergei Kachkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 2 03:50:07 PST 2023


kachkov98 created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
kachkov98 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

isSafeToSpeculativelyExecute() is always true for cast instructions, so
remove this redundant check. Also try to simplify cast in similar way as
for GEP and ADD with constant.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143167

Files:
  llvm/lib/Analysis/PHITransAddr.cpp


Index: llvm/lib/Analysis/PHITransAddr.cpp
===================================================================
--- llvm/lib/Analysis/PHITransAddr.cpp
+++ llvm/lib/Analysis/PHITransAddr.cpp
@@ -26,12 +26,7 @@
     cl::desc("Enable phi-translation of add instructions"));
 
 static bool canPHITrans(Instruction *Inst) {
-  if (isa<PHINode>(Inst) ||
-      isa<GetElementPtrInst>(Inst))
-    return true;
-
-  if (isa<CastInst>(Inst) &&
-      isSafeToSpeculativelyExecute(Inst))
+  if (isa<PHINode>(Inst) || isa<GetElementPtrInst>(Inst) || isa<CastInst>(Inst))
     return true;
 
   if (Inst->getOpcode() == Instruction::Add &&
@@ -179,7 +174,6 @@
   // operands need to be phi translated, and if so, reconstruct it.
 
   if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
-    if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
     Value *PHIIn = translateSubExpr(Cast->getOperand(0), CurBB, PredBB, DT);
     if (!PHIIn) return nullptr;
     if (PHIIn == Cast->getOperand(0))
@@ -187,10 +181,12 @@
 
     // Find an available version of this cast.
 
-    // Constants are trivial to find.
-    if (Constant *C = dyn_cast<Constant>(PHIIn))
-      return addAsInput(
-          ConstantExpr::getCast(Cast->getOpcode(), C, Cast->getType()));
+    // Try to simplify cast first.
+    if (Value *V = simplifyCastInst(Cast->getOpcode(), PHIIn, Cast->getType(),
+                                    {DL, TLI, DT, AC})) {
+      RemoveInstInputs(PHIIn, InstInputs);
+      return addAsInput(V);
+    }
 
     // Otherwise we have to see if a casted version of the incoming pointer
     // is available.  If so, we can use it, otherwise we have to fail.
@@ -371,7 +367,6 @@
 
   // Handle cast of PHI translatable value.
   if (CastInst *Cast = dyn_cast<CastInst>(Inst)) {
-    if (!isSafeToSpeculativelyExecute(Cast)) return nullptr;
     Value *OpVal = insertTranslatedSubExpr(Cast->getOperand(0), CurBB, PredBB,
                                            DT, NewInsts);
     if (!OpVal) return nullptr;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143167.494249.patch
Type: text/x-patch
Size: 2003 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230202/5b8e5e9c/attachment.bin>


More information about the llvm-commits mailing list