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,29 +13,33 @@ ; 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-NEXT: define void @only_return() +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable "nofree" +; ATTRIBUTOR-NEXT: define void @only_return() define void @only_return() #0 { ret void } -; TEST 2 (nagative case) +; TEST 2 (negative case) ; Only free ; void only_free(char* p) { ; 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-NEXT: define void @only_free(i8* nocapture) local_unnamed_addr +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable +; ATTRIBUTOR-NOT: "nofree" +; ATTRIBUTOR-NEXT: 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 } -; TEST 3 (nagative case) +; TEST 3 (negative case) ; Free occurs in same scc. ; void free_in_scc1(char*p){ ; free_in_scc2(p); @@ -44,17 +50,23 @@ ; } -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define void @free_in_scc1(i8* nocapture) local_unnamed_addr +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR-NEXT: define void @free_in_scc1(i8* nocapture) local_unnamed_addr +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable +; ATTRIBUTOR-NOT: "nofree" +; ATTRIBUTOR-NEXT :define void @free_in_scc1(i8* nocapture) local_unnamed_addr 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-NEXT: define void @free_in_scc2(i8* nocapture) local_unnamed_addr +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable +; ATTRIBUTOR-NOT: "nofree" +; ATTRIBUTOR: define void @free_in_scc2(i8* nocapture) local_unnamed_addr +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 +83,19 @@ ; } -; FIXME: missing "nofree" -; CHECK: Function Attrs: noinline nounwind readnone uwtable -; CHECK: define void @mutual_recursion1() +; FNATTR: Function Attrs: noinline nounwind readnone uwtable +; FNATTR-NEXT: define void @mutual_recursion1() +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable "nofree" +; ATTRIBUTOR-NEXT: define void @mutual_recursion1() 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-NEXT: define void @mutual_recursion2() +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable "nofree" +; ATTRIBUTOR-NEXT: define void @mutual_recursion2() define void @mutual_recursion2() #0 { call void @mutual_recursion1() ret void @@ -89,13 +103,16 @@ ; TEST 5 -; C++ delete operation (nagative case) +; C++ delete operation (negative case) ; void delete_op (char p[]){ ; delete [] p; ; } -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define void @_Z9delete_opPc(i8*) local_unnamed_addr +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR-NEXT: define void @_Z9delete_opPc(i8*) local_unnamed_addr +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable +; ATTRIBUTOR-NOT: "nofree" +; ATTRIBUTOR-NEXT: 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,9 +128,12 @@ ; TEST 6 (negative case) ; Call realloc -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define noalias i8* @call_realloc(i8* nocapture, i64) local_unnamed_addr -define noalias i8* @call_realloc(i8*nocapture, i64) local_unnamed_addr #0 { +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR-NEXT: define noalias i8* @call_realloc(i8* nocapture, i64) local_unnamed_addr +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable +; ATTRIBUTOR-NOT: "nofree" +; ATTRIBUTOR-NEXT: define noalias i8* @call_realloc(i8* nocapture, i64) local_unnamed_addr +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 } @@ -122,35 +142,50 @@ ; TEST 7 (positive case) ; Call function declaration with "nofree" + +; FNATTR: Function Attrs: noinline nounwind readnone uwtable "nofree" +; FNATTR-NEXT: declare void @nofree_function() +; ATTRIBUTOR: Function Attrs: noinline nounwind readnone uwtable "nofree" +; ATTRIBUTOR-NEXT: declare void @nofree_function() 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-NEXT: define void @call_nofree_function() +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable "nofree" +; ATTRIBUTOR-NEXT: define void @call_nofree_function() define void @call_nofree_function() #0 { tail call void @nofree_function() ret void } -; TEST 8 (nagative case) +; TEST 8 (negative case) ; Call function declaration without "nofree" + +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable +; ATTRIBUTOR-NEXT: declare void @maybe_free() 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: Function Attrs: noinline nounwind uwtable +; ATTRIBUTOR-NOT: "nofree" +; ATTRIBUTOR-NEXT: define void @call_maybe_free() define void @call_maybe_free() #0 { tail call void @maybe_free() ret void } -; TEST 9 (nagative case) +; TEST 9 (negative case) ; Call both of above functions -; CHECK: Function Attrs: noinline nounwind uwtable -; CHECK: define void @call_both() +; FNATTR: Function Attrs: noinline nounwind uwtable +; FNATTR-NEXT: define void @call_both() +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable +; ATTRIBUTOR-NOT: "nofree" +; ATTRIBUTOR-NEXT: define void @call_both() define void @call_both() #0 { tail call void @maybe_free() tail call void @nofree_function() @@ -158,6 +193,47 @@ } +; TEST 10 (positive case) +; Call intrinsic function +; FNATTRS: Function Attrs: noinline readnone speculatable +; FNATTRS-NEXT: declare float @llvm.floor.f32(float) +; ATTRIBUTOR: Function Attrs: nounwind readnone speculatable +; ATTRIBUTOR-NEXT: declare float @llvm.floor.f32(float) +declare float @llvm.floor.f32(float) + +; FNATTRS: Function Attrs: noinline nounwind uwtable +; FNATTRS-NEXT: define void @call_floor(float %a) +; FIXME: missing "nofree" +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable +; ATTRIBUTOR-NEXT: define void @call_floor(float %a) + +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-NEXT: define void @f1() +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable "nofree" +; ATTRIBUTOR-NEXT: define void @f1() +define void @f1() #0 { + tail call void @nofree_function() + ret void +} + +; FNATTRS: Function Attrs: noinline nounwind uwtable +; FNATTRS-NEXT: define void @f2() +; ATTRIBUTOR: Function Attrs: noinline nounwind uwtable "nofree" +; ATTRIBUTOR-NEXT: define void @f2() +define void @f2() #0 { + tail call void @f1() + ret void +} + + attributes #0 = { nounwind uwtable noinline } attributes #1 = { nounwind } attributes #2 = { nobuiltin nounwind }