ReduceIRReferences.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===- ReduceIRReferences.cpp - Specialized Delta Pass --------------------===//
  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 file implements a function which calls the Generic Delta pass in order
  10. // to remove backreferences to the IR from MIR. In particular, this will remove
  11. // the Value references in MachineMemOperands.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ReduceIRReferences.h"
  15. #include "Delta.h"
  16. #include "llvm/CodeGen/MachineFrameInfo.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineModuleInfo.h"
  19. using namespace llvm;
  20. static void dropIRReferencesFromInstructions(Oracle &O, MachineFunction &MF) {
  21. for (MachineBasicBlock &MBB : MF) {
  22. for (MachineInstr &MI : MBB) {
  23. if (!O.shouldKeep()) {
  24. for (MachineMemOperand *MMO : MI.memoperands()) {
  25. // Leave behind pseudo source values.
  26. // TODO: Removing all MemOperand values is a further reduction step.
  27. if (MMO->getPointerInfo().V.is<const Value *>())
  28. MMO->setValue(static_cast<const Value *>(nullptr));
  29. }
  30. // TODO: Try to remove GlobalValue references and metadata
  31. }
  32. }
  33. }
  34. }
  35. static void stripIRFromInstructions(Oracle &O, ReducerWorkItem &WorkItem) {
  36. for (const Function &F : WorkItem.getModule()) {
  37. if (auto *MF = WorkItem.MMI->getMachineFunction(F))
  38. dropIRReferencesFromInstructions(O, *MF);
  39. }
  40. }
  41. static void stripIRFromBlocks(Oracle &O, ReducerWorkItem &WorkItem) {
  42. for (const Function &F : WorkItem.getModule()) {
  43. if (auto *MF = WorkItem.MMI->getMachineFunction(F)) {
  44. for (MachineBasicBlock &MBB : *MF) {
  45. if (!O.shouldKeep())
  46. MBB.clearBasicBlock();
  47. }
  48. }
  49. }
  50. }
  51. static void stripIRFromFunctions(Oracle &O, ReducerWorkItem &WorkItem) {
  52. for (const Function &F : WorkItem.getModule()) {
  53. if (!O.shouldKeep()) {
  54. if (auto *MF = WorkItem.MMI->getMachineFunction(F)) {
  55. MachineFrameInfo &MFI = MF->getFrameInfo();
  56. for (int I = MFI.getObjectIndexBegin(), E = MFI.getObjectIndexEnd();
  57. I != E; ++I)
  58. MFI.clearObjectAllocation(I);
  59. }
  60. }
  61. }
  62. }
  63. void llvm::reduceIRInstructionReferencesDeltaPass(TestRunner &Test) {
  64. runDeltaPass(Test, stripIRFromInstructions,
  65. "Reducing IR references from instructions");
  66. }
  67. void llvm::reduceIRBlockReferencesDeltaPass(TestRunner &Test) {
  68. runDeltaPass(Test, stripIRFromBlocks, "Reducing IR references from blocks");
  69. }
  70. void llvm::reduceIRFunctionReferencesDeltaPass(TestRunner &Test) {
  71. runDeltaPass(Test, stripIRFromFunctions,
  72. "Reducing IR references from functions");
  73. }