From 24c79ab7cc6fb3538087b98bf3641f2075ecc63d Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 1 Mar 2015 23:35:20 +0000 Subject: [PATCH] ArrayRef: Put back std::equal for operator== with a check for the empty ArrayRefs This has the nice property of compiling down to memcmp when feasible. An empty ArrayRef can have a nullptr in its Data field. I didn't find anything in the standard speaking against std::equal(nullptr, nullptr, nullptr) begin valid but MSVC asserts. The way libstdc++ lowers std::equal down to memcmp also makes invoking std::equal with a nullptr undefined behavior so checking is the only way to be safe. The extra check doesn't cost us perf either because we're essentially peeling the loop header away from the rotated loop. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@230920 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/ArrayRef.h | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/include/llvm/ADT/ArrayRef.h b/include/llvm/ADT/ArrayRef.h index 7f5fa3271ac..e973054daef 100644 --- a/include/llvm/ADT/ArrayRef.h +++ b/include/llvm/ADT/ArrayRef.h @@ -135,15 +135,9 @@ namespace llvm { /// equals - Check for element-wise equality. bool equals(ArrayRef RHS) const { - if (Length != RHS.Length) + if (Length != RHS.Length || Length == 0) return false; - // Don't use std::equal(), since it asserts in MSVC on nullptr iterators. - for (auto L = begin(), LE = end(), R = RHS.begin(); L != LE; ++L, ++R) - // Match std::equal() in using == (instead of !=) to minimize API - // requirements of ArrayRef'ed types. - if (!(*L == *R)) - return false; - return true; + return std::equal(begin(), end(), RHS.begin()); } /// slice(n) - Chop off the first N elements of the array. -- 2.34.1