[llvm-commits] [poolalloc] r54815 - in /poolalloc/trunk: include/poolalloc/PoolAllocate.h lib/PoolAllocate/PoolAllocate.cpp lib/PoolAllocate/PoolOptimize.cpp lib/PoolAllocate/TransformFunctionBody.cpp

John Criswell criswell at uiuc.edu
Fri Aug 15 10:52:47 PDT 2008


Author: criswell
Date: Fri Aug 15 12:52:42 2008
New Revision: 54815

URL: http://llvm.org/viewvc/llvm-project?rev=54815&view=rev
Log:
Made the SAFECode options a run-time option that is configured when the
PoolAllocate pass is created.  This should make configuration easier.
Also removed code that ensures that instructions are added after allocas in
the entry basic block.  Such care should no longer be necessary.

Modified:
    poolalloc/trunk/include/poolalloc/PoolAllocate.h
    poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
    poolalloc/trunk/lib/PoolAllocate/PoolOptimize.cpp
    poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp

Modified: poolalloc/trunk/include/poolalloc/PoolAllocate.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/poolalloc/PoolAllocate.h?rev=54815&r1=54814&r2=54815&view=diff

==============================================================================
--- poolalloc/trunk/include/poolalloc/PoolAllocate.h (original)
+++ poolalloc/trunk/include/poolalloc/PoolAllocate.h Fri Aug 15 12:52:42 2008
@@ -77,12 +77,10 @@
     /// function.
     std::map<const DSNode*, Value*> PoolDescriptors;
 
-#ifdef SAFECODE
-    //This is a map from Old to New Value Map reverse of the one above
-    //Useful in SAFECode for check insertion
+    /// This is a map from Old to New Values (the reverse of NewToOldValueMap).
+    /// SAFECode uses this for check insertion.
     std::map<const Value*, Value*> ValueMap;
-#endif
-    
+
     /// NewToOldValueMap - When and if a function needs to be cloned, this map
     /// contains a mapping from all of the values in the new function back to
     /// the values they correspond to in the old function.
@@ -110,6 +108,8 @@
 public:
   static char ID;
   Constant *PoolRegister;
+  bool SAFECodeEnabled;
+  bool BoundsChecksEnabled;
 
   virtual ~PoolAllocateGroup () {return;}
   virtual PA::FuncInfo *getFuncInfo(Function &F) { return 0;}
@@ -169,13 +169,12 @@
 
  public:
   static char ID;
-#ifdef SAFECODE  
-  PoolAllocate(bool passAllArguments = true, intptr_t IDp = (intptr_t) (&ID))
-    : ModulePass((intptr_t)IDp), PassAllArguments(passAllArguments) {}
-#else
-  PoolAllocate(bool passAllArguments = false, intptr_t IDp = (intptr_t) (&ID))
-    : ModulePass((intptr_t)IDp), PassAllArguments(passAllArguments) {}
-#endif
+  PoolAllocate (bool passAllArguments = false,
+                bool SAFECode = false,
+                intptr_t IDp = (intptr_t) (&ID))
+    : ModulePass((intptr_t)IDp),
+      PassAllArguments(passAllArguments)
+      {SAFECodeEnabled = BoundsChecksEnabled = SAFECode;}
   virtual bool runOnModule(Module &M);
   
   virtual void getAnalysisUsage(AnalysisUsage &AU) const;
@@ -230,11 +229,10 @@
   /// getPoolType - Return the type of a pool descriptor
   const Type * getPoolType() {
     Type * VoidPtrType = PointerType::getUnqual(Type::Int8Ty);
-#ifdef SAFECODE
-    return ArrayType::get(VoidPtrType, 50);
-#else
-    return ArrayType::get(VoidPtrType, 16);
-#endif
+    if (SAFECodeEnabled)
+      return ArrayType::get(VoidPtrType, 50);
+    else
+      return ArrayType::get(VoidPtrType, 16);
   }
 
   virtual DSGraph & getDSGraph (const Function & F) const {
@@ -368,7 +366,8 @@
   TargetData * TD;
 public:
   static char ID;
-  PoolAllocateSimple() : PoolAllocate(false, (intptr_t)&ID) {}
+  PoolAllocateSimple(bool passAllArgs=false, bool SAFECode = false)
+    : PoolAllocate (passAllArgs, SAFECode, (intptr_t)&ID) {}
   ~PoolAllocateSimple() {return;}
   void getAnalysisUsage(AnalysisUsage &AU) const;
   bool runOnModule(Module &M);

Modified: poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp?rev=54815&r1=54814&r2=54815&view=diff

==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PoolAllocate.cpp Fri Aug 15 12:52:42 2008
@@ -93,14 +93,11 @@
 void PoolAllocate::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.addRequiredTransitive<EquivClassGraphs>();
   AU.addPreserved<EquivClassGraphs>();
-#ifdef SAFECODE  
-  //Dinakar for preserving the pool information across passes
-  AU.setPreservesAll();
-#endif  
-#ifdef BOUNDS_CHECK
-  //Dinakar hack for preserving the pool information across passes
-  AU.setPreservesAll();
-#endif
+
+  // Preserve the pool information across passes
+  if (SAFECodeEnabled)
+    AU.setPreservesAll();
+
   AU.addRequired<TargetData>();
   if (UseTDResolve)
     AU.addRequired<CallTargetFinder>();
@@ -225,11 +222,9 @@
   // Get the poolfree function.
   PoolFree = M->getOrInsertFunction("poolfree", Type::VoidTy,
                                             PoolDescPtrTy, VoidPtrTy, NULL);
-#if defined(SAFECODE) || defined(BOUNDS_CHECK)
   //Get the poolregister function
   PoolRegister = M->getOrInsertFunction("poolregister", Type::VoidTy,
                                  PoolDescPtrTy, VoidPtrTy, Type::Int32Ty, NULL);
-#endif
 }
 
 static void getCallsOf(Constant *C, std::vector<CallInst*> &Calls) {
@@ -413,11 +408,10 @@
   // Map the existing arguments of the old function to the corresponding
   // arguments of the new function, and copy over the names.
   DenseMap<const Value*, Value*> ValueMap;
-#ifdef SAFECODE  
-  for (std::map<const Value*, Value*>::iterator I = FI.ValueMap.begin(),
-         E = FI.ValueMap.end(); I != E; ++I)
-    ValueMap.insert(std::make_pair(I->first, I->second));
-#endif  
+  if (SAFECodeEnabled)
+    for (std::map<const Value*, Value*>::iterator I = FI.ValueMap.begin(),
+           E = FI.ValueMap.end(); I != E; ++I)
+      ValueMap.insert(std::make_pair(I->first, I->second));
   for (Function::arg_iterator I = F.arg_begin();
        NI != New->arg_end(); ++I, ++NI) {
     ValueMap[I] = NI;
@@ -486,12 +480,19 @@
   for (hash_set<const DSNode*>::iterator I = GlobalHeapNodes.begin(),
          E = GlobalHeapNodes.end(); I != E; ) {
     hash_set<const DSNode*>::iterator Last = I++;
-#ifndef SAFECODE
-#ifndef BOUNDS_CHECK    
-    //    if (!(*Last)->isHeapNode());
-    //       GlobalHeapNodes.erase(Last);
-#endif       
+
+#if 0
+    //
+    // FIXME:
+    //  This code was disabled for regular pool allocation, but I don't know
+    //  why.
+    //
+    if (!SAFECodeEnabled) {
+      if (!(*Last)->isHeapNode());
+      GlobalHeapNodes.erase(Last);
+    }
 #endif
+
     const DSNode *tmp = *Last;
     //    std::cerr << "test \n";
     if (!(tmp->isHeapNode() || tmp->isArray()))
@@ -516,11 +517,7 @@
   CurHeuristic->AssignToPools(NodesToPA, 0, GG, ResultPools);
 
   BasicBlock::iterator InsertPt = MainFunc->getEntryBlock().begin();
-#ifndef SAFECODE
-#ifndef BOUNDS_CHECK  
-  while (isa<AllocaInst>(InsertPt)) ++InsertPt;
-#endif  
-#endif
+
   // Perform all global assignments as specified.
   for (unsigned i = 0, e = ResultPools.size(); i != e; ++i) {
     Heuristic::OnePool &Pool = ResultPools[i];
@@ -600,11 +597,7 @@
   std::set<const DSNode*> UnallocatedNodes(NodesToPA.begin(), NodesToPA.end());
 
   BasicBlock::iterator InsertPoint = F.front().begin();
-#ifndef SAFECODE
-#ifndef BOUNDS_CHECK  
-  while (isa<AllocaInst>(InsertPoint)) ++InsertPoint;
-#endif
-#endif  
+
   // Is this main?  If so, make the pool descriptors globals, not automatic
   // vars.
   bool IsMain = F.getName() == "main" && F.hasExternalLinkage();
@@ -675,11 +668,7 @@
   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;
-#ifdef BOUNDS_CHECK
-    if ((N->isArray()) || (N->isHeapNode()))
-#else
-    if (N->isHeapNode())
-#endif		 
+    if ((N->isHeapNode()) || (BoundsChecksEnabled && (N->isArray())))
       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=54815&r1=54814&r2=54815&view=diff

==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/PoolOptimize.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/PoolOptimize.cpp Fri Aug 15 12:52:42 2008
@@ -26,7 +26,11 @@
 
   struct PoolOptimize : public ModulePass {
     static char ID;
-    PoolOptimize() : ModulePass((intptr_t)&ID) {}
+    bool SAFECodeEnabled;
+
+    PoolOptimize(bool SAFECode = false) : ModulePass((intptr_t)&ID) {
+      SAFECodeEnabled = SAFECode;
+    }
     bool runOnModule(Module &M);
   };
 
@@ -51,12 +55,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
-
+  const Type *PoolDescPtrTy;
+  if (SAFECodeEnabled)
+    PoolDescPtrTy = PointerType::getUnqual(ArrayType::get(VoidPtrTy, 50));
+  else
+    PoolDescPtrTy = PointerType::getUnqual(ArrayType::get(VoidPtrTy, 16));
 
   // Get poolinit function.
   Constant *PoolInit = M.getOrInsertFunction("poolinit", Type::VoidTy,

Modified: poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp?rev=54815&r1=54814&r2=54815&view=diff

==============================================================================
--- poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp (original)
+++ poolalloc/trunk/lib/PoolAllocate/TransformFunctionBody.cpp Fri Aug 15 12:52:42 2008
@@ -208,50 +208,52 @@
   if (MI.isArrayAllocation())
     AllocSize = BinaryOperator::create(Instruction::Mul, AllocSize,
                                        MI.getOperand(0), "sizetmp", &MI);
-//
-// NOTE:
-//  The code below used to be used by SAFECode.  However, it requires
-//  Pool Allocation to depend upon SAFECode passes, which is messy.
-//
-//  I believe the code below is an unneeded optimization.  Basically, when
-//  SAFECode promotes a stack allocation to the heap, this makes it a stack
-//  allocation again if the DSNode has no heap allocations.  This seems to be
-//  a performance optimization and unnecessary for the first prototype.
-//
-#ifdef SAFECODE
+  //
+  // NOTE:
+  //  The code below used to be used by SAFECode.  However, it requires
+  //  Pool Allocation to depend upon SAFECode passes, which is messy.
+  //
+  //  I believe the code below is an unneeded optimization.  Basically, when
+  //  SAFECode promotes a stack allocation to the heap, this makes it a stack
+  //  allocation again if the DSNode has no heap allocations.  This seems to be
+  //  a performance optimization and unnecessary for the first prototype.
+  //
+  if (PAInfo.SAFECodeEnabled) {
 #if 0
-  const MallocInst *originalMalloc = &MI;
-  if (FI.NewToOldValueMap.count(&MI)) {
-    originalMalloc = cast<MallocInst>(FI.NewToOldValueMap[&MI]);
-  }
-  //Dinakar to test stack safety & array safety 
-  if (PAInfo.CUAPass->ArrayMallocs.find(originalMalloc) ==
-      PAInfo.CUAPass->ArrayMallocs.end()) {
-    TransformAllocationInstr(&MI, AllocSize);
-  } else {
-    AllocaInst *AI = new AllocaInst(MI.getType()->getElementType(), MI.getArraySize(), MI.getName(), MI.getNext());
-    MI.replaceAllUsesWith(AI);
-    MI.getParent()->getInstList().erase(&MI);
-    Value *Casted = AI;
-    Instruction *aiNext = AI->getNext();
-    if (AI->getType() != PointerType::getUnqual(Type::Int8Ty))
-      Casted = CastInst::createPointerCast(AI, PointerType::getUnqual(Type::Int8Ty),
-			      AI->getName()+".casted",aiNext);
-    
-    Instruction *V = CallInst::Create(PAInfo.PoolRegister,
-				  make_vector(PH, AllocSize, Casted, 0), "", aiNext);
-    AddPoolUse(*V, PH, PoolUses);
-  }
-#else
-  TransformAllocationInstr(&MI, AllocSize);  
-#endif
+    const MallocInst *originalMalloc = &MI;
+    if (FI.NewToOldValueMap.count(&MI)) {
+      originalMalloc = cast<MallocInst>(FI.NewToOldValueMap[&MI]);
+    }
+    //Dinakar to test stack safety & array safety 
+    if (PAInfo.CUAPass->ArrayMallocs.find(originalMalloc) ==
+        PAInfo.CUAPass->ArrayMallocs.end()) {
+      TransformAllocationInstr(&MI, AllocSize);
+    } else {
+      AllocaInst *AI = new AllocaInst(MI.getType()->getElementType(), MI.getArraySize(), MI.getName(), MI.getNext());
+      MI.replaceAllUsesWith(AI);
+      MI.getParent()->getInstList().erase(&MI);
+      Value *Casted = AI;
+      Instruction *aiNext = AI->getNext();
+      if (AI->getType() != PointerType::getUnqual(Type::Int8Ty))
+        Casted = CastInst::createPointerCast(AI, PointerType::getUnqual(Type::Int8Ty),
+              AI->getName()+".casted",aiNext);
+      
+      Instruction *V = CallInst::Create(PAInfo.PoolRegister,
+            make_vector(PH, AllocSize, Casted, 0), "", aiNext);
+      AddPoolUse(*V, PH, PoolUses);
+    }
 #else
-  TransformAllocationInstr(&MI, AllocSize);  
+    TransformAllocationInstr(&MI, AllocSize);  
 #endif
+  } else {
+    TransformAllocationInstr(&MI, AllocSize);  
+  }
 }
 
 void FuncTransform::visitAllocaInst(AllocaInst &MI) {
-#ifdef BOUNDS_CHECK
+  // Don't do anything if bounds checking will not be done by SAFECode later.
+  if (!(PAInfo.BoundsChecksEnabled)) return;
+
   // Get the pool handle for the node that this contributes to...
   DSNode *Node = getDSNodeHFor(&MI).getNode();
   if (Node->isArray()) {
@@ -278,7 +280,6 @@
 				  args.begin(), args.end(), "", InsertPt);
     AddPoolUse(*V, PH, PoolUses);
   }
-#endif  
 }
 
 
@@ -728,28 +729,24 @@
         if (FI.PoolDescriptors.count(LocalNode))
           ArgVal = FI.PoolDescriptors.find(LocalNode)->second;
     if (isa<Constant>(ArgVal) && cast<Constant>(ArgVal)->isNullValue()) {
-#ifdef BOUNDS_CHECK
-      if (ArgNodes[i]->isArray()) {
-#endif
-      if (!isa<InvokeInst>(TheCall)) {
-        // Dinakar: We need pooldescriptors for allocas in the callee if it
-        //          escapes
-        BasicBlock::iterator InsertPt = TheCall->getParent()->getParent()->front().begin();
-        Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
-        ArgVal =  new AllocaInst(PAInfo.getPoolType(), 0, "PD", InsertPt);
-        Value *ElSize = ConstantInt::get(Type::Int32Ty,0);
-        Value *Align  = ConstantInt::get(Type::Int32Ty,0);
-        Value* Opts[3] = {ArgVal, ElSize, Align};
-        CallInst::Create(PAInfo.PoolInit, Opts, Opts + 3,"", TheCall);
-        BasicBlock::iterator BBI = TheCall;
-        CallInst::Create(PAInfo.PoolDestroy, ArgVal, "", ++BBI);
-      }
+      if ((!(PAInfo.BoundsChecksEnabled)) || (ArgNodes[i]->isArray())) {
+        if (!isa<InvokeInst>(TheCall)) {
+          // Dinakar: We need pooldescriptors for allocas in the callee if it
+          //          escapes
+          BasicBlock::iterator InsertPt = TheCall->getParent()->getParent()->front().begin();
+          Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
+          ArgVal =  new AllocaInst(PAInfo.getPoolType(), 0, "PD", InsertPt);
+          Value *ElSize = ConstantInt::get(Type::Int32Ty,0);
+          Value *Align  = ConstantInt::get(Type::Int32Ty,0);
+          Value* Opts[3] = {ArgVal, ElSize, Align};
+          CallInst::Create(PAInfo.PoolInit, Opts, Opts + 3,"", TheCall);
+          BasicBlock::iterator BBI = TheCall;
+          CallInst::Create(PAInfo.PoolDestroy, ArgVal, "", ++BBI);
+        }
 
-      //probably need to update DSG
-      //      std::cerr << "WARNING: NULL POOL ARGUMENTS ARE PASSED IN!\n";
-#ifdef BOUNDS_CHECK
+        //probably need to update DSG
+        //      std::cerr << "WARNING: NULL POOL ARGUMENTS ARE PASSED IN!\n";
       }
-#endif
     }
     Args.push_back(ArgVal);
   }





More information about the llvm-commits mailing list