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-07 13:41:20 +02:00
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
2020-05-10 19:26:41 +02:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2020-07-14 21:41:45 +02:00
|
|
|
#include "llvm/ADT/Statistic.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"
|
2020-05-07 13:41:20 +02:00
|
|
|
#include "llvm/InitializePasses.h"
|
2020-02-02 14:46:59 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2020-07-14 21:41:45 +02:00
|
|
|
#include "llvm/Support/DebugCounter.h"
|
2020-05-07 13:41:20 +02:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2020-02-02 14:46:59 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2021-05-07 20:15:43 +02:00
|
|
|
namespace llvm {
|
2020-02-02 14:46:59 +01:00
|
|
|
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"));
|
2021-05-07 20:15:43 +02:00
|
|
|
} // namespace llvm
|
2020-03-13 14:14:55 +01:00
|
|
|
|
2021-02-09 19:20:50 +01:00
|
|
|
#define DEBUG_TYPE "assume-builder"
|
|
|
|
|
2020-07-14 21:41:45 +02:00
|
|
|
STATISTIC(NumAssumeBuilt, "Number of assume built by the assume builder");
|
|
|
|
STATISTIC(NumBundlesInAssumes, "Total number of Bundles in the assume built");
|
|
|
|
STATISTIC(NumAssumesMerged,
|
|
|
|
"Number of assume merged by the assume simplify pass");
|
|
|
|
STATISTIC(NumAssumesRemoved,
|
|
|
|
"Number of assume removed by the assume simplify pass");
|
|
|
|
|
|
|
|
DEBUG_COUNTER(BuildAssumeCounter, "assume-builder-counter",
|
|
|
|
"Controls which assumes gets created");
|
|
|
|
|
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:
|
2020-10-12 09:23:18 +02:00
|
|
|
case Attribute::NoUndef:
|
2020-03-13 14:35:26 +01:00
|
|
|
case Attribute::Alignment:
|
|
|
|
case Attribute::Dereferenceable:
|
|
|
|
case Attribute::DereferenceableOrNull:
|
|
|
|
case Attribute::Cold:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 10:32:09 +02:00
|
|
|
/// This function will try to transform the given knowledge into a more
|
|
|
|
/// canonical one. the canonical knowledge maybe the given one.
|
2021-02-09 19:20:50 +01:00
|
|
|
RetainedKnowledge canonicalizedKnowledge(RetainedKnowledge RK, DataLayout DL) {
|
2020-06-19 10:32:09 +02:00
|
|
|
switch (RK.AttrKind) {
|
|
|
|
default:
|
|
|
|
return RK;
|
|
|
|
case Attribute::NonNull:
|
2020-07-31 11:09:54 +02:00
|
|
|
RK.WasOn = getUnderlyingObject(RK.WasOn);
|
2020-06-19 10:32:09 +02:00
|
|
|
return RK;
|
|
|
|
case Attribute::Alignment: {
|
|
|
|
Value *V = RK.WasOn->stripInBoundsOffsets([&](const Value *Strip) {
|
|
|
|
if (auto *GEP = dyn_cast<GEPOperator>(Strip))
|
|
|
|
RK.ArgValue =
|
2021-02-09 19:20:50 +01:00
|
|
|
MinAlign(RK.ArgValue, GEP->getMaxPreservedAlignment(DL).value());
|
2020-06-19 10:32:09 +02:00
|
|
|
});
|
|
|
|
RK.WasOn = V;
|
|
|
|
return RK;
|
|
|
|
}
|
|
|
|
case Attribute::Dereferenceable:
|
|
|
|
case Attribute::DereferenceableOrNull: {
|
|
|
|
int64_t Offset = 0;
|
2021-02-09 19:20:50 +01:00
|
|
|
Value *V = GetPointerBaseWithConstantOffset(RK.WasOn, Offset, DL,
|
|
|
|
/*AllowNonInBounds*/ false);
|
2020-06-19 10:32:09 +02:00
|
|
|
if (Offset < 0)
|
|
|
|
return RK;
|
|
|
|
RK.ArgValue = RK.ArgValue + Offset;
|
|
|
|
RK.WasOn = V;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RK;
|
|
|
|
}
|
|
|
|
|
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;
|
2021-02-09 19:20:50 +01:00
|
|
|
Instruction *InstBeingModified = 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)
|
2021-02-09 19:20:50 +01:00
|
|
|
: M(M), InstBeingModified(I), AC(AC), DT(DT) {}
|
2020-05-10 18:20:34 +02:00
|
|
|
|
|
|
|
bool tryToPreserveWithoutAddingAssume(RetainedKnowledge RK) {
|
2021-02-09 19:20:50 +01:00
|
|
|
if (!InstBeingModified || !RK.WasOn)
|
2020-05-10 18:20:34 +02:00
|
|
|
return false;
|
|
|
|
bool HasBeenPreserved = false;
|
|
|
|
Use* ToUpdate = nullptr;
|
|
|
|
getKnowledgeForValue(
|
|
|
|
RK.WasOn, {RK.AttrKind}, AC,
|
|
|
|
[&](RetainedKnowledge RKOther, Instruction *Assume,
|
|
|
|
const CallInst::BundleOpInfo *Bundle) {
|
2021-02-09 19:20:50 +01:00
|
|
|
if (!isValidAssumeForContext(Assume, InstBeingModified, DT))
|
2020-05-10 18:20:34 +02:00
|
|
|
return false;
|
|
|
|
if (RKOther.ArgValue >= RK.ArgValue) {
|
|
|
|
HasBeenPreserved = true;
|
|
|
|
return true;
|
2021-02-09 19:20:50 +01:00
|
|
|
} else if (isValidAssumeForContext(InstBeingModified, Assume, DT)) {
|
2020-05-10 18:20:34 +02:00
|
|
|
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-06-19 10:32:09 +02:00
|
|
|
bool isKnowledgeWorthPreserving(RetainedKnowledge RK) {
|
|
|
|
if (!RK)
|
|
|
|
return false;
|
|
|
|
if (!RK.WasOn)
|
|
|
|
return true;
|
|
|
|
if (RK.WasOn->getType()->isPointerTy()) {
|
2020-07-31 11:09:54 +02:00
|
|
|
Value *UnderlyingPtr = getUnderlyingObject(RK.WasOn);
|
2020-06-19 10:32:09 +02:00
|
|
|
if (isa<AllocaInst>(UnderlyingPtr) || isa<GlobalValue>(UnderlyingPtr))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (auto *Arg = dyn_cast<Argument>(RK.WasOn)) {
|
|
|
|
if (Arg->hasAttribute(RK.AttrKind) &&
|
2021-07-12 21:25:46 +02:00
|
|
|
(!Attribute::isIntAttrKind(RK.AttrKind) ||
|
2020-06-19 10:32:09 +02:00
|
|
|
Arg->getAttribute(RK.AttrKind).getValueAsInt() >= RK.ArgValue))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (auto *Inst = dyn_cast<Instruction>(RK.WasOn))
|
|
|
|
if (wouldInstructionBeTriviallyDead(Inst)) {
|
|
|
|
if (RK.WasOn->use_empty())
|
|
|
|
return false;
|
|
|
|
Use *SingleUse = RK.WasOn->getSingleUndroppableUse();
|
2021-02-09 19:20:50 +01:00
|
|
|
if (SingleUse && SingleUse->getUser() == InstBeingModified)
|
2020-06-19 10:32:09 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 22:34:55 +02:00
|
|
|
void addKnowledge(RetainedKnowledge RK) {
|
2021-02-09 19:20:50 +01:00
|
|
|
RK = canonicalizedKnowledge(RK, M->getDataLayout());
|
2020-06-19 10:32:09 +02:00
|
|
|
|
|
|
|
if (!isKnowledgeWorthPreserving(RK))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (tryToPreserveWithoutAddingAssume(RK))
|
2020-05-10 18:20:34 +02:00
|
|
|
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++)
|
2021-03-16 01:20:49 +01:00
|
|
|
for (Attribute Attr : AttrList.getAttributes(Idx)) {
|
|
|
|
bool IsPoisonAttr = Attr.hasAttribute(Attribute::NonNull) ||
|
|
|
|
Attr.hasAttribute(Attribute::Alignment);
|
|
|
|
if (!IsPoisonAttr || Call->isPassingUndefUB(Idx - 1))
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:45:20 +02:00
|
|
|
AssumeInst *build() {
|
2020-04-24 22:34:55 +02:00
|
|
|
if (AssumedKnowledgeMap.empty())
|
2020-02-02 14:46:59 +01:00
|
|
|
return nullptr;
|
2020-07-14 21:41:45 +02:00
|
|
|
if (!DebugCounter::shouldExecute(BuildAssumeCounter))
|
|
|
|
return nullptr;
|
2020-02-02 14:46:59 +01:00
|
|
|
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-07-14 21:41:45 +02:00
|
|
|
NumBundlesInAssumes++;
|
2020-02-02 14:46:59 +01:00
|
|
|
}
|
2020-07-14 21:41:45 +02:00
|
|
|
NumAssumeBuilt++;
|
2021-04-06 21:45:20 +02:00
|
|
|
return cast<AssumeInst>(CallInst::Create(
|
2020-03-25 18:28:51 +01:00
|
|
|
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
|
|
|
|
|
2021-04-06 21:45:20 +02:00
|
|
|
AssumeInst *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-11 11:50:35 +02:00
|
|
|
void llvm::salvageKnowledge(Instruction *I, AssumptionCache *AC,
|
|
|
|
DominatorTree *DT) {
|
|
|
|
if (!EnableKnowledgeRetention || I->isTerminator())
|
2020-04-24 22:34:55 +02:00
|
|
|
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);
|
2021-04-06 21:45:20 +02:00
|
|
|
if (auto *Intr = Builder.build()) {
|
2020-03-25 22:07:03 +01:00
|
|
|
Intr->insertBefore(I);
|
2020-04-13 11:27:27 +02:00
|
|
|
if (AC)
|
2021-04-06 21:45:20 +02:00
|
|
|
AC->registerAssumption(Intr);
|
2020-04-13 11:27:27 +02:00
|
|
|
}
|
2020-03-25 22:07:03 +01:00
|
|
|
}
|
2021-02-09 19:20:50 +01:00
|
|
|
|
2021-04-06 21:45:20 +02:00
|
|
|
AssumeInst *
|
2021-02-09 19:20:50 +01:00
|
|
|
llvm::buildAssumeFromKnowledge(ArrayRef<RetainedKnowledge> Knowledge,
|
|
|
|
Instruction *CtxI, AssumptionCache *AC,
|
|
|
|
DominatorTree *DT) {
|
|
|
|
AssumeBuilderState Builder(CtxI->getModule(), CtxI, AC, DT);
|
|
|
|
for (const RetainedKnowledge &RK : Knowledge)
|
|
|
|
Builder.addKnowledge(RK);
|
|
|
|
return Builder.build();
|
|
|
|
}
|
|
|
|
|
2021-04-06 21:45:20 +02:00
|
|
|
RetainedKnowledge llvm::simplifyRetainedKnowledge(AssumeInst *Assume,
|
2021-02-09 19:20:50 +01:00
|
|
|
RetainedKnowledge RK,
|
|
|
|
AssumptionCache *AC,
|
|
|
|
DominatorTree *DT) {
|
|
|
|
AssumeBuilderState Builder(Assume->getModule(), Assume, AC, DT);
|
|
|
|
RK = canonicalizedKnowledge(RK, Assume->getModule()->getDataLayout());
|
|
|
|
|
|
|
|
if (!Builder.isKnowledgeWorthPreserving(RK))
|
|
|
|
return RetainedKnowledge::none();
|
|
|
|
|
|
|
|
if (Builder.tryToPreserveWithoutAddingAssume(RK))
|
|
|
|
return RetainedKnowledge::none();
|
|
|
|
return RK;
|
|
|
|
}
|
2020-03-25 22:07:03 +01:00
|
|
|
|
2020-05-07 13:41:20 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct AssumeSimplify {
|
|
|
|
Function &F;
|
|
|
|
AssumptionCache &AC;
|
|
|
|
DominatorTree *DT;
|
|
|
|
LLVMContext &C;
|
|
|
|
SmallDenseSet<IntrinsicInst *> CleanupToDo;
|
|
|
|
StringMapEntry<uint32_t> *IgnoreTag;
|
|
|
|
SmallDenseMap<BasicBlock *, SmallVector<IntrinsicInst *, 4>, 8> BBToAssume;
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
AssumeSimplify(Function &F, AssumptionCache &AC, DominatorTree *DT,
|
|
|
|
LLVMContext &C)
|
|
|
|
: F(F), AC(AC), DT(DT), C(C),
|
|
|
|
IgnoreTag(C.getOrInsertBundleTag(IgnoreBundleTag)) {}
|
|
|
|
|
|
|
|
void buildMapping(bool FilterBooleanArgument) {
|
|
|
|
BBToAssume.clear();
|
|
|
|
for (Value *V : AC.assumptions()) {
|
|
|
|
if (!V)
|
|
|
|
continue;
|
|
|
|
IntrinsicInst *Assume = cast<IntrinsicInst>(V);
|
|
|
|
if (FilterBooleanArgument) {
|
|
|
|
auto *Arg = dyn_cast<ConstantInt>(Assume->getOperand(0));
|
|
|
|
if (!Arg || Arg->isZero())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
BBToAssume[Assume->getParent()].push_back(Assume);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &Elem : BBToAssume) {
|
|
|
|
llvm::sort(Elem.second,
|
|
|
|
[](const IntrinsicInst *LHS, const IntrinsicInst *RHS) {
|
|
|
|
return LHS->comesBefore(RHS);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove all asumes in CleanupToDo if there boolean argument is true and
|
|
|
|
/// ForceCleanup is set or the assume doesn't hold valuable knowledge.
|
|
|
|
void RunCleanup(bool ForceCleanup) {
|
|
|
|
for (IntrinsicInst *Assume : CleanupToDo) {
|
|
|
|
auto *Arg = dyn_cast<ConstantInt>(Assume->getOperand(0));
|
|
|
|
if (!Arg || Arg->isZero() ||
|
2021-04-06 21:45:20 +02:00
|
|
|
(!ForceCleanup &&
|
|
|
|
!isAssumeWithEmptyBundle(cast<AssumeInst>(*Assume))))
|
2020-05-07 13:41:20 +02:00
|
|
|
continue;
|
|
|
|
MadeChange = true;
|
2020-07-14 21:41:45 +02:00
|
|
|
if (ForceCleanup)
|
|
|
|
NumAssumesMerged++;
|
|
|
|
else
|
|
|
|
NumAssumesRemoved++;
|
2020-05-07 13:41:20 +02:00
|
|
|
Assume->eraseFromParent();
|
|
|
|
}
|
|
|
|
CleanupToDo.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove knowledge stored in assume when it is already know by an attribute
|
|
|
|
/// or an other assume. This can when valid update an existing knowledge in an
|
|
|
|
/// attribute or an other assume.
|
|
|
|
void dropRedundantKnowledge() {
|
|
|
|
struct MapValue {
|
|
|
|
IntrinsicInst *Assume;
|
|
|
|
unsigned ArgValue;
|
|
|
|
CallInst::BundleOpInfo *BOI;
|
|
|
|
};
|
|
|
|
buildMapping(false);
|
|
|
|
SmallDenseMap<std::pair<Value *, Attribute::AttrKind>,
|
|
|
|
SmallVector<MapValue, 2>, 16>
|
|
|
|
Knowledge;
|
|
|
|
for (BasicBlock *BB : depth_first(&F))
|
|
|
|
for (Value *V : BBToAssume[BB]) {
|
|
|
|
if (!V)
|
|
|
|
continue;
|
|
|
|
IntrinsicInst *Assume = cast<IntrinsicInst>(V);
|
|
|
|
for (CallInst::BundleOpInfo &BOI : Assume->bundle_op_infos()) {
|
|
|
|
auto RemoveFromAssume = [&]() {
|
|
|
|
CleanupToDo.insert(Assume);
|
|
|
|
if (BOI.Begin != BOI.End) {
|
|
|
|
Use *U = &Assume->op_begin()[BOI.Begin + ABA_WasOn];
|
|
|
|
U->set(UndefValue::get(U->get()->getType()));
|
|
|
|
}
|
|
|
|
BOI.Tag = IgnoreTag;
|
|
|
|
};
|
|
|
|
if (BOI.Tag == IgnoreTag) {
|
|
|
|
CleanupToDo.insert(Assume);
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-06 21:45:20 +02:00
|
|
|
RetainedKnowledge RK =
|
|
|
|
getKnowledgeFromBundle(cast<AssumeInst>(*Assume), BOI);
|
2020-05-07 13:41:20 +02:00
|
|
|
if (auto *Arg = dyn_cast_or_null<Argument>(RK.WasOn)) {
|
|
|
|
bool HasSameKindAttr = Arg->hasAttribute(RK.AttrKind);
|
|
|
|
if (HasSameKindAttr)
|
2021-07-12 21:25:46 +02:00
|
|
|
if (!Attribute::isIntAttrKind(RK.AttrKind) ||
|
2020-05-07 13:41:20 +02:00
|
|
|
Arg->getAttribute(RK.AttrKind).getValueAsInt() >=
|
|
|
|
RK.ArgValue) {
|
|
|
|
RemoveFromAssume();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isValidAssumeForContext(
|
|
|
|
Assume, &*F.getEntryBlock().getFirstInsertionPt()) ||
|
|
|
|
Assume == &*F.getEntryBlock().getFirstInsertionPt()) {
|
|
|
|
if (HasSameKindAttr)
|
|
|
|
Arg->removeAttr(RK.AttrKind);
|
|
|
|
Arg->addAttr(Attribute::get(C, RK.AttrKind, RK.ArgValue));
|
|
|
|
MadeChange = true;
|
|
|
|
RemoveFromAssume();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto &Lookup = Knowledge[{RK.WasOn, RK.AttrKind}];
|
|
|
|
for (MapValue &Elem : Lookup) {
|
|
|
|
if (!isValidAssumeForContext(Elem.Assume, Assume, DT))
|
|
|
|
continue;
|
|
|
|
if (Elem.ArgValue >= RK.ArgValue) {
|
|
|
|
RemoveFromAssume();
|
|
|
|
continue;
|
|
|
|
} else if (isValidAssumeForContext(Assume, Elem.Assume, DT)) {
|
|
|
|
Elem.Assume->op_begin()[Elem.BOI->Begin + ABA_Argument].set(
|
|
|
|
ConstantInt::get(Type::getInt64Ty(C), RK.ArgValue));
|
|
|
|
MadeChange = true;
|
|
|
|
RemoveFromAssume();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Lookup.push_back({Assume, RK.ArgValue, &BOI});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
using MergeIterator = SmallVectorImpl<IntrinsicInst *>::iterator;
|
|
|
|
|
|
|
|
/// Merge all Assumes from Begin to End in and insert the resulting assume as
|
|
|
|
/// high as possible in the basicblock.
|
|
|
|
void mergeRange(BasicBlock *BB, MergeIterator Begin, MergeIterator End) {
|
|
|
|
if (Begin == End || std::next(Begin) == End)
|
|
|
|
return;
|
|
|
|
/// Provide no additional information so that AssumeBuilderState doesn't
|
|
|
|
/// try to do any punning since it already has been done better.
|
|
|
|
AssumeBuilderState Builder(F.getParent());
|
|
|
|
|
|
|
|
/// For now it is initialized to the best value it could have
|
|
|
|
Instruction *InsertPt = BB->getFirstNonPHI();
|
|
|
|
if (isa<LandingPadInst>(InsertPt))
|
|
|
|
InsertPt = InsertPt->getNextNode();
|
|
|
|
for (IntrinsicInst *I : make_range(Begin, End)) {
|
|
|
|
CleanupToDo.insert(I);
|
|
|
|
for (CallInst::BundleOpInfo &BOI : I->bundle_op_infos()) {
|
2021-04-06 21:45:20 +02:00
|
|
|
RetainedKnowledge RK =
|
|
|
|
getKnowledgeFromBundle(cast<AssumeInst>(*I), BOI);
|
2020-05-07 13:41:20 +02:00
|
|
|
if (!RK)
|
|
|
|
continue;
|
|
|
|
Builder.addKnowledge(RK);
|
|
|
|
if (auto *I = dyn_cast_or_null<Instruction>(RK.WasOn))
|
|
|
|
if (I->getParent() == InsertPt->getParent() &&
|
|
|
|
(InsertPt->comesBefore(I) || InsertPt == I))
|
|
|
|
InsertPt = I->getNextNode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adjust InsertPt if it is before Begin, since mergeAssumes only
|
|
|
|
/// guarantees we can place the resulting assume between Begin and End.
|
|
|
|
if (InsertPt->comesBefore(*Begin))
|
|
|
|
for (auto It = (*Begin)->getIterator(), E = InsertPt->getIterator();
|
|
|
|
It != E; --It)
|
|
|
|
if (!isGuaranteedToTransferExecutionToSuccessor(&*It)) {
|
|
|
|
InsertPt = It->getNextNode();
|
|
|
|
break;
|
|
|
|
}
|
2021-04-06 21:45:20 +02:00
|
|
|
auto *MergedAssume = Builder.build();
|
2020-05-07 13:41:20 +02:00
|
|
|
if (!MergedAssume)
|
|
|
|
return;
|
|
|
|
MadeChange = true;
|
|
|
|
MergedAssume->insertBefore(InsertPt);
|
2021-04-06 21:45:20 +02:00
|
|
|
AC.registerAssumption(MergedAssume);
|
2020-05-07 13:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Merge assume when they are in the same BasicBlock and for all instruction
|
|
|
|
/// between them isGuaranteedToTransferExecutionToSuccessor returns true.
|
|
|
|
void mergeAssumes() {
|
|
|
|
buildMapping(true);
|
|
|
|
|
|
|
|
SmallVector<MergeIterator, 4> SplitPoints;
|
|
|
|
for (auto &Elem : BBToAssume) {
|
|
|
|
SmallVectorImpl<IntrinsicInst *> &AssumesInBB = Elem.second;
|
|
|
|
if (AssumesInBB.size() < 2)
|
|
|
|
continue;
|
|
|
|
/// AssumesInBB is already sorted by order in the block.
|
|
|
|
|
|
|
|
BasicBlock::iterator It = AssumesInBB.front()->getIterator();
|
|
|
|
BasicBlock::iterator E = AssumesInBB.back()->getIterator();
|
|
|
|
SplitPoints.push_back(AssumesInBB.begin());
|
|
|
|
MergeIterator LastSplit = AssumesInBB.begin();
|
|
|
|
for (; It != E; ++It)
|
|
|
|
if (!isGuaranteedToTransferExecutionToSuccessor(&*It)) {
|
|
|
|
for (; (*LastSplit)->comesBefore(&*It); ++LastSplit)
|
|
|
|
;
|
|
|
|
if (SplitPoints.back() != LastSplit)
|
|
|
|
SplitPoints.push_back(LastSplit);
|
|
|
|
}
|
|
|
|
SplitPoints.push_back(AssumesInBB.end());
|
|
|
|
for (auto SplitIt = SplitPoints.begin();
|
|
|
|
SplitIt != std::prev(SplitPoints.end()); SplitIt++) {
|
|
|
|
mergeRange(Elem.first, *SplitIt, *(SplitIt + 1));
|
|
|
|
}
|
|
|
|
SplitPoints.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool simplifyAssumes(Function &F, AssumptionCache *AC, DominatorTree *DT) {
|
|
|
|
AssumeSimplify AS(F, *AC, DT, F.getContext());
|
|
|
|
|
|
|
|
/// Remove knowledge that is already known by a dominating other assume or an
|
|
|
|
/// attribute.
|
|
|
|
AS.dropRedundantKnowledge();
|
|
|
|
|
|
|
|
/// Remove assume that are empty.
|
|
|
|
AS.RunCleanup(false);
|
|
|
|
|
|
|
|
/// Merge assume in the same basicblock when possible.
|
|
|
|
AS.mergeAssumes();
|
|
|
|
|
|
|
|
/// Remove assume that were merged.
|
|
|
|
AS.RunCleanup(true);
|
|
|
|
return AS.MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
PreservedAnalyses AssumeSimplifyPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
|
|
|
if (!EnableKnowledgeRetention)
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
simplifyAssumes(F, &AM.getResult<AssumptionAnalysis>(F),
|
|
|
|
AM.getCachedResult<DominatorTreeAnalysis>(F));
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
}
|
|
|
|
|
2020-05-19 22:16:15 +02:00
|
|
|
namespace {
|
2020-05-07 13:41:20 +02:00
|
|
|
class AssumeSimplifyPassLegacyPass : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
AssumeSimplifyPassLegacyPass() : FunctionPass(ID) {
|
|
|
|
initializeAssumeSimplifyPassLegacyPassPass(
|
|
|
|
*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
bool runOnFunction(Function &F) override {
|
|
|
|
if (skipFunction(F) || !EnableKnowledgeRetention)
|
|
|
|
return false;
|
|
|
|
AssumptionCache &AC =
|
|
|
|
getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
2020-07-04 19:04:28 +02:00
|
|
|
DominatorTreeWrapperPass *DTWP =
|
|
|
|
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
|
|
|
|
return simplifyAssumes(F, &AC, DTWP ? &DTWP->getDomTree() : nullptr);
|
2020-05-07 13:41:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2020-07-04 19:04:28 +02:00
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
|
|
|
|
2020-05-07 13:41:20 +02:00
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2020-05-19 22:16:15 +02:00
|
|
|
} // namespace
|
2020-05-07 13:41:20 +02:00
|
|
|
|
|
|
|
char AssumeSimplifyPassLegacyPass::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(AssumeSimplifyPassLegacyPass, "assume-simplify",
|
|
|
|
"Assume Simplify", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
|
|
|
INITIALIZE_PASS_END(AssumeSimplifyPassLegacyPass, "assume-simplify",
|
|
|
|
"Assume Simplify", false, false)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createAssumeSimplifyPass() {
|
|
|
|
return new AssumeSimplifyPassLegacyPass();
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:46:59 +01:00
|
|
|
PreservedAnalyses AssumeBuilderPass::run(Function &F,
|
|
|
|
FunctionAnalysisManager &AM) {
|
2020-07-04 19:04:28 +02:00
|
|
|
AssumptionCache *AC = &AM.getResult<AssumptionAnalysis>(F);
|
2020-05-10 18:20:34 +02:00
|
|
|
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();
|
|
|
|
}
|
2020-07-04 19:04:28 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class AssumeBuilderPassLegacyPass : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
AssumeBuilderPassLegacyPass() : FunctionPass(ID) {
|
|
|
|
initializeAssumeBuilderPassLegacyPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
bool runOnFunction(Function &F) override {
|
|
|
|
AssumptionCache &AC =
|
|
|
|
getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
|
|
|
DominatorTreeWrapperPass *DTWP =
|
|
|
|
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
|
|
|
|
for (Instruction &I : instructions(F))
|
|
|
|
salvageKnowledge(&I, &AC, DTWP ? &DTWP->getDomTree() : nullptr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
|
|
|
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
char AssumeBuilderPassLegacyPass::ID = 0;
|
|
|
|
|
|
|
|
INITIALIZE_PASS_BEGIN(AssumeBuilderPassLegacyPass, "assume-builder",
|
|
|
|
"Assume Builder", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
|
|
|
INITIALIZE_PASS_END(AssumeBuilderPassLegacyPass, "assume-builder",
|
|
|
|
"Assume Builder", false, false)
|