summaryrefslogtreecommitdiff
path: root/compiler/optimizing/constant_folding.cc
AgeCommit message (Collapse)Author
2025-02-14Optimize HConstantFoldingVisitor::PropagateValue Santiago Aboy Solanes
We can avoid creating constants which were created with GetGraph()->GetIntConstant. In theory, we could still be creating a constant that ReplaceUsesDominatedBy doesn't use. In practice, we can eliminate most (95%+) of constants being created without impacting or refactoring ReplaceUsesDominatedBy. Bug: 240543764 Bug: 393108375 Test: art/test/testrunner/testrunner.py --host --64 -b --optimizing Change-Id: I91f93c373b9097d5598398cc92767ecde5d4aec7
2024-10-22Optimizing: Clean up graph visiting. Vladimir Marko
Change `PrepareForRegisterAllocation` to `HOptimization` and use a helper visitor class. Make some visitor classes final. Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Bug: 181943478 Change-Id: I87700b92e06ff1a6cf7cd8a1e3ab8b7a87568dae
2024-04-11Clean up string data access in `DexFile`. Vladimir Marko
The `*ByIdx()` and `*ByTypeIdx()` functions were doing validity checks that were needed only for processing the debug data, so move the checks to these callers. Replace these functions with new overloads of other functions to provide consistent naming. In a few cases, rewrite calls to these functions to fetch and work with a `string_view` instead. Rename `GetStringLength()` to `GetStringUtf16Length()` and change its return type to `uint32_t`. Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Change-Id: I561899606f6e5ec5f23aa4be617349dacdb376e3
2024-03-04Fold opposite values for OR and XOR into -1 Santiago Aboy Solanes
We can fold operations like `a | ~a` and `a ^ ~a` into 0xFF...FF. These operations only exist for int/long. Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Change-Id: I765e294ca2938710576a94de5978994ab1227a6b
2024-02-09Clean up `HGraphVisitor::VisitBasicBlock()`. Vladimir Marko
Skip `HPhi::Accept()` and add functions to visit only Phis or only non-Phi instructions. Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Bug: 181943478 Change-Id: Iba0690ae70f46d6a2bafa9055b2ae5167e58a2f4
2024-01-30Speed up HConstantFoldingVisitor::PropagateValue Santiago Aboy Solanes
We can speed it up in two ways: 1) Don't call it if it has exactly one element, as we will never be able to replace its use in the if clause 2) Lazily compute the dominated blocks when needed Compiling locally GMS, HConstantFoldingVisitor::VisitIf goes down from 1.8% of the compile time to 0.7%. Most of this improvement (90%+) is coming from the `1)` optimization. This is because there are many cases where we have only one use (the if), which is in the same block so we compute the domination to always end up not doing the optimization. Bug: 278626992 Test: Locally compile gms Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Change-Id: Ic17b4b44840c7efa0224504031bf635584850ced
2024-01-15Revert "Restrict the use of ConstantFolding's VisitIf" Santiago Aboy Solanes
This reverts commit 5eb1fd0dae3832ceee2102613bb08c291daca6f3. Reason for revert: In aosp/2903248 we implemented a faster way of doing `ReplaceUsesDominatedBy` which is used by `VisitIf`. The impact of `VisitIf` is now small enough that running VisitIf in all passes is faster that the previous implementation running some of the time. This CLs re-enables the optimization in all constant folding passes because: A) Lets this optimization (and others that can use the result) kick in earlier B) Run it for callee graphs in the inliner (which has been disabled as of CL aosp/2543831) C) Consistency of the ConstantFolding pass, which helps to have a simpler mental model Bug: 278626992 Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Test: Locally compiled GMS and compared time to compile Change-Id: I5dc5f591557c8de0bc4d23dbfd0b91b5b7e56ab5
2023-11-30Constant fold DivideUnsigned intrinsic Santiago Aboy Solanes
Bug: 309886589 Test: art/test/testrunner/testrunner.py --host --64 -b --optimizing Change-Id: If60ccbea30b3fe16b86c1630409b540b05b14c56
2023-11-28Constant fold BitCount intrinsic Santiago Aboy Solanes
Bug: 309886589 Test: art/test/testrunner/testrunner.py --host --64 -b --optimizing Change-Id: Id0db658e1139886b7bffd63431a7cd2d4c75506d
2023-11-15Constant fold Reverse/ReverseBytes Santiago Aboy Solanes
Bug: 309886589 Test: art/test/testrunner/testrunner.py --host --64 -b --optimizing Change-Id: Ibdf60f579b44b5d2609e0e89b98a41dd0a8b7a89
2023-11-13Constant fold Highest/LowestOneBit Santiago Aboy Solanes
Bug: 309886589 Test: art/test/testrunner/testrunner.py --host --64 -b --optimizing Change-Id: I64b8889bcef2e4c65d85bffec560f4078a17648c
2023-11-10Constant fold NumberOfTrailing/LeadingZeros Santiago Aboy Solanes
Recognize intrinsics in constant folding and optimize the invoke away if the value is known at compile time. Bug: 309886589 Test: art/test/testrunner/testrunner.py --host --64 -b --optimizing Change-Id: I989af53c17f018eb6b3f790ee7d9d9e8255dfd41
2023-10-10Add optimization to simplify Select+Binary/Unary ops Santiago Aboy Solanes
We can simplify a Select + Binary/Unary Op if: * Both inputs to the Select instruction are constant, and * The Select instruction is not used in another instruction to avoid duplicating Selects. * In the case of Binary ops, both inputs can't be Select. Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Change-Id: Ic716155e9a8515126c2867bb1d54593fa63011ae
2023-04-27Optimizing: Add `HInstruction::As##type()`. Vladimir Marko
After the old implementation was renamed in https://android-review.googlesource.com/2526708 , we introduce a new function with the old name but new behavior, just `DCHECK()`-ing the instruction kind before casting down the pointer. We change appropriate calls from `As##type##OrNull()` to `As##type()` to avoid unncessary run-time checks and reduce the size of libart-compiler.so. Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Test: run-gtests.sh Test: testrunner.py --target --optimizing Bug: 181943478 Change-Id: I025681612a77ca2157fed4886ca47f2053975d4e
2023-04-27Optimizing: Rename `As##type` to `As##type##OrNull`. Vladimir Marko
The null type check in the current implementation of `HInstruction::As##type()` often cannot be optimized away by clang++. It is therefore beneficial to have two functions HInstruction::As##type() HInstruction::As##type##OrNull() where the first function never returns null but the second one can return null. The additional text "OrNull" shall also flag the possibility of yielding null to the developer which may help avoid bugs similar to what we have seen previously. This requires renaming the existing function that can return null and introducing new function that cannot. However, defining the new function `HInstruction::As##type()` in the same change as renaming the old one would risk introducing bugs by missing a rename. Therefore we simply rename the old function here and the new function shall be introduced in a separate change with all behavioral changes being explicit. Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Test: buildbot-build.sh --target Bug: 181943478 Change-Id: I4defd85038e28fe3506903ba3f33f723682b3298
2023-04-19Restrict the use of ConstantFolding's VisitIf Santiago Aboy Solanes
It was taking a lot of time for the improvement it got. We can get 99.99% of the improvement, with only one VisitIf call. This is roughly 20% of the compile time it used to take. Bug: 278626992 Fixes: 278626992 Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Change-Id: Icc00c9ad6a9eb4f4fd18677bcb65655cbbe9d027
2023-04-05Mark graph visitor classes as final. Vladimir Marko
This should improve optimization opportunities for clang++. Test: buildbot-build.sh Change-Id: Ib0c1ebfb157176a9063cca2ed465cff6fb280442
2023-04-03Optimize String's length/isEmpty for constant strings Santiago Aboy Solanes
If at compile time we know the length of the string, we can insert that constant into the graph. As part of this CL, I introduced VisitArrayLength in ConstantFolding. This is done because: * We are folding the instrucion into a constant so it makes logical sense, and * It plays better with isEmpty since that's an ArrayLength+Equal which can be dealt with in one ConstantFolding pass. As a note, there's VisitArrayLength in InstructionSimplifier which would make sense to also move to ConstantFolding. However, in doing so we get code size regressions. Added a TODO to see if we can deal with that in the future. Locally speed-compiling in a Pixel 5: * SystemServer: -4.38 KB (-0.01%) * SystemUIGoogle: -23.92KB (-0.09%) * AGSA: -217.66KB (-0.07%) Bug: 276416001 Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Change-Id: Ic2081d489482157b016762e0ec103319d3b9a46e
2023-03-21Optimize comparisons where lhs and rhs are the same Santiago Aboy Solanes
Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Change-Id: Ia9001e9f3a32c3979f78c76bc0b8b86fe6119ecd
2022-11-07Reland "Make compiler/optimizing/ symbols hidden." VladimĂ­r Marko
This reverts commit 0a51605ddd81635135463dab08b6f7c21b58ffb0. Reason for revert: Reland after some of the required work was merged in other CLs. Also address a TODO from the original CL to mark required symbols with EXPORT in `intrinsic_objects.h`. Also mark symbols in new files as HIDDEN. Bug: 186902856 Test: m test-art-host-gtest Test: testrunner.py --host --optimizing Change-Id: I936d448983928af23614ca82c2d0bf9a645e2c52
2022-08-17Reland "Propagating values from if clauses to its successors" Santiago Aboy Solanes
This reverts commit fa1034c563b44c4f557814c50e2678e14dcd1d13. Reason for revert: Relanding after float/double fix. In short, don't deal with floats/doubles since they bring a lot of edge cases e.g. if (f == 0.0f) { // f is not guaranteed to be 0.0f, e.g. it could be -0.0f. } Bug: 240543764 Change-Id: I400bdab71dba0934e6f1740538fe6e6c0a7bf5fc
2022-08-09Revert "Propagating values from if clauses to its successors" Santiago Aboy Solanes
This reverts commit c6b816ceb2b35300c937ef2e7d008598b6afba21. Reason for revert: Broke libcore test https://ci.chromium.org/ui/p/art/builders/ci/angler-armv7-ndebug/3179/overview Change-Id: I4f238bd20cc485e49078104e0225c373cac23415
2022-08-09Propagating values from if clauses to its successors Santiago Aboy Solanes
We have knowledge of the value of some variables at compile time due to the fact they are used in if clauses. For example: if (variable == constant) { // SSA `variable` guaranteed to be equal to constant here. } else { // No guarantees can be made here (except for booleans since // they only have two values). } Similarly with `variable != constant`. We can also apply this to boolean parameters e.g. void foo (boolean val) { if (val) { // `val` guaranteed to be true here. ... } ... } Test: art/test/testrunner/testrunner.py --host --64 --optimizing -b Change-Id: I55df0252d672870993d06e5ac92f5bba44d902bd
2019-10-14Revert "Make compiler/optimizing/ symbols hidden." Vladimir Marko
This reverts commit e2727154f25e0db9a5bb92af494d8e47b181dfcf. Reason for revert: Breaks ASAN tests (ODR violation). Bug: 142365358 Change-Id: I38103d74a1297256c81d90872b6902ff1e9ef7a4
2019-10-14Make compiler/optimizing/ symbols hidden. Vladimir Marko
Make symbols in compiler/optimizing hidden by a namespace attribute. The unit intrinsic_objects.{h,cc} is excluded as it is needed by dex2oat. As the symbols are no longer exported, gtests are now linked with the static version of the libartd-compiler library. libart-compiler.so size: - before: arm: 2396152 arm64: 3345280 - after: arm: 2016176 (-371KiB, -15.9%) arm64: 2874480 (-460KiB, -14.1%) Test: m test-art-host-gtest Test: testrunner.py --host --optimizing --jit Bug: 142365358 Change-Id: I1fb04a33351f53f00b389a1642e81a68e40912a8
2019-09-18[optimizing] Improve constant folding on logical and Balaram Makam
Constant fold a & ~a = ~a & a = 0. Test: added test to 442-checker-constant-folding Test: test-art-host,test-art-target Change-Id: Ib637c93e99ce22dd1ecd5684d05ce5ca4c9c823a
2018-08-28Use 'final' and 'override' specifiers directly in ART. Roland Levillain
Remove all uses of macros 'FINAL' and 'OVERRIDE' and replace them with 'final' and 'override' specifiers. Remove all definitions of these macros as well, which were located in these files: - libartbase/base/macros.h - test/913-heaps/heaps.cc - test/ti-agent/ti_macros.h ART is now using C++14; the 'final' and 'override' specifiers have been introduced in C++11. Test: mmma art Change-Id: I256c7758155a71a2940ef2574925a44076feeebf
2018-04-26Step 1 of 2: conditional passes. Aart Bik
Rationale: The change adds a return value to Run() in preparation of conditional pass execution. The value returned by Run() is best effort, returning false means no optimizations were applied or no useful information was obtained. I filled in a few cases with more exact information, others still just return true. In addition, it integrates inlining as a regular pass, avoiding the ugly "break" into optimizations1 and optimziations2. Bug: b/78171933, b/74026074 Test: test-art-host,target Change-Id: Ia39c5c83c01dcd79841e4b623917d61c754cf075
2017-11-30Type conversion elimination of constants Mingyao Yang
A better way of eliminating type conversion for constants. Test: run-test on host. 711-checker-type-conversion. Change-Id: I457bc091542a5ac4cc4e77cadb012ee7cb040ce8
2017-09-25ART: Introduce compiler data type. Vladimir Marko
Replace most uses of the runtime's Primitive in compiler with a new class DataType. This prepares for introducing new types, such as Uint8, that the runtime does not need to know about. Test: m test-art-host-gtest Test: testrunner.py --host Bug: 23964345 Change-Id: Iec2ad82454eec678fffcd8279a9746b90feb9b0c
2016-08-31Constant fold Equal/NotEqual between null and non-null. Vladimir Marko
Test: Add new test cases to 442-checker-constant-folding. Test: m test-art-host Change-Id: I14509d5e13d30a66b3c2ac3d76d514f58501c9ab
2016-03-18Clean up art::HConstant predicates. Roland Levillain
- Make the difference between arithmetic zero and zero-bit pattern non ambiguous. - Introduce Boolean predicates in art::HIntConstant for when they are used as Booleans. - Introduce aritmetic positive and negative zero predicates for floating-point constants. Bug: 27639313 Change-Id: Ia04ecc6f6aa7450136028c5362ed429760c883bd
2016-02-17Implement Optimizing's constant folding as a visitor. Roland Levillain
Refactor the logic of art::HConstantFolding::Run into a new visitor, art::HConstantFoldingVisitor. Change-Id: Id8d3c3826f6dff6cc2d18a01f6c48d79bde483b3
2016-02-17Extend constant folding to float and double operations. Roland Levillain
Change-Id: I2837064b2ceea587bc171fc520507f13355292c6
2015-11-23Revert "Add stats support for existing optimizations" Nicolas Geoffray
Breaks the build. Please ensure your changes build. This reverts commit 06241b1b07fb031b7d2cf55f4b78d3444d07cc2d. Change-Id: I68b18f99a9882719bf6654d3313531a7965b8483
2015-11-22Add stats support for existing optimizations Jean-Philippe Halimi
This patch adds support for the --dump-stats facility with existing optimizations. Change-Id: I68751b119a030952a11057cb651a3c63e87e73ea Signed-off-by: Jean-Philippe Halimi <jean-philippe.halimi@intel.com>
2015-10-29Simplify unsigned comparisons against zero (with unit tests). Aart Bik
Rationale: Such cases occurs a lot after dynamic bound check optimization (where the lower bound is test against upper bound). Removing this unnecessary test improves quality of code. Change-Id: I3e4dc9f9d799aad342e1c344013ac60fcc3073ac
2015-09-30Tidy up spelling Kenny Root
To match the directory "compiler/optimizing" change the spelling of comments. Change-Id: I816da4836ce86f4a44ddd2754d8e788d77a27de3
2015-05-13[optimizing] Fold HTypeConversion of constants Mark Mendell
While looking into optimizing long shifts on x86, I found that the compiler wasn't folding HTypeConversion of constants. Add simple conversions of constants, taking care of float/double values with NaNs and small/large values, ensuring Java conversion semantics. Add checker cases to see that constant folding of HTypeConversion is done. Ensure 422-type-conversion type conversion routiness don't get inlined to avoid compile time folding. Change-Id: I5a4eb376b64bc4e41bf908af5875bed312efb228 Signed-off-by: Mark Mendell <mark.p.mendell@intel.com>
2015-05-08Simplify floating-point comparisons with NaN in Optimizing. Roland Levillain
This change was suggested by Ian. Also, simplify some art::HFloatConstant and art::HDoubleConstant methods. Change-Id: I7908df23581a7f61c8ec79c290fe5f70798ac3be
2015-03-26ART: Force constants into the entry block David Brazdil
Optimizations such as GVN and BCE make the assumption that all constants are located in the entry block of the CFG, but not all passes adhere to this rule. This patch makes constructors of constants private and only accessible to friend classes - HGraph for int/long constants and SsaBuilder for float/double - which ensure that they are placed correctly and not duplicated. Note that the ArenaAllocatorAdapter was modified to not increment the ArenaAllocator's internal reference counter in order to allow for use of ArenaSafeMap inside an arena-allocated objects. Because their destructor is not called, the counter does not get decremented. Change-Id: I36a4fa29ae34fb905cdefd482ccbf386cff14166
2015-03-11Opt compiler: Basic simplification for arithmetic operations. Alexandre Rames
The optimisations in this patch do not look further than the inputs of each operation. Change-Id: Iddd0ab6b360b9e7bb042db22086d51a31be85530
2014-11-04ART: More warnings Andreas Gampe
Enable -Wno-conversion-null, -Wredundant-decls and -Wshadow in general, and -Wunused-but-set-parameter for GCC builds. Change-Id: I81bbdd762213444673c65d85edae594a523836e5
2014-10-21Constant folding on unary operations in the optimizing compiler. Roland Levillain
Change-Id: I4b77afa2a89f5ad2eedd4d6c0c6c382585419349
2014-10-17Revert "Revert "Introduce a class to implement optimization passes."" Roland Levillain
This reverts commit 1ddbf6d4b37979a9f11a203c12befd5ae8b65df4. Change-Id: I110a14668d1564ee0604dc958b91394b40da89fc
2014-10-01Revert "Introduce a class to implement optimization passes." Nicolas Geoffray
This reverts commit bf9cd7ba2118a75f5aa9b56241c4d5fa00dedeb8. Change-Id: I0a483446666c9c24c45925a5fc199debdefd8b3e
2014-10-01Introduce a class to implement optimization passes. Roland Levillain
- Add art::HOptimization. - Rename art::ConstantPropagation to art::HConstantFolding in compiler/optimizing/constant_folding.h to avoid name clashes with a class of the same name in compiler/dex/post_opt_passes.h. - Rename art::DeadCodeElimination to art::HDeadCodeElimination for consistency reasons. - Have art::HDeadCodeElimination and art::HConstantFolding derive from art::HOptimization. - Start to use these optimizations in art:OptimizingCompiler::TryCompile. Change-Id: Iaab350c122d87b2333b3760312b15c0592d7e010