[llvm-dev] What is relationship between Operator and Instruction?

399 views
Skip to first unread message

周书林 via llvm-dev

unread,
Jun 10, 2021, 4:38:19 AM6/10/21
to llvm-dev
Hello everyone,

I am learning the IR in LLVM and trying to do some analysis. Now I am doing a dataflow analysis via the def-use chain provided by Users.

In my opinion, the Users of a Value should be an instruction. However, when I iterate the User of a Value, sometimes I get an Operator, like GEPOperator, or an ConstantExpr.
I am quite confused about these situations. My question is, what is Operator in IR? and what is the difference between Operator and Instruction? Why can I get a GEPOperator as an Operand in a instruction rather than two instruction( a gep instruction and the next User instruction) ?

Best regards,
Shulin

周书林 via llvm-dev

unread,
Jun 10, 2021, 7:16:34 AM6/10/21
to rahul utkoor, llvm-dev
Hi, 

Thank you for your suggestions. 
I have learnt the basic IR structures in simple programs, but not all the objects could be generated. As I mentioned, about the Operators and ConstantExprs. I only got GEPOperator objects from .bc of MySQL. So is there any reference to learn about the relationship between those classes in IR? 

Sincerely,
Shulin

rahul utkoor <rahulut...@gmail.com> 于2021年6月10日周四 下午5:25写道:
Hello,
Since you are starting to learn IR, I recommend you to start with one simple program, compile the program, genreate CFG and understand the hierarchy of types/operands/instructions.
It helped me to understand the compilation pipeline in a quick way.


Thanks,
Rahul Utkoor


_______________________________________________
LLVM Developers mailing list
llvm...@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev

jeniffer lesley via llvm-dev

unread,
Jun 10, 2021, 7:29:19 AM6/10/21
to 周书林, llvm-dev, rahul utkoor
I second this question. I never undersrood this in LLVM IR. LangRef fails to explain this.

Craig Topper via llvm-dev

unread,
Jun 10, 2021, 2:50:52 PM6/10/21
to jeniffer lesley, llvm-dev, rahul utkoor
Operator is a subclass of User that contains methods that expose common functionality between Instruction and ConstantExpr. For example, having an opcode and operands. This allows a lot of code to treat them the same way without carrying about the differences. A GEPOperator is either a GEP Instruction or a GEP ConstantExpr. GEPOperator lets you not care which it really is. An Instruction can have a ConstantExpr as an operand, but a ConstantExpr cannot have an Instruction as an operand.

The real class hierarchy is such that Instructions and ConstantExpr don't have the same immediate base class though they both inherit from User. ConstantExpr inherits from Constant which inherits from User. Constant doesn't have an opcode(other than the one from Value) operands, but ConstantExpr does. Nothing inherits from Operator and it is never instantiated. Because it doesn't have any data members that aren't part of User we can static_cast both Instruction and ConstantExpr to it. All of the methods exposed by Operator look at member variables available in User to determine if it is an Instruction or a ConstantExpr and call the appropriate method by static_casting to Instruction or ConstantExpr first.

Hope that helps.

~Craig

周书林 via llvm-dev

unread,
Jun 10, 2021, 9:01:33 PM6/10/21
to Craig Topper, llvm-dev
Hi Craig,
 
Thank you very much!

Sincerely,
Shulin

Craig Topper <craig....@gmail.com> 于2021年6月11日周五 上午2:50写道:

周书林 via llvm-dev

unread,
Jun 10, 2021, 9:02:06 PM6/10/21
to rahul utkoor, llvm-dev
Hi Rahul,

Thank you very much!

Sincerely,
Shulin

rahul utkoor <rahulut...@gmail.com> 于2021年6月10日周四 下午9:21写道:
Hi,

LLVM modularity provides developers a unique opportunity to come up with better design decisions on individual components. Your questions related to "Operators and ConstantExprs", I suggest you spend some time, and understand the passes that are responsible for generating those instructions. 
As the code base is really huge and many smart people from different parts of the world are contributing to this project, there might be some things that can get missed in LangRef. In such a case, many times I used gdb/lldb debugger tools for understanding the pipeline.

Use debugging tools and identify the logic that is responsible for generating instructions(Operators and ConstantExprs). I am giving you directions to get started with understanding LLVM internals.


Thanks,
Rahul Utkoor
Reply all
Reply to author
Forward
0 new messages