[llvm-commits] [poolalloc] r159777 - in /poolalloc/trunk: include/dsa/CStdLib.h include/poolalloc/PoolAllocate.h include/poolalloc/RuntimeChecks.h lib/PoolAllocate/PASimple.cpp lib/PoolAllocate/PoolAllocate.cpp lib/PoolAllocate/TransformFunctionBody.cpp
Matthew Wala
mttjwl at gmail.com
Thu Jul 5 14:01:02 PDT 2012
Author: wala1
Date: Thu Jul 5 16:01:02 2012
New Revision: 159777
URL: http://llvm.org/viewvc/llvm-project?rev=159777&view=rev
Log:
Move runtime check information into a single header file.
Give the poolalloc global constructor highest priority.
Make PoolAllocateSimple use the poolalloc global constructor.
Added:
poolalloc/trunk/include/poolalloc/RuntimeChecks.h
- copied, changed from r159758, poolalloc/trunk/include/dsa/CStdLib.h
Removed:
poolalloc/trunk/include/dsa/CStdLib.h
Modified:
poolalloc/trunk/include/poolalloc/PoolAllocate.h
poolalloc/trunk/lib/PoolAllocate/PASimple.cpp
poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp
Removed: poolalloc/trunk/include/dsa/CStdLib.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/dsa/CStdLib.h?rev=159776&view=auto
==============================================================================
--- poolalloc/trunk/include/dsa/CStdLib.h (original)
+++ poolalloc/trunk/include/dsa/CStdLib.h (removed)
@@ -1,81 +0,0 @@
-//===- CStdLib.h - CStdLib Function Information ------------*- C++ -*-===//
-//
-// 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.
-//
-//===----------------------------------------------------------------------===//
-//
-// Tables to hold information about transformed CStdLib functions.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_CSTDLIB_H
-#define LLVM_CSTDLIB_H
-
-namespace llvm {
-
- const struct CStdLibPoolArgCountEntry {
- const char *function;
- unsigned pool_argc;
- } \
- CStdLibPoolArgCounts[] = {
- { "pool_vprintf", 1 },
- { "pool_vfprint", 2 },
- { "pool_vsprintf", 2 },
- { "pool_vsnprintf", 2 },
- { "pool_vscanf", 1 },
- { "pool_vsscanf", 2 },
- { "pool_vfscanf", 2 },
- { "pool_vsyslog", 1 },
- { "pool_memccpy", 2 },
- { "pool_memchr", 1 },
- { "pool_memcmp", 2 },
- { "pool_memcpy", 2 },
- { "pool_memmove", 2 },
- { "pool_memset", 1 },
- { "pool_strcat", 2 },
- { "pool_strchr", 1 },
- { "pool_strcmp", 2 },
- { "pool_strcoll", 2 },
- { "pool_strcpy", 2 },
- { "pool_strcspn", 2 },
- { "pool_strlen", 1 },
- { "pool_strncat", 2 },
- { "pool_strncmp", 2 },
- { "pool_strncpy", 2 },
- { "pool_strpbrk", 2 },
- { "pool_strrchr", 1 },
- { "pool_strspn", 2 },
- { "pool_strstr", 2 },
- { "pool_strxfrm", 2 },
- { "pool_mempcpy", 2 },
- { "pool_strcasestr", 2 },
- { "pool_stpcpy", 2 },
- { "pool_strnlen", 1 },
- { "pool_bcmp", 2 },
- { "pool_bcopy", 2 },
- { "pool_bzero", 1 },
- { "pool_index", 1 },
- { "pool_rindex", 1 },
- { "pool_strcasecmp", 2 },
- { "pool_strncasecmp", 2 },
- { "pool_fgets", 1 },
- { "pool_fputs", 1 },
- { "pool_fwrite", 1 },
- { "pool_fread", 1 },
- { "pool_gets", 1 },
- { "pool_puts", 1 },
- { "pool_read", 1 },
- { "pool_recv", 1 },
- { "pool_write", 1 },
- { "pool_send", 1 },
- { "pool_readlink", 2 },
- { "pool_realpath", 2 },
- { 0, 0 }
- };
-
-} // End llvm namespace
-
-#endif
Modified: poolalloc/trunk/include/poolalloc/PoolAllocate.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/poolalloc/PoolAllocate.h?rev=159777&r1=159776&r2=159777&view=diff
==============================================================================
--- poolalloc/trunk/include/poolalloc/PoolAllocate.h (original)
+++ poolalloc/trunk/include/poolalloc/PoolAllocate.h Thu Jul 5 16:01:02 2012
@@ -422,8 +422,10 @@
return I->second;
}
- // Get the initial pool argument count for a CStdLib function.
- unsigned getCStdLibPoolArguments(StringRef funcname);
+ /// getNumInitialPoolArguments - If the passed function name is recognized
+ /// as a runtime check, return the number of initial pool arguments for
+ /// the runtime check. Otherwise return 0.
+ static unsigned getNumInitialPoolArguments(StringRef Name);
protected:
@@ -433,6 +435,10 @@
///
void AddPoolPrototypes(Module*);
+ /// createGlobalPoolCtor - Create an empty function and insert it into the
+ /// constructor list. The created function can be used for poolinit calls.
+ Function *createGlobalPoolCtor(Module &M);
+
private:
/// MicroOptimizePoolCalls - Apply any microoptimizations to calls to pool
Copied: poolalloc/trunk/include/poolalloc/RuntimeChecks.h (from r159758, poolalloc/trunk/include/dsa/CStdLib.h)
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/poolalloc/RuntimeChecks.h?p2=poolalloc/trunk/include/poolalloc/RuntimeChecks.h&p1=poolalloc/trunk/include/dsa/CStdLib.h&r1=159758&r2=159777&rev=159777&view=diff
==============================================================================
--- poolalloc/trunk/include/dsa/CStdLib.h (original)
+++ poolalloc/trunk/include/poolalloc/RuntimeChecks.h Thu Jul 5 16:01:02 2012
@@ -1,81 +1,130 @@
-//===- CStdLib.h - CStdLib Function Information ------------*- C++ -*-===//
+//===- RuntimeChecks.h - Runtime check information -------------*- C++ -*-===//
//
// 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.
//
-//===----------------------------------------------------------------------===//
+//===---------------------------------------------------------------------===//
//
-// Tables to hold information about transformed CStdLib functions.
+// Provides a table holding information about runtime checks that take pool
+// handles.
//
-//===----------------------------------------------------------------------===//
+//===---------------------------------------------------------------------===//
-#ifndef LLVM_CSTDLIB_H
-#define LLVM_CSTDLIB_H
+#ifndef _RUNTIME_CHECKS_H
+#define _RUNTIME_CHECKS_H
-namespace llvm {
+namespace {
- const struct CStdLibPoolArgCountEntry {
- const char *function;
- unsigned pool_argc;
- } \
- CStdLibPoolArgCounts[] = {
- { "pool_vprintf", 1 },
- { "pool_vfprint", 2 },
- { "pool_vsprintf", 2 },
- { "pool_vsnprintf", 2 },
- { "pool_vscanf", 1 },
- { "pool_vsscanf", 2 },
- { "pool_vfscanf", 2 },
- { "pool_vsyslog", 1 },
- { "pool_memccpy", 2 },
- { "pool_memchr", 1 },
- { "pool_memcmp", 2 },
- { "pool_memcpy", 2 },
- { "pool_memmove", 2 },
- { "pool_memset", 1 },
- { "pool_strcat", 2 },
- { "pool_strchr", 1 },
- { "pool_strcmp", 2 },
- { "pool_strcoll", 2 },
- { "pool_strcpy", 2 },
- { "pool_strcspn", 2 },
- { "pool_strlen", 1 },
- { "pool_strncat", 2 },
- { "pool_strncmp", 2 },
- { "pool_strncpy", 2 },
- { "pool_strpbrk", 2 },
- { "pool_strrchr", 1 },
- { "pool_strspn", 2 },
- { "pool_strstr", 2 },
- { "pool_strxfrm", 2 },
- { "pool_mempcpy", 2 },
- { "pool_strcasestr", 2 },
- { "pool_stpcpy", 2 },
- { "pool_strnlen", 1 },
- { "pool_bcmp", 2 },
- { "pool_bcopy", 2 },
- { "pool_bzero", 1 },
- { "pool_index", 1 },
- { "pool_rindex", 1 },
- { "pool_strcasecmp", 2 },
- { "pool_strncasecmp", 2 },
- { "pool_fgets", 1 },
- { "pool_fputs", 1 },
- { "pool_fwrite", 1 },
- { "pool_fread", 1 },
- { "pool_gets", 1 },
- { "pool_puts", 1 },
- { "pool_read", 1 },
- { "pool_recv", 1 },
- { "pool_write", 1 },
- { "pool_send", 1 },
- { "pool_readlink", 2 },
- { "pool_realpath", 2 },
- { 0, 0 }
- };
+typedef enum { SAFECodeCheck, CStdLibCheck, CIFCheck } CheckKindTy;
-} // End llvm namespace
+const struct RuntimeCheckEntry {
+ const char *Function;
+ unsigned PoolArgc;
+ CheckKindTy CheckKind;
+} RuntimeCheckEntries[] = {
+ //
+ // SAFECode runtime checks
+ //
+ { "pool_register", 1, SAFECodeCheck },
+ { "pool_register_debug", 1, SAFECodeCheck },
+ { "pool_register_stack", 1, SAFECodeCheck },
+ { "pool_register_stack_debug", 1, SAFECodeCheck },
+ { "pool_register_global", 1, SAFECodeCheck },
+ { "pool_register_global_debug", 1, SAFECodeCheck },
+ { "pool_reregister", 1, SAFECodeCheck },
+ { "pool_reregister_debug", 1, SAFECodeCheck },
+ { "pool_unregister", 1, SAFECodeCheck },
+ { "pool_unregister_debug", 1, SAFECodeCheck },
+ { "pool_unregister_stack", 1, SAFECodeCheck },
+ { "pool_unregister_stack_debug", 1, SAFECodeCheck },
+ { "poolcheck", 1, SAFECodeCheck },
+ { "poolcheck_debug", 1, SAFECodeCheck },
+ { "poolcheckui", 1, SAFECodeCheck },
+ { "poolcheckui_debug", 1, SAFECodeCheck },
+ { "poolcheckalign", 1, SAFECodeCheck },
+ { "poolcheckalign_debug", 1, SAFECodeCheck },
+ { "poolcheckstr", 1, SAFECodeCheck },
+ { "poolcheckstr_debug", 1, SAFECodeCheck },
+ { "poolcheckstrui", 1, SAFECodeCheck },
+ { "poolcheckstrui_debug", 1, SAFECodeCheck },
+ { "boundscheck", 1, SAFECodeCheck },
+ { "boundscheck_debug", 1, SAFECodeCheck },
+ { "boundscheckui", 1, SAFECodeCheck },
+ { "boundscheckui_debug", 1, SAFECodeCheck },
+ { "poolcheck_free", 1, SAFECodeCheck },
+ { "poolcheck_free_debug", 1, SAFECodeCheck },
+ { "poolcheck_freeui", 1, SAFECodeCheck },
+ { "poolcheck_freeui_debug", 1, SAFECodeCheck },
+ { "pchk_getActualValue", 1, SAFECodeCheck },
+ { "__sc_fsparameter", 1, SAFECodeCheck },
+
+ //
+ // CStdLib runtime checks
+ //
+ { "pool_vprintf", 1, CStdLibCheck },
+ { "pool_vfprint", 2, CStdLibCheck },
+ { "pool_vsprintf", 2, CStdLibCheck },
+ { "pool_vsnprintf", 2, CStdLibCheck },
+ { "pool_vscanf", 1, CStdLibCheck },
+ { "pool_vsscanf", 2, CStdLibCheck },
+ { "pool_vfscanf", 2, CStdLibCheck },
+ { "pool_vsyslog", 1, CStdLibCheck },
+ { "pool_memccpy", 2, CStdLibCheck },
+ { "pool_memchr", 1, CStdLibCheck },
+ { "pool_memcmp", 2, CStdLibCheck },
+ { "pool_memcpy", 2, CStdLibCheck },
+ { "pool_memmove", 2, CStdLibCheck },
+ { "pool_memset", 1, CStdLibCheck },
+ { "pool_strcat", 2, CStdLibCheck },
+ { "pool_strchr", 1, CStdLibCheck },
+ { "pool_strcmp", 2, CStdLibCheck },
+ { "pool_strcoll", 2, CStdLibCheck },
+ { "pool_strcpy", 2, CStdLibCheck },
+ { "pool_strcspn", 2, CStdLibCheck },
+ { "pool_strlen", 1, CStdLibCheck },
+ { "pool_strncat", 2, CStdLibCheck },
+ { "pool_strncmp", 2, CStdLibCheck },
+ { "pool_strncpy", 2, CStdLibCheck },
+ { "pool_strpbrk", 2, CStdLibCheck },
+ { "pool_strrchr", 1, CStdLibCheck },
+ { "pool_strspn", 2, CStdLibCheck },
+ { "pool_strstr", 2, CStdLibCheck },
+ { "pool_strxfrm", 2, CStdLibCheck },
+ { "pool_mempcpy", 2, CStdLibCheck },
+ { "pool_strcasestr", 2, CStdLibCheck },
+ { "pool_stpcpy", 2, CStdLibCheck },
+ { "pool_strnlen", 1, CStdLibCheck },
+ { "pool_bcmp", 2, CStdLibCheck },
+ { "pool_bcopy", 2, CStdLibCheck },
+ { "pool_bzero", 1, CStdLibCheck },
+ { "pool_index", 1, CStdLibCheck },
+ { "pool_rindex", 1, CStdLibCheck },
+ { "pool_strcasecmp", 2, CStdLibCheck },
+ { "pool_strncasecmp", 2, CStdLibCheck },
+ { "pool_fgets", 1, CStdLibCheck },
+ { "pool_fputs", 1, CStdLibCheck },
+ { "pool_gets", 1, CStdLibCheck },
+ { "pool_puts", 1, CStdLibCheck },
+ { "pool_fread", 1, CStdLibCheck },
+ { "pool_fwrite", 1, CStdLibCheck },
+ { "pool_tmpnam", 1, CStdLibCheck },
+ { "pool_readdir_r", 2, CStdLibCheck },
+ { "pool_read", 1, CStdLibCheck },
+ { "pool_recv", 1, CStdLibCheck },
+ { "pool_write", 1, CStdLibCheck },
+ { "pool_send", 1, CStdLibCheck },
+ { "pool_readlink", 2, CStdLibCheck },
+ { "pool_realpath", 2, CStdLibCheck },
+
+ //
+ // CIFCheck intrinsics
+ //
+ { "__if_pool_get_label", 1, CIFCheck },
+ { "__if_pool_set_label", 1, CIFCheck }
+};
+
+}
#endif
Modified: poolalloc/trunk/lib/PoolAllocate/PASimple.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PASimple.cpp?rev=159777&r1=159776&r2=159777&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PASimple.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PASimple.cpp Thu Jul 5 16:01:02 2012
@@ -70,45 +70,6 @@
return CastInst::CreateZExtOrBitCast (V, Ty, Name, InsertPt);
}
-//
-// Function: initialPoolArguments()
-//
-// Description:
-// This function determines if the specified function has inital pool arguments
-// that should be replaced, and if so, returns the numbers of initial pool arguments
-// to replace.
-//
-// Inputs:
-// funcname - A reference to a string containing the name of the function.
-//
-// Return value:
-// 0 - The function does not have any initial pool arguments to replace.
-// Otherwise, the number of initial pool arguments to replace.
-//
-static unsigned
-initialPoolArguments(const std::string & funcname) {
- if ((funcname == "sc.lscheck") ||
- (funcname == "sc.lscheckui") ||
- (funcname == "sc.lscheckalign") ||
- (funcname == "sc.lscheckalignui") ||
- (funcname == "sc.boundscheck") ||
- (funcname == "sc.boundscheckui") ||
- (funcname == "sc.pool_register_stack") ||
- (funcname == "sc.pool_unregister_stack") ||
- (funcname == "sc.pool_register_global") ||
- (funcname == "sc.pool_unregister_global") ||
- (funcname == "sc.pool_register") ||
- (funcname == "sc.pool_unregister") ||
- (funcname == "sc.get_actual_val") ||
- (funcname == "__if_pool_get_label") ||
- (funcname == "__if_pool_set_label") ||
- (funcname == "sc.fsparameter")) {
- return 1;
- }
-
- return 0;
-}
-
void PoolAllocateSimple::getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired<TargetData>();
// Get the Target Data information and the Graphs
@@ -290,7 +251,7 @@
//
// Insert the call to poolalloc()
//
- Value* Opts[3] = {TheGlobalPool, Size};
+ Value* Opts[2] = {TheGlobalPool, Size};
Instruction *V = CallInst::Create (PoolAlloc,
Opts,
Name,
@@ -501,14 +462,11 @@
// pool.
//
if (CF) {
- unsigned count;
- if ((count = initialPoolArguments(CF->getName())) || \
- (count = getCStdLibPoolArguments(CF->getName()))) {
- Type * VoidPtrTy = PointerType::getUnqual(Int8Type);
- Value * Pool = castTo (TheGlobalPool, VoidPtrTy, "pool", ii);
- for (unsigned index = 1; index <= count; index++ )
- CI->setOperand (index, Pool);
- }
+ unsigned Count = getNumInitialPoolArguments(CF->getName());
+ Type * VoidPtrTy = PointerType::getUnqual(Int8Type);
+ Value * Pool = castTo (TheGlobalPool, VoidPtrTy, "pool", ii);
+ for (unsigned ArgIndex = 0; ArgIndex < Count; ArgIndex++ )
+ CI->setArgOperand (ArgIndex, Pool);
}
}
}
@@ -522,8 +480,7 @@
}
/// 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.
+/// poolinit for it into the global constructor.
GlobalVariable *
PoolAllocateSimple::CreateGlobalPool (unsigned RecSize,
unsigned Align,
@@ -537,32 +494,26 @@
}
//
- // Give poolinit() a dummy body. A later transform will remove the dummy
- // body.
+ // Create the global pool descriptor.
//
- if (SAFECodeEnabled) {
- LLVMContext & Context = M.getContext();
- Function * PoolInitFunc = dyn_cast<Function>(PoolInit);
- BasicBlock * entryBB = BasicBlock::Create (Context, "entry", PoolInitFunc);
- ReturnInst::Create (Context, entryBB);
- }
-
GlobalVariable *GV =
new GlobalVariable(M,
- getPoolType(&M.getContext()), false, GlobalValue::ExternalLinkage,
- ConstantAggregateZero::get(getPoolType(&M.getContext())),
- "__poolalloc_GlobalPool");
-
- Function *InitFunc = Function::Create
- (FunctionType::get(VoidType, std::vector<Type*>(), false),
- GlobalValue::ExternalLinkage, "__poolalloc_init", &M);
+ getPoolType(&M.getContext()), false, GlobalValue::ExternalLinkage,
+ ConstantAggregateZero::get(getPoolType(&M.getContext())),
+ "__poolalloc_GlobalPool"
+ );
- BasicBlock * BB = BasicBlock::Create(M.getContext(), "entry", InitFunc);
+ //
+ // Get the global pool constructor. Create and insert the poolinit call
+ // inside it.
+ //
+ Function *InitFunc = createGlobalPoolCtor(M);
Value *ElSize = ConstantInt::get(Int32Type, RecSize);
Value *AlignV = ConstantInt::get(Int32Type, Align);
- Value* Opts[3] = {GV, ElSize, AlignV};
- CallInst::Create(PoolInit, Opts, "", BB);
+ Value *Opts[3] = { GV, ElSize, AlignV };
+
+ CallInst *InitCall = CallInst::Create(PoolInit, Opts, "");
+ InitCall->insertBefore(&InitFunc->getEntryBlock().front());
- ReturnInst::Create(M.getContext(), BB);
return GV;
}
Modified: poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp?rev=159777&r1=159776&r2=159777&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp Thu Jul 5 16:01:02 2012
@@ -16,11 +16,11 @@
#define DEBUG_TYPE "poolalloc"
-#include "dsa/CStdLib.h"
#include "dsa/DataStructure.h"
#include "dsa/DSGraph.h"
#include "poolalloc/Heuristic.h"
#include "poolalloc/PoolAllocate.h"
+#include "poolalloc/RuntimeChecks.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
@@ -114,8 +114,7 @@
// (i.e., global ctor). Pool Allocation will eventually add code to it to
// initialize all of the global pools.
//
-static Function *
-createGlobalPoolCtor (Module & M) {
+Function * PoolAllocate::createGlobalPoolCtor (Module & M) {
//
// Create the global pool ctor function.
//
@@ -140,7 +139,12 @@
//
Type * Int32Type = IntegerType::getInt32Ty(Context);
std::vector<Constant *> CtorInits;
- CtorInits.push_back (ConstantInt::get (Int32Type, 65535));
+
+ //
+ // We need to ensure that this constructor gets called before any other code
+ // in the module executes, so give the constructor a priority of 0 (highest).
+ //
+ CtorInits.push_back (ConstantInt::get (Int32Type, 0));
CtorInits.push_back (InitFunc);
Constant * RuntimeCtorInit = ConstantStruct::getAnon(Context, CtorInits);
@@ -1577,11 +1581,39 @@
}
}
-// Return the number of initial pool arguments for the specified CStdLib
-// function, or 0 if it is not found in the table.
-unsigned PoolAllocate::getCStdLibPoolArguments(StringRef funcname) {
- const CStdLibPoolArgCountEntry *entries = &CStdLibPoolArgCounts[0];
- while (entries->function && entries->function != funcname)
- entries++;
- return entries->pool_argc;
+//
+// Function: getNumInitialPoolArguments()
+//
+// Description:
+// This function determines if the specified function has inital pool arguments
+// that should be replaced, and if so, returns the numbers of initial pool arguments
+// to replace.
+//
+// Inputs:
+// funcname - A reference to a string containing the name of the function.
+//
+// Return value:
+// 0 - The function does not have any initial pool arguments to replace.
+// Otherwise, the number of initial pool arguments to replace.
+//
+unsigned PoolAllocate::getNumInitialPoolArguments(StringRef FuncName) {
+ const unsigned EntryCount =
+ sizeof(RuntimeCheckEntries) / sizeof(RuntimeCheckEntries[0]);
+
+ for (unsigned Index = 0; Index < EntryCount; ++Index) {
+
+ if (RuntimeCheckEntries[Index].Function == FuncName)
+ return RuntimeCheckEntries[Index].PoolArgc;
+
+ else if (RuntimeCheckEntries[Index].CheckKind == CStdLibCheck) {
+ // Check for _debug() versions of CStdLib functions.
+ std::string DebugName =
+ RuntimeCheckEntries[Index].Function + std::string("_debug");
+
+ if (DebugName == FuncName)
+ return RuntimeCheckEntries[Index].PoolArgc;
+ }
+ }
+
+ return 0;
}
Modified: poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp?rev=159777&r1=159776&r2=159777&view=diff
==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp Thu Jul 5 16:01:02 2012
@@ -17,6 +17,7 @@
#include "dsa/DSGraph.h"
#include "dsa/CallTargets.h"
#include "poolalloc/PoolAllocate.h"
+#include "poolalloc/RuntimeChecks.h"
#include "llvm/Module.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Constants.h"
@@ -72,8 +73,7 @@
void visitReallocCall(CallSite CS);
void visitMemAlignCall(CallSite CS);
void visitStrdupCall(CallSite CS);
- void visitRuntimeCheck(CallSite CS);
- void visitCStdLibCheck(CallSite CS, const unsigned argc);
+ void visitRuntimeCheck(CallSite CS, const unsigned PoolArgc);
void visitFreeCall(CallSite &CS);
void visitCallSite(CallSite &CS);
void visitCallInst(CallInst &CI) {
@@ -715,74 +715,37 @@
//
// Description:
// Visit a call to a run-time check (or related function) and insert pool
-// arguments where needed.
+// arguments where needed. PoolArgc is the number of initial pool arguments
+// that should be filled at the call site with pool handles for the
+// corresponding pointer arguments.
//
void
-FuncTransform::visitRuntimeCheck (CallSite CS) {
- // A run-time check should have at least one argument for a pool
- assert ((CS.arg_size() > 1) && "Runtime check takes more than one argument!");
-
- //
- // Get the pool handle for the pointer argument.
- //
- Value *PH = getPoolHandle(CS.getArgument(1)->stripPointerCasts());
- //assert (PH && "Pool Handle for run-time checks is null!\n");
-
- //
- // Insert the pool handle into the run-time check.
- //
- if (PH) {
- Type * Int8Type = Type::getInt8Ty(CS.getInstruction()->getContext());
- Type * VoidPtrTy = PointerType::getUnqual(Int8Type);
- PH = castTo (PH, VoidPtrTy, PH->getName(), CS.getInstruction());
- CS.setArgument (0, PH);
+FuncTransform::visitRuntimeCheck (CallSite CS, const unsigned PoolArgc) {
+ // A call to the runtime check should have positions for each pool argument
+ // and the corresponding pointer.
+ assert ((CS.arg_size() >= 2 * PoolArgc) &&
+ "Not enough arguments to call of a runtime check!");
+ for (unsigned PoolIndex = 0; PoolIndex < PoolArgc; ++PoolIndex) {
//
- // Record that we've used the pool here.
+ // Get the pool handle for the pointer argument.
//
- AddPoolUse (*(CS.getInstruction()), PH, PoolUses);
- }
-}
+ Value *PH =
+ getPoolHandle(CS.getArgument(PoolArgc + PoolIndex)->stripPointerCasts());
-/**
- * Visit the call site and replace null pool arguments with the correct ones
- *
- * @param argc Number of pool arguments to insert
- */
-void FuncTransform::visitCStdLibCheck(CallSite CS, const unsigned argc) {
- // Check for the correct number of pool arguments
- assert ((CS.arg_size() > argc) && "Incorrect number of pool arguments!");
-
- Type *Int8Ty = Type::getInt8Ty(CS.getInstruction()->getContext());
- Type *VoidPtrTy = PointerType::getUnqual(Int8Ty);
-
- if (argc == 1) {
- // Get the pool handle for the pointer argument
- Value *PH = getPoolHandle(CS.getArgument(1)->stripPointerCasts());
-
- // Insert the pool handle
+ //
+ // Insert the pool handle into the run-time check.
+ //
if (PH) {
- PH = castTo(PH, VoidPtrTy, PH->getName(), CS.getInstruction());
- CS.setArgument(0, PH);
- AddPoolUse(*(CS.getInstruction()), PH, PoolUses);
- }
- } else if (argc == 2) {
- // Get the pool handles for the pointer arguments
- Value *dstPH = getPoolHandle(CS.getArgument(2)->stripPointerCasts());
- Value *srcPH = getPoolHandle(CS.getArgument(3)->stripPointerCasts());
-
- // Insert the destination pool handle
- if (dstPH) {
- dstPH = castTo(dstPH, VoidPtrTy, dstPH->getName(), CS.getInstruction());
- CS.setArgument(0, dstPH);
- AddPoolUse(*(CS.getInstruction()), dstPH, PoolUses);
- }
-
- // Insert the source pool handle
- if (srcPH) {
- srcPH = castTo(srcPH, VoidPtrTy, srcPH->getName(), CS.getInstruction());
- CS.setArgument(1, srcPH);
- AddPoolUse(*(CS.getInstruction()), srcPH, PoolUses);
+ Type * Int8Type = Type::getInt8Ty(CS.getInstruction()->getContext());
+ Type * VoidPtrTy = PointerType::getUnqual(Int8Type);
+ PH = castTo (PH, VoidPtrTy, PH->getName(), CS.getInstruction());
+ CS.setArgument (PoolIndex, PH);
+
+ //
+ // Record that we've used the pool here.
+ //
+ AddPoolUse (*(CS.getInstruction()), PH, PoolUses);
}
}
}
@@ -801,7 +764,6 @@
const Function *CF = CS.getCalledFunction();
Instruction *TheCall = CS.getInstruction();
bool thread_creation_point = false;
- unsigned argc;
//
// Get the value that is called at this call site. Strip away any pointer
@@ -838,49 +800,33 @@
// If this function is one of the memory manipulating functions built into
// libc, emulate it with pool calls as appropriate.
if (CF && CF->isDeclaration()) {
- if (CF->getName() == "free" ||
- CF->getName() == "cfree") {
+ std::string Name = CF->getName();
+
+ if (Name == "free" || Name == "cfree") {
visitFreeCall(CS);
return;
- } else if (CF->getName() == "malloc") {
+ } else if (Name == "malloc") {
visitMallocCall(CS);
return;
- } else if (CF->getName() == "calloc") {
+ } else if (Name == "calloc") {
visitCallocCall(CS);
return;
- } else if (CF->getName() == "realloc") {
+ } else if (Name == "realloc") {
visitReallocCall(CS);
return;
- } else if (CF->getName() == "memalign" ||
- CF->getName() == "posix_memalign") {
+ } else if (Name == "memalign" || Name == "posix_memalign") {
visitMemAlignCall(CS);
return;
- } else if (CF->getName() == "strdup") {
+ } else if (Name == "strdup") {
visitStrdupCall(CS);
return;
- } else if (CF->getName() == "valloc") {
+ } else if (Name == "valloc") {
errs() << "VALLOC USED BUT NOT HANDLED!\n";
abort();
- } else if ((CF->getName() == "sc.lscheck") ||
- (CF->getName() == "sc.lscheckui") ||
- (CF->getName() == "sc.lscheckalign") ||
- (CF->getName() == "sc.lscheckalignui") ||
- (CF->getName() == "sc.boundscheck") ||
- (CF->getName() == "sc.boundscheckui") ||
- (CF->getName() == "sc.pool_register_stack") ||
- (CF->getName() == "sc.pool_unregister_stack") ||
- (CF->getName() == "sc.pool_register_global") ||
- (CF->getName() == "sc.pool_unregister_global") ||
- (CF->getName() == "sc.pool_register") ||
- (CF->getName() == "sc.pool_unregister") ||
- (CF->getName() == "sc.get_actual_val") ||
- (CF->getName() == "__if_pool_get_label") ||
- (CF->getName() == "__if_pool_set_label") ||
- (CF->getName() == "sc.fsparameter")) {
- visitRuntimeCheck (CS);
- } else if ((argc = PAInfo.getCStdLibPoolArguments(CF->getName())) > 0) {
- visitCStdLibCheck(CS, argc);
- } else if (CF->getName() == "pthread_create") {
+ } else if (unsigned PoolArgc = PAInfo.getNumInitialPoolArguments(Name)) {
+ visitRuntimeCheck(CS, PoolArgc);
+ return;
+ } else if (Name == "pthread_create") {
thread_creation_point = true;
//
@@ -888,8 +834,7 @@
// the pthread_create call
//
DSNode* thread_callee_node = G->getNodeForValue(CS.getArgument(2)).getNode();
- if(!thread_callee_node)
- {
+ if (!thread_callee_node) {
assert(0 && "apparently you need this code");
FuncInfo *CFI = PAInfo.getFuncInfo(*CF);
thread_callee_node = G->getNodeForValue(CFI->MapValueToOriginal(CS.getArgument(2))).getNode();
More information about the llvm-commits
mailing list