[llvm-commits] [poolalloc] r132720 - in /poolalloc/trunk: include/assistDS/TypeChecksOpt.h lib/AssistDS/TypeChecksOpt.cpp
Arushi Aggarwal
aggarwa4 at illinois.edu
Tue Jun 7 11:39:48 PDT 2011
Author: aggarwa4
Date: Tue Jun 7 13:39:48 2011
New Revision: 132720
URL: http://llvm.org/viewvc/llvm-project?rev=132720&view=rev
Log:
Initial work on removing typesafe runtime checks.
Added:
poolalloc/trunk/include/assistDS/TypeChecksOpt.h
poolalloc/trunk/lib/AssistDS/TypeChecksOpt.cpp
Added: poolalloc/trunk/include/assistDS/TypeChecksOpt.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/assistDS/TypeChecksOpt.h?rev=132720&view=auto
==============================================================================
--- poolalloc/trunk/include/assistDS/TypeChecksOpt.h (added)
+++ poolalloc/trunk/include/assistDS/TypeChecksOpt.h Tue Jun 7 13:39:48 2011
@@ -0,0 +1,54 @@
+//===---------- TypeChecksOpt.h - Remove safe runtime type checks ---------===//
+//
+// 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 pass removes type checks that are statically proven safe
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TYPE_CHECKS_OPT_H
+#define TYPE_CHECKS_OPT_H
+
+#include "assistDS/TypeAnalysis.h"
+#include "dsa/TypeSafety.h"
+#include "dsa/AddressTakenAnalysis.h"
+
+#include "llvm/Instructions.h"
+#include "llvm/Pass.h"
+#include "llvm/Target/TargetData.h"
+#include "llvm/Support/CallSite.h"
+
+#include <map>
+
+namespace llvm {
+
+class Type;
+class Value;
+
+class TypeChecksOpt : public ModulePass {
+
+private:
+
+ // Analysis from other passes.
+ dsa::TypeSafety<TDDataStructures> *TS;
+ std::list<Instruction *> toDelete;
+
+public:
+ static char ID;
+ TypeChecksOpt() : ModulePass(&ID) {}
+ virtual bool runOnModule(Module &M);
+
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.addRequired<dsa::TypeSafety<TDDataStructures> >();
+ }
+
+};
+
+} // End llvm namespace
+
+#endif
Added: poolalloc/trunk/lib/AssistDS/TypeChecksOpt.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/TypeChecksOpt.cpp?rev=132720&view=auto
==============================================================================
--- poolalloc/trunk/lib/AssistDS/TypeChecksOpt.cpp (added)
+++ poolalloc/trunk/lib/AssistDS/TypeChecksOpt.cpp Tue Jun 7 13:39:48 2011
@@ -0,0 +1,186 @@
+//===---------- TypeChecksOpt.h - Remove safe runtime type checks ---------===//
+//
+// 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 pass removes type checks that are statically proven safe
+//
+//===----------------------------------------------------------------------===//
+
+#include "assistDS/TypeChecksOpt.h"
+#include "llvm/Constants.h"
+#include "llvm/Transforms/Utils/Cloning.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Module.h"
+#include "llvm/Assembly/Writer.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/InstIterator.h"
+#include "llvm/Support/raw_ostream.h"
+#include "llvm/Intrinsics.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/ADT/Statistic.h"
+
+#include <set>
+#include <vector>
+
+using namespace llvm;
+
+char TypeChecksOpt::ID = 0;
+
+static RegisterPass<TypeChecksOpt>
+TC("typechecks-opt", "Remove safe runtime type checks", false, true);
+
+// Pass statistics
+STATISTIC(numSafe, "Number of statically proven safe type checks");
+
+static const Type *VoidTy = 0;
+static const Type *Int8Ty = 0;
+static const Type *Int32Ty = 0;
+static const Type *Int64Ty = 0;
+static const PointerType *VoidPtrTy = 0;
+static Constant *trackGlobal;
+static Constant *trackStringInput;
+static Constant *trackArray;
+static Constant *trackInitInst;
+static Constant *trackStoreInst;
+static Constant *trackLoadInst;
+static Constant *copyTypeInfo;
+
+bool TypeChecksOpt::runOnModule(Module &M) {
+ bool modified = false; // Flags whether we modified the module.
+ TS = &getAnalysis<dsa::TypeSafety<TDDataStructures> >();
+
+ // Create the necessary prototypes
+ VoidTy = IntegerType::getVoidTy(M.getContext());
+ Int8Ty = IntegerType::getInt8Ty(M.getContext());
+ Int32Ty = IntegerType::getInt32Ty(M.getContext());
+ Int64Ty = IntegerType::getInt64Ty(M.getContext());
+ VoidPtrTy = PointerType::getUnqual(Int8Ty);
+
+ trackGlobal = M.getOrInsertFunction("trackGlobal",
+ VoidTy,
+ VoidPtrTy,/*ptr*/
+ Int8Ty,/*type*/
+ Int64Ty,/*size*/
+ Int32Ty,/*tag*/
+ NULL);
+ trackArray = M.getOrInsertFunction("trackArray",
+ VoidTy,
+ VoidPtrTy,/*ptr*/
+ Int64Ty,/*size*/
+ Int64Ty,/*count*/
+ Int32Ty,/*tag*/
+ NULL);
+ trackInitInst = M.getOrInsertFunction("trackInitInst",
+ VoidTy,
+ VoidPtrTy,/*ptr*/
+ Int64Ty,/*size*/
+ Int32Ty,/*tag*/
+ NULL);
+ trackStoreInst = M.getOrInsertFunction("trackStoreInst",
+ VoidTy,
+ VoidPtrTy,/*ptr*/
+ Int8Ty,/*type*/
+ Int64Ty,/*size*/
+ Int32Ty,/*tag*/
+ NULL);
+ trackLoadInst = M.getOrInsertFunction("trackLoadInst",
+ VoidTy,
+ VoidPtrTy,/*ptr*/
+ Int8Ty,/*type*/
+ Int64Ty,/*size*/
+ Int32Ty,/*tag*/
+ NULL);
+ copyTypeInfo = M.getOrInsertFunction("copyTypeInfo",
+ VoidTy,
+ VoidPtrTy,/*dest ptr*/
+ VoidPtrTy,/*src ptr*/
+ Int64Ty,/*size*/
+ Int32Ty,/*tag*/
+ NULL);
+ trackStringInput = M.getOrInsertFunction("trackStringInput",
+ VoidTy,
+ VoidPtrTy,
+ Int32Ty,
+ NULL);
+
+ for(Value::use_iterator User = trackGlobal->use_begin(); User != trackGlobal->use_end(); ++User) {
+ CallInst *CI = dyn_cast<CallInst>(User);
+ assert(CI);
+
+ if(TS->isTypeSafe(cast<GlobalValue>(CI->getOperand(1)->stripPointerCasts()))) {
+ toDelete.push_back(CI);
+ }
+ }
+
+ for(Value::use_iterator User = trackLoadInst->use_begin(); User != trackLoadInst->use_end(); ++User) {
+ CallInst *CI = dyn_cast<CallInst>(User);
+ assert(CI);
+
+ if(TS->isTypeSafe(CI->getOperand(1)->stripPointerCasts(), CI->getParent()->getParent())) {
+ toDelete.push_back(CI);
+ }
+ }
+
+ for(Value::use_iterator User = trackStoreInst->use_begin(); User != trackStoreInst->use_end(); ++User) {
+ CallInst *CI = dyn_cast<CallInst>(User);
+ assert(CI);
+
+ if(TS->isTypeSafe(CI->getOperand(1)->stripPointerCasts(), CI->getParent()->getParent())) {
+ toDelete.push_back(CI);
+ }
+ }
+
+ for(Value::use_iterator User = trackInitInst->use_begin(); User != trackInitInst->use_end(); ++User) {
+ CallInst *CI = dyn_cast<CallInst>(User);
+ assert(CI);
+
+ if(TS->isTypeSafe(CI->getOperand(1)->stripPointerCasts(), CI->getParent()->getParent())) {
+ toDelete.push_back(CI);
+ }
+ }
+
+ for(Value::use_iterator User = trackArray->use_begin(); User != trackArray->use_end(); ++User) {
+ CallInst *CI = dyn_cast<CallInst>(User);
+ assert(CI);
+
+ if(TS->isTypeSafe(CI->getOperand(1)->stripPointerCasts(), CI->getParent()->getParent())) {
+ toDelete.push_back(CI);
+ }
+ }
+ for(Value::use_iterator User = copyTypeInfo->use_begin(); User != copyTypeInfo->use_end(); ++User) {
+ CallInst *CI = dyn_cast<CallInst>(User);
+ assert(CI);
+
+ if(TS->isTypeSafe(CI->getOperand(1)->stripPointerCasts(), CI->getParent()->getParent())) {
+ toDelete.push_back(CI);
+ continue;
+ }
+ if(TS->isTypeSafe(CI->getOperand(2)->stripPointerCasts(), CI->getParent()->getParent())) {
+ std::vector<Value*> Args;
+ Args.push_back(CI->getOperand(1));
+ Args.push_back(CI->getOperand(3)); // size
+ Args.push_back(CI->getOperand(4));
+ CallInst::Create(trackInitInst, Args.begin(), Args.end(), "", CI);
+ toDelete.push_back(CI);
+ }
+ }
+
+ numSafe += toDelete.size();
+
+ numSafe += toDelete.size();
+
+ while(!toDelete.empty()) {
+ Instruction *I = toDelete.back();
+ toDelete.pop_back();
+ I->eraseFromParent();
+ }
+
+ return modified;
+}
+
+
More information about the llvm-commits
mailing list