101 lines · plain
1Known Bits Analysis2===================3 4The Known Bits Analysis pass makes information about the known values of bits5available to other passes to enable transformations like those in the examples6below. The information is lazily computed so you should only pay for what you7use.8 9Examples10--------11 12A simple example is that transforming::13 14 a + 115 16into::17 18 a | 119 20is only valid when the addition doesn't carry. In other words it's only valid21if ``a & 1`` is zero.22 23Another example is:24 25.. code-block:: none26 27 %1:(s32) = G_CONSTANT i32 0xFF028 %2:(s32) = G_AND %0, %129 %3:(s32) = G_CONSTANT i32 0x0FF30 %4:(s32) = G_AND %2, %331 32We can use the constants and the definition of ``G_AND`` to determine the known33bits:34 35.. code-block:: none36 37 ; %0 = 0x????????38 %1:(s32) = G_CONSTANT i32 0xFF0 ; %1 = 0x00000FF039 %2:(s32) = G_AND %0, %1 ; %2 = 0x00000??040 %3:(s32) = G_CONSTANT i32 0x0FF ; %3 = 0x000000FF41 %4:(s32) = G_AND %2, %3 ; %4 = 0x000000?042 43and then use this to simplify the expression:44 45.. code-block:: none46 47 ; %0 = 0x????????48 %5:(s32) = G_CONSTANT i32 0x0F0 ; %5 = 0x00000FF049 %4:(s32) = G_AND %0, %5 ; %4 = 0x000000?050 51Note that ``%4`` still has the same known bits as before the transformation.52Many transformations share this property. The main exception being when the53transform causes undefined bits to become defined to either zero, one, or54defined but unknown.55 56Usage57-----58 59To use Known Bits Analysis in a pass, first include the header and register the60dependency with ``INITIALIZE_PASS_DEPENDENCY``.61 62.. code-block:: c++63 64 #include "llvm/CodeGen/GlobalISel/GISelValueTracking.h"65 66 ...67 68 INITIALIZE_PASS_BEGIN(...)69 INITIALIZE_PASS_DEPENDENCY(GISelValueTrackingAnalysisLegacy)70 INITIALIZE_PASS_END(...)71 72and require the pass in ``getAnalysisUsage``.73 74.. code-block:: c++75 76 void MyPass::getAnalysisUsage(AnalysisUsage &AU) const {77 AU.addRequired<GISelValueTrackingAnalysisLegacy>();78 // Optional: If your pass preserves known bits analysis (many do) then79 // indicate that it's preserved for re-use by another pass here.80 AU.addPreserved<GISelValueTrackingAnalysisLegacy>();81 }82 83Then it's just a matter of fetching the analysis and using it:84 85.. code-block:: c++86 87 bool MyPass::runOnMachineFunction(MachineFunction &MF) {88 ...89 GISelValueTracking &VT = getAnalysis<GISelValueTrackingAnalysisLegacy>().get(MF);90 ...91 MachineInstr *MI = ...;92 KnownBits Known = VT->getKnownBits(MI->getOperand(0).getReg());93 if (Known.Zeros & 1) {94 // Bit 0 is known to be zero95 }96 ...97 }98 99There are many more API's beyond ``getKnownBits()``. See the `API reference100<https://llvm.org/doxygen>`_ for more information101