[llvm-c] Accept GEP operators in some APIs

As requested in D115787, I've added a test for LLVMConstGEP2 and
LLVMConstInBoundsGEP2. However, to make this work in the echo test,
I also had to change a couple of APIs to work on GEP operators,
rather than only GEP instructions.

Differential Revision: https://reviews.llvm.org/D115858
This commit is contained in:
Nikita Popov 2021-12-16 09:36:42 +01:00
parent aa27bab5a1
commit 65777addbd
4 changed files with 22 additions and 6 deletions

View File

@ -3504,7 +3504,7 @@ LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca);
*/
/**
* Check whether the given GEP instruction is inbounds.
* Check whether the given GEP operator is inbounds.
*/
LLVMBool LLVMIsInBounds(LLVMValueRef GEP);
@ -3514,7 +3514,7 @@ LLVMBool LLVMIsInBounds(LLVMValueRef GEP);
void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds);
/**
* Get the source element type of the given GEP instruction.
* Get the source element type of the given GEP operator.
*/
LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP);
@ -3568,7 +3568,7 @@ LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index);
/**
* Obtain the number of indices.
* NB: This also works on GEP.
* NB: This also works on GEP operators.
*/
unsigned LLVMGetNumIndices(LLVMValueRef Inst);

View File

@ -3024,7 +3024,7 @@ LLVMTypeRef LLVMGetAllocatedType(LLVMValueRef Alloca) {
/*--.. Operations on gep instructions (only) ...............................--*/
LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
return unwrap<GetElementPtrInst>(GEP)->isInBounds();
return unwrap<GEPOperator>(GEP)->isInBounds();
}
void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
@ -3032,7 +3032,7 @@ void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) {
}
LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) {
return wrap(unwrap<GetElementPtrInst>(GEP)->getSourceElementType());
return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
}
/*--.. Operations on phi nodes .............................................--*/
@ -3060,7 +3060,7 @@ LLVMBasicBlockRef LLVMGetIncomingBlock(LLVMValueRef PhiNode, unsigned Index) {
unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
auto *I = unwrap(Inst);
if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
if (auto *GEP = dyn_cast<GEPOperator>(I))
return GEP->getNumIndices();
if (auto *EV = dyn_cast<ExtractValueInst>(I))
return EV->getNumIndices();

View File

@ -23,6 +23,9 @@ module asm "classical GAS"
@align = global i32 31, align 4
@nullptr = global i32* null
@const_gep = global i32* getelementptr (i32, i32* @var, i64 2)
@const_inbounds_gep = global i32* getelementptr inbounds (i32, i32* @var, i64 1)
@aliased1 = alias i32, i32* @var
@aliased2 = internal alias i32, i32* @var
@aliased3 = external alias i32, i32* @var

View File

@ -402,6 +402,19 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
case LLVMBitCast:
return LLVMConstBitCast(clone_constant(LLVMGetOperand(Cst, 0), M),
TypeCloner(M).Clone(Cst));
case LLVMGetElementPtr: {
LLVMTypeRef ElemTy =
TypeCloner(M).Clone(LLVMGetGEPSourceElementType(Cst));
LLVMValueRef Ptr = clone_constant(LLVMGetOperand(Cst, 0), M);
int NumIdx = LLVMGetNumIndices(Cst);
SmallVector<LLVMValueRef, 8> Idx;
for (int i = 1; i <= NumIdx; i++)
Idx.push_back(clone_constant(LLVMGetOperand(Cst, i), M));
if (LLVMIsInBounds(Cst))
return LLVMConstInBoundsGEP2(ElemTy, Ptr, Idx.data(), NumIdx);
else
return LLVMConstGEP2(ElemTy, Ptr, Idx.data(), NumIdx);
}
default:
fprintf(stderr, "%d is not a supported opcode for constant expressions\n",
Op);