Optimization breaks inline asm code w/ptrs
Liu Hao
lh_mouse@126.com
Tue Aug 15 10:26:00 GMT 2017
On 2017/8/14 20:41, Alan Modra wrote:
> On Sun, Aug 13, 2017 at 10:25:14PM +0930, Alan Modra wrote:
>> On Sun, Aug 13, 2017 at 03:35:15AM -0700, David Wohlferd wrote:
>>> Using "m"(*pStr) as an (unused) input parameter has no effect.
>>
>> Use "m" (*(const void *)pStr) and ignore the warning, or use
>> "m" (*(const struct {char a; char x[];} *) pStr).
>
> or even better "m" (*(const char (*)[]) pStr).
>
This should work in the sense that GCC now thinks bytes adjacent to
`pStr` are subject to modification by the asm statement.
But I just tried GCC 7.2 and it seems that even if such a "+m"
constraint is the only output parameter of an asm statement and there is
no `volatile` or the "memory" clobber, GCC optimizer will not optimize
the asm statement away, which is the case if a plain `"+m"(*pStr)` is used.
>> The issue is one of letting gcc know what memory is accessed by the
>> asm, if you don't want to use a "memory" clobber. And there are very
>> good reasons to avoid clobbering all memory.
>>
>> "m"(*pStr) ought to work IMO, but apparently just tells gcc you are
>> only interested in the first character. Of course that is exactly
>> what *pStr is, but in this context it would be nicer if it meant the
>> entire array.
>
> I take that back. The relatively simple cast to differentiate a
> pointer to a char from a pointer to an indeterminate length char array
> makes it quite unnecessary for "m"(*pStr) to be treated as as array
> reference.
>
> I've opened https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81845 to
> track the lack of documentation.
>
Yes. I hope there will be a memory-range constraint in the future.
--
Best regards,
LH_Mouse
More information about the Gcc
mailing list