Giter VIP home page Giter VIP logo

llvm-middle-end-optimizations's Introduction

LLVM-Middle-End-Optimizations

Machine independent passes to optimise LLVM intermediate representation.

Local Optimizations

Installation

In order to run the optimization passes, it is necessary having LLVM 17.0.6 source code installed.
Official repository here.

The following steps assume that LLVM 17.0.6 has been installed.
Clone the current repository:

git clone https://github.com/RaffaeleTranfaglia/LLVM-Middle-End-Optimizations.git

src/LocalOpts.cpp file must be moved to the following directory:

$SRC/llvm/lib/Transforms/Utils

Add LocalOpts.cpp in $SRC/llvm/lib/Transforms/Utils/CMakeLists.txt.
src/LocalOpts.h file must be moved to the following directory:

$SRC/llvm/include/llvm/Transforms/Utils

Replace SRC/llvm/lib/Passes/PassRegistry.def with the provided src/PassRegistry.def.
Add the follwing line to SRC/llvm/lib/Passes/PassBuilder.cpp:

#include "llvm/Transforms/Utils/LocalOpts.h"

Where $SRC is the source folder of the project.
To compile the source code:

cd $ROOT/BUILD
make opt
make install

Otherwise, to install the source code already containig the optimized passes' files: here.

Introduced Optimizations

Constant Folding

Constant Folding execute at compile time operations involving contant values.
Examples:

  • x = 4 + 9x = 13 + 0
  • x = 6 * 2x = 12 + 0

Observation: The assignment of the computed constant is carryed out using an addition of the given constant with zero.
Afterwards the introduced add will furtherly be optimized by the Algebric Identity optimization.

Algebraic Identity optimization

Algebraic Identity aims to optimise operation containing neutral values.
Examples:

  • y = x + 0 → every use of y is replaced with x
  • a = b * 1 → every use of a is replaced with b

Strength Reduction

A strength reduction pass replace mul instructions with shift instruction to reduce computational complexity. Example:

  • x * 15(x << 4) - x
  • x * 17(x << 5) - 15x
  • x / 16x >> 4

Observations:
In case the element "subtracted" can be optimized, the algorithm will optimize further more the operation. In case it is used on divisions, the divisor must be an exact multiple of 2.

Multi-instruction optimization

Multi instruction optimization operates in cases where, given a SSA register, the same fixed amount is addend and subtracted from it. Examples:

  • y = x + 2; z = y - 2 → every use of z is replaced with x
  • y = x + 2; z = y / 2 → every use of z is replaced with x

Global Optimizations

DataFlowAnalysis folder contains global optimizations algorithms.
Optimization tasks addressed:

  • Very Busy Expressions
  • Dominator Analysis
  • Constant Propagation

Loop Invariant Code Motion (LICM)

Instructions that meet the following requirements are candidate for code motion:

  • are invariant
  • dominate their uses
  • dominate the exits of the loop or are dead outside the loop

Candidated instruction may be moved outside the loop (just before the loop header, in the so called preheader block) in order to be executed only one time.

LoopOpts.cpp and LoopOpts.h files contain the Loop Invariant Code Motion pass.
In order to make the pass work, src/LoopOpts.cpp file must be moved to the following directory:

$SRC/llvm/lib/Transforms/Utils

Add LoopOpts.cpp in $SRC/llvm/lib/Transforms/Utils/CMakeLists.txt.
src/LoopOpts.h file must be moved to the following directory:

$SRC/llvm/include/llvm/Transforms/Utils

Replace SRC/llvm/lib/Passes/PassRegistry.def with the provided src/PassRegistry.def.
Add the follwing line to SRC/llvm/lib/Passes/PassBuilder.cpp:

#include "llvm/Transforms/Utils/LoopOpts.h"

Where $SRC is the source folder of the project.
To compile the source code:

cd $ROOT/BUILD
make opt
make install

Testing

Tests folder contains different LLVM files to test the optimizations.

Compile the source code:

cd $ROOT/BUILD
make opt
make install

To run tests:

opt -p <optimization_pass_name> <file_name>.ll -o <file_name_optimized>.bc
llvm-dis <file_name_optimized>.bc -o <file_name_optimized>.ll

Note: Implemented passes names are localopts and loopopts.

Authors

  • Raffaele Tranfaglia
  • Matteo Venturi
  • Mirco Piccinini

llvm-middle-end-optimizations's People

Contributors

raffaeletranfaglia avatar glixes avatar

Stargazers

 avatar  avatar  avatar Antonio Stano avatar Francesco Mecatti avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.