Tutorial 2 - Intro to LLVM (Cont)

展开查看详情

1.Introduction to LLVM (II) Bojian Zheng CSCD70 Spring 2018 bojian@cs.toronto.edu 1

2.Makefile Error: Optimize.mk In the Optimize.mk file provided in the first assignment, you might need to add ./ in front of the optimizer target FunctionInfo.so . Thanks a lot to Chengyu (Tyrone) Xiong for pointing this out. 2

3.Review Keywords: Intermediate Representation (IR) Optimization Pass Analysis & Transformation 3

4.Review Keywords: Program Structure Iterators Downcasting LLVM Pass Interface 4

5.Transformations 5

6.Insert/Remove/Move/Replace Instructions Three Options Instruction class methods. Ask parent ( BasicBlock ) to do this. Make use of BasicBlockUtils . 6

7.Attention! Iterator Hazard As you do transformations, iterators might be invalidated . Demo on std ::vector < unsigned > ::iterator Thanks a lot to Qiongsi Wu for bringing this up.   7

8.Attention! Reference Updates Original Code %2 = add %1, 0 %3 = mul %2, 2 Transformed Code %2 = add %1, 0 %3 = mul ??? , 2 8

9.Questions? Keywords: Iterator Hazard References Update (More Later On) 9

10.LLVM Instruction: The User-Use- Usee Design Pattern 10

11.LLVM Class Hierarchies 11 Value Instruction User

12.Value ( Usee ) The Value class is the most important base class in LLVM. It has a type (integer, floating point, …): getType () It might or might not have a name: hasName () , getName () Keeps track of a list of User s that are using this Value . 12

13.Instruction (User) An User keeps track of a list of Values that it is using as Operands : User user = … for (auto iter = user.op_begin (); iter != user.op_end (); ++ iter ) {Value * operand = * iter ; …} An Instruction is a User . 13

14.But wait, … Is Instruction (User) a Value ( Usee ) ? DO NOT interpret this statement as “the result of Instruction is assigned to ”, instead, think this way – “ is the Value Representation of Instruction ”. Therefore, whenever we use the Value , we mean to use the Instruction .   14

15.To Conclude Suppose we have an instruction: Instruction inst = … What is this instruction using? for (auto iter = inst. op _begin (); iter != inst. op _end (); ++ iter ) {…} What is using this instruction? for (auto iter = inst. user _begin (); iter != inst. user _end (); ++ iter ) {…} 15

16.Questions? Keywords: User-Use- Usee Design Pattern 16

17.Optimizer Manager 17

18.Optimizer Manager What is this doing? void Analysis:: getAnalysisUsage ( AnalysisUsage & AU) const { AU.setPreservesAll (); } Very frequently, when writing a pass, we want the followings: What information does this pass require? Will this information still be preserved after this pass? 18

19.Questions Keywords: Require Preserve 19

20.Code Download Links https://github.com/ArmageddonKnight/CSCD70-Tutorial-Demo Visitor Design Pattern serves as an alternative to Dynamic Casting. You can find an example on this in the repository. 20