blob: 8491c5582d979c2b4912cc196e215ea8b1e54f17 [file] [log] [blame]
Devang Patel2ad3f932011-10-31 23:58:51 +00001//===-- 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
14#include "llvm/Transforms/Utils/ModuleUtils.h"
15#include "llvm/DerivedTypes.h"
16#include "llvm/Function.h"
17#include "llvm/Module.h"
18#include "llvm/Support/IRBuilder.h"
Kostya Serebryanya2a2d1f2011-11-16 01:14:38 +000019
Devang Patel2ad3f932011-10-31 23:58:51 +000020using namespace llvm;
21
Kostya Serebryany7bcfc992011-12-15 21:59:03 +000022static void appendToGlobalArray(const char *Array,
23 Module &M, Function *F, int Priority) {
Devang Patel2ad3f932011-10-31 23:58:51 +000024 IRBuilder<> IRB(M.getContext());
25 FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
26 StructType *Ty = StructType::get(
27 IRB.getInt32Ty(), PointerType::getUnqual(FnTy), NULL);
28
29 Constant *RuntimeCtorInit = ConstantStruct::get(
30 Ty, IRB.getInt32(Priority), F, NULL);
31
32 // Get the current set of static global constructors and add the new ctor
33 // to the list.
34 SmallVector<Constant *, 16> CurrentCtors;
Kostya Serebryany7bcfc992011-12-15 21:59:03 +000035 if (GlobalVariable * GVCtor = M.getNamedGlobal(Array)) {
Devang Patel2ad3f932011-10-31 23:58:51 +000036 if (Constant *Init = GVCtor->getInitializer()) {
37 unsigned n = Init->getNumOperands();
38 CurrentCtors.reserve(n + 1);
39 for (unsigned i = 0; i != n; ++i)
40 CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
41 }
42 GVCtor->eraseFromParent();
43 }
44
45 CurrentCtors.push_back(RuntimeCtorInit);
46
47 // Create a new initializer.
48 ArrayType *AT = ArrayType::get(RuntimeCtorInit->getType(),
49 CurrentCtors.size());
50 Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
51
52 // Create the new global variable and replace all uses of
53 // the old global variable with the new one.
54 (void)new GlobalVariable(M, NewInit->getType(), false,
Kostya Serebryany7bcfc992011-12-15 21:59:03 +000055 GlobalValue::AppendingLinkage, NewInit, Array);
56}
57
58void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority) {
59 appendToGlobalArray("llvm.global_ctors", M, F, Priority);
60}
61
62void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority) {
63 appendToGlobalArray("llvm.global_dtors", M, F, Priority);
Devang Patel2ad3f932011-10-31 23:58:51 +000064}