[llvm-bugs] [Bug 51360] New: SimplifyCFGPass hoists unsafe instruction and caused undefined behaviour

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Aug 5 08:31:26 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51360

            Bug ID: 51360
           Summary: SimplifyCFGPass hoists unsafe instruction and caused
                    undefined behaviour
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: C
          Assignee: unassignedclangbugs at nondot.org
          Reporter: Zhi.Zhuang at mediatek.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Reduced test case:
#include <stdint.h>
int32_t foo(int32_t a, int32_t b) {
  return (a>0 && b>0 && a>INT32_MAX-b) || (a<0 && b<0 && a<INT32_MAX-b) ? 0 :
(a+b);
}
and using sha <2e75986a21e5>, command is "clang -O3 -S -mllvm -print-after-all
test.c"


Before second SimplifyCFGPass, the IR was like follows:
2:
  %3 = icmp sgt i32 %0, 0
  %4 = icmp sgt i32 %1, 0
  %5 = select i1 %3, i1 %4, i1 false
  br i1 %5, label %6, label %9

6:                                                ; preds = %2
  %7 = sub nsw i32 2147483647, %1
The sub nsw will only be executed when %1 was positive since that was the
condition when %6 was branched, so sub will not overflow and nsw keyword makes
sense.


But after second SimplifyCFGPass, that sub was hoisted to first block:
2:
  %3 = icmp sgt i32 %0, 0
  %4 = icmp sgt i32 %1, 0
  %5 = select i1 %3, i1 %4, i1 false
  %6 = sub nsw i32 2147483647, %1
  %7 = icmp slt i32 %6, %0
  %8 = select i1 %5, i1 %7, i1 false
  br i1 %8, label %18, label %9
This %6=sub nsw does not makes sense anymore since %1 maybe negative, and this
block branching using the result of %6 and may cause undefined behavior

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210805/001a1d4c/attachment.html>


More information about the llvm-bugs mailing list