diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h index cb7a4b0123f010..90d88ce21e9ad5 100644 --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -252,22 +252,14 @@ struct IRPosition { /// sufficient to determine where arguments will be manifested. This is, so /// far, only the case for call site arguments as the value is not sufficient /// to pinpoint them. Instead, we can use the call site as an anchor. - /// - ///{ - Value &getAnchorValue() { + Value &getAnchorValue() const { assert(KindOrArgNo != IRP_INVALID && "Invalid position does not have an anchor value!"); return *AnchorVal; } - const Value &getAnchorValue() const { - return const_cast(this)->getAnchorValue(); - } - ///} /// Return the associated function, if any. - /// - ///{ - Function *getAssociatedFunction() { + Function *getAssociatedFunction() const { if (auto *CB = dyn_cast(AnchorVal)) return CB->getCalledFunction(); assert(KindOrArgNo != IRP_INVALID && @@ -281,15 +273,9 @@ struct IRPosition { return cast(V).getFunction(); return nullptr; } - const Function *getAssociatedFunction() const { - return const_cast(this)->getAssociatedFunction(); - } - ///} /// Return the associated argument, if any. - /// - ///{ - Argument *getAssociatedArgument() { + Argument *getAssociatedArgument() const { if (auto *Arg = dyn_cast(&getAnchorValue())) return Arg; int ArgNo = getArgNo(); @@ -300,10 +286,6 @@ struct IRPosition { return nullptr; return AssociatedFn->arg_begin() + ArgNo; } - const Argument *getAssociatedArgument() const { - return const_cast(this)->getAssociatedArgument(); - } - ///} /// Return true if the position refers to a function interface, that is the /// function scope, the function return, or an argumnt. @@ -319,9 +301,7 @@ struct IRPosition { } /// Return the Function surrounding the anchor value. - /// - ///{ - Function *getAnchorScope() { + Function *getAnchorScope() const { Value &V = getAnchorValue(); if (isa(V)) return &cast(V); @@ -331,15 +311,9 @@ struct IRPosition { return cast(V).getFunction(); return nullptr; } - const Function *getAnchorScope() const { - return const_cast(this)->getAnchorScope(); - } - ///} /// Return the context instruction, if any. - /// - ///{ - Instruction *getCtxI() { + Instruction *getCtxI() const { Value &V = getAnchorValue(); if (auto *I = dyn_cast(&V)) return I; @@ -351,15 +325,9 @@ struct IRPosition { return &(F->getEntryBlock().front()); return nullptr; } - const Instruction *getCtxI() const { - return const_cast(this)->getCtxI(); - } - ///} /// Return the value this abstract attribute is associated with. - /// - ///{ - Value &getAssociatedValue() { + Value &getAssociatedValue() const { assert(KindOrArgNo != IRP_INVALID && "Invalid position does not have an associated value!"); if (getArgNo() < 0 || isa(AnchorVal)) @@ -367,10 +335,6 @@ struct IRPosition { assert(isa(AnchorVal) && "Expected a call base!"); return *cast(AnchorVal)->getArgOperand(getArgNo()); } - const Value &getAssociatedValue() const { - return const_cast(this)->getAssociatedValue(); - } - ///} /// Return the argument number of the associated value if it is an argument or /// call site argument, otherwise a negative value. @@ -449,7 +413,7 @@ struct IRPosition { } /// Remove the attribute of kind \p AKs existing in the IR at this position. - void removeAttrs(ArrayRef AKs) { + void removeAttrs(ArrayRef AKs) const { if (getPositionKind() == IRP_INVALID || getPositionKind() == IRP_FLOAT) return; @@ -502,6 +466,7 @@ struct IRPosition { /// Verify internal invariants. void verify(); +protected: /// The value this position is anchored at. Value *AnchorVal; @@ -785,7 +750,7 @@ struct Attributor { "'AbstractAttribute'!"); // Put the attribute in the lookup map structure and the container we use to // keep track of all attributes. - IRPosition &IRP = AA.getIRPosition(); + const IRPosition &IRP = AA.getIRPosition(); auto &KindToAbstractAttributeMap = AAMap[IRP]; assert(!KindToAbstractAttributeMap.count(&AAType::ID) && "Attribute already in map!"); @@ -1320,7 +1285,7 @@ struct BooleanState : public IntegerStateBase { /// Helper struct necessary as the modular build fails if the virtual method /// IRAttribute::manifest is defined in the Attributor.cpp. struct IRAttributeManifest { - static ChangeStatus manifestAttrs(Attributor &A, IRPosition &IRP, + static ChangeStatus manifestAttrs(Attributor &A, const IRPosition &IRP, const ArrayRef &DeducedAttrs); }; @@ -1383,11 +1348,7 @@ struct IRAttribute : public IRPosition, public Base { } /// Return an IR position, see struct IRPosition. - /// - ///{ - IRPosition &getIRPosition() override { return *this; } const IRPosition &getIRPosition() const override { return *this; } - ///} }; /// Base struct for all "concrete attribute" deductions. @@ -1492,9 +1453,6 @@ struct AbstractAttribute { /// add statistics for them. virtual void trackStatistics() const = 0; - /// Return an IR position, see struct IRPosition. - virtual IRPosition &getIRPosition() = 0; - /// The actual update/transfer function which has to be implemented by the /// derived classes. /// @@ -1751,11 +1709,7 @@ struct AAIsDead : public StateWrapper, } /// Return an IR position, see struct IRPosition. - /// - ///{ - IRPosition &getIRPosition() override { return *this; } const IRPosition &getIRPosition() const override { return *this; } - ///} /// Create an abstract attribute view for the position \p IRP. static AAIsDead &createForPosition(const IRPosition &IRP, Attributor &A); @@ -1957,11 +1911,7 @@ struct AAValueSimplify : public StateWrapper, AAValueSimplify(const IRPosition &IRP) : IRPosition(IRP) {} /// Return an IR position, see struct IRPosition. - /// - ///{ - IRPosition &getIRPosition() { return *this; } const IRPosition &getIRPosition() const { return *this; } - ///} /// Return an assumed simplified value if a single candidate is found. If /// there cannot be one, return original value. If it is not clear yet, return @@ -1987,11 +1937,7 @@ struct AAHeapToStack : public StateWrapper, bool isKnownHeapToStack() const { return getKnown(); } /// Return an IR position, see struct IRPosition. - /// - ///{ - IRPosition &getIRPosition() { return *this; } const IRPosition &getIRPosition() const { return *this; } - ///} /// Create an abstract attribute view for the position \p IRP. static AAHeapToStack &createForPosition(const IRPosition &IRP, Attributor &A); diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp index a811471d37eaca..b397f5a1655826 100644 --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -328,7 +328,7 @@ ChangeStatus AbstractAttribute::update(Attributor &A) { } ChangeStatus -IRAttributeManifest::manifestAttrs(Attributor &A, IRPosition &IRP, +IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP, const ArrayRef &DeducedAttrs) { Function *ScopeFn = IRP.getAssociatedFunction(); IRPosition::Kind PK = IRP.getPositionKind(); @@ -705,7 +705,7 @@ struct AAFromMustBeExecutedContext : public Base { void initialize(Attributor &A) override { Base::initialize(A); - IRPosition &IRP = this->getIRPosition(); + const IRPosition &IRP = this->getIRPosition(); Instruction *CtxI = IRP.getCtxI(); if (!CtxI) @@ -987,7 +987,9 @@ ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) { // If the assumed unique return value is an argument, annotate it. if (auto *UniqueRVArg = dyn_cast(UniqueRV.getValue())) { - getIRPosition() = IRPosition::argument(*UniqueRVArg); + // TODO: This should be handled differently! + this->AnchorVal = UniqueRVArg; + this->KindOrArgNo = UniqueRVArg->getArgNo(); Changed = IRAttribute::manifest(A); } else if (auto *RVC = dyn_cast(UniqueRV.getValue())) { // We can replace the returned value with the unique returned constant. @@ -3816,7 +3818,7 @@ struct AAMemoryBehaviorImpl : public AAMemoryBehavior { /// See AbstractAttribute::manifest(...). ChangeStatus manifest(Attributor &A) override { - IRPosition &IRP = getIRPosition(); + const IRPosition &IRP = getIRPosition(); // Check if we would improve the existing attributes first. SmallVector DeducedAttrs;