FinalizeISel.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //===-- llvm/CodeGen/FinalizeISel.cpp ---------------------------*- C++ -*-===//
  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 expands Pseudo-instructions produced by ISel, fixes register
  10. /// reservations and may do machine frame information adjustments.
  11. /// The pseudo instructions are used to allow the expansion to contain control
  12. /// flow, such as a conditional move implemented with a conditional branch and a
  13. /// phi, or an atomic operation implemented with a loop.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineFunctionPass.h"
  18. #include "llvm/CodeGen/Passes.h"
  19. #include "llvm/CodeGen/TargetLowering.h"
  20. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  21. #include "llvm/Support/Debug.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "finalize-isel"
  24. namespace {
  25. class FinalizeISel : public MachineFunctionPass {
  26. public:
  27. static char ID; // Pass identification, replacement for typeid
  28. FinalizeISel() : MachineFunctionPass(ID) {}
  29. private:
  30. bool runOnMachineFunction(MachineFunction &MF) override;
  31. void getAnalysisUsage(AnalysisUsage &AU) const override {
  32. MachineFunctionPass::getAnalysisUsage(AU);
  33. }
  34. };
  35. } // end anonymous namespace
  36. char FinalizeISel::ID = 0;
  37. char &llvm::FinalizeISelID = FinalizeISel::ID;
  38. INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,
  39. "Finalize ISel and expand pseudo-instructions", false, false)
  40. bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {
  41. bool Changed = false;
  42. const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
  43. // Iterate through each instruction in the function, looking for pseudos.
  44. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
  45. MachineBasicBlock *MBB = &*I;
  46. for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
  47. MBBI != MBBE; ) {
  48. MachineInstr &MI = *MBBI++;
  49. // If MI is a pseudo, expand it.
  50. if (MI.usesCustomInsertionHook()) {
  51. Changed = true;
  52. MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB);
  53. // The expansion may involve new basic blocks.
  54. if (NewMBB != MBB) {
  55. MBB = NewMBB;
  56. I = NewMBB->getIterator();
  57. MBBI = NewMBB->begin();
  58. MBBE = NewMBB->end();
  59. }
  60. }
  61. }
  62. }
  63. TLI->finalizeLowering(MF);
  64. return Changed;
  65. }