From 6f9775925b370b2eba49312f2573de16895f376d Mon Sep 17 00:00:00 2001 From: Sunil Singh Date: Mon, 9 Sep 2019 23:24:22 +0530 Subject: [PATCH 1/3] A directory for Data Flow Analysis is added in lib/ and include/. A sample class is added just to check how to add any functionality in llvm. --- llvm/include/llvm/DataFlowAnalysis/Hello.h | 21 +++++++++++++++++++++ llvm/lib/CMakeLists.txt | 1 + llvm/lib/DataFlowAnalysis/CMakeLists.txt | 9 +++++++++ llvm/lib/DataFlowAnalysis/Hello.cpp | 13 +++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 llvm/include/llvm/DataFlowAnalysis/Hello.h create mode 100644 llvm/lib/DataFlowAnalysis/CMakeLists.txt create mode 100644 llvm/lib/DataFlowAnalysis/Hello.cpp diff --git a/llvm/include/llvm/DataFlowAnalysis/Hello.h b/llvm/include/llvm/DataFlowAnalysis/Hello.h new file mode 100644 index 00000000000000..bc19f594ac291a --- /dev/null +++ b/llvm/include/llvm/DataFlowAnalysis/Hello.h @@ -0,0 +1,21 @@ +#ifndef LLVM_DFA_HELLO_H +#define LLVM_DFA_HELLO_H + +#include + +namespace llvm{ + +namespace Intrinsic{ +enum ID : unsigned; +} + +class Hello{ + int n; +public: + Hello(int); + int data(); +}; + +} // end namespace LLVM + +#endif // LLVM_DFA_HELLO_H diff --git a/llvm/lib/CMakeLists.txt b/llvm/lib/CMakeLists.txt index 5a41d65b0cf377..42330a2ad3a5dc 100644 --- a/llvm/lib/CMakeLists.txt +++ b/llvm/lib/CMakeLists.txt @@ -28,6 +28,7 @@ add_subdirectory(Passes) add_subdirectory(TextAPI) add_subdirectory(ToolDrivers) add_subdirectory(XRay) +add_subdirectory(DataFlowAnalysis) if (LLVM_INCLUDE_TESTS) add_subdirectory(Testing) endif() diff --git a/llvm/lib/DataFlowAnalysis/CMakeLists.txt b/llvm/lib/DataFlowAnalysis/CMakeLists.txt new file mode 100644 index 00000000000000..b187d38cbee57b --- /dev/null +++ b/llvm/lib/DataFlowAnalysis/CMakeLists.txt @@ -0,0 +1,9 @@ +add_llvm_library(LLVMDFA + Hello.cpp + + ADDITIONAL_HEADER_DIRS + ${LLVM_MAIN_INCLUDE_DIR}/llvm/DataFlowAnalysis + + DEPENDS + intrinsics_gen + ) diff --git a/llvm/lib/DataFlowAnalysis/Hello.cpp b/llvm/lib/DataFlowAnalysis/Hello.cpp new file mode 100644 index 00000000000000..a70ac3a562ed63 --- /dev/null +++ b/llvm/lib/DataFlowAnalysis/Hello.cpp @@ -0,0 +1,13 @@ +#include +#include "llvm/DataFlowAnalysis/Hello.h" + +using namespace llvm; + +Hello::Hello(int d){ + n = d; +} + +int Hello::data(){ + return n; +} + From ef18c4029c01939e41c12000fc837c44428f9768 Mon Sep 17 00:00:00 2001 From: Sunil Singh Date: Sun, 29 Sep 2019 04:33:59 +0530 Subject: [PATCH 2/3] FlowSet abstract class is being created as a template that would be a linkedlist to support different type of values(llvm). But there is some issue in the push_back function declaration. After doing changes, llvm is being built successfully but while using this in a pass an error is being thrown when trying to run the pass. Pass is being built successfully but while loading the pass using opt and running it with a program symbol lookup error is being thrown. --- llvm/include/llvm/DataFlowAnalysis/FlowSet.h | 101 +++++++++++++++++++ llvm/lib/DataFlowAnalysis/FlowSet.cpp | 25 +++++ 2 files changed, 126 insertions(+) create mode 100644 llvm/include/llvm/DataFlowAnalysis/FlowSet.h create mode 100644 llvm/lib/DataFlowAnalysis/FlowSet.cpp diff --git a/llvm/include/llvm/DataFlowAnalysis/FlowSet.h b/llvm/include/llvm/DataFlowAnalysis/FlowSet.h new file mode 100644 index 00000000000000..2bfc7bcceb46a1 --- /dev/null +++ b/llvm/include/llvm/DataFlowAnalysis/FlowSet.h @@ -0,0 +1,101 @@ +#ifndef LLVM_DFA_FLOWSET_H +#define LLVM_DFA_FLOWSET_H +#include + +using namespace std; +namespace llvm{ + +template +class FlowSet +{ + class Node; + +public: + FlowSet() noexcept + { + //m_spRoot = nullptr; + } + + class Iterator; + + Iterator begin() + { + return Iterator(m_spRoot); + } + + Iterator end() + { + return Iterator(nullptr); + } + + void push_back(T data); + //void traverse(); + + class Iterator + { + public: + Iterator() noexcept: + m_pCurrentNode(m_spRoot){ } + + Iterator(const Node* pNode) noexcept: + m_pCurrentNode(pNode){ } + + Iterator& operator=(Node* pNode) + { + this->m_pCurrentNode = pNode; + return *this; + } + + Iterator& operator++() + { + if(m_pCurrentNode) + m_pCurrentNode = m_pCurrentNode->pNext; + return *this; + } + + Iterator& operator++(int) + { + Iterator it = *this; + ++*this; + return it; + } + + Iterator& operator!=(const Iterator& it) + { + return m_pCurrentNode != it.m_pCurrentNode; + } + + int operator*() + { + return m_pCurrentNode->data; + } + private: + const Node* m_pCurrentNode; + }; + +private: + class Node + { + T data; + Node* pNext; + + friend class FlowSet; + }; + + Node* getNode(T d) + { + Node* pTmp = new Node; + pTmp->data = d; + pTmp->pNext = nullptr; + return pTmp; + } + Node*& getRoot() + { + return m_spRoot; + } + static Node* m_spRoot; +}; + +} // end namepspace llvm + +#endif // LLVM_DFA_FLOWSET_H diff --git a/llvm/lib/DataFlowAnalysis/FlowSet.cpp b/llvm/lib/DataFlowAnalysis/FlowSet.cpp new file mode 100644 index 00000000000000..055b63ba2521a7 --- /dev/null +++ b/llvm/lib/DataFlowAnalysis/FlowSet.cpp @@ -0,0 +1,25 @@ +#include "llvm/DataFlowAnalysis/FlowSet.h" + +using namespace llvm; + +template +typename FlowSet::Node* FlowSet::m_spRoot = nullptr; + +template +void FlowSet::push_back(T data) +{ + Node* pTemp = getNode(data); + if(!getRoot()) + { + getRoot() = pTemp; + } + else + { + Node* pC = getRoot(); + while(pC->pNext) + { + pC = pC->pNext; + } + pC->pNext = pTemp; + } +} From a5ee392e4e2aba5395ef228d4e073eb57691beb6 Mon Sep 17 00:00:00 2001 From: Sunil Singh Date: Thu, 3 Oct 2019 10:25:04 +0530 Subject: [PATCH 3/3] The issue is fixed. This issue was there because in order for the compiler to use a template, it must see both the template definition (not just a declaration) and the template type used to instantiate the template. refer https://www.learncpp.com/cpp-tutorial/133-template-classes/ (last section) Look at the ways to work around. --- llvm/include/llvm/DataFlowAnalysis/FlowSet.h | 74 ++++++++++++++------ llvm/include/llvm/DataFlowAnalysis/Hello.h | 21 ------ llvm/lib/DataFlowAnalysis/CMakeLists.txt | 1 + llvm/lib/DataFlowAnalysis/FlowSet.cpp | 22 +++--- llvm/lib/DataFlowAnalysis/Hello.cpp | 13 ---- 5 files changed, 63 insertions(+), 68 deletions(-) delete mode 100644 llvm/include/llvm/DataFlowAnalysis/Hello.h delete mode 100644 llvm/lib/DataFlowAnalysis/Hello.cpp diff --git a/llvm/include/llvm/DataFlowAnalysis/FlowSet.h b/llvm/include/llvm/DataFlowAnalysis/FlowSet.h index 2bfc7bcceb46a1..48993b624d5686 100644 --- a/llvm/include/llvm/DataFlowAnalysis/FlowSet.h +++ b/llvm/include/llvm/DataFlowAnalysis/FlowSet.h @@ -5,37 +5,35 @@ using namespace std; namespace llvm{ -template +template class FlowSet { class Node; - public: - FlowSet() noexcept + FlowSet() noexcept { - //m_spRoot = nullptr; + m_spRoot = nullptr; } class Iterator; - + Iterator begin() { return Iterator(m_spRoot); } - + Iterator end() { return Iterator(nullptr); } void push_back(T data); - //void traverse(); class Iterator { public: Iterator() noexcept: - m_pCurrentNode(m_spRoot){ } + m_pCurrentNode(m_spRoot){ } Iterator(const Node* pNode) noexcept: m_pCurrentNode(pNode){ } @@ -46,6 +44,16 @@ class FlowSet return *this; } + bool operator!=(const Iterator& it) + { + return this->m_pCurrentNode != it.m_pCurrentNode; + } + + bool operator==(const Iterator& it) + { + return this->m_pCurrentNode == it.m_pCurrentNode; + } + Iterator& operator++() { if(m_pCurrentNode) @@ -53,18 +61,14 @@ class FlowSet return *this; } - Iterator& operator++(int) + // post increment (must be non reference) + Iterator operator++(int) { Iterator it = *this; - ++*this; + ++(*this); return it; } - Iterator& operator!=(const Iterator& it) - { - return m_pCurrentNode != it.m_pCurrentNode; - } - int operator*() { return m_pCurrentNode->data; @@ -72,30 +76,54 @@ class FlowSet private: const Node* m_pCurrentNode; }; - private: class Node { T data; Node* pNext; - + friend class FlowSet; }; - Node* getNode(T d) + Node* getNode(T data) { - Node* pTmp = new Node; - pTmp->data = d; - pTmp->pNext = nullptr; - return pTmp; + Node* pNewNode = new Node; + pNewNode->data = data; + pNewNode->pNext = nullptr; + + return pNewNode; } - Node*& getRoot() + + Node*& getRootNode() { return m_spRoot; } + static Node* m_spRoot; }; +template +typename FlowSet::Node* FlowSet::m_spRoot = nullptr; +/* +template +void FlowSet::push_back(T data) +{ + Node* pTemp = getNode(data); + if(!getRootNode()) + { + getRootNode() = pTemp; + } + else + { + Node* pC = getRootNode(); + while(pC->pNext) + { + pC = pC->pNext; + } + pC->pNext = pTemp; + } +} +*/ } // end namepspace llvm #endif // LLVM_DFA_FLOWSET_H diff --git a/llvm/include/llvm/DataFlowAnalysis/Hello.h b/llvm/include/llvm/DataFlowAnalysis/Hello.h deleted file mode 100644 index bc19f594ac291a..00000000000000 --- a/llvm/include/llvm/DataFlowAnalysis/Hello.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef LLVM_DFA_HELLO_H -#define LLVM_DFA_HELLO_H - -#include - -namespace llvm{ - -namespace Intrinsic{ -enum ID : unsigned; -} - -class Hello{ - int n; -public: - Hello(int); - int data(); -}; - -} // end namespace LLVM - -#endif // LLVM_DFA_HELLO_H diff --git a/llvm/lib/DataFlowAnalysis/CMakeLists.txt b/llvm/lib/DataFlowAnalysis/CMakeLists.txt index b187d38cbee57b..360b3a31afc78b 100644 --- a/llvm/lib/DataFlowAnalysis/CMakeLists.txt +++ b/llvm/lib/DataFlowAnalysis/CMakeLists.txt @@ -1,5 +1,6 @@ add_llvm_library(LLVMDFA Hello.cpp + FlowSet.cpp ADDITIONAL_HEADER_DIRS ${LLVM_MAIN_INCLUDE_DIR}/llvm/DataFlowAnalysis diff --git a/llvm/lib/DataFlowAnalysis/FlowSet.cpp b/llvm/lib/DataFlowAnalysis/FlowSet.cpp index 055b63ba2521a7..94be0ce7ffdf81 100644 --- a/llvm/lib/DataFlowAnalysis/FlowSet.cpp +++ b/llvm/lib/DataFlowAnalysis/FlowSet.cpp @@ -2,24 +2,24 @@ using namespace llvm; -template -typename FlowSet::Node* FlowSet::m_spRoot = nullptr; - template void FlowSet::push_back(T data) { Node* pTemp = getNode(data); - if(!getRoot()) + if(!getRootNode()) { - getRoot() = pTemp; + getRootNode() = pTemp; } else { - Node* pC = getRoot(); - while(pC->pNext) - { - pC = pC->pNext; - } - pC->pNext = pTemp; + Node* pC = getRootNode(); + while(pC->pNext) + { + pC = pC->pNext; + } + pC->pNext = pTemp; } } + + +template class llvm::FlowSet; diff --git a/llvm/lib/DataFlowAnalysis/Hello.cpp b/llvm/lib/DataFlowAnalysis/Hello.cpp deleted file mode 100644 index a70ac3a562ed63..00000000000000 --- a/llvm/lib/DataFlowAnalysis/Hello.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include -#include "llvm/DataFlowAnalysis/Hello.h" - -using namespace llvm; - -Hello::Hello(int d){ - n = d; -} - -int Hello::data(){ - return n; -} -