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

This was added to the C++ API in
https://github.com/llvm/llvm-project/pull/111419 so this change adds
accessors in the C API, along with a couple tests.
This commit is contained in:
Benji Smith
2025-06-22 18:05:17 -04:00
committed by GitHub
parent 40eee8ec7f
commit 8f01edfa11
5 changed files with 39 additions and 0 deletions

View File

@@ -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
-------------------------------------

View File

@@ -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.
*

View File

@@ -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();

View File

@@ -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}

View File

@@ -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: {