[llvm-commits] [poolalloc] r47855 - in /poolalloc/trunk/lib/PoolAllocate: PoolAllocate.cpp PoolOptimize.cpp TransformFunctionBody.cpp
John Criswell
criswell at uiuc.edu
Mon Mar 3 12:50:33 PST 2008
Author: criswell
Date: Mon Mar 3 14:50:33 2008
New Revision: 47855
URL: http://llvm.org/viewvc/llvm-project?rev=47855&view=rev
Log:
Added support for strdup() (which is not automatically inlined on Mac OS X).
Fixed the arguments used for poolregister().
Removed unneeded header file from PoolAllocate.cpp.
Improved some formatting.
Updated bounds checking code to compile correctly.
Modified:
poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
poolalloc/trunk/lib/PoolAllocate/PoolOptimize.cpp
poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp
Modified: poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp?rev=47855&r1=47854&r2=47855&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp Mon Mar 3 14:50:33 2008
@@ -25,7 +25,6 @@
#include "llvm/Module.h"
#include "llvm/Constants.h"
#include "llvm/ParamAttrsList.h"
-#include "llvm/ParameterAttributes.h"
#include "llvm/Support/CFG.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -215,18 +214,19 @@
Type::Int32Ty, Type::Int32Ty,
NULL);
+ // The poolstrdup function.
+ PoolStrdup = CurModule->getOrInsertFunction("poolstrdup",
+ VoidPtrTy, PoolDescPtrTy,
+ VoidPtrTy, NULL);
+ // The poolmemalign function.
// Get the poolfree function.
PoolFree = CurModule->getOrInsertFunction("poolfree", Type::VoidTy,
PoolDescPtrTy, VoidPtrTy, NULL);
-#ifdef SAFECODE
+#if defined(SAFECODE) || defined(BOUNDS_CHECK)
//Get the poolregister function
PoolRegister = CurModule->getOrInsertFunction("poolregister", Type::VoidTy,
- PoolDescPtrTy, Type::Int32Ty, VoidPtrTy, NULL);
+ PoolDescPtrTy, VoidPtrTy, Type::Int32Ty, NULL);
#endif
-#ifdef BOUNDS_CHECK
- PoolRegister = CurModule->getOrInsertFunction("poolregister", Type::VoidTy,
- PoolDescPtrTy, VoidPtrTy, Type::Int32Ty, NULL);
-#endif
}
static void getCallsOf(Constant *C, std::vector<CallInst*> &Calls) {
@@ -672,11 +672,11 @@
for (DSGraph::node_iterator I = G.node_begin(), E = G.node_end(); I != E;++I){
// We only need to make a pool if there is a heap object in it...
DSNode *N = I;
- if (
#ifdef BOUNDS_CHECK
- (N->isArray() ||
+ if ((N->isArray()) || (N->isHeapNode()))
+#else
+ if (N->isHeapNode())
#endif
- (N->isHeapNode()))
if (GlobalsGraphNodeMapping.count(N)) {
// If it is a global pool, set up the pool descriptor appropriately.
DSNode *GGN = GlobalsGraphNodeMapping[N].getNode();
Modified: poolalloc/trunk/lib/PoolAllocate/PoolOptimize.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PoolOptimize.cpp?rev=47855&r1=47854&r2=47855&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PoolOptimize.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PoolOptimize.cpp Mon Mar 3 14:50:33 2008
@@ -51,7 +51,11 @@
bool PoolOptimize::runOnModule(Module &M) {
const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
+#ifdef SAFECODE
+ const Type *PoolDescPtrTy = PointerType::getUnqual(ArrayType::get(VoidPtrTy, 50));
+#else
const Type *PoolDescPtrTy = PointerType::getUnqual(ArrayType::get(VoidPtrTy, 16));
+#endif
// Get poolinit function.
Modified: poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp?rev=47855&r1=47854&r2=47855&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp Mon Mar 3 14:50:33 2008
@@ -68,6 +68,7 @@
void visitCallocCall(CallSite CS);
void visitReallocCall(CallSite CS);
void visitMemAlignCall(CallSite CS);
+ void visitStrdupCall(CallSite CS);
void visitFreeInst(FreeInst &FI);
void visitCallSite(CallSite CS);
void visitCallInst(CallInst &CI) { visitCallSite(&CI); }
@@ -261,10 +262,16 @@
MI.getOperand(0), "sizetmp", &MI);
// TransformAllocationInstr(&MI, AllocSize);
+ BasicBlock::iterator InsertPt(MI);
+ ++InsertPt;
Instruction *Casted = CastInst::createPointerCast(&MI, PointerType::getUnqual(Type::Int8Ty),
- MI.getName()+".casted", MI.getNext());
+ MI.getName()+".casted", InsertPt);
+ std::vector<Value *> args;
+ args.push_back (PH);
+ args.push_back (Casted);
+ args.push_back (AllocSize);
Instruction *V = new CallInst(PAInfo.PoolRegister,
- make_vector(PH, Casted, AllocSize, 0), "", Casted->getNext());
+ args.begin(), args.end(), "", InsertPt);
AddPoolUse(*V, PH, PoolUses);
}
#endif
@@ -464,6 +471,57 @@
I->eraseFromParent();
}
+/// visitStrdupCall - Handle strdup().
+///
+void FuncTransform::visitStrdupCall(CallSite CS) {
+ assert(CS.arg_end()-CS.arg_begin() == 1 && "strdup takes one argument!");
+ Instruction *I = CS.getInstruction();
+ DSNode *Node = getDSNodeHFor(I).getNode();
+ assert (Node && "strdup has NULL DSNode!\n");
+ Value *PH = getPoolHandle(I);
+#if 0
+ assert (PH && "PH for strdup is null!\n");
+#else
+ if (!PH) {
+ std::cerr << "strdup: NoPH" << std::endl;
+ return;
+ }
+#endif
+ Value *OldPtr = CS.getArgument(0);
+
+ static Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
+ if (OldPtr->getType() != VoidPtrTy)
+ OldPtr = CastInst::createPointerCast(OldPtr, VoidPtrTy, OldPtr->getName(), I);
+
+ std::string Name = I->getName(); I->setName("");
+ Value* Opts[3] = {PH, OldPtr, 0};
+ Instruction *V = new CallInst(PAInfo.PoolStrdup, Opts, Opts + 2, Name, I);
+ Instruction *Casted = V;
+ if (V->getType() != I->getType())
+ Casted = CastInst::createPointerCast(V, I->getType(), V->getName(), I);
+
+ // Update def-use info
+ I->replaceAllUsesWith(Casted);
+
+ // If we are modifying the original function, update the DSGraph.
+ if (!FI.Clone) {
+ // V and Casted now point to whatever the original allocation did.
+ G.getScalarMap().replaceScalar(I, V);
+ if (V != Casted)
+ G.getScalarMap()[Casted] = G.getScalarMap()[V];
+ } else { // Otherwise, update the NewToOldValueMap
+ UpdateNewToOldValueMap(I, V, V != Casted ? Casted : 0);
+ }
+
+ // If this was an invoke, fix up the CFG.
+ if (InvokeInst *II = dyn_cast<InvokeInst>(I)) {
+ new BranchInst(II->getNormalDest(), I);
+ II->getUnwindDest()->removePredecessor(II->getParent(), true);
+ }
+
+ // Remove old allocation instruction.
+ I->eraseFromParent();
+}
void FuncTransform::visitCallSite(CallSite CS) {
@@ -496,7 +554,10 @@
visitMemAlignCall(CS);
return;
} else if (CF->getName() == "strdup") {
- assert(0 && "strdup should have been linked into the program!");
+#if 1
+ visitStrdupCall(CS);
+#endif
+ return;
} else if (CF->getName() == "valloc") {
std::cerr << "VALLOC USED BUT NOT HANDLED!\n";
abort();
More information about the llvm-commits
mailing list