yershuachu
yershuachu

Reputation: 788

LLVM if else optimization

Will LLVM remove unused else from such code?

if (some_var) {
  some_var++;
}
else {
  // i will do some day something here
}

Upvotes: 1

Views: 279

Answers (1)

yershuachu
yershuachu

Reputation: 788

So in case anyone wondered clang is making big magic with optimization and it all can be checked using:

clang -c -mllvm -print-after-all -O2 test.c

For following code I got following IR optimized code:

int a(int b) {
  int i = 2;
  if (b>=i) {
    b++;
  }
  else{
  }
  return b;
}

define i32 @a(i32 %b) #0 {
  %1 = icmp sgt i32 %b, 1
  %2 = zext i1 %1 to i32
  %.b = add nsw i32 %2, %b
  ret i32 %.b
}


int a(int b) {
  int i = 2;
  if (b>=i) {
    b++;
  }
  else{
    b--;
  }
  return b;
}

define i32 @a(i32 %b) #0 {
  %1 = icmp sgt i32 %b, 1
  %.0.v = select i1 %1, i32 1, i32 -1
  %.0 = add i32 %.0.v, %b
  ret i32 %.0
}

Which means it removes unused else.

Upvotes: 1

Related Questions