Use-def analysis on struct fields in llvm - llvm

I have a code which involves struct in C. I have converted this to LLVM IR. The struct field access are converted to GEP instructions with index based access.
Is it possible to do use-def analysis on such struct fields ? I want to remove struct field which is not being accessed anywhere in the module.
Thanks in advance.

Related

C++ LLVM class functionality

I'm playing around with LLVM, but now I got stuck at generating code for classes.
How would one create class functionality using LLVM?
Are class operators handled just like functions?
How is automated allocation handled, (like C++)?
How to support interfaces like Java does, through virtual inheritence like C++?
Long Version
General class behavior
A straightforward approach is to create structs, then model methods as regular functions that receive a pointer to a struct representing the containing class - in essence, a this pointer - as the first parameter. Allocation could be modeled by allocating the struct and then calling a special initializing function - the constructor, really - on the allocated data.
Inheritance could be done by building a struct which contains a special "parent" field (or fields, for multiple inheritance), that has a type identical to the type of the struct for the base class.
Polymorphism
Read about virtual tables; I think they're the best starting point. You could find that the compiler basically:
Creates a static table in memory, mapping from a function "name" to its implementation,
Adds a pointer to the class struct that points to such a table,
Whenever a virtual method is called, compiles it into an indirect call which dereferences the address from the appropriate virtual table entry.
Short Version
Write some code which uses classes in C++, then compile it to LLVM IR with Clang and look at the generated code.

C++: Extend an enum definition?

The Java code by Oracle tends to use constant integer identifiers where the equivalent would be an enum in C++. The nice thing about using the CIIs that you can easily add more in the base class or a derived class and not break the code (too badly...). My question is: Is there a way to achieve this using an enum in C++ or would I have to stick to constants?
Assuming that you are unable to change the original enum declaration and you absolutely need to do this it's possible using a static cast.
enum ENUM_TYPE {
VALUE1,
VALUE2,
VALUE3
};
static const ENUM_TYPE VALUE4 = static_cast<ENUM_TYPE>(VALUE3 + 1);
You can do that in Java because it's bytecodes are interpreted (or, more likely recently "Just-in-Time" compiled) : You have separate pieces and they are put together at run-time.
C++ is fully compiled to native code. Any change will require a ful compile. If a full recompile is acceptable, then adding a new item to a enum is possible, and even less likely to break code than adding CIIs.

Dynamic structures in C++

I am running a simulation in which I have objects of a class which use different models. These models are randomly selected for some objects of the class and specifically decided for some objects too. These objects communicate with each other for which I am using structures (aka struct) in C++ which has some
standard variables and
some additional variables which depends on models which the objects communicating with each other have.
So, how can I do this?
Thanks in advance.
You can hack around with:
the preprocessor;
template meta-programming;
inheritance/polymorphism.
Each gives a different way of producing a different user-defined type, based on different kinds of conditions.
Without knowing what you're trying to accomplish, this is the best I can do.
All instances of a structure or class have the same structure. Luckily, there are some tricks that can be used to 'simulate' what you try to do.
The first trick (which can also be used in C), is to use a union, e.g.:
struct MyStruct
{
int field1;
char field2;
int type;
union
{
int field3a;
char field3b;
double field3c;
} field3;
};
In a union, all members take up the same space in memory. As a programmer you have to be careful. You can only get out of the union what you put in. If you initialize one member of a union, but you read another member, you will probable get garbage (unless you want to do some low-level hacks, but don't do this unless you are very experienced).
Unions often come together with another field (outside the union) that indicates which member is actually used in the union. You could consider this your 'condition'.
A second trick is use the 'state' pattern (see http://en.wikipedia.org/wiki/State_pattern). From the outside world, the context class looks always the same, but internally, the different states can contain different kinds of information.
A somewhat simplified approach for state is to use simple inheritance, and to use dynamic casts. Depending on your 'condition', use a different subclass, and perform a dynamic cast to get the specific information.
E.g., suppose that we have a Country class. Some countries have a president, others have a king, others have an emperor. You could something like this:
class Country
{
...
};
class Republic : public Country
{
public:
const string &getPresident() const;
const string &getVicePresident() const;
};
class Monarchy : public Country
{
public:
const string &getKing() const;
const string &getQueen() const;
};
In your application you could work with pointers to Country, and do a dynamic cast to Republic or Monarchy where the president or king is needed.
This example can be easily transformed into one using the 'state' pattern, but I leave this as an exercise for you.
Personally, I would go for the state pattern. I'm not a big fan of dynamic casts and they always seem to be kind-of-hack for me.
If it's at compile-time, a simple #ifdef or template specialization will serve this purpose just fine. If it's at run-time and you need value semantics, you can use a boost::optional<my_struct_of_optional_members>, and if you're fine with reference semantics, inheritance will solve the problem at hand.
A union and that kind of dirty trick is not necessary.
There are several common approaches for "dynamic" attributes/properties in languages, and a few that tend to work well in C++.
For example, you can make a C++ class called "MyProperties" that has a sparse set of values, and your MyStructureClass would have its well-known members, plus a single MyProperties instance which may have zero-or-more values.
Similarly, languages like Python and Perl make extensive use of Associative Arrays/Dictionaries/Hashes to achieve this: The (string) key uniquely identifies the value. In C++, you can index your MyProperties class with a string or any type you want (after overloading the operator[]()), and the value can be a string, a MyVariant, or any other pointer-or-type that you want to inspect. The values are dynamically added to the parent container as they are assigned (e.g., the class "remembers" the last value it is given, uniquely identified by key).
Finally, in the "olden days", what you describe was commonly done for distributed application processing: You defined a C-struct with "well-known" (typed) fields/members, and the last field was a char* member. Then, that char* member would identify the start of a serialized stream of bytes that were also part of that struct (you merely serialized that array of chars when you marshalled the struct across systems). In the context of C++, you could similarly extract your values dynamically from that char* stream buffer on-access-demand (which logically should be "owned" by the class). This worked for marshalling across systems because the size of the struct was the size of everything (including the last char* member), but the "allocation" for that struct was much larger (e.g., the size of the struct itself, which was logically a "header", plus a certain number of bytes after that header, which represented the "payload" and which was indexed by the last member, the char* member.) Thus, it was a contiguous-block-of-memory struct, with dynamic size. (This would also work in C++ as long as you passed-by-reference, and never by value.)
embed an union into your structure, and use a flag to tell which part of the union is valid.
enum struct_type
{
cool,
fine,
bad
};
struct demo
{
struct_type type;
union
{
struct
{
double cool_factor;
} cool_part;
struct
{
int fineness;
} fine_part;
struct
{
char *bad_stuff;
} bad_part;
};
struct
{
int life_is_cool;
} common_part;
};
The pure and simple C++ answer is: use classes.
I can't determine from your question what you are trying to achieve: runtime variation or compile time variation, but either way, I doubt you'll get a workable implementation any other way. (Template metaprogramming aside... which isn't for the faint of heart.)

Getting name and type of a struct field from its object

For example, I have a struct which is something like this:
struct Test
{
int i;
float f;
char ch[10];
};
And I have an object of this struct such as:
Test obj;
Now, I want to programmatically get the field names and type of obj. Is it possible?
This is C++ BTW.
You are asking for Reflection in C++.
I'm afraid you cannot get the field names, but you can get the type of obj using Boost.Typeof:
#include <boost/typeof/typeof.hpp>
typedef BOOST_TYPEOF(obj) ObjType;
No its not possible without writing your own "struct" system. You can get the sizeof of a member but you need to know its name. C++ does not allow you, to my knowledge, to enumerate at compile or run-time the members of a given object. You could put a couple of functions such as "GetNumMembers()" and "GetMemberSize( index )" etc to get the info you are after ...
You may also want to search the web for "C++ serialization", especially the Boost libraries. I'd also search Stack Overflow for "C++ serialization".
Many C++ newbies would like to create object instances from a class name or fill in class fields based on names. This is where Serialization or Deserialization comes in handy.
My experience needing class and member names comes from printing debug information. Class and field names would be useful when handling exceptions, especially generating them.

DRYing c++ structure

I have a simple c++ struct that is extensively used in a program. Now I wish to persist the structure in a sqlite database as individual fields (iow not as a blob).
What good ways are there to map the attributes of the struct to database columns?
Since C++ isn't not a very "dynamic" language, it is running short of the kinds of ORM's you might commonly find available in other languages that make this task light work.
Personally speaking, I've always ended up having to write very thin wrapper classes for each table manually. Basically, you need a structure that maps to each table and an accessor class to get data in and out of the table as needed.
The structures should have a field per column and you'll need methods for each database operation you want to perform (CRUD for example).
Some interpreted / scripting languages (PHP, etc) support "refection", where code can examine itself. That would allow a database framework to automatically serialize struct members to / from a database. Unfortunately, C/C++ do not natively support this. Therefore, unless you want to store it as a giant BLOB (which certainly has drawbacks), you will need to manually map each member of the struct to a db column.
The only tricky part (aside from time consuming), is to choose the db column type that best corresponds to the C data type. (char[] -> varchar, etc). As jkp suggested, it's nice to have a thin wrapper class to read / write each of your persistent structures.
Hard to answer in general. The easiest approach would be one column per attribute, that may or may not be appropriate for your application.
The other extreme would be to merge it all into one column, depending on how you are going to use the data stored.
Maybe use some other persistence framework? sqlite might not be the best solution here.
I like to use a one to one relationship between my data structure fields and data base fields. Where each record in the table represents a complete structure instance. The only exception is if it will cause excessive de-normalization in the table. Now to get the data to/from the database from the structure I implement a template class that takes the structure as template parameter. I then derive from the template and implement the get/set features of the structure to the database. I use the OTL library for all the real database IO. This makes the burden of a special class per structure type less intrusive.
I have created a system of Fields and Records, now based on the Composite Design Pattern. The Fields contain a method to return the field name and optionally the field type (for an SQL statement). I'm currently moving the SQL stuff out of the field and into a Visitor object.
The record contains a function to return the table name.
Using this scheme, I can create an SQL table without knowing the details of the fields or records. I just call polymorphic methods in the base class.
I've tried other techniques, but my code has evolved to this implementation.
Contrary to some of the other answers, I say it is possible for this task to be automated. E.g. take a look at quince (http://quince-lib.com). It lets you do stuff like this:
struct point {
float x;
float y;
};
QUINCE_MAP_CLASS(point, (x)(y))
extern database db;
table<point> points(db, "points");
(Full disclosure: I wrote quince.)