DxilValueCache.h 2.5 KB

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