LLVM API Documentation

ModuleUtils.cpp
Go to the documentation of this file.
00001 //===-- ModuleUtils.cpp - Functions to manipulate Modules -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This family of functions perform manipulations on Modules.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Transforms/Utils/ModuleUtils.h"
00015 #include "llvm/ADT/SmallPtrSet.h"
00016 #include "llvm/IR/DerivedTypes.h"
00017 #include "llvm/IR/Function.h"
00018 #include "llvm/IR/IRBuilder.h"
00019 #include "llvm/IR/Module.h"
00020 
00021 using namespace llvm;
00022 
00023 static void appendToGlobalArray(const char *Array, 
00024                                 Module &M, Function *F, int Priority) {
00025   IRBuilder<> IRB(M.getContext());
00026   FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
00027 
00028   // Get the current set of static global constructors and add the new ctor
00029   // to the list.
00030   SmallVector<Constant *, 16> CurrentCtors;
00031   StructType *EltTy;
00032   if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
00033     // If there is a global_ctors array, use the existing struct type, which can
00034     // have 2 or 3 fields.
00035     ArrayType *ATy = cast<ArrayType>(GVCtor->getType()->getElementType());
00036     EltTy = cast<StructType>(ATy->getElementType());
00037     if (Constant *Init = GVCtor->getInitializer()) {
00038       unsigned n = Init->getNumOperands();
00039       CurrentCtors.reserve(n + 1);
00040       for (unsigned i = 0; i != n; ++i)
00041         CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
00042     }
00043     GVCtor->eraseFromParent();
00044   } else {
00045     // Use a simple two-field struct if there isn't one already.
00046     EltTy = StructType::get(IRB.getInt32Ty(), PointerType::getUnqual(FnTy),
00047                             nullptr);
00048   }
00049 
00050   // Build a 2 or 3 field global_ctor entry.  We don't take a comdat key.
00051   Constant *CSVals[3];
00052   CSVals[0] = IRB.getInt32(Priority);
00053   CSVals[1] = F;
00054   // FIXME: Drop support for the two element form in LLVM 4.0.
00055   if (EltTy->getNumElements() >= 3)
00056     CSVals[2] = llvm::Constant::getNullValue(IRB.getInt8PtrTy());
00057   Constant *RuntimeCtorInit =
00058       ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
00059 
00060   CurrentCtors.push_back(RuntimeCtorInit);
00061 
00062   // Create a new initializer.
00063   ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
00064   Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
00065 
00066   // Create the new global variable and replace all uses of
00067   // the old global variable with the new one.
00068   (void)new GlobalVariable(M, NewInit->getType(), false,
00069                            GlobalValue::AppendingLinkage, NewInit, Array);
00070 }
00071 
00072 void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority) {
00073   appendToGlobalArray("llvm.global_ctors", M, F, Priority);
00074 }
00075 
00076 void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority) {
00077   appendToGlobalArray("llvm.global_dtors", M, F, Priority);
00078 }
00079 
00080 GlobalVariable *
00081 llvm::collectUsedGlobalVariables(Module &M, SmallPtrSetImpl<GlobalValue *> &Set,
00082                                  bool CompilerUsed) {
00083   const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
00084   GlobalVariable *GV = M.getGlobalVariable(Name);
00085   if (!GV || !GV->hasInitializer())
00086     return GV;
00087 
00088   const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
00089   for (unsigned I = 0, E = Init->getNumOperands(); I != E; ++I) {
00090     Value *Op = Init->getOperand(I);
00091     GlobalValue *G = cast<GlobalValue>(Op->stripPointerCastsNoFollowAliases());
00092     Set.insert(G);
00093   }
00094   return GV;
00095 }