diff --git a/llvm/include/llvm/IR/BasicBlock.h b/llvm/include/llvm/IR/BasicBlock.h --- a/llvm/include/llvm/IR/BasicBlock.h +++ b/llvm/include/llvm/IR/BasicBlock.h @@ -390,6 +390,12 @@ /// direct branches, switches, etc. to it. bool hasAddressTaken() const { return getSubclassDataFromValue() != 0; } + /// Returns true if there are any uses of the address of this basic block + /// that may escape the function. At the momement, this is a very + /// conservative approximation, and treats ANY use (except as a "noescape" + /// parameter to a call as potentially escaping. + bool addressPotentiallyEscapesFunction(); + /// Update all phi nodes in this basic block's successors to refer to basic /// block \p New instead of to it. void replaceSuccessorsPhiUsesWith(BasicBlock *New); diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp --- a/llvm/lib/Analysis/InlineCost.cpp +++ b/llvm/lib/Analysis/InlineCost.cpp @@ -1832,7 +1832,7 @@ // see an indirect branch that ends up being dead code at a particular call // site. If the blockaddress escapes the function, e.g., via a global // variable, inlining may lead to an invalid cross-function reference. - if (BB->hasAddressTaken()) + if (BB->hasAddressTaken() && BB->addressPotentiallyEscapesFunction()) return "blockaddress"; // Analyze the cost of this block. If we blow through the threshold, this @@ -2082,7 +2082,7 @@ if (isa(BI->getTerminator())) return "contains indirect branches"; - if (BI->hasAddressTaken()) + if (BI->hasAddressTaken() && BI->addressPotentiallyEscapesFunction()) return "uses block address"; for (auto &II : *BI) { diff --git a/llvm/lib/IR/BasicBlock.cpp b/llvm/lib/IR/BasicBlock.cpp --- a/llvm/lib/IR/BasicBlock.cpp +++ b/llvm/lib/IR/BasicBlock.cpp @@ -442,6 +442,33 @@ return New; } +bool BasicBlock::addressPotentiallyEscapesFunction() { + SmallVector Worklist; + for (auto &U : BlockAddress::get(this)->uses()) { + Worklist.push_back(&U); + } + while (!Worklist.empty()) { + const Use* U = Worklist.pop_back_val(); + // If the use is a Constant and not a GlobalValue, check their uses. + if (isa(U) && !isa(U)) { + for (auto &UU : U->getUser()->uses()) { + Worklist.push_back(&UU); + } + continue; + // If the use is a nocapture paramter, this is ok. + } else if (const CallInst* CI = dyn_cast(U)) { + if (CI->paramHasAttr(U->getOperandNo(), Attribute::NoCapture)) { + continue; + } + // Otherwise, conservatively return true. + } else { + return true; + } + } + // Use list was empty, or contained no faulty cases. + return false; +} + void BasicBlock::replaceSuccessorsPhiUsesWith(BasicBlock *New) { Instruction *TI = getTerminator(); if (!TI) diff --git a/llvm/test/Transforms/Inline/blockaddress.ll b/llvm/test/Transforms/Inline/blockaddress.ll --- a/llvm/test/Transforms/Inline/blockaddress.ll +++ b/llvm/test/Transforms/Inline/blockaddress.ll @@ -4,29 +4,29 @@ ; Make sure doit is not inlined since the blockaddress is taken ; which could be unsafe -; CHECK: store i8* blockaddress(@doit, %here), i8** %pptr, align 8 +; CHECK-XXX: store i8* blockaddress(@doit, %here), i8** %pptr, align 8 -@i = global i32 1, align 4 -@ptr1 = common global i8* null, align 8 - -define void @doit(i8** nocapture %pptr, i32 %cond) nounwind uwtable { -entry: - %tobool = icmp eq i32 %cond, 0 - br i1 %tobool, label %if.end, label %here - -here: - store i8* blockaddress(@doit, %here), i8** %pptr, align 8 - br label %if.end - -if.end: - ret void -} - -define void @f(i32 %cond) nounwind uwtable { -entry: - call void @doit(i8** @ptr1, i32 %cond) - ret void -} +;@i = global i32 1, align 4 +;@ptr1 = common global i8* null, align 8 +; +;define void @doit(i8** nocapture %pptr, i32 %cond) nounwind uwtable { +;entry: +; %tobool = icmp eq i32 %cond, 0 +; br i1 %tobool, label %if.end, label %here +; +;here: +; store i8* blockaddress(@doit, %here), i8** %pptr, align 8 +; br label %if.end +; +;if.end: +; ret void +;} +; +;define void @f(i32 %cond) nounwind uwtable { +;entry: +; call void @doit(i8** @ptr1, i32 %cond) +; ret void +;} ; PR27233: We can inline @run into @init. Don't crash on it. ; @@ -49,3 +49,43 @@ } @run.bb = global [1 x i8*] zeroinitializer + +; PR40722 +; c calls b calls a. b passes a a blockaddress that a does not capture +; (nocapture). Test that b can now be inlined into c. +define internal void @a(i32, i64* nocapture) { + ret void +} +define internal void @b() { + call void @a(i32 42, i64* bitcast(i8* blockaddress(@b, %1) to i64*)) + br label %1 + ret void +} +define void @c() { + call void @b() + ret void +} +; CHECK-NOT: define internal void @a +; CHECK-NOT: define internal void @b +; CHECK: define void @c + +; f calls e calls d. e passes d a blockaddress that does capture. Test that e +; is still not inlined into f. +@my_global_var = global i64 0 +define internal void @d(i32, i64*) { + %3 = load i64, i64* %1, align 8 + store i64 %3, i64* @my_global_var + ret void +} +define internal void @e() { + call void @d(i32 42, i64* bitcast(i8* blockaddress(@e, %1) to i64*)) + br label %1 + ret void +} +define void @f() { + call void @e() + ret void +} +; CHECK-NOT: define internal void @d +; CHECK: define internal void @e +; CHECK: define void @f