SlideShare a Scribd company logo
Introduction to LLVM
on Program Analysis
Tao He
elfinhe@gmail.com
Department of Computer Science, Sun Yat-Sen University
Department of Computer Science and Engineering, HKUST
Group Discussion
June 2012
HKUST, Hong Kong, China
1/34
Outline
 Objectives
 A quick scenario
 LLVM IR
 ‘opt’ command
 Installation of LLVM
2/34
Objectives -
What do we want to do?
3/34
Objectives
 To implement a symbolic execution engine.
 A expression-based engine [BH07]
different from
most existing implementations (path-based
engines).
 Program analysis on C programs.
 To generate static single assignment (SSA)
representation of C first.
4/34
[BH07] Domagoj Babić and Alan J. Hu. Structural Abstraction of Software Verification Conditions. In Proceedings
of the 19th international conference on Computer aided verification (CAV'07), Lecture Notes in Computer Science,
2007, Volume 4590/2007, 366-378
A Quick Scenario -
What can LLVM do?
5/34
!A Quick Scenario
6/34
 Given a C program:
 #include <stdio.h>
 int branch(int n){
 if (n>0) printf("Positiven");
 else if (n==0) printf("Zeron");
 else if (n<0) printf("Negativen");
 return 0;
 }
 int main() {
 branch(-4); branch(0); branch(6);
 return 0;
 }
!A Quick Scenario
7/34
 Generate immediate representation (IR) of
LLVM – the SSA representation in LLVM
 clang -O3 -emit-llvm hello.c -S -o hello.ll
 define i32 @main() nounwind uwtable {
 %1 = alloca i32, align 4
 store i32 0, i32* %1
 %2 = call i32 @branch(i32 -4)
 %3 = call i32 @branch(i32 0)
 %4 = call i32 @branch(i32 6)
 ret i32 0
 }
 ...
[SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes.
URL: http://llvm.org/docs/Passes.html.
!A Quick Scenario
8/34
 Print call graph
 opt method_para_int_branch.ll -S -dot-
callgraph 2>output_file >/dev/null
 dot -Tsvg in.dot -o out.svg
[SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes.
URL: http://llvm.org/docs/Passes.html.
!A Quick Scenario
9/34
 Print control flow graph (CFG)
 opt method_para_int_branch.ll -S -dot-cfg
2>output_file >/dev/null
[SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes.
URL: http://llvm.org/docs/Passes.html.
# A Quick Scenario
10/34
 More:
 Dead Global Elimination
 Interprocedural Constant Propagation
 Dead Argument Elimination
 Inlining
 Reassociation
 Loop Invariant Code Motion
 Loop Opts
 Memory Promotion
 Dead Store Elimination
 Aggressive Dead Code Elimination
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
What is the SSA representation in LLVM?
- LLVM IR
11/34
LLVM IR
12/34
 “A Static Single Assignment (SSA) based
representation that provides type safety, low-
level operations, flexibility, and the capability
of representing 'all' high-level languages
cleanly.”
[Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
LLVM IR
13/34
 Three address code
 SSA-based
 Three different forms
 An in-memory compiler IR
 An on-disk bitcode representation (suitable for
fast loading by a Just-In-Time compiler)
 A human readable assembly language
representation
[Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
LLVM IR
14/34
 An example
 To multiply the integer variable '%X' by 8
 Syntax:
 <result> = mul <ty> <op1>, <op2>
 IR code:
 %result = mul i32 %X, 8
 More
 For floating point, use fmul
[Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
LLVM IR
15/34
 Another example
 Instruction jump – to change control flow
 Branches or loops
 Syntax:
 br i1 <cond>, label <iftrue>, label <iffalse>
 br label <dest> ; Unconditional branch
[Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
LLVM IR
16/34
 IR code:
 Test:
 %cond = icmp eq i32 %a, %b
 br i1 %cond, label %IfEqual, label %IfUnequal
 IfEqual:
 ret i32 1
 IfUnequal:
[Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
LLVM IR
17/34
 3rd
example
 Function call
 A simplified syntax:
 <result> = call <ty> <fnptrval>(<function args>)
 IR code:
 call i32 (i8*, ...)* @printf(i8* %msg, i32 12, i8 42)
[Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
LLVM IR
18/34
 4th
example
 Function definition
 A simplified syntax:
 define <ResultType> @<FunctionName> ([argument list]) { ... }
 IR code:
 define i32 @main() { … }
 define i32 @test(i32 %X, ...) { … }
[Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
LLVM IR
19/34
 The majority of instructions in C programs:
 Operations (binary/bitwise)
 Jumps
 Function calls
 Function definitions
 Many keywords in LLVM IR will not be
used for C programs. (e.g., invoke)
[Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
How to analyze programs
by using LLVM?
- ‘opt’ command
20/34
‘opt’ command
 Compiler is organized as a series of ‘passes’:
 Each pass is one analysis or transformation
21/34
[SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes.
URL: http://llvm.org/docs/Passes.html.
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
!‘opt’ command
 An example
 -dot-callgraph
22/34
[SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes.
URL: http://llvm.org/docs/Passes.html.
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
!‘opt’ command
23/34
An example
Print call graph: -dot-callgraph
 opt method_para_int_branch.ll -S -dot-
callgraph 2>output_file >/dev/null
 dot -Tsvg in.dot -o out.svg
[SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes.
URL: http://llvm.org/docs/Passes.html.
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
How to write your own pass?
24/34
How to write your own pass?
 Four types of pass:
 ModulePass: general interprocedural pass
 CallGraphSCCPass: bottom-up on the call graph
 FunctionPass: process a function at a time
 BasicBlockPass: process a basic block at a time
25/34
How to write your own pass?
 Two important classes
 User: http://llvm.org/docs/doxygen/html/classllvm_1_1User.html
 This class defines the interface that one who uses a
Value must implement.
 Instructions
 Constants
 Operators
 Value: http://llvm.org/docs/doxygen/html/classllvm_1_1Value.html
 It is the base class of all values computed by a
program that may be used as operands to other
values.
 e.g., instruction and function.
26/34
How to write your own pass?
 An example – print function names
27/34
How to write your own pass?
 An example – print function names
 First generate bytecode:
 clang -emit-llvm hello.c -o hello.bc
 Then
28/34
How to write your own pass?
 Another example – print def-use chain
29/34
How to install LLVM?
30/34
How to install LLVM?
 To compile programs faster and use built-in
transformation and analysis
 Install both ‘llvm’ and ‘clang’ from package
management software
 E.g., Synaptic, yum, apt.
 To write your own pass
 Build from source code and add your own pass
 http://llvm.org/docs/GettingStarted.html#quickstart
 http://llvm.org/docs/WritingAnLLVMPass.html
31/34
LLVM IR
32/34
 The majority of instructions in C programs:
 Operation (binary/bitwise)
 Jump
 Function call
 Function definition
[Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html
[LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini
Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
Q & A
33/34
Thank you!
Contact me via elfinhe@gmail.com
34/34

More Related Content

What's hot (20)

PPTX
Transpilers(Source-to-Source Compilers)
Shivang Bajaniya
 
PDF
LinkedIn - Disassembling Dalvik Bytecode
Alain Leon
 
DOCX
C++ question and answers
AdenKheire
 
PDF
Installation of PC-Lint and its using in Visual Studio 2005
PVS-Studio
 
PPTX
Linker and loader upload
Bin Yang
 
PDF
Towards easy program migration using language virtualization
ESUG
 
PDF
PIL - A Platform Independent Language
zefhemel
 
PPT
Nakov dot net-framework-overview-english
srivathsan.10
 
PPT
Overview of c++
geeeeeet
 
PPTX
C++vs java
Pradeep wolf king
 
PPTX
Compilation of c
Way2itech
 
PPTX
C compilation process
RajKumar Rampelli
 
PDF
Tail Call Elimination in Open Smalltalk
ESUG
 
PPT
Net Framework Overview
Luis Goldster
 
PPT
How a Compiler Works ?
Hirdesh Vishwdewa
 
KEY
In-depth look at the Flex compiler and HFCD
Stop Coding
 
PDF
C Programming - Refresher - Part I
Emertxe Information Technologies Pvt Ltd
 
PDF
CORBA Programming with TAOX11/C++11 tutorial
Remedy IT
 
PDF
OFI libfabric Tutorial
dgoodell
 
PPT
Mixing Python and Java
Andreas Schreiber
 
Transpilers(Source-to-Source Compilers)
Shivang Bajaniya
 
LinkedIn - Disassembling Dalvik Bytecode
Alain Leon
 
C++ question and answers
AdenKheire
 
Installation of PC-Lint and its using in Visual Studio 2005
PVS-Studio
 
Linker and loader upload
Bin Yang
 
Towards easy program migration using language virtualization
ESUG
 
PIL - A Platform Independent Language
zefhemel
 
Nakov dot net-framework-overview-english
srivathsan.10
 
Overview of c++
geeeeeet
 
C++vs java
Pradeep wolf king
 
Compilation of c
Way2itech
 
C compilation process
RajKumar Rampelli
 
Tail Call Elimination in Open Smalltalk
ESUG
 
Net Framework Overview
Luis Goldster
 
How a Compiler Works ?
Hirdesh Vishwdewa
 
In-depth look at the Flex compiler and HFCD
Stop Coding
 
C Programming - Refresher - Part I
Emertxe Information Technologies Pvt Ltd
 
CORBA Programming with TAOX11/C++11 tutorial
Remedy IT
 
OFI libfabric Tutorial
dgoodell
 
Mixing Python and Java
Andreas Schreiber
 

Similar to Introduction to llvm (20)

PDF
TMPA-2017: Vellvm - Verifying the LLVM
Iosif Itkin
 
PPTX
07 140430-ipp-languages used in llvm during compilation
Adam Husár
 
PPTX
LLVM-Based-Compiler-for-a-Custom-Language (2).pptx
chinthalareddy0128ss
 
PDF
Smalltalk JIT Compilation: LLVM Experimentation
ESUG
 
PDF
LCU14 209- LLVM Linux
Linaro
 
PPTX
LLVM Compiler
Chayan Pathak
 
PDF
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
Min-Yih Hsu
 
PDF
The true story_of_hello_world
fantasy zheng
 
PPTX
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
Akihiro Hayashi
 
PDF
Appsec obfuscator reloaded
Cyber Security Alliance
 
PPTX
LLVM Compiler for different lagguage.pptx
chinthalareddy0128ss
 
PDF
LLVM Workshop Osaka Umeda, Japan
ujihisa
 
PPTX
OptView2 - C++ on Sea 2022
Ofek Shilon
 
PDF
Os Lattner
oscon2007
 
PPTX
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
corehard_by
 
PPT
CC Week 11.ppt
KamranAli649587
 
PPTX
Reames-FalconKeynote java falcon about jit.pptx
GuoliangDing3
 
PDF
BUD17-302: LLVM Internals #2
Linaro
 
PDF
Lessons Of Binary Analysis - Christien Rioux
crioux1
 
PDF
Developments in LLVM-based toolchains and tooling for RISC-V
Igalia
 
TMPA-2017: Vellvm - Verifying the LLVM
Iosif Itkin
 
07 140430-ipp-languages used in llvm during compilation
Adam Husár
 
LLVM-Based-Compiler-for-a-Custom-Language (2).pptx
chinthalareddy0128ss
 
Smalltalk JIT Compilation: LLVM Experimentation
ESUG
 
LCU14 209- LLVM Linux
Linaro
 
LLVM Compiler
Chayan Pathak
 
[COSCUP 2021] LLVM Project: The Good, The Bad, and The Ugly
Min-Yih Hsu
 
The true story_of_hello_world
fantasy zheng
 
LLVM Optimizations for PGAS Programs -Case Study: LLVM Wide Optimization in C...
Akihiro Hayashi
 
Appsec obfuscator reloaded
Cyber Security Alliance
 
LLVM Compiler for different lagguage.pptx
chinthalareddy0128ss
 
LLVM Workshop Osaka Umeda, Japan
ujihisa
 
OptView2 - C++ on Sea 2022
Ofek Shilon
 
Os Lattner
oscon2007
 
Как работает LLVM бэкенд в C#. Егор Богатов ➠ CoreHard Autumn 2019
corehard_by
 
CC Week 11.ppt
KamranAli649587
 
Reames-FalconKeynote java falcon about jit.pptx
GuoliangDing3
 
BUD17-302: LLVM Internals #2
Linaro
 
Lessons Of Binary Analysis - Christien Rioux
crioux1
 
Developments in LLVM-based toolchains and tooling for RISC-V
Igalia
 
Ad

More from Tao He (18)

PPTX
Java 并发编程笔记:01. 并行与并发 —— 概念
Tao He
 
PPTX
A software fault localization technique based on program mutations
Tao He
 
PDF
Testing survey
Tao He
 
DOC
Testing survey by_directions
Tao He
 
PPT
Smart debugger
Tao He
 
PPT
Mutation testing
Tao He
 
DOCX
C语言benchmark覆盖信息收集总结4
Tao He
 
PPT
Django
Tao He
 
DOC
基于覆盖信息的软件错误定位技术综述
Tao He
 
DOC
Java覆盖信息收集工具比较
Tao He
 
PPT
Testing group’s work on fault localization
Tao He
 
PPTX
Muffler a tool using mutation to facilitate fault localization 2.0
Tao He
 
PPTX
Muffler a tool using mutation to facilitate fault localization 2.3
Tao He
 
PPT
Semantic Parsing in Bayesian Anti Spam
Tao He
 
PPT
Problems
Tao He
 
PPT
A survey of software testing
Tao He
 
PPT
Cleansing test suites from coincidental correctness to enhance falut localiza...
Tao He
 
PPTX
Concrete meta research - how to collect, manage, and read papers?
Tao He
 
Java 并发编程笔记:01. 并行与并发 —— 概念
Tao He
 
A software fault localization technique based on program mutations
Tao He
 
Testing survey
Tao He
 
Testing survey by_directions
Tao He
 
Smart debugger
Tao He
 
Mutation testing
Tao He
 
C语言benchmark覆盖信息收集总结4
Tao He
 
Django
Tao He
 
基于覆盖信息的软件错误定位技术综述
Tao He
 
Java覆盖信息收集工具比较
Tao He
 
Testing group’s work on fault localization
Tao He
 
Muffler a tool using mutation to facilitate fault localization 2.0
Tao He
 
Muffler a tool using mutation to facilitate fault localization 2.3
Tao He
 
Semantic Parsing in Bayesian Anti Spam
Tao He
 
Problems
Tao He
 
A survey of software testing
Tao He
 
Cleansing test suites from coincidental correctness to enhance falut localiza...
Tao He
 
Concrete meta research - how to collect, manage, and read papers?
Tao He
 
Ad

Recently uploaded (20)

PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
Salesforce Experience Cloud Consultant.pdf
VALiNTRY360
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
Everything you need to know about pricing & licensing Microsoft 365 Copilot f...
Q-Advise
 
PDF
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
PDF
UITP Summit Meep Pitch may 2025 MaaS Rebooted
campoamor1
 
PDF
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PPTX
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Smart Doctor Appointment Booking option in odoo.pptx
AxisTechnolabs
 
PPTX
iaas vs paas vs saas :choosing your cloud strategy
CloudlayaTechnology
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Salesforce Experience Cloud Consultant.pdf
VALiNTRY360
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
Everything you need to know about pricing & licensing Microsoft 365 Copilot f...
Q-Advise
 
[Solution] Why Choose the VeryPDF DRM Protector Custom-Built Solution for You...
Lingwen1998
 
UITP Summit Meep Pitch may 2025 MaaS Rebooted
campoamor1
 
ERP Consulting Services and Solutions by Contetra Pvt Ltd
jayjani123
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Smart Doctor Appointment Booking option in odoo.pptx
AxisTechnolabs
 
iaas vs paas vs saas :choosing your cloud strategy
CloudlayaTechnology
 

Introduction to llvm

  • 1. Introduction to LLVM on Program Analysis Tao He [email protected] Department of Computer Science, Sun Yat-Sen University Department of Computer Science and Engineering, HKUST Group Discussion June 2012 HKUST, Hong Kong, China 1/34
  • 2. Outline  Objectives  A quick scenario  LLVM IR  ‘opt’ command  Installation of LLVM 2/34
  • 3. Objectives - What do we want to do? 3/34
  • 4. Objectives  To implement a symbolic execution engine.  A expression-based engine [BH07] different from most existing implementations (path-based engines).  Program analysis on C programs.  To generate static single assignment (SSA) representation of C first. 4/34 [BH07] Domagoj Babić and Alan J. Hu. Structural Abstraction of Software Verification Conditions. In Proceedings of the 19th international conference on Computer aided verification (CAV'07), Lecture Notes in Computer Science, 2007, Volume 4590/2007, 366-378
  • 5. A Quick Scenario - What can LLVM do? 5/34
  • 6. !A Quick Scenario 6/34  Given a C program:  #include <stdio.h>  int branch(int n){  if (n>0) printf("Positiven");  else if (n==0) printf("Zeron");  else if (n<0) printf("Negativen");  return 0;  }  int main() {  branch(-4); branch(0); branch(6);  return 0;  }
  • 7. !A Quick Scenario 7/34  Generate immediate representation (IR) of LLVM – the SSA representation in LLVM  clang -O3 -emit-llvm hello.c -S -o hello.ll  define i32 @main() nounwind uwtable {  %1 = alloca i32, align 4  store i32 0, i32* %1  %2 = call i32 @branch(i32 -4)  %3 = call i32 @branch(i32 0)  %4 = call i32 @branch(i32 6)  ret i32 0  }  ... [SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes. URL: http://llvm.org/docs/Passes.html.
  • 8. !A Quick Scenario 8/34  Print call graph  opt method_para_int_branch.ll -S -dot- callgraph 2>output_file >/dev/null  dot -Tsvg in.dot -o out.svg [SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes. URL: http://llvm.org/docs/Passes.html.
  • 9. !A Quick Scenario 9/34  Print control flow graph (CFG)  opt method_para_int_branch.ll -S -dot-cfg 2>output_file >/dev/null [SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes. URL: http://llvm.org/docs/Passes.html.
  • 10. # A Quick Scenario 10/34  More:  Dead Global Elimination  Interprocedural Constant Propagation  Dead Argument Elimination  Inlining  Reassociation  Loop Invariant Code Motion  Loop Opts  Memory Promotion  Dead Store Elimination  Aggressive Dead Code Elimination [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 11. What is the SSA representation in LLVM? - LLVM IR 11/34
  • 12. LLVM IR 12/34  “A Static Single Assignment (SSA) based representation that provides type safety, low- level operations, flexibility, and the capability of representing 'all' high-level languages cleanly.” [Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 13. LLVM IR 13/34  Three address code  SSA-based  Three different forms  An in-memory compiler IR  An on-disk bitcode representation (suitable for fast loading by a Just-In-Time compiler)  A human readable assembly language representation [Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 14. LLVM IR 14/34  An example  To multiply the integer variable '%X' by 8  Syntax:  <result> = mul <ty> <op1>, <op2>  IR code:  %result = mul i32 %X, 8  More  For floating point, use fmul [Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 15. LLVM IR 15/34  Another example  Instruction jump – to change control flow  Branches or loops  Syntax:  br i1 <cond>, label <iftrue>, label <iffalse>  br label <dest> ; Unconditional branch [Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 16. LLVM IR 16/34  IR code:  Test:  %cond = icmp eq i32 %a, %b  br i1 %cond, label %IfEqual, label %IfUnequal  IfEqual:  ret i32 1  IfUnequal: [Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 17. LLVM IR 17/34  3rd example  Function call  A simplified syntax:  <result> = call <ty> <fnptrval>(<function args>)  IR code:  call i32 (i8*, ...)* @printf(i8* %msg, i32 12, i8 42) [Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 18. LLVM IR 18/34  4th example  Function definition  A simplified syntax:  define <ResultType> @<FunctionName> ([argument list]) { ... }  IR code:  define i32 @main() { … }  define i32 @test(i32 %X, ...) { … } [Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 19. LLVM IR 19/34  The majority of instructions in C programs:  Operations (binary/bitwise)  Jumps  Function calls  Function definitions  Many keywords in LLVM IR will not be used for C programs. (e.g., invoke) [Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 20. How to analyze programs by using LLVM? - ‘opt’ command 20/34
  • 21. ‘opt’ command  Compiler is organized as a series of ‘passes’:  Each pass is one analysis or transformation 21/34 [SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes. URL: http://llvm.org/docs/Passes.html. [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 22. !‘opt’ command  An example  -dot-callgraph 22/34 [SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes. URL: http://llvm.org/docs/Passes.html. [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 23. !‘opt’ command 23/34 An example Print call graph: -dot-callgraph  opt method_para_int_branch.ll -S -dot- callgraph 2>output_file >/dev/null  dot -Tsvg in.dot -o out.svg [SH] Reid Spencer and Gordon Henriksen. LLVM's Analysis and Transform Passes. URL: http://llvm.org/docs/Passes.html. [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.
  • 24. How to write your own pass? 24/34
  • 25. How to write your own pass?  Four types of pass:  ModulePass: general interprocedural pass  CallGraphSCCPass: bottom-up on the call graph  FunctionPass: process a function at a time  BasicBlockPass: process a basic block at a time 25/34
  • 26. How to write your own pass?  Two important classes  User: http://llvm.org/docs/doxygen/html/classllvm_1_1User.html  This class defines the interface that one who uses a Value must implement.  Instructions  Constants  Operators  Value: http://llvm.org/docs/doxygen/html/classllvm_1_1Value.html  It is the base class of all values computed by a program that may be used as operands to other values.  e.g., instruction and function. 26/34
  • 27. How to write your own pass?  An example – print function names 27/34
  • 28. How to write your own pass?  An example – print function names  First generate bytecode:  clang -emit-llvm hello.c -o hello.bc  Then 28/34
  • 29. How to write your own pass?  Another example – print def-use chain 29/34
  • 30. How to install LLVM? 30/34
  • 31. How to install LLVM?  To compile programs faster and use built-in transformation and analysis  Install both ‘llvm’ and ‘clang’ from package management software  E.g., Synaptic, yum, apt.  To write your own pass  Build from source code and add your own pass  http://llvm.org/docs/GettingStarted.html#quickstart  http://llvm.org/docs/WritingAnLLVMPass.html 31/34
  • 32. LLVM IR 32/34  The majority of instructions in C programs:  Operation (binary/bitwise)  Jump  Function call  Function definition [Lat] Chris Lattner. LLVM Language Reference Manual. URL: http://llvm.org/docs/LangRef.html [LA04] Chris Lattner and Vikram Adve. The LLVM Compiler Framework and Infrastructure Tutorial. Mini Workshop on Compiler Research Infrastructures (LCPC'04), West Lafayette, Indiana, Sep. 2004.