Loop invariant code motion initial implementation
[platform/upstream/SPIRV-Tools.git] / source / opt / loop_utils.h
1 // Copyright (c) 2018 Google LLC.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef LIBSPIRV_OPT_LOOP_UTILS_H_
16 #define LIBSPIRV_OPT_LOOP_UTILS_H_
17
18 namespace spvtools {
19
20 namespace ir {
21 class Loop;
22 class IRContext;
23 }  // namespace ir
24
25 namespace opt {
26
27 // Set of basic loop transformation.
28 class LoopUtils {
29  public:
30   LoopUtils(ir::IRContext* context, ir::Loop* loop)
31       : context_(context), loop_(loop) {}
32
33   // The converts the current loop to loop closed SSA form.
34   // In the loop closed SSA, all loop exiting values go through a dedicated Phi
35   // instruction. For instance:
36   //
37   // for (...) {
38   //   A1 = ...
39   //   if (...)
40   //     A2 = ...
41   //   A = phi A1, A2
42   // }
43   // ... = op A ...
44   //
45   // Becomes
46   //
47   // for (...) {
48   //   A1 = ...
49   //   if (...)
50   //     A2 = ...
51   //   A = phi A1, A2
52   // }
53   // C = phi A
54   // ... = op C ...
55   //
56   // This makes some loop transformations (such as loop unswitch) simpler
57   // (removes the needs to take care of exiting variables).
58   void MakeLoopClosedSSA();
59
60   // Create dedicate exit basic block. This ensure all exit basic blocks has the
61   // loop as sole predecessors.
62   // By construction, structured control flow already has a dedicated exit
63   // block.
64   // Preserves: CFG, def/use and instruction to block mapping.
65   void CreateLoopDedicatedExits();
66
67  private:
68   ir::IRContext* context_;
69   ir::Loop* loop_;
70 };
71
72 }  // namespace opt
73 }  // namespace spvtools
74
75 #endif  // LIBSPIRV_OPT_LOOP_UTILS_H_