Thumb2ITBlockPass.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //===-- Thumb2ITBlockPass.cpp - Insert Thumb-2 IT blocks ------------------===//
  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. #include "ARM.h"
  9. #include "ARMMachineFunctionInfo.h"
  10. #include "ARMSubtarget.h"
  11. #include "MCTargetDesc/ARMBaseInfo.h"
  12. #include "Thumb2InstrInfo.h"
  13. #include "llvm/ADT/SmallSet.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/ADT/Statistic.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/CodeGen/MachineBasicBlock.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/CodeGen/MachineInstr.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineInstrBundle.h"
  23. #include "llvm/CodeGen/MachineOperand.h"
  24. #include "llvm/IR/DebugLoc.h"
  25. #include "llvm/MC/MCInstrDesc.h"
  26. #include "llvm/MC/MCRegisterInfo.h"
  27. #include <cassert>
  28. #include <new>
  29. using namespace llvm;
  30. #define DEBUG_TYPE "thumb2-it"
  31. #define PASS_NAME "Thumb IT blocks insertion pass"
  32. STATISTIC(NumITs, "Number of IT blocks inserted");
  33. STATISTIC(NumMovedInsts, "Number of predicated instructions moved");
  34. using RegisterSet = SmallSet<unsigned, 4>;
  35. namespace {
  36. class Thumb2ITBlock : public MachineFunctionPass {
  37. public:
  38. static char ID;
  39. bool restrictIT;
  40. const Thumb2InstrInfo *TII;
  41. const TargetRegisterInfo *TRI;
  42. ARMFunctionInfo *AFI;
  43. Thumb2ITBlock() : MachineFunctionPass(ID) {}
  44. bool runOnMachineFunction(MachineFunction &Fn) override;
  45. MachineFunctionProperties getRequiredProperties() const override {
  46. return MachineFunctionProperties().set(
  47. MachineFunctionProperties::Property::NoVRegs);
  48. }
  49. StringRef getPassName() const override {
  50. return PASS_NAME;
  51. }
  52. private:
  53. bool MoveCopyOutOfITBlock(MachineInstr *MI,
  54. ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
  55. RegisterSet &Defs, RegisterSet &Uses);
  56. bool InsertITInstructions(MachineBasicBlock &Block);
  57. };
  58. char Thumb2ITBlock::ID = 0;
  59. } // end anonymous namespace
  60. INITIALIZE_PASS(Thumb2ITBlock, DEBUG_TYPE, PASS_NAME, false, false)
  61. /// TrackDefUses - Tracking what registers are being defined and used by
  62. /// instructions in the IT block. This also tracks "dependencies", i.e. uses
  63. /// in the IT block that are defined before the IT instruction.
  64. static void TrackDefUses(MachineInstr *MI, RegisterSet &Defs, RegisterSet &Uses,
  65. const TargetRegisterInfo *TRI) {
  66. using RegList = SmallVector<unsigned, 4>;
  67. RegList LocalDefs;
  68. RegList LocalUses;
  69. for (auto &MO : MI->operands()) {
  70. if (!MO.isReg())
  71. continue;
  72. Register Reg = MO.getReg();
  73. if (!Reg || Reg == ARM::ITSTATE || Reg == ARM::SP)
  74. continue;
  75. if (MO.isUse())
  76. LocalUses.push_back(Reg);
  77. else
  78. LocalDefs.push_back(Reg);
  79. }
  80. auto InsertUsesDefs = [&](RegList &Regs, RegisterSet &UsesDefs) {
  81. for (unsigned Reg : Regs)
  82. for (MCSubRegIterator Subreg(Reg, TRI, /*IncludeSelf=*/true);
  83. Subreg.isValid(); ++Subreg)
  84. UsesDefs.insert(*Subreg);
  85. };
  86. InsertUsesDefs(LocalDefs, Defs);
  87. InsertUsesDefs(LocalUses, Uses);
  88. }
  89. /// Clear kill flags for any uses in the given set. This will likely
  90. /// conservatively remove more kill flags than are necessary, but removing them
  91. /// is safer than incorrect kill flags remaining on instructions.
  92. static void ClearKillFlags(MachineInstr *MI, RegisterSet &Uses) {
  93. for (MachineOperand &MO : MI->operands()) {
  94. if (!MO.isReg() || MO.isDef() || !MO.isKill())
  95. continue;
  96. if (!Uses.count(MO.getReg()))
  97. continue;
  98. MO.setIsKill(false);
  99. }
  100. }
  101. static bool isCopy(MachineInstr *MI) {
  102. switch (MI->getOpcode()) {
  103. default:
  104. return false;
  105. case ARM::MOVr:
  106. case ARM::MOVr_TC:
  107. case ARM::tMOVr:
  108. case ARM::t2MOVr:
  109. return true;
  110. }
  111. }
  112. bool
  113. Thumb2ITBlock::MoveCopyOutOfITBlock(MachineInstr *MI,
  114. ARMCC::CondCodes CC, ARMCC::CondCodes OCC,
  115. RegisterSet &Defs, RegisterSet &Uses) {
  116. if (!isCopy(MI))
  117. return false;
  118. // llvm models select's as two-address instructions. That means a copy
  119. // is inserted before a t2MOVccr, etc. If the copy is scheduled in
  120. // between selects we would end up creating multiple IT blocks.
  121. assert(MI->getOperand(0).getSubReg() == 0 &&
  122. MI->getOperand(1).getSubReg() == 0 &&
  123. "Sub-register indices still around?");
  124. Register DstReg = MI->getOperand(0).getReg();
  125. Register SrcReg = MI->getOperand(1).getReg();
  126. // First check if it's safe to move it.
  127. if (Uses.count(DstReg) || Defs.count(SrcReg))
  128. return false;
  129. // If the CPSR is defined by this copy, then we don't want to move it. E.g.,
  130. // if we have:
  131. //
  132. // movs r1, r1
  133. // rsb r1, 0
  134. // movs r2, r2
  135. // rsb r2, 0
  136. //
  137. // we don't want this to be converted to:
  138. //
  139. // movs r1, r1
  140. // movs r2, r2
  141. // itt mi
  142. // rsb r1, 0
  143. // rsb r2, 0
  144. //
  145. const MCInstrDesc &MCID = MI->getDesc();
  146. if (MI->hasOptionalDef() &&
  147. MI->getOperand(MCID.getNumOperands() - 1).getReg() == ARM::CPSR)
  148. return false;
  149. // Then peek at the next instruction to see if it's predicated on CC or OCC.
  150. // If not, then there is nothing to be gained by moving the copy.
  151. MachineBasicBlock::iterator I = MI;
  152. ++I;
  153. MachineBasicBlock::iterator E = MI->getParent()->end();
  154. while (I != E && I->isDebugInstr())
  155. ++I;
  156. if (I != E) {
  157. Register NPredReg;
  158. ARMCC::CondCodes NCC = getITInstrPredicate(*I, NPredReg);
  159. if (NCC == CC || NCC == OCC)
  160. return true;
  161. }
  162. return false;
  163. }
  164. bool Thumb2ITBlock::InsertITInstructions(MachineBasicBlock &MBB) {
  165. bool Modified = false;
  166. RegisterSet Defs, Uses;
  167. MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
  168. while (MBBI != E) {
  169. MachineInstr *MI = &*MBBI;
  170. DebugLoc dl = MI->getDebugLoc();
  171. Register PredReg;
  172. ARMCC::CondCodes CC = getITInstrPredicate(*MI, PredReg);
  173. if (CC == ARMCC::AL) {
  174. ++MBBI;
  175. continue;
  176. }
  177. Defs.clear();
  178. Uses.clear();
  179. TrackDefUses(MI, Defs, Uses, TRI);
  180. // Insert an IT instruction.
  181. MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
  182. .addImm(CC);
  183. // Add implicit use of ITSTATE to IT block instructions.
  184. MI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/,
  185. true/*isImp*/, false/*isKill*/));
  186. MachineInstr *LastITMI = MI;
  187. MachineBasicBlock::iterator InsertPos = MIB.getInstr();
  188. ++MBBI;
  189. // Form IT block.
  190. ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
  191. unsigned Mask = 0, Pos = 3;
  192. // v8 IT blocks are limited to one conditional op unless -arm-no-restrict-it
  193. // is set: skip the loop
  194. if (!restrictIT) {
  195. // Branches, including tricky ones like LDM_RET, need to end an IT
  196. // block so check the instruction we just put in the block.
  197. for (; MBBI != E && Pos &&
  198. (!MI->isBranch() && !MI->isReturn()) ; ++MBBI) {
  199. if (MBBI->isDebugInstr())
  200. continue;
  201. MachineInstr *NMI = &*MBBI;
  202. MI = NMI;
  203. Register NPredReg;
  204. ARMCC::CondCodes NCC = getITInstrPredicate(*NMI, NPredReg);
  205. if (NCC == CC || NCC == OCC) {
  206. Mask |= ((NCC ^ CC) & 1) << Pos;
  207. // Add implicit use of ITSTATE.
  208. NMI->addOperand(MachineOperand::CreateReg(ARM::ITSTATE, false/*ifDef*/,
  209. true/*isImp*/, false/*isKill*/));
  210. LastITMI = NMI;
  211. } else {
  212. if (NCC == ARMCC::AL &&
  213. MoveCopyOutOfITBlock(NMI, CC, OCC, Defs, Uses)) {
  214. --MBBI;
  215. MBB.remove(NMI);
  216. MBB.insert(InsertPos, NMI);
  217. ClearKillFlags(MI, Uses);
  218. ++NumMovedInsts;
  219. continue;
  220. }
  221. break;
  222. }
  223. TrackDefUses(NMI, Defs, Uses, TRI);
  224. --Pos;
  225. }
  226. }
  227. // Finalize IT mask.
  228. Mask |= (1 << Pos);
  229. MIB.addImm(Mask);
  230. // Last instruction in IT block kills ITSTATE.
  231. LastITMI->findRegisterUseOperand(ARM::ITSTATE)->setIsKill();
  232. // Finalize the bundle.
  233. finalizeBundle(MBB, InsertPos.getInstrIterator(),
  234. ++LastITMI->getIterator());
  235. Modified = true;
  236. ++NumITs;
  237. }
  238. return Modified;
  239. }
  240. bool Thumb2ITBlock::runOnMachineFunction(MachineFunction &Fn) {
  241. const ARMSubtarget &STI =
  242. static_cast<const ARMSubtarget &>(Fn.getSubtarget());
  243. if (!STI.isThumb2())
  244. return false;
  245. AFI = Fn.getInfo<ARMFunctionInfo>();
  246. TII = static_cast<const Thumb2InstrInfo *>(STI.getInstrInfo());
  247. TRI = STI.getRegisterInfo();
  248. restrictIT = STI.restrictIT();
  249. if (!AFI->isThumbFunction())
  250. return false;
  251. bool Modified = false;
  252. for (auto &MBB : Fn )
  253. Modified |= InsertITInstructions(MBB);
  254. if (Modified)
  255. AFI->setHasITBlocks(true);
  256. return Modified;
  257. }
  258. /// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks
  259. /// insertion pass.
  260. FunctionPass *llvm::createThumb2ITBlockPass() { return new Thumb2ITBlock(); }