Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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_OPTIMIZING_INTRINSICS_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_INTRINSICS_H_ |
| 19 | |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 20 | #include "code_generator.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 21 | #include "nodes.h" |
| 22 | #include "optimization.h" |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 23 | #include "parallel_move_resolver.h" |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class CompilerDriver; |
| 28 | class DexFile; |
| 29 | |
Andreas Gampe | e6d0d8d | 2015-12-28 09:54:29 -0800 | [diff] [blame^] | 30 | // Temporary measure until we have caught up with the Java 7 definition of Math.round. b/26327751 |
| 31 | static constexpr bool kRoundIsPlusPointFive = false; |
| 32 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 33 | // Recognize intrinsics from HInvoke nodes. |
| 34 | class IntrinsicsRecognizer : public HOptimization { |
| 35 | public: |
Nicolas Geoffray | e34648d | 2015-11-23 08:59:07 +0000 | [diff] [blame] | 36 | IntrinsicsRecognizer(HGraph* graph, CompilerDriver* driver) |
| 37 | : HOptimization(graph, kIntrinsicsRecognizerPassName), |
Nicolas Geoffray | d5111bf | 2015-05-22 15:37:09 +0100 | [diff] [blame] | 38 | driver_(driver) {} |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 39 | |
| 40 | void Run() OVERRIDE; |
| 41 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 42 | static constexpr const char* kIntrinsicsRecognizerPassName = "intrinsics_recognition"; |
| 43 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 44 | private: |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 45 | CompilerDriver* driver_; |
| 46 | |
| 47 | DISALLOW_COPY_AND_ASSIGN(IntrinsicsRecognizer); |
| 48 | }; |
| 49 | |
| 50 | class IntrinsicVisitor : public ValueObject { |
| 51 | public: |
| 52 | virtual ~IntrinsicVisitor() {} |
| 53 | |
| 54 | // Dispatch logic. |
| 55 | |
| 56 | void Dispatch(HInvoke* invoke) { |
| 57 | switch (invoke->GetIntrinsic()) { |
| 58 | case Intrinsics::kNone: |
| 59 | return; |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 60 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment) \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 61 | case Intrinsics::k ## Name: \ |
| 62 | Visit ## Name(invoke); \ |
| 63 | return; |
| 64 | #include "intrinsics_list.h" |
| 65 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 66 | #undef INTRINSICS_LIST |
| 67 | #undef OPTIMIZING_INTRINSICS |
| 68 | |
| 69 | // Do not put a default case. That way the compiler will complain if we missed a case. |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Define visitor methods. |
| 74 | |
agicsaki | 57b81ec | 2015-08-11 17:39:37 -0700 | [diff] [blame] | 75 | #define OPTIMIZING_INTRINSICS(Name, IsStatic, NeedsEnvironment) \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 76 | virtual void Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 77 | } |
| 78 | #include "intrinsics_list.h" |
| 79 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 80 | #undef INTRINSICS_LIST |
| 81 | #undef OPTIMIZING_INTRINSICS |
| 82 | |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 83 | static void MoveArguments(HInvoke* invoke, |
| 84 | CodeGenerator* codegen, |
| 85 | InvokeDexCallingConventionVisitor* calling_convention_visitor) { |
| 86 | if (kIsDebugBuild && invoke->IsInvokeStaticOrDirect()) { |
| 87 | HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect(); |
| 88 | // When we do not run baseline, explicit clinit checks triggered by static |
| 89 | // invokes must have been pruned by art::PrepareForRegisterAllocation. |
| 90 | DCHECK(codegen->IsBaseline() || !invoke_static_or_direct->IsStaticWithExplicitClinitCheck()); |
| 91 | } |
| 92 | |
| 93 | if (invoke->GetNumberOfArguments() == 0) { |
| 94 | // No argument to move. |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | LocationSummary* locations = invoke->GetLocations(); |
| 99 | |
| 100 | // We're moving potentially two or more locations to locations that could overlap, so we need |
| 101 | // a parallel move resolver. |
| 102 | HParallelMove parallel_move(codegen->GetGraph()->GetArena()); |
| 103 | |
| 104 | for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) { |
| 105 | HInstruction* input = invoke->InputAt(i); |
| 106 | Location cc_loc = calling_convention_visitor->GetNextLocation(input->GetType()); |
| 107 | Location actual_loc = locations->InAt(i); |
| 108 | |
| 109 | parallel_move.AddMove(actual_loc, cc_loc, input->GetType(), nullptr); |
| 110 | } |
| 111 | |
| 112 | codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 113 | } |
| 114 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 115 | protected: |
| 116 | IntrinsicVisitor() {} |
| 117 | |
| 118 | private: |
| 119 | DISALLOW_COPY_AND_ASSIGN(IntrinsicVisitor); |
| 120 | }; |
| 121 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 122 | #define GENERIC_OPTIMIZATION(name, bit) \ |
Nicolas Geoffray | 12be662 | 2015-10-07 11:52:21 +0100 | [diff] [blame] | 123 | public: \ |
| 124 | void Set##name() { SetBit(k##name); } \ |
| 125 | bool Get##name() const { return IsBitSet(k##name); } \ |
| 126 | private: \ |
| 127 | static constexpr int k##name = bit |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 128 | |
| 129 | class IntrinsicOptimizations : public ValueObject { |
| 130 | public: |
Nicolas Geoffray | 12be662 | 2015-10-07 11:52:21 +0100 | [diff] [blame] | 131 | explicit IntrinsicOptimizations(HInvoke* invoke) : value_(invoke->GetIntrinsicOptimizations()) {} |
| 132 | explicit IntrinsicOptimizations(const HInvoke& invoke) |
| 133 | : value_(invoke.GetIntrinsicOptimizations()) {} |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 134 | |
| 135 | static constexpr int kNumberOfGenericOptimizations = 2; |
| 136 | GENERIC_OPTIMIZATION(DoesNotNeedDexCache, 0); |
| 137 | GENERIC_OPTIMIZATION(DoesNotNeedEnvironment, 1); |
| 138 | |
| 139 | protected: |
| 140 | bool IsBitSet(uint32_t bit) const { |
| 141 | return (*value_ & (1 << bit)) != 0u; |
| 142 | } |
| 143 | |
| 144 | void SetBit(uint32_t bit) { |
| 145 | *(const_cast<uint32_t*>(value_)) |= (1 << bit); |
| 146 | } |
| 147 | |
| 148 | private: |
| 149 | const uint32_t *value_; |
| 150 | |
| 151 | DISALLOW_COPY_AND_ASSIGN(IntrinsicOptimizations); |
| 152 | }; |
| 153 | |
| 154 | #undef GENERIC_OPTIMIZATION |
| 155 | |
| 156 | #define INTRINSIC_OPTIMIZATION(name, bit) \ |
Nicolas Geoffray | 12be662 | 2015-10-07 11:52:21 +0100 | [diff] [blame] | 157 | public: \ |
| 158 | void Set##name() { SetBit(k##name); } \ |
| 159 | bool Get##name() const { return IsBitSet(k##name); } \ |
| 160 | private: \ |
| 161 | static constexpr int k##name = bit + kNumberOfGenericOptimizations |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 162 | |
| 163 | class StringEqualsOptimizations : public IntrinsicOptimizations { |
| 164 | public: |
Nicolas Geoffray | 12be662 | 2015-10-07 11:52:21 +0100 | [diff] [blame] | 165 | explicit StringEqualsOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {} |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 166 | |
| 167 | INTRINSIC_OPTIMIZATION(ArgumentNotNull, 0); |
| 168 | INTRINSIC_OPTIMIZATION(ArgumentIsString, 1); |
| 169 | |
| 170 | private: |
| 171 | DISALLOW_COPY_AND_ASSIGN(StringEqualsOptimizations); |
| 172 | }; |
| 173 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 174 | class SystemArrayCopyOptimizations : public IntrinsicOptimizations { |
| 175 | public: |
| 176 | explicit SystemArrayCopyOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {} |
| 177 | |
| 178 | INTRINSIC_OPTIMIZATION(SourceIsNotNull, 0); |
| 179 | INTRINSIC_OPTIMIZATION(DestinationIsNotNull, 1); |
| 180 | INTRINSIC_OPTIMIZATION(DestinationIsSource, 2); |
| 181 | INTRINSIC_OPTIMIZATION(CountIsSourceLength, 3); |
| 182 | INTRINSIC_OPTIMIZATION(CountIsDestinationLength, 4); |
| 183 | INTRINSIC_OPTIMIZATION(DoesNotNeedTypeCheck, 5); |
| 184 | INTRINSIC_OPTIMIZATION(DestinationIsTypedObjectArray, 6); |
| 185 | INTRINSIC_OPTIMIZATION(DestinationIsNonPrimitiveArray, 7); |
| 186 | INTRINSIC_OPTIMIZATION(DestinationIsPrimitiveArray, 8); |
| 187 | INTRINSIC_OPTIMIZATION(SourceIsNonPrimitiveArray, 9); |
| 188 | INTRINSIC_OPTIMIZATION(SourceIsPrimitiveArray, 10); |
| 189 | |
| 190 | private: |
| 191 | DISALLOW_COPY_AND_ASSIGN(SystemArrayCopyOptimizations); |
| 192 | }; |
| 193 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 194 | #undef INTRISIC_OPTIMIZATION |
| 195 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 196 | } // namespace art |
| 197 | |
| 198 | #endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_ |