Index: lib/Transforms/Scalar/GVN.cpp =================================================================== --- lib/Transforms/Scalar/GVN.cpp +++ lib/Transforms/Scalar/GVN.cpp @@ -1807,6 +1807,13 @@ return false; } + // If this was a shortcut PRE, it will have been inserted at the end + // of the block; ensure that it dominates all uses of the original. + if (Instruction *R = dyn_cast(Repl)) { + if (DT->dominates(I, R)) { + R->moveBefore(I); + } + } // Remove it! patchAndReplaceAllUsesWith(I, Repl); if (MD && Repl->getType()->isPtrOrPtrVectorTy())