Skip to content

Commit fad77dc

Browse files
authored
[C API] Add support for setting/getting new nneg flag on zext instructions (#73592)
This flag was added in #67982, but was not yet accessible via the C API. This commit adds a getter/setter for this flag, and a test for it.
1 parent 8aeaceb commit fad77dc

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

llvm/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ Changes to the C API
199199
The option structure exposes an additional setting (i.e., the target ABI) and
200200
provides default values for unspecified settings.
201201

202+
* Added ``LLVMGetNNeg`` and ``LLVMSetNNeg`` for setting/getting the new nneg flag
203+
on zext instructions
204+
202205
Changes to the CodeGen infrastructure
203206
-------------------------------------
204207

llvm/include/llvm-c/Core.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3974,6 +3974,17 @@ void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW);
39743974
LLVMBool LLVMGetExact(LLVMValueRef DivOrShrInst);
39753975
void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact);
39763976

3977+
/**
3978+
* Gets if the instruction has the non-negative flag set
3979+
* Only valid for zext instructions
3980+
*/
3981+
LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst);
3982+
/**
3983+
* Sets the non-negative flag for the instruction
3984+
* Only valid for zext instructions
3985+
*/
3986+
void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg);
3987+
39773988
/* Memory */
39783989
LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
39793990
LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef, LLVMTypeRef Ty,

llvm/lib/IR/Core.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3454,6 +3454,16 @@ void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
34543454
cast<Instruction>(P)->setIsExact(IsExact);
34553455
}
34563456

3457+
LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) {
3458+
Value *P = unwrap<Value>(NonNegInst);
3459+
return cast<Instruction>(P)->hasNonNeg();
3460+
}
3461+
3462+
void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {
3463+
Value *P = unwrap<Value>(NonNegInst);
3464+
cast<Instruction>(P)->setNonNeg(IsNonNeg);
3465+
}
3466+
34573467
/*--.. Memory ..............................................................--*/
34583468

34593469
LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ define i32 @iops(i32 %a, i32 %b) {
9090
%21 = sdiv exact i32 %20, %2
9191
%22 = lshr exact i32 %21, %4
9292
%23 = ashr exact i32 %22, %14
93+
%24 = zext i32 %23 to i64
94+
%25 = zext nneg i32 %23 to i64
9395
ret i32 %23
9496
}
9597

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,14 @@ struct FunCloner {
899899
Dst = LLVMBuildFence(Builder, Ordering, IsSingleThreaded, Name);
900900
break;
901901
}
902+
case LLVMZExt: {
903+
LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
904+
LLVMTypeRef DestTy = CloneType(LLVMTypeOf(Src));
905+
LLVMBool NNeg = LLVMGetNNeg(Src);
906+
Dst = LLVMBuildZExt(Builder, Val, DestTy, Name);
907+
LLVMSetNNeg(Dst, NNeg);
908+
break;
909+
}
902910
default:
903911
break;
904912
}

0 commit comments

Comments
 (0)