Index: llvm/lib/IR/Value.cpp =================================================================== --- llvm/lib/IR/Value.cpp +++ llvm/lib/IR/Value.cpp @@ -455,13 +455,20 @@ } void Value::replaceUsesExceptBlockAddr(Value *New) { - use_iterator UI = use_begin(), E = use_end(); - for (; UI != E;) { - Use &U = *UI; - ++UI; + SmallVector, 8> BA; + while (!use_empty()) { + Use &U = *UseList; - if (isa(U.getUser())) + // Don't process blockaddress uses. Remove them from UseList + // temporarily and place onto a separate list. We can't use + // UseList iterator here as the list is changing inside the loop. + // That's why we need to remove skiped uses from the list. + if (isa(U.getUser())) { + Value *V = U.get(); + BA.push_back(std::make_pair(&U, V)); + U.set(nullptr); continue; + } // Must handle Constants specially, we cannot call replaceUsesOfWith on a // constant because they are uniqued. @@ -474,6 +481,14 @@ U.set(New); } + + // Go over the saved block address uses and add them back to UseList. + while (!BA.empty()) { + std::pair P = BA.pop_back_val(); + Use *U = P.first; + Value *V = P.second; + U->set(V); + } } namespace { Index: llvm/test/Transforms/LowerTypeTests/blockaddress-2.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/LowerTypeTests/blockaddress-2.ll @@ -0,0 +1,27 @@ +; RUN: opt -S %s -lowertypetests | FileCheck %s + +; CHECK: @badfileops = internal global %struct.f { i32 (i32*)* @bad_f, i32 (i32*)* @bad_f } +; CHECK: @bad_f = internal alias i32 (i32*), bitcast (void ()* @.cfi.jumptable to i32 (i32*)*) +; CHECK: define internal i32 @bad_f.cfi(i32* nocapture readnone) !type !0 { +; CHECK-NEXT: ret i32 9 + +target triple = "x86_64-unknown-linux" + +%struct.f = type { i32 (i32*)*, i32 (i32*)* } +@llvm.used = external global [43 x i8*], section "llvm.metadata" +@badfileops = internal global %struct.f { i32 (i32*)* @bad_f, i32 (i32*)* @bad_f }, align 8 + +declare i1 @llvm.type.test(i8*, metadata) + +define internal i32 @bad_f(i32* nocapture readnone) !type !1 { + ret i32 9 +} + +define internal fastcc i32 @do_f(i32, i32, i32*, i32, i64, i32) unnamed_addr !type !2 { + %7 = tail call i1 @llvm.type.test(i8* undef, metadata !"_ZTSFiP4fileP3uioP5ucrediP6threadE"), !nosanitize !3 + ret i32 undef +} + +!1 = !{i64 0, !"_ZTSFiP4fileP3uioP5ucrediP6threadE"} +!2 = !{i64 0, !"_ZTSFiP6threadiP4fileP3uioliE"} +!3 = !{}