From eb54909c8c7381fefdb9a0ebd457fee0ebd3088c Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 16 Mar 2017 22:25:45 +0000 Subject: Make Argument::getArgNo() constant time, not O(#args) getArgNo is actually hot in LLVM, because its how we check for attributes on arguments: bool Argument::hasNonNullAttr() const { if (!getType()->isPointerTy()) return false; if (getParent()->getAttributes(). hasAttribute(getArgNo()+1, Attribute::NonNull)) return true; It actually shows up as the 23rd hottest leaf function in a 13s sample of LTO of llc. This grows Argument by four bytes, but I have another pending patch to shrink it by removing its ilist_node base. Reviewed By: chandlerc Subscribers: inglorion, llvm-commits, mehdi_amini Differential Revision: https://reviews.llvm.org/D31057 llvm-svn: 298003 --- llvm/lib/IR/Function.cpp | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) (limited to 'llvm/lib/IR/Function.cpp') diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp index 1318b5163be..228152e99fb 100644 --- a/llvm/lib/IR/Function.cpp +++ b/llvm/lib/IR/Function.cpp @@ -39,8 +39,8 @@ template class llvm::SymbolTableListTraits; void Argument::anchor() { } -Argument::Argument(Type *Ty, const Twine &Name, Function *Par) - : Value(Ty, Value::ArgumentVal) { +Argument::Argument(Type *Ty, const Twine &Name, Function *Par, unsigned ArgNo) + : Value(Ty, Value::ArgumentVal), ArgNo(ArgNo) { Parent = nullptr; if (Par) @@ -52,18 +52,6 @@ void Argument::setParent(Function *parent) { Parent = parent; } -unsigned Argument::getArgNo() const { - const Function *F = getParent(); - assert(F && "Argument is not in a function"); - - Function::const_arg_iterator AI = F->arg_begin(); - unsigned ArgIdx = 0; - for (; &*AI != this; ++AI) - ++ArgIdx; - - return ArgIdx; -} - bool Argument::hasNonNullAttr() const { if (!getType()->isPointerTy()) return false; if (getParent()->getAttributes(). @@ -244,7 +232,8 @@ void Function::BuildLazyArguments() const { for (unsigned i = 0, e = FT->getNumParams(); i != e; ++i) { assert(!FT->getParamType(i)->isVoidTy() && "Cannot have void typed arguments!"); - ArgumentList.push_back(new Argument(FT->getParamType(i))); + ArgumentList.push_back( + new Argument(FT->getParamType(i), "", nullptr, i)); } // Clear the lazy arguments bit. -- cgit v1.2.3