inline asm and multi-alternative constraints
David Wohlferd
dw@LimeGreenSocks.com
Sat Oct 31 03:10:00 GMT 2015
On 10/29/2015 1:47 PM, Richard Henderson wrote:
> On 10/27/2015 02:05 PM, Jeff Law wrote:
>> On 10/25/2015 09:41 PM, David Wohlferd wrote:
>>> Does gcc's inline asm support multi-alternative constraints? Or are
>>> they only supported for md?
>>>
>>> dw
>>>
>>> PS If it *is* supported, then the docs need some work.
>> I think Richard corrected me last I spoke on this topic :-) They *are*
>> supported. ie, something like this should work on a ciscy target.
>>
>> asm("add %0,%1" : "=r,m"(x) : "rim,ri"(y))
> Correct.
>
> They are supported, so long as the assembly can use the same text for all
> alternatives. Thus multiple alternatives in inline asm is basically useless
> for RISC targets, but occasionally useful for CISC targets.
Aha. Thank you for the info.
Ok, I have discarded the other patch. I'm not sure what this means for
10396, since that seemed to be the solution there.
I have updated the non-md text with (most of) the changes I think it
needs (attached). These changes are pleasantly minor, mostly just
adding some example text and a bit of formatting.
However. Trying to actually use the information on this page is turning
up some problems.
First, it could use a bit more clarity in the text that describes how
gcc chooses among the alternatives. There are apparently 3 criteria
that affect this decision: # of statements needed to copy params, order
of alternatives, and flags. But it appears that they aren't all
weighted equally. For example no number of '?' seem to be able to
override an alternative that causes a reload (change the example below
to use eax instead of ebx). But before I try to re-phrase this
paragraph, I'm hoping someone can provide more details. What exactly
are the rules here?
Second, attempting to use ! and $ isn't working the way the docs led me
to expect (actually, I can't make them work at all). Starting with this
(contrived) i386 code, gcc selects the second alternative (using -O2 for
x64). This makes sense to me.
int main()
{
int x = 3;
int y = 17;
asm("or %1,%0" : "+r,b"(x) : "r,i"(y));
return x;
}
It took two '?' in front of the 'b' to convince gcc to use the first
alternative. This seems reasonable, and shows that the first
alternative is indeed viable.
The problem started when I tried to replace the '??' with '!'. Being
"severe," I figured one '!' should do the work of two '?'. But even
using multiple '!' doesn't cause it to switch to the first alternative.
Parsing the current docs:
"! - Disparage severely the alternative that the '!' appears in. This
alternative can still be used if it fits without reloading, but if
reloading is needed, some other alternative will be used. "
The alternative I am trying to disparage uses ebx. Using that register
causes a push/pop. That seems to me like "reloading is needed," so I
expected that by definition the other alternative would be used. Even
if reloading isn't a factor (or if I don't understand it correctly), the
alternative '!' is applied to should still be "severely" disparaged (ie
at least as much as 2 '?'). But apparently it's not.
Using '^' does change the selection if I use two of them. But according
to the docs, '^' *only* applies if "the operand with the '^' needs a
reload." Since the '^' is having an effect, doesn't that imply that
there is a reload associated with the second alternative? But if there
is, why didn't '!' work as expected?
'$' also doesn't affect the selection, probably for the same reasons as '!'.
I don't know if ! and $ are broken, or if the docs just aren't
explaining them well enough. But something isn't working here.
Lastly, while it is a core concept to compiler-writers, 'reloads' isn't
really a compiler-user concept. Perhaps some of these flags aren't user
manual appropriate (I'm looking at you !^$).
dw
PS
Current text: https://gcc.gnu.org/onlinedocs/gcc/Multi-Alternative.html
Proposed text: http://limegreensocks.com/gcc/Multi_002dAlternative.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ma2.patch
Type: text/x-patch
Size: 3988 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20151031/1afcc098/attachment.bin>
More information about the Gcc
mailing list