[LLVMdev] "icmp sgt" when it should be "ugt" ?

Jonas Gefele llvm.org at schrieb.de
Mon Aug 1 09:11:57 PDT 2011


Hello,

while writing a new LLVM backend I have observed that in some cases the
optimizer produces an "icmp sgt i32 %a, 0" where I would have expected an
"icmp ugt i32 %a, 0".

For example when I feed "opt -O3 -S ..."  (LLVM 2.9, Windows) with


------------------------------------------------------------------------
target datalayout = "E-p:32:32:32"

define void @foo(i8* %buf, i32 %bufLen, i32* %ret) nounwind {
entry:
  %add.ptr = getelementptr inbounds i8* %buf, i32 %bufLen
  %cmp = icmp ult i8* %buf, %add.ptr
  br i1 %cmp, label %if.then, label %if.end

if.then:                                          ; preds = %entry
  store i32 0, i32* %ret
  br label %if.end

if.end:                                           ; preds = %if.then, %entry
  ret void
}
------------------------------------------------------------------------


I get as output


------------------------------------------------------------------------
...
entry:
  %cmp = icmp sgt i32 %bufLen, 0
  br i1 %cmp, label %if.then, label %if.end
...
------------------------------------------------------------------------


This is no problem if one thinks of "icmp sgt %a, 0" as "compute %a - 0
and set flags accordingly" because then signedness makes no difference.
But if I understand things correctly, it is a problem according to the
LLVM IR reference which states "sgt: interprets the operands as signed
values and yields true if op1 is greater than op2". Under this definition
and for %a=0xFFFFFFFF "ugt" would return true while "sgt" would return
false, wouldn't it?

Is this a bug in my thinking, a bug in the reference manual, or is this a
bug in some pass which introduces the "sgt"?


Cheers,
Jonas



More information about the llvm-dev mailing list