[llvm] r200218 - ConstantHoisting: We can't insert instructions directly in front of a PHI node.

Benjamin Kramer benny.kra at googlemail.com
Mon Jan 27 05:11:43 PST 2014


Author: d0k
Date: Mon Jan 27 07:11:43 2014
New Revision: 200218

URL: http://llvm.org/viewvc/llvm-project?rev=200218&view=rev
Log:
ConstantHoisting: We can't insert instructions directly in front of a PHI node.

Insert before the terminating instruction of the dominating block instead.

Added:
    llvm/trunk/test/Transforms/ConstantHoisting/
    llvm/trunk/test/Transforms/ConstantHoisting/X86/
    llvm/trunk/test/Transforms/ConstantHoisting/X86/lit.local.cfg
    llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp?rev=200218&r1=200217&r2=200218&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/ConstantHoisting.cpp Mon Jan 27 07:11:43 2014
@@ -311,6 +311,20 @@ FindConstantInsertionPoint(Function &F,
   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();
+}
+
 /// \brief Emit materialization code for all rebased constants and update their
 /// users.
 void ConstantHoisting::EmitBaseConstants(Function &F, User *U,
@@ -320,7 +334,7 @@ void ConstantHoisting::EmitBaseConstants
     Instruction *Mat = Base;
     if (!Offset->isNullValue()) {
       Mat = BinaryOperator::Create(Instruction::Add, Base, Offset,
-                                   "const_mat", I);
+                                   "const_mat", getMatInsertPt(I, DT));
 
       // Use the same debug location as the instruction we are about to update.
       Mat->setDebugLoc(I->getDebugLoc());
@@ -346,9 +360,10 @@ void ConstantHoisting::EmitBaseConstants
         continue;
 
       Instruction *Mat = Base;
+      Instruction *InsertBefore = getMatInsertPt(I, DT);
       if (!Offset->isNullValue()) {
         Mat = BinaryOperator::Create(Instruction::Add, Base, Offset,
-                                     "const_mat", I);
+                                     "const_mat", InsertBefore);
 
         // Use the same debug location as the instruction we are about to
         // update.
@@ -360,7 +375,7 @@ void ConstantHoisting::EmitBaseConstants
       }
       Instruction *ICE = CE->getAsInstruction();
       ICE->replaceUsesOfWith(OriginalConstant, Mat);
-      ICE->insertBefore(I);
+      ICE->insertBefore(InsertBefore);
 
       // Use the same debug location as the instruction we are about to update.
       ICE->setDebugLoc(I->getDebugLoc());

Added: llvm/trunk/test/Transforms/ConstantHoisting/X86/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstantHoisting/X86/lit.local.cfg?rev=200218&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/ConstantHoisting/X86/lit.local.cfg (added)
+++ llvm/trunk/test/Transforms/ConstantHoisting/X86/lit.local.cfg Mon Jan 27 07:11:43 2014
@@ -0,0 +1,4 @@
+targets = set(config.root.targets_to_build.split())
+if not 'X86' in targets:
+    config.unsupported = True
+

Added: llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll?rev=200218&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll (added)
+++ llvm/trunk/test/Transforms/ConstantHoisting/X86/phi.ll Mon Jan 27 07:11:43 2014
@@ -0,0 +1,48 @@
+; RUN: opt -S -consthoist < %s | FileCheck %s
+
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.9.0"
+
+; PR18626
+define i8* @test1(i1 %cmp, i64* %tmp) {
+entry:
+  call void @foo(i8* inttoptr (i64 68719476735 to i8*))
+  br i1 %cmp, label %if.end, label %return
+
+if.end:                                           ; preds = %bb1
+  call void @foo(i8* inttoptr (i64 68719476736 to i8*))
+  br label %return
+
+return:
+  %retval.0 = phi i8* [ null, %entry ], [ inttoptr (i64 68719476736 to i8*), %if.end ]
+  store i64 1172321806, i64* %tmp
+  ret i8* %retval.0
+
+; CHECK-LABEL: @test1
+; CHECK: entry:
+; CHECK: %const_mat = add i64 %const, 1
+; CHECK-NEXT: %1 = inttoptr i64 %const_mat to i8*
+; CHECK-NEXT: br i1 %cmp
+; CHECK: %retval.0 = phi i8* [ null, %entry ], [ %1, %if.end ]
+}
+
+define void @test2(i1 %cmp, i64** %tmp) {
+entry:
+  call void @foo(i8* inttoptr (i64 68719476736 to i8*))
+  br i1 %cmp, label %if.end, label %return
+
+if.end:                                           ; preds = %bb1
+  call void @foo(i8* inttoptr (i64 68719476736 to i8*))
+  br label %return
+
+return:
+  store i64* inttoptr (i64 68719476735 to i64*), i64** %tmp
+  ret void
+
+; CHECK-LABEL: @test2
+; CHECK: return:
+; CHECK-NEXT: %const_mat = add i64 %const, -1
+; CHECK-NEXT: inttoptr i64 %const_mat to i64*
+}
+
+declare void @foo(i8*)





More information about the llvm-commits mailing list