[PATCH] D115858: [llvm-c] Accept GEP operators in some APIs

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 16 00:45:49 PST 2021


nikic created this revision.
nikic added a reviewer: opaque-pointers.
Herald added a reviewer: deadalnix.
Herald added subscribers: dexonsmith, hiraditya.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

As requested in D115787 <https://reviews.llvm.org/D115787>, I've added a test for LLVMConstGEP2/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.


https://reviews.llvm.org/D115858

Files:
  llvm/include/llvm-c/Core.h
  llvm/lib/IR/Core.cpp
  llvm/test/Bindings/llvm-c/echo.ll
  llvm/tools/llvm-c-test/echo.cpp


Index: llvm/tools/llvm-c-test/echo.cpp
===================================================================
--- llvm/tools/llvm-c-test/echo.cpp
+++ 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<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);
Index: llvm/test/Bindings/llvm-c/echo.ll
===================================================================
--- llvm/test/Bindings/llvm-c/echo.ll
+++ llvm/test/Bindings/llvm-c/echo.ll
@@ -23,6 +23,9 @@
 @align = global i32 31, align 4
 @nullptr = global i32* null
 
+ at const_gep = global i32* getelementptr (i32, i32* @var, i64 2)
+ at 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
Index: llvm/lib/IR/Core.cpp
===================================================================
--- llvm/lib/IR/Core.cpp
+++ llvm/lib/IR/Core.cpp
@@ -3024,7 +3024,7 @@
 /*--.. 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 @@
 }
 
 LLVMTypeRef LLVMGetGEPSourceElementType(LLVMValueRef GEP) {
-  return wrap(unwrap<GetElementPtrInst>(GEP)->getSourceElementType());
+  return wrap(unwrap<GEPOperator>(GEP)->getSourceElementType());
 }
 
 /*--.. Operations on phi nodes .............................................--*/
@@ -3060,7 +3060,7 @@
 
 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();
Index: llvm/include/llvm-c/Core.h
===================================================================
--- llvm/include/llvm-c/Core.h
+++ 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);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115858.394775.patch
Type: text/x-patch
Size: 3507 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211216/92cef649/attachment.bin>


More information about the llvm-commits mailing list