DxilUtil.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // DxilUtil.h //
  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. // DXIL helper functions. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #pragma once
  12. #include <unordered_set>
  13. #include <string>
  14. #include <memory>
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/IR/Constants.h"
  17. namespace llvm {
  18. class Type;
  19. class GlobalVariable;
  20. class Function;
  21. class Module;
  22. class MemoryBuffer;
  23. class LLVMContext;
  24. class DiagnosticInfo;
  25. class Value;
  26. class Instruction;
  27. class BasicBlock;
  28. class raw_ostream;
  29. }
  30. namespace hlsl {
  31. class DxilFieldAnnotation;
  32. class DxilTypeSystem;
  33. namespace dxilutil {
  34. extern const char ManglingPrefix[];
  35. extern const char EntryPrefix[];
  36. extern const llvm::StringRef kResourceMapErrorMsg;
  37. unsigned
  38. GetLegacyCBufferFieldElementSize(DxilFieldAnnotation &fieldAnnotation,
  39. llvm::Type *Ty, DxilTypeSystem &typeSys);
  40. llvm::Type *GetArrayEltTy(llvm::Type *Ty);
  41. bool HasDynamicIndexing(llvm::Value *V);
  42. // Find alloca insertion point, given instruction
  43. llvm::Instruction *FindAllocaInsertionPt(llvm::Instruction* I);
  44. llvm::Instruction *FindAllocaInsertionPt(llvm::Function* F);
  45. llvm::Instruction *SkipAllocas(llvm::Instruction *I);
  46. // Get first non-alloca insertion point, to avoid inserting non-allocas before alloca
  47. llvm::Instruction *FirstNonAllocaInsertionPt(llvm::Instruction* I);
  48. llvm::Instruction *FirstNonAllocaInsertionPt(llvm::BasicBlock* BB);
  49. llvm::Instruction *FirstNonAllocaInsertionPt(llvm::Function* F);
  50. bool IsStaticGlobal(llvm::GlobalVariable *GV);
  51. bool IsSharedMemoryGlobal(llvm::GlobalVariable *GV);
  52. bool RemoveUnusedFunctions(llvm::Module &M, llvm::Function *EntryFunc,
  53. llvm::Function *PatchConstantFunc, bool IsLib);
  54. void EmitErrorOnInstruction(llvm::Instruction *I, llvm::StringRef Msg);
  55. void EmitResMappingError(llvm::Instruction *Res);
  56. // Simple demangle just support case "\01?name@" pattern.
  57. llvm::StringRef DemangleFunctionName(llvm::StringRef name);
  58. // ReplaceFunctionName replaces the undecorated portion of originalName with undecorated newName
  59. std::string ReplaceFunctionName(llvm::StringRef originalName, llvm::StringRef newName);
  60. void PrintEscapedString(llvm::StringRef Name, llvm::raw_ostream &Out);
  61. void PrintUnescapedString(llvm::StringRef Name, llvm::raw_ostream &Out);
  62. // Change select/phi on operands into select/phi on operation.
  63. // phi0 = phi a0, b0, c0
  64. // phi1 = phi a1, b1, c1
  65. // Inst = Add(phi0, phi1);
  66. // into
  67. // A = Add(a0, a1);
  68. // B = Add(b0, b1);
  69. // C = Add(c0, c1);
  70. // NewInst = phi A, B, C
  71. // Only support 1 operand now, other oerands should be Constant.
  72. llvm::Value * SelectOnOperation(llvm::Instruction *Inst, unsigned operandIdx);
  73. // Collect all select operand used by Inst.
  74. void CollectSelect(llvm::Instruction *Inst,
  75. std::unordered_set<llvm::Instruction *> &selectSet);
  76. // If all operands are the same for a select inst, replace it with the operand.
  77. // Returns replacement value if successful
  78. llvm::Value *MergeSelectOnSameValue(llvm::Instruction *SelInst,
  79. unsigned startOpIdx,
  80. unsigned numOperands);
  81. std::unique_ptr<llvm::Module> LoadModuleFromBitcode(llvm::StringRef BC,
  82. llvm::LLVMContext &Ctx, std::string &DiagStr);
  83. std::unique_ptr<llvm::Module> LoadModuleFromBitcode(llvm::MemoryBuffer *MB,
  84. llvm::LLVMContext &Ctx, std::string &DiagStr);
  85. void PrintDiagnosticHandler(const llvm::DiagnosticInfo &DI, void *Context);
  86. // Returns true if type contains HLSL Object type (resource)
  87. bool ContainsHLSLObjectType(llvm::Type *Ty);
  88. bool IsSplat(llvm::ConstantDataVector *cdv);
  89. }
  90. }