From b6c36e405899a9bc3ac526b27c6db1f44fec69d4 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Tue, 12 Feb 2008 12:08:14 +0000 Subject: [PATCH] Fix for bug 1996: optimize out loads of undef. This code basically just checks for a malloc/alloca immediately followed by a load. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@47006 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/GVN.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index 42d7a939705..69f06909f6c 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -1010,7 +1010,34 @@ bool GVN::processLoad(LoadInst* L, dep = MD.getDependency(L, dep); } } - + + if (dep != MemoryDependenceAnalysis::None && + dep != MemoryDependenceAnalysis::NonLocal && + isa(dep)) { + // Check that this load is actually from the + // allocation we found + Value* v = L->getOperand(0); + while (true) { + if (BitCastInst *BC = dyn_cast(v)) + v = BC->getOperand(0); + else if (GetElementPtrInst *GEP = dyn_cast(v)) + v = GEP->getOperand(0); + else + break; + } + if (v == dep) { + // If this load depends directly on an allocation, there isn't + // anything stored there; therefore, we can optimize this load + // to undef. + MD.removeInstruction(L); + + L->replaceAllUsesWith(UndefValue::get(L->getType())); + toErase.push_back(L); + deletedLoad = true; + NumGVNLoad++; + } + } + if (!deletedLoad) last = L;