Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize pass examples in C in llvm 15 #33

Open
hstk30-hw opened this issue Oct 9, 2024 · 4 comments
Open

Optimize pass examples in C in llvm 15 #33

hstk30-hw opened this issue Oct 9, 2024 · 4 comments

Comments

@hstk30-hw
Copy link

Hope to test the special optimize pass easily

@hstk30-hw
Copy link
Author

hstk30-hw commented Oct 9, 2024

Tail Recursion Elimination

https://godbolt.org/z/YM7v4n3G9

code:

int factorial (int x, int y){
  if (x==0)
    return y;
  else
    return factorial(x-1,y*x);
}

cmd: -O2 -fno-vectorize

@hstk30-hw
Copy link
Author

GlobalOpt

SROA(scalar replacement of aggregates)

https://godbolt.org/z/13jKve8Ef

#include <stdio.h>

typedef struct {
    int x;
    int y;
} Point;

static Point p = {10, 20};

void updatePoint() {
    p.x += 5;
    p.y += 10;
}

void printPoint() {
    printf("Point: (%d, %d)\n", p.x, p.y);
}

cmd: -O2

OptimizeGlobalAddressOfAllocation

body

https://godbolt.org/z/qxx1aMWhM

extern void *malloc(unsigned long size);
extern void f1();

static int *g_ptr;

int f() {
    g_ptr = malloc(sizeof(int) * 1);
    *g_ptr = -1;
    return 0;
}

int main() {
    f();
    *g_ptr = 1;
    f1();
    *((char*)g_ptr) = 2;

    return *g_ptr;
}

init

https://godbolt.org/z/37hevohzd

extern void *malloc(unsigned long size);

static int *g_ptr;

static void init_g() {
    g_ptr = malloc(sizeof(int) * 1);
    return ;
}

static int is_init() {
    int* local_ptr = g_ptr;
    init_g();
    if (local_ptr == 0)
        return 1;
    else
        return -1;
}

int main() {
    g_ptr = 0;
    int res = is_init();

    return res;
}

ShrinkGlobalToBoolean

https://godbolt.org/z/n4rqj6Tec

static int flag = 0;

void store() {
    flag = 5;
    return ;
}

int bar() {
    return flag;
}

@hstk30-hw
Copy link
Author

PartialInlining

https://godbolt.org/z/76dvM8MKn

__attribute__((noinline)) int notinline(int* align_val){
  *align_val = 1;
  return 0;
}

int inlinedFunc(int cond, int* align_val) {
  if (cond) {
    *align_val = 10;
  }
  notinline(align_val);
  notinline(align_val);
  notinline(align_val);
  notinline(align_val);
  notinline(align_val);
  notinline(align_val);
  notinline(align_val);
  notinline(align_val);

  return 0;
}

extern void disableTailCallEli(int *);
int dummyCaller(int cond, int* align_val) {
  int l = 3;
  disableTailCallEli(&l);
  int val = inlinedFunc(cond, align_val);
  return val;
}

cmd: -O1 -mllvm -enable-partial-inlining=1 -mllvm -skip-partial-inlining-cost-analysis=1

@hstk30-hw
Copy link
Author

Constant Propagation and Dead Argument Elimination

https://godbolt.org/z/fd3M1s5Yq

extern void use(int a);

__attribute__((noinline))
static void test1(int a) {
    use(a);
}

__attribute__((noinline))
static int test2(int a) {
    use(a);
    return a + 1;
}

int main() {
    test1(1);
    // test1(2); 
    test2(2);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant