Skip to content

Instantly share code, notes, and snippets.

@dannas
Last active September 25, 2020 06:01
Show Gist options
  • Save dannas/7696d9565f530f8d27a165a565053ef7 to your computer and use it in GitHub Desktop.
Save dannas/7696d9565f530f8d27a165a565053ef7 to your computer and use it in GitHub Desktop.
Examples of loop hoisting (LICM)
#include <stddef.h>
// TODO(dannas): Find examples of loop hoisting. WIP
// https://godbolt.org/z/YWf5c7
// baseline
void f1(int a[], size_t N) {
for (size_t i = 0; i < N; i++) {
a[i] = i;
}
}
// Mult strength-reduced to shift
void f2(int a[], size_t N) {
for (size_t i = 0; i < N; i++) {
a[i] = i*2;
}
}
// x is hoisted outside the loop
void f3(int a[], size_t N) {
for (size_t i = 0; i < N; i++) {
int x = N*N;
a[i] = x + i;
}
}
// N*N is hoisted outside the loop
void f4(int a[], size_t N) {
for (size_t i = 0; i < N*N; i++) {
a[i] = i;
}
}
// Was hoping for loop unswitching, but doesn't happen
void f5(int a[], size_t N, int x) {
for (size_t i = 0; i < N; i++) {
if (x) {
a[i] = i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment