[llvm-commits] [poolalloc] r110606 - in /poolalloc/trunk: include/assistDS/Devirt.h lib/AssistDS/Devirt.cpp

John Criswell criswell at uiuc.edu
Mon Aug 9 14:27:42 PDT 2010


Author: criswell
Date: Mon Aug  9 16:27:42 2010
New Revision: 110606

URL: http://llvm.org/viewvc/llvm-project?rev=110606&view=rev
Log:
Refactored parts of the buildBounce() method so that it properly adds an entry
basic block to do initial casting and then comparison basic blocks that
actually handle the function selection and dispatch.

Modified:
    poolalloc/trunk/include/assistDS/Devirt.h
    poolalloc/trunk/lib/AssistDS/Devirt.cpp

Modified: poolalloc/trunk/include/assistDS/Devirt.h
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/include/assistDS/Devirt.h?rev=110606&r1=110605&r2=110606&view=diff
==============================================================================
--- poolalloc/trunk/include/assistDS/Devirt.h (original)
+++ poolalloc/trunk/include/assistDS/Devirt.h Mon Aug  9 16:27:42 2010
@@ -22,6 +22,7 @@
 #include "llvm/Instructions.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Support/InstVisitor.h"
+#include "llvm/Target/TargetData.h"
 
 using namespace llvm;
 
@@ -39,6 +40,9 @@
       // Access to analysis pass which finds targets of indirect function calls
       CallTargetFinder* CTF;
 
+      // Access to the target data analysis pass
+      TargetData * TD;
+
       // Worklist of call sites to transform
       std::vector<Instruction *> Worklist;
 
@@ -54,6 +58,7 @@
 
       virtual void getAnalysisUsage(AnalysisUsage &AU) const {
         AU.addRequired<CallTargetFinder>();
+        AU.addRequired<TargetData>();
       }
 
       // Visitor methods for analyzing instructions

Modified: poolalloc/trunk/lib/AssistDS/Devirt.cpp
URL: http://llvm.org/viewvc/llvm-project/poolalloc/trunk/lib/AssistDS/Devirt.cpp?rev=110606&r1=110605&r2=110606&view=diff
==============================================================================
--- poolalloc/trunk/lib/AssistDS/Devirt.cpp (original)
+++ poolalloc/trunk/lib/AssistDS/Devirt.cpp Mon Aug  9 16:27:42 2010
@@ -32,6 +32,21 @@
 X ("devirt", "Devirtualize indirect function calls");
 
 //
+// Function: getVoidPtrType()
+//
+// Description:
+//  Return a pointer to the LLVM type for a void pointer.
+//
+// Return value:
+//  A pointer to an LLVM type for the void pointer.
+//
+static inline
+PointerType * getVoidPtrType (LLVMContext & C) {
+  const Type * Int8Type  = IntegerType::getInt8Ty(C);
+  return PointerType::getUnqual(Int8Type);
+}
+
+//
 // Function: castTo()
 //
 // Description:
@@ -111,6 +126,12 @@
     }
 
   //
+  // Create an entry basic block for the function.  All it should do is perform
+  // some cast instructions and branch to the first comparison basic block.
+  //
+  BasicBlock* entryBB = BasicBlock::Create (M->getContext(), "entry", F);
+
+  //
   // For each function target, create a basic block that will call that
   // function directly.
   //
@@ -137,48 +158,70 @@
   }
 
   //
-  // Create a set of tests that search for the correct function target
-  // and call it directly.  If none of the target functions match,
-  // abort (or make the result unreachable).
+  // Create a failure basic block.  This basic block should simply be an
+  // unreachable instruction.
   //
+  BasicBlock * failBB = BasicBlock::Create (M->getContext(),
+                                            "fail",
+                                            F);
+  new UnreachableInst (M->getContext(), failBB);
 
   //
-  // Create the failure basic block.  This basic block should simply be an
-  // unreachable instruction.
+  // Setup the entry basic block.  For now, just have it call the failure
+  // basic block.  We'll change the basic block to which it branches later.
   //
-  BasicBlock* tail = BasicBlock::Create (M->getContext(),
-                                         "fail",
-                                         F,
-                                         &F->getEntryBlock());
-  Instruction * InsertPt;
-  InsertPt = new UnreachableInst (M->getContext(), tail);
-
-  //
-  // Create basic blocks for valid target functions.
-  //
-  const Type * Int8Type  = IntegerType::getInt8Ty(M->getContext());
-  const Type * VoidPtrTy = PointerType::getUnqual(Int8Type);
-  Value * FArg = CastInst::CreateZExtOrBitCast (F->arg_begin(),
-                                                VoidPtrTy,
-                                                "",
-                                                F->getEntryBlock().getTerminator());
+  BranchInst * InsertPt = BranchInst::Create (failBB, entryBB);
+
+  //
+  // Create basic blocks which will test the value of the incoming function
+  // pointer and branch to the appropriate basic block to call the function.
+  //
+  const Type * VoidPtrType = getVoidPtrType (M->getContext());
+  Value * FArg = castTo (F->arg_begin(), VoidPtrType, "", InsertPt);
+  BasicBlock * tailBB = failBB;
   for (unsigned index = 0; index < Targets.size(); ++index) {
+    //
+    // Cast the function pointer to an integer.  This can go in the entry
+    // block.
+    //
+    Value * TargetInt = castTo ((Value *)(Targets[index]),
+                                VoidPtrType,
+                                "",
+                                InsertPt);
+
+    //
+    // Create a new basic block that compares the function pointer to the
+    // function target.  If the function pointer matches, we'll branch to the
+    // basic block performing the direct call for that function; otherwise,
+    // we'll branch to the next function call target.
+    //
     BasicBlock* TB = targets[Targets[index]];
     BasicBlock* newB = BasicBlock::Create (M->getContext(),
                                            "test." + Targets[index]->getName(),
-                                           F,
-                                           &F->getEntryBlock());
-    Value * Target = (Value *) Targets[index];
-    Target = castTo (Target, VoidPtrTy, "", F->getEntryBlock().getTerminator());
+                                           F);
     CmpInst * setcc = CmpInst::Create (Instruction::ICmp,
                                        CmpInst::ICMP_EQ,
-                                       Target,
+                                       TargetInt,
                                        FArg,
                                        "sc",
                                        newB);
-    BranchInst::Create (TB, tail, setcc, newB);
-    tail = newB;
+    BranchInst::Create (TB, tailBB, setcc, newB);
+
+    //
+    // Make this newly created basic block the next block that will be reached
+    // when the next comparison will need to be done.
+    //
+    tailBB = newB;
   }
+
+  //
+  // Make the entry basic block branch to the first comparison basic block.
+  //
+  InsertPt->setUnconditionalDest (tailBB);
+
+  //
+  // Return the newly created bounce function.
+  //
   return F;
 }
 
@@ -296,6 +339,11 @@
   CTF = &getAnalysis<CallTargetFinder>();
 
   //
+  // Get information on the target system.
+  //
+  //
+  TD = &getAnalysis<TargetData>();
+
   // Visit all of the call instructions in this function and record those that
   // are indirect function calls.
   //





More information about the llvm-commits mailing list