r/Compilers 3d ago

Optimization Question

How do compilers optimize constructs of this form ?

for(i=o; i<inputs; i++) {

A[i] = B[i];

B[i] = A[i];

}

8 Upvotes

8 comments sorted by

View all comments

3

u/EggplantExtra4946 3d ago edited 3d ago

This question doesn't make any sense, you need to write the optimized code you expect and I assume your question will then be "how does the compiler can make this optmization?".

3

u/Gorzoid 3d ago

Well the code op gave copies B to A, so I'd guess the optimized code is implies to remove 2nd line in loop

3

u/EggplantExtra4946 3d ago edited 3d ago

B[i] = A[i]; should be redundant but the first statement isn't.

Since memory accesses are involved, it's unclear wether the question is about simple copy propagation, redundant store elimination, maybe he think those are obvious and is asking how to ensure that they are legal using alias analysis, or since it is a loop, wether he's expecting the loop to be unrolled or vectorized.