IVGCVSW-2458 Refactor the Optimize function (Network.cpp) so that
subgraphs are optimized by the backends

 * Added a new method OptimizeSubGraph to the backend interface
 * Refactored the Optimize function so that the backend-specific
   optimization is performed by the backend itself (through the new
   OptimizeSubGraph interface method)
 * Added a new ApplyBackendOptimizations function to apply the new
   changes
 * Added some new convenient constructors to the SubGraph class
 * Added AddLayer method and a pointer to the parent graph to the
   SubGraph class
 * Updated the sub-graph unit tests to match the changes
 * Added SelectSubGraphs and ReplaceSubGraphConnections overloads
   that work with sub-graphs
 * Removed unused code and minor refactoring where necessary

Change-Id: I46181794c6a9e3b10558944f804e06a8f693a6d0
diff --git a/src/backends/backendsCommon/IBackendInternal.hpp b/src/backends/backendsCommon/IBackendInternal.hpp
index 2e6b056..6e6d47f 100644
--- a/src/backends/backendsCommon/IBackendInternal.hpp
+++ b/src/backends/backendsCommon/IBackendInternal.hpp
@@ -43,6 +43,8 @@
 
     using ISubGraphConverterPtr = std::unique_ptr<ISubGraphConverter>;
 
+    using SubGraphUniquePtr = std::unique_ptr<SubGraph>;
+
     virtual IMemoryManagerUniquePtr CreateMemoryManager() const = 0;
 
     virtual IWorkloadFactoryPtr CreateWorkloadFactory(
@@ -54,6 +56,8 @@
 
     virtual Optimizations GetOptimizations() const = 0;
     virtual ILayerSupportSharedPtr GetLayerSupport() const = 0;
+
+    virtual SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph, bool& optimizationAttempted) const = 0;
 };
 
 using IBackendInternalUniquePtr = std::unique_ptr<IBackendInternal>;
diff --git a/src/backends/cl/ClBackend.cpp b/src/backends/cl/ClBackend.cpp
index 2f9dfa9..dfa5e7c 100644
--- a/src/backends/cl/ClBackend.cpp
+++ b/src/backends/cl/ClBackend.cpp
@@ -80,4 +80,13 @@
     return layerSupport;
 }
 
+IBackendInternal::SubGraphUniquePtr ClBackend::OptimizeSubGraph(const SubGraph& subGraph,
+                                                                bool& optimizationAttempted) const
+{
+    // Not trying to optimize the given sub-graph
+    optimizationAttempted = false;
+
+    return SubGraphUniquePtr{};
+}
+
 } // namespace armnn
diff --git a/src/backends/cl/ClBackend.hpp b/src/backends/cl/ClBackend.hpp
index 84b5b9a..8133ce4 100644
--- a/src/backends/cl/ClBackend.hpp
+++ b/src/backends/cl/ClBackend.hpp
@@ -30,6 +30,9 @@
 
     IBackendInternal::Optimizations GetOptimizations() const override;
     IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override;
+
+    IBackendInternal::SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph,
+                                                         bool& optimizationAttempted) const override;
 };
 
-} // namespace armnn
\ No newline at end of file
+} // namespace armnn
diff --git a/src/backends/neon/NeonBackend.cpp b/src/backends/neon/NeonBackend.cpp
index ce97a1d..046685a 100644
--- a/src/backends/neon/NeonBackend.cpp
+++ b/src/backends/neon/NeonBackend.cpp
@@ -80,4 +80,13 @@
     return layerSupport;
 }
 
+IBackendInternal::SubGraphUniquePtr NeonBackend::OptimizeSubGraph(const SubGraph& subGraph,
+                                                                  bool& optimizationAttempted) const
+{
+    // Not trying to optimize the given sub-graph
+    optimizationAttempted = false;
+
+    return SubGraphUniquePtr{};
+}
+
 } // namespace armnn
diff --git a/src/backends/neon/NeonBackend.hpp b/src/backends/neon/NeonBackend.hpp
index 3b1d186..6347045 100644
--- a/src/backends/neon/NeonBackend.hpp
+++ b/src/backends/neon/NeonBackend.hpp
@@ -30,6 +30,9 @@
 
     IBackendInternal::Optimizations GetOptimizations() const override;
     IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override;
+
+    IBackendInternal::SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph,
+                                                         bool& optimizationAttempted) const override;
 };
 
-} // namespace armnn
\ No newline at end of file
+} // namespace armnn
diff --git a/src/backends/reference/RefBackend.cpp b/src/backends/reference/RefBackend.cpp
index e4f468c..d5f5348 100644
--- a/src/backends/reference/RefBackend.cpp
+++ b/src/backends/reference/RefBackend.cpp
@@ -73,4 +73,13 @@
     return layerSupport;
 }
 
-} // namespace armnn
\ No newline at end of file
+IBackendInternal::SubGraphUniquePtr RefBackend::OptimizeSubGraph(const SubGraph& subGraph,
+                                                                 bool& optimizationAttempted) const
+{
+    // Not trying to optimize the given sub-graph
+    optimizationAttempted = false;
+
+    return SubGraphUniquePtr{};
+}
+
+} // namespace armnn
diff --git a/src/backends/reference/RefBackend.hpp b/src/backends/reference/RefBackend.hpp
index 5136622..6305bf5 100644
--- a/src/backends/reference/RefBackend.hpp
+++ b/src/backends/reference/RefBackend.hpp
@@ -30,6 +30,9 @@
 
     IBackendInternal::Optimizations GetOptimizations() const override;
     IBackendInternal::ILayerSupportSharedPtr GetLayerSupport() const override;
+
+    IBackendInternal::SubGraphUniquePtr OptimizeSubGraph(const SubGraph& subGraph,
+                                                         bool& optimizationAttempted) const override;
 };
 
-} // namespace armnn
\ No newline at end of file
+} // namespace armnn