Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:24

0001 //===- llvm/CodeGen/GlobalISel/GISelKnownBits.h ---------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 /// Provides analysis for querying information about KnownBits during GISel
0010 /// passes.
0011 ///
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CODEGEN_GLOBALISEL_GISELKNOWNBITS_H
0015 #define LLVM_CODEGEN_GLOBALISEL_GISELKNOWNBITS_H
0016 
0017 #include "llvm/ADT/DenseMap.h"
0018 #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
0019 #include "llvm/CodeGen/MachineFunctionPass.h"
0020 #include "llvm/CodeGen/Register.h"
0021 #include "llvm/InitializePasses.h"
0022 #include "llvm/Support/KnownBits.h"
0023 
0024 namespace llvm {
0025 
0026 class TargetLowering;
0027 class DataLayout;
0028 
0029 class GISelKnownBits : public GISelChangeObserver {
0030   MachineFunction &MF;
0031   MachineRegisterInfo &MRI;
0032   const TargetLowering &TL;
0033   const DataLayout &DL;
0034   unsigned MaxDepth;
0035   /// Cache maintained during a computeKnownBits request.
0036   SmallDenseMap<Register, KnownBits, 16> ComputeKnownBitsCache;
0037 
0038   void computeKnownBitsMin(Register Src0, Register Src1, KnownBits &Known,
0039                            const APInt &DemandedElts,
0040                            unsigned Depth = 0);
0041 
0042   unsigned computeNumSignBitsMin(Register Src0, Register Src1,
0043                                  const APInt &DemandedElts, unsigned Depth = 0);
0044 
0045 public:
0046   GISelKnownBits(MachineFunction &MF, unsigned MaxDepth = 6);
0047   virtual ~GISelKnownBits() = default;
0048 
0049   const MachineFunction &getMachineFunction() const {
0050     return MF;
0051   }
0052 
0053   const DataLayout &getDataLayout() const {
0054     return DL;
0055   }
0056 
0057   virtual void computeKnownBitsImpl(Register R, KnownBits &Known,
0058                                     const APInt &DemandedElts,
0059                                     unsigned Depth = 0);
0060 
0061   unsigned computeNumSignBits(Register R, const APInt &DemandedElts,
0062                               unsigned Depth = 0);
0063   unsigned computeNumSignBits(Register R, unsigned Depth = 0);
0064 
0065   // KnownBitsAPI
0066   KnownBits getKnownBits(Register R);
0067   KnownBits getKnownBits(Register R, const APInt &DemandedElts,
0068                          unsigned Depth = 0);
0069 
0070   // Calls getKnownBits for first operand def of MI.
0071   KnownBits getKnownBits(MachineInstr &MI);
0072   APInt getKnownZeroes(Register R);
0073   APInt getKnownOnes(Register R);
0074 
0075   /// \return true if 'V & Mask' is known to be zero in DemandedElts. We use
0076   /// this predicate to simplify operations downstream.
0077   /// Mask is known to be zero for bits that V cannot have.
0078   bool maskedValueIsZero(Register Val, const APInt &Mask) {
0079     return Mask.isSubsetOf(getKnownBits(Val).Zero);
0080   }
0081 
0082   /// \return true if the sign bit of Op is known to be zero.  We use this
0083   /// predicate to simplify operations downstream.
0084   bool signBitIsZero(Register Op);
0085 
0086   static void computeKnownBitsForAlignment(KnownBits &Known,
0087                                            Align Alignment) {
0088     // The low bits are known zero if the pointer is aligned.
0089     Known.Zero.setLowBits(Log2(Alignment));
0090   }
0091 
0092   /// \return The known alignment for the pointer-like value \p R.
0093   Align computeKnownAlignment(Register R, unsigned Depth = 0);
0094 
0095   // Observer API. No-op for non-caching implementation.
0096   void erasingInstr(MachineInstr &MI) override {}
0097   void createdInstr(MachineInstr &MI) override {}
0098   void changingInstr(MachineInstr &MI) override {}
0099   void changedInstr(MachineInstr &MI) override {}
0100 
0101 protected:
0102   unsigned getMaxDepth() const { return MaxDepth; }
0103 };
0104 
0105 /// To use KnownBitsInfo analysis in a pass,
0106 /// KnownBitsInfo &Info = getAnalysis<GISelKnownBitsInfoAnalysis>().get(MF);
0107 /// Add to observer if the Info is caching.
0108 /// WrapperObserver.addObserver(Info);
0109 
0110 /// Eventually add other features such as caching/ser/deserializing
0111 /// to MIR etc. Those implementations can derive from GISelKnownBits
0112 /// and override computeKnownBitsImpl.
0113 class GISelKnownBitsAnalysis : public MachineFunctionPass {
0114   std::unique_ptr<GISelKnownBits> Info;
0115 
0116 public:
0117   static char ID;
0118   GISelKnownBitsAnalysis() : MachineFunctionPass(ID) {
0119     initializeGISelKnownBitsAnalysisPass(*PassRegistry::getPassRegistry());
0120   }
0121   GISelKnownBits &get(MachineFunction &MF);
0122   void getAnalysisUsage(AnalysisUsage &AU) const override;
0123   bool runOnMachineFunction(MachineFunction &MF) override;
0124   void releaseMemory() override { Info.reset(); }
0125 };
0126 } // namespace llvm
0127 
0128 #endif // LLVM_CODEGEN_GLOBALISEL_GISELKNOWNBITS_H