LLVM GlobalStatus possible bug (patch provided)

Hi everyone,

I writed my own pass (which play with global variables). I can call my pass with “clang++ -mllvm -mypass” and everything is working.
However if I use my pass with optimizations passes like -O2 or -O3, clang crashs :

0 clang-3.4 0x0000000001b451bb llvm::sys::PrintStackTrace(_IO_FILE*) + 38
1 clang-3.4 0x0000000001b45438
2 clang-3.4 0x0000000001b44e8e
3 libpthread.so.0 0x00007fe7708a2340
4 clang-3.4 0x0000000000d5ea1a
5 clang-3.4 0x00000000017c264f
6 clang-3.4 0x00000000017c2c51 llvm::GlobalStatus::analyzeGlobal(llvm::Value const*, llvm::GlobalStatus&) + 73
7 clang-3.4 0x000000000115bd78
8 clang-3.4 0x000000000115ca60
9 clang-3.4 0x0000000001160d70
10 clang-3.4 0x0000000001a208aa
11 clang-3.4 0x0000000001a20dc6 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 244
12 clang-3.4 0x0000000001a20fd1 llvm::legacy::PassManager::run(llvm::Module&) + 39
13 clang-3.4 0x0000000001b7d4aa
14 clang-3.4 0x0000000001b7d5f3 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::Module*, clang::BackendAction, llvm::raw_ostream*) + 136
15 clang-3.4 0x0000000001b78efe
16 clang-3.4 0x0000000001f00754 clang::ParseAST(clang::Sema&, bool, bool) + 780
17 clang-3.4 0x0000000001e0b188 clang::ASTFrontendAction::ExecuteAction() + 322
18 clang-3.4 0x0000000001b77f19 clang::CodeGenAction::ExecuteAction() + 1145
19 clang-3.4 0x0000000001e0accb clang::FrontendAction::Execute() + 205
20 clang-3.4 0x0000000001de5128 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 564
21 clang-3.4 0x0000000001b4aea1 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 1026
22 clang-3.4 0x0000000000c1ac73 cc1_main(char const**, char const**, char const*, void*) + 717
23 clang-3.4 0x0000000000c15997 main + 785
24 libc.so.6 0x00007fe76fccdec5 __libc_start_main + 245
25 clang-3.4 0x0000000000c14499

After further investigation i fixed the problem (cf patch attached).
In the lib/Transforms/Utils/GlobalStatus.cpp :
const Function *F = I->getParent()->getParent();

There is no check if the first parent is null…

I don’t know if this is me doing something wrong and I->getParent() should never be null or if this is a bug?
I’m working with clang/llvm 3.4 but same problem in 3.5 and trunk …

Regards,
Christophe

bugfix.patch (1.01 KB)