aboutsummaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-c-test
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2022-06-23 11:44:20 +0200
committerNikita Popov <npopov@redhat.com>2022-06-23 14:50:54 +0200
commitda34966a5a5fea47e3381707f26b3c24a6973569 (patch)
tree47da9df5196d1183b7172b92b70a1ce39875e93d /llvm/tools/llvm-c-test
parent0eb17a9d8672c3503c76a808b0773235b042f5a9 (diff)
downloadllvm-da34966a5a5fea47e3381707f26b3c24a6973569.zip
llvm-da34966a5a5fea47e3381707f26b3c24a6973569.tar.gz
llvm-da34966a5a5fea47e3381707f26b3c24a6973569.tar.bz2
[llvm-c] Add LLVMGetAggregateElement() function
This adds LLVMGetAggregateElement() as a wrapper for Constant::getAggregateElement(), which allows fetching a struct/array/vector element without handling different possible underlying representations. As the changed echo test shows, previously you for example had to treat ConstantArray (use LLVMGetOperand) and ConstantDataArray (use LLVMGetElementAsConstant) separately, not to mention all the other possible representations (like PoisonValue). I've deprecated LLVMGetElementAsConstant() in favor of the new function, which is strictly more powerful (but I could be convinced to drop the deprecation). This is partly motivated by https://reviews.llvm.org/D125795, which drops LLVMConstExtractValue() because the underlying constant expression no longer exists. This function could previously be used as a poor man's getAggregateElement(). Differential Revision: https://reviews.llvm.org/D128417
Diffstat (limited to 'llvm/tools/llvm-c-test')
-rw-r--r--llvm/tools/llvm-c-test/echo.cpp42
1 files changed, 12 insertions, 30 deletions
diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp
index 2323132..b7b7730 100644
--- a/llvm/tools/llvm-c-test/echo.cpp
+++ b/llvm/tools/llvm-c-test/echo.cpp
@@ -301,25 +301,16 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
return LLVMConstNull(TypeCloner(M).Clone(Cst));
}
- // Try constant array
- if (LLVMIsAConstantArray(Cst)) {
- check_value_kind(Cst, LLVMConstantArrayValueKind);
+ // Try constant array or constant data array
+ if (LLVMIsAConstantArray(Cst) || LLVMIsAConstantDataArray(Cst)) {
+ check_value_kind(Cst, LLVMIsAConstantArray(Cst)
+ ? LLVMConstantArrayValueKind
+ : LLVMConstantDataArrayValueKind);
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
unsigned EltCount = LLVMGetArrayLength(Ty);
SmallVector<LLVMValueRef, 8> Elts;
for (unsigned i = 0; i < EltCount; i++)
- Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
- return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
- }
-
- // Try constant data array
- if (LLVMIsAConstantDataArray(Cst)) {
- check_value_kind(Cst, LLVMConstantDataArrayValueKind);
- LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
- unsigned EltCount = LLVMGetArrayLength(Ty);
- SmallVector<LLVMValueRef, 8> Elts;
- for (unsigned i = 0; i < EltCount; i++)
- Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
+ Elts.push_back(clone_constant(LLVMGetAggregateElement(Cst, i), M));
return LLVMConstArray(LLVMGetElementType(Ty), Elts.data(), EltCount);
}
@@ -369,25 +360,16 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
report_fatal_error("ConstantFP is not supported");
}
- // Try ConstantVector
- if (LLVMIsAConstantVector(Cst)) {
- check_value_kind(Cst, LLVMConstantVectorValueKind);
- LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
- unsigned EltCount = LLVMGetVectorSize(Ty);
- SmallVector<LLVMValueRef, 8> Elts;
- for (unsigned i = 0; i < EltCount; i++)
- Elts.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
- return LLVMConstVector(Elts.data(), EltCount);
- }
-
- // Try ConstantDataVector
- if (LLVMIsAConstantDataVector(Cst)) {
- check_value_kind(Cst, LLVMConstantDataVectorValueKind);
+ // Try ConstantVector or ConstantDataVector
+ if (LLVMIsAConstantVector(Cst) || LLVMIsAConstantDataVector(Cst)) {
+ check_value_kind(Cst, LLVMIsAConstantVector(Cst)
+ ? LLVMConstantVectorValueKind
+ : LLVMConstantDataVectorValueKind);
LLVMTypeRef Ty = TypeCloner(M).Clone(Cst);
unsigned EltCount = LLVMGetVectorSize(Ty);
SmallVector<LLVMValueRef, 8> Elts;
for (unsigned i = 0; i < EltCount; i++)
- Elts.push_back(clone_constant(LLVMGetElementAsConstant(Cst, i), M));
+ Elts.push_back(clone_constant(LLVMGetAggregateElement(Cst, i), M));
return LLVMConstVector(Elts.data(), EltCount);
}