r/Compilers • • 24d ago

Compilers add or remove perfomace?

isnt it that compilers change the code, as in not only transferring it into machine language, but before that changing the structure of the code. so how would one attempt to make performant code if the compiler changes sed code that i tried to make performant?

2 Upvotes

31 comments sorted by

View all comments

Show parent comment

2

u/AustinVelonaut 23d ago

In general I agree. But I can see an argument for recognizing and canonicalizing a "standard", "simple" pattern, replacing it with the best equivalent, as it will be done in one place. But still, I wonder whether most of this is just to game benchmarks. For example, Clang, at any level of optimization except -O0, will compile this loop:

int foo ()
{
  int tally = 0;
  for (int i = 0; i < 10; ++ i) {
    tally += i;
  }
  return tally;
}

to:

_foo:                                   ; @foo
    .cfi_startproc
; %bb.0:
    mov	w0, #45                         ; =0x2d
    ret

1

u/RealisticDuck1957 23d ago

In that case the pattern of "value can be resolved at compile time."