Skip to content

Commit 28df685

Browse files
committed
[C API] Add getter/setter for samesign flag on icmp
This was added to the C++ API in llvm#111419 so this change adds accessors in the C API, along with a couple tests.
1 parent 99cdc26 commit 28df685

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

llvm/docs/ReleaseNotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ Changes to the C API
251251
* Added ``LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision`` for creating
252252
debugging metadata of enumerators larger than 64 bits.
253253

254+
* Added ``LLVMGetICmpSameSign`` and ``LLVMSetICmpSameSign`` for the `samesign`
255+
flag on `icmp` instructions.
256+
254257
Changes to the CodeGen infrastructure
255258
-------------------------------------
256259

llvm/include/llvm-c/Core.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,6 +3675,24 @@ LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst);
36753675
*/
36763676
LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst);
36773677

3678+
/**
3679+
* Get whether or not an icmp instruction has the samesign flag.
3680+
*
3681+
* This is only valid for instructions that correspond to llvm::ICmpInst.
3682+
*
3683+
* @see llvm::ICmpInst::hasSameSign()
3684+
*/
3685+
LLVMBool LLVMGetICmpSameSign(LLVMValueRef Inst);
3686+
3687+
/**
3688+
* Set the samesign flag on an icmp instruction.
3689+
*
3690+
* This is only valid for instructions that correspond to llvm::ICmpInst.
3691+
*
3692+
* @see llvm::ICmpInst::setSameSign()
3693+
*/
3694+
void LLVMSetICmpSameSign(LLVMValueRef Inst, LLVMBool SameSign);
3695+
36783696
/**
36793697
* Obtain the float predicate of an instruction.
36803698
*

llvm/lib/IR/Core.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,6 +2951,14 @@ LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
29512951
return (LLVMIntPredicate)0;
29522952
}
29532953

2954+
LLVMBool LLVMGetICmpSameSign(LLVMValueRef Inst) {
2955+
return unwrap<ICmpInst>(Inst)->hasSameSign();
2956+
}
2957+
2958+
void LLVMSetICmpSameSign(LLVMValueRef Inst, LLVMBool SameSign) {
2959+
unwrap<ICmpInst>(Inst)->setSameSign(SameSign);
2960+
}
2961+
29542962
LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
29552963
if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
29562964
return (LLVMRealPredicate)I->getPredicate();

llvm/test/Bindings/llvm-c/echo.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,14 @@ define ptr @test_gep_no_wrap_flags(ptr %0) {
417417
ret ptr %gep.nusw
418418
}
419419

420+
define void @test_icmp_same_sign(i32 %a, i32 %b) {
421+
%icmp.1 = icmp eq i32 %a, %b
422+
%icmp.2 = icmp slt i32 %a, %b
423+
%icmp.3 = icmp samesign eq i32 %a, %b
424+
%icmp.4 = icmp samesign slt i32 %a, %b
425+
ret void
426+
}
427+
420428
!llvm.dbg.cu = !{!0, !2}
421429
!llvm.module.flags = !{!3}
422430

llvm/tools/llvm-c-test/echo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,9 +823,11 @@ struct FunCloner {
823823
}
824824
case LLVMICmp: {
825825
LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
826+
LLVMBool IsSameSign = LLVMGetICmpSameSign(Src);
826827
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
827828
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
828829
Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
830+
LLVMSetICmpSameSign(Dst, IsSameSign);
829831
break;
830832
}
831833
case LLVMPHI: {

0 commit comments

Comments
 (0)