<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 13, 2015, at 11:56 AM, David Blaikie <<a href="mailto:dblaikie@gmail.com" class="">dblaikie@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><br class="Apple-interchange-newline"><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 14px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 14px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">On Wed, May 13, 2015 at 11:48 AM, Pete Cooper<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:peter_cooper@apple.com" target="_blank" class="">peter_cooper@apple.com</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><br class=""><div class=""><div class=""><div class="h5"><blockquote type="cite" class=""><div class="">On May 13, 2015, at 10:51 AM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank" class="">dblaikie@gmail.com</a>> wrote:</div><br class=""><div class=""><br class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 14px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;" class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 14px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;">On Tue, May 12, 2015 at 6:12 PM, Pete Cooper<span class=""> </span><span dir="ltr" class=""><<a href="mailto:peter_cooper@apple.com" target="_blank" class="">peter_cooper@apple.com</a>></span><span class=""> </span>wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;">Author: pete<br class="">Date: Tue May 12 20:12:09 2015<br class="">New Revision: 237224<br class=""><br class="">URL:<span class=""> </span><a href="http://llvm.org/viewvc/llvm-project?rev=237224&view=rev" target="_blank" class="">http://llvm.org/viewvc/llvm-project?rev=237224&view=rev</a><br class="">Log:<br class="">Change a loop in LoopInfo to foreach. NFC<br class=""><br class="">Modified:<br class=""> <span class=""> </span>llvm/trunk/lib/Analysis/LoopInfo.cpp<br class=""><br class="">Modified: llvm/trunk/lib/Analysis/LoopInfo.cpp<br class="">URL:<span class=""> </span><a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LoopInfo.cpp?rev=237224&r1=237223&r2=237224&view=diff" target="_blank" class="">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LoopInfo.cpp?rev=237224&r1=237223&r2=237224&view=diff</a><br class="">==============================================================================<br class="">--- llvm/trunk/lib/Analysis/LoopInfo.cpp (original)<br class="">+++ llvm/trunk/lib/Analysis/LoopInfo.cpp Tue May 12 20:12:09 2015<br class="">@@ -65,8 +65,8 @@ bool Loop::isLoopInvariant(const Value *<br class=""> /// hasLoopInvariantOperands - Return true if all the operands of the<br class=""> /// specified instruction are loop invariant.<br class=""> bool Loop::hasLoopInvariantOperands(const Instruction *I) const {<br class="">- for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)<br class="">- if (!isLoopInvariant(I->getOperand(i)))<br class="">+ for (auto &Op : I->operands())<br class="">+ if (!isLoopInvariant(Op))<br class=""> return false;<br class=""></blockquote><div class=""><br class="">Could be written as:<br class=""><br class=""> <span class="Apple-converted-space"> </span>return std::all_of(I->operands().begin(), I->operands().end(), [&](Value *V) { return isLoopInvariant(V); );<br class=""><br class="">If you like. (but the range-based-ness does make that a bit inconvenient, though std::all_of adds a little readability improvement, I think - at some point we'll just write range-based versions of algorithms like this so we don't have to pass begin/end everywhere)<br class=""></div></div></div></blockquote></div></div>Thanks. Something like this:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class="">namespace<span style="" class=""><span class="Apple-converted-space"> </span>llvm {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">template</span><<span style="color: rgb(187, 44, 162);" class="">typename</span><span class="Apple-converted-space"> </span>T,<span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">class</span><span class="Apple-converted-space"> </span>UnaryPredicate></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">static</span><span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">inline</span><span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">bool</span><span class="Apple-converted-space"> </span>all_of(<span style="color: rgb(79, 129, 135);" class="">llvm</span>::<span style="color: rgb(112, 61, 170);" class="">iterator_range</span><T> Range, UnaryPredicate p) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">return</span><span class="Apple-converted-space"> </span><span style="color: rgb(112, 61, 170);" class="">std</span>::all_of(Range.begin(), Range.end(), p);</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">};</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span class=""><span style="color: rgb(187, 44, 162);" class="">bool</span> <span style="color: rgb(79, 129, 135);" class="">Loop</span>::hasLoopInvariantOperands(<span style="color: rgb(187, 44, 162);" class="">const</span> <span style="color: rgb(79, 129, 135);" class="">Instruction</span> *I) <span style="color: rgb(187, 44, 162);" class="">const</span> {</span><div style="margin: 0px; line-height: normal;" class=""> <span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">return</span><span class="Apple-converted-space"> </span><span style="color: rgb(49, 89, 93);" class="">all_of</span>(I-><span style="color: rgb(49, 89, 93);" class="">operands</span>(), [&](<span style="color: rgb(79, 129, 135);" class="">Value</span><span class="Apple-converted-space"> </span>*V) {<span class="Apple-converted-space"> </span><span style="color: rgb(187, 44, 162);" class="">return</span><span class="Apple-converted-space"> </span><span style="color: rgb(49, 89, 93);" class="">isLoopInvariant</span>(V); });</div><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">I chose to constrain is to <span style="color: rgb(112, 61, 170); line-height: normal;" class="">iterator_range for now just to avoid complicating it to require begin/end of any kind of container.<span class="Apple-converted-space"> </span></span></div></div></div></div></blockquote><div class=""><br class="">I wouldn't bother with the constraint - I'd just start with a generic ref range R:<br class=""></div></div></div></blockquote>Sounds good to me. Unfortunately operands is an iterator_range which can’t be passed by reference:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(120, 73, 42); min-height: 13px;" class=""><div style="margin: 0px; line-height: normal;" class=""><b class="">../lib/Analysis/LoopInfo.cpp:84:10: </b><span style="font-variant-ligatures: no-common-ligatures; color: #c33720" class=""><b class="">error: </b></span><b class="">no matching function for call to 'all_of'</b></div><div style="margin: 0px; line-height: normal;" class=""> return all_of(I->operands(), [this](Value *V) { return isLoopInvariant(V); });</div><div style="margin: 0px; line-height: normal; color: rgb(52, 189, 38);" class=""><b class=""> ^~~~~~</b></div><div style="margin: 0px; line-height: normal;" class=""><b class="">../lib/Analysis/LoopInfo.cpp:67:20: note: </b>candidate function [with R = llvm::iterator_range<const llvm::Use *>, UnaryPredicate = (lambda at ../lib/Analysis/LoopInfo.cpp:84:32)] not viable: expects an l-value for 1st argument</div><div style="margin: 0px; line-height: normal;" class="">static inline bool all_of(R &Range, UnaryPredicate p) {</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">So I ended up having to define by value and by reference all_of.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">template</span><<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">typename</span> R, <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> UnaryPredicate></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">inline</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">bool</span> all_of(R Range, UnaryPredicate p) {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">std</span>::all_of(Range.begin(), Range.end(), p);</div><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">template</span><<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">typename</span> R, <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> UnaryPredicate></div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">static</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">inline</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">bool</span> all_of(R &Range, UnaryPredicate p) {</div><div style="margin: 0px; line-height: normal;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">std</span>::all_of(Range.begin(), Range.end(), p);</div><div style="margin: 0px; line-height: normal;" class="">}</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">Pete</div><blockquote type="cite" class=""><div class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 14px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class=""><br class=""><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">template</span><<span style="color: rgb(187, 44, 162);" class="">typename</span> R, <font color="#bb2ca2" class="">typename</font> UnaryPredicate></div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""><span style="color: rgb(187, 44, 162);" class="">bool</span> all_of(<font color="#4f8187" class="">R &</font>Range, UnaryPredicate p) {</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class=""> <span style="color: rgb(187, 44, 162);" class="">return</span> <span style="color: rgb(112, 61, 170);" class="">std</span>::all_of(Range.begin(), Range.end(), p);</div><div style="margin: 0px; font-size: 11px; font-family: Menlo;" class="">}<br class=""><br class="">And if we one day need to, we can SFINAE this as appropriate. (heh, now I'm imagining writing this as an adaptor... but of course it wouldn't work because we need template argument deduction, etc... but it'd be fun to "rangify(std::all_of, Range, Predicate)" ;))</div> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(112, 61, 170); line-height: normal;" class=""> What do you think?</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(112, 61, 170); line-height: normal;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(112, 61, 170); line-height: normal;" class="">Cheers,</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(112, 61, 170); line-height: normal;" class="">Pete</span></div></div><span class=""><blockquote type="cite" class=""><div class=""><div class="gmail_quote" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 14px; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px;"><div class=""> </div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><br class=""> return true;<br class=""><br class=""><br class="">_______________________________________________<br class="">llvm-commits mailing list<br class=""><a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank" class="">llvm-commits@cs.uiuc.edu</a><br class=""><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank" class="">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a></blockquote></div></div></blockquote></span></div></div></blockquote></div></div></blockquote></div><br class=""></body></html>