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,83 @@ return Changed; } +/// ------------------------ No-Free Attributes ---------------------------- + +struct AANoFreeFunction : AbstractAttribute, BooleanState { + + /// 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; + } + + /// See AbstractAttribute::getAsStr(). + 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; + } + + /// Return true if "nofree" is assumed. + bool isAssumedNoFree() const { return getAssumed(); } + + /// Return true if "nofree" is known. + 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]) { + // FIXME: Assume that all intrinsic function would free memory for now. + + auto ICS = ImmutableCallSite(I); + auto *NoFreeAA = A.getAAFor(*this, *I); + + if ((!NoFreeAA || !NoFreeAA->isValidState() || + !NoFreeAA->isAssumedNoFree()) && + !ICS.hasFnAttr("nofree")) { + indicatePessimisticFixpoint(); + return ChangeStatus::CHANGED; + } + } + } + return ChangeStatus::UNCHANGED; +} + /// ---------------------------------------------------------------------------- /// Attributor /// ---------------------------------------------------------------------------- @@ -849,6 +936,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. @@ -860,7 +950,14 @@ switch (I.getOpcode()) { default: + assert((!ImmutableCallSite(&I)) && (!isa(&I)) && + "New call site/base instruction type needs to be known in the " + "attributor!"); break; + case Instruction::Call: + case Instruction::CallBr: + case Instruction::Invoke: + // Call-like instructions are interesting for AANoFreeFunction. case Instruction::Ret: // ReturnInst are interesting for AAReturnedValues. IsInterestingOpcode = true; } Index: llvm/test/Transforms/FunctionAttrs/nofree.ll =================================================================== --- llvm/test/Transforms/FunctionAttrs/nofree.ll +++ llvm/test/Transforms/FunctionAttrs/nofree.ll @@ -1,4 +1,6 @@ -; RUN: opt -functionattrs -S < %s | FileCheck %s +; RUN: opt -functionattrs -S < %s | FileCheck %s --check-prefix=FNATTR +; RUN: opt -attributor -S < %s | FileCheck %s --check-prefix=ATTRIBUTOR + target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" ; Test cases specifically designed for the "nofree" function attribute. @@ -11,9 +13,9 @@ ; TEST 1 (positive case) -; FIXME: missing "nofree" -; CHECK: Function Attrs: noinline norecurse nounwind readnone uwtable -; CHECK: define void @only_return() +; FNATTR: Function Attrs: noinline norecurse nounwind readnone uwtable +; FNATTR: define void @only_return() +; ATTRIBUTOR: define void @only_return() #3 define void @only_return() #0 { ret void } @@ -25,8 +27,9 @@ ; free(p); ; } -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define void @only_free(i8* nocapture) local_unnamed_addr +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR: define void @only_free(i8* nocapture) local_unnamed_addr +; ATTRIBUTOR: define void @only_free(i8* nocapture) local_unnamed_addr #1 define void @only_free(i8* nocapture) local_unnamed_addr #0 { tail call void @free(i8* %0) #1 ret void @@ -44,17 +47,19 @@ ; } -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define void @free_in_scc1(i8* nocapture) local_unnamed_addr +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR: define void @free_in_scc1(i8* nocapture) local_unnamed_addr +; ATTRIBUTOR :define void @free_in_scc1(i8* nocapture) local_unnamed_addr #1 define void @free_in_scc1(i8* nocapture) local_unnamed_addr #0 { tail call void @free_in_scc2(i8* %0) #1 ret void } -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define void @free_in_scc2(i8* nocapture) local_unnamed_addr -define void @free_in_scc2(i8*) local_unnamed_addr #0 { +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR: define void @free_in_scc2(i8* nocapture) local_unnamed_addr +; ATTRIBUTOR: define void @free_in_scc2(i8* nocapture) local_unnamed_addr #1 +define void @free_in_scc2(i8* nocapture) local_unnamed_addr #0 { tail call void @free_in_scc1(i8* %0) tail call void @free(i8* %0) #1 ret void @@ -71,17 +76,17 @@ ; } -; FIXME: missing "nofree" -; CHECK: Function Attrs: noinline nounwind readnone uwtable -; CHECK: define void @mutual_recursion1() +; FNATTR: Function Attrs: noinline nounwind readnone uwtable +; FNATTR: define void @mutual_recursion1() +; ATTRIBUTOR: define void @mutual_recursion1() #3 define void @mutual_recursion1() #0 { call void @mutual_recursion2() ret void } -; FIXME: missing "nofree" -; CHECK: Function Attrs: noinline nounwind readnone uwtable -; CHECK: define void @mutual_recursion2() +; FNATTR: Function Attrs: noinline nounwind readnone uwtable +; FNATTR: define void @mutual_recursion2() +; ATTRIBUTOR: define void @mutual_recursion2() #3 define void @mutual_recursion2() #0 { call void @mutual_recursion1() ret void @@ -94,8 +99,9 @@ ; delete [] p; ; } -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define void @_Z9delete_opPc(i8*) local_unnamed_addr +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR: define void @_Z9delete_opPc(i8*) local_unnamed_addr +; ATTRIBUTOR: define void @_Z9delete_opPc(i8*) local_unnamed_addr #1 define void @_Z9delete_opPc(i8*) local_unnamed_addr #0 { %2 = icmp eq i8* %0, null br i1 %2, label %4, label %3 @@ -111,8 +117,9 @@ ; TEST 6 (negative case) ; Call realloc -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define noalias i8* @call_realloc(i8* nocapture, i64) local_unnamed_addr +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR: define noalias i8* @call_realloc(i8* nocapture, i64) local_unnamed_addr +; ATTRIBUTOR: define noalias i8* @call_realloc(i8* nocapture, i64) local_unnamed_addr #1 define noalias i8* @call_realloc(i8*nocapture, i64) local_unnamed_addr #0 { %ret = tail call i8* @realloc(i8* %0, i64 %1) #2 ret i8* %ret @@ -124,9 +131,9 @@ declare void @nofree_function() "nofree" readnone #0 -; FIXME: missing "nofree" -; Function Attrs: noinline nounwind readnone uwtable -; CHECK: define void @call_nofree_function() +; FNATTR: Function Attrs: noinline nounwind readnone uwtable +; FNATTR: define void @call_nofree_function() +; ATTRIBUTOR: define void @call_nofree_function() #3 define void @call_nofree_function() #0 { tail call void @nofree_function() ret void @@ -138,8 +145,9 @@ declare void @maybe_free() #0 -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define void @call_maybe_free() +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR: define void @call_maybe_free() +; ATTRIBUTOR: define void @call_maybe_free() #1 define void @call_maybe_free() #0 { tail call void @maybe_free() ret void @@ -149,8 +157,9 @@ ; TEST 9 (nagative case) ; Call both of above functions -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define void @call_both() +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR: define void @call_both() +; ATTRIBUTOR: define void @call_both() #1 define void @call_both() #0 { tail call void @maybe_free() tail call void @nofree_function() @@ -158,6 +167,42 @@ } +; TEST 10 (positive case) +; Call intrinsic function +declare float @llvm.floor.f32(float) + +; FIXME: missing "nofree" +; FNATTRS: Function Attrs: noinline nounwind uwtable +; FNATTRS : define void @call_floor(float %a) +; ATTRIBUTOR : define void @call_floor(float %a) #1 +define void @call_floor(float %a) #0 { + tail call float @llvm.floor.f32(float %a) + ret void +} + +; TEST 11 (positive case) +; Check propagation. + +; FNATTRS: Function Attrs: noinline nounwind uwtable +; FNATTRS: define void @f1() +; ATTRIBUTOR: define void @f1() #3 +define void @f1() #0 { + tail call void @nofree_function() + ret void +} + +; FNATTRS: Function Attrs: noinline nounwind uwtable +; FNATTRS: define void @f2() +; ATTRIBUTOR: define void @f2() #3 +define void @f2() #0 { + tail call void @f1() + ret void +} + + attributes #0 = { nounwind uwtable noinline } attributes #1 = { nounwind } attributes #2 = { nobuiltin nounwind } + +; ATTRIBUTOR: attributes #1 = { noinline nounwind uwtable } +; ATTRIBUTOR: attributes #3 = { noinline nounwind uwtable "nofree" } \ No newline at end of file