<div dir="rtl"><div dir="ltr">> <span style="font-size:12.8000001907349px">Which doesn't matter at all because a StringRef is just a pointer + size and the size can be determined statically</span></div><div dir="ltr"><span style="font-size:12.8000001907349px">>for GCSafepointPollName. The StringRef construction is folded away completely at clang -O3.</span></div><div dir="ltr"><span style="font-size:12.8000001907349px"><br></span></div><div dir="ltr"><span style="font-size:12.8000001907349px">A </span><span style="font-size:12.8000001907349px">global variable</span><span style="font-size:12.8000001907349px"> StringRef(const char *) does not get optimized the same way?</span></div><div dir="ltr"><br></div><div class="gmail_extra"><br><div class="gmail_quote"><div dir="ltr">2015-06-07 22:44 GMT+03:00 Benjamin Kramer <span dir="ltr"><<a href="mailto:benny.kra@gmail.com" target="_blank">benny.kra@gmail.com</a>></span>:</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><br>
> On 07.06.2015, at 21:33, Yaron Keren <<a href="mailto:yaron.keren@gmail.com" target="_blank">yaron.keren@gmail.com</a>> wrote:<br>
><br>
> If StringRef (const char *) is optimized away than StringRef GCSafepointPollName is better.<br>
<br>
</span>This creates the global in .bss and writes to it in a global constructor at clang -O3. This also means that LLVM cannot constant fold accesses to the global variable.<br>
<span><br>
> If StringRef (const char *) is not optimized away then<br>
><br>
> static bool isGCSafepointPoll(Function &F) {<br>
>   return F.getName().equals(GCSafepointPollName);<br>
> }<br>
><br>
> will construct a StringRef on every call to isGCSafepointPoll instead of once globally.<br>
<br>
</span>Which doesn't matter at all because a StringRef is just a pointer + size and the size can be determined statically for GCSafepointPollName. The StringRef construction is folded away completely at clang -O3.<br>
<br>
- Ben<br>
<div><div><br>
><br>
><br>
> 2015-06-07 22:13 GMT+03:00 Benjamin Kramer <<a href="mailto:benny.kra@gmail.com" target="_blank">benny.kra@gmail.com</a>>:<br>
><br>
> > On 07.06.2015, at 20:24, Yaron Keren <<a href="mailto:yaron.keren@gmail.com" target="_blank">yaron.keren@gmail.com</a>> wrote:<br>
> ><br>
> > I don't think any of this would actually matter since all would be optimized away, but if anything, it's probably best to match GCSafepointPollName two code users which expect a StringRef by making this a static StringRef.<br>
> ><br>
> > In general, unless a null-terminated C string is actually required (for example, for OS API) StringRefs are always superior  to const char *. Just think about the easy mistake S == "some string".<br>
><br>
> I don't know of a compiler that can optimize away global std::strings, it is very hard and probably not feasible without a domain-specific C++ optimizer. Clang for example emits a global constructor and destructor for this example which is badness.<br>
><br>
> I agree that StringRef is superior but not in a global variable. LLVM's globalopt can optimize them away if you get lucky, but I don't want to rely on that. The only acceptable way for complex types in global constants is constexpr, but making StringRef's ctor constexpr isn't possible because of the strlen call in there.<br>
><br>
> We also have warnings for common char* misuse in clang.<br>
><br>
> - Ben<br>
><br>
> ><br>
> ><br>
> > 2015-06-07 19:36 GMT+03:00 Benjamin Kramer <<a href="mailto:benny.kra@googlemail.com" target="_blank">benny.kra@googlemail.com</a>>:<br>
> > Author: d0k<br>
> > Date: Sun Jun  7 11:36:28 2015<br>
> > New Revision: 239254<br>
> ><br>
> > URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject-3Frev-3D239254-26view-3Drev&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=jrLukn1Cal7nOhfB6DlOEVlCbPigFg7i57PrQhHGSWQ&s=VrxVVF8iPwRvPPg4DMnwbNgi2eKUHww8zW5svFAEYEc&e=" target="_blank">http://llvm.org/viewvc/llvm-project?rev=239254&view=rev</a><br>
> > Log:<br>
> > Remove global std::string. NFC.<br>
> ><br>
> > Modified:<br>
> >     llvm/trunk/lib/Transforms/Scalar/PlaceSafepoints.cpp<br>
> ><br>
> > Modified: llvm/trunk/lib/Transforms/Scalar/PlaceSafepoints.cpp<br>
> > URL: <a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__llvm.org_viewvc_llvm-2Dproject_llvm_trunk_lib_Transforms_Scalar_PlaceSafepoints.cpp-3Frev-3D239254-26r1-3D239253-26r2-3D239254-26view-3Ddiff&d=AwMFaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=mQ4LZ2PUj9hpadE3cDHZnIdEwhEBrbAstXeMaFoB9tg&m=jrLukn1Cal7nOhfB6DlOEVlCbPigFg7i57PrQhHGSWQ&s=cZYt1a4P3cuKjELiy1tCgTUdMhVBhsMP9U8fdNj5VFU&e=" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/PlaceSafepoints.cpp?rev=239254&r1=239253&r2=239254&view=diff</a><br>
> > ==============================================================================<br>
> > --- llvm/trunk/lib/Transforms/Scalar/PlaceSafepoints.cpp (original)<br>
> > +++ llvm/trunk/lib/Transforms/Scalar/PlaceSafepoints.cpp Sun Jun  7 11:36:28 2015<br>
> > @@ -496,7 +496,7 @@ template <typename T> static void unique<br>
> >    }<br>
> >  }<br>
> ><br>
> > -static std::string GCSafepointPollName("gc.safepoint_poll");<br>
> > +static const char *const GCSafepointPollName = "gc.safepoint_poll";<br>
> ><br>
> >  static bool isGCSafepointPoll(Function &F) {<br>
> >    return F.getName().equals(GCSafepointPollName);<br>
> ><br>
> ><br>
> > _______________________________________________<br>
> > llvm-commits mailing list<br>
> > <a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
> > <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
> ><br>
><br>
><br>
<br>
</div></div></blockquote></div><br></div></div>