From patchwork Mon Jan 30 21:05:07 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Moore, Catherine" X-Patchwork-Id: 138636 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 44DF5B6EEC for ; Tue, 31 Jan 2012 08:05:30 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1328562331; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:From:To:CC:Subject:Date:Message-ID:Content-Type: MIME-Version:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=m9Mol6k KIea+ARpUHeKcfU20Ajo=; b=pFxnI0BkdX/Prj1bgsqzXmca6qUskiUOljIa/Ht ubCJ09vSGi9Noljl8R0THRsepBtkB3bMCaNof4Amvdx0N5vTaCZhFUzdRtqKJpfz MPl/iSWcabX7/CbQNykI78tabRXXaexLdwxUqan0PJ6EThv6eBxyLby90YPQkCiZ ulyQ= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:From:To:CC:Subject:Date:Message-ID:Content-Type:MIME-Version:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=kXOYns+9U5nklOrEoE8jtiwqe4UTwafbU7wbJgAj312pwIXZwFfC6c+cxIk0nC fbploK1zOUk9725pNEVUFi7KcuaOig+G4WUVvq7YGgDzONE62Vlp5zIF3rczJU3W frE0b/ssSttQHHROL/lCoSQbRmzY1o5VqxgMC2jrUl4qg=; Received: (qmail 28616 invoked by alias); 30 Jan 2012 21:05:26 -0000 Received: (qmail 28601 invoked by uid 22791); 30 Jan 2012 21:05:24 -0000 X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=BAYES_00 X-Spam-Check-By: sourceware.org Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 30 Jan 2012 21:05:10 +0000 Received: from svr-orw-fem-01.mgc.mentorg.com ([147.34.98.93]) by relay1.mentorg.com with esmtp id 1RryPU-0003Tq-Ne from Catherine_Moore@mentor.com ; Mon, 30 Jan 2012 13:05:08 -0800 Received: from SVR-ORW-FEM-02.mgc.mentorg.com ([147.34.96.206]) by svr-orw-fem-01.mgc.mentorg.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.4675); Mon, 30 Jan 2012 13:05:08 -0800 Received: from NA-MBX-04.mgc.mentorg.com ([169.254.4.37]) by SVR-ORW-FEM-02.mgc.mentorg.com ([147.34.96.168]) with mapi id 14.01.0289.001; Mon, 30 Jan 2012 13:05:07 -0800 From: "Moore, Catherine" To: "Tang, Chung-Lin" , "gcc-patches@gcc.gnu.org" , "rdsandiford@googlemail.com" CC: "Moore, Catherine" Subject: RE: [committed] PR 51931: force non-MIPS16ness for long-branch tests (NOW RFA: MIPS16 Long Branch Patch) Date: Mon, 30 Jan 2012 21:05:07 +0000 Message-ID: MIME-Version: 1.0 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 > -----Original Message----- > From: Chung-Lin Tang [mailto:cltang@codesourcery.com] > Sent: Monday, January 30, 2012 4:36 AM > To: gcc-patches@gcc.gnu.org; rdsandiford@googlemail.com > Cc: Moore, Catherine > Subject: Re: [committed] PR 51931: force non-MIPS16ness for long-branch tests > > On 2012/1/22 06:33 PM, Richard Sandiford wrote: > > The MIPS16 port has never handled long branches properly; see PR 51931 > > for the details. It isn't easy to xfail MIPS16-specific problems at > > the dejagnu level because of -mflip-mips16, so the patch below forces > > a nomips16 attribute instead. > > > > Tested on mips64-linux-gnu and applied. > > > > Richard > > CCing Catherine, I think we have a fix for this? > I do have a patch. It's a heuristic and will not work in all instances, but it does allow many additional programs to successfully compile. For example, this scheme allowed me to build glibc in MIPS16-mode for a MIPS-Linux toolchain. The patch causes reorg to examine mips16 branches. For branches that are out-of-range, reorg will look for branches to the same target. If that branch is in range, the destination of the original branch becomes the new branch. If branches to the same target do not exist, then reorg will search for barriers that are in range and insert label+ branch at the barrier. Of the test cases mentioned in the bug report, gcc.c-torture/compile/20001226-1.c still fails due to a lack of barriers in the instruction stream. g++.dg/opt/longbranch1.C will pass. I've set off a test run with my patch applied against mainline. In the meantime, here's the patch. Richard, what do you think? Catherine 2012-01-30 Catherine Moore gcc/ * config/mips/mips.c (WALK_INSNS): New macro. (OK_FOR_MIPS16_BRANCH_OFFSET): New macro. (mips16_count_matching_branches): New function. (walk_insns_forward): New function. (walk_insns_reverse): New function. (mips16_update_branch_info): New function. (mips16_zero_branch_info): New function. (mips16_check_branches): New function. (mips_reorg_process_instructions): Call mips16_check_branches. * config/mips.h (mips16_branch_info_t): New typedef. Index: mips.c =================================================================== --- mips.c (revision 183732) +++ mips.c (working copy) @@ -123,6 +123,12 @@ (SUBINSN) != NEXT_INSN (SEQ_END (INSN)); \ (SUBINSN) = NEXT_INSN (SUBINSN)) +/* Walk the instruction chain either backwards or forwards. */ +#define WALK_INSNS(START_INSN, INSN, WALK_FUNCTION) \ + for ((INSN) = (WALK_FUNCTION) (START_INSN); \ + (INSN) != 0; \ + (INSN) = (WALK_FUNCTION) (INSN)) + /* True if bit BIT is set in VALUE. */ #define BITSET_P(VALUE, BIT) (((VALUE) & (1 << (BIT))) != 0) @@ -14938,7 +14944,214 @@ } return INTVAL (offset) <= entry->offset; } +#define OK_FOR_MIPS16_BRANCH(OFFSET) ((OFFSET) >= -65000 && (OFFSET) <= 65000) +static int +mips16_count_matching_branches (rtx jump_insn) +{ + rtx insn; + int label_uid, jump_uid; + int matches = 0; + + jump_uid = INSN_UID (jump_insn); + label_uid = INSN_UID (JUMP_LABEL (jump_insn)); + + /* Count the number of branches to the same label. */ + for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn)) + { + if (simplejump_p (insn)) + { + int insn_uid = INSN_UID (JUMP_LABEL (insn)); + if (insn_uid != jump_uid && label_uid == insn_uid) + matches++; + } + } + + return matches; +} + +static rtx +walk_insns_forward (rtx insn) +{ + return NEXT_INSN (insn); +} + +static rtx +walk_insns_reverse (rtx insn) +{ + return PREV_INSN (insn); +} + +static void +mips16_update_branch_info (mips16_branch_info_t *previous, + mips16_branch_info_t *current, + int insn_uid) +{ + /* Move current to previous. */ + previous->uid = current->uid; + previous->address = current->address; + previous->distance_to_dest = current->distance_to_dest; + previous->distance_from_src = current->distance_from_src; + + /* Update current. */ + current->uid = insn_uid; + current->address = INSN_ADDRESSES (insn_uid); +} + +static void +mips16_zero_branch_info (mips16_branch_info_t *branch_info) +{ + branch_info->uid = 0; + branch_info->address = 0; + branch_info->distance_to_dest = 0; + branch_info->distance_from_src = 0; +} + +static void +mips16_check_branches (rtx jump_insn) +{ + int label1_uid, label2_uid, jump_uid, insn_uid; + int offset; + rtx label1; + rtx insn, new_label, new_label_insn, new_jump, new_jump_label; + + rtx (*walk_function) (rtx); + + mips16_branch_info_t first_branch, current, previous; + + mips16_zero_branch_info (&first_branch); + mips16_zero_branch_info (¤t); + mips16_zero_branch_info (&previous); + + jump_uid = INSN_UID (jump_insn); + first_branch.uid = jump_uid; + first_branch.address = INSN_ADDRESSES (jump_uid); + + /* Don't reprocess branches that were introduced by mips16_check_branches. + The branches are known to be within range. We'll update INSN_ADDRESSES + for all of the new branches when we finish here. */ + if (first_branch.address == -1) + return; + + label1 = JUMP_LABEL (jump_insn); + label1_uid = INSN_UID (label1); + + offset = INSN_ADDRESSES (label1_uid) - INSN_ADDRESSES (jump_uid); + first_branch.distance_to_dest = offset; + + /* The branch offset is in range, nothing to do. */ + if (OK_FOR_MIPS16_BRANCH (offset)) + return; + + if (offset > 0) + walk_function = &walk_insns_forward; + else + walk_function = &walk_insns_reverse; + + /* The branch offset is too large. First figure out if there are + other branches to the same destination. If other branches to + the same destination are available, we'll branch there instead. */ + + if (mips16_count_matching_branches (jump_insn)) + { + /* If branches to the same label are available and in range, + then use those first. */ + WALK_INSNS (jump_insn, insn, walk_function) + { + if (JUMP_P (insn)) + { + insn_uid = INSN_UID (insn); + label2_uid = INSN_UID (JUMP_LABEL (insn)); + + if (label2_uid != label1_uid) + continue; + + mips16_update_branch_info (&previous, ¤t, insn_uid); + offset = INSN_ADDRESSES (insn_uid) - INSN_ADDRESSES (jump_uid); + current.distance_from_src = offset; + if (!OK_FOR_MIPS16_BRANCH (offset)) + { + /* If the previous candidate was in range, use it. */ + if (previous.address != 0 + && OK_FOR_MIPS16_BRANCH (previous.distance_from_src)) + { + new_label = gen_label_rtx (); + emit_label_before (new_label, insn); + redirect_jump (jump_insn, new_label, 0); + return; + } + continue; + } + + offset = INSN_ADDRESSES (label2_uid) - INSN_ADDRESSES (insn_uid); + current.distance_to_dest = offset; + if (!OK_FOR_MIPS16_BRANCH (offset)) + continue; + + /* The offset is in range. The new destination will + be this branch. */ + new_label = gen_label_rtx (); + emit_label_before (new_label, insn); + redirect_jump (jump_insn, new_label, 0); + return; + } + } + } + + /* Reset current and previous branch information. */ + mips16_zero_branch_info (¤t); + mips16_zero_branch_info (&previous); + + /* Search for a barrier instruction/insert a label and a jump. */ + WALK_INSNS (jump_insn, insn, walk_function) + { + int barrier_off, label_off; + + if (GET_CODE (insn) == BARRIER) + { + insn_uid = INSN_UID (insn); + mips16_update_branch_info (&previous, ¤t, insn_uid); + + barrier_off = INSN_ADDRESSES (insn_uid) - INSN_ADDRESSES (jump_uid); + label_off = INSN_ADDRESSES (label1_uid) - INSN_ADDRESSES (insn_uid); + current.distance_from_src = barrier_off; + current.distance_to_dest = label_off; + + if (!OK_FOR_MIPS16_BRANCH (barrier_off)) + { + /* If the previous candidate was in range, use that instead. */ + if (previous.address != 0 + && OK_FOR_MIPS16_BRANCH (previous.distance_from_src)) + { + new_label = gen_label_rtx (); + new_label_insn = emit_label_before (new_label, insn); + redirect_jump (jump_insn, new_label, 0); + new_jump_label = gen_jump (label1); + new_jump = emit_jump_insn_after (new_jump_label, + new_label_insn); + INSN_ADDRESSES_NEW (new_jump, -1); + JUMP_LABEL (new_jump) = new_jump_label; + return; + } + continue; + } + + /* If the offset is too large, search for another candidate. */ + if (!OK_FOR_MIPS16_BRANCH (label_off)) + continue; + + new_label = gen_label_rtx (); + new_label_insn = emit_label_before (new_label, insn); + redirect_jump (jump_insn, new_label, 0); + new_jump_label = gen_jump (label1); + new_jump = emit_jump_insn_after (new_jump_label, new_label_insn); + INSN_ADDRESSES_NEW (new_jump, -1); + JUMP_LABEL (new_jump) = new_jump_label; + return; + } + } +} + /* A for_each_rtx callback for which DATA is a mips_lo_sum_offset hash table. Record every LO_SUM in *LOC. */ @@ -15103,6 +15316,17 @@ htab = htab_create (37, mips_lo_sum_offset_hash, mips_lo_sum_offset_eq, free); + /* Ensure that MIPS16 branches are within range. */ + if (TARGET_MIPS16) + { + for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn)) + { + if (simplejump_p (insn) || any_condjump_p (insn)) + mips16_check_branches (insn); + } + shorten_branches (get_insns ()); + } + /* Make a first pass over the instructions, recording all the LO_SUMs. */ for (insn = get_insns (); insn != 0; insn = NEXT_INSN (insn)) FOR_EACH_SUBINSN (subinsn, insn)