https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109071

qinzhao at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |qinzhao at gcc dot gnu.org

--- Comment #4 from qinzhao at gcc dot gnu.org ---
(In reply to Andrew Pinski from comment #1)
> Jump threading is happening which is causing some code to be duplicated. I
> am 100% sure there is a dup of this bug already filed too.

Yes, the false positive warning is due to the code duplication by jump
threading. 

***without jump threading (adding fno-thread-jumps), the IR in vrp1 is:

  <bb 2> [local count: 1073741824]:
  if (index_3(D) > 3)
    goto <bb 3>; [33.00%]
  else
    goto <bb 4>; [67.00%]

  <bb 3> [local count: 354334800]:
  warn ();

  <bb 4> [local count: 1073741824]:
  *ptr_5(D) = 0;
  _1 = MEM <struct nums> [(int *)sg_2(D)].vals[index_3(D)];
  if (index_3(D) > 3)
    goto <bb 5>; [33.00%]
  else
    goto <bb 6>; [67.00%]

  <bb 5> [local count: 354334800]:
  warn ();

***with jump threading, the "Bad" IR in vrp1 is:

  <bb 2> [local count: 1073741824]:
  if (index_3(D) > 3)
    goto <bb 4>; [33.00%]
  else
    goto <bb 3>; [67.00%]

  <bb 3> [local count: 719407024]:
  *ptr_5(D) = 0;
  _14 = MEM <struct nums> [(int *)sg_2(D)].vals[index_3(D)];
  goto <bb 5>; [100.00%]

  <bb 4> [local count: 354334800]:
  warn ();
  *ptr_5(D) = 0;
  _17 = MEM <struct nums> [(int *)sg_2(D)].vals[index_3(D)];
  warn ();

in the above "Bad" IR with jump threading, we can see the problematic part is:

  <bb 4> [local count: 354334800]:
  warn ();
  *ptr_5(D) = 0;
  _17 = MEM <struct nums> [(int *)sg_2(D)].vals[index_3(D)];
  warn ();

in which the "_17 = MEM <struct nums> [(int *)sg_2(D)].vals[index_3(D)];" is
the one that was duplicated by jump threading and also is the one that caused
array bound checker to report the false positive warning based on the value
range propagation result in block 4: the value range for "index_3" is > 3,
which is out-of-range of the array "Vals", therefore the warning. 

my though is:

1. the jump threading and code duplication are all correct optimization;
2. but the duplication of the array reference caused the false positive
warning;

So, the following is my proposed solution to this problem:

1. when jump threading applies the code duplication, mark those array
references that are duplicated as ARTIFICIAL (or something else..);
2. during array bound checker, check whether the array references are
ARTIFICIAL, if it's true, do not emit the warning.

is the proposed solution feasible?

Reply via email to