Add utility to append a function to the list of global constructors.
[oota-llvm.git] / lib / Transforms / Utils / ModuleUtils.cpp
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
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"
19 using namespace llvm;
20
21 void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority) {
22   IRBuilder<> IRB(M.getContext());
23   FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
24   StructType *Ty = StructType::get(
25       IRB.getInt32Ty(), PointerType::getUnqual(FnTy), NULL);
26
27   Constant *RuntimeCtorInit = ConstantStruct::get(
28       Ty, IRB.getInt32(Priority), F, NULL);
29
30   // Get the current set of static global constructors and add the new ctor
31   // to the list.
32   SmallVector<Constant *, 16> CurrentCtors;
33   if (GlobalVariable * GVCtor = M.getNamedGlobal("llvm.global_ctors")) {
34     if (Constant *Init = GVCtor->getInitializer()) {
35       unsigned n = Init->getNumOperands();
36       CurrentCtors.reserve(n + 1);
37       for (unsigned i = 0; i != n; ++i)
38         CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
39     }
40     GVCtor->eraseFromParent();
41   }
42
43   CurrentCtors.push_back(RuntimeCtorInit);
44
45   // Create a new initializer.
46   ArrayType *AT = ArrayType::get(RuntimeCtorInit->getType(),
47                                  CurrentCtors.size());
48   Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
49
50   // Create the new global variable and replace all uses of
51   // the old global variable with the new one.
52   (void)new GlobalVariable(M, NewInit->getType(), false,
53                            GlobalValue::AppendingLinkage, NewInit,
54                            "llvm.global_ctors");
55 }