[PATCH] D20847: BitCast infinite loop fix

Evgeny Stupachenko via llvm-commits llvm-commits at lists.llvm.org
Tue May 31 18:23:18 PDT 2016


evstupac created this revision.
evstupac added reviewers: majnemer, Carrot.
evstupac added subscribers: llvm-commits, davidxl, echristo, kbarton.
evstupac set the repository for this revision to rL LLVM.

The patch helps to avoid infinite loop while BitCast instruction combining for the case in D14596.

  A -> B cast
  PHI
  B -> A cast

While combining (iterating through worklist) LLVM compiler creates one new PHI and one new BitCast for load and store instructions.
InstCombine builder implicitly add the instructions to worklist. This potentially creates infinite loop.


Repository:
  rL LLVM

http://reviews.llvm.org/D20847

Files:
  lib/Transforms/InstCombine/InstCombineCasts.cpp

Index: lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1873,6 +1873,10 @@
       } else if (auto *LI = dyn_cast<LoadInst>(V)) {
         Builder->SetInsertPoint(OldPN->getIncomingBlock(j)->getTerminator());
         NewV = Builder->CreateBitCast(LI, DestTy);
+        // Builder->CreateBitCast implicitly add NewV to Instruction Combiner
+        // Worklist.  This potentially leads to internal loop.  Therefore
+        // do not continue analysis of newly added BitCast Instruction.
+        Worklist.Remove(cast<Instruction>(NewV));
         Worklist.Add(LI);
       } else if (auto *BCI = dyn_cast<BitCastInst>(V)) {
         NewV = BCI->getOperand(0);
@@ -1889,7 +1893,12 @@
     auto *SI = dyn_cast<StoreInst>(U);
     if (SI && SI->isSimple() && SI->getOperand(0) == PN) {
       Builder->SetInsertPoint(SI);
-      SI->setOperand(0, Builder->CreateBitCast(NewPNodes[PN], SrcTy));
+      Value *BC = Builder->CreateBitCast(NewPNodes[PN], SrcTy);
+      SI->setOperand(0, BC);
+      // Builder->CreateBitCast implicitly add BC to Instruction Combiner
+      // Worklist.  This potentially leads to internal loop.  Therefore
+      // do not continue analysis of newly added BitCast Instruction.
+      Worklist.Remove(cast<Instruction>(BC));
       Worklist.Add(SI);
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20847.59163.patch
Type: text/x-patch
Size: 1463 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160601/7583b705/attachment.bin>


More information about the llvm-commits mailing list