2
0

PHIEliminationUtils.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //===-- PHIEliminationUtils.cpp - Helper functions for PHI elimination ----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "PHIEliminationUtils.h"
  10. #include "llvm/ADT/SmallPtrSet.h"
  11. #include "llvm/CodeGen/MachineBasicBlock.h"
  12. #include "llvm/CodeGen/MachineFunction.h"
  13. #include "llvm/CodeGen/MachineRegisterInfo.h"
  14. using namespace llvm;
  15. // findCopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg
  16. // when following the CFG edge to SuccMBB. This needs to be after any def of
  17. // SrcReg, but before any subsequent point where control flow might jump out of
  18. // the basic block.
  19. MachineBasicBlock::iterator
  20. llvm::findPHICopyInsertPoint(MachineBasicBlock* MBB, MachineBasicBlock* SuccMBB,
  21. unsigned SrcReg) {
  22. // Handle the trivial case trivially.
  23. if (MBB->empty())
  24. return MBB->begin();
  25. // Usually, we just want to insert the copy before the first terminator
  26. // instruction. However, for the edge going to a landing pad, we must insert
  27. // the copy before the call/invoke instruction.
  28. if (!SuccMBB->isLandingPad())
  29. return MBB->getFirstTerminator();
  30. // Discover any defs/uses in this basic block.
  31. SmallPtrSet<MachineInstr*, 8> DefUsesInMBB;
  32. MachineRegisterInfo& MRI = MBB->getParent()->getRegInfo();
  33. for (MachineInstr &RI : MRI.reg_instructions(SrcReg)) {
  34. if (RI.getParent() == MBB)
  35. DefUsesInMBB.insert(&RI);
  36. }
  37. MachineBasicBlock::iterator InsertPoint;
  38. if (DefUsesInMBB.empty()) {
  39. // No defs. Insert the copy at the start of the basic block.
  40. InsertPoint = MBB->begin();
  41. } else if (DefUsesInMBB.size() == 1) {
  42. // Insert the copy immediately after the def/use.
  43. InsertPoint = *DefUsesInMBB.begin();
  44. ++InsertPoint;
  45. } else {
  46. // Insert the copy immediately after the last def/use.
  47. InsertPoint = MBB->end();
  48. while (!DefUsesInMBB.count(&*--InsertPoint)) {}
  49. ++InsertPoint;
  50. }
  51. // Make sure the copy goes after any phi nodes however.
  52. return MBB->SkipPHIsAndLabels(InsertPoint);
  53. }