From 0f53f592ad8b81bb5e28e0d0e8b9461ddfa3ae01 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 30 Nov 2010 00:01:19 +0000 Subject: [PATCH] two changes to DSE that shouldn't affect anything: 1. Don't bother trying to optimize: lifetime.end(ptr) store(ptr) as it is undefined, and therefore shouldn't exist. 2. Move the 'storing a loaded pointer' xform up, simplifying the may-aliased store code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120359 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/DeadStoreElimination.cpp | 96 +++++++------------ 1 file changed, 33 insertions(+), 63 deletions(-) diff --git a/lib/Transforms/Scalar/DeadStoreElimination.cpp b/lib/Transforms/Scalar/DeadStoreElimination.cpp index 4540ccce8c6..e1d89cb9109 100644 --- a/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -204,10 +204,10 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) { // If we find a store or a free, get its memory dependence. if (!doesClobberMemory(Inst) && !isFreeCall(Inst)) continue; - + MemDepResult InstDep = MD.getDependency(Inst); - // Ignore non-local stores. + // Ignore non-local store liveness. // FIXME: cross-block DSE would be fun. :) if (InstDep.isNonLocal()) continue; @@ -216,55 +216,7 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) { MadeChange |= handleFreeWithNonTrivialDependency(F, Inst, InstDep); continue; } - - if (!InstDep.isDef()) { - // If this is a may-aliased store that is clobbering the store value, we - // can keep searching past it for another must-aliased pointer that stores - // to the same location. For example, in: - // store -> P - // store -> Q - // store -> P - // we can remove the first store to P even though we don't know if P and Q - // alias. - if (StoreInst *SI = dyn_cast(Inst)) { - AliasAnalysis::Location Loc = - getAnalysis().getLocation(SI); - while (InstDep.isClobber() && isa(InstDep.getInst()) && - InstDep.getInst() != &BB.front()) - InstDep = MD.getPointerDependencyFrom(Loc, false, InstDep.getInst(), - &BB); - } - - // If not a definite must-alias store dependency, ignore it. If this is a - // load from the same pointer, we don't want to transform load+store into - // a noop. - if (!InstDep.isDef() || !isa(InstDep.getInst())) - continue; - } - - // If this is a store-store dependence, then the previous store is dead so - // long as this store is at least as big as it. - if (doesClobberMemory(InstDep.getInst())) { - Instruction *DepStore = InstDep.getInst(); - if (isStoreAtLeastAsWideAs(Inst, DepStore, TD) && - isElidable(DepStore)) { - // Delete the store and now-dead instructions that feed it. - DeleteDeadInstruction(DepStore); - ++NumFastStores; - MadeChange = true; - // DeleteDeadInstruction can delete the current instruction in loop - // cases, reset BBI. - BBI = Inst; - if (BBI != BB.begin()) - --BBI; - continue; - } - } - - if (!isElidable(Inst)) - continue; - // If we're storing the same value back to a pointer that we just // loaded from, then the store can be removed. if (StoreInst *SI = dyn_cast(Inst)) { @@ -287,23 +239,41 @@ bool DSE::runOnBasicBlock(BasicBlock &BB) { } } } + + if (!InstDep.isDef()) { + // If this is a may-aliased store that is clobbering the store value, we + // can keep searching past it for another must-aliased pointer that stores + // to the same location. For example, in: + // store -> P + // store -> Q + // store -> P + // we can remove the first store to P even though we don't know if P and Q + // alias. + if (StoreInst *SI = dyn_cast(Inst)) { + AliasAnalysis::Location Loc = + getAnalysis().getLocation(SI); + while (InstDep.isClobber() && isa(InstDep.getInst()) && + InstDep.getInst() != &BB.front()) + InstDep = MD.getPointerDependencyFrom(Loc, false, InstDep.getInst(), + &BB); + } + } - // If this is a lifetime end marker, we can throw away the store. - if (IntrinsicInst *II = dyn_cast(InstDep.getInst())) { - if (II->getIntrinsicID() == Intrinsic::lifetime_end) { + // If this is a store-store dependence, then the previous store is dead so + // long as this store is at least as big as it. + if (InstDep.isDef() && doesClobberMemory(InstDep.getInst())) { + Instruction *DepStore = InstDep.getInst(); + if (isStoreAtLeastAsWideAs(Inst, DepStore, TD) && isElidable(DepStore)) { // Delete the store and now-dead instructions that feed it. - // DeleteDeadInstruction can delete the current instruction. Save BBI - // in case we need it. - WeakVH NextInst(BBI); - - DeleteDeadInstruction(Inst); - - if (NextInst == 0) // Next instruction deleted. - BBI = BB.begin(); - else if (BBI != BB.begin()) // Revisit this instruction if possible. - --BBI; + DeleteDeadInstruction(DepStore); ++NumFastStores; MadeChange = true; + + // DeleteDeadInstruction can delete the current instruction in loop + // cases, reset BBI. + BBI = Inst; + if (BBI != BB.begin()) + --BBI; continue; } }