Replacing std::iostreams with llvm iostreams. Some of these changes involve
[oota-llvm.git] / lib / Bytecode / Writer / SlotCalculator.cpp
index 4a95c7b80f9f2017ce1ec1c7345fc710814a4b9b..efb6c436cd24213fb1c216229a7a051ef8181277 100644 (file)
@@ -1,10 +1,10 @@
 //===-- SlotCalculator.cpp - Calculate what slots values land in ----------===//
-// 
+//
 //                     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 file implements a useful analysis step to figure out what numbered slots
@@ -18,6 +18,7 @@
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Function.h"
+#include "llvm/InlineAsm.h"
 #include "llvm/Instructions.h"
 #include "llvm/Module.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/ADT/STLExtras.h"
 #include <algorithm>
 #include <functional>
-
 using namespace llvm;
 
 #if 0
-#include <iostream>
-#define SC_DEBUG(X) std::cerr << X
+#include "llvm/Support/Streams.h"
+#define SC_DEBUG(X) llvm_cerr << X
 #else
 #define SC_DEBUG(X)
 #endif
@@ -121,8 +121,8 @@ void SlotCalculator::processModule() {
 
   // Add all of the global variables to the value table...
   //
-  for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
-       I != E; ++I)
+  for (Module::const_global_iterator I = TheModule->global_begin(),
+         E = TheModule->global_end(); I != E; ++I)
     getOrCreateSlot(I);
 
   // Scavenge the types out of the functions, then add the functions themselves
@@ -134,8 +134,8 @@ void SlotCalculator::processModule() {
 
   // Add all of the module level constants used as initializers
   //
-  for (Module::const_giterator I = TheModule->gbegin(), E = TheModule->gend();
-       I != E; ++I)
+  for (Module::const_global_iterator I = TheModule->global_begin(),
+         E = TheModule->global_end(); I != E; ++I)
     if (I->hasInitializer())
       getOrCreateSlot(I->getInitializer());
 
@@ -150,15 +150,15 @@ void SlotCalculator::processModule() {
         TypePlane &Plane = Table[plane];
         unsigned FirstNonStringID = 0;
         for (unsigned i = 0, e = Plane.size(); i != e; ++i)
-          if (isa<ConstantAggregateZero>(Plane[i]) || 
-             (isa<ConstantArray>(Plane[i]) &&
+          if (isa<ConstantAggregateZero>(Plane[i]) ||
+              (isa<ConstantArray>(Plane[i]) &&
                cast<ConstantArray>(Plane[i])->isString())) {
             // Check to see if we have to shuffle this string around.  If not,
             // don't do anything.
             if (i != FirstNonStringID) {
               // Swap the plane entries....
               std::swap(Plane[i], Plane[FirstNonStringID]);
-              
+
               // Keep the NodeMap up to date.
               NodeMap[Plane[i]] = i;
               NodeMap[Plane[FirstNonStringID]] = FirstNonStringID;
@@ -167,28 +167,28 @@ void SlotCalculator::processModule() {
           }
       }
   }
-  
-  // Scan all of the functions for their constants, which allows us to emit 
-  // more compact modules.  This is optional, and is just used to compactify 
+
+  // Scan all of the functions for their constants, which allows us to emit
+  // more compact modules.  This is optional, and is just used to compactify
   // the constants used by different functions together.
   //
-  // This functionality tends to produce smaller bytecode files.  This should 
-  // not be used in the future by clients that want to, for example, build and 
-  // emit functions on the fly.  For now, however, it is unconditionally 
+  // This functionality tends to produce smaller bytecode files.  This should
+  // not be used in the future by clients that want to, for example, build and
+  // emit functions on the fly.  For now, however, it is unconditionally
   // enabled.
   ModuleContainsAllFunctionConstants = true;
 
   SC_DEBUG("Inserting function constants:\n");
   for (Module::const_iterator F = TheModule->begin(), E = TheModule->end();
        F != E; ++F) {
-    for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I){
-      for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
-        if (isa<Constant>(I->getOperand(op)) && 
-            !isa<GlobalValue>(I->getOperand(op)))
-          getOrCreateSlot(I->getOperand(op));
+    for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
+      for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); 
+           OI != E; ++OI) {
+        if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
+            isa<InlineAsm>(*OI))
+          getOrCreateSlot(*OI);
+      }
       getOrCreateSlot(I->getType());
-      if (const VANextInst *VAN = dyn_cast<VANextInst>(&*I))
-        getOrCreateSlot(VAN->getArgType());
     }
     processSymbolTableConstants(&F->getSymbolTable());
   }
@@ -244,7 +244,7 @@ void SlotCalculator::processSymbolTable(const SymbolTable *ST) {
     getOrCreateSlot(TI->second);
 
   // Now do the values.
-  for (SymbolTable::plane_const_iterator PI = ST->plane_begin(), 
+  for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
        PE = ST->plane_end(); PI != PE; ++PI)
     for (SymbolTable::value_const_iterator VI = PI->second.begin(),
            VE = PI->second.end(); VI != VE; ++VI)
@@ -258,7 +258,7 @@ void SlotCalculator::processSymbolTableConstants(const SymbolTable *ST) {
     getOrCreateSlot(TI->second);
 
   // Now do the constant values in all planes
-  for (SymbolTable::plane_const_iterator PI = ST->plane_begin(), 
+  for (SymbolTable::plane_const_iterator PI = ST->plane_begin(),
        PE = ST->plane_end(); PI != PE; ++PI)
     for (SymbolTable::value_const_iterator VI = PI->second.begin(),
            VE = PI->second.end(); VI != VE; ++VI)
@@ -275,7 +275,7 @@ void SlotCalculator::incorporateFunction(const Function *F) {
   SC_DEBUG("begin processFunction!\n");
 
   // If we emitted all of the function constants, build a compaction table.
-  if ( ModuleContainsAllFunctionConstants)
+  if (ModuleContainsAllFunctionConstants)
     buildCompactionTable(F);
 
   // Update the ModuleLevel entries to be accurate.
@@ -285,28 +285,25 @@ void SlotCalculator::incorporateFunction(const Function *F) {
   ModuleTypeLevel = Types.size();
 
   // Iterate over function arguments, adding them to the value table...
-  for(Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
+  for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E; ++I)
     getOrCreateSlot(I);
 
-  if ( !ModuleContainsAllFunctionConstants ) {
+  if (!ModuleContainsAllFunctionConstants) {
     // Iterate over all of the instructions in the function, looking for
     // constant values that are referenced.  Add these to the value pools
     // before any nonconstant values.  This will be turned into the constant
     // pool for the bytecode writer.
     //
-    
+
     // Emit all of the constants that are being used by the instructions in
     // the function...
-    constant_iterator CI = constant_begin(F);
-    constant_iterator CE = constant_end(F);
-    while ( CI != CE ) {
-      this->getOrCreateSlot(*CI);
-      ++CI;
-    }
-    
+    for (constant_iterator CI = constant_begin(F), CE = constant_end(F);
+         CI != CE; ++CI)
+      getOrCreateSlot(*CI);
+
     // If there is a symbol table, it is possible that the user has names for
     // constants that are not being used.  In this case, we will have problems
-    // if we don't emit the constants now, because otherwise we will get 
+    // if we don't emit the constants now, because otherwise we will get
     // symbol table references to constants not in the output.  Scan for these
     // constants now.
     //
@@ -320,8 +317,6 @@ void SlotCalculator::incorporateFunction(const Function *F) {
     getOrCreateSlot(BB);
     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
       getOrCreateSlot(I);
-      if (const VANextInst *VAN = dyn_cast<VANextInst>(I))
-        getOrCreateSlot(VAN->getArgType());
     }
   }
 
@@ -380,7 +375,7 @@ void SlotCalculator::purgeFunction() {
         NodeMap.erase(Plane.back());   // Erase from nodemap
         Plane.pop_back();              // Shrink plane
       }
-      
+
       Table.pop_back();                // Nuke the plane, we don't like it.
     }
   }
@@ -388,8 +383,8 @@ void SlotCalculator::purgeFunction() {
   SC_DEBUG("end purgeFunction!\n");
 }
 
-static inline bool hasNullValue(unsigned TyID) {
-  return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
+static inline bool hasNullValue(const Type *Ty) {
+  return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
 }
 
 /// getOrCreateCompactionTableSlot - This method is used to build up the initial
@@ -413,7 +408,7 @@ unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
 
   // Make sure to insert the null entry if the thing we are inserting is not a
   // null constant.
-  if (TyPlane.empty() && hasNullValue(V->getType()->getTypeID())) {
+  if (TyPlane.empty() && hasNullValue(V->getType())) {
     Value *ZeroInitializer = Constant::getNullValue(V->getType());
     if (V != ZeroInitializer) {
       TyPlane.push_back(ZeroInitializer);
@@ -461,7 +456,8 @@ void SlotCalculator::buildCompactionTable(const Function *F) {
   }
 
   // Next, include any types used by function arguments.
-  for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
+  for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
+       I != E; ++I)
     getOrCreateCompactionTableSlot(I->getType());
 
   // Next, find all of the types and values that are referred to by the
@@ -469,10 +465,8 @@ void SlotCalculator::buildCompactionTable(const Function *F) {
   for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
     getOrCreateCompactionTableSlot(I->getType());
     for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
-      if (isa<Constant>(I->getOperand(op)))
+      if (isa<Constant>(I->getOperand(op)) || isa<InlineAsm>(I->getOperand(op)))
         getOrCreateCompactionTableSlot(I->getOperand(op));
-    if (const VANextInst *VAN = dyn_cast<VANextInst>(&*I))
-      getOrCreateCompactionTableSlot(VAN->getArgType());
   }
 
   // Do the types in the symbol table
@@ -482,7 +476,7 @@ void SlotCalculator::buildCompactionTable(const Function *F) {
     getOrCreateCompactionTableSlot(TI->second);
 
   // Now do the constants and global values
-  for (SymbolTable::plane_const_iterator PI = ST.plane_begin(), 
+  for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
        PE = ST.plane_end(); PI != PE; ++PI)
     for (SymbolTable::value_const_iterator VI = PI->second.begin(),
            VE = PI->second.end(); VI != VE; ++VI)
@@ -503,14 +497,14 @@ void SlotCalculator::buildCompactionTable(const Function *F) {
       assert(Ty->getTypeID() != Type::LabelTyID);
       getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
     }
-  
+
   // Okay, now at this point, we have a legal compaction table.  Since we want
   // to emit the smallest possible binaries, do not compactify the type plane if
   // it will not save us anything.  Because we have not yet incorporated the
   // function body itself yet, we don't know whether or not it's a good idea to
   // compactify other planes.  We will defer this decision until later.
   TypeList &GlobalTypes = Types;
-  
+
   // All of the values types will be scrunched to the start of the types plane
   // of the global table.  Figure out just how many there are.
   assert(!GlobalTypes.empty() && "No global types???");
@@ -530,7 +524,7 @@ void SlotCalculator::buildCompactionTable(const Function *F) {
     std::swap(CompactionTable, TmpCompactionTable);
     TypeList TmpTypes;
     std::swap(TmpTypes, CompactionTypes);
-    
+
     // Move each plane back over to the uncompactified plane
     while (!TmpTypes.empty()) {
       const Type *Ty = TmpTypes.back();
@@ -540,7 +534,7 @@ void SlotCalculator::buildCompactionTable(const Function *F) {
       // Find the global slot number for this type.
       int TySlot = getSlot(Ty);
       assert(TySlot != -1 && "Type doesn't exist in global table?");
-      
+
       // Now we know where to put the compaction table plane.
       if (CompactionTable.size() <= unsigned(TySlot))
         CompactionTable.resize(TySlot+1);
@@ -575,7 +569,7 @@ void SlotCalculator::pruneCompactionTable() {
       if (GlobalSlot >= Table.size())
         Table.resize(GlobalSlot+1);
       TypePlane &GPlane = Table[GlobalSlot];
-      
+
       unsigned ModLevel = getModuleLevel(ctp);
       unsigned NumFunctionObjs = CPlane.size()-ModLevel;
 
@@ -624,7 +618,7 @@ void SlotCalculator::pruneCompactionTable() {
 }
 
 /// Determine if the compaction table is actually empty. Because the
-/// compaction table always includes the primitive type planes, we 
+/// compaction table always includes the primitive type planes, we
 /// can't just check getCompactionTable().size() because it will never
 /// be zero. Furthermore, the ModuleLevel factors into whether a given
 /// plane is empty or not. This function does the necessary computation
@@ -640,7 +634,7 @@ bool SlotCalculator::CompactionTableIsEmpty() const {
       // If the module level is non-zero then at least the
       // first element of the plane is valid and therefore not empty.
       unsigned End = getModuleLevel(i);
-      if (End != 0) 
+      if (End != 0)
         return false;
     }
   }
@@ -699,8 +693,8 @@ int SlotCalculator::getOrCreateSlot(const Value *V) {
       assert(CompactionNodeMap.empty() &&
              "All needed constants should be in the compaction map already!");
 
-      // Do not index the characters that make up constant strings.  We emit 
-      // constant strings as special entities that don't require their 
+      // Do not index the characters that make up constant strings.  We emit
+      // constant strings as special entities that don't require their
       // individual characters to be emitted.
       if (!isa<ConstantArray>(C) || !cast<ConstantArray>(C)->isString()) {
         // This makes sure that if a constant has uses (for example an array of
@@ -746,7 +740,7 @@ int SlotCalculator::insertValue(const Value *D, bool dontIgnore) {
       return getOrCreateCompactionTableSlot(D);
   }
 
-  // If this node does not contribute to a plane, or if the node has a 
+  // If this node does not contribute to a plane, or if the node has a
   // name and we don't want names, then ignore the silly node... Note that types
   // do need slot numbers so that we can keep track of where other values land.
   //
@@ -806,7 +800,7 @@ int SlotCalculator::doInsertValue(const Value *D) {
 
   // Used for debugging DefSlot=-1 assertion...
   //if (Typ == Type::TypeTy)
-  //  cerr << "Inserting type '" << cast<Type>(D)->getDescription() << "'!\n";
+  //  llvm_cerr << "Inserting type '"<<cast<Type>(D)->getDescription() <<"'!\n";
 
   if (Typ->isDerivedType()) {
     int ValSlot;
@@ -823,13 +817,13 @@ int SlotCalculator::doInsertValue(const Value *D) {
   } else {
     Ty = Typ->getTypeID();
   }
-  
+
   if (Table.size() <= Ty)    // Make sure we have the type plane allocated...
     Table.resize(Ty+1, TypePlane());
 
   // If this is the first value to get inserted into the type plane, make sure
   // to insert the implicit null value...
-  if (Table[Ty].empty() &&  hasNullValue(Ty)) {
+  if (Table[Ty].empty() && hasNullValue(Typ)) {
     Value *ZeroInitializer = Constant::getNullValue(Typ);
 
     // If we are pushing zeroinit, it will be handled below.
@@ -843,10 +837,10 @@ int SlotCalculator::doInsertValue(const Value *D) {
   unsigned DestSlot = NodeMap[D] = Table[Ty].size();
   Table[Ty].push_back(D);
 
-  SC_DEBUG("  Inserting value [" << Ty << "] = " << D << " slot=" << 
+  SC_DEBUG("  Inserting value [" << Ty << "] = " << D << " slot=" <<
            DestSlot << " [");
   // G = Global, C = Constant, T = Type, F = Function, o = other
-  SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" : 
+  SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
            (isa<Function>(D) ? "F" : "o"))));
   SC_DEBUG("]\n");
   return (int)DestSlot;
@@ -865,4 +859,3 @@ int SlotCalculator::doInsertType(const Type *Ty) {
   return (int)DestSlot;
 }
 
-// vim: sw=2 ai