[llvm-commits] [poolalloc] r128794 - in /poolalloc/trunk/lib/AssistDS: TypeChecks.cpp TypeChecks.h

Brice Lin Brice.Lin at gmail.com
Sun Apr 3 13:01:34 PDT 2011


Author: bglin2
Date: Sun Apr  3 15:01:34 2011
New Revision: 128794

URL: http://llvm.org/viewvc/llvm-project?rev=128794&view=rev
Log:
Modified the dynamic check insertion pass to call the shadow memory runtime functions.

Modified:
    poolalloc/trunk/lib/AssistDS/TypeChecks.cpp
    poolalloc/trunk/lib/AssistDS/TypeChecks.h

Modified: poolalloc/trunk/lib/AssistDS/TypeChecks.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/TypeChecks.cpp?rev=128794&r1=128793&r2=128794&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/TypeChecks.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/TypeChecks.cpp Sun Apr  3 15:01:34 2011
@@ -24,6 +24,11 @@
 char TypeChecks::ID = 0;
 static RegisterPass<TypeChecks> TC("typechecks", "Insert runtime type checks", false, true);
 
+static const Type *VoidTy = 0;
+static const Type *Int8Ty = 0;
+static const Type *Int32Ty = 0;
+static const PointerType *VoidPtrTy = 0;
+
 // Incorporate one type and all of its subtypes into the collection of used types.
 void TypeChecks::IncorporateType(const Type *Ty) {
   // If Ty doesn't already exist in the used types map, add it now. Otherwise, return.
@@ -56,8 +61,13 @@
 }
 
 bool TypeChecks::runOnModule(Module &M) {
-  // Flags whether we modified the module.
-  bool modified = false;
+  bool modified = false; // Flags whether we modified the module.
+  bool firstSI = true;
+
+  VoidTy = IntegerType::getVoidTy(M.getContext());
+  Int8Ty = IntegerType::getInt8Ty(M.getContext());
+  Int32Ty = IntegerType::getInt32Ty(M.getContext());
+  VoidPtrTy = PointerType::getUnqual(Int8Ty);
 
   UsedTypes.clear(); // Reset if run multiple times.
   maxType = 1;
@@ -84,7 +94,20 @@
       }
 
       if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
+        if (firstSI) {
+          modified |= initShadow(M, *SI);
+          firstSI = false;
+        }
+
         modified |= visitStoreInst(M, *SI);
+      } else if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
+        // Unlikely, but just in case
+        if (firstSI) {
+          modified |= initShadowLI(M, *LI);
+          firstSI = false;
+        }
+
+        modified |= visitLoadInst(M, *LI);
       }
     }
   }
@@ -113,20 +136,60 @@
   OS << "\nNumber of types: " << maxType << '\n';
 }
 
+// Initialize the shadow memory which contains the 1:1 mapping.
+bool TypeChecks::initShadow(Module &M, StoreInst &SI) {
+  // Create the call to the runtime initialization function and place it before the store instruction.
+  Constant *F = M.getOrInsertFunction("shadowInit", VoidTy, NULL);
+  CallInst::Create(F, "", &SI);
+
+  return true;
+}
+
+// Initialize the shadow memory which contains the 1:1 mapping.
+bool TypeChecks::initShadowLI(Module &M, LoadInst &LI) {
+  // Create the call to the runtime initialization function and place it before the load instruction.
+  Constant *F = M.getOrInsertFunction("shadowInit", VoidTy, NULL);
+  CallInst::Create(F, "", &LI);
+
+  return true;
+}
+
+// Initialize the shadow memory which contains the 1:1 mapping.
+bool TypeChecks::unmapShadow(Module &M, Instruction &I) {
+  // Create the call to the runtime shadow memory unmap function and place it before any exiting instruction.
+  Constant *F = M.getOrInsertFunction("shadowUnmap", VoidTy, NULL);
+  CallInst::Create(F, "", &I);
+
+  return true;
+}
+
+// Insert runtime checks before all load instructions.
+bool TypeChecks::visitLoadInst(Module &M, LoadInst &LI) {
+  // Cast the pointer operand to i8* for the runtime function.
+  CastInst *BCI = BitCastInst::CreatePointerCast(LI.getPointerOperand(), VoidPtrTy, "", &LI);
+
+  std::vector<Value *> Args;
+  Args.push_back(BCI);
+  Args.push_back(ConstantInt::get(Int32Ty, UsedTypes[LI.getType()]));
+
+  // Create the call to the runtime check and place it before the load instruction.
+  Constant *F = M.getOrInsertFunction("trackLoadInst", Int32Ty, VoidPtrTy, Int32Ty, NULL);
+  CallInst::Create(F, Args.begin(), Args.end(), "", &LI);
+
+  return true;
+}
+
 // Insert runtime checks before all store instructions.
 bool TypeChecks::visitStoreInst(Module &M, StoreInst &SI) {
-  const Type *Int8Ty = IntegerType::getInt8Ty(M.getContext());
-  const Type *Int32Ty = IntegerType::getInt32Ty(M.getContext());
-  PointerType *VoidPtrTy = PointerType::getUnqual(Int8Ty);
-
+  // Cast the pointer operand to i8* for the runtime function.
   CastInst *BCI = BitCastInst::CreatePointerCast(SI.getPointerOperand(), VoidPtrTy, "", &SI);
 
   std::vector<Value *> Args;
   Args.push_back(BCI);
   Args.push_back(ConstantInt::get(Int32Ty, UsedTypes[SI.getOperand(0)->getType()])); // SI.getValueOperand()
 
-  // Create the call to the runtime check, and place it before the store instruction.
-  Constant *F = M.getOrInsertFunction("trackStoreInst", Int8Ty, VoidPtrTy, Int32Ty, NULL);
+  // Create the call to the runtime check and place it before the store instruction.
+  Constant *F = M.getOrInsertFunction("trackStoreInst", Int32Ty, VoidPtrTy, Int32Ty, NULL);
   CallInst::Create(F, Args.begin(), Args.end(), "", &SI);
 
   return true;

Modified: poolalloc/trunk/lib/AssistDS/TypeChecks.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/TypeChecks.h?rev=128794&r1=128793&r2=128794&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/TypeChecks.h (original)
+++ poolalloc/trunk/lib/AssistDS/TypeChecks.h Sun Apr  3 15:01:34 2011
@@ -45,6 +45,10 @@
   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
   }
 
+  bool initShadow(Module &M, StoreInst &SI);
+  bool initShadowLI(Module &M, LoadInst &LI);
+  bool unmapShadow(Module &M, Instruction &I);
+  bool visitLoadInst(Module &M, LoadInst &LI);
   bool visitStoreInst(Module &M, StoreInst &SI);
 
   // Return the map containing all of the types used in the module.





More information about the llvm-commits mailing list