Transform memmove -> memcpy when the source is obviously constant memory.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16932 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2004-10-12 04:52:52 +00:00
parent b18dfebb3a
commit 35b9e48cfd

View File

@ -35,10 +35,8 @@
#define DEBUG_TYPE "instcombine" #define DEBUG_TYPE "instcombine"
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Instructions.h" #include "llvm/IntrinsicInst.h"
#include "llvm/Intrinsics.h"
#include "llvm/Pass.h" #include "llvm/Pass.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/GlobalVariable.h" #include "llvm/GlobalVariable.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
@ -3094,23 +3092,42 @@ Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
// CallInst simplification // CallInst simplification
// //
Instruction *InstCombiner::visitCallInst(CallInst &CI) { Instruction *InstCombiner::visitCallInst(CallInst &CI) {
// Intrinsics cannot occur in an invoke, so handle them here instead of in // Intrinsics cannot occur in an invoke, so handle them here instead of in
// visitCallSite. // visitCallSite.
if (Function *F = CI.getCalledFunction()) if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(&CI)) {
switch (F->getIntrinsicID()) { bool Changed = false;
case Intrinsic::memmove:
case Intrinsic::memcpy: // memmove/cpy/set of zero bytes is a noop.
case Intrinsic::memset: if (Constant *NumBytes = dyn_cast<Constant>(MI->getLength())) {
// memmove/cpy/set of zero bytes is a noop. if (NumBytes->isNullValue()) return EraseInstFromFunction(CI);
if (Constant *NumBytes = dyn_cast<Constant>(CI.getOperand(3))) {
if (NumBytes->isNullValue()) // FIXME: Increase alignment here.
return EraseInstFromFunction(CI);
} if (ConstantInt *CI = dyn_cast<ConstantInt>(NumBytes))
break; if (CI->getRawValue() == 1) {
default: // Replace the instruction with just byte operations. We would
break; // transform other cases to loads/stores, but we don't know if
// alignment is sufficient.
}
} }
// If we have a memmove and the source operation is a constant global,
// then the source and dest pointers can't alias, so we can change this
// into a call to memcpy.
if (MemMoveInst *MMI = dyn_cast<MemMoveInst>(MI))
if (GlobalVariable *GVSrc = dyn_cast<GlobalVariable>(MMI->getSource()))
if (GVSrc->isConstant()) {
Module *M = CI.getParent()->getParent()->getParent();
Function *MemCpy = M->getOrInsertFunction("llvm.memcpy",
CI.getCalledFunction()->getFunctionType());
CI.setOperand(0, MemCpy);
Changed = true;
}
if (Changed) return &CI;
}
return visitCallSite(&CI); return visitCallSite(&CI);
} }