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

            Bug ID: 49014
           Summary: IRCE wouldn't work when trip count is unsigned and br
                    condition is equality.
           Product: libraries
           Version: 11.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: Loop Optimizer
          Assignee: unassignedb...@nondot.org
          Reporter: jie.he...@gmail.com
                CC: llvm-bugs@lists.llvm.org

please refer to Bug 49012 for the story.

this time, I made a little change in the previous code. I updated the exit
conditon to "NE" from "ULT", like below:

void testIRCE(unsigned int * buf, unsigned int len, unsigned int
iteration_count) {
    if (iteration_count > 0) {
        unsigned int i = 0;
        do {
            if (i >= len) { // range check
                printf("overflow\n");
                return;
            }

            buf[i] = i;

            i ++;

        } while (i != iteration_count);
    }
}

from C code, we know iteration_count is always an non-negative value (it's an
unsigned int and I add a condition guard outside the loop), and the loop will
exit as expected. but IRCE doesn't work because it thinks iteration_count will
overflow. see the code in function LoopStructure::parseLoopStructure():

if (ICI->isEquality() && !HasNoSignedWrap(IndVarBase)) {
    FailureReason = "LHS in icmp needs nsw for equality predicates";
    return None;
  }

then I skip the check and go to the next. soon,I meet another check when try to
replace NE to ULT, it seems still can't prove iteration_count is non-negative.
see the code in  function LoopStructure::parseLoopStructure():

if (isKnownNonNegativeInLoop(IndVarStart, &L, SE) &&
    isKnownNonNegativeInLoop(RightSCEV, &L, SE))
    Pred = ICmpInst::ICMP_ULT;
else
    Pred = ICmpInst::ICMP_SLT;

the function isKnownNonNegativeInLoop is intended to prove iteration_count SGE
0. from C code, iteration_count is an unsigned int and "> 0", but at LLVM IR
code, it's hard to prove iteration_count SGE 0, LLVM IR doesn't care if it's an
unsigned int. I don't know if there is better way to let the compiler knows
iteration_count is great than 0.

finally, I have to change the type of iteration_count from unsigned to signed,
the optimization IRCE works.

I don't know if there is better idea to let the optimzation

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to