[llvm] r261174 - Add support for memory operations (load/store/gep) in C API echo test

Amaury Sechet via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 17 14:51:03 PST 2016


Author: deadalnix
Date: Wed Feb 17 16:51:03 2016
New Revision: 261174

URL: http://llvm.org/viewvc/llvm-project?rev=261174&view=rev
Log:
Add support for memory operations (load/store/gep) in C API echo test

Summary: As per title.

Reviewers: bogner, chandlerc, echristo, dblaikie, joker.eph, Wallbraker

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D17245

Added:
    llvm/trunk/test/Bindings/llvm-c/memops.ll
Modified:
    llvm/trunk/include/llvm-c/Core.h
    llvm/trunk/lib/IR/Core.cpp
    llvm/trunk/tools/llvm-c-test/echo.cpp

Modified: llvm/trunk/include/llvm-c/Core.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Core.h?rev=261174&r1=261173&r2=261174&view=diff
==============================================================================
--- llvm/trunk/include/llvm-c/Core.h (original)
+++ llvm/trunk/include/llvm-c/Core.h Wed Feb 17 16:51:03 2016
@@ -2561,6 +2561,29 @@ LLVMTypeRef LLVMGetAllocatedType(LLVMVal
  */
 
 /**
+ * @defgroup LLVMCCoreValueInstructionGetElementPointer GEPs
+ *
+ * Functions in this group only apply to instructions that map to
+ * llvm::GetElementPtrInst instances.
+ *
+ * @{
+ */
+
+/**
+ * Check whether the given GEP instruction is inbounds.
+ */
+LLVMBool LLVMIsInBounds(LLVMValueRef GEP);
+
+/**
+ * Set the given GEP instruction to be inbounds or not.
+ */
+void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool b);
+
+/**
+ * @}
+ */
+
+/**
  * @defgroup LLVMCCoreValueInstructionPHINode PHI Nodes
  *
  * Functions in this group only apply to instructions that map to
@@ -2606,6 +2629,7 @@ LLVMBasicBlockRef LLVMGetIncomingBlock(L
 
 /**
  * Obtain the number of indices.
+ * NB: This also works on GEP.
  */
 unsigned LLVMGetNumIndices(LLVMValueRef Inst);
 

Modified: llvm/trunk/lib/IR/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Core.cpp?rev=261174&r1=261173&r2=261174&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Core.cpp (original)
+++ llvm/trunk/lib/IR/Core.cpp Wed Feb 17 16:51:03 2016
@@ -2147,6 +2147,16 @@ LLVMTypeRef LLVMGetAllocatedType(LLVMVal
   return wrap(unwrap<AllocaInst>(Alloca)->getAllocatedType());
 }
 
+/*--.. Operations on gep instructions (only) ...............................--*/
+
+LLVMBool LLVMIsInBounds(LLVMValueRef GEP) {
+  return unwrap<GetElementPtrInst>(GEP)->isInBounds();
+}
+
+void LLVMSetIsInBounds(LLVMValueRef GEP, LLVMBool b) {
+  return unwrap<GetElementPtrInst>(GEP)->setIsInBounds(b);
+}
+
 /*--.. Operations on phi nodes .............................................--*/
 
 void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
@@ -2172,6 +2182,8 @@ LLVMBasicBlockRef LLVMGetIncomingBlock(L
 
 unsigned LLVMGetNumIndices(LLVMValueRef Inst) {
   auto *I = unwrap(Inst);
+  if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
+    return GEP->getNumIndices();
   if (auto *EV = dyn_cast<ExtractValueInst>(I))
     return EV->getNumIndices();
   if (auto *IV = dyn_cast<InsertValueInst>(I))

Added: llvm/trunk/test/Bindings/llvm-c/memops.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bindings/llvm-c/memops.ll?rev=261174&view=auto
==============================================================================
--- llvm/trunk/test/Bindings/llvm-c/memops.ll (added)
+++ llvm/trunk/test/Bindings/llvm-c/memops.ll Wed Feb 17 16:51:03 2016
@@ -0,0 +1,37 @@
+; RUN: llvm-as < %s | llvm-dis > %t.orig
+; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo
+; RUN: diff -w %t.orig %t.echo
+
+%S = type { i32, i32 }
+
+define i32 @method(%S* %this, i32 %arg.a, i32 %arg.b) {
+  %a = alloca i32
+  store i32 %arg.a, i32* %a, align 4
+  %b = alloca i32
+  store i32 %arg.b, i32* %b
+  %1 = load i32, i32* %a, align 4
+  %2 = load i32, i32* %b, align 4
+  %3 = add i32 %1, %2
+  %4 = getelementptr inbounds %S, %S* %this, i32 0, i32 0
+  %5 = load i32, i32* %4, align 4
+  %6 = add i32 %3, %5
+  %7 = getelementptr inbounds %S, %S* %this, i32 0, i32 1
+  %8 = load i32, i32* %7, align 4
+  %9 = add i32 %6, %8
+  ret i32 %9
+}
+
+define i32 @main() {
+  %s = alloca %S
+  store %S zeroinitializer, %S* %s
+  %1 = getelementptr inbounds %S, %S* %s, i32 0, i32 0
+  %2 = getelementptr inbounds %S, %S* %s, i32 0, i32 1
+  store i32 1, i32* %2
+  store i32 1, i32* %1
+  %3 = insertvalue { %S*, i32 (%S*, i32, i32)* } undef, %S* %s, 0
+  %4 = insertvalue { %S*, i32 (%S*, i32, i32)* } %3, i32 (%S*, i32, i32)* @method, 1
+  %5 = extractvalue { %S*, i32 (%S*, i32, i32)* } %4, 0
+  %6 = extractvalue { %S*, i32 (%S*, i32, i32)* } %4, 1
+  %7 = call i32 %6(%S* %5, i32 38, i32 2)
+  ret i32 %7
+}

Modified: llvm/trunk/tools/llvm-c-test/echo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/echo.cpp?rev=261174&r1=261173&r2=261174&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/echo.cpp (original)
+++ llvm/trunk/tools/llvm-c-test/echo.cpp Wed Feb 17 16:51:03 2016
@@ -464,6 +464,31 @@ struct FunCloner {
         Dst = LLVMBuildAlloca(Builder, Ty, Name);
         break;
       }
+      case LLVMLoad: {
+        LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
+        Dst = LLVMBuildLoad(Builder, Ptr, Name);
+        LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
+        break;
+      }
+      case LLVMStore: {
+        LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
+        LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 1));
+        Dst = LLVMBuildStore(Builder, Val, Ptr);
+        LLVMSetAlignment(Dst, LLVMGetAlignment(Src));
+        break;
+      }
+      case LLVMGetElementPtr: {
+        LLVMValueRef Ptr = CloneValue(LLVMGetOperand(Src, 0));
+        SmallVector<LLVMValueRef, 8> Idx;
+        int NumIdx = LLVMGetNumIndices(Src);
+        for (int i = 1; i <= NumIdx; i++)
+          Idx.push_back(CloneValue(LLVMGetOperand(Src, i)));
+        if (LLVMIsInBounds(Src))
+          Dst = LLVMBuildInBoundsGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
+        else
+          Dst = LLVMBuildGEP(Builder, Ptr, Idx.data(), NumIdx, Name);
+        break;
+      }
       case LLVMICmp: {
         LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
         LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));




More information about the llvm-commits mailing list