Index: lib/Transforms/IPO/GlobalOpt.cpp =================================================================== --- lib/Transforms/IPO/GlobalOpt.cpp +++ lib/Transforms/IPO/GlobalOpt.cpp @@ -2026,6 +2026,17 @@ continue; } + // LLVM's definition of dominance allows instructions that are cyclic + // in unreachable blocks, e.g.: + // %pat = select i1 %condition, @global, i16* %pat + // because any instruction dominates an instruction in a block that's + // not reachable from entry. + // So, remove unreachable blocks from the function, because a) there's + // no point in analyzing them and b) GlobalOpt should otherwise grow + // some more complicated logic to break these cycles. + if (!F->isDeclaration()) + Changed |= removeUnreachableBlocks(*F); + Changed |= processGlobal(*F, TLI, LookupDomTree); if (!F->hasLocalLinkage()) Index: test/Transforms/GlobalOpt/pr33686.ll =================================================================== --- /dev/null +++ test/Transforms/GlobalOpt/pr33686.ll @@ -0,0 +1,17 @@ +; RUN: opt -S -globalopt %s | FileCheck %s + +; CHECK-LABEL: define void @beth +; CHECK-NEXT: entry: +; CHECK-NEXT: ret void +; CHEC-NEXT: } + +@glob = external global i16, align 1 + +define void @beth() { +entry: + ret void + +notreachable: + %patatino = select i1 undef, i16* @glob, i16* %patatino + br label %notreachable +}