DxilValueCache.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===--------- DxilValueCache.h - Dxil Constant Value Cache --------------===//
  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. #ifndef LLVM_ANALYSIS_DXILVALUECACHE_H
  10. #define LLVM_ANALYSIS_DXILVALUECACHE_H
  11. #include "llvm/Pass.h"
  12. #include "llvm/IR/ValueMap.h"
  13. namespace llvm {
  14. class Module;
  15. class DominatorTree;
  16. class Constant;
  17. class ConstantInt;
  18. struct DxilValueCache : public ImmutablePass {
  19. static char ID;
  20. // Special Weak Value to Weak Value map.
  21. struct WeakValueMap {
  22. struct ValueVH : public CallbackVH {
  23. ValueVH(Value *V) : CallbackVH(V) {}
  24. void allUsesReplacedWith(Value *) override { setValPtr(nullptr); }
  25. };
  26. struct ValueEntry {
  27. WeakVH Value;
  28. ValueVH Self;
  29. ValueEntry() : Value(nullptr), Self(nullptr) {}
  30. inline void Set(llvm::Value *Key, llvm::Value *V) { Self = Key; Value = V; }
  31. inline bool IsStale() const { return Self == nullptr; }
  32. };
  33. ValueMap<const Value *, ValueEntry> Map;
  34. Value *Get(Value *V);
  35. void Set(Value *Key, Value *V);
  36. bool Seen(Value *v);
  37. void SetSentinel(Value *V);
  38. void ResetUnknowns();
  39. void dump() const;
  40. private:
  41. Value *GetSentinel(LLVMContext &Ctx);
  42. std::unique_ptr<Value> Sentinel;
  43. };
  44. private:
  45. WeakValueMap ValueMap;
  46. void MarkAlwaysReachable(BasicBlock *BB);
  47. void MarkUnreachable(BasicBlock *BB);
  48. bool IsAlwaysReachable_(BasicBlock *BB);
  49. bool IsUnreachable_(BasicBlock *BB);
  50. bool MayBranchTo(BasicBlock *A, BasicBlock *B);
  51. Value *TryGetCachedValue(Value *V);
  52. Value *ProcessValue(Value *V, DominatorTree *DT);
  53. Value *ProcessAndSimplify_PHI(Instruction *I, DominatorTree *DT);
  54. Value *ProcessAndSimplify_Br(Instruction *I, DominatorTree *DT);
  55. Value *ProcessAndSimplify_Load(Instruction *LI, DominatorTree *DT);
  56. Value *SimplifyAndCacheResult(Instruction *I, DominatorTree *DT);
  57. public:
  58. const char *getPassName() const override;
  59. DxilValueCache();
  60. void getAnalysisUsage(AnalysisUsage &) const override;
  61. void dump() const;
  62. Value *GetValue(Value *V, DominatorTree *DT=nullptr);
  63. Constant *GetConstValue(Value *V, DominatorTree *DT = nullptr);
  64. ConstantInt *GetConstInt(Value *V, DominatorTree *DT = nullptr);
  65. void ResetUnknowns() { ValueMap.ResetUnknowns(); }
  66. bool IsAlwaysReachable(BasicBlock *BB, DominatorTree *DT=nullptr);
  67. bool IsUnreachable(BasicBlock *BB, DominatorTree *DT=nullptr);
  68. };
  69. void initializeDxilValueCachePass(class llvm::PassRegistry &);
  70. Pass *createDxilValueCachePass();
  71. }
  72. #endif