Simplify LLVM passes.

Use a typedef to minimize difference between LLVM 3.1 and Â3.2+.
Use IRBuilder method CreateMemCpy.
This commit is contained in:
Kai Nacke
2013-12-16 15:00:00 +01:00
parent bac536a29c
commit 74630ed7f1
2 changed files with 24 additions and 60 deletions

View File

@@ -56,6 +56,10 @@
using namespace llvm;
#if LDC_LLVM_VER < 302
typedef TargetData DataLayout;
#endif
STATISTIC(NumGcToStack, "Number of calls promoted to constant-size allocas");
STATISTIC(NumToDynSize, "Number of calls promoted to dynamically-sized allocas");
STATISTIC(NumDeleted, "Number of GC calls deleted because the return value was unused");
@@ -66,11 +70,7 @@ SizeLimit("dgc2stack-size-limit", cl::init(1024), cl::Hidden,
namespace {
struct Analysis {
#if LDC_LLVM_VER >= 302
DataLayout& TD;
#else
TargetData& TD;
#endif
DataLayout& DL;
const Module& M;
CallGraph* CG;
CallGraphNode* CGNode;
@@ -151,7 +151,7 @@ namespace {
APInt Mask = APInt::getLowBitsSet(Bits, BitsLimit);
Mask.flipAllBits();
APInt KnownZero(Bits, 0), KnownOne(Bits, 0);
ComputeMaskedBits(Val, KnownZero, KnownOne, &A.TD);
ComputeMaskedBits(Val, KnownZero, KnownOne, &A.DL);
if ((KnownZero & Mask) != Mask)
return false;
@@ -171,7 +171,7 @@ namespace {
Value* TypeInfo = CS.getArgument(TypeInfoArgNr);
Ty = A.getTypeFor(TypeInfo);
if (!Ty) return false;
return A.TD.getTypeAllocSize(Ty) < SizeLimit;
return A.DL.getTypeAllocSize(Ty) < SizeLimit;
}
};
@@ -207,7 +207,7 @@ namespace {
// (set bits) inference algorithm is rather limited, this is
// useful for experimenting.
if (SizeLimit > 0) {
uint64_t ElemSize = A.TD.getTypeAllocSize(Ty);
uint64_t ElemSize = A.DL.getTypeAllocSize(Ty);
if (!isKnownLessThan(arrSize, SizeLimit / ElemSize, A))
return false;
}
@@ -237,7 +237,7 @@ namespace {
if (Initialized) {
// For now, only zero-init is supported.
uint64_t size = A.TD.getTypeStoreSize(Ty);
uint64_t size = A.DL.getTypeStoreSize(Ty);
Value* TypeSize = ConstantInt::get(arrSize->getType(), size);
// Use the original B to put initialization at the
// allocation site.
@@ -295,7 +295,7 @@ namespace {
return false;
Ty =node->getOperand(CD_BodyType)->getType();
return A.TD.getTypeAllocSize(Ty) < SizeLimit;
return A.DL.getTypeAllocSize(Ty) < SizeLimit;
}
// The default promote() should be fine.
@@ -390,11 +390,7 @@ namespace {
bool runOnFunction(Function &F);
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
#if LDC_LLVM_VER >= 302
AU.addRequired<DataLayout>();
#else
AU.addRequired<TargetData>();
#endif
AU.addRequired<DominatorTree>();
#if LDC_LLVM_VER >= 305
@@ -458,11 +454,7 @@ static bool isSafeToStackAllocate(Instruction* Alloc, Value* V, DominatorTree& D
bool GarbageCollect2Stack::runOnFunction(Function &F) {
DEBUG(errs() << "\nRunning -dgc2stack on function " << F.getName() << '\n');
#if LDC_LLVM_VER >= 302
DataLayout& TD = getAnalysis<DataLayout>();
#else
TargetData& TD = getAnalysis<TargetData>();
#endif
DataLayout& DL = getAnalysis<DataLayout>();
DominatorTree& DT = getAnalysis<DominatorTree>();
#if LDC_LLVM_VER >= 305
CallGraphWrapperPass* CGPass = getAnalysisIfAvailable<CallGraphWrapperPass>();
@@ -472,7 +464,7 @@ bool GarbageCollect2Stack::runOnFunction(Function &F) {
#endif
CallGraphNode* CGNode = CG ? (*CG)[&F] : NULL;
Analysis A = { TD, *M, CG, CGNode };
Analysis A = { DL, *M, CG, CGNode };
BasicBlock& Entry = F.getEntryBlock();

View File

@@ -45,6 +45,10 @@
using namespace llvm;
#if LDC_LLVM_VER < 302
typedef TargetData DataLayout;
#endif
STATISTIC(NumSimplified, "Number of runtime calls simplified");
STATISTIC(NumDeleted, "Number of runtime calls deleted");
@@ -59,11 +63,7 @@ namespace {
protected:
Function *Caller;
bool* Changed;
#if LDC_LLVM_VER >= 302
const DataLayout *TD;
#else
const TargetData *TD;
#endif
const DataLayout *DL;
AliasAnalysis *AA;
LLVMContext *Context;
@@ -85,16 +85,11 @@ namespace {
/// delete CI.
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)=0;
#if LDC_LLVM_VER >= 302
Value *OptimizeCall(CallInst *CI, bool& Changed, const DataLayout &TD,
Value *OptimizeCall(CallInst *CI, bool& Changed, const DataLayout &DL,
AliasAnalysis& AA, IRBuilder<> &B) {
#else
Value *OptimizeCall(CallInst *CI, bool& Changed, const TargetData &TD,
AliasAnalysis& AA, IRBuilder<> &B) {
#endif
Caller = CI->getParent()->getParent();
this->Changed = &Changed;
this->TD = &TD;
this->DL = &DL;
this->AA = &AA;
if (CI->getCalledFunction())
Context = &CI->getCalledFunction()->getContext();
@@ -112,14 +107,7 @@ Value *LibCallOptimization::CastToCStr(Value *V, IRBuilder<> &B) {
/// expects that the size has type 'intptr_t' and Dst/Src are pointers.
Value *LibCallOptimization::EmitMemCpy(Value *Dst, Value *Src, Value *Len,
unsigned Align, IRBuilder<> &B) {
Module *M = Caller->getParent();
Type* intTy = Len->getType();
Type *VoidPtrTy = PointerType::getUnqual(B.getInt8Ty());
Type *Tys[3] ={VoidPtrTy, VoidPtrTy, intTy};
Value *MemCpy = Intrinsic::getDeclaration(M, Intrinsic::memcpy, llvm::makeArrayRef(Tys, 3));
return B.CreateCall5(MemCpy, CastToCStr(Dst, B), CastToCStr(Src, B), Len,
ConstantInt::get(B.getInt32Ty(), Align), B.getFalse());
return B.CreateMemCpy(CastToCStr(Dst, B), CastToCStr(Src, B), Len, Align, false);
}
//===----------------------------------------------------------------------===//
@@ -312,18 +300,10 @@ namespace {
void InitOptimizations();
bool runOnFunction(Function &F);
#if LDC_LLVM_VER >= 302
bool runOnce(Function &F, const DataLayout& DL, AliasAnalysis& AA);
#else
bool runOnce(Function &F, const TargetData& TD, AliasAnalysis& AA);
#endif
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
#if LDC_LLVM_VER >= 302
AU.addRequired<DataLayout>();
#else
AU.addRequired<TargetData>();
#endif
AU.addRequired<AliasAnalysis>();
}
};
@@ -373,11 +353,7 @@ bool SimplifyDRuntimeCalls::runOnFunction(Function &F) {
if (Optimizations.empty())
InitOptimizations();
#if LDC_LLVM_VER >= 302
const DataLayout &TD = getAnalysis<DataLayout>();
#else
const TargetData &TD = getAnalysis<TargetData>();
#endif
const DataLayout &DL = getAnalysis<DataLayout>();
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
// Iterate to catch opportunities opened up by other optimizations,
@@ -388,18 +364,14 @@ bool SimplifyDRuntimeCalls::runOnFunction(Function &F) {
bool EverChanged = false;
bool Changed;
do {
Changed = runOnce(F, TD, AA);
Changed = runOnce(F, DL, AA);
EverChanged |= Changed;
} while (Changed);
return EverChanged;
}
#if LDC_LLVM_VER >= 302
bool SimplifyDRuntimeCalls::runOnce(Function &F, const DataLayout& TD, AliasAnalysis& AA) {
#else
bool SimplifyDRuntimeCalls::runOnce(Function &F, const TargetData& TD, AliasAnalysis& AA) {
#endif
bool SimplifyDRuntimeCalls::runOnce(Function &F, const DataLayout& DL, AliasAnalysis& AA) {
IRBuilder<> Builder(F.getContext());
bool Changed = false;
@@ -426,7 +398,7 @@ bool SimplifyDRuntimeCalls::runOnce(Function &F, const TargetData& TD, AliasAnal
Builder.SetInsertPoint(BB, I);
// Try to optimize this call.
Value *Result = OMI->second->OptimizeCall(CI, Changed, TD, AA, Builder);
Value *Result = OMI->second->OptimizeCall(CI, Changed, DL, AA, Builder);
if (Result == 0) continue;
DEBUG(errs() << "SimplifyDRuntimeCalls simplified: " << *CI;