[llvm] 0cc2acc - [LEB128] Don't handle edge cases in every loop iteration

Adrian Prantl via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 29 12:17:23 PST 2023


Author: Adrian Prantl
Date: 2023-11-29T12:16:32-08:00
New Revision: 0cc2acc30b3d2f4e914fd49c599cfde8a17f26a6

URL: https://github.com/llvm/llvm-project/commit/0cc2acc30b3d2f4e914fd49c599cfde8a17f26a6
DIFF: https://github.com/llvm/llvm-project/commit/0cc2acc30b3d2f4e914fd49c599cfde8a17f26a6.diff

LOG: [LEB128] Don't handle edge cases in every loop iteration

Previously the overflow check was done for every byte even though it
is only needed for the case where Shift == 63.

Added: 
    

Modified: 
    llvm/include/llvm/Support/LEB128.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Support/LEB128.h b/llvm/include/llvm/Support/LEB128.h
index 76c93acaadfdcc7..40b6cf79bacf758 100644
--- a/llvm/include/llvm/Support/LEB128.h
+++ b/llvm/include/llvm/Support/LEB128.h
@@ -142,7 +142,8 @@ inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
       break;
     }
     uint64_t Slice = *p & 0x7f;
-    if ((Shift >= 64 && Slice != 0) || Slice << Shift >> Shift != Slice) {
+    if (Shift >= 63 && ((Shift == 63 && (Slice << Shift >> Shift) != Slice) ||
+                        (Shift > 63 && Slice != 0))) {
       if (error)
         *error = "uleb128 too big for uint64";
       Value = 0;
@@ -177,8 +178,8 @@ inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
     }
     Byte = *p;
     uint64_t Slice = Byte & 0x7f;
-    if ((Shift >= 64 && Slice != (Value < 0 ? 0x7f : 0x00)) ||
-        (Shift == 63 && Slice != 0 && Slice != 0x7f)) {
+    if ((Shift >= 63) && ((Shift == 63 && Slice != 0 && Slice != 0x7f) ||
+                          (Shift > 63 && Slice != (Value < 0 ? 0x7f : 0x00)))) {
       if (error)
         *error = "sleb128 too big for int64";
       if (n)


        


More information about the llvm-commits mailing list