Change for loops to use const refs, per performance-for-range-copy clang-tidy.

This CL optimizes C++11 range-based for loops where the variable is copied in each iteration but it would suffice to obtain it by const reference.  This is only applied to loop variables of types that are expensive to copy which means they are not trivially copyable or have a non-trivial copy constructor or destructor.

To ensure that it is safe to replace the copy with a const reference the following heuristic is employed:
  The loop variable is const qualified.
  The loop variable is not const, but only const methods or operators are invoked on it, or it is used as const reference or value argument in constructors or function calls.

See cl/304001946 for more information.

Bug: None.
Change-Id: Idbb5a0dbf19c6dccb16a0d525900aa99419a8527
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/43368
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Ben Clayton <bclayton@google.com>
diff --git a/src/OpenGL/compiler/preprocessor/MacroExpander.cpp b/src/OpenGL/compiler/preprocessor/MacroExpander.cpp
index a235112..7df8c09 100644
--- a/src/OpenGL/compiler/preprocessor/MacroExpander.cpp
+++ b/src/OpenGL/compiler/preprocessor/MacroExpander.cpp
@@ -83,11 +83,11 @@
 MacroExpander::ScopedMacroReenabler::~ScopedMacroReenabler()
 {
 	mExpander->mDeferReenablingMacros = false;
-	for (auto macro : mExpander->mMacrosToReenable)
+	for (const auto &macro : mExpander->mMacrosToReenable)
 	{
 		// Copying the string here by using substr is a check for use-after-free. It detects
 		// use-after-free more reliably than just toggling the disabled flag.
-		assert(macro->name.substr() != "");
+		assert(!macro->name.substr().empty());
 		macro->disabled = false;
 	}
 	mExpander->mMacrosToReenable.clear();
diff --git a/src/Pipeline/SpirvShaderControlFlow.cpp b/src/Pipeline/SpirvShaderControlFlow.cpp
index 6139002..670dbfd 100644
--- a/src/Pipeline/SpirvShaderControlFlow.cpp
+++ b/src/Pipeline/SpirvShaderControlFlow.cpp
@@ -472,7 +472,7 @@
 	// Continue emitting from the merge block.
 	Nucleus::setInsertBlock(mergeBasicBlock);
 	state->pending->push_back(mergeBlockId);
-	for(auto it : mergeActiveLaneMasks)
+	for(const auto &it : mergeActiveLaneMasks)
 	{
 		state->addActiveLaneMaskEdge(it.first, mergeBlockId, it.second);
 	}
@@ -574,7 +574,7 @@
 	ASSERT(function.blocks.size() == 1);
 	spv::Op wrapOpKill[] = { spv::OpLabel, spv::OpKill };
 
-	for(auto block : function.blocks)
+	for(const auto &block : function.blocks)
 	{
 		int insnNumber = 0;
 		for(auto blockInsn : block.second)
diff --git a/src/Shader/Shader.cpp b/src/Shader/Shader.cpp
index 7b60697..561ccbd 100644
--- a/src/Shader/Shader.cpp
+++ b/src/Shader/Shader.cpp
@@ -2078,6 +2078,7 @@
 			}
 		}
 
+#if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
 		// Assert that every function is reachable (these should have been
 		// stripped in earlier stages). Unreachable functions may be code
 		// generated, but their own limits are not considered below, potentially
@@ -2085,7 +2086,8 @@
 		// If we ever find cases where there are unreachable functions, we can
 		// replace this assert with NO-OPing or stripping out the dead
 		// functions.
-		for (auto it : functions) { ASSERT(it.second.reachable); }
+		for (const auto &it : functions) { ASSERT(it.second.reachable); }
+#endif
 
 		// We have now gathered all the information about each of the functions
 		// in the shader. Traverse these functions starting from the main
diff --git a/third_party/subzero/src/IceTargetLoweringX8664Traits.h b/third_party/subzero/src/IceTargetLoweringX8664Traits.h
index e82b03a..7eea684 100644
--- a/third_party/subzero/src/IceTargetLoweringX8664Traits.h
+++ b/third_party/subzero/src/IceTargetLoweringX8664Traits.h
@@ -679,7 +679,7 @@
     RandomNumberGeneratorWrapper RNGW(RNG);
 
     // Shuffle the resulting equivalence classes.
-    for (auto I : EquivalenceClasses) {
+    for (const auto &I : EquivalenceClasses) {
       const RegisterList &List = I.second;
       RegisterList Shuffled(List);
       RandomShuffle(Shuffled.begin(), Shuffled.end(), RNGW);
@@ -695,7 +695,7 @@
       OstreamLocker L(Func->getContext());
       Ostream &Str = Func->getContext()->getStrDump();
       Str << "Register equivalence classes:\n";
-      for (auto I : EquivalenceClasses) {
+      for (const auto &I : EquivalenceClasses) {
         Str << "{";
         const RegisterList &List = I.second;
         bool First = true;