Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp5368833ybe; Tue, 17 Sep 2019 07:05:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqz2Ti0MGYg4tzhWeiDoQlPtlBAoO0aUkZrYsTmO1SWIfg1SnrwUad0tbHFKFjrHUITfQc1H X-Received: by 2002:a50:ec94:: with SMTP id e20mr4788763edr.231.1568729105241; Tue, 17 Sep 2019 07:05:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568729105; cv=none; d=google.com; s=arc-20160816; b=Uu3dBOtWjnHLEKBb/x6yUF518nra+TmmS4H+rSqw1tLJffzVbpQNIfm2Dui2rk5klJ 59YBbHjhLz/fQauvozzwCi3ZTvgSSj6syTumM4tD854IOIyF7YLnqNxCJLjaZ7WnHBvk RJ/JafUlw7WvbLTFj3WLliRLX4qB//74Ri65rt+rOf8z4QufjfOAspEcm9s4uT11EMMJ v1q/agf1xEokrocL/AYHhQmwuzt4ve00IuH7MRBBvkPUVhjScYFwb4BkVlPF31P4HnL1 QfDlZJTi/mZhbCLD5XwTD0ob3NigcvE3sBMaZantBb3XJNGkxOaosA66BPffWr9K3lcR zaew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=VIY5WmWJiiJmv8DkjeO64uuun9nCjDaTAoH4yu6pwYc=; b=CpHMoQYgwCElkZTAxwtRmialm8Au7GCydQjl0Bn//KQlJokPOGz+cXomBx30R1GQEF aC+xUO6zhDiOhBGSd4pX93Aa7IFB6WnwgOJmGiO2J7fZPZHA6tYbOwNKCUpfoyWqouAP MWYxNs6PeGvW9vcj+nuxwuHqUuxpT4eYLrjKw46I7D/oT1HgbkW92sv6WWBBHKsc/apx LKhZk5ngEUn5qOOZ1AD40M0zCQQxl49yAslASFv78+kp3f2SsJ42HYE5x5tcr6crOSGZ EPw3Stt+f+d1Fw67j1wDQcYrdzqEHaPPaT4NHjSJVvTGnz9EJhFtOdQGes4fGsjzcXbw UgxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LUFE8WEN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 13si953433edw.357.2019.09.17.07.04.32; Tue, 17 Sep 2019 07:05:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LUFE8WEN; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732700AbfIPXOE (ORCPT + 99 others); Mon, 16 Sep 2019 19:14:04 -0400 Received: from mail.kernel.org ([198.145.29.99]:44518 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726649AbfIPXOE (ORCPT ); Mon, 16 Sep 2019 19:14:04 -0400 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 83A6D2171F for ; Mon, 16 Sep 2019 23:14:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1568675642; bh=RFV8tAN00Kz44FNU9hmqQHgEoIVUG1TxC7m3LsBAClQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=LUFE8WENCVCj7dkzHa+oPoSR3tQFO1vbc8SAtKMLlJD9DPlmGpjKycm5HMO8PcPmd cvbosdNVbWjMqfv/gEAHvXv4kpIOOvRGFwab1nNlFnP/VSQQPD9MF7APa3A1yzwlyl vMrPOBRjVnNiyU78NVieafQ9o+KkYoxNGXCu2eGQ= Received: by mail-wr1-f41.google.com with SMTP id i18so1066382wru.11 for ; Mon, 16 Sep 2019 16:14:02 -0700 (PDT) X-Gm-Message-State: APjAAAUDk79r/+05cyh3zYdaoadEkJUdBqFGJRzn+IKX8z8hmIID69cD i9OjrzZ1iCoKEZP5YicY5lMc8NmbFry5YpCQYpCPiQ== X-Received: by 2002:adf:fe0f:: with SMTP id n15mr559686wrr.343.1568675641000; Mon, 16 Sep 2019 16:14:01 -0700 (PDT) MIME-Version: 1.0 References: <20190913072237.GA12381@zn.tnic> <9dc9f1e6-5d19-167c-793d-2f4a5ebee097@rasmusvillemoes.dk> <20190913104232.GA4190@zn.tnic> <20190913163645.GC4190@zn.tnic> <3fc31917-9452-3a10-d11d-056bf2d8b97d@rasmusvillemoes.dk> In-Reply-To: From: Andy Lutomirski Date: Mon, 16 Sep 2019 16:13:48 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC] Improve memset To: Linus Torvalds Cc: Andy Lutomirski , Rasmus Villemoes , Borislav Petkov , Rasmus Villemoes , x86-ml , Josh Poimboeuf , lkml Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 16, 2019 at 2:30 PM Linus Torvalds wrote: > > On Mon, Sep 16, 2019 at 10:41 AM Andy Lutomirski wrote: > > > > After some experimentation, I think y'all are just doing it wrong. > > GCC is very clever about this as long as it's given the chance. This > > test, for example, generates excellent code: > > > > #include > > > > __THROW __nonnull ((1)) __attribute__((always_inline)) void > > *memset(void *s, int c, size_t n) > > { > > asm volatile ("nop"); > > return s; > > } > > > > /* generates 'nop' */ > > void zero(void *dest, size_t size) > > { > > __builtin_memset(dest, 0, size); > > } > > I think the point was that we'd like to get the default memset (for > when __builtin_memset() doesn't generate inline code) also inlined > into just "rep stosb", instead of that tail-call "jmp memset". Well, when I wrote this email, I *thought* it was inlining the 'memset' function, but maybe I just can't read gcc's output today. It seems like gcc is maybe smart enough to occasionally optimize memset just because it's called 'memset'. This generates good code: #include inline void *memset(void *dest, int c, size_t n) { /* Boris' implementation */ void *ret, *dummy; asm volatile("push %%rdi\n\t" "mov %%rax, %%rsi\n\t" "mov %%rcx, %%rdx\n\t" "andl $7,%%edx\n\t" "shrq $3,%%rcx\n\t" "movzbl %%sil,%%esi\n\t" "movabs $0x0101010101010101,%%rax\n\t" "imulq %%rsi,%%rax\n\t" "rep stosq\n\t" "movl %%edx,%%ecx\n\t" "rep stosb\n\t" "pop %%rax\n\t" : "=&D" (ret), "=c" (dummy) : "0" (dest), "a" (c), "c" (n) : "rsi", "rdx", "memory"); return ret; } int one_word(void) { int x; memset(&x, 0, sizeof(x)); return x; } So maybe Boris' patch is good after all.