From 00dba4f734d1fd64803a3ef16bc153ba63b6a283 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Tue, 12 Feb 2008 21:15:18 +0000 Subject: Re-apply the patch to improve the optimizations of memcpy's, with several bugs fixed. This now passes PPC bootstrap. llvm-svn: 47026 --- llvm/lib/Transforms/Scalar/GVN.cpp | 80 +++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) (limited to 'llvm/lib/Transforms/Scalar/GVN.cpp') diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp index 69f06909f6c..95bb0ddfbc4 100644 --- a/llvm/lib/Transforms/Scalar/GVN.cpp +++ b/llvm/lib/Transforms/Scalar/GVN.cpp @@ -19,6 +19,7 @@ #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/Function.h" +#include "llvm/IntrinsicInst.h" #include "llvm/Instructions.h" #include "llvm/Value.h" #include "llvm/ADT/BitVector.h" @@ -736,6 +737,7 @@ namespace { SmallVector& toErase); bool processNonLocalLoad(LoadInst* L, SmallVector& toErase); + bool processMemCpy(MemCpyInst* M, SmallVector& toErase); Value *GetValueForBlock(BasicBlock *BB, LoadInst* orig, DenseMap &Phis, bool top_level = false); @@ -1044,6 +1046,79 @@ bool GVN::processLoad(LoadInst* L, return deletedLoad; } +/// processMemCpy - perform simplication of memcpy's. If we have memcpy A which +/// copies X to Y, and memcpy B which copies Y to Z, then we can rewrite B to be +/// a memcpy from X to Z (or potentially a memmove, depending on circumstances). +/// This allows later passes to remove the first memcpy altogether. +bool GVN::processMemCpy(MemCpyInst* M, + SmallVector& toErase) { + MemoryDependenceAnalysis& MD = getAnalysis(); + + // First, we have to check that the dependency is another memcpy + Instruction* dep = MD.getDependency(M); + if (dep == MemoryDependenceAnalysis::None || + dep == MemoryDependenceAnalysis::NonLocal || + !isa(dep)) + return false; + + // We can only transforms memcpy's where the dest of one is the source of the + // other + MemCpyInst* MDep = cast(dep); + if (M->getSource() != MDep->getDest()) + return false; + + // Second, the length of the memcpy's must be the same, or the preceeding one + // must be larger than the following one. + ConstantInt* C1 = dyn_cast(MDep->getLength()); + ConstantInt* C2 = dyn_cast(M->getLength()); + if (!C1 || !C2) + return false; + + uint64_t CpySize = C1->getValue().getZExtValue(); + uint64_t DepSize = C2->getValue().getZExtValue(); + + if (DepSize < CpySize) + return false; + + // Finally, we have to make sure that the dest of the second does not + // alias the source of the first + AliasAnalysis& AA = getAnalysis(); + if (AA.alias(M->getRawDest(), CpySize, MDep->getRawSource(), DepSize) != + AliasAnalysis::NoAlias) + return false; + else if (AA.alias(M->getRawDest(), CpySize, M->getRawSource(), CpySize) != + AliasAnalysis::NoAlias) + return false; + else if (AA.alias(MDep->getRawDest(), DepSize, MDep->getRawSource(), DepSize) + != AliasAnalysis::NoAlias) + return false; + + // If all checks passed, then we can transform these memcpy's + bool is32bit = M->getIntrinsicID() == Intrinsic::memcpy_i32; + Function* MemMoveFun = Intrinsic::getDeclaration( + M->getParent()->getParent()->getParent(), + is32bit ? Intrinsic::memcpy_i32 : + Intrinsic::memcpy_i64); + + std::vector args; + args.push_back(M->getRawDest()); + args.push_back(MDep->getRawSource()); + args.push_back(M->getLength()); + args.push_back(M->getAlignment()); + + CallInst* C = new CallInst(MemMoveFun, args.begin(), args.end(), "", M); + + if (MD.getDependency(C) == MDep) { + MD.dropInstruction(M); + toErase.push_back(M); + return true; + } else { + MD.removeInstruction(C); + toErase.push_back(C); + return false; + } +} + /// processInstruction - When calculating availability, handle an instruction /// by inserting it into the appropriate sets bool GVN::processInstruction(Instruction* I, @@ -1052,6 +1127,8 @@ bool GVN::processInstruction(Instruction* I, SmallVector& toErase) { if (LoadInst* L = dyn_cast(I)) { return processLoad(L, lastSeenLoad, toErase); + } else if (MemCpyInst* M = dyn_cast(I)) { + return processMemCpy(M, toErase); } unsigned num = VN.lookup_or_add(I); @@ -1161,8 +1238,9 @@ bool GVN::iterateOnFunction(Function &F) { ++BI; for (SmallVector::iterator I = toErase.begin(), - E = toErase.end(); I != E; ++I) + E = toErase.end(); I != E; ++I) { (*I)->eraseFromParent(); + } toErase.clear(); } -- cgit v1.2.3