MVEVPTBlockPass.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //===-- MVEVPTBlockPass.cpp - Insert MVE VPT 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 "llvm/Support/Debug.h"
  28. #include <cassert>
  29. #include <new>
  30. using namespace llvm;
  31. #define DEBUG_TYPE "arm-mve-vpt"
  32. namespace {
  33. class MVEVPTBlock : public MachineFunctionPass {
  34. public:
  35. static char ID;
  36. const Thumb2InstrInfo *TII;
  37. const TargetRegisterInfo *TRI;
  38. MVEVPTBlock() : MachineFunctionPass(ID) {}
  39. bool runOnMachineFunction(MachineFunction &Fn) override;
  40. MachineFunctionProperties getRequiredProperties() const override {
  41. return MachineFunctionProperties().set(
  42. MachineFunctionProperties::Property::NoVRegs);
  43. }
  44. StringRef getPassName() const override {
  45. return "MVE VPT block insertion pass";
  46. }
  47. private:
  48. bool InsertVPTBlocks(MachineBasicBlock &MBB);
  49. };
  50. char MVEVPTBlock::ID = 0;
  51. } // end anonymous namespace
  52. INITIALIZE_PASS(MVEVPTBlock, DEBUG_TYPE, "ARM MVE VPT block pass", false, false)
  53. static MachineInstr *findVCMPToFoldIntoVPST(MachineBasicBlock::iterator MI,
  54. const TargetRegisterInfo *TRI,
  55. unsigned &NewOpcode) {
  56. // Search backwards to the instruction that defines VPR. This may or not
  57. // be a VCMP, we check that after this loop. If we find another instruction
  58. // that reads cpsr, we return nullptr.
  59. MachineBasicBlock::iterator CmpMI = MI;
  60. while (CmpMI != MI->getParent()->begin()) {
  61. --CmpMI;
  62. if (CmpMI->modifiesRegister(ARM::VPR, TRI))
  63. break;
  64. if (CmpMI->readsRegister(ARM::VPR, TRI))
  65. break;
  66. }
  67. if (CmpMI == MI)
  68. return nullptr;
  69. NewOpcode = VCMPOpcodeToVPT(CmpMI->getOpcode());
  70. if (NewOpcode == 0)
  71. return nullptr;
  72. // Search forward from CmpMI to MI, checking if either register was def'd
  73. if (registerDefinedBetween(CmpMI->getOperand(1).getReg(), std::next(CmpMI),
  74. MI, TRI))
  75. return nullptr;
  76. if (registerDefinedBetween(CmpMI->getOperand(2).getReg(), std::next(CmpMI),
  77. MI, TRI))
  78. return nullptr;
  79. return &*CmpMI;
  80. }
  81. // Advances Iter past a block of predicated instructions.
  82. // Returns true if it successfully skipped the whole block of predicated
  83. // instructions. Returns false when it stopped early (due to MaxSteps), or if
  84. // Iter didn't point to a predicated instruction.
  85. static bool StepOverPredicatedInstrs(MachineBasicBlock::instr_iterator &Iter,
  86. MachineBasicBlock::instr_iterator EndIter,
  87. unsigned MaxSteps,
  88. unsigned &NumInstrsSteppedOver) {
  89. ARMVCC::VPTCodes NextPred = ARMVCC::None;
  90. Register PredReg;
  91. NumInstrsSteppedOver = 0;
  92. while (Iter != EndIter) {
  93. if (Iter->isDebugInstr()) {
  94. // Skip debug instructions
  95. ++Iter;
  96. continue;
  97. }
  98. NextPred = getVPTInstrPredicate(*Iter, PredReg);
  99. assert(NextPred != ARMVCC::Else &&
  100. "VPT block pass does not expect Else preds");
  101. if (NextPred == ARMVCC::None || MaxSteps == 0)
  102. break;
  103. --MaxSteps;
  104. ++Iter;
  105. ++NumInstrsSteppedOver;
  106. };
  107. return NumInstrsSteppedOver != 0 &&
  108. (NextPred == ARMVCC::None || Iter == EndIter);
  109. }
  110. // Returns true if at least one instruction in the range [Iter, End) defines
  111. // or kills VPR.
  112. static bool IsVPRDefinedOrKilledByBlock(MachineBasicBlock::iterator Iter,
  113. MachineBasicBlock::iterator End) {
  114. for (; Iter != End; ++Iter)
  115. if (Iter->definesRegister(ARM::VPR) || Iter->killsRegister(ARM::VPR))
  116. return true;
  117. return false;
  118. }
  119. // Creates a T, TT, TTT or TTTT BlockMask depending on BlockSize.
  120. static ARM::PredBlockMask GetInitialBlockMask(unsigned BlockSize) {
  121. switch (BlockSize) {
  122. case 1:
  123. return ARM::PredBlockMask::T;
  124. case 2:
  125. return ARM::PredBlockMask::TT;
  126. case 3:
  127. return ARM::PredBlockMask::TTT;
  128. case 4:
  129. return ARM::PredBlockMask::TTTT;
  130. default:
  131. llvm_unreachable("Invalid BlockSize!");
  132. }
  133. }
  134. // Given an iterator (Iter) that points at an instruction with a "Then"
  135. // predicate, tries to create the largest block of continuous predicated
  136. // instructions possible, and returns the VPT Block Mask of that block.
  137. //
  138. // This will try to perform some minor optimization in order to maximize the
  139. // size of the block.
  140. static ARM::PredBlockMask
  141. CreateVPTBlock(MachineBasicBlock::instr_iterator &Iter,
  142. MachineBasicBlock::instr_iterator EndIter,
  143. SmallVectorImpl<MachineInstr *> &DeadInstructions) {
  144. MachineBasicBlock::instr_iterator BlockBeg = Iter;
  145. (void)BlockBeg;
  146. assert(getVPTInstrPredicate(*Iter) == ARMVCC::Then &&
  147. "Expected a Predicated Instruction");
  148. LLVM_DEBUG(dbgs() << "VPT block created for: "; Iter->dump());
  149. unsigned BlockSize;
  150. StepOverPredicatedInstrs(Iter, EndIter, 4, BlockSize);
  151. LLVM_DEBUG(for (MachineBasicBlock::instr_iterator AddedInstIter =
  152. std::next(BlockBeg);
  153. AddedInstIter != Iter; ++AddedInstIter) {
  154. if (AddedInstIter->isDebugInstr())
  155. continue;
  156. dbgs() << " adding: ";
  157. AddedInstIter->dump();
  158. });
  159. // Generate the initial BlockMask
  160. ARM::PredBlockMask BlockMask = GetInitialBlockMask(BlockSize);
  161. // Remove VPNOTs while there's still room in the block, so we can make the
  162. // largest block possible.
  163. ARMVCC::VPTCodes CurrentPredicate = ARMVCC::Else;
  164. while (BlockSize < 4 && Iter != EndIter &&
  165. Iter->getOpcode() == ARM::MVE_VPNOT) {
  166. // Try to skip all of the predicated instructions after the VPNOT, stopping
  167. // after (4 - BlockSize). If we can't skip them all, stop.
  168. unsigned ElseInstCnt = 0;
  169. MachineBasicBlock::instr_iterator VPNOTBlockEndIter = std::next(Iter);
  170. if (!StepOverPredicatedInstrs(VPNOTBlockEndIter, EndIter, (4 - BlockSize),
  171. ElseInstCnt))
  172. break;
  173. // Check if this VPNOT can be removed or not: It can only be removed if at
  174. // least one of the predicated instruction that follows it kills or sets
  175. // VPR.
  176. if (!IsVPRDefinedOrKilledByBlock(Iter, VPNOTBlockEndIter))
  177. break;
  178. LLVM_DEBUG(dbgs() << " removing VPNOT: "; Iter->dump());
  179. // Record the new size of the block
  180. BlockSize += ElseInstCnt;
  181. assert(BlockSize <= 4 && "Block is too large!");
  182. // Record the VPNot to remove it later.
  183. DeadInstructions.push_back(&*Iter);
  184. ++Iter;
  185. // Replace the predicates of the instructions we're adding.
  186. // Note that we are using "Iter" to iterate over the block so we can update
  187. // it at the same time.
  188. for (; Iter != VPNOTBlockEndIter; ++Iter) {
  189. if (Iter->isDebugInstr())
  190. continue;
  191. // Find the register in which the predicate is
  192. int OpIdx = findFirstVPTPredOperandIdx(*Iter);
  193. assert(OpIdx != -1);
  194. // Change the predicate and update the mask
  195. Iter->getOperand(OpIdx).setImm(CurrentPredicate);
  196. BlockMask = expandPredBlockMask(BlockMask, CurrentPredicate);
  197. LLVM_DEBUG(dbgs() << " adding : "; Iter->dump());
  198. }
  199. CurrentPredicate =
  200. (CurrentPredicate == ARMVCC::Then ? ARMVCC::Else : ARMVCC::Then);
  201. }
  202. return BlockMask;
  203. }
  204. bool MVEVPTBlock::InsertVPTBlocks(MachineBasicBlock &Block) {
  205. bool Modified = false;
  206. MachineBasicBlock::instr_iterator MBIter = Block.instr_begin();
  207. MachineBasicBlock::instr_iterator EndIter = Block.instr_end();
  208. SmallVector<MachineInstr *, 4> DeadInstructions;
  209. while (MBIter != EndIter) {
  210. MachineInstr *MI = &*MBIter;
  211. Register PredReg;
  212. DebugLoc DL = MI->getDebugLoc();
  213. ARMVCC::VPTCodes Pred = getVPTInstrPredicate(*MI, PredReg);
  214. // The idea of the predicate is that None, Then and Else are for use when
  215. // handling assembly language: they correspond to the three possible
  216. // suffixes "", "t" and "e" on the mnemonic. So when instructions are read
  217. // from assembly source or disassembled from object code, you expect to
  218. // see a mixture whenever there's a long VPT block. But in code
  219. // generation, we hope we'll never generate an Else as input to this pass.
  220. assert(Pred != ARMVCC::Else && "VPT block pass does not expect Else preds");
  221. if (Pred == ARMVCC::None) {
  222. ++MBIter;
  223. continue;
  224. }
  225. ARM::PredBlockMask BlockMask =
  226. CreateVPTBlock(MBIter, EndIter, DeadInstructions);
  227. // Search back for a VCMP that can be folded to create a VPT, or else
  228. // create a VPST directly
  229. MachineInstrBuilder MIBuilder;
  230. unsigned NewOpcode;
  231. LLVM_DEBUG(dbgs() << " final block mask: " << (unsigned)BlockMask << "\n");
  232. if (MachineInstr *VCMP = findVCMPToFoldIntoVPST(MI, TRI, NewOpcode)) {
  233. LLVM_DEBUG(dbgs() << " folding VCMP into VPST: "; VCMP->dump());
  234. MIBuilder = BuildMI(Block, MI, DL, TII->get(NewOpcode));
  235. MIBuilder.addImm((uint64_t)BlockMask);
  236. MIBuilder.add(VCMP->getOperand(1));
  237. MIBuilder.add(VCMP->getOperand(2));
  238. MIBuilder.add(VCMP->getOperand(3));
  239. // We need to remove any kill flags between the original VCMP and the new
  240. // insertion point.
  241. for (MachineInstr &MII :
  242. make_range(VCMP->getIterator(), MI->getIterator())) {
  243. MII.clearRegisterKills(VCMP->getOperand(1).getReg(), TRI);
  244. MII.clearRegisterKills(VCMP->getOperand(2).getReg(), TRI);
  245. }
  246. VCMP->eraseFromParent();
  247. } else {
  248. MIBuilder = BuildMI(Block, MI, DL, TII->get(ARM::MVE_VPST));
  249. MIBuilder.addImm((uint64_t)BlockMask);
  250. }
  251. // Erase all dead instructions (VPNOT's). Do that now so that they do not
  252. // mess with the bundle creation.
  253. for (MachineInstr *DeadMI : DeadInstructions)
  254. DeadMI->eraseFromParent();
  255. DeadInstructions.clear();
  256. finalizeBundle(
  257. Block, MachineBasicBlock::instr_iterator(MIBuilder.getInstr()), MBIter);
  258. Modified = true;
  259. }
  260. return Modified;
  261. }
  262. bool MVEVPTBlock::runOnMachineFunction(MachineFunction &Fn) {
  263. const ARMSubtarget &STI = Fn.getSubtarget<ARMSubtarget>();
  264. if (!STI.isThumb2() || !STI.hasMVEIntegerOps())
  265. return false;
  266. TII = static_cast<const Thumb2InstrInfo *>(STI.getInstrInfo());
  267. TRI = STI.getRegisterInfo();
  268. LLVM_DEBUG(dbgs() << "********** ARM MVE VPT BLOCKS **********\n"
  269. << "********** Function: " << Fn.getName() << '\n');
  270. bool Modified = false;
  271. for (MachineBasicBlock &MBB : Fn)
  272. Modified |= InsertVPTBlocks(MBB);
  273. LLVM_DEBUG(dbgs() << "**************************************\n");
  274. return Modified;
  275. }
  276. /// createMVEVPTBlock - Returns an instance of the MVE VPT block
  277. /// insertion pass.
  278. FunctionPass *llvm::createMVEVPTBlockPass() { return new MVEVPTBlock(); }