LegUp Miscompilation

The following C code was found to produce the wrong result in LegUp 4.0 and 7.5.

./bug.c

volatile int a = 0;
int b = 1;

int main() {
  int d = 1;
  if (d + a)
    b || 1;
  else
    b = 0;
  return b;
}

To reproduce the bug, it has to point to the main Makefile in the examples folder in LegUp (LEGUP_EXAMPLES).

./Makefile

NAME := bug
NO_OPT := 1
LEGUP_EXAMPLES := ..
include $(LEGUP_EXAMPLES)/Makefile.common

Then the bug can be reproduced by running make, which also runs vsim to simulate the produced Verilog.

make default v | tail -n6
# At t=              110000 clk=1 finish=1 return_val=         0
# Cycles:                    3
# ** Note: $finish    : loop.v(1015)
#    Time: 110 ns  Iteration: 2  Instance: /main_tb
# End time: 16:08:12 on Oct 02,2020, Elapsed time: 0:00:01
# Errors: 0, Warnings: 8

This shows that LegUp returns 0, however, the actual result that should be returned is 1, which can be verified with GCC.

gcc ./bug.c -o bug
./bug
echo $?
1