Index: include/llvm/Transforms/Scalar/GVN.h =================================================================== --- include/llvm/Transforms/Scalar/GVN.h +++ include/llvm/Transforms/Scalar/GVN.h @@ -111,6 +111,7 @@ const TargetLibraryInfo *TLI; AssumptionCache *AC; SetVector DeadBlocks; + SmallSet NumberedBlocks; OptimizationRemarkEmitter *ORE; ValueTable VN; Index: lib/Transforms/Scalar/GVN.cpp =================================================================== --- lib/Transforms/Scalar/GVN.cpp +++ lib/Transforms/Scalar/GVN.cpp @@ -2053,6 +2053,11 @@ toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum)); return false; } + + // Don't PRE on blocks that have not been numbered. + if (NumberedBlocks.count(PREPred) == 0) + return false; + // We need to insert somewhere, so let's give it a shot PREInstr = CurInst->clone(); if (!performScalarPREInsertion(PREInstr, PREPred, ValNo)) { @@ -2103,7 +2108,9 @@ /// control flow patterns and attempts to perform simple PRE at the join point. bool GVN::performPRE(Function &F) { bool Changed = false; + NumberedBlocks.clear(); for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) { + NumberedBlocks.insert(CurrentBlock); // Nothing to PRE in the entry block. if (CurrentBlock == &F.getEntryBlock()) continue; @@ -2153,6 +2160,7 @@ /// Executes one iteration of GVN bool GVN::iterateOnFunction(Function &F) { cleanupGlobalSets(); + NumberedBlocks.clear(); // Top-down walk of the dominator tree bool Changed = false; @@ -2160,8 +2168,10 @@ // RPOT walks the graph in its constructor and will not be invalidated during // processBlock. ReversePostOrderTraversal RPOT(&F); - for (BasicBlock *BB : RPOT) + for (BasicBlock *BB : RPOT) { + NumberedBlocks.insert(BB); Changed |= processBlock(BB); + } return Changed; }