InstructionSelector.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //===- llvm/CodeGen/GlobalISel/InstructionSelector.cpp --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file
  10. /// This file implements the InstructionSelector class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
  14. #include "llvm/CodeGen/GlobalISel/Utils.h"
  15. #include "llvm/CodeGen/MachineInstr.h"
  16. #include "llvm/CodeGen/MachineOperand.h"
  17. #include "llvm/CodeGen/MachineRegisterInfo.h"
  18. #define DEBUG_TYPE "instructionselector"
  19. using namespace llvm;
  20. InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers)
  21. : Renderers(MaxRenderers) {}
  22. InstructionSelector::InstructionSelector() = default;
  23. bool InstructionSelector::isOperandImmEqual(
  24. const MachineOperand &MO, int64_t Value,
  25. const MachineRegisterInfo &MRI) const {
  26. if (MO.isReg() && MO.getReg())
  27. if (auto VRegVal = getIConstantVRegValWithLookThrough(MO.getReg(), MRI))
  28. return VRegVal->Value.getSExtValue() == Value;
  29. return false;
  30. }
  31. bool InstructionSelector::isBaseWithConstantOffset(
  32. const MachineOperand &Root, const MachineRegisterInfo &MRI) const {
  33. if (!Root.isReg())
  34. return false;
  35. MachineInstr *RootI = MRI.getVRegDef(Root.getReg());
  36. if (RootI->getOpcode() != TargetOpcode::G_PTR_ADD)
  37. return false;
  38. MachineOperand &RHS = RootI->getOperand(2);
  39. MachineInstr *RHSI = MRI.getVRegDef(RHS.getReg());
  40. if (RHSI->getOpcode() != TargetOpcode::G_CONSTANT)
  41. return false;
  42. return true;
  43. }
  44. bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI,
  45. MachineInstr &IntoMI) const {
  46. // Immediate neighbours are already folded.
  47. if (MI.getParent() == IntoMI.getParent() &&
  48. std::next(MI.getIterator()) == IntoMI.getIterator())
  49. return true;
  50. // Convergent instructions cannot be moved in the CFG.
  51. if (MI.isConvergent() && MI.getParent() != IntoMI.getParent())
  52. return false;
  53. return !MI.mayLoadOrStore() && !MI.mayRaiseFPException() &&
  54. !MI.hasUnmodeledSideEffects() && MI.implicit_operands().empty();
  55. }