RISCVSExtWRemoval.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //===-------------- RISCVSExtWRemoval.cpp - MI sext.w Removal -------------===//
  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 pass removes unneeded sext.w instructions at the MI level. Either
  10. // because the sign extended bits aren't consumed or because the input was
  11. // already sign extended by an earlier instruction.
  12. //
  13. //===---------------------------------------------------------------------===//
  14. #include "RISCV.h"
  15. #include "RISCVMachineFunctionInfo.h"
  16. #include "RISCVSubtarget.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/CodeGen/MachineFunctionPass.h"
  19. #include "llvm/CodeGen/TargetInstrInfo.h"
  20. using namespace llvm;
  21. #define DEBUG_TYPE "riscv-sextw-removal"
  22. STATISTIC(NumRemovedSExtW, "Number of removed sign-extensions");
  23. STATISTIC(NumTransformedToWInstrs,
  24. "Number of instructions transformed to W-ops");
  25. static cl::opt<bool> DisableSExtWRemoval("riscv-disable-sextw-removal",
  26. cl::desc("Disable removal of sext.w"),
  27. cl::init(false), cl::Hidden);
  28. namespace {
  29. class RISCVSExtWRemoval : public MachineFunctionPass {
  30. public:
  31. static char ID;
  32. RISCVSExtWRemoval() : MachineFunctionPass(ID) {
  33. initializeRISCVSExtWRemovalPass(*PassRegistry::getPassRegistry());
  34. }
  35. bool runOnMachineFunction(MachineFunction &MF) override;
  36. void getAnalysisUsage(AnalysisUsage &AU) const override {
  37. AU.setPreservesCFG();
  38. MachineFunctionPass::getAnalysisUsage(AU);
  39. }
  40. StringRef getPassName() const override { return "RISCV sext.w Removal"; }
  41. };
  42. } // end anonymous namespace
  43. char RISCVSExtWRemoval::ID = 0;
  44. INITIALIZE_PASS(RISCVSExtWRemoval, DEBUG_TYPE, "RISCV sext.w Removal", false,
  45. false)
  46. FunctionPass *llvm::createRISCVSExtWRemovalPass() {
  47. return new RISCVSExtWRemoval();
  48. }
  49. // This function returns true if the machine instruction always outputs a value
  50. // where bits 63:32 match bit 31.
  51. static bool isSignExtendingOpW(const MachineInstr &MI,
  52. const MachineRegisterInfo &MRI) {
  53. uint64_t TSFlags = MI.getDesc().TSFlags;
  54. // Instructions that can be determined from opcode are marked in tablegen.
  55. if (TSFlags & RISCVII::IsSignExtendingOpWMask)
  56. return true;
  57. // Special cases that require checking operands.
  58. switch (MI.getOpcode()) {
  59. // shifting right sufficiently makes the value 32-bit sign-extended
  60. case RISCV::SRAI:
  61. return MI.getOperand(2).getImm() >= 32;
  62. case RISCV::SRLI:
  63. return MI.getOperand(2).getImm() > 32;
  64. // The LI pattern ADDI rd, X0, imm is sign extended.
  65. case RISCV::ADDI:
  66. return MI.getOperand(1).isReg() && MI.getOperand(1).getReg() == RISCV::X0;
  67. // An ANDI with an 11 bit immediate will zero bits 63:11.
  68. case RISCV::ANDI:
  69. return isUInt<11>(MI.getOperand(2).getImm());
  70. // An ORI with an >11 bit immediate (negative 12-bit) will set bits 63:11.
  71. case RISCV::ORI:
  72. return !isUInt<11>(MI.getOperand(2).getImm());
  73. // Copying from X0 produces zero.
  74. case RISCV::COPY:
  75. return MI.getOperand(1).getReg() == RISCV::X0;
  76. }
  77. return false;
  78. }
  79. static bool isSignExtendedW(Register SrcReg, const MachineRegisterInfo &MRI,
  80. const RISCVInstrInfo &TII,
  81. SmallPtrSetImpl<MachineInstr *> &FixableDef) {
  82. SmallPtrSet<const MachineInstr *, 4> Visited;
  83. SmallVector<MachineInstr *, 4> Worklist;
  84. auto AddRegDefToWorkList = [&](Register SrcReg) {
  85. if (!SrcReg.isVirtual())
  86. return false;
  87. MachineInstr *SrcMI = MRI.getVRegDef(SrcReg);
  88. if (!SrcMI)
  89. return false;
  90. // Add SrcMI to the worklist.
  91. Worklist.push_back(SrcMI);
  92. return true;
  93. };
  94. if (!AddRegDefToWorkList(SrcReg))
  95. return false;
  96. while (!Worklist.empty()) {
  97. MachineInstr *MI = Worklist.pop_back_val();
  98. // If we already visited this instruction, we don't need to check it again.
  99. if (!Visited.insert(MI).second)
  100. continue;
  101. // If this is a sign extending operation we don't need to look any further.
  102. if (isSignExtendingOpW(*MI, MRI))
  103. continue;
  104. // Is this an instruction that propagates sign extend?
  105. switch (MI->getOpcode()) {
  106. default:
  107. // Unknown opcode, give up.
  108. return false;
  109. case RISCV::COPY: {
  110. const MachineFunction *MF = MI->getMF();
  111. const RISCVMachineFunctionInfo *RVFI =
  112. MF->getInfo<RISCVMachineFunctionInfo>();
  113. // If this is the entry block and the register is livein, see if we know
  114. // it is sign extended.
  115. if (MI->getParent() == &MF->front()) {
  116. Register VReg = MI->getOperand(0).getReg();
  117. if (MF->getRegInfo().isLiveIn(VReg) && RVFI->isSExt32Register(VReg))
  118. continue;
  119. }
  120. Register CopySrcReg = MI->getOperand(1).getReg();
  121. if (CopySrcReg == RISCV::X10) {
  122. // For a method return value, we check the ZExt/SExt flags in attribute.
  123. // We assume the following code sequence for method call.
  124. // PseudoCALL @bar, ...
  125. // ADJCALLSTACKUP 0, 0, implicit-def dead $x2, implicit $x2
  126. // %0:gpr = COPY $x10
  127. //
  128. // We use the PseudoCall to look up the IR function being called to find
  129. // its return attributes.
  130. const MachineBasicBlock *MBB = MI->getParent();
  131. auto II = MI->getIterator();
  132. if (II == MBB->instr_begin() ||
  133. (--II)->getOpcode() != RISCV::ADJCALLSTACKUP)
  134. return false;
  135. const MachineInstr &CallMI = *(--II);
  136. if (!CallMI.isCall() || !CallMI.getOperand(0).isGlobal())
  137. return false;
  138. auto *CalleeFn =
  139. dyn_cast_if_present<Function>(CallMI.getOperand(0).getGlobal());
  140. if (!CalleeFn)
  141. return false;
  142. auto *IntTy = dyn_cast<IntegerType>(CalleeFn->getReturnType());
  143. if (!IntTy)
  144. return false;
  145. const AttributeSet &Attrs = CalleeFn->getAttributes().getRetAttrs();
  146. unsigned BitWidth = IntTy->getBitWidth();
  147. if ((BitWidth <= 32 && Attrs.hasAttribute(Attribute::SExt)) ||
  148. (BitWidth < 32 && Attrs.hasAttribute(Attribute::ZExt)))
  149. continue;
  150. }
  151. if (!AddRegDefToWorkList(CopySrcReg))
  152. return false;
  153. break;
  154. }
  155. // For these, we just need to check if the 1st operand is sign extended.
  156. case RISCV::BCLRI:
  157. case RISCV::BINVI:
  158. case RISCV::BSETI:
  159. if (MI->getOperand(2).getImm() >= 31)
  160. return false;
  161. [[fallthrough]];
  162. case RISCV::REM:
  163. case RISCV::ANDI:
  164. case RISCV::ORI:
  165. case RISCV::XORI:
  166. // |Remainder| is always <= |Dividend|. If D is 32-bit, then so is R.
  167. // DIV doesn't work because of the edge case 0xf..f 8000 0000 / (long)-1
  168. // Logical operations use a sign extended 12-bit immediate.
  169. if (!AddRegDefToWorkList(MI->getOperand(1).getReg()))
  170. return false;
  171. break;
  172. case RISCV::PseudoCCADDW:
  173. case RISCV::PseudoCCSUBW:
  174. // Returns operand 4 or an ADDW/SUBW of operands 5 and 6. We only need to
  175. // check if operand 4 is sign extended.
  176. if (!AddRegDefToWorkList(MI->getOperand(4).getReg()))
  177. return false;
  178. break;
  179. case RISCV::REMU:
  180. case RISCV::AND:
  181. case RISCV::OR:
  182. case RISCV::XOR:
  183. case RISCV::ANDN:
  184. case RISCV::ORN:
  185. case RISCV::XNOR:
  186. case RISCV::MAX:
  187. case RISCV::MAXU:
  188. case RISCV::MIN:
  189. case RISCV::MINU:
  190. case RISCV::PseudoCCMOVGPR:
  191. case RISCV::PseudoCCAND:
  192. case RISCV::PseudoCCOR:
  193. case RISCV::PseudoCCXOR:
  194. case RISCV::PHI: {
  195. // If all incoming values are sign-extended, the output of AND, OR, XOR,
  196. // MIN, MAX, or PHI is also sign-extended.
  197. // The input registers for PHI are operand 1, 3, ...
  198. // The input registers for PseudoCCMOVGPR are 4 and 5.
  199. // The input registers for PseudoCCAND/OR/XOR are 4, 5, and 6.
  200. // The input registers for others are operand 1 and 2.
  201. unsigned B = 1, E = 3, D = 1;
  202. switch (MI->getOpcode()) {
  203. case RISCV::PHI:
  204. E = MI->getNumOperands();
  205. D = 2;
  206. break;
  207. case RISCV::PseudoCCMOVGPR:
  208. B = 4;
  209. E = 6;
  210. break;
  211. case RISCV::PseudoCCAND:
  212. case RISCV::PseudoCCOR:
  213. case RISCV::PseudoCCXOR:
  214. B = 4;
  215. E = 7;
  216. break;
  217. }
  218. for (unsigned I = B; I != E; I += D) {
  219. if (!MI->getOperand(I).isReg())
  220. return false;
  221. if (!AddRegDefToWorkList(MI->getOperand(I).getReg()))
  222. return false;
  223. }
  224. break;
  225. }
  226. case RISCV::VT_MASKC:
  227. case RISCV::VT_MASKCN:
  228. // Instructions return zero or operand 1. Result is sign extended if
  229. // operand 1 is sign extended.
  230. if (!AddRegDefToWorkList(MI->getOperand(1).getReg()))
  231. return false;
  232. break;
  233. // With these opcode, we can "fix" them with the W-version
  234. // if we know all users of the result only rely on bits 31:0
  235. case RISCV::SLLI:
  236. // SLLIW reads the lowest 5 bits, while SLLI reads lowest 6 bits
  237. if (MI->getOperand(2).getImm() >= 32)
  238. return false;
  239. [[fallthrough]];
  240. case RISCV::ADDI:
  241. case RISCV::ADD:
  242. case RISCV::LD:
  243. case RISCV::LWU:
  244. case RISCV::MUL:
  245. case RISCV::SUB:
  246. if (TII.hasAllWUsers(*MI, MRI)) {
  247. FixableDef.insert(MI);
  248. break;
  249. }
  250. return false;
  251. }
  252. }
  253. // If we get here, then every node we visited produces a sign extended value
  254. // or propagated sign extended values. So the result must be sign extended.
  255. return true;
  256. }
  257. static unsigned getWOp(unsigned Opcode) {
  258. switch (Opcode) {
  259. case RISCV::ADDI:
  260. return RISCV::ADDIW;
  261. case RISCV::ADD:
  262. return RISCV::ADDW;
  263. case RISCV::LD:
  264. case RISCV::LWU:
  265. return RISCV::LW;
  266. case RISCV::MUL:
  267. return RISCV::MULW;
  268. case RISCV::SLLI:
  269. return RISCV::SLLIW;
  270. case RISCV::SUB:
  271. return RISCV::SUBW;
  272. default:
  273. llvm_unreachable("Unexpected opcode for replacement with W variant");
  274. }
  275. }
  276. bool RISCVSExtWRemoval::runOnMachineFunction(MachineFunction &MF) {
  277. if (skipFunction(MF.getFunction()) || DisableSExtWRemoval)
  278. return false;
  279. MachineRegisterInfo &MRI = MF.getRegInfo();
  280. const RISCVSubtarget &ST = MF.getSubtarget<RISCVSubtarget>();
  281. const RISCVInstrInfo &TII = *ST.getInstrInfo();
  282. if (!ST.is64Bit())
  283. return false;
  284. bool MadeChange = false;
  285. for (MachineBasicBlock &MBB : MF) {
  286. for (auto I = MBB.begin(), IE = MBB.end(); I != IE;) {
  287. MachineInstr *MI = &*I++;
  288. // We're looking for the sext.w pattern ADDIW rd, rs1, 0.
  289. if (!RISCV::isSEXT_W(*MI))
  290. continue;
  291. Register SrcReg = MI->getOperand(1).getReg();
  292. SmallPtrSet<MachineInstr *, 4> FixableDefs;
  293. // If all users only use the lower bits, this sext.w is redundant.
  294. // Or if all definitions reaching MI sign-extend their output,
  295. // then sext.w is redundant.
  296. if (!TII.hasAllWUsers(*MI, MRI) &&
  297. !isSignExtendedW(SrcReg, MRI, TII, FixableDefs))
  298. continue;
  299. Register DstReg = MI->getOperand(0).getReg();
  300. if (!MRI.constrainRegClass(SrcReg, MRI.getRegClass(DstReg)))
  301. continue;
  302. // Convert Fixable instructions to their W versions.
  303. for (MachineInstr *Fixable : FixableDefs) {
  304. LLVM_DEBUG(dbgs() << "Replacing " << *Fixable);
  305. Fixable->setDesc(TII.get(getWOp(Fixable->getOpcode())));
  306. Fixable->clearFlag(MachineInstr::MIFlag::NoSWrap);
  307. Fixable->clearFlag(MachineInstr::MIFlag::NoUWrap);
  308. Fixable->clearFlag(MachineInstr::MIFlag::IsExact);
  309. LLVM_DEBUG(dbgs() << " with " << *Fixable);
  310. ++NumTransformedToWInstrs;
  311. }
  312. LLVM_DEBUG(dbgs() << "Removing redundant sign-extension\n");
  313. MRI.replaceRegWith(DstReg, SrcReg);
  314. MRI.clearKillFlags(SrcReg);
  315. MI->eraseFromParent();
  316. ++NumRemovedSExtW;
  317. MadeChange = true;
  318. }
  319. }
  320. return MadeChange;
  321. }