├── tests ├── test2.input ├── test3.input ├── test8.input ├── test4.input ├── test6.input ├── test8.cargs ├── test1.c ├── test2.c ├── test3.c ├── test2.pattern ├── test7.pattern ├── test7.c ├── CMakeLists.txt ├── test9.c ├── test4.c ├── test3.pattern ├── test1.pattern ├── test9.pattern ├── test5.pattern ├── test4.pattern ├── test-runner.sh ├── test10.cpp ├── test6.c ├── test10.pattern ├── test8.c ├── test6.pattern ├── test8.pattern └── test5.c ├── artifact ├── utils │ ├── jupytext.toml │ ├── requirements.txt │ ├── README.md │ └── slices-analysis.py └── docker │ ├── README.md │ └── Dockerfile ├── assets └── images │ └── BannerDaedalus.png ├── lib ├── debugCommon.cpp ├── CMakeLists.txt ├── reports.cpp ├── daedalusPlugin.cpp ├── PHIGateAnalyzer.cpp └── daedalus.cpp ├── .clang-format ├── include ├── reports.h ├── debugCommon.h ├── daedalus.h ├── PHIGateAnalyzer.h └── ProgramSlice.h ├── .gitignore ├── CMakeLists.txt ├── .github └── workflows │ └── main.yml ├── README.md └── LICENSE.md /tests/test2.input: -------------------------------------------------------------------------------- 1 | 12 -------------------------------------------------------------------------------- /tests/test3.input: -------------------------------------------------------------------------------- 1 | word -------------------------------------------------------------------------------- /tests/test8.input: -------------------------------------------------------------------------------- 1 | 2325 5 -------------------------------------------------------------------------------- /tests/test4.input: -------------------------------------------------------------------------------- 1 | 10 20 30 -------------------------------------------------------------------------------- /tests/test6.input: -------------------------------------------------------------------------------- 1 | -i 3000 -------------------------------------------------------------------------------- /tests/test8.cargs: -------------------------------------------------------------------------------- 1 | -std=gnu99 -------------------------------------------------------------------------------- /artifact/utils/jupytext.toml: -------------------------------------------------------------------------------- 1 | formats = "ipynb,py:percent" 2 | -------------------------------------------------------------------------------- /artifact/utils/requirements.txt: -------------------------------------------------------------------------------- 1 | matplotlib 2 | pandas 3 | jupytext 4 | jupyterlab 5 | -------------------------------------------------------------------------------- /assets/images/BannerDaedalus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lac-dcc/Daedalus/HEAD/assets/images/BannerDaedalus.png -------------------------------------------------------------------------------- /lib/debugCommon.cpp: -------------------------------------------------------------------------------- 1 | #include "../include/debugCommon.h" 2 | #include "llvm/Support/Debug.h" 3 | 4 | using namespace llvm; 5 | 6 | #define DEBUG_TYPE "debugCommon" 7 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: LLVM 2 | IndentWidth: 2 3 | AllowShortIfStatementsOnASingleLine: true 4 | AllowShortFunctionsOnASingleLine: true 5 | AllowShortLambdasOnASingleLine: true 6 | AllowShortLoopsOnASingleLine: true 7 | -------------------------------------------------------------------------------- /tests/test1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char **argv) { 4 | int a = 0, b = 0; 5 | 6 | for (int i = 0; i < argc; ++i) { 7 | a += argc + a * argc + 1; 8 | b += argc + b * argc + 1; 9 | } 10 | printf("%d\n", a); 11 | printf("%d\n", b); 12 | } 13 | -------------------------------------------------------------------------------- /lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(daedalus SHARED 2 | daedalus.cpp 3 | daedalusPlugin.cpp 4 | debugCommon.cpp 5 | ProgramSlice.cpp 6 | reports.cpp 7 | PHIGateAnalyzer.cpp 8 | ) 9 | 10 | target_include_directories(daedalus PUBLIC 11 | "${CMAKE_CURRENT_SOURCE_DIR}/../include") 12 | -------------------------------------------------------------------------------- /artifact/docker/README.md: -------------------------------------------------------------------------------- 1 | Given the Dockerfile in this directory, one needs to run the following commands to build the image: 2 | 3 | ```bash 4 | $ docker build -t daedalus-artifact . 5 | ``` 6 | 7 | After that, it is possible to interact with the experiment results by running the existing container: 8 | 9 | ```bash 10 | $ docker run -ti daedalus-artifact 11 | ``` 12 | -------------------------------------------------------------------------------- /include/reports.h: -------------------------------------------------------------------------------- 1 | #ifndef REPORTS_H 2 | #define REPORTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class ReportWriter { 9 | public: 10 | ReportWriter(const std::filesystem::path &filename); 11 | ~ReportWriter(); 12 | void writeLine(const std::string &line); 13 | 14 | private: 15 | std::ofstream file; 16 | }; 17 | 18 | #endif // REPORTS_H -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | compile_commands.json 3 | .cache/ 4 | docs/ 5 | venv/ 6 | .venv/ 7 | *.ipynb 8 | .ipynb_checkpoints 9 | *.DS_Store 10 | __pycache__/ 11 | *.py[cod] 12 | *$py.class 13 | *.dot 14 | .vscode/ 15 | tests/gen.sh 16 | tests/genall.sh 17 | tests/*.ll 18 | tests/*.bin 19 | tests/*.log 20 | artifact/bash/generated_tests/*.ll 21 | artifact/bash/generated_tests/*.bin 22 | artifact/bash/generated_tests/*.log 23 | .idea/ -------------------------------------------------------------------------------- /tests/test2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int a; 5 | int b; 6 | 7 | int f() { 8 | printf("%d\n", a + b); 9 | return a + b; 10 | } 11 | 12 | void g(int c) { 13 | b = c * b + a; 14 | a = c + a; 15 | } 16 | 17 | int main(int argc, char **argv) { 18 | if (argc < 2) return 42; 19 | a = 3; 20 | b = atoi(argv[1]); 21 | f(); 22 | g(b + a); 23 | a = atoi(argv[1]); 24 | f(); 25 | g(b + a); 26 | f(); 27 | } 28 | -------------------------------------------------------------------------------- /tests/test3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char **argv) { 4 | int char_count = 0; 5 | int sum = 0; 6 | for (int i = 0; i < argc; i++) { 7 | sum = char_count; 8 | for (char *p = argv[1]; *p != '\0'; p++) { 9 | char_count++; 10 | } 11 | int diff = char_count - sum; // slice criterion 12 | printf("%d", diff); 13 | } 14 | printf("Total number of characters in argv: %d\n", char_count); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /tests/test2.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: noinline nounwind optsize willreturn 2 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %0) #3 { 3 | ; CHECK-NEXT: sliceclone_BB_1: 4 | ; CHECK-NEXT: %1 = getelementptr inbounds ptr, ptr %0, i64 1 5 | ; CHECK-NEXT: %2 = load ptr, ptr %1, align 8, !tbaa !11 6 | ; CHECK-NEXT: %3 = tail call i32 @atoi(ptr nocapture noundef %2) #4 7 | ; CHECK-NEXT: %4 = add nsw i32 %3, 3 8 | ; CHECK-NEXT: ret i32 %4 9 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /tests/test7.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: noinline nounwind optsize willreturn 2 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %0) #3 { 3 | ; CHECK-NEXT: sliceclone_BB_1: 4 | ; CHECK-NEXT: %1 = getelementptr inbounds ptr, ptr %0, i64 1 5 | ; CHECK-NEXT: %2 = load ptr, ptr %1, align 8, !tbaa !7 6 | ; CHECK-NEXT: %3 = tail call i32 @atoi(ptr nocapture noundef %2) #4 7 | ; CHECK-NEXT: %4 = add nsw i32 %3, 3 8 | ; CHECK-NEXT: ret i32 %4 9 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /tests/test7.c: -------------------------------------------------------------------------------- 1 | /* test2.c, but with no global variables */ 2 | #include 3 | #include 4 | 5 | int f(int a, int b) { 6 | printf("%d\n", a + b); 7 | return a + b; 8 | } 9 | 10 | void g(int a, int b, int c) { 11 | b = c * b + a; 12 | a = c + a; 13 | } 14 | 15 | int main(int argc, char **argv) { 16 | if (argc < 2) return 42; 17 | int a = 3; 18 | int b = atoi(argv[1]); 19 | f(a, b); 20 | g(a, b, b + a); 21 | a = atoi(argv[1]); 22 | f(a, b); 23 | g(a, b, b + a); 24 | f(a, b); 25 | } 26 | -------------------------------------------------------------------------------- /lib/reports.cpp: -------------------------------------------------------------------------------- 1 | #include "../include/reports.h" 2 | 3 | ReportWriter::ReportWriter(const std::filesystem::path &filename) 4 | : file(filename, std::ios::app) { 5 | if (!file.is_open()) { 6 | throw std::runtime_error("Unable to open file: " + filename.string()); 7 | } 8 | } 9 | 10 | ReportWriter::~ReportWriter() { 11 | if (file.is_open()) { 12 | file.close(); 13 | } 14 | } 15 | 16 | void ReportWriter::writeLine(const std::string &line) { 17 | if (file.is_open()) { 18 | file << line << std::endl; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /include/debugCommon.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUG_COMMON_H 2 | #define DEBUG_COMMON_H 3 | 4 | #include "llvm/IR/Instructions.h" 5 | #include "llvm/Support/Debug.h" 6 | #include 7 | 8 | namespace COLOR { 9 | const std::string BLACK = "\033[30m"; 10 | const std::string RED = "\033[31m"; 11 | const std::string GREEN = "\033[32m"; 12 | const std::string YELLOW = "\033[33m"; 13 | const std::string BLUE = "\033[34m"; 14 | const std::string MAGENTA = "\033[35m"; 15 | const std::string CYAN = "\033[36m"; 16 | const std::string WHITE = "\033[37m"; 17 | const std::string CLEAN = "\033[0m"; 18 | }; // namespace COLOR 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | file(GLOB TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.c" "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") 2 | 3 | configure_file(${CMAKE_CURRENT_SOURCE_DIR}/test-runner.sh ${CMAKE_CURRENT_BINARY_DIR}/test-runner.sh COPYONLY) 4 | 5 | foreach(TEST_SOURCE ${TEST_SOURCES}) 6 | get_filename_component(TEST_NAME ${TEST_SOURCE} NAME_WE) 7 | set(TEST_INPUT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${TEST_NAME}.input") 8 | if (EXISTS ${TEST_INPUT_FILE}) 9 | file(READ ${TEST_INPUT_FILE} TEST_INPUT) 10 | else() 11 | set(TEST_INPUT "") 12 | endif() 13 | add_test(NAME ${TEST_NAME} COMMAND ./test-runner.sh ${TEST_SOURCE} "${TEST_INPUT}") 14 | endforeach() 15 | -------------------------------------------------------------------------------- /artifact/utils/README.md: -------------------------------------------------------------------------------- 1 | # Utilitary Scripts 2 | 3 | The scripts contained in this folder helps doing diverse tasks such plotting graphs given a report file. 4 | 5 | ## Dependencies 6 | 7 | - Python 3.8+ 8 | - venv 9 | - Required Python libraries (see `requirements.txt`). 10 | 11 | 1. Create a virtual environment: 12 | ```bash 13 | python3 -m venv venv 14 | ``` 15 | 2. Activate the virtual environment: 16 | - On Windows: 17 | ```plaintext 18 | venv\Scripts\activate 19 | ``` 20 | - On macOS/Linux: 21 | ```bash 22 | source venv/bin/activate 23 | ``` 24 | 3. Install the dependencies: 25 | ```bash 26 | pip install -r requirements.txt 27 | ``` 28 | 29 | ## Run 30 | 31 | 1. Create a jupyter notebook from the provided source: 32 | ```bash 33 | jupytext --to ipynb slices-analysis.py 34 | ``` 35 | 36 | 2. Start JupyterLab and run `slices-analysis.ipynb` notebook: 37 | ```bash 38 | jupyter lab 39 | ``` 40 | 41 | **Note**: every time you save your modifications to the `.ipynb`, the relative `.py` file will be updated and available to be commited. 42 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16.3) 2 | project(daedalus) 3 | 4 | include(CTest) 5 | 6 | set(CMAKE_CXX_STANDARD 17) 7 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 8 | set(CMAKE_BUILD_TYPE Debug) 9 | 10 | find_package(LLVM REQUIRED CONFIG) 11 | 12 | message( 13 | "LLVM: 14 | Definitions ${LLVM_DEFINITIONS} 15 | Includes ${LLVM_INCLUDE_DIRS} 16 | Libraries ${LLVM_LIBRARY_DIRS} 17 | Targets ${LLVM_TARGETS_TO_BUILD} 18 | " 19 | ) 20 | 21 | include_directories(${LLVM_INCLUDE_DIRS}) 22 | include_directories("include/") 23 | link_directories(${LLVM_LIBRARY_DIRS}) 24 | add_definitions(${LLVM_DEFINITIONS}) 25 | 26 | # Build 27 | if(NOT LLVM_ENABLE_RTTI) 28 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti") 29 | endif() 30 | 31 | include(CheckCXXCompilerFlag) 32 | check_cxx_compiler_flag("-fvisibility-inlines-hidden" SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG) 33 | 34 | if (${SUPPORTS_FVISIBILITY_INLINES_HIDDEN_FLAG} EQUAL "1") 35 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden") 36 | endif() 37 | 38 | add_subdirectory(lib) 39 | add_subdirectory(tests) 40 | -------------------------------------------------------------------------------- /tests/test9.c: -------------------------------------------------------------------------------- 1 | /* Simplified function inspired by stepanov_container */ 2 | #include 3 | #include 4 | 5 | void simplified(double *arr, int64_t idx, int64_t last, double val) { 6 | int64_t i = idx; 7 | while ((last - 1) / 2 > i) { 8 | int64_t left = 2 * i + 2; 9 | int64_t right = 2 * i + 1; 10 | 11 | int64_t max_child = (arr[left] < arr[right]) ? right : left; 12 | arr[i] = arr[max_child]; 13 | 14 | i = max_child; 15 | } 16 | int64_t j = i; 17 | if ((last & 1) == 0) { 18 | int64_t mid = (last - 2) / 2; 19 | if (j == mid) { 20 | int64_t k = (j << 1) | 1; 21 | arr[j] = arr[k]; 22 | j = k; 23 | } 24 | } 25 | while (j > idx) { 26 | int64_t parent = (j - 1) / 2; 27 | if (arr[parent] < val) { 28 | arr[j] = arr[parent]; 29 | j = parent; 30 | } else { 31 | break; 32 | } 33 | } 34 | arr[j] = val; 35 | } 36 | 37 | int main(void) { 38 | double heap[16] = {0}; 39 | for (int i = 0; i < 16; ++i) 40 | heap[i] = (double)(32 - i); 41 | 42 | simplified(heap, 0, 15, 10.5); 43 | 44 | for (int i = 0; i < 8; ++i) 45 | printf("%f\n", heap[i]); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /tests/test4.c: -------------------------------------------------------------------------------- 1 | /* 2 | This is an example of tricky ladder graph, inspired by the LLVM Test Suite program 3 | called "SingleSource/UnitTests/2003-07-09-SignedArgs". 4 | */ 5 | #include 6 | #include 7 | 8 | void tricky_ladder(int a, int b, int c) { 9 | int d = a + b; 10 | if (a < 41) { 11 | if (b < 33) { 12 | if (c < 25) { 13 | d = d * c; 14 | if (c < 17) { 15 | printf("d value: %d\n", d); 16 | } else { 17 | block3: 18 | printf("w value: %d\n", d); 19 | } 20 | } else { 21 | block2: 22 | d = a * d; 23 | printf("y value: %d\n", d); 24 | goto block3; 25 | } 26 | } else { 27 | block1: 28 | printf("q value: %d\n", d); 29 | goto block2; 30 | } 31 | } else { 32 | printf("x value: %d\n", d); 33 | goto block1; 34 | } 35 | int t = d << 16; 36 | printf("t value: %d\n", t); 37 | } 38 | 39 | int main(int argc, char *argv[]) { 40 | if (argc < 4) { 41 | fprintf(stderr, "Usage: %s \n", argv[0]); 42 | return 1; 43 | } 44 | int arg1 = atoi(argv[1]); 45 | int arg2 = atoi(argv[2]); 46 | int arg3 = atoi(argv[3]); 47 | int val1 = arg1 * arg2; 48 | int val2 = arg3 * arg2; 49 | tricky_ladder(argc, val1, val2); 50 | return 0; 51 | } -------------------------------------------------------------------------------- /tests/test3.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: noinline nounwind optsize willreturn 2 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](i32 %0, ptr %1) #2 { 3 | ; CHECK-NEXT: sliceclone_BB_3: 4 | ; CHECK-NEXT: %2 = load ptr, ptr %1, align 8, !tbaa !7 5 | ; CHECK-NEXT: %3 = load i8, ptr %2, align 1, !tbaa !11 6 | ; CHECK-NEXT: %4 = icmp eq i8 %3, 0 7 | ; CHECK-NEXT: br i1 %4, label %sliceclone_BB_4, label %sliceclone_BB_5 8 | ; CHECK-EMPTY: 9 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_5, %sliceclone_BB_3 10 | ; CHECK-NEXT: %5 = phi i32 [ %0, %sliceclone_BB_3 ], [ %9, %sliceclone_BB_5 ] 11 | ; CHECK-NEXT: %6 = sub nsw i32 %5, %0 12 | ; CHECK-NEXT: ret i32 %6 13 | ; CHECK-EMPTY: 14 | ; CHECK-NEXT: sliceclone_BB_5: ; preds = %sliceclone_BB_5, %sliceclone_BB_3 15 | ; CHECK-NEXT: %7 = phi ptr [ %10, %sliceclone_BB_5 ], [ %2, %sliceclone_BB_3 ] 16 | ; CHECK-NEXT: %8 = phi i32 [ %9, %sliceclone_BB_5 ], [ %0, %sliceclone_BB_3 ] 17 | ; CHECK-NEXT: %9 = add nsw i32 %8, 1 18 | ; CHECK-NEXT: %10 = getelementptr inbounds i8, ptr %7, i64 1 19 | ; CHECK-NEXT: %11 = load i8, ptr %10, align 1, !tbaa !11 20 | ; CHECK-NEXT: %12 = icmp eq i8 %11, 0 21 | ; CHECK-NEXT: br i1 %12, label %sliceclone_BB_4, label %sliceclone_BB_5, !llvm.loop !14 22 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /tests/test1.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: nofree nounwind optsize uwtable 2 | ; CHECK-NEXT: define dso_local i32 @main(i32 noundef %0, ptr nocapture noundef readnone %1) #0 { 3 | ; CHECK-NEXT: BB_0: 4 | ; CHECK-NEXT: %2 = icmp sgt i32 %0, 0 5 | ; CHECK-NEXT: br i1 %2, label %BB_1, label %BB_2 6 | ; CHECK-EMPTY: 7 | ; CHECK-NEXT: BB_1: ; preds = %BB_0 8 | ; CHECK-NEXT: %3 = add i32 %0, 1 9 | ; CHECK-NEXT: %4 = add i32 %0, 1 10 | ; CHECK-NEXT: br label %BB_3 11 | ; CHECK-EMPTY: 12 | ; CHECK-NEXT: BB_2: ; preds = %BB_3, %BB_0 13 | ; CHECK-NEXT: %5 = phi i32 [ 0, %BB_0 ], [ %13, %BB_3 ] 14 | ; CHECK-NEXT: %6 = phi i32 [ 0, %BB_0 ], [ %15, %BB_3 ] 15 | ; CHECK-NEXT: %7 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %5) #2 16 | ; CHECK-NEXT: %8 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %6) #2 17 | ; CHECK-NEXT: ret i32 0 18 | ; CHECK-EMPTY: 19 | ; CHECK-NEXT: BB_3: ; preds = %BB_3, %BB_1 20 | ; CHECK-NEXT: %9 = phi i32 [ %16, %BB_3 ], [ 0, %BB_1 ] 21 | ; CHECK-NEXT: %10 = phi i32 [ %15, %BB_3 ], [ 0, %BB_1 ] 22 | ; CHECK-NEXT: %11 = phi i32 [ %13, %BB_3 ], [ 0, %BB_1 ] 23 | ; CHECK-NEXT: %12 = add i32 %11, 1 24 | ; CHECK-NEXT: %13 = mul i32 %12, %3 25 | ; CHECK-NEXT: %14 = add i32 %10, 1 26 | ; CHECK-NEXT: %15 = mul i32 %14, %4 27 | ; CHECK-NEXT: %16 = add nuw nsw i32 %9, 1 28 | ; CHECK-NEXT: %17 = icmp eq i32 %16, %0 29 | ; CHECK-NEXT: br i1 %17, label %BB_2, label %BB_3, !llvm.loop !7 30 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /tests/test9.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: noinline nounwind optsize willreturn 2 | ; CHECK-NEXT: define internal i64 @_daedalus_slice_simplified_[[ID:[0-9]+]](i64 %0, ptr %1, i64 %2) #5 { 3 | ; CHECK-NEXT: sliceclone_BB_0: 4 | ; CHECK-NEXT: %3 = add nsw i64 %2, -1 5 | ; CHECK-NEXT: %4 = sdiv i64 %3, 2 6 | ; CHECK-NEXT: %5 = icmp sgt i64 %4, %0 7 | ; CHECK-NEXT: br i1 %5, label %sliceclone_BB_1, label %sliceclone_BB_2 8 | ; CHECK-EMPTY: 9 | ; CHECK-NEXT: sliceclone_BB_1: ; preds = %sliceclone_BB_1, %sliceclone_BB_0 10 | ; CHECK-NEXT: %6 = phi i64 [ %15, %sliceclone_BB_1 ], [ %0, %sliceclone_BB_0 ] 11 | ; CHECK-NEXT: %7 = shl nsw i64 %6, 1 12 | ; CHECK-NEXT: %8 = add nsw i64 %7, 2 13 | ; CHECK-NEXT: %9 = or i64 %7, 1 14 | ; CHECK-NEXT: %10 = getelementptr inbounds double, ptr %1, i64 %8 15 | ; CHECK-NEXT: %11 = load double, ptr %10, align 8, !tbaa !7 16 | ; CHECK-NEXT: %12 = getelementptr inbounds double, ptr %1, i64 %9 17 | ; CHECK-NEXT: %13 = load double, ptr %12, align 8, !tbaa !7 18 | ; CHECK-NEXT: %14 = fcmp olt double %11, %13 19 | ; CHECK-NEXT: %15 = select i1 %14, i64 %9, i64 %8 20 | ; CHECK-NEXT: %16 = icmp sgt i64 %4, %15 21 | ; CHECK-NEXT: br i1 %16, label %sliceclone_BB_1, label %sliceclone_BB_2, !llvm.loop !11 22 | ; CHECK-EMPTY: 23 | ; CHECK-NEXT: sliceclone_BB_2: ; preds = %sliceclone_BB_1, %sliceclone_BB_0 24 | ; CHECK-NEXT: %17 = phi i64 [ %0, %sliceclone_BB_0 ], [ %15, %sliceclone_BB_1 ] 25 | ; CHECK-NEXT: br label %sliceclone_BB_4 26 | ; CHECK-EMPTY: 27 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_2 28 | ; CHECK-NEXT: %18 = shl nsw i64 %17, 1 29 | ; CHECK-NEXT: ret i64 %18 30 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /artifact/docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM debian:12 2 | WORKDIR /src 3 | 4 | RUN apt update && apt upgrade -y 5 | RUN apt install -y sudo \ 6 | git \ 7 | tcl \ 8 | tcl-dev \ 9 | vim \ 10 | build-essential \ 11 | cmake \ 12 | ninja-build \ 13 | python3 \ 14 | python3-venv 15 | 16 | RUN python3 -m venv /src/venv-py 17 | ENV PATH="/src/venv-py/bin:$PATH" 18 | 19 | # Clone and Build LLVM 17 with the Daedalus and func-merging patches 20 | RUN git clone --depth 100 -b merge-functions-pass https://github.com/Casperento/llvm-project.git 21 | WORKDIR llvm-project 22 | RUN mkdir -p build 23 | RUN cmake -G Ninja \ 24 | -DLLVM_ENABLE_PROJECTS='clang;lld' \ 25 | -DCMAKE_BUILD_TYPE="Release" \ 26 | -DLLVM_TARGETS_TO_BUILD=X86 \ 27 | -DLLVM_ENABLE_ASSERTIONS=On \ 28 | -S llvm -B build 29 | RUN cmake --build build -- -j 20 30 | ENV PATH="/src/llvm-project/build/bin:$PATH" 31 | 32 | # Clone and Build Daedalus 33 | WORKDIR /src 34 | RUN git clone -b main https://github.com/lac-dcc/Daedalus.git 35 | WORKDIR Daedalus 36 | RUN mkdir -p build 37 | RUN cmake -G Ninja -DLLVM_DIR=/src/llvm-project -S . -B build 38 | RUN cmake --build build 39 | 40 | # Clone and Build LLVM 17 - Test Suite 41 | WORKDIR /src 42 | RUN git clone --depth 100 -b daedalus https://github.com/Casperento/llvm-test-suite.git 43 | WORKDIR llvm-test-suite 44 | RUN mkdir -p build 45 | RUN mkdir -p /lit-results 46 | 47 | # Clone daedalus-dbg-toolkit to run the experiment 48 | WORKDIR /src 49 | RUN git clone https://github.com/Casperento/daedalus-dbg-toolkit.git 50 | RUN python3 -m pip install -r /src/daedalus-dbg-toolkit/requirements.txt 51 | 52 | # Run experiment 1 53 | WORKDIR /src/daedalus-dbg-toolkit 54 | RUN ./run-experiment.sh -w 20 \ 55 | -t 120 \ 56 | --llvm-project /src/llvm-project \ 57 | --llvm-test-suite /src/llvm-test-suite \ 58 | --daedalus /src/Daedalus \ 59 | --daedalus-branch main \ 60 | --lit-results /lit-results \ 61 | --errors-dbg /src/daedalus-dbg-toolkit \ 62 | --venv /src/venv-py 63 | 64 | # Run experiment 2 65 | RUN ./run-experiment-2.sh -w 20 \ 66 | -t 120 \ 67 | --llvm-project /src/llvm-project \ 68 | --llvm-test-suite /src/llvm-test-suite \ 69 | --daedalus /src/Daedalus \ 70 | --daedalus-branch main \ 71 | --lit-results /lit-results \ 72 | --errors-dbg /src/daedalus-dbg-toolkit \ 73 | --venv /src/venv-py 74 | -------------------------------------------------------------------------------- /lib/daedalusPlugin.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file daedalusPlugin.cpp 3 | * @brief Daedalus Pass' Plugin Source File 4 | * @author Compilers Lab (UFMG) 5 | * @date 2024-07-08 6 | ***********************************************/ 7 | #include "../include/daedalus.h" 8 | #include "llvm/IR/PassManager.h" 9 | #include "llvm/Transforms/Utils/LCSSA.h" 10 | 11 | using namespace llvm; 12 | 13 | /** 14 | * @brief Registers a custom pipeline for the LLVM pass manager. 15 | * 16 | * @details This function registers "daedalus" into passes pipeline. 17 | * If the name matches "daedalus", it adds specific passes to the MPM. 18 | * 19 | * @param Name The name of the pipeline to register. 20 | * @param MPM The module pass manager to which the pipeline is added. 21 | * @param Pipeline An array reference to the pipeline elements. 22 | * @return True if the pipeline was successfully registered, false otherwise. 23 | */ 24 | bool registerPipeline(StringRef Name, ModulePassManager &MPM, 25 | ArrayRef) { 26 | if (Name == "daedalus") { 27 | MPM.addPass(createModuleToFunctionPassAdaptor(LCSSAPass())); 28 | MPM.addPass(Daedalus::DaedalusPass()); 29 | return true; 30 | } 31 | return false; 32 | } 33 | 34 | /** 35 | * @brief Provides plugin information for the Daedalus LLVM pass manager. 36 | * 37 | * @details This function returns the plugin information required by LLVM to 38 | * load the Daedalus pass. It specifies the LLVM plugin API version, the name of 39 | * the plugin, and the LLVM version string. Additionally, it registers the 40 | * pipeline parsing callback. 41 | * 42 | * @return A PassPluginLibraryInfo struct containing the plugin information. 43 | */ 44 | PassPluginLibraryInfo DaedalusPluginInfo() { 45 | return {LLVM_PLUGIN_API_VERSION, "Daedalus", LLVM_VERSION_STRING, 46 | [](PassBuilder &PB) { 47 | PB.registerPipelineParsingCallback(registerPipeline); 48 | }}; 49 | } 50 | 51 | /** 52 | * @brief Retrieves the Daedalus plugin information for LLVM. 53 | * 54 | * @details This function provides a weakly linked symbol to retrieve the plugin 55 | * information, which is required for the plugin to be recognized by LLVM. 56 | * 57 | * @return The plugin information for the Daedalus pass. 58 | */ 59 | extern "C" LLVM_ATTRIBUTE_WEAK PassPluginLibraryInfo llvmGetPassPluginInfo() { 60 | return DaedalusPluginInfo(); 61 | } 62 | -------------------------------------------------------------------------------- /tests/test5.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: noinline nounwind optsize willreturn 2 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](i64 %0, i64 %1) #9 { 3 | ; CHECK-NEXT: sliceclone_BB_2: 4 | ; CHECK-NEXT: %2 = mul nuw nsw i64 %0, %1 5 | ; CHECK-NEXT: %3 = trunc i64 %2 to i32 6 | ; CHECK-NEXT: %4 = and i32 %3, 1 7 | ; CHECK-NEXT: ret i32 %4 8 | ; CHECK-NEXT: } 9 | ; CHECK-EMPTY: 10 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 11 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](i64 %0, i64 %1) #9 { 12 | ; CHECK-NEXT: sliceclone_BB_2: 13 | ; CHECK-NEXT: %2 = sub nsw i64 %0, %1 14 | ; CHECK-NEXT: %3 = trunc i64 %2 to i32 15 | ; CHECK-NEXT: %4 = sdiv i32 %3, 50 16 | ; CHECK-NEXT: ret i32 %4 17 | ; CHECK-NEXT: } 18 | ; CHECK-EMPTY: 19 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 20 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](i64 %0, i64 %1) #9 { 21 | ; CHECK-NEXT: sliceclone_BB_6: 22 | ; CHECK-NEXT: %2 = mul nuw nsw i64 %0, %1 23 | ; CHECK-NEXT: %3 = trunc i64 %2 to i32 24 | ; CHECK-NEXT: %4 = and i32 %3, 1 25 | ; CHECK-NEXT: ret i32 %4 26 | ; CHECK-NEXT: } 27 | ; CHECK-EMPTY: 28 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 29 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](i64 %0, i64 %1) #9 { 30 | ; CHECK-NEXT: sliceclone_BB_6: 31 | ; CHECK-NEXT: %2 = sub nsw i64 %0, %1 32 | ; CHECK-NEXT: %3 = trunc i64 %2 to i32 33 | ; CHECK-NEXT: %4 = sdiv i32 %3, 50 34 | ; CHECK-NEXT: ret i32 %4 35 | ; CHECK-NEXT: } 36 | ; CHECK-EMPTY: 37 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 38 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_kernel_dynprog_[[ID:[0-9]+]](ptr %0, i32 %1, i64 %2, i64 %3) #9 { 39 | ; CHECK-NEXT: sliceclone_BB_6: 40 | ; CHECK-NEXT: %4 = getelementptr inbounds [50 x i32], ptr %0, i64 %2, i64 %3 41 | ; CHECK-NEXT: %5 = load i32, ptr %4, align 4, !tbaa !11 42 | ; CHECK-NEXT: %6 = add nsw i32 %5, %1 43 | ; CHECK-NEXT: ret i32 %6 44 | ; CHECK-NEXT: } 45 | ; CHECK-EMPTY: 46 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 47 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_kernel_dynprog_[[ID:[0-9]+]](ptr %0, i32 %1, i64 %2, i64 %3, i64 %4) #9 { 48 | ; CHECK-NEXT: sliceclone_BB_6: 49 | ; CHECK-NEXT: %5 = call i32 @_daedalus_slice_kernel_dynprog_[[ID:[0-9]+]](ptr %0, i32 %1, i64 %2, i64 %3) 50 | ; CHECK-NEXT: %6 = getelementptr inbounds [50 x i32], ptr %0, i64 %3, i64 %4 51 | ; CHECK-NEXT: %7 = load i32, ptr %6, align 4, !tbaa !11 52 | ; CHECK-NEXT: %8 = add nsw i32 %5, %7 53 | ; CHECK-NEXT: ret i32 %8 54 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /tests/test4.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: noinline nounwind optsize willreturn 2 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_tricky_ladder_[[ID:[0-9]+]](i32 %0, i32 %1, i32 %2) #3 { 3 | ; CHECK-NEXT: sliceclone_BB_0: 4 | ; CHECK-NEXT: %3 = add nsw i32 %1, %2 5 | ; CHECK-NEXT: %4 = icmp slt i32 %2, 41 6 | ; CHECK-NEXT: br i1 %4, label %sliceclone_BB_1, label %sliceclone_BB_6 7 | ; CHECK-EMPTY: 8 | ; CHECK-NEXT: sliceclone_BB_1: ; preds = %sliceclone_BB_0 9 | ; CHECK-NEXT: %5 = icmp slt i32 %1, 33 10 | ; CHECK-NEXT: br i1 %5, label %sliceclone_BB_2, label %sliceclone_BB_6 11 | ; CHECK-EMPTY: 12 | ; CHECK-NEXT: sliceclone_BB_3: ; preds = %sliceclone_BB_2 13 | ; CHECK-NEXT: %6 = mul nsw i32 %3, %0 14 | ; CHECK-NEXT: %7 = icmp slt i32 %0, 17 15 | ; CHECK-NEXT: br i1 %7, label %sliceclone_BB_4, label %sliceclone_BB_5 16 | ; CHECK-EMPTY: 17 | ; CHECK-NEXT: sliceclone_BB_2: ; preds = %sliceclone_BB_1 18 | ; CHECK-NEXT: %8 = icmp slt i32 %0, 25 19 | ; CHECK-NEXT: br i1 %8, label %sliceclone_BB_3, label %sliceclone_BB_6 20 | ; CHECK-EMPTY: 21 | ; CHECK-NEXT: sliceclone_BB_6: ; preds = %sliceclone_BB_2, %sliceclone_BB_0, %sliceclone_BB_1 22 | ; CHECK-NEXT: %9 = mul nsw i32 %3, %2 23 | ; CHECK-NEXT: br label %sliceclone_BB_5 24 | ; CHECK-EMPTY: 25 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_3 26 | ; CHECK-NEXT: br label %sliceclone_BB_9 27 | ; CHECK-EMPTY: 28 | ; CHECK-NEXT: sliceclone_BB_5: ; preds = %sliceclone_BB_6, %sliceclone_BB_3 29 | ; CHECK-NEXT: %10 = phi i32 [ %6, %sliceclone_BB_3 ], [ %9, %sliceclone_BB_6 ] 30 | ; CHECK-NEXT: br label %sliceclone_BB_9 31 | ; CHECK-EMPTY: 32 | ; CHECK-NEXT: sliceclone_BB_9: ; preds = %sliceclone_BB_5, %sliceclone_BB_4 33 | ; CHECK-NEXT: %11 = phi i32 [ %6, %sliceclone_BB_4 ], [ %10, %sliceclone_BB_5 ] 34 | ; CHECK-NEXT: %12 = shl i32 %11, 16 35 | ; CHECK-NEXT: ret i32 %12 36 | ; CHECK-NEXT: } 37 | ; CHECK-EMPTY: 38 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 39 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %0) #3 { 40 | ; CHECK-NEXT: sliceclone_BB_2: 41 | ; CHECK-NEXT: %1 = getelementptr inbounds ptr, ptr %0, i64 1 42 | ; CHECK-NEXT: %2 = load ptr, ptr %1, align 8, !tbaa !7 43 | ; CHECK-NEXT: %3 = tail call i32 @atoi(ptr nocapture noundef %2) #6 44 | ; CHECK-NEXT: %4 = getelementptr inbounds ptr, ptr %0, i64 2 45 | ; CHECK-NEXT: %5 = load ptr, ptr %4, align 8, !tbaa !7 46 | ; CHECK-NEXT: %6 = tail call i32 @atoi(ptr nocapture noundef %5) #6 47 | ; CHECK-NEXT: %7 = mul nsw i32 %6, %3 48 | ; CHECK-NEXT: ret i32 %7 49 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Build and Test Workflow 2 | 3 | on: 4 | push: 5 | branches: [main] 6 | pull_request: 7 | branches: [main] 8 | 9 | jobs: 10 | build-and-test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | # Checkout your own repo 14 | - uses: actions/checkout@v4 15 | 16 | # Install dependencies 17 | - name: Install dependencies 18 | run: | 19 | sudo apt update 20 | sudo apt install -y git build-essential cmake ninja-build 21 | 22 | # Always clone llvm-project repo (needed even if build is cached) 23 | - name: Clone LLVM Project 24 | run: | 25 | git clone --depth 100 -b merge-functions-pass https://github.com/Casperento/llvm-project.git llvm-project 26 | 27 | # Cache the LLVM *build* directory 28 | - name: Cache LLVM 17 build 29 | id: cache-llvm-17-build 30 | uses: actions/cache@v4 31 | with: 32 | path: llvm-project/build 33 | key: llvm-17-build-${{ runner.os }}-${{ hashFiles('llvm-project/llvm/CMakeLists.txt') }} 34 | restore-keys: | 35 | llvm-17-build-${{ runner.os }}- 36 | 37 | # Only build LLVM if the cache was not hit 38 | - name: Build LLVM 17 39 | if: steps.cache-llvm-17-build.outputs.cache-hit != 'true' 40 | run: | 41 | mkdir -p llvm-project/build 42 | cmake -G Ninja \ 43 | -DLLVM_ENABLE_PROJECTS='clang;compiler-rt;lld' \ 44 | -DCMAKE_BUILD_TYPE=Release \ 45 | -DLLVM_TARGETS_TO_BUILD=X86 \ 46 | -DLLVM_ENABLE_ASSERTIONS=On \ 47 | -S llvm-project/llvm \ 48 | -B llvm-project/build 49 | cmake --build llvm-project/build 50 | 51 | # Force build save 52 | - name: Save LLVM build 53 | if: always() 54 | uses: actions/cache/save@v4 55 | with: 56 | path: llvm-project/build 57 | key: llvm-17-build-${{ runner.os }}-${{ hashFiles('llvm-project/llvm/CMakeLists.txt') }} 58 | 59 | # Add LLVM to PATH 60 | - name: Add LLVM to PATH 61 | run: echo "${{ github.workspace }}/llvm-project/build/bin" >> $GITHUB_PATH 62 | 63 | # Debug Environment 64 | - name: Debug environment 65 | run: | 66 | uname -a 67 | clang --version 68 | llvm-config --version 69 | 70 | # Build your own project (Daedalus) 71 | - name: Build Daedalus 72 | run: | 73 | mkdir -p build 74 | cmake -G Ninja \ 75 | -DLLVM_DIR=${{ github.workspace }}/llvm-project/build/lib/cmake/llvm \ 76 | -S . -B build 77 | cmake --build build 78 | 79 | # Run tests 80 | - name: Run tests with ctest 81 | run: ctest --rerun-failed --output-on-failure --test-dir build 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Daedalus 2 | 3 |

4 | Daedalus drawing
5 |

6 | 7 | Daedalus is an LLVM pass that maps instructions to program slices. Our goal is to use program slices as a unit of program compression by outlining common slices, thereby making code shorter. 8 | 9 | # Table of Contents 10 | 11 | - [Documentation](#documentation) 12 | - [Building](#building) 13 | - [Running](#running) 14 | 15 | ## Documentation 16 | 17 | To generate the project's documentation, execute the following command from the root directory of the repository: 18 | 19 | ```shell 20 | $ doxygen 21 | ``` 22 | 23 | The complete documentation will be generated inside the `docs/` folder. To view it, open `docs/html/index.html` in your web browser. 24 | 25 | ## Building 26 | 27 | Daedalus is an out-of-tree LLVM pass. Therefore, you can compile and install it as a library by doing the following: 28 | 29 | ```shell 30 | $ mkdir build 31 | $ cmake -DLLVM_DIR=$(llvm-config --cmakedir) -S . -B build 32 | $ cmake --build build 33 | ``` 34 | 35 | **Disclaimer**: This pass depends on a custom fork of [LLVM 17](https://github.com/Casperento/llvm-project/tree/merge-functions-pass). 36 | 37 | ## Running 38 | 39 | For a given test file name (without its extension), the following files are created inside the `build/tests/` folder: 40 | 41 | Test Name: `test.c` 42 | - `test.ll`: IR file created before running Daedalus; 43 | - `test.d.ll`: IR file created after running Daedalus; 44 | - `test.bin`: executable created before running Daedalus; 45 | - `test.d.bin`: executable created after running Daedalus; 46 | 47 | If you prefer to run the pass directly, you can simply do the following: 48 | 49 | ```shell 50 | $ opt -passes=mem2reg,lcssa -S {path_to_ll_file} -o {path_to_output_ll_file} 51 | $ opt -passes=daedalus -load-pass-plugin=path/to/lib/libdaedalus.so -S {path_to_ll_file} -o {path_to_output_ll_file} 2>&1 52 | ``` 53 | 54 | ### Visualize Generated Slices 55 | 56 | ```bash 57 | $ opt -load-pass-plugin=path/to/lib/libdaedalus.so -passes=daedalus -dump-dot {path_to_ll_file} 58 | ``` 59 | Here's the result of the running above command assuming its ran on a source file named `test.ll` and the Daedalus path add a new function slice named **slice_foo**: 60 | 61 | - `$(pwd)/test.ll.dump_dot/`: A new directory 62 | - `$(pwd)/test.ll.dump_dot/slice_foo.dot`: **slice_foo** CFG in DOT format 63 | 64 | Now that slice_foo.dot is generated we can visualize with graphviz by running the following command: 65 | ```bash 66 | dot -Tpng slice_foo.dot -o slice_foo.png 67 | ``` 68 | 69 | ### Tests 70 | 71 | After building Daedalus, you can test it using the source files inside the `tests/` folder. 72 | 73 | Run the following commands to run tests: 74 | 75 | ```shell 76 | $ cd build 77 | $ ctest 78 | ``` 79 | -------------------------------------------------------------------------------- /artifact/utils/slices-analysis.py: -------------------------------------------------------------------------------- 1 | # --- 2 | # jupyter: 3 | # jupytext: 4 | # text_representation: 5 | # extension: .py 6 | # format_name: percent 7 | # format_version: '1.3' 8 | # jupytext_version: 1.16.6 9 | # kernelspec: 10 | # display_name: Python 3 (ipykernel) 11 | # language: python 12 | # name: python3 13 | # --- 14 | 15 | # %% [markdown] 16 | # # Notebook to analyze Daedalus reports 17 | # 18 | # This script plots histograms based on the data produced by a Daedalus report. 19 | # 20 | 21 | # %% 22 | import os 23 | import pandas as pd 24 | import matplotlib.pyplot as plt 25 | from IPython.display import display 26 | 27 | def parse_report(file_path): 28 | """ 29 | Parses a Daedalus report and returns summary metrics and merged slice data. 30 | """ 31 | with open(file_path, 'r') as file: 32 | lines = file.readlines() 33 | 34 | parsed_data = {} 35 | current_dict = parsed_data 36 | current_slice = None 37 | 38 | for line in lines: 39 | if line.startswith('mergedSlicesMetadata:'): 40 | current_dict = dict() 41 | elif line.startswith('\t'): 42 | if '=' in line: 43 | key, value = line.split('=') 44 | key = key.strip() 45 | value = int(value.strip()) 46 | current_dict[current_slice][key] = value 47 | else: 48 | current_slice = line.strip(':').strip() 49 | current_dict[current_slice] = {} 50 | elif '=' in line: 51 | key, value = line.split('=') 52 | parsed_data[key.strip()] = int(value.strip()) 53 | 54 | return parsed_data, current_dict 55 | 56 | # Directory containing the log files 57 | log_dir = '../../tests/' 58 | log_files = [os.path.join(log_dir, f) for f in os.listdir(log_dir) if f.endswith('_slices_report.log')] 59 | 60 | # Debug: Check if log files are found 61 | if not log_files: 62 | print("No log files found in the chosen directory!") 63 | else: 64 | print(f"Found {len(log_files)} log files.") 65 | 66 | # Initialize container for summary data 67 | summary_data = [] 68 | 69 | # Process each file 70 | for log_file in log_files: 71 | try: 72 | summary_metrics, mergedSlicesMetadata = parse_report(log_file) 73 | if len(mergedSlicesMetadata) > 0: 74 | print(mergedSlicesMetadata) 75 | summary_metrics['file'] = os.path.basename(log_file) # Add filename for reference 76 | summary_data.append(summary_metrics) 77 | except Exception as e: 78 | print(f"Error processing file {log_file}: {e}") 79 | 80 | # Create summary dataframe 81 | if summary_data: 82 | summary_df = pd.DataFrame(summary_data) 83 | print("Summary DataFrame:") 84 | display(summary_df) 85 | else: 86 | print("No summary data to display.") 87 | 88 | 89 | # %% [markdown] 90 | # ## Histogram of Total Largest Slices After Merging 91 | # 92 | # Plot histogram if sizeOfLargestSliceAfterMerging data is available 93 | # 94 | 95 | # %% 96 | if not summary_df.empty: 97 | fig, ax = plt.subplots(figsize=(10, 6)) 98 | summary_df['sizeOfLargestSliceAfterMerging'].value_counts().sort_index().plot(kind='bar', ax=ax) 99 | ax.set_title('Histogram of Total Largest Slices After Merging') 100 | ax.set_xlabel('Total Slices By Largest Size') 101 | ax.set_ylabel('Count') 102 | plt.tight_layout() 103 | plt.show() 104 | else: 105 | print("No data available to plot the histogram.") 106 | -------------------------------------------------------------------------------- /include/daedalus.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file daedalus.h 3 | * @brief Daedalus Pass Header File 4 | * @author Compilers Lab (UFMG) 5 | * @date 2024-07-08 6 | ***********************************************/ 7 | #ifndef PFHEADER 8 | #define PFHEADER 9 | #include "llvm/Analysis/TargetLibraryInfo.h" 10 | #include "llvm/IR/BasicBlock.h" 11 | #include "llvm/IR/Function.h" 12 | #include "llvm/IR/InstrTypes.h" 13 | #include "llvm/IR/Instruction.h" 14 | #include "llvm/IR/Instructions.h" 15 | #include "llvm/IR/PassManager.h" 16 | #include "llvm/IR/Value.h" 17 | #include "llvm/Passes/PassBuilder.h" 18 | #include "llvm/Passes/PassPlugin.h" 19 | #include "llvm/Support/CommandLine.h" 20 | #include "llvm/Support/raw_ostream.h" 21 | #include 22 | #include 23 | #include 24 | 25 | extern llvm::cl::opt maxFuncParams; 26 | extern llvm::cl::opt maxFuncSize; 27 | extern llvm::cl::opt maxFuncUsers; 28 | 29 | /** 30 | * @brief Represents an outlined program slice 31 | */ 32 | struct SliceStruct { 33 | llvm::Instruction *I; // Criterion 34 | llvm::CallInst *callInst; // CallInst to F 35 | llvm::Function *F; // Slice function 36 | llvm::SmallVector 37 | functionArguments; // Arguments to pass on new function call 38 | std::set 39 | originalInstructionsSet; // Set of instructions from the original function 40 | bool wasRemoved; 41 | }; 42 | 43 | /** 44 | * @brief Determines if an instruction type can be used as slice criterion. 45 | */ 46 | bool canBeSliceCriterion(const llvm::Instruction &I); 47 | 48 | /** 49 | * @brief Attempts to remove an instruction if it meets specific criteria. 50 | */ 51 | uint listInstructionsToRemove( 52 | llvm::Instruction *start, llvm::Instruction *sliceCriterion, 53 | const std::set &constOriginalInst, 54 | std::set &vis, 55 | std::set &toRemove); 56 | 57 | /** 58 | * @brief Removes instructions from slices and simplifies functions. 59 | */ 60 | uint removeInstructions(const std::vector &allSlices, 61 | const std::set &mergeTo, 62 | std::set &toSimplify); 63 | 64 | /** 65 | * @brief Removes a function and its call instructions from the LLVM IR. 66 | */ 67 | void removeCallInstruction(llvm::Function *, llvm::CallInst *, 68 | llvm::Instruction *); 69 | 70 | /** 71 | * @brief Collects and returns a set of instructions from a given function that 72 | * meet certain criteria. 73 | */ 74 | llvm::SmallVector instSetMeetCriterion(llvm::Function *F); 75 | 76 | /** 77 | * @brief Counts the number of instructions in a given function. 78 | */ 79 | unsigned int numberOfInstructions(llvm::Function *F); 80 | 81 | /** 82 | * @brief Counts the number of functions that have been merged into a given 83 | * function. 84 | */ 85 | unsigned int numberOfMergedFunctions( 86 | const llvm::Function *F, 87 | std::map &delToNewFunc); 88 | 89 | /** 90 | * @brief Generates DOT files for a set of functions and stores them in a 91 | * directory. 92 | */ 93 | void functionSlicesToDot(const llvm::Module &M, 94 | const std::set &newFunctions); 95 | 96 | /** 97 | * @brief Analyzes the control flow graph of a function to identify 98 | * try-catch logic. 99 | */ 100 | std::set searchForTryCatchLogic(llvm::Function &F); 101 | 102 | namespace Daedalus { 103 | 104 | struct DaedalusPass : public llvm::PassInfoMixin { 105 | /** 106 | * @brief Runs the Daedalus LLVM pass on a given module. 107 | */ 108 | static llvm::PreservedAnalyses run(llvm::Module &M, 109 | llvm::ModuleAnalysisManager &MAM); 110 | }; 111 | }; // namespace Daedalus 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /tests/test-runner.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# -ne 2 ]; then 4 | printf "Usage: %s SOURCEFILENAME ARGUMENTS" "$0" 5 | exit 1 6 | fi 7 | 8 | command -v clang >/dev/null 2>&1 || { 9 | echo >&2 "clang is required but it's not installed. Aborting..." 10 | exit 1 11 | } 12 | command -v llvm-objcopy >/dev/null 2>&1 || { 13 | echo >&2 "llvm-objcopy is required but it's not installed. Aborting..." 14 | exit 1 15 | } 16 | command -v opt >/dev/null 2>&1 || { 17 | echo >&2 "opt is required but it's not installed. Aborting..." 18 | exit 1 19 | } 20 | command -v FileCheck >/dev/null 2>&1 || { 21 | echo >&2 "FileCheck is required but it's not installed. Aborting..." 22 | exit 1 23 | } 24 | 25 | remove_old_file() { 26 | local FILENAME 27 | FILENAME="$1" 28 | if [ -e "${FILENAME}" ]; then 29 | rm "${FILENAME}" 30 | printf "Old %s file removed...\n" "${FILENAME}" 31 | fi 32 | } 33 | 34 | SOURCEFILENAME="$1" 35 | ARGUMENTS="$2" 36 | SOURCEFOLDER=$(dirname "$SOURCEFILENAME") 37 | BUILDPATH=$(realpath "$SOURCEFOLDER/../build") 38 | BUILDTESTSPATH="$BUILDPATH/tests" 39 | SHAREDOBJECTFILE="$BUILDPATH/lib/libdaedalus.so" 40 | SOURCEFILEBASENAMEWEXT=$(basename "$SOURCEFILENAME" | sed 's/\.[^.]*$//') 41 | SOURCEFILENAMELL="$BUILDTESTSPATH/$SOURCEFILEBASENAMEWEXT.ll" 42 | SOURCEFILENAMEDLL="$BUILDTESTSPATH/$SOURCEFILEBASENAMEWEXT.d.ll" 43 | SOURCEFILENAMEPARENTLL="$BUILDTESTSPATH/$SOURCEFILEBASENAMEWEXT.parent_module.ll" 44 | SLICESREPORTLOGFILE="$BUILDTESTSPATH/${SOURCEFILEBASENAMEWEXT}_slices_report.log" 45 | TRANSFORMATIONLOGFILE="$BUILDTESTSPATH/${SOURCEFILEBASENAMEWEXT}_transformation.log" 46 | ORIGINAL_EXECUTABLE="$BUILDTESTSPATH/$SOURCEFILEBASENAMEWEXT.bin" 47 | FINAL_EXECUTABLE="$BUILDTESTSPATH/$SOURCEFILEBASENAMEWEXT.d.bin" 48 | 49 | CARGS_FILE="$SOURCEFOLDER/$SOURCEFILEBASENAMEWEXT.cargs" 50 | if [ -f "$CARGS_FILE" ]; then 51 | EXTRAPARAMS=$(<"$CARGS_FILE") 52 | echo -e "\nRead extra parameters from ${CARGS_FILE}: $EXTRAPARAMS" 53 | else 54 | EXTRAPARAMS="" 55 | echo -e "\nNo extra parameters provided and ${CARGS_FILE} not found. Proceeding without extra parameters." 56 | fi 57 | 58 | remove_old_file "$SLICESREPORTLOGFILE" 59 | remove_old_file "$TRANSFORMATIONLOGFILE" 60 | remove_old_file "$ORIGINAL_EXECUTABLE" 61 | remove_old_file "$FINAL_EXECUTABLE" 62 | 63 | clang $EXTRAPARAMS -Os -flto -fuse-ld=lld -Wl,--plugin-opt=-lto-embed-bitcode=post-merge-pre-opt "$SOURCEFILENAME" -o "$ORIGINAL_EXECUTABLE" 64 | 65 | llvm-objcopy --dump-section .llvmbc="$SOURCEFILENAMELL" "$ORIGINAL_EXECUTABLE" 66 | 67 | opt -S -passes=mem2reg,lcssa "$SOURCEFILENAMELL" -o "$SOURCEFILENAMELL" 68 | 69 | if ! opt -stats \ 70 | -time-passes \ 71 | -debug-only=daedalus,ProgramSlice,PHIGateAnalyzer \ 72 | -passes=daedalus \ 73 | -load-pass-plugin="$SHAREDOBJECTFILE" \ 74 | -dump-dot \ 75 | -S "$SOURCEFILENAMELL" \ 76 | -o "$SOURCEFILENAMEDLL" &>>"$TRANSFORMATIONLOGFILE"; then 77 | echo "opt exited with error code $?" 78 | echo "Dumping last 50 lines of the transformation log file:" 79 | tail --lines 50 "$TRANSFORMATIONLOGFILE" 80 | fi 81 | 82 | clang $EXTRAPARAMS -Os "$SOURCEFILENAMEDLL" -o "$FINAL_EXECUTABLE" 83 | 84 | if [ -e "$FINAL_EXECUTABLE" ]; then 85 | "$FINAL_EXECUTABLE" $ARGUMENTS >"${SOURCEFILEBASENAMEWEXT}.output" 86 | fi 87 | if [ -e "$ORIGINAL_EXECUTABLE" ]; then 88 | "$ORIGINAL_EXECUTABLE" $ARGUMENTS >"${SOURCEFILEBASENAMEWEXT}.reference_output" 89 | fi 90 | 91 | # Run FileCheck on both possible files, but only one should succeed (not both), and cmp must succeed too 92 | for CHECKFILE in "$SOURCEFILENAMEPARENTLL" \ 93 | "$SOURCEFILENAMEDLL" \ 94 | "$TRANSFORMATIONLOGFILE"; do 95 | if FileCheck "$SOURCEFOLDER/$SOURCEFILEBASENAMEWEXT.pattern" <"$CHECKFILE"; then 96 | CHECK=1 97 | echo -e "\nFileCheck succeed on $CHECKFILE!" 98 | break 99 | fi 100 | echo -e "\nFileCheck failed on $CHECKFILE" 101 | CHECK=0 102 | done 103 | if [ $CHECK -eq 1 ] && cmp -s "${SOURCEFILEBASENAMEWEXT}.output" "${SOURCEFILEBASENAMEWEXT}.reference_output"; then 104 | 105 | # Also check if the output of the bitcode file is the same as the original one using lli over the .ll files 106 | if ! diff <(lli "$SOURCEFILENAMELL" $ARGUMENTS) <(lli "$SOURCEFILENAMEDLL" $ARGUMENTS); then 107 | echo -e "\nlli outputs do not match..." 108 | exit 1 109 | else 110 | echo -e "\nlli outputs match!" 111 | exit 0 112 | fi 113 | else 114 | echo -e "\nFileCheck failed or outputs do not match..." 115 | exit 1 116 | fi 117 | -------------------------------------------------------------------------------- /tests/test10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Mock structures 4 | typedef int Clause_size_t; 5 | typedef float Clause_activity_t; 6 | typedef int Clause_data_t[2]; 7 | 8 | struct Clause { 9 | Clause_size_t size; 10 | Clause_activity_t activity; 11 | Clause_data_t data; 12 | }; 13 | 14 | // Function declarations 15 | void mock_sort_clauses(Clause **, int); 16 | void mock_detach_clause(void *, Clause *); 17 | 18 | int main() { 19 | 20 | // Runtime-dependent solver state 21 | double solver_clause_activity = 0.0; 22 | int solver_clauses_size = 10; // Example size, can be set at runtime 23 | 24 | // Allocate at runtime 25 | Clause **solver_clauses = 26 | (Clause **)malloc(sizeof(Clause *) * solver_clauses_size); 27 | Clause **solver_clauses_data1 = 28 | (Clause **)malloc(sizeof(Clause *) * solver_clauses_size); 29 | char *solver_clauses_data2 = 30 | (char *)malloc(sizeof(char) * solver_clauses_size); 31 | 32 | // Initialize with mock data for demonstration 33 | for (int idx = 0; idx < solver_clauses_size; ++idx) { 34 | solver_clauses[idx] = (Clause *)malloc(sizeof(Clause)); 35 | solver_clauses[idx]->size = 24; 36 | solver_clauses[idx]->activity = (float)idx; 37 | solver_clauses[idx]->data[0] = idx * 2; 38 | solver_clauses[idx]->data[1] = idx * 2 + 1; 39 | solver_clauses_data1[idx] = solver_clauses[idx]; 40 | solver_clauses_data2[idx] = 1; 41 | } 42 | solver_clause_activity = 5.0; 43 | 44 | // All variables declared at function scope 45 | double threshold; 46 | int i = 0, j = 0, k = 0; 47 | Clause *current_clause = nullptr; 48 | int temp_int = 0; 49 | float temp_float = 0.0f; 50 | char temp_char = 0; 51 | int var_index = 0; 52 | Clause *watch_clause = nullptr; 53 | char assignment = 0; 54 | char expected_value = 0; 55 | int phi_i = 0, phi_j = 0, phi_clauses = 0; 56 | 57 | // Block 1: Entry 58 | threshold = solver_clause_activity; 59 | temp_int = solver_clauses_size; 60 | threshold = threshold / (double)temp_int; // Original fdiv 61 | mock_sort_clauses(solver_clauses, solver_clauses_size); 62 | 63 | if (solver_clauses_size > 1) 64 | goto block12; 65 | else 66 | goto block17; 67 | 68 | block12: 69 | i = 0; 70 | j = 0; 71 | 72 | block26: 73 | temp_int = solver_clauses_size; 74 | temp_int = temp_int / 2; // Original sdiv 75 | 76 | current_clause = solver_clauses[i]; 77 | temp_int = current_clause->size; 78 | if (temp_int > 23) 79 | goto block35; 80 | else 81 | goto block55; 82 | 83 | block35: 84 | temp_int = current_clause->data[0]; 85 | var_index = temp_int >> 1; // Original ashr 86 | watch_clause = solver_clauses_data1[var_index]; 87 | 88 | if (watch_clause != current_clause) goto block53; 89 | 90 | assignment = solver_clauses_data2[var_index]; 91 | temp_int = current_clause->data[0]; 92 | temp_int = temp_int & 1; // Original and 93 | expected_value = temp_int ? (char)-assignment : assignment; 94 | if (expected_value == 1) 95 | goto block55; 96 | else 97 | goto block53; 98 | 99 | block53: 100 | mock_detach_clause(NULL, current_clause); 101 | free(current_clause); 102 | phi_clauses = solver_clauses_size; // Original load 103 | goto block59; 104 | 105 | block55: 106 | solver_clauses[j] = current_clause; 107 | j++; 108 | goto block59; 109 | 110 | block59: 111 | i = i + j; 112 | temp_int = solver_clauses_size; 113 | temp_int = temp_int / 2; // Original sdiv 114 | if (i < temp_int) goto block26; 115 | 116 | // Phi node handling 117 | phi_i = i; 118 | phi_j = j; 119 | phi_clauses = solver_clauses_size; 120 | goto block17; 121 | 122 | block17: 123 | if (phi_i < phi_clauses) 124 | goto block22; 125 | else 126 | goto block110; 127 | 128 | block22: 129 | k = phi_i; 130 | 131 | block66: 132 | k = k + j; 133 | if (k >= phi_clauses) goto block110; 134 | 135 | current_clause = solver_clauses[k]; 136 | temp_int = current_clause->size; 137 | if (temp_int > 23) 138 | goto block75; 139 | else 140 | goto block100; 141 | 142 | block75: 143 | temp_int = current_clause->data[0]; 144 | var_index = temp_int >> 1; // Original ashr 145 | watch_clause = solver_clauses_data1[var_index]; 146 | 147 | if (watch_clause != current_clause) goto block93; 148 | 149 | assignment = solver_clauses_data2[var_index]; 150 | temp_int = current_clause->data[0]; 151 | temp_int = temp_int & 1; // Original and 152 | expected_value = temp_int ? (char)-assignment : assignment; 153 | if (expected_value == 1) 154 | goto block100; 155 | else 156 | goto block93; 157 | 158 | block93: 159 | temp_float = current_clause->activity; 160 | if (threshold > (double)temp_float) 161 | goto block98; // Original fcmp 162 | else 163 | goto block100; 164 | 165 | block98: 166 | mock_detach_clause(NULL, current_clause); 167 | free(current_clause); 168 | phi_clauses = solver_clauses_size; // Original load 169 | goto block104; 170 | 171 | block100: 172 | solver_clauses[j] = current_clause; 173 | j++; 174 | goto block104; 175 | 176 | block104: 177 | k++; 178 | if (k < phi_clauses) 179 | goto block66; 180 | else 181 | goto block110; 182 | 183 | block110: 184 | temp_int = k - j; // Original sub 185 | if (temp_int > 0) { // Original icmp 186 | solver_clauses_size = solver_clauses_size - temp_int; // Original sub 187 | } 188 | 189 | return 0; 190 | } 191 | 192 | void mock_sort_clauses(Clause **arr, int size) { 193 | // Empty 194 | } 195 | 196 | void mock_detach_clause(void *solver, Clause *clause) { 197 | // Empty 198 | } -------------------------------------------------------------------------------- /include/PHIGateAnalyzer.h: -------------------------------------------------------------------------------- 1 | #ifndef PHI_GATE_ANALYZER_H 2 | #define PHI_GATE_ANALYZER_H 3 | 4 | #include "llvm/ADT/DenseMap.h" 5 | #include "llvm/ADT/SmallPtrSet.h" 6 | #include "llvm/ADT/SmallVector.h" 7 | #include "llvm/IR/BasicBlock.h" 8 | #include "llvm/IR/CFG.h" 9 | #include "llvm/IR/Dominators.h" 10 | #include "llvm/IR/Function.h" 11 | #include "llvm/IR/Instructions.h" 12 | #include "llvm/IR/Value.h" 13 | #include "llvm/Pass.h" 14 | #include "llvm/Support/raw_ostream.h" 15 | 16 | #include 17 | 18 | namespace llvm { 19 | 20 | /** 21 | * @brief Simple path expression representation without variants 22 | */ 23 | struct PathExpr { 24 | enum ExprType { 25 | EDGE, 26 | UNION, 27 | CONCAT, 28 | LAMBDA, 29 | EMPTY 30 | } Type; 31 | 32 | // For EDGE 33 | const Instruction *Branch; 34 | unsigned SuccessorIndex; 35 | 36 | // For UNION/CONCAT 37 | PathExpr *Lhs; 38 | PathExpr *Rhs; 39 | 40 | // For LAMBDA 41 | const BasicBlock *Predecessor; 42 | 43 | PathExpr() : Type(EMPTY), Branch(nullptr), SuccessorIndex(0), Lhs(nullptr), Rhs(nullptr), Predecessor(nullptr) {} 44 | PathExpr(ExprType T) : Type(T), Branch(nullptr), SuccessorIndex(0), Lhs(nullptr), Rhs(nullptr), Predecessor(nullptr) {} 45 | 46 | ~PathExpr() { 47 | delete Lhs; 48 | delete Rhs; 49 | } 50 | 51 | static PathExpr* createEdge(const Instruction *branch, unsigned idx) { 52 | PathExpr *expr = new PathExpr(EDGE); 53 | expr->Branch = branch; 54 | expr->SuccessorIndex = idx; 55 | return expr; 56 | } 57 | 58 | static PathExpr* createUnion(PathExpr *lhs, PathExpr *rhs) { 59 | PathExpr *expr = new PathExpr(UNION); 60 | expr->Lhs = lhs; 61 | expr->Rhs = rhs; 62 | return expr; 63 | } 64 | 65 | static PathExpr* createConcat(PathExpr *lhs, PathExpr *rhs) { 66 | PathExpr *expr = new PathExpr(CONCAT); 67 | expr->Lhs = lhs; 68 | expr->Rhs = rhs; 69 | return expr; 70 | } 71 | 72 | static PathExpr* createLambda(const BasicBlock *pred = nullptr) { 73 | PathExpr *expr = new PathExpr(LAMBDA); 74 | expr->Predecessor = pred; 75 | return expr; 76 | } 77 | 78 | static PathExpr* createEmpty() { 79 | return new PathExpr(EMPTY); 80 | } 81 | 82 | void print(raw_ostream &OS) const { 83 | switch (Type) { 84 | case EDGE: 85 | OS << "Edge(" << Branch->getParent()->getName() << " -> " << SuccessorIndex << ")"; 86 | break; 87 | case UNION: 88 | OS << "("; 89 | Lhs->print(OS); 90 | OS << " U "; 91 | Rhs->print(OS); 92 | OS << ")"; 93 | break; 94 | case CONCAT: 95 | OS << "("; 96 | Lhs->print(OS); 97 | OS << " . "; 98 | Rhs->print(OS); 99 | OS << ")"; 100 | break; 101 | case LAMBDA: 102 | OS << "Lambda"; 103 | if (Predecessor) { 104 | OS << "[pred:" << Predecessor->getName() << "]"; 105 | } 106 | break; 107 | case EMPTY: 108 | OS << "Empty"; 109 | break; 110 | } 111 | } 112 | }; 113 | 114 | /** 115 | * @brief Implements the algorithm from "Efficient Building and Placing of Gating Functions" by Tu and Padua. 116 | */ 117 | class PHIGateAnalyzer { 118 | public: 119 | PHIGateAnalyzer(Function &F, DominatorTree &DT) : F(F), DT(DT) {} 120 | 121 | std::unordered_map> 122 | getGatesForAllPhis(); 123 | 124 | private: 125 | // --- Core Data Structures from the Paper --- 126 | 127 | /** 128 | * @brief Represents the forest for path compression (Tarjan's algorithm). 129 | * Maps a node to its parent in the disjoint-set forest. 130 | */ 131 | DenseMap Parent; 132 | 133 | /** 134 | * @brief The path expression from a node to its parent in the forest. 135 | */ 136 | DenseMap R; 137 | 138 | /** 139 | * @brief GP(v): The gating path from idom(v) to v. 140 | */ 141 | DenseMap GP; 142 | 143 | /** 144 | * @brief G*(v): The gating path for loop-carried values at v. 145 | */ 146 | DenseMap G_star; 147 | 148 | /** 149 | * @brief Phi(v): Flag indicating if v needs a gating function. 150 | */ 151 | DenseMap NeedsPhi; 152 | 153 | /** 154 | * @brief X(v): Flag indicating if v is in the initial definition set. 155 | */ 156 | DenseMap IsInitialDef; 157 | 158 | // --- Algorithm Helper Functions --- 159 | 160 | void initialize(const SmallPtrSet &InitialDefs); 161 | 162 | // Implements EVAL(e) from the paper using path compression. 163 | // Returns a pair: 164 | std::pair EVAL(DomTreeNode *Node); 165 | 166 | // Implements LINK(u, v) 167 | void LINK(DomTreeNode *u, DomTreeNode *v); 168 | 169 | // Implements UPDATE(v, P) 170 | void UPDATE(DomTreeNode *v, PathExpr *P); 171 | 172 | // Helper to find the root of a node in the forest. 173 | DomTreeNode *FIND(DomTreeNode *Node); 174 | 175 | // Helper to create a path expression for a single edge. 176 | PathExpr *createEdgeExpr(const BasicBlock *From, const BasicBlock *To); 177 | 178 | // Helper to merge two path expressions with the union operator. 179 | PathExpr *mergePaths(PathExpr *P1, PathExpr *P2); 180 | 181 | // Helper to resolve a PathExpr into a list of Value* gates. 182 | void collectGates(PathExpr *Expr, 183 | SmallVectorImpl &Gates, 184 | SmallPtrSetImpl &Visited) const; 185 | 186 | void addBranchGate(const Value *Branch, SmallVectorImpl &Gates, 187 | SmallPtrSetImpl &Visited) const; 188 | 189 | Function &F; 190 | DominatorTree &DT; 191 | }; 192 | 193 | } // namespace llvm 194 | 195 | #endif // PHI_GATE_ANALYZER_H -------------------------------------------------------------------------------- /tests/test6.c: -------------------------------------------------------------------------------- 1 | /* 2 | * LLUBENCHMARK 3 | * Craig Zilles (zilles@cs.wisc.edu) 4 | * http://www.cs.wisc.edu/~zilles/llubenchmark.html 5 | * 6 | * This program is a linked list traversal micro-benchmark, which can 7 | * be used (among other things) to approximate the non-benchmark 8 | * Health. 9 | * 10 | * The benchmark executes for a proscribed number of iterations (-i), 11 | * and on every iteration the lists are traversed and potentially 12 | * extended. The number of lists can be specified (-n) as well as the 13 | * size of the elements in the list (-s). The initial length of the 14 | * lists can be set (-l) as well as the growth rate (-g). The growth 15 | * rate must be non-negative, but can be a floating point number, in 16 | * which case random numbers are used to determine whether a list is 17 | * extended on a particular cycle (all lists are extended 18 | * independently). If the -t option is specified, the insertion 19 | * occurs at the tail, otherwise at the head. If the -d option is 20 | * specified, the elements are dirtied during the traversal (which 21 | * will necessitate a write-back when the data is evicted from the 22 | * cache). 23 | * 24 | * To approximate the non-benchmark Health, use the options: 25 | * -i -g .333 -d -t -n 341 26 | * 27 | * (the growth rate of the lists in health is different for different 28 | * levels of the hierarchy and the constant .333 is just my 29 | * approximation of the growth rate). 30 | * 31 | */ 32 | 33 | #include 34 | #include 35 | #if 0 36 | #include 37 | #else 38 | #define assert(x) 39 | #endif 40 | 41 | /* This file should compile stand alone */ 42 | 43 | struct element { 44 | struct element *next; 45 | int count; 46 | }; 47 | 48 | void usage(char *name) { 49 | printf("%s:\n", name); 50 | printf("-i \n"); 51 | printf("[-l ] (default 1)\n"); 52 | printf("[-n <(N)umber of lists>] (default 1 list)\n"); 53 | printf("[-s <(S)ize of element>] (default 32 bytes)\n"); 54 | printf( 55 | "[-g <(G)rowth rate per list, in elements per iteration>] (default 0)\n"); 56 | printf("[-d] ((D)irty each element during traversal, default off)\n"); 57 | printf("[-t] (insert at (T)ail of list, default off)\n"); 58 | } 59 | 60 | #define ALLOC_SIZE 127 /* pick wierd num to break strides */ 61 | struct element *free_list = NULL; 62 | int next_free = ALLOC_SIZE; 63 | int element_size = 32; 64 | int num_allocated = 0; 65 | 66 | #if 0 67 | struct element * 68 | allocate() { 69 | if (next_free == ALLOC_SIZE) { 70 | next_free = 0; 71 | free_list = (struct element *) malloc (ALLOC_SIZE * element_size); 72 | assert(free_list != 0); 73 | } 74 | num_allocated ++; 75 | return (struct element *) 76 | (((char *)free_list) + ((next_free ++) * element_size)); 77 | } 78 | #else 79 | struct element *allocate() { 80 | num_allocated++; 81 | return (struct element *)malloc(sizeof(struct element)); 82 | } 83 | #endif 84 | 85 | int main(int argc, char *argv[]) { 86 | int max_iterations = 1000, dirty = 1, num_lists = 196, tail = 1, 87 | initial_length = 1; 88 | float growth_rate = 0.333; 89 | char c = 0; 90 | int i = 0, j = 0, k = 0; 91 | int accumulate = 0; 92 | 93 | struct element **lists = NULL; 94 | float growth = 0.0; 95 | 96 | int arg = 1; 97 | 98 | printf("This benchmark modified to not use hard coded pool allocation!\n"); 99 | while (arg < argc) { 100 | if ((argv[arg][0] != '-') || (argv[arg][2] != 0)) { 101 | printf("parse error in %s\n", argv[arg]); 102 | usage(argv[0]); 103 | return (-1); 104 | } 105 | c = argv[arg][1]; 106 | arg++; 107 | switch (c) { 108 | case 'd': 109 | dirty = 1; 110 | break; 111 | case 'g': 112 | growth_rate = atof(argv[arg++]); 113 | break; 114 | case 'i': 115 | max_iterations = atoi(argv[arg++]); 116 | break; 117 | case 'l': 118 | initial_length = atoi(argv[arg++]); 119 | break; 120 | case 'n': 121 | num_lists = atoi(argv[arg++]); 122 | break; 123 | case 's': 124 | element_size = atoi(argv[arg++]); 125 | break; 126 | case 't': 127 | tail = 1; 128 | break; 129 | default: 130 | printf("unrecognized option: %c\n", c); 131 | usage(argv[0]); 132 | return (-1); 133 | } 134 | } 135 | 136 | assert(element_size > sizeof(struct element)); 137 | assert(initial_length > 0); 138 | 139 | /* build lists */ 140 | lists = (struct element **)malloc(num_lists * sizeof(struct element *)); 141 | assert(lists != 0); 142 | 143 | for (i = 0; i < num_lists; i++) { 144 | lists[i] = NULL; 145 | } 146 | 147 | for (i = 0; i < initial_length; i++) { 148 | for (j = 0; j < num_lists; j++) { 149 | struct element *e = allocate(); 150 | e->next = lists[j]; 151 | e->count = 0; 152 | lists[j] = e; 153 | } 154 | } 155 | 156 | /* iterate */ 157 | for (i = 0; i < max_iterations; i++) { 158 | if ((i % 1000) == 0) { 159 | printf("%d\n", i); 160 | } 161 | /* traverse lists */ 162 | for (j = 0; j < num_lists; j++) { 163 | struct element *trav = lists[j]; 164 | while (trav != NULL) { 165 | accumulate += trav->count; 166 | if (dirty) { 167 | trav->count++; 168 | } 169 | trav = trav->next; 170 | } 171 | } 172 | 173 | /* grow lists */ 174 | growth += growth_rate; 175 | j = growth; 176 | growth -= j; 177 | for (; j > 0; j--) { 178 | for (k = 0; k < num_lists; k++) { 179 | struct element *e = allocate(); 180 | e->count = k + j; 181 | if (tail) { 182 | struct element *trav = lists[k]; 183 | while (trav->next != NULL) { 184 | trav = trav->next; 185 | } 186 | trav->next = e; 187 | e->next = NULL; 188 | } else { 189 | e->next = lists[k]; 190 | lists[k] = e; 191 | } 192 | } 193 | } 194 | } 195 | printf("output = %d\n", accumulate); 196 | 197 | printf("num allocated %d\n", num_allocated); 198 | return 0; 199 | } 200 | -------------------------------------------------------------------------------- /tests/test10.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: noinline nounwind optsize willreturn 2 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](i64 %0) #4 { 3 | ; CHECK-NEXT: sliceclone_BB_1: 4 | ; CHECK-NEXT: %1 = shl nuw nsw i64 %0, 1 5 | ; CHECK-NEXT: %2 = trunc i64 %1 to i32 6 | ; CHECK-NEXT: %3 = or i32 %2, 1 7 | ; CHECK-NEXT: ret i32 %3 8 | ; CHECK-NEXT: } 9 | ; CHECK-EMPTY: 10 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 11 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %0, i32 %1) #4 { 12 | ; CHECK-NEXT: sliceclone_BB_2: 13 | ; CHECK-NEXT: %2 = sext i32 %1 to i64 14 | ; CHECK-NEXT: %3 = getelementptr inbounds ptr, ptr %0, i64 %2 15 | ; CHECK-NEXT: %4 = load ptr, ptr %3, align 8, !tbaa !10 16 | ; CHECK-NEXT: br label %sliceclone_BB_3 17 | ; CHECK-EMPTY: 18 | ; CHECK-NEXT: sliceclone_BB_3: ; preds = %sliceclone_BB_2 19 | ; CHECK-NEXT: %5 = getelementptr inbounds %struct.Clause, ptr %4, i64 0, i32 2 20 | ; CHECK-NEXT: %6 = load i32, ptr %5, align 4, !tbaa !17 21 | ; CHECK-NEXT: %7 = ashr i32 %6, 1 22 | ; CHECK-NEXT: ret i32 %7 23 | ; CHECK-NEXT: } 24 | ; CHECK-EMPTY: 25 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 26 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %0, i32 %1) #4 { 27 | ; CHECK-NEXT: sliceclone_BB_2: 28 | ; CHECK-NEXT: %2 = sext i32 %1 to i64 29 | ; CHECK-NEXT: %3 = getelementptr inbounds ptr, ptr %0, i64 %2 30 | ; CHECK-NEXT: %4 = load ptr, ptr %3, align 8, !tbaa !10 31 | ; CHECK-NEXT: br label %sliceclone_BB_3 32 | ; CHECK-EMPTY: 33 | ; CHECK-NEXT: sliceclone_BB_3: ; preds = %sliceclone_BB_2 34 | ; CHECK-NEXT: %5 = getelementptr inbounds %struct.Clause, ptr %4, i64 0, i32 2 35 | ; CHECK-NEXT: %6 = load i32, ptr %5, align 4, !tbaa !17 36 | ; CHECK-NEXT: br label %sliceclone_BB_4 37 | ; CHECK-EMPTY: 38 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_3 39 | ; CHECK-NEXT: %7 = and i32 %6, 1 40 | ; CHECK-NEXT: ret i32 %7 41 | ; CHECK-NEXT: } 42 | ; CHECK-EMPTY: 43 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 44 | ; CHECK-NEXT: define internal i8 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %0, ptr %1, i32 %2) #4 { 45 | ; CHECK-NEXT: sliceclone_BB_3: 46 | ; CHECK-NEXT: %3 = call i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %1, i32 %2) 47 | ; CHECK-NEXT: %4 = sext i32 %3 to i64 48 | ; CHECK-NEXT: br label %sliceclone_BB_4 49 | ; CHECK-EMPTY: 50 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_3 51 | ; CHECK-NEXT: %5 = getelementptr inbounds i8, ptr %0, i64 %4 52 | ; CHECK-NEXT: %6 = load i8, ptr %5, align 1, !tbaa !7 53 | ; CHECK-NEXT: %7 = sub i8 0, %6 54 | ; CHECK-NEXT: ret i8 %7 55 | ; CHECK-NEXT: } 56 | ; CHECK-EMPTY: 57 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 58 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](i32 %0, i32 %1, ptr %2, ptr %3, ptr %4) #4 { 59 | ; CHECK-NEXT: sliceclone_BB_2: 60 | ; CHECK-NEXT: %5 = sext i32 %0 to i64 61 | ; CHECK-NEXT: %6 = getelementptr inbounds ptr, ptr %3, i64 %5 62 | ; CHECK-NEXT: %7 = load ptr, ptr %6, align 8, !tbaa !10 63 | ; CHECK-NEXT: %8 = load i32, ptr %7, align 4, !tbaa !12 64 | ; CHECK-NEXT: %9 = icmp sgt i32 %8, 23 65 | ; CHECK-NEXT: br i1 %9, label %sliceclone_BB_3, label %sliceclone_BB_6 66 | ; CHECK-EMPTY: 67 | ; CHECK-NEXT: sliceclone_BB_7: ; preds = %sliceclone_BB_5, %sliceclone_BB_6 68 | ; CHECK-NEXT: %10 = phi i32 [ %1, %sliceclone_BB_5 ], [ %17, %sliceclone_BB_6 ] 69 | ; CHECK-NEXT: %11 = add nsw i32 %10, %0 70 | ; CHECK-NEXT: ret i32 %11 71 | ; CHECK-EMPTY: 72 | ; CHECK-NEXT: sliceclone_BB_3: ; preds = %sliceclone_BB_2 73 | ; CHECK-NEXT: %12 = call i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %3, i32 %0) 74 | ; CHECK-NEXT: %13 = sext i32 %12 to i64 75 | ; CHECK-NEXT: %14 = getelementptr inbounds ptr, ptr %4, i64 %13 76 | ; CHECK-NEXT: %15 = load ptr, ptr %14, align 8, !tbaa !10 77 | ; CHECK-NEXT: %16 = icmp eq ptr %15, %7 78 | ; CHECK-NEXT: br i1 %16, label %sliceclone_BB_4, label %sliceclone_BB_5 79 | ; CHECK-EMPTY: 80 | ; CHECK-NEXT: sliceclone_BB_6: ; preds = %sliceclone_BB_4, %sliceclone_BB_2 81 | ; CHECK-NEXT: %17 = add nsw i32 %1, 1 82 | ; CHECK-NEXT: br label %sliceclone_BB_7 83 | ; CHECK-EMPTY: 84 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_3 85 | ; CHECK-NEXT: %18 = getelementptr inbounds i8, ptr %2, i64 %13 86 | ; CHECK-NEXT: %19 = load i8, ptr %18, align 1, !tbaa !7 87 | ; CHECK-NEXT: %20 = call i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %3, i32 %0) 88 | ; CHECK-NEXT: %21 = icmp eq i32 %20, 0 89 | ; CHECK-NEXT: %22 = call i8 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %2, ptr %3, i32 %0) 90 | ; CHECK-NEXT: %23 = select i1 %21, i8 %19, i8 %22 91 | ; CHECK-NEXT: %24 = icmp eq i8 %23, 1 92 | ; CHECK-NEXT: br i1 %24, label %sliceclone_BB_6, label %sliceclone_BB_5 93 | ; CHECK-EMPTY: 94 | ; CHECK-NEXT: sliceclone_BB_5: ; preds = %sliceclone_BB_4, %sliceclone_BB_3 95 | ; CHECK-NEXT: br label %sliceclone_BB_7 96 | ; CHECK-NEXT: } 97 | ; CHECK-EMPTY: 98 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 99 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %0, i32 %1, i32 %2) #4 { 100 | ; CHECK-NEXT: sliceclone_BB_9: 101 | ; CHECK-NEXT: %3 = add nsw i32 %1, %2 102 | ; CHECK-NEXT: br label %sliceclone_BB_10 103 | ; CHECK-EMPTY: 104 | ; CHECK-NEXT: sliceclone_BB_10: ; preds = %sliceclone_BB_9 105 | ; CHECK-NEXT: %4 = sext i32 %3 to i64 106 | ; CHECK-NEXT: %5 = getelementptr inbounds ptr, ptr %0, i64 %4 107 | ; CHECK-NEXT: %6 = load ptr, ptr %5, align 8, !tbaa !10 108 | ; CHECK-NEXT: br label %sliceclone_BB_11 109 | ; CHECK-EMPTY: 110 | ; CHECK-NEXT: sliceclone_BB_11: ; preds = %sliceclone_BB_10 111 | ; CHECK-NEXT: %7 = getelementptr inbounds %struct.Clause, ptr %6, i64 0, i32 2 112 | ; CHECK-NEXT: %8 = load i32, ptr %7, align 4, !tbaa !17 113 | ; CHECK-NEXT: %9 = ashr i32 %8, 1 114 | ; CHECK-NEXT: ret i32 %9 115 | ; CHECK-NEXT: } 116 | ; CHECK-EMPTY: 117 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 118 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %0, i32 %1, i32 %2) #4 { 119 | ; CHECK-NEXT: sliceclone_BB_9: 120 | ; CHECK-NEXT: %3 = add nsw i32 %1, %2 121 | ; CHECK-NEXT: br label %sliceclone_BB_10 122 | ; CHECK-EMPTY: 123 | ; CHECK-NEXT: sliceclone_BB_10: ; preds = %sliceclone_BB_9 124 | ; CHECK-NEXT: %4 = sext i32 %3 to i64 125 | ; CHECK-NEXT: %5 = getelementptr inbounds ptr, ptr %0, i64 %4 126 | ; CHECK-NEXT: %6 = load ptr, ptr %5, align 8, !tbaa !10 127 | ; CHECK-NEXT: br label %sliceclone_BB_11 128 | ; CHECK-EMPTY: 129 | ; CHECK-NEXT: sliceclone_BB_11: ; preds = %sliceclone_BB_10 130 | ; CHECK-NEXT: %7 = getelementptr inbounds %struct.Clause, ptr %6, i64 0, i32 2 131 | ; CHECK-NEXT: %8 = load i32, ptr %7, align 4, !tbaa !17 132 | ; CHECK-NEXT: br label %sliceclone_BB_12 133 | ; CHECK-EMPTY: 134 | ; CHECK-NEXT: sliceclone_BB_12: ; preds = %sliceclone_BB_11 135 | ; CHECK-NEXT: %9 = and i32 %8, 1 136 | ; CHECK-NEXT: ret i32 %9 137 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /include/ProgramSlice.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file ProgramSlice.h 3 | * @brief Daedalus' Program Slicer Header File 4 | * @author Compilers Lab (UFMG) 5 | * @date 2024-07-08 6 | ***********************************************/ 7 | #ifndef PROGRAM_SLICE_H 8 | #define PROGRAM_SLICE_H 9 | 10 | #include 11 | #include 12 | 13 | #include "PHIGateAnalyzer.h" 14 | #include "llvm/Analysis/AliasAnalysis.h" 15 | #include "llvm/Analysis/LoopInfo.h" 16 | #include "llvm/Analysis/PostDominators.h" 17 | #include "llvm/IR/Dominators.h" 18 | #include "llvm/IR/Function.h" 19 | #include "llvm/IR/Instructions.h" 20 | #include "llvm/IR/PassManager.h" 21 | #include "llvm/Support/Error.h" 22 | 23 | namespace llvm { 24 | 25 | class ProgramSlice { 26 | public: 27 | ProgramSlice() = default; 28 | /// Constructs a ProgramSlice object. 29 | ProgramSlice(Instruction &I, Function &F, FunctionAnalysisManager &FAM, 30 | std::unordered_map> &predicates); 32 | 33 | /// Checks if current slice can be outlined into a standalone function. 34 | uint canOutline(AAResults *AA, TargetLibraryInfo &TLI, 35 | const std::set &tryCatchBlocks) const; 36 | 37 | /// Checks if outlining the slice is feasible. 38 | std::pair _canOutline = {true, ""}; 39 | 40 | /// Retrieves the original function arguments as a SmallVector of Values. 41 | SmallVector getOrigFunctionArgs(); 42 | 43 | /// Retrieves the mapping of original instructions to their 44 | /// corresponding instructions in the sliced function. 45 | std::map getInstructionInSlice(); 46 | 47 | /// Outlines the given slice into a standalone Function. 48 | Function *outline(unsigned int *counter); 49 | 50 | /// A function to simplify basic blocks of a function using the same 51 | /// method as the SimplifyCFGPass 52 | static void simplifyCfg(Function *F, FunctionAnalysisManager &AM); 53 | 54 | /// Retrieves the current slice's parent function. 55 | Function *getParentFunction() const; 56 | 57 | private: 58 | /// Inserts a new entry block in function F if its current entry block has 59 | /// predecessors. 60 | void createNewEntryBlock(Function *F); 61 | 62 | /// Reorders basic blocks in the new function F, ensuring 63 | /// that the sliced function's entry block (the only one with no predecessors) 64 | /// is first in the layout. This is necessary because LLVM assumes the first 65 | /// block of a function is always its entry block. 66 | static void reorderBlocks(Function *F); 67 | 68 | /// Adds a return instruction to function F, returning the computed 69 | /// value of the sliced function. 70 | ReturnInst *addReturnValue(Function *F); 71 | 72 | /// Fixes the instruction/argument/BB uses in new function F, 73 | /// to use their corresponding versions in the sliced function, rather 74 | /// than the originals from whom they were cloned. 75 | void reorganizeUses(Function *F); 76 | 77 | /// Adds slice instructions to function F, corresponding to instructions in 78 | /// the original function. 79 | void populateBBsWithInsts(Function *F); 80 | 81 | /// Adjusts references between the function arguments and the operands of the 82 | /// instructions in function F. 83 | static void replaceArgs(Function *F, DenseMap dt); 84 | 85 | /// Populates function F with BasicBlocks corresponding to the BBs in 86 | /// the original function being sliced which contained instructions included 87 | /// in the slice. 88 | void populateFunctionWithBBs(Function *F); 89 | 90 | /// Inserts a new BasicBlock in Function F corresponding to the originalBB 91 | /// from the original function being sliced. 92 | void insertNewBB(const BasicBlock *originalBB, Function *F); 93 | 94 | /// Finds the first dominator in the slice for each basic block in the parent 95 | /// function. 96 | std::map> 97 | computeFirstDominatorsInSlice() const; 98 | 99 | /// Returns a new target basic block determined by the first dominator of the 100 | /// given successor block. 101 | BasicBlock *getNewTargetByFirstDominator(const BasicBlock *successor, 102 | const BasicBlock *originalBB, 103 | const DominatorTree &DT, 104 | const PostDominatorTree &PDT); 105 | 106 | // Checks if the first dominator of curBB in the slice is originalBB 107 | bool isFirstDominatorInSlice(const BasicBlock *curBB, 108 | const BasicBlock *originalBB, 109 | const DominatorTree &DT) const; 110 | 111 | /// Helper function to create an unreachable block. 112 | static BasicBlock *createUnreachableBlock(Function *F); 113 | 114 | /// Reroutes branches in the slice to properly build control flow in the 115 | /// delegate function. 116 | void rerouteBranches(Function *F, const PostDominatorTree &PDT); 117 | 118 | void reconstructTerminator(BasicBlock &BB, const BasicBlock *origBB, 119 | Function *F, const DominatorTree &DT, 120 | BasicBlock *unreachableBlock, 121 | const PostDominatorTree &PDT); 122 | 123 | void rerouteTerminatorSuccessors(Instruction *terminator, BasicBlock &BB, 124 | const BasicBlock *origBB, Function *F, 125 | const DominatorTree &DT, 126 | BasicBlock *unreachableBlock, 127 | const PostDominatorTree &PDT); 128 | 129 | Value *getClonedCond(Value *origCond); 130 | 131 | /// Determines the target block for a successor, potentially finding a 132 | /// dominated node if direct mapping fails. 133 | BasicBlock *getOrCreateTargetBlock(const BasicBlock *successor, 134 | const BasicBlock *originalBB, 135 | const DominatorTree &DT, 136 | const PostDominatorTree &PDT); 137 | 138 | /// Updates PHI nodes in the new successor block. 139 | static void 140 | updatePHINodesForSuccessor(BasicBlock *newSuccessor, 141 | const BasicBlock *originalIncomingBlock, 142 | BasicBlock *currentBB); 143 | 144 | /// Cleans up the unreachable block if it wasn't used. 145 | static void cleanupUnreachableBlock(BasicBlock *unreachableBlock); 146 | 147 | /// Debugging helper to log predecessors. 148 | static void logPredecessors(Function *F); 149 | 150 | /// pointer to the Instruction used as slice criterion 151 | Instruction *_initial; 152 | 153 | /// Pointer to the instruction that produces the return value of the program 154 | /// slice. This may be nullptr if the slice does not yield a return value. 155 | Instruction *_instRetValue; 156 | 157 | /// function being sliced 158 | Function *_parentFunction; 159 | 160 | /// list of formal arguments on which the slice depends on (if any) 161 | SmallVector _depArgs; 162 | 163 | /// set of instructions that must be in the slice, accordingto dependence 164 | /// analysis 165 | std::set _instsInSlice; 166 | 167 | /// set of BasicBLocks that must be in the slice, according to dependence 168 | /// analysis 169 | std::set _BBsInSlice; 170 | 171 | /// maps each BasicBlock to its attractor (its first dominator), used for 172 | /// rearranging control flow 173 | std::map> 174 | _firstDominators; 175 | 176 | /// maps BasicBlocks in the original function to their new cloned counterparts 177 | /// in the slice 178 | std::map _origToNewBBmap; 179 | std::map _newToOrigBBmap; 180 | 181 | /// maps Instructions in the original function to their cloned counterparts in 182 | /// the slice 183 | std::map _origToNewInst; 184 | std::map _newToOrigInst; 185 | 186 | LoopInfo *_loopInfo; 187 | Loop *_loop; 188 | BasicBlock *_loopHeader; 189 | }; 190 | } // namespace llvm 191 | 192 | #endif 193 | -------------------------------------------------------------------------------- /lib/PHIGateAnalyzer.cpp: -------------------------------------------------------------------------------- 1 | #include "../include/PHIGateAnalyzer.h" 2 | #include "llvm/ADT/PostOrderIterator.h" 3 | #include "llvm/Support/Debug.h" 4 | 5 | #include 6 | 7 | #define DEBUG_TYPE "PHIGateAnalyzer" 8 | 9 | using namespace llvm; 10 | 11 | void PHIGateAnalyzer::initialize( 12 | const SmallPtrSet &InitialDefs) { 13 | // Clear all data structures 14 | Parent.clear(); 15 | R.clear(); 16 | GP.clear(); 17 | G_star.clear(); 18 | NeedsPhi.clear(); 19 | IsInitialDef.clear(); 20 | 21 | for (auto const &BB : F) { 22 | DomTreeNode *Node = DT.getNode(&BB); 23 | if (!Node) continue; 24 | 25 | Parent[Node] = Node; 26 | R[Node] = PathExpr::createEmpty(); 27 | GP[&BB] = PathExpr::createEmpty(); 28 | G_star[&BB] = PathExpr::createEmpty(); 29 | NeedsPhi[&BB] = false; 30 | IsInitialDef[&BB] = InitialDefs.count(&BB); 31 | } 32 | } 33 | 34 | PathExpr *PHIGateAnalyzer::createEdgeExpr(const BasicBlock *From, 35 | const BasicBlock *To) { 36 | const Instruction *TI = From->getTerminator(); 37 | if (TI->getNumSuccessors() == 1) { 38 | return PathExpr::createLambda(From); 39 | } 40 | 41 | for (unsigned i = 0; i < TI->getNumSuccessors(); ++i) { 42 | if (TI->getSuccessor(i) == To) { 43 | return PathExpr::createEdge(TI, i); 44 | } 45 | } 46 | return PathExpr::createEmpty(); 47 | } 48 | 49 | PathExpr *PHIGateAnalyzer::mergePaths(PathExpr *P1, PathExpr *P2) { 50 | if (P1->Type == PathExpr::EMPTY) return P2; 51 | if (P2->Type == PathExpr::EMPTY) return P1; 52 | return PathExpr::createUnion(P1, P2); 53 | } 54 | 55 | DomTreeNode *PHIGateAnalyzer::FIND(DomTreeNode *Node) { 56 | DomTreeNode *P = Parent[Node]; 57 | if (P == Node) { 58 | return Node; 59 | } 60 | 61 | // Path Compression 62 | DomTreeNode *Root = FIND(P); 63 | DomTreeNode *Current = Node; 64 | while (Parent[Current] != Root) { 65 | DomTreeNode *NextParent = Parent[Current]; 66 | R[Current] = PathExpr::createConcat(R[NextParent], R[Current]); 67 | Parent[Current] = Root; 68 | Current = NextParent; 69 | } 70 | return Root; 71 | } 72 | 73 | std::pair PHIGateAnalyzer::EVAL(DomTreeNode *Node) { 74 | FIND(Node); // Perform path compression 75 | 76 | bool needsPhi = false; 77 | DomTreeNode *Current = Node; 78 | 79 | while (Parent[Current] != Current) { 80 | needsPhi |= 81 | NeedsPhi[Current->getBlock()] || IsInitialDef[Current->getBlock()]; 82 | Current = Parent[Current]; 83 | } 84 | needsPhi |= 85 | NeedsPhi[Current->getBlock()] || IsInitialDef[Current->getBlock()]; 86 | 87 | return {needsPhi, R[Node]}; 88 | } 89 | 90 | void PHIGateAnalyzer::LINK(DomTreeNode *u, DomTreeNode *v) { Parent[v] = u; } 91 | 92 | void PHIGateAnalyzer::UPDATE(DomTreeNode *vNode, PathExpr *P) { R[vNode] = P; } 93 | 94 | void PHIGateAnalyzer::addBranchGate( 95 | const Value *Branch, SmallVectorImpl &Gates, 96 | SmallPtrSetImpl &Visited) const { 97 | if (!Visited.insert(Branch).second) return; 98 | 99 | if (const auto *BI = dyn_cast(Branch)) { 100 | if (BI->isConditional()) { 101 | Gates.push_back(BI); 102 | } 103 | } else if (const auto *SI = dyn_cast(Branch)) { 104 | Gates.push_back(SI); 105 | } 106 | } 107 | 108 | void PHIGateAnalyzer::collectGates( 109 | PathExpr *Expr, SmallVectorImpl &Gates, 110 | SmallPtrSetImpl &Visited) const { 111 | if (!Expr) return; 112 | 113 | // Simple recursion guard 114 | static thread_local SmallPtrSet VisitedExprs; 115 | if (!VisitedExprs.insert(Expr).second) return; 116 | 117 | switch (Expr->Type) { 118 | case PathExpr::EDGE: 119 | addBranchGate(Expr->Branch, Gates, Visited); 120 | break; 121 | case PathExpr::UNION: 122 | case PathExpr::CONCAT: 123 | collectGates(Expr->Lhs, Gates, Visited); 124 | collectGates(Expr->Rhs, Gates, Visited); 125 | break; 126 | case PathExpr::LAMBDA: 127 | // ~special case~ The paper assumes that SpecialExpr is an 128 | // unconditional branch, then a lambda expression is set with the 129 | // controlling predecessor. But we want to propagate the gate of that 130 | // predecessor, so we collect the predecessor's gates. 131 | if (Expr->Predecessor) { 132 | collectGates(GP.lookup(Expr->Predecessor), Gates, Visited); 133 | } 134 | break; 135 | case PathExpr::EMPTY: 136 | break; 137 | } 138 | 139 | VisitedExprs.erase(Expr); 140 | } 141 | 142 | std::unordered_map> 143 | PHIGateAnalyzer::getGatesForAllPhis() { 144 | // To get the most detailed path expressions for all nodes, we can treat 145 | // every block as being part of the initial definition set. This forces 146 | // the algorithm to preserve complex path expressions everywhere. 147 | SmallPtrSet InitialDefs; 148 | for (auto const &BB : F) { 149 | InitialDefs.insert(&BB); 150 | } 151 | 152 | initialize(InitialDefs); 153 | 154 | // Process nodes in reverse depth-first order 155 | ReversePostOrderTraversal RPO(DT.getRootNode()); 156 | 157 | for (DomTreeNode *uNode : RPO) { 158 | if (!uNode) continue; 159 | 160 | // DERIVE PHASE 161 | DenseMap>> 163 | ListP; 164 | 165 | for (auto const &ChildNode : *uNode) { 166 | const BasicBlock *v = ChildNode->getBlock(); 167 | for (const BasicBlock *w : predecessors(v)) { 168 | DomTreeNode *wNode = DT.getNode(w); 169 | if (!wNode) continue; 170 | 171 | if (wNode == uNode) { 172 | GP[v] = mergePaths(GP[v], createEdgeExpr(w, v)); 173 | } else { 174 | auto [phi, path] = EVAL(wNode); 175 | NeedsPhi[v] |= phi; 176 | 177 | DomTreeNode *subrootW = FIND(wNode); 178 | auto edgePath = createEdgeExpr(w, v); 179 | auto fullPath = PathExpr::createConcat(path, edgePath); 180 | 181 | if (DT.dominates(v, w)) { 182 | G_star[v] = mergePaths(G_star[v], fullPath); 183 | } else { 184 | ListP[v].push_back({subrootW->getBlock(), fullPath}); 185 | } 186 | } 187 | } 188 | } 189 | 190 | // MERGE PHASE 191 | bool changed = true; 192 | int pass = 0; 193 | const int maxPasses = uNode->getNumChildren() + 1; 194 | 195 | while (changed && pass++ < maxPasses) { 196 | changed = false; 197 | for (auto const &ChildNode : *uNode) { 198 | const BasicBlock *v = ChildNode->getBlock(); 199 | if (ListP.count(v)) { 200 | auto &predPaths = ListP[v]; 201 | for (auto it = predPaths.begin(); it != predPaths.end();) { 202 | auto &[subrootW, path] = *it; 203 | auto newPath = PathExpr::createConcat(GP[subrootW], path); 204 | auto oldGP = GP[v]; 205 | GP[v] = mergePaths(GP[v], newPath); 206 | if (oldGP != GP[v]) { 207 | changed = true; 208 | } 209 | it = predPaths.erase(it); 210 | } 211 | } 212 | } 213 | } 214 | 215 | // UPDATE and LINK PHASE 216 | for (auto const &ChildNode : *uNode) { 217 | UPDATE(ChildNode, GP[ChildNode->getBlock()]); 218 | LINK(uNode, ChildNode); 219 | } 220 | } 221 | 222 | // Debug output for path expressions 223 | LLVM_DEBUG({ 224 | for (const auto &BB : F) { 225 | if (NeedsPhi[&BB]) { 226 | dbgs() << "Block: " << BB.getName() << "\n"; 227 | dbgs() << " Gamma: "; 228 | if (GP[&BB] && GP[&BB]->Type != PathExpr::EMPTY) 229 | GP[&BB]->print(dbgs()); 230 | else 231 | dbgs() << "(none)"; 232 | dbgs() << "\n"; 233 | dbgs() << " Mu: "; 234 | if (G_star[&BB] && G_star[&BB]->Type != PathExpr::EMPTY) 235 | G_star[&BB]->print(dbgs()); 236 | else 237 | dbgs() << "(none)"; 238 | dbgs() << "\n"; 239 | } 240 | } 241 | }); 242 | 243 | // Collect gates 244 | std::unordered_map> AllGates; 245 | 246 | for (const auto &BB : F) { 247 | if (BB.hasNPredecessors(0)) continue; 248 | 249 | SmallVector GatesForBlock; 250 | SmallPtrSet VisitedGates; 251 | 252 | // Collect from both GP and G_star 253 | if (GP.count(&BB)) { 254 | collectGates(GP.lookup(&BB), GatesForBlock, VisitedGates); 255 | } 256 | if (G_star.count(&BB)) { 257 | collectGates(G_star.lookup(&BB), GatesForBlock, VisitedGates); 258 | } 259 | 260 | if (!GatesForBlock.empty()) { 261 | AllGates[&BB] = GatesForBlock; 262 | } 263 | } 264 | 265 | return AllGates; 266 | } -------------------------------------------------------------------------------- /tests/test8.c: -------------------------------------------------------------------------------- 1 | // Self-contained version of ControlFlow-flt/tsc.c with all includes expanded 2 | #define TYPE float 3 | #define ALIGNMENT 16 4 | 5 | // --- Begin tests.h --- 6 | #define LINEAR_DEPENDENCE (1 << 0) 7 | #define INDUCTION_VARIABLE (1 << 1) 8 | #define GLOBAL_DATA_FLOW (1 << 2) 9 | #define CONTROL_FLOW (1 << 3) 10 | #define SYMBOLICS (1 << 4) 11 | #define STATEMENT_REORDERING (1 << 5) 12 | #define LOOP_RESTRUCTURING (1 << 6) 13 | #define NODE_SPLITTING (1 << 7) 14 | #define EXPANSION (1 << 8) 15 | #define CROSSING_THRESHOLDS (1 << 9) 16 | #define REDUCTIONS (1 << 10) 17 | #define RECURRENCES (1 << 11) 18 | #define SEARCHING (1 << 12) 19 | #define PACKING (1 << 13) 20 | #define LOOP_REROLLING (1 << 14) 21 | #define EQUIVALENCING (1 << 15) 22 | #define INDIRECT_ADDRESSING (1 << 16) 23 | #define CONTROL_LOOPS (1 << 17) 24 | // --- End tests.h --- 25 | 26 | #define TESTS CONTROL_FLOW 27 | 28 | // --- Begin types.h --- 29 | #define LEN 32000 30 | #define LEN2 256 31 | #ifndef TYPE 32 | #define TYPE float 33 | #define FABS(x) fabsf(x) 34 | #else 35 | #define FABS(x) fabs(x) 36 | #endif 37 | #ifndef X_TYPE 38 | #define X_TYPE TYPE 39 | #endif 40 | #ifndef ALIGNMENT 41 | #define ALIGNMENT 16 42 | #endif 43 | // --- End types.h --- 44 | 45 | // --- Begin tsc.inc (reduced to only CONTROL_FLOW code and dependencies) --- 46 | #define ntimes_default 200000 47 | #define digits_default 6 48 | #define _XOPEN_SOURCE 600 49 | #include 50 | #include 51 | #include 52 | #include 53 | #include 54 | #include 55 | #include 56 | #include 57 | #include 58 | 59 | static int ntimes = ntimes_default; 60 | static int digits = digits_default; 61 | 62 | #define lll LEN 63 | #ifndef USE_CLOCK 64 | #define clock() 0 65 | #endif 66 | 67 | __attribute__((aligned(ALIGNMENT))) X_TYPE X[lll], Y[lll], Z[lll], U[lll], 68 | V[lll]; 69 | TYPE array[LEN2 * LEN2] __attribute__((aligned(ALIGNMENT))); 70 | TYPE x[LEN] __attribute__((aligned(ALIGNMENT))); 71 | TYPE temp; 72 | int temp_int; 73 | struct GlobalData { 74 | __attribute__((aligned(ALIGNMENT))) TYPE a[LEN]; 75 | int pad1[3]; 76 | __attribute__((aligned(ALIGNMENT))) TYPE b[LEN]; 77 | int pad2[5]; 78 | __attribute__((aligned(ALIGNMENT))) TYPE c[LEN]; 79 | int pad3[7]; 80 | __attribute__((aligned(ALIGNMENT))) TYPE d[LEN]; 81 | int pad4[11]; 82 | __attribute__((aligned(ALIGNMENT))) TYPE e[LEN]; 83 | int pad5[13]; 84 | __attribute__((aligned(ALIGNMENT))) TYPE aa[LEN2][LEN2]; 85 | int pad6[17]; 86 | __attribute__((aligned(ALIGNMENT))) TYPE bb[LEN2][LEN2]; 87 | int pad7[19]; 88 | __attribute__((aligned(ALIGNMENT))) TYPE cc[LEN2][LEN2]; 89 | int pad8[23]; 90 | __attribute__((aligned(ALIGNMENT))) TYPE tt[LEN2][LEN2]; 91 | } global_data; 92 | __attribute__((aligned(ALIGNMENT))) TYPE *const a = global_data.a; 93 | __attribute__((aligned(ALIGNMENT))) TYPE *const b = global_data.b; 94 | __attribute__((aligned(ALIGNMENT))) TYPE *const c = global_data.c; 95 | __attribute__((aligned(ALIGNMENT))) TYPE *const d = global_data.d; 96 | __attribute__((aligned(ALIGNMENT))) TYPE *const e = global_data.e; 97 | __attribute__((aligned(ALIGNMENT))) TYPE (*const aa)[LEN2] = global_data.aa; 98 | __attribute__((aligned(ALIGNMENT))) TYPE (*const bb)[LEN2] = global_data.bb; 99 | __attribute__((aligned(ALIGNMENT))) TYPE (*const cc)[LEN2] = global_data.cc; 100 | __attribute__((aligned(ALIGNMENT))) TYPE (*const tt)[LEN2] = global_data.tt; 101 | int indx[LEN] __attribute__((aligned(ALIGNMENT))); 102 | TYPE *__restrict__ xx; 103 | TYPE *yy; 104 | int dummy(TYPE x1[LEN], TYPE x2[LEN], TYPE x3[LEN], TYPE x4[LEN], TYPE x5[LEN], 105 | TYPE x6[LEN2][LEN2], TYPE x7[LEN2][LEN2], TYPE x8[LEN2][LEN2], 106 | TYPE x9) { 107 | return 0; 108 | } 109 | int set1d(TYPE arr[LEN], TYPE value, int stride) { 110 | for (int i = 0; i < LEN; i++) 111 | arr[i] = value; 112 | return 0; 113 | } 114 | int set2d(TYPE arr[LEN2][LEN2], TYPE value, int stride) { 115 | for (int i = 0; i < LEN2; i++) 116 | for (int j = 0; j < LEN2; j++) 117 | arr[i][j] = value; 118 | return 0; 119 | } 120 | void check(int name){ 121 | 122 | TYPE suma = 0; 123 | TYPE sumb = 0; 124 | TYPE sumc = 0; 125 | TYPE sumd = 0; 126 | TYPE sume = 0; 127 | for (int i = 0; i < LEN; i++){ 128 | suma += a[i]; 129 | sumb += b[i]; 130 | sumc += c[i]; 131 | sumd += d[i]; 132 | sume += e[i]; 133 | } 134 | TYPE sumaa = 0; 135 | TYPE sumbb = 0; 136 | TYPE sumcc = 0; 137 | for (int i = 0; i < LEN2; i++){ 138 | for (int j = 0; j < LEN2; j++){ 139 | sumaa += aa[i][j]; 140 | sumbb += bb[i][j]; 141 | sumcc += cc[i][j]; 142 | 143 | } 144 | } 145 | TYPE sumarray = 0; 146 | for (int i = 0; i < LEN2*LEN2; i++){ 147 | sumarray += array[i]; 148 | } 149 | 150 | if (name == 1) printf("%.*G \n",digits,suma); 151 | if (name == 2) printf("%.*G \n",digits,sumb); 152 | if (name == 3) printf("%.*G \n",digits,sumc); 153 | if (name == 4) printf("%.*G \n",digits,sumd); 154 | if (name == 5) printf("%.*G \n",digits,sume); 155 | if (name == 11) printf("%.*G \n",digits,sumaa); 156 | if (name == 22) printf("%.*G \n",digits,sumbb); 157 | if (name == 33) printf("%.*G \n",digits,sumcc); 158 | if (name == 0) printf("%.*G \n",digits,sumarray); 159 | if (name == 12) printf("%.*G \n",digits,suma+sumb); 160 | if (name == 25) printf("%.*G \n",digits,sumb+sume); 161 | if (name == 13) printf("%.*G \n",digits,suma+sumc); 162 | if (name == 123) printf("%.*G \n",digits,suma+sumb+sumc); 163 | if (name == 1122) printf("%.*G \n",digits,sumaa+sumbb); 164 | if (name == 112233) printf("%.*G \n",digits,sumaa+sumbb+sumcc); 165 | if (name == 111) printf("%.*G \n",digits,sumaa+suma); 166 | if (name == -1) printf("%.*G \n",digits,temp); 167 | if (name == -12) printf("%.*G \n",digits,temp+sumb); 168 | 169 | } 170 | int init(char *name) { 171 | set1d(a, 1.0, 1); 172 | set1d(b, 1.0, 1); 173 | set1d(c, 1.0, 1); 174 | set1d(d, 1.0, 1); 175 | set1d(e, 1.0, 1); 176 | set2d(aa, 0.0, 1); 177 | set2d(bb, 0.0, 1); 178 | set2d(cc, 0.0, 1); 179 | return 0; 180 | } 181 | 182 | // --- Only CONTROL_FLOW test functions --- 183 | int s161() { 184 | clock_t start_t, end_t, clock_dif; 185 | double clock_dif_sec; 186 | init("s161 "); 187 | start_t = clock(); 188 | for (int nl = 0; nl < ntimes / 2; nl++) { 189 | for (int i = 0; i < LEN - 1; ++i) { 190 | if (b[i] < (TYPE)0.) { 191 | c[i + 1] = a[i] + d[i] * d[i]; 192 | } else { 193 | a[i] = c[i] + d[i] * e[i]; 194 | } 195 | } 196 | dummy(a, b, c, d, e, aa, bb, cc, 0.); 197 | } 198 | end_t = clock(); 199 | clock_dif = end_t - start_t; 200 | clock_dif_sec = (double)(clock_dif / 1000000.0); 201 | printf("S161\t %.2f \t\t", clock_dif_sec); 202 | check(13); 203 | return 0; 204 | } 205 | int s1161() { 206 | clock_t start_t, end_t, clock_dif; 207 | double clock_dif_sec; 208 | init("s161 "); 209 | start_t = clock(); 210 | for (int nl = 0; nl < ntimes; nl++) { 211 | for (int i = 0; i < LEN - 1; ++i) { 212 | if (c[i] < (TYPE)0.) { 213 | b[i] = a[i] + d[i] * d[i]; 214 | } else { 215 | a[i] = c[i] + d[i] * e[i]; 216 | } 217 | } 218 | dummy(a, b, c, d, e, aa, bb, cc, 0.); 219 | } 220 | end_t = clock(); 221 | clock_dif = end_t - start_t; 222 | clock_dif_sec = (double)(clock_dif / 1000000.0); 223 | printf("S1161\t %.2f \t\t", clock_dif_sec); 224 | check(13); 225 | return 0; 226 | } 227 | int s162(int k) { 228 | clock_t start_t, end_t, clock_dif; 229 | double clock_dif_sec; 230 | init("s162 "); 231 | start_t = clock(); 232 | for (int nl = 0; nl < ntimes; nl++) { 233 | if (k > 0) { 234 | for (int i = 0; i < LEN - 1; i++) { 235 | a[i] = a[i + k] + b[i] * c[i]; 236 | } 237 | } 238 | dummy(a, b, c, d, e, aa, bb, cc, 0.); 239 | } 240 | end_t = clock(); 241 | clock_dif = end_t - start_t; 242 | clock_dif_sec = (double)(clock_dif / 1000000.0); 243 | printf("S162\t %.2f \t\t", clock_dif_sec); 244 | check(1); 245 | return 0; 246 | } 247 | int s271() { 248 | clock_t start_t, end_t, clock_dif; 249 | double clock_dif_sec; 250 | init("s271 "); 251 | start_t = clock(); 252 | for (int nl = 0; nl < 4 * ntimes; nl++) { 253 | for (int i = 0; i < LEN; i++) { 254 | if (b[i] > (TYPE)0.) { 255 | a[i] += b[i] * c[i]; 256 | } 257 | } 258 | dummy(a, b, c, d, e, aa, bb, cc, 0.); 259 | } 260 | end_t = clock(); 261 | clock_dif = end_t - start_t; 262 | clock_dif_sec = (double)(clock_dif / 1000000.0); 263 | printf("S271\t %.2f \t\t", clock_dif_sec); 264 | check(1); 265 | return 0; 266 | } 267 | // ... (other CONTROL_FLOW test functions can be added similarly) ... 268 | 269 | int main(int argc, char *argv[]) { 270 | int n1 = 1; 271 | int n3 = 1; 272 | int *ip; 273 | TYPE s1, s2; 274 | posix_memalign((void **)&ip, ALIGNMENT, LEN * sizeof(TYPE)); 275 | if (argc > 1) 276 | ntimes = atoi(argv[1]); 277 | printf("Running each loop %d times...\n", ntimes); 278 | if (argc > 2) 279 | digits = atoi(argv[2]); 280 | printf("Loop \t Time(Sec) \t Checksum \n"); 281 | s161(); 282 | s1161(); 283 | s162(n1); 284 | s271(); 285 | // ... (call other CONTROL_FLOW test functions as needed) ... 286 | return 0; 287 | } 288 | // --- End tsc.inc --- 289 | -------------------------------------------------------------------------------- /tests/test6.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: noinline nounwind optsize willreturn 2 | ; CHECK-NEXT: define internal i64 @_daedalus_slice_main_[[ID:[0-9]+]](i32 %0, ptr %1) #6 { 3 | ; CHECK-NEXT: sliceclone_BB_0: 4 | ; CHECK-NEXT: %2 = icmp sgt i32 %0, 1 5 | ; CHECK-NEXT: br i1 %2, label %sliceclone_BB_1, label %sliceclone_BB_12 6 | ; CHECK-EMPTY: 7 | ; CHECK-NEXT: sliceclone_BB_1: ; preds = %sliceclone_BB_11, %sliceclone_BB_0 8 | ; CHECK-NEXT: %3 = phi i32 [ %12, %sliceclone_BB_11 ], [ 1, %sliceclone_BB_0 ] 9 | ; CHECK-NEXT: %4 = phi i32 [ %11, %sliceclone_BB_11 ], [ 196, %sliceclone_BB_0 ] 10 | ; CHECK-NEXT: %5 = sext i32 %3 to i64 11 | ; CHECK-NEXT: %6 = getelementptr inbounds ptr, ptr %1, i64 %5 12 | ; CHECK-NEXT: %7 = load ptr, ptr %6, align 8, !tbaa !7 13 | ; CHECK-NEXT: br label %sliceclone_BB_4 14 | ; CHECK-EMPTY: 15 | ; CHECK-NEXT: sliceclone_BB_12: ; preds = %sliceclone_BB_11, %sliceclone_BB_0 16 | ; CHECK-NEXT: %8 = phi i32 [ 196, %sliceclone_BB_0 ], [ %11, %sliceclone_BB_11 ] 17 | ; CHECK-NEXT: %9 = sext i32 %8 to i64 18 | ; CHECK-NEXT: %10 = shl nsw i64 %9, 3 19 | ; CHECK-NEXT: ret i64 %10 20 | ; CHECK-EMPTY: 21 | ; CHECK-NEXT: sliceclone_BB_11: ; preds = %sliceclone_BB_9, %sliceclone_BB_8, %sliceclone_BB_7, %sliceclone_BB_6, %sliceclone_BB_5, %sliceclone_BB_4, %sliceclone_BB_4 22 | ; CHECK-NEXT: %11 = phi i32 [ %4, %sliceclone_BB_9 ], [ %25, %sliceclone_BB_8 ], [ %4, %sliceclone_BB_7 ], [ %4, %sliceclone_BB_6 ], [ %4, %sliceclone_BB_5 ], [ %4, %sliceclone_BB_4 ], [ %4, %sliceclone_BB_4 ] 23 | ; CHECK-NEXT: %12 = phi i32 [ %26, %sliceclone_BB_9 ], [ %21, %sliceclone_BB_8 ], [ %20, %sliceclone_BB_7 ], [ %19, %sliceclone_BB_6 ], [ %18, %sliceclone_BB_5 ], [ %16, %sliceclone_BB_4 ], [ %16, %sliceclone_BB_4 ] 24 | ; CHECK-NEXT: %13 = icmp slt i32 %12, %0 25 | ; CHECK-NEXT: br i1 %13, label %sliceclone_BB_1, label %sliceclone_BB_12, !llvm.loop !14 26 | ; CHECK-EMPTY: 27 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_1 28 | ; CHECK-NEXT: %14 = getelementptr inbounds i8, ptr %7, i64 1 29 | ; CHECK-NEXT: %15 = load i8, ptr %14, align 1, !tbaa !11 30 | ; CHECK-NEXT: %16 = add nsw i32 %3, 1 31 | ; CHECK-NEXT: %17 = sext i8 %15 to i32 32 | ; CHECK-NEXT: switch i32 %17, label %_daedalus_unreachable [ 33 | ; CHECK-NEXT: i32 100, label %sliceclone_BB_11 34 | ; CHECK-NEXT: i32 103, label %sliceclone_BB_5 35 | ; CHECK-NEXT: i32 105, label %sliceclone_BB_6 36 | ; CHECK-NEXT: i32 108, label %sliceclone_BB_7 37 | ; CHECK-NEXT: i32 110, label %sliceclone_BB_8 38 | ; CHECK-NEXT: i32 115, label %sliceclone_BB_9 39 | ; CHECK-NEXT: i32 116, label %sliceclone_BB_11 40 | ; CHECK-NEXT: ] 41 | ; CHECK-EMPTY: 42 | ; CHECK-NEXT: sliceclone_BB_5: ; preds = %sliceclone_BB_4 43 | ; CHECK-NEXT: %18 = add nsw i32 %3, 2 44 | ; CHECK-NEXT: br label %sliceclone_BB_11 45 | ; CHECK-EMPTY: 46 | ; CHECK-NEXT: sliceclone_BB_6: ; preds = %sliceclone_BB_4 47 | ; CHECK-NEXT: %19 = add nsw i32 %3, 2 48 | ; CHECK-NEXT: br label %sliceclone_BB_11 49 | ; CHECK-EMPTY: 50 | ; CHECK-NEXT: sliceclone_BB_7: ; preds = %sliceclone_BB_4 51 | ; CHECK-NEXT: %20 = add nsw i32 %3, 2 52 | ; CHECK-NEXT: br label %sliceclone_BB_11 53 | ; CHECK-EMPTY: 54 | ; CHECK-NEXT: sliceclone_BB_8: ; preds = %sliceclone_BB_4 55 | ; CHECK-NEXT: %21 = add nsw i32 %3, 2 56 | ; CHECK-NEXT: %22 = sext i32 %16 to i64 57 | ; CHECK-NEXT: %23 = getelementptr inbounds ptr, ptr %1, i64 %22 58 | ; CHECK-NEXT: %24 = load ptr, ptr %23, align 8, !tbaa !7 59 | ; CHECK-NEXT: %25 = tail call i32 @atoi(ptr nocapture noundef %24) #8 60 | ; CHECK-NEXT: br label %sliceclone_BB_11 61 | ; CHECK-EMPTY: 62 | ; CHECK-NEXT: sliceclone_BB_9: ; preds = %sliceclone_BB_4 63 | ; CHECK-NEXT: %26 = add nsw i32 %3, 2 64 | ; CHECK-NEXT: br label %sliceclone_BB_11 65 | ; CHECK-EMPTY: 66 | ; CHECK-NEXT: _daedalus_unreachable: ; preds = %sliceclone_BB_4 67 | ; CHECK-NEXT: unreachable 68 | ; CHECK-NEXT: } 69 | ; CHECK-EMPTY: 70 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 71 | ; CHECK-NEXT: define internal i64 @_daedalus_slice_main_[[ID:[0-9]+]](i32 %0, ptr %1) #6 { 72 | ; CHECK-NEXT: sliceclone_BB_0: 73 | ; CHECK-NEXT: %2 = icmp sgt i32 %0, 1 74 | ; CHECK-NEXT: br i1 %2, label %sliceclone_BB_1, label %sliceclone_BB_12 75 | ; CHECK-EMPTY: 76 | ; CHECK-NEXT: sliceclone_BB_1: ; preds = %sliceclone_BB_11, %sliceclone_BB_0 77 | ; CHECK-NEXT: %3 = phi i32 [ %10, %sliceclone_BB_11 ], [ 1, %sliceclone_BB_0 ] 78 | ; CHECK-NEXT: %4 = phi i32 [ %9, %sliceclone_BB_11 ], [ 196, %sliceclone_BB_0 ] 79 | ; CHECK-NEXT: %5 = sext i32 %3 to i64 80 | ; CHECK-NEXT: %6 = getelementptr inbounds ptr, ptr %1, i64 %5 81 | ; CHECK-NEXT: %7 = load ptr, ptr %6, align 8, !tbaa !7 82 | ; CHECK-NEXT: br label %sliceclone_BB_4 83 | ; CHECK-EMPTY: 84 | ; CHECK-NEXT: sliceclone_BB_12: ; preds = %sliceclone_BB_11, %sliceclone_BB_0 85 | ; CHECK-NEXT: %8 = phi i32 [ 196, %sliceclone_BB_0 ], [ %9, %sliceclone_BB_11 ] 86 | ; CHECK-NEXT: br label %sliceclone_BB_13 87 | ; CHECK-EMPTY: 88 | ; CHECK-NEXT: sliceclone_BB_11: ; preds = %sliceclone_BB_9, %sliceclone_BB_8, %sliceclone_BB_7, %sliceclone_BB_6, %sliceclone_BB_5, %sliceclone_BB_4, %sliceclone_BB_4 89 | ; CHECK-NEXT: %9 = phi i32 [ %4, %sliceclone_BB_9 ], [ %23, %sliceclone_BB_8 ], [ %4, %sliceclone_BB_7 ], [ %4, %sliceclone_BB_6 ], [ %4, %sliceclone_BB_5 ], [ %4, %sliceclone_BB_4 ], [ %4, %sliceclone_BB_4 ] 90 | ; CHECK-NEXT: %10 = phi i32 [ %24, %sliceclone_BB_9 ], [ %19, %sliceclone_BB_8 ], [ %18, %sliceclone_BB_7 ], [ %17, %sliceclone_BB_6 ], [ %16, %sliceclone_BB_5 ], [ %14, %sliceclone_BB_4 ], [ %14, %sliceclone_BB_4 ] 91 | ; CHECK-NEXT: %11 = icmp slt i32 %10, %0 92 | ; CHECK-NEXT: br i1 %11, label %sliceclone_BB_1, label %sliceclone_BB_12, !llvm.loop !14 93 | ; CHECK-EMPTY: 94 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_1 95 | ; CHECK-NEXT: %12 = getelementptr inbounds i8, ptr %7, i64 1 96 | ; CHECK-NEXT: %13 = load i8, ptr %12, align 1, !tbaa !11 97 | ; CHECK-NEXT: %14 = add nsw i32 %3, 1 98 | ; CHECK-NEXT: %15 = sext i8 %13 to i32 99 | ; CHECK-NEXT: switch i32 %15, label %_daedalus_unreachable [ 100 | ; CHECK-NEXT: i32 100, label %sliceclone_BB_11 101 | ; CHECK-NEXT: i32 103, label %sliceclone_BB_5 102 | ; CHECK-NEXT: i32 105, label %sliceclone_BB_6 103 | ; CHECK-NEXT: i32 108, label %sliceclone_BB_7 104 | ; CHECK-NEXT: i32 110, label %sliceclone_BB_8 105 | ; CHECK-NEXT: i32 115, label %sliceclone_BB_9 106 | ; CHECK-NEXT: i32 116, label %sliceclone_BB_11 107 | ; CHECK-NEXT: ] 108 | ; CHECK-EMPTY: 109 | ; CHECK-NEXT: sliceclone_BB_5: ; preds = %sliceclone_BB_4 110 | ; CHECK-NEXT: %16 = add nsw i32 %3, 2 111 | ; CHECK-NEXT: br label %sliceclone_BB_11 112 | ; CHECK-EMPTY: 113 | ; CHECK-NEXT: sliceclone_BB_6: ; preds = %sliceclone_BB_4 114 | ; CHECK-NEXT: %17 = add nsw i32 %3, 2 115 | ; CHECK-NEXT: br label %sliceclone_BB_11 116 | ; CHECK-EMPTY: 117 | ; CHECK-NEXT: sliceclone_BB_7: ; preds = %sliceclone_BB_4 118 | ; CHECK-NEXT: %18 = add nsw i32 %3, 2 119 | ; CHECK-NEXT: br label %sliceclone_BB_11 120 | ; CHECK-EMPTY: 121 | ; CHECK-NEXT: sliceclone_BB_8: ; preds = %sliceclone_BB_4 122 | ; CHECK-NEXT: %19 = add nsw i32 %3, 2 123 | ; CHECK-NEXT: %20 = sext i32 %14 to i64 124 | ; CHECK-NEXT: %21 = getelementptr inbounds ptr, ptr %1, i64 %20 125 | ; CHECK-NEXT: %22 = load ptr, ptr %21, align 8, !tbaa !7 126 | ; CHECK-NEXT: %23 = tail call i32 @atoi(ptr nocapture noundef %22) #8 127 | ; CHECK-NEXT: br label %sliceclone_BB_11 128 | ; CHECK-EMPTY: 129 | ; CHECK-NEXT: sliceclone_BB_9: ; preds = %sliceclone_BB_4 130 | ; CHECK-NEXT: %24 = add nsw i32 %3, 2 131 | ; CHECK-NEXT: br label %sliceclone_BB_11 132 | ; CHECK-EMPTY: 133 | ; CHECK-NEXT: sliceclone_BB_13: ; preds = %sliceclone_BB_12 134 | ; CHECK-NEXT: %25 = zext i32 %8 to i64 135 | ; CHECK-NEXT: %26 = shl nuw nsw i64 %25, 3 136 | ; CHECK-NEXT: ret i64 %26 137 | ; CHECK-EMPTY: 138 | ; CHECK-NEXT: _daedalus_unreachable: ; preds = %sliceclone_BB_4 139 | ; CHECK-NEXT: unreachable 140 | ; CHECK-NEXT: } 141 | ; CHECK-EMPTY: 142 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 143 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](i32 %0, ptr %1) #6 { 144 | ; CHECK-NEXT: sliceclone_BB_26: 145 | ; CHECK-NEXT: %2 = getelementptr inbounds %struct.element, ptr %1, i64 0, i32 1 146 | ; CHECK-NEXT: %3 = load i32, ptr %2, align 8, !tbaa !18 147 | ; CHECK-NEXT: %4 = add nsw i32 %3, %0 148 | ; CHECK-NEXT: ret i32 %4 149 | ; CHECK-NEXT: } 150 | ; CHECK-EMPTY: 151 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 152 | ; CHECK-NEXT: define internal i32 @_daedalus_slice_main_[[ID:[0-9]+]](ptr %0) #6 { 153 | ; CHECK-NEXT: sliceclone_BB_26: 154 | ; CHECK-NEXT: %1 = getelementptr inbounds %struct.element, ptr %0, i64 0, i32 1 155 | ; CHECK-NEXT: %2 = load i32, ptr %1, align 8, !tbaa !18 156 | ; CHECK-NEXT: %3 = add nsw i32 %2, 1 157 | ; CHECK-NEXT: ret i32 %3 158 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /tests/test8.pattern: -------------------------------------------------------------------------------- 1 | ; CHECK: ; Function Attrs: noinline nounwind optsize willreturn 2 | ; CHECK-NEXT: define internal float @_daedalus_slice_check_[[ID:[0-9]+]](float %0, i64 %1, i64 %2) #8 { 3 | ; CHECK-NEXT: sliceclone_BB_4: 4 | ; CHECK-NEXT: %3 = getelementptr inbounds [256 x float], ptr getelementptr inbounds (%struct.GlobalData, ptr @global_data, i64 0, i32 15, i64 0, i64 0), i64 %1, i64 %2 5 | ; CHECK-NEXT: %4 = load float, ptr %3, align 4, !tbaa !7 6 | ; CHECK-NEXT: %5 = fadd float %0, %4 7 | ; CHECK-NEXT: ret float %5 8 | ; CHECK-NEXT: } 9 | ; CHECK-EMPTY: 10 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 11 | ; CHECK-NEXT: define internal float @_daedalus_slice_check_[[ID:[0-9]+]](float %0, i64 %1, i64 %2) #8 { 12 | ; CHECK-NEXT: sliceclone_BB_4: 13 | ; CHECK-NEXT: %3 = getelementptr inbounds [256 x float], ptr getelementptr inbounds (%struct.GlobalData, ptr @global_data, i64 0, i32 18, i64 0, i64 0), i64 %1, i64 %2 14 | ; CHECK-NEXT: %4 = load float, ptr %3, align 4, !tbaa !7 15 | ; CHECK-NEXT: %5 = fadd float %0, %4 16 | ; CHECK-NEXT: ret float %5 17 | ; CHECK-NEXT: } 18 | ; CHECK-EMPTY: 19 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 20 | ; CHECK-NEXT: define internal float @_daedalus_slice_check_[[ID:[0-9]+]](float %0, i64 %1, i64 %2) #8 { 21 | ; CHECK-NEXT: sliceclone_BB_4: 22 | ; CHECK-NEXT: %3 = getelementptr inbounds [256 x float], ptr getelementptr inbounds (%struct.GlobalData, ptr @global_data, i64 0, i32 21, i64 0, i64 0), i64 %1, i64 %2 23 | ; CHECK-NEXT: %4 = load float, ptr %3, align 4, !tbaa !7 24 | ; CHECK-NEXT: %5 = fadd float %0, %4 25 | ; CHECK-NEXT: ret float %5 26 | ; CHECK-NEXT: } 27 | ; CHECK-EMPTY: 28 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 29 | ; CHECK-NEXT: define internal float @_daedalus_slice_check_[[ID:[0-9]+]]() #8 { 30 | ; CHECK-NEXT: sliceclone_BB_0: 31 | ; CHECK-NEXT: br label %sliceclone_BB_1 32 | ; CHECK-EMPTY: 33 | ; CHECK-NEXT: sliceclone_BB_1: ; preds = %sliceclone_BB_0, %sliceclone_BB_1 34 | ; CHECK-NEXT: %0 = phi i64 [ 0, %sliceclone_BB_0 ], [ %1, %sliceclone_BB_1 ] 35 | ; CHECK-NEXT: %1 = add nuw nsw i64 %0, 1 36 | ; CHECK-NEXT: %2 = icmp eq i64 %1, 32000 37 | ; CHECK-NEXT: br i1 %2, label %sliceclone_BB_2, label %sliceclone_BB_1 38 | ; CHECK-EMPTY: 39 | ; CHECK-NEXT: sliceclone_BB_2: ; preds = %sliceclone_BB_3, %sliceclone_BB_1 40 | ; CHECK-NEXT: %3 = phi i64 [ %8, %sliceclone_BB_3 ], [ 0, %sliceclone_BB_1 ] 41 | ; CHECK-NEXT: %4 = phi float [ %7, %sliceclone_BB_3 ], [ 0.000000e+00, %sliceclone_BB_1 ] 42 | ; CHECK-NEXT: %5 = phi float [ %6, %sliceclone_BB_3 ], [ 0.000000e+00, %sliceclone_BB_1 ] 43 | ; CHECK-NEXT: br label %sliceclone_BB_4 44 | ; CHECK-EMPTY: 45 | ; CHECK-NEXT: sliceclone_BB_3: ; preds = %sliceclone_BB_4 46 | ; CHECK-NEXT: %6 = phi float [ %13, %sliceclone_BB_4 ] 47 | ; CHECK-NEXT: %7 = phi float [ %14, %sliceclone_BB_4 ] 48 | ; CHECK-NEXT: %8 = add nuw nsw i64 %3, 1 49 | ; CHECK-NEXT: %9 = icmp eq i64 %8, 256 50 | ; CHECK-NEXT: br i1 %9, label %sliceclone_BB_6, label %sliceclone_BB_2 51 | ; CHECK-EMPTY: 52 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_4, %sliceclone_BB_2 53 | ; CHECK-NEXT: %10 = phi i64 [ 0, %sliceclone_BB_2 ], [ %15, %sliceclone_BB_4 ] 54 | ; CHECK-NEXT: %11 = phi float [ %4, %sliceclone_BB_2 ], [ %14, %sliceclone_BB_4 ] 55 | ; CHECK-NEXT: %12 = phi float [ %5, %sliceclone_BB_2 ], [ %13, %sliceclone_BB_4 ] 56 | ; CHECK-NEXT: %13 = call float @_daedalus_slice_check_[[ID:[0-9]+]](float %12, i64 %3, i64 %10) 57 | ; CHECK-NEXT: %14 = call float @_daedalus_slice_check_[[ID:[0-9]+]](float %11, i64 %3, i64 %10) 58 | ; CHECK-NEXT: %15 = add nuw nsw i64 %10, 1 59 | ; CHECK-NEXT: %16 = icmp eq i64 %15, 256 60 | ; CHECK-NEXT: br i1 %16, label %sliceclone_BB_3, label %sliceclone_BB_4 61 | ; CHECK-EMPTY: 62 | ; CHECK-NEXT: sliceclone_BB_6: ; preds = %sliceclone_BB_6, %sliceclone_BB_3 63 | ; CHECK-NEXT: %17 = phi float [ %17, %sliceclone_BB_6 ], [ %6, %sliceclone_BB_3 ] 64 | ; CHECK-NEXT: %18 = phi float [ %18, %sliceclone_BB_6 ], [ %7, %sliceclone_BB_3 ] 65 | ; CHECK-NEXT: %19 = phi i64 [ %20, %sliceclone_BB_6 ], [ 0, %sliceclone_BB_3 ] 66 | ; CHECK-NEXT: %20 = add nuw nsw i64 %19, 1 67 | ; CHECK-NEXT: %21 = icmp eq i64 %20, 65536 68 | ; CHECK-NEXT: br i1 %21, label %sliceclone_BB_5, label %sliceclone_BB_6 69 | ; CHECK-EMPTY: 70 | ; CHECK-NEXT: sliceclone_BB_5: ; preds = %sliceclone_BB_6 71 | ; CHECK-NEXT: %22 = phi float [ %17, %sliceclone_BB_6 ] 72 | ; CHECK-NEXT: %23 = phi float [ %18, %sliceclone_BB_6 ] 73 | ; CHECK-NEXT: switch i32 undef, label %_daedalus_unreachable [ 74 | ; CHECK-NEXT: i32 1, label %_daedalus_unreachable 75 | ; CHECK-NEXT: i32 2, label %_daedalus_unreachable 76 | ; CHECK-NEXT: i32 3, label %_daedalus_unreachable 77 | ; CHECK-NEXT: i32 4, label %_daedalus_unreachable 78 | ; CHECK-NEXT: i32 5, label %_daedalus_unreachable 79 | ; CHECK-NEXT: i32 11, label %_daedalus_unreachable 80 | ; CHECK-NEXT: i32 22, label %_daedalus_unreachable 81 | ; CHECK-NEXT: i32 33, label %_daedalus_unreachable 82 | ; CHECK-NEXT: i32 0, label %_daedalus_unreachable 83 | ; CHECK-NEXT: i32 12, label %_daedalus_unreachable 84 | ; CHECK-NEXT: i32 25, label %_daedalus_unreachable 85 | ; CHECK-NEXT: i32 13, label %_daedalus_unreachable 86 | ; CHECK-NEXT: i32 123, label %_daedalus_unreachable 87 | ; CHECK-NEXT: i32 1122, label %sliceclone_BB_19 88 | ; CHECK-NEXT: i32 112233, label %_daedalus_unreachable 89 | ; CHECK-NEXT: i32 111, label %_daedalus_unreachable 90 | ; CHECK-NEXT: i32 -1, label %_daedalus_unreachable 91 | ; CHECK-NEXT: i32 -12, label %_daedalus_unreachable 92 | ; CHECK-NEXT: ] 93 | ; CHECK-EMPTY: 94 | ; CHECK-NEXT: sliceclone_BB_19: ; preds = %sliceclone_BB_5 95 | ; CHECK-NEXT: %24 = fadd float %22, %23 96 | ; CHECK-NEXT: ret float %24 97 | ; CHECK-EMPTY: 98 | ; CHECK-NEXT: _daedalus_unreachable: ; preds = %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5 99 | ; CHECK-NEXT: unreachable 100 | ; CHECK-NEXT: } 101 | ; CHECK-EMPTY: 102 | ; CHECK-NEXT: ; Function Attrs: noinline nounwind optsize willreturn 103 | ; CHECK-NEXT: define internal float @_daedalus_slice_check_[[ID:[0-9]+]]() #8 { 104 | ; CHECK-NEXT: sliceclone_BB_0: 105 | ; CHECK-NEXT: br label %sliceclone_BB_1 106 | ; CHECK-EMPTY: 107 | ; CHECK-NEXT: sliceclone_BB_1: ; preds = %sliceclone_BB_0, %sliceclone_BB_1 108 | ; CHECK-NEXT: %0 = phi i64 [ 0, %sliceclone_BB_0 ], [ %1, %sliceclone_BB_1 ] 109 | ; CHECK-NEXT: %1 = add nuw nsw i64 %0, 1 110 | ; CHECK-NEXT: %2 = icmp eq i64 %1, 32000 111 | ; CHECK-NEXT: br i1 %2, label %sliceclone_BB_2, label %sliceclone_BB_1 112 | ; CHECK-EMPTY: 113 | ; CHECK-NEXT: sliceclone_BB_2: ; preds = %sliceclone_BB_3, %sliceclone_BB_1 114 | ; CHECK-NEXT: %3 = phi i64 [ %8, %sliceclone_BB_3 ], [ 0, %sliceclone_BB_1 ] 115 | ; CHECK-NEXT: %4 = phi float [ %7, %sliceclone_BB_3 ], [ 0.000000e+00, %sliceclone_BB_1 ] 116 | ; CHECK-NEXT: %5 = phi float [ %6, %sliceclone_BB_3 ], [ 0.000000e+00, %sliceclone_BB_1 ] 117 | ; CHECK-NEXT: br label %sliceclone_BB_4 118 | ; CHECK-EMPTY: 119 | ; CHECK-NEXT: sliceclone_BB_3: ; preds = %sliceclone_BB_4 120 | ; CHECK-NEXT: %6 = phi float [ %13, %sliceclone_BB_4 ] 121 | ; CHECK-NEXT: %7 = phi float [ %14, %sliceclone_BB_4 ] 122 | ; CHECK-NEXT: %8 = add nuw nsw i64 %3, 1 123 | ; CHECK-NEXT: %9 = icmp eq i64 %8, 256 124 | ; CHECK-NEXT: br i1 %9, label %sliceclone_BB_6, label %sliceclone_BB_2 125 | ; CHECK-EMPTY: 126 | ; CHECK-NEXT: sliceclone_BB_4: ; preds = %sliceclone_BB_4, %sliceclone_BB_2 127 | ; CHECK-NEXT: %10 = phi i64 [ 0, %sliceclone_BB_2 ], [ %15, %sliceclone_BB_4 ] 128 | ; CHECK-NEXT: %11 = phi float [ %4, %sliceclone_BB_2 ], [ %14, %sliceclone_BB_4 ] 129 | ; CHECK-NEXT: %12 = phi float [ %5, %sliceclone_BB_2 ], [ %13, %sliceclone_BB_4 ] 130 | ; CHECK-NEXT: %13 = call float @_daedalus_slice_check_[[ID:[0-9]+]](float %12, i64 %3, i64 %10) 131 | ; CHECK-NEXT: %14 = call float @_daedalus_slice_check_[[ID:[0-9]+]](float %11, i64 %3, i64 %10) 132 | ; CHECK-NEXT: %15 = add nuw nsw i64 %10, 1 133 | ; CHECK-NEXT: %16 = icmp eq i64 %15, 256 134 | ; CHECK-NEXT: br i1 %16, label %sliceclone_BB_3, label %sliceclone_BB_4 135 | ; CHECK-EMPTY: 136 | ; CHECK-NEXT: sliceclone_BB_6: ; preds = %sliceclone_BB_6, %sliceclone_BB_3 137 | ; CHECK-NEXT: %17 = phi float [ %17, %sliceclone_BB_6 ], [ %6, %sliceclone_BB_3 ] 138 | ; CHECK-NEXT: %18 = phi float [ %18, %sliceclone_BB_6 ], [ %7, %sliceclone_BB_3 ] 139 | ; CHECK-NEXT: %19 = phi i64 [ %20, %sliceclone_BB_6 ], [ 0, %sliceclone_BB_3 ] 140 | ; CHECK-NEXT: %20 = add nuw nsw i64 %19, 1 141 | ; CHECK-NEXT: %21 = icmp eq i64 %20, 65536 142 | ; CHECK-NEXT: br i1 %21, label %sliceclone_BB_5, label %sliceclone_BB_6 143 | ; CHECK-EMPTY: 144 | ; CHECK-NEXT: sliceclone_BB_5: ; preds = %sliceclone_BB_6 145 | ; CHECK-NEXT: %22 = phi float [ %17, %sliceclone_BB_6 ] 146 | ; CHECK-NEXT: %23 = phi float [ %18, %sliceclone_BB_6 ] 147 | ; CHECK-NEXT: switch i32 undef, label %_daedalus_unreachable [ 148 | ; CHECK-NEXT: i32 1, label %_daedalus_unreachable 149 | ; CHECK-NEXT: i32 2, label %_daedalus_unreachable 150 | ; CHECK-NEXT: i32 3, label %_daedalus_unreachable 151 | ; CHECK-NEXT: i32 4, label %_daedalus_unreachable 152 | ; CHECK-NEXT: i32 5, label %_daedalus_unreachable 153 | ; CHECK-NEXT: i32 11, label %_daedalus_unreachable 154 | ; CHECK-NEXT: i32 22, label %_daedalus_unreachable 155 | ; CHECK-NEXT: i32 33, label %_daedalus_unreachable 156 | ; CHECK-NEXT: i32 0, label %_daedalus_unreachable 157 | ; CHECK-NEXT: i32 12, label %_daedalus_unreachable 158 | ; CHECK-NEXT: i32 25, label %_daedalus_unreachable 159 | ; CHECK-NEXT: i32 13, label %_daedalus_unreachable 160 | ; CHECK-NEXT: i32 123, label %_daedalus_unreachable 161 | ; CHECK-NEXT: i32 1122, label %_daedalus_unreachable 162 | ; CHECK-NEXT: i32 112233, label %sliceclone_BB_20 163 | ; CHECK-NEXT: i32 111, label %_daedalus_unreachable 164 | ; CHECK-NEXT: i32 -1, label %_daedalus_unreachable 165 | ; CHECK-NEXT: i32 -12, label %_daedalus_unreachable 166 | ; CHECK-NEXT: ] 167 | ; CHECK-EMPTY: 168 | ; CHECK-NEXT: sliceclone_BB_20: ; preds = %sliceclone_BB_5 169 | ; CHECK-NEXT: %24 = fadd float %22, %23 170 | ; CHECK-NEXT: ret float %24 171 | ; CHECK-EMPTY: 172 | ; CHECK-NEXT: _daedalus_unreachable: ; preds = %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5, %sliceclone_BB_5 173 | ; CHECK-NEXT: unreachable 174 | ; CHECK-NEXT: } -------------------------------------------------------------------------------- /tests/test5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #ifndef POLYBENCH_PADDING_FACTOR 13 | 14 | #define POLYBENCH_PADDING_FACTOR 0 15 | #endif 16 | 17 | #define POLYBENCH_C99_SELECT(x, y) x 18 | 19 | #define POLYBENCH_LOOP_BOUND(x, y) y 20 | 21 | #define POLYBENCH_ARRAY(x) *x 22 | #define POLYBENCH_FREE_ARRAY(x) free((void *)x); 23 | #define POLYBENCH_DECL_VAR(x) (*x) 24 | 25 | #define POLYBENCH_1D(var, dim1, ddim1) \ 26 | var[POLYBENCH_C99_SELECT(dim1, ddim1) + POLYBENCH_PADDING_FACTOR] 27 | #define POLYBENCH_2D(var, dim1, dim2, ddim1, ddim2) \ 28 | var[POLYBENCH_C99_SELECT(dim1, ddim1) + POLYBENCH_PADDING_FACTOR] \ 29 | [POLYBENCH_C99_SELECT(dim2, ddim2) + POLYBENCH_PADDING_FACTOR] 30 | #define POLYBENCH_3D(var, dim1, dim2, dim3, ddim1, ddim2, ddim3) \ 31 | var[POLYBENCH_C99_SELECT(dim1, ddim1) + POLYBENCH_PADDING_FACTOR] \ 32 | [POLYBENCH_C99_SELECT(dim2, ddim2) + POLYBENCH_PADDING_FACTOR] \ 33 | [POLYBENCH_C99_SELECT(dim3, ddim3) + POLYBENCH_PADDING_FACTOR] 34 | #define POLYBENCH_4D(var, dim1, dim2, dim3, dim4, ddim1, ddim2, ddim3, ddim4) \ 35 | var[POLYBENCH_C99_SELECT(dim1, ddim1) + POLYBENCH_PADDING_FACTOR] \ 36 | [POLYBENCH_C99_SELECT(dim2, ddim2) + POLYBENCH_PADDING_FACTOR] \ 37 | [POLYBENCH_C99_SELECT(dim3, ddim3) + POLYBENCH_PADDING_FACTOR] \ 38 | [POLYBENCH_C99_SELECT(dim4, ddim4) + POLYBENCH_PADDING_FACTOR] 39 | #define POLYBENCH_5D(var, dim1, dim2, dim3, dim4, dim5, ddim1, ddim2, ddim3, \ 40 | ddim4, ddim5) \ 41 | var[POLYBENCH_C99_SELECT(dim1, ddim1) + POLYBENCH_PADDING_FACTOR] \ 42 | [POLYBENCH_C99_SELECT(dim2, ddim2) + POLYBENCH_PADDING_FACTOR] \ 43 | [POLYBENCH_C99_SELECT(dim3, ddim3) + POLYBENCH_PADDING_FACTOR] \ 44 | [POLYBENCH_C99_SELECT(dim4, ddim4) + POLYBENCH_PADDING_FACTOR] \ 45 | [POLYBENCH_C99_SELECT(dim5, ddim5) + POLYBENCH_PADDING_FACTOR] 46 | 47 | #define POLYBENCH_ALLOC_1D_ARRAY(n1, type) \ 48 | (type(*)[n1 + POLYBENCH_PADDING_FACTOR]) \ 49 | polybench_alloc_data(n1 + POLYBENCH_PADDING_FACTOR, sizeof(type)) 50 | #define POLYBENCH_ALLOC_2D_ARRAY(n1, n2, type) \ 51 | (type(*)[n1 + POLYBENCH_PADDING_FACTOR][n2 + POLYBENCH_PADDING_FACTOR]) \ 52 | polybench_alloc_data((n1 + POLYBENCH_PADDING_FACTOR) * \ 53 | (n2 + POLYBENCH_PADDING_FACTOR), \ 54 | sizeof(type)) 55 | #define POLYBENCH_ALLOC_3D_ARRAY(n1, n2, n3, type) \ 56 | (type(*)[n1 + POLYBENCH_PADDING_FACTOR][n2 + POLYBENCH_PADDING_FACTOR] \ 57 | [n3 + POLYBENCH_PADDING_FACTOR]) \ 58 | polybench_alloc_data((n1 + POLYBENCH_PADDING_FACTOR) * \ 59 | (n2 + POLYBENCH_PADDING_FACTOR) * \ 60 | (n3 + POLYBENCH_PADDING_FACTOR), \ 61 | sizeof(type)) 62 | #define POLYBENCH_ALLOC_4D_ARRAY(n1, n2, n3, n4, type) \ 63 | (type(*)[n1 + POLYBENCH_PADDING_FACTOR][n2 + POLYBENCH_PADDING_FACTOR] \ 64 | [n3 + POLYBENCH_PADDING_FACTOR][n4 + POLYBENCH_PADDING_FACTOR]) \ 65 | polybench_alloc_data((n1 + POLYBENCH_PADDING_FACTOR) * \ 66 | (n2 + POLYBENCH_PADDING_FACTOR) * \ 67 | (n3 + POLYBENCH_PADDING_FACTOR) * \ 68 | (n4 + POLYBENCH_PADDING_FACTOR), \ 69 | sizeof(type)) 70 | #define POLYBENCH_ALLOC_5D_ARRAY(n1, n2, n3, n4, n5, type) \ 71 | (type(*)[n1 + POLYBENCH_PADDING_FACTOR][n2 + POLYBENCH_PADDING_FACTOR] \ 72 | [n3 + POLYBENCH_PADDING_FACTOR][n4 + POLYBENCH_PADDING_FACTOR] \ 73 | [n5 + POLYBENCH_PADDING_FACTOR]) \ 74 | polybench_alloc_data((n1 + POLYBENCH_PADDING_FACTOR) * \ 75 | (n2 + POLYBENCH_PADDING_FACTOR) * \ 76 | (n3 + POLYBENCH_PADDING_FACTOR) * \ 77 | (n4 + POLYBENCH_PADDING_FACTOR) * \ 78 | (n5 + POLYBENCH_PADDING_FACTOR), \ 79 | sizeof(type)) 80 | 81 | #define POLYBENCH_1D_ARRAY_DECL(var, type, dim1, ddim1) \ 82 | type POLYBENCH_1D(POLYBENCH_DECL_VAR(var), dim1, ddim1); \ 83 | var = POLYBENCH_ALLOC_1D_ARRAY(POLYBENCH_C99_SELECT(dim1, ddim1), type); 84 | #define POLYBENCH_2D_ARRAY_DECL(var, type, dim1, dim2, ddim1, ddim2) \ 85 | type POLYBENCH_2D(POLYBENCH_DECL_VAR(var), dim1, dim2, ddim1, ddim2); \ 86 | var = POLYBENCH_ALLOC_2D_ARRAY(POLYBENCH_C99_SELECT(dim1, ddim1), \ 87 | POLYBENCH_C99_SELECT(dim2, ddim2), type); 88 | #define POLYBENCH_3D_ARRAY_DECL(var, type, dim1, dim2, dim3, ddim1, ddim2, \ 89 | ddim3) \ 90 | type POLYBENCH_3D(POLYBENCH_DECL_VAR(var), dim1, dim2, dim3, ddim1, ddim2, \ 91 | ddim3); \ 92 | var = POLYBENCH_ALLOC_3D_ARRAY(POLYBENCH_C99_SELECT(dim1, ddim1), \ 93 | POLYBENCH_C99_SELECT(dim2, ddim2), \ 94 | POLYBENCH_C99_SELECT(dim3, ddim3), type); 95 | #define POLYBENCH_4D_ARRAY_DECL(var, type, dim1, dim2, dim3, dim4, ddim1, \ 96 | ddim2, ddim3, ddim4) \ 97 | type POLYBENCH_4D(POLYBENCH_DECL_VAR(var), dim1, dim2, , dim3, dim4, ddim1, \ 98 | ddim2, ddim3, ddim4); \ 99 | var = POLYBENCH_ALLOC_4D_ARRAY(POLYBENCH_C99_SELECT(dim1, ddim1), \ 100 | POLYBENCH_C99_SELECT(dim2, ddim2), \ 101 | POLYBENCH_C99_SELECT(dim3, ddim3), \ 102 | POLYBENCH_C99_SELECT(dim4, ddim4), type); 103 | #define POLYBENCH_5D_ARRAY_DECL(var, type, dim1, dim2, dim3, dim4, dim5, \ 104 | ddim1, ddim2, ddim3, ddim4, ddim5) \ 105 | type POLYBENCH_5D(POLYBENCH_DECL_VAR(var), dim1, dim2, dim3, dim4, dim5, \ 106 | ddim1, ddim2, ddim3, ddim4, ddim5); \ 107 | var = POLYBENCH_ALLOC_5D_ARRAY( \ 108 | POLYBENCH_C99_SELECT(dim1, ddim1), POLYBENCH_C99_SELECT(dim2, ddim2), \ 109 | POLYBENCH_C99_SELECT(dim3, ddim3), POLYBENCH_C99_SELECT(dim4, ddim4), \ 110 | POLYBENCH_C99_SELECT(dim5, ddim5), type); 111 | 112 | #define POLYBENCH_DCE_ONLY_CODE if (argc > 42 && !strcmp(argv[0], "")) 113 | 114 | #define polybench_prevent_dce(func) \ 115 | POLYBENCH_DCE_ONLY_CODE \ 116 | func 117 | 118 | #define polybench_start_instruments 119 | #define polybench_stop_instruments 120 | #define polybench_print_instruments 121 | 122 | extern void *polybench_alloc_data(unsigned long long int n, int elt_size); 123 | 124 | #ifndef POLYBENCH_THREAD_MONITOR 125 | #define POLYBENCH_THREAD_MONITOR 0 126 | #endif 127 | 128 | #ifndef POLYBENCH_CACHE_SIZE_KB 129 | #define POLYBENCH_CACHE_SIZE_KB 32770 130 | #endif 131 | 132 | int polybench_papi_counters_threadid = POLYBENCH_THREAD_MONITOR; 133 | double polybench_program_total_flops = 0; 134 | 135 | double polybench_t_start, polybench_t_end; 136 | 137 | unsigned long long int polybench_c_start, polybench_c_end; 138 | 139 | static double rtclock() { return 0; } 140 | 141 | void polybench_flush_cache() { 142 | int cs = POLYBENCH_CACHE_SIZE_KB * 1024 / sizeof(double); 143 | double *flush = (double *)calloc(cs, sizeof(double)); 144 | int i; 145 | double tmp = 0.0; 146 | for (i = 0; i < cs; i++) tmp += flush[i]; 147 | assert(tmp <= 10.0); 148 | free(flush); 149 | } 150 | 151 | void polybench_prepare_instruments() { polybench_flush_cache(); } 152 | 153 | void polybench_timer_start() { 154 | polybench_prepare_instruments(); 155 | polybench_t_start = rtclock(); 156 | } 157 | 158 | void polybench_timer_stop() { polybench_t_end = rtclock(); } 159 | 160 | void polybench_timer_print() { 161 | printf("%0.6f\n", polybench_t_end - polybench_t_start); 162 | } 163 | 164 | static void *xmalloc(size_t num) { 165 | void *new_ = NULL; 166 | int ret = posix_memalign(&new_, 32, num); 167 | if (!new_ || ret) { 168 | fprintf(stderr, "[PolyBench] posix_memalign: cannot allocate memory"); 169 | exit(1); 170 | } 171 | return new_; 172 | } 173 | 174 | void *polybench_alloc_data(unsigned long long int n, int elt_size) { 175 | size_t val = n; 176 | val *= elt_size; 177 | void *ret = xmalloc(val); 178 | return ret; 179 | } 180 | 181 | static inline void print_element(double el, int pos, char *out) { 182 | union { 183 | double datum; 184 | char bytes[8]; 185 | } block; 186 | block.datum = el; 187 | *(out + pos) = (block.bytes[0] & 0xF0 >> 4) + '0'; 188 | *(out + pos + 1) = (block.bytes[0] & 0x0F) + '0'; 189 | *(out + pos + 2) = (block.bytes[1] & 0xF0 >> 4) + '0'; 190 | *(out + pos + 3) = (block.bytes[1] & 0x0F) + '0'; 191 | *(out + pos + 4) = (block.bytes[2] & 0xF0 >> 4) + '0'; 192 | *(out + pos + 5) = (block.bytes[2] & 0x0F) + '0'; 193 | *(out + pos + 6) = (block.bytes[3] & 0xF0 >> 4) + '0'; 194 | *(out + pos + 7) = (block.bytes[3] & 0x0F) + '0'; 195 | *(out + pos + 8) = (block.bytes[4] & 0xF0 >> 4) + '0'; 196 | *(out + pos + 9) = (block.bytes[4] & 0x0F) + '0'; 197 | *(out + pos + 10) = (block.bytes[5] & 0xF0 >> 4) + '0'; 198 | *(out + pos + 11) = (block.bytes[5] & 0x0F) + '0'; 199 | *(out + pos + 12) = (block.bytes[6] & 0xF0 >> 4) + '0'; 200 | *(out + pos + 13) = (block.bytes[6] & 0x0F) + '0'; 201 | *(out + pos + 14) = (block.bytes[7] & 0xF0 >> 4) + '0'; 202 | *(out + pos + 15) = (block.bytes[7] & 0x0F) + '0'; 203 | } 204 | 205 | #define STANDARD_DATASET 206 | 207 | #define TSTEPS 10000 208 | #define LENGTH 50 209 | 210 | #define _PB_TSTEPS POLYBENCH_LOOP_BOUND(TSTEPS, tsteps) 211 | #define _PB_LENGTH POLYBENCH_LOOP_BOUND(LENGTH, length) 212 | 213 | #define DATA_TYPE int 214 | #define DATA_PRINTF_MODIFIER "%d " 215 | 216 | static void 217 | init_array(int length, 218 | DATA_TYPE POLYBENCH_2D(c, LENGTH, LENGTH, length, length), 219 | DATA_TYPE POLYBENCH_2D(W, LENGTH, LENGTH, length, length)) { 220 | #pragma STDC FP_CONTRACT OFF 221 | int i, j; 222 | for (i = 0; i < length; i++) 223 | for (j = 0; j < length; j++) { 224 | c[i][j] = i * j % 2; 225 | W[i][j] = ((DATA_TYPE)i - j) / length; 226 | } 227 | } 228 | 229 | static void print_array(DATA_TYPE out) { 230 | fprintf(stderr, DATA_PRINTF_MODIFIER, out); 231 | fprintf(stderr, "\n"); 232 | } 233 | 234 | static void 235 | kernel_dynprog(int tsteps, int length, 236 | DATA_TYPE POLYBENCH_2D(c, LENGTH, LENGTH, length, length), 237 | DATA_TYPE POLYBENCH_2D(W, LENGTH, LENGTH, length, length), 238 | DATA_TYPE POLYBENCH_3D(sum_c, LENGTH, LENGTH, LENGTH, length, 239 | length, length), 240 | DATA_TYPE *out) { 241 | int iter, i, j, k; 242 | 243 | DATA_TYPE out_l = 0; 244 | 245 | #pragma scop 246 | for (iter = 0; iter < _PB_TSTEPS; iter++) { 247 | for (i = 0; i <= _PB_LENGTH - 1; i++) 248 | for (j = 0; j <= _PB_LENGTH - 1; j++) c[i][j] = 0; 249 | 250 | for (i = 0; i <= _PB_LENGTH - 2; i++) { 251 | for (j = i + 1; j <= _PB_LENGTH - 1; j++) { 252 | sum_c[i][j][i] = 0; 253 | for (k = i + 1; k <= j - 1; k++) 254 | sum_c[i][j][k] = sum_c[i][j][k - 1] + c[i][k] + c[k][j]; 255 | c[i][j] = sum_c[i][j][j - 1] + W[i][j]; 256 | } 257 | } 258 | out_l += c[0][_PB_LENGTH - 1]; 259 | } 260 | #pragma endscop 261 | 262 | *out = out_l; 263 | } 264 | 265 | static void kernel_dynprog_StrictFP( 266 | int tsteps, int length, 267 | DATA_TYPE POLYBENCH_2D(c, LENGTH, LENGTH, length, length), 268 | DATA_TYPE POLYBENCH_2D(W, LENGTH, LENGTH, length, length), 269 | DATA_TYPE POLYBENCH_3D(sum_c, LENGTH, LENGTH, LENGTH, length, length, 270 | length), 271 | DATA_TYPE *out) { 272 | #pragma STDC FP_CONTRACT OFF 273 | int iter, i, j, k; 274 | 275 | DATA_TYPE out_l = 0; 276 | 277 | for (iter = 0; iter < _PB_TSTEPS; iter++) { 278 | for (i = 0; i <= _PB_LENGTH - 1; i++) 279 | for (j = 0; j <= _PB_LENGTH - 1; j++) c[i][j] = 0; 280 | 281 | for (i = 0; i <= _PB_LENGTH - 2; i++) { 282 | for (j = i + 1; j <= _PB_LENGTH - 1; j++) { 283 | sum_c[i][j][i] = 0; 284 | for (k = i + 1; k <= j - 1; k++) 285 | sum_c[i][j][k] = sum_c[i][j][k - 1] + c[i][k] + c[k][j]; 286 | c[i][j] = sum_c[i][j][j - 1] + W[i][j]; 287 | } 288 | } 289 | out_l += c[0][_PB_LENGTH - 1]; 290 | } 291 | 292 | *out = out_l; 293 | } 294 | 295 | #define FP_ABSTOLERANCE 1e-5 296 | 297 | static int check_FP(DATA_TYPE A, DATA_TYPE B) { 298 | double AbsTolerance = FP_ABSTOLERANCE; 299 | double V1 = A; 300 | double V2 = B; 301 | double Diff = fabs(V1 - V2); 302 | if (Diff > AbsTolerance) { 303 | fprintf(stderr, 304 | "A = %lf and B = %lf differ more than" 305 | " FP_ABSTOLERANCE = %lf\n", 306 | V1, V2, AbsTolerance); 307 | return 0; 308 | } 309 | return 1; 310 | } 311 | 312 | int main(int argc, char **argv) { 313 | int length = LENGTH; 314 | int tsteps = TSTEPS; 315 | DATA_TYPE out; 316 | DATA_TYPE out_StrictFP; 317 | POLYBENCH_3D_ARRAY_DECL(sum_c, DATA_TYPE, LENGTH, LENGTH, LENGTH, length, 318 | length, length); 319 | POLYBENCH_2D_ARRAY_DECL(c, DATA_TYPE, LENGTH, LENGTH, length, length); 320 | POLYBENCH_2D_ARRAY_DECL(W, DATA_TYPE, LENGTH, LENGTH, length, length); 321 | init_array(length, POLYBENCH_ARRAY(c), POLYBENCH_ARRAY(W)); 322 | polybench_start_instruments; 323 | kernel_dynprog(tsteps, length, POLYBENCH_ARRAY(c), POLYBENCH_ARRAY(W), 324 | POLYBENCH_ARRAY(sum_c), &out); 325 | polybench_stop_instruments; 326 | polybench_print_instruments; 327 | init_array(length, POLYBENCH_ARRAY(c), POLYBENCH_ARRAY(W)); 328 | kernel_dynprog(tsteps, length, POLYBENCH_ARRAY(c), POLYBENCH_ARRAY(W), 329 | POLYBENCH_ARRAY(sum_c), &out_StrictFP); 330 | if (!check_FP(out, out_StrictFP)) return 1; 331 | polybench_prevent_dce(print_array(out_StrictFP)); 332 | POLYBENCH_FREE_ARRAY(sum_c); 333 | POLYBENCH_FREE_ARRAY(c); 334 | POLYBENCH_FREE_ARRAY(W); 335 | 336 | return 0; 337 | } 338 | -------------------------------------------------------------------------------- /lib/daedalus.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @file daedalus.cpp 3 | * @brief Daedalus Pass Source File 4 | * @author Compilers Lab (UFMG) 5 | * @date 2024-07-08 6 | ***********************************************/ 7 | #include "../include/daedalus.h" 8 | #include "../include/PHIGateAnalyzer.h" 9 | #include "../include/ProgramSlice.h" 10 | #include "../include/debugCommon.h" 11 | #include "../include/reports.h" 12 | #include "llvm/ADT/Statistic.h" 13 | #include "llvm/ADT/StringRef.h" 14 | #include "llvm/Analysis/CFGPrinter.h" 15 | #include "llvm/Analysis/PostDominators.h" 16 | #include "llvm/Analysis/TargetLibraryInfo.h" 17 | #include "llvm/IR/Attributes.h" 18 | #include "llvm/IR/BasicBlock.h" 19 | #include "llvm/IR/Constants.h" 20 | #include "llvm/IR/Function.h" 21 | #include "llvm/IR/InstrTypes.h" 22 | #include "llvm/IR/Instructions.h" 23 | #include "llvm/IR/PassManager.h" 24 | #include "llvm/IR/Verifier.h" 25 | #include "llvm/Support/Casting.h" 26 | #include "llvm/Support/CommandLine.h" 27 | #include "llvm/Support/Debug.h" 28 | #include "llvm/Support/GraphWriter.h" 29 | #include "llvm/Support/Timer.h" 30 | #include "llvm/Support/raw_ostream.h" 31 | #include "llvm/Transforms/IPO/MergeFunctions.h" 32 | #include "llvm/Transforms/Utils/Cloning.h" 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | using namespace llvm; 39 | 40 | #define DEBUG_TYPE "daedalus" 41 | 42 | static TimerGroup PhasesTiming("PhasesTimers", "Timers for Passes' phases"); 43 | static Timer OutlinePhaseTimer("OutlinePhaseTimer", "Outline Phase Timer", 44 | PhasesTiming); 45 | static Timer MergePhaseTimer("MergePhaseTimer", "Merge Phase Timer", 46 | PhasesTiming); 47 | static Timer RemoveInstPhaseTimer("RemoveInstPhaseTimer", 48 | "Remove Instructions Phase Timer", 49 | PhasesTiming); 50 | static Timer SimplifyPhaseTimer("SimplifyPhaseTimer", "Simplify Phase Timer", 51 | PhasesTiming); 52 | 53 | static TimerGroup OutlinePhasesTiming("OutlinePhasesTimers", 54 | "Timers for Outline subphases"); 55 | static Timer GSAConstructionPhaseTimer("GSAConstructionPhasePhaseTimer", 56 | "GSA Construction Phase Timer", 57 | OutlinePhasesTiming); 58 | static Timer SliceIdentificationPhaseTimer("SliceIdentificationPhasePhaseTimer", 59 | "Slice Identification Phase Timer", 60 | OutlinePhasesTiming); 61 | static Timer CanOutlinePhaseTimer("CanOutlinePhasePhaseTimer", 62 | "canOutline Phase Timer", 63 | OutlinePhasesTiming); 64 | static Timer FunctionOutlinePhaseTimer("FunctionOutlinePhasePhaseTimer", 65 | "Function Outline Phase Timer", 66 | OutlinePhasesTiming); 67 | 68 | STATISTIC(TotalFunctionsOutlined, "Total number of functions outlined"); 69 | STATISTIC(TotalSlicesMerged, "Total number of slices that got merged"); 70 | STATISTIC(TotalSlicesDiscarded, "Total number of slices that got discarded"); 71 | STATISTIC(SizeOfLargestSliceBeforeMerging, 72 | "Size of the largest slice function before merging step"); 73 | STATISTIC(SizeOfLargestSliceAfterMerging, 74 | "Size of the largest slice function after merging step"); 75 | 76 | static cl::opt 77 | dumpDot("dump-dot", 78 | cl::desc("Export function slice CFGs as DOT graph files in a " 79 | "dedicated directory per source file"), 80 | cl::init(false)); 81 | cl::opt maxFuncParams( 82 | "max-slice-params", 83 | cl::desc("Maximum number of parameters an outlined function can have"), 84 | cl::init(5)); 85 | cl::opt maxFuncSize( 86 | "max-slice-size", 87 | cl::desc( 88 | "Maximum number of LLVM IR instructions an outlined function can have"), 89 | cl::init(50)); 90 | cl::opt maxFuncUsers( 91 | "max-slice-users", 92 | cl::desc("Maximum number of users an outlined function can have"), 93 | cl::init(100)); 94 | 95 | /** 96 | * @brief Determines if an instruction type can be used as slice criterion. 97 | * 98 | * @details This function checks if the given instruction is one of several 99 | * types that should not be considered for slicing, such as branch instructions, 100 | * return instructions, alloca instructions, comparison instructions, load 101 | * instructions, and store instructions. If the instruction is a PHI 102 | * node, it must not have users that are also PHI nodes within the same basic 103 | * block. 104 | * 105 | * @param I The instruction to check. 106 | * @return True if the instruction type can be sliced, false otherwise. 107 | */ 108 | bool canBeSliceCriterion(const Instruction &I) { 109 | if (isa(I)) return false; 110 | if (isa(I)) return false; 111 | if (isa(I)) return false; 112 | if (isa(I)) return false; 113 | if (isa(I)) return false; 114 | if (isa(I)) return false; 115 | return true; 116 | } 117 | 118 | /** 119 | * @brief Identifies and lists instructions to be removed from a program slice. 120 | * 121 | * This function determines which instructions can be safely removed from a 122 | * program slice, starting from a given instruction and considering a slice 123 | * criterion. It ensures that only instructions that are not global values, 124 | * terminators, or the slice criterion itself are considered removable. 125 | * Additionally, an instruction is deemed removable only if all its users 126 | * are either removable or the slice criterion. 127 | * 128 | * @param start The starting instruction from which to begin the analysis. 129 | * @param sliceCriterion The instruction that serves as the slice criterion 130 | * and should not be removed. 131 | * @param constOriginalInst A set of original instructions that are considered 132 | * valid for removal. 133 | * @param toRemove A set to store the instructions identified as removable. 134 | * 135 | * @return The number of instructions added to the `toRemove` set. 136 | */ 137 | uint listInstructionsToRemove(Instruction *start, 138 | const Instruction *sliceCriterion, 139 | const std::set &constOriginalInst, 140 | std::set &toRemove) { 141 | // First, collect all relevant instructions reachable from 'start'. 142 | std::vector reachable; 143 | std::stack worklist; 144 | std::set visited; 145 | worklist.push(start); 146 | 147 | while (!worklist.empty()) { 148 | Instruction *cur = worklist.top(); 149 | worklist.pop(); 150 | if (!cur) { 151 | LLVM_DEBUG(dbgs() << "\t\tCurrent instruction is null...\n"); 152 | continue; 153 | } 154 | if (visited.count(cur)) continue; 155 | visited.insert(cur); 156 | 157 | LLVM_DEBUG(dbgs() << "\t\tVisiting: " << *cur << "\n"); 158 | 159 | // Skip if it's the slice criterion 160 | if (cur == sliceCriterion) continue; 161 | // Must be in original set 162 | if (!constOriginalInst.count(cur)) continue; 163 | // Cannot remove globals, terminators 164 | if (isa(cur) || cur->isTerminator()) continue; 165 | 166 | reachable.push_back(cur); 167 | 168 | // Enqueue users 169 | for (const auto U : cur->users()) { 170 | if (auto *J = dyn_cast(U)) { 171 | worklist.push(J); 172 | } 173 | } 174 | } 175 | 176 | // Process in reverse order: an instruction is removable if all its users 177 | // are either removable or the sliceCriterion. 178 | for (auto it = reachable.rbegin(); it != reachable.rend(); ++it) { 179 | Instruction *cur = *it; 180 | bool canRem = true; 181 | for (const auto U : cur->users()) { 182 | if (auto *J = dyn_cast(U)) { 183 | if (J == sliceCriterion) continue; 184 | if (!toRemove.count(J)) { 185 | canRem = false; 186 | break; 187 | } 188 | } 189 | } 190 | if (canRem) { 191 | toRemove.insert(cur); 192 | } 193 | } 194 | 195 | return toRemove.size(); 196 | } 197 | 198 | /** 199 | * @brief Removes instructions from slices and simplifies functions. 200 | * 201 | * This function processes a collection of instruction slices, removing 202 | * instructions that are not self-contained or belong to functions that 203 | * should not be merged. It also simplifies functions by removing unnecessary 204 | * instructions and updating function attributes. 205 | * 206 | * @param allSlices A vector of instruction slices to process. 207 | * @param mergedFunctions A set of functions that are allowed to be merged. 208 | * @param toSimplify A set of functions that need to be simplified. 209 | * @return A pair of unsigned integers representing the count of slices that 210 | * were not merged and the count of slices that were not self-contained. 211 | */ 212 | uint removeInstructions(const std::vector &allSlices, 213 | const std::set &mergedFunctions, 214 | std::set &toSimplify) { 215 | std::set toRemove; 216 | uint dontMerge = 0; 217 | 218 | for (const SliceStruct &slice : allSlices) { 219 | Instruction *sliceCriterion = slice.I; 220 | CallInst *callInst = slice.callInst; 221 | Function *F = slice.F; 222 | std::set origInst = slice.originalInstructionsSet; 223 | 224 | if (F == nullptr) continue; 225 | 226 | if (F->hasName()) 227 | LLVM_DEBUG(dbgs() << "Processing slice: " << F->getName() << "\n"); 228 | else 229 | LLVM_DEBUG(dbgs() << "Processing slice: no name\n"); 230 | 231 | F = callInst->getCalledFunction(); 232 | if (mergedFunctions.count(F) == 0) { 233 | 234 | if (F->hasName()) 235 | LLVM_DEBUG(dbgs() << "Function '" << F->getName() 236 | << "' was not merged. Hence, it will be discarded..." 237 | << "\n"); 238 | else 239 | LLVM_DEBUG(dbgs() << "Processing slice: no name\n"); 240 | 241 | removeCallInstruction(F, callInst, sliceCriterion); 242 | ++dontMerge; 243 | continue; 244 | } 245 | 246 | BasicBlock *realEntry = nullptr; 247 | for (BasicBlock &BB : *F) { 248 | if (BB.hasNPredecessors(0)) { 249 | realEntry = &BB; 250 | } 251 | } 252 | if (realEntry) realEntry->moveBefore(&F->getEntryBlock()); 253 | 254 | if (sliceCriterion->getParent() == nullptr) continue; 255 | 256 | std::set tempToRemove; 257 | for (Instruction *J : origInst) { 258 | if (J->getParent() && sliceCriterion != J) { 259 | const uint totalToRemove = 260 | listInstructionsToRemove(J, sliceCriterion, origInst, tempToRemove); 261 | LLVM_DEBUG(dbgs() << "\t" << totalToRemove 262 | << " instruction(s) will be removed...\n"); 263 | } 264 | } 265 | for (Instruction *inst : tempToRemove) { 266 | if (toRemove.insert(inst).second) { 267 | if (const auto *cInst = dyn_cast(inst)) { 268 | if (Function *G = cInst->getCalledFunction(); 269 | G && G->hasFnAttribute(Attribute::NoInline)) { 270 | G->removeFnAttr(Attribute::NoInline); 271 | } 272 | } 273 | } 274 | } 275 | toSimplify.insert(F); 276 | toRemove.insert(sliceCriterion); 277 | } 278 | 279 | for (auto &e : toRemove) { 280 | LLVM_DEBUG(dbgs() << "Removing instruction:" << *e << "\n"); 281 | e->replaceAllUsesWith(UndefValue::get(e->getType())); 282 | e->eraseFromParent(); 283 | } 284 | return dontMerge; 285 | } 286 | 287 | /** 288 | * @brief Removes a function and its call instructions from the LLVM IR. 289 | * 290 | * This function replaces all uses of a specified call instruction with a given 291 | * criterion instruction, then erases the call instruction from its parent. It 292 | * also removes the NoInline attribute from the function, if present, and 293 | * replaces all uses of the function with an undefined value before erasing the 294 | * function from its parent. 295 | * 296 | * @param F The function to be removed. 297 | * @param callInst The call instruction to be replaced and erased. 298 | * @param criterion The instruction to replace the call instruction with. 299 | */ 300 | void removeCallInstruction(Function *F, CallInst *callInst, 301 | Instruction *criterion) { 302 | callInst->replaceAllUsesWith(criterion); 303 | callInst->eraseFromParent(); 304 | 305 | if (!F->getParent()) return; 306 | // AttrBuilder builder(F->getContext()); 307 | // builder.removeAttribute(Attribute::NoInline); 308 | F->removeFnAttr(Attribute::NoInline); 309 | for (auto it = F->user_begin(); it != F->user_end();) { 310 | if (auto *X = dyn_cast(*it)) { 311 | ++it; 312 | X->replaceAllUsesWith(UndefValue::get(X->getType())); 313 | X->eraseFromParent(); 314 | } else { 315 | LLVM_DEBUG(dbgs() << "Warning: Use of function is not a CallInst!\n"); 316 | } 317 | } 318 | F->eraseFromParent(); 319 | } 320 | 321 | /** 322 | * @brief Collects and returns a set of instructions from a given function that 323 | * meet certain criteria. 324 | * 325 | * This function iterates over all basic blocks in the provided function and 326 | * collects instructions that meet specific criteria into a set. The current 327 | * criteria include: 328 | * - Instructions that are instances of BinaryOperator. 329 | * 330 | * @param F A pointer to the function from which instructions are to be 331 | * collected. 332 | * @return A set of pointers to instructions that meet the specified criteria. 333 | */ 334 | SmallVector instSetMeetCriterion(Function *F) { 335 | SmallVector instVec; 336 | for (auto &BB : *F) { 337 | const Instruction *term = BB.getTerminator(); 338 | assert(term && "Error: A basic block in an original function is missing a " 339 | "terminator instruction..."); 340 | for (Instruction &I : BB) { 341 | if (isa(I)) { 342 | instVec.push_back(&I); 343 | } 344 | } 345 | } 346 | 347 | return instVec; 348 | } 349 | 350 | /** 351 | * @brief Counts the number of instructions in a given function. 352 | * 353 | * This function iterates over all basic blocks in the provided function 354 | * and sums up the number of instructions in each basic block. 355 | * 356 | * @param F Pointer to the function whose instructions are to be counted. 357 | * @return The total number of instructions in the function. 358 | */ 359 | unsigned int numberOfInstructions(Function *F) { 360 | unsigned int instCount = 0; 361 | for (BasicBlock &BB : *F) instCount += BB.size(); 362 | return instCount; 363 | } 364 | 365 | /** 366 | * @brief Counts the number of functions that have been merged into a given 367 | * function. 368 | * 369 | * This function iterates through a map of deleted functions to their 370 | * corresponding new functions and counts how many times the given function 371 | * appears as a target of merging. 372 | * 373 | * @param F The function to check for merged functions. 374 | * @param delToNewFunc A map where the key is a deleted function and the value 375 | * is the function it was merged into. 376 | * @return The number of functions that have been merged into the given 377 | * function, including the function itself. 378 | */ 379 | unsigned int 380 | numberOfMergedFunctions(const Function *F, 381 | std::map &delToNewFunc) { 382 | unsigned int mergedFuncCount = 1; 383 | for (auto &[_, snd] : delToNewFunc) 384 | if (snd == F) mergedFuncCount++; 385 | return mergedFuncCount; 386 | } 387 | 388 | /** 389 | * @brief Generates DOT files for a set of functions and stores them in a 390 | * directory. 391 | * 392 | * This function creates a directory named after the module identifier with a 393 | * suffix ".dump_dot". It then iterates over the provided set of functions, and 394 | * for each function that has a name, it generates a DOT file representing the 395 | * function's structure. 396 | * 397 | * @param M The module containing the functions. 398 | * @param newFunctions A set of pointers to functions for which DOT files will 399 | * be generated. 400 | */ 401 | void functionSlicesToDot(const Module &M, 402 | const std::set &newFunctions) { 403 | 404 | // Create directory 405 | const std::filesystem::path dotDir = 406 | std::filesystem::current_path() / (M.getModuleIdentifier() + ".dump_dot"); 407 | 408 | std::error_code errorCode; 409 | 410 | std::filesystem::create_directory(dotDir, errorCode); 411 | 412 | if (errorCode) { 413 | errs() << "Failed to create directory '" 414 | << std::filesystem::absolute(dotDir) 415 | << "' Reason: " << errorCode.message() << "\n"; 416 | return; 417 | } 418 | 419 | for (const auto newFunc : newFunctions) { 420 | if (newFunc->hasName()) { 421 | // Create a DOT file for the function and handle errors gracefully. 422 | auto dotFilePath = dotDir / (newFunc->getName().str() + ".dot"); 423 | raw_fd_ostream sliceDotFile(dotFilePath.string(), errorCode); 424 | 425 | // If the file cannot be opened, report the error and skip processing. 426 | if (errorCode) { 427 | errs() << "Failed to create slice dot file '" 428 | << std::filesystem::absolute(dotFilePath) 429 | << "' Reason: " << errorCode.message() << "\n"; 430 | continue; 431 | } 432 | 433 | errs() << "Writing '" << std::filesystem::absolute(dotFilePath) 434 | << "'... "; 435 | DOTFuncInfo fnInfo(newFunc); 436 | WriteGraph(sliceDotFile, &fnInfo); 437 | sliceDotFile.close(); 438 | errs() << "Done.\n"; 439 | } 440 | } 441 | } 442 | 443 | /** 444 | * @brief Identifies and collects basic blocks in a function that are involved 445 | * in try-catch logic, including blocks dominated by invoke instructions and 446 | * blocks post-dominated by exception destinations. 447 | * 448 | * This function analyzes the control flow of a given function to detect basic 449 | * blocks that are part of try-catch constructs. It uses dominator and 450 | * post-dominator trees to determine the relationships between blocks. 451 | * 452 | * @param F The function to analyze for try-catch logic. 453 | * @return A set of pointers to basic blocks that are part of try-catch logic. 454 | */ 455 | std::set searchForTryCatchLogic(Function &F) { 456 | const DominatorTree DT(F); 457 | const PostDominatorTree PDT(F); 458 | std::set tryCatchBlocks; 459 | for (auto &BB : F) { 460 | for (auto &I : BB) { 461 | if (const auto *Invoke = dyn_cast(&I)) { 462 | const BasicBlock *exceptionDest = Invoke->getUnwindDest(); 463 | SmallVector Descendants; 464 | DT.getDescendants(&BB, Descendants); 465 | for (BasicBlock *DomBlock : Descendants) { 466 | if (DT.dominates(&BB, DomBlock)) { 467 | tryCatchBlocks.insert(DomBlock); 468 | } 469 | } 470 | for (BasicBlock &CatchBB : F) { 471 | if (PDT.dominates(&CatchBB, exceptionDest)) { 472 | tryCatchBlocks.insert(&CatchBB); 473 | } 474 | } 475 | } 476 | } 477 | } 478 | return tryCatchBlocks; 479 | } 480 | 481 | void outlinePhase(std::set &FtoMap, FunctionAnalysisManager &FAM, 482 | std::vector &allSlices) { 483 | unsigned int outline_counter = 0; 484 | for (Function *F : FtoMap) { 485 | uint ki = 0; 486 | for (auto &BB : *F) { 487 | BB.setName((BB.hasName()) ? BB.getName() : "BB_" + std::to_string(ki++)); 488 | } 489 | 490 | // Criterion Set 491 | SmallVector S = instSetMeetCriterion(F); 492 | // filter binary instructions for building a set of instructions 493 | // that can be used as slicing criterion. this function enables us 494 | // to change how we manage the slicing criterion. 495 | 496 | // Search for try-catch logic inside the current function 497 | std::set tryCatchBlocks = searchForTryCatchLogic(*F); 498 | 499 | // Construct gating functions for all PHI nodes in the function 500 | DominatorTree &DT = FAM.getResult(*F); 501 | std::unordered_map> 502 | predicates; 503 | { 504 | if (llvm::TimePassesIsEnabled) { 505 | TimeRegion ScopedTimerGSA(GSAConstructionPhaseTimer); 506 | } 507 | PHIGateAnalyzer GSAAnalyzer(*F, DT); 508 | predicates = GSAAnalyzer.getGatesForAllPhis(); 509 | } 510 | 511 | LLVM_DEBUG(dbgs() << "daedalus.cpp: Function: " << F->getName() << ",\n"); 512 | 513 | for (Instruction *I : S) { 514 | if (!canBeSliceCriterion(*I)) continue; 515 | 516 | LLVM_DEBUG(dbgs() << "\tInstruction (Basic Block: " 517 | << I->getParent()->getName() << "):\n\t\t" << *I 518 | << "\n"); 519 | 520 | if (maxFuncSize.getNumOccurrences() > 0 && 521 | I->getNumUses() > maxFuncUsers) { 522 | LLVM_DEBUG(dbgs() << COLOR::RED 523 | << "Slice has too many users: " << I->getNumUses() 524 | << ", max number of users: " << maxFuncUsers << "\n" 525 | << COLOR::CLEAN); 526 | continue; 527 | } 528 | 529 | ProgramSlice ps; 530 | { 531 | if (llvm::TimePassesIsEnabled) { 532 | TimeRegion ScopedTimerSlicer(SliceIdentificationPhaseTimer); 533 | } 534 | ps = ProgramSlice(*I, *F, FAM, predicates); 535 | } 536 | TargetLibraryInfo &TLI = FAM.getResult(*F); 537 | AAResults *AA = &FAM.getResult(*F); 538 | 539 | uint canOutlineResult; 540 | 541 | { 542 | if (llvm::TimePassesIsEnabled) { 543 | TimeRegion ScopedTimerCanOutline(CanOutlinePhaseTimer); 544 | } 545 | canOutlineResult = ps.canOutline(AA, TLI, tryCatchBlocks); 546 | } 547 | 548 | if (!canOutlineResult) { 549 | LLVM_DEBUG(dbgs() << "Daedalus could not outline a slice function " 550 | "for the criterion: " 551 | << *I << "\n"); 552 | continue; 553 | } 554 | 555 | LLVM_DEBUG({ 556 | // Print the entire module containing the parent function to a file, 557 | // to extract the faulty function separately later 558 | Module *parentModule = ps.getParentFunction()->getParent(); 559 | if (parentModule) { 560 | std::string baseName = 561 | std::filesystem::path(parentModule->getModuleIdentifier()) 562 | .stem() 563 | .string(); 564 | std::string fileName = baseName + ".parent_module.ll"; 565 | std::error_code ec; 566 | raw_fd_ostream outFile(fileName, ec, sys::fs::OF_Text); 567 | if (!ec) { 568 | parentModule->print(outFile, nullptr); 569 | outFile.close(); 570 | dbgs() << "\nParent function module written to file: " << fileName 571 | << "\n"; 572 | } else { 573 | dbgs() << "\nFailed to write parent module to file: " 574 | << ec.message() << "\n"; 575 | } 576 | } 577 | }); 578 | 579 | Function *G; 580 | { 581 | if (llvm::TimePassesIsEnabled) { 582 | TimeRegion ScopedTimerOutline(FunctionOutlinePhaseTimer); 583 | } 584 | G = ps.outline(&outline_counter); 585 | } 586 | 587 | if (G == nullptr) continue; 588 | outline_counter++; 589 | 590 | // Get the original instruction to check if it can be removed 591 | std::map constOriginalInst = 592 | ps.getInstructionInSlice(); 593 | 594 | std::set originInstructionSet; 595 | for (auto &[fst, _] : constOriginalInst) originInstructionSet.insert(fst); 596 | 597 | // Replace all uses of I with the correpondent call to the new outlined 598 | // function 599 | SmallVector funcArgs = ps.getOrigFunctionArgs(); 600 | CallInst *callInst = 601 | CallInst::Create(G, funcArgs, I->getName(), I->getParent()); 602 | Instruction *moveTo = I; 603 | if (isa(I)) moveTo = I->getParent()->getFirstNonPHI(); 604 | callInst->moveBefore(moveTo); 605 | I->replaceAllUsesWith(callInst); 606 | 607 | SliceStruct slice = {I, callInst, G, funcArgs, originInstructionSet, 608 | false}; 609 | allSlices.push_back(slice); 610 | 611 | LLVM_DEBUG(dbgs() << COLOR::GREEN << "outlined!" << COLOR::CLEAN << '\n'); 612 | } 613 | } 614 | } 615 | 616 | void mergePhase(std::set &originalFunctions, 617 | std::set &outlinedFunctions, 618 | std::vector &allSlices, 619 | std::map &delToNewFunc) { 620 | for (SliceStruct &slice : allSlices) { 621 | Instruction *sliceCriterion = slice.I; 622 | Function *F = slice.F; 623 | Function *originalF = sliceCriterion->getParent()->getParent(); 624 | originalFunctions.insert(originalF); 625 | outlinedFunctions.insert(F); 626 | LLVM_DEBUG(if (numberOfInstructions(F) > SizeOfLargestSliceBeforeMerging) 627 | SizeOfLargestSliceBeforeMerging = numberOfInstructions(F);); 628 | } 629 | 630 | // Say S and T are two slices that will merge, if we replace S by T, Then 631 | // delToNewFunc is a map from S to T "deleted function to newFunction". 632 | auto [mergeFunc, delToNewFuncTmp] = 633 | MergeFunctionsPass::runOnFunctions(outlinedFunctions); 634 | 635 | if (mergeFunc) { 636 | LLVM_DEBUG(dbgs() << "MergeFunc returned true!\n"); 637 | delToNewFunc = delToNewFuncTmp; 638 | } else { 639 | LLVM_DEBUG(dbgs() << "MergeFunc returned false...\n"); 640 | } 641 | } 642 | 643 | void removeInstPhase(uint *dontMerge, std::set &toSimplify, 644 | std::vector &allSlices, 645 | std::map &delToNewFunc) { 646 | std::set 647 | mergedFunctions; // If a function is on this set, there are some 648 | // other function that merges with it. 649 | for (auto [A, B] : delToNewFunc) { 650 | if (B == nullptr) continue; 651 | while (delToNewFunc.count(B)) B = delToNewFunc[B]; 652 | LLVM_DEBUG(if (numberOfInstructions(B) > SizeOfLargestSliceAfterMerging) 653 | SizeOfLargestSliceAfterMerging = numberOfInstructions(B);); 654 | mergedFunctions.insert(B); 655 | } 656 | 657 | *dontMerge = removeInstructions(allSlices, mergedFunctions, toSimplify); 658 | } 659 | 660 | void simplifyPhase(std::set &toSimplify, 661 | std::set &originalFunctions, 662 | FunctionAnalysisManager &FAM) { 663 | for (auto F : toSimplify) { 664 | llvm::ProgramSlice::simplifyCfg(F, FAM); 665 | } 666 | for (auto originalF : originalFunctions) { 667 | llvm::ProgramSlice::simplifyCfg(originalF, FAM); 668 | } 669 | } 670 | 671 | void printPhase(Module &M, std::map &delToNewFunc) { 672 | LLVM_DEBUG({ 673 | dbgs() << "== PRINT PHASE ==\n"; 674 | if (!delToNewFunc.empty()) { 675 | M.print(llvm::outs(), nullptr); 676 | } else { 677 | dbgs() << "No functions were merged!\n"; 678 | } 679 | }); 680 | } 681 | 682 | void reportGenPhase(Module &M, uint *dontMerge, 683 | std::set &toSimplify, 684 | std::vector &allSlices, 685 | std::map &delToNewFunc) { 686 | LLVM_DEBUG( 687 | LLVM_DEBUG(dbgs() << "== REPORT GENERATION PHASE ==\n"); 688 | LLVM_DEBUG(dbgs() << "Exporting slices' metadata to disk...\n"); 689 | std::filesystem::path sourceFileName = 690 | std::filesystem::path(M.getModuleIdentifier()).stem().string(); 691 | std::filesystem::path exportedFileName = 692 | sourceFileName.string() + "_slices_report.log"; 693 | 694 | TotalFunctionsOutlined = allSlices.size(); 695 | TotalSlicesMerged = delToNewFunc.size(); 696 | TotalSlicesDiscarded = *dontMerge; 697 | 698 | ReportWriter ReportWriterObj(exportedFileName); ReportWriterObj.writeLine( 699 | "totalFunctionsOutlined = " + std::to_string(TotalFunctionsOutlined)); 700 | ReportWriterObj.writeLine( 701 | "totalSlicesMerged = " + 702 | std::to_string(TotalSlicesMerged)); // Note: all delToNewFunc keys 703 | // are unique slices 704 | ReportWriterObj.writeLine("totalSlicesDiscarded = " + 705 | std::to_string(TotalSlicesDiscarded)); 706 | ReportWriterObj.writeLine( 707 | "sizeOfLargestSliceBeforeMerging = " + 708 | std::to_string(SizeOfLargestSliceBeforeMerging)); 709 | ReportWriterObj.writeLine("sizeOfLargestSliceAfterMerging = " + 710 | std::to_string(SizeOfLargestSliceAfterMerging)); 711 | ReportWriterObj.writeLine("mergedSlicesMetadata:"); 712 | 713 | std::set checkedFunctions; 714 | for (auto [deletedFunc, newFunc] : delToNewFunc) { 715 | while (delToNewFunc.count(newFunc)) newFunc = delToNewFunc[newFunc]; 716 | if (newFunc->hasName() && checkedFunctions.count(newFunc) == 0) { 717 | checkedFunctions.insert(newFunc); 718 | ReportWriterObj.writeLine("\t" + newFunc->getName().str() + ":"); 719 | ReportWriterObj.writeLine( 720 | "\t\tsize = " + std::to_string(numberOfInstructions(newFunc))); 721 | ReportWriterObj.writeLine( 722 | "\t\tnumberOfMergedFunctions = " + 723 | std::to_string(numberOfMergedFunctions(newFunc, delToNewFunc))); 724 | } 725 | } 726 | 727 | LLVM_DEBUG(dbgs() << "Metadata written into '" << exportedFileName 728 | << "' file...\n");); 729 | 730 | if (dumpDot) { 731 | functionSlicesToDot(M, toSimplify); 732 | } 733 | } 734 | 735 | namespace Daedalus { 736 | 737 | /** 738 | * @brief Runs the Daedalus LLVM pass on a given module. 739 | * 740 | * @details This function performs slicing on the given module, creating and 741 | * outlining program slices, and removing instructions that meet specific 742 | * criteria. It attempts to merge slices and remove unused instructions from 743 | * the original functions. 744 | * 745 | * @param M The module to run the pass on. 746 | * @param MAM The module analysis manager. 747 | * @return The preserved analyses after running the pass. 748 | */ 749 | PreservedAnalyses DaedalusPass::run(Module &M, ModuleAnalysisManager &MAM) { 750 | std::set FtoMap; 751 | std::vector allSlices; 752 | std::set originalFunctions; 753 | std::set outlinedFunctions; 754 | std::map delToNewFunc; 755 | std::set toSimplify; 756 | uint dontMerge = 0; 757 | 758 | if (Error Err = M.materializeAll()) { 759 | handleAllErrors(std::move(Err), [](const ErrorInfoBase &EIB) { 760 | errs() << "Error materializing module: " << EIB.message() << "\n"; 761 | }); 762 | } 763 | 764 | for (Function &F : M.getFunctionList()) 765 | if (!F.empty()) FtoMap.insert(&F); 766 | 767 | FunctionAnalysisManager &FAM = 768 | MAM.getResult(M).getManager(); 769 | 770 | LLVM_DEBUG(dbgs() << "== OUTLINING INST PHASE ==\n"); 771 | { 772 | if (llvm::TimePassesIsEnabled) { 773 | TimeRegion ScopedTimer(OutlinePhaseTimer); 774 | } 775 | outlinePhase(FtoMap, FAM, allSlices); 776 | } 777 | 778 | LLVM_DEBUG(dbgs() << "== MERGE SLICES FUNC PHASE ==\n"); 779 | { 780 | if (llvm::TimePassesIsEnabled) { 781 | TimeRegion ScopedTimer(MergePhaseTimer); 782 | } 783 | mergePhase(originalFunctions, outlinedFunctions, allSlices, delToNewFunc); 784 | } 785 | 786 | LLVM_DEBUG(dbgs() << "== REMOVING INST PHASE ==\n"); 787 | { 788 | if (llvm::TimePassesIsEnabled) { 789 | TimeRegion ScopedTimer(RemoveInstPhaseTimer); 790 | } 791 | removeInstPhase(&dontMerge, toSimplify, allSlices, delToNewFunc); 792 | } 793 | 794 | LLVM_DEBUG(dbgs() << "== SIMPLIFY PHASE ==\n"); 795 | { 796 | if (llvm::TimePassesIsEnabled) { 797 | TimeRegion ScopedTimer(SimplifyPhaseTimer); 798 | } 799 | simplifyPhase(toSimplify, originalFunctions, FAM); 800 | } 801 | 802 | // debug related 803 | printPhase(M, delToNewFunc); 804 | reportGenPhase(M, &dontMerge, toSimplify, allSlices, delToNewFunc); 805 | 806 | if (verifyModule(M, &errs())) { 807 | errs() << "Module verification failed! Printing module:\n"; 808 | M.print(errs(), nullptr); 809 | assert(false && "Module verification failed!"); 810 | } 811 | 812 | return PreservedAnalyses::none(); 813 | } 814 | } // namespace Daedalus 815 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------