[llvm-dev] How to know the sub-class of a Value class?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

[llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev

As a concrete question, I understand 'Value' class is the parent of many concrete sub-classes. Let's say I retrieve a Value* value = store_inst->getValueOperand(). Unless I know what the sub-type is, how can I further use this object? I tried something like this:

=================================================

         Value* value = store_inst->getValueOperand()
         errs() << value->getValueID; // Which ID corresponds to which sub-class?
         errs() << value->getValueName(); // Prints numeric memory addresses
         errs() << *value->getValueName(); // Doesn't compile       
         
=================================================

I have mentioned the issues in each of the cases above. I am not sure if the approach is correct or not. Please help me out.

--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
Try value->dump() assuming you're using a debug build.

~Craig

On Sun, Jun 11, 2017 at 10:54 PM, Dipanjan Das via llvm-dev <[hidden email]> wrote:

As a concrete question, I understand 'Value' class is the parent of many concrete sub-classes. Let's say I retrieve a Value* value = store_inst->getValueOperand(). Unless I know what the sub-type is, how can I further use this object? I tried something like this:

=================================================

         Value* value = store_inst->getValueOperand()
         errs() << value->getValueID; // Which ID corresponds to which sub-class?
         errs() << value->getValueName(); // Prints numeric memory addresses
         errs() << *value->getValueName(); // Doesn't compile       
         
=================================================

I have mentioned the issues in each of the cases above. I am not sure if the approach is correct or not. Please help me out.

--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
In reply to this post by Gerolf Hoflehner via llvm-dev

On Mon, Jun 12, 2017 at 3:24 PM, Dipanjan Das via llvm-dev <[hidden email]> wrote:

As a concrete question, I understand 'Value' class is the parent of many concrete sub-classes. Let's say I retrieve a Value* value = store_inst->getValueOperand(). Unless I know what the sub-type is, how can I further use this object? I tried something like this:

=================================================

         Value* value = store_inst->getValueOperand()
         errs() << value->getValueID; // Which ID corresponds to which sub-class?
         errs() << value->getValueName(); // Prints numeric memory addresses
         errs() << *value->getValueName(); // Doesn't compile       
         
=================================================

I have mentioned the issues in each of the cases above. I am not sure if the approach is correct or not. Please help me out.

--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
In reply to this post by Gerolf Hoflehner via llvm-dev


On 11 June 2017 at 23:03, Craig Topper <[hidden email]> wrote:
Try value->dump() assuming you're using a debug build.


The information displayed is non-uniform. There are two different cases based on my sample program:

         (1) dump() shows the Type and *some* value which I don't understand what it is
         (2) dump() shows the instruction that creates the Value*

But, it doesn't display the concrete sub-class.

 
~Craig

On Sun, Jun 11, 2017 at 10:54 PM, Dipanjan Das via llvm-dev <[hidden email]> wrote:

As a concrete question, I understand 'Value' class is the parent of many concrete sub-classes. Let's say I retrieve a Value* value = store_inst->getValueOperand(). Unless I know what the sub-type is, how can I further use this object? I tried something like this:

=================================================

         Value* value = store_inst->getValueOperand()
         errs() << value->getValueID; // Which ID corresponds to which sub-class?
         errs() << value->getValueName(); // Prints numeric memory addresses
         errs() << *value->getValueName(); // Doesn't compile       
         
=================================================

I have mentioned the issues in each of the cases above. I am not sure if the approach is correct or not. Please help me out.

--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev





--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
In reply to this post by Gerolf Hoflehner via llvm-dev


On 11 June 2017 at 23:06, Jeremy Lakeman <[hidden email]> wrote:

I understand isa and dyn-cast let you test the type of an object at run-time by leveraging LLVM's custom implementation of RTTI. However, it doesn't make much sense to test out for all possible sub-classes to get to know what it actually is. If I just want to know sub-class a Value* is, what's the way out (other than testing with isa<>())?

 


On Mon, Jun 12, 2017 at 3:24 PM, Dipanjan Das via llvm-dev <[hidden email]> wrote:

As a concrete question, I understand 'Value' class is the parent of many concrete sub-classes. Let's say I retrieve a Value* value = store_inst->getValueOperand(). Unless I know what the sub-type is, how can I further use this object? I tried something like this:

=================================================

         Value* value = store_inst->getValueOperand()
         errs() << value->getValueID; // Which ID corresponds to which sub-class?
         errs() << value->getValueName(); // Prints numeric memory addresses
         errs() << *value->getValueName(); // Doesn't compile       
         
=================================================

I have mentioned the issues in each of the cases above. I am not sure if the approach is correct or not. Please help me out.

--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev





--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
On Sun, Jun 11, 2017 at 11:12 PM, Dipanjan Das via llvm-dev
<[hidden email]> wrote:

>
>
> On 11 June 2017 at 23:06, Jeremy Lakeman <[hidden email]> wrote:
>>
>>
>> http://llvm.org/docs/ProgrammersManual.html#the-isa-cast-and-dyn-cast-templates
>
>
> I understand isa and dyn-cast let you test the type of an object at run-time
> by leveraging LLVM's custom implementation of RTTI. However, it doesn't make
> much sense to test out for all possible sub-classes to get to know what it
> actually is. If I just want to know sub-class a Value* is, what's the way
> out (other than testing with isa<>())?

Does this pattern (or some variant of it) solve your problem:
https://github.com/llvm-mirror/llvm/blob/master/lib/Analysis/BasicAliasAnalysis.cpp#L232

?

-- Sanjoy
_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
In reply to this post by Gerolf Hoflehner via llvm-dev
I'm certainly no expert, but how you use that Value* would depend on what you want to do with it.

From the documentation, it looks like Value->getValueID() is how the RTTI implementation determines the subclass.
So you could switch over the values returned (defined in llvm/IR/Value.def).

Or dyn_cast to each of the types you need to deal with. Or use some other helper functions that strip noops like bitcasts for you.

On Mon, Jun 12, 2017 at 3:42 PM, Dipanjan Das via llvm-dev <[hidden email]> wrote:


On 11 June 2017 at 23:06, Jeremy Lakeman <[hidden email]> wrote:

I understand isa and dyn-cast let you test the type of an object at run-time by leveraging LLVM's custom implementation of RTTI. However, it doesn't make much sense to test out for all possible sub-classes to get to know what it actually is. If I just want to know sub-class a Value* is, what's the way out (other than testing with isa<>())?

 


On Mon, Jun 12, 2017 at 3:24 PM, Dipanjan Das via llvm-dev <[hidden email]> wrote:

As a concrete question, I understand 'Value' class is the parent of many concrete sub-classes. Let's say I retrieve a Value* value = store_inst->getValueOperand(). Unless I know what the sub-type is, how can I further use this object? I tried something like this:

=================================================

         Value* value = store_inst->getValueOperand()
         errs() << value->getValueID; // Which ID corresponds to which sub-class?
         errs() << value->getValueName(); // Prints numeric memory addresses
         errs() << *value->getValueName(); // Doesn't compile       
         
=================================================

I have mentioned the issues in each of the cases above. I am not sure if the approach is correct or not. Please help me out.

--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev





--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev



_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
In reply to this post by Gerolf Hoflehner via llvm-dev


On 11 June 2017 at 23:20, Sanjoy Das <[hidden email]> wrote:
 
Does this pattern (or some variant of it) solve your problem:
https://github.com/llvm-mirror/llvm/blob/master/lib/Analysis/BasicAliasAnalysis.cpp#L232

It switches over BinaryOperator->getOpcode(). I don't think it addresses my query.
 
--

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
In reply to this post by Gerolf Hoflehner via llvm-dev


On 11 June 2017 at 23:25, Jeremy Lakeman <[hidden email]> wrote:
I'm certainly no expert, but how you use that Value* would depend on what you want to do with it.

From the documentation, it looks like Value->getValueID() is how the RTTI implementation determines the subclass.
So you could switch over the values returned (defined in llvm/IR/Value.def).

In my <llvm-root>/include/llvm/IR/Value.def, there's no mapping between the numeric ID (as returned by getValueID()) and corresponding sub-class.
 

Thanks & Regards,

Dipanjan

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
In reply to this post by Gerolf Hoflehner via llvm-dev
On 6/12/17 1:09 AM, Dipanjan Das via llvm-dev wrote:


On 11 June 2017 at 23:03, Craig Topper <[hidden email]> wrote:
Try value->dump() assuming you're using a debug build.


The information displayed is non-uniform. There are two different cases based on my sample program:

         (1) dump() shows the Type and *some* value which I don't understand what it is
         (2) dump() shows the instruction that creates the Value*

But, it doesn't display the concrete sub-class.

The LLVM class names are largely one-to-one with the instructions defined in the LLVM Language Reference. The main exception is the class of operators that get bundled into the BinaryOperator class (e.g., add, or, xor, fadd, etc.), and BinaryOperator is one of those classes that Doxygen completely chokes on since it's so macro-heavy. Browsing the subclass list of llvm::Instruction on Doxygen for something that looks like the name of the instruction in question.

The other main set of values are the constants. In the dump output, constant scalars (such as ConstantFP, ConstantInt, ConstantPointerNull) show up as <type> <val>, e.g., i64 0 or double 0.0. The ConstantAggregateZero class shows up as zeroinitializer, and ConstantExprs show up as something like trunc (<expr> to <ty>). Again, you can read the language manual to see examples, it's usually pretty clear what type something is.

If you're dead set on actually printing the name of the class of a Value, there's no canned method to do that. Something like this code would work (untested):
const char *kind_name = nullptr;
switch(val->getValueID()) {
#define HANDLE_VALUE(name) case Value::name##Val: kind_name = #name; break;
default: kind_name = "well this was unexpected";
}
-- 
Joshua Cranmer
Thunderbird and DXR developer
Source code archæologist

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
On 6/12/17 1:34 AM, Joshua Cranmer 🐧 wrote:
> If you're dead set on actually printing the name of the class of a
> Value, there's no canned method to do that. Something like this code
> would work (untested):
> const char *kind_name = nullptr;
> switch(val->getValueID()) {
> #define HANDLE_VALUE(name) case Value::name##Val: kind_name = #name;
> break;
> default: kind_name = "well this was unexpected";
> }

Arggh, I'm an idiot. The correct code should be:
const char *kind_name = nullptr;
switch(val->getValueID()) {
#define HANDLE_VALUE(name) case Value::name##Val: kind_name = #name; break;
#include "llvm/IR/Value.def"
#undef HANDLE_VALUE
default: kind_name = "well this was unexpected";
}

The #include is kinda the most important part in that statement...

--
Joshua Cranmer
Thunderbird module owner
DXR coauthor

_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Reply | Threaded
Open this post in threaded view
|

Re: [llvm-dev] How to know the sub-class of a Value class?

Gerolf Hoflehner via llvm-dev
Like I said, how you determine what you're looking at might change based on what you want to do with each subclass of Value, which still hasn't been clarified.

You get better answers if you ask more specific questions and provided more context. And lately it seems like you've been asking a lot of questions.

Why do you need to know the specific type of a Value * ?
Or, implicit in your question, what types of stored values are you looking for?
Do you want to look through bitcasts to discover the source Value?
Is this an attempt to get a different answer to your "[llvm-dev] Force casting a Value*" question?


On Mon, Jun 12, 2017 at 4:05 PM, Joshua Cranmer via llvm-dev <[hidden email]> wrote:
On 6/12/17 1:34 AM, Joshua Cranmer 🐧 wrote:
If you're dead set on actually printing the name of the class of a Value, there's no canned method to do that. Something like this code would work (untested):
const char *kind_name = nullptr;
switch(val->getValueID()) {
#define HANDLE_VALUE(name) case Value::name##Val: kind_name = #name; break;
default: kind_name = "well this was unexpected";
}

Arggh, I'm an idiot. The correct code should be:
const char *kind_name = nullptr;
switch(val->getValueID()) {
#define HANDLE_VALUE(name) case Value::name##Val: kind_name = #name; break;
#include "llvm/IR/Value.def"
#undef HANDLE_VALUE
default: kind_name = "well this was unexpected";
}

The #include is kinda the most important part in that statement...

--
Joshua Cranmer
Thunderbird module owner
DXR coauthor


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


_______________________________________________
LLVM Developers mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev