compiler: omit write barrier for assignment to *(convert(&local))
Assignments to local variables don't need a write barrier. But
currently the compiler inserts a write barrier if the LHS is a
local variable with type converted, as *(convert(&local)). Let
the compiler recognize this pattern and omit the write barrier.
Change-Id: Iabd73cd2890ff62850c316079bc1f25cda9870fc
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/182541
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/go/wb.cc b/go/wb.cc
index c1d54e6..501ad6a 100644
--- a/go/wb.cc
+++ b/go/wb.cc
@@ -735,6 +735,26 @@
}
}
+ // Nothing to do for an assignment to *(convert(&x)) where
+ // x is local variable or a temporary variable.
+ Unary_expression* ue = lhs->unary_expression();
+ if (ue != NULL && ue->op() == OPERATOR_MULT)
+ {
+ Expression* expr = ue->operand();
+ while (true)
+ {
+ if (expr->conversion_expression() != NULL)
+ expr = expr->conversion_expression()->expr();
+ else if (expr->unsafe_conversion_expression() != NULL)
+ expr = expr->unsafe_conversion_expression()->expr();
+ else
+ break;
+ }
+ ue = expr->unary_expression();
+ if (ue != NULL && ue->op() == OPERATOR_AND)
+ return this->assign_needs_write_barrier(ue->operand());
+ }
+
// For a struct assignment, we don't need a write barrier if all the
// pointer types can not be in the heap.
Struct_type* st = lhs->type()->struct_type();