Don't materialize on insertion block changes

Materializing all variables should be done prior to actual branches, not
when changing the insert point. The only reason we did it in the latter
too was due to changing insert point before creating the branch for an
If statement.

Bug: b/180131694
Change-Id: Ic755bf07a098bda4af7e4d5a7d22138bee37d0c6
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/52968
Tested-by: Nicolas Capens <nicolascapens@google.com>
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/src/Reactor/LLVMReactor.cpp b/src/Reactor/LLVMReactor.cpp
index 3a56b4c..f93cb35 100644
--- a/src/Reactor/LLVMReactor.cpp
+++ b/src/Reactor/LLVMReactor.cpp
@@ -665,9 +665,7 @@
 
 void Nucleus::setInsertBlock(BasicBlock *basicBlock)
 {
-	//	assert(jit->builder->GetInsertBlock()->back().isTerminator());
-
-	Variable::materializeAll();
+	// assert(jit->builder->GetInsertBlock()->back().isTerminator());
 
 	jit->builder->SetInsertPoint(B(basicBlock));
 }
diff --git a/src/Reactor/Reactor.hpp b/src/Reactor/Reactor.hpp
index 87bfffd..bf5d8c0 100644
--- a/src/Reactor/Reactor.hpp
+++ b/src/Reactor/Reactor.hpp
@@ -3519,6 +3519,11 @@
 		falseBB = nullptr;
 		endBB = Nucleus::createBasicBlock();
 
+		// The conditional branch won't be appended until we've reached the 'end'
+		// basic block, so we must materialize all variables now (i.e. emit store
+		// instrutions to write them to memory).
+		Variable::materializeAll();
+
 		Nucleus::setInsertBlock(trueBB);
 	}
 
@@ -3526,6 +3531,8 @@
 	{
 		Nucleus::createBr(endBB);
 
+		// Append the conditional branch instruction to the 'begin' basic block.
+		// Note that it's too late to materialize variables at this point.
 		Nucleus::setInsertBlock(beginBB);
 		Nucleus::createCondBr(condition, trueBB, falseBB ? falseBB : endBB);
 
diff --git a/src/Reactor/SubzeroReactor.cpp b/src/Reactor/SubzeroReactor.cpp
index 1e0cf63..c536639 100644
--- a/src/Reactor/SubzeroReactor.cpp
+++ b/src/Reactor/SubzeroReactor.cpp
@@ -1122,9 +1122,7 @@
 
 void Nucleus::setInsertBlock(BasicBlock *basicBlock)
 {
-	//	ASSERT(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator");
-
-	Variable::materializeAll();
+	// ASSERT(::basicBlock->getInsts().back().getTerminatorEdges().size() >= 0 && "Previous basic block must have a terminator");
 
 	::basicBlock = basicBlock;
 }