mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-17 21:29:20 +00:00
The meaning of getTypeSize was not clear - clarifying it is important now that we have x86 long double and arbitrary precision integers. The issue with long double is that it requires 80 bits, and this is not a multiple of its alignment. This gives a primitive type for which getTypeSize differed from getABITypeSize. For arbitrary precision integers it is even worse: there is the minimum number of bits needed to hold the type (eg: 36 for an i36), the maximum number of bits that will be overwriten when storing the type (40 bits for i36) and the ABI size (i.e. the storage size rounded up to a multiple of the alignment; 64 bits for i36). This patch removes getTypeSize (not really - it is still there but deprecated to allow for a gradual transition). Instead there is: (1) getTypeSizeInBits - a number of bits that suffices to hold all values of the type. For a primitive type, this is the minimum number of bits. For an i36 this is 36 bits. For x86 long double it is 80. This corresponds to gcc's TYPE_PRECISION. (2) getTypeStoreSizeInBits - the maximum number of bits that is written when storing the type (or read when reading it). For an i36 this is 40 bits, for an x86 long double it is 80 bits. This is the size alias analysis is interested in (getTypeStoreSize returns the number of bytes). There doesn't seem to be anything corresponding to this in gcc. (3) getABITypeSizeInBits - this is getTypeStoreSizeInBits rounded up to a multiple of the alignment. For an i36 this is 64, for an x86 long double this is 96 or 128 depending on the OS. This is the spacing between consecutive elements when you form an array out of this type (getABITypeSize returns the number of bytes). This is TYPE_SIZE in gcc. Since successive elements in a SequentialType (arrays, pointers and vectors) need to be aligned, the spacing between them will be given by getABITypeSize. This means that the size of an array is the length times the getABITypeSize. It also means that GEP computations need to use getABITypeSize when computing offsets. Furthermore, if an alloca allocates several elements at once then these too need to be aligned, so the size of the alloca has to be the number of elements multiplied by getABITypeSize. Logically speaking this doesn't have to be the case when allocating just one element, but it is simpler to also use getABITypeSize in this case. So alloca's and mallocs should use getABITypeSize. Finally, since gcc's only notion of size is that given by getABITypeSize, if you want to output assembler etc the same as gcc then getABITypeSize is the size you want. Since a store will overwrite no more than getTypeStoreSize bytes, and a read will read no more than that many bytes, this is the notion of size appropriate for alias analysis calculations. In this patch I have corrected all type size uses except some of those in ScalarReplAggregates, lib/Codegen, lib/Target (the hard cases). I will get around to auditing these too at some point, but I could do with some help. Finally, I made one change which I think wise but others might consider pointless and suboptimal: in an unpacked struct the amount of space allocated for a field is now given by the ABI size rather than getTypeStoreSize. I did this because every other place that reserves memory for a type (eg: alloca) now uses getABITypeSize, and I didn't want to make an exception for unpacked structs, i.e. I did it to make things more uniform. This only effects structs containing long doubles and arbitrary precision integers. If someone wants to pack these types more tightly they can always use a packed struct. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43620 91177308-0d34-0410-b5e6-96231b3b80d8
240 lines
8.6 KiB
C++
240 lines
8.6 KiB
C++
//===- AliasAnalysisEvaluator.cpp - Alias Analysis Accuracy Evaluator -----===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements a simple N^2 alias analysis accuracy evaluator.
|
|
// Basically, for each function in the program, it simply queries to see how the
|
|
// alias analysis implementation answers alias queries between each pair of
|
|
// pointers in the function.
|
|
//
|
|
// This is inspired and adapted from code by: Naveen Neelakantam, Francesco
|
|
// Spadini, and Wojciech Stryjewski.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Constants.h"
|
|
#include "llvm/DerivedTypes.h"
|
|
#include "llvm/Function.h"
|
|
#include "llvm/Instructions.h"
|
|
#include "llvm/Pass.h"
|
|
#include "llvm/Analysis/Passes.h"
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
#include "llvm/Assembly/Writer.h"
|
|
#include "llvm/Target/TargetData.h"
|
|
#include "llvm/Support/InstIterator.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
#include "llvm/Support/Compiler.h"
|
|
#include "llvm/Support/Streams.h"
|
|
#include <set>
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
cl::opt<bool> PrintAll("print-all-alias-modref-info", cl::ReallyHidden);
|
|
|
|
cl::opt<bool> PrintNoAlias("print-no-aliases", cl::ReallyHidden);
|
|
cl::opt<bool> PrintMayAlias("print-may-aliases", cl::ReallyHidden);
|
|
cl::opt<bool> PrintMustAlias("print-must-aliases", cl::ReallyHidden);
|
|
|
|
cl::opt<bool> PrintNoModRef("print-no-modref", cl::ReallyHidden);
|
|
cl::opt<bool> PrintMod("print-mod", cl::ReallyHidden);
|
|
cl::opt<bool> PrintRef("print-ref", cl::ReallyHidden);
|
|
cl::opt<bool> PrintModRef("print-modref", cl::ReallyHidden);
|
|
|
|
class VISIBILITY_HIDDEN AAEval : public FunctionPass {
|
|
unsigned NoAlias, MayAlias, MustAlias;
|
|
unsigned NoModRef, Mod, Ref, ModRef;
|
|
|
|
public:
|
|
static char ID; // Pass identification, replacement for typeid
|
|
AAEval() : FunctionPass((intptr_t)&ID) {}
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
AU.addRequired<AliasAnalysis>();
|
|
AU.setPreservesAll();
|
|
}
|
|
|
|
bool doInitialization(Module &M) {
|
|
NoAlias = MayAlias = MustAlias = 0;
|
|
NoModRef = Mod = Ref = ModRef = 0;
|
|
|
|
if (PrintAll) {
|
|
PrintNoAlias = PrintMayAlias = PrintMustAlias = true;
|
|
PrintNoModRef = PrintMod = PrintRef = PrintModRef = true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool runOnFunction(Function &F);
|
|
bool doFinalization(Module &M);
|
|
};
|
|
|
|
char AAEval::ID = 0;
|
|
RegisterPass<AAEval>
|
|
X("aa-eval", "Exhaustive Alias Analysis Precision Evaluator");
|
|
}
|
|
|
|
FunctionPass *llvm::createAAEvalPass() { return new AAEval(); }
|
|
|
|
static inline void PrintResults(const char *Msg, bool P, Value *V1, Value *V2,
|
|
Module *M) {
|
|
if (P) {
|
|
cerr << " " << Msg << ":\t";
|
|
WriteAsOperand(*cerr.stream(), V1, true, M) << ", ";
|
|
WriteAsOperand(*cerr.stream(), V2, true, M) << "\n";
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
PrintModRefResults(const char *Msg, bool P, Instruction *I, Value *Ptr,
|
|
Module *M) {
|
|
if (P) {
|
|
cerr << " " << Msg << ": Ptr: ";
|
|
WriteAsOperand(*cerr.stream(), Ptr, true, M);
|
|
cerr << "\t<->" << *I;
|
|
}
|
|
}
|
|
|
|
bool AAEval::runOnFunction(Function &F) {
|
|
AliasAnalysis &AA = getAnalysis<AliasAnalysis>();
|
|
|
|
const TargetData &TD = AA.getTargetData();
|
|
|
|
std::set<Value *> Pointers;
|
|
std::set<CallSite> CallSites;
|
|
|
|
for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
|
|
if (isa<PointerType>(I->getType())) // Add all pointer arguments
|
|
Pointers.insert(I);
|
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
|
|
if (isa<PointerType>(I->getType())) // Add all pointer instructions
|
|
Pointers.insert(&*I);
|
|
Instruction &Inst = *I;
|
|
User::op_iterator OI = Inst.op_begin();
|
|
if ((isa<InvokeInst>(Inst) || isa<CallInst>(Inst)) &&
|
|
isa<Function>(Inst.getOperand(0)))
|
|
++OI; // Skip actual functions for direct function calls.
|
|
for (; OI != Inst.op_end(); ++OI)
|
|
if (isa<PointerType>((*OI)->getType()) && !isa<ConstantPointerNull>(*OI))
|
|
Pointers.insert(*OI);
|
|
|
|
CallSite CS = CallSite::get(&*I);
|
|
if (CS.getInstruction()) CallSites.insert(CS);
|
|
}
|
|
|
|
if (PrintNoAlias || PrintMayAlias || PrintMustAlias ||
|
|
PrintNoModRef || PrintMod || PrintRef || PrintModRef)
|
|
cerr << "Function: " << F.getName() << ": " << Pointers.size()
|
|
<< " pointers, " << CallSites.size() << " call sites\n";
|
|
|
|
// iterate over the worklist, and run the full (n^2)/2 disambiguations
|
|
for (std::set<Value *>::iterator I1 = Pointers.begin(), E = Pointers.end();
|
|
I1 != E; ++I1) {
|
|
unsigned I1Size = 0;
|
|
const Type *I1ElTy = cast<PointerType>((*I1)->getType())->getElementType();
|
|
if (I1ElTy->isSized()) I1Size = TD.getTypeStoreSize(I1ElTy);
|
|
|
|
for (std::set<Value *>::iterator I2 = Pointers.begin(); I2 != I1; ++I2) {
|
|
unsigned I2Size = 0;
|
|
const Type *I2ElTy =cast<PointerType>((*I2)->getType())->getElementType();
|
|
if (I2ElTy->isSized()) I2Size = TD.getTypeStoreSize(I2ElTy);
|
|
|
|
switch (AA.alias(*I1, I1Size, *I2, I2Size)) {
|
|
case AliasAnalysis::NoAlias:
|
|
PrintResults("NoAlias", PrintNoAlias, *I1, *I2, F.getParent());
|
|
++NoAlias; break;
|
|
case AliasAnalysis::MayAlias:
|
|
PrintResults("MayAlias", PrintMayAlias, *I1, *I2, F.getParent());
|
|
++MayAlias; break;
|
|
case AliasAnalysis::MustAlias:
|
|
PrintResults("MustAlias", PrintMustAlias, *I1, *I2, F.getParent());
|
|
++MustAlias; break;
|
|
default:
|
|
cerr << "Unknown alias query result!\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
// Mod/ref alias analysis: compare all pairs of calls and values
|
|
for (std::set<CallSite>::iterator C = CallSites.begin(),
|
|
Ce = CallSites.end(); C != Ce; ++C) {
|
|
Instruction *I = C->getInstruction();
|
|
|
|
for (std::set<Value *>::iterator V = Pointers.begin(), Ve = Pointers.end();
|
|
V != Ve; ++V) {
|
|
unsigned Size = 0;
|
|
const Type *ElTy = cast<PointerType>((*V)->getType())->getElementType();
|
|
if (ElTy->isSized()) Size = TD.getTypeStoreSize(ElTy);
|
|
|
|
switch (AA.getModRefInfo(*C, *V, Size)) {
|
|
case AliasAnalysis::NoModRef:
|
|
PrintModRefResults("NoModRef", PrintNoModRef, I, *V, F.getParent());
|
|
++NoModRef; break;
|
|
case AliasAnalysis::Mod:
|
|
PrintModRefResults(" Mod", PrintMod, I, *V, F.getParent());
|
|
++Mod; break;
|
|
case AliasAnalysis::Ref:
|
|
PrintModRefResults(" Ref", PrintRef, I, *V, F.getParent());
|
|
++Ref; break;
|
|
case AliasAnalysis::ModRef:
|
|
PrintModRefResults(" ModRef", PrintModRef, I, *V, F.getParent());
|
|
++ModRef; break;
|
|
default:
|
|
cerr << "Unknown alias query result!\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static void PrintPercent(unsigned Num, unsigned Sum) {
|
|
cerr << "(" << Num*100ULL/Sum << "."
|
|
<< ((Num*1000ULL/Sum) % 10) << "%)\n";
|
|
}
|
|
|
|
bool AAEval::doFinalization(Module &M) {
|
|
unsigned AliasSum = NoAlias + MayAlias + MustAlias;
|
|
cerr << "===== Alias Analysis Evaluator Report =====\n";
|
|
if (AliasSum == 0) {
|
|
cerr << " Alias Analysis Evaluator Summary: No pointers!\n";
|
|
} else {
|
|
cerr << " " << AliasSum << " Total Alias Queries Performed\n";
|
|
cerr << " " << NoAlias << " no alias responses ";
|
|
PrintPercent(NoAlias, AliasSum);
|
|
cerr << " " << MayAlias << " may alias responses ";
|
|
PrintPercent(MayAlias, AliasSum);
|
|
cerr << " " << MustAlias << " must alias responses ";
|
|
PrintPercent(MustAlias, AliasSum);
|
|
cerr << " Alias Analysis Evaluator Pointer Alias Summary: "
|
|
<< NoAlias*100/AliasSum << "%/" << MayAlias*100/AliasSum << "%/"
|
|
<< MustAlias*100/AliasSum << "%\n";
|
|
}
|
|
|
|
// Display the summary for mod/ref analysis
|
|
unsigned ModRefSum = NoModRef + Mod + Ref + ModRef;
|
|
if (ModRefSum == 0) {
|
|
cerr << " Alias Analysis Mod/Ref Evaluator Summary: no mod/ref!\n";
|
|
} else {
|
|
cerr << " " << ModRefSum << " Total ModRef Queries Performed\n";
|
|
cerr << " " << NoModRef << " no mod/ref responses ";
|
|
PrintPercent(NoModRef, ModRefSum);
|
|
cerr << " " << Mod << " mod responses ";
|
|
PrintPercent(Mod, ModRefSum);
|
|
cerr << " " << Ref << " ref responses ";
|
|
PrintPercent(Ref, ModRefSum);
|
|
cerr << " " << ModRef << " mod & ref responses ";
|
|
PrintPercent(ModRef, ModRefSum);
|
|
cerr << " Alias Analysis Evaluator Mod/Ref Summary: "
|
|
<< NoModRef*100/ModRefSum << "%/" << Mod*100/ModRefSum << "%/"
|
|
<< Ref*100/ModRefSum << "%/" << ModRef*100/ModRefSum << "%\n";
|
|
}
|
|
|
|
return false;
|
|
}
|