FinalizeISel.cpp 2.7 KB

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