mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-11-01 15:17:25 +00:00
Reapply address space patch after fixing an issue in MemCopyOptimizer.
Added support for address spaces and added a isVolatile field to memcpy, memmove, and memset, e.g., llvm.memcpy.i32(i8*, i8*, i32, i32) -> llvm.memcpy.p0i8.p0i8.i32(i8*, i8*, i32, i32, i1) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100191 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -413,7 +413,6 @@ bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
|
||||
// interesting as a small compile-time optimization.
|
||||
Ranges.addStore(0, SI);
|
||||
|
||||
Function *MemSetF = 0;
|
||||
|
||||
// Now that we have full information about ranges, loop over the ranges and
|
||||
// emit memset's for anything big enough to be worthwhile.
|
||||
@@ -433,29 +432,40 @@ bool MemCpyOpt::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
|
||||
// memset block. This ensure that the memset is dominated by any addressing
|
||||
// instruction needed by the start of the block.
|
||||
BasicBlock::iterator InsertPt = BI;
|
||||
|
||||
if (MemSetF == 0) {
|
||||
const Type *Ty = Type::getInt64Ty(Context);
|
||||
MemSetF = Intrinsic::getDeclaration(M, Intrinsic::memset, &Ty, 1);
|
||||
}
|
||||
|
||||
|
||||
// Get the starting pointer of the block.
|
||||
StartPtr = Range.StartPtr;
|
||||
|
||||
|
||||
// Determine alignment
|
||||
unsigned Alignment = Range.Alignment;
|
||||
if (Alignment == 0) {
|
||||
const Type *EltType =
|
||||
cast<PointerType>(StartPtr->getType())->getElementType();
|
||||
Alignment = TD->getABITypeAlignment(EltType);
|
||||
}
|
||||
|
||||
// Cast the start ptr to be i8* as memset requires.
|
||||
const Type *i8Ptr = Type::getInt8PtrTy(Context);
|
||||
if (StartPtr->getType() != i8Ptr)
|
||||
const PointerType* StartPTy = cast<PointerType>(StartPtr->getType());
|
||||
const PointerType *i8Ptr = Type::getInt8PtrTy(Context,
|
||||
StartPTy->getAddressSpace());
|
||||
if (StartPTy!= i8Ptr)
|
||||
StartPtr = new BitCastInst(StartPtr, i8Ptr, StartPtr->getName(),
|
||||
InsertPt);
|
||||
|
||||
|
||||
Value *Ops[] = {
|
||||
StartPtr, ByteVal, // Start, value
|
||||
// size
|
||||
ConstantInt::get(Type::getInt64Ty(Context), Range.End-Range.Start),
|
||||
// align
|
||||
ConstantInt::get(Type::getInt32Ty(Context), Range.Alignment)
|
||||
ConstantInt::get(Type::getInt32Ty(Context), Alignment),
|
||||
// volatile
|
||||
ConstantInt::get(Type::getInt1Ty(Context), 0),
|
||||
};
|
||||
Value *C = CallInst::Create(MemSetF, Ops, Ops+4, "", InsertPt);
|
||||
const Type *Tys[] = { Ops[0]->getType(), Ops[2]->getType() };
|
||||
|
||||
Function *MemSetF = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys, 2);
|
||||
|
||||
Value *C = CallInst::Create(MemSetF, Ops, Ops+5, "", InsertPt);
|
||||
DEBUG(dbgs() << "Replace stores:\n";
|
||||
for (unsigned i = 0, e = Range.TheStores.size(); i != e; ++i)
|
||||
dbgs() << *Range.TheStores[i];
|
||||
@@ -680,16 +690,19 @@ bool MemCpyOpt::processMemCpy(MemCpyInst *M) {
|
||||
return false;
|
||||
|
||||
// If all checks passed, then we can transform these memcpy's
|
||||
const Type *Ty = M->getLength()->getType();
|
||||
const Type *ArgTys[3] = { M->getRawDest()->getType(),
|
||||
MDep->getRawSource()->getType(),
|
||||
M->getLength()->getType() };
|
||||
Function *MemCpyFun = Intrinsic::getDeclaration(
|
||||
M->getParent()->getParent()->getParent(),
|
||||
M->getIntrinsicID(), &Ty, 1);
|
||||
M->getIntrinsicID(), ArgTys, 3);
|
||||
|
||||
Value *Args[4] = {
|
||||
M->getRawDest(), MDep->getRawSource(), M->getLength(), M->getAlignmentCst()
|
||||
Value *Args[5] = {
|
||||
M->getRawDest(), MDep->getRawSource(), M->getLength(),
|
||||
M->getAlignmentCst(), M->getVolatileCst()
|
||||
};
|
||||
|
||||
CallInst *C = CallInst::Create(MemCpyFun, Args, Args+4, "", M);
|
||||
CallInst *C = CallInst::Create(MemCpyFun, Args, Args+5, "", M);
|
||||
|
||||
|
||||
// If C and M don't interfere, then this is a valid transformation. If they
|
||||
@@ -728,8 +741,10 @@ bool MemCpyOpt::processMemMove(MemMoveInst *M) {
|
||||
|
||||
// If not, then we know we can transform this.
|
||||
Module *Mod = M->getParent()->getParent()->getParent();
|
||||
const Type *Ty = M->getLength()->getType();
|
||||
M->setOperand(0, Intrinsic::getDeclaration(Mod, Intrinsic::memcpy, &Ty, 1));
|
||||
const Type *ArgTys[3] = { M->getRawDest()->getType(),
|
||||
M->getRawSource()->getType(),
|
||||
M->getLength()->getType() };
|
||||
M->setOperand(0,Intrinsic::getDeclaration(Mod, Intrinsic::memcpy, ArgTys, 3));
|
||||
|
||||
// MemDep may have over conservative information about this instruction, just
|
||||
// conservatively flush it from the cache.
|
||||
|
||||
@@ -858,8 +858,17 @@ void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst,
|
||||
EltPtr = new BitCastInst(EltPtr, BytePtrTy, EltPtr->getName(), MI);
|
||||
|
||||
// Cast the other pointer (if we have one) to BytePtrTy.
|
||||
if (OtherElt && OtherElt->getType() != BytePtrTy)
|
||||
OtherElt = new BitCastInst(OtherElt, BytePtrTy, OtherElt->getName(), MI);
|
||||
if (OtherElt && OtherElt->getType() != BytePtrTy) {
|
||||
// Preserve address space of OtherElt
|
||||
const PointerType* OtherPTy = cast<PointerType>(OtherElt->getType());
|
||||
const PointerType* PTy = cast<PointerType>(BytePtrTy);
|
||||
if (OtherPTy->getElementType() != PTy->getElementType()) {
|
||||
Type *NewOtherPTy = PointerType::get(PTy->getElementType(),
|
||||
OtherPTy->getAddressSpace());
|
||||
OtherElt = new BitCastInst(OtherElt, NewOtherPTy,
|
||||
OtherElt->getNameStr(), MI);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned EltSize = TD->getTypeAllocSize(EltTy);
|
||||
|
||||
@@ -870,17 +879,28 @@ void SROA::RewriteMemIntrinUserOfAlloca(MemIntrinsic *MI, Instruction *Inst,
|
||||
SROADest ? OtherElt : EltPtr, // Src ptr
|
||||
ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
|
||||
// Align
|
||||
ConstantInt::get(Type::getInt32Ty(MI->getContext()), OtherEltAlign)
|
||||
ConstantInt::get(Type::getInt32Ty(MI->getContext()), OtherEltAlign),
|
||||
MI->getVolatileCst()
|
||||
};
|
||||
CallInst::Create(TheFn, Ops, Ops + 4, "", MI);
|
||||
// In case we fold the address space overloaded memcpy of A to B
|
||||
// with memcpy of B to C, change the function to be a memcpy of A to C.
|
||||
const Type *Tys[] = { Ops[0]->getType(), Ops[1]->getType(),
|
||||
Ops[2]->getType() };
|
||||
Module *M = MI->getParent()->getParent()->getParent();
|
||||
TheFn = Intrinsic::getDeclaration(M, MI->getIntrinsicID(), Tys, 3);
|
||||
CallInst::Create(TheFn, Ops, Ops + 5, "", MI);
|
||||
} else {
|
||||
assert(isa<MemSetInst>(MI));
|
||||
Value *Ops[] = {
|
||||
EltPtr, MI->getOperand(2), // Dest, Value,
|
||||
ConstantInt::get(MI->getOperand(3)->getType(), EltSize), // Size
|
||||
Zero // Align
|
||||
Zero, // Align
|
||||
ConstantInt::get(Type::getInt1Ty(MI->getContext()), 0) // isVolatile
|
||||
};
|
||||
CallInst::Create(TheFn, Ops, Ops + 4, "", MI);
|
||||
const Type *Tys[] = { Ops[0]->getType(), Ops[2]->getType() };
|
||||
Module *M = MI->getParent()->getParent()->getParent();
|
||||
TheFn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys, 2);
|
||||
CallInst::Create(TheFn, Ops, Ops + 5, "", MI);
|
||||
}
|
||||
}
|
||||
DeadInsts.push_back(MI);
|
||||
|
||||
@@ -142,7 +142,8 @@ struct StrCatOpt : public LibCallOptimization {
|
||||
// We have enough information to now generate the memcpy call to do the
|
||||
// concatenation for us. Make a memcpy to copy the nul byte with align = 1.
|
||||
EmitMemCpy(CpyDst, Src,
|
||||
ConstantInt::get(TD->getIntPtrType(*Context), Len+1), 1, B, TD);
|
||||
ConstantInt::get(TD->getIntPtrType(*Context), Len+1),
|
||||
1, false, B, TD);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -383,7 +384,8 @@ struct StrCpyOpt : public LibCallOptimization {
|
||||
CI->getOperand(3), B, TD);
|
||||
else
|
||||
EmitMemCpy(Dst, Src,
|
||||
ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
|
||||
ConstantInt::get(TD->getIntPtrType(*Context), Len),
|
||||
1, false, B, TD);
|
||||
return Dst;
|
||||
}
|
||||
};
|
||||
@@ -411,8 +413,8 @@ struct StrNCpyOpt : public LibCallOptimization {
|
||||
|
||||
if (SrcLen == 0) {
|
||||
// strncpy(x, "", y) -> memset(x, '\0', y, 1)
|
||||
EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'), LenOp,
|
||||
B, TD);
|
||||
EmitMemSet(Dst, ConstantInt::get(Type::getInt8Ty(*Context), '\0'),
|
||||
LenOp, false, B, TD);
|
||||
return Dst;
|
||||
}
|
||||
|
||||
@@ -432,7 +434,8 @@ struct StrNCpyOpt : public LibCallOptimization {
|
||||
|
||||
// strncpy(x, s, c) -> memcpy(x, s, c, 1) [s and c are constant]
|
||||
EmitMemCpy(Dst, Src,
|
||||
ConstantInt::get(TD->getIntPtrType(*Context), Len), 1, B, TD);
|
||||
ConstantInt::get(TD->getIntPtrType(*Context), Len),
|
||||
1, false, B, TD);
|
||||
|
||||
return Dst;
|
||||
}
|
||||
@@ -593,7 +596,7 @@ struct MemCpyOpt : public LibCallOptimization {
|
||||
|
||||
// memcpy(x, y, n) -> llvm.memcpy(x, y, n, 1)
|
||||
EmitMemCpy(CI->getOperand(1), CI->getOperand(2),
|
||||
CI->getOperand(3), 1, B, TD);
|
||||
CI->getOperand(3), 1, false, B, TD);
|
||||
return CI->getOperand(1);
|
||||
}
|
||||
};
|
||||
@@ -615,7 +618,7 @@ struct MemMoveOpt : public LibCallOptimization {
|
||||
|
||||
// memmove(x, y, n) -> llvm.memmove(x, y, n, 1)
|
||||
EmitMemMove(CI->getOperand(1), CI->getOperand(2),
|
||||
CI->getOperand(3), 1, B, TD);
|
||||
CI->getOperand(3), 1, false, B, TD);
|
||||
return CI->getOperand(1);
|
||||
}
|
||||
};
|
||||
@@ -637,8 +640,8 @@ struct MemSetOpt : public LibCallOptimization {
|
||||
|
||||
// memset(p, v, n) -> llvm.memset(p, v, n, 1)
|
||||
Value *Val = B.CreateIntCast(CI->getOperand(2), Type::getInt8Ty(*Context),
|
||||
false);
|
||||
EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), B, TD);
|
||||
false);
|
||||
EmitMemSet(CI->getOperand(1), Val, CI->getOperand(3), false, B, TD);
|
||||
return CI->getOperand(1);
|
||||
}
|
||||
};
|
||||
@@ -999,7 +1002,7 @@ struct SPrintFOpt : public LibCallOptimization {
|
||||
// sprintf(str, fmt) -> llvm.memcpy(str, fmt, strlen(fmt)+1, 1)
|
||||
EmitMemCpy(CI->getOperand(1), CI->getOperand(2), // Copy the nul byte.
|
||||
ConstantInt::get(TD->getIntPtrType(*Context),
|
||||
FormatStr.size()+1), 1, B, TD);
|
||||
FormatStr.size()+1), 1, false, B, TD);
|
||||
return ConstantInt::get(CI->getType(), FormatStr.size());
|
||||
}
|
||||
|
||||
@@ -1013,11 +1016,11 @@ struct SPrintFOpt : public LibCallOptimization {
|
||||
// sprintf(dst, "%c", chr) --> *(i8*)dst = chr; *((i8*)dst+1) = 0
|
||||
if (!CI->getOperand(3)->getType()->isIntegerTy()) return 0;
|
||||
Value *V = B.CreateTrunc(CI->getOperand(3),
|
||||
Type::getInt8Ty(*Context), "char");
|
||||
Type::getInt8Ty(*Context), "char");
|
||||
Value *Ptr = CastToCStr(CI->getOperand(1), B);
|
||||
B.CreateStore(V, Ptr);
|
||||
Ptr = B.CreateGEP(Ptr, ConstantInt::get(Type::getInt32Ty(*Context), 1),
|
||||
"nul");
|
||||
"nul");
|
||||
B.CreateStore(Constant::getNullValue(Type::getInt8Ty(*Context)), Ptr);
|
||||
|
||||
return ConstantInt::get(CI->getType(), 1);
|
||||
@@ -1034,7 +1037,7 @@ struct SPrintFOpt : public LibCallOptimization {
|
||||
Value *IncLen = B.CreateAdd(Len,
|
||||
ConstantInt::get(Len->getType(), 1),
|
||||
"leninc");
|
||||
EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, B, TD);
|
||||
EmitMemCpy(CI->getOperand(1), CI->getOperand(3), IncLen, 1, false, B, TD);
|
||||
|
||||
// The sprintf result is the unincremented number of bytes in the string.
|
||||
return B.CreateIntCast(Len, CI->getType(), false);
|
||||
|
||||
Reference in New Issue
Block a user