Skip to content

Commit

Permalink
[C API] Add support for setting/getting new nneg flag on zext instruc…
Browse files Browse the repository at this point in the history
…tions (#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.
  • Loading branch information
Benjins committed Nov 29, 2023
1 parent 8aeaceb commit fad77dc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ Changes to the C API
The option structure exposes an additional setting (i.e., the target ABI) and
provides default values for unspecified settings.

* Added ``LLVMGetNNeg`` and ``LLVMSetNNeg`` for setting/getting the new nneg flag
on zext instructions

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

Expand Down
11 changes: 11 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -3974,6 +3974,17 @@ void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW);
LLVMBool LLVMGetExact(LLVMValueRef DivOrShrInst);
void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact);

/**
* Gets if the instruction has the non-negative flag set
* Only valid for zext instructions
*/
LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst);
/**
* Sets the non-negative flag for the instruction
* Only valid for zext instructions
*/
void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg);

/* Memory */
LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef, LLVMTypeRef Ty,
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3454,6 +3454,16 @@ void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
cast<Instruction>(P)->setIsExact(IsExact);
}

LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) {
Value *P = unwrap<Value>(NonNegInst);
return cast<Instruction>(P)->hasNonNeg();
}

void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {
Value *P = unwrap<Value>(NonNegInst);
cast<Instruction>(P)->setNonNeg(IsNonNeg);
}

/*--.. Memory ..............................................................--*/

LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/Bindings/llvm-c/echo.ll
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ define i32 @iops(i32 %a, i32 %b) {
%21 = sdiv exact i32 %20, %2
%22 = lshr exact i32 %21, %4
%23 = ashr exact i32 %22, %14
%24 = zext i32 %23 to i64
%25 = zext nneg i32 %23 to i64
ret i32 %23
}

Expand Down
8 changes: 8 additions & 0 deletions llvm/tools/llvm-c-test/echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,14 @@ struct FunCloner {
Dst = LLVMBuildFence(Builder, Ordering, IsSingleThreaded, Name);
break;
}
case LLVMZExt: {
LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
LLVMTypeRef DestTy = CloneType(LLVMTypeOf(Src));
LLVMBool NNeg = LLVMGetNNeg(Src);
Dst = LLVMBuildZExt(Builder, Val, DestTy, Name);
LLVMSetNNeg(Dst, NNeg);
break;
}
default:
break;
}
Expand Down

0 comments on commit fad77dc

Please sign in to comment.