[PATCH] D31057: Make Argument::getArgNo() constant time, not O(#args)

Reid Kleckner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 16 15:38:07 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL298003: Make Argument::getArgNo() constant time, not O(#args) (authored by rnk).

Changed prior to commit:
  https://reviews.llvm.org/D31057?vs=92064&id=92077#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D31057

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


Index: llvm/trunk/include/llvm/IR/Argument.h
===================================================================
--- llvm/trunk/include/llvm/IR/Argument.h
+++ llvm/trunk/include/llvm/IR/Argument.h
@@ -32,22 +32,24 @@
 class Argument : public Value, public ilist_node<Argument> {
   virtual void anchor();
   Function *Parent;
+  unsigned ArgNo;
 
   friend class SymbolTableListTraits<Argument>;
   void setParent(Function *parent);
 
 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; }
 
   /// 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
Index: llvm/trunk/lib/IR/Function.cpp
===================================================================
--- llvm/trunk/lib/IR/Function.cpp
+++ llvm/trunk/lib/IR/Function.cpp
@@ -39,8 +39,8 @@
 
 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 @@
   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 @@
   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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31057.92077.patch
Type: text/x-patch
Size: 2583 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170316/df0d26a0/attachment.bin>


More information about the llvm-commits mailing list