LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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>::createNode() {
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>::createNode() {
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 
00066 // Specialize setName to take care of symbol table majik
00067 void Argument::setName(const std::string &name, SymbolTable *ST) {
00068   Function *P;
00069   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
00070          "Invalid symtab argument!");
00071   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
00072   Value::setName(name);
00073   if (P && hasName()) P->getSymbolTable().insert(this);
00074 }
00075 
00076 void Argument::setParent(Function *parent) {
00077   if (getParent())
00078     LeakDetector::addGarbageObject(this);
00079   Parent = parent;
00080   if (getParent())
00081     LeakDetector::removeGarbageObject(this);
00082 }
00083 
00084 //===----------------------------------------------------------------------===//
00085 // Function Implementation
00086 //===----------------------------------------------------------------------===//
00087 
00088 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
00089                    const std::string &name, Module *ParentModule)
00090   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
00091   BasicBlocks.setItemParent(this);
00092   BasicBlocks.setParent(this);
00093   ArgumentList.setItemParent(this);
00094   ArgumentList.setParent(this);
00095   SymTab = new SymbolTable();
00096 
00097   assert((getReturnType()->isFirstClassType() ||getReturnType() == Type::VoidTy)
00098          && "LLVM functions cannot return aggregate values!");
00099 
00100   // Create the arguments vector, all arguments start out unnamed.
00101   for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
00102     assert(Ty->getParamType(i) != Type::VoidTy &&
00103            "Cannot have void typed arguments!");
00104     ArgumentList.push_back(new Argument(Ty->getParamType(i)));
00105   }
00106 
00107   // Make sure that we get added to a function
00108   LeakDetector::addGarbageObject(this);
00109 
00110   if (ParentModule)
00111     ParentModule->getFunctionList().push_back(this);
00112 }
00113 
00114 Function::~Function() {
00115   dropAllReferences();    // After this it is safe to delete instructions.
00116 
00117   // Delete all of the method arguments and unlink from symbol table...
00118   ArgumentList.clear();
00119   ArgumentList.setParent(0);
00120   delete SymTab;
00121 }
00122 
00123 // Specialize setName to take care of symbol table majik
00124 void Function::setName(const std::string &name, SymbolTable *ST) {
00125   Module *P;
00126   assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
00127          "Invalid symtab argument!");
00128   if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
00129   Value::setName(name);
00130   if (P && hasName()) P->getSymbolTable().insert(this);
00131 }
00132 
00133 void Function::setParent(Module *parent) {
00134   if (getParent())
00135     LeakDetector::addGarbageObject(this);
00136   Parent = parent;
00137   if (getParent())
00138     LeakDetector::removeGarbageObject(this);
00139 }
00140 
00141 const FunctionType *Function::getFunctionType() const {
00142   return cast<FunctionType>(getType()->getElementType());
00143 }
00144 
00145 const Type *Function::getReturnType() const { 
00146   return getFunctionType()->getReturnType();
00147 }
00148 
00149 void Function::removeFromParent() {
00150   getParent()->getFunctionList().remove(this);
00151 }
00152 
00153 void Function::eraseFromParent() {
00154   getParent()->getFunctionList().erase(this);
00155 }
00156 
00157 
00158 /// renameLocalSymbols - This method goes through the Function's symbol table
00159 /// and renames any symbols that conflict with symbols at global scope.  This is
00160 /// required before printing out to a textual form, to ensure that there is no
00161 /// ambiguity when parsing.
00162 void Function::renameLocalSymbols() {
00163   SymbolTable &LST = getSymbolTable();                 // Local Symtab
00164   SymbolTable &GST = getParent()->getSymbolTable();    // Global Symtab
00165 
00166   for (SymbolTable::plane_iterator LPI = LST.plane_begin(), E = LST.plane_end();
00167        LPI != E; ++LPI)
00168     // All global symbols are of pointer type, ignore any non-pointer planes.
00169     if (isa<PointerType>(LPI->first)) {
00170       // Only check if the global plane has any symbols of this type.
00171       SymbolTable::plane_iterator GPI = GST.find(LPI->first);
00172       if (GPI != GST.plane_end()) {
00173         SymbolTable::ValueMap &LVM       = LPI->second;
00174         const SymbolTable::ValueMap &GVM = GPI->second;
00175 
00176         // Loop over all local symbols, renaming those that are in the global
00177         // symbol table already.
00178         for (SymbolTable::value_iterator VI = LVM.begin(), E = LVM.end();
00179              VI != E;) {
00180           Value *V                = VI->second;
00181           const std::string &Name = VI->first;
00182           ++VI;
00183           if (GVM.count(Name)) {
00184             static unsigned UniqueNum = 0;
00185             // Find a name that does not conflict!
00186             while (GVM.count(Name + "_" + utostr(++UniqueNum)) ||
00187                    LVM.count(Name + "_" + utostr(UniqueNum)))
00188               /* scan for UniqueNum that works */;
00189             V->setName(Name + "_" + utostr(UniqueNum));
00190           }
00191         }
00192       }
00193     }
00194 }
00195 
00196 
00197 // dropAllReferences() - This function causes all the subinstructions to "let
00198 // go" of all references that they are maintaining.  This allows one to
00199 // 'delete' a whole class at a time, even though there may be circular
00200 // references... first all references are dropped, and all use counts go to
00201 // zero.  Then everything is deleted for real.  Note that no operations are
00202 // valid on an object that has "dropped all references", except operator 
00203 // delete.
00204 //
00205 void Function::dropAllReferences() {
00206   for (iterator I = begin(), E = end(); I != E; ++I)
00207     I->dropAllReferences();
00208   BasicBlocks.clear();    // Delete all basic blocks...
00209 }
00210 
00211 /// getIntrinsicID - This method returns the ID number of the specified
00212 /// function, or Intrinsic::not_intrinsic if the function is not an
00213 /// intrinsic, or if the pointer is null.  This value is always defined to be
00214 /// zero to allow easy checking for whether a function is intrinsic or not.  The
00215 /// particular intrinsic functions which correspond to this value are defined in
00216 /// llvm/Intrinsics.h.
00217 ///
00218 unsigned Function::getIntrinsicID() const {
00219   if (getName().size() < 5 || getName()[4] != '.' || getName()[0] != 'l' ||
00220       getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
00221     return 0;  // All intrinsics start with 'llvm.'
00222 
00223   assert(getName().size() != 5 && "'llvm.' is an invalid intrinsic name!");
00224   
00225   switch (getName()[5]) {
00226   case 'd':
00227     if (getName() == "llvm.dbg.stoppoint")   return Intrinsic::dbg_stoppoint;
00228     if (getName() == "llvm.dbg.region.start")return Intrinsic::dbg_region_start;
00229     if (getName() == "llvm.dbg.region.end")  return Intrinsic::dbg_region_end;
00230     if (getName() == "llvm.dbg.func.start")  return Intrinsic::dbg_func_start;
00231     if (getName() == "llvm.dbg.declare")     return Intrinsic::dbg_declare;
00232     break;
00233   case 'f':
00234     if (getName() == "llvm.frameaddress")  return Intrinsic::frameaddress;
00235     break;
00236   case 'g':
00237     if (getName() == "llvm.gcwrite") return Intrinsic::gcwrite;
00238     if (getName() == "llvm.gcread")  return Intrinsic::gcread;
00239     if (getName() == "llvm.gcroot")  return Intrinsic::gcroot;
00240     break;
00241   case 'i':
00242     if (getName() == "llvm.isunordered") return Intrinsic::isunordered;
00243     break;
00244   case 'l':
00245     if (getName() == "llvm.longjmp")  return Intrinsic::longjmp;
00246     break;
00247   case 'm':
00248     if (getName() == "llvm.memcpy")  return Intrinsic::memcpy;
00249     if (getName() == "llvm.memmove")  return Intrinsic::memmove;
00250     if (getName() == "llvm.memset")  return Intrinsic::memset;
00251     break;
00252   case 'r':
00253     if (getName() == "llvm.returnaddress")  return Intrinsic::returnaddress;
00254     if (getName() == "llvm.readport")       return Intrinsic::readport;
00255     if (getName() == "llvm.readio")         return Intrinsic::readio;
00256     break;
00257   case 's':
00258     if (getName() == "llvm.setjmp")     return Intrinsic::setjmp;
00259     if (getName() == "llvm.sigsetjmp")  return Intrinsic::sigsetjmp;
00260     if (getName() == "llvm.siglongjmp") return Intrinsic::siglongjmp;
00261     break;
00262   case 'v':
00263     if (getName() == "llvm.va_copy")  return Intrinsic::vacopy;
00264     if (getName() == "llvm.va_end")   return Intrinsic::vaend;
00265     if (getName() == "llvm.va_start") return Intrinsic::vastart;
00266   case 'w':
00267     if (getName() == "llvm.writeport") return Intrinsic::writeport;
00268     if (getName() == "llvm.writeio")   return Intrinsic::writeio;
00269     break;
00270   }
00271   // The "llvm." namespace is reserved!
00272   assert(0 && "Unknown LLVM intrinsic function!");
00273   return 0;
00274 }
00275 
00276 Value *IntrinsicInst::StripPointerCasts(Value *Ptr) {
00277   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr)) {
00278     if (CE->getOpcode() == Instruction::Cast) {
00279       if (isa<PointerType>(CE->getOperand(0)->getType()))
00280         return StripPointerCasts(CE->getOperand(0));
00281     } else if (CE->getOpcode() == Instruction::GetElementPtr) {
00282       for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
00283         if (!CE->getOperand(i)->isNullValue())
00284           return Ptr;
00285       return StripPointerCasts(CE->getOperand(0));
00286     }
00287     return Ptr;
00288   }
00289 
00290   if (CastInst *CI = dyn_cast<CastInst>(Ptr)) {
00291     if (isa<PointerType>(CI->getOperand(0)->getType()))
00292       return StripPointerCasts(CI->getOperand(0));
00293   } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
00294     for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
00295       if (!isa<Constant>(GEP->getOperand(i)) ||
00296           !cast<Constant>(GEP->getOperand(i))->isNullValue())
00297         return Ptr;
00298     return StripPointerCasts(GEP->getOperand(0));
00299   }
00300   return Ptr;
00301 }
00302 
00303 // vim: sw=2 ai