[Refactor] Expose the runtime debug builder
authorJohannes Doerfert <jdoerfert@codeaurora.org>
Thu, 24 Jul 2014 23:55:19 +0000 (23:55 +0000)
committerJohannes Doerfert <jdoerfert@codeaurora.org>
Thu, 24 Jul 2014 23:55:19 +0000 (23:55 +0000)
llvm-svn: 213908

polly/include/polly/CodeGen/RuntimeDebugBuilder.h [new file with mode: 0644]
polly/lib/CodeGen/IslCodeGeneration.cpp
polly/lib/CodeGen/RuntimeDebugBuilder.cpp [new file with mode: 0644]

diff --git a/polly/include/polly/CodeGen/RuntimeDebugBuilder.h b/polly/include/polly/CodeGen/RuntimeDebugBuilder.h
new file mode 100644 (file)
index 0000000..5d8d4c6
--- /dev/null
@@ -0,0 +1,59 @@
+//===--- RuntimeDebugBuilder.h --- Helper to insert prints into LLVM-IR ---===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef RUNTIME_DEBUG_BUILDER_H
+#define RUNTIME_DEBUG_BUILDER_H
+
+#include "polly/CodeGen/IRBuilder.h"
+
+#include <string>
+
+namespace llvm {
+class Value;
+class Function;
+}
+
+namespace polly {
+
+/// @brief Insert function calls that print certain LLVM values at run time.
+///
+/// This class inserts libc function calls to print certain LLVM values at
+/// run time.
+struct RuntimeDebugBuilder {
+
+  /// @brief Print a string to stdout.
+  ///
+  /// @param String The string to print.
+  static void createStrPrinter(PollyIRBuilder &Builder,
+                               const std::string &String);
+
+  /// @brief Print a value to stdout.
+  ///
+  /// @param V The value to print.
+  ///
+  /// @note Only integer, floating point and pointer values up to 64bit are
+  ///       supported.
+  static void createValuePrinter(PollyIRBuilder &Builder, llvm::Value *V);
+
+  /// @brief Add a call to the fflush function with no file pointer given.
+  ///
+  /// This call will flush all opened file pointers including stdout and stderr.
+  static void createFlush(PollyIRBuilder &Builder);
+
+  /// @brief Get a reference to the 'printf' function.
+  ///
+  /// If the current module does not yet contain a reference to printf, we
+  /// insert a reference to it. Otherwise the existing reference is returned.
+  static llvm::Function *getPrintF(PollyIRBuilder &Builder);
+};
+}
+
+#endif
index fd038b9..59e8476 100644 (file)
@@ -54,89 +54,6 @@ using namespace llvm;
 
 #define DEBUG_TYPE "polly-codegen-isl"
 
-/// @brief Insert function calls that print certain LLVM values at run time.
-///
-/// This class inserts libc function calls to print certain LLVM values at
-/// run time.
-class RuntimeDebugBuilder {
-public:
-  RuntimeDebugBuilder(PollyIRBuilder &Builder) : Builder(Builder) {}
-
-  /// @brief Print a string to stdout.
-  ///
-  /// @param String The string to print.
-  void createStrPrinter(std::string String);
-
-  /// @brief Print an integer value to stdout.
-  ///
-  /// @param V The value to print.
-  void createIntPrinter(Value *V);
-
-private:
-  PollyIRBuilder &Builder;
-
-  /// @brief Add a call to the fflush function with no file pointer given.
-  ///
-  /// This call will flush all opened file pointers including stdout and stderr.
-  void createFlush();
-
-  /// @brief Get a reference to the 'printf' function.
-  ///
-  /// If the current module does not yet contain a reference to printf, we
-  /// insert a reference to it. Otherwise the existing reference is returned.
-  Function *getPrintF();
-};
-
-Function *RuntimeDebugBuilder::getPrintF() {
-  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
-  const char *Name = "printf";
-  Function *F = M->getFunction(Name);
-
-  if (!F) {
-    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
-    FunctionType *Ty =
-        FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), true);
-    F = Function::Create(Ty, Linkage, Name, M);
-  }
-
-  return F;
-}
-
-void RuntimeDebugBuilder::createFlush() {
-  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
-  const char *Name = "fflush";
-  Function *F = M->getFunction(Name);
-
-  if (!F) {
-    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
-    FunctionType *Ty =
-        FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), false);
-    F = Function::Create(Ty, Linkage, Name, M);
-  }
-
-  Builder.CreateCall(F, Constant::getNullValue(Builder.getInt8PtrTy()));
-}
-
-void RuntimeDebugBuilder::createStrPrinter(std::string String) {
-  Function *F = getPrintF();
-  Value *StringValue = Builder.CreateGlobalStringPtr(String);
-  Builder.CreateCall(F, StringValue);
-
-  createFlush();
-}
-
-void RuntimeDebugBuilder::createIntPrinter(Value *V) {
-  IntegerType *Ty = dyn_cast<IntegerType>(V->getType());
-  (void)Ty;
-  assert(Ty && Ty->getBitWidth() == 64 &&
-         "Cannot insert printer for this type.");
-
-  Function *F = getPrintF();
-  Value *String = Builder.CreateGlobalStringPtr("%ld");
-  Builder.CreateCall2(F, String, V);
-  createFlush();
-}
-
 /// @brief LLVM-IR generator for isl_ast_expr[essions]
 ///
 /// This generator generates LLVM-IR that performs the computation described by
diff --git a/polly/lib/CodeGen/RuntimeDebugBuilder.cpp b/polly/lib/CodeGen/RuntimeDebugBuilder.cpp
new file mode 100644 (file)
index 0000000..f539d5e
--- /dev/null
@@ -0,0 +1,74 @@
+//===--- RuntimeDebugBuilder.cpp - Helper to insert prints into LLVM-IR ---===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//===----------------------------------------------------------------------===//
+
+#include "polly/CodeGen/RuntimeDebugBuilder.h"
+
+#include "llvm/IR/Module.h"
+
+using namespace llvm;
+using namespace polly;
+
+Function *RuntimeDebugBuilder::getPrintF(PollyIRBuilder &Builder) {
+  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
+  const char *Name = "printf";
+  Function *F = M->getFunction(Name);
+
+  if (!F) {
+    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
+    FunctionType *Ty =
+        FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), true);
+    F = Function::Create(Ty, Linkage, Name, M);
+  }
+
+  return F;
+}
+
+void RuntimeDebugBuilder::createFlush(PollyIRBuilder &Builder) {
+  Module *M = Builder.GetInsertBlock()->getParent()->getParent();
+  const char *Name = "fflush";
+  Function *F = M->getFunction(Name);
+
+  if (!F) {
+    GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
+    FunctionType *Ty =
+        FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), false);
+    F = Function::Create(Ty, Linkage, Name, M);
+  }
+
+  Builder.CreateCall(F, Constant::getNullValue(Builder.getInt8PtrTy()));
+}
+
+void RuntimeDebugBuilder::createStrPrinter(PollyIRBuilder &Builder,
+                                           const std::string &String) {
+  Value *StringValue = Builder.CreateGlobalStringPtr(String);
+  Builder.CreateCall(getPrintF(Builder), StringValue);
+
+  createFlush(Builder);
+}
+
+void RuntimeDebugBuilder::createValuePrinter(PollyIRBuilder &Builder,
+                                             Value *V) {
+  const char *Format = nullptr;
+
+  Type *Ty = V->getType();
+  if (Ty->isIntegerTy())
+    Format = "%ld";
+  else if (Ty->isFloatingPointTy())
+    Format = "%lf";
+  else if (Ty->isPointerTy())
+    Format = "%p";
+
+  assert(Format && Ty->getPrimitiveSizeInBits() <= 64 && "Bad type to print.");
+
+  Value *FormatString = Builder.CreateGlobalStringPtr(Format);
+  Builder.CreateCall2(getPrintF(Builder), FormatString, V);
+  createFlush(Builder);
+}