[PATCH] D67138: Tweak StringRef operators for ever-so-slightly smaller assembly

Jordan Rose via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 3 18:20:04 PDT 2019


jordan_rose created this revision.
jordan_rose added a reviewer: jfb.
jordan_rose added a project: LLVM.
Herald added subscribers: llvm-commits, dexonsmith.
jordan_rose retitled this revision from "Tweak StringRef operators for ever-so-slightly faster assembly" to "Tweak StringRef operators for ever-so-slightly smaller assembly".

Although StringRef::compare guarantees to return 0, 1, or -1, testing against 0 saves a single instruction <https://godbolt.org/z/lWU0Zc> in the implementation of `<` by replacing a `cmp`/`sete` with `shr`, at least when compiling with Clang. (GCC appears to figure out the idiom regardless.) It's also more obvious that the implementations are correct because the bodies match the operator names.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D67138

Files:
  llvm/include/llvm/ADT/StringRef.h


Index: llvm/include/llvm/ADT/StringRef.h
===================================================================
--- llvm/include/llvm/ADT/StringRef.h
+++ llvm/include/llvm/ADT/StringRef.h
@@ -884,19 +884,19 @@
   inline bool operator!=(StringRef LHS, StringRef RHS) { return !(LHS == RHS); }
 
   inline bool operator<(StringRef LHS, StringRef RHS) {
-    return LHS.compare(RHS) == -1;
+    return LHS.compare(RHS) < 0;
   }
 
   inline bool operator<=(StringRef LHS, StringRef RHS) {
-    return LHS.compare(RHS) != 1;
+    return LHS.compare(RHS) <= 0;
   }
 
   inline bool operator>(StringRef LHS, StringRef RHS) {
-    return LHS.compare(RHS) == 1;
+    return LHS.compare(RHS) > 0;
   }
 
   inline bool operator>=(StringRef LHS, StringRef RHS) {
-    return LHS.compare(RHS) != -1;
+    return LHS.compare(RHS) >= 0;
   }
 
   inline std::string &operator+=(std::string &buffer, StringRef string) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67138.218572.patch
Type: text/x-patch
Size: 904 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190904/59802354/attachment.bin>


More information about the llvm-commits mailing list