FinalizeISel.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/TargetLowering.h"
  19. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  20. #include "llvm/InitializePasses.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "finalize-isel"
  23. namespace {
  24. class FinalizeISel : public MachineFunctionPass {
  25. public:
  26. static char ID; // Pass identification, replacement for typeid
  27. FinalizeISel() : MachineFunctionPass(ID) {}
  28. private:
  29. bool runOnMachineFunction(MachineFunction &MF) override;
  30. void getAnalysisUsage(AnalysisUsage &AU) const override {
  31. MachineFunctionPass::getAnalysisUsage(AU);
  32. }
  33. };
  34. } // end anonymous namespace
  35. char FinalizeISel::ID = 0;
  36. char &llvm::FinalizeISelID = FinalizeISel::ID;
  37. INITIALIZE_PASS(FinalizeISel, DEBUG_TYPE,
  38. "Finalize ISel and expand pseudo-instructions", false, false)
  39. bool FinalizeISel::runOnMachineFunction(MachineFunction &MF) {
  40. bool Changed = false;
  41. const TargetLowering *TLI = MF.getSubtarget().getTargetLowering();
  42. // Iterate through each instruction in the function, looking for pseudos.
  43. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
  44. MachineBasicBlock *MBB = &*I;
  45. for (MachineBasicBlock::iterator MBBI = MBB->begin(), MBBE = MBB->end();
  46. MBBI != MBBE; ) {
  47. MachineInstr &MI = *MBBI++;
  48. // If MI is a pseudo, expand it.
  49. if (MI.usesCustomInsertionHook()) {
  50. Changed = true;
  51. MachineBasicBlock *NewMBB = TLI->EmitInstrWithCustomInserter(MI, MBB);
  52. // The expansion may involve new basic blocks.
  53. if (NewMBB != MBB) {
  54. MBB = NewMBB;
  55. I = NewMBB->getIterator();
  56. MBBI = NewMBB->begin();
  57. MBBE = NewMBB->end();
  58. }
  59. }
  60. }
  61. }
  62. TLI->finalizeLowering(MF);
  63. return Changed;
  64. }