LLVM API Documentation

Function.cpp

Go to the documentation of this file.
00001 //===-- Function.cpp - Implement the Global object classes ----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Function & GlobalVariable classes for the VMCore
00011 // library.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Module.h"
00016 #include "llvm/DerivedTypes.h"
00017 #include "llvm/IntrinsicInst.h"
00018 #include "llvm/Support/LeakDetector.h"
00019 #include "SymbolTableListTraitsImpl.h"
00020 #include "llvm/ADT/StringExtras.h"
00021 using namespace llvm;
00022 
00023 BasicBlock *ilist_traits<BasicBlock>::createSentinel() {
00024   BasicBlock *Ret = new BasicBlock();
00025   // This should not be garbage monitored.
00026   LeakDetector::removeGarbageObject(Ret);
00027   return Ret;
00028 }
00029 
00030 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
00031   return F->getBasicBlockList();
00032 }
00033 
00034 Argument *ilist_traits<Argument>::createSentinel() {
00035   Argument *Ret = new Argument(Type::IntTy);
00036   // This should not be garbage monitored.
00037   LeakDetector::removeGarbageObject(Ret);
00038   return Ret;
00039 }
00040 
00041 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
00042   return F->getArgumentList();
00043 }
00044 
00045 // Explicit instantiations of SymbolTableListTraits since some of the methods
00046 // are not in the public header file...
00047 template class SymbolTableListTraits<Argument, Function, Function>;
00048 template class SymbolTableListTraits<BasicBlock, Function, Function>;
00049 
00050 //===----------------------------------------------------------------------===//
00051 // Argument Implementation
00052 //===----------------------------------------------------------------------===//
00053 
00054 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
00055   : Value(Ty, Value::ArgumentVal, Name) {
00056   Parent = 0;
00057 
00058   // Make sure that we get added to a function
00059   LeakDetector::addGarbageObject(this);
00060 
00061   if (Par)
00062     Par->getArgumentList().push_back(this);
00063 }
00064 
00065 void Argument::setParent(Function *parent) {
00066   if (getParent())
00067     LeakDetector::addGarbageObject(this);
00068   Parent = parent;
00069   if (getParent())
00070     LeakDetector::removeGarbageObject(this);
00071 }
00072 
00073 //===----------------------------------------------------------------------===//
00074 // Function Implementation
00075 //===----------------------------------------------------------------------===//
00076 
00077 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
00078                    const std::string &name, Module *ParentModule)
00079   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, 0, 0, Linkage, name) {
00080   CallingConvention = 0;
00081   BasicBlocks.setItemParent(this);
00082   BasicBlocks.setParent(this);
00083   ArgumentList.setItemParent(this);
00084   ArgumentList.setParent(this);
00085   SymTab = new SymbolTable();
00086 
00087   assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
00088          && "LLVM functions cannot return aggregate values!");
00089 
00090   // Create the arguments vector, all arguments start out unnamed.
00091   for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
00092     assert(Ty->getParamType(i) != Type::VoidTy &&
00093            "Cannot have void typed arguments!");
00094     ArgumentList.push_back(new Argument(Ty->getParamType(i)));
00095   }
00096 
00097   // Make sure that we get added to a function
00098   LeakDetector::addGarbageObject(this);
00099 
00100   if (ParentModule)
00101     ParentModule->getFunctionList().push_back(this);
00102 }
00103 
00104 Function::~Function() {
00105   dropAllReferences();    // After this it is safe to delete instructions.
00106 
00107   // Delete all of the method arguments and unlink from symbol table...
00108   ArgumentList.clear();
00109   ArgumentList.setParent(0);
00110   delete SymTab;
00111 }
00112 
00113 void Function::setParent(Module *parent) {
00114   if (getParent())
00115     LeakDetector::addGarbageObject(this);
00116   Parent = parent;
00117   if (getParent())
00118     LeakDetector::removeGarbageObject(this);
00119 }
00120 
00121 const FunctionType *Function::getFunctionType() const {
00122   return cast<FunctionType>(getType()->getElementType());
00123 }
00124 
00125 bool Function::isVarArg() const {
00126   return getFunctionType()->isVarArg();
00127 }
00128 
00129 const Type *Function::getReturnType() const {
00130   return getFunctionType()->getReturnType();
00131 }
00132 
00133 void Function::removeFromParent() {
00134   getParent()->getFunctionList().remove(this);
00135 }
00136 
00137 void Function::eraseFromParent() {
00138   getParent()->getFunctionList().erase(this);
00139 }
00140 
00141 
00142 /// renameLocalSymbols - This method goes through the Function's symbol table
00143 /// and renames any symbols that conflict with symbols at global scope.  This is
00144 /// required before printing out to a textual form, to ensure that there is no
00145 /// ambiguity when parsing.
00146 void Function::renameLocalSymbols() {
00147   SymbolTable &LST = getSymbolTable();                 // Local Symtab
00148   SymbolTable &GST = getParent()->getSymbolTable();    // Global Symtab
00149 
00150   for (SymbolTable::plane_iterator LPI = LST.plane_begin(), E = LST.plane_end();
00151        LPI != E; ++LPI)
00152     // All global symbols are of pointer type, ignore any non-pointer planes.
00153     if (const PointerType *CurTy = dyn_cast<PointerType>(LPI->first)) {
00154       // Only check if the global plane has any symbols of this type.
00155       SymbolTable::plane_iterator GPI = GST.find(LPI->first);
00156       if (GPI != GST.plane_end()) {
00157         SymbolTable::ValueMap &LVM       = LPI->second;
00158         const SymbolTable::ValueMap &GVM = GPI->second;
00159 
00160         // Loop over all local symbols, renaming those that are in the global
00161         // symbol table already.
00162         for (SymbolTable::value_iterator VI = LVM.begin(), E = LVM.end();
00163              VI != E;) {
00164           Value *V                = VI->second;
00165           const std::string &Name = VI->first;
00166           ++VI;
00167           if (GVM.count(Name)) {
00168             static unsigned UniqueNum = 0;
00169             // Find a name that does not conflict!
00170             while (GVM.count(Name + "_" + utostr(++UniqueNum)) ||
00171                    LVM.count(Name + "_" + utostr(UniqueNum)))
00172               /* scan for UniqueNum that works */;
00173             V->setName(Name + "_" + utostr(UniqueNum));
00174           }
00175         }
00176       }
00177     }
00178 }
00179 
00180 
00181 // dropAllReferences() - This function causes all the subinstructions to "let
00182 // go" of all references that they are maintaining.  This allows one to
00183 // 'delete' a whole class at a time, even though there may be circular
00184 // references... first all references are dropped, and all use counts go to
00185 // zero.  Then everything is deleted for real.  Note that no operations are
00186 // valid on an object that has "dropped all references", except operator
00187 // delete.
00188 //
00189 void Function::dropAllReferences() {
00190   for (iterator I = begin(), E = end(); I != E; ++I)
00191     I->dropAllReferences();
00192   BasicBlocks.clear();    // Delete all basic blocks...
00193 }
00194 
00195 /// getIntrinsicID - This method returns the ID number of the specified
00196 /// function, or Intrinsic::not_intrinsic if the function is not an
00197 /// intrinsic, or if the pointer is null.  This value is always defined to be
00198 /// zero to allow easy checking for whether a function is intrinsic or not.  The
00199 /// particular intrinsic functions which correspond to this value are defined in
00200 /// llvm/Intrinsics.h.
00201 ///
00202 unsigned Function::getIntrinsicID() const {
00203   const std::string& Name = this->getName();
00204   if (Name.size() < 5 || Name[4] != '.' || Name[0] != 'l' || Name[1] != 'l'
00205       || Name[2] != 'v' || Name[3] != 'm')
00206     return 0;  // All intrinsics start with 'llvm.'
00207 
00208   assert(Name.size() != 5 && "'llvm.' is an invalid intrinsic name!");
00209 
00210 #define GET_FUNCTION_RECOGNIZER
00211 #include "llvm/Intrinsics.gen"
00212 #undef GET_FUNCTION_RECOGNIZER
00213   return 0;
00214 }
00215 
00216 const char *Intrinsic::getName(ID id) {
00217   assert(id < num_intrinsics && "Invalid intrinsic ID!");
00218   const char * const Table[] = {
00219     "not_intrinsic",
00220 #define GET_INTRINSIC_NAME_TABLE
00221 #include "llvm/Intrinsics.gen"
00222 #undef GET_INTRINSIC_NAME_TABLE
00223   };
00224   return Table[id];
00225 }
00226 
00227 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
00228   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
00229     if (CE->getOpcode() == Instruction::Cast) {
00230       if (isa<PointerType>(CE->getOperand(0)->getType()))
00231         return StripPointerCasts(CE->getOperand(0));
00232     } else if (CE->getOpcode() == Instruction::GetElementPtr) {
00233       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
00234         if (!CE->getOperand(i)->isNullValue())
00235           return Ptr;
00236       return StripPointerCasts(CE->getOperand(0));
00237     }
00238     return Ptr;
00239   }
00240 
00241   if (CastInst *CI = dyn_cast<CastInst>(Ptr)) {
00242     if (isa<PointerType>(CI->getOperand(0)->getType()))
00243       return StripPointerCasts(CI->getOperand(0));
00244   } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
00245     for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
00246       if (!isa<Constant>(GEP->getOperand(i)) ||
00247           !cast<Constant>(GEP->getOperand(i))->isNullValue())
00248         return Ptr;
00249     return StripPointerCasts(GEP->getOperand(0));
00250   }
00251   return Ptr;
00252 }
00253 
00254 // vim: sw=2 ai