From e8a290fefcbf443d7c85149f5c14a5a305aeaf29 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Wed, 1 Apr 2009 23:53:49 +0000 Subject: [PATCH] Reapply r68211, with the miscompilations it caused fixed. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@68262 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/GVN.cpp | 39 +++++++++++++++++++------ test/Transforms/GVN/condprop.ll | 52 +++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 test/Transforms/GVN/condprop.ll diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index e537986f306..d605ffb6602 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -1294,9 +1294,28 @@ bool GVN::processInstruction(Instruction *I, uint32_t nextNum = VN.getNextUnusedValueNumber(); unsigned num = VN.lookup_or_add(I); + if (BranchInst* BI = dyn_cast(I)) { + localAvail[I->getParent()]->table.insert(std::make_pair(num, I)); + + if (!BI->isConditional() || isa(BI->getCondition())) + return false; + + Value* branchCond = BI->getCondition(); + uint32_t condVN = VN.lookup_or_add(branchCond); + + BasicBlock* trueSucc = BI->getSuccessor(0); + BasicBlock* falseSucc = BI->getSuccessor(1); + + if (trueSucc->getSinglePredecessor()) + localAvail[trueSucc]->table[condVN] = ConstantInt::getTrue(); + if (falseSucc->getSinglePredecessor()) + localAvail[falseSucc]->table[condVN] = ConstantInt::getFalse(); + + return false; + // Allocations are always uniquely numbered, so we can save time and memory - // by fast failing them. - if (isa(I) || isa(I)) { + // by fast failing them. + } else if (isa(I) || isa(I)) { localAvail[I->getParent()]->table.insert(std::make_pair(num, I)); return false; } @@ -1405,18 +1424,11 @@ bool GVN::runOnFunction(Function& F) { bool GVN::processBlock(BasicBlock* BB) { - DomTreeNode* DTN = DT->getNode(BB); // FIXME: Kill off toErase by doing erasing eagerly in a helper function (and // incrementing BI before processing an instruction). SmallVector toErase; bool changed_function = false; - if (DTN->getIDom()) - localAvail[BB] = - new ValueNumberScope(localAvail[DTN->getIDom()->getBlock()]); - else - localAvail[BB] = new ValueNumberScope(0); - for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { changed_function |= processInstruction(BI, toErase); @@ -1607,6 +1619,15 @@ bool GVN::performPRE(Function& F) { bool GVN::iterateOnFunction(Function &F) { cleanupGlobalSets(); + for (df_iterator DI = df_begin(DT->getRootNode()), + DE = df_end(DT->getRootNode()); DI != DE; ++DI) { + if (DI->getIDom()) + localAvail[DI->getBlock()] = + new ValueNumberScope(localAvail[DI->getIDom()->getBlock()]); + else + localAvail[DI->getBlock()] = new ValueNumberScope(0); + } + // Top-down walk of the dominator tree bool changed = false; #if 0 diff --git a/test/Transforms/GVN/condprop.ll b/test/Transforms/GVN/condprop.ll new file mode 100644 index 00000000000..53cbb509fcb --- /dev/null +++ b/test/Transforms/GVN/condprop.ll @@ -0,0 +1,52 @@ +; RUN: llvm-as < %s | opt -gvn | llvm-dis | grep {br i1 false} + +@a = external global i32 ; [#uses=7] + +define i32 @foo() nounwind { +entry: + %0 = load i32* @a, align 4 ; [#uses=1] + %1 = icmp eq i32 %0, 4 ; [#uses=1] + br i1 %1, label %bb, label %bb1 + +bb: ; preds = %entry + br label %bb8 + +bb1: ; preds = %entry + %2 = load i32* @a, align 4 ; [#uses=1] + %3 = icmp eq i32 %2, 5 ; [#uses=1] + br i1 %3, label %bb2, label %bb3 + +bb2: ; preds = %bb1 + br label %bb8 + +bb3: ; preds = %bb1 + %4 = load i32* @a, align 4 ; [#uses=1] + %5 = icmp eq i32 %4, 4 ; [#uses=1] + br i1 %5, label %bb4, label %bb5 + +bb4: ; preds = %bb3 + %6 = load i32* @a, align 4 ; [#uses=1] + %7 = add i32 %6, 5 ; [#uses=1] + br label %bb8 + +bb5: ; preds = %bb3 + %8 = load i32* @a, align 4 ; [#uses=1] + %9 = icmp eq i32 %8, 5 ; [#uses=1] + br i1 %9, label %bb6, label %bb7 + +bb6: ; preds = %bb5 + %10 = load i32* @a, align 4 ; [#uses=1] + %11 = add i32 %10, 4 ; [#uses=1] + br label %bb8 + +bb7: ; preds = %bb5 + %12 = load i32* @a, align 4 ; [#uses=1] + br label %bb8 + +bb8: ; preds = %bb7, %bb6, %bb4, %bb2, %bb + %.0 = phi i32 [ %12, %bb7 ], [ %11, %bb6 ], [ %7, %bb4 ], [ 4, %bb2 ], [ 5, %bb ] ; [#uses=1] + br label %return + +return: ; preds = %bb8 + ret i32 %.0 +}