aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/InlineCost.cpp
diff options
context:
space:
mode:
authorDenis Antrushin <dantrushin@gmail.com>2023-05-03 00:16:42 +0700
committerDenis Antrushin <dantrushin@gmail.com>2023-05-25 18:43:16 +0300
commit291223409c6139b5d56efc769088e23ecee44faf (patch)
tree0d10a6e2ddabb8e2cb6d9eb58a1cf57c250dc90e /llvm/lib/Analysis/InlineCost.cpp
parent5c0f96da4c81d96cf5d288a2e64176d786e934b8 (diff)
downloadllvm-291223409c6139b5d56efc769088e23ecee44faf.zip
llvm-291223409c6139b5d56efc769088e23ecee44faf.tar.gz
llvm-291223409c6139b5d56efc769088e23ecee44faf.tar.bz2
[InlineCost] Consider branches with !make.implicit metadata as free.
!make.implicit metadata attached to branch means it will very likely be eliminated (together with associated cmp instruction). Reviewed By: apilipenko Differential Revision: https://reviews.llvm.org/D149747
Diffstat (limited to 'llvm/lib/Analysis/InlineCost.cpp')
-rw-r--r--llvm/lib/Analysis/InlineCost.cpp26
1 files changed, 20 insertions, 6 deletions
diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index 09d4b81..02871aa 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -1976,14 +1976,27 @@ bool CallAnalyzer::visitCmpInst(CmpInst &I) {
}
}
+ auto isImplicitNullCheckCmp = [](const CmpInst &I) {
+ for (auto *User : I.users())
+ if (auto *Instr = dyn_cast<Instruction>(User))
+ if (!Instr->getMetadata(LLVMContext::MD_make_implicit))
+ return false;
+ return true;
+ };
+
// If the comparison is an equality comparison with null, we can simplify it
// if we know the value (argument) can't be null
- if (I.isEquality() && isa<ConstantPointerNull>(I.getOperand(1)) &&
- isKnownNonNullInCallee(I.getOperand(0))) {
- bool IsNotEqual = I.getPredicate() == CmpInst::ICMP_NE;
- SimplifiedValues[&I] = IsNotEqual ? ConstantInt::getTrue(I.getType())
- : ConstantInt::getFalse(I.getType());
- return true;
+ if (I.isEquality() && isa<ConstantPointerNull>(I.getOperand(1))) {
+ if (isKnownNonNullInCallee(I.getOperand(0))) {
+ bool IsNotEqual = I.getPredicate() == CmpInst::ICMP_NE;
+ SimplifiedValues[&I] = IsNotEqual ? ConstantInt::getTrue(I.getType())
+ : ConstantInt::getFalse(I.getType());
+ return true;
+ }
+ // Implicit null checks act as unconditional branches and their comparisons
+ // should be treated as simplified and free of cost.
+ if (isImplicitNullCheckCmp(I))
+ return true;
}
return handleSROA(I.getOperand(0), isa<ConstantPointerNull>(I.getOperand(1)));
}
@@ -2265,6 +2278,7 @@ bool CallAnalyzer::visitBranchInst(BranchInst &BI) {
// inliner more regular and predictable. Interestingly, conditional branches
// which will fold away are also free.
return BI.isUnconditional() || isa<ConstantInt>(BI.getCondition()) ||
+ BI.getMetadata(LLVMContext::MD_make_implicit) ||
isa_and_nonnull<ConstantInt>(
SimplifiedValues.lookup(BI.getCondition()));
}