├── .gitattributes ├── .github ├── History.md ├── PL.md ├── clang-llvm.md ├── paper.md └── readings.md ├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── andersen-pointer-analysis ├── README.md └── paper │ ├── andersen-thesis94.pdf │ ├── hardekopf07ant.pdf │ └── hardekopf07exploiting.pdf ├── array-optimization └── README.md ├── ast-interpreter ├── ASTInterpreter.cpp ├── Environment.h ├── Makefile ├── README.md ├── test-cases │ ├── test0.c │ ├── test1.c │ ├── test1.txt │ ├── test10.c │ ├── test11.c │ ├── test12.c │ ├── test13.c │ ├── test14.c │ ├── test15.c │ ├── test16.c │ ├── test16.txt │ ├── test17.c │ ├── test18.c │ ├── test19.c │ ├── test2.c │ ├── test20.c │ ├── test21.c │ ├── test22.c │ ├── test23.c │ ├── test24.c │ ├── test3.c │ ├── test4.c │ ├── test4.txt │ ├── test5.c │ ├── test6.c │ ├── test7.c │ ├── test8.c │ └── test9.c └── test │ ├── test-array.c │ ├── test-deref.c │ ├── test-fn.c │ ├── test-fn.txt │ ├── test-if.c │ ├── test-if.dot │ ├── test-if.txt │ ├── test-malloc.c │ ├── test-struct.c │ ├── test-while.c │ ├── test-while.txt │ ├── test.c │ └── test.txt ├── auto-vectorization └── README.md ├── clang-driver ├── Clang_Architecture_-_ChristopherGuntli.pdf ├── OneHourCourse.pptx └── clang-driver.md ├── clang-dsl └── README.md ├── clang-new-type └── README.md ├── clang-offload-bundler ├── README.md ├── ibm_openmp_offloading.pdf ├── img │ ├── bind-arch.xml │ ├── export.png │ └── openmp-offload-example.png └── openmp-offloading-support.pdf ├── cmu-15-745 ├── README.md ├── asst1 │ └── .gitignore ├── asst2 │ └── .gitignore └── asst3 │ └── .gitignore ├── code-pointer-integrity ├── Code-Pointer-Integrity.md └── tbaa │ ├── Gohman-AliasAnalysis.pdf │ └── examples │ ├── compile.sh │ ├── test.c │ └── test.log ├── cpp-insights ├── README.md ├── afertig-ndcolo-2017-fast-and-small.pdf └── foo │ ├── lambda-insights.cpp │ ├── lambda.cpp │ ├── structure-binding-insights.cpp │ └── structure-binding.cpp ├── dataflow-analysis ├── README.md ├── Value-Range Analysis of C Programs.pdf ├── extra │ └── benchmarks │ │ ├── Makefile │ │ ├── branchloop │ │ ├── Makefile │ │ ├── branchloop.bc │ │ ├── branchloop.cpp │ │ └── branchloop.ll │ │ ├── expression │ │ ├── expression.bc │ │ └── expression.c │ │ ├── must-pointer │ │ ├── Makefile │ │ ├── must-pointer.cpp │ │ └── must-pointer.ll │ │ ├── pointer │ │ ├── Makefile │ │ ├── pointer.bc │ │ └── pointer.cpp │ │ └── range │ │ ├── Makefile │ │ ├── range.bc │ │ └── range.cpp ├── llvm │ └── src │ │ └── lib │ │ └── CSE231 │ │ ├── AnalysisPass.cpp │ │ ├── AnalysisPass.h │ │ ├── AvailableExprAnalysis.cpp │ │ ├── AvailableExprAnalysis.h │ │ ├── ConstantPropAnalysis.cpp │ │ ├── ConstantPropAnalysis.h │ │ ├── Expression.cpp │ │ ├── Expression.h │ │ ├── ExpressionContainer.cpp │ │ ├── ExpressionContainer.h │ │ ├── Lattice.h │ │ ├── Makefile │ │ ├── PointerAnalysis.cpp │ │ ├── PointerAnalysis.h │ │ ├── RangeAnalysis.cpp │ │ └── RangeAnalysis.h ├── report.pdf └── runscripts │ ├── RangeAnalysis.sh │ ├── avail_expr.sh │ ├── constantprop.sh │ ├── may-pointer.sh │ └── must-pointer.sh ├── directive └── README.md ├── function-pointer-analysis ├── LLVMAssignment.cpp ├── Makefile ├── README.md ├── note.txt └── testcases │ ├── bonus.bc │ ├── bonus.c │ ├── bonus.opt │ ├── funcptr0.bc │ ├── funcptr0.c │ ├── funcptr0.ll │ ├── funcptr0.opt │ ├── funcptr1.bc │ ├── funcptr1.c │ ├── funcptr1.ll │ ├── funcptr1.opt │ ├── funcptr11.bc │ ├── funcptr11.c │ ├── funcptr11.opt │ ├── funcptr12.bc │ ├── funcptr12.c │ ├── funcptr12.opt │ ├── funcptr13.bc │ ├── funcptr13.c │ ├── funcptr13.opt │ ├── funcptr14.bc │ ├── funcptr14.c │ ├── funcptr14.opt │ ├── funcptr2.bc │ ├── funcptr2.c │ ├── funcptr2.ll │ ├── funcptr2.opt │ ├── funcptr20.bc │ ├── funcptr20.c │ ├── funcptr20.opt │ ├── funcptr21.bc │ ├── funcptr21.c │ ├── funcptr21.opt │ ├── funcptr22.bc │ ├── funcptr22.c │ ├── funcptr22.opt │ ├── funcptr23.bc │ ├── funcptr23.c │ ├── funcptr23.opt │ ├── funcptr24.bc │ ├── funcptr24.c │ ├── funcptr24.opt │ ├── funcptr25.bc │ ├── funcptr25.c │ ├── funcptr25.opt │ ├── funcptr26.bc │ ├── funcptr26.c │ ├── funcptr26.opt │ ├── funcptr3.bc │ ├── funcptr3.c │ └── funcptr3.opt ├── intel-mpx └── Intel-MPX.md ├── kaleidoscope ├── README.md ├── ast │ ├── Makefile │ ├── bnf.md │ └── parser.cpp ├── dbg-info │ ├── KaleidoscopeJIT.h │ ├── Makefile │ └── dbg_info.cpp ├── functional_programming_language │ ├── KaleidoscopeJIT.h │ ├── Makefile │ └── functional.cpp ├── ir │ ├── Makefile │ └── gen-ir.cpp ├── jit │ ├── KaleidoscopeJIT.h │ ├── Makefile │ └── jit.cpp └── target-triple │ ├── Makefile │ └── target-triple.cpp ├── lambda-calculus ├── 03.2-lambda-calculus.pdf ├── Chapter5-lambda-calculus.pdf ├── README.md ├── lambdanotes.pdf └── pfpl-2nded-chap-4-5-6.pdf ├── live-variable-analysis ├── Dataflow.h ├── LiveVariable.cpp ├── Liveness.md ├── Makefile ├── README.md ├── benchmark │ ├── add.c │ ├── add.ll │ ├── alias.c │ ├── alias.ll │ ├── bar.c │ ├── bar.ll │ ├── foo.c │ ├── foo.ll │ ├── ir-gen.sh │ ├── sum.c │ ├── sum.ll │ ├── sv_add.c │ ├── sv_add.ll │ ├── test1.c │ └── test1.ll ├── img │ └── live-variable-analysis-review.png └── test.sh ├── llvm-backend ├── LLVM - Another Toolchain Platform.pptx ├── cpu0 │ └── CPU0.md ├── foo │ ├── build.sh │ ├── insns.td │ └── result.txt └── llvm-backend.md ├── llvm-profiling ├── README.md └── examples │ ├── README.md │ ├── dynamic-instruction-count │ ├── CountDynamicInstructions.cpp │ ├── Makefile │ ├── README.md │ ├── benchmark │ │ ├── ir-gen.sh │ │ ├── random.c │ │ ├── random.ll │ │ ├── sum.c │ │ └── sum.ll │ ├── runtime.cpp │ └── test.sh │ └── profiling-branch-bias │ ├── Makefile │ ├── ProfilingBranchBias.cpp │ ├── README.md │ ├── benchmark │ ├── br.c │ ├── br.ll │ ├── ir-gen.sh │ ├── random.c │ ├── random.ll │ ├── sum.c │ └── sum.ll │ ├── runtime.cpp │ └── test.sh ├── memory-ssa ├── .gitignore ├── README.md └── examples │ ├── ir-gen.sh │ ├── mm-ssa.ll │ ├── sum.c │ └── sum.ll ├── mlir-examples ├── README.md └── docs │ ├── MLIR C4ML CGO Workshop Talk.pdf │ └── eurollvm-19 │ ├── Keynote-ShpeismanLattner-MLIR.pdf │ └── Tutorial-AminiVasilacheZinenko-MLIR.pdf ├── soft-pipeline ├── README.md └── loop-pipeline.md ├── source-to-source ├── building_a_cpp_reflection_system_in_one_weekend_using_clang_and_llvm__arvid_gerstmann__cppcon_2018.pdf └── krzikalla-slides-source-to-source-with-clang.pdf ├── static-program-analysis └── README.md ├── type-system ├── 5.SemanticAnalysis.types.pdf ├── README.md └── lecture11.pdf ├── ucsd-cse231 ├── README.md ├── proj1 │ ├── README.md │ └── src │ │ ├── BranchBias │ │ ├── BranchBias.cpp │ │ ├── Makefile │ │ ├── README.md │ │ └── benchmark │ │ │ ├── compile.sh │ │ │ └── sum.c │ │ ├── CountDynamicInsts │ │ ├── CountDynamicInstructions.cpp │ │ ├── Makefile │ │ ├── README.md │ │ └── benchmark │ │ │ ├── compile.sh │ │ │ ├── sum.bc │ │ │ ├── sum.c │ │ │ └── sum.ll │ │ └── CountStaticInsts │ │ ├── CountStaticInstructions.cpp │ │ ├── Makefile │ │ ├── README.md │ │ ├── benchmark │ │ ├── compile.sh │ │ ├── foo.c │ │ ├── foo.ll │ │ ├── sum.c │ │ └── sum.ll │ │ └── test.sh └── proj2 │ └── README.md └── value-range-analysis ├── Dataflow.h ├── Dataflow_.h ├── LLVMAssignment.cpp ├── Liveness.h ├── Makefile ├── README.md ├── Ref └── SBLP2011_douglas.pdf ├── ans.txt └── test-cases ├── gen-bitcode.cpp ├── run.cpp └── test-cases ├── test1.c ├── test1.dot ├── test1.opt ├── test10.c ├── test10.dot ├── test10.opt ├── test11.c ├── test11.dot ├── test11.opt ├── test12.c ├── test12.dot ├── test12.opt ├── test13.c ├── test13.dot ├── test13.opt ├── test14.c ├── test14.dot ├── test14.opt ├── test15.c ├── test15.dot ├── test15.opt ├── test16.c ├── test16.dot ├── test16.opt ├── test17.c ├── test17.dot ├── test17.opt ├── test18.c ├── test18.dot ├── test18.opt ├── test19.c ├── test19.dot ├── test19.opt ├── test2.c ├── test2.dot ├── test2.opt ├── test20.c ├── test20.dot ├── test20.opt ├── test21.c ├── test21.dot ├── test21.opt ├── test22.c ├── test22.dot ├── test22.opt ├── test23.c ├── test23.dot ├── test23.opt ├── test24.c ├── test24.dot ├── test24.opt ├── test25.c ├── test25.dot ├── test25.opt ├── test26.c ├── test26.dot ├── test26.opt ├── test27.c ├── test27.dot ├── test27.opt ├── test28.c ├── test28.dot ├── test28.opt ├── test29.c ├── test29.dot ├── test29.opt ├── test3.c ├── test3.dot ├── test3.opt ├── test30.c ├── test30.dot ├── test30.opt ├── test31.c ├── test31.dot ├── test31.opt ├── test32.c ├── test32.dot ├── test32.opt ├── test33.c ├── test33.dot ├── test33.opt ├── test34.c ├── test34.dot ├── test34.opt ├── test4.c ├── test4.dot ├── test4.opt ├── test5.c ├── test5.dot ├── test5.opt ├── test6.c ├── test6.dot ├── test6.opt ├── test7.c ├── test7.dot ├── test7.opt ├── test8.c ├── test8.dot ├── test8.opt ├── test9.c ├── test9.dot └── test9.opt /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.github/History.md: -------------------------------------------------------------------------------- 1 | # Big People 2 | 3 | - Alonzo Church: 4 | 5 | Princeton in the 1930's was an exciting place for logic. There was Church together with his students Rosser and Kleene. There was John von Neumann. Alan Turing, who had been thinking about the notion of effective calculability, came as a visiting graduate student in 1936 and stayed to complete his Ph.D. under Church. And Kurt Gödel visited the Institute for Advanced Study in 1933 and 1935, before moving there permanently. 6 | 7 | -------------------------------------------------------------------------------- /.github/PL.md: -------------------------------------------------------------------------------- 1 | ## Programming Languages 2 | 3 | ### 1. Continuation 4 | 5 | Q: What is continuation? 6 | A: A continuation is a representation of the control flow of your program at any point in time, essentially the stack. 7 | 8 | In abstract terms, it represents "the rest of your program." 9 | In some programming languages like `Scheme` that `expose continuations as first-class values`, you can capture the current continuation and invoke it later. When invoked, the current program state is replaced with the state at which the continuation was captured (i.e. the current stack is replaced with the stack from the continuation). 10 | 11 | Continuations allow you to literally "jump" to different places in your code. They are a low-level primitive that gives you control over execution flow, allowing you implement everything from resumable exceptions to coroutines. Understanding continuations was the best thing I did as a young programmer; it forces you to understand how control flow works. 12 | 13 | The control and data flow of a program can be represented using continuations. 14 | 15 | 在目前的DNN领域,计算图的表示方式,其本质上只能表达到函数调用这个级别,是无法表达分支等语义的, 16 | 17 | An interesting idea: 在计算图中引入continuation的概念,是否存在一些有意思的研究问题? 18 | 19 | #### REFs 20 | 21 | - 22 | - 23 | - Graph Oriented Programming: 24 | - Asynchronous continuations: 25 | - A nice book, 26 | 27 | ### 2. CPS(Continuation-passing style) 28 | 29 | ### 3. Type System 30 | 31 | A nice example is Google's Dart: 32 | 33 | 34 | #### REFs 35 | 36 | - 37 | - 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "source-to-source/meta-reflect"] 2 | path = source-to-source/meta-reflect 3 | url = https://github.com/Leandros/metareflect.git 4 | [submodule "mapping-high-level-constructs-to-llvm-ir"] 5 | path = mapping-high-level-constructs-to-llvm-ir 6 | url = https://github.com/f0rki/mapping-high-level-constructs-to-llvm-ir 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2016 Json Lee 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /andersen-pointer-analysis/README.md: -------------------------------------------------------------------------------- 1 | # Andersen Pointer Analysis 2 | `Pointer analysis`, or `points-to analysis`, is a static code analysis technique that establishes which pointers, or heap references, can point to which variables, or storage locations. Techniques range widely in performance and precision. Relevant algorithm is [Steensgaard's algorithm](https://en.wikipedia.org/wiki/Steensgaard%27s_algorithm) and Andersen's algorithm.
3 | For Andersen's pointer analysis algorithm, more details can refer to his [Ph.D paper...](https://github.com/lijiansong/clang-llvm-tutorial/blob/master/andersen-pointer-analysis/paper/andersen-thesis94.pdf) It can be summarized in this form: 4 | ``` 5 | Base: a = & b; b ∈ pts(a) 6 | Assign: a = b; pts(b) = pts(a) 7 | Store: *p = b; ∀ v ∈ pts(p), pts(b) = pts(v) 8 | Load: a = *q; ∀ v ∈ pts(q), pts(v) = pts(a) 9 | ``` 10 | 11 | In LLVM, it has its own pointer analysis infrastructure, more details can be seen [here](http://llvm.org/docs/AliasAnalysis.html)...
12 | In this archieve I will implement Andersen's pointer analysis algorithm by taking use of llvm. 13 | -------------------------------------------------------------------------------- /andersen-pointer-analysis/paper/andersen-thesis94.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/andersen-pointer-analysis/paper/andersen-thesis94.pdf -------------------------------------------------------------------------------- /andersen-pointer-analysis/paper/hardekopf07ant.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/andersen-pointer-analysis/paper/hardekopf07ant.pdf -------------------------------------------------------------------------------- /andersen-pointer-analysis/paper/hardekopf07exploiting.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/andersen-pointer-analysis/paper/hardekopf07exploiting.pdf -------------------------------------------------------------------------------- /array-optimization/README.md: -------------------------------------------------------------------------------- 1 | ## LLVM Array Optimization 2 | 3 | Transformation 4 | -------------------------------------------------------------------------------- /ast-interpreter/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 Json Lee. All rights reserved. 2 | # For more info, see [here](https://github.com/lijiansong) or my [github page](https://lijiansong.github.io/) 3 | 4 | LLVM_CONFIG?=llvm-config 5 | 6 | ifndef VERBOSE 7 | QUIET:=@ 8 | endif 9 | 10 | SRC_DIR?=$(PWD) 11 | LDFLAGS+=$(shell $(LLVM_CONFIG) --ldflags) 12 | 13 | #Debug flags 14 | COMMON_FLAGS=-Wall -Wextra -g 15 | CXXFLAGS+=$(COMMON_FLAGS) $(shell $(LLVM_CONFIG) --cxxflags) -fno-rtti 16 | CPPFLAGS+=$(shell $(LLVM_CONFIG) --cppflags) -I$(SRC_DIR) 17 | # include all dependency clang libs 18 | CLANGLIBS=\ 19 | -Wl,--start-group\ 20 | -lclang\ 21 | -lclangFrontend\ 22 | -lclangDriver\ 23 | -lclangSerialization\ 24 | -lclangParse\ 25 | -lclangSema\ 26 | -lclangAnalysis\ 27 | -lclangEdit\ 28 | -lclangAST\ 29 | -lclangLex\ 30 | -lclangBasic\ 31 | -lclangTooling\ 32 | -Wl,--end-group 33 | LLVMLIBS=$(shell $(LLVM_CONFIG) --libs) 34 | SYSTEMLIBS=$(shell $(LLVM_CONFIG) --system-libs) 35 | 36 | PROJECT=ast-interpreter 37 | PROJECT_OBJECTS=ASTInterpreter.o 38 | 39 | default: $(PROJECT) 40 | 41 | # rule to build object files 42 | %.o : $(SRC_DIR)/%.cpp 43 | @echo Compiling $*.cpp 44 | $(QUIET)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< 45 | 46 | $(PROJECT) : $(PROJECT_OBJECTS) 47 | @echo Linking $@ 48 | $(QUIET)$(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) $^ $(CLANGLIBS) $(LLVMLIBS) $(SYSTEMLIBS) 49 | 50 | clean:: 51 | $(QUIET)rm -f $(PROJECT) $(PROJECT_OBJECTS) 52 | -------------------------------------------------------------------------------- /ast-interpreter/README.md: -------------------------------------------------------------------------------- 1 | ## AST Interpreter 2 | 3 | Toy code, an AST interpreter, based on Calng & LLVM, that only support integer type. 4 | It supports basic modules and array, e.g. ifstmt, whilestmt, function call, malloc and array. 5 | It can run normally for the following test cases: test-if.c test-while.c test-fn.c test-malloc.c and test-array.c that are in the test folder. 6 | Makefile is self-written, so the build step is easy, by following commands: 7 | 8 | ``` 9 | $ make 10 | $ ./ast-interpreter "`cat ./test/test_file_name`" 11 | ``` 12 | 13 | If you want to rebuild it, you can take use of `make clean` in the command line. 14 | 15 | For more info, see [here...](https://github.com/lijiansong/Postgraduate-Course/tree/master/Compilation/Clang%20%26%20LLVM%20Tutorial/hw1) 16 | 17 | ## Notice 18 | - Clang / LLVM version 4.0.0; 19 | 20 | - Time limit, deref and struct haven't finished, I will complete it later... 21 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test0.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int main() { 7 | int a; 8 | a=100; 9 | PRINT(a); 10 | } 11 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test1.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b=10; 7 | int main() { 8 | PRINT(b); 9 | } 10 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test1.txt: -------------------------------------------------------------------------------- 1 | TranslationUnitDecl 0xa413a10 <> 2 | |-TypedefDecl 0xa413f58 <> implicit __int128_t '__int128' 3 | | `-BuiltinType 0xa413c80 '__int128' 4 | |-TypedefDecl 0xa413fb8 <> implicit __uint128_t 'unsigned __int128' 5 | | `-BuiltinType 0xa413ca0 'unsigned __int128' 6 | |-TypedefDecl 0xa414288 <> implicit __NSConstantString 'struct __NSConstantString_tag' 7 | | `-RecordType 0xa414090 'struct __NSConstantString_tag' 8 | | `-Record 0xa414008 '__NSConstantString_tag' 9 | |-TypedefDecl 0xa414318 <> implicit __builtin_ms_va_list 'char *' 10 | | `-PointerType 0xa4142e0 'char *' 11 | | `-BuiltinType 0xa413aa0 'char' 12 | |-TypedefDecl 0xa4145d8 <> implicit __builtin_va_list 'struct __va_list_tag [1]' 13 | | `-ConstantArrayType 0xa414580 'struct __va_list_tag [1]' 1 14 | | `-RecordType 0xa4143f0 'struct __va_list_tag' 15 | | `-Record 0xa414368 '__va_list_tag' 16 | |-FunctionDecl 0xa414678 col:12 GET 'int ()' extern 17 | |-FunctionDecl 0xa469500 col:13 MALLOC 'long (int)' extern 18 | | `-ParmVarDecl 0xa469440 col:23 'int' 19 | |-FunctionDecl 0xa469680 col:13 FREE 'void (long)' extern 20 | | `-ParmVarDecl 0xa4695c0 col:22 'long' 21 | |-FunctionDecl 0xa469800 col:13 used PRINT 'void (int)' extern 22 | | `-ParmVarDecl 0xa469740 col:22 'int' 23 | |-VarDecl 0xa4698c0 col:5 used b 'int' cinit 24 | | `-IntegerLiteral 0xa469920 'int' 10 25 | `-FunctionDecl 0xa469960 line:7:5 main 'int ()' 26 | `-CompoundStmt 0xa469ae8 27 | `-CallExpr 0xa469aa0 'void' 28 | |-ImplicitCastExpr 0xa469a88 'void (*)(int)' 29 | | `-DeclRefExpr 0xa469a08 'void (int)' Function 0xa469800 'PRINT' 'void (int)' 30 | `-ImplicitCastExpr 0xa469ad0 'int' 31 | `-DeclRefExpr 0xa469a30 'int' lvalue Var 0xa4698c0 'b' 'int' 32 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test10.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int main() { 7 | int a; 8 | int b; 9 | a = 0; 10 | while (a < 10) { 11 | a = a + 1; 12 | } 13 | if (a > 10) b = 10; 14 | else b = 20; 15 | PRINT(b); 16 | } 17 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test11.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b; 7 | int main() { 8 | int a; 9 | a = 0; 10 | while (a < 10) { 11 | a = a + 1; 12 | } 13 | if (a > 10) 14 | b = 10; 15 | else 16 | b = 20; 17 | PRINT(b); 18 | } 19 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test12.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b; 7 | int main() { 8 | int a; 9 | a = 0; 10 | while (a < 10) { 11 | a = a + 1; 12 | } 13 | if (a > 10) 14 | b = a+10; 15 | else 16 | b = a+20; 17 | PRINT(b); 18 | } 19 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test13.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int f(int x) { 7 | return x + 10; 8 | } 9 | int main() { 10 | int a; 11 | int b; 12 | a = 0; 13 | b = f(a); 14 | PRINT(b); 15 | } 16 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test14.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int f(int x) { 7 | if (x> 0) return x; 8 | return -x; 9 | } 10 | int main() { 11 | int a; 12 | int b; 13 | a = -10; 14 | b = f(a); 15 | PRINT(b); 16 | } 17 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test15.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b; 7 | int f(int x) { 8 | return x + 10; 9 | } 10 | int main() { 11 | int a; 12 | int b; 13 | a = -10; 14 | if (a > 0) 15 | b = f(a); 16 | else 17 | b = f(-a); 18 | PRINT(b); 19 | } 20 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test16.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b; 7 | int f(int x) { 8 | return x + 10; 9 | } 10 | int main() { 11 | int a; 12 | int b; 13 | a = -10; 14 | if (a > 0) 15 | b = f(a); 16 | else 17 | b = f(-a); 18 | PRINT(b); 19 | } 20 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test17.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | 7 | int main() { 8 | int* a; 9 | a = MALLOC(sizeof(int)); 10 | *a = 10; 11 | PRINT(*a); 12 | FREE(a); 13 | } 14 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test18.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | 7 | int main() { 8 | int* a; 9 | a = MALLOC(sizeof(int)*2); 10 | *a = 10; 11 | *(a+1) = 20; 12 | PRINT(*a); 13 | PRINT(*(a+1)); 14 | FREE(a); 15 | } 16 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test19.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | 7 | int main() { 8 | int* a; 9 | a = MALLOC(sizeof(int)*2); 10 | FREE(a); 11 | a = MALLOC(sizeof(int)*2); 12 | *a = 0; 13 | PRINT(a); 14 | FREE(a); 15 | } 16 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test2.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b; 7 | int main() { 8 | int a; 9 | a = 10; 10 | b = 10; 11 | PRINT(a+b); 12 | } 13 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test20.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | 7 | int main() { 8 | int* a; 9 | int b; 10 | b = 10; 11 | 12 | a = MALLOC(sizeof(int)); 13 | *a = b; 14 | PRINT(*a); 15 | FREE(a); 16 | } 17 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test21.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | 7 | int main() { 8 | int* a; 9 | int b; 10 | b = 10; 11 | 12 | a = MALLOC(sizeof(int) * b); 13 | *(a+5) = b; 14 | PRINT(*(a+5)); 15 | FREE(a); 16 | } 17 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test22.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | 7 | int * f(int b) { 8 | int *a = MALLOC(sizeof(int)); 9 | *a = b; 10 | return a; 11 | } 12 | 13 | int main() { 14 | int* a; 15 | 16 | a = f(b); 17 | 18 | PRINT(*a); 19 | FREE(a); 20 | } 21 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test23.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | 7 | int f(int b) { 8 | return b+1; 9 | } 10 | 11 | int main() { 12 | int a; 13 | a = 10; 14 | 15 | a = f(a); 16 | } 17 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test24.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | 7 | int f(int b) { 8 | return b+1; 9 | } 10 | 11 | int main() { 12 | int a; 13 | a = 10; 14 | 15 | while(a == 10) { 16 | a = f(a) + 1; 17 | } 18 | 19 | PRINT(a); 20 | } 21 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test3.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b; 7 | int main() { 8 | int a; 9 | a = 10; 10 | b = 10; 11 | PRINT(a*b); 12 | } 13 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test4.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b = 10; 7 | int main() { 8 | int a; 9 | a = 10; 10 | if (a == 10) { 11 | b = 20; 12 | } 13 | PRINT(b); 14 | } 15 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test5.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b = 10; 7 | int main() { 8 | int a; 9 | a = 10; 10 | if (a == 10) { 11 | b = 20; 12 | } else { 13 | b = 0; 14 | } 15 | PRINT(a*b); 16 | } 17 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test6.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b = 10; 7 | int main() { 8 | int a; 9 | a = -10; 10 | if (a > 0 ) { 11 | b = a; 12 | } else { 13 | b = -a; 14 | } 15 | print(b); 16 | } 17 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test7.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b = 10; 7 | int main() { 8 | int a; 9 | a = 10; 10 | b = a + 1; 11 | PRINT(b); 12 | } 13 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test8.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int b = 10; 7 | int main() { 8 | int a; 9 | a = 10; 10 | if (a > 5 ) { 11 | b = a+5; 12 | } else { 13 | b = a; 14 | } 15 | PRINT(b); 16 | } 17 | -------------------------------------------------------------------------------- /ast-interpreter/test-cases/test9.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(long); 4 | extern void PRINT(int); 5 | 6 | int main() { 7 | int a; 8 | a = 0; 9 | while (a < 10) { 10 | a = a + 1; 11 | } 12 | PRINT(a); 13 | } 14 | -------------------------------------------------------------------------------- /ast-interpreter/test/test-array.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern int MALLOC(); 3 | extern void FREE(int); 4 | extern void PRINT(int); 5 | 6 | int main() { 7 | int a[2]; 8 | int i; 9 | 10 | i = 0; 11 | a[0] = GET(); 12 | a[1] = GET(); 13 | PRINT(a[1]); 14 | PRINT(a[0]); 15 | } 16 | -------------------------------------------------------------------------------- /ast-interpreter/test/test-deref.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern int MALLOC(); 3 | extern void FREE(int); 4 | extern void PRINT(int); 5 | 6 | int main() { 7 | int a; 8 | int *p = &a; 9 | a = GET(); 10 | PRINT(*p); 11 | } 12 | -------------------------------------------------------------------------------- /ast-interpreter/test/test-fn.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern int MALLOC(); 3 | extern void FREE(int); 4 | extern void PRINT(int); 5 | 6 | int f(int a) { 7 | int b = a + 10; 8 | return b; 9 | } 10 | int main() { 11 | int a; 12 | a = GET(); 13 | PRINT(f(a)); 14 | } 15 | -------------------------------------------------------------------------------- /ast-interpreter/test/test-if.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern int MALLOC(); 3 | extern void FREE(int); 4 | extern void PRINT(int); 5 | 6 | int main() { 7 | int a; 8 | a = GET(); 9 | if (a < 0) { 10 | PRINT(-a); 11 | } 12 | else { 13 | PRINT(a); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /ast-interpreter/test/test-if.dot: -------------------------------------------------------------------------------- 1 | digraph unnamed { 2 | 3 | Node0xb3ceca8 [shape=record,label="{CompoundStmt}"]; 4 | Node0xb3ceca8 -> Node0xb3ce900; 5 | Node0xb3ceca8 -> Node0xb3ce9d8; 6 | Node0xb3ceca8 -> Node0xb3cec70; 7 | Node0xb3ce900 [shape=record,label="{DeclStmt}"]; 8 | Node0xb3ce9d8 [shape=record,label="{BinaryOperator}"]; 9 | Node0xb3ce9d8 -> Node0xb3ce918; 10 | Node0xb3ce9d8 -> Node0xb3ce9b0; 11 | Node0xb3ce918 [shape=record,label="{DeclRefExpr}"]; 12 | Node0xb3ce9b0 [shape=record,label="{CallExpr}"]; 13 | Node0xb3ce9b0 -> Node0xb3ce998; 14 | Node0xb3ce998 [shape=record,label="{ImplicitCastExpr}"]; 15 | Node0xb3ce998 -> Node0xb3ce940; 16 | Node0xb3ce940 [shape=record,label="{DeclRefExpr}"]; 17 | Node0xb3cec70 [shape=record,label="{IfStmt}"]; 18 | Node0xb3cec70 -> Node0xb3cea60; 19 | Node0xb3cec70 -> Node0xb3ceb80; 20 | Node0xb3cec70 -> Node0xb3cec50; 21 | Node0x0 [shape=record,label="{\}"]; 22 | Node0xb3cea60 [shape=record,label="{BinaryOperator}"]; 23 | Node0xb3cea60 -> Node0xb3cea48; 24 | Node0xb3cea60 -> Node0xb3cea28; 25 | Node0xb3cea48 [shape=record,label="{ImplicitCastExpr}"]; 26 | Node0xb3cea48 -> Node0xb3cea00; 27 | Node0xb3cea00 [shape=record,label="{DeclRefExpr}"]; 28 | Node0xb3cea28 [shape=record,label="{IntegerLiteral}"]; 29 | Node0xb3ceb80 [shape=record,label="{CompoundStmt}"]; 30 | Node0xb3ceb80 -> Node0xb3ceb50; 31 | Node0xb3ceb50 [shape=record,label="{CallExpr}"]; 32 | Node0xb3ceb50 -> Node0xb3ceb38; 33 | Node0xb3ceb50 -> Node0xb3ceaf0; 34 | Node0xb3ceb38 [shape=record,label="{ImplicitCastExpr}"]; 35 | Node0xb3ceb38 -> Node0xb3cea88; 36 | Node0xb3cea88 [shape=record,label="{DeclRefExpr}"]; 37 | Node0xb3ceaf0 [shape=record,label="{UnaryOperator}"]; 38 | Node0xb3ceaf0 -> Node0xb3cead8; 39 | Node0xb3cead8 [shape=record,label="{ImplicitCastExpr}"]; 40 | Node0xb3cead8 -> Node0xb3ceab0; 41 | Node0xb3ceab0 [shape=record,label="{DeclRefExpr}"]; 42 | Node0xb3cec50 [shape=record,label="{CompoundStmt}"]; 43 | Node0xb3cec50 -> Node0xb3cec08; 44 | Node0xb3cec08 [shape=record,label="{CallExpr}"]; 45 | Node0xb3cec08 -> Node0xb3cebf0; 46 | Node0xb3cec08 -> Node0xb3cec38; 47 | Node0xb3cebf0 [shape=record,label="{ImplicitCastExpr}"]; 48 | Node0xb3cebf0 -> Node0xb3ceba0; 49 | Node0xb3ceba0 [shape=record,label="{DeclRefExpr}"]; 50 | Node0xb3cec38 [shape=record,label="{ImplicitCastExpr}"]; 51 | Node0xb3cec38 -> Node0xb3cebc8; 52 | Node0xb3cebc8 [shape=record,label="{DeclRefExpr}"]; 53 | } 54 | -------------------------------------------------------------------------------- /ast-interpreter/test/test-malloc.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern long MALLOC(int); 3 | extern void FREE(int *); 4 | extern void PRINT(int); 5 | 6 | int main() { 7 | int a; 8 | int b; 9 | int *ptr; 10 | int **dptr; 11 | 12 | ptr = (int *)MALLOC(sizeof(int)); 13 | dptr = (int **)MALLOC(sizeof(int *)); 14 | *dptr = ptr; 15 | a = GET(); 16 | *ptr = a; 17 | b = **dptr; 18 | PRINT(b); 19 | FREE((int *)ptr); 20 | FREE((int *)dptr); 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /ast-interpreter/test/test-struct.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern int MALLOC(); 3 | extern void FREE(int); 4 | extern void PRINT(int); 5 | 6 | struct TY{ 7 | int a; 8 | int b; 9 | }; 10 | int main() { 11 | struct TY t; 12 | 13 | t.a = GET(); 14 | t.b = GET(); 15 | PRINT(t.a); 16 | PRINT(t.b); 17 | } 18 | -------------------------------------------------------------------------------- /ast-interpreter/test/test-while.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern int MALLOC(); 3 | extern void FREE(int); 4 | extern void PRINT(int); 5 | 6 | int main() { 7 | int a; 8 | a = GET(); 9 | while (a != 0) { 10 | if (a < 0) { 11 | PRINT(-a); 12 | } 13 | else { 14 | PRINT(a); 15 | } 16 | a = GET(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ast-interpreter/test/test.c: -------------------------------------------------------------------------------- 1 | extern int GET(); 2 | extern int MALLOC(); 3 | extern void FREE(int); 4 | extern void PRINT(int); 5 | 6 | int main() { 7 | int a; 8 | a = GET(); 9 | PRINT(a); 10 | if (a > 0) { 11 | PRINT(a); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /auto-vectorization/README.md: -------------------------------------------------------------------------------- 1 | # Autovectorization 2 | 3 | ## TODO 4 | 5 | - [ ] Implement some examples. 6 | 7 | ## REF 8 | - LLVM official page: 9 | - Autovectorization with LLVM: 10 | - Auto-Vectorization in LLVM, checkout this slide: 11 | - An Automatic Superword Vectorization in LLVM, checkout this paper: 12 | - Benchmarking LLVM's Automatic Loop Vectorizer: 13 | - Using pragmas to control auto-vectorization: 14 | - Code Generation and Autovectorization with LLVM, checkout this slide: 15 | - Intel software LLVM Compiler Implementation for Explicit Parallelization and SIMD Vectorization: 16 | - Automatic SIMD Vectorization of Loops: Issues, Energy Efficiency and Performance on Intel Processors, checkout this paper: 17 | - Vectorization examples: 18 | - An Introduction to Scout, a Vectorizing Source-to-Source Transformator, checkout this slide: 19 | - Compiler auto-vectorization: techniques and challenges, checkout this slide: 20 | - Dynamic Trace-Based Analysis of Vectorization Potential of Applications, checkout this paper: 21 | -------------------------------------------------------------------------------- /clang-driver/Clang_Architecture_-_ChristopherGuntli.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/clang-driver/Clang_Architecture_-_ChristopherGuntli.pdf -------------------------------------------------------------------------------- /clang-driver/OneHourCourse.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/clang-driver/OneHourCourse.pptx -------------------------------------------------------------------------------- /clang-driver/clang-driver.md: -------------------------------------------------------------------------------- 1 | ## Clang Driver 2 | - clang driver offloading action, 3 | -------------------------------------------------------------------------------- /clang-dsl/README.md: -------------------------------------------------------------------------------- 1 | ## Clang Domain Specific Language Example 2 | 3 | 4 | -------------------------------------------------------------------------------- /clang-new-type/README.md: -------------------------------------------------------------------------------- 1 | ## Clang New Type 2 | TBD. 3 | Add new type for clang, e.g. NDArray or Tensor 4 | 5 | ## REF 6 | - Extending LLVM: Adding instructions, intrinsics, types, etc. 7 | - A Journey of OpenCL 2.0 Development in Clang, 8 | - Tutorial for building tools using LibTooling and LibASTMatchers: 9 | - Clang multiple Array / Vector or LLVM Array impl. 10 | -------------------------------------------------------------------------------- /clang-offload-bundler/ibm_openmp_offloading.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/clang-offload-bundler/ibm_openmp_offloading.pdf -------------------------------------------------------------------------------- /clang-offload-bundler/img/bind-arch.xml: -------------------------------------------------------------------------------- 1 | 7VxNd6M2FP01XoYDSHwtJ27SLtpz5pws2lnKIBsajCjIidNfX2EkbEC2mQYJfySLGXjCQui+qyddPZiB+Xr7a4Hy+A8S4XRmm9F2Bn6Z2bYPIPu3MnzUBtfxa8OqSKLaZO0NL8m/mBtNbt0kES5bF1JCUprkbWNIsgyHtGVDRUHe25ctSdq+a45WuGd4CVHat/6ZRDTmj2V7e/tvOFnF4s6WG9QlCxS+rgqyyfj9ZjZY7v7q4jUSdfEHLWMUkfcDE3iagXlBCK2P1ts5TquuFd1W/+75SGnT7gJndMgPbN4M+iEeHUesJ/gpKWhMViRD6dPe+rh7PFxVYLKzmK5TdmixQ7xN6F+V2XD42Q9RktHi46CoOv3BK/gbU/rBXQBtKGGm/X1/JyTndZS0IK8NHKynHpdJms5JSopdy1lHYzcMmysPSiIvWJjVveqnrR6x1V8l2RQhNwHubqhYYd6Fsm61GrAYBzBZY/ZE7JICp4gmb+3qEffGVXPdHhF2wEGRA8Sb84bSDRYu5aa0enrC2nIInfvPhoiCh3LXod/YBZafb/eF7GhV/W8aSZZvqKiMNaOury7tecUe8wqK9zih+CVHuz57ZyNA2w+O4YLSZJUxW8h6ERcDgHrDBcXbo659BAP+A8gp9iE4ysn7vie0JWgYH5DZNT+PGrxmWg0giSMhCZiOJM7nSeLKSAKNvMB5QUJclsxFdXDFD/FArix8BzoV0qsCRQm77KAMI3fhuuPwqAnKnEisswxHH5XcE/278/PNOhfEWnV79yYikichmzcYxdHJ5imKSLYRknWepFgP0S4iKNm+xqDkn2HSZYccMQod0sCyp6NBMHZvuqDVn4bJJizc8B0XCWtg5Z4TdTScrqNFe0YfcFyjWq1htlT7Cuy7pYYf6BuMLKvXvUqmyGqpYktCszMhVWxFVPEMVJZ4vdAVnC+QLH6bLDDQSRZw3aEbSiKKOyFNoCKaBAZZLlOC1EeUjGR4ED+evrmPOw60kQPjsMJyYIsVjtYQ4lw3K1xJ8JhwXSfaMzorLNNIk+z1fkNHZ57lag0dypbrprFIsugBFWF8wwt2OwiE2tUIyaIWLfj1V+23JSVbQX8YnHIKHSjiC9C44XL5Y6DW3Zhms/UqOaREU2521Q9YN+HcQ9Qx+g6OpX8H5zKEZb2bNLZ9zQwbwhdJWoAY0iYhjKq8AEfvLszlRyqtWzSSxIGrYokkL2BKoWeMxAApS/x7E3q68qdWoUeSA3BF0UXN/E2WFBBMyDRVMoNlaaPaPldzCN+en5/c+VwV34RExOnmCfppodtVSw5q6CYRKcCEWaG2KpXCsrVuik+zVuqQy3c1kgv0tYjbpg6QKA1wwlxRMILUIKcO0LxJfhHkCaBGmQFctcyghl4SYQJMOBEEIwgTjpRezr1IeY7IjWq0PKBTywMXrEGooZBEtZg0QqlSLSz/LiJUlz9aU7XAl1bR45dEqwBT8kuZVgFv/qW6LrcsSye3voSJHrckwgSc8nVVZcKEew+vB/WmfjpldviVJtGlF5SIF2DC7CSoTLzw7kD367JL6wsv8Eu76LFLllQxYRYSVJVUYQU6M6AvJlHWBDo/uaDqpQ4GW4bThxClqRFuRJ2LYg8etwhDTEr6EG4idID2ont1zwP61Z4UYcbyiQiVceNo1cl3RFlJtrOwLlHtNp28NahxKwaOoLMEMo9BRpjnt+Yox73gjAvZ4zhKT9XR6SmqXo356bElwm9JiC/OaQZnTZi7P21O003m0+o0qmSmCvyHJaKLJPv5+cQAdM9AAzsOcDpDTUwirXEABZ144ZkDp/BwDET9TyNqWzJEq0AwFErWpCQv8XkYjw/YfWQGRwEFA7kP7KHbW/4YIH5eoZKDWA/N6mFsEqHPw3g0u/pUDnXVbL5GtEdibRfywPZ0Qu6cFrb4APa/PivlnVh+e6319+c+RiEHpbe67kFyPj+7to2/uman+89Y7soOPhUKnv4D -------------------------------------------------------------------------------- /clang-offload-bundler/img/export.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/clang-offload-bundler/img/export.png -------------------------------------------------------------------------------- /clang-offload-bundler/img/openmp-offload-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/clang-offload-bundler/img/openmp-offload-example.png -------------------------------------------------------------------------------- /clang-offload-bundler/openmp-offloading-support.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/clang-offload-bundler/openmp-offloading-support.pdf -------------------------------------------------------------------------------- /cmu-15-745/README.md: -------------------------------------------------------------------------------- 1 | # CMU CS 15-745, Optimizing Compilers 2 | 3 | Course slides, checkout this [page](http://www.cs.cmu.edu/afs/cs.cmu.edu/academic/class/15745-s15/public/lectures/): 4 | -------------------------------------------------------------------------------- /cmu-15-745/asst1/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/cmu-15-745/asst1/.gitignore -------------------------------------------------------------------------------- /cmu-15-745/asst2/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/cmu-15-745/asst2/.gitignore -------------------------------------------------------------------------------- /cmu-15-745/asst3/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/cmu-15-745/asst3/.gitignore -------------------------------------------------------------------------------- /code-pointer-integrity/Code-Pointer-Integrity.md: -------------------------------------------------------------------------------- 1 | # Code-Pointer Integrity 2 | 3 | - http://dslab.epfl.ch/proj/cpi/ 4 | -------------------------------------------------------------------------------- /code-pointer-integrity/tbaa/Gohman-AliasAnalysis.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/code-pointer-integrity/tbaa/Gohman-AliasAnalysis.pdf -------------------------------------------------------------------------------- /code-pointer-integrity/tbaa/examples/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | clang -cc1 -O1 -no-struct-path-tbaa -disable-llvm-optzns test.c -emit-llvm -o - 2>&1 | tee test.log 4 | -------------------------------------------------------------------------------- /code-pointer-integrity/tbaa/examples/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct test_t { 5 | int (*add)(int a, int b); 6 | int (*sub)(int a, int b); 7 | int (*mult)(int a, int b); 8 | }; 9 | 10 | static int test_add(int a, int b) { return (a + b); } 11 | 12 | static int test_sub(int a, int b) { return (a - b); } 13 | 14 | static int test_mult(int a, int b) { return (a * b); } 15 | 16 | void print_usage() { printf("./test \n"); } 17 | 18 | int main() { 19 | print_usage(); 20 | struct test_t test_var; 21 | test_var.add = test_add; 22 | test_var.sub = test_sub; 23 | test_var.mult = test_mult; 24 | 25 | int add_rst; 26 | int sub_rst; 27 | int mult_rst; 28 | add_rst = test_var.add(5, 6); 29 | sub_rst = test_var.sub(5, 6); 30 | mult_rst = test_var.mult(5, 6); 31 | printf("add_rst: %d; sub_rst: %d; mult_rst: %d\n", add_rst, sub_rst, 32 | mult_rst); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /cpp-insights/README.md: -------------------------------------------------------------------------------- 1 | # C++ Insights - See your source code with the eyes of a compiler 2 | 3 | C++ Insights is a clang-based tool which does a source to source transformation. Its goal is it to make things visible which normally, and intentionally, happen behind the scenes. It's about the magic the compiler does for us to make things work. 4 | 5 | More details, see here: and 6 | -------------------------------------------------------------------------------- /cpp-insights/afertig-ndcolo-2017-fast-and-small.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/cpp-insights/afertig-ndcolo-2017-fast-and-small.pdf -------------------------------------------------------------------------------- /cpp-insights/foo/lambda-insights.cpp: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | int x = 1; 4 | 5 | class __lambda_3_18 6 | { 7 | public: 8 | inline /*constexpr */ void operator()() const 9 | { 10 | ++x; 11 | } 12 | 13 | private: 14 | int & x; 15 | 16 | public: 17 | __lambda_3_18(int & _x) 18 | : x{_x} 19 | {} 20 | 21 | }; 22 | 23 | __lambda_3_18 lambda = __lambda_3_18{x}; 24 | lambda.operator()(); 25 | return x; 26 | } 27 | -------------------------------------------------------------------------------- /cpp-insights/foo/lambda.cpp: -------------------------------------------------------------------------------- 1 | int main() { 2 | int x = 1; 3 | auto lambda = [&]() { ++x; }; 4 | lambda(); 5 | return x; 6 | } 7 | -------------------------------------------------------------------------------- /cpp-insights/foo/structure-binding-insights.cpp: -------------------------------------------------------------------------------- 1 | int main() 2 | { 3 | struct point 4 | { 5 | int x; 6 | int y; 7 | // inline constexpr point(const point &) noexcept = default; 8 | }; 9 | 10 | point pt = {1, 2}; 11 | point __pt10 = point(pt); 12 | int& x = __pt10.x; 13 | int& y = __pt10.y; 14 | } 15 | -------------------------------------------------------------------------------- /cpp-insights/foo/structure-binding.cpp: -------------------------------------------------------------------------------- 1 | int main() { 2 | struct point { 3 | int x; 4 | int y; 5 | }; 6 | point pt{1, 2}; 7 | auto[x, y] = pt; 8 | } 9 | -------------------------------------------------------------------------------- /dataflow-analysis/README.md: -------------------------------------------------------------------------------- 1 | # dataflow analysis 2 | todo: complete it later 3 | 4 | ## Reference 5 | [1] [UCSD-CSE 231: Advanced Compilers](http://cseweb.ucsd.edu/classes/sp11/cse231-a/)
6 | [2] [Sorin Lerner's Homepage](http://cseweb.ucsd.edu/~lerner/)
7 | [3] [The Cetus Compiler Manual](https://engineering.purdue.edu/Cetus/Documentation/manual/index.html)
8 | -------------------------------------------------------------------------------- /dataflow-analysis/Value-Range Analysis of C Programs.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/dataflow-analysis/Value-Range Analysis of C Programs.pdf -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/Makefile: -------------------------------------------------------------------------------- 1 | LEVEL = .. 2 | 3 | SUBDIRS = welcome gcd hadamard compression branchloop range must-pointer 4 | 5 | include $(LEVEL)/Makefile.subdir 6 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/branchloop/Makefile: -------------------------------------------------------------------------------- 1 | LEVEL = ../.. 2 | 3 | OBJ = branchloop.bc 4 | 5 | include $(LEVEL)/Makefile.common 6 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/branchloop/branchloop.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/dataflow-analysis/extra/benchmarks/branchloop/branchloop.bc -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/branchloop/branchloop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int i = 1; 5 | int j = 1; 6 | if (i != 5) 7 | i = 0; 8 | while(i == 0) { 9 | while(i==0) { 10 | i = i+1; 11 | } 12 | } 13 | return i; 14 | } 15 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/branchloop/branchloop.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'branchloop.bc' 2 | target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32-n8:16:32-S128" 3 | target triple = "i386-pc-linux-gnu" 4 | 5 | ; Function Attrs: nounwind 6 | define i32 @main() #0 { 7 | entry: 8 | %retval = alloca i32, align 4 9 | %i = alloca i32, align 4 10 | %j = alloca i32, align 4 11 | store i32 0, i32* %retval 12 | store i32 1, i32* %i, align 4 13 | store i32 1, i32* %j, align 4 14 | %0 = load i32* %i, align 4 15 | %cmp = icmp ne i32 %0, 5 16 | br i1 %cmp, label %if.then, label %if.end 17 | 18 | if.then: ; preds = %entry 19 | store i32 0, i32* %i, align 4 20 | br label %if.end 21 | 22 | if.end: ; preds = %if.then, %entry 23 | br label %while.cond 24 | 25 | while.cond: ; preds = %while.end, %if.end 26 | %1 = load i32* %i, align 4 27 | %cmp1 = icmp eq i32 %1, 0 28 | br i1 %cmp1, label %while.body, label %while.end5 29 | 30 | while.body: ; preds = %while.cond 31 | br label %while.cond2 32 | 33 | while.cond2: ; preds = %while.body4, %while.body 34 | %2 = load i32* %i, align 4 35 | %cmp3 = icmp eq i32 %2, 0 36 | br i1 %cmp3, label %while.body4, label %while.end 37 | 38 | while.body4: ; preds = %while.cond2 39 | %3 = load i32* %i, align 4 40 | %add = add nsw i32 %3, 1 41 | store i32 %add, i32* %i, align 4 42 | br label %while.cond2 43 | 44 | while.end: ; preds = %while.cond2 45 | br label %while.cond 46 | 47 | while.end5: ; preds = %while.cond 48 | %4 = load i32* %i, align 4 49 | ret i32 %4 50 | } 51 | 52 | attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } 53 | 54 | !llvm.ident = !{!0} 55 | 56 | !0 = metadata !{metadata !"clang version 3.4 (tags/RELEASE_34/final)"} 57 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/expression/expression.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/dataflow-analysis/extra/benchmarks/expression/expression.bc -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/expression/expression.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | int main(void) 4 | { 5 | int i = 5; 6 | int j = 5 + i; 7 | j = i + 5; 8 | int k = i + 5; 9 | int l = 5 + i; 10 | int m = 6 + i; 11 | int n = 7 - i; 12 | int o = i - 7; 13 | int p = 10 * i; 14 | int q = i * 10; 15 | int r = i / 10; 16 | int s = 10 / i; 17 | int t = i % 10; 18 | int u = 10 % i; 19 | 20 | if (i == 10) { 21 | j = 5; 22 | } 23 | else { 24 | j = 10; 25 | } 26 | 27 | if (i == 5) { 28 | j = 5; 29 | } 30 | else { 31 | j = 5; 32 | } 33 | 34 | while (i < 10) { 35 | if (i == 10) { 36 | k = 15; 37 | } 38 | else { 39 | m = 10; 40 | } 41 | } 42 | 43 | return i; 44 | } 45 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/must-pointer/Makefile: -------------------------------------------------------------------------------- 1 | LEVEL = ../.. 2 | 3 | OBJ = must-pointer.bc 4 | 5 | include $(LEVEL)/Makefile.common 6 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/must-pointer/must-pointer.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | int main(void) { 4 | int *i; 5 | int j; 6 | int *k; 7 | 8 | if (i != &j) { 9 | k = i + 4; 10 | } 11 | else { 12 | k = i + 8; 13 | } 14 | 15 | 16 | //int j = 0; 17 | /* 18 | i = &j; 19 | *i = j; 20 | */ 21 | /* 22 | int * j = &i; 23 | int * k; 24 | 25 | j = k; 26 | */ 27 | return *i; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/must-pointer/must-pointer.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'must-pointer.bc' 2 | target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" 3 | target triple = "x86_64-unknown-linux-gnu" 4 | 5 | ; Function Attrs: nounwind uwtable 6 | define i32 @main() #0 { 7 | entry: 8 | %retval = alloca i32, align 4 9 | %i = alloca i32*, align 8 10 | %j = alloca i32, align 4 11 | %k = alloca i32*, align 8 12 | store i32 0, i32* %retval 13 | %0 = load i32** %i, align 8 14 | %cmp = icmp ne i32* %0, %j 15 | br i1 %cmp, label %if.then, label %if.else 16 | 17 | if.then: ; preds = %entry 18 | %1 = load i32** %i, align 8 19 | %add.ptr = getelementptr inbounds i32* %1, i64 4 20 | store i32* %add.ptr, i32** %k, align 8 21 | br label %if.end 22 | 23 | if.else: ; preds = %entry 24 | %2 = load i32** %i, align 8 25 | %add.ptr1 = getelementptr inbounds i32* %2, i64 8 26 | store i32* %add.ptr1, i32** %k, align 8 27 | br label %if.end 28 | 29 | if.end: ; preds = %if.else, %if.then 30 | %3 = load i32** %i, align 8 31 | %4 = load i32* %3, align 4 32 | ret i32 %4 33 | } 34 | 35 | attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } 36 | 37 | !llvm.ident = !{!0} 38 | 39 | !0 = metadata !{metadata !"clang version 3.4 (tags/RELEASE_34/final)"} 40 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/pointer/Makefile: -------------------------------------------------------------------------------- 1 | LEVEL = ../.. 2 | 3 | OBJ = pointer.bc 4 | 5 | include $(LEVEL)/Makefile.common 6 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/pointer/pointer.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/dataflow-analysis/extra/benchmarks/pointer/pointer.bc -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/pointer/pointer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int * x; 5 | int * z; 6 | int * a; 7 | int y = 5; 8 | x = &y; 9 | z = x; 10 | if (*x <= 5) 11 | a = x; 12 | else 13 | a = z; 14 | while(*x > y) { 15 | while(*x < 0) { 16 | *x = y+1; 17 | } 18 | } 19 | return *x; 20 | } 21 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/range/Makefile: -------------------------------------------------------------------------------- 1 | LEVEL = ../.. 2 | 3 | OBJ = range.bc 4 | 5 | include $(LEVEL)/Makefile.common 6 | -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/range/range.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/dataflow-analysis/extra/benchmarks/range/range.bc -------------------------------------------------------------------------------- /dataflow-analysis/extra/benchmarks/range/range.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int i = 1; 5 | int j = i + 33; 6 | if (i <= 5) 7 | i = 0; 8 | while(i > j) { 9 | while(i < 0) { 10 | i = i+1; 11 | } 12 | } 13 | return i; 14 | } 15 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/AnalysisPass.cpp: -------------------------------------------------------------------------------- 1 | //===- test.cpp - ---------------===// 2 | // 3 | // Sample pass to understand basic block behavior 4 | // 5 | // 6 | //===----------------------------------------------------------------------===// 7 | 8 | #define DEBUG_TYPE "AvailableExpr" 9 | #include "llvm/IR/Module.h" 10 | #include "llvm/IR/Function.h" 11 | #include "llvm/IR/Instruction.h" 12 | #include "llvm/IR/BasicBlock.h" 13 | #include "llvm/IR/DerivedTypes.h" 14 | #include "llvm/IR/IRBuilder.h" 15 | #include "llvm/IR/LLVMContext.h" 16 | #include "llvm/IR/User.h" 17 | #include "llvm/Pass.h" 18 | #include "llvm/Support/raw_ostream.h" 19 | #include "llvm/IR/Module.h" 20 | #include "llvm/Support/Format.h" 21 | 22 | #include 23 | #include "llvm/Support/CFG.h" 24 | #include "AnalysisPass.h" 25 | #include "AvailableExprAnalysis.h" 26 | #include "ConstantPropAnalysis.h" 27 | #include "RangeAnalysis.h" 28 | #include "PointerAnalysis.h" 29 | 30 | #include 31 | 32 | using namespace llvm; 33 | using namespace std; 34 | 35 | template<> char AnalysisPass::ID = 0; 36 | template<> char AnalysisPass >::ID = 1; 37 | template<> char AnalysisPass > >::ID = 2; 38 | template<> char AnalysisPass > >::ID = 3; 39 | template<> char AnalysisPass > >::ID = 4; 40 | 41 | static RegisterPass > X("avail_expr", "AvailableExpr Pass"); 42 | static RegisterPass > > Y("block_order","Block Order Pass"); 43 | static RegisterPass > > > Z("RangePass","RangePass"); 44 | static RegisterPass > > > A("must_pointer","Must Pointer Pass"); 45 | static RegisterPass > > > B("may_pointer","May Pointer Pass"); 46 | 47 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/AvailableExprAnalysis.h: -------------------------------------------------------------------------------- 1 | #include "Lattice.h" 2 | #include "ExpressionContainer.h" 3 | #include "llvm/IR/Instructions.h" 4 | #include "llvm/IR/Value.h" 5 | 6 | 7 | using namespace llvm; 8 | using namespace std; 9 | 10 | class AvailableExprAnalysis { 11 | public: 12 | AvailableExprAnalysis(Instruction * inst, Lattice * incoming); 13 | Instruction * getInstruction(); 14 | Lattice * getOutgoingEdge(); 15 | Lattice * getOutgoingEdge(BasicBlock * toSuccessor); 16 | void setIncomingEdge(Lattice * incoming); 17 | void applyFlowFunction(); 18 | void dump(); 19 | static void dump(Lattice * container); 20 | 21 | static Lattice * merge(Lattice * edge_1, Lattice * edge_2); 22 | static bool equal(Lattice * edge1, Lattice * edge2); 23 | private: 24 | Instruction * _instruction; 25 | Lattice * _incomingEdge; 26 | Lattice * _outgoingEdge; 27 | void handleBinaryOp(Instruction * inst); 28 | ConstantInt * tryGetConstantValue(Value * value); 29 | }; 30 | 31 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/ConstantPropAnalysis.h: -------------------------------------------------------------------------------- 1 | #include "Lattice.h" 2 | #include "llvm/IR/Instructions.h" 3 | #include "llvm/IR/Value.h" 4 | 5 | 6 | using namespace llvm; 7 | using namespace std; 8 | 9 | class ConstantPropAnalysis { 10 | public: 11 | ConstantPropAnalysis(Instruction * inst, Lattice< map > * incoming); 12 | Instruction * getInstruction(); 13 | Lattice< map > * getOutgoingEdge(); 14 | Lattice< map > * getOutgoingEdge(BasicBlock * toSuccessor); 15 | void setIncomingEdge(Lattice< map > * incoming); 16 | bool isConditionalBranch(); 17 | void applyFlowFunction(); 18 | void dump(); 19 | static void dump(Lattice > * lattice); 20 | 21 | static Lattice< map > * merge(Lattice< map > * edge_1, Lattice< map > * edge_2); 22 | static bool equal(Lattice< map > * edge1, Lattice< map > * edge2); 23 | 24 | private: 25 | Instruction * _instruction; 26 | Lattice< map > * _incomingEdge; 27 | Lattice< map > * _outgoingEdge; 28 | Lattice< map > * _outgoingTrueEdge; 29 | Lattice< map > * _outgoingFalseEdge; 30 | bool _isConditionalBranch; 31 | void handleStoreInst(StoreInst * storeInst); 32 | void handleLoadInst(LoadInst * loadInst); 33 | void handleBinaryOp(Instruction * inst); 34 | void handleConditionalBranchInst(BranchInst * inst); 35 | ConstantInt * tryGetConstantValue(Value * value); 36 | }; 37 | 38 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/Expression.h: -------------------------------------------------------------------------------- 1 | #include "llvm/IR/Instruction.h" 2 | #include "llvm/IR/Value.h" 3 | 4 | using namespace llvm; 5 | using namespace std; 6 | 7 | class Expression { 8 | public: 9 | Expression(Instruction * instruction); 10 | Value * getOperand1(); 11 | Value * getOperand2(); 12 | string operand1Name(); 13 | string operand2Name(); 14 | bool isEqualTo(Expression * expression); 15 | 16 | void dump(); 17 | 18 | bool isEqual(Expression * expression); 19 | 20 | private: 21 | string getNameOf(Value * value); 22 | 23 | Instruction * _instruction; 24 | }; 25 | 26 | /* 27 | class UnaryExpression : public Expression { 28 | public: 29 | UnaryExpression(Instruction * instruction); 30 | 31 | Value * getOperand(); 32 | void dump(); 33 | }; 34 | 35 | class BinaryExpression : public Expression { 36 | public: 37 | BinaryExpression(Instruction * instruction); 38 | 39 | Value * getOperand1(); 40 | Value * getOperand2(); 41 | string operand1Name(); 42 | string operand2Name(); 43 | bool isEqualTo(BinaryExpression * expression); 44 | 45 | void dump(); 46 | }; 47 | */ 48 | 49 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/ExpressionContainer.cpp: -------------------------------------------------------------------------------- 1 | #include "ExpressionContainer.h" 2 | #include "llvm/Support/raw_ostream.h" 3 | 4 | ExpressionContainer::ExpressionContainer() { 5 | } 6 | 7 | void ExpressionContainer::addExpression(string variable, Expression * expression) { 8 | _expressionMap[variable] = expression; 9 | } 10 | 11 | map ExpressionContainer::getMap() { 12 | return _expressionMap; 13 | } 14 | 15 | vector ExpressionContainer::getVariablesFor(Expression * expression) { 16 | vector v; 17 | 18 | for (map::iterator i = _expressionMap.begin(); i != _expressionMap.end(); i++) { 19 | if (expression->isEqual(i->second)) { 20 | v.push_back(i->first); 21 | } 22 | } 23 | 24 | return v; 25 | } 26 | 27 | map,expressionComp> ExpressionContainer::getExpressionMap() { 28 | map,expressionComp> expMap; 29 | 30 | for (map::iterator i = _expressionMap.begin(); i != _expressionMap.end(); i++) { 31 | 32 | expMap[i->second].push_back(i->first); 33 | 34 | /* 35 | for (map::iterator j = _expressionMap.begin(); j != _expressionMap.end(); j++) { 36 | if (i->second->isEqualTo(j->) { 37 | expMap[i->second].push_back(i->first); 38 | } 39 | } 40 | */ 41 | } 42 | 43 | return expMap; 44 | } 45 | 46 | void ExpressionContainer::dump() { 47 | map,expressionComp> expMap = getExpressionMap(); 48 | 49 | for (map,expressionComp>::iterator i = expMap.begin(); i != expMap.end(); i++) { 50 | errs() << "\t\t\t\t"; 51 | i->first->dump(); 52 | errs() << " -> "; 53 | 54 | for (unsigned int j = 0; j < i->second.size(); j++) { 55 | errs() << i->second[j] << ","; 56 | } 57 | 58 | errs() << "\n"; 59 | } 60 | } 61 | 62 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/ExpressionContainer.h: -------------------------------------------------------------------------------- 1 | #include "Expression.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace llvm; 8 | using namespace std; 9 | 10 | struct expressionComp : public binary_function { 11 | bool operator()(Expression * const a, Expression * const b) { 12 | return a->isEqualTo(b); 13 | } 14 | }; 15 | 16 | class ExpressionContainer { 17 | public: 18 | ExpressionContainer(); 19 | 20 | void addExpression(string variable, Expression * expression); 21 | vector getVariablesFor(Expression * expression); 22 | map getMap(); 23 | map,expressionComp> getExpressionMap(); 24 | void dump(); 25 | 26 | private: 27 | map _expressionMap; 28 | }; 29 | 30 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/Lattice.h: -------------------------------------------------------------------------------- 1 | #ifndef _LATTICE_T_H 2 | #define _LATTICE_T_H 3 | 4 | #include "llvm/IR/Instructions.h" 5 | #include "llvm/IR/Constants.h" 6 | #include "llvm/IR/Module.h" 7 | #include "llvm/IR/Function.h" 8 | #include "llvm/IR/Instructions.h" 9 | #include "llvm/Support/InstIterator.h" 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | using namespace llvm; 16 | using namespace std; 17 | 18 | struct valueComp { 19 | bool operator()(Value* const a, Value* const b) { 20 | return a->getName() < b->getName(); 21 | } 22 | }; 23 | 24 | template 25 | class Lattice { 26 | public: 27 | Lattice(); 28 | Lattice(bool isTop, bool isBottom, T facts); 29 | Lattice(Lattice& other); 30 | Lattice& operator=(const Lattice& other); 31 | ~Lattice(); 32 | T getFacts(); 33 | void setNewFacts(bool isTop, bool isBottom, T facts); 34 | bool isTop(); 35 | bool isBottom(); 36 | 37 | private: 38 | T _facts; 39 | bool _isTop; 40 | bool _isBottom; 41 | 42 | }; 43 | 44 | template 45 | Lattice::Lattice() {} 46 | 47 | template 48 | Lattice::Lattice(bool isTop, bool isBottom, T facts) { 49 | // Must Analysis, bottom is Full set, top is empty set 50 | _facts = facts; 51 | _isTop = isTop; 52 | _isBottom = isBottom; 53 | } 54 | 55 | template 56 | Lattice::Lattice(Lattice& other) { 57 | _facts = other._facts; 58 | _isTop = other._isTop; 59 | _isBottom = other._isBottom; 60 | } 61 | 62 | template 63 | Lattice& Lattice::operator=( const Lattice& other ) { 64 | _facts = other._facts; 65 | _isTop = other._isTop; 66 | _isBottom = other._isBottom; 67 | return *this; 68 | } 69 | 70 | template 71 | Lattice::~Lattice() {} 72 | 73 | template 74 | T Lattice::getFacts() { 75 | return _facts; //returns a copy 76 | } 77 | 78 | template 79 | void Lattice::setNewFacts(bool isTop, bool isBottom, T facts) { 80 | _facts = facts; 81 | _isTop = isTop; 82 | _isBottom = isBottom; 83 | } 84 | 85 | template 86 | bool Lattice::isTop() { 87 | return _isTop; 88 | } 89 | 90 | template 91 | bool Lattice::isBottom() { 92 | return _isBottom; 93 | } 94 | 95 | #endif 96 | 97 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/Makefile: -------------------------------------------------------------------------------- 1 | ##===- lib/Transforms/Hello/Makefile -----------------------*- Makefile -*-===## 2 | # 3 | # The LLVM Compiler Infrastructure 4 | # 5 | # This file is distributed under the University of Illinois Open Source 6 | # License. See LICENSE.TXT for details. 7 | # 8 | ##===----------------------------------------------------------------------===## 9 | 10 | LEVEL = ../.. 11 | LIBRARYNAME = CSE231 12 | LOADABLE_MODULE = 1 13 | 14 | include $(LEVEL)/Makefile.common 15 | 16 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/PointerAnalysis.h: -------------------------------------------------------------------------------- 1 | #include "Lattice.h" 2 | #include "llvm/IR/Instructions.h" 3 | #include "llvm/IR/Value.h" 4 | 5 | 6 | using namespace llvm; 7 | using namespace std; 8 | 9 | class PointerAnalysis { 10 | public: 11 | Instruction * getInstruction(); 12 | Lattice< map > > * getOutgoingEdge(); 13 | Lattice< map > > * getOutgoingEdge(BasicBlock * toSuccessor); 14 | void setIncomingEdge(Lattice< map > > * incoming); 15 | bool isConditionalBranch(); 16 | void applyFlowFunction(); 17 | void dump(); 18 | static void dump(Lattice > > * lattice); 19 | 20 | static bool equal(Lattice< map > > * edge1, Lattice< map > > * edge2); 21 | 22 | protected: 23 | PointerAnalysis(Instruction * inst, Lattice< map > > * incoming); 24 | 25 | Instruction * _instruction; 26 | Lattice< map > > * _incomingEdge; 27 | Lattice< map > > * _outgoingEdge; 28 | Lattice< map > > * _outgoingTrueEdge; 29 | Lattice< map > > * _outgoingFalseEdge; 30 | bool _isConditionalBranch; 31 | void handleStoreInst(StoreInst * storeInst); 32 | void handleLoadInst(LoadInst * loadInst); 33 | }; 34 | 35 | class MustPointerAnalysis : public PointerAnalysis { 36 | public: 37 | MustPointerAnalysis(Instruction * inst, Lattice< map > > * incoming); 38 | 39 | static Lattice< map > > * merge(Lattice< map > > * edge_1, Lattice< map > > * edge_2); 40 | }; 41 | 42 | class MayPointerAnalysis : public PointerAnalysis { 43 | public: 44 | MayPointerAnalysis(Instruction * inst, Lattice< map > > * incoming); 45 | 46 | static Lattice< map > > * merge(Lattice< map > > * edge_1, Lattice< map > > * edge_2); 47 | }; 48 | 49 | -------------------------------------------------------------------------------- /dataflow-analysis/llvm/src/lib/CSE231/RangeAnalysis.h: -------------------------------------------------------------------------------- 1 | #include "Lattice.h" 2 | #include "llvm/IR/Instructions.h" 3 | #include "llvm/IR/Value.h" 4 | #include // std::max and min 5 | 6 | 7 | using namespace llvm; 8 | using namespace std; 9 | 10 | class RangeAnalysis { 11 | public: 12 | RangeAnalysis(Instruction * inst, Lattice< map > > * incoming); 13 | Instruction * getInstruction(); 14 | Lattice< map > > * getOutgoingEdge(); 15 | Lattice< map > > * getOutgoingEdge(BasicBlock * toSuccessor); 16 | void setIncomingEdge(Lattice< map > > * incoming); 17 | bool isConditionalBranch(); 18 | void applyFlowFunction(); 19 | void dump(); 20 | static void dump(Lattice > > * lattice); 21 | 22 | static Lattice< map > > * join(Lattice< map > > * edge_1, Lattice< map > > * edge_2); 23 | static Lattice< map > > * merge(Lattice< map > > * edge_1, Lattice< map > > * edge_2); 24 | static bool equal(Lattice< map > > * edge1, Lattice< map > > * edge2); 25 | private: 26 | Instruction * _instruction; 27 | Lattice< map > > * _incomingEdge; 28 | Lattice< map > > * _outgoingEdge; 29 | Lattice< map > > * _outgoingTrueEdge; 30 | Lattice< map > > * _outgoingFalseEdge; 31 | bool _isConditionalBranch; 32 | void handleAllocaInst(AllocaInst * allocaInst); 33 | void handleStoreInst(StoreInst * storeInst); 34 | void handleLoadInst(LoadInst * loadInst); 35 | void handleBinaryOp(Instruction * inst); 36 | void handleConditionalBranchInst(BranchInst * inst); 37 | ConstantInt * tryGetConstantValue(Value * value); 38 | vector tryGetRange(Value * value); 39 | vector intersect(vector left, vector right); 40 | }; 41 | 42 | -------------------------------------------------------------------------------- /dataflow-analysis/report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/dataflow-analysis/report.pdf -------------------------------------------------------------------------------- /dataflow-analysis/runscripts/RangeAnalysis.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | filename=$(basename "$1") 4 | opt -analyze -load $LLVMLIB/CSE231.so -instnamer -RangePass < $1/$filename.bc > /dev/null 5 | -------------------------------------------------------------------------------- /dataflow-analysis/runscripts/avail_expr.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | filename=$(basename "$1") 4 | opt -mem2reg -analyze -load $LLVMLIB/CSE231.so -instnamer -avail_expr < $1/$filename.bc > /dev/null 5 | #opt -analyze -load $LLVMLIB/CSE231.so -instnamer -avail_expr < $1/$filename.bc > /dev/null 6 | 7 | 8 | -------------------------------------------------------------------------------- /dataflow-analysis/runscripts/constantprop.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | filename=$(basename "$1") 4 | opt -analyze -load $LLVMLIB/CSE231.so -instnamer -block_order < $1/$filename.bc > /dev/null 5 | -------------------------------------------------------------------------------- /dataflow-analysis/runscripts/may-pointer.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | filename=$(basename "$1") 4 | opt -analyze -load $LLVMLIB/CSE231.so -instnamer -may_pointer < $1/$filename.bc > /dev/null 5 | -------------------------------------------------------------------------------- /dataflow-analysis/runscripts/must-pointer.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | filename=$(basename "$1") 4 | opt -analyze -load $LLVMLIB/CSE231.so -instnamer -must_pointer < $1/$filename.bc > /dev/null 5 | -------------------------------------------------------------------------------- /directive/README.md: -------------------------------------------------------------------------------- 1 | # Directive Based Auto Vectorization 2 | -------------------------------------------------------------------------------- /function-pointer-analysis/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 Json Lee. All rights reserved. 2 | # For more info, see [here](https://github.com/lijiansong) or my [github page](https://lijiansong.github.io/) 3 | LLVM_CONFIG?=llvm-config 4 | 5 | ifndef VERBOSE 6 | QUIET:=@ 7 | endif 8 | 9 | SRC_DIR?=$(PWD) 10 | LDFLAGS+=$(shell $(LLVM_CONFIG) --ldflags) 11 | #Debug flags 12 | COMMON_FLAGS=-Wall -Wextra -g 13 | CXXFLAGS+=$(COMMON_FLAGS) $(shell $(LLVM_CONFIG) --cxxflags) -fno-rtti 14 | CPPFLAGS+=$(shell $(LLVM_CONFIG) --cppflags) -I$(SRC_DIR) 15 | #Add the static libs that may be linked 16 | LLVMLIBS=$(shell $(LLVM_CONFIG) --libs irreader bitwriter passes core support) 17 | SYSTEMLIBS=$(shell $(LLVM_CONFIG) --system-libs) 18 | 19 | PROJECT=llvm-assignment 20 | PROJECT_OBJECTS=LLVMAssignment.o 21 | 22 | default: $(PROJECT) 23 | 24 | #%.o : $(SRC_DIR)/%.cpp 25 | $(PROJECT_OBJECTS) : $(SRC_DIR)/LLVMAssignment.cpp 26 | @echo Compiling $*.cpp 27 | $(QUIET)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< 28 | 29 | $(PROJECT) : $(PROJECT_OBJECTS) 30 | @echo Linking $@ 31 | $(QUIET)$(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) $^ $(LLVMLIBS) $(SYSTEMLIBS) 32 | 33 | clean:: 34 | $(QUIET)rm -f $(PROJECT) $(PROJECT_OBJECTS) 35 | -------------------------------------------------------------------------------- /function-pointer-analysis/README.md: -------------------------------------------------------------------------------- 1 | ## Function Pointer Pass 2 | 3 | Toy code about use-def chains based on the LLVM IR and bitcode. Output the function call info in the bitcode file, e.g. 4 | - Direct function calls. Print the function name and line no. 5 | - FuncPtr i.e. function pointer. Calculate the functions that may call, if it is determined, replace it with direct function call. Consider the situation that the function pointers are stored in the memory. 6 | 7 | ## Build 8 | 9 | ``` 10 | $ cd your-directory 11 | $ make 12 | $ ./llvm-assignment your-bitcode-file 13 | ``` 14 | 15 | LLVM IR has three equivalent forms: 16 | - An in-memory representation(the Instruction class, among others) 17 | - An on-disk representation that is encoded in a space-efficient form(the bitcodefiles) 18 | - An on-disk representation in a human-readable text form(the LLVM assembly files) 19 | 20 | 21 | ## Gen bitcode 22 | - bitcode generation using following cmds 23 | ``` 24 | $ clang -c -emit-llvm -g3 funcptr.c 25 | $ opt -dot-cfg-only funcptr.bc 26 | $ opt -S –mem2reg funcptr.bc –o funcptr.opt 27 | ``` 28 | Clang / LLVM version 4.0.0 29 | 30 | ## Postscripts 31 | - Remove every uses of [getGlobalContext](https://reviews.llvm.org/rL266379), but the C API, and the [solution...](https://reviews.llvm.org/D19094) 32 | -------------------------------------------------------------------------------- /function-pointer-analysis/note.txt: -------------------------------------------------------------------------------- 1 | -lLLVMLTO -lLLVMObjCARCOpts -lLLVMSymbolize -lLLVMDebugInfoPDB -lLLVMDebugInfoDWARF -lLLVMMIRParser -lLLVMCoverage -lLLVMTableGen -lLLVMOrcJIT -lLLVMXCoreDisassembler -lLLVMXCoreCodeGen -lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMXCoreAsmPrinter -lLLVMSystemZDisassembler -lLLVMSystemZCodeGen -lLLVMSystemZAsmParser -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSystemZAsmPrinter -lLLVMSparcDisassembler -lLLVMSparcCodeGen -lLLVMSparcAsmParser -lLLVMSparcDesc -lLLVMSparcInfo -lLLVMSparcAsmPrinter -lLLVMPowerPCDisassembler -lLLVMPowerPCCodeGen -lLLVMPowerPCAsmParser -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMPowerPCAsmPrinter -lLLVMNVPTXCodeGen -lLLVMNVPTXDesc -lLLVMNVPTXInfo -lLLVMNVPTXAsmPrinter -lLLVMMSP430CodeGen -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMSP430AsmPrinter -lLLVMMipsDisassembler -lLLVMMipsCodeGen -lLLVMMipsAsmParser -lLLVMMipsDesc -lLLVMMipsInfo -lLLVMMipsAsmPrinter -lLLVMLanaiDisassembler -lLLVMLanaiCodeGen -lLLVMLanaiAsmParser -lLLVMLanaiDesc -lLLVMLanaiInstPrinter -lLLVMLanaiInfo -lLLVMHexagonDisassembler -lLLVMHexagonCodeGen -lLLVMHexagonAsmParser -lLLVMHexagonDesc -lLLVMHexagonInfo -lLLVMBPFCodeGen -lLLVMBPFDesc -lLLVMBPFInfo -lLLVMBPFAsmPrinter -lLLVMARMDisassembler -lLLVMARMCodeGen -lLLVMARMAsmParser -lLLVMARMDesc -lLLVMARMInfo -lLLVMARMAsmPrinter -lLLVMAMDGPUDisassembler -lLLVMAMDGPUCodeGen -lLLVMAMDGPUAsmParser -lLLVMAMDGPUDesc -lLLVMAMDGPUInfo -lLLVMAMDGPUAsmPrinter -lLLVMAMDGPUUtils -lLLVMAArch64Disassembler -lLLVMAArch64CodeGen -lLLVMGlobalISel -lLLVMAArch64AsmParser -lLLVMAArch64Desc -lLLVMAArch64Info -lLLVMAArch64AsmPrinter -lLLVMAArch64Utils -lLLVMObjectYAML -lLLVMLibDriver -lLLVMOption -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMDebugInfoCodeView -lLLVMDebugInfoMSF -lLLVMX86Desc -lLLVMMCDisassembler -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMMCJIT -lLLVMLineEditor -lLLVMPasses -lLLVMInterpreter -lLLVMExecutionEngine -lLLVMRuntimeDyld -lLLVMCodeGen -lLLVMTarget -lLLVMBitWriter -lLLVMCoroutines -lLLVMipo -lLLVMInstrumentation -lLLVMVectorize -lLLVMScalarOpts -lLLVMObject -lLLVMMCParser -lLLVMMC -lLLVMLinker -lLLVMIRReader -lLLVMBitReader -lLLVMAsmParser -lLLVMInstCombine -lLLVMTransformUtils -lLLVMAnalysis -lLLVMProfileData -lLLVMCore -lLLVMSupport -lLLVMDemangle 2 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/bonus.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/bonus.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/bonus.c: -------------------------------------------------------------------------------- 1 | struct A { 2 | int op; 3 | int (*a_fptr)(int, int); 4 | }; 5 | 6 | 7 | int add(int a, int b) { 8 | return a+b; 9 | } 10 | 11 | int foo() { 12 | struct A a; 13 | a.a_fptr = add; 14 | 15 | int op1 =1, op2=2; 16 | 17 | unsigned result = a.a_fptr(op1, op2); 18 | return 0; 19 | } 20 | 21 | 22 | /// 10 : add 23 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr0.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr0.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr0.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int foo() { 6 | int (*a_fptr)(int, int) = add; 7 | 8 | int op1 =1, op2=2; 9 | 10 | unsigned result = a_fptr(op1, op2); 11 | return 0; 12 | } 13 | 14 | 15 | /// 10 : add 16 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr1.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr1.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr1.c: -------------------------------------------------------------------------------- 1 | #include 2 | int add(int a, int b) { 3 | return a+b; 4 | } 5 | 6 | int sub(int a, int b) { 7 | return a-b; 8 | } 9 | 10 | int foo(int x) { 11 | int (*a_fptr)(int, int) = add; 12 | int (*s_fptr)(int, int) = sub; 13 | int (*t_fptr)(int, int) = 0; 14 | 15 | int op1=1, op2=2; 16 | 17 | if (x == 3) { 18 | t_fptr = a_fptr; 19 | } 20 | 21 | if (t_fptr != NULL) { 22 | unsigned result = t_fptr(op1, op2); 23 | } 24 | return 0; 25 | } 26 | 27 | /// 21 : add (it is OK to print 21 : add, NULL) 28 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr11.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr11.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr11.c: -------------------------------------------------------------------------------- 1 | #include 2 | int add(int a, int b) { 3 | return a+b; 4 | } 5 | 6 | int sub(int a, int b) { 7 | return a-b; 8 | } 9 | 10 | int foo(int x) { 11 | int (*a_fptr)(int, int) = add; 12 | int (*s_fptr)(int, int) = sub; 13 | int (*t_fptr)(int, int) = 0; 14 | 15 | int op1=1, op2=2; 16 | 17 | if (x == 3) { 18 | t_fptr = a_fptr; 19 | } else { 20 | t_fptr = s_fptr; 21 | } 22 | 23 | if (t_fptr != NULL) { 24 | unsigned result = t_fptr(op1, op2); 25 | } 26 | return 0; 27 | } 28 | 29 | /// 23 : add, sub 30 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr12.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr12.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr12.c: -------------------------------------------------------------------------------- 1 | #include 2 | int add(int a, int b) { 3 | return a+b; 4 | } 5 | 6 | int sub(int a, int b) { 7 | return a-b; 8 | } 9 | 10 | int foo(int x) { 11 | int (*a_fptr)(int, int) = add; 12 | int (*s_fptr)(int, int) = sub; 13 | int (*t_fptr)(int, int) = 0; 14 | 15 | int op1=1, op2=2; 16 | 17 | if (x == 3) { 18 | t_fptr = a_fptr; 19 | } 20 | if (x == 4) { 21 | t_fptr = s_fptr; 22 | } 23 | 24 | if (t_fptr != NULL) { 25 | unsigned result = t_fptr(op1, op2); 26 | } 27 | return 0; 28 | } 29 | 30 | /// 25 : add sub 31 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr13.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr13.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr13.c: -------------------------------------------------------------------------------- 1 | #include 2 | int add(int a, int b) { 3 | return a+b; 4 | } 5 | 6 | int sub(int a, int b) { 7 | return a-b; 8 | } 9 | 10 | int foo(int x) { 11 | int (*a_fptr)(int, int) = add; 12 | int (*s_fptr)(int, int) = sub; 13 | int (*t_fptr)(int, int) = 0; 14 | int (*q_fptr)(int, int) = 0; 15 | int (*r_fptr)(int, int) = 0; 16 | 17 | int op1=1, op2=2; 18 | 19 | if (x >= 3) { 20 | t_fptr = a_fptr; 21 | } 22 | if (x >= 4) { 23 | t_fptr = s_fptr; 24 | } 25 | if (x >= 5) { 26 | q_fptr = t_fptr; 27 | } 28 | 29 | if (t_fptr != NULL) { 30 | unsigned result = q_fptr(op1, op2); 31 | } 32 | return 0; 33 | } 34 | 35 | /// 30 : add, sub 36 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr14.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr14.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr14.c: -------------------------------------------------------------------------------- 1 | #include 2 | int add(int a, int b) { 3 | return a+b; 4 | } 5 | 6 | int sub(int a, int b) { 7 | return a-b; 8 | } 9 | 10 | int foo(int x) { 11 | int (*a_fptr)(int, int) = add; 12 | int (*s_fptr)(int, int) = sub; 13 | int (*t_fptr)(int, int) = 0; 14 | int (*q_fptr)(int, int) = 0; 15 | int (*r_fptr)(int, int) = 0; 16 | 17 | int op1=1, op2=2; 18 | 19 | if (x >= 3) { 20 | t_fptr = a_fptr; 21 | } 22 | if (x >= 4) { 23 | t_fptr = s_fptr; 24 | } 25 | if (x >= 5) { 26 | q_fptr = t_fptr; 27 | } 28 | if (x >= 6) 29 | r_fptr = q_fptr; 30 | 31 | if (t_fptr != NULL) { 32 | unsigned result = r_fptr(op1, op2); 33 | } 34 | return 0; 35 | } 36 | 37 | /// 32 : add, sub 38 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr2.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr2.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr2.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int sub(int a, int b) { 6 | return a-b; 7 | } 8 | 9 | int foo(int a, int b, int (*a_fptr)(int, int)) { 10 | return a_fptr(a, b); 11 | } 12 | 13 | 14 | int moo(char x, int op1, int op2) { 15 | int (*a_fptr)(int, int) = add; 16 | int (*s_fptr)(int, int) = sub; 17 | int (*t_fptr)(int, int) = 0; 18 | 19 | if (x == '+') { 20 | t_fptr = a_fptr; 21 | } 22 | if (x == '-') { 23 | t_fptr = s_fptr; 24 | } 25 | 26 | unsigned result = foo(op1, op2, t_fptr); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr20.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr20.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr20.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int sub(int a, int b) { 6 | return a-b; 7 | } 8 | 9 | int foo(int a, int b, int (*a_fptr)(int, int)) { 10 | return a_fptr(a, b); 11 | } 12 | 13 | 14 | int moo(char x, int op1, int op2) { 15 | int (*a_fptr)(int, int) = add; 16 | int (*s_fptr)(int, int) = sub; 17 | int (*t_fptr)(int, int) = 0; 18 | 19 | if (x == '+') { 20 | t_fptr = a_fptr; 21 | } 22 | unsigned result = foo(op1, op2, t_fptr); 23 | return 0; 24 | } 25 | 26 | /// It is OK to print 10 : add, NULL 27 | /// 10 : add 28 | /// 22 : foo 29 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr21.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr21.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr21.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int sub(int a, int b) { 6 | return a-b; 7 | } 8 | 9 | int foo(int a, int b, int (*a_fptr)(int, int)) { 10 | return a_fptr(a, b); 11 | } 12 | 13 | 14 | int moo(char x, int op1, int op2) { 15 | int (*a_fptr)(int, int) = add; 16 | int (*s_fptr)(int, int) = sub; 17 | int (*t_fptr)(int, int) = 0; 18 | 19 | if (x == '+') { 20 | t_fptr = a_fptr; 21 | } 22 | else if (x == '-') { 23 | t_fptr = s_fptr; 24 | } 25 | 26 | unsigned result = foo(op1, op2, t_fptr); 27 | return 0; 28 | } 29 | 30 | /// It is OK to print 10 : add, sub, NULL 31 | /// 10 : add, sub 32 | /// 26: foo 33 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr22.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr22.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr22.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int sub(int a, int b) { 6 | return a-b; 7 | } 8 | 9 | int goo(int a, int b, int (*a_fptr)(int, int)) { 10 | return a_fptr(a, b); 11 | } 12 | 13 | int foo(int a, int b, int (*a_fptr)(int, int)) { 14 | return goo(a, b, a_fptr); 15 | } 16 | 17 | 18 | int moo(char x, int op1, int op2) { 19 | int (*a_fptr)(int, int) = add; 20 | int (*s_fptr)(int, int) = sub; 21 | int (*t_fptr)(int, int) = 0; 22 | 23 | if (x == '+') { 24 | t_fptr = a_fptr; 25 | } 26 | else if (x == '-') { 27 | t_fptr = s_fptr; 28 | } 29 | 30 | unsigned result = foo(op1, op2, t_fptr); 31 | return 0; 32 | } 33 | 34 | /// 10 : add, sub 35 | /// 14 : goo 36 | /// 30 : foo 37 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr23.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr23.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr23.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int sub(int a, int b) { 6 | return a-b; 7 | } 8 | 9 | int goo(int a, int b, int (*a_fptr)(int, int), int(*b_fptr)(int, int) ) { 10 | int (*s_fptr)(int, int); 11 | if (a > 0) 12 | s_fptr = a_fptr; 13 | else 14 | s_fptr = b_fptr; 15 | return s_fptr(a, b); 16 | } 17 | 18 | int foo(int a, int b, int (*a_fptr)(int, int), int(*b_fptr)(int, int)) { 19 | return goo(a, b, a_fptr, b_fptr); 20 | } 21 | 22 | 23 | int moo(char x, int op1, int op2) { 24 | int (*a_fptr)(int, int) = add; 25 | int (*s_fptr)(int, int) = sub; 26 | int (*t_fptr)(int, int) = 0; 27 | 28 | if (x == '+') { 29 | t_fptr = a_fptr; 30 | } 31 | else if (x == '-') { 32 | t_fptr = s_fptr; 33 | } 34 | 35 | unsigned result = foo(op1, op2, a_fptr, t_fptr); 36 | return 0; 37 | } 38 | 39 | /// 15 : add, sub 40 | /// 19 : goo 41 | /// 35 : foo 42 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr24.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr24.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr24.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int sub(int a, int b) { 6 | return a-b; 7 | } 8 | 9 | int (*goo(int a, int b, int (*a_fptr)(int, int), int(*b_fptr)(int, int) ))(int, int) { 10 | return a_fptr; 11 | } 12 | 13 | int foo(int a, int b, int (*a_fptr)(int, int), int(*b_fptr)(int, int)) { 14 | int (*s_fptr)(int, int); 15 | s_fptr = goo(a, b, a_fptr, b_fptr); 16 | return s_fptr(a, b); 17 | } 18 | 19 | 20 | int moo(char x, int op1, int op2) { 21 | int (*a_fptr)(int, int) = add; 22 | int (*s_fptr)(int, int) = sub; 23 | int (*t_fptr)(int, int) = 0; 24 | 25 | if (x == '+') { 26 | t_fptr = a_fptr; 27 | } 28 | else if (x == '-') { 29 | t_fptr = s_fptr; 30 | } 31 | 32 | unsigned result = foo(op1, op2, a_fptr, t_fptr); 33 | return 0; 34 | } 35 | 36 | 37 | /// 15 : goo 38 | /// 16 : add 39 | /// 32 : foo 40 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr25.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr25.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr25.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int sub(int a, int b) { 6 | return a-b; 7 | } 8 | 9 | int (*goo(int a, int b, int (*a_fptr)(int, int), int(*b_fptr)(int, int) ))(int, int) { 10 | return b_fptr; 11 | } 12 | 13 | int foo(int a, int b, int (*a_fptr)(int, int), int(*b_fptr)(int, int)) { 14 | int (*s_fptr)(int, int); 15 | s_fptr = goo(a, b, a_fptr, b_fptr); 16 | return s_fptr(a, b); 17 | } 18 | 19 | 20 | int moo(char x, int op1, int op2) { 21 | int (*a_fptr)(int, int) = add; 22 | int (*s_fptr)(int, int) = sub; 23 | int (*t_fptr)(int, int) = 0; 24 | 25 | if (x == '+') { 26 | t_fptr = a_fptr; 27 | } 28 | else if (x == '-') { 29 | t_fptr = s_fptr; 30 | } 31 | 32 | unsigned result = foo(op1, op2, a_fptr, t_fptr); 33 | return 0; 34 | } 35 | 36 | 37 | /// 15 : goo 38 | /// 16 : add, sub 39 | /// 32 : foo 40 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr26.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr26.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr26.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int sub(int a, int b) { 6 | return a-b; 7 | } 8 | 9 | int (*goo(int a, int b, int (*a_fptr)(int, int), int(*b_fptr)(int, int) ))(int, int) { 10 | return a_fptr; 11 | } 12 | 13 | int (*foo(int a, int b, int (*a_fptr)(int, int), int(*b_fptr)(int, int) ))(int, int) { 14 | return b_fptr; 15 | } 16 | 17 | int moo(char x, int op1, int op2) { 18 | int (*a_fptr)(int, int) = add; 19 | int (*s_fptr)(int, int) = sub; 20 | int (* (*goo_ptr)(int, int, int (*)(int, int), int(*)(int, int)))(int, int); 21 | int (*t_fptr)(int, int) = 0; 22 | 23 | if (x == '+') { 24 | goo_ptr = goo; 25 | } 26 | else if (x == '-') { 27 | goo_ptr = foo; 28 | } 29 | 30 | t_fptr = goo_ptr(op1, op2, a_fptr, s_fptr); 31 | t_fptr(op1, op2); 32 | 33 | return 0; 34 | } 35 | 36 | 37 | /// 15 : goo 38 | /// 16 : add 39 | /// 32 : foo 40 | -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr3.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/function-pointer-analysis/testcases/funcptr3.bc -------------------------------------------------------------------------------- /function-pointer-analysis/testcases/funcptr3.c: -------------------------------------------------------------------------------- 1 | int add(int a, int b) { 2 | return a+b; 3 | } 4 | 5 | int foo(int a, int b, int (*a_fptr)(int, int)) { 6 | return a_fptr(a, b); 7 | } 8 | 9 | 10 | int moo(char x, int op1, int op2) { 11 | int (*a_fptr)(int, int) = add; 12 | unsigned result = foo(op1, op2, a_fptr); 13 | return 0; 14 | } 15 | 16 | /// 6 : add < you need to replace the call with call to add 17 | // 12 : foo 18 | -------------------------------------------------------------------------------- /intel-mpx/Intel-MPX.md: -------------------------------------------------------------------------------- 1 | ## Intel MPX 2 | - Intel MPX, see: Intel MPX Explained & 3 | - Intel MPX support in llvm, see this page: and softbonud see this github repo: 4 | - LLVM MPX feature in LLVM see this commit: & and this news: 5 | - LLVM MPX feature in GCC, see this page: 6 | - Microsoft MPX test case: 7 | - Intel MPX explained, experiments: 8 | - Clang dev mail list for `-fmpx`: 9 | - Google Address Sanitizer: 10 | - LLVM mpx example see here: 11 | - Clang mpx example see here: 12 | - code review, X86-MPX: Implemented encoding for MPX instructions: 13 | - Simple readme about MPX see this paper's background: 14 | - Until now, for clang and llvm-6.0 release version, only the MPX (Intel Memory Protection eXtensions) feature bit and bound registers are now supported on the X86 backend. See [r238916](https://reviews.llvm.org/rL238916) 15 | - MPX experiments in gcc, see google's address sanitizer wiki page: 16 | -------------------------------------------------------------------------------- /kaleidoscope/README.md: -------------------------------------------------------------------------------- 1 | ## Kaleidoscope 2 | Implementation of a toy language named Kaleidoscope, the code uses global variables all over the place, doesn’t use nice design patterns like visitors, but it is simple. 3 | 4 | ## Notice 5 | For the new version LLVM some API maybe abandoned, so latest code ref to official [page](http://llvm.org/docs/tutorial/) and GitHub [repo](https://github.com/llvm-mirror/llvm/tree/master/examples/Kaleidoscope) 6 | 7 | ## Reference 8 | LLVM official website, more details see this [page](http://llvm.org/docs/tutorial/) 9 | -------------------------------------------------------------------------------- /kaleidoscope/ast/Makefile: -------------------------------------------------------------------------------- 1 | CXX =g++ -std=gnu++11 2 | all: 3 | $(CXX) -g -O3 parser.cpp -o parser 4 | clean: 5 | rm -f parser 6 | -------------------------------------------------------------------------------- /kaleidoscope/ast/bnf.md: -------------------------------------------------------------------------------- 1 | ## Backus-Naur Form 2 | Backus-Naur Form of Kaleidoscope is shown below: 3 | ``` 4 | numberexpr ::= number 5 | 6 | parenexpr ::= '(' expression ')' 7 | 8 | identifierexpr ::= identifier | 9 | identifier '(' expression* ')' 10 | 11 | primary ::= identifierexpr | 12 | numberexpr | 13 | parenexpr 14 | 15 | binoprhs ::= ('+' primary)* 16 | 17 | expression ::= primary binoprhs 18 | 19 | prototype ::= id '(' id* ')' 20 | 21 | definition ::= 'def' prototype expression 22 | 23 | toplevelexpr ::= expression 24 | 25 | external ::= 'extern' prototype 26 | 27 | number ::= [0-9.]+ 28 | 29 | identifier ::= [a-zA-Z][a-zA-Z0-9]* 30 | 31 | ``` 32 | -------------------------------------------------------------------------------- /kaleidoscope/dbg-info/Makefile: -------------------------------------------------------------------------------- 1 | TARGET=dbg-info 2 | all: 3 | @clang++ -g -O3 dbg_info.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core executionengine object support native` -o $(TARGET) 4 | clean: 5 | @rm -f $(TARGET) 6 | -------------------------------------------------------------------------------- /kaleidoscope/functional_programming_language/Makefile: -------------------------------------------------------------------------------- 1 | TARGET=func_prog 2 | all: 3 | clang++ -g -O3 functional.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native` -o $(TARGET) 4 | clean: 5 | @rm -f $(TARGET) 6 | -------------------------------------------------------------------------------- /kaleidoscope/ir/Makefile: -------------------------------------------------------------------------------- 1 | TARGET=ir-gen 2 | all: 3 | clang++ -g -O3 gen-ir.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o $(TARGET) 4 | clean: 5 | rm -f $(TARGET) 6 | -------------------------------------------------------------------------------- /kaleidoscope/jit/Makefile: -------------------------------------------------------------------------------- 1 | TARGET=jit 2 | all: 3 | @clang++ -g -O3 jit.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native` -o $(TARGET) 4 | clean: 5 | @rm -f $(TARGET) 6 | -------------------------------------------------------------------------------- /kaleidoscope/target-triple/Makefile: -------------------------------------------------------------------------------- 1 | TARGET=target-triple 2 | all: 3 | clang++ -g -O3 target-triple.cpp `llvm-config --cxxflags --ldflags --system-libs --libs all` -o $(TARGET) 4 | clean: 5 | @rm -f $(TARGET) 6 | -------------------------------------------------------------------------------- /lambda-calculus/03.2-lambda-calculus.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/lambda-calculus/03.2-lambda-calculus.pdf -------------------------------------------------------------------------------- /lambda-calculus/Chapter5-lambda-calculus.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/lambda-calculus/Chapter5-lambda-calculus.pdf -------------------------------------------------------------------------------- /lambda-calculus/README.md: -------------------------------------------------------------------------------- 1 | ## Lambda Calculus 2 | 3 | - Stanford CS242, Programming Languages: 4 | -------------------------------------------------------------------------------- /lambda-calculus/lambdanotes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/lambda-calculus/lambdanotes.pdf -------------------------------------------------------------------------------- /lambda-calculus/pfpl-2nded-chap-4-5-6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/lambda-calculus/pfpl-2nded-chap-4-5-6.pdf -------------------------------------------------------------------------------- /live-variable-analysis/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=live-var 2 | UNAME_S := $(shell uname -s) 3 | ifeq ($(UNAME_S),Darwin) 4 | DBG_FILES := ${PROJECT}.dSYM 5 | endif 6 | ifeq ($(UNAME_S),Linux) 7 | DBG_FILES := 8 | endif 9 | 10 | COMMON_FLAGS=-Wall -Wextra #-g 11 | SRC_DIR?=$(PWD) 12 | SRC=LiveVariable.cpp 13 | 14 | all: 15 | @echo Compiling $(SRC) 16 | clang++ $(SRC) $(COMMON_FLAGS) `llvm-config --cxxflags --ldflags --system-libs --libs irreader bitwriter passes core support` -O3 -o $(PROJECT) 17 | clean: 18 | rm -rf $(PROJECT) $(DBG_FILES) 19 | -------------------------------------------------------------------------------- /live-variable-analysis/README.md: -------------------------------------------------------------------------------- 1 | # Live Variable Analysis 2 | Target independent live variable analysis via backward data flow equations, this implement is NOT based on the SSA algorithm. 3 | 4 | Notice: This implement is `NOT` a backend target specific pass, i.e. current implement is a `Module` or `Function` level pass, not a `MachineFunction` level pass, for `MachineFunction` level implement please refer to LLVM source [lib/CodeGen](https://github.com/llvm-mirror/llvm/blob/master/lib/CodeGen/LiveVariables.cpp) or [cranelift's annotation](https://github.com/CraneStation/cranelift/blob/8033deda3ac152d0b95bb0ad80b419625c3f0d58/lib/cretonne/src/regalloc/liveness.rs#L1-L176)... 5 | 6 | ## Preface 7 | More precise definition of liveness: 8 | 9 | A variable v is `live` on a CFG edge if 10 | - existing a direct path from that edge to use of v(node in use[v]), and 11 | - that path does not go through any def of v(no nodes in def[v]) 12 | Here, `use[v]` is the set of CFG nodes that define variable v, while `def[v]` is the set of CFG nodes that use variable v. 13 | 14 | ==> Liveness data flow equations: 15 | 16 | - out[B] = U{S \in succ[B]} in[S] 17 | - in[B] = use[B] U (out[B] - def[B]) 18 | 19 | where, 20 | in[B]: live variable set of block B at the start point; 21 | out[B]: live variable set of block B at the end point; 22 | def[B]: set of variables that are defined at block B; 23 | use[B]: set of variables that are used at block B; 24 | 25 | ![image](./img/live-variable-analysis-review.png "CMU 15-745 Live Variable Analysis Review") 26 | 27 | ## Implementation 28 | - Identify `Basic Blocks` and `Instructions` in the source code; 29 | - Gather information about functions in the program, e.g.: no. of arguments, call sites and instructions; 30 | - Gather information such as variable name, slot index from the source code; 31 | - Perform `Live Variable Analysis` and dump the result. 32 | 33 | ## Build 34 | 35 | ``` 36 | $ make 37 | $ ./liveness your_bc_file 38 | ``` 39 | 40 | Notice: Current implement is based on llvm 5.0.0 41 | 42 | 43 | ## REF 44 | - CMU CS course, 15-411 compiler design: 45 | - 46 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/add.c: -------------------------------------------------------------------------------- 1 | inline void __attribute__((__always_inline__)) 2 | __sv_add(int *c, int *a, int *b, int n) { 3 | for (int i = 0; i < n; ++i) { 4 | c[i] = a[i] + b[i]; 5 | } 6 | } 7 | 8 | int main() { 9 | int a[10], b[10], c[10]; 10 | __sv_add(c, a, b, 10); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/alias.c: -------------------------------------------------------------------------------- 1 | int sum(int *a, int *b, int *c, int n) { 2 | int *p_a = &a[1]; 3 | int *p_b = &b[1]; 4 | int *p_c = &c[1]; 5 | for (int i = 1; i < n; ++i) { 6 | p_c[i] = p_a[i] + p_b[i]; 7 | } 8 | switch (n) { 9 | case 0: 10 | return p_c[0]; 11 | case 1: 12 | return p_c[1]; 13 | case 2: 14 | return p_c[2]; 15 | } 16 | return p_c[n - 1]; 17 | } 18 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/bar.c: -------------------------------------------------------------------------------- 1 | int sum(int a, int b) { return a + b; } 2 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/bar.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'bar.ll' 2 | source_filename = "bar.c" 3 | target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" 4 | target triple = "x86_64-apple-macosx10.13.0" 5 | 6 | ; Function Attrs: noinline nounwind optnone ssp uwtable 7 | define i32 @sum(i32 %arg, i32 %arg1) #0 { 8 | bb: 9 | %tmp = alloca i32, align 4 10 | %tmp2 = alloca i32, align 4 11 | store i32 %arg, i32* %tmp, align 4 12 | store i32 %arg1, i32* %tmp2, align 4 13 | %tmp3 = load i32, i32* %tmp, align 4 14 | %tmp4 = load i32, i32* %tmp2, align 4 15 | %tmp5 = add nsw i32 %tmp3, %tmp4 16 | ret i32 %tmp5 17 | } 18 | 19 | attributes #0 = { noinline nounwind optnone ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 20 | 21 | !llvm.module.flags = !{!0, !1} 22 | !llvm.ident = !{!2} 23 | 24 | !0 = !{i32 1, !"wchar_size", i32 4} 25 | !1 = !{i32 7, !"PIC Level", i32 2} 26 | !2 = !{!"clang version 5.0.0 (tags/RELEASE_500/final)"} 27 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/foo.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | inline int __attribute__((__always_inline__)) sum(int a, int b) { 4 | int c = a + b; 5 | if (c == 10) { 6 | c += a; 7 | } else { 8 | c += b; 9 | } 10 | for (int i = 0; i < b; ++i) { 11 | c += i; 12 | } 13 | return c; 14 | } 15 | 16 | int main() { sum(1, 2); } 17 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/ir-gen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ###################################### 3 | # LLVM ir assembly file generation. 4 | ###################################### 5 | 6 | set -e 7 | #clang -O0 -emit-llvm -c sum.c -o sum.bc 8 | #llvm-dis sum.bc 9 | #clang -O0 -S -emit-llvm sum.c 10 | for file in `ls *.c` 11 | do 12 | echo compiling ${file}... 13 | #clang -g3 -O0 -S -emit-llvm ${file} 14 | prefix=`basename ${file} .c` 15 | #clang -O3 -S -emit-llvm ${file} -o ${prefix}.ll 16 | clang -S -emit-llvm ${file} -o ${prefix}.ll 17 | # Assign names to anonymous instructions 18 | opt -instnamer ${prefix}.ll -o ${prefix}.ll 19 | #opt -mem2reg ${prefix}.ll -o ${prefix}.ll 20 | llvm-dis ${prefix}.ll -o ${prefix}.ll 21 | done 22 | echo DONE! 23 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/sum.c: -------------------------------------------------------------------------------- 1 | inline int __attribute__((__always_inline__)) add1(int a, int b) { 2 | return a + b; 3 | } 4 | 5 | inline int __attribute__((__always_inline__)) add2(int a, int b) { 6 | int ret = 0; 7 | if (a == 1) 8 | ret = b; 9 | if (a == 10) 10 | ret = a + 10; 11 | if (a > b) 12 | ret += a * 10; 13 | else 14 | ret = b * 10; 15 | return ret; 16 | } 17 | 18 | int main() { 19 | int n = 100; 20 | int s = 0; 21 | for (int i = 0; i < n; ++i) { 22 | s += i; 23 | if (s < 50) { 24 | s += 1; 25 | } 26 | } 27 | add1(n, s); 28 | add2(n, s); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/sv_add.c: -------------------------------------------------------------------------------- 1 | inline void __attribute__((__always_inline__)) 2 | __sv_add(int *c, int *a, int *b, int n) { 3 | for (int i = 0; i < n; ++i) { 4 | c[i] = a[i] + b[i]; 5 | } 6 | } 7 | 8 | int a[10], b[10], c[10]; 9 | 10 | int main() { 11 | __sv_add(c, a, b, 10); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/test1.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | if (a) { 4 | x = 0; 5 | } else { 6 | x = 10; 7 | } 8 | return x; /// Range of x is [0..10] 9 | } 10 | 11 | /// Expected output 12 | /// [0..10] 13 | -------------------------------------------------------------------------------- /live-variable-analysis/benchmark/test1.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'test1.ll' 2 | source_filename = "test1.c" 3 | target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" 4 | target triple = "x86_64-apple-macosx10.13.0" 5 | 6 | ; Function Attrs: noinline nounwind optnone ssp uwtable 7 | define i32 @foo(i32 %arg) #0 { 8 | bb: 9 | %tmp = alloca i32, align 4 10 | %tmp1 = alloca i32, align 4 11 | store i32 %arg, i32* %tmp, align 4 12 | store i32 0, i32* %tmp1, align 4 13 | %tmp2 = load i32, i32* %tmp, align 4 14 | %tmp3 = icmp ne i32 %tmp2, 0 15 | br i1 %tmp3, label %bb4, label %bb5 16 | 17 | bb4: ; preds = %bb 18 | store i32 0, i32* %tmp1, align 4 19 | br label %bb6 20 | 21 | bb5: ; preds = %bb 22 | store i32 10, i32* %tmp1, align 4 23 | br label %bb6 24 | 25 | bb6: ; preds = %bb5, %bb4 26 | %tmp7 = load i32, i32* %tmp1, align 4 27 | ret i32 %tmp7 28 | } 29 | 30 | attributes #0 = { noinline nounwind optnone ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 31 | 32 | !llvm.module.flags = !{!0, !1} 33 | !llvm.ident = !{!2} 34 | 35 | !0 = !{i32 1, !"wchar_size", i32 4} 36 | !1 = !{i32 7, !"PIC Level", i32 2} 37 | !2 = !{!"clang version 5.0.0 (tags/RELEASE_500/final)"} 38 | -------------------------------------------------------------------------------- /live-variable-analysis/img/live-variable-analysis-review.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/live-variable-analysis/img/live-variable-analysis-review.png -------------------------------------------------------------------------------- /live-variable-analysis/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PROJ=live-var 3 | TEST_SUITE=benchmark 4 | if [ ! -f ${PROJ} ] 5 | then 6 | echo Please build ${PROJ} 7 | exit 8 | fi 9 | for ir in `ls ${TEST_SUITE}/*.ll` 10 | do 11 | if [ -f ${ir} ] 12 | then 13 | echo ${ir} 14 | ./${PROJ} ${ir} 15 | fi 16 | done 17 | -------------------------------------------------------------------------------- /llvm-backend/LLVM - Another Toolchain Platform.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/llvm-backend/LLVM - Another Toolchain Platform.pptx -------------------------------------------------------------------------------- /llvm-backend/cpu0/CPU0.md: -------------------------------------------------------------------------------- 1 | # CPU0 Architecture 2 | More details about CPU0 arch and its instruction set, checkout this page(http://ccckmit.wikidot.com/ocs:cpu0), and git repo see this [page](https://github.com/Jonathan2251/lbt.git), It include the implementation of a `compiler backend, linker, llvm-objdump, elf2hex` as well as `Verilog` language source code of Cpu0 instruction set. 3 | 4 | This tutorial started using the LLVM 3.1 Mips backend as a reference and sync to llvm 3.5 Mips at version 3.5.3. You will learn how to develop a toolchain as well as design a CPU based on the LLVM infrastructure. 5 | 6 | 7 | -------------------------------------------------------------------------------- /llvm-backend/foo/build.sh: -------------------------------------------------------------------------------- 1 | # !/bin/bash 2 | llvm-tblgen -print-records insns.td 2>&1 | tee result.txt 3 | 4 | -------------------------------------------------------------------------------- /llvm-backend/foo/insns.td: -------------------------------------------------------------------------------- 1 | class Insn MajOpc, bit MinOpc>{ 2 | bits<32> insnEncoding; 3 | let insnEncoding{15-12}=MajOpc; 4 | let insnEncoding{11}=MinOpc; 5 | } 6 | 7 | multiclass RegAndImmInsn opcode>{ 8 | def rr: Insn; 9 | def ri: Insn; 10 | } 11 | def SUB : Insn<0x00,0>; 12 | defm ADD : RegAndImmInsn<0x01>; 13 | -------------------------------------------------------------------------------- /llvm-backend/foo/result.txt: -------------------------------------------------------------------------------- 1 | ------------- Classes ----------------- 2 | class Insn Insn:MajOpc = { ?, ?, ?, ? }, bit Insn:MinOpc = ?> { 3 | bits<32> insnEncoding = { ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, Insn:MajOpc{3}, Insn:MajOpc{2}, Insn:MajOpc{1}, Insn:MajOpc{0}, Insn:MinOpc, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? }; 4 | string NAME = ?; 5 | } 6 | ------------- Defs ----------------- 7 | def ADDri { // Insn ri 8 | bits<32> insnEncoding = { ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, 1, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? }; 9 | string NAME = "ADD"; 10 | } 11 | def ADDrr { // Insn rr 12 | bits<32> insnEncoding = { ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, 1, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? }; 13 | string NAME = "ADD"; 14 | } 15 | def SUB { // Insn 16 | bits<32> insnEncoding = { ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, 0, 0, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? }; 17 | string NAME = ?; 18 | } 19 | -------------------------------------------------------------------------------- /llvm-backend/llvm-backend.md: -------------------------------------------------------------------------------- 1 | ## LLVM Backend 2 | -------------------------------------------------------------------------------- /llvm-profiling/README.md: -------------------------------------------------------------------------------- 1 | # LLVM Profiling 2 | 3 | 4 | ## REF 5 | - Efficient Profiling in the LLVM Compiler Infrastructure, Andreas Neustifter, M.S. Thesis: 6 | 7 | -------------------------------------------------------------------------------- /llvm-profiling/examples/README.md: -------------------------------------------------------------------------------- 1 | LLVM profiling examples. 2 | 3 | 4 | -------------------------------------------------------------------------------- /llvm-profiling/examples/dynamic-instruction-count/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=cdi 2 | UNAME_S := $(shell uname -s) 3 | ifeq ($(UNAME_S),Darwin) 4 | DBG_FILES := ${PROJECT}.dSYM 5 | endif 6 | ifeq ($(UNAME_S),Linux) 7 | DBG_FILES := 8 | endif 9 | 10 | COMMON_FLAGS=-Wall -Wextra #-g 11 | SRC_DIR?=$(PWD) 12 | SRC=CountDynamicInstructions.cpp 13 | 14 | all: 15 | @echo Compiling $(SRC) 16 | clang++ $(SRC) $(COMMON_FLAGS) `llvm-config --cxxflags --ldflags --system-libs --libs irreader bitwriter passes core support` -O3 -o $(PROJECT) 17 | clean: 18 | rm -rf $(PROJECT) $(DBG_FILES) foo a.out 19 | -------------------------------------------------------------------------------- /llvm-profiling/examples/dynamic-instruction-count/README.md: -------------------------------------------------------------------------------- 1 | ## Collecting Dynamic Instruction Counts 2 | At a high level, this pass obtains dynamic instruction counts by inserting an instrumentation module into the program at compile time. 3 | At the end of every basic block, the LLVM pass inserts calls to the instrumentation module, which is subsequently linked to the original executable.These calls record the executed instructions within the current basic block. When the program is run, it prints out the dynamic instruction counts just before the main function terminates. 4 | 5 | The implementation is that we go through every module, every function and every basic block. For each basic block, we statically create a C++ map, which keeps track of the instructions in the basic block as well as their counts. Next, we find the terminator of this block, e.g.: a ret or jump instruction. 6 | However, given that entering a basic block implies that every instruction within it `must` be executed, we can significantly reduce the number of calls to the external statistics-collection code. This can be done by aggregating the instruction counts per basic block at compile time, and recording them once before exiting a basic block. 7 | 8 | ## Build 9 | 10 | ``` 11 | $ make 12 | $ ./cdi benchmark/sum.ll 13 | ``` 14 | 15 | then you will get an output of the bc file after instrumentation which maybe unreadable, you can use `llvm-dis` to see the instrumented IR. 16 | Now we can link the instrumented ir with the runtime library `runtime.cpp` by the following commands: 17 | 18 | ``` 19 | $ llc -filetype=obj benchmark/sum.ll 20 | $ clang++ benchmark/sum.o runtime.cpp -o foo 21 | $ ./foo 22 | ``` 23 | 24 | Now we can get the following output: 25 | 26 | ``` 27 | ret 1 28 | br 6148 29 | add 5120 30 | alloca 10 31 | load 19459 32 | store 6151 33 | getelementptr 6144 34 | sext 6144 35 | icmp 2050 36 | ret 1 37 | getelementptr 3 38 | call 1 39 | ``` 40 | 41 | Notice: Current implementation based on LLVM version 5.0.0 42 | 43 | ## REF 44 | - CSE231 project part1: 45 | -------------------------------------------------------------------------------- /llvm-profiling/examples/dynamic-instruction-count/benchmark/ir-gen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ###################################### 3 | # LLVM ir assembly file generation. 4 | ###################################### 5 | 6 | set -e 7 | #clang -O0 -emit-llvm -c sum.c -o sum.bc 8 | #llvm-dis sum.bc 9 | #clang -O0 -S -emit-llvm sum.c 10 | rm -f *.o 11 | for file in `ls *.c` 12 | do 13 | echo compiling ${file}... 14 | #clang -g3 -O0 -S -emit-llvm ${file} 15 | prefix=`basename ${file} .c` 16 | #clang -O3 -S -emit-llvm ${file} -o ${prefix}.ll 17 | clang -S -emit-llvm ${file} -o ${prefix}.ll 18 | # Assign names to anonymous instructions 19 | #opt -instnamer ${prefix}.ll -o ${prefix}.ll 20 | #opt -mem2reg ${prefix}.ll -o ${prefix}.ll 21 | #llvm-dis ${prefix}.ll -o ${prefix}.ll 22 | done 23 | echo DONE! 24 | -------------------------------------------------------------------------------- /llvm-profiling/examples/dynamic-instruction-count/benchmark/random.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void __sv_add(int *c, int *a, int *b, int n) { 4 | for (int i = 0; i < n; ++i) c[i] = a[i] + b[i]; 5 | } 6 | 7 | int main() { 8 | srand(time(NULL)); 9 | int n = rand() % 1024; 10 | int *a = (int*)malloc(n * sizeof(int)); 11 | int *b = (int*)malloc(n * sizeof(int)); 12 | int *c = (int*)malloc(n * sizeof(int)); 13 | for (int i = 0; i < n; ++i) { 14 | a[i] = i; 15 | b[i] = i + 1; 16 | c[i] = i + 2; 17 | } 18 | __sv_add(c, a, b, n); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /llvm-profiling/examples/dynamic-instruction-count/benchmark/sum.c: -------------------------------------------------------------------------------- 1 | void __sv_add(int *c, int *a, int *b, int n) { 2 | for (int i = 0; i < n; ++i) c[i] = a[i] + b[i]; 3 | } 4 | 5 | int main() { 6 | int a[1024], b[1024], c[1024]; 7 | for (int i = 0; i < 1024; ++i) { 8 | a[i] = i; 9 | b[i] = i + 1; 10 | c[i] = i + 2; 11 | } 12 | __sv_add(c, a, b, 1024); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /llvm-profiling/examples/dynamic-instruction-count/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PROJ=cdi 3 | TEST_SUITE=benchmark 4 | RUNTIME_LIB=runtime.cpp 5 | if [ ! -f ${PROJ} ] 6 | then 7 | echo Please build ${PROJ} 8 | exit 9 | fi 10 | 11 | rm -f ${TEST_SUITE}/*.o ${TEST_SUITE}/*.ll 12 | cd ${TEST_SUITE} 13 | ./ir-gen.sh 14 | cd .. 15 | for ir in `ls ${TEST_SUITE}/*.ll` 16 | do 17 | if [ -f ${ir} ] 18 | then 19 | prefix=`basename ${ir} .ll` 20 | echo "===----- ${ir} -----===" 21 | ./${PROJ} ${ir} 22 | llvm-dis ${ir} -o ${ir} 23 | llc -filetype=obj ${ir} 24 | clang++ ${TEST_SUITE}/${prefix}.o ${RUNTIME_LIB} -o foo 25 | ./foo 26 | rm -f foo ${TEST_SUITE}/${prefix}.o 27 | fi 28 | done 29 | -------------------------------------------------------------------------------- /llvm-profiling/examples/profiling-branch-bias/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=pbb 2 | UNAME_S := $(shell uname -s) 3 | ifeq ($(UNAME_S),Darwin) 4 | DBG_FILES := ${PROJECT}.dSYM 5 | endif 6 | ifeq ($(UNAME_S),Linux) 7 | DBG_FILES := 8 | endif 9 | 10 | COMMON_FLAGS=-Wall -Wextra #-g 11 | SRC_DIR?=$(PWD) 12 | SRC=ProfilingBranchBias.cpp 13 | 14 | all: 15 | @echo Compiling $(SRC) 16 | clang++ $(SRC) $(COMMON_FLAGS) `llvm-config --cxxflags --ldflags --system-libs --libs irreader bitwriter passes core support` -O3 -o $(PROJECT) 17 | clean: 18 | rm -rf $(PROJECT) $(DBG_FILES) foo a.out 19 | -------------------------------------------------------------------------------- /llvm-profiling/examples/profiling-branch-bias/README.md: -------------------------------------------------------------------------------- 1 | ## Profiling Branch Bias 2 | Count the number of times conditional branch instructions are executed and the number of times conditional branch instructions are taken. Note that we only consider `conditional branches`. 3 | A conditional branch is considered taken if its condition evaluates to true. Each instrumented function should output these two counts before function termination. 4 | 5 | ## Build 6 | 7 | ``` 8 | $ make 9 | $ ./pbb benchmark/sum.ll 10 | ``` 11 | 12 | then you will get an output of the bc file after instrumentation which maybe unreadable, you can use `llvm-dis` to see the instrumented IR. 13 | Now we can link the instrumented ir with the runtime library `runtime.cpp` by the following commands: 14 | 15 | ``` 16 | $ llc -filetype=obj benchmark/sum.ll 17 | $ clang++ benchmark/sum.o runtime.cpp -o foo 18 | $ ./foo 19 | ``` 20 | 21 | Now we can get the following output: 22 | 23 | ``` 24 | taken 2048 25 | total 2050 26 | taken 0 27 | total 0 28 | ``` 29 | 30 | Notice: Current implementation based on LLVM version 5.0.0 31 | 32 | ## REF 33 | - CSE231 project part1: 34 | -------------------------------------------------------------------------------- /llvm-profiling/examples/profiling-branch-bias/benchmark/br.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void __sv_add(int *c, int *a, int *b, int n) { 4 | for (int i = 0; i < n; ++i) 5 | c[i] = a[i] + b[i]; 6 | } 7 | 8 | int main() { 9 | srand(time(NULL)); 10 | int n = rand() % 1024; 11 | int *a = (int *)malloc(n * sizeof(int)); 12 | int *b = (int *)malloc(n * sizeof(int)); 13 | int *c = (int *)malloc(n * sizeof(int)); 14 | for (int i = 0; i < n; ++i) { 15 | if ((i % 2) == 1) { 16 | a[i] = i / 2 + 0; 17 | b[i] = i / 2 + 1; 18 | c[i] = i % 2 + 1; 19 | } else { 20 | a[i] = i % 2 + 0; 21 | b[i] = i % 2 + 1; 22 | c[i] = i / 2 + 1; 23 | } 24 | } 25 | __sv_add(c, a, b, n); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /llvm-profiling/examples/profiling-branch-bias/benchmark/ir-gen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ###################################### 3 | # LLVM ir assembly file generation. 4 | ###################################### 5 | 6 | set -e 7 | #clang -O0 -emit-llvm -c sum.c -o sum.bc 8 | #llvm-dis sum.bc 9 | #clang -O0 -S -emit-llvm sum.c 10 | rm -f *.o 11 | for file in `ls *.c` 12 | do 13 | echo compiling ${file}... 14 | #clang -g3 -O0 -S -emit-llvm ${file} 15 | prefix=`basename ${file} .c` 16 | #clang -O3 -S -emit-llvm ${file} -o ${prefix}.ll 17 | clang -S -emit-llvm ${file} -o ${prefix}.ll 18 | # Assign names to anonymous instructions 19 | #opt -instnamer ${prefix}.ll -o ${prefix}.ll 20 | #opt -mem2reg ${prefix}.ll -o ${prefix}.ll 21 | #llvm-dis ${prefix}.ll -o ${prefix}.ll 22 | done 23 | echo DONE! 24 | -------------------------------------------------------------------------------- /llvm-profiling/examples/profiling-branch-bias/benchmark/random.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void __sv_add(int *c, int *a, int *b, int n) { 4 | for (int i = 0; i < n; ++i) 5 | c[i] = a[i] + b[i]; 6 | } 7 | 8 | int main() { 9 | srand(time(NULL)); 10 | int n = rand() % 1024; 11 | int *a = (int *)malloc(n * sizeof(int)); 12 | int *b = (int *)malloc(n * sizeof(int)); 13 | int *c = (int *)malloc(n * sizeof(int)); 14 | for (int i = 0; i < n; ++i) { 15 | a[i] = i; 16 | b[i] = i + 1; 17 | c[i] = i + 2; 18 | } 19 | __sv_add(c, a, b, n); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /llvm-profiling/examples/profiling-branch-bias/benchmark/sum.c: -------------------------------------------------------------------------------- 1 | void __sv_add(int *c, int *a, int *b, int n) { 2 | for (int i = 0; i < n; ++i) 3 | c[i] = a[i] + b[i]; 4 | } 5 | 6 | int main() { 7 | int a[1024], b[1024], c[1024]; 8 | for (int i = 0; i < 1024; ++i) { 9 | a[i] = i; 10 | b[i] = i + 1; 11 | c[i] = i + 2; 12 | } 13 | __sv_add(c, a, b, 1024); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /llvm-profiling/examples/profiling-branch-bias/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PROJ=pbb 3 | TEST_SUITE=benchmark 4 | RUNTIME_LIB=runtime.cpp 5 | if [ ! -f ${PROJ} ] 6 | then 7 | echo Please build ${PROJ} 8 | exit 9 | fi 10 | 11 | rm -f ${TEST_SUITE}/*.o ${TEST_SUITE}/*.ll 12 | cd ${TEST_SUITE} 13 | ./ir-gen.sh 14 | cd .. 15 | for ir in `ls ${TEST_SUITE}/*.ll` 16 | do 17 | if [ -f ${ir} ] 18 | then 19 | prefix=`basename ${ir} .ll` 20 | echo "===----- ${ir} -----===" 21 | ./${PROJ} ${ir} 22 | llvm-dis ${ir} -o ${ir} 23 | llc -filetype=obj ${ir} 24 | clang++ ${TEST_SUITE}/${prefix}.o ${RUNTIME_LIB} -o foo 25 | ./foo 26 | rm -f foo ${TEST_SUITE}/${prefix}.o 27 | fi 28 | done 29 | -------------------------------------------------------------------------------- /memory-ssa/.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/memory-ssa/.gitignore -------------------------------------------------------------------------------- /memory-ssa/README.md: -------------------------------------------------------------------------------- 1 | # LLVM Memory SSA 2 | 3 | LLVM's memory SSA implement, see its source: [lib/Analysis/MemorySSA.cpp](https://github.com/llvm-mirror/llvm/blob/master/lib/Analysis/MemorySSA.cpp) 4 | 5 | LLVM's memory dependence analysis pass implementation checkout this page: [lib/Analysis/MemoryDependenceAnalysis.cpp](https://github.com/llvm-mirror/llvm/blob/master/lib/Analysis/MemoryDependenceAnalysis.cpp) 6 | 7 | ## REF 8 | Memory SSA: 9 | 10 | - LLVM official page: 11 | - GCC Memory SSA implement note: 12 | - 2007 GCC developers' summit: 13 | - JSS'18, Parallel construction of interprocedual memory SSA form: 14 | 15 | SSA: 16 | 17 | - CC'96, Effective Representation of Alias and Indirect Memory Operations in SSA Form: 18 | 19 | LLVM Examples: 20 | 21 | - LLVM patch adds MemorySSA: 22 | - GVN-Hoist: Hoisting Computations from Branches, and source: 23 | - LLVM early cse, a pass performs a simple dominator tree walk that eliminates trivially redundant instructions: 24 | -------------------------------------------------------------------------------- /memory-ssa/examples/ir-gen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | clang -S -emit-llvm sum.c -o sum.ll 4 | opt -instnamer sum.ll -o sum.ll 5 | llvm-dis sum.ll -o sum.ll 6 | opt -passes='print' -disable-output sum.ll 2>&1 | tee mm-ssa.ll 7 | -------------------------------------------------------------------------------- /memory-ssa/examples/mm-ssa.ll: -------------------------------------------------------------------------------- 1 | MemorySSA for function: sum 2 | 3 | ; Function Attrs: noinline nounwind optnone ssp uwtable 4 | define void @sum(i32* %arg, i32* %arg1, i32* %arg2, i32 %arg3) #0 { 5 | bb: 6 | %tmp = alloca i32*, align 8 7 | %tmp4 = alloca i32*, align 8 8 | %tmp5 = alloca i32*, align 8 9 | %tmp6 = alloca i32, align 4 10 | %tmp7 = alloca i32, align 4 11 | ; 1 = MemoryDef(liveOnEntry) 12 | store i32* %arg, i32** %tmp, align 8 13 | ; 2 = MemoryDef(1) 14 | store i32* %arg1, i32** %tmp4, align 8 15 | ; 3 = MemoryDef(2) 16 | store i32* %arg2, i32** %tmp5, align 8 17 | ; 4 = MemoryDef(3) 18 | store i32 %arg3, i32* %tmp6, align 4 19 | ; 5 = MemoryDef(4) 20 | store i32 0, i32* %tmp7, align 4 21 | br label %bb8 22 | 23 | bb8: ; preds = %bb28, %bb 24 | ; 8 = MemoryPhi({bb,5},{bb28,7}) 25 | ; MemoryUse(8) 26 | %tmp9 = load i32, i32* %tmp7, align 4 27 | ; MemoryUse(8) 28 | %tmp10 = load i32, i32* %tmp6, align 4 29 | %tmp11 = icmp slt i32 %tmp9, %tmp10 30 | br i1 %tmp11, label %bb12, label %bb31 31 | 32 | bb12: ; preds = %bb8 33 | ; MemoryUse(8) 34 | %tmp13 = load i32*, i32** %tmp, align 8 35 | ; MemoryUse(8) 36 | %tmp14 = load i32, i32* %tmp7, align 4 37 | %tmp15 = sext i32 %tmp14 to i64 38 | %tmp16 = getelementptr inbounds i32, i32* %tmp13, i64 %tmp15 39 | ; MemoryUse(8) 40 | %tmp17 = load i32, i32* %tmp16, align 4 41 | ; MemoryUse(8) 42 | %tmp18 = load i32*, i32** %tmp4, align 8 43 | ; MemoryUse(8) 44 | %tmp19 = load i32, i32* %tmp7, align 4 45 | %tmp20 = sext i32 %tmp19 to i64 46 | %tmp21 = getelementptr inbounds i32, i32* %tmp18, i64 %tmp20 47 | ; MemoryUse(8) 48 | %tmp22 = load i32, i32* %tmp21, align 4 49 | %tmp23 = add nsw i32 %tmp17, %tmp22 50 | ; MemoryUse(8) 51 | %tmp24 = load i32*, i32** %tmp5, align 8 52 | ; MemoryUse(8) 53 | %tmp25 = load i32, i32* %tmp7, align 4 54 | %tmp26 = sext i32 %tmp25 to i64 55 | %tmp27 = getelementptr inbounds i32, i32* %tmp24, i64 %tmp26 56 | ; 6 = MemoryDef(8) 57 | store i32 %tmp23, i32* %tmp27, align 4 58 | br label %bb28 59 | 60 | bb28: ; preds = %bb12 61 | ; MemoryUse(6) 62 | %tmp29 = load i32, i32* %tmp7, align 4 63 | %tmp30 = add nsw i32 %tmp29, 1 64 | ; 7 = MemoryDef(6) 65 | store i32 %tmp30, i32* %tmp7, align 4 66 | br label %bb8 67 | 68 | bb31: ; preds = %bb8 69 | ret void 70 | } 71 | -------------------------------------------------------------------------------- /memory-ssa/examples/sum.c: -------------------------------------------------------------------------------- 1 | void sum(int *a, int *b, int *c, int n) { 2 | for (int i = 0; i < n; ++i) 3 | c[i] = a[i] + b[i]; 4 | } 5 | -------------------------------------------------------------------------------- /mlir-examples/README.md: -------------------------------------------------------------------------------- 1 | # MLIR Toys 2 | 3 | 4 | 5 | ## REF 6 | 7 | - EuroLLVM'19, Tutorial: 8 | - EuroLLVM'19, Chris Lattner's talk: 9 | - MLIR arXiv publication: 10 | - MLIR talks: 11 | -------------------------------------------------------------------------------- /mlir-examples/docs/MLIR C4ML CGO Workshop Talk.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/mlir-examples/docs/MLIR C4ML CGO Workshop Talk.pdf -------------------------------------------------------------------------------- /mlir-examples/docs/eurollvm-19/Keynote-ShpeismanLattner-MLIR.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/mlir-examples/docs/eurollvm-19/Keynote-ShpeismanLattner-MLIR.pdf -------------------------------------------------------------------------------- /mlir-examples/docs/eurollvm-19/Tutorial-AminiVasilacheZinenko-MLIR.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/mlir-examples/docs/eurollvm-19/Tutorial-AminiVasilacheZinenko-MLIR.pdf -------------------------------------------------------------------------------- /soft-pipeline/loop-pipeline.md: -------------------------------------------------------------------------------- 1 | 2 | Tips: 3 | 4 | 硬件加速器(FPGA/ASIC)本质上是在实现一个定制化的计算流水线,所以片上buffer得大小、port数量、并行度等超参数的设置都要服务于流水线。 5 | 流水线中最重要的指标是II(initiation interval),II=1最好,所以编译器朝着这个方向优化,很容易推导出buffer之类的最优参数。 6 | -------------------------------------------------------------------------------- /source-to-source/building_a_cpp_reflection_system_in_one_weekend_using_clang_and_llvm__arvid_gerstmann__cppcon_2018.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/source-to-source/building_a_cpp_reflection_system_in_one_weekend_using_clang_and_llvm__arvid_gerstmann__cppcon_2018.pdf -------------------------------------------------------------------------------- /source-to-source/krzikalla-slides-source-to-source-with-clang.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/source-to-source/krzikalla-slides-source-to-source-with-clang.pdf -------------------------------------------------------------------------------- /static-program-analysis/README.md: -------------------------------------------------------------------------------- 1 | # Static Program Analysis 2 | 3 | Lecture notes on `static program analysis`, by Anders Møller and Michael I. Schwartzbach, at Department of Computer Science, Aarhus University. 4 | 5 | Course text book see: and slides can be downloaded from the course page: . 6 | 7 | 8 | ## REFs 9 | 10 | - SPA course page: 11 | - tipc(a compiler from TIP to llvm bitcode) for the `Static Program Analysis` course lecture notes: 12 | - Partial LLVM pass impl. of Anders Møller's notes: 13 | -------------------------------------------------------------------------------- /type-system/5.SemanticAnalysis.types.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/type-system/5.SemanticAnalysis.types.pdf -------------------------------------------------------------------------------- /type-system/lecture11.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/type-system/lecture11.pdf -------------------------------------------------------------------------------- /ucsd-cse231/README.md: -------------------------------------------------------------------------------- 1 | ## UCSD, CSE231 2 | UCSD, CSE 231: Advanced Compilers, for more details about this course you can checkout this [PAGE](https://cseweb.ucsd.edu/classes/sp14/cse231-a/index.html), for the course project, see this [page](https://cseweb.ucsd.edu/classes/sp14/cse231-a/project.html)... 3 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/README.md: -------------------------------------------------------------------------------- 1 | ## Project1 2 | 3 | This archive is the homework of USCD course, CSE231, Advanced Compiler, for more info about this project, please checkout this [page](https://cseweb.ucsd.edu/classes/sp14/cse231-a/proj1.html) 4 | 5 | ### Notice 6 | Current implementation is based on LLVM 4.0.0 7 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/BranchBias/BranchBias.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/ucsd-cse231/proj1/src/BranchBias/BranchBias.cpp -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/BranchBias/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=branch-bias 2 | COMMON_FLAGS=-Wall -Wextra -g 3 | SRC_DIR?=$(PWD) 4 | SRC=BranchBias.cpp 5 | all: 6 | @echo Compiling $(SRC) 7 | clang++ -g -O3 $(SRC_DIR)/$(SRC) $(COMMON_FLAGS) -I$(SRC_DIR) `llvm-config --cxxflags --ldflags --system-libs --libs irreader bitwriter passes core support` -fno-rtti -o $(PROJECT) 8 | clean: 9 | rm -f $(PROJECT) 10 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/BranchBias/README.md: -------------------------------------------------------------------------------- 1 | ## Count Static Instructions 2 | For more detail about this assignment, checkout this [page](https://cseweb.ucsd.edu/classes/sp14/cse231-a/proj1.html#part3)... 3 | 4 | ### Build 5 | ``` 6 | $ make 7 | $ ./branch-bias your_bc_file 8 | ``` 9 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/BranchBias/benchmark/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | clang -O0 -emit-llvm -c sum.c -o sum.bc 4 | llvm-dis sum.bc 5 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/BranchBias/benchmark/sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | int sum(int a, int b) 3 | { 4 | int c = a + b; 5 | return c; 6 | } 7 | 8 | int main() 9 | { 10 | sum(1, 2); 11 | sum(1, 2); 12 | printf("%d\n", sum(1, 2)); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountDynamicInsts/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=count-dynamic-insts 2 | COMMON_FLAGS=-Wall -Wextra #-g 3 | SRC_DIR?=$(PWD) 4 | SRC=CountDynamicInstructions.cpp 5 | all: 6 | @echo Compiling $(SRC) 7 | clang++ -std=c++11 -O3 $(SRC_DIR)/$(SRC) $(COMMON_FLAGS) -I$(SRC_DIR) `llvm-config --cxxflags --ldflags --system-libs --libs irreader bitwriter passes core support` -fno-rtti -o $(PROJECT) 8 | clean: 9 | rm -f $(PROJECT) 10 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountDynamicInsts/README.md: -------------------------------------------------------------------------------- 1 | ## Count Static Instructions 2 | For more detail about this assignment, checkout this [page](https://cseweb.ucsd.edu/classes/sp14/cse231-a/proj1.html#part2)... 3 | 4 | ### Build 5 | ``` 6 | $ make 7 | $ ./count-dynamic-insts your_bc_file 8 | ``` 9 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountDynamicInsts/benchmark/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | clang -O0 -emit-llvm -c sum.c -o sum.bc 4 | llvm-dis sum.bc 5 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountDynamicInsts/benchmark/sum.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/ucsd-cse231/proj1/src/CountDynamicInsts/benchmark/sum.bc -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountDynamicInsts/benchmark/sum.c: -------------------------------------------------------------------------------- 1 | #include 2 | int sum(int a, int b) { 3 | int c = a + b; 4 | return c; 5 | } 6 | 7 | int main() { 8 | sum(1, 2); 9 | sum(1, 2); 10 | printf("%d\n", sum(1, 2)); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountDynamicInsts/benchmark/sum.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'sum.bc' 2 | source_filename = "sum.c" 3 | target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" 4 | target triple = "x86_64-unknown-linux-gnu" 5 | 6 | @.str = private unnamed_addr constant [4 x i8] c"%d\0A\00", align 1 7 | 8 | ; Function Attrs: nounwind uwtable 9 | define i32 @sum(i32 %a, i32 %b) #0 { 10 | entry: 11 | %a.addr = alloca i32, align 4 12 | %b.addr = alloca i32, align 4 13 | %c = alloca i32, align 4 14 | store i32 %a, i32* %a.addr, align 4 15 | store i32 %b, i32* %b.addr, align 4 16 | %0 = load i32, i32* %a.addr, align 4 17 | %1 = load i32, i32* %b.addr, align 4 18 | %add = add nsw i32 %0, %1 19 | store i32 %add, i32* %c, align 4 20 | %2 = load i32, i32* %c, align 4 21 | ret i32 %2 22 | } 23 | 24 | ; Function Attrs: nounwind uwtable 25 | define i32 @main() #0 { 26 | entry: 27 | %retval = alloca i32, align 4 28 | store i32 0, i32* %retval, align 4 29 | %call = call i32 @sum(i32 1, i32 2) 30 | %call1 = call i32 @sum(i32 1, i32 2) 31 | %call2 = call i32 @sum(i32 1, i32 2) 32 | %call3 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %call2) 33 | ret i32 0 34 | } 35 | 36 | declare i32 @printf(i8*, ...) #1 37 | 38 | attributes #0 = { nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 39 | attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 40 | 41 | !llvm.ident = !{!0} 42 | 43 | !0 = !{!"clang version 4.0.0 (trunk 280809) (llvm/trunk 280808)"} 44 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountStaticInsts/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=count-static-insts 2 | COMMON_FLAGS=-Wall -Wextra -g 3 | SRC_DIR?=$(PWD) 4 | SRC=CountStaticInstructions.cpp 5 | all: 6 | @echo Compiling $(SRC) 7 | clang++ $(SRC) $(COMMON_FLAGS) `llvm-config --cxxflags --ldflags --system-libs --libs irreader bitwriter passes core support` -O3 -o $(PROJECT) 8 | clean: 9 | rm -rf $(PROJECT) ${PROJECT}.dSYM 10 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountStaticInsts/README.md: -------------------------------------------------------------------------------- 1 | ## Count Static Instructions 2 | For more detail about this assignment, checkout this [page](https://cseweb.ucsd.edu/classes/sp14/cse231-a/proj1.html#part1)... 3 | 4 | ### Build 5 | ``` 6 | $ make 7 | $ ./count-static-insts your_bc_file 8 | ``` 9 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountStaticInsts/benchmark/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ###################################### 3 | # LLVM ir assembly file generation. 4 | ###################################### 5 | 6 | set -e 7 | #clang -O0 -emit-llvm -c sum.c -o sum.bc 8 | #llvm-dis sum.bc 9 | #clang -O0 -S -emit-llvm sum.c 10 | for file in `ls *.c` 11 | do 12 | echo compiling ${file}... 13 | clang -O0 -S -emit-llvm ${file} 14 | done 15 | echo DONE! 16 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountStaticInsts/benchmark/foo.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int sum(int a, int b) { 4 | return a + b; 5 | } 6 | 7 | int main() { 8 | sum(1, 2); 9 | } 10 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountStaticInsts/benchmark/foo.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'foo.c' 2 | source_filename = "foo.c" 3 | target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" 4 | target triple = "x86_64-apple-macosx10.13.0" 5 | 6 | ; Function Attrs: noinline nounwind optnone ssp uwtable 7 | define i32 @sum(i32, i32) #0 { 8 | %3 = alloca i32, align 4 9 | %4 = alloca i32, align 4 10 | store i32 %0, i32* %3, align 4 11 | store i32 %1, i32* %4, align 4 12 | %5 = load i32, i32* %3, align 4 13 | %6 = load i32, i32* %4, align 4 14 | %7 = add nsw i32 %5, %6 15 | ret i32 %7 16 | } 17 | 18 | ; Function Attrs: noinline nounwind optnone ssp uwtable 19 | define i32 @main() #0 { 20 | %1 = call i32 @sum(i32 1, i32 2) 21 | ret i32 0 22 | } 23 | 24 | attributes #0 = { noinline nounwind optnone ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 25 | 26 | !llvm.module.flags = !{!0, !1} 27 | !llvm.ident = !{!2} 28 | 29 | !0 = !{i32 1, !"wchar_size", i32 4} 30 | !1 = !{i32 7, !"PIC Level", i32 2} 31 | !2 = !{!"clang version 5.0.0 (tags/RELEASE_500/final)"} 32 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountStaticInsts/benchmark/sum.c: -------------------------------------------------------------------------------- 1 | int sum(int a, int b) 2 | { 3 | int c = a + b; 4 | return c; 5 | } 6 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountStaticInsts/benchmark/sum.ll: -------------------------------------------------------------------------------- 1 | ; ModuleID = 'sum.c' 2 | source_filename = "sum.c" 3 | target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" 4 | target triple = "x86_64-apple-macosx10.13.0" 5 | 6 | ; Function Attrs: noinline nounwind optnone ssp uwtable 7 | define i32 @sum(i32, i32) #0 { 8 | %3 = alloca i32, align 4 9 | %4 = alloca i32, align 4 10 | %5 = alloca i32, align 4 11 | store i32 %0, i32* %3, align 4 12 | store i32 %1, i32* %4, align 4 13 | %6 = load i32, i32* %3, align 4 14 | %7 = load i32, i32* %4, align 4 15 | %8 = add nsw i32 %6, %7 16 | store i32 %8, i32* %5, align 4 17 | %9 = load i32, i32* %5, align 4 18 | ret i32 %9 19 | } 20 | 21 | attributes #0 = { noinline nounwind optnone ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } 22 | 23 | !llvm.module.flags = !{!0, !1} 24 | !llvm.ident = !{!2} 25 | 26 | !0 = !{i32 1, !"wchar_size", i32 4} 27 | !1 = !{i32 7, !"PIC Level", i32 2} 28 | !2 = !{!"clang version 5.0.0 (tags/RELEASE_500/final)"} 29 | -------------------------------------------------------------------------------- /ucsd-cse231/proj1/src/CountStaticInsts/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | PROJ=count-static-insts 3 | TEST_SUITE=benchmark 4 | if [ ! -f ${PROJ} ] 5 | then 6 | echo Please build ${PROJ} 7 | exit 8 | fi 9 | for ir in `ls ${TEST_SUITE}/*.ll` 10 | do 11 | if [ -f ${ir} ] 12 | then 13 | echo ${ir} 14 | ./${PROJ} ${ir} 15 | fi 16 | done 17 | -------------------------------------------------------------------------------- /ucsd-cse231/proj2/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/ucsd-cse231/proj2/README.md -------------------------------------------------------------------------------- /value-range-analysis/Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2016 Json Lee. All rights reserved. 2 | # For more info, see [here](https://github.com/lijiansong) or my [github page](https://lijiansong.github.io/) 3 | LLVM_CONFIG?=llvm-config 4 | 5 | ifndef VERBOSE 6 | QUIET:=@ 7 | endif 8 | 9 | SRC_DIR?=$(PWD) 10 | LDFLAGS+=$(shell $(LLVM_CONFIG) --ldflags) 11 | #Debug flags 12 | COMMON_FLAGS=-Wall -Wextra -g 13 | CXXFLAGS+=$(COMMON_FLAGS) $(shell $(LLVM_CONFIG) --cxxflags) -fno-rtti 14 | CPPFLAGS+=$(shell $(LLVM_CONFIG) --cppflags) -I$(SRC_DIR) 15 | #Add the static libs that may be linked 16 | LLVMLIBS=$(shell $(LLVM_CONFIG) --libs irreader bitwriter passes core support) 17 | SYSTEMLIBS=$(shell $(LLVM_CONFIG) --system-libs) 18 | 19 | PROJECT=llvm-assignment 20 | PROJECT_OBJECTS=LLVMAssignment.o 21 | 22 | default: $(PROJECT) 23 | 24 | #%.o : $(SRC_DIR)/%.cpp 25 | $(PROJECT_OBJECTS) : $(SRC_DIR)/LLVMAssignment.cpp 26 | @echo Compiling $*.cpp 27 | $(QUIET)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< 28 | 29 | $(PROJECT) : $(PROJECT_OBJECTS) 30 | @echo Linking $@ 31 | $(QUIET)$(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) $^ $(LLVMLIBS) $(SYSTEMLIBS) 32 | 33 | clean:: 34 | $(QUIET)rm -f $(PROJECT) $(PROJECT_OBJECTS) 35 | -------------------------------------------------------------------------------- /value-range-analysis/Ref/SBLP2011_douglas.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lijiansong/clang-llvm-tutorial/91e7d8a7e967c44d54e21f5994260175dc1c7f8a/value-range-analysis/Ref/SBLP2011_douglas.pdf -------------------------------------------------------------------------------- /value-range-analysis/ans.txt: -------------------------------------------------------------------------------- 1 | [0..10] 2 | [1..11] 3 | [5..55] 4 | [-55..-5] 5 | [-11..9] 6 | [1..21] 7 | [0..110] 8 | [-10..90] 9 | [1..100] 10 | [-1..11] 11 | [-10..110] 12 | [-10..100] 13 | [-100..1000] 14 | [0..10000] 15 | [0..36] 16 | [5..11] 17 | [4..33] 18 | [-3..60] 19 | [min..10] 20 | [1..10] 21 | [2..100] 22 | [1..100] 23 | [-49..60] 24 | [-79..50] 25 | [-49..110] 26 | [2..90] 27 | [-30..90] 28 | [-30..90] 29 | [10..10] 30 | [10..12] 31 | [0..0] 32 | [0..0] 33 | [0..max] 34 | [min..0] 35 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/gen-bitcode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | int main(int argc, char const *argv[]) 8 | { 9 | for(int i=1;i<=34;++i) 10 | { 11 | stringstream ss; 12 | ss< 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | int main(int argc, char const *argv[]) 9 | { 10 | freopen("run-result.txt","w",stdout); 11 | for(int i=1;i<=34;++i) 12 | { 13 | stringstream ss; 14 | ss<T|F}}"]; 5 | Node0x71ab500:s0 -> Node0x71affb0; 6 | Node0x71ab500:s1 -> Node0x71b0060; 7 | Node0x71affb0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !18\l}"]; 8 | Node0x71affb0 -> Node0x71b01a0; 9 | Node0x71b0060 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end\l}"]; 10 | Node0x71b0060 -> Node0x71b01a0; 11 | Node0x71b01a0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l ret i32 %x.0, !dbg !20\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test10.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | if (a) { 5 | x = 0; 6 | } else { 7 | x = 10; 8 | } 9 | // range of x is [0..10] 10 | if (b) { 11 | y = x + 1; //range of y is [1..11] 12 | } else { 13 | y = x -1; // range of y is [-1..9] 14 | } 15 | // range of y is [-1, 11] 16 | return y; 17 | } 18 | 19 | /// Expected output 20 | /// [-1..11] 21 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test10.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x5849ac0 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %tobool = icmp ne i32 %a, 0, !dbg !19\l br i1 %tobool, label %if.then, label %if.else, !dbg !21\l|{T|F}}"]; 5 | Node0x5849ac0:s0 -> Node0x5861940; 6 | Node0x5849ac0:s1 -> Node0x58619f0; 7 | Node0x5861940 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end, !dbg !22\l}"]; 8 | Node0x5861940 -> Node0x5861b30; 9 | Node0x58619f0 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end\l}"]; 10 | Node0x58619f0 -> Node0x5861b30; 11 | Node0x5861b30 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %tobool1 = icmp ne i32 %b, 0, !dbg !24\l br i1 %tobool1, label %if.then2, label %if.else3, !dbg !26\l|{T|F}}"]; 12 | Node0x5861b30:s0 -> Node0x5862a20; 13 | Node0x5861b30:s1 -> Node0x5862ae0; 14 | Node0x5862a20 [shape=record,label="{if.then2: \l %add = add nsw i32 %x.0, 1, !dbg !27\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !17, metadata\l... !11), !dbg !18\l br label %if.end4, !dbg !29\l}"]; 15 | Node0x5862a20 -> Node0x58637a0; 16 | Node0x5862ae0 [shape=record,label="{if.else3: \l %sub = sub nsw i32 %x.0, 1, !dbg !30\l call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !17, metadata\l... !11), !dbg !18\l br label %if.end4\l}"]; 17 | Node0x5862ae0 -> Node0x58637a0; 18 | Node0x58637a0 [shape=record,label="{if.end4: \l %y.0 = phi i32 [ %add, %if.then2 ], [ %sub, %if.else3 ]\l ret i32 %y.0, !dbg !32\l}"]; 19 | } 20 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test11.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | if (b) { 12 | y = x + 1; //range of y is [1..11] 13 | z = y * x; //range of z is [0..110] 14 | } else { 15 | y = x -1; // range of y is [-1..9] 16 | z = y * x; //range of z is [-10..90] 17 | } 18 | // range of z is [-10, 110] 19 | return z; 20 | } 21 | 22 | /// Expected output 23 | /// [-10..110] 24 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test12.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [100..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | if (b) { 12 | y = x + 1; //range of y is [1..11] 13 | } else { 14 | y = x -1; // range of y is [-1..9] 15 | z = y * x; //range of z is [-10..90] 16 | } 17 | // range of z is [-10, 100] 18 | return z; 19 | } 20 | 21 | /// Expected output 22 | /// [-10..100] 23 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test13.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [-10..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | if (b) { 12 | y = x + 1; //range of y is [1..11] 13 | } else { 14 | y = x -1; // range of y is [-1..9] 15 | z = y * x; //range of z is [-10..90] 16 | } 17 | // range of z is [-10.. 100] 18 | y = z * x; // range of y is [-100..1000] 19 | return y; 20 | } 21 | 22 | /// Expected output 23 | /// [-100..1000] 24 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test14.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [-10..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | if (b) { 12 | y = x + 1; //range of y is [1..11] 13 | } else { 14 | y = x -1; // range of y is [-1..9] 15 | z = y * x; //range of z is [-10..90] 16 | } 17 | // range of z is [-10.. 100] 18 | y = z * z; // range of y is [0..10000] 19 | return y; 20 | } 21 | 22 | /// Expected output 23 | /// [0..10000] 24 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test15.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [-10..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | if (b) { 12 | y = x - 5; //range of y is [-5..5] 13 | } else { 14 | y = x -6; // range of y is [-6..4] 15 | } 16 | // range of y is [-6.. 5] 17 | z = y * y; // range of z is [0..36] 18 | return z; 19 | } 20 | 21 | /// Expected output 22 | /// [0..36] 23 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test16.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [100..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | 13 | if (y < 7) { 14 | // range of y is [1..6] 15 | z = y + 4; //range of z is [5..10] 16 | } else { 17 | z = y; //range of z is[7..11] 18 | } 19 | 20 | return z; //range of z is[5..11] 21 | } 22 | 23 | /// Expected output 24 | /// [5..11] 25 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test16.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x5bd2ba0 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !17, metadata\l... !11), !dbg !18\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %tobool = icmp ne i32 %a, 0, !dbg !21\l br i1 %tobool, label %if.then, label %if.else, !dbg !23\l|{T|F}}"]; 5 | Node0x5bd2ba0:s0 -> Node0x5beadb0; 6 | Node0x5bd2ba0:s1 -> Node0x5beae60; 7 | Node0x5beadb0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end, !dbg !24\l}"]; 8 | Node0x5beadb0 -> Node0x5beafa0; 9 | Node0x5beae60 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end\l}"]; 10 | Node0x5beae60 -> Node0x5beafa0; 11 | Node0x5beafa0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !26\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %cmp = icmp slt i32 %add, 7, !dbg !27\l br i1 %cmp, label %if.then1, label %if.else3, !dbg !29\l|{T|F}}"]; 12 | Node0x5beafa0:s0 -> Node0x5bec8d0; 13 | Node0x5beafa0:s1 -> Node0x5bec990; 14 | Node0x5bec8d0 [shape=record,label="{if.then1: \l %add2 = add nsw i32 %add, 4, !dbg !30\l call void @llvm.dbg.value(metadata i32 %add2, i64 0, metadata !19, metadata\l... !11), !dbg !20\l br label %if.end4, !dbg !32\l}"]; 15 | Node0x5bec8d0 -> Node0x5bd4af0; 16 | Node0x5bec990 [shape=record,label="{if.else3: \l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !19, metadata\l... !11), !dbg !20\l br label %if.end4\l}"]; 17 | Node0x5bec990 -> Node0x5bd4af0; 18 | Node0x5bd4af0 [shape=record,label="{if.end4: \l %z.0 = phi i32 [ %add2, %if.then1 ], [ %add, %if.else3 ]\l ret i32 %z.0, !dbg !33\l}"]; 19 | } 20 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test17.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [100..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | 13 | if (y < 7) { 14 | // range of y is [1..6] 15 | z = y * 4; //range of z is [4..24] 16 | } else { 17 | z = y * 3; //range of z is[21..33] 18 | } 19 | 20 | return z; //range of z is[4..33] 21 | } 22 | 23 | /// Expected output 24 | /// [4..33] 25 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test18.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [100..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | 13 | if (y < 7) { 14 | // range of y is [1..6] 15 | z = y * x; //range of z is [0..60] 16 | } else { 17 | // range of y is [7..11] 18 | z = y -x ; //range of z is[-3..11] 19 | } 20 | 21 | return z; //range of z is[-3..60] 22 | } 23 | 24 | /// Expected output 25 | /// [-3..60] 26 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test19.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 10; 3 | if (a < 10) { 4 | x = a; // range of x is [min..9] 5 | } 6 | return x; // range of x is [min..10] 7 | } 8 | 9 | /// Expected output 10 | /// [min..10] 11 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test19.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x60cc440 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l %cmp = icmp slt i32 %a, 10, !dbg !15\l br i1 %cmp, label %if.then, label %if.end, !dbg !17\l|{T|F}}"]; 5 | Node0x60cc440:s0 -> Node0x60d0ef0; 6 | Node0x60cc440:s1 -> Node0x60d0fa0; 7 | Node0x60d0ef0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !18\l}"]; 8 | Node0x60d0ef0 -> Node0x60d0fa0; 9 | Node0x60d0fa0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ %a, %if.then ], [ 10, %entry ]\l ret i32 %x.0, !dbg !20\l}"]; 10 | } 11 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test2.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 1; 4 | if (a) { 5 | x = 0; 6 | } else { 7 | x = 10; 8 | } 9 | y = x + 1; 10 | return y; 11 | } 12 | 13 | /// Expected output 14 | /// [1..11] 15 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test2.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x6686680 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l %tobool = icmp ne i32 %a, 0, !dbg !17\l br i1 %tobool, label %if.then, label %if.else, !dbg !19\l|{T|F}}"]; 5 | Node0x6686680:s0 -> Node0x668b5c0; 6 | Node0x6686680:s1 -> Node0x668b670; 7 | Node0x668b5c0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !20\l}"]; 8 | Node0x668b5c0 -> Node0x668b7b0; 9 | Node0x668b670 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end\l}"]; 10 | Node0x668b670 -> Node0x668b7b0; 11 | Node0x668b7b0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !22\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !15, metadata\l... !11), !dbg !16\l ret i32 %add, !dbg !23\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test20.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 10; 3 | if (a < 10 && a > 0 ) { 4 | x = a; // range of x is [1..9] 5 | } 6 | return x; // range of x is [1..10] 7 | } 8 | 9 | /// Expected output 10 | /// [1..10] 11 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test20.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x727f5d0 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l %cmp = icmp slt i32 %a, 10, !dbg !15\l br i1 %cmp, label %land.lhs.true, label %if.end, !dbg !17\l|{T|F}}"]; 5 | Node0x727f5d0:s0 -> Node0x7284080; 6 | Node0x727f5d0:s1 -> Node0x7284140; 7 | Node0x7284080 [shape=record,label="{land.lhs.true: \l %cmp1 = icmp sgt i32 %a, 0, !dbg !18\l br i1 %cmp1, label %if.then, label %if.end, !dbg !20\l|{T|F}}"]; 8 | Node0x7284080:s0 -> Node0x72845f0; 9 | Node0x7284080:s1 -> Node0x7284140; 10 | Node0x72845f0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !21\l}"]; 11 | Node0x72845f0 -> Node0x7284140; 12 | Node0x7284140 [shape=record,label="{if.end: \l %x.0 = phi i32 [ %a, %if.then ], [ 10, %land.lhs.true ], [ 10, %entry ]\l ret i32 %x.0, !dbg !23\l}"]; 13 | } 14 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test21.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [100..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | 13 | if (y < x) { 14 | // range of x is [2..10] 15 | z = x; 16 | } 17 | return z; //range of z is[2..100] 18 | } 19 | 20 | /// Expected output 21 | /// [2..100] 22 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test21.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x7235a90 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !17, metadata\l... !11), !dbg !18\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %tobool = icmp ne i32 %a, 0, !dbg !21\l br i1 %tobool, label %if.then, label %if.else, !dbg !23\l|{T|F}}"]; 5 | Node0x7235a90:s0 -> Node0x724dcb0; 6 | Node0x7235a90:s1 -> Node0x724dd60; 7 | Node0x724dcb0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end, !dbg !24\l}"]; 8 | Node0x724dcb0 -> Node0x724dea0; 9 | Node0x724dd60 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end\l}"]; 10 | Node0x724dd60 -> Node0x724dea0; 11 | Node0x724dea0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !26\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %cmp = icmp slt i32 %add, %x.0, !dbg !27\l br i1 %cmp, label %if.then1, label %if.end2, !dbg !29\l|{T|F}}"]; 12 | Node0x724dea0:s0 -> Node0x724f790; 13 | Node0x724dea0:s1 -> Node0x724f850; 14 | Node0x724f790 [shape=record,label="{if.then1: \l call void @llvm.dbg.value(metadata i32 %x.0, i64 0, metadata !19, metadata\l... !11), !dbg !20\l br label %if.end2, !dbg !30\l}"]; 15 | Node0x724f790 -> Node0x724f850; 16 | Node0x724f850 [shape=record,label="{if.end2: \l %z.0 = phi i32 [ %x.0, %if.then1 ], [ 100, %if.end ]\l ret i32 %z.0, !dbg !32\l}"]; 17 | } 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test22.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [100..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | 13 | if (y < x) { 14 | // range of y is [1..9] 15 | z = y; 16 | } 17 | return z; //range of z is[1..100] 18 | } 19 | 20 | /// Expected output 21 | /// [1..100] 22 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test22.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x5b3fa90 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !17, metadata\l... !11), !dbg !18\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %tobool = icmp ne i32 %a, 0, !dbg !21\l br i1 %tobool, label %if.then, label %if.else, !dbg !23\l|{T|F}}"]; 5 | Node0x5b3fa90:s0 -> Node0x5b57cb0; 6 | Node0x5b3fa90:s1 -> Node0x5b57d60; 7 | Node0x5b57cb0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end, !dbg !24\l}"]; 8 | Node0x5b57cb0 -> Node0x5b57ea0; 9 | Node0x5b57d60 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end\l}"]; 10 | Node0x5b57d60 -> Node0x5b57ea0; 11 | Node0x5b57ea0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !26\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %cmp = icmp slt i32 %add, %x.0, !dbg !27\l br i1 %cmp, label %if.then1, label %if.end2, !dbg !29\l|{T|F}}"]; 12 | Node0x5b57ea0:s0 -> Node0x5b59790; 13 | Node0x5b57ea0:s1 -> Node0x5b59850; 14 | Node0x5b59790 [shape=record,label="{if.then1: \l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !19, metadata\l... !11), !dbg !20\l br label %if.end2, !dbg !30\l}"]; 15 | Node0x5b59790 -> Node0x5b59850; 16 | Node0x5b59850 [shape=record,label="{if.end2: \l %z.0 = phi i32 [ %add, %if.then1 ], [ 100, %if.end ]\l ret i32 %z.0, !dbg !32\l}"]; 17 | } 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test23.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [100..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | z = x * y; // range of z is [0..110] 13 | y = 5*x; // range of y is [0 .. 50] 14 | 15 | if (z > y) { 16 | //range of z is [1..110] 17 | z = z - 50; //range of z is [-49..60] 18 | } 19 | return z; //range of z is[-49..60] 20 | } 21 | 22 | /// Expected output 23 | /// [-49..60] 24 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test23.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x5a20c60 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !17, metadata\l... !11), !dbg !18\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %tobool = icmp ne i32 %a, 0, !dbg !21\l br i1 %tobool, label %if.then, label %if.else, !dbg !23\l|{T|F}}"]; 5 | Node0x5a20c60:s0 -> Node0x5a38e70; 6 | Node0x5a20c60:s1 -> Node0x5a38f20; 7 | Node0x5a38e70 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end, !dbg !24\l}"]; 8 | Node0x5a38e70 -> Node0x5a39060; 9 | Node0x5a38f20 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end\l}"]; 10 | Node0x5a38f20 -> Node0x5a39060; 11 | Node0x5a39060 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !26\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %mul = mul nsw i32 %x.0, %add, !dbg !27\l call void @llvm.dbg.value(metadata i32 %mul, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %mul1 = mul nsw i32 5, %x.0, !dbg !28\l call void @llvm.dbg.value(metadata i32 %mul1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %cmp = icmp sgt i32 %mul, %mul1, !dbg !29\l br i1 %cmp, label %if.then2, label %if.end3, !dbg !31\l|{T|F}}"]; 12 | Node0x5a39060:s0 -> Node0x5a3b720; 13 | Node0x5a39060:s1 -> Node0x5a22b60; 14 | Node0x5a3b720 [shape=record,label="{if.then2: \l %sub = sub nsw i32 %mul, 50, !dbg !32\l call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !19, metadata\l... !11), !dbg !20\l br label %if.end3, !dbg !34\l}"]; 15 | Node0x5a3b720 -> Node0x5a22b60; 16 | Node0x5a22b60 [shape=record,label="{if.end3: \l %z.0 = phi i32 [ %sub, %if.then2 ], [ %mul, %if.end ]\l ret i32 %z.0, !dbg !35\l}"]; 17 | } 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test24.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [100..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | z = x * y; // range of z is [0..110] 13 | y = 5*x; // range of y is [0 .. 50] 14 | 15 | if (z > y) { 16 | //range of z is [1..110] 17 | z = z - 80; //range of z is [-79..30] 18 | } 19 | // range of z is [0..50]if the branch is not taken 20 | return z; //range of z is[-79..50] 21 | } 22 | 23 | /// Expected output 24 | /// [-79..50] 25 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test24.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x671ec60 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !17, metadata\l... !11), !dbg !18\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %tobool = icmp ne i32 %a, 0, !dbg !21\l br i1 %tobool, label %if.then, label %if.else, !dbg !23\l|{T|F}}"]; 5 | Node0x671ec60:s0 -> Node0x6736e70; 6 | Node0x671ec60:s1 -> Node0x6736f20; 7 | Node0x6736e70 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end, !dbg !24\l}"]; 8 | Node0x6736e70 -> Node0x6737060; 9 | Node0x6736f20 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end\l}"]; 10 | Node0x6736f20 -> Node0x6737060; 11 | Node0x6737060 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !26\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %mul = mul nsw i32 %x.0, %add, !dbg !27\l call void @llvm.dbg.value(metadata i32 %mul, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %mul1 = mul nsw i32 5, %x.0, !dbg !28\l call void @llvm.dbg.value(metadata i32 %mul1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %cmp = icmp sgt i32 %mul, %mul1, !dbg !29\l br i1 %cmp, label %if.then2, label %if.end3, !dbg !31\l|{T|F}}"]; 12 | Node0x6737060:s0 -> Node0x6739720; 13 | Node0x6737060:s1 -> Node0x6720b60; 14 | Node0x6739720 [shape=record,label="{if.then2: \l %sub = sub nsw i32 %mul, 80, !dbg !32\l call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !19, metadata\l... !11), !dbg !20\l br label %if.end3, !dbg !34\l}"]; 15 | Node0x6739720 -> Node0x6720b60; 16 | Node0x6720b60 [shape=record,label="{if.end3: \l %z.0 = phi i32 [ %sub, %if.then2 ], [ %mul, %if.end ]\l ret i32 %z.0, !dbg !35\l}"]; 17 | } 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test25.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 100; // range of z is [100..100] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | z = x * y; // range of z is [0..110] 13 | y = 5*x; // range of y is [0 .. 50] 14 | 15 | if (z > y) { 16 | //range of z is [1..110] 17 | x = z - y; //range of x is [-49..110] 18 | } 19 | return x; //range of x is[-49..110] 20 | } 21 | 22 | /// Expected output 23 | /// [-49..110] 24 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test25.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x6768c60 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !17, metadata\l... !11), !dbg !18\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %tobool = icmp ne i32 %a, 0, !dbg !21\l br i1 %tobool, label %if.then, label %if.else, !dbg !23\l|{T|F}}"]; 5 | Node0x6768c60:s0 -> Node0x6780e70; 6 | Node0x6768c60:s1 -> Node0x6780f20; 7 | Node0x6780e70 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end, !dbg !24\l}"]; 8 | Node0x6780e70 -> Node0x6781060; 9 | Node0x6780f20 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end\l}"]; 10 | Node0x6780f20 -> Node0x6781060; 11 | Node0x6781060 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !26\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %mul = mul nsw i32 %x.0, %add, !dbg !27\l call void @llvm.dbg.value(metadata i32 %mul, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %mul1 = mul nsw i32 5, %x.0, !dbg !28\l call void @llvm.dbg.value(metadata i32 %mul1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %cmp = icmp sgt i32 %mul, %mul1, !dbg !29\l br i1 %cmp, label %if.then2, label %if.end3, !dbg !31\l|{T|F}}"]; 12 | Node0x6781060:s0 -> Node0x6783720; 13 | Node0x6781060:s1 -> Node0x676ab60; 14 | Node0x6783720 [shape=record,label="{if.then2: \l %sub = sub nsw i32 %mul, %mul1, !dbg !32\l call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end3, !dbg !34\l}"]; 15 | Node0x6783720 -> Node0x676ab60; 16 | Node0x676ab60 [shape=record,label="{if.end3: \l %x.1 = phi i32 [ %sub, %if.then2 ], [ %x.0, %if.end ]\l ret i32 %x.1, !dbg !35\l}"]; 17 | } 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test26.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 10; // range of z is [10..10] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | if (y < x) { 13 | // range of y is [1..9] 14 | // range of x is [2..10] 15 | z = y * x; //range of z is [2..90] 16 | } 17 | return z; //range of z is[2..90] 18 | } 19 | 20 | /// Expected output 21 | /// [2..90] 22 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test26.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x7264af0 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !17, metadata\l... !11), !dbg !18\l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !19, metadata\l... !11), !dbg !20\l %tobool = icmp ne i32 %a, 0, !dbg !21\l br i1 %tobool, label %if.then, label %if.else, !dbg !23\l|{T|F}}"]; 5 | Node0x7264af0:s0 -> Node0x727ce00; 6 | Node0x7264af0:s1 -> Node0x727ceb0; 7 | Node0x727ce00 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end, !dbg !24\l}"]; 8 | Node0x727ce00 -> Node0x727cff0; 9 | Node0x727ceb0 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end\l}"]; 10 | Node0x727ceb0 -> Node0x727cff0; 11 | Node0x727cff0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !26\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %cmp = icmp slt i32 %add, %x.0, !dbg !27\l br i1 %cmp, label %if.then1, label %if.end2, !dbg !29\l|{T|F}}"]; 12 | Node0x727cff0:s0 -> Node0x727e7e0; 13 | Node0x727cff0:s1 -> Node0x727e8a0; 14 | Node0x727e7e0 [shape=record,label="{if.then1: \l %mul = mul nsw i32 %add, %x.0, !dbg !30\l call void @llvm.dbg.value(metadata i32 %mul, i64 0, metadata !19, metadata\l... !11), !dbg !20\l br label %if.end2, !dbg !32\l}"]; 15 | Node0x727e7e0 -> Node0x727e8a0; 16 | Node0x727e8a0 [shape=record,label="{if.end2: \l %z.0 = phi i32 [ %mul, %if.then1 ], [ 10, %if.end ]\l ret i32 %z.0, !dbg !33\l}"]; 17 | } 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test27.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 10; // range of z is [10..10] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | if (y < x) { 13 | // range of y is [1..9] 14 | // range of x is [2..10] 15 | z = y * x; //range of z is [2..90] 16 | } else { 17 | // range of x is [0..10] 18 | // range of y is [1..11] 19 | z = y * x; // range of z is [0..110] 20 | z = z -30; // range of z is [-30..80] 21 | } 22 | return z; //range of z is[-30..90] 23 | } 24 | 25 | /// Expected output 26 | /// [-30..90] 27 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test28.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | int z = 10; // range of z is [10..10] 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; //range of y is [1..11] 12 | if (y < x) { 13 | // range of y is [1..9] 14 | // range of x is [2..10] 15 | z = y * x; //range of z is [2..90] 16 | } else { 17 | // range of x is [0..10] 18 | // range of y is [1..11] 19 | z = y * x; // range of z is [0..110] 20 | z = z -30; // range of z is [-30..80] 21 | } 22 | // range of z is [-30..90] 23 | if (z > 100) { 24 | ///According to the range of z, this branch will not be taken 25 | z = 100; 26 | } 27 | return z; //range of z is[-30..90] 28 | } 29 | 30 | /// Expected output 31 | /// [-30..90] 32 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test29.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 1; 4 | for (; x < 10; ) { 5 | ///First pass, 6 | ///compute x as [0..max], apply the special operator after two iterations 7 | /// old range of x is [0..2], new range of x is [0..3], since 2!=3, widening the upper 8 | /// bound to max, so we have new range of x as [0..max] 9 | ///Second pass, 10 | ///compute x as [0..9], initial range of x is [0..max] 11 | x = x + 1; 12 | } 13 | return x; 14 | } 15 | 16 | /// Expected output 17 | /// [10..10] 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test29.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x72dc710 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %for.cond, !dbg !17\l}"]; 5 | Node0x72dc710 -> Node0x72e1480; 6 | Node0x72e1480 [shape=record,label="{for.cond: \l %x.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]\l %cmp = icmp slt i32 %x.0, 10, !dbg !18\l br i1 %cmp, label %for.body, label %for.end, !dbg !22\l|{T|F}}"]; 7 | Node0x72e1480:s0 -> Node0x72e1760; 8 | Node0x72e1480:s1 -> Node0x72e1b20; 9 | Node0x72e1760 [shape=record,label="{for.body: \l %add = add nsw i32 %x.0, 1, !dbg !23\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %for.cond, !dbg !25, !llvm.loop !27\l}"]; 10 | Node0x72e1760 -> Node0x72e1480; 11 | Node0x72e1b20 [shape=record,label="{for.end: \l ret i32 %x.0, !dbg !28\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test3.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 1; 4 | int z = 1; 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; // range of y is [1..11] 12 | z = 5 * y; // range of z is [5..55] 13 | return z; 14 | } 15 | 16 | /// Expected output 17 | /// [5..55] 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test3.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x6a44810 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %tobool = icmp ne i32 %a, 0, !dbg !19\l br i1 %tobool, label %if.then, label %if.else, !dbg !21\l|{T|F}}"]; 5 | Node0x6a44810:s0 -> Node0x6a49ae0; 6 | Node0x6a44810:s1 -> Node0x6a49b90; 7 | Node0x6a49ae0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !22\l}"]; 8 | Node0x6a49ae0 -> Node0x6a5c6d0; 9 | Node0x6a49b90 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end\l}"]; 10 | Node0x6a49b90 -> Node0x6a5c6d0; 11 | Node0x6a5c6d0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !24\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !15, metadata\l... !11), !dbg !16\l %mul = mul nsw i32 5, %add, !dbg !25\l call void @llvm.dbg.value(metadata i32 %mul, i64 0, metadata !17, metadata\l... !11), !dbg !18\l ret i32 %mul, !dbg !26\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test30.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 1; 4 | for (; x < 10; ) { 5 | ///First pass, 6 | ///compute x as [0..max], apply the special operator after two iterations 7 | /// old range of x is [0..6], new range of x is [0..9], since 6!=9, widening the upper 8 | /// bound to max, so we have new range of x as [0..max] 9 | ///Second pass, 10 | ///compute x as [0..12], initial range of x is [0..max] 11 | 12 | x = x + 3; 13 | } 14 | return x; 15 | } 16 | 17 | /// Expected output 18 | /// [10..12] 19 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test30.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x70b3710 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %for.cond, !dbg !17\l}"]; 5 | Node0x70b3710 -> Node0x70b8480; 6 | Node0x70b8480 [shape=record,label="{for.cond: \l %x.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]\l %cmp = icmp slt i32 %x.0, 10, !dbg !18\l br i1 %cmp, label %for.body, label %for.end, !dbg !22\l|{T|F}}"]; 7 | Node0x70b8480:s0 -> Node0x70b8760; 8 | Node0x70b8480:s1 -> Node0x70b8b20; 9 | Node0x70b8760 [shape=record,label="{for.body: \l %add = add nsw i32 %x.0, 3, !dbg !23\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %for.cond, !dbg !25, !llvm.loop !27\l}"]; 10 | Node0x70b8760 -> Node0x70b8480; 11 | Node0x70b8b20 [shape=record,label="{for.end: \l ret i32 %x.0, !dbg !28\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test31.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 10; 3 | for (; x > 0; ) { 4 | ///First pass, 5 | ///compute x as [min..10], apply the special operator after two iterations 6 | /// old range of x is [8..10], new range of x is [7..10], since 8!=7, widening the lower 7 | /// bound to min, so we have new range of x as [min..10] 8 | ///Second pass, 9 | ///compute x as [1..10], initial range of x is [min..10] 10 | 11 | x = x -1 ; 12 | } 13 | return x; 14 | } 15 | 16 | /// Expected output 17 | /// [0..0] 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test31.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x5cc7640 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %for.cond, !dbg !15\l}"]; 5 | Node0x5cc7640 -> Node0x5ccbf20; 6 | Node0x5ccbf20 [shape=record,label="{for.cond: \l %x.0 = phi i32 [ 10, %entry ], [ %sub, %for.body ]\l %cmp = icmp sgt i32 %x.0, 0, !dbg !16\l br i1 %cmp, label %for.body, label %for.end, !dbg !20\l|{T|F}}"]; 7 | Node0x5ccbf20:s0 -> Node0x5ccc200; 8 | Node0x5ccbf20:s1 -> Node0x5ccc5c0; 9 | Node0x5ccc200 [shape=record,label="{for.body: \l %sub = sub nsw i32 %x.0, 1, !dbg !21\l call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %for.cond, !dbg !23, !llvm.loop !25\l}"]; 10 | Node0x5ccc200 -> Node0x5ccbf20; 11 | Node0x5ccc5c0 [shape=record,label="{for.end: \l ret i32 %x.0, !dbg !26\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test32.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 10; 3 | for (; x > 0; ) { 4 | ///First pass, 5 | ///compute x as [min..max], apply the special operator after two iterations 6 | /// old range of x is [8..12], new range of x is [7..13], widening the lowerbound and the upper bound 7 | /// so we have new range of x as [min..max] 8 | ///Second pass, 9 | ///compute x as [1..max], initial range of x is [min..max] 10 | if (a) 11 | x = x + 1; 12 | else 13 | x = x -1 ; 14 | } 15 | return x; 16 | } 17 | 18 | /// Expected output 19 | /// [0..0] 20 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test32.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x6ad9980 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %for.cond, !dbg !15\l}"]; 5 | Node0x6ad9980 -> Node0x6ade260; 6 | Node0x6ade260 [shape=record,label="{for.cond: \l %x.0 = phi i32 [ 10, %entry ], [ %x.1, %if.end ]\l %cmp = icmp sgt i32 %x.0, 0, !dbg !16\l br i1 %cmp, label %for.body, label %for.end, !dbg !20\l|{T|F}}"]; 7 | Node0x6ade260:s0 -> Node0x6ade8f0; 8 | Node0x6ade260:s1 -> Node0x6ade9b0; 9 | Node0x6ade8f0 [shape=record,label="{for.body: \l %tobool = icmp ne i32 %a, 0, !dbg !21\l br i1 %tobool, label %if.then, label %if.else, !dbg !24\l|{T|F}}"]; 10 | Node0x6ade8f0:s0 -> Node0x6af1800; 11 | Node0x6ade8f0:s1 -> Node0x6af18b0; 12 | Node0x6af1800 [shape=record,label="{if.then: \l %add = add nsw i32 %x.0, 1, !dbg !25\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !26\l}"]; 13 | Node0x6af1800 -> Node0x6ade540; 14 | Node0x6af18b0 [shape=record,label="{if.else: \l %sub = sub nsw i32 %x.0, 1, !dbg !27\l call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end\l}"]; 15 | Node0x6af18b0 -> Node0x6ade540; 16 | Node0x6ade540 [shape=record,label="{if.end: \l %x.1 = phi i32 [ %add, %if.then ], [ %sub, %if.else ]\l br label %for.cond, !dbg !28, !llvm.loop !30\l}"]; 17 | Node0x6ade540 -> Node0x6ade260; 18 | Node0x6ade9b0 [shape=record,label="{for.end: \l ret i32 %x.0, !dbg !31\l}"]; 19 | } 20 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test33.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 0; 4 | for (; x < 10; ) { 5 | x = x + 1; 6 | y = y + 1; 7 | } 8 | return y; 9 | } 10 | 11 | /// Expected output 12 | /// [0..MAX] 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test33.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x6eb1800 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %for.cond, !dbg !17\l}"]; 5 | Node0x6eb1800 -> Node0x6eb6470; 6 | Node0x6eb6470 [shape=record,label="{for.cond: \l %x.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]\l %y.0 = phi i32 [ 0, %entry ], [ %add1, %for.body ]\l %cmp = icmp slt i32 %x.0, 10, !dbg !18\l br i1 %cmp, label %for.body, label %for.end, !dbg !22\l|{T|F}}"]; 7 | Node0x6eb6470:s0 -> Node0x6eb6750; 8 | Node0x6eb6470:s1 -> Node0x6ec96a0; 9 | Node0x6eb6750 [shape=record,label="{for.body: \l %add = add nsw i32 %x.0, 1, !dbg !23\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !13, metadata\l... !11), !dbg !14\l %add1 = add nsw i32 %y.0, 1, !dbg !25\l call void @llvm.dbg.value(metadata i32 %add1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %for.cond, !dbg !26, !llvm.loop !28\l}"]; 10 | Node0x6eb6750 -> Node0x6eb6470; 11 | Node0x6ec96a0 [shape=record,label="{for.end: \l ret i32 %y.0, !dbg !29\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test34.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 0; 4 | for (; x < 10; ) { 5 | x = x + 1; 6 | y = y - 1; 7 | } 8 | return y; 9 | } 10 | 11 | /// Expected output 12 | /// [min..0] 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test34.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x64c7800 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %for.cond, !dbg !17\l}"]; 5 | Node0x64c7800 -> Node0x64cc470; 6 | Node0x64cc470 [shape=record,label="{for.cond: \l %x.0 = phi i32 [ 0, %entry ], [ %add, %for.body ]\l %y.0 = phi i32 [ 0, %entry ], [ %sub, %for.body ]\l %cmp = icmp slt i32 %x.0, 10, !dbg !18\l br i1 %cmp, label %for.body, label %for.end, !dbg !22\l|{T|F}}"]; 7 | Node0x64cc470:s0 -> Node0x64cc750; 8 | Node0x64cc470:s1 -> Node0x64df6a0; 9 | Node0x64cc750 [shape=record,label="{for.body: \l %add = add nsw i32 %x.0, 1, !dbg !23\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !13, metadata\l... !11), !dbg !14\l %sub = sub nsw i32 %y.0, 1, !dbg !25\l call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %for.cond, !dbg !26, !llvm.loop !28\l}"]; 10 | Node0x64cc750 -> Node0x64cc470; 11 | Node0x64df6a0 [shape=record,label="{for.end: \l ret i32 %y.0, !dbg !29\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test4.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 1; 4 | int z = 1; 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; // range of y is [1..11] 12 | z = -5 * y; // range of z is [-55..-5] 13 | return z; 14 | } 15 | 16 | /// Expected output 17 | /// [-55..-5] 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test4.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x6b4c810 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %tobool = icmp ne i32 %a, 0, !dbg !19\l br i1 %tobool, label %if.then, label %if.else, !dbg !21\l|{T|F}}"]; 5 | Node0x6b4c810:s0 -> Node0x6b51ae0; 6 | Node0x6b4c810:s1 -> Node0x6b51b90; 7 | Node0x6b51ae0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !22\l}"]; 8 | Node0x6b51ae0 -> Node0x6b646d0; 9 | Node0x6b51b90 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end\l}"]; 10 | Node0x6b51b90 -> Node0x6b646d0; 11 | Node0x6b646d0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !24\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !15, metadata\l... !11), !dbg !16\l %mul = mul nsw i32 -5, %add, !dbg !25\l call void @llvm.dbg.value(metadata i32 %mul, i64 0, metadata !17, metadata\l... !11), !dbg !18\l ret i32 %mul, !dbg !26\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test5.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 1; 4 | int z = 1; 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; // range of y is [1..11] 12 | z = x - y; // range of z is [-11..9] 13 | return z; 14 | } 15 | 16 | /// Expected output 17 | /// [-11..9] 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test5.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x6754810 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %tobool = icmp ne i32 %a, 0, !dbg !19\l br i1 %tobool, label %if.then, label %if.else, !dbg !21\l|{T|F}}"]; 5 | Node0x6754810:s0 -> Node0x6759ae0; 6 | Node0x6754810:s1 -> Node0x6759b90; 7 | Node0x6759ae0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !22\l}"]; 8 | Node0x6759ae0 -> Node0x676c6d0; 9 | Node0x6759b90 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end\l}"]; 10 | Node0x6759b90 -> Node0x676c6d0; 11 | Node0x676c6d0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !24\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !15, metadata\l... !11), !dbg !16\l %sub = sub nsw i32 %x.0, %add, !dbg !25\l call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !17, metadata\l... !11), !dbg !18\l ret i32 %sub, !dbg !26\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test6.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 1; 4 | int z = 1; 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; // range of y is [1..11] 12 | z = x + y; // range of z is [1..21] 13 | return z; 14 | } 15 | 16 | /// Expected output 17 | /// [1..21] 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test6.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x5f86810 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %tobool = icmp ne i32 %a, 0, !dbg !19\l br i1 %tobool, label %if.then, label %if.else, !dbg !21\l|{T|F}}"]; 5 | Node0x5f86810:s0 -> Node0x5f8bae0; 6 | Node0x5f86810:s1 -> Node0x5f8bb90; 7 | Node0x5f8bae0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !22\l}"]; 8 | Node0x5f8bae0 -> Node0x5f9e6d0; 9 | Node0x5f8bb90 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end\l}"]; 10 | Node0x5f8bb90 -> Node0x5f9e6d0; 11 | Node0x5f9e6d0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !24\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !15, metadata\l... !11), !dbg !16\l %add1 = add nsw i32 %x.0, %add, !dbg !25\l call void @llvm.dbg.value(metadata i32 %add1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l ret i32 %add1, !dbg !26\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test7.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 1; 4 | int z = 1; 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x + 1; // range of y is [1..11] 12 | z = x * y; // range of z is [0..110] 13 | return z; 14 | } 15 | 16 | /// Expected output 17 | /// [0..110] 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test7.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x6768810 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %tobool = icmp ne i32 %a, 0, !dbg !19\l br i1 %tobool, label %if.then, label %if.else, !dbg !21\l|{T|F}}"]; 5 | Node0x6768810:s0 -> Node0x676dae0; 6 | Node0x6768810:s1 -> Node0x676db90; 7 | Node0x676dae0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !22\l}"]; 8 | Node0x676dae0 -> Node0x67806d0; 9 | Node0x676db90 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end\l}"]; 10 | Node0x676db90 -> Node0x67806d0; 11 | Node0x67806d0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %add = add nsw i32 %x.0, 1, !dbg !24\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !15, metadata\l... !11), !dbg !16\l %mul = mul nsw i32 %x.0, %add, !dbg !25\l call void @llvm.dbg.value(metadata i32 %mul, i64 0, metadata !17, metadata\l... !11), !dbg !18\l ret i32 %mul, !dbg !26\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test8.c: -------------------------------------------------------------------------------- 1 | int foo(int a) { 2 | int x = 0; 3 | int y = 1; 4 | int z = 1; 5 | if (a) { 6 | x = 0; 7 | } else { 8 | x = 10; 9 | } 10 | // range of x is [0..10] 11 | y = x - 1; // range of y is [-1..9] 12 | z = x * y; // range of z is [-10..90] 13 | return z; 14 | } 15 | 16 | /// Expected output 17 | /// [-10..90] 18 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test8.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x5c4d810 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 1, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %tobool = icmp ne i32 %a, 0, !dbg !19\l br i1 %tobool, label %if.then, label %if.else, !dbg !21\l|{T|F}}"]; 5 | Node0x5c4d810:s0 -> Node0x5c52ae0; 6 | Node0x5c4d810:s1 -> Node0x5c52b90; 7 | Node0x5c52ae0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end, !dbg !22\l}"]; 8 | Node0x5c52ae0 -> Node0x5c656d0; 9 | Node0x5c52b90 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !13, metadata\l... !11), !dbg !14\l br label %if.end\l}"]; 10 | Node0x5c52b90 -> Node0x5c656d0; 11 | Node0x5c656d0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %sub = sub nsw i32 %x.0, 1, !dbg !24\l call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !15, metadata\l... !11), !dbg !16\l %mul = mul nsw i32 %x.0, %sub, !dbg !25\l call void @llvm.dbg.value(metadata i32 %mul, i64 0, metadata !17, metadata\l... !11), !dbg !18\l ret i32 %mul, !dbg !26\l}"]; 12 | } 13 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test9.c: -------------------------------------------------------------------------------- 1 | int foo(int a, int b) { 2 | int x = 0; 3 | int y = 100; // range of y is [100..100] 4 | if (a) { 5 | x = 0; 6 | } else { 7 | x = 10; 8 | } 9 | // range of x is [0..10] 10 | if (b) { 11 | y = x + 1; //range of y is [1..11] 12 | } 13 | // range of y is [1, 100] 14 | return y; 15 | } 16 | 17 | /// Expected output 18 | /// [1..100] 19 | -------------------------------------------------------------------------------- /value-range-analysis/test-cases/test-cases/test9.dot: -------------------------------------------------------------------------------- 1 | digraph "CFG for 'foo' function" { 2 | label="CFG for 'foo' function"; 3 | 4 | Node0x7192960 [shape=record,label="{entry:\l call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !10, metadata\l... !11), !dbg !12\l call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !13, metadata\l... !11), !dbg !14\l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l call void @llvm.dbg.value(metadata i32 100, i64 0, metadata !17, metadata\l... !11), !dbg !18\l %tobool = icmp ne i32 %a, 0, !dbg !19\l br i1 %tobool, label %if.then, label %if.else, !dbg !21\l|{T|F}}"]; 5 | Node0x7192960:s0 -> Node0x71aa7e0; 6 | Node0x7192960:s1 -> Node0x71aa890; 7 | Node0x71aa7e0 [shape=record,label="{if.then: \l call void @llvm.dbg.value(metadata i32 0, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end, !dbg !22\l}"]; 8 | Node0x71aa7e0 -> Node0x71aa9d0; 9 | Node0x71aa890 [shape=record,label="{if.else: \l call void @llvm.dbg.value(metadata i32 10, i64 0, metadata !15, metadata\l... !11), !dbg !16\l br label %if.end\l}"]; 10 | Node0x71aa890 -> Node0x71aa9d0; 11 | Node0x71aa9d0 [shape=record,label="{if.end: \l %x.0 = phi i32 [ 0, %if.then ], [ 10, %if.else ]\l %tobool1 = icmp ne i32 %b, 0, !dbg !24\l br i1 %tobool1, label %if.then2, label %if.end3, !dbg !26\l|{T|F}}"]; 12 | Node0x71aa9d0:s0 -> Node0x71ab8c0; 13 | Node0x71aa9d0:s1 -> Node0x71ab980; 14 | Node0x71ab8c0 [shape=record,label="{if.then2: \l %add = add nsw i32 %x.0, 1, !dbg !27\l call void @llvm.dbg.value(metadata i32 %add, i64 0, metadata !17, metadata\l... !11), !dbg !18\l br label %if.end3, !dbg !29\l}"]; 15 | Node0x71ab8c0 -> Node0x71ab980; 16 | Node0x71ab980 [shape=record,label="{if.end3: \l %y.0 = phi i32 [ %add, %if.then2 ], [ 100, %if.end ]\l ret i32 %y.0, !dbg !30\l}"]; 17 | } 18 | --------------------------------------------------------------------------------