mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-09 11:25:55 +00:00
Enhance analysis passes so that they apply the same analysis to malloc calls as to MallocInst.
Reviewed by Eli Friedman. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82281 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
#include "llvm/Analysis/AliasAnalysis.h"
|
#include "llvm/Analysis/AliasAnalysis.h"
|
||||||
#include "llvm/Analysis/CaptureTracking.h"
|
#include "llvm/Analysis/CaptureTracking.h"
|
||||||
|
#include "llvm/Analysis/MallocHelper.h"
|
||||||
#include "llvm/Analysis/Passes.h"
|
#include "llvm/Analysis/Passes.h"
|
||||||
#include "llvm/Constants.h"
|
#include "llvm/Constants.h"
|
||||||
#include "llvm/DerivedTypes.h"
|
#include "llvm/DerivedTypes.h"
|
||||||
@@ -102,7 +103,7 @@ static bool isNonEscapingLocalObject(const Value *V) {
|
|||||||
/// isObjectSmallerThan - Return true if we can prove that the object specified
|
/// isObjectSmallerThan - Return true if we can prove that the object specified
|
||||||
/// by V is smaller than Size.
|
/// by V is smaller than Size.
|
||||||
static bool isObjectSmallerThan(const Value *V, unsigned Size,
|
static bool isObjectSmallerThan(const Value *V, unsigned Size,
|
||||||
const TargetData &TD) {
|
LLVMContext &Context, const TargetData &TD) {
|
||||||
const Type *AccessTy;
|
const Type *AccessTy;
|
||||||
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
|
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
|
||||||
AccessTy = GV->getType()->getElementType();
|
AccessTy = GV->getType()->getElementType();
|
||||||
@@ -111,6 +112,12 @@ static bool isObjectSmallerThan(const Value *V, unsigned Size,
|
|||||||
AccessTy = AI->getType()->getElementType();
|
AccessTy = AI->getType()->getElementType();
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
|
} else if (const CallInst* CI = extractMallocCall(V)) {
|
||||||
|
if (!isArrayMalloc(V, Context, &TD))
|
||||||
|
// The size is the argument to the malloc call.
|
||||||
|
if (const ConstantInt* C = dyn_cast<ConstantInt>(CI->getOperand(1)))
|
||||||
|
return (C->getZExtValue() < Size);
|
||||||
|
return false;
|
||||||
} else if (const Argument *A = dyn_cast<Argument>(V)) {
|
} else if (const Argument *A = dyn_cast<Argument>(V)) {
|
||||||
if (A->hasByValAttr())
|
if (A->hasByValAttr())
|
||||||
AccessTy = cast<PointerType>(A->getType())->getElementType();
|
AccessTy = cast<PointerType>(A->getType())->getElementType();
|
||||||
@@ -340,9 +347,10 @@ BasicAliasAnalysis::alias(const Value *V1, unsigned V1Size,
|
|||||||
|
|
||||||
// If the size of one access is larger than the entire object on the other
|
// If the size of one access is larger than the entire object on the other
|
||||||
// side, then we know such behavior is undefined and can assume no alias.
|
// side, then we know such behavior is undefined and can assume no alias.
|
||||||
|
LLVMContext &Context = V1->getContext();
|
||||||
if (TD)
|
if (TD)
|
||||||
if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, *TD)) ||
|
if ((V1Size != ~0U && isObjectSmallerThan(O2, V1Size, Context, *TD)) ||
|
||||||
(V2Size != ~0U && isObjectSmallerThan(O1, V2Size, *TD)))
|
(V2Size != ~0U && isObjectSmallerThan(O1, V2Size, Context, *TD)))
|
||||||
return NoAlias;
|
return NoAlias;
|
||||||
|
|
||||||
// If one pointer is the result of a call/invoke and the other is a
|
// If one pointer is the result of a call/invoke and the other is a
|
||||||
|
@@ -64,6 +64,7 @@
|
|||||||
#include "llvm/Support/InstIterator.h"
|
#include "llvm/Support/InstIterator.h"
|
||||||
#include "llvm/Support/InstVisitor.h"
|
#include "llvm/Support/InstVisitor.h"
|
||||||
#include "llvm/Analysis/AliasAnalysis.h"
|
#include "llvm/Analysis/AliasAnalysis.h"
|
||||||
|
#include "llvm/Analysis/MallocHelper.h"
|
||||||
#include "llvm/Analysis/Passes.h"
|
#include "llvm/Analysis/Passes.h"
|
||||||
#include "llvm/Support/Debug.h"
|
#include "llvm/Support/Debug.h"
|
||||||
#include "llvm/System/Atomic.h"
|
#include "llvm/System/Atomic.h"
|
||||||
@@ -592,9 +593,12 @@ namespace {
|
|||||||
friend class InstVisitor<Andersens>;
|
friend class InstVisitor<Andersens>;
|
||||||
void visitReturnInst(ReturnInst &RI);
|
void visitReturnInst(ReturnInst &RI);
|
||||||
void visitInvokeInst(InvokeInst &II) { visitCallSite(CallSite(&II)); }
|
void visitInvokeInst(InvokeInst &II) { visitCallSite(CallSite(&II)); }
|
||||||
void visitCallInst(CallInst &CI) { visitCallSite(CallSite(&CI)); }
|
void visitCallInst(CallInst &CI) {
|
||||||
|
if (isMalloc(&CI)) visitAllocationInst(CI);
|
||||||
|
else visitCallSite(CallSite(&CI));
|
||||||
|
}
|
||||||
void visitCallSite(CallSite CS);
|
void visitCallSite(CallSite CS);
|
||||||
void visitAllocationInst(AllocationInst &AI);
|
void visitAllocationInst(Instruction &I);
|
||||||
void visitLoadInst(LoadInst &LI);
|
void visitLoadInst(LoadInst &LI);
|
||||||
void visitStoreInst(StoreInst &SI);
|
void visitStoreInst(StoreInst &SI);
|
||||||
void visitGetElementPtrInst(GetElementPtrInst &GEP);
|
void visitGetElementPtrInst(GetElementPtrInst &GEP);
|
||||||
@@ -790,6 +794,8 @@ void Andersens::IdentifyObjects(Module &M) {
|
|||||||
ValueNodes[&*II] = NumObjects++;
|
ValueNodes[&*II] = NumObjects++;
|
||||||
if (AllocationInst *AI = dyn_cast<AllocationInst>(&*II))
|
if (AllocationInst *AI = dyn_cast<AllocationInst>(&*II))
|
||||||
ObjectNodes[AI] = NumObjects++;
|
ObjectNodes[AI] = NumObjects++;
|
||||||
|
else if (isMalloc(&*II))
|
||||||
|
ObjectNodes[&*II] = NumObjects++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calls to inline asm need to be added as well because the callee isn't
|
// Calls to inline asm need to be added as well because the callee isn't
|
||||||
@@ -1161,10 +1167,10 @@ void Andersens::visitInstruction(Instruction &I) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Andersens::visitAllocationInst(AllocationInst &AI) {
|
void Andersens::visitAllocationInst(Instruction &I) {
|
||||||
unsigned ObjectIndex = getObject(&AI);
|
unsigned ObjectIndex = getObject(&I);
|
||||||
GraphNodes[ObjectIndex].setValue(&AI);
|
GraphNodes[ObjectIndex].setValue(&I);
|
||||||
Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(AI),
|
Constraints.push_back(Constraint(Constraint::AddressOf, getNodeValue(I),
|
||||||
ObjectIndex));
|
ObjectIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2813,7 +2819,7 @@ void Andersens::PrintNode(const Node *N) const {
|
|||||||
else
|
else
|
||||||
errs() << "(unnamed)";
|
errs() << "(unnamed)";
|
||||||
|
|
||||||
if (isa<GlobalValue>(V) || isa<AllocationInst>(V))
|
if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isMalloc(V))
|
||||||
if (N == &GraphNodes[getObject(V)])
|
if (N == &GraphNodes[getObject(V)])
|
||||||
errs() << "<mem>";
|
errs() << "<mem>";
|
||||||
}
|
}
|
||||||
|
@@ -23,6 +23,7 @@
|
|||||||
#include "llvm/DerivedTypes.h"
|
#include "llvm/DerivedTypes.h"
|
||||||
#include "llvm/Analysis/AliasAnalysis.h"
|
#include "llvm/Analysis/AliasAnalysis.h"
|
||||||
#include "llvm/Analysis/CallGraph.h"
|
#include "llvm/Analysis/CallGraph.h"
|
||||||
|
#include "llvm/Analysis/MallocHelper.h"
|
||||||
#include "llvm/Support/Compiler.h"
|
#include "llvm/Support/Compiler.h"
|
||||||
#include "llvm/Support/CommandLine.h"
|
#include "llvm/Support/CommandLine.h"
|
||||||
#include "llvm/Support/InstIterator.h"
|
#include "llvm/Support/InstIterator.h"
|
||||||
@@ -236,6 +237,9 @@ bool GlobalsModRef::AnalyzeUsesOfPointer(Value *V,
|
|||||||
}
|
}
|
||||||
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
|
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(*UI)) {
|
||||||
if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true;
|
if (AnalyzeUsesOfPointer(GEP, Readers, Writers)) return true;
|
||||||
|
} else if (BitCastInst *BCI = dyn_cast<BitCastInst>(*UI)) {
|
||||||
|
if (AnalyzeUsesOfPointer(BCI, Readers, Writers, OkayStoreDest))
|
||||||
|
return true;
|
||||||
} else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
|
} else if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
|
||||||
// Make sure that this is just the function being called, not that it is
|
// Make sure that this is just the function being called, not that it is
|
||||||
// passing into the function.
|
// passing into the function.
|
||||||
@@ -299,7 +303,7 @@ bool GlobalsModRef::AnalyzeIndirectGlobalMemory(GlobalValue *GV) {
|
|||||||
// Check the value being stored.
|
// Check the value being stored.
|
||||||
Value *Ptr = SI->getOperand(0)->getUnderlyingObject();
|
Value *Ptr = SI->getOperand(0)->getUnderlyingObject();
|
||||||
|
|
||||||
if (isa<MallocInst>(Ptr)) {
|
if (isa<MallocInst>(Ptr) || isMalloc(Ptr)) {
|
||||||
// Okay, easy case.
|
// Okay, easy case.
|
||||||
} else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) {
|
} else if (CallInst *CI = dyn_cast<CallInst>(Ptr)) {
|
||||||
Function *F = CI->getCalledFunction();
|
Function *F = CI->getCalledFunction();
|
||||||
@@ -435,7 +439,8 @@ void GlobalsModRef::AnalyzeCallGraph(CallGraph &CG, Module &M) {
|
|||||||
if (cast<StoreInst>(*II).isVolatile())
|
if (cast<StoreInst>(*II).isVolatile())
|
||||||
// Treat volatile stores as reading memory somewhere.
|
// Treat volatile stores as reading memory somewhere.
|
||||||
FunctionEffect |= Ref;
|
FunctionEffect |= Ref;
|
||||||
} else if (isa<MallocInst>(*II) || isa<FreeInst>(*II)) {
|
} else if (isa<MallocInst>(*II) || isa<FreeInst>(*II) ||
|
||||||
|
isMalloc(&cast<Instruction>(*II))) {
|
||||||
FunctionEffect |= ModRef;
|
FunctionEffect |= ModRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,6 +20,7 @@
|
|||||||
#include "llvm/IntrinsicInst.h"
|
#include "llvm/IntrinsicInst.h"
|
||||||
#include "llvm/Function.h"
|
#include "llvm/Function.h"
|
||||||
#include "llvm/Analysis/AliasAnalysis.h"
|
#include "llvm/Analysis/AliasAnalysis.h"
|
||||||
|
#include "llvm/Analysis/MallocHelper.h"
|
||||||
#include "llvm/ADT/Statistic.h"
|
#include "llvm/ADT/Statistic.h"
|
||||||
#include "llvm/ADT/STLExtras.h"
|
#include "llvm/ADT/STLExtras.h"
|
||||||
#include "llvm/Support/PredIteratorCache.h"
|
#include "llvm/Support/PredIteratorCache.h"
|
||||||
@@ -233,6 +234,15 @@ getPointerDependencyFrom(Value *MemPtr, uint64_t MemSize, bool isLoad,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isMalloc(Inst)) {
|
||||||
|
Value *AccessPtr = MemPtr->getUnderlyingObject();
|
||||||
|
|
||||||
|
if (AccessPtr == Inst ||
|
||||||
|
AA->alias(Inst, 1, AccessPtr, 1) == AliasAnalysis::MustAlias)
|
||||||
|
return MemDepResult::getDef(Inst);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
|
// See if this instruction (e.g. a call or vaarg) mod/ref's the pointer.
|
||||||
switch (AA->getModRefInfo(Inst, MemPtr, MemSize)) {
|
switch (AA->getModRefInfo(Inst, MemPtr, MemSize)) {
|
||||||
case AliasAnalysis::NoModRef:
|
case AliasAnalysis::NoModRef:
|
||||||
|
@@ -13,6 +13,7 @@
|
|||||||
#include "llvm/Analysis/ConstantFolding.h"
|
#include "llvm/Analysis/ConstantFolding.h"
|
||||||
#include "llvm/Analysis/Dominators.h"
|
#include "llvm/Analysis/Dominators.h"
|
||||||
#include "llvm/Analysis/LoopInfo.h"
|
#include "llvm/Analysis/LoopInfo.h"
|
||||||
|
#include "llvm/Analysis/MallocHelper.h"
|
||||||
#include "llvm/Analysis/PointerTracking.h"
|
#include "llvm/Analysis/PointerTracking.h"
|
||||||
#include "llvm/Analysis/ScalarEvolution.h"
|
#include "llvm/Analysis/ScalarEvolution.h"
|
||||||
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
||||||
@@ -99,6 +100,14 @@ const SCEV *PointerTracking::computeAllocationCount(Value *P,
|
|||||||
return SE->getSCEV(arraySize);
|
return SE->getSCEV(arraySize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (CallInst *CI = extractMallocCall(V)) {
|
||||||
|
Value *arraySize = getMallocArraySize(CI, P->getContext(), TD);
|
||||||
|
Ty = getMallocAllocatedType(CI);
|
||||||
|
if (!Ty || !arraySize) return SE->getCouldNotCompute();
|
||||||
|
// arraySize elements of type Ty.
|
||||||
|
return SE->getSCEV(arraySize);
|
||||||
|
}
|
||||||
|
|
||||||
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
|
||||||
if (GV->hasDefinitiveInitializer()) {
|
if (GV->hasDefinitiveInitializer()) {
|
||||||
Constant *C = GV->getInitializer();
|
Constant *C = GV->getInitializer();
|
||||||
|
Reference in New Issue
Block a user