PPCExpandISEL.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. //===------------- PPCExpandISEL.cpp - Expand ISEL instruction ------------===//
  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. // A pass that expands the ISEL instruction into an if-then-else sequence.
  10. // This pass must be run post-RA since all operands must be physical registers.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "PPC.h"
  14. #include "PPCInstrInfo.h"
  15. #include "PPCSubtarget.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/Statistic.h"
  18. #include "llvm/CodeGen/LivePhysRegs.h"
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/CodeGen/MachineInstrBuilder.h"
  21. #include "llvm/CodeGen/MachineRegisterInfo.h"
  22. #include "llvm/Support/CommandLine.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. using namespace llvm;
  26. #define DEBUG_TYPE "ppc-expand-isel"
  27. STATISTIC(NumExpanded, "Number of ISEL instructions expanded");
  28. STATISTIC(NumRemoved, "Number of ISEL instructions removed");
  29. STATISTIC(NumFolded, "Number of ISEL instructions folded");
  30. // If -ppc-gen-isel=false is set, we will disable generating the ISEL
  31. // instruction on all PPC targets. Otherwise, if the user set option
  32. // -misel or the platform supports ISEL by default, still generate the
  33. // ISEL instruction, else expand it.
  34. static cl::opt<bool>
  35. GenerateISEL("ppc-gen-isel",
  36. cl::desc("Enable generating the ISEL instruction."),
  37. cl::init(true), cl::Hidden);
  38. namespace {
  39. class PPCExpandISEL : public MachineFunctionPass {
  40. DebugLoc dl;
  41. MachineFunction *MF;
  42. const TargetInstrInfo *TII;
  43. bool IsTrueBlockRequired;
  44. bool IsFalseBlockRequired;
  45. MachineBasicBlock *TrueBlock;
  46. MachineBasicBlock *FalseBlock;
  47. MachineBasicBlock *NewSuccessor;
  48. MachineBasicBlock::iterator TrueBlockI;
  49. MachineBasicBlock::iterator FalseBlockI;
  50. typedef SmallVector<MachineInstr *, 4> BlockISELList;
  51. typedef SmallDenseMap<int, BlockISELList> ISELInstructionList;
  52. // A map of MBB numbers to their lists of contained ISEL instructions.
  53. // Please note when we traverse this list and expand ISEL, we only remove
  54. // the ISEL from the MBB not from this list.
  55. ISELInstructionList ISELInstructions;
  56. /// Initialize the object.
  57. void initialize(MachineFunction &MFParam);
  58. void handleSpecialCases(BlockISELList &BIL, MachineBasicBlock *MBB);
  59. void reorganizeBlockLayout(BlockISELList &BIL, MachineBasicBlock *MBB);
  60. void populateBlocks(BlockISELList &BIL);
  61. void expandMergeableISELs(BlockISELList &BIL);
  62. void expandAndMergeISELs();
  63. bool canMerge(MachineInstr *PrevPushedMI, MachineInstr *MI);
  64. /// Is this instruction an ISEL or ISEL8?
  65. static bool isISEL(const MachineInstr &MI) {
  66. return (MI.getOpcode() == PPC::ISEL || MI.getOpcode() == PPC::ISEL8);
  67. }
  68. /// Is this instruction an ISEL8?
  69. static bool isISEL8(const MachineInstr &MI) {
  70. return (MI.getOpcode() == PPC::ISEL8);
  71. }
  72. /// Are the two operands using the same register?
  73. bool useSameRegister(const MachineOperand &Op1, const MachineOperand &Op2) {
  74. return (Op1.getReg() == Op2.getReg());
  75. }
  76. ///
  77. /// Collect all ISEL instructions from the current function.
  78. ///
  79. /// Walk the current function and collect all the ISEL instructions that are
  80. /// found. The instructions are placed in the ISELInstructions vector.
  81. ///
  82. /// \return true if any ISEL instructions were found, false otherwise
  83. ///
  84. bool collectISELInstructions();
  85. public:
  86. static char ID;
  87. PPCExpandISEL() : MachineFunctionPass(ID) {
  88. initializePPCExpandISELPass(*PassRegistry::getPassRegistry());
  89. }
  90. ///
  91. /// Determine whether to generate the ISEL instruction or expand it.
  92. ///
  93. /// Expand ISEL instruction into if-then-else sequence when one of
  94. /// the following two conditions hold:
  95. /// (1) -ppc-gen-isel=false
  96. /// (2) hasISEL() return false
  97. /// Otherwise, still generate ISEL instruction.
  98. /// The -ppc-gen-isel option is set to true by default. Which means the ISEL
  99. /// instruction is still generated by default on targets that support them.
  100. ///
  101. /// \return true if ISEL should be expanded into if-then-else code sequence;
  102. /// false if ISEL instruction should be generated, i.e. not expanded.
  103. ///
  104. static bool isExpandISELEnabled(const MachineFunction &MF);
  105. #ifndef NDEBUG
  106. void DumpISELInstructions() const;
  107. #endif
  108. bool runOnMachineFunction(MachineFunction &MF) override {
  109. LLVM_DEBUG(dbgs() << "Function: "; MF.dump(); dbgs() << "\n");
  110. initialize(MF);
  111. if (!collectISELInstructions()) {
  112. LLVM_DEBUG(dbgs() << "No ISEL instructions in this function\n");
  113. return false;
  114. }
  115. #ifndef NDEBUG
  116. DumpISELInstructions();
  117. #endif
  118. expandAndMergeISELs();
  119. return true;
  120. }
  121. };
  122. } // end anonymous namespace
  123. void PPCExpandISEL::initialize(MachineFunction &MFParam) {
  124. MF = &MFParam;
  125. TII = MF->getSubtarget().getInstrInfo();
  126. ISELInstructions.clear();
  127. }
  128. bool PPCExpandISEL::isExpandISELEnabled(const MachineFunction &MF) {
  129. return !GenerateISEL || !MF.getSubtarget<PPCSubtarget>().hasISEL();
  130. }
  131. bool PPCExpandISEL::collectISELInstructions() {
  132. for (MachineBasicBlock &MBB : *MF) {
  133. BlockISELList thisBlockISELs;
  134. for (MachineInstr &MI : MBB)
  135. if (isISEL(MI))
  136. thisBlockISELs.push_back(&MI);
  137. if (!thisBlockISELs.empty())
  138. ISELInstructions.insert(std::make_pair(MBB.getNumber(), thisBlockISELs));
  139. }
  140. return !ISELInstructions.empty();
  141. }
  142. #ifndef NDEBUG
  143. void PPCExpandISEL::DumpISELInstructions() const {
  144. for (const auto &I : ISELInstructions) {
  145. LLVM_DEBUG(dbgs() << printMBBReference(*MF->getBlockNumbered(I.first))
  146. << ":\n");
  147. for (const auto &VI : I.second)
  148. LLVM_DEBUG(dbgs() << " "; VI->print(dbgs()));
  149. }
  150. }
  151. #endif
  152. /// Contiguous ISELs that have the same condition can be merged.
  153. bool PPCExpandISEL::canMerge(MachineInstr *PrevPushedMI, MachineInstr *MI) {
  154. // Same Condition Register?
  155. if (!useSameRegister(PrevPushedMI->getOperand(3), MI->getOperand(3)))
  156. return false;
  157. MachineBasicBlock::iterator PrevPushedMBBI = *PrevPushedMI;
  158. MachineBasicBlock::iterator MBBI = *MI;
  159. return (std::prev(MBBI) == PrevPushedMBBI); // Contiguous ISELs?
  160. }
  161. void PPCExpandISEL::expandAndMergeISELs() {
  162. bool ExpandISELEnabled = isExpandISELEnabled(*MF);
  163. for (auto &BlockList : ISELInstructions) {
  164. LLVM_DEBUG(
  165. dbgs() << "Expanding ISEL instructions in "
  166. << printMBBReference(*MF->getBlockNumbered(BlockList.first))
  167. << "\n");
  168. BlockISELList &CurrentISELList = BlockList.second;
  169. auto I = CurrentISELList.begin();
  170. auto E = CurrentISELList.end();
  171. while (I != E) {
  172. assert(isISEL(**I) && "Expecting an ISEL instruction");
  173. MachineOperand &Dest = (*I)->getOperand(0);
  174. MachineOperand &TrueValue = (*I)->getOperand(1);
  175. MachineOperand &FalseValue = (*I)->getOperand(2);
  176. // Special case 1, all registers used by ISEL are the same one.
  177. // The non-redundant isel 0, 0, 0, N would not satisfy these conditions
  178. // as it would be ISEL %R0, %ZERO, %R0, %CRN.
  179. if (useSameRegister(Dest, TrueValue) &&
  180. useSameRegister(Dest, FalseValue)) {
  181. LLVM_DEBUG(dbgs() << "Remove redundant ISEL instruction: " << **I
  182. << "\n");
  183. // FIXME: if the CR field used has no other uses, we could eliminate the
  184. // instruction that defines it. This would have to be done manually
  185. // since this pass runs too late to run DCE after it.
  186. NumRemoved++;
  187. (*I)->eraseFromParent();
  188. I++;
  189. } else if (useSameRegister(TrueValue, FalseValue)) {
  190. // Special case 2, the two input registers used by ISEL are the same.
  191. // Note: the non-foldable isel RX, 0, 0, N would not satisfy this
  192. // condition as it would be ISEL %RX, %ZERO, %R0, %CRN, which makes it
  193. // safe to fold ISEL to MR(OR) instead of ADDI.
  194. MachineBasicBlock *MBB = (*I)->getParent();
  195. LLVM_DEBUG(
  196. dbgs() << "Fold the ISEL instruction to an unconditional copy:\n");
  197. LLVM_DEBUG(dbgs() << "ISEL: " << **I << "\n");
  198. NumFolded++;
  199. // Note: we're using both the TrueValue and FalseValue operands so as
  200. // not to lose the kill flag if it is set on either of them.
  201. BuildMI(*MBB, (*I), dl, TII->get(isISEL8(**I) ? PPC::OR8 : PPC::OR))
  202. .add(Dest)
  203. .add(TrueValue)
  204. .add(FalseValue);
  205. (*I)->eraseFromParent();
  206. I++;
  207. } else if (ExpandISELEnabled) { // Normal cases expansion enabled
  208. LLVM_DEBUG(dbgs() << "Expand ISEL instructions:\n");
  209. LLVM_DEBUG(dbgs() << "ISEL: " << **I << "\n");
  210. BlockISELList SubISELList;
  211. SubISELList.push_back(*I++);
  212. // Collect the ISELs that can be merged together.
  213. // This will eat up ISEL instructions without considering whether they
  214. // may be redundant or foldable to a register copy. So we still keep
  215. // the handleSpecialCases() downstream to handle them.
  216. while (I != E && canMerge(SubISELList.back(), *I)) {
  217. LLVM_DEBUG(dbgs() << "ISEL: " << **I << "\n");
  218. SubISELList.push_back(*I++);
  219. }
  220. expandMergeableISELs(SubISELList);
  221. } else { // Normal cases expansion disabled
  222. I++; // leave the ISEL as it is
  223. }
  224. } // end while
  225. } // end for
  226. }
  227. void PPCExpandISEL::handleSpecialCases(BlockISELList &BIL,
  228. MachineBasicBlock *MBB) {
  229. IsTrueBlockRequired = false;
  230. IsFalseBlockRequired = false;
  231. auto MI = BIL.begin();
  232. while (MI != BIL.end()) {
  233. assert(isISEL(**MI) && "Expecting an ISEL instruction");
  234. LLVM_DEBUG(dbgs() << "ISEL: " << **MI << "\n");
  235. MachineOperand &Dest = (*MI)->getOperand(0);
  236. MachineOperand &TrueValue = (*MI)->getOperand(1);
  237. MachineOperand &FalseValue = (*MI)->getOperand(2);
  238. // If at least one of the ISEL instructions satisfy the following
  239. // condition, we need the True Block:
  240. // The Dest Register and True Value Register are not the same
  241. // Similarly, if at least one of the ISEL instructions satisfy the
  242. // following condition, we need the False Block:
  243. // The Dest Register and False Value Register are not the same.
  244. bool IsADDIInstRequired = !useSameRegister(Dest, TrueValue);
  245. bool IsORIInstRequired = !useSameRegister(Dest, FalseValue);
  246. // Special case 1, all registers used by ISEL are the same one.
  247. if (!IsADDIInstRequired && !IsORIInstRequired) {
  248. LLVM_DEBUG(dbgs() << "Remove redundant ISEL instruction.");
  249. // FIXME: if the CR field used has no other uses, we could eliminate the
  250. // instruction that defines it. This would have to be done manually
  251. // since this pass runs too late to run DCE after it.
  252. NumRemoved++;
  253. (*MI)->eraseFromParent();
  254. // Setting MI to the erase result keeps the iterator valid and increased.
  255. MI = BIL.erase(MI);
  256. continue;
  257. }
  258. // Special case 2, the two input registers used by ISEL are the same.
  259. // Note 1: We favor merging ISEL expansions over folding a single one. If
  260. // the passed list has multiple merge-able ISEL's, we won't fold any.
  261. // Note 2: There is no need to test for PPC::R0/PPC::X0 because PPC::ZERO/
  262. // PPC::ZERO8 will be used for the first operand if the value is meant to
  263. // be zero. In this case, the useSameRegister method will return false,
  264. // thereby preventing this ISEL from being folded.
  265. if (useSameRegister(TrueValue, FalseValue) && (BIL.size() == 1)) {
  266. LLVM_DEBUG(
  267. dbgs() << "Fold the ISEL instruction to an unconditional copy.");
  268. NumFolded++;
  269. // Note: we're using both the TrueValue and FalseValue operands so as
  270. // not to lose the kill flag if it is set on either of them.
  271. BuildMI(*MBB, (*MI), dl, TII->get(isISEL8(**MI) ? PPC::OR8 : PPC::OR))
  272. .add(Dest)
  273. .add(TrueValue)
  274. .add(FalseValue);
  275. (*MI)->eraseFromParent();
  276. // Setting MI to the erase result keeps the iterator valid and increased.
  277. MI = BIL.erase(MI);
  278. continue;
  279. }
  280. IsTrueBlockRequired |= IsADDIInstRequired;
  281. IsFalseBlockRequired |= IsORIInstRequired;
  282. MI++;
  283. }
  284. }
  285. void PPCExpandISEL::reorganizeBlockLayout(BlockISELList &BIL,
  286. MachineBasicBlock *MBB) {
  287. if (BIL.empty())
  288. return;
  289. assert((IsTrueBlockRequired || IsFalseBlockRequired) &&
  290. "Should have been handled by special cases earlier!");
  291. MachineBasicBlock *Successor = nullptr;
  292. const BasicBlock *LLVM_BB = MBB->getBasicBlock();
  293. MachineBasicBlock::iterator MBBI = (*BIL.back());
  294. NewSuccessor = (MBBI != MBB->getLastNonDebugInstr() || !MBB->canFallThrough())
  295. // Another BB is needed to move the instructions that
  296. // follow this ISEL. If the ISEL is the last instruction
  297. // in a block that can't fall through, we also need a block
  298. // to branch to.
  299. ? MF->CreateMachineBasicBlock(LLVM_BB)
  300. : nullptr;
  301. MachineFunction::iterator It = MBB->getIterator();
  302. ++It; // Point to the successor block of MBB.
  303. // If NewSuccessor is NULL then the last ISEL in this group is the last
  304. // non-debug instruction in this block. Find the fall-through successor
  305. // of this block to use when updating the CFG below.
  306. if (!NewSuccessor) {
  307. for (auto &Succ : MBB->successors()) {
  308. if (MBB->isLayoutSuccessor(Succ)) {
  309. Successor = Succ;
  310. break;
  311. }
  312. }
  313. } else
  314. Successor = NewSuccessor;
  315. // The FalseBlock and TrueBlock are inserted after the MBB block but before
  316. // its successor.
  317. // Note this need to be done *after* the above setting the Successor code.
  318. if (IsFalseBlockRequired) {
  319. FalseBlock = MF->CreateMachineBasicBlock(LLVM_BB);
  320. MF->insert(It, FalseBlock);
  321. }
  322. if (IsTrueBlockRequired) {
  323. TrueBlock = MF->CreateMachineBasicBlock(LLVM_BB);
  324. MF->insert(It, TrueBlock);
  325. }
  326. if (NewSuccessor) {
  327. MF->insert(It, NewSuccessor);
  328. // Transfer the rest of this block into the new successor block.
  329. NewSuccessor->splice(NewSuccessor->end(), MBB,
  330. std::next(MachineBasicBlock::iterator(BIL.back())),
  331. MBB->end());
  332. NewSuccessor->transferSuccessorsAndUpdatePHIs(MBB);
  333. // Update the liveins for NewSuccessor.
  334. LivePhysRegs LPR;
  335. computeAndAddLiveIns(LPR, *NewSuccessor);
  336. } else {
  337. // Remove successor from MBB.
  338. MBB->removeSuccessor(Successor);
  339. }
  340. // Note that this needs to be done *after* transfering the successors from MBB
  341. // to the NewSuccessor block, otherwise these blocks will also be transferred
  342. // as successors!
  343. MBB->addSuccessor(IsTrueBlockRequired ? TrueBlock : Successor);
  344. MBB->addSuccessor(IsFalseBlockRequired ? FalseBlock : Successor);
  345. if (IsTrueBlockRequired) {
  346. TrueBlockI = TrueBlock->begin();
  347. TrueBlock->addSuccessor(Successor);
  348. }
  349. if (IsFalseBlockRequired) {
  350. FalseBlockI = FalseBlock->begin();
  351. FalseBlock->addSuccessor(Successor);
  352. }
  353. // Conditional branch to the TrueBlock or Successor
  354. BuildMI(*MBB, BIL.back(), dl, TII->get(PPC::BC))
  355. .add(BIL.back()->getOperand(3))
  356. .addMBB(IsTrueBlockRequired ? TrueBlock : Successor);
  357. // Jump over the true block to the new successor if the condition is false.
  358. BuildMI(*(IsFalseBlockRequired ? FalseBlock : MBB),
  359. (IsFalseBlockRequired ? FalseBlockI : BIL.back()), dl,
  360. TII->get(PPC::B))
  361. .addMBB(Successor);
  362. if (IsFalseBlockRequired)
  363. FalseBlockI = FalseBlock->begin(); // get the position of PPC::B
  364. }
  365. void PPCExpandISEL::populateBlocks(BlockISELList &BIL) {
  366. for (auto &MI : BIL) {
  367. assert(isISEL(*MI) && "Expecting an ISEL instruction");
  368. MachineOperand &Dest = MI->getOperand(0); // location to store to
  369. MachineOperand &TrueValue = MI->getOperand(1); // Value to store if
  370. // condition is true
  371. MachineOperand &FalseValue = MI->getOperand(2); // Value to store if
  372. // condition is false
  373. LLVM_DEBUG(dbgs() << "Dest: " << Dest << "\n");
  374. LLVM_DEBUG(dbgs() << "TrueValue: " << TrueValue << "\n");
  375. LLVM_DEBUG(dbgs() << "FalseValue: " << FalseValue << "\n");
  376. LLVM_DEBUG(dbgs() << "ConditionRegister: " << MI->getOperand(3) << "\n");
  377. // If the Dest Register and True Value Register are not the same one, we
  378. // need the True Block.
  379. bool IsADDIInstRequired = !useSameRegister(Dest, TrueValue);
  380. bool IsORIInstRequired = !useSameRegister(Dest, FalseValue);
  381. // Copy the result into the destination if the condition is true.
  382. if (IsADDIInstRequired)
  383. BuildMI(*TrueBlock, TrueBlockI, dl,
  384. TII->get(isISEL8(*MI) ? PPC::ADDI8 : PPC::ADDI))
  385. .add(Dest)
  386. .add(TrueValue)
  387. .add(MachineOperand::CreateImm(0));
  388. // Copy the result into the destination if the condition is false.
  389. if (IsORIInstRequired)
  390. BuildMI(*FalseBlock, FalseBlockI, dl,
  391. TII->get(isISEL8(*MI) ? PPC::ORI8 : PPC::ORI))
  392. .add(Dest)
  393. .add(FalseValue)
  394. .add(MachineOperand::CreateImm(0));
  395. MI->eraseFromParent(); // Remove the ISEL instruction.
  396. NumExpanded++;
  397. }
  398. if (IsTrueBlockRequired) {
  399. // Update the liveins for TrueBlock.
  400. LivePhysRegs LPR;
  401. computeAndAddLiveIns(LPR, *TrueBlock);
  402. }
  403. if (IsFalseBlockRequired) {
  404. // Update the liveins for FalseBlock.
  405. LivePhysRegs LPR;
  406. computeAndAddLiveIns(LPR, *FalseBlock);
  407. }
  408. }
  409. void PPCExpandISEL::expandMergeableISELs(BlockISELList &BIL) {
  410. // At this stage all the ISELs of BIL are in the same MBB.
  411. MachineBasicBlock *MBB = BIL.back()->getParent();
  412. handleSpecialCases(BIL, MBB);
  413. reorganizeBlockLayout(BIL, MBB);
  414. populateBlocks(BIL);
  415. }
  416. INITIALIZE_PASS(PPCExpandISEL, DEBUG_TYPE, "PowerPC Expand ISEL Generation",
  417. false, false)
  418. char PPCExpandISEL::ID = 0;
  419. FunctionPass *llvm::createPPCExpandISELPass() { return new PPCExpandISEL(); }