From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ciao.gmane.io (ciao.gmane.io [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id C452C385332C for ; Fri, 5 Jan 2024 16:31:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C452C385332C Authentication-Results: sourceware.org; dmarc=fail (p=quarantine dis=none) header.from=westcontrol.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=m.gmane-mx.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C452C385332C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=116.202.254.214 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704472262; cv=none; b=Pn3iiw9CDP4Td4KgM2yLDoo0i88LlErT8ho9OxUtbDOh0H/dpAm2TbGbu/+u5olqVSJ6X6hFn2WZDIdytSs1auylg9KMJVmFFJ08cvEt6tAuKHn3jVYA39LueVLPTWdHVOXOcZeFLuF4pvhqRWBmf8ij4zIwcv+Z1oR69MjDCNo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704472262; c=relaxed/simple; bh=MpEBFHgjs8Rmr8iPtSOUljinzpekh/5QRyfN6ME8/L8=; h=To:From:Subject:Date:Message-ID:Mime-Version; b=cyeVD7plrp2UdJlOcEp20WnUG9aqTala9djjLxL+Ry6HDmmcyhASVaYBJMQxeb3wAc2OY/OyOChFLywJrJEmcXnvR8PL7kzScFIG/HkKc5OW+hiaflVSR1MDGG6hJGThVD83FDjwGK/U9a4gZfVVoNnjwbog7zWAP/JYFIUIyLI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1rLn62-0005pb-VW for gcc-help@gcc.gnu.org; Fri, 05 Jan 2024 17:30:59 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: gcc-help@gcc.gnu.org From: David Brown Subject: Odd error with the "X" inline assembly constraint Date: Fri, 5 Jan 2024 17:30:51 +0100 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Content-Language: en-GB X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: I was testing a little by looking at some code generation for optimisations and re-arrangements when -ffast-math is enabled, using code like this in : typedef float T; T test(T a, T b) { T x = a + b; //asm ("" :: "X" (x)); asm ("" : "+X" (x)); //asm ("" : "+X" (x) : "X" (x)); return x - b; } Without the asm statements, gcc - as expected - skips the calculation of "x" and can then simplify "a + b - b" to "a". I have previously used inline assembly of the form: asm ("" : "+g" (x)); to tell gcc "You need to calculate x before running this assembly and put it in a general register or memory, but it might change during the assembly so you must forget anything you knew about it before". I've found it useful to force particular orders on calculations, or for debugging, or as a kind of fine-tuned alternative to a memory barrier. But the "+g" operand is not ideal for floating point variables - it forces the compiler to move the variable from a floating point register into a general-purpose register, then back again. The ideal choice seems to be "+X", since "X" matches any operand whatsoever. However, when I use just "asm ("" : "+X" (x));", I get an error message "error: inconsistent operand constraints in an 'asm'". I have no idea why this is an issue. Getting weirder, on x86-64, there is no error if I use asm ("" : "+X" (x) : "X" (x)); This gives me the desired effect of forcing "x" to be calculated and used in the final "x - b". Even weirder, on 32-bit ARM, this still gives the inconsistent operand error. Weirder still, this works error-free on both targets : asm ("" :: "X" (x)); asm ("" : "+X" (x)); In my (non-exhaustive) testing, this gives optimal results on both targets, independent of the compiler version and type T. I'd imagine that the "X" operand doesn't see much use in real inline assembly - on x86 and ARM the assembly instruction template would usually depend on where the data is put. But if anyone can explain this behaviour to me, I am very curious to know what is going on. David