1dff0c46cSDimitry Andric //===-- ModuleUtils.cpp - Functions to manipulate Modules -----------------===//
2dff0c46cSDimitry Andric //
3dff0c46cSDimitry Andric //                     The LLVM Compiler Infrastructure
4dff0c46cSDimitry Andric //
5dff0c46cSDimitry Andric // This file is distributed under the University of Illinois Open Source
6dff0c46cSDimitry Andric // License. See LICENSE.TXT for details.
7dff0c46cSDimitry Andric //
8dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
9dff0c46cSDimitry Andric //
10dff0c46cSDimitry Andric // This family of functions perform manipulations on Modules.
11dff0c46cSDimitry Andric //
12dff0c46cSDimitry Andric //===----------------------------------------------------------------------===//
13dff0c46cSDimitry Andric 
14dff0c46cSDimitry Andric #include "llvm/Transforms/Utils/ModuleUtils.h"
15dff0c46cSDimitry Andric #include "llvm/DerivedTypes.h"
16dff0c46cSDimitry Andric #include "llvm/Function.h"
17dff0c46cSDimitry Andric #include "llvm/Module.h"
18dff0c46cSDimitry Andric #include "llvm/Support/IRBuilder.h"
19dff0c46cSDimitry Andric 
20dff0c46cSDimitry Andric using namespace llvm;
21dff0c46cSDimitry Andric 
22dff0c46cSDimitry Andric static void appendToGlobalArray(const char *Array,
23dff0c46cSDimitry Andric                                 Module &M, Function *F, int Priority) {
24dff0c46cSDimitry Andric   IRBuilder<> IRB(M.getContext());
25dff0c46cSDimitry Andric   FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
26dff0c46cSDimitry Andric   StructType *Ty = StructType::get(
27dff0c46cSDimitry Andric       IRB.getInt32Ty(), PointerType::getUnqual(FnTy), NULL);
28dff0c46cSDimitry Andric 
29dff0c46cSDimitry Andric   Constant *RuntimeCtorInit = ConstantStruct::get(
30dff0c46cSDimitry Andric       Ty, IRB.getInt32(Priority), F, NULL);
31dff0c46cSDimitry Andric 
32dff0c46cSDimitry Andric   // Get the current set of static global constructors and add the new ctor
33dff0c46cSDimitry Andric   // to the list.
34dff0c46cSDimitry Andric   SmallVector<Constant *, 16> CurrentCtors;
35dff0c46cSDimitry Andric   if (GlobalVariable * GVCtor = M.getNamedGlobal(Array)) {
36dff0c46cSDimitry Andric     if (Constant *Init = GVCtor->getInitializer()) {
37dff0c46cSDimitry Andric       unsigned n = Init->getNumOperands();
38dff0c46cSDimitry Andric       CurrentCtors.reserve(n + 1);
39dff0c46cSDimitry Andric       for (unsigned i = 0; i != n; ++i)
40dff0c46cSDimitry Andric         CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
41dff0c46cSDimitry Andric     }
42dff0c46cSDimitry Andric     GVCtor->eraseFromParent();
43dff0c46cSDimitry Andric   }
44dff0c46cSDimitry Andric 
45dff0c46cSDimitry Andric   CurrentCtors.push_back(RuntimeCtorInit);
46dff0c46cSDimitry Andric 
47dff0c46cSDimitry Andric   // Create a new initializer.
48dff0c46cSDimitry Andric   ArrayType *AT = ArrayType::get(RuntimeCtorInit->getType(),
49dff0c46cSDimitry Andric                                  CurrentCtors.size());
50dff0c46cSDimitry Andric   Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
51dff0c46cSDimitry Andric 
52dff0c46cSDimitry Andric   // Create the new global variable and replace all uses of
53dff0c46cSDimitry Andric   // the old global variable with the new one.
54dff0c46cSDimitry Andric   (void)new GlobalVariable(M, NewInit->getType(), false,
55dff0c46cSDimitry Andric                            GlobalValue::AppendingLinkage, NewInit, Array);
56dff0c46cSDimitry Andric }
57dff0c46cSDimitry Andric 
58dff0c46cSDimitry Andric void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority) {
59dff0c46cSDimitry Andric   appendToGlobalArray("llvm.global_ctors", M, F, Priority);
60dff0c46cSDimitry Andric }
61dff0c46cSDimitry Andric 
62dff0c46cSDimitry Andric void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority) {
63dff0c46cSDimitry Andric   appendToGlobalArray("llvm.global_dtors", M, F, Priority);
64dff0c46cSDimitry Andric }
65