Index: include/llvm/IR/Value.h =================================================================== --- include/llvm/IR/Value.h +++ include/llvm/IR/Value.h @@ -299,6 +299,12 @@ /// values or constant users. void replaceUsesOutsideBlock(Value *V, BasicBlock *BB); + /// replaceUsesExceptBlockAddr - Go through the uses list for this definition + /// and make each use point to "V" instead of "this" when the use is outside + /// the block. 'This's use list is expected to have at least one element. + /// Unlike replaceAllUsesWith this function skips blockaddr uses. + void replaceUsesExceptBlockAddr(Value *New); + //---------------------------------------------------------------------- // Methods for handling the chain of uses of this Value. // Index: lib/IR/Value.cpp =================================================================== --- lib/IR/Value.cpp +++ lib/IR/Value.cpp @@ -454,6 +454,29 @@ } } +void Value::replaceUsesExceptBlockAddr(Value *New) { + use_iterator UI = use_begin(), E = use_end(); + for (; UI != E;) { + Use &U = *UI; + ++UI; + + auto *Usr = dyn_cast(U.getUser()); + if (Usr && isa(Usr)) + continue; + + // Must handle Constants specially, we cannot call replaceUsesOfWith on a + // constant because they are uniqued. + if (auto *C = dyn_cast(U.getUser())) { + if (!isa(C)) { + C->handleOperandChange(this, New); + continue; + } + } + + U.set(New); + } +} + namespace { // Various metrics for how much to strip off of pointers. enum PointerStripKind { Index: lib/Transforms/IPO/LowerTypeTests.cpp =================================================================== --- lib/Transforms/IPO/LowerTypeTests.cpp +++ lib/Transforms/IPO/LowerTypeTests.cpp @@ -1401,7 +1401,7 @@ FAlias->takeName(F); if (FAlias->hasName()) F->setName(FAlias->getName() + ".cfi"); - F->replaceAllUsesWith(FAlias); + F->replaceUsesExceptBlockAddr(FAlias); } if (!F->isDeclarationForLinker()) F->setLinkage(GlobalValue::InternalLinkage); Index: test/Transforms/LowerTypeTests/blockaddress.ll =================================================================== --- /dev/null +++ test/Transforms/LowerTypeTests/blockaddress.ll @@ -0,0 +1,53 @@ +; RUN: opt -S %s -lowertypetests | FileCheck %s + +; CHECK: define internal void @fl() !type !0 !type !1 { +; CHECK-NEXT: ret void +; CHECK-NEXT: } +; CHECK: define internal void @fk() !type !0 !type !1 { +; CHECK-NEXT: entry: +; CHECK-NEXT: ret void +; CHECK-NEXT: } +; CHECK: define internal void @patatino.cfi(i8* %bp) unnamed_addr !type !2 !type !3 { +; CHECK-NEXT: ret void +; CHECK-NEXT: } +; CHECK: define internal void @fo.cfi(i8* %bp) !type !2 !type !3 { +; CHECK-NEXT: br label %p +; CHECK: p: +; CHECK-NEXT: call void @fq(i8* blockaddress(@fo.cfi, %p)) +; CHECK-NEXT: ret void +; CHECK-NEXT: } +; CHECK: define private void @.cfi.jumptable() #1 section ".text.cfi" align 8 { +; CHECK-NEXT: entry: +; CHECK-NEXT: call void asm sideeffect "jmp ${0:c}@plt\0Aint3\0Aint3\0Aint3\0Ajmp ${1:c}@plt\0Aint3\0Aint3\0Aint3\0A", "s,s"(void (i8*)* @patatino.cfi, void (i8*)* @fo.cfi) +; CHECK-NEXT: unreachable +; CHECK-NEXT: } + +target triple = "x86_64-unknown-linux" +define internal void @fl() #0 !type !2 !type !3 { + ret void +} +define internal void @fk() #0 !type !2 !type !3 { +entry: + %0 = call i1 @llvm.type.test(i8* bitcast (void ()* @fl to i8*), metadata !"_ZTSFvP3bioE") + ret void +} + +declare i1 @llvm.type.test(i8*, metadata) + +define internal void @patatino(i8* %bp) unnamed_addr #0 !type !5 !type !6 { + ret void +} + +define internal void @fo(i8* %bp) #0 !type !5 !type !6 { + br label %p +p: ; preds = %entry + call void @fq(i8* blockaddress(@fo, %p)) + ret void +} + +declare !type !7 !type !6 void @fq(i8*) +!2 = !{i64 0, !"_ZTSFvE"} +!3 = !{i64 0, !"_ZTSFvE.generalized"} +!5 = !{i64 0, !"_ZTSFvP3bioE"} +!6 = !{i64 0, !"_ZTSFvPvE.generalized"} +!7 = !{i64 0, !"_ZTSFvPvE"}