From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by sourceware.org (Postfix) with ESMTPS id 42B083858D32 for ; Tue, 4 Oct 2022 17:34:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 42B083858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x62f.google.com with SMTP id x6so7975819pll.11 for ; Tue, 04 Oct 2022 10:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date; bh=F1Rmn5fJnE9qMcFqOuR1f38Ot/MR4Hg7TW6l1RpI7oQ=; b=USqTNi8g9HtTIyu2pNVJRW7KfEHkUTtELhA6+sBBFgTXi1bjf9r3HN2JhuU06V1cNn wklyVxplPM6WS33sAmCz/P3q+wh39ENIjISmx+M95kUiAml03akifSXLDA6daNmResG2 U/fX3fMiYgoGItni9TlfCF/ayOVx72wr+o1ElTgvJZfREPrW7RMIGY+AnsmsgpWnV0Bl MFzDUekVFf6mmih4YuWowcw+gDQLu0KvReFc82I4Rt8wed14gRJxFLYKtvUNIJpdrvTY FDSdP0El/xWkg4dJOc4MvdodYHvFOesSXvDLs05plH/6/RGc4RM4jnxGkHQ9EedaFk3R BhkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date; bh=F1Rmn5fJnE9qMcFqOuR1f38Ot/MR4Hg7TW6l1RpI7oQ=; b=2xgIBQuokB7DRBESiPUN35PPc/PWCeKEEy8JqBRtIJz7obCj3OEyt4Ku1o8F03fG5S 2JJMZRcQ1Pg4YrdGkQAkV+v6z+xXa3ARrYXBCKkSqu2hYoVufDu0jfsjvhbuVO8eUIBT RHpSWuvl+3B7sUx9MrCtTex9tfVoBRCikqQtPed/YkA133Jtc4NlIP/5Unb9erIzwDH3 Oog6h5wTiftyZTD/l8xPuxhxZ/jsqO2sDhA8AiD3rVSjRsgT9awQOFjC7Yd0ZicUqcZU gcOIXjbsKQW0bmXP3Nmhcb0EM+/K7hBdmHBNPZQUPK2Nr2uoIMot9bc21ydTDoyQnCEp Casw== X-Gm-Message-State: ACrzQf2OGpavbtoRpTp5/vjaZej5V1xG4TObMFKsvrHQRIgydxFpHB20 xuqYWP7KpbX6Sh/HWYayJjCeitSiq1nhLpqWf2tz/TiHH/w= X-Google-Smtp-Source: AMsMyM4fRGD44NueWro+p7KiZemP+8NH3P20sov85G2updKQjcAAwex0HPEZmQsdHVWDcQZ6WoVIhJmfg8x7D23ARm4= X-Received: by 2002:a17:902:7294:b0:178:a2be:ac18 with SMTP id d20-20020a170902729400b00178a2beac18mr28297098pll.93.1664904862866; Tue, 04 Oct 2022 10:34:22 -0700 (PDT) MIME-Version: 1.0 From: Alexander Binzberger Date: Tue, 4 Oct 2022 19:34:11 +0200 Message-ID: Subject: [BUG] bug in shift/add optimizer for avr targets To: gcc-bugs@gcc.gnu.org Content-Type: multipart/alternative; boundary="000000000000efa21305ea38ded9" X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --000000000000efa21305ea38ded9 Content-Type: text/plain; charset="UTF-8" hi, I realized gcc does not use optimized const shifts well but instead does replace some (3-7) left shifts on 32bit variables with add and adc operations. If the shift is not optimized and just unrolled they should be the same cycle count but for some reason it also adds some mov operations in the later part making it even worse performance wise. In terms of bytecode size shifts are better than add and adc operations. Also all const shifts can get optimized even better as one can see for the other variable sizes e.g. 16bit. Also I should mention this only happens with left shifts on 32bit (maybe also on 24bit) and with some non Os optimizer option. I sent const case optimisation to the patch mailing list but was not able to figure out where this bad optimisation is coming from. I prepared a compiler explorer example for you to get a easy grasp on it: https://godbolt.org/z/x75EM94rE In case compiler explorer does not work out for you the example code is: unsigned long lshift32_c(const unsigned long value) { return value << 7; } resulting in a lot of in case of O0 showing the wrong replacement by optimizer: add r24,r24 adc r25,r25 adc r26,r26 adc r27,r27 resulting in a lot of singe not optimized shifts and some additional useless mov near the end with O2: lsl r24 rol r25 rol r26 rol r27 ... movw r18,r24 movw r20,r26 lsl r18 rol r19 rol r20 rol r21 --000000000000efa21305ea38ded9--