Return the LogMessage reference inline from modifier and streaming methods.

When out-of-line methods return a reference to the message, the compiler cannot see at the callsite that the returned reference is the same object. Chained calls end up copying the return register into the argument register and keeping an extra saved register alive across the statement.

Move the reference return into small inline wrappers in the header that delegate the work to out-of-line helpers returning nothing.

PiperOrigin-RevId: 1015115288
diff --git a/absl/log/internal/log_message.cc b/absl/log/internal/log_message.cc
index e56ef25..8bdbf60 100644
--- a/absl/log/internal/log_message.cc
+++ b/absl/log/internal/log_message.cc
@@ -300,39 +300,32 @@
 // This cannot go in the header since LogMessageData is defined in this file.
 LogMessage::~LogMessage() = default;
 
-LogMessage& LogMessage::AtLocation(absl::string_view file, int line) {
+void LogMessage::AtLocationImpl(absl::string_view file, int line) {
   data_->entry.full_filename_ = file;
   data_->entry.base_filename_ = Basename(file);
   data_->entry.line_ = line;
   LogBacktraceIfNeeded();
-  return *this;
 }
 
-LogMessage& LogMessage::NoPrefix() {
-  data_->entry.prefix_ = false;
-  return *this;
-}
+void LogMessage::NoPrefixImpl() { data_->entry.prefix_ = false; }
 
-LogMessage& LogMessage::WithVerbosity(int verbose_level) {
+void LogMessage::WithVerbosityImpl(int verbose_level) {
   if (verbose_level == absl::LogEntry::kNoVerbosityLevel) {
     data_->entry.verbose_level_ = absl::LogEntry::kNoVerbosityLevel;
   } else {
     data_->entry.verbose_level_ = std::max(0, verbose_level);
   }
-  return *this;
 }
 
-LogMessage& LogMessage::WithTimestamp(absl::Time timestamp) {
+void LogMessage::WithTimestampImpl(absl::Time timestamp) {
   data_->entry.timestamp_ = timestamp;
-  return *this;
 }
 
-LogMessage& LogMessage::WithThreadID(absl::LogEntry::tid_t tid) {
+void LogMessage::WithThreadIDImpl(absl::LogEntry::tid_t tid) {
   data_->entry.tid_ = tid;
-  return *this;
 }
 
-LogMessage& LogMessage::WithMetadataFrom(const absl::LogEntry& entry) {
+void LogMessage::WithMetadataFromImpl(const absl::LogEntry& entry) {
   data_->entry.full_filename_ = entry.full_filename_;
   data_->entry.base_filename_ = entry.base_filename_;
   data_->entry.line_ = entry.line_;
@@ -341,26 +334,20 @@
   data_->entry.verbose_level_ = entry.verbose_level_;
   data_->entry.timestamp_ = entry.timestamp_;
   data_->entry.tid_ = entry.tid_;
-  return *this;
 }
 
-LogMessage& LogMessage::WithPerror() {
-  data_->is_perror = true;
-  return *this;
-}
+void LogMessage::WithPerrorImpl() { data_->is_perror = true; }
 
-LogMessage& LogMessage::ToSinkAlso(absl::LogSink* absl_nonnull sink) {
+void LogMessage::ToSinkAlsoImpl(absl::LogSink* absl_nonnull sink) {
   ABSL_INTERNAL_CHECK(sink, "null LogSink*");
   data_->extra_sinks.push_back(sink);
-  return *this;
 }
 
-LogMessage& LogMessage::ToSinkOnly(absl::LogSink* absl_nonnull sink) {
+void LogMessage::ToSinkOnlyImpl(absl::LogSink* absl_nonnull sink) {
   ABSL_INTERNAL_CHECK(sink, "null LogSink*");
   data_->extra_sinks.clear();
   data_->extra_sinks.push_back(sink);
   data_->extra_sinks_only = true;
-  return *this;
 }
 
 #ifdef __ELF__
@@ -400,16 +387,6 @@
   _exit(1);
 }
 
-LogMessage& LogMessage::operator<<(const std::string& v) {
-  CopyToEncodedBuffer<StringType::kNotLiteral>(v);
-  return *this;
-}
-
-LogMessage& LogMessage::operator<<(absl::string_view v) {
-  CopyToEncodedBuffer<StringType::kNotLiteral>(v);
-  return *this;
-}
-
 LogMessage& LogMessage::operator<<(const std::wstring& v) {
   CopyToEncodedBuffer<StringType::kNotLiteral>(v);
   return *this;
@@ -449,22 +426,22 @@
 }
 // NOLINTBEGIN(runtime/int)
 // NOLINTBEGIN(google-runtime-int)
-template LogMessage& LogMessage::operator<<(const char& v);
-template LogMessage& LogMessage::operator<<(const signed char& v);
-template LogMessage& LogMessage::operator<<(const unsigned char& v);
-template LogMessage& LogMessage::operator<<(const short& v);
-template LogMessage& LogMessage::operator<<(const unsigned short& v);
-template LogMessage& LogMessage::operator<<(const int& v);
-template LogMessage& LogMessage::operator<<(const unsigned int& v);
-template LogMessage& LogMessage::operator<<(const long& v);
-template LogMessage& LogMessage::operator<<(const unsigned long& v);
-template LogMessage& LogMessage::operator<<(const long long& v);
-template LogMessage& LogMessage::operator<<(const unsigned long long& v);
-template LogMessage& LogMessage::operator<<(void* const& v);
-template LogMessage& LogMessage::operator<<(const void* const& v);
-template LogMessage& LogMessage::operator<<(const float& v);
-template LogMessage& LogMessage::operator<<(const double& v);
-template LogMessage& LogMessage::operator<<(const bool& v);
+template void LogMessage::Stream(const char& v);
+template void LogMessage::Stream(const signed char& v);
+template void LogMessage::Stream(const unsigned char& v);
+template void LogMessage::Stream(const short& v);
+template void LogMessage::Stream(const unsigned short& v);
+template void LogMessage::Stream(const int& v);
+template void LogMessage::Stream(const unsigned int& v);
+template void LogMessage::Stream(const long& v);
+template void LogMessage::Stream(const unsigned long& v);
+template void LogMessage::Stream(const long long& v);
+template void LogMessage::Stream(const unsigned long long& v);
+template void LogMessage::Stream(void* const& v);
+template void LogMessage::Stream(const void* const& v);
+template void LogMessage::Stream(const float& v);
+template void LogMessage::Stream(const double& v);
+template void LogMessage::Stream(const bool& v);
 // NOLINTEND(google-runtime-int)
 // NOLINTEND(runtime/int)
 
@@ -632,6 +609,17 @@
     absl::string_view str);
 template void LogMessage::CopyToEncodedBuffer<
     LogMessage::StringType::kNotLiteral>(absl::string_view str);
+
+template <LogMessage::StringType str_type>
+ABSL_ATTRIBUTE_NOINLINE void LogMessage::CopyToEncodedBuffer(
+    const std::string& str) {
+  CopyToEncodedBuffer<str_type>(absl::string_view(str));
+}
+template void LogMessage::CopyToEncodedBuffer<LogMessage::StringType::kLiteral>(
+    const std::string& str);
+template void LogMessage::CopyToEncodedBuffer<
+    LogMessage::StringType::kNotLiteral>(const std::string& str);
+
 template <LogMessage::StringType str_type>
 ABSL_ATTRIBUTE_NOINLINE void LogMessage::CopyToEncodedBuffer(char ch,
                                                              size_t num) {
diff --git a/absl/log/internal/log_message.h b/absl/log/internal/log_message.h
index cd12833..1663827 100644
--- a/absl/log/internal/log_message.h
+++ b/absl/log/internal/log_message.h
@@ -85,17 +85,28 @@
   LogMessage& operator=(const LogMessage&) = delete;
   ABSL_ATTRIBUTE_COLD ~LogMessage();
 
+  // Methods that return `*this` are inline wrappers around out-of-line void
+  // helpers so the compiler knows the returned reference aliases `this`.
+
   // Overrides the location inferred from the callsite.  The string pointed to
   // by `file` must be valid until the end of the statement.
-  LogMessage& AtLocation(absl::string_view file, int line);
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& AtLocation(absl::string_view file,
+                                                      int line) {
+    AtLocationImpl(file, line);
+    return *this;
+  }
   // `loc` doesn't default to `absl::SourceLocation::current()` here since the
   // callsite is already the default location for `LOG` statements.
-  LogMessage& AtLocation(absl::SourceLocation loc) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& AtLocation(
+      absl::SourceLocation loc) {
     return AtLocation(loc.file_name(), static_cast<int>(loc.line()));
   }
   // Omits the prefix from this line.  The prefix includes metadata about the
   // logged data such as source code location and timestamp.
-  LogMessage& NoPrefix();
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& NoPrefix() {
+    NoPrefixImpl();
+    return *this;
+  }
   // Sets the verbosity field of the logged message as if it was logged by
   // `VLOG(verbose_level)`.  Unlike `VLOG`, this method does not affect
   // evaluation of the statement when the specified `verbose_level` has been
@@ -103,74 +114,118 @@
   // make use of the `absl::LogSink::verbosity()` value.  The value
   // `absl::LogEntry::kNoVerbosityLevel` can be specified to mark the message
   // not verbose.
-  LogMessage& WithVerbosity(int verbose_level);
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& WithVerbosity(int verbose_level) {
+    WithVerbosityImpl(verbose_level);
+    return *this;
+  }
   // Uses the specified timestamp instead of one collected in the constructor.
-  LogMessage& WithTimestamp(absl::Time timestamp);
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& WithTimestamp(absl::Time timestamp) {
+    WithTimestampImpl(timestamp);
+    return *this;
+  }
   // Uses the specified thread ID instead of one collected in the constructor.
-  LogMessage& WithThreadID(absl::LogEntry::tid_t tid);
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& WithThreadID(
+      absl::LogEntry::tid_t tid) {
+    WithThreadIDImpl(tid);
+    return *this;
+  }
   // Copies all metadata (but no data) from the specified `absl::LogEntry`.
-  LogMessage& WithMetadataFrom(const absl::LogEntry& entry);
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& WithMetadataFrom(
+      const absl::LogEntry& entry) {
+    WithMetadataFromImpl(entry);
+    return *this;
+  }
   // Appends to the logged message a colon, a space, a textual description of
   // the current value of `errno` (as by strerror(3)), and the numerical value
   // of `errno`.
-  LogMessage& WithPerror();
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& WithPerror() {
+    WithPerrorImpl();
+    return *this;
+  }
   // Sends this message to `*sink` in addition to whatever other sinks it would
   // otherwise have been sent to.
-  LogMessage& ToSinkAlso(absl::LogSink* absl_nonnull sink);
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& ToSinkAlso(
+      absl::LogSink* absl_nonnull sink) {
+    ToSinkAlsoImpl(sink);
+    return *this;
+  }
   // Sends this message to `*sink` and no others.
-  LogMessage& ToSinkOnly(absl::LogSink* absl_nonnull sink);
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& ToSinkOnly(
+      absl::LogSink* absl_nonnull sink) {
+    ToSinkOnlyImpl(sink);
+    return *this;
+  }
 
   // Don't call this method from outside this library.
-  LogMessage& InternalStream() { return *this; }
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& InternalStream() { return *this; }
 
   // By-value overloads for small, common types let us overlook common failures
   // to define globals and static data members (i.e. in a .cc file).
   // NOLINTBEGIN(runtime/int)
   // NOLINTBEGIN(google-runtime-int)
   // clang-format off:  The CUDA toolchain cannot handle these <<<'s
-  LogMessage& operator<<(char v) { return operator<< <char>(v); }
-  LogMessage& operator<<(signed char v) { return operator<< <signed char>(v); }
-  LogMessage& operator<<(unsigned char v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(char v) {
+    return operator<< <char>(v);
+  }
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed char v) {
+    return operator<< <signed char>(v);
+  }
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned char v) {
     return operator<< <unsigned char>(v);
   }
-  LogMessage& operator<<(signed short v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed short v) {
     return operator<< <signed short>(v);
   }
-  LogMessage& operator<<(signed int v) { return operator<< <signed int>(v); }
-  LogMessage& operator<<(signed long v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed int v) {
+    return operator<< <signed int>(v);
+  }
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed long v) {
     return operator<< <signed long>(v);
   }
-  LogMessage& operator<<(signed long long v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(signed long long v) {
     return operator<< <signed long long>(v);
   }
-  LogMessage& operator<<(unsigned short v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned short v) {
     return operator<< <unsigned short>(v);
   }
-  LogMessage& operator<<(unsigned int v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned int v) {
     return operator<< <unsigned int>(v);
   }
-  LogMessage& operator<<(unsigned long v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned long v) {
     return operator<< <unsigned long>(v);
   }
-  LogMessage& operator<<(unsigned long long v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(unsigned long long v) {
     return operator<< <unsigned long long>(v);
   }
-  LogMessage& operator<<(void* absl_nullable  v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(void* absl_nullable v) {
     return operator<< <void*>(v);
   }
-  LogMessage& operator<<(const void* absl_nullable  v) {
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(
+      const void* absl_nullable v) {
     return operator<< <const void*>(v);
   }
-  LogMessage& operator<<(float v) { return operator<< <float>(v); }
-  LogMessage& operator<<(double v) { return operator<< <double>(v); }
-  LogMessage& operator<<(bool v) { return operator<< <bool>(v); }
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(float v) {
+    return operator<< <float>(v);
+  }
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(double v) {
+    return operator<< <double>(v);
+  }
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(bool v) {
+    return operator<< <bool>(v);
+  }
   // clang-format on
   // NOLINTEND(google-runtime-int)
   // NOLINTEND(runtime/int)
 
   // These overloads are more efficient since no `ostream` is involved.
-  LogMessage& operator<<(const std::string& v);
-  LogMessage& operator<<(absl::string_view v);
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(const std::string& v) {
+    CopyToEncodedBuffer<StringType::kNotLiteral>(v);
+    return *this;
+  }
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(absl::string_view v) {
+    CopyToEncodedBuffer<StringType::kNotLiteral>(v);
+    return *this;
+  }
 
   // Wide string overloads (since std::ostream does not provide them).
   LogMessage& operator<<(const std::wstring& v);
@@ -216,7 +271,7 @@
   // Types that don't support `AbslStringify()` but do support streaming into a
   // `std::ostream&` are serialized that way.
   template <typename T>
-  ABSL_ATTRIBUTE_NOINLINE LogMessage& operator<<(const T& v);
+  ABSL_ATTRIBUTE_ALWAYS_INLINE LogMessage& operator<<(const T& v);
 
   // Dispatches the completed `absl::LogEntry` to applicable `absl::LogSink`s.
   void Flush();
@@ -266,12 +321,28 @@
     absl::Span<char> string_start_;
   };
 
+  void AtLocationImpl(absl::string_view file, int line);
+  void NoPrefixImpl();
+  void WithVerbosityImpl(int verbose_level);
+  void WithTimestampImpl(absl::Time timestamp);
+  void WithThreadIDImpl(absl::LogEntry::tid_t tid);
+  void WithMetadataFromImpl(const absl::LogEntry& entry);
+  void WithPerrorImpl();
+  void ToSinkAlsoImpl(absl::LogSink* absl_nonnull sink);
+  void ToSinkOnlyImpl(absl::LogSink* absl_nonnull sink);
+
+  template <typename T>
+  ABSL_ATTRIBUTE_NOINLINE void Stream(const T& v);
+
   enum class StringType {
     kLiteral,
     kNotLiteral,
   };
   template <StringType str_type>
   void CopyToEncodedBuffer(absl::string_view str);
+  // Keeps std::string data and size access out of callsites.
+  template <StringType str_type>
+  void CopyToEncodedBuffer(const std::string& str);
   template <StringType str_type>
   void CopyToEncodedBuffer(char ch, size_t num);
   template <StringType str_type>
@@ -345,9 +416,15 @@
   LogMessage& message_;
 };
 
+template <typename T>
+inline LogMessage& LogMessage::operator<<(const T& v) {
+  Stream(v);
+  return *this;
+}
+
 // Note: the following is declared `ABSL_ATTRIBUTE_NOINLINE`
 template <typename T>
-LogMessage& LogMessage::operator<<(const T& v) {
+void LogMessage::Stream(const T& v) {
   if constexpr (absl::HasAbslStringify<T>::value) {
     StringifySink sink(*this);
     // Replace with public API.
@@ -356,12 +433,11 @@
     OstreamView view(*data_);
     view.stream() << log_internal::NullGuard<T>().Guard(v);
   }
-  return *this;
 }
 
 template <int SIZE>
 LogMessage& LogMessage::operator<<(const char (&buf)[SIZE]) {
-  CopyToEncodedBuffer<StringType::kLiteral>(buf);
+  CopyToEncodedBuffer<StringType::kLiteral>(absl::string_view(buf));
   return *this;
 }
 
@@ -374,7 +450,7 @@
 // Note: the following is declared `ABSL_ATTRIBUTE_NOINLINE`
 template <int SIZE>
 LogMessage& LogMessage::operator<<(char (&buf)[SIZE]) {
-  CopyToEncodedBuffer<StringType::kNotLiteral>(buf);
+  CopyToEncodedBuffer<StringType::kNotLiteral>(absl::string_view(buf));
   return *this;
 }
 // We instantiate these specializations in the library's TU to save space in
@@ -382,24 +458,22 @@
 // emitting a function call either way.
 // NOLINTBEGIN(runtime/int)
 // NOLINTBEGIN(google-runtime-int)
-extern template LogMessage& LogMessage::operator<<(const char& v);
-extern template LogMessage& LogMessage::operator<<(const signed char& v);
-extern template LogMessage& LogMessage::operator<<(const unsigned char& v);
-extern template LogMessage& LogMessage::operator<<(const short& v);
-extern template LogMessage& LogMessage::operator<<(const unsigned short& v);
-extern template LogMessage& LogMessage::operator<<(const int& v);
-extern template LogMessage& LogMessage::operator<<(const unsigned int& v);
-extern template LogMessage& LogMessage::operator<<(const long& v);
-extern template LogMessage& LogMessage::operator<<(const unsigned long& v);
-extern template LogMessage& LogMessage::operator<<(const long long& v);
-extern template LogMessage& LogMessage::operator<<(const unsigned long long& v);
-extern template LogMessage& LogMessage::operator<<(
-    void* absl_nullable const& v);
-extern template LogMessage& LogMessage::operator<<(
-    const void* absl_nullable const& v);
-extern template LogMessage& LogMessage::operator<<(const float& v);
-extern template LogMessage& LogMessage::operator<<(const double& v);
-extern template LogMessage& LogMessage::operator<<(const bool& v);
+extern template void LogMessage::Stream(const char& v);
+extern template void LogMessage::Stream(const signed char& v);
+extern template void LogMessage::Stream(const unsigned char& v);
+extern template void LogMessage::Stream(const short& v);
+extern template void LogMessage::Stream(const unsigned short& v);
+extern template void LogMessage::Stream(const int& v);
+extern template void LogMessage::Stream(const unsigned int& v);
+extern template void LogMessage::Stream(const long& v);
+extern template void LogMessage::Stream(const unsigned long& v);
+extern template void LogMessage::Stream(const long long& v);
+extern template void LogMessage::Stream(const unsigned long long& v);
+extern template void LogMessage::Stream(void* absl_nullable const& v);
+extern template void LogMessage::Stream(const void* absl_nullable const& v);
+extern template void LogMessage::Stream(const float& v);
+extern template void LogMessage::Stream(const double& v);
+extern template void LogMessage::Stream(const bool& v);
 // NOLINTEND(google-runtime-int)
 // NOLINTEND(runtime/int)
 
@@ -407,6 +481,10 @@
     LogMessage::StringType::kLiteral>(absl::string_view str);
 extern template void LogMessage::CopyToEncodedBuffer<
     LogMessage::StringType::kNotLiteral>(absl::string_view str);
+extern template void LogMessage::CopyToEncodedBuffer<
+    LogMessage::StringType::kLiteral>(const std::string& str);
+extern template void LogMessage::CopyToEncodedBuffer<
+    LogMessage::StringType::kNotLiteral>(const std::string& str);
 extern template void
 LogMessage::CopyToEncodedBuffer<LogMessage::StringType::kLiteral>(char ch,
                                                                   size_t num);