DxilLoopDeletion.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //===- DxilLoopDeletion.cpp - Dead Loop Deletion Pass -----------===//
  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. //
  10. // This file run LoopDeletion SimplifyCFG and DCE more than once to make sure
  11. // all unused loop can be removed. Use kMaxIteration to avoid dead loop.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/Scalar.h"
  15. #include "llvm/IR/Function.h"
  16. #include "dxc/HLSL/DxilGenerationPass.h"
  17. #include "llvm/IR/LegacyPassManager.h"
  18. using namespace llvm;
  19. namespace {
  20. class DxilLoopDeletion : public FunctionPass {
  21. public:
  22. static char ID; // Pass ID, replacement for typeid
  23. DxilLoopDeletion() : FunctionPass(ID) {
  24. }
  25. bool runOnFunction(Function &F) override;
  26. };
  27. }
  28. char DxilLoopDeletion::ID = 0;
  29. INITIALIZE_PASS(DxilLoopDeletion, "dxil-loop-deletion",
  30. "Delete dead loops", false, false)
  31. FunctionPass *llvm::createDxilLoopDeletionPass() { return new DxilLoopDeletion(); }
  32. bool DxilLoopDeletion::runOnFunction(Function &F) {
  33. // Run loop simplify first to make sure loop invariant is moved so loop
  34. // deletion will not update the function if not delete.
  35. legacy::FunctionPassManager DeleteLoopPM(F.getParent());
  36. DeleteLoopPM.add(createLoopDeletionPass());
  37. bool bUpdated = false;
  38. legacy::FunctionPassManager SimpilfyPM(F.getParent());
  39. SimpilfyPM.add(createCFGSimplificationPass());
  40. SimpilfyPM.add(createDeadCodeEliminationPass());
  41. const unsigned kMaxIteration = 3;
  42. unsigned i=0;
  43. while (i<kMaxIteration) {
  44. if (!DeleteLoopPM.run(F))
  45. break;
  46. SimpilfyPM.run(F);
  47. i++;
  48. bUpdated = true;
  49. }
  50. return bUpdated;
  51. }