[llvm-commits] [poolalloc] r48603 - in /poolalloc/trunk: include/dsa/DataStructure.h include/poolalloc/PoolAllocate.h lib/DSA/EquivClassGraphs.cpp lib/DSA/Makefile lib/Makefile lib/PoolAllocate/Makefile lib/PoolAllocate/PASimple.cpp lib/PoolAllocate/PoolAllocate.cpp
Andrew Lenharth
alenhar2 at cs.uiuc.edu
Thu Mar 20 10:35:55 PDT 2008
Author: alenhar2
Date: Thu Mar 20 12:35:55 2008
New Revision: 48603
URL: http://llvm.org/viewvc/llvm-project?rev=48603&view=rev
Log:
some cleanup and a simple DSA-free PA to test other stuff with
Added:
poolalloc/trunk/lib/PoolAllocate/PASimple.cpp
Modified:
poolalloc/trunk/include/dsa/DataStructure.h
poolalloc/trunk/include/poolalloc/PoolAllocate.h
poolalloc/trunk/lib/DSA/EquivClassGraphs.cpp
poolalloc/trunk/lib/DSA/Makefile
poolalloc/trunk/lib/Makefile
poolalloc/trunk/lib/PoolAllocate/Makefile
poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
Modified: poolalloc/trunk/include/dsa/DataStructure.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/dsa/DataStructure.h?rev=48603&r1=48602&r2=48603&view=diff
==============================================================================
--- poolalloc/trunk/include/dsa/DataStructure.h (original)
+++ poolalloc/trunk/include/dsa/DataStructure.h Thu Mar 20 12:35:55 2008
@@ -364,7 +364,7 @@
public:
static char ID;
- EquivClassGraphs() : ModulePass((intptr_t)&ID) {}
+ EquivClassGraphs();
/// EquivClassGraphs - Computes the equivalence classes and then the
/// folded DS graphs for each class.
Modified: poolalloc/trunk/include/poolalloc/PoolAllocate.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/poolalloc/PoolAllocate.h?rev=48603&r1=48602&r2=48603&view=diff
==============================================================================
--- poolalloc/trunk/include/poolalloc/PoolAllocate.h (original)
+++ poolalloc/trunk/include/poolalloc/PoolAllocate.h Thu Mar 20 12:35:55 2008
@@ -155,7 +155,7 @@
PoolAllocate(bool passAllArguments = false, intptr_t IDp = (intptr_t) (&ID))
: ModulePass((intptr_t)IDp), PassAllArguments(passAllArguments) {}
#endif
- bool runOnModule(Module &M);
+ virtual bool runOnModule(Module &M);
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
@@ -216,13 +216,15 @@
#endif
}
- private:
+protected:
/// AddPoolPrototypes - Add prototypes for the pool functions to the
/// specified module and update the Pool* instance variables to point to
/// them.
///
- void AddPoolPrototypes();
+ void AddPoolPrototypes(Module*);
+
+ private:
/// MicroOptimizePoolCalls - Apply any microoptimizations to calls to pool
/// allocation function calls that we can.
Modified: poolalloc/trunk/lib/DSA/EquivClassGraphs.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/DSA/EquivClassGraphs.cpp?rev=48603&r1=48602&r2=48603&view=diff
==============================================================================
--- poolalloc/trunk/lib/DSA/EquivClassGraphs.cpp (original)
+++ poolalloc/trunk/lib/DSA/EquivClassGraphs.cpp Thu Mar 20 12:35:55 2008
@@ -37,7 +37,9 @@
"Number of graphs inlined");
}
-char EquivClassGraphs::ID;
+char EquivClassGraphs::ID = 0;
+
+EquivClassGraphs::EquivClassGraphs() : ModulePass((intptr_t)&ID) {}
#ifndef NDEBUG
template<typename GT>
Modified: poolalloc/trunk/lib/DSA/Makefile
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/DSA/Makefile?rev=48603&r1=48602&r2=48603&view=diff
==============================================================================
--- poolalloc/trunk/lib/DSA/Makefile (original)
+++ poolalloc/trunk/lib/DSA/Makefile Thu Mar 20 12:35:55 2008
@@ -11,5 +11,7 @@
SHARED_LIBRARY=1
LIBRARYNAME = LLVMDataStructure
+CFlags += -fPIC
+
include $(LEVEL)/Makefile.common
Modified: poolalloc/trunk/lib/Makefile
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/Makefile?rev=48603&r1=48602&r2=48603&view=diff
==============================================================================
--- poolalloc/trunk/lib/Makefile (original)
+++ poolalloc/trunk/lib/Makefile Thu Mar 20 12:35:55 2008
@@ -6,6 +6,6 @@
#
# List all of the subdirectories that we will compile.
#
-PARALLEL_DIRS=DSA PoolAllocate
+DIRS=DSA PoolAllocate
include $(LEVEL)/Makefile.common
Modified: poolalloc/trunk/lib/PoolAllocate/Makefile
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/Makefile?rev=48603&r1=48602&r2=48603&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/Makefile (original)
+++ poolalloc/trunk/lib/PoolAllocate/Makefile Thu Mar 20 12:35:55 2008
@@ -14,3 +14,6 @@
#
include $(LEVEL)/Makefile.common
+LDFLAGS += -lLLVMDataStructure -r $(LibDir)
+CFLAGS += -fPIC
+
Added: poolalloc/trunk/lib/PoolAllocate/PASimple.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PASimple.cpp?rev=48603&view=auto
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PASimple.cpp (added)
+++ poolalloc/trunk/lib/PoolAllocate/PASimple.cpp Thu Mar 20 12:35:55 2008
@@ -0,0 +1,166 @@
+//===-- PASimple.cpp - Simple Pool Allocation Pass ------------------------===//
+//
+// 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.
+//
+//===----------------------------------------------------------------------===//
+//
+// A minimal poolallocator that assignes all allocation to one common global pool
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "poolalloc"
+
+#include "dsa/DataStructure.h"
+#include "dsa/DSGraph.h"
+#include "dsa/CallTargets.h"
+#include "poolalloc/PoolAllocate.h"
+#include "Heuristic.h"
+#include "llvm/Constants.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Instructions.h"
+#include "llvm/Module.h"
+#include "llvm/Constants.h"
+#include "llvm/ParameterAttributes.h"
+#include "llvm/Support/CFG.h"
+#include "llvm/Target/TargetData.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
+#include "llvm/Transforms/Utils/Cloning.h"
+#include "llvm/ADT/DepthFirstIterator.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/Timer.h"
+
+#include <iostream>
+
+using namespace llvm;
+using namespace PA;
+#ifdef SAFECODE
+using namespace CUA;
+#endif
+
+namespace {
+ class PoolAllocateSimple : public PoolAllocate {
+ GlobalValue* TheGlobalPool;
+ public:
+ static char ID;
+ PoolAllocateSimple() : PoolAllocate(false, (intptr_t)&ID) {}
+ void getAnalysisUsage(AnalysisUsage &AU) const;
+ bool runOnModule(Module &M);
+ GlobalVariable *CreateGlobalPool(unsigned RecSize, unsigned Align,
+ Instruction *IPHint, Module& M);
+ void ProcessFunctionBodySimple(Function& F);
+
+ };
+
+ char PoolAllocateSimple::ID = 0;
+
+ RegisterPass<PoolAllocateSimple>
+ X("poolalloc-simple", "Pool allocate everything togeather");
+
+}
+
+void PoolAllocateSimple::getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.addRequired<TargetData>();
+}
+
+bool PoolAllocateSimple::runOnModule(Module &M) {
+ if (M.begin() == M.end()) return false;
+
+ // Add the pool* prototypes to the module
+ AddPoolPrototypes(&M);
+
+ // Get the main function to insert the poolinit calls.
+ Function *MainFunc = M.getFunction("main");
+ if (MainFunc == 0 || MainFunc->isDeclaration()) {
+ std::cerr << "Cannot pool allocate this program: it has global "
+ << "pools but no 'main' function yet!\n";
+ return true;
+ }
+
+ TheGlobalPool = CreateGlobalPool(1, 1, MainFunc->getEntryBlock().begin(), M);
+
+
+ // Now that all call targets are available, rewrite the function bodies of the
+ // clones.
+ for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
+ if (!I->isDeclaration())
+ ProcessFunctionBodySimple(*I);
+
+ return true;
+}
+
+void PoolAllocateSimple::ProcessFunctionBodySimple(Function& F) {
+ std::vector<Instruction*> toDelete;
+ std::vector<ReturnInst*> Returns;
+ std::vector<Instruction*> ToFree;
+ for (Function::iterator i = F.begin(), e = F.end(); i != e; ++i)
+ for (BasicBlock::iterator ii = i->begin(), ee = i->end(); ii != ee; ++ii) {
+ if (isa<MallocInst>(ii)) {
+ toDelete.push_back(ii);
+ //Fixme: fixup size
+ Value* args[] = {TheGlobalPool, ii->getOperand(0)};
+ Instruction* x = new CallInst(PoolAlloc, &args[0], &args[2], "", ii);
+ ii->replaceAllUsesWith(CastInst::createPointerCast(x, ii->getType(), "", ii));
+ } else if (isa<AllocaInst>(ii)) {
+ toDelete.push_back(ii);
+ //Fixme: fixup size
+ Value* args[] = {TheGlobalPool, ii->getOperand(0)};
+ Instruction* x = new CallInst(PoolAlloc, &args[0], &args[2], "", ii);
+ ToFree.push_back(x);
+ ii->replaceAllUsesWith(CastInst::createPointerCast(x, ii->getType(), "", ii));
+ } else if (isa<FreeInst>(ii)) {
+ toDelete.push_back(ii);
+ Value* args[] = {TheGlobalPool, ii->getOperand(0)};
+ new CallInst(PoolFree, &args[0], &args[2], "", ii);
+ } else if (isa<ReturnInst>(ii)) {
+ Returns.push_back(cast<ReturnInst>(ii));
+ }
+ }
+
+ //add frees at each return for the allocas
+ for (std::vector<ReturnInst*>::iterator i = Returns.begin(), e = Returns.end();
+ i != e; ++i)
+ for (std::vector<Instruction*>::iterator ii = ToFree.begin(), ee = ToFree.end();
+ ii != ee; ++ii) {
+ Value* args[] = {TheGlobalPool, *ii};
+ new CallInst(PoolFree, &args[0], &args[2], "", *i);
+ }
+
+ //delete malloc and alloca insts
+ for (unsigned x = 0; x < toDelete.size(); ++x)
+ toDelete[x]->eraseFromParent();
+}
+
+/// CreateGlobalPool - Create a global pool descriptor object, and insert a
+/// poolinit for it into main. IPHint is an instruction that we should insert
+/// the poolinit before if not null.
+GlobalVariable *PoolAllocateSimple::CreateGlobalPool(unsigned RecSize, unsigned Align,
+ Instruction *IPHint, Module& M) {
+ GlobalVariable *GV =
+ new GlobalVariable(getPoolType(), false, GlobalValue::InternalLinkage,
+ Constant::getNullValue(getPoolType()), "GlobalPool",
+ &M);
+
+ Function *MainFunc = M.getFunction("main");
+ assert(MainFunc && "No main in program??");
+
+ BasicBlock::iterator InsertPt;
+ if (IPHint)
+ InsertPt = IPHint;
+ else {
+ InsertPt = MainFunc->getEntryBlock().begin();
+ while (isa<AllocaInst>(InsertPt)) ++InsertPt;
+ }
+
+ Value *ElSize = ConstantInt::get(Type::Int32Ty, RecSize);
+ Value *AlignV = ConstantInt::get(Type::Int32Ty, Align);
+ Value* Opts[3] = {GV, ElSize, AlignV};
+ new CallInst(PoolInit, Opts, Opts + 3, "", InsertPt);
+ return GV;
+}
+
Modified: poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp?rev=48603&r1=48602&r2=48603&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp Thu Mar 20 12:35:55 2008
@@ -122,7 +122,7 @@
CurHeuristic->Initialize(M, ECGraphs->getGlobalsGraph(), *this);
// Add the pool* prototypes to the module
- AddPoolPrototypes();
+ AddPoolPrototypes(&M);
// Create the pools for memory objects reachable by global variables.
if (SetupGlobalPools(M))
@@ -180,7 +180,7 @@
//
// NOTE: If these are changed, make sure to update PoolOptimize.cpp as well!
//
-void PoolAllocate::AddPoolPrototypes() {
+void PoolAllocate::AddPoolPrototypes(Module* M) {
if (VoidPtrTy == 0) {
// NOTE: If these are changed, make sure to update PoolOptimize.cpp as well!
VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
@@ -188,43 +188,43 @@
PoolDescPtrTy = PointerType::getUnqual(PoolDescType);
}
- CurModule->addTypeName("PoolDescriptor", PoolDescType);
+ M->addTypeName("PoolDescriptor", PoolDescType);
// Get poolinit function.
- PoolInit = CurModule->getOrInsertFunction("poolinit", Type::VoidTy,
+ PoolInit = M->getOrInsertFunction("poolinit", Type::VoidTy,
PoolDescPtrTy, Type::Int32Ty,
Type::Int32Ty, NULL);
// Get pooldestroy function.
- PoolDestroy = CurModule->getOrInsertFunction("pooldestroy", Type::VoidTy,
+ PoolDestroy = M->getOrInsertFunction("pooldestroy", Type::VoidTy,
PoolDescPtrTy, NULL);
// The poolalloc function.
- PoolAlloc = CurModule->getOrInsertFunction("poolalloc",
+ PoolAlloc = M->getOrInsertFunction("poolalloc",
VoidPtrTy, PoolDescPtrTy,
Type::Int32Ty, NULL);
// The poolrealloc function.
- PoolRealloc = CurModule->getOrInsertFunction("poolrealloc",
+ PoolRealloc = M->getOrInsertFunction("poolrealloc",
VoidPtrTy, PoolDescPtrTy,
VoidPtrTy, Type::Int32Ty, NULL);
// The poolmemalign function.
- PoolMemAlign = CurModule->getOrInsertFunction("poolmemalign",
+ PoolMemAlign = M->getOrInsertFunction("poolmemalign",
VoidPtrTy, PoolDescPtrTy,
Type::Int32Ty, Type::Int32Ty,
NULL);
// The poolstrdup function.
- PoolStrdup = CurModule->getOrInsertFunction("poolstrdup",
+ PoolStrdup = M->getOrInsertFunction("poolstrdup",
VoidPtrTy, PoolDescPtrTy,
VoidPtrTy, NULL);
// The poolmemalign function.
// Get the poolfree function.
- PoolFree = CurModule->getOrInsertFunction("poolfree", Type::VoidTy,
+ PoolFree = M->getOrInsertFunction("poolfree", Type::VoidTy,
PoolDescPtrTy, VoidPtrTy, NULL);
#if defined(SAFECODE) || defined(BOUNDS_CHECK)
//Get the poolregister function
- PoolRegister = CurModule->getOrInsertFunction("poolregister", Type::VoidTy,
+ PoolRegister = M->getOrInsertFunction("poolregister", Type::VoidTy,
PoolDescPtrTy, VoidPtrTy, Type::Int32Ty, NULL);
#endif
}
More information about the llvm-commits
mailing list