void PerfMonitor::addToGlobalConstructors(Function *Fn) { const char *Name = "llvm.global_ctors"; GlobalVariable *GV = M->getGlobalVariable(Name); std::vector<Constant *> V; if (GV) { Constant *Array = GV->getInitializer(); for (Value *X : Array->operand_values()) V.push_back(cast<Constant>(X)); GV->eraseFromParent(); } StructType *ST = StructType::get(Builder.getInt32Ty(), Fn->getType(), Builder.getInt8PtrTy()); V.push_back( ConstantStruct::get(ST, Builder.getInt32(10), Fn, ConstantPointerNull::get(Builder.getInt8PtrTy()))); ArrayType *Ty = ArrayType::get(ST, V.size()); GV = new GlobalVariable(*M, Ty, true, GlobalValue::AppendingLinkage, ConstantArray::get(Ty, V), Name, nullptr, GlobalVariable::NotThreadLocal); }