Skip to content

Commit f5e4ffa

Browse files
Revert "[llvm] Use computeConstantRange to improve llvm.objectsize computation (#114673)"
This reverts commit 5f34281. This seems to break various builders, such as https://lab.llvm.org/buildbot/#/builders/41/builds/3259 https://lab.llvm.org/buildbot/#/builders/76/builds/4298
1 parent 9123dc6 commit f5e4ffa

File tree

6 files changed

+12
-206
lines changed

6 files changed

+12
-206
lines changed

llvm/include/llvm/Analysis/MemoryBuiltins.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class AAResults;
3232
class Argument;
3333
class ConstantPointerNull;
3434
class DataLayout;
35-
class DominatorTree;
3635
class ExtractElementInst;
3736
class ExtractValueInst;
3837
class GEPOperator;
@@ -161,10 +160,8 @@ struct ObjectSizeOpts {
161160
/// though they can't be evaluated. Otherwise, null is always considered to
162161
/// point to a 0 byte region of memory.
163162
bool NullIsUnknownSize = false;
164-
/// If set, used for more accurate evaluation.
163+
/// If set, used for more accurate evaluation
165164
AAResults *AA = nullptr;
166-
/// If set, used for more accurate evaluation.
167-
DominatorTree *DT = nullptr;
168165
};
169166

170167
/// Compute the size of the object pointed by Ptr. Returns true and the
@@ -189,12 +186,6 @@ Value *lowerObjectSizeCall(
189186
const TargetLibraryInfo *TLI, AAResults *AA, bool MustSucceed,
190187
SmallVectorImpl<Instruction *> *InsertedInstructions = nullptr);
191188

192-
Value *lowerObjectSizeCall(
193-
IntrinsicInst *ObjectSize, const DataLayout &DL,
194-
const TargetLibraryInfo *TLI, AAResults *AA, DominatorTree *DT,
195-
bool MustSucceed,
196-
SmallVectorImpl<Instruction *> *InsertedInstructions = nullptr);
197-
198189
/// SizeOffsetType - A base template class for the object size visitors. Used
199190
/// here as a self-documenting way to handle the values rather than using a
200191
/// \p std::pair.

llvm/include/llvm/IR/Value.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -723,16 +723,12 @@ class Value {
723723
bool AllowInvariantGroup = false,
724724
function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis =
725725
nullptr) const;
726-
727-
Value *stripAndAccumulateConstantOffsets(
728-
const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
729-
bool AllowInvariantGroup = false,
730-
function_ref<bool(Value &Value, APInt &Offset)> ExternalAnalysis =
731-
nullptr) {
726+
Value *stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset,
727+
bool AllowNonInbounds,
728+
bool AllowInvariantGroup = false) {
732729
return const_cast<Value *>(
733730
static_cast<const Value *>(this)->stripAndAccumulateConstantOffsets(
734-
DL, Offset, AllowNonInbounds, AllowInvariantGroup,
735-
ExternalAnalysis));
731+
DL, Offset, AllowNonInbounds, AllowInvariantGroup));
736732
}
737733

738734
/// This is a wrapper around stripAndAccumulateConstantOffsets with the

llvm/lib/Analysis/MemoryBuiltins.cpp

Lines changed: 4 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "llvm/IR/Constants.h"
2626
#include "llvm/IR/DataLayout.h"
2727
#include "llvm/IR/DerivedTypes.h"
28-
#include "llvm/IR/Dominators.h"
2928
#include "llvm/IR/Function.h"
3029
#include "llvm/IR/GlobalAlias.h"
3130
#include "llvm/IR/GlobalVariable.h"
@@ -590,28 +589,19 @@ Value *llvm::lowerObjectSizeCall(IntrinsicInst *ObjectSize,
590589
const TargetLibraryInfo *TLI,
591590
bool MustSucceed) {
592591
return lowerObjectSizeCall(ObjectSize, DL, TLI, /*AAResults=*/nullptr,
593-
/*DT=*/nullptr, MustSucceed);
592+
MustSucceed);
594593
}
595594

596595
Value *llvm::lowerObjectSizeCall(
597596
IntrinsicInst *ObjectSize, const DataLayout &DL,
598597
const TargetLibraryInfo *TLI, AAResults *AA, bool MustSucceed,
599598
SmallVectorImpl<Instruction *> *InsertedInstructions) {
600-
return lowerObjectSizeCall(ObjectSize, DL, TLI, AA, /*DT=*/nullptr,
601-
MustSucceed, InsertedInstructions);
602-
}
603-
604-
Value *llvm::lowerObjectSizeCall(
605-
IntrinsicInst *ObjectSize, const DataLayout &DL,
606-
const TargetLibraryInfo *TLI, AAResults *AA, DominatorTree *DT,
607-
bool MustSucceed, SmallVectorImpl<Instruction *> *InsertedInstructions) {
608599
assert(ObjectSize->getIntrinsicID() == Intrinsic::objectsize &&
609600
"ObjectSize must be a call to llvm.objectsize!");
610601

611602
bool MaxVal = cast<ConstantInt>(ObjectSize->getArgOperand(1))->isZero();
612603
ObjectSizeOpts EvalOptions;
613604
EvalOptions.AA = AA;
614-
EvalOptions.DT = DT;
615605

616606
// Unless we have to fold this to something, try to be as accurate as
617607
// possible.
@@ -718,46 +708,14 @@ OffsetSpan ObjectSizeOffsetVisitor::computeImpl(Value *V) {
718708
// readjust the APInt as we pass it upwards in order for the APInt to match
719709
// the type the caller passed in.
720710
APInt Offset(InitialIntTyBits, 0);
721-
722-
// External Analysis used to compute the Min/Max value of individual Offsets
723-
// within a GEP.
724-
auto OffsetRangeAnalysis = [this, V](Value &VOffset, APInt &Offset) {
725-
if (auto *C = dyn_cast<ConstantInt>(&VOffset)) {
726-
Offset = C->getValue();
727-
return true;
728-
}
729-
if (Options.EvalMode != ObjectSizeOpts::Mode::Min &&
730-
Options.EvalMode != ObjectSizeOpts::Mode::Max) {
731-
return false;
732-
}
733-
ConstantRange CR = computeConstantRange(
734-
&VOffset, /*ForSigned*/ true, /*UseInstrInfo*/ true, /*AC=*/nullptr,
735-
/*CtxtI=*/dyn_cast<Instruction>(V), /*DT=*/Options.DT);
736-
if (CR.isFullSet())
737-
return false;
738-
739-
if (Options.EvalMode == ObjectSizeOpts::Mode::Min) {
740-
Offset = CR.getSignedMax();
741-
// Upper bound actually unknown.
742-
if (Offset.isMaxSignedValue())
743-
return false;
744-
} else {
745-
Offset = CR.getSignedMin();
746-
// Lower bound actually unknown.
747-
if (Offset.isMinSignedValue())
748-
return false;
749-
}
750-
return true;
751-
};
752-
753711
V = V->stripAndAccumulateConstantOffsets(
754-
DL, Offset, /* AllowNonInbounds */ true, /* AllowInvariantGroup */ true,
755-
/*ExternalAnalysis=*/OffsetRangeAnalysis);
712+
DL, Offset, /* AllowNonInbounds */ true, /* AllowInvariantGroup */ true);
756713

757714
// Later we use the index type size and zero but it will match the type of the
758715
// value that is passed to computeImpl.
759716
IntTyBits = DL.getIndexTypeSizeInBits(V->getType());
760717
Zero = APInt::getZero(IntTyBits);
718+
761719
OffsetSpan ORT = computeValue(V);
762720

763721
bool IndexTypeSizeChanged = InitialIntTyBits != IntTyBits;
@@ -835,26 +793,6 @@ OffsetSpan ObjectSizeOffsetVisitor::visitAllocaInst(AllocaInst &I) {
835793
Size = Size.umul_ov(NumElems, Overflow);
836794
return Overflow ? ObjectSizeOffsetVisitor::unknown()
837795
: OffsetSpan(Zero, align(Size, I.getAlign()));
838-
} else {
839-
ConstantRange CR =
840-
computeConstantRange(ArraySize, /*ForSigned*/ false,
841-
/*UseInstrInfo*/ true, /*AC=*/nullptr,
842-
/*CtxtI=*/&I, /*DT=*/Options.DT);
843-
if (CR.isFullSet())
844-
return ObjectSizeOffsetVisitor::unknown();
845-
APInt Bound;
846-
if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
847-
Bound = CR.getUnsignedMax();
848-
// Upper bound actually unknown.
849-
if (Bound.isMaxValue())
850-
return ObjectSizeOffsetVisitor::unknown();
851-
} else {
852-
Bound = CR.getUnsignedMin();
853-
// Lower bound actually unknown.
854-
if (Bound.isMinValue())
855-
return ObjectSizeOffsetVisitor::unknown();
856-
}
857-
return OffsetSpan(Zero, align(Bound, I.getAlign()));
858796
}
859797
return ObjectSizeOffsetVisitor::unknown();
860798
}
@@ -872,32 +810,7 @@ OffsetSpan ObjectSizeOffsetVisitor::visitArgument(Argument &A) {
872810
}
873811

874812
OffsetSpan ObjectSizeOffsetVisitor::visitCallBase(CallBase &CB) {
875-
if (std::optional<APInt> Size =
876-
getAllocSize(&CB, TLI, [&CB, this](const Value *V) -> const Value * {
877-
if (!V->getType()->isIntegerTy())
878-
return V;
879-
if (isa<ConstantInt>(V))
880-
return V;
881-
ConstantRange CR = computeConstantRange(
882-
V, /*ForSigned*/ false, /*UseInstrInfo*/ true, /*AC=*/nullptr,
883-
/*CtxtI=*/&CB, /*DT=*/Options.DT);
884-
if (CR.isFullSet())
885-
return V;
886-
887-
APInt Bound;
888-
if (Options.EvalMode == ObjectSizeOpts::Mode::Max) {
889-
Bound = CR.getUnsignedMax();
890-
// Upper bound actually unknown.
891-
if (Bound.isMaxValue())
892-
return V;
893-
} else {
894-
Bound = CR.getUnsignedMin();
895-
// Lower bound actually unknown.
896-
if (Bound.isMinValue())
897-
return V;
898-
}
899-
return ConstantInt::get(V->getType(), Bound);
900-
}))
813+
if (std::optional<APInt> Size = getAllocSize(&CB, TLI))
901814
return OffsetSpan(Zero, *Size);
902815
return ObjectSizeOffsetVisitor::unknown();
903816
}

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3318,9 +3318,8 @@ Instruction *InstCombinerImpl::visitAllocSite(Instruction &MI) {
33183318
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
33193319
if (II->getIntrinsicID() == Intrinsic::objectsize) {
33203320
SmallVector<Instruction *> InsertedInstructions;
3321-
Value *Result =
3322-
lowerObjectSizeCall(II, DL, &TLI, AA, &DT,
3323-
/*MustSucceed=*/true, &InsertedInstructions);
3321+
Value *Result = lowerObjectSizeCall(
3322+
II, DL, &TLI, AA, /*MustSucceed=*/true, &InsertedInstructions);
33243323
for (Instruction *Inserted : InsertedInstructions)
33253324
Worklist.add(Inserted);
33263325
replaceInstUsesWith(*I, Result);

llvm/lib/Transforms/Scalar/LowerConstantIntrinsics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ bool llvm::lowerConstantIntrinsics(Function &F, const TargetLibraryInfo &TLI,
143143
IsConstantIntrinsicsHandled++;
144144
break;
145145
case Intrinsic::objectsize:
146-
NewValue = lowerObjectSizeCall(II, DL, &TLI, /*AA=*/nullptr, DT, true);
146+
NewValue = lowerObjectSizeCall(II, DL, &TLI, true);
147147
LLVM_DEBUG(dbgs() << "Folding " << *II << " to " << *NewValue << "\n");
148148
ObjectSizeIntrinsicsHandled++;
149149
break;

llvm/test/Transforms/LowerConstantIntrinsics/builtin-object-size-range.ll

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)