[llvm-commits] CVS: llvm/lib/Reoptimizer/Mapping/getLLVMinfo.cpp

Brian Gaeke gaeke at cs.uiuc.edu
Wed Aug 20 14:25:01 PDT 2003


Changes in directory llvm/lib/Reoptimizer/Mapping:

getLLVMinfo.cpp updated: 1.16 -> 1.17

---
Log message:

Correct a typo in file comment.
Add more comments.
Delete some commented-out code and excess whitespace.
Make FunctionKey, BasicBlockKey, and llvmInstructionKey local to
 getLLVMInstrPositionInfo.
Correct typo: FunctionInlinelable -> FunctionInlinable.
Zap forward decls for 7 functions which are gone now.
Inline createFunctionKey, createBasicBlockKey, and
 createllvmInstructionKey into getLLVMInstrPositionInfo, and combine
 them into a single loop nest.
Inline getLLVMInstrInfo into getLLVMInstrPositionInfo.
Combine createBBMapRevF and createBBMapFwdF into a single function,
 called createBBMapRevFwdF.


---
Diffs of the changes:

Index: llvm/lib/Reoptimizer/Mapping/getLLVMinfo.cpp
diff -u llvm/lib/Reoptimizer/Mapping/getLLVMinfo.cpp:1.16 llvm/lib/Reoptimizer/Mapping/getLLVMinfo.cpp:1.17
--- llvm/lib/Reoptimizer/Mapping/getLLVMinfo.cpp:1.16	Wed Aug 20 13:17:57 2003
+++ llvm/lib/Reoptimizer/Mapping/getLLVMinfo.cpp	Wed Aug 20 14:23:57 2003
@@ -2,13 +2,12 @@
 //
 // Create Maps from the LLVM info in the .s file. These Maps map 
 // Basic Blocks to first and last Machine Instr and LLVM instructions 
-// to Machine Instructions. Defines query interfaec to query these maps
+// to Machine Instructions. Defines query interface to query these maps
 // F== Function, BB == BasicBlock, MI == MachineInstruction
 // LI = LLVM Instruction
 //
 //===--------------------------------------------------------------------===//
 
-
 #include "llvm/Reoptimizer/Mapping/LLVMinfo.h"
 #include "llvm/Module.h"
 #include <map>
@@ -16,9 +15,11 @@
 using std::pair;
 using std::vector;
 
+// Global symbols added to the program by the opt -emitfuncs pass:
 extern void** llvmFunctionTable[];
 extern char llvmSimpleFunction[];
 extern int llvmFunctionCount;
+// Global symbols added to the program by FunctionInfo pass:
 extern unsigned char* FunctionBB[];
 extern unsigned char* FunctionLI[];
 
@@ -26,28 +27,13 @@
 //reverse of BBMAP
 static map<unsigned, BasicBlock *> BBmapReverse;
 static map<unsigned/*BB*/,map<unsigned/*LI*/,vector<unsigned>/*MI*/ > > MImapF; 
-static map<Function*, unsigned> FunctionKey;
-static map<BasicBlock*, unsigned> BasicBlockKey;
-static map<Instruction*, unsigned> llvmInstructionKey;
 static map<uint64_t, Function *> revFunction;
 static map<BasicBlock *, std::pair<uint64_t, uint64_t> > BBMapFwd;
-static map<Function *, char> FunctionInlinelable;
+static map<Function *, char> FunctionInlinable;
 
 static void createBBmapF(unsigned char *BBinfo, unsigned length);
 static void createMImapF(unsigned char *LMIinfo, unsigned length);
-static unsigned getFunctionNo(Function *F);
-static unsigned getBasicBlockNo(BasicBlock *BB);
-static unsigned getLLVMInstrNo(Instruction *LI);
-static void createFunctionKey(Module *M);
-static void createBasicBlockKey(Module *M);
-static void createllvmInstructionKey(Module *M);
-static void createBBmapRevF(Module *M);
-static void createBBmapFwdF(Module *M);
-
-static vector<unsigned> &getLLVMInstrInfo(unsigned FunctionNo, 
-					 unsigned BasicBlockNo, 
-					 unsigned LLVMInstrNo);
-
+static void createBBmapRevFwdF(Module *M);
 
 /// readNumber - Read in a 32-bit number from the beginning of BBinfo,
 /// decoding it from the compressed encoding in which the mapping
@@ -64,8 +50,9 @@
   return Val + (*BBinfo++ << Shift);
 }
 
-/// getLLVMInstrPositionInfo - Look up the given Instruction using
-/// getLLVMInstrInfo (I'll figure this out later.)
+/// getLLVMInstrPositionInfo - Look up the given Instruction in the
+/// LLVM-to-MI map for the Function that contains it, and return the
+/// vector of integers (?) that represents it.
 ///
 vector<unsigned int> getLLVMInstrPositionInfo(Instruction *LI){
   // Get pointers to the BasicBlock, Function, and Module which contain LI.
@@ -73,17 +60,45 @@
   Function *F = BB->getParent();
   Module *M = F->getParent();
 
+  // Tables we use to look up Instructions for the current Module:
+  static map<Function*, unsigned> FunctionKey;
+  static map<BasicBlock*, unsigned> BasicBlockKey;
+  static map<Instruction*, unsigned> llvmInstructionKey;
+
+  // First time we see a given Module, we have to fill in the above tables.
   static Module *lastUsedModule = NULL;
   if (lastUsedModule != M) {
-    // First time we see a given Module, we have to create the tables
-    // we use to look up Instructions for that Module.
-    createFunctionKey(M);
-    createBasicBlockKey(M);
-    createllvmInstructionKey(M);
+    unsigned f = 0;
+    for(Module::iterator FI=M->begin(), FE=M->end(); FE!=FI; ++FI){
+      if(FI->isExternal()) continue;
+      FunctionKey[FI] = f;
+      unsigned b = 0;
+      for(Function::iterator BI=FI->begin(), BE=FI->end(); BE!=BI; ++BI){
+	BasicBlockKey[BI] = b;
+	unsigned i = 0;
+	for(BasicBlock::iterator II=BI->begin(), IE=BI->end(); IE!=II; ++II){
+	  llvmInstructionKey[II] = i;
+	  ++i;
+	}
+	++b;
+      }
+      ++f;
+    }
     lastUsedModule = M;
   }
 
-  return getLLVMInstrInfo(FunctionKey[F], BasicBlockKey[BB], llvmInstructionKey[LI]);
+  unsigned FunctionNo = FunctionKey[F];
+  static unsigned lastUsedFunctionNo = ~0;
+  if (lastUsedFunctionNo != FunctionNo) {
+    // First time we see a given Function, we have to initialize
+    // MImapF for that Function.
+    unsigned char *LIMap = FunctionLI[FunctionNo];
+    unsigned length = *(unsigned *)LIMap - 4;
+    createMImapF(&LIMap[4], length);
+    lastUsedFunctionNo = FunctionNo;
+  }
+
+  return MImapF[BasicBlockKey[BB]][llvmInstructionKey[LI]];
 }
 
 /// getBasicBlockInfo(BasicBlock *) - Find the given BasicBlock in the
@@ -100,7 +115,7 @@
   if (lastUsedModule != M) {
     // First time we see a given Module, create the table we use to
     // look up addresses for BasicBlocks in that Module.
-    createBBmapFwdF(M);
+    createBBmapRevFwdF(M);
     lastUsedModule = M;
   }
 
@@ -119,7 +134,7 @@
   if (lastUsedModule != M) {
     // First time we see a given Module, create the table we use to
     // look up BasicBlocks for addresses in that module.
-    createBBmapRevF(M);
+    createBBmapRevFwdF(M);
     lastUsedModule = M;
   }
 
@@ -130,179 +145,110 @@
   return false;
 }
 
-// getLLVMInstrInfo -
-//           Return MI no for the F no, BB no, LI No
-static vector<unsigned> &getLLVMInstrInfo(unsigned FunctionNo, 
-					 unsigned BasicBlockNo, 
-					 unsigned LLVMInstrNo){
-  static unsigned lastUsedFunctionNo = ~0;
-  if (lastUsedFunction != FunctionNo) {
-    unsigned char *LIMap = FunctionLI[FunctionNo];
-    unsigned length = *(unsigned *)LIMap - 4;
-    createMImapF(&LIMap[4], length);
-    lastUsedFunctionNo = FunctionNo;
-  }
-  return MImapF[BasicBlockNo][LLVMInstrNo];
-} 
 
-// createBBmap -
-//          create the BB map from the info in the .s file
-// It contains the F no, BB no, 1st MI no, no of MI in BB
-// for all F and BB
+/// createBBmapF - Create the BB map from the information stored in
+/// the executable.  It contains the function number, basic block
+/// number, 1st MI number, and the number of MIs in the basic block,
+/// for all functions and basic blocks.
+///
 static void createBBmapF(unsigned char *BBinfo, unsigned length){
-
   unsigned char *BBi = BBinfo;
   unsigned char *BBend = BBi+length;
   while (BBi != BBend){
     assert(BBi < BBend && "Trying to read off the end of list of BMIMap");
-    
+    // Read the encoded numbers out of the executable.
     unsigned BasicBlockNumber = readNumber(BBi);
-    //std::cerr<<"\tByte: "<<BasicBlockNumber<<"\n";
     unsigned StartNumber      = readNumber(BBi);
-    //std::cerr<<"\tByte: "<<StartNumber<<"\n";
     unsigned TotalNumber      = readNumber(BBi);
-    //std::cerr<<"\tByte: "<<TotalNumber<<"\n";
-    
+    // Fill in the map.
     BBmapF[BasicBlockNumber]=std::make_pair(StartNumber, TotalNumber);
   }
 }
 
-static void createBBmapRevF(Module *M){
+static void createBBmapRevFwdF(Module *M){
+  static Module *lastUsedModule = NULL;
+  if (lastUsedModule == M) return; // Already did this module.
   unsigned funNo = 0;
-  for(Module::iterator FI=M->begin(), FE=M->end(); 
-      FE!=FI; ++FI){
+  for(Module::iterator FI=M->begin(), FE=M->end(); FE!=FI; ++FI){
     if(FI->isExternal()) continue;
-    
     unsigned char *BBMap = FunctionBB[funNo];
     unsigned length = *(unsigned *)BBMap - 4;
-    
     createBBmapF(&BBMap[4], length);
-    
     unsigned bbNo = 0;
     for(Function::iterator BB = FI->begin(), BE = FI->end(); BB != BE; ++BB){
       uint64_t Faddr = (uint64_t)(intptr_t)llvmFunctionTable[funNo];
-      
       BBmapReverse[Faddr + BBmapF[bbNo].first*4] = BB;
-      bbNo++;
-    }
-    
-    funNo++;
-  }
-}
-
-static void createBBmapFwdF(Module *M){
-  unsigned funNo = 0;
-  for(Module::iterator FI=M->begin(), FE=M->end(); 
-      FE!=FI; ++FI){
-    if(FI->isExternal()) continue;
-    
-    unsigned char *BBMap = FunctionBB[funNo];
-    unsigned length = *(unsigned *)BBMap - 4;
-    
-    createBBmapF(&BBMap[4], length);
-    
-    unsigned bbNo = 0;
-    for(Function::iterator BB = FI->begin(), BE = FI->end(); BB != BE; ++BB){
-      uint64_t Faddr = (uint64_t)(intptr_t)llvmFunctionTable[funNo];
-      
       pair<unsigned, unsigned> bpr= BBmapF[bbNo];
       BBMapFwd[BB] = std::make_pair(Faddr + bpr.first*4, 
-				       Faddr + (bpr.first + bpr.second -1)*4);
+				    Faddr + (bpr.first + bpr.second -1)*4);
       bbNo++;
     }
-    
     funNo++;
   }
+  lastUsedModule = M;
 }
 
-// createMImap -
-//       create the MI map from the info on the .s file
-// It contains the F no, BB no, and MI [for all corresponding 
-// MIs] for all Fs and BBs
+/// createMImapF - Create the MI map from the information stored in the
+/// executable.  It contains the function number, basic block number,
+/// and MI [for all corresponding MIs] for all functions and basic
+/// blocks.
+///
 static void createMImapF(unsigned char *LMIinfo, unsigned length){
   unsigned i = 0; 
-  
   unsigned char *LIi = LMIinfo;
   unsigned char *LIend = LIi+length;
   
   while(LIi != LIend){
     assert(LIi < LIend && "Trying to read off the end of list of LMIMap");
-    
     unsigned BasicBlockNumber = readNumber(LIi);
-    //std::cerr<<"BasicBlockNumber= "<<BasicBlockNumber<<"\n";
     unsigned BasicBlockSize   = readNumber(LIi);    
-    //std::cerr<<"BasicBlockSize  = "<<BasicBlockSize<<"\n";
-    
     map<unsigned, vector<unsigned> > LImap;
     for(unsigned j=0; j<BasicBlockSize; ++j){    
-
       unsigned InstructionNumber =readNumber(LIi);
-      //std::cerr<<"InstructionNumber= "<<InstructionNumber<<"\n";
       unsigned InstructionSize   =readNumber(LIi);
-      //std::cerr<<"InstructionSize  = "<<InstructionSize<<"\n";
-      
       vector<unsigned> oneLI;
       for(unsigned  k=0; k<InstructionSize; ++k){
 	unsigned MachineInstruction=readNumber(LIi);
-	//std::cerr<<"MachineInstruction= "<<MachineInstruction<<"\n";
 	oneLI.push_back(MachineInstruction);
       }
       LImap[InstructionNumber]=oneLI;
     }
-    
     MImapF[BasicBlockNumber]=LImap;
   }
 }
 
-static void createFunctionKey(Module *M){
-  unsigned i = 0;
-  for(Module::iterator FI=M->begin(), FE=M->end(); 
-      FE!=FI; ++FI){
-    if(FI->isExternal()) continue;
-    FunctionKey[FI] = i;
-    ++i;
-  }
-}
-
-bool isInlinable(Function *F){
-  static bool initialize_inline = false;
-
-  if(!F)
-    return false;
-
-  if(F->isExternal())
+/// isInlinable - Looks up the given function in the
+/// llvmSimpleFunction table. llvmSimpleFunction and llvmFunctionCount
+/// are provided by the -emitfuncs pass.  Returns false for null or
+/// external functions.
+///
+bool isInlinable (Function *F) {
+  if ((!F) || F->isExternal ())
     return false;
 
-  if(!initialize_inline){
+  Module *M = F->getParent ();
+  static Module *lastUsedModule = NULL;
+  if (lastUsedModule != M) {
     int i = 0;
-
-    for(Module::iterator FI=F->getParent()->begin(), FE=F->getParent()->end(); 
-	FE!=FI; ++FI){
-      if(FI->isExternal()) continue;
-      //std::cerr<<llvmFunctionCount<<"\t"<<i<<"\n";
-
-      //std::cerr<<"--------------------------------------\n";
-      //std::cerr<<FI;
-
-      assert(i < llvmFunctionCount && "No such function in function table");
-      FunctionInlinelable[FI] = llvmSimpleFunction[i];
+    for (Module::iterator FI = M->begin (), FE = M->end (); FE != FI; ++FI) {
+      if (FI->isExternal ()) continue;
+      assert (i < llvmFunctionCount && "No such function in function table");
+      FunctionInlinable[FI] = llvmSimpleFunction[i];
       ++i;
     }
-    initialize_inline = true;
+    lastUsedModule = M;
   }
 
-  if(FunctionInlinelable[F])
-    return true;
-
-  return false;
+  return FunctionInlinable[F];
 }
 
-//given an address, get the Function that maps to the address
-//return NULL when its an external function
+/// getRevFunction - Given an address and a Module, get the Function
+/// that maps to the address.  Return NULL when it's an external
+/// function.
+///
 Function *getRevFunction(Module *M, uint64_t addr){
-  static bool initialized_RevFun = false;
-
-  if(!initialized_RevFun){
+  static Module *lastUsedModule = NULL;
+  if (lastUsedModule != M) {
     unsigned i = 0;
     for(Module::iterator FI=M->begin(), FE=M->end(); 
 	FE!=FI; ++FI){
@@ -310,44 +256,11 @@
       revFunction[(uint64_t)(intptr_t)llvmFunctionTable[i]] = (Function *)FI;
       ++i;
     }
-
-    initialized_RevFun = true; 
+    lastUsedModule = M;
   }
 
   if(revFunction.find(addr) != revFunction.end())
     return revFunction[addr];
 
   return NULL;
-}
-
-
-static void createBasicBlockKey(Module *M){
-  
-  for(Module::iterator FI=M->begin(), FE=M->end();
-      FE!=FI; ++FI){
-    if(FI->isExternal()) continue;
-    unsigned i =0;
-    for(Function::iterator BI=FI->begin(), BE=FI->end();
-	BE!=BI; ++BI){
-      BasicBlockKey[BI] = i;
-      ++i;
-    }
-  }
-}
-
-static void createllvmInstructionKey(Module *M){
-  
-  for(Module::iterator FI=M->begin(), FE=M->end();
-      FE!=FI; ++FI){
-    if(FI->isExternal()) continue;
-    for(Function::iterator BI=FI->begin(), BE=FI->end();
-	BE!=BI; ++BI){
-      unsigned i =0;
-      for(BasicBlock::iterator II=BI->begin(), IE=BI->end();
-	  IE!=II; ++II){
-	llvmInstructionKey[II] = i;
-	++i;
-      }
-    }
-  }
 }





More information about the llvm-commits mailing list