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