From patchwork Thu Sep 9 06:21:16 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 64257 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 36151B6EF7 for ; Thu, 9 Sep 2010 16:21:32 +1000 (EST) Received: (qmail 9908 invoked by alias); 9 Sep 2010 06:21:30 -0000 Received: (qmail 9896 invoked by uid 22791); 9 Sep 2010 06:21:29 -0000 X-SWARE-Spam-Status: No, hits=-2.3 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, TW_CC, T_RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (74.125.121.35) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 09 Sep 2010 06:21:24 +0000 Received: from kpbe16.cbf.corp.google.com (kpbe16.cbf.corp.google.com [172.25.105.80]) by smtp-out.google.com with ESMTP id o896LL0h027028 for ; Wed, 8 Sep 2010 23:21:21 -0700 Received: from pvg3 (pvg3.prod.google.com [10.241.210.131]) by kpbe16.cbf.corp.google.com with ESMTP id o896LKTt030623 for ; Wed, 8 Sep 2010 23:21:20 -0700 Received: by pvg3 with SMTP id 3so426976pvg.21 for ; Wed, 08 Sep 2010 23:21:19 -0700 (PDT) Received: by 10.142.117.21 with SMTP id p21mr15136wfc.86.1284013279756; Wed, 08 Sep 2010 23:21:19 -0700 (PDT) Received: from coign.google.com ([216.239.45.130]) by mx.google.com with ESMTPS id 9sm991045wfd.12.2010.09.08.23.21.18 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 08 Sep 2010 23:21:18 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: [gccgo] Don't lower len/cap of function call Date: Wed, 08 Sep 2010 23:21:16 -0700 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-System-Of-Record: true X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org If len or cap is called with a function call as an argument, then the function call has to be evaluated. This is true even if the result is known at compile time, as when the function returns an array. This patch makes sure that the function is called. Committed to gccgo branch. Ian diff -r 8151188f1b79 go/expressions.cc --- a/go/expressions.cc Wed Sep 08 22:46:45 2010 -0700 +++ b/go/expressions.cc Wed Sep 08 23:19:22 2010 -0700 @@ -6346,6 +6346,38 @@ this->set_args(new_args); } +// A traversal class which looks for a call expression. + +class Find_call_expression : public Traverse +{ + public: + Find_call_expression() + : Traverse(traverse_expressions), + found_(false) + { } + + int + expression(Expression**); + + bool + found() + { return this->found_; } + + private: + bool found_; +}; + +int +Find_call_expression::expression(Expression** pexpr) +{ + if ((*pexpr)->call_expression() != NULL) + { + this->found_ = true; + return TRAVERSE_EXIT; + } + return TRAVERSE_CONTINUE; +} + // Lower a builtin call expression. This turns new and make into // specific expressions. We also convert to a constant if we can. @@ -6404,6 +6436,20 @@ } else if (this->is_constant()) { + // We can only lower len and cap if there are no function calls + // in the arguments. Otherwise we have to make the call. + if (this->code_ == BUILTIN_LEN || this->code_ == BUILTIN_CAP) + { + Expression* arg = this->one_arg(); + if (!arg->is_constant()) + { + Find_call_expression find_call; + Expression::traverse(&arg, &find_call); + if (find_call.found()) + return this; + } + } + mpz_t ival; mpz_init(ival); Type* type;