Skip to content

Commit 0b58bf4

Browse files
committed
Change the logic to drop all attributes that we do not explicitly
handle. That should be more future proof as we automatically would drop any new attributes being added.
1 parent 8b8ce48 commit 0b58bf4

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "llvm/Analysis/TargetLibraryInfo.h"
5151
#include "llvm/Analysis/ValueTracking.h"
5252
#include "llvm/IR/Argument.h"
53+
#include "llvm/IR/AttributeMask.h"
5354
#include "llvm/IR/BasicBlock.h"
5455
#include "llvm/IR/Constant.h"
5556
#include "llvm/IR/ConstantRangeList.h"
@@ -563,6 +564,43 @@ static void shortenAssignment(Instruction *Inst, Value *OriginalDest,
563564
for_each(LinkedDVRAssigns, InsertAssignForOverlap);
564565
}
565566

567+
/// Update the attributes given that a memory access is updated (the
568+
/// dereferenced pointer could be moved forward when shortening a
569+
/// mem intrinsic).
570+
static void adjustArgAttributes(AnyMemIntrinsic *Intrinsic, unsigned ArgNo,
571+
uint64_t PtrOffset) {
572+
// Remember old attributes.
573+
AttributeSet OldAttrs = Intrinsic->getParamAttributes(ArgNo);
574+
575+
// Find attributes that should be kept, and remove the rest.
576+
AttributeMask AttrsToRemove;
577+
for (auto &Attr : OldAttrs) {
578+
if (Attr.hasKindAsEnum()) {
579+
switch (Attr.getKindAsEnum()) {
580+
default:
581+
break;
582+
case Attribute::Alignment:
583+
// Only keep alignment if PtrOffset satisfy the alignment.
584+
if (isAligned(Attr.getAlignment().valueOrOne(), PtrOffset))
585+
continue;
586+
break;
587+
case Attribute::Dereferenceable:
588+
case Attribute::DereferenceableOrNull:
589+
// We could reduce the size of these attributes according to
590+
// PtrOffset. But we simply drop these for now.
591+
break;
592+
case Attribute::NonNull:
593+
case Attribute::NoUndef:
594+
continue;
595+
}
596+
}
597+
AttrsToRemove.addAttribute(Attr);
598+
}
599+
600+
// Remove the attributes that should be dropped.
601+
Intrinsic->removeParamAttrs(ArgNo, AttrsToRemove);
602+
}
603+
566604
static bool tryToShorten(Instruction *DeadI, int64_t &DeadStart,
567605
uint64_t &DeadSize, int64_t KillingStart,
568606
uint64_t KillingSize, bool IsOverwriteEnd) {
@@ -644,11 +682,7 @@ static bool tryToShorten(Instruction *DeadI, int64_t &DeadStart,
644682
DeadI->getIterator());
645683
NewDestGEP->setDebugLoc(DeadIntrinsic->getDebugLoc());
646684
DeadIntrinsic->setDest(NewDestGEP);
647-
// Simply drop any dereferenceable attributes (memset with a constant length
648-
// already implies dereferenceability by itself, so dropping is simpler than
649-
// trying to adjust the dereferenceable size).
650-
DeadIntrinsic->removeParamAttr(0, Attribute::Dereferenceable);
651-
DeadIntrinsic->removeParamAttr(0, Attribute::DereferenceableOrNull);
685+
adjustArgAttributes(DeadIntrinsic, 0, ToRemoveSize);
652686
}
653687

654688
// Update attached dbg.assign intrinsics. Assume 8-bit byte.

0 commit comments

Comments
 (0)