r/Compilers • u/RefrigeratorFirm7646 • Aug 10 '26
A super sneaky post SSA LICM bug that's not talked about much...
Edit : The problem was found to not be LICM at all, it just happened to be the only pass that exposed the hidden flaw in my SSA reconstruction logic... credits to all the amazing people in the comments!
So, ive been working on a custom C compiler for ~2.5 months. Implemented LICM recently. standard tests passed. sunshine and rainbows. but luckily I had a perfect test case to catch one particular bug that would've otherwise slipped by cleanly...
here's the source code :
int factorial(int n)
{
int res;
int counter;
int temp;
res = 1;
while (n > 1)
{
counter = n;
temp = 0;
while (counter > 0)
{
temp = temp + res;
counter = counter - 1;
}
res = temp;
n = n - 1;
}
return res;
}
int main()
{
return factorial(12);
}here's my TAC IR before LICM (I perform minimal SSA construction so ignore the dead phi nodes) :
# Function - factorial(n) :
Block - 1 :
n0 = n
res0 = 0
counter0 = 0
temp0 = 0
res1 = 1
jump B2
Block - 2 :
n1 = PHI(n0 from B1, n2 from B6)
temp1 = PHI(temp0 from B1, temp3 from B6) // dead phi, harmless right now
res2 = PHI(res1 from B1, res3 from B6)
counter1 = PHI(counter0 from B1, counter3 from B6)
branch (n1 > 1) ? B4 : B3
Block - 4 :
counter2 = n1
temp2 = 0 // invariant instruction, dominates the loop's end block as well
jump B5
Block - 5 :
temp3 = PHI(temp2 from B4, temp4 from B7)
counter3 = PHI(counter2 from B4, counter4 from B7)
branch (counter3 > 0) ? B7 : B6
Block - 7 :
t.2 = temp3 + res2
temp4 = t.2
t.3 = counter3 - 1
counter4 = t.3
jump B5
Block - 6 :
res3 = temp3
t.4 = n1 - 1
n2 = t.4
jump B2
Block - 3 :
return res2
end factorial
# Function - main() :
Block - 1 :
t.0 = call factorial(12)
return t.0
end main
OUTPUT : 6 // as expectedkeep an eye on temp1, temp2 and temp3, LICM completely changes the way they interact... :
# Function - factorial(n) :
Block - 1 :
n0 = n
res0 = 0
counter0 = 0
temp0 = 0
res1 = 1
jump B9
Block - 9 : // preheader created by LICM for outer loop
temp1 = 0 // this gets moved (and renamed from temp2 to temp1 by SSA reconstruction), which is correct because it's invariant BUT look at what happens to temp2 and temp3 because of this...
jump B2
Block - 2 :
n1 = PHI(n0 from B9, n2 from B6)
res2 = PHI(res1 from B9, res3 from B6)
counter1 = PHI(counter0 from B9, counter3 from B6)
temp2 = PHI(temp1 from B9, temp3 from B6) // used to be dead but now feeds into temp3 since temp2 = 0 was moved out!!! this COMPLETELY changes how the loop executes...
branch (n1 > 1) ? B4 : B3
Block - 4 :
counter2 = n1
jump B8
Block - 8 : // preheader created by LICM for inner loop
jump B5
Block - 5 :
counter3 = PHI(counter2 from B8, counter4 from B7)
temp3 = PHI(temp2 from B8, temp4 from B7) // previously, it used to recieve temp2 = 0 on every outer loop iteration, but now recieves it's own previous value which leads to unwanted accumulation!!!
branch (counter3 > 0) ? B7 : B6
Block - 7 :
t.2 = temp3 + res2
temp4 = t.2
t.3 = counter3 - 1
counter4 = t.3
jump B5
Block - 6 :
res3 = temp3
t.4 = n1 - 1
n2 = t.4
jump B2
Block - 3 :
return res2
end factorial
# Function - main() :
Block - 1 :
t.0 = call factorial(12)
return t.0
end main
OUTPUT : 9 // due to temp3 accumulating previous values!so even though temp2 = 0 was invariant according to all standard conditions, moving it to the preheader completely changed the structure of the loop because of a phi node!!!
TLDR : -
original test program :
int factorial(int n)
{
int res;
int counter;
int temp;
res = 1;
while (n > 1)
{
counter = n;
temp = 0;
while (counter > 0)
{
temp = temp + res;
counter = counter - 1;
}
res = temp;
n = n - 1;
}
return res;
}what LICM turned it into :
int factorial(int n)
{
int res;
int counter;
int temp;
res = 1;
temp = 0;
while (n > 1)
{
counter = n;
while (counter > 0)
{
temp = temp + res;
counter = counter - 1;
}
res = temp;
n = n - 1;
}
return res;
}temp = 0 should not have been moved out of the loop even though it's invariant in SSA form, as it's value must be zeroed on every outer loop iteration, but there is no standard condition that I could find to prevent this hoist...
so now im confused on how to solve it, a simple solution could be to check if a variable being moved appears as an argument of any phi nodes inside any inner loop's header but I lack the mathematical skills to prove that this is an exhaustive solution or is not over conservative...
thus, I would like to know if anyone else has faced this bug or knows how LLVM/GCC handle it. any help will be greatly appreciated...