[llvm] r298003 - Make Argument::getArgNo() constant time, not O(#args)

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 16 15:25:45 PDT 2017


Author: rnk
Date: Thu Mar 16 17:25:45 2017
New Revision: 298003

URL: http://llvm.org/viewvc/llvm-project?rev=298003&view=rev
Log:
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

Modified:
    llvm/trunk/include/llvm/IR/Argument.h
    llvm/trunk/lib/IR/Function.cpp

Modified: llvm/trunk/include/llvm/IR/Argument.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Argument.h?rev=298003&r1=298002&r2=298003&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Argument.h (original)
+++ llvm/trunk/include/llvm/IR/Argument.h Thu Mar 16 17:25:45 2017
@@ -32,6 +32,7 @@ template <typename NodeTy> class SymbolT
 class Argument : public Value, public ilist_node<Argument> {
   virtual void anchor();
   Function *Parent;
+  unsigned ArgNo;
 
   friend class SymbolTableListTraits<Argument>;
   void setParent(Function *parent);
@@ -39,7 +40,8 @@ class Argument : public Value, public il
 public:
   /// If \p F is specified, the argument is inserted at the end of the argument
   /// list for \p F.
-  explicit Argument(Type *Ty, const Twine &Name = "", Function *F = nullptr);
+  explicit Argument(Type *Ty, const Twine &Name = "", Function *F = nullptr,
+                    unsigned ArgNo = 0);
 
   inline const Function *getParent() const { return Parent; }
   inline       Function *getParent()       { return Parent; }
@@ -47,7 +49,7 @@ public:
   /// Return the index of this formal argument in its containing function.
   ///
   /// For example in "void foo(int a, float b)" a is 0 and b is 1.
-  unsigned getArgNo() const;
+  unsigned getArgNo() const { return ArgNo; }
 
   /// Return true if this argument has the nonnull attribute. Also returns true
   /// if at least one byte is known to be dereferenceable and the pointer is in

Modified: llvm/trunk/lib/IR/Function.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Function.cpp?rev=298003&r1=298002&r2=298003&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Function.cpp (original)
+++ llvm/trunk/lib/IR/Function.cpp Thu Mar 16 17:25:45 2017
@@ -39,8 +39,8 @@ template class llvm::SymbolTableListTrai
 
 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 *paren
   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() cons
   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.




More information about the llvm-commits mailing list