DirectXShaderCompiler/lib/Analysis/AssumptionCache.cpp

141 строка
4.9 KiB
C++
Исходник Обычный вид История

2016-12-28 22:52:27 +03:00
//===- AssumptionCache.cpp - Cache finding @llvm.assume calls -------------===//
Revert license text in banner comments to original llvm verbage (#33) Fix #30: Revert license text in banner comments to original llvm verbage This commit removes the Microsoft-specific copyright in llvm files and reverts the copyright wording to the original llvm wording. We used the following method to find the files to change: 1. Find all files in DirectXShaderCompiler that are also in llvm 3.7 2. For those files that have the Microsoft-specific copyright, revert it to the original llvm copyright as present in llvm 3.7 3. Revert the copyright in a few files that are not in llvm, but are mostly copies of files in llvm: lib\Transforms\Scalar\ScalarReplAggregatesHLSL.cpp lib\Transforms\Scalar\Reg2MemHLSL.cpp Leave the Microsoft-specific copyright header in files not present in stock llvm: include\dxc\* lib\HLSL\* lib\DxcSupport\* tools\clang\test\HLSL\* tools\clang\test\CodeGenHLSL\* tools\clang\unittests\HLSL\* tools\clang\unittests\HLSLHost\* tools\clang\tools\dxcompiler\* tools\clang\tools\dxa\* tools\clang\tools\dxc\* tools\clang\tools\dxopt\* tools\clang\tools\dxr\* tools\clang\tools\dxv\* tools\clang\tools\dotnetc\* utils\hct\* CONTRIBUTING.md COPYRIGHT LICENSE-MIT README.md cmake\modules\FindD3D12.cmake cmake\modules\FindDiaSDK.cmake cmake\modules\FindTAEF.cmake docs\DXIL.rst docs\HLSLChanges.rst docs\_themes\dxc-theme\layout.html docs\_themes\dxc-theme\theme.conf docs\_themes\dxc-theme\static\dxc-theme.css include\llvm\llvm_assert\assert.h include\llvm\llvm_assert\cassert include\llvm\Support\MSFileSystem.h include\llvm\Support\OacrIgnoreCond.h lib\MSSupport\CMakeLists.txt lib\MSSupport\MSFileSystemImpl.cpp lib\Support\assert.cpp lib\Support\MSFileSystemBasic.cpp lib\Support\Windows\MSFileSystem.inc.cpp lib\Transforms\Scalar\Reg2MemHLSL.cpp lib\Transforms\Scalar\ScalarReplAggregatesHLSL.cpp tools\clang\docs\UsingDxc.rst tools\clang\include\clang\AST\HlslTypes.h tools\clang\include\clang\Basic\BuiltinsDXIL.def tools\clang\include\clang\Basic\LangOptions.fixed.def tools\clang\include\clang\Parse\ParseHLSL.h tools\clang\include\clang\Sema\SemaHLSL.h tools\clang\lib\AST\ASTContextHLSL.cpp tools\clang\lib\AST\HlslTypes.cpp tools\clang\lib\CodeGen\CGHLSLMS.cpp tools\clang\lib\CodeGen\CGHLSLRuntime.cpp tools\clang\lib\CodeGen\CGHLSLRuntime.h tools\clang\lib\Frontend\Rewrite\FrontendActions_rewrite.cpp tools\clang\lib\Parse\HLSLRootSignature.cpp tools\clang\lib\Parse\HLSLRootSignature.h tools\clang\lib\Parse\ParseHLSL.cpp tools\clang\lib\Sema\gen_intrin_main_tables_15.h tools\clang\lib\Sema\SemaHLSL.cpp tools\clang\tools\d3dcomp\CMakeLists.txt tools\clang\tools\d3dcomp\d3dcomp.cpp tools\clang\tools\d3dcomp\d3dcomp.def tools\clang\tools\libclang\dxcisenseimpl.cpp tools\clang\tools\libclang\dxcisenseimpl.h tools\clang\tools\libclang\dxcrewriteunused.cpp tools\clang\tools\libclang\libclang.rc tools\dxexp\CMakeLists.txt tools\dxexp\dxexp.cpp tools\dxexp\LLVMBuild.txt
2017-01-25 04:54:00 +03:00
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains a pass that keeps track of @llvm.assume intrinsics in
// the functions of a module.
//
//===----------------------------------------------------------------------===//
2016-12-28 22:52:27 +03:00
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/Debug.h"
using namespace llvm;
using namespace llvm::PatternMatch;
void AssumptionCache::scanFunction() {
assert(!Scanned && "Tried to scan the function twice!");
assert(AssumeHandles.empty() && "Already have assumes when scanning!");
// Go through all instructions in all blocks, add all calls to @llvm.assume
// to this cache.
for (BasicBlock &B : F)
for (Instruction &II : B)
if (match(&II, m_Intrinsic<Intrinsic::assume>()))
AssumeHandles.push_back(&II);
// Mark the scan as complete.
Scanned = true;
}
void AssumptionCache::registerAssumption(CallInst *CI) {
assert(match(CI, m_Intrinsic<Intrinsic::assume>()) &&
"Registered call does not call @llvm.assume");
// If we haven't scanned the function yet, just drop this assumption. It will
// be found when we scan later.
if (!Scanned)
return;
AssumeHandles.push_back(CI);
#ifndef NDEBUG
assert(CI->getParent() &&
"Cannot register @llvm.assume call not in a basic block");
assert(&F == CI->getParent()->getParent() &&
"Cannot register @llvm.assume call not in this function");
// We expect the number of assumptions to be small, so in an asserts build
// check that we don't accumulate duplicates and that all assumptions point
// to the same function.
SmallPtrSet<Value *, 16> AssumptionSet;
for (auto &VH : AssumeHandles) {
if (!VH)
continue;
assert(&F == cast<Instruction>(VH)->getParent()->getParent() &&
"Cached assumption not inside this function!");
assert(match(cast<CallInst>(VH), m_Intrinsic<Intrinsic::assume>()) &&
"Cached something other than a call to @llvm.assume!");
assert(AssumptionSet.insert(VH).second &&
"Cache contains multiple copies of a call!");
}
#endif
}
char AssumptionAnalysis::PassID;
PreservedAnalyses AssumptionPrinterPass::run(Function &F,
AnalysisManager<Function> *AM) {
AssumptionCache &AC = AM->getResult<AssumptionAnalysis>(F);
OS << "Cached assumptions for function: " << F.getName() << "\n";
for (auto &VH : AC.assumptions())
if (VH)
OS << " " << *cast<CallInst>(VH)->getArgOperand(0) << "\n";
return PreservedAnalyses::all();
}
void AssumptionCacheTracker::FunctionCallbackVH::deleted() {
auto I = ACT->AssumptionCaches.find_as(cast<Function>(getValPtr()));
if (I != ACT->AssumptionCaches.end())
ACT->AssumptionCaches.erase(I);
// 'this' now dangles!
}
AssumptionCache &AssumptionCacheTracker::getAssumptionCache(Function &F) {
// We probe the function map twice to try and avoid creating a value handle
// around the function in common cases. This makes insertion a bit slower,
// but if we have to insert we're going to scan the whole function so that
// shouldn't matter.
auto I = AssumptionCaches.find_as(&F);
if (I != AssumptionCaches.end())
return *I->second;
// Ok, build a new cache by scanning the function, insert it and the value
// handle into our map, and return the newly populated cache.
auto IP = AssumptionCaches.insert(std::make_pair(
FunctionCallbackVH(&F, this), llvm::make_unique<AssumptionCache>(F)));
assert(IP.second && "Scanning function already in the map?");
return *IP.first->second;
}
void AssumptionCacheTracker::verifyAnalysis() const {
#ifndef NDEBUG
SmallPtrSet<const CallInst *, 4> AssumptionSet;
for (const auto &I : AssumptionCaches) {
for (auto &VH : I.second->assumptions())
if (VH)
AssumptionSet.insert(cast<CallInst>(VH));
for (const BasicBlock &B : cast<Function>(*I.first))
for (const Instruction &II : B)
if (match(&II, m_Intrinsic<Intrinsic::assume>()))
assert(AssumptionSet.count(cast<CallInst>(&II)) &&
"Assumption in scanned function not in cache");
}
#endif
}
AssumptionCacheTracker::AssumptionCacheTracker() : ImmutablePass(ID) {
initializeAssumptionCacheTrackerPass(*PassRegistry::getPassRegistry());
}
AssumptionCacheTracker::~AssumptionCacheTracker() {}
INITIALIZE_PASS(AssumptionCacheTracker, "assumption-cache-tracker",
"Assumption Cache Tracker", false, true)
char AssumptionCacheTracker::ID = 0;