Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_DEX_PASS_H_ |
| 18 | #define ART_COMPILER_DEX_PASS_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 22 | #include "compiler_ir.h" |
| 23 | #include "base/logging.h" |
| 24 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 25 | namespace art { |
| 26 | |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 27 | // Forward declarations. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 28 | class BasicBlock; |
Jean Christophe Beyler | 44e5bde | 2014-04-29 14:40:41 -0700 | [diff] [blame] | 29 | struct CompilationUnit; |
| 30 | class Pass; |
| 31 | |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 32 | // Empty Pass Data Class, can be extended by any pass extending the base Pass class. |
| 33 | class PassDataHolder { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * @class Pass |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 38 | * @brief Base Pass class, can be extended to perform a more defined way of doing the work call. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 39 | */ |
| 40 | class Pass { |
| 41 | public: |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 42 | explicit Pass(const char* name) |
| 43 | : pass_name_(name) { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Vladimir Marko | 75ba13f | 2014-01-28 12:15:24 +0000 | [diff] [blame] | 46 | virtual ~Pass() { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 49 | virtual const char* GetName() const { |
| 50 | return pass_name_; |
| 51 | } |
| 52 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 53 | /** |
| 54 | * @brief Gate for the pass: determines whether to execute the pass or not considering a CompilationUnit |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 55 | * @param data the PassDataHolder. |
| 56 | * @return whether or not to execute the pass. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 57 | */ |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 58 | virtual bool Gate(const PassDataHolder* data) const { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 59 | // Unused parameter. |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 60 | UNUSED(data); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 61 | |
| 62 | // Base class says yes. |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | /** |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 67 | * @brief Start of the pass: called before the Worker function. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 68 | */ |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 69 | virtual void Start(PassDataHolder* data) const { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 70 | // Unused parameter. |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 71 | UNUSED(data); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 75 | * @brief End of the pass: called after the WalkBasicBlocks function. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 76 | */ |
Jean Christophe Beyler | e1f65bc | 2014-06-09 10:18:26 -0700 | [diff] [blame] | 77 | virtual void End(PassDataHolder* data) const { |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 78 | // Unused parameter. |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 79 | UNUSED(data); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
James C Scott | 4f59668 | 2014-05-01 05:52:04 -0700 | [diff] [blame] | 83 | * @param data the object containing data necessary for the pass. |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 84 | * @return whether or not there is a change when walking the BasicBlock |
| 85 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 86 | virtual bool Worker(PassDataHolder* data ATTRIBUTE_UNUSED) const { |
Vladimir Marko | aa7b8a3 | 2014-10-15 11:35:44 +0100 | [diff] [blame] | 87 | // Passes that do all their work in Start() or End() should not allow useless node iteration. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 88 | LOG(FATAL) << "Unsupported default Worker() used for " << GetName(); |
| 89 | UNREACHABLE(); |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jean Christophe Beyler | 8bcecce | 2014-04-29 13:42:08 -0700 | [diff] [blame] | 92 | static void BasePrintMessage(CompilationUnit* c_unit, const char* pass_name, const char* message, ...) { |
| 93 | // Check if we want to log something or not. |
| 94 | if (c_unit->print_pass) { |
| 95 | // Stringify the message. |
| 96 | va_list args; |
| 97 | va_start(args, message); |
| 98 | std::string stringified_message; |
| 99 | StringAppendV(&stringified_message, message, args); |
| 100 | va_end(args); |
| 101 | |
| 102 | // Log the message and ensure to include pass name. |
| 103 | LOG(INFO) << pass_name << ": " << stringified_message; |
| 104 | } |
| 105 | } |
| 106 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 107 | protected: |
| 108 | /** @brief The pass name: used for searching for a pass when running a particular pass or debugging. */ |
| 109 | const char* const pass_name_; |
| 110 | |
Jean Christophe Beyler | 4e97c53 | 2014-01-07 10:07:18 -0800 | [diff] [blame] | 111 | private: |
| 112 | // In order to make the all passes not copy-friendly. |
| 113 | DISALLOW_COPY_AND_ASSIGN(Pass); |
| 114 | }; |
| 115 | } // namespace art |
| 116 | #endif // ART_COMPILER_DEX_PASS_H_ |