[llvm] r202687 - [C++11] Add two range adaptor views to User: operands and

Chandler Carruth chandlerc at gmail.com
Mon Mar 3 02:42:59 PST 2014


Author: chandlerc
Date: Mon Mar  3 04:42:58 2014
New Revision: 202687

URL: http://llvm.org/viewvc/llvm-project?rev=202687&view=rev
Log:
[C++11] Add two range adaptor views to User: operands and
operand_values. The first provides a range view over operand Use
objects, and the second provides a range view over the Value*s being
used by those operands.

The naming is "STL-style" rather than "LLVM-style" because we have
historically named iterator methods STL-style, and range methods seem to
have far more in common with their iterator counterparts than with
"normal" APIs. Feel free to bikeshed on this one if you want, I'm happy
to change these around if people feel strongly.

I've switched code in SROA and LCG to exercise these mostly to ensure
they work correctly -- we don't really have an easy way to unittest this
and they're trivial.

Modified:
    llvm/trunk/include/llvm/IR/User.h
    llvm/trunk/lib/Analysis/LazyCallGraph.cpp
    llvm/trunk/lib/Transforms/Scalar/SROA.cpp

Modified: llvm/trunk/include/llvm/IR/User.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/User.h?rev=202687&r1=202686&r2=202687&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/User.h (original)
+++ llvm/trunk/include/llvm/IR/User.h Mon Mar  3 04:42:58 2014
@@ -19,6 +19,7 @@
 #ifndef LLVM_IR_USER_H
 #define LLVM_IR_USER_H
 
+#include "llvm/ADT/iterator_range.h"
 #include "llvm/IR/Value.h"
 #include "llvm/Support/ErrorHandling.h"
 
@@ -112,11 +113,19 @@ public:
   //
   typedef Use*       op_iterator;
   typedef const Use* const_op_iterator;
+  typedef iterator_range<op_iterator> op_range;
+  typedef iterator_range<const_op_iterator> const_op_range;
 
   inline op_iterator       op_begin()       { return OperandList; }
   inline const_op_iterator op_begin() const { return OperandList; }
   inline op_iterator       op_end()         { return OperandList+NumOperands; }
   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }
+  inline op_range operands() {
+    return {op_begin(), op_end()};
+  }
+  inline const_op_range operands() const {
+    return {op_begin(), op_end()};
+  }
 
   /// Convenience iterator for directly iterating over the Values in the
   /// OperandList
@@ -156,6 +165,9 @@ public:
   inline value_op_iterator value_op_end() {
     return value_op_iterator(op_end());
   }
+  inline iterator_range<value_op_iterator> operand_values() {
+    return {value_op_begin(), value_op_end()};
+  }
 
   // dropAllReferences() - This function is in charge of "letting go" of all
   // objects that this User refers to.  This allows one to

Modified: llvm/trunk/lib/Analysis/LazyCallGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LazyCallGraph.cpp?rev=202687&r1=202686&r2=202687&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/LazyCallGraph.cpp (original)
+++ llvm/trunk/lib/Analysis/LazyCallGraph.cpp Mon Mar  3 04:42:58 2014
@@ -40,11 +40,9 @@ static void findCallees(
       continue;
     }
 
-    for (User::value_op_iterator OI = C->value_op_begin(),
-                                 OE = C->value_op_end();
-         OI != OE; ++OI)
-      if (Visited.insert(cast<Constant>(*OI)))
-        Worklist.push_back(cast<Constant>(*OI));
+    for (Value *Op : C->operand_values())
+      if (Visited.insert(cast<Constant>(Op)))
+        Worklist.push_back(cast<Constant>(Op));
   }
 }
 
@@ -56,10 +54,8 @@ LazyCallGraph::Node::Node(LazyCallGraph
   for (Function::iterator BBI = F.begin(), BBE = F.end(); BBI != BBE; ++BBI)
     for (BasicBlock::iterator II = BBI->begin(), IE = BBI->end(); II != IE;
          ++II)
-      for (User::value_op_iterator OI = II->value_op_begin(),
-                                   OE = II->value_op_end();
-           OI != OE; ++OI)
-        if (Constant *C = dyn_cast<Constant>(*OI))
+      for (Value *Op : II->operand_values())
+        if (Constant *C = dyn_cast<Constant>(Op))
           if (Visited.insert(C))
             Worklist.push_back(C);
 

Modified: llvm/trunk/lib/Transforms/Scalar/SROA.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SROA.cpp?rev=202687&r1=202686&r2=202687&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SROA.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SROA.cpp Mon Mar  3 04:42:58 2014
@@ -3447,9 +3447,8 @@ bool SROA::runOnAlloca(AllocaInst &AI) {
                                         DE = S.dead_user_end();
        DI != DE; ++DI) {
     // Free up everything used by this instruction.
-    for (User::op_iterator DOI = (*DI)->op_begin(), DOE = (*DI)->op_end();
-         DOI != DOE; ++DOI)
-      clobberUse(*DOI);
+    for (Use &DeadOp : (*DI)->operands())
+      clobberUse(DeadOp);
 
     // Now replace the uses of this instruction.
     (*DI)->replaceAllUsesWith(UndefValue::get((*DI)->getType()));
@@ -3498,10 +3497,10 @@ void SROA::deleteDeadInstructions(SmallP
 
     I->replaceAllUsesWith(UndefValue::get(I->getType()));
 
-    for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)
-      if (Instruction *U = dyn_cast<Instruction>(*OI)) {
+    for (Use &Operand : I->operands())
+      if (Instruction *U = dyn_cast<Instruction>(Operand)) {
         // Zero out the operand and see if it becomes trivially dead.
-        *OI = 0;
+        Operand = 0;
         if (isInstructionTriviallyDead(U))
           DeadInsts.insert(U);
       }





More information about the llvm-commits mailing list