#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Statepoint.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/User.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define DEBUG_TYPE "rewrite-statepoints-for-gc"
using namespace llvm;
static cl::opt<bool> PrintLiveSet("spp-print-liveset", cl::Hidden,
cl::init(false));
static cl::opt<bool> PrintLiveSetSize("spp-print-liveset-size", cl::Hidden,
cl::init(false));
static cl::opt<bool> PrintBasePointers("spp-print-base-pointers", cl::Hidden,
cl::init(false));
static cl::opt<unsigned>
RematerializationThreshold("spp-rematerialization-threshold", cl::Hidden,
cl::init(6));
#ifdef EXPENSIVE_CHECKS
static bool ClobberNonLive = true;
#else
static bool ClobberNonLive = false;
#endif
static cl::opt<bool, true> ClobberNonLiveOverride("rs4gc-clobber-non-live",
cl::location(ClobberNonLive),
cl::Hidden);
static cl::opt<bool>
AllowStatepointWithNoDeoptInfo("rs4gc-allow-statepoint-with-no-deopt-info",
cl::Hidden, cl::init(true));
static void stripNonValidData(Module &M);
static bool shouldRewriteStatepointsIn(Function &F);
PreservedAnalyses RewriteStatepointsForGC::run(Module &M,
ModuleAnalysisManager &AM) {
bool Changed = false;
auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
for (Function &F : M) {
if (F.isDeclaration() || F.empty())
continue;
if (!shouldRewriteStatepointsIn(F))
continue;
auto &DT = FAM.getResult<DominatorTreeAnalysis>(F);
auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
auto &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
Changed |= runOnFunction(F, DT, TTI, TLI);
}
if (!Changed)
return PreservedAnalyses::all();
stripNonValidData(M);
PreservedAnalyses PA;
PA.preserve<TargetIRAnalysis>();
PA.preserve<TargetLibraryAnalysis>();
return PA;
}
namespace {
class RewriteStatepointsForGCLegacyPass : public ModulePass {
RewriteStatepointsForGC Impl;
public:
static char ID;
RewriteStatepointsForGCLegacyPass() : ModulePass(ID), Impl() {
initializeRewriteStatepointsForGCLegacyPassPass(
*PassRegistry::getPassRegistry());
}
bool runOnModule(Module &M) override {
bool Changed = false;
for (Function &F : M) {
if (F.isDeclaration() || F.empty())
continue;
if (!shouldRewriteStatepointsIn(F))
continue;
TargetTransformInfo &TTI =
getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
const TargetLibraryInfo &TLI =
getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
auto &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
Changed |= Impl.runOnFunction(F, DT, TTI, TLI);
}
if (!Changed)
return false;
stripNonValidData(M);
return true;
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<TargetTransformInfoWrapperPass>();
AU.addRequired<TargetLibraryInfoWrapperPass>();
}
};
}
char RewriteStatepointsForGCLegacyPass::ID = 0;
ModulePass *llvm::createRewriteStatepointsForGCLegacyPass() {
return new RewriteStatepointsForGCLegacyPass();
}
INITIALIZE_PASS_BEGIN(RewriteStatepointsForGCLegacyPass,
"rewrite-statepoints-for-gc",
"Make relocations explicit at statepoints", false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
INITIALIZE_PASS_END(RewriteStatepointsForGCLegacyPass,
"rewrite-statepoints-for-gc",
"Make relocations explicit at statepoints", false, false)
namespace {
struct GCPtrLivenessData {
MapVector<BasicBlock *, SetVector<Value *>> KillSet;
MapVector<BasicBlock *, SetVector<Value *>> LiveSet;
MapVector<BasicBlock *, SetVector<Value *>> LiveIn;
MapVector<BasicBlock *, SetVector<Value *>> LiveOut;
};
using DefiningValueMapTy = MapVector<Value *, Value *>;
using IsKnownBaseMapTy = MapVector<Value *, bool>;
using PointerToBaseTy = MapVector<Value *, Value *>;
using StatepointLiveSetTy = SetVector<Value *>;
using RematerializedValueMapTy =
MapVector<AssertingVH<Instruction>, AssertingVH<Value>>;
struct PartiallyConstructedSafepointRecord {
StatepointLiveSetTy LiveSet;
GCStatepointInst *StatepointToken;
Instruction *UnwindToken;
RematerializedValueMapTy RematerializedValues;
};
struct RematerizlizationCandidateRecord {
SmallVector<Instruction *, 3> ChainToBase;
Value *RootOfChain;
InstructionCost Cost;
};
using RematCandTy = MapVector<Value *, RematerizlizationCandidateRecord>;
}
static ArrayRef<Use> GetDeoptBundleOperands(const CallBase *Call) {
Optional<OperandBundleUse> DeoptBundle =
Call->getOperandBundle(LLVMContext::OB_deopt);
if (!DeoptBundle) {
assert(AllowStatepointWithNoDeoptInfo &&
"Found non-leaf call without deopt info!");
return None;
}
return DeoptBundle->Inputs;
}
static void computeLiveInValues(DominatorTree &DT, Function &F,
GCPtrLivenessData &Data);
static void findLiveSetAtInst(Instruction *inst, GCPtrLivenessData &Data,
StatepointLiveSetTy &out);
static bool isGCPointerType(Type *T) {
if (auto *PT = dyn_cast<PointerType>(T))
return PT->getAddressSpace() == 1;
return false;
}
static bool isHandledGCPointerType(Type *T) {
if (isGCPointerType(T))
return true;
if (auto VT = dyn_cast<VectorType>(T))
if (isGCPointerType(VT->getElementType()))
return true;
return false;
}
#ifndef NDEBUG
static bool containsGCPtrType(Type *Ty) {
if (isGCPointerType(Ty))
return true;
if (VectorType *VT = dyn_cast<VectorType>(Ty))
return isGCPointerType(VT->getScalarType());
if (ArrayType *AT = dyn_cast<ArrayType>(Ty))
return containsGCPtrType(AT->getElementType());
if (StructType *ST = dyn_cast<StructType>(Ty))
return llvm::any_of(ST->elements(), containsGCPtrType);
return false;
}
static bool isUnhandledGCPointerType(Type *Ty) {
return containsGCPtrType(Ty) && !isHandledGCPointerType(Ty);
}
#endif
static std::string suffixed_name_or(Value *V, StringRef Suffix,
StringRef DefaultName) {
return V->hasName() ? (V->getName() + Suffix).str() : DefaultName.str();
}
static void analyzeParsePointLiveness(
DominatorTree &DT, GCPtrLivenessData &OriginalLivenessData, CallBase *Call,
PartiallyConstructedSafepointRecord &Result) {
StatepointLiveSetTy LiveSet;
findLiveSetAtInst(Call, OriginalLivenessData, LiveSet);
if (PrintLiveSet) {
dbgs() << "Live Variables:\n";
for (Value *V : LiveSet)
dbgs() << " " << V->getName() << " " << *V << "\n";
}
if (PrintLiveSetSize) {
dbgs() << "Safepoint For: " << Call->getCalledOperand()->getName() << "\n";
dbgs() << "Number live values: " << LiveSet.size() << "\n";
}
Result.LiveSet = LiveSet;
}
static bool isKnownBase(Value *V, const IsKnownBaseMapTy &KnownBases);
static void setKnownBase(Value *V, bool IsKnownBase,
IsKnownBaseMapTy &KnownBases);
static Value *findBaseDefiningValue(Value *I, DefiningValueMapTy &Cache,
IsKnownBaseMapTy &KnownBases);
static Value *findBaseDefiningValueOfVector(Value *I, DefiningValueMapTy &Cache,
IsKnownBaseMapTy &KnownBases) {
auto Cached = Cache.find(I);
if (Cached != Cache.end())
return Cached->second;
if (isa<Argument>(I)) {
Cache[I] = I;
setKnownBase(I, true, KnownBases);
return I;
}
if (isa<Constant>(I)) {
auto *CAZ = ConstantAggregateZero::get(I->getType());
Cache[I] = CAZ;
setKnownBase(CAZ, true, KnownBases);
return CAZ;
}
if (isa<LoadInst>(I)) {
Cache[I] = I;
setKnownBase(I, true, KnownBases);
return I;
}
if (isa<InsertElementInst>(I)) {
Cache[I] = I;
setKnownBase(I, false, KnownBases);
return I;
}
if (isa<ShuffleVectorInst>(I)) {
Cache[I] = I;
setKnownBase(I, false, KnownBases);
return I;
}
if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
auto *BDV =
findBaseDefiningValue(GEP->getPointerOperand(), Cache, KnownBases);
Cache[GEP] = BDV;
return BDV;
}
if (auto *Freeze = dyn_cast<FreezeInst>(I)) {
auto *BDV = findBaseDefiningValue(Freeze->getOperand(0), Cache, KnownBases);
Cache[Freeze] = BDV;
return BDV;
}
if (auto *BC = dyn_cast<BitCastInst>(I)) {
auto *BDV = findBaseDefiningValue(BC->getOperand(0), Cache, KnownBases);
Cache[BC] = BDV;
return BDV;
}
if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
Cache[I] = I;
setKnownBase(I, true, KnownBases);
return I;
}
assert((isa<SelectInst>(I) || isa<PHINode>(I)) &&
"unknown vector instruction - no base found for vector element");
Cache[I] = I;
setKnownBase(I, false, KnownBases);
return I;
}
static Value *findBaseDefiningValue(Value *I, DefiningValueMapTy &Cache,
IsKnownBaseMapTy &KnownBases) {
assert(I->getType()->isPtrOrPtrVectorTy() &&
"Illegal to ask for the base pointer of a non-pointer type");
auto Cached = Cache.find(I);
if (Cached != Cache.end())
return Cached->second;
if (I->getType()->isVectorTy())
return findBaseDefiningValueOfVector(I, Cache, KnownBases);
if (isa<Argument>(I)) {
Cache[I] = I;
setKnownBase(I, true, KnownBases);
return I;
}
if (isa<Constant>(I)) {
auto *CPN = ConstantPointerNull::get(cast<PointerType>(I->getType()));
Cache[I] = CPN;
setKnownBase(CPN, true, KnownBases);
return CPN;
}
if (isa<IntToPtrInst>(I)) {
Cache[I] = I;
setKnownBase(I, true, KnownBases);
return I;
}
if (CastInst *CI = dyn_cast<CastInst>(I)) {
Value *Def = CI->stripPointerCasts();
assert(cast<PointerType>(Def->getType())->getAddressSpace() ==
cast<PointerType>(CI->getType())->getAddressSpace() &&
"unsupported addrspacecast");
assert(!isa<CastInst>(Def) && "shouldn't find another cast here");
auto *BDV = findBaseDefiningValue(Def, Cache, KnownBases);
Cache[CI] = BDV;
return BDV;
}
if (isa<LoadInst>(I)) {
Cache[I] = I;
setKnownBase(I, true, KnownBases);
return I;
}
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I)) {
auto *BDV =
findBaseDefiningValue(GEP->getPointerOperand(), Cache, KnownBases);
Cache[GEP] = BDV;
return BDV;
}
if (auto *Freeze = dyn_cast<FreezeInst>(I)) {
auto *BDV = findBaseDefiningValue(Freeze->getOperand(0), Cache, KnownBases);
Cache[Freeze] = BDV;
return BDV;
}
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
switch (II->getIntrinsicID()) {
default:
break;
case Intrinsic::experimental_gc_statepoint:
llvm_unreachable("statepoints don't produce pointers");
case Intrinsic::experimental_gc_relocate:
llvm_unreachable("repeat safepoint insertion is not supported");
case Intrinsic::gcroot:
llvm_unreachable(
"interaction with the gcroot mechanism is not supported");
case Intrinsic::experimental_gc_get_pointer_base:
auto *BDV = findBaseDefiningValue(II->getOperand(0), Cache, KnownBases);
Cache[II] = BDV;
return BDV;
}
}
if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
Cache[I] = I;
setKnownBase(I, true, KnownBases);
return I;
}
assert(!isa<LandingPadInst>(I) && "Landing Pad is unimplemented");
if (isa<AtomicCmpXchgInst>(I)) {
Cache[I] = I;
setKnownBase(I, true, KnownBases);
return I;
}
assert(!isa<AtomicRMWInst>(I) && "Xchg handled above, all others are "
"binary ops which don't apply to pointers");
if (isa<ExtractValueInst>(I)) {
Cache[I] = I;
setKnownBase(I, true, KnownBases);
return I;
}
assert(!isa<InsertValueInst>(I) &&
"Base pointer for a struct is meaningless");
bool IsKnownBase =
isa<Instruction>(I) && cast<Instruction>(I)->getMetadata("is_base_value");
setKnownBase(I, IsKnownBase, KnownBases);
Cache[I] = I;
if (isa<ExtractElementInst>(I))
return I;
assert((isa<SelectInst>(I) || isa<PHINode>(I)) &&
"missing instruction case in findBaseDefiningValue");
return I;
}
static Value *findBaseDefiningValueCached(Value *I, DefiningValueMapTy &Cache,
IsKnownBaseMapTy &KnownBases) {
if (Cache.find(I) == Cache.end()) {
auto *BDV = findBaseDefiningValue(I, Cache, KnownBases);
Cache[I] = BDV;
LLVM_DEBUG(dbgs() << "fBDV-cached: " << I->getName() << " -> "
<< Cache[I]->getName() << ", is known base = "
<< KnownBases[I] << "\n");
}
assert(Cache[I] != nullptr);
assert(KnownBases.find(Cache[I]) != KnownBases.end() &&
"Cached value must be present in known bases map");
return Cache[I];
}
static Value *findBaseOrBDV(Value *I, DefiningValueMapTy &Cache,
IsKnownBaseMapTy &KnownBases) {
Value *Def = findBaseDefiningValueCached(I, Cache, KnownBases);
auto Found = Cache.find(Def);
if (Found != Cache.end()) {
return Found->second;
}
return Def;
}
#ifndef NDEBUG
static bool isOriginalBaseResult(Value *V) {
return !isa<PHINode>(V) && !isa<SelectInst>(V) &&
!isa<ExtractElementInst>(V) && !isa<InsertElementInst>(V) &&
!isa<ShuffleVectorInst>(V);
}
#endif
static bool isKnownBase(Value *V, const IsKnownBaseMapTy &KnownBases) {
auto It = KnownBases.find(V);
assert(It != KnownBases.end() && "Value not present in the map");
return It->second;
}
static void setKnownBase(Value *V, bool IsKnownBase,
IsKnownBaseMapTy &KnownBases) {
#ifndef NDEBUG
auto It = KnownBases.find(V);
if (It != KnownBases.end())
assert(It->second == IsKnownBase && "Changing already present value");
#endif
KnownBases[V] = IsKnownBase;
}
static bool areBothVectorOrScalar(Value *First, Value *Second) {
return isa<VectorType>(First->getType()) ==
isa<VectorType>(Second->getType());
}
namespace {
class BDVState {
public:
enum StatusTy {
Unknown,
Base,
Conflict
};
BDVState() {
llvm_unreachable("missing state in map");
}
explicit BDVState(Value *OriginalValue)
: OriginalValue(OriginalValue) {}
explicit BDVState(Value *OriginalValue, StatusTy Status, Value *BaseValue = nullptr)
: OriginalValue(OriginalValue), Status(Status), BaseValue(BaseValue) {
assert(Status != Base || BaseValue);
}
StatusTy getStatus() const { return Status; }
Value *getOriginalValue() const { return OriginalValue; }
Value *getBaseValue() const { return BaseValue; }
bool isBase() const { return getStatus() == Base; }
bool isUnknown() const { return getStatus() == Unknown; }
bool isConflict() const { return getStatus() == Conflict; }
void meet(const BDVState &Other) {
auto markConflict = [&]() {
Status = BDVState::Conflict;
BaseValue = nullptr;
};
if (isConflict())
return;
if (isUnknown()) {
Status = Other.getStatus();
BaseValue = Other.getBaseValue();
return;
}
assert(isBase() && "Unknown state");
if (Other.isUnknown())
return;
if (Other.isConflict())
return markConflict();
assert(Other.isBase() && "Unknown state");
if (getBaseValue() != Other.getBaseValue())
return markConflict();
}
bool operator==(const BDVState &Other) const {
return OriginalValue == Other.OriginalValue && BaseValue == Other.BaseValue &&
Status == Other.Status;
}
bool operator!=(const BDVState &other) const { return !(*this == other); }
LLVM_DUMP_METHOD
void dump() const {
print(dbgs());
dbgs() << '\n';
}
void print(raw_ostream &OS) const {
switch (getStatus()) {
case Unknown:
OS << "U";
break;
case Base:
OS << "B";
break;
case Conflict:
OS << "C";
break;
}
OS << " (base " << getBaseValue() << " - "
<< (getBaseValue() ? getBaseValue()->getName() : "nullptr") << ")"
<< " for " << OriginalValue->getName() << ":";
}
private:
AssertingVH<Value> OriginalValue; StatusTy Status = Unknown;
AssertingVH<Value> BaseValue = nullptr; };
}
#ifndef NDEBUG
static raw_ostream &operator<<(raw_ostream &OS, const BDVState &State) {
State.print(OS);
return OS;
}
#endif
static Value *findBasePointer(Value *I, DefiningValueMapTy &Cache,
IsKnownBaseMapTy &KnownBases) {
Value *Def = findBaseOrBDV(I, Cache, KnownBases);
if (isKnownBase(Def, KnownBases) && areBothVectorOrScalar(Def, I))
return Def;
#ifndef NDEBUG
auto isExpectedBDVType = [](Value *BDV) {
return isa<PHINode>(BDV) || isa<SelectInst>(BDV) ||
isa<ExtractElementInst>(BDV) || isa<InsertElementInst>(BDV) ||
isa<ShuffleVectorInst>(BDV);
};
#endif
MapVector<Value *, BDVState> States;
#ifndef NDEBUG
auto VerifyStates = [&]() {
for (auto &Entry : States) {
assert(Entry.first == Entry.second.getOriginalValue());
}
};
#endif
auto visitBDVOperands = [](Value *BDV, std::function<void (Value*)> F) {
if (PHINode *PN = dyn_cast<PHINode>(BDV)) {
for (Value *InVal : PN->incoming_values())
F(InVal);
} else if (SelectInst *SI = dyn_cast<SelectInst>(BDV)) {
F(SI->getTrueValue());
F(SI->getFalseValue());
} else if (auto *EE = dyn_cast<ExtractElementInst>(BDV)) {
F(EE->getVectorOperand());
} else if (auto *IE = dyn_cast<InsertElementInst>(BDV)) {
F(IE->getOperand(0));
F(IE->getOperand(1));
} else if (auto *SV = dyn_cast<ShuffleVectorInst>(BDV)) {
F(SV->getOperand(0));
if (!SV->isZeroEltSplat())
F(SV->getOperand(1));
} else {
llvm_unreachable("unexpected BDV type");
}
};
{
SmallVector<Value*, 16> Worklist;
Worklist.push_back(Def);
States.insert({Def, BDVState(Def)});
while (!Worklist.empty()) {
Value *Current = Worklist.pop_back_val();
assert(!isOriginalBaseResult(Current) && "why did it get added?");
auto visitIncomingValue = [&](Value *InVal) {
Value *Base = findBaseOrBDV(InVal, Cache, KnownBases);
if (isKnownBase(Base, KnownBases) && areBothVectorOrScalar(Base, InVal))
return;
assert(isExpectedBDVType(Base) && "the only non-base values "
"we see should be base defining values");
if (States.insert(std::make_pair(Base, BDVState(Base))).second)
Worklist.push_back(Base);
};
visitBDVOperands(Current, visitIncomingValue);
}
}
#ifndef NDEBUG
VerifyStates();
LLVM_DEBUG(dbgs() << "States after initialization:\n");
for (const auto &Pair : States) {
LLVM_DEBUG(dbgs() << " " << Pair.second << " for " << *Pair.first << "\n");
}
#endif
SmallVector<Value *> ToRemove;
do {
ToRemove.clear();
for (auto Pair : States) {
Value *BDV = Pair.first;
auto canPruneInput = [&](Value *V) {
if (V->stripPointerCasts() == BDV)
return true;
Value *VBDV = findBaseOrBDV(V, Cache, KnownBases);
if (V->stripPointerCasts() != VBDV)
return false;
return States.count(VBDV) == 0;
};
bool CanPrune = true;
visitBDVOperands(BDV, [&](Value *Op) {
CanPrune = CanPrune && canPruneInput(Op);
});
if (CanPrune)
ToRemove.push_back(BDV);
}
for (Value *V : ToRemove) {
States.erase(V);
Cache[V] = V;
}
} while (!ToRemove.empty());
if (!States.count(Def))
return Def;
auto GetStateForBDV = [&](Value *BaseValue, Value *Input) {
auto I = States.find(BaseValue);
if (I != States.end())
return I->second;
assert(areBothVectorOrScalar(BaseValue, Input));
return BDVState(BaseValue, BDVState::Base, BaseValue);
};
bool Progress = true;
while (Progress) {
#ifndef NDEBUG
const size_t OldSize = States.size();
#endif
Progress = false;
for (auto Pair : States) {
Value *BDV = Pair.first;
assert((!isKnownBase(BDV, KnownBases) ||
!areBothVectorOrScalar(BDV, Pair.second.getBaseValue())) &&
"why did it get added?");
BDVState NewState(BDV);
visitBDVOperands(BDV, [&](Value *Op) {
Value *BDV = findBaseOrBDV(Op, Cache, KnownBases);
auto OpState = GetStateForBDV(BDV, Op);
NewState.meet(OpState);
});
BDVState OldState = States[BDV];
if (OldState != NewState) {
Progress = true;
States[BDV] = NewState;
}
}
assert(OldSize == States.size() &&
"fixed point shouldn't be adding any new nodes to state");
}
#ifndef NDEBUG
VerifyStates();
LLVM_DEBUG(dbgs() << "States after meet iteration:\n");
for (const auto &Pair : States) {
LLVM_DEBUG(dbgs() << " " << Pair.second << " for " << *Pair.first << "\n");
}
#endif
for (auto Pair : States) {
Instruction *I = cast<Instruction>(Pair.first);
BDVState State = Pair.second;
auto *BaseValue = State.getBaseValue();
assert(
(!isKnownBase(I, KnownBases) || !areBothVectorOrScalar(I, BaseValue)) &&
"why did it get added?");
assert(!State.isUnknown() && "Optimistic algorithm didn't complete!");
if (!State.isBase() || !isa<VectorType>(BaseValue->getType()))
continue;
if (isa<ExtractElementInst>(I)) {
auto *EE = cast<ExtractElementInst>(I);
auto *BaseInst = ExtractElementInst::Create(
State.getBaseValue(), EE->getIndexOperand(), "base_ee", EE);
BaseInst->setMetadata("is_base_value", MDNode::get(I->getContext(), {}));
States[I] = BDVState(I, BDVState::Base, BaseInst);
setKnownBase(BaseInst, true, KnownBases);
} else if (!isa<VectorType>(I->getType())) {
States[I] = BDVState(I, BDVState::Conflict);
}
}
#ifndef NDEBUG
VerifyStates();
#endif
for (auto Pair : States) {
Instruction *I = cast<Instruction>(Pair.first);
BDVState State = Pair.second;
assert((!isKnownBase(I, KnownBases) ||
!areBothVectorOrScalar(I, State.getBaseValue())) &&
"why did it get added?");
assert(!State.isUnknown() && "Optimistic algorithm didn't complete!");
assert(!isa<InsertElementInst>(I) || State.isConflict());
if (!State.isConflict())
continue;
auto getMangledName = [](Instruction *I) -> std::string {
if (isa<PHINode>(I)) {
return suffixed_name_or(I, ".base", "base_phi");
} else if (isa<SelectInst>(I)) {
return suffixed_name_or(I, ".base", "base_select");
} else if (isa<ExtractElementInst>(I)) {
return suffixed_name_or(I, ".base", "base_ee");
} else if (isa<InsertElementInst>(I)) {
return suffixed_name_or(I, ".base", "base_ie");
} else {
return suffixed_name_or(I, ".base", "base_sv");
}
};
Instruction *BaseInst = I->clone();
BaseInst->insertBefore(I);
BaseInst->setName(getMangledName(I));
BaseInst->setMetadata("is_base_value", MDNode::get(I->getContext(), {}));
States[I] = BDVState(I, BDVState::Conflict, BaseInst);
setKnownBase(BaseInst, true, KnownBases);
}
#ifndef NDEBUG
VerifyStates();
#endif
auto getBaseForInput = [&](Value *Input, Instruction *InsertPt) {
Value *BDV = findBaseOrBDV(Input, Cache, KnownBases);
Value *Base = nullptr;
if (!States.count(BDV)) {
assert(areBothVectorOrScalar(BDV, Input));
Base = BDV;
} else {
assert(States.count(BDV));
Base = States[BDV].getBaseValue();
}
assert(Base && "Can't be null");
if (Base->getType() != Input->getType() && InsertPt)
Base = new BitCastInst(Base, Input->getType(), "cast", InsertPt);
return Base;
};
for (auto Pair : States) {
Instruction *BDV = cast<Instruction>(Pair.first);
BDVState State = Pair.second;
assert((!isKnownBase(BDV, KnownBases) ||
!areBothVectorOrScalar(BDV, State.getBaseValue())) &&
"why did it get added?");
assert(!State.isUnknown() && "Optimistic algorithm didn't complete!");
if (!State.isConflict())
continue;
if (PHINode *BasePHI = dyn_cast<PHINode>(State.getBaseValue())) {
PHINode *PN = cast<PHINode>(BDV);
const unsigned NumPHIValues = PN->getNumIncomingValues();
DenseMap<BasicBlock *, Value*> BlockToValue;
for (unsigned i = 0; i < NumPHIValues; i++) {
Value *InVal = PN->getIncomingValue(i);
BasicBlock *InBB = PN->getIncomingBlock(i);
if (!BlockToValue.count(InBB))
BlockToValue[InBB] = getBaseForInput(InVal, InBB->getTerminator());
else {
#ifndef NDEBUG
Value *OldBase = BlockToValue[InBB];
Value *Base = getBaseForInput(InVal, nullptr);
auto StripBitCasts = [](Value *V) -> Value * {
while (auto *BC = dyn_cast<BitCastInst>(V))
V = BC->getOperand(0);
return V;
};
assert(StripBitCasts(Base) == StripBitCasts(OldBase) &&
"findBaseOrBDV should be pure!");
#endif
}
Value *Base = BlockToValue[InBB];
BasePHI->setIncomingValue(i, Base);
}
} else if (SelectInst *BaseSI =
dyn_cast<SelectInst>(State.getBaseValue())) {
SelectInst *SI = cast<SelectInst>(BDV);
BaseSI->setTrueValue(getBaseForInput(SI->getTrueValue(), BaseSI));
BaseSI->setFalseValue(getBaseForInput(SI->getFalseValue(), BaseSI));
} else if (auto *BaseEE =
dyn_cast<ExtractElementInst>(State.getBaseValue())) {
Value *InVal = cast<ExtractElementInst>(BDV)->getVectorOperand();
BaseEE->setOperand(0, getBaseForInput(InVal, BaseEE));
} else if (auto *BaseIE = dyn_cast<InsertElementInst>(State.getBaseValue())){
auto *BdvIE = cast<InsertElementInst>(BDV);
auto UpdateOperand = [&](int OperandIdx) {
Value *InVal = BdvIE->getOperand(OperandIdx);
Value *Base = getBaseForInput(InVal, BaseIE);
BaseIE->setOperand(OperandIdx, Base);
};
UpdateOperand(0); UpdateOperand(1); } else {
auto *BaseSV = cast<ShuffleVectorInst>(State.getBaseValue());
auto *BdvSV = cast<ShuffleVectorInst>(BDV);
auto UpdateOperand = [&](int OperandIdx) {
Value *InVal = BdvSV->getOperand(OperandIdx);
Value *Base = getBaseForInput(InVal, BaseSV);
BaseSV->setOperand(OperandIdx, Base);
};
UpdateOperand(0); if (!BdvSV->isZeroEltSplat())
UpdateOperand(1); else {
Value *InVal = BdvSV->getOperand(1);
BaseSV->setOperand(1, UndefValue::get(InVal->getType()));
}
}
}
#ifndef NDEBUG
VerifyStates();
#endif
for (auto Pair : States) {
auto *BDV = Pair.first;
Value *Base = Pair.second.getBaseValue();
assert(BDV && Base);
assert(
(!isKnownBase(BDV, KnownBases) || !areBothVectorOrScalar(BDV, Base)) &&
"why did it get added?");
LLVM_DEBUG(
dbgs() << "Updating base value cache"
<< " for: " << BDV->getName() << " from: "
<< (Cache.count(BDV) ? Cache[BDV]->getName().str() : "none")
<< " to: " << Base->getName() << "\n");
Cache[BDV] = Base;
}
assert(Cache.count(Def));
return Cache[Def];
}
static void findBasePointers(const StatepointLiveSetTy &live,
PointerToBaseTy &PointerToBase, DominatorTree *DT,
DefiningValueMapTy &DVCache,
IsKnownBaseMapTy &KnownBases) {
for (Value *ptr : live) {
Value *base = findBasePointer(ptr, DVCache, KnownBases);
assert(base && "failed to find base pointer");
PointerToBase[ptr] = base;
assert((!isa<Instruction>(base) || !isa<Instruction>(ptr) ||
DT->dominates(cast<Instruction>(base)->getParent(),
cast<Instruction>(ptr)->getParent())) &&
"The base we found better dominate the derived pointer");
}
}
static void findBasePointers(DominatorTree &DT, DefiningValueMapTy &DVCache,
CallBase *Call,
PartiallyConstructedSafepointRecord &result,
PointerToBaseTy &PointerToBase,
IsKnownBaseMapTy &KnownBases) {
StatepointLiveSetTy PotentiallyDerivedPointers = result.LiveSet;
if (auto Opt = Call->getOperandBundle(LLVMContext::OB_deopt))
for (Value *V : Opt->Inputs) {
if (!PotentiallyDerivedPointers.count(V))
continue;
PotentiallyDerivedPointers.remove(V);
PointerToBase[V] = V;
}
findBasePointers(PotentiallyDerivedPointers, PointerToBase, &DT, DVCache,
KnownBases);
}
static void recomputeLiveInValues(GCPtrLivenessData &RevisedLivenessData,
CallBase *Call,
PartiallyConstructedSafepointRecord &result,
PointerToBaseTy &PointerToBase);
static void recomputeLiveInValues(
Function &F, DominatorTree &DT, ArrayRef<CallBase *> toUpdate,
MutableArrayRef<struct PartiallyConstructedSafepointRecord> records,
PointerToBaseTy &PointerToBase) {
GCPtrLivenessData RevisedLivenessData;
computeLiveInValues(DT, F, RevisedLivenessData);
for (size_t i = 0; i < records.size(); i++) {
struct PartiallyConstructedSafepointRecord &info = records[i];
recomputeLiveInValues(RevisedLivenessData, toUpdate[i], info,
PointerToBase);
}
}
static BasicBlock *
normalizeForInvokeSafepoint(BasicBlock *BB, BasicBlock *InvokeParent,
DominatorTree &DT) {
BasicBlock *Ret = BB;
if (!BB->getUniquePredecessor())
Ret = SplitBlockPredecessors(BB, InvokeParent, "", &DT);
FoldSingleEntryPHINodes(Ret);
assert(!isa<PHINode>(Ret->begin()) &&
"All PHI nodes should have been removed!");
return Ret;
}
static constexpr Attribute::AttrKind FnAttrsToStrip[] =
{Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly,
Attribute::ArgMemOnly, Attribute::InaccessibleMemOnly,
Attribute::InaccessibleMemOrArgMemOnly,
Attribute::NoSync, Attribute::NoFree};
static AttributeList legalizeCallAttributes(LLVMContext &Ctx,
AttributeList OrigAL,
AttributeList StatepointAL) {
if (OrigAL.isEmpty())
return StatepointAL;
AttrBuilder FnAttrs(Ctx, OrigAL.getFnAttrs());
for (auto Attr : FnAttrsToStrip)
FnAttrs.removeAttribute(Attr);
for (Attribute A : OrigAL.getFnAttrs()) {
if (isStatepointDirectiveAttr(A))
FnAttrs.removeAttribute(A);
}
return StatepointAL.addFnAttributes(Ctx, FnAttrs);
}
static void CreateGCRelocates(ArrayRef<Value *> LiveVariables,
ArrayRef<Value *> BasePtrs,
Instruction *StatepointToken,
IRBuilder<> &Builder) {
if (LiveVariables.empty())
return;
auto FindIndex = [](ArrayRef<Value *> LiveVec, Value *Val) {
auto ValIt = llvm::find(LiveVec, Val);
assert(ValIt != LiveVec.end() && "Val not found in LiveVec!");
size_t Index = std::distance(LiveVec.begin(), ValIt);
assert(Index < LiveVec.size() && "Bug in std::find?");
return Index;
};
Module *M = StatepointToken->getModule();
auto getGCRelocateDecl = [&] (Type *Ty) {
assert(isHandledGCPointerType(Ty));
auto AS = Ty->getScalarType()->getPointerAddressSpace();
Type *NewTy = Type::getInt8PtrTy(M->getContext(), AS);
if (auto *VT = dyn_cast<VectorType>(Ty))
NewTy = FixedVectorType::get(NewTy,
cast<FixedVectorType>(VT)->getNumElements());
return Intrinsic::getDeclaration(M, Intrinsic::experimental_gc_relocate,
{NewTy});
};
DenseMap<Type *, Function *> TypeToDeclMap;
for (unsigned i = 0; i < LiveVariables.size(); i++) {
Value *BaseIdx = Builder.getInt32(FindIndex(LiveVariables, BasePtrs[i]));
Value *LiveIdx = Builder.getInt32(i);
Type *Ty = LiveVariables[i]->getType();
if (!TypeToDeclMap.count(Ty))
TypeToDeclMap[Ty] = getGCRelocateDecl(Ty);
Function *GCRelocateDecl = TypeToDeclMap[Ty];
CallInst *Reloc = Builder.CreateCall(
GCRelocateDecl, {StatepointToken, BaseIdx, LiveIdx},
suffixed_name_or(LiveVariables[i], ".relocated", ""));
Reloc->setCallingConv(CallingConv::Cold);
}
}
namespace {
class DeferredReplacement {
AssertingVH<Instruction> Old;
AssertingVH<Instruction> New;
bool IsDeoptimize = false;
DeferredReplacement() = default;
public:
static DeferredReplacement createRAUW(Instruction *Old, Instruction *New) {
assert(Old != New && Old && New &&
"Cannot RAUW equal values or to / from null!");
DeferredReplacement D;
D.Old = Old;
D.New = New;
return D;
}
static DeferredReplacement createDelete(Instruction *ToErase) {
DeferredReplacement D;
D.Old = ToErase;
return D;
}
static DeferredReplacement createDeoptimizeReplacement(Instruction *Old) {
#ifndef NDEBUG
auto *F = cast<CallInst>(Old)->getCalledFunction();
assert(F && F->getIntrinsicID() == Intrinsic::experimental_deoptimize &&
"Only way to construct a deoptimize deferred replacement");
#endif
DeferredReplacement D;
D.Old = Old;
D.IsDeoptimize = true;
return D;
}
void doReplacement() {
Instruction *OldI = Old;
Instruction *NewI = New;
assert(OldI != NewI && "Disallowed at construction?!");
assert((!IsDeoptimize || !New) &&
"Deoptimize intrinsics are not replaced!");
Old = nullptr;
New = nullptr;
if (NewI)
OldI->replaceAllUsesWith(NewI);
if (IsDeoptimize) {
auto *RI = cast<ReturnInst>(OldI->getParent()->getTerminator());
new UnreachableInst(RI->getContext(), RI);
RI->eraseFromParent();
}
OldI->eraseFromParent();
}
};
}
static StringRef getDeoptLowering(CallBase *Call) {
const char *DeoptLowering = "deopt-lowering";
if (Call->hasFnAttr(DeoptLowering)) {
const AttributeList &CSAS = Call->getAttributes();
if (CSAS.hasFnAttr(DeoptLowering))
return CSAS.getFnAttr(DeoptLowering).getValueAsString();
Function *F = Call->getCalledFunction();
assert(F && F->hasFnAttribute(DeoptLowering));
return F->getFnAttribute(DeoptLowering).getValueAsString();
}
return "live-through";
}
static void
makeStatepointExplicitImpl(CallBase *Call,
const SmallVectorImpl<Value *> &BasePtrs,
const SmallVectorImpl<Value *> &LiveVariables,
PartiallyConstructedSafepointRecord &Result,
std::vector<DeferredReplacement> &Replacements,
const PointerToBaseTy &PointerToBase) {
assert(BasePtrs.size() == LiveVariables.size());
IRBuilder<> Builder(Call);
ArrayRef<Value *> GCArgs(LiveVariables);
uint64_t StatepointID = StatepointDirectives::DefaultStatepointID;
uint32_t NumPatchBytes = 0;
uint32_t Flags = uint32_t(StatepointFlags::None);
SmallVector<Value *, 8> CallArgs(Call->args());
Optional<ArrayRef<Use>> DeoptArgs;
if (auto Bundle = Call->getOperandBundle(LLVMContext::OB_deopt))
DeoptArgs = Bundle->Inputs;
Optional<ArrayRef<Use>> TransitionArgs;
if (auto Bundle = Call->getOperandBundle(LLVMContext::OB_gc_transition)) {
TransitionArgs = Bundle->Inputs;
Flags |= uint32_t(StatepointFlags::GCTransition);
}
bool IsDeoptimize = false;
StatepointDirectives SD =
parseStatepointDirectivesFromAttrs(Call->getAttributes());
if (SD.NumPatchBytes)
NumPatchBytes = *SD.NumPatchBytes;
if (SD.StatepointID)
StatepointID = *SD.StatepointID;
StringRef DeoptLowering = getDeoptLowering(Call);
if (DeoptLowering.equals("live-in"))
Flags |= uint32_t(StatepointFlags::DeoptLiveIn);
else {
assert(DeoptLowering.equals("live-through") && "Unsupported value!");
}
FunctionCallee CallTarget(Call->getFunctionType(), Call->getCalledOperand());
if (Function *F = dyn_cast<Function>(CallTarget.getCallee())) {
auto IID = F->getIntrinsicID();
if (IID == Intrinsic::experimental_deoptimize) {
SmallVector<Type *, 8> DomainTy;
for (Value *Arg : CallArgs)
DomainTy.push_back(Arg->getType());
auto *FTy = FunctionType::get(Type::getVoidTy(F->getContext()), DomainTy,
false);
CallTarget = F->getParent()
->getOrInsertFunction("__llvm_deoptimize", FTy);
IsDeoptimize = true;
} else if (IID == Intrinsic::memcpy_element_unordered_atomic ||
IID == Intrinsic::memmove_element_unordered_atomic) {
auto &Context = Call->getContext();
auto &DL = Call->getModule()->getDataLayout();
auto GetBaseAndOffset = [&](Value *Derived) {
Value *Base = nullptr;
if (isa<Constant>(Derived))
Base =
ConstantPointerNull::get(cast<PointerType>(Derived->getType()));
else {
assert(PointerToBase.count(Derived));
Base = PointerToBase.find(Derived)->second;
}
unsigned AddressSpace = Derived->getType()->getPointerAddressSpace();
unsigned IntPtrSize = DL.getPointerSizeInBits(AddressSpace);
Value *Base_int = Builder.CreatePtrToInt(
Base, Type::getIntNTy(Context, IntPtrSize));
Value *Derived_int = Builder.CreatePtrToInt(
Derived, Type::getIntNTy(Context, IntPtrSize));
return std::make_pair(Base, Builder.CreateSub(Derived_int, Base_int));
};
auto *Dest = CallArgs[0];
Value *DestBase, *DestOffset;
std::tie(DestBase, DestOffset) = GetBaseAndOffset(Dest);
auto *Source = CallArgs[1];
Value *SourceBase, *SourceOffset;
std::tie(SourceBase, SourceOffset) = GetBaseAndOffset(Source);
auto *LengthInBytes = CallArgs[2];
auto *ElementSizeCI = cast<ConstantInt>(CallArgs[3]);
CallArgs.clear();
CallArgs.push_back(DestBase);
CallArgs.push_back(DestOffset);
CallArgs.push_back(SourceBase);
CallArgs.push_back(SourceOffset);
CallArgs.push_back(LengthInBytes);
SmallVector<Type *, 8> DomainTy;
for (Value *Arg : CallArgs)
DomainTy.push_back(Arg->getType());
auto *FTy = FunctionType::get(Type::getVoidTy(F->getContext()), DomainTy,
false);
auto GetFunctionName = [](Intrinsic::ID IID, ConstantInt *ElementSizeCI) {
uint64_t ElementSize = ElementSizeCI->getZExtValue();
if (IID == Intrinsic::memcpy_element_unordered_atomic) {
switch (ElementSize) {
case 1:
return "__llvm_memcpy_element_unordered_atomic_safepoint_1";
case 2:
return "__llvm_memcpy_element_unordered_atomic_safepoint_2";
case 4:
return "__llvm_memcpy_element_unordered_atomic_safepoint_4";
case 8:
return "__llvm_memcpy_element_unordered_atomic_safepoint_8";
case 16:
return "__llvm_memcpy_element_unordered_atomic_safepoint_16";
default:
llvm_unreachable("unexpected element size!");
}
}
assert(IID == Intrinsic::memmove_element_unordered_atomic);
switch (ElementSize) {
case 1:
return "__llvm_memmove_element_unordered_atomic_safepoint_1";
case 2:
return "__llvm_memmove_element_unordered_atomic_safepoint_2";
case 4:
return "__llvm_memmove_element_unordered_atomic_safepoint_4";
case 8:
return "__llvm_memmove_element_unordered_atomic_safepoint_8";
case 16:
return "__llvm_memmove_element_unordered_atomic_safepoint_16";
default:
llvm_unreachable("unexpected element size!");
}
};
CallTarget =
F->getParent()
->getOrInsertFunction(GetFunctionName(IID, ElementSizeCI), FTy);
}
}
GCStatepointInst *Token = nullptr;
if (auto *CI = dyn_cast<CallInst>(Call)) {
CallInst *SPCall = Builder.CreateGCStatepointCall(
StatepointID, NumPatchBytes, CallTarget, Flags, CallArgs,
TransitionArgs, DeoptArgs, GCArgs, "safepoint_token");
SPCall->setTailCallKind(CI->getTailCallKind());
SPCall->setCallingConv(CI->getCallingConv());
SPCall->setAttributes(legalizeCallAttributes(
CI->getContext(), CI->getAttributes(), SPCall->getAttributes()));
Token = cast<GCStatepointInst>(SPCall);
assert(CI->getNextNode() && "Not a terminator, must have next!");
Builder.SetInsertPoint(CI->getNextNode());
Builder.SetCurrentDebugLocation(CI->getNextNode()->getDebugLoc());
} else {
auto *II = cast<InvokeInst>(Call);
InvokeInst *SPInvoke = Builder.CreateGCStatepointInvoke(
StatepointID, NumPatchBytes, CallTarget, II->getNormalDest(),
II->getUnwindDest(), Flags, CallArgs, TransitionArgs, DeoptArgs, GCArgs,
"statepoint_token");
SPInvoke->setCallingConv(II->getCallingConv());
SPInvoke->setAttributes(legalizeCallAttributes(
II->getContext(), II->getAttributes(), SPInvoke->getAttributes()));
Token = cast<GCStatepointInst>(SPInvoke);
BasicBlock *UnwindBlock = II->getUnwindDest();
assert(!isa<PHINode>(UnwindBlock->begin()) &&
UnwindBlock->getUniquePredecessor() &&
"can't safely insert in this block!");
Builder.SetInsertPoint(&*UnwindBlock->getFirstInsertionPt());
Builder.SetCurrentDebugLocation(II->getDebugLoc());
Instruction *ExceptionalToken = UnwindBlock->getLandingPadInst();
Result.UnwindToken = ExceptionalToken;
CreateGCRelocates(LiveVariables, BasePtrs, ExceptionalToken, Builder);
BasicBlock *NormalDest = II->getNormalDest();
assert(!isa<PHINode>(NormalDest->begin()) &&
NormalDest->getUniquePredecessor() &&
"can't safely insert in this block!");
Builder.SetInsertPoint(&*NormalDest->getFirstInsertionPt());
}
assert(Token && "Should be set in one of the above branches!");
if (IsDeoptimize) {
Replacements.push_back(
DeferredReplacement::createDeoptimizeReplacement(Call));
} else {
Token->setName("statepoint_token");
if (!Call->getType()->isVoidTy() && !Call->use_empty()) {
StringRef Name = Call->hasName() ? Call->getName() : "";
CallInst *GCResult = Builder.CreateGCResult(Token, Call->getType(), Name);
GCResult->setAttributes(
AttributeList::get(GCResult->getContext(), AttributeList::ReturnIndex,
Call->getAttributes().getRetAttrs()));
Replacements.emplace_back(
DeferredReplacement::createRAUW(Call, GCResult));
} else {
Replacements.emplace_back(DeferredReplacement::createDelete(Call));
}
}
Result.StatepointToken = Token;
CreateGCRelocates(LiveVariables, BasePtrs, Token, Builder);
}
static void
makeStatepointExplicit(DominatorTree &DT, CallBase *Call,
PartiallyConstructedSafepointRecord &Result,
std::vector<DeferredReplacement> &Replacements,
const PointerToBaseTy &PointerToBase) {
const auto &LiveSet = Result.LiveSet;
SmallVector<Value *, 64> BaseVec, LiveVec;
LiveVec.reserve(LiveSet.size());
BaseVec.reserve(LiveSet.size());
for (Value *L : LiveSet) {
LiveVec.push_back(L);
assert(PointerToBase.count(L));
Value *Base = PointerToBase.find(L)->second;
BaseVec.push_back(Base);
}
assert(LiveVec.size() == BaseVec.size());
makeStatepointExplicitImpl(Call, BaseVec, LiveVec, Result, Replacements,
PointerToBase);
}
static void
insertRelocationStores(iterator_range<Value::user_iterator> GCRelocs,
DenseMap<Value *, AllocaInst *> &AllocaMap,
DenseSet<Value *> &VisitedLiveValues) {
for (User *U : GCRelocs) {
GCRelocateInst *Relocate = dyn_cast<GCRelocateInst>(U);
if (!Relocate)
continue;
Value *OriginalValue = Relocate->getDerivedPtr();
assert(AllocaMap.count(OriginalValue));
Value *Alloca = AllocaMap[OriginalValue];
assert(Relocate->getNextNode() &&
"Should always have one since it's not a terminator");
IRBuilder<> Builder(Relocate->getNextNode());
Value *CastedRelocatedValue =
Builder.CreateBitCast(Relocate,
cast<AllocaInst>(Alloca)->getAllocatedType(),
suffixed_name_or(Relocate, ".casted", ""));
new StoreInst(CastedRelocatedValue, Alloca,
cast<Instruction>(CastedRelocatedValue)->getNextNode());
#ifndef NDEBUG
VisitedLiveValues.insert(OriginalValue);
#endif
}
}
static void insertRematerializationStores(
const RematerializedValueMapTy &RematerializedValues,
DenseMap<Value *, AllocaInst *> &AllocaMap,
DenseSet<Value *> &VisitedLiveValues) {
for (auto RematerializedValuePair: RematerializedValues) {
Instruction *RematerializedValue = RematerializedValuePair.first;
Value *OriginalValue = RematerializedValuePair.second;
assert(AllocaMap.count(OriginalValue) &&
"Can not find alloca for rematerialized value");
Value *Alloca = AllocaMap[OriginalValue];
new StoreInst(RematerializedValue, Alloca,
RematerializedValue->getNextNode());
#ifndef NDEBUG
VisitedLiveValues.insert(OriginalValue);
#endif
}
}
static void relocationViaAlloca(
Function &F, DominatorTree &DT, ArrayRef<Value *> Live,
ArrayRef<PartiallyConstructedSafepointRecord> Records) {
#ifndef NDEBUG
int InitialAllocaNum = 0;
for (Instruction &I : F.getEntryBlock())
if (isa<AllocaInst>(I))
InitialAllocaNum++;
#endif
DenseMap<Value *, AllocaInst *> AllocaMap;
SmallVector<AllocaInst *, 200> PromotableAllocas;
std::size_t NumRematerializedValues = 0;
PromotableAllocas.reserve(Live.size());
const DataLayout &DL = F.getParent()->getDataLayout();
auto emitAllocaFor = [&](Value *LiveValue) {
AllocaInst *Alloca = new AllocaInst(LiveValue->getType(),
DL.getAllocaAddrSpace(), "",
F.getEntryBlock().getFirstNonPHI());
AllocaMap[LiveValue] = Alloca;
PromotableAllocas.push_back(Alloca);
};
for (Value *V : Live)
emitAllocaFor(V);
for (const auto &Info : Records)
for (auto RematerializedValuePair : Info.RematerializedValues) {
Value *OriginalValue = RematerializedValuePair.second;
if (AllocaMap.count(OriginalValue) != 0)
continue;
emitAllocaFor(OriginalValue);
++NumRematerializedValues;
}
for (const auto &Info : Records) {
Value *Statepoint = Info.StatepointToken;
DenseSet<Value *> VisitedLiveValues;
insertRelocationStores(Statepoint->users(), AllocaMap, VisitedLiveValues);
if (isa<InvokeInst>(Statepoint)) {
insertRelocationStores(Info.UnwindToken->users(), AllocaMap,
VisitedLiveValues);
}
insertRematerializationStores(Info.RematerializedValues, AllocaMap,
VisitedLiveValues);
if (ClobberNonLive) {
SmallVector<AllocaInst *, 64> ToClobber;
for (auto Pair : AllocaMap) {
Value *Def = Pair.first;
AllocaInst *Alloca = Pair.second;
if (VisitedLiveValues.count(Def)) {
continue;
}
ToClobber.push_back(Alloca);
}
auto InsertClobbersAt = [&](Instruction *IP) {
for (auto *AI : ToClobber) {
auto PT = cast<PointerType>(AI->getAllocatedType());
Constant *CPN = ConstantPointerNull::get(PT);
new StoreInst(CPN, AI, IP);
}
};
if (auto II = dyn_cast<InvokeInst>(Statepoint)) {
InsertClobbersAt(&*II->getNormalDest()->getFirstInsertionPt());
InsertClobbersAt(&*II->getUnwindDest()->getFirstInsertionPt());
} else {
InsertClobbersAt(cast<Instruction>(Statepoint)->getNextNode());
}
}
}
for (auto Pair : AllocaMap) {
Value *Def = Pair.first;
AllocaInst *Alloca = Pair.second;
SmallVector<Instruction *, 20> Uses;
Uses.reserve(Def->getNumUses());
for (User *U : Def->users()) {
if (!isa<ConstantExpr>(U)) {
Uses.push_back(cast<Instruction>(U));
}
}
llvm::sort(Uses);
auto Last = std::unique(Uses.begin(), Uses.end());
Uses.erase(Last, Uses.end());
for (Instruction *Use : Uses) {
if (isa<PHINode>(Use)) {
PHINode *Phi = cast<PHINode>(Use);
for (unsigned i = 0; i < Phi->getNumIncomingValues(); i++) {
if (Def == Phi->getIncomingValue(i)) {
LoadInst *Load =
new LoadInst(Alloca->getAllocatedType(), Alloca, "",
Phi->getIncomingBlock(i)->getTerminator());
Phi->setIncomingValue(i, Load);
}
}
} else {
LoadInst *Load =
new LoadInst(Alloca->getAllocatedType(), Alloca, "", Use);
Use->replaceUsesOfWith(Def, Load);
}
}
StoreInst *Store = new StoreInst(Def, Alloca, false,
DL.getABITypeAlign(Def->getType()));
if (Instruction *Inst = dyn_cast<Instruction>(Def)) {
if (InvokeInst *Invoke = dyn_cast<InvokeInst>(Inst)) {
BasicBlock *NormalDest = Invoke->getNormalDest();
Store->insertBefore(NormalDest->getFirstNonPHI());
} else {
assert(!Inst->isTerminator() &&
"The only terminator that can produce a value is "
"InvokeInst which is handled above.");
Store->insertAfter(Inst);
}
} else {
assert(isa<Argument>(Def));
Store->insertAfter(cast<Instruction>(Alloca));
}
}
assert(PromotableAllocas.size() == Live.size() + NumRematerializedValues &&
"we must have the same allocas with lives");
(void) NumRematerializedValues;
if (!PromotableAllocas.empty()) {
PromoteMemToReg(PromotableAllocas, DT);
}
#ifndef NDEBUG
for (auto &I : F.getEntryBlock())
if (isa<AllocaInst>(I))
InitialAllocaNum--;
assert(InitialAllocaNum == 0 && "We must not introduce any extra allocas");
#endif
}
template <typename T> static void unique_unsorted(SmallVectorImpl<T> &Vec) {
SmallSet<T, 8> Seen;
erase_if(Vec, [&](const T &V) { return !Seen.insert(V).second; });
}
static void insertUseHolderAfter(CallBase *Call, const ArrayRef<Value *> Values,
SmallVectorImpl<CallInst *> &Holders) {
if (Values.empty())
return;
Module *M = Call->getModule();
FunctionCallee Func = M->getOrInsertFunction(
"__tmp_use", FunctionType::get(Type::getVoidTy(M->getContext()), true));
if (isa<CallInst>(Call)) {
Holders.push_back(
CallInst::Create(Func, Values, "", &*++Call->getIterator()));
return;
}
auto *II = cast<InvokeInst>(Call);
Holders.push_back(CallInst::Create(
Func, Values, "", &*II->getNormalDest()->getFirstInsertionPt()));
Holders.push_back(CallInst::Create(
Func, Values, "", &*II->getUnwindDest()->getFirstInsertionPt()));
}
static void findLiveReferences(
Function &F, DominatorTree &DT, ArrayRef<CallBase *> toUpdate,
MutableArrayRef<struct PartiallyConstructedSafepointRecord> records) {
GCPtrLivenessData OriginalLivenessData;
computeLiveInValues(DT, F, OriginalLivenessData);
for (size_t i = 0; i < records.size(); i++) {
struct PartiallyConstructedSafepointRecord &info = records[i];
analyzeParsePointLiveness(DT, OriginalLivenessData, toUpdate[i], info);
}
}
static Value* findRematerializableChainToBasePointer(
SmallVectorImpl<Instruction*> &ChainToBase,
Value *CurrentValue) {
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CurrentValue)) {
ChainToBase.push_back(GEP);
return findRematerializableChainToBasePointer(ChainToBase,
GEP->getPointerOperand());
}
if (CastInst *CI = dyn_cast<CastInst>(CurrentValue)) {
if (!CI->isNoopCast(CI->getModule()->getDataLayout()))
return CI;
ChainToBase.push_back(CI);
return findRematerializableChainToBasePointer(ChainToBase,
CI->getOperand(0));
}
return CurrentValue;
}
static InstructionCost
chainToBasePointerCost(SmallVectorImpl<Instruction *> &Chain,
TargetTransformInfo &TTI) {
InstructionCost Cost = 0;
for (Instruction *Instr : Chain) {
if (CastInst *CI = dyn_cast<CastInst>(Instr)) {
assert(CI->isNoopCast(CI->getModule()->getDataLayout()) &&
"non noop cast is found during rematerialization");
Type *SrcTy = CI->getOperand(0)->getType();
Cost += TTI.getCastInstrCost(CI->getOpcode(), CI->getType(), SrcTy,
TTI::getCastContextHint(CI),
TargetTransformInfo::TCK_SizeAndLatency, CI);
} else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Instr)) {
Type *ValTy = GEP->getSourceElementType();
Cost += TTI.getAddressComputationCost(ValTy);
if (!GEP->hasAllConstantIndices())
Cost += 2;
} else {
llvm_unreachable("unsupported instruction type during rematerialization");
}
}
return Cost;
}
static bool AreEquivalentPhiNodes(PHINode &OrigRootPhi, PHINode &AlternateRootPhi) {
unsigned PhiNum = OrigRootPhi.getNumIncomingValues();
if (PhiNum != AlternateRootPhi.getNumIncomingValues() ||
OrigRootPhi.getParent() != AlternateRootPhi.getParent())
return false;
SmallDenseMap<Value *, BasicBlock *, 8> CurrentIncomingValues;
for (unsigned i = 0; i < PhiNum; i++)
CurrentIncomingValues[OrigRootPhi.getIncomingValue(i)] =
OrigRootPhi.getIncomingBlock(i);
for (unsigned i = 0; i < PhiNum; i++) {
auto CIVI =
CurrentIncomingValues.find(AlternateRootPhi.getIncomingValue(i));
if (CIVI == CurrentIncomingValues.end())
return false;
BasicBlock *CurrentIncomingBB = CIVI->second;
if (CurrentIncomingBB != AlternateRootPhi.getIncomingBlock(i))
return false;
}
return true;
}
static void
findRematerializationCandidates(PointerToBaseTy PointerToBase,
RematCandTy &RematerizationCandidates,
TargetTransformInfo &TTI) {
const unsigned int ChainLengthThreshold = 10;
for (auto P2B : PointerToBase) {
auto *Derived = P2B.first;
auto *Base = P2B.second;
if (Derived == Base)
continue;
SmallVector<Instruction *, 3> ChainToBase;
Value *RootOfChain =
findRematerializableChainToBasePointer(ChainToBase, Derived);
if ( ChainToBase.size() == 0 ||
ChainToBase.size() > ChainLengthThreshold)
continue;
if (RootOfChain != PointerToBase[Derived]) {
PHINode *OrigRootPhi = dyn_cast<PHINode>(RootOfChain);
PHINode *AlternateRootPhi = dyn_cast<PHINode>(PointerToBase[Derived]);
if (!OrigRootPhi || !AlternateRootPhi)
continue;
if (!AreEquivalentPhiNodes(*OrigRootPhi, *AlternateRootPhi))
continue;
}
InstructionCost Cost = chainToBasePointerCost(ChainToBase, TTI);
RematerizlizationCandidateRecord Record;
Record.ChainToBase = ChainToBase;
Record.RootOfChain = RootOfChain;
Record.Cost = Cost;
RematerizationCandidates.insert({ Derived, Record });
}
}
static void rematerializeLiveValues(CallBase *Call,
PartiallyConstructedSafepointRecord &Info,
PointerToBaseTy &PointerToBase,
RematCandTy &RematerizationCandidates,
TargetTransformInfo &TTI) {
SmallVector<Value *, 32> LiveValuesToBeDeleted;
for (Value *LiveValue : Info.LiveSet) {
auto It = RematerizationCandidates.find(LiveValue);
if (It == RematerizationCandidates.end())
continue;
RematerizlizationCandidateRecord &Record = It->second;
InstructionCost Cost = Record.Cost;
if (isa<InvokeInst>(Call))
Cost *= 2;
if (Cost >= RematerializationThreshold)
continue;
LiveValuesToBeDeleted.push_back(LiveValue);
SmallVector<Instruction *, 3> ChainToBase = Record.ChainToBase;
std::reverse(ChainToBase.begin(), ChainToBase.end());
auto rematerializeChain = [&ChainToBase](
Instruction *InsertBefore, Value *RootOfChain, Value *AlternateLiveBase) {
Instruction *LastClonedValue = nullptr;
Instruction *LastValue = nullptr;
for (Instruction *Instr: ChainToBase) {
assert(isa<GetElementPtrInst>(Instr) || isa<CastInst>(Instr));
Instruction *ClonedValue = Instr->clone();
ClonedValue->insertBefore(InsertBefore);
ClonedValue->setName(Instr->getName() + ".remat");
if (LastClonedValue) {
assert(LastValue);
ClonedValue->replaceUsesOfWith(LastValue, LastClonedValue);
#ifndef NDEBUG
for (auto OpValue : ClonedValue->operand_values()) {
assert(!is_contained(ChainToBase, OpValue) &&
"incorrect use in rematerialization chain");
assert(OpValue != RootOfChain && OpValue != AlternateLiveBase);
}
#endif
} else {
if (RootOfChain != AlternateLiveBase)
ClonedValue->replaceUsesOfWith(RootOfChain, AlternateLiveBase);
}
LastClonedValue = ClonedValue;
LastValue = Instr;
}
assert(LastClonedValue);
return LastClonedValue;
};
if (isa<CallInst>(Call)) {
Instruction *InsertBefore = Call->getNextNode();
assert(InsertBefore);
Instruction *RematerializedValue = rematerializeChain(
InsertBefore, Record.RootOfChain, PointerToBase[LiveValue]);
Info.RematerializedValues[RematerializedValue] = LiveValue;
} else {
auto *Invoke = cast<InvokeInst>(Call);
Instruction *NormalInsertBefore =
&*Invoke->getNormalDest()->getFirstInsertionPt();
Instruction *UnwindInsertBefore =
&*Invoke->getUnwindDest()->getFirstInsertionPt();
Instruction *NormalRematerializedValue = rematerializeChain(
NormalInsertBefore, Record.RootOfChain, PointerToBase[LiveValue]);
Instruction *UnwindRematerializedValue = rematerializeChain(
UnwindInsertBefore, Record.RootOfChain, PointerToBase[LiveValue]);
Info.RematerializedValues[NormalRematerializedValue] = LiveValue;
Info.RematerializedValues[UnwindRematerializedValue] = LiveValue;
}
}
for (auto LiveValue: LiveValuesToBeDeleted) {
Info.LiveSet.remove(LiveValue);
}
}
static bool inlineGetBaseAndOffset(Function &F,
SmallVectorImpl<CallInst *> &Intrinsics,
DefiningValueMapTy &DVCache,
IsKnownBaseMapTy &KnownBases) {
auto &Context = F.getContext();
auto &DL = F.getParent()->getDataLayout();
bool Changed = false;
for (auto *Callsite : Intrinsics)
switch (Callsite->getIntrinsicID()) {
case Intrinsic::experimental_gc_get_pointer_base: {
Changed = true;
Value *Base =
findBasePointer(Callsite->getOperand(0), DVCache, KnownBases);
assert(!DVCache.count(Callsite));
auto *BaseBC = IRBuilder<>(Callsite).CreateBitCast(
Base, Callsite->getType(), suffixed_name_or(Base, ".cast", ""));
if (BaseBC != Base)
DVCache[BaseBC] = Base;
Callsite->replaceAllUsesWith(BaseBC);
if (!BaseBC->hasName())
BaseBC->takeName(Callsite);
Callsite->eraseFromParent();
break;
}
case Intrinsic::experimental_gc_get_pointer_offset: {
Changed = true;
Value *Derived = Callsite->getOperand(0);
Value *Base = findBasePointer(Derived, DVCache, KnownBases);
assert(!DVCache.count(Callsite));
unsigned AddressSpace = Derived->getType()->getPointerAddressSpace();
unsigned IntPtrSize = DL.getPointerSizeInBits(AddressSpace);
IRBuilder<> Builder(Callsite);
Value *BaseInt =
Builder.CreatePtrToInt(Base, Type::getIntNTy(Context, IntPtrSize),
suffixed_name_or(Base, ".int", ""));
Value *DerivedInt =
Builder.CreatePtrToInt(Derived, Type::getIntNTy(Context, IntPtrSize),
suffixed_name_or(Derived, ".int", ""));
Value *Offset = Builder.CreateSub(DerivedInt, BaseInt);
Callsite->replaceAllUsesWith(Offset);
Offset->takeName(Callsite);
Callsite->eraseFromParent();
break;
}
default:
llvm_unreachable("Unknown intrinsic");
}
return Changed;
}
static bool insertParsePoints(Function &F, DominatorTree &DT,
TargetTransformInfo &TTI,
SmallVectorImpl<CallBase *> &ToUpdate,
DefiningValueMapTy &DVCache,
IsKnownBaseMapTy &KnownBases) {
#ifndef NDEBUG
std::set<CallBase *> Uniqued;
Uniqued.insert(ToUpdate.begin(), ToUpdate.end());
assert(Uniqued.size() == ToUpdate.size() && "no duplicates please!");
for (CallBase *Call : ToUpdate)
assert(Call->getFunction() == &F);
#endif
for (CallBase *Call : ToUpdate) {
auto *II = dyn_cast<InvokeInst>(Call);
if (!II)
continue;
normalizeForInvokeSafepoint(II->getNormalDest(), II->getParent(), DT);
normalizeForInvokeSafepoint(II->getUnwindDest(), II->getParent(), DT);
}
SmallVector<CallInst *, 64> Holders;
for (CallBase *Call : ToUpdate) {
SmallVector<Value *, 64> DeoptValues;
for (Value *Arg : GetDeoptBundleOperands(Call)) {
assert(!isUnhandledGCPointerType(Arg->getType()) &&
"support for FCA unimplemented");
if (isHandledGCPointerType(Arg->getType()))
DeoptValues.push_back(Arg);
}
insertUseHolderAfter(Call, DeoptValues, Holders);
}
SmallVector<PartiallyConstructedSafepointRecord, 64> Records(ToUpdate.size());
findLiveReferences(F, DT, ToUpdate, Records);
PointerToBaseTy PointerToBase;
for (size_t i = 0; i < Records.size(); i++) {
PartiallyConstructedSafepointRecord &info = Records[i];
findBasePointers(DT, DVCache, ToUpdate[i], info, PointerToBase, KnownBases);
}
if (PrintBasePointers) {
errs() << "Base Pairs (w/o Relocation):\n";
for (auto &Pair : PointerToBase) {
errs() << " derived ";
Pair.first->printAsOperand(errs(), false);
errs() << " base ";
Pair.second->printAsOperand(errs(), false);
errs() << "\n";
;
}
}
Holders.reserve(Holders.size() + Records.size());
for (size_t i = 0; i < Records.size(); i++) {
PartiallyConstructedSafepointRecord &Info = Records[i];
SmallVector<Value *, 128> Bases;
for (auto *Derived : Info.LiveSet) {
assert(PointerToBase.count(Derived) && "Missed base for derived pointer");
Bases.push_back(PointerToBase[Derived]);
}
insertUseHolderAfter(ToUpdate[i], Bases, Holders);
}
recomputeLiveInValues(F, DT, ToUpdate, Records, PointerToBase);
if (PrintBasePointers) {
errs() << "Base Pairs: (w/Relocation)\n";
for (auto Pair : PointerToBase) {
errs() << " derived ";
Pair.first->printAsOperand(errs(), false);
errs() << " base ";
Pair.second->printAsOperand(errs(), false);
errs() << "\n";
}
}
for (auto &Info : Records) {
Info.LiveSet.remove_if([&](Value *LiveV) {
assert(PointerToBase.count(LiveV) && "Missed base for derived pointer");
return isa<Constant>(PointerToBase[LiveV]);
});
}
for (CallInst *CI : Holders)
CI->eraseFromParent();
Holders.clear();
RematCandTy RematerizationCandidates;
findRematerializationCandidates(PointerToBase, RematerizationCandidates, TTI);
for (size_t i = 0; i < Records.size(); i++)
rematerializeLiveValues(ToUpdate[i], Records[i], PointerToBase,
RematerizationCandidates, TTI);
std::vector<DeferredReplacement> Replacements;
for (size_t i = 0; i < Records.size(); i++)
makeStatepointExplicit(DT, ToUpdate[i], Records[i], Replacements,
PointerToBase);
ToUpdate.clear();
for (auto &PR : Replacements)
PR.doReplacement();
Replacements.clear();
for (auto &Info : Records) {
Info.LiveSet.clear();
}
PointerToBase.clear();
SmallVector<Value *, 128> Live;
for (size_t i = 0; i < Records.size(); i++) {
PartiallyConstructedSafepointRecord &Info = Records[i];
llvm::append_range(Live, Info.StatepointToken->gc_args());
#ifndef NDEBUG
assert(DT.isReachableFromEntry(Info.StatepointToken->getParent()) &&
"statepoint must be reachable or liveness is meaningless");
for (Value *V : Info.StatepointToken->gc_args()) {
if (!isa<Instruction>(V))
continue;
auto *LiveInst = cast<Instruction>(V);
assert(DT.isReachableFromEntry(LiveInst->getParent()) &&
"unreachable values should never be live");
assert(DT.dominates(LiveInst, Info.StatepointToken) &&
"basic SSA liveness expectation violated by liveness analysis");
}
#endif
}
unique_unsorted(Live);
#ifndef NDEBUG
for (auto *Ptr : Live)
assert(isHandledGCPointerType(Ptr->getType()) &&
"must be a gc pointer type");
#endif
relocationViaAlloca(F, DT, Live, Records);
return !Records.empty();
}
static AttributeMask getParamAndReturnAttributesToRemove() {
AttributeMask R;
R.addAttribute(Attribute::Dereferenceable);
R.addAttribute(Attribute::DereferenceableOrNull);
R.addAttribute(Attribute::ReadNone);
R.addAttribute(Attribute::ReadOnly);
R.addAttribute(Attribute::WriteOnly);
R.addAttribute(Attribute::NoAlias);
R.addAttribute(Attribute::NoFree);
return R;
}
static void stripNonValidAttributesFromPrototype(Function &F) {
LLVMContext &Ctx = F.getContext();
if (Intrinsic::ID id = F.getIntrinsicID()) {
F.setAttributes(Intrinsic::getAttributes(Ctx, id));
return;
}
AttributeMask R = getParamAndReturnAttributesToRemove();
for (Argument &A : F.args())
if (isa<PointerType>(A.getType()))
F.removeParamAttrs(A.getArgNo(), R);
if (isa<PointerType>(F.getReturnType()))
F.removeRetAttrs(R);
for (auto Attr : FnAttrsToStrip)
F.removeFnAttr(Attr);
}
static void stripInvalidMetadataFromInstruction(Instruction &I) {
if (!isa<LoadInst>(I) && !isa<StoreInst>(I))
return;
unsigned ValidMetadataAfterRS4GC[] = {LLVMContext::MD_tbaa,
LLVMContext::MD_range,
LLVMContext::MD_alias_scope,
LLVMContext::MD_nontemporal,
LLVMContext::MD_nonnull,
LLVMContext::MD_align,
LLVMContext::MD_type};
I.dropUnknownNonDebugMetadata(ValidMetadataAfterRS4GC);
}
static void stripNonValidDataFromBody(Function &F) {
if (F.empty())
return;
LLVMContext &Ctx = F.getContext();
MDBuilder Builder(Ctx);
SmallVector<IntrinsicInst*, 12> InvariantStartInstructions;
for (Instruction &I : instructions(F)) {
if (auto *II = dyn_cast<IntrinsicInst>(&I))
if (II->getIntrinsicID() == Intrinsic::invariant_start) {
InvariantStartInstructions.push_back(II);
continue;
}
if (MDNode *Tag = I.getMetadata(LLVMContext::MD_tbaa)) {
MDNode *MutableTBAA = Builder.createMutableTBAAAccessTag(Tag);
I.setMetadata(LLVMContext::MD_tbaa, MutableTBAA);
}
stripInvalidMetadataFromInstruction(I);
AttributeMask R = getParamAndReturnAttributesToRemove();
if (auto *Call = dyn_cast<CallBase>(&I)) {
for (int i = 0, e = Call->arg_size(); i != e; i++)
if (isa<PointerType>(Call->getArgOperand(i)->getType()))
Call->removeParamAttrs(i, R);
if (isa<PointerType>(Call->getType()))
Call->removeRetAttrs(R);
}
}
for (auto *II : InvariantStartInstructions) {
II->replaceAllUsesWith(UndefValue::get(II->getType()));
II->eraseFromParent();
}
}
static bool shouldRewriteStatepointsIn(Function &F) {
if (F.hasGC()) {
const auto &FunctionGCName = F.getGC();
const StringRef StatepointExampleName("statepoint-example");
const StringRef CoreCLRName("coreclr");
return (StatepointExampleName == FunctionGCName) ||
(CoreCLRName == FunctionGCName);
} else
return false;
}
static void stripNonValidData(Module &M) {
#ifndef NDEBUG
assert(llvm::any_of(M, shouldRewriteStatepointsIn) && "precondition!");
#endif
for (Function &F : M)
stripNonValidAttributesFromPrototype(F);
for (Function &F : M)
stripNonValidDataFromBody(F);
}
bool RewriteStatepointsForGC::runOnFunction(Function &F, DominatorTree &DT,
TargetTransformInfo &TTI,
const TargetLibraryInfo &TLI) {
assert(!F.isDeclaration() && !F.empty() &&
"need function body to rewrite statepoints in");
assert(shouldRewriteStatepointsIn(F) && "mismatch in rewrite decision");
auto NeedsRewrite = [&TLI](Instruction &I) {
if (const auto *Call = dyn_cast<CallBase>(&I)) {
if (isa<GCStatepointInst>(Call))
return false;
if (callsGCLeafFunction(Call, TLI))
return false;
if (!AllowStatepointWithNoDeoptInfo &&
!Call->getOperandBundle(LLVMContext::OB_deopt)) {
assert((isa<AtomicMemCpyInst>(Call) || isa<AtomicMemMoveInst>(Call)) &&
"Don't expect any other calls here!");
return false;
}
return true;
}
return false;
};
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
bool MadeChange = removeUnreachableBlocks(F, &DTU);
DTU.getDomTree();
SmallVector<CallBase *, 64> ParsePointNeeded;
SmallVector<CallInst *, 64> Intrinsics;
for (Instruction &I : instructions(F)) {
if (NeedsRewrite(I)) {
assert(DT.isReachableFromEntry(I.getParent()) &&
"no unreachable blocks expected");
ParsePointNeeded.push_back(cast<CallBase>(&I));
}
if (auto *CI = dyn_cast<CallInst>(&I))
if (CI->getIntrinsicID() == Intrinsic::experimental_gc_get_pointer_base ||
CI->getIntrinsicID() == Intrinsic::experimental_gc_get_pointer_offset)
Intrinsics.emplace_back(CI);
}
if (ParsePointNeeded.empty() && Intrinsics.empty())
return MadeChange;
for (BasicBlock &BB : F)
if (BB.getUniquePredecessor())
MadeChange |= FoldSingleEntryPHINodes(&BB);
auto getConditionInst = [](Instruction *TI) -> Instruction * {
if (auto *BI = dyn_cast<BranchInst>(TI))
if (BI->isConditional())
return dyn_cast<Instruction>(BI->getCondition());
return nullptr;
};
for (BasicBlock &BB : F) {
Instruction *TI = BB.getTerminator();
if (auto *Cond = getConditionInst(TI))
if (isa<ICmpInst>(Cond) && Cond->hasOneUse()) {
MadeChange = true;
Cond->moveBefore(TI);
}
}
for (Instruction &I : instructions(F)) {
if (!isa<GetElementPtrInst>(I))
continue;
unsigned VF = 0;
for (unsigned i = 0; i < I.getNumOperands(); i++)
if (auto *OpndVTy = dyn_cast<VectorType>(I.getOperand(i)->getType())) {
assert(VF == 0 ||
VF == cast<FixedVectorType>(OpndVTy)->getNumElements());
VF = cast<FixedVectorType>(OpndVTy)->getNumElements();
}
if (!I.getOperand(0)->getType()->isVectorTy() && VF != 0) {
IRBuilder<> B(&I);
auto *Splat = B.CreateVectorSplat(VF, I.getOperand(0));
I.setOperand(0, Splat);
MadeChange = true;
}
}
DefiningValueMapTy DVCache;
IsKnownBaseMapTy KnownBases;
if (!Intrinsics.empty())
MadeChange |= inlineGetBaseAndOffset(F, Intrinsics, DVCache, KnownBases);
if (!ParsePointNeeded.empty())
MadeChange |=
insertParsePoints(F, DT, TTI, ParsePointNeeded, DVCache, KnownBases);
return MadeChange;
}
static void computeLiveInValues(BasicBlock::reverse_iterator Begin,
BasicBlock::reverse_iterator End,
SetVector<Value *> &LiveTmp) {
for (auto &I : make_range(Begin, End)) {
LiveTmp.remove(&I);
if (isa<PHINode>(I))
continue;
for (Value *V : I.operands()) {
assert(!isUnhandledGCPointerType(V->getType()) &&
"support for FCA unimplemented");
if (isHandledGCPointerType(V->getType()) && !isa<Constant>(V)) {
LiveTmp.insert(V);
}
}
}
}
static void computeLiveOutSeed(BasicBlock *BB, SetVector<Value *> &LiveTmp) {
for (BasicBlock *Succ : successors(BB)) {
for (auto &I : *Succ) {
PHINode *PN = dyn_cast<PHINode>(&I);
if (!PN)
break;
Value *V = PN->getIncomingValueForBlock(BB);
assert(!isUnhandledGCPointerType(V->getType()) &&
"support for FCA unimplemented");
if (isHandledGCPointerType(V->getType()) && !isa<Constant>(V))
LiveTmp.insert(V);
}
}
}
static SetVector<Value *> computeKillSet(BasicBlock *BB) {
SetVector<Value *> KillSet;
for (Instruction &I : *BB)
if (isHandledGCPointerType(I.getType()))
KillSet.insert(&I);
return KillSet;
}
#ifndef NDEBUG
static void checkBasicSSA(DominatorTree &DT, SetVector<Value *> &Live,
Instruction *TI, bool TermOkay = false) {
for (Value *V : Live) {
if (auto *I = dyn_cast<Instruction>(V)) {
if (TermOkay && TI == I)
continue;
assert(DT.dominates(I, TI) &&
"basic SSA liveness expectation violated by liveness analysis");
}
}
}
static void checkBasicSSA(DominatorTree &DT, GCPtrLivenessData &Data,
BasicBlock &BB) {
checkBasicSSA(DT, Data.LiveSet[&BB], BB.getTerminator());
checkBasicSSA(DT, Data.LiveOut[&BB], BB.getTerminator(), true);
checkBasicSSA(DT, Data.LiveIn[&BB], BB.getTerminator());
}
#endif
static void computeLiveInValues(DominatorTree &DT, Function &F,
GCPtrLivenessData &Data) {
SmallSetVector<BasicBlock *, 32> Worklist;
for (BasicBlock &BB : F) {
Data.KillSet[&BB] = computeKillSet(&BB);
Data.LiveSet[&BB].clear();
computeLiveInValues(BB.rbegin(), BB.rend(), Data.LiveSet[&BB]);
#ifndef NDEBUG
for (Value *Kill : Data.KillSet[&BB])
assert(!Data.LiveSet[&BB].count(Kill) && "live set contains kill");
#endif
Data.LiveOut[&BB] = SetVector<Value *>();
computeLiveOutSeed(&BB, Data.LiveOut[&BB]);
Data.LiveIn[&BB] = Data.LiveSet[&BB];
Data.LiveIn[&BB].set_union(Data.LiveOut[&BB]);
Data.LiveIn[&BB].set_subtract(Data.KillSet[&BB]);
if (!Data.LiveIn[&BB].empty())
Worklist.insert(pred_begin(&BB), pred_end(&BB));
}
while (!Worklist.empty()) {
BasicBlock *BB = Worklist.pop_back_val();
SetVector<Value *> LiveOut = Data.LiveOut[BB];
const auto OldLiveOutSize = LiveOut.size();
for (BasicBlock *Succ : successors(BB)) {
assert(Data.LiveIn.count(Succ));
LiveOut.set_union(Data.LiveIn[Succ]);
}
if (OldLiveOutSize == LiveOut.size()) {
continue;
}
Data.LiveOut[BB] = LiveOut;
SetVector<Value *> LiveTmp = LiveOut;
LiveTmp.set_union(Data.LiveSet[BB]);
LiveTmp.set_subtract(Data.KillSet[BB]);
assert(Data.LiveIn.count(BB));
const SetVector<Value *> &OldLiveIn = Data.LiveIn[BB];
if (OldLiveIn.size() != LiveTmp.size()) {
Data.LiveIn[BB] = LiveTmp;
Worklist.insert(pred_begin(BB), pred_end(BB));
}
}
#ifndef NDEBUG
for (BasicBlock &BB : F)
checkBasicSSA(DT, Data, BB);
#endif
}
static void findLiveSetAtInst(Instruction *Inst, GCPtrLivenessData &Data,
StatepointLiveSetTy &Out) {
BasicBlock *BB = Inst->getParent();
assert(Data.LiveOut.count(BB));
SetVector<Value *> LiveOut = Data.LiveOut[BB];
computeLiveInValues(BB->rbegin(), ++Inst->getIterator().getReverse(),
LiveOut);
LiveOut.remove(Inst);
Out.insert(LiveOut.begin(), LiveOut.end());
}
static void recomputeLiveInValues(GCPtrLivenessData &RevisedLivenessData,
CallBase *Call,
PartiallyConstructedSafepointRecord &Info,
PointerToBaseTy &PointerToBase) {
StatepointLiveSetTy Updated;
findLiveSetAtInst(Call, RevisedLivenessData, Updated);
for (auto V : Updated)
PointerToBase.insert({ V, V });
Info.LiveSet = Updated;
}