- // Make sure that all the flush function list is deleted.
- Builder.CreateCall(AtExitFn, getDeleteFlushFunctionListFunc());
+ if (M->getFunction("main")) {
+ Constant *AtExitFn = M->getOrInsertFunction("atexit", FTy);
+
+ // In the module that has the 'main' function, make sure that the flush
+ // and writeout function lists are deleted. Also make sure that the
+ // writeout function list is deleted.
+ Builder.CreateCall(AtExitFn, getDeleteWriteoutFunctionListFunc());
+ Builder.CreateCall(AtExitFn, getDeleteFlushFunctionListFunc());
+
+ // Make sure we write out all files when exiting. Note: This is called
+ // first from atexit().
+ FTy = FunctionType::get(Type::getVoidTy(*Ctx), false);
+ Builder.CreateCall(AtExitFn,
+ M->getOrInsertFunction("__llvm_writeout_files", FTy));
+ }
+