ModuleUtils.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //===-- ModuleUtils.cpp - Functions to manipulate Modules -----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This family of functions perform manipulations on Modules.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/ModuleUtils.h"
  14. #include "llvm/ADT/SmallPtrSet.h"
  15. #include "llvm/IR/DerivedTypes.h"
  16. #include "llvm/IR/Function.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/Module.h"
  19. using namespace llvm;
  20. static void appendToGlobalArray(const char *Array,
  21. Module &M, Function *F, int Priority) {
  22. IRBuilder<> IRB(M.getContext());
  23. FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
  24. // Get the current set of static global constructors and add the new ctor
  25. // to the list.
  26. SmallVector<Constant *, 16> CurrentCtors;
  27. StructType *EltTy;
  28. if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
  29. // If there is a global_ctors array, use the existing struct type, which can
  30. // have 2 or 3 fields.
  31. ArrayType *ATy = cast<ArrayType>(GVCtor->getType()->getElementType());
  32. EltTy = cast<StructType>(ATy->getElementType());
  33. if (Constant *Init = GVCtor->getInitializer()) {
  34. unsigned n = Init->getNumOperands();
  35. CurrentCtors.reserve(n + 1);
  36. for (unsigned i = 0; i != n; ++i)
  37. CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
  38. }
  39. GVCtor->eraseFromParent();
  40. } else {
  41. // Use a simple two-field struct if there isn't one already.
  42. EltTy = StructType::get(IRB.getInt32Ty(), PointerType::getUnqual(FnTy),
  43. nullptr);
  44. }
  45. // Build a 2 or 3 field global_ctor entry. We don't take a comdat key.
  46. Constant *CSVals[3];
  47. CSVals[0] = IRB.getInt32(Priority);
  48. CSVals[1] = F;
  49. // FIXME: Drop support for the two element form in LLVM 4.0.
  50. if (EltTy->getNumElements() >= 3)
  51. CSVals[2] = llvm::Constant::getNullValue(IRB.getInt8PtrTy());
  52. Constant *RuntimeCtorInit =
  53. ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
  54. CurrentCtors.push_back(RuntimeCtorInit);
  55. // Create a new initializer.
  56. ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
  57. Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
  58. // Create the new global variable and replace all uses of
  59. // the old global variable with the new one.
  60. (void)new GlobalVariable(M, NewInit->getType(), false,
  61. GlobalValue::AppendingLinkage, NewInit, Array);
  62. }
  63. void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority) {
  64. appendToGlobalArray("llvm.global_ctors", M, F, Priority);
  65. }
  66. void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority) {
  67. appendToGlobalArray("llvm.global_dtors", M, F, Priority);
  68. }
  69. GlobalVariable *
  70. llvm::collectUsedGlobalVariables(Module &M, SmallPtrSetImpl<GlobalValue *> &Set,
  71. bool CompilerUsed) {
  72. const char *Name = CompilerUsed ? "llvm.compiler.used" : "llvm.used";
  73. GlobalVariable *GV = M.getGlobalVariable(Name);
  74. if (!GV || !GV->hasInitializer())
  75. return GV;
  76. const ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
  77. for (unsigned I = 0, E = Init->getNumOperands(); I != E; ++I) {
  78. Value *Op = Init->getOperand(I);
  79. GlobalValue *G = cast<GlobalValue>(Op->stripPointerCastsNoFollowAliases());
  80. Set.insert(G);
  81. }
  82. return GV;
  83. }