Index: llvm/include/llvm/IR/Attributes.td =================================================================== --- llvm/include/llvm/IR/Attributes.td +++ llvm/include/llvm/IR/Attributes.td @@ -202,6 +202,10 @@ /// Zero extended before/after call. def ZExt : EnumAttr<"zeroext">; +/// No Free Call +/// This is an experimental attribute. +def NoFree : StrBoolAttr<"nofree">; + /// Target-independent string attributes. def LessPreciseFPMAD : StrBoolAttr<"less-precise-fpmad">; def NoInfsFPMath : StrBoolAttr<"no-infs-fp-math">; Index: llvm/lib/Transforms/IPO/Attributor.cpp =================================================================== --- llvm/lib/Transforms/IPO/Attributor.cpp +++ llvm/lib/Transforms/IPO/Attributor.cpp @@ -50,6 +50,8 @@ STATISTIC(NumFnArgumentReturned, "Number of function arguments marked returned"); +STATISTIC(NumFnNoFree, "Number of function marked nofree"); + // TODO: Determine a good default value. // // In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads @@ -179,6 +181,14 @@ if (!AreStatisticsEnabled()) return; + if (Attr.isStringAttribute()) { + StringRef StringAttr = Attr.getKindAsString(); + if (StringAttr == "nofree") { + NumFnNoFree++; + } + return; + } + if (!Attr.isEnumAttribute()) return; switch (Attr.getKindAsEnum()) { @@ -700,6 +710,102 @@ return Changed; } +/// ------------------------ No-Free Attributes ---------------------------- + +class AANoFreeFunction final : public AbstractAttribute, BooleanState { +public: + /// See AbstractAttribute::AbstractAttribute(...). + AANoFreeFunction(Function &F, InformationCache &InfoCache) + : AbstractAttribute(F, InfoCache) {} + + /// See AbstractAttribute::getState() + ///{ + AbstractState &getState() override { return *this; } + const AbstractState &getState() const override { return *this; } + ///} + + /// See AbstractAttribute::getManifestPosition(). + virtual ManifestPosition getManifestPosition() const override { + return MP_FUNCTION; + } + virtual const std::string getAsStr() const override { + return getAssumed() ? "nofree" : "may-free"; + } + + /// See AbstractAttribute::updateImpl(...). + virtual ChangeStatus updateImpl(Attributor &A) override; + + /// Return the deduced attributes in \p Attrs. + virtual void + getDeducedAttributes(SmallVectorImpl &Attrs) const override { + LLVMContext &Ctx = AnchoredVal.getContext(); + Attrs.emplace_back(Attribute::get(Ctx, "nofree")); + } + + /// See AbstractAttribute::getAttrKind(). + virtual Attribute::AttrKind getAttrKind() const override { + return Attribute::None; + } + + bool isAssumedNoFree() const { return getAssumed(); } + bool isKnownNoFree() const { return getKnown(); } + + // FIXME: I tried ((AttrKind) - 2) for ID but I got exception so I use + // Attribute::None + 1 for now. + static constexpr Attribute::AttrKind ID = + Attribute::AttrKind(Attribute::None + 1); +}; + +ChangeStatus AANoFreeFunction::updateImpl(Attributor &A) { + Function &F = getAnchorScope(); + + // The map from instruction opcodes to those instructions in the function. + auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F); + + for (unsigned Opcode : + {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr, + (unsigned)Instruction::Call}) { + for (Instruction *I : OpcodeInstMap[Opcode]) { + if (IntrinsicInst *II = dyn_cast(I)) { + switch (II->getIntrinsicID()) { + // TODO: Enumerate all nofree intrinsic functions. + // I think we need to deal with intrinsic function in a more + // appropriate way. + + case Intrinsic::cos: + case Intrinsic::donothing: + case Intrinsic::exp: + case Intrinsic::exp2: + case Intrinsic::expect: + case Intrinsic::fabs: + case Intrinsic::floor: + case Intrinsic::log: + case Intrinsic::log10: + case Intrinsic::log2: + case Intrinsic::memset: + case Intrinsic::memcpy: + case Intrinsic::memmove: + case Intrinsic::pow: + case Intrinsic::powi: + case Intrinsic::sin: + case Intrinsic::sqrt: + continue; + default: + break; + } + } + ImmutableCallSite ICS(I); + auto *NoFreeAA = A.getAAFor(*this, *I); + if ((!NoFreeAA || !NoFreeAA->isAssumedNoFree()) && + !ICS.hasFnAttr("nofree")) { + indicatePessimisticFixpoint(); + return ChangeStatus::CHANGED; + } + } + } + return ChangeStatus::UNCHANGED; +} + /// ---------------------------------------------------------------------------- /// Attributor /// ---------------------------------------------------------------------------- @@ -849,6 +955,9 @@ registerAA(*new AAReturnedValuesImpl(F, InfoCache)); } + // Every function might be "no-free". + registerAA(*new AANoFreeFunction(F, InfoCache)); + // Walk all instructions to find more attribute opportunities and also // interesting instructions that might be queried by abstract attributes // during their initialization or update. @@ -862,6 +971,9 @@ default: break; case Instruction::Ret: // ReturnInst are interesting for AAReturnedValues. + case Instruction::Call: + case Instruction::CallBr: + case Instruction::Invoke: IsInterestingOpcode = true; } if (IsInterestingOpcode)