DxilNoOptLegalize.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilNoOptLegalize.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include "llvm/Pass.h"
  10. #include "llvm/IR/Module.h"
  11. #include "llvm/IR/Instructions.h"
  12. #include "dxc/HLSL/DxilGenerationPass.h"
  13. #include "dxc/HLSL/DxilNoops.h"
  14. #include "llvm/IR/Operator.h"
  15. #include "llvm/Analysis/DxilValueCache.h"
  16. using namespace llvm;
  17. class DxilNoOptLegalize : public ModulePass {
  18. SmallVector<Value *, 16> Worklist;
  19. public:
  20. static char ID;
  21. DxilNoOptLegalize() : ModulePass(ID) {
  22. initializeDxilNoOptLegalizePass(*PassRegistry::getPassRegistry());
  23. }
  24. bool runOnModule(Module &M) override;
  25. bool RemoveStoreUndefsFromPtr(Value *V);
  26. bool RemoveStoreUndefs(Module &M);
  27. };
  28. char DxilNoOptLegalize::ID;
  29. bool DxilNoOptLegalize::RemoveStoreUndefsFromPtr(Value *Ptr) {
  30. bool Changed = false;
  31. Worklist.clear();
  32. Worklist.push_back(Ptr);
  33. while (Worklist.size()) {
  34. Value *V = Worklist.back();
  35. Worklist.pop_back();
  36. if (isa<AllocaInst>(V) || isa<GlobalVariable>(V) || isa<GEPOperator>(V)) {
  37. for (User *U : V->users())
  38. Worklist.push_back(U);
  39. }
  40. else if (StoreInst *Store = dyn_cast<StoreInst>(V)) {
  41. if (isa<UndefValue>(Store->getValueOperand())) {
  42. Store->eraseFromParent();
  43. Changed = true;
  44. }
  45. }
  46. }
  47. return Changed;
  48. }
  49. bool DxilNoOptLegalize::RemoveStoreUndefs(Module &M) {
  50. bool Changed = false;
  51. for (GlobalVariable &GV : M.globals()) {
  52. Changed |= RemoveStoreUndefsFromPtr(&GV);
  53. }
  54. for (Function &F : M) {
  55. if (F.empty())
  56. continue;
  57. BasicBlock &Entry = F.getEntryBlock();
  58. for (Instruction &I : Entry) {
  59. if (isa<AllocaInst>(&I))
  60. Changed |= RemoveStoreUndefsFromPtr(&I);
  61. }
  62. }
  63. return Changed;
  64. }
  65. bool DxilNoOptLegalize::runOnModule(Module &M) {
  66. bool Changed = false;
  67. Changed |= RemoveStoreUndefs(M);
  68. return Changed;
  69. }
  70. ModulePass *llvm::createDxilNoOptLegalizePass() {
  71. return new DxilNoOptLegalize();
  72. }
  73. INITIALIZE_PASS(DxilNoOptLegalize, "dxil-o0-legalize", "DXIL No-Opt Legalize", false, false)
  74. class DxilNoOptSimplifyInstructions : public ModulePass {
  75. SmallVector<Value *, 16> Worklist;
  76. public:
  77. static char ID;
  78. DxilNoOptSimplifyInstructions() : ModulePass(ID) {
  79. initializeDxilNoOptSimplifyInstructionsPass(*PassRegistry::getPassRegistry());
  80. }
  81. void getAnalysisUsage(AnalysisUsage &AU) const override {
  82. AU.addRequired<DxilValueCache>();
  83. }
  84. bool runOnModule(Module &M) override {
  85. bool Changed = false;
  86. DxilValueCache *DVC = &getAnalysis<DxilValueCache>();
  87. for (Function &F : M) {
  88. for (BasicBlock &BB : F) {
  89. for (auto it = BB.begin(), end = BB.end(); it != end;) {
  90. Instruction *I = &*(it++);
  91. if (I->getOpcode() == Instruction::Select) {
  92. if (hlsl::IsPreserve(I))
  93. continue;
  94. if (Value *C = DVC->GetValue(I)) {
  95. I->replaceAllUsesWith(C);
  96. I->eraseFromParent();
  97. Changed = true;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. return Changed;
  104. }
  105. };
  106. char DxilNoOptSimplifyInstructions::ID;
  107. ModulePass *llvm::createDxilNoOptSimplifyInstructionsPass() {
  108. return new DxilNoOptSimplifyInstructions();
  109. }
  110. INITIALIZE_PASS(DxilNoOptSimplifyInstructions, "dxil-o0-simplify-inst", "DXIL No-Opt Simplify Inst", false, false)