Skip to content
This repository was archived by the owner on Sep 2, 2018. It is now read-only.

Commit f5b2749

Browse files
committed
NFC. Move getAlignment helper function from ValueTracking to Value class.
Reviewed By: reames, hfinkel Differential Revision: http://reviews.llvm.org/D16144 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@261735 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent f9678d6 commit f5b2749

File tree

3 files changed

+50
-42
lines changed

3 files changed

+50
-42
lines changed

include/llvm/IR/Value.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,12 @@ class Value {
501501
return const_cast<Value*>(this)->stripInBoundsOffsets();
502502
}
503503

504+
/// \brief Returns an alignment of the pointer value.
505+
///
506+
/// Returns an alignment which is either specified explicitly, e.g. via
507+
/// align attribute of a function argument, or guaranteed by DataLayout.
508+
unsigned getPointerAlignment(const DataLayout &DL) const;
509+
504510
/// \brief Translate PHI node to its predecessor from the given basic block.
505511
///
506512
/// If this value is a PHI node with CurBB as its parent, return the value in

lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,46 +1569,6 @@ static void computeKnownBitsFromOperator(Operator *I, APInt &KnownZero,
15691569
}
15701570
}
15711571

1572-
static unsigned getAlignment(const Value *V, const DataLayout &DL) {
1573-
unsigned Align = 0;
1574-
if (auto *GO = dyn_cast<GlobalObject>(V)) {
1575-
Align = GO->getAlignment();
1576-
if (Align == 0) {
1577-
if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
1578-
Type *ObjectType = GVar->getValueType();
1579-
if (ObjectType->isSized()) {
1580-
// If the object is defined in the current Module, we'll be giving
1581-
// it the preferred alignment. Otherwise, we have to assume that it
1582-
// may only have the minimum ABI alignment.
1583-
if (GVar->isStrongDefinitionForLinker())
1584-
Align = DL.getPreferredAlignment(GVar);
1585-
else
1586-
Align = DL.getABITypeAlignment(ObjectType);
1587-
}
1588-
}
1589-
}
1590-
} else if (const Argument *A = dyn_cast<Argument>(V)) {
1591-
Align = A->getType()->isPointerTy() ? A->getParamAlignment() : 0;
1592-
1593-
if (!Align && A->hasStructRetAttr()) {
1594-
// An sret parameter has at least the ABI alignment of the return type.
1595-
Type *EltTy = cast<PointerType>(A->getType())->getElementType();
1596-
if (EltTy->isSized())
1597-
Align = DL.getABITypeAlignment(EltTy);
1598-
}
1599-
} else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V))
1600-
Align = AI->getAlignment();
1601-
else if (auto CS = ImmutableCallSite(V))
1602-
Align = CS.getAttributes().getParamAlignment(AttributeSet::ReturnIndex);
1603-
else if (const LoadInst *LI = dyn_cast<LoadInst>(V))
1604-
if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
1605-
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
1606-
Align = CI->getLimitedValue();
1607-
}
1608-
1609-
return Align;
1610-
}
1611-
16121572
/// Determine which bits of V are known to be either zero or one and return
16131573
/// them in the KnownZero/KnownOne bit sets.
16141574
///
@@ -1691,7 +1651,7 @@ void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne,
16911651

16921652
// Aligned pointers have trailing zeros - refine KnownZero set
16931653
if (V->getType()->isPointerTy()) {
1694-
unsigned Align = getAlignment(V, Q.DL);
1654+
unsigned Align = V->getPointerAlignment(Q.DL);
16951655
if (Align)
16961656
KnownZero |= APInt::getLowBitsSet(BitWidth, countTrailingZeros(Align));
16971657
}
@@ -3206,7 +3166,7 @@ static bool isDereferenceableFromAttribute(const Value *V, const DataLayout &DL,
32063166

32073167
static bool isAligned(const Value *Base, APInt Offset, unsigned Align,
32083168
const DataLayout &DL) {
3209-
APInt BaseAlign(Offset.getBitWidth(), getAlignment(Base, DL));
3169+
APInt BaseAlign(Offset.getBitWidth(), Base->getPointerAlignment(DL));
32103170

32113171
if (!BaseAlign) {
32123172
Type *Ty = Base->getType()->getPointerElementType();

lib/IR/Value.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,48 @@ Value *Value::stripInBoundsOffsets() {
519519
return stripPointerCastsAndOffsets<PSK_InBounds>(this);
520520
}
521521

522+
unsigned Value::getPointerAlignment(const DataLayout &DL) const {
523+
assert(getType()->isPointerTy() && "must be pointer");
524+
525+
unsigned Align = 0;
526+
if (auto *GO = dyn_cast<GlobalObject>(this)) {
527+
Align = GO->getAlignment();
528+
if (Align == 0) {
529+
if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
530+
Type *ObjectType = GVar->getValueType();
531+
if (ObjectType->isSized()) {
532+
// If the object is defined in the current Module, we'll be giving
533+
// it the preferred alignment. Otherwise, we have to assume that it
534+
// may only have the minimum ABI alignment.
535+
if (GVar->isStrongDefinitionForLinker())
536+
Align = DL.getPreferredAlignment(GVar);
537+
else
538+
Align = DL.getABITypeAlignment(ObjectType);
539+
}
540+
}
541+
}
542+
} else if (const Argument *A = dyn_cast<Argument>(this)) {
543+
Align = A->getParamAlignment();
544+
545+
if (!Align && A->hasStructRetAttr()) {
546+
// An sret parameter has at least the ABI alignment of the return type.
547+
Type *EltTy = cast<PointerType>(A->getType())->getElementType();
548+
if (EltTy->isSized())
549+
Align = DL.getABITypeAlignment(EltTy);
550+
}
551+
} else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this))
552+
Align = AI->getAlignment();
553+
else if (auto CS = ImmutableCallSite(this))
554+
Align = CS.getAttributes().getParamAlignment(AttributeSet::ReturnIndex);
555+
else if (const LoadInst *LI = dyn_cast<LoadInst>(this))
556+
if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
557+
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
558+
Align = CI->getLimitedValue();
559+
}
560+
561+
return Align;
562+
}
563+
522564
Value *Value::DoPHITranslation(const BasicBlock *CurBB,
523565
const BasicBlock *PredBB) {
524566
PHINode *PN = dyn_cast<PHINode>(this);

0 commit comments

Comments
 (0)