Skip to content

[C API] Add getter/setter for samesign flag on icmp #145247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ Changes to the C API
* Added ``LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision`` for creating
debugging metadata of enumerators larger than 64 bits.

* Added ``LLVMGetICmpSameSign`` and ``LLVMSetICmpSameSign`` for the `samesign`
flag on `icmp` instructions.

Changes to the CodeGen infrastructure
-------------------------------------

Expand Down
18 changes: 18 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -3675,6 +3675,24 @@ LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst);
*/
LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst);

/**
* Get whether or not an icmp instruction has the samesign flag.
*
* This is only valid for instructions that correspond to llvm::ICmpInst.
*
* @see llvm::ICmpInst::hasSameSign()
*/
LLVMBool LLVMGetICmpSameSign(LLVMValueRef Inst);

/**
* Set the samesign flag on an icmp instruction.
*
* This is only valid for instructions that correspond to llvm::ICmpInst.
*
* @see llvm::ICmpInst::setSameSign()
*/
void LLVMSetICmpSameSign(LLVMValueRef Inst, LLVMBool SameSign);

/**
* Obtain the float predicate of an instruction.
*
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2951,6 +2951,14 @@ LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
return (LLVMIntPredicate)0;
}

LLVMBool LLVMGetICmpSameSign(LLVMValueRef Inst) {
return unwrap<ICmpInst>(Inst)->hasSameSign();
}

void LLVMSetICmpSameSign(LLVMValueRef Inst, LLVMBool SameSign) {
unwrap<ICmpInst>(Inst)->setSameSign(SameSign);
}

LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
return (LLVMRealPredicate)I->getPredicate();
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/Bindings/llvm-c/echo.ll
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,14 @@ define ptr @test_gep_no_wrap_flags(ptr %0) {
ret ptr %gep.nusw
}

define void @test_icmp_same_sign(i32 %a, i32 %b) {
%icmp.1 = icmp eq i32 %a, %b
%icmp.2 = icmp slt i32 %a, %b
%icmp.3 = icmp samesign eq i32 %a, %b
%icmp.4 = icmp samesign slt i32 %a, %b
ret void
}

!llvm.dbg.cu = !{!0, !2}
!llvm.module.flags = !{!3}

Expand Down
2 changes: 2 additions & 0 deletions llvm/tools/llvm-c-test/echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,9 +823,11 @@ struct FunCloner {
}
case LLVMICmp: {
LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
LLVMBool IsSameSign = LLVMGetICmpSameSign(Src);
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
LLVMSetICmpSameSign(Dst, IsSameSign);
break;
}
case LLVMPHI: {
Expand Down
Loading