diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -3504,7 +3504,7 @@ */ /** - * Check whether the given GEP instruction is inbounds. + * Check whether the given GEP operator is inbounds. */ LLVMBool LLVMIsInBounds(LLVMValueRef GEP); @@ -3514,7 +3514,7 @@ 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 @@ /** * Obtain the number of indices. - * NB: This also works on GEP. + * NB: This also works on GEP operators. */ unsigned LLVMGetNumIndices(LLVMValueRef Inst); diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -3024,7 +3024,7 @@ /*--.. Operations on gep instructions (only) ...............................--*/ LLVMBool LLVMIsInBounds(LLVMValueRef GEP) { - return unwrap(GEP)->isInBounds(); + return unwrap(GEP)->isInBounds(); } void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool InBounds) { @@ -3032,7 +3032,7 @@ } LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) { - return wrap(unwrap(GEP)->getSourceElementType()); + return wrap(unwrap(GEP)->getSourceElementType()); } /*--.. Operations on phi nodes .............................................--*/ @@ -3060,7 +3060,7 @@ unsigned LLVMGetNumIndices(LLVMValueRef Inst) { auto *I = unwrap(Inst); - if (auto *GEP = dyn_cast(I)) + if (auto *GEP = dyn_cast(I)) return GEP->getNumIndices(); if (auto *EV = dyn_cast(I)) return EV->getNumIndices(); diff --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll --- a/llvm/test/Bindings/llvm-c/echo.ll +++ b/llvm/test/Bindings/llvm-c/echo.ll @@ -23,6 +23,9 @@ @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 diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -402,6 +402,19 @@ 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 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);