InstructionSelector.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/MachineBasicBlock.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineInstr.h"
  18. #include "llvm/CodeGen/MachineOperand.h"
  19. #include "llvm/CodeGen/MachineRegisterInfo.h"
  20. #include "llvm/CodeGen/TargetRegisterInfo.h"
  21. #include "llvm/MC/MCInstrDesc.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include <cassert>
  25. #define DEBUG_TYPE "instructionselector"
  26. using namespace llvm;
  27. InstructionSelector::MatcherState::MatcherState(unsigned MaxRenderers)
  28. : Renderers(MaxRenderers) {}
  29. InstructionSelector::InstructionSelector() = default;
  30. bool InstructionSelector::isOperandImmEqual(
  31. const MachineOperand &MO, int64_t Value,
  32. const MachineRegisterInfo &MRI) const {
  33. if (MO.isReg() && MO.getReg())
  34. if (auto VRegVal = getIConstantVRegValWithLookThrough(MO.getReg(), MRI))
  35. return VRegVal->Value.getSExtValue() == Value;
  36. return false;
  37. }
  38. bool InstructionSelector::isBaseWithConstantOffset(
  39. const MachineOperand &Root, const MachineRegisterInfo &MRI) const {
  40. if (!Root.isReg())
  41. return false;
  42. MachineInstr *RootI = MRI.getVRegDef(Root.getReg());
  43. if (RootI->getOpcode() != TargetOpcode::G_PTR_ADD)
  44. return false;
  45. MachineOperand &RHS = RootI->getOperand(2);
  46. MachineInstr *RHSI = MRI.getVRegDef(RHS.getReg());
  47. if (RHSI->getOpcode() != TargetOpcode::G_CONSTANT)
  48. return false;
  49. return true;
  50. }
  51. bool InstructionSelector::isObviouslySafeToFold(MachineInstr &MI,
  52. MachineInstr &IntoMI) const {
  53. // Immediate neighbours are already folded.
  54. if (MI.getParent() == IntoMI.getParent() &&
  55. std::next(MI.getIterator()) == IntoMI.getIterator())
  56. return true;
  57. return !MI.mayLoadOrStore() && !MI.mayRaiseFPException() &&
  58. !MI.hasUnmodeledSideEffects() && MI.implicit_operands().empty();
  59. }