Skip to content

Commit

Permalink
[LLVM] Refine undef to null values
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcxzyw committed Jun 22, 2024
1 parent a9efcbf commit 69f4f2a
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 34 deletions.
3 changes: 1 addition & 2 deletions llvm/include/llvm/CodeGen/ScheduleDAGInstrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 8 additions & 8 deletions llvm/include/llvm/IR/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/MachinePipeliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/ScheduleDAGInstrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
17 changes: 7 additions & 10 deletions llvm/lib/IR/Constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/IPO/AttributorAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()))
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/unittests/IR/PatternMatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 69f4f2a

Please sign in to comment.