[llvm] r262916 - Revert "[InstCombine] Combine A->B->A BitCast"

Junmo Park via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 7 23:09:47 PST 2016


Author: flyingforyou
Date: Tue Mar  8 01:09:46 2016
New Revision: 262916

URL: http://llvm.org/viewvc/llvm-project?rev=262916&view=rev
Log:
Revert "[InstCombine] Combine A->B->A BitCast"

This reverts commit r262670 due to compile failure.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp?rev=262916&r1=262915&r2=262916&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCasts.cpp Tue Mar  8 01:09:46 2016
@@ -12,7 +12,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "InstCombineInternal.h"
-#include "llvm/ADT/SetVector.h"
 #include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/PatternMatch.h"
@@ -1787,103 +1786,6 @@ static Instruction *canonicalizeBitCastE
   return ExtractElementInst::Create(NewBC, ExtElt->getIndexOperand());
 }
 
-/// This function handles following case
-///
-///     A  ->  B    cast
-///     PHI
-///     B  ->  A    cast
-///
-/// All the related PHI nodes can be replaced by new PHI nodes with type A.
-/// The uses of \p CI can be changed to the new PHI node corresponding to \p PN.
-Instruction *InstCombiner::optimizeBitCastFromPhi(CastInst &CI, PHINode *PN) {
-  Value *Src = CI.getOperand(0);
-  Type *SrcTy = Src->getType();         // Type B
-  Type *DestTy = CI.getType();          // Type A
-
-  SmallVector<PHINode *, 4> Worklist;
-  SmallSetVector<PHINode *, 4> OldPhiNodes;
-
-  // Find all of the A->B casts and PHI nodes.
-  // We need to inpect all related PHI nodes, but PHIs can be cyclic, so
-  // OldPhiNodes is used to track all known PHI nodes, before adding a new
-  // PHI to Worklist, it is checked against and added to OldPhiNodes first.
-  Worklist.push_back(PN);
-  OldPhiNodes.insert(PN);
-  while (!Worklist.empty()) {
-    auto *OldPN = Worklist.pop_back_val();
-    for (Value *IncValue : OldPN->incoming_values()) {
-      if (isa<Constant>(IncValue))
-        continue;
-
-      if (isa<LoadInst>(IncValue)) {
-        if (IncValue->hasOneUse())
-          continue;
-        // If a LoadInst has more than one use, changing the type of loaded
-        // value may create another bitcast.
-        return nullptr;
-      }
-
-      auto *PNode = dyn_cast<PHINode>(IncValue);
-      if (PNode) {
-        if (OldPhiNodes.insert(PNode))
-          Worklist.push_back(PNode);
-        continue;
-      }
-
-      auto *BCI = dyn_cast<BitCastInst>(IncValue);
-      // We can't handle other instructions.
-      if (!BCI)
-        return nullptr;
-
-      // Verify it's a A->B cast.
-      Type *TyA = BCI->getOperand(0)->getType();
-      Type *TyB = BCI->getType();
-      if (TyA != DestTy || TyB != SrcTy)
-        return nullptr;
-    }
-  }
-
-  // For each old PHI node, create a corresponding new PHI node with a type A.
-  SmallDenseMap<PHINode *, PHINode *> NewPNodes;
-  for (auto *OldPN : OldPhiNodes) {
-    Builder->SetInsertPoint(OldPN);
-    PHINode *NewPN = Builder->CreatePHI(DestTy, OldPN->getNumOperands());
-    NewPNodes[OldPN] = NewPN;
-  }
-
-  // Fill in the operands of new PHI nodes.
-  for (auto *OldPN : OldPhiNodes) {
-    PHINode *NewPN = NewPNodes[OldPN];
-    for (unsigned j = 0, e = OldPN->getNumOperands(); j != e; ++j) {
-      Value *V = OldPN->getOperand(j);
-      Value *NewV = nullptr;
-      if (auto *C = dyn_cast<Constant>(V)) {
-        NewV = Builder->CreateBitCast(C, DestTy);
-      } else if (auto *LI = dyn_cast<LoadInst>(V)) {
-        Builder->SetInsertPoint(OldPN->getIncomingBlock(j)->getTerminator());
-        NewV = Builder->CreateBitCast(LI, DestTy);
-      } else if (auto *BCI = dyn_cast<BitCastInst>(V)) {
-        NewV = BCI->getOperand(0);
-      } else if (auto *PrevPN = dyn_cast<PHINode>(V)) {
-        NewV = NewPNodes[PrevPN];
-      }
-      assert(NewV);
-      NewPN->addIncoming(NewV, OldPN->getIncomingBlock(j));
-    }
-  }
-
-  // If there is a store with type B, change it to type A.
-  for (User *U : PN->users()) {
-    auto *SI = dyn_cast<StoreInst>(U);
-    if (SI && SI->getOperand(0) == PN) {
-      Builder->SetInsertPoint(SI);
-      SI->setOperand(0, Builder->CreateBitCast(NewPNodes[PN], SrcTy));
-    }
-  }
-
-  return replaceInstUsesWith(CI, NewPNodes[PN]);
-}
-
 Instruction *InstCombiner::visitBitCast(BitCastInst &CI) {
   // If the operands are integer typed then apply the integer transforms,
   // otherwise just apply the common ones.
@@ -2000,11 +1902,6 @@ Instruction *InstCombiner::visitBitCast(
     }
   }
 
-  // Handle the A->B->A cast, and there is an intervening PHI node.
-  if (PHINode *PN = dyn_cast<PHINode>(Src))
-    if (Instruction *I = optimizeBitCastFromPhi(CI, PN))
-      return I;
-
   if (Instruction *I = canonicalizeBitCastExtElt(CI, *this, DL))
     return I;
 

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h?rev=262916&r1=262915&r2=262916&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h Tue Mar  8 01:09:46 2016
@@ -391,7 +391,6 @@ private:
   Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
   Value *EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask);
   Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
-  Instruction *optimizeBitCastFromPhi(CastInst &CI, PHINode *PN);
 
 public:
   /// \brief Inserts an instruction \p New before instruction \p Old




More information about the llvm-commits mailing list