ARMInstrInfo.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //===-- ARMInstrInfo.cpp - ARM Instruction Information --------------------===//
  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. // This file contains the ARM implementation of the TargetInstrInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ARMInstrInfo.h"
  13. #include "ARM.h"
  14. #include "ARMConstantPoolValue.h"
  15. #include "ARMMachineFunctionInfo.h"
  16. #include "ARMTargetMachine.h"
  17. #include "MCTargetDesc/ARMAddressingModes.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/CodeGen/LiveVariables.h"
  20. #include "llvm/CodeGen/MachineFrameInfo.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineJumpTableInfo.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/IR/Function.h"
  25. #include "llvm/IR/GlobalVariable.h"
  26. #include "llvm/MC/MCAsmInfo.h"
  27. #include "llvm/MC/MCInst.h"
  28. using namespace llvm;
  29. ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI) : ARMBaseInstrInfo(STI) {}
  30. /// Return the noop instruction to use for a noop.
  31. MCInst ARMInstrInfo::getNop() const {
  32. MCInst NopInst;
  33. if (hasNOP()) {
  34. NopInst.setOpcode(ARM::HINT);
  35. NopInst.addOperand(MCOperand::createImm(0));
  36. NopInst.addOperand(MCOperand::createImm(ARMCC::AL));
  37. NopInst.addOperand(MCOperand::createReg(0));
  38. } else {
  39. NopInst.setOpcode(ARM::MOVr);
  40. NopInst.addOperand(MCOperand::createReg(ARM::R0));
  41. NopInst.addOperand(MCOperand::createReg(ARM::R0));
  42. NopInst.addOperand(MCOperand::createImm(ARMCC::AL));
  43. NopInst.addOperand(MCOperand::createReg(0));
  44. NopInst.addOperand(MCOperand::createReg(0));
  45. }
  46. return NopInst;
  47. }
  48. unsigned ARMInstrInfo::getUnindexedOpcode(unsigned Opc) const {
  49. switch (Opc) {
  50. default:
  51. break;
  52. case ARM::LDR_PRE_IMM:
  53. case ARM::LDR_PRE_REG:
  54. case ARM::LDR_POST_IMM:
  55. case ARM::LDR_POST_REG:
  56. return ARM::LDRi12;
  57. case ARM::LDRH_PRE:
  58. case ARM::LDRH_POST:
  59. return ARM::LDRH;
  60. case ARM::LDRB_PRE_IMM:
  61. case ARM::LDRB_PRE_REG:
  62. case ARM::LDRB_POST_IMM:
  63. case ARM::LDRB_POST_REG:
  64. return ARM::LDRBi12;
  65. case ARM::LDRSH_PRE:
  66. case ARM::LDRSH_POST:
  67. return ARM::LDRSH;
  68. case ARM::LDRSB_PRE:
  69. case ARM::LDRSB_POST:
  70. return ARM::LDRSB;
  71. case ARM::STR_PRE_IMM:
  72. case ARM::STR_PRE_REG:
  73. case ARM::STR_POST_IMM:
  74. case ARM::STR_POST_REG:
  75. return ARM::STRi12;
  76. case ARM::STRH_PRE:
  77. case ARM::STRH_POST:
  78. return ARM::STRH;
  79. case ARM::STRB_PRE_IMM:
  80. case ARM::STRB_PRE_REG:
  81. case ARM::STRB_POST_IMM:
  82. case ARM::STRB_POST_REG:
  83. return ARM::STRBi12;
  84. }
  85. return 0;
  86. }
  87. void ARMInstrInfo::expandLoadStackGuard(MachineBasicBlock::iterator MI) const {
  88. MachineFunction &MF = *MI->getParent()->getParent();
  89. const ARMSubtarget &Subtarget = MF.getSubtarget<ARMSubtarget>();
  90. const TargetMachine &TM = MF.getTarget();
  91. Module &M = *MF.getFunction().getParent();
  92. if (M.getStackProtectorGuard() == "tls") {
  93. expandLoadStackGuardBase(MI, ARM::MRC, ARM::LDRi12);
  94. return;
  95. }
  96. const GlobalValue *GV =
  97. cast<GlobalValue>((*MI->memoperands_begin())->getValue());
  98. if (!Subtarget.useMovt() || Subtarget.isGVInGOT(GV)) {
  99. if (TM.isPositionIndependent())
  100. expandLoadStackGuardBase(MI, ARM::LDRLIT_ga_pcrel, ARM::LDRi12);
  101. else
  102. expandLoadStackGuardBase(MI, ARM::LDRLIT_ga_abs, ARM::LDRi12);
  103. return;
  104. }
  105. if (!TM.isPositionIndependent()) {
  106. expandLoadStackGuardBase(MI, ARM::MOVi32imm, ARM::LDRi12);
  107. return;
  108. }
  109. if (!Subtarget.isGVIndirectSymbol(GV)) {
  110. expandLoadStackGuardBase(MI, ARM::MOV_ga_pcrel, ARM::LDRi12);
  111. return;
  112. }
  113. MachineBasicBlock &MBB = *MI->getParent();
  114. DebugLoc DL = MI->getDebugLoc();
  115. Register Reg = MI->getOperand(0).getReg();
  116. MachineInstrBuilder MIB;
  117. MIB = BuildMI(MBB, MI, DL, get(ARM::MOV_ga_pcrel_ldr), Reg)
  118. .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);
  119. auto Flags = MachineMemOperand::MOLoad |
  120. MachineMemOperand::MODereferenceable |
  121. MachineMemOperand::MOInvariant;
  122. MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(
  123. MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, Align(4));
  124. MIB.addMemOperand(MMO);
  125. BuildMI(MBB, MI, DL, get(ARM::LDRi12), Reg)
  126. .addReg(Reg, RegState::Kill)
  127. .addImm(0)
  128. .cloneMemRefs(*MI)
  129. .add(predOps(ARMCC::AL));
  130. }