<div dir="ltr">Yep, +1 to that - if you're trying to write an optimization, debug info isn't the place to be looking (& type information for pointers isn't the place to be looking either). If you'd like to describe more of what your goals are, perhaps we can point you in the right direction.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 30, 2020 at 8:39 AM Nicolai Hähnle <<a href="mailto:nhaehnle@gmail.com">nhaehnle@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Henri,<br>
<br>
On Fri, Oct 30, 2020 at 9:51 AM Henri Rosten via llvm-dev<br>
<<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
><br>
> On Thu, Oct 29, 2020 at 12:06:15PM -0700, David Blaikie wrote:<br>
> > On Thu, Oct 29, 2020 at 6:39 AM Henri Rosten via llvm-dev <<br>
> > <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
> ><br>
> > > Given a DICompositeType, is there a convenient way to map the<br>
> > > DICompositeType to llvm::Type?<br>
> > ><br>
> ><br>
> > No there isn't - that's not usually a thing that folks need to do. What use<br>
> > case do you have in mind?<br>
> ><br>
><br>
> I need to know what is the actual type of an object when it is<br>
> referenced through a global base pointer variable.<br>
><br>
> For instance, in the example from the previous mail, when<br>
> baseptr->base_pure_virtual() is called, I would need to know that the<br>
> global variable baseptr was actually initialized using class.Child*.<br>
><br>
> I'm currently iterating all GlobalVariables to find out what values they<br>
> were initialized with. Something like:<br>
><br>
> ```<br>
> for (Module::global_iterator gi = M->global_begin(); gi != M->global_end(); ++gi) {<br>
> GlobalVariable *GV = &*gi;<br>
> if (!GV->hasInitializer()) {<br>
> continue;<br>
> }<br>
> Constant *Ini = GV->getInitializer();<br>
> Type *FromTy = Ini->stripPointerCasts()->getType();<br>
> Type *ToTy = Ini->getType();<br>
> if (ToTy->isPointerTy()) {<br>
> ToTy = ToTy->getPointerElementType();<br>
> }<br>
> if (FromTy->isPointerTy()) {<br>
> FromTy = FromTy->getPointerElementType();<br>
<br>
LLVM pointers should be treated as untyped. The goal is to get rid of<br>
getPointerElementType() eventually, so adding new uses should be<br>
avoided.<br>
<br>
I don't have an answer to your original question. It may be that<br>
trying to do whatever it is you're trying to do purely in LLVM IR is<br>
not at the right level of abstraction because the information you care<br>
about has already been discarded. It would be helpful to have more<br>
context about what you're ultimately trying to achieve.<br>
<br>
Cheers,<br>
Nicolai<br>
<br>
<br>
> }<br>
> }<br>
> ```<br>
><br>
> In the example case from the previous mail for the baseptr, ToTy would<br>
> be (%class.Base) and FromTy would be { i8** }. I'm trying to figure out<br>
> a way to deduce that the actual type the baseptr is initialized with is<br>
> a pointer to (%class.Child).<br>
><br>
> ><br>
> > ><br>
> > > As an example:<br>
> > ><br>
> > > ```<br>
> > > class Base {<br>
> > > public:<br>
> > > virtual void base_pure_virtual() = 0;<br>
> > > };<br>
> > ><br>
> > > class Child : public Base {<br>
> > > public:<br>
> > > void base_pure_virtual() {<br>
> > > std::cout << __PRETTY_FUNCTION__ << "\n";<br>
> > > }<br>
> > > };<br>
> > ><br>
> > > Child child;<br>
> > > Base *baseptr = &child;<br>
> > > ```<br>
> > ><br>
> > > Relevant IR snippets:<br>
> > ><br>
> > > ```<br>
> > > @child = dso_local global { i8** } { i8** getelementptr inbounds ({ [3 x<br>
> > > i8*] }, { [3 x i8*] }* @_ZTV5Child, i32 0, inrange i32 0, i32 2) }, align<br>
> > > 8, !dbg !19<br>
> > > @baseptr = dso_local global %class.Base* bitcast ({ i8** }* @child to<br>
> > > %class.Base*), align 8, !dbg !25<br>
> > > ...<br>
> > > !914 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "Child",<br>
> > > file: !22, line: 8, size: 64, flags: DIFlagTypePassByReference |<br>
> > > DIFlagNonTrivial, elements: !915, vtableHolder: !28, identifier:<br>
> > > "_ZTS5Child")<br>
> > > ```<br>
> > ><br>
> > > I would like to know the Type of the value assigned to baseptr.<br>
> > ><br>
> > > I figured global variable has a link to DIGlobalVariable, which has a<br>
> > > link to DICompositeType. However, it seems there is no way to get to the<br>
> > > llvm::Type from DICompositeType. Is this the case or did I overlook<br>
> > > something?<br>
> > ><br>
> > > Alternatively, if there's a simpler way to get the Type of the value<br>
> > > assigned to baseptr (class.Child*), I would welcome your suggestion.<br>
> > ><br>
> ><br>
> > if you have the baseptr llvm::Value* you shuold be able to ask that<br>
> > llvm::Value for its type directly<br>
> > <a href="https://llvm.org/doxygen/classllvm_1_1Value.html#a6393a2d4fe7e10b28a0dcc35f881567b" rel="noreferrer" target="_blank">https://llvm.org/doxygen/classllvm_1_1Value.html#a6393a2d4fe7e10b28a0dcc35f881567b</a><br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
> <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br>
<br>
<br>
-- <br>
Lerne, wie die Welt wirklich ist,<br>
aber vergiss niemals, wie sie sein sollte.<br>
</blockquote></div>