[llvm] r201009 - [Constant Hoisting] Fix insertion point for constant materialization.

Juergen Ributzka juergen at apple.com
Fri Feb 7 16:20:49 PST 2014


Author: ributzka
Date: Fri Feb  7 18:20:49 2014
New Revision: 201009

URL: http://llvm.org/viewvc/llvm-project?rev=201009&view=rev
Log:
[Constant Hoisting] Fix insertion point for constant materialization.

The bitcast instruction during constant materialization was not placed correcly
in the presence of phi nodes. This commit fixes the insertion point to be in the
idom instead.

This fixes PR18768

Modified:
    llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp
    llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll

Modified: llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp?rev=201009&r1=201008&r2=201009&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp Fri Feb  7 18:20:49 2014
@@ -148,6 +148,8 @@ void ConstantHoisting::CollectConstant(U
     ConstantCandidate &CC = ConstantMap[C];
     CC.CumulativeCost += Cost;
     CC.Uses.push_back(U);
+    DEBUG(dbgs() << "Collect constant " << *C << " with cost " << Cost
+                 << " from " << *U << '\n');
   }
 }
 
@@ -279,6 +281,20 @@ static void CollectBasicBlocks(SmallPtrS
           BBs.insert(I->getParent());
 }
 
+/// \brief Find the instruction we should insert the constant materialization
+/// before.
+static Instruction *getMatInsertPt(Instruction *I, const DominatorTree *DT) {
+  if (!isa<PHINode>(I) && !isa<LandingPadInst>(I)) // Simple case.
+    return I;
+
+  // We can't insert directly before a phi node or landing pad. Insert before
+  // the terminator of the dominating block.
+  assert(&I->getParent()->getParent()->getEntryBlock() != I->getParent() &&
+         "PHI or landing pad in entry block!");
+  BasicBlock *IDom = DT->getNode(I->getParent())->getIDom()->getBlock();
+  return IDom->getTerminator();
+}
+
 /// \brief Find an insertion point that dominates all uses.
 Instruction *ConstantHoisting::
 FindConstantInsertionPoint(Function &F, const ConstantInfo &CI) const {
@@ -291,10 +307,10 @@ FindConstantInsertionPoint(Function &F,
        RCI != RCE; ++RCI)
     for (SmallVectorImpl<User *>::const_iterator U = RCI->Uses.begin(),
          E = RCI->Uses.end(); U != E; ++U)
-        CollectBasicBlocks(BBs, F, *U);
+      CollectBasicBlocks(BBs, F, *U);
 
   if (BBs.count(Entry))
-    return Entry->getFirstInsertionPt();
+    return getMatInsertPt(&Entry->front(), DT);
 
   while (BBs.size() >= 2) {
     BasicBlock *BB, *BB1, *BB2;
@@ -302,27 +318,14 @@ FindConstantInsertionPoint(Function &F,
     BB2 = *llvm::next(BBs.begin());
     BB = DT->findNearestCommonDominator(BB1, BB2);
     if (BB == Entry)
-      return Entry->getFirstInsertionPt();
+      return getMatInsertPt(&Entry->front(), DT);
     BBs.erase(BB1);
     BBs.erase(BB2);
     BBs.insert(BB);
   }
   assert((BBs.size() == 1) && "Expected only one element.");
-  return (*BBs.begin())->getFirstInsertionPt();
-}
-
-/// \brief Find the instruction we should insert the constant materialization
-/// before.
-static Instruction *getMatInsertPt(Instruction *I, const DominatorTree *DT) {
-  if (!isa<PHINode>(I) && !isa<LandingPadInst>(I)) // Simple case.
-    return I;
-
-  // We can't insert directly before a phi node or landing pad. Insert before
-  // the terminator of the dominating block.
-  assert(&I->getParent()->getParent()->getEntryBlock() != I->getParent() &&
-         "PHI or landing pad in entry block!");
-  BasicBlock *IDom = DT->getNode(I->getParent())->getIDom()->getBlock();
-  return IDom->getTerminator();
+  Instruction &FirstInst = (*BBs.begin())->front();
+  return getMatInsertPt(&FirstInst, DT);
 }
 
 /// \brief Emit materialization code for all rebased constants and update their

Modified: llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll?rev=201009&r1=201008&r2=201009&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll (original)
+++ llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll Fri Feb  7 18:20:49 2014
@@ -46,3 +46,25 @@ return:
 }
 
 declare void @foo(i8*)
+
+; PR18768
+define i32 @test3(i1 %c) {
+entry:
+  br i1 %c, label %if.then, label %if.end3
+
+if.then:                                          ; preds = %entry
+  br label %if.end3
+
+if.end3:                                          ; preds = %if.then, %entry
+  %d.0 = phi i32* [ inttoptr (i64 985162435264511 to i32*), %entry ], [ null, %if.then ]
+  %cmp4 = icmp eq i32* %d.0, inttoptr (i64 985162435264511 to i32*)
+  %cmp6 = icmp eq i32* %d.0, inttoptr (i64 985162418487296 to i32*)
+  %or = or i1 %cmp4, %cmp6
+  br i1 %or, label %if.then8, label %if.end9
+
+if.then8:                                         ; preds = %if.end3
+  ret i32 1
+
+if.end9:                                          ; preds = %if.then8, %if.end3
+  ret i32 undef
+}





More information about the llvm-commits mailing list