<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Mar 3, 2014 at 2:42 AM, Chandler Carruth <span dir="ltr"><<a href="mailto:chandlerc@gmail.com" target="_blank">chandlerc@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: chandlerc<br>
Date: Mon Mar  3 04:42:58 2014<br>
New Revision: 202687<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=202687&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=202687&view=rev</a><br>
Log:<br>
[C++11] Add two range adaptor views to User: operands and<br>
operand_values. The first provides a range view over operand Use<br>
objects, and the second provides a range view over the Value*s being<br>
used by those operands.<br>
<br>
The naming is "STL-style" rather than "LLVM-style" because we have<br>
historically named iterator methods STL-style, and range methods seem to<br>
have far more in common with their iterator counterparts than with<br>
"normal" APIs. Feel free to bikeshed on this one if you want, I'm happy<br>
to change these around if people feel strongly.<br>
<br>
I've switched code in SROA and LCG to exercise these mostly to ensure<br>
they work correctly -- we don't really have an easy way to unittest this<br>
and they're trivial.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/IR/User.h<br>
    llvm/trunk/lib/Analysis/LazyCallGraph.cpp<br>
    llvm/trunk/lib/Transforms/Scalar/SROA.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/IR/User.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/User.h?rev=202687&r1=202686&r2=202687&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/User.h?rev=202687&r1=202686&r2=202687&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/include/llvm/IR/User.h (original)<br>
+++ llvm/trunk/include/llvm/IR/User.h Mon Mar  3 04:42:58 2014<br>
@@ -19,6 +19,7 @@<br>
 #ifndef LLVM_IR_USER_H<br>
 #define LLVM_IR_USER_H<br>
<br>
+#include "llvm/ADT/iterator_range.h"<br>
 #include "llvm/IR/Value.h"<br>
 #include "llvm/Support/ErrorHandling.h"<br>
<br>
@@ -112,11 +113,19 @@ public:<br>
   //<br>
   typedef Use*       op_iterator;<br>
   typedef const Use* const_op_iterator;<br>
+  typedef iterator_range<op_iterator> op_range;<br>
+  typedef iterator_range<const_op_iterator> const_op_range;<br></blockquote><div><br></div><div>Just because this is a little precedent-setting: is there much/any value in having these typedefs? With old for loops we needed to name the iterator type so typedefs were helpful. If the range accessors are generally just going to be used via range-based-for, there seems less need for the typedefs.<br>
<br>But I don't feel strongly about it - just figured the conversation might be helpful.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
   inline op_iterator       op_begin()       { return OperandList; }<br>
   inline const_op_iterator op_begin() const { return OperandList; }<br>
   inline op_iterator       op_end()         { return OperandList+NumOperands; }<br>
   inline const_op_iterator op_end()   const { return OperandList+NumOperands; }<br>
+  inline op_range operands() {<br>
+    return {op_begin(), op_end()};<br>
+  }<br>
+  inline const_op_range operands() const {<br>
+    return {op_begin(), op_end()};<br>
+  }<br>
<br>
   /// Convenience iterator for directly iterating over the Values in the<br>
   /// OperandList<br>
@@ -156,6 +165,9 @@ public:<br>
   inline value_op_iterator value_op_end() {<br>
     return value_op_iterator(op_end());<br>
   }<br>
+  inline iterator_range<value_op_iterator> operand_values() {<br>
+    return {value_op_begin(), value_op_end()};<br>
+  }<br>
<br>
   // dropAllReferences() - This function is in charge of "letting go" of all<br>
   // objects that this User refers to.  This allows one to<br>
<br>
Modified: llvm/trunk/lib/Analysis/LazyCallGraph.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LazyCallGraph.cpp?rev=202687&r1=202686&r2=202687&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LazyCallGraph.cpp?rev=202687&r1=202686&r2=202687&view=diff</a><br>

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

==============================================================================<br>
--- llvm/trunk/lib/Transforms/Scalar/SROA.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Scalar/SROA.cpp Mon Mar  3 04:42:58 2014<br>
@@ -3447,9 +3447,8 @@ bool SROA::runOnAlloca(AllocaInst &AI) {<br>
                                         DE = S.dead_user_end();<br>
        DI != DE; ++DI) {<br>
     // Free up everything used by this instruction.<br>
-    for (User::op_iterator DOI = (*DI)->op_begin(), DOE = (*DI)->op_end();<br>
-         DOI != DOE; ++DOI)<br>
-      clobberUse(*DOI);<br>
+    for (Use &DeadOp : (*DI)->operands())<br>
+      clobberUse(DeadOp);<br>
<br>
     // Now replace the uses of this instruction.<br>
     (*DI)->replaceAllUsesWith(UndefValue::get((*DI)->getType()));<br>
@@ -3498,10 +3497,10 @@ void SROA::deleteDeadInstructions(SmallP<br>
<br>
     I->replaceAllUsesWith(UndefValue::get(I->getType()));<br>
<br>
-    for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI)<br>
-      if (Instruction *U = dyn_cast<Instruction>(*OI)) {<br>
+    for (Use &Operand : I->operands())<br>
+      if (Instruction *U = dyn_cast<Instruction>(Operand)) {<br>
         // Zero out the operand and see if it becomes trivially dead.<br>
-        *OI = 0;<br>
+        Operand = 0;<br>
         if (isInstructionTriviallyDead(U))<br>
           DeadInsts.insert(U);<br>
       }<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>