2020-04-02 13:41:24 +02:00
|
|
|
//===- AssumeBundleBuilder.cpp - tools to preserve informations -*- C++ -*-===//
|
2020-02-02 14:46:59 +01:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-04-02 13:41:24 +02:00
|
|
|
#include "llvm/Transforms/Utils/AssumeBundleBuilder.h"
|
2020-05-10 19:26:41 +02:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2020-04-02 13:41:24 +02:00
|
|
|
#include "llvm/Analysis/AssumeBundleQueries.h"
|
2020-04-13 11:27:27 +02:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2020-05-10 18:20:34 +02:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
2020-03-25 21:55:35 +01:00
|
|
|
#include "llvm/IR/Function.h"
|
2020-02-02 14:46:59 +01:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
cl::opt<bool> ShouldPreserveAllAttributes(
|
|
|
|
"assume-preserve-all", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc("enable preservation of all attrbitues. even those that are "
|
|
|
|
"unlikely to be usefull"));
|
|
|
|
|
2020-03-13 14:14:55 +01:00
|
|
|
cl::opt<bool> EnableKnowledgeRetention(
|
|
|
|
"enable-knowledge-retention", cl::init(false), cl::Hidden,
|
|
|
|
cl::desc(
|
|
|
|
"enable preservation of attributes throughout code transformation"));
|
|
|
|
|
2020-02-18 19:06:30 +01:00
|
|
|
namespace {
|
|
|
|
|
2020-03-13 14:35:26 +01:00
|
|
|
bool isUsefullToPreserve(Attribute::AttrKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case Attribute::NonNull:
|
|
|
|
case Attribute::Alignment:
|
|
|
|
case Attribute::Dereferenceable:
|
|
|
|
case Attribute::DereferenceableOrNull:
|
|
|
|
case Attribute::Cold:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:46:59 +01:00
|
|
|
/// This class contain all knowledge that have been gather while building an
|
|
|
|
/// llvm.assume and the function to manipulate it.
|
|
|
|
struct AssumeBuilderState {
|
|
|
|
Module *M;
|
|
|
|
|
2020-04-24 22:34:55 +02:00
|
|
|
using MapKey = std::pair<Value *, Attribute::AttrKind>;
|
2020-05-10 19:26:41 +02:00
|
|
|
SmallMapVector<MapKey, unsigned, 8> AssumedKnowledgeMap;
|
2020-04-24 22:34:55 +02:00
|
|
|
Instruction *InsertBeforeInstruction = nullptr;
|
2020-05-10 18:20:34 +02:00
|
|
|
AssumptionCache* AC = nullptr;
|
|
|
|
DominatorTree* DT = nullptr;
|
2020-02-02 14:46:59 +01:00
|
|
|
|
2020-05-10 18:20:34 +02:00
|
|
|
AssumeBuilderState(Module *M, Instruction *I = nullptr,
|
|
|
|
AssumptionCache *AC = nullptr, DominatorTree *DT = nullptr)
|
|
|
|
: M(M), InsertBeforeInstruction(I), AC(AC), DT(DT) {}
|
|
|
|
|
|
|
|
bool tryToPreserveWithoutAddingAssume(RetainedKnowledge RK) {
|
|
|
|
if (!InsertBeforeInstruction || !AC || !RK.WasOn)
|
|
|
|
return false;
|
|
|
|
bool HasBeenPreserved = false;
|
|
|
|
Use* ToUpdate = nullptr;
|
|
|
|
getKnowledgeForValue(
|
|
|
|
RK.WasOn, {RK.AttrKind}, AC,
|
|
|
|
[&](RetainedKnowledge RKOther, Instruction *Assume,
|
|
|
|
const CallInst::BundleOpInfo *Bundle) {
|
|
|
|
if (!isValidAssumeForContext(Assume, InsertBeforeInstruction, DT))
|
|
|
|
return false;
|
|
|
|
if (RKOther.ArgValue >= RK.ArgValue) {
|
|
|
|
HasBeenPreserved = true;
|
|
|
|
return true;
|
|
|
|
} else if (isValidAssumeForContext(InsertBeforeInstruction, Assume,
|
|
|
|
DT)) {
|
|
|
|
HasBeenPreserved = true;
|
|
|
|
IntrinsicInst *Intr = cast<IntrinsicInst>(Assume);
|
|
|
|
ToUpdate = &Intr->op_begin()[Bundle->Begin + ABA_Argument];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
if (ToUpdate)
|
|
|
|
ToUpdate->set(
|
|
|
|
ConstantInt::get(Type::getInt64Ty(M->getContext()), RK.ArgValue));
|
|
|
|
return HasBeenPreserved;
|
|
|
|
}
|
2020-02-02 14:46:59 +01:00
|
|
|
|
2020-04-24 22:34:55 +02:00
|
|
|
void addKnowledge(RetainedKnowledge RK) {
|
2020-05-10 18:20:34 +02:00
|
|
|
if (tryToPreserveWithoutAddingAssume(RK))
|
|
|
|
return;
|
2020-04-24 22:34:55 +02:00
|
|
|
MapKey Key{RK.WasOn, RK.AttrKind};
|
|
|
|
auto Lookup = AssumedKnowledgeMap.find(Key);
|
|
|
|
if (Lookup == AssumedKnowledgeMap.end()) {
|
|
|
|
AssumedKnowledgeMap[Key] = RK.ArgValue;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(((Lookup->second == 0 && RK.ArgValue == 0) ||
|
|
|
|
(Lookup->second != 0 && RK.ArgValue != 0)) &&
|
|
|
|
"inconsistent argument value");
|
|
|
|
|
|
|
|
/// This is only desirable because for all attributes taking an argument
|
|
|
|
/// higher is better.
|
|
|
|
Lookup->second = std::max(Lookup->second, RK.ArgValue);
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:46:59 +01:00
|
|
|
void addAttribute(Attribute Attr, Value *WasOn) {
|
2020-04-24 22:34:55 +02:00
|
|
|
if (Attr.isTypeAttribute() || Attr.isStringAttribute() ||
|
|
|
|
(!ShouldPreserveAllAttributes &&
|
2020-03-13 14:35:26 +01:00
|
|
|
!isUsefullToPreserve(Attr.getKindAsEnum())))
|
|
|
|
return;
|
2020-04-24 22:34:55 +02:00
|
|
|
unsigned AttrArg = 0;
|
2020-02-02 14:46:59 +01:00
|
|
|
if (Attr.isIntAttribute())
|
2020-04-24 22:34:55 +02:00
|
|
|
AttrArg = Attr.getValueAsInt();
|
|
|
|
addKnowledge({Attr.getKindAsEnum(), AttrArg, WasOn});
|
2020-02-02 14:46:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void addCall(const CallBase *Call) {
|
|
|
|
auto addAttrList = [&](AttributeList AttrList) {
|
|
|
|
for (unsigned Idx = AttributeList::FirstArgIndex;
|
|
|
|
Idx < AttrList.getNumAttrSets(); Idx++)
|
|
|
|
for (Attribute Attr : AttrList.getAttributes(Idx))
|
|
|
|
addAttribute(Attr, Call->getArgOperand(Idx - 1));
|
2020-03-13 14:35:26 +01:00
|
|
|
for (Attribute Attr : AttrList.getFnAttributes())
|
|
|
|
addAttribute(Attr, nullptr);
|
2020-02-02 14:46:59 +01:00
|
|
|
};
|
|
|
|
addAttrList(Call->getAttributes());
|
|
|
|
if (Function *Fn = Call->getCalledFunction())
|
|
|
|
addAttrList(Fn->getAttributes());
|
|
|
|
}
|
|
|
|
|
2020-03-25 18:28:51 +01:00
|
|
|
IntrinsicInst *build() {
|
2020-04-24 22:34:55 +02:00
|
|
|
if (AssumedKnowledgeMap.empty())
|
2020-02-02 14:46:59 +01:00
|
|
|
return nullptr;
|
|
|
|
Function *FnAssume = Intrinsic::getDeclaration(M, Intrinsic::assume);
|
|
|
|
LLVMContext &C = M->getContext();
|
|
|
|
SmallVector<OperandBundleDef, 8> OpBundle;
|
2020-04-24 22:34:55 +02:00
|
|
|
for (auto &MapElem : AssumedKnowledgeMap) {
|
2020-02-02 14:46:59 +01:00
|
|
|
SmallVector<Value *, 2> Args;
|
2020-04-24 22:34:55 +02:00
|
|
|
if (MapElem.first.first)
|
|
|
|
Args.push_back(MapElem.first.first);
|
|
|
|
|
|
|
|
/// This is only valid because for all attribute that currently exist a
|
|
|
|
/// value of 0 is useless. and should not be preserved.
|
|
|
|
if (MapElem.second)
|
|
|
|
Args.push_back(ConstantInt::get(Type::getInt64Ty(M->getContext()),
|
|
|
|
MapElem.second));
|
|
|
|
OpBundle.push_back(OperandBundleDefT<Value *>(
|
|
|
|
std::string(Attribute::getNameFromAttrKind(MapElem.first.second)),
|
|
|
|
Args));
|
2020-02-02 14:46:59 +01:00
|
|
|
}
|
2020-03-25 18:28:51 +01:00
|
|
|
return cast<IntrinsicInst>(CallInst::Create(
|
|
|
|
FnAssume, ArrayRef<Value *>({ConstantInt::getTrue(C)}), OpBundle));
|
2020-02-02 14:46:59 +01:00
|
|
|
}
|
|
|
|
|
2020-03-31 19:38:08 +02:00
|
|
|
void addAccessedPtr(Instruction *MemInst, Value *Pointer, Type *AccType,
|
|
|
|
MaybeAlign MA) {
|
2020-04-24 22:34:55 +02:00
|
|
|
unsigned DerefSize = MemInst->getModule()
|
2020-03-31 19:38:08 +02:00
|
|
|
->getDataLayout()
|
|
|
|
.getTypeStoreSize(AccType)
|
|
|
|
.getKnownMinSize();
|
|
|
|
if (DerefSize != 0) {
|
2020-04-24 22:34:55 +02:00
|
|
|
addKnowledge({Attribute::Dereferenceable, DerefSize, Pointer});
|
2020-03-31 19:38:08 +02:00
|
|
|
if (!NullPointerIsDefined(MemInst->getFunction(),
|
|
|
|
Pointer->getType()->getPointerAddressSpace()))
|
2020-04-24 22:34:55 +02:00
|
|
|
addKnowledge({Attribute::NonNull, 0u, Pointer});
|
2020-03-31 19:38:08 +02:00
|
|
|
}
|
|
|
|
if (MA.valueOrOne() > 1)
|
2020-04-24 22:34:55 +02:00
|
|
|
addKnowledge(
|
|
|
|
{Attribute::Alignment, unsigned(MA.valueOrOne().value()), Pointer});
|
2020-03-25 21:55:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void addInstruction(Instruction *I) {
|
2020-02-02 14:46:59 +01:00
|
|
|
if (auto *Call = dyn_cast<CallBase>(I))
|
2020-03-25 21:55:35 +01:00
|
|
|
return addCall(Call);
|
2020-03-31 19:38:08 +02:00
|
|
|
if (auto *Load = dyn_cast<LoadInst>(I))
|
|
|
|
return addAccessedPtr(I, Load->getPointerOperand(), Load->getType(),
|
|
|
|
Load->getAlign());
|
|
|
|
if (auto *Store = dyn_cast<StoreInst>(I))
|
|
|
|
return addAccessedPtr(I, Store->getPointerOperand(),
|
|
|
|
Store->getValueOperand()->getType(),
|
|
|
|
Store->getAlign());
|
2020-02-02 14:46:59 +01:00
|
|
|
// TODO: Add support for the other Instructions.
|
|
|
|
// TODO: Maybe we should look around and merge with other llvm.assume.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-03-25 18:28:51 +01:00
|
|
|
IntrinsicInst *llvm::buildAssumeFromInst(Instruction *I) {
|
2020-03-13 14:14:55 +01:00
|
|
|
if (!EnableKnowledgeRetention)
|
|
|
|
return nullptr;
|
2020-03-25 18:28:51 +01:00
|
|
|
AssumeBuilderState Builder(I->getModule());
|
2020-02-02 14:46:59 +01:00
|
|
|
Builder.addInstruction(I);
|
|
|
|
return Builder.build();
|
|
|
|
}
|
|
|
|
|
2020-05-10 18:20:34 +02:00
|
|
|
void llvm::salvageKnowledge(Instruction *I, AssumptionCache *AC, DominatorTree* DT) {
|
2020-04-24 22:34:55 +02:00
|
|
|
if (!EnableKnowledgeRetention)
|
|
|
|
return;
|
2020-05-10 18:20:34 +02:00
|
|
|
AssumeBuilderState Builder(I->getModule(), I, AC, DT);
|
2020-04-24 22:34:55 +02:00
|
|
|
Builder.addInstruction(I);
|
|
|
|
if (IntrinsicInst *Intr = Builder.build()) {
|
2020-03-25 22:07:03 +01:00
|
|
|
Intr->insertBefore(I);
|
2020-04-13 11:27:27 +02:00
|
|
|
if (AC)
|
|
|
|
AC->registerAssumption(Intr);
|
|
|
|
}
|
2020-03-25 22:07:03 +01:00
|
|
|
}
|
|
|
|
|
2020-02-02 14:46:59 +01:00
|
|
|
PreservedAnalyses AssumeBuilderPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
2020-05-10 18:20:34 +02:00
|
|
|
AssumptionCache* AC = AM.getCachedResult<AssumptionAnalysis>(F);
|
|
|
|
DominatorTree* DT = AM.getCachedResult<DominatorTreeAnalysis>(F);
|
2020-02-02 14:46:59 +01:00
|
|
|
for (Instruction &I : instructions(F))
|
2020-05-10 18:20:34 +02:00
|
|
|
salvageKnowledge(&I, AC, DT);
|
2020-02-02 14:46:59 +01:00
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|