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 | |
Anton Kirilov | a3ffea2 | 2016-04-07 17:02:37 +0100 | [diff] [blame] | 30 | // Positive floating-point infinities. |
| 31 | static constexpr uint32_t kPositiveInfinityFloat = 0x7f800000U; |
| 32 | static constexpr uint64_t kPositiveInfinityDouble = UINT64_C(0x7ff0000000000000); |
| 33 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 34 | // Recognize intrinsics from HInvoke nodes. |
| 35 | class IntrinsicsRecognizer : public HOptimization { |
| 36 | public: |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 37 | IntrinsicsRecognizer(HGraph* graph, OptimizingCompilerStats* stats) |
| 38 | : HOptimization(graph, kIntrinsicsRecognizerPassName, stats) {} |
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 | DISALLOW_COPY_AND_ASSIGN(IntrinsicsRecognizer); |
| 46 | }; |
| 47 | |
| 48 | class IntrinsicVisitor : public ValueObject { |
| 49 | public: |
| 50 | virtual ~IntrinsicVisitor() {} |
| 51 | |
| 52 | // Dispatch logic. |
| 53 | |
| 54 | void Dispatch(HInvoke* invoke) { |
| 55 | switch (invoke->GetIntrinsic()) { |
| 56 | case Intrinsics::kNone: |
| 57 | return; |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 58 | #define OPTIMIZING_INTRINSICS(Name, ...) \ |
Aart Bik | 5d75afe | 2015-12-14 11:57:01 -0800 | [diff] [blame] | 59 | case Intrinsics::k ## Name: \ |
| 60 | Visit ## Name(invoke); \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 61 | return; |
| 62 | #include "intrinsics_list.h" |
| 63 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 64 | #undef INTRINSICS_LIST |
| 65 | #undef OPTIMIZING_INTRINSICS |
| 66 | |
| 67 | // Do not put a default case. That way the compiler will complain if we missed a case. |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Define visitor methods. |
| 72 | |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 73 | #define OPTIMIZING_INTRINSICS(Name, ...) \ |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 74 | virtual void Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 75 | } |
| 76 | #include "intrinsics_list.h" |
| 77 | INTRINSICS_LIST(OPTIMIZING_INTRINSICS) |
| 78 | #undef INTRINSICS_LIST |
| 79 | #undef OPTIMIZING_INTRINSICS |
| 80 | |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 81 | static void MoveArguments(HInvoke* invoke, |
| 82 | CodeGenerator* codegen, |
| 83 | InvokeDexCallingConventionVisitor* calling_convention_visitor) { |
| 84 | if (kIsDebugBuild && invoke->IsInvokeStaticOrDirect()) { |
| 85 | HInvokeStaticOrDirect* invoke_static_or_direct = invoke->AsInvokeStaticOrDirect(); |
David Brazdil | 58282f4 | 2016-01-14 12:45:10 +0000 | [diff] [blame] | 86 | // Explicit clinit checks triggered by static invokes must have been |
| 87 | // pruned by art::PrepareForRegisterAllocation. |
| 88 | DCHECK(!invoke_static_or_direct->IsStaticWithExplicitClinitCheck()); |
Roland Levillain | ec525fc | 2015-04-28 15:50:20 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (invoke->GetNumberOfArguments() == 0) { |
| 92 | // No argument to move. |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | LocationSummary* locations = invoke->GetLocations(); |
| 97 | |
| 98 | // We're moving potentially two or more locations to locations that could overlap, so we need |
| 99 | // a parallel move resolver. |
| 100 | HParallelMove parallel_move(codegen->GetGraph()->GetArena()); |
| 101 | |
| 102 | for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) { |
| 103 | HInstruction* input = invoke->InputAt(i); |
| 104 | Location cc_loc = calling_convention_visitor->GetNextLocation(input->GetType()); |
| 105 | Location actual_loc = locations->InAt(i); |
| 106 | |
| 107 | parallel_move.AddMove(actual_loc, cc_loc, input->GetType(), nullptr); |
| 108 | } |
| 109 | |
| 110 | codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); |
| 111 | } |
| 112 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 113 | protected: |
| 114 | IntrinsicVisitor() {} |
| 115 | |
| 116 | private: |
| 117 | DISALLOW_COPY_AND_ASSIGN(IntrinsicVisitor); |
| 118 | }; |
| 119 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 120 | #define GENERIC_OPTIMIZATION(name, bit) \ |
Nicolas Geoffray | 12be662 | 2015-10-07 11:52:21 +0100 | [diff] [blame] | 121 | public: \ |
| 122 | void Set##name() { SetBit(k##name); } \ |
| 123 | bool Get##name() const { return IsBitSet(k##name); } \ |
| 124 | private: \ |
Roland Levillain | ebea3d2 | 2016-04-12 15:42:57 +0100 | [diff] [blame] | 125 | static constexpr size_t k##name = bit |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 126 | |
| 127 | class IntrinsicOptimizations : public ValueObject { |
| 128 | public: |
Roland Levillain | ebea3d2 | 2016-04-12 15:42:57 +0100 | [diff] [blame] | 129 | explicit IntrinsicOptimizations(HInvoke* invoke) |
| 130 | : value_(invoke->GetIntrinsicOptimizations()) {} |
Nicolas Geoffray | 12be662 | 2015-10-07 11:52:21 +0100 | [diff] [blame] | 131 | explicit IntrinsicOptimizations(const HInvoke& invoke) |
| 132 | : value_(invoke.GetIntrinsicOptimizations()) {} |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 133 | |
| 134 | static constexpr int kNumberOfGenericOptimizations = 2; |
| 135 | GENERIC_OPTIMIZATION(DoesNotNeedDexCache, 0); |
| 136 | GENERIC_OPTIMIZATION(DoesNotNeedEnvironment, 1); |
| 137 | |
| 138 | protected: |
| 139 | bool IsBitSet(uint32_t bit) const { |
Roland Levillain | ebea3d2 | 2016-04-12 15:42:57 +0100 | [diff] [blame] | 140 | DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte); |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 141 | return (*value_ & (1 << bit)) != 0u; |
| 142 | } |
| 143 | |
| 144 | void SetBit(uint32_t bit) { |
Roland Levillain | ebea3d2 | 2016-04-12 15:42:57 +0100 | [diff] [blame] | 145 | DCHECK_LT(bit, sizeof(uint32_t) * kBitsPerByte); |
| 146 | *(const_cast<uint32_t* const>(value_)) |= (1 << bit); |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | private: |
Roland Levillain | ebea3d2 | 2016-04-12 15:42:57 +0100 | [diff] [blame] | 150 | const uint32_t* const value_; |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 151 | |
| 152 | DISALLOW_COPY_AND_ASSIGN(IntrinsicOptimizations); |
| 153 | }; |
| 154 | |
| 155 | #undef GENERIC_OPTIMIZATION |
| 156 | |
| 157 | #define INTRINSIC_OPTIMIZATION(name, bit) \ |
Nicolas Geoffray | 12be662 | 2015-10-07 11:52:21 +0100 | [diff] [blame] | 158 | public: \ |
| 159 | void Set##name() { SetBit(k##name); } \ |
| 160 | bool Get##name() const { return IsBitSet(k##name); } \ |
| 161 | private: \ |
Chih-Hung Hsieh | fba3997 | 2016-05-11 11:26:48 -0700 | [diff] [blame] | 162 | static constexpr size_t k##name = (bit) + kNumberOfGenericOptimizations |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 163 | |
| 164 | class StringEqualsOptimizations : public IntrinsicOptimizations { |
| 165 | public: |
Nicolas Geoffray | 12be662 | 2015-10-07 11:52:21 +0100 | [diff] [blame] | 166 | explicit StringEqualsOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {} |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 167 | |
| 168 | INTRINSIC_OPTIMIZATION(ArgumentNotNull, 0); |
| 169 | INTRINSIC_OPTIMIZATION(ArgumentIsString, 1); |
| 170 | |
| 171 | private: |
| 172 | DISALLOW_COPY_AND_ASSIGN(StringEqualsOptimizations); |
| 173 | }; |
| 174 | |
Nicolas Geoffray | ee3cf07 | 2015-10-06 11:45:02 +0100 | [diff] [blame] | 175 | class SystemArrayCopyOptimizations : public IntrinsicOptimizations { |
| 176 | public: |
| 177 | explicit SystemArrayCopyOptimizations(HInvoke* invoke) : IntrinsicOptimizations(invoke) {} |
| 178 | |
| 179 | INTRINSIC_OPTIMIZATION(SourceIsNotNull, 0); |
| 180 | INTRINSIC_OPTIMIZATION(DestinationIsNotNull, 1); |
| 181 | INTRINSIC_OPTIMIZATION(DestinationIsSource, 2); |
| 182 | INTRINSIC_OPTIMIZATION(CountIsSourceLength, 3); |
| 183 | INTRINSIC_OPTIMIZATION(CountIsDestinationLength, 4); |
| 184 | INTRINSIC_OPTIMIZATION(DoesNotNeedTypeCheck, 5); |
| 185 | INTRINSIC_OPTIMIZATION(DestinationIsTypedObjectArray, 6); |
| 186 | INTRINSIC_OPTIMIZATION(DestinationIsNonPrimitiveArray, 7); |
| 187 | INTRINSIC_OPTIMIZATION(DestinationIsPrimitiveArray, 8); |
| 188 | INTRINSIC_OPTIMIZATION(SourceIsNonPrimitiveArray, 9); |
| 189 | INTRINSIC_OPTIMIZATION(SourceIsPrimitiveArray, 10); |
| 190 | |
| 191 | private: |
| 192 | DISALLOW_COPY_AND_ASSIGN(SystemArrayCopyOptimizations); |
| 193 | }; |
| 194 | |
Nicolas Geoffray | a83a54d | 2015-10-02 17:30:26 +0100 | [diff] [blame] | 195 | #undef INTRISIC_OPTIMIZATION |
| 196 | |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 197 | // |
| 198 | // Macros for use in the intrinsics code generators. |
| 199 | // |
| 200 | |
| 201 | // Defines an unimplemented intrinsic: that is, a method call that is recognized as an |
| 202 | // intrinsic to exploit e.g. no side-effects or exceptions, but otherwise not handled |
| 203 | // by this architecture-specific intrinsics code generator. Eventually it is implemented |
| 204 | // as a true method call. |
| 205 | #define UNIMPLEMENTED_INTRINSIC(Arch, Name) \ |
| 206 | void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 207 | } \ |
| 208 | void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke ATTRIBUTE_UNUSED) { \ |
| 209 | } |
| 210 | |
| 211 | // Defines a list of unreached intrinsics: that is, method calls that are recognized as |
| 212 | // an intrinsic, and then always converted into HIR instructions before they reach any |
| 213 | // architecture-specific intrinsics code generator. |
| 214 | #define UNREACHABLE_INTRINSIC(Arch, Name) \ |
| 215 | void IntrinsicLocationsBuilder ## Arch::Visit ## Name(HInvoke* invoke) { \ |
| 216 | LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \ |
| 217 | << " should have been converted to HIR"; \ |
| 218 | } \ |
| 219 | void IntrinsicCodeGenerator ## Arch::Visit ## Name(HInvoke* invoke) { \ |
| 220 | LOG(FATAL) << "Unreachable: intrinsic " << invoke->GetIntrinsic() \ |
| 221 | << " should have been converted to HIR"; \ |
| 222 | } |
| 223 | #define UNREACHABLE_INTRINSICS(Arch) \ |
| 224 | UNREACHABLE_INTRINSIC(Arch, FloatFloatToIntBits) \ |
| 225 | UNREACHABLE_INTRINSIC(Arch, DoubleDoubleToLongBits) \ |
| 226 | UNREACHABLE_INTRINSIC(Arch, FloatIsNaN) \ |
| 227 | UNREACHABLE_INTRINSIC(Arch, DoubleIsNaN) \ |
| 228 | UNREACHABLE_INTRINSIC(Arch, IntegerRotateLeft) \ |
| 229 | UNREACHABLE_INTRINSIC(Arch, LongRotateLeft) \ |
| 230 | UNREACHABLE_INTRINSIC(Arch, IntegerRotateRight) \ |
| 231 | UNREACHABLE_INTRINSIC(Arch, LongRotateRight) \ |
| 232 | UNREACHABLE_INTRINSIC(Arch, IntegerCompare) \ |
| 233 | UNREACHABLE_INTRINSIC(Arch, LongCompare) \ |
| 234 | UNREACHABLE_INTRINSIC(Arch, IntegerSignum) \ |
Aart Bik | 1193259 | 2016-03-08 12:42:25 -0800 | [diff] [blame] | 235 | UNREACHABLE_INTRINSIC(Arch, LongSignum) \ |
Vladimir Marko | 87f3fcb | 2016-04-28 15:52:11 +0100 | [diff] [blame] | 236 | UNREACHABLE_INTRINSIC(Arch, StringCharAt) \ |
Vladimir Marko | dce016e | 2016-04-28 13:10:02 +0100 | [diff] [blame] | 237 | UNREACHABLE_INTRINSIC(Arch, StringIsEmpty) \ |
| 238 | UNREACHABLE_INTRINSIC(Arch, StringLength) \ |
Aart Bik | 1193259 | 2016-03-08 12:42:25 -0800 | [diff] [blame] | 239 | UNREACHABLE_INTRINSIC(Arch, UnsafeLoadFence) \ |
| 240 | UNREACHABLE_INTRINSIC(Arch, UnsafeStoreFence) \ |
| 241 | UNREACHABLE_INTRINSIC(Arch, UnsafeFullFence) |
Aart Bik | 2f9fcc9 | 2016-03-01 15:16:54 -0800 | [diff] [blame] | 242 | |
Vladimir Marko | 68c981f | 2016-08-26 13:13:33 +0100 | [diff] [blame] | 243 | template <typename IntrinsicLocationsBuilder, typename Codegenerator> |
| 244 | bool IsCallFreeIntrinsic(HInvoke* invoke, Codegenerator* codegen) { |
| 245 | if (invoke->GetIntrinsic() != Intrinsics::kNone) { |
| 246 | // This invoke may have intrinsic code generation defined. However, we must |
| 247 | // now also determine if this code generation is truly there and call-free |
| 248 | // (not unimplemented, no bail on instruction features, or call on slow path). |
| 249 | // This is done by actually calling the locations builder on the instruction |
| 250 | // and clearing out the locations once result is known. We assume this |
| 251 | // call only has creating locations as side effects! |
| 252 | // TODO: Avoid wasting Arena memory. |
| 253 | IntrinsicLocationsBuilder builder(codegen); |
| 254 | bool success = builder.TryDispatch(invoke) && !invoke->GetLocations()->CanCall(); |
| 255 | invoke->SetLocations(nullptr); |
| 256 | return success; |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
Andreas Gampe | 71fb52f | 2014-12-29 17:43:08 -0800 | [diff] [blame] | 261 | } // namespace art |
| 262 | |
| 263 | #endif // ART_COMPILER_OPTIMIZING_INTRINSICS_H_ |