[llvm-dev] DICompositeType to Type

Nicolai Hähnle via llvm-dev
Fri, 30 Oct 2020 15:39:21 -0000

Hi Henri,

On Fri, Oct 30, 2020 at 9:51 AM Henri Rosten via llvm-dev
<llvm-dev at lists.llvm.org> wrote:
>
> On Thu, Oct 29, 2020 at 12:06:15PM -0700, David Blaikie wrote:
> > On Thu, Oct 29, 2020 at 6:39 AM Henri Rosten via llvm-dev <
> > llvm-dev at lists.llvm.org> wrote:
> >
> > > Given a DICompositeType, is there a convenient way to map the
> > > DICompositeType to llvm::Type?
> > >
> >
> > No there isn't - that's not usually a thing that folks need to
do. What use
> > case do you have in mind?
> >
>
> I need to know what is the actual type of an object when it is
> referenced through a global base pointer variable.
>
> For instance, in the example from the previous mail, when
> baseptr->base_pure_virtual() is called, I would need to know that the
> global variable baseptr was actually initialized using class.Child*.
>
> I'm currently iterating all GlobalVariables to find out what values
they
> were initialized with. Something like:
>
> ```
> for (Module::global_iterator gi = M->global_begin(); gi !=
M->global_end(); ++gi) {
>   GlobalVariable *GV = &*gi;
>   if (!GV->hasInitializer()) {
>     continue;
>   }
>   Constant *Ini = GV->getInitializer();
>   Type *FromTy = Ini->stripPointerCasts()->getType();
>   Type *ToTy = Ini->getType();
>   if (ToTy->isPointerTy()) {
>     ToTy = ToTy->getPointerElementType();
>   }
>   if (FromTy->isPointerTy()) {
>     FromTy = FromTy->getPointerElementType();

LLVM pointers should be treated as untyped. The goal is to get rid of
getPointerElementType() eventually, so adding new uses should be
avoided.

I don't have an answer to your original question. It may be that
trying to do whatever it is you're trying to do purely in LLVM IR is
not at the right level of abstraction because the information you care
about has already been discarded. It would be helpful to have more
context about what you're ultimately trying to achieve.

Cheers,
Nicolai


>   }
> }
> ```
>
> In the example case from the previous mail for the baseptr, ToTy would
> be (%class.Base) and FromTy would be { i8** }. I'm trying to figure out
> a way to deduce that the actual type the baseptr is initialized with is
> a pointer to (%class.Child).
>
> >
> > >
> > > As an example:
> > >
> > > ```
> > > class Base {
> > > public:
> > >     virtual void base_pure_virtual() = 0;
> > > };
> > >
> > > class Child : public Base {
> > > public:
> > >     void base_pure_virtual() {
> > >         std::cout << __PRETTY_FUNCTION__ <<
"\n";
> > >     }
> > > };
> > >
> > > Child child;
> > > Base *baseptr = &child;
> > > ```
> > >
> > > Relevant IR snippets:
> > >
> > > ```
> > > @child = dso_local global { i8** } { i8** getelementptr inbounds
({ [3 x
> > > i8*] }, { [3 x i8*] }* @_ZTV5Child, i32 0, inrange i32 0, i32 2)
}, align
> > > 8, !dbg !19
> > > @baseptr = dso_local global %class.Base* bitcast ({ i8** }*
@child to
> > > %class.Base*), align 8, !dbg !25
> > > ...
> > > !914 = distinct !DICompositeType(tag: DW_TAG_class_type, name:
"Child",
> > > file: !22, line: 8, size: 64, flags: DIFlagTypePassByReference |
> > > DIFlagNonTrivial, elements: !915, vtableHolder: !28, identifier:
> > > "_ZTS5Child")
> > > ```
> > >
> > > I would like to know the Type of the value assigned to baseptr.
> > >
> > > I figured global variable has a link to DIGlobalVariable, which
has a
> > > link to DICompositeType. However, it seems there is no way to get
to the
> > > llvm::Type from DICompositeType. Is this the case or did I
overlook
> > > something?
> > >
> > > Alternatively, if there's a simpler way to get the Type of
the value
> > > assigned to baseptr (class.Child*), I would welcome your
suggestion.
> > >
> >
> > if you have the baseptr llvm::Value* you shuold be able to ask that
> > llvm::Value for its type directly
> >
https://llvm.org/doxygen/classllvm_1_1Value.html#a6393a2d4fe7e10b28a0dcc35f881567b
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



--
Lerne, wie die Welt wirklich ist,
aber vergiss niemals, wie sie sein sollte.