[llvm] [Don't merge][LLVM] Refine undef to null values (PR #96375)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 21 23:05:27 PDT 2024
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/96375
>From 60d13b16a5a0c3743051aa4ed12eba023f7827d6 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
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/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 ++--
11 files changed, 27 insertions(+), 30 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/ScheduleDAGInstrs.h b/llvm/include/llvm/CodeGen/ScheduleDAGInstrs.h
index 32ff15fc75936..7e741a8704711 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 4d13c3880c692..8daaed00fbf10 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/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 900c33b580f15..8056cc13efe35 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -7715,7 +7715,7 @@ class VectorPromoteHelper {
if (!EC.isScalable()) {
SmallVector<Constant *, 4> 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 7ff14a6cf36bf..7fb3390b0ce17 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<const Value *, SmallVector<SUnit *, 4>> 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 c848ce4af86cc..94f92f41d287c 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 bc91f904d7e87..5f08f77fa08e5 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<ArrayType>(getType()))
return UndefValue::get(ATy->getElementType());
return UndefValue::get(cast<VectorType>(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<ArrayType>(getType()) || isa<VectorType>(getType()))
return getSequentialElement();
return getStructElement(cast<ConstantInt>(C)->getZExtValue());
}
-UndefValue *UndefValue::getElementValue(unsigned Idx) const {
+Constant *UndefValue::getElementValue(unsigned Idx) const {
if (isa<ArrayType>(getType()) || isa<VectorType>(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<UndefValue> &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 c4b9375a53a27..f2448c4004fda 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<UndefValue>(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<ReturnInst>(I);
if (!isa<UndefValue>(RI.getReturnValue()))
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 7b1268939e9c4..278423995dc44 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 2adbdca4b5286..c527fdb2b4ee0 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<Instruction>(Operand)) {
diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
index 002ed381a4fd2..031aafd63aab2 100644
--- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -276,7 +276,7 @@ static void buildPartialUnswitchConditionalBranch(
SmallVector<Value *> 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 9f91b4f3f9939..f590ff2cfd36e 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()));
More information about the llvm-commits
mailing list