From 69f4f2aefd0a476d9ae815152ec2aa70b3e0c7eb Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Sat, 22 Jun 2024 13:54:58 +0800 Subject: [PATCH] [LLVM] Refine undef to null values --- llvm/include/llvm/CodeGen/ScheduleDAGInstrs.h | 3 +-- llvm/include/llvm/IR/Constants.h | 16 ++++++++-------- llvm/lib/Analysis/ValueTracking.cpp | 4 ---- llvm/lib/CodeGen/CodeGenPrepare.cpp | 2 +- llvm/lib/CodeGen/MachinePipeliner.cpp | 2 +- llvm/lib/CodeGen/ScheduleDAGInstrs.cpp | 2 +- llvm/lib/IR/Constants.cpp | 17 +++++++---------- .../lib/Transforms/IPO/AttributorAttributes.cpp | 4 ++-- .../Transforms/InstCombine/InstCombineCasts.cpp | 2 +- llvm/lib/Transforms/Scalar/SROA.cpp | 3 ++- .../Transforms/Scalar/SimpleLoopUnswitch.cpp | 2 +- llvm/unittests/IR/PatternMatch.cpp | 4 ++-- 12 files changed, 27 insertions(+), 34 deletions(-) diff --git a/llvm/include/llvm/CodeGen/ScheduleDAGInstrs.h b/llvm/include/llvm/CodeGen/ScheduleDAGInstrs.h index 32ff15fc75936a..7e741a87047116 100644 --- a/llvm/include/llvm/CodeGen/ScheduleDAGInstrs.h +++ b/llvm/include/llvm/CodeGen/ScheduleDAGInstrs.h @@ -247,8 +247,7 @@ namespace llvm { void insertBarrierChain(Value2SUsMap &map); /// For an unanalyzable memory access, this Value is used in maps. - UndefValue *UnknownValue; - + Constant *UnknownValue; /// Topo - A topological ordering for SUnits which permits fast IsReachable /// and similar queries. diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h index 4d13c3880c6929..8daaed00fbf10b 100644 --- a/llvm/include/llvm/IR/Constants.h +++ b/llvm/include/llvm/IR/Constants.h @@ -1385,33 +1385,33 @@ DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantExpr, Constant) class UndefValue : public ConstantData { friend class Constant; - explicit UndefValue(Type *T) : ConstantData(T, UndefValueVal) {} - void destroyConstantImpl(); protected: - explicit UndefValue(Type *T, ValueTy vty) : ConstantData(T, vty) {} + explicit UndefValue(Type *T, ValueTy vty) : ConstantData(T, vty) { + assert(vty == PoisonValueVal && "undef value is not allowed"); + } public: UndefValue(const UndefValue &) = delete; /// Static factory methods - Return an 'undef' object of the specified type. - static UndefValue *get(Type *T); + static Constant *get(Type *T); /// If this Undef has array or vector type, return a undef with the right /// element type. - UndefValue *getSequentialElement() const; + Constant *getSequentialElement() const; /// If this undef has struct type, return a undef with the right element type /// for the specified element. - UndefValue *getStructElement(unsigned Elt) const; + Constant *getStructElement(unsigned Elt) const; /// Return an undef of the right value for the specified GEP index if we can, /// otherwise return null (e.g. if C is a ConstantExpr). - UndefValue *getElementValue(Constant *C) const; + Constant *getElementValue(Constant *C) const; /// Return an undef of the right value for the specified GEP index. - UndefValue *getElementValue(unsigned Idx) const; + Constant *getElementValue(unsigned Idx) const; /// Return the number of elements in the array, vector, or struct. unsigned getNumElements() const; diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 0df061923f625a..0157ca194c1d8e 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -5877,10 +5877,6 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { return LHS; if (!LHS || !RHS) return nullptr; - if (LHS == UndefInt8) - return RHS; - if (RHS == UndefInt8) - return LHS; return nullptr; }; diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp index 900c33b580f15d..8056cc13efe350 100644 --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -7715,7 +7715,7 @@ class VectorPromoteHelper { if (!EC.isScalable()) { SmallVector ConstVec; - UndefValue *UndefVal = UndefValue::get(Val->getType()); + Constant *UndefVal = UndefValue::get(Val->getType()); for (unsigned Idx = 0; Idx != EC.getKnownMinValue(); ++Idx) { if (Idx == ExtractIdx) ConstVec.push_back(Val); diff --git a/llvm/lib/CodeGen/MachinePipeliner.cpp b/llvm/lib/CodeGen/MachinePipeliner.cpp index 7ff14a6cf36bf6..7fb3390b0ce17a 100644 --- a/llvm/lib/CodeGen/MachinePipeliner.cpp +++ b/llvm/lib/CodeGen/MachinePipeliner.cpp @@ -828,7 +828,7 @@ static void getUnderlyingObjects(const MachineInstr *MI, void SwingSchedulerDAG::addLoopCarriedDependences(AliasAnalysis *AA) { MapVector> PendingLoads; Value *UnknownValue = - UndefValue::get(Type::getVoidTy(MF.getFunction().getContext())); + PoisonValue::get(Type::getVoidTy(MF.getFunction().getContext())); for (auto &SU : SUnits) { MachineInstr &MI = *SU.getInstr(); if (isDependenceBarrier(MI)) diff --git a/llvm/lib/CodeGen/ScheduleDAGInstrs.cpp b/llvm/lib/CodeGen/ScheduleDAGInstrs.cpp index c848ce4af86ccc..94f92f41d287c2 100644 --- a/llvm/lib/CodeGen/ScheduleDAGInstrs.cpp +++ b/llvm/lib/CodeGen/ScheduleDAGInstrs.cpp @@ -115,7 +115,7 @@ ScheduleDAGInstrs::ScheduleDAGInstrs(MachineFunction &mf, bool RemoveKillFlags) : ScheduleDAG(mf), MLI(mli), MFI(mf.getFrameInfo()), RemoveKillFlags(RemoveKillFlags), - UnknownValue(UndefValue::get( + UnknownValue(PoisonValue::get( Type::getVoidTy(mf.getFunction().getContext()))), Topo(SUnits, &ExitSU) { DbgValues.clear(); diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index bc91f904d7e87a..5f08f77fa08e55 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -1145,23 +1145,23 @@ ElementCount ConstantAggregateZero::getElementCount() const { // UndefValue Implementation //===----------------------------------------------------------------------===// -UndefValue *UndefValue::getSequentialElement() const { +Constant *UndefValue::getSequentialElement() const { if (ArrayType *ATy = dyn_cast(getType())) return UndefValue::get(ATy->getElementType()); return UndefValue::get(cast(getType())->getElementType()); } -UndefValue *UndefValue::getStructElement(unsigned Elt) const { +Constant *UndefValue::getStructElement(unsigned Elt) const { return UndefValue::get(getType()->getStructElementType(Elt)); } -UndefValue *UndefValue::getElementValue(Constant *C) const { +Constant *UndefValue::getElementValue(Constant *C) const { if (isa(getType()) || isa(getType())) return getSequentialElement(); return getStructElement(cast(C)->getZExtValue()); } -UndefValue *UndefValue::getElementValue(unsigned Idx) const { +Constant *UndefValue::getElementValue(unsigned Idx) const { if (isa(getType()) || isa(getType())) return getSequentialElement(); return getStructElement(Idx); @@ -1792,12 +1792,9 @@ void ConstantTargetNone::destroyConstantImpl() { getContext().pImpl->CTNConstants.erase(getType()); } -UndefValue *UndefValue::get(Type *Ty) { - std::unique_ptr &Entry = Ty->getContext().pImpl->UVConstants[Ty]; - if (!Entry) - Entry.reset(new UndefValue(Ty)); - - return Entry.get(); +Constant *UndefValue::get(Type *Ty) { + // errs() << *Ty << '\n'; + return Constant::getNullValue(Ty); } /// Remove the constant from the constant table. diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp index c4b9375a53a271..f2448c4004fda0 100644 --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -4352,7 +4352,7 @@ struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl { Use &U = CB.getArgOperandUse(getCallSiteArgNo()); assert(!isa(U.get()) && "Expected undef values to be filtered out!"); - UndefValue &UV = *UndefValue::get(U->getType()); + Constant &UV = *UndefValue::get(U->getType()); if (A.changeUseAfterManifest(U, UV)) return ChangeStatus::CHANGED; return ChangeStatus::UNCHANGED; @@ -4442,7 +4442,7 @@ struct AAIsDeadReturned : public AAIsDeadValueImpl { ChangeStatus manifest(Attributor &A) override { // TODO: Rewrite the signature to return void? bool AnyChange = false; - UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); + Constant &UV = *UndefValue::get(getAssociatedFunction()->getReturnType()); auto RetInstPred = [&](Instruction &I) { ReturnInst &RI = cast(I); if (!isa(RI.getReturnValue())) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp index 7b1268939e9c4b..278423995dc441 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -666,7 +666,7 @@ static Instruction *shrinkInsertElt(CastInst &Trunc, if (match(VecOp, m_Undef())) { // trunc (inselt undef, X, Index) --> inselt undef, (trunc X), Index // fptrunc (inselt undef, X, Index) --> inselt undef, (fptrunc X), Index - UndefValue *NarrowUndef = UndefValue::get(DestTy); + Constant *NarrowUndef = UndefValue::get(DestTy); Value *NarrowOp = Builder.CreateCast(Opcode, ScalarOp, DestScalarTy); return InsertElementInst::Create(NarrowUndef, NarrowOp, Index); } diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index 2adbdca4b52860..c527fdb2b4ee05 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -5315,7 +5315,8 @@ bool SROA::deleteDeadInstructions( } at::deleteAssignmentMarkers(I); - I->replaceAllUsesWith(UndefValue::get(I->getType())); + if (!I->use_empty()) + I->replaceAllUsesWith(UndefValue::get(I->getType())); for (Use &Operand : I->operands()) if (Instruction *U = dyn_cast(Operand)) { diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp index 002ed381a4fd23..031aafd63aab2d 100644 --- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp +++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp @@ -276,7 +276,7 @@ static void buildPartialUnswitchConditionalBranch( SmallVector FrozenInvariants; for (Value *Inv : Invariants) { - if (InsertFreeze && !isGuaranteedNotToBeUndefOrPoison(Inv, AC, I, &DT)) + if (InsertFreeze) Inv = IRB.CreateFreeze(Inv, Inv->getName() + ".fr"); FrozenInvariants.push_back(Inv); } diff --git a/llvm/unittests/IR/PatternMatch.cpp b/llvm/unittests/IR/PatternMatch.cpp index 9f91b4f3f9939f..f590ff2cfd36ec 100644 --- a/llvm/unittests/IR/PatternMatch.cpp +++ b/llvm/unittests/IR/PatternMatch.cpp @@ -1277,8 +1277,8 @@ TEST_F(PatternMatchTest, UndefPoisonMix) { StructType *StTy2 = StructType::get(ScalarTy, StTy); StructType *StTy3 = StructType::get(StTy, ScalarTy); Constant *Zero = ConstantInt::getNullValue(ScalarTy); - UndefValue *U = UndefValue::get(ScalarTy); - UndefValue *P = PoisonValue::get(ScalarTy); + Constant *U = UndefValue::get(ScalarTy); + Constant *P = PoisonValue::get(ScalarTy); EXPECT_TRUE(match(ConstantVector::get({U, P}), m_Undef())); EXPECT_TRUE(match(ConstantVector::get({P, U}), m_Undef()));