Fix CPP Backend for GEP API changes for opaque pointer types
authorDavid Blaikie <dblaikie@gmail.com>
Tue, 8 Sep 2015 18:42:29 +0000 (18:42 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Tue, 8 Sep 2015 18:42:29 +0000 (18:42 +0000)
Based on a patch by Jerome Witmann.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247047 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/CppBackend/CPPBackend.cpp
test/CodeGen/CPP/gep.ll [new file with mode: 0644]

index d9d2de4..58b0ee8 100644 (file)
@@ -1356,23 +1356,18 @@ void CppWriter::printInstruction(const Instruction *I,
   }
   case Instruction::GetElementPtr: {
     const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
   }
   case Instruction::GetElementPtr: {
     const GetElementPtrInst* gep = cast<GetElementPtrInst>(I);
-    if (gep->getNumOperands() <= 2) {
-      Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
-          << opNames[0];
-      if (gep->getNumOperands() == 2)
-        Out << ", " << opNames[1];
-    } else {
-      Out << "std::vector<Value*> " << iName << "_indices;";
-      nl(Out);
-      for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
-        Out << iName << "_indices.push_back("
-            << opNames[i] << ");";
-        nl(Out);
+    Out << "GetElementPtrInst* " << iName << " = GetElementPtrInst::Create("
+        << getCppName(gep->getSourceElementType()) << ", " << opNames[0] << ", {";
+    in();
+    for (unsigned i = 1; i < gep->getNumOperands(); ++i ) {
+      if (i != 1) {
+        Out << ", ";
       }
       }
-      Out << "Instruction* " << iName << " = GetElementPtrInst::Create("
-          << opNames[0] << ", " << iName << "_indices";
+      nl(Out);
+      Out << opNames[i];
     }
     }
-    Out << ", \"";
+    out();
+    nl(Out) << "}, \"";
     printEscapedString(gep->getName());
     Out << "\", " << bbname << ");";
     break;
     printEscapedString(gep->getName());
     Out << "\", " << bbname << ");";
     break;
diff --git a/test/CodeGen/CPP/gep.ll b/test/CodeGen/CPP/gep.ll
new file mode 100644 (file)
index 0000000..88a0bf1
--- /dev/null
@@ -0,0 +1,10 @@
+; RUN: llc -march=cpp -o - %s | FileCheck %s
+
+define void @f1(i32* %addr) {
+  %x = getelementptr i32, i32* %addr, i32 1
+; CHECK: ConstantInt* [[INT_1:.*]] = ConstantInt::get(mod->getContext(), APInt(32, StringRef("1"), 10));
+; CHECK: GetElementPtrInst::Create(IntegerType::get(mod->getContext(), 32), ptr_addr,
+; CHECK-NEXT:  [[INT_1]]
+; CHECK-NEXT: }, "x", label_3);
+  ret void
+}