Skip to content

Commit a8c318b

Browse files
committed
[BasicAA] Use index size instead of pointer size
When accumulating the GEP offset in BasicAA, we should use the pointer index size rather than the pointer size. Differential Revision: https://reviews.llvm.org/D112370
1 parent aee86f9 commit a8c318b

File tree

5 files changed

+49
-31
lines changed

5 files changed

+49
-31
lines changed

llvm/include/llvm/IR/DataLayout.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,8 @@ class DataLayout {
377377
/// the backends/clients are updated.
378378
unsigned getPointerSize(unsigned AS = 0) const;
379379

380-
/// Returns the maximum pointer size over all address spaces.
381-
unsigned getMaxPointerSize() const;
380+
/// Returns the maximum index size over all address spaces.
381+
unsigned getMaxIndexSize() const;
382382

383383
// Index size used for address calculation.
384384
unsigned getIndexSize(unsigned AS) const;
@@ -410,9 +410,9 @@ class DataLayout {
410410
return getPointerSize(AS) * 8;
411411
}
412412

413-
/// Returns the maximum pointer size over all address spaces.
414-
unsigned getMaxPointerSizeInBits() const {
415-
return getMaxPointerSize() * 8;
413+
/// Returns the maximum index size over all address spaces.
414+
unsigned getMaxIndexSizeInBits() const {
415+
return getMaxIndexSize() * 8;
416416
}
417417

418418
/// Size in bits of index used for address calculation in getelementptr.

llvm/lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,14 @@ static LinearExpression GetLinearExpression(
465465
return Val;
466466
}
467467

468-
/// To ensure a pointer offset fits in an integer of size PointerSize
469-
/// (in bits) when that size is smaller than the maximum pointer size. This is
468+
/// To ensure a pointer offset fits in an integer of size IndexSize
469+
/// (in bits) when that size is smaller than the maximum index size. This is
470470
/// an issue, for example, in particular for 32b pointers with negative indices
471471
/// that rely on two's complement wrap-arounds for precise alias information
472-
/// where the maximum pointer size is 64b.
473-
static APInt adjustToPointerSize(const APInt &Offset, unsigned PointerSize) {
474-
assert(PointerSize <= Offset.getBitWidth() && "Invalid PointerSize!");
475-
unsigned ShiftBits = Offset.getBitWidth() - PointerSize;
472+
/// where the maximum index size is 64b.
473+
static APInt adjustToIndexSize(const APInt &Offset, unsigned IndexSize) {
474+
assert(IndexSize <= Offset.getBitWidth() && "Invalid IndexSize!");
475+
unsigned ShiftBits = Offset.getBitWidth() - IndexSize;
476476
return (Offset << ShiftBits).ashr(ShiftBits);
477477
}
478478

@@ -549,9 +549,9 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
549549
SearchTimes++;
550550
const Instruction *CxtI = dyn_cast<Instruction>(V);
551551

552-
unsigned MaxPointerSize = DL.getMaxPointerSizeInBits();
552+
unsigned MaxIndexSize = DL.getMaxIndexSizeInBits();
553553
DecomposedGEP Decomposed;
554-
Decomposed.Offset = APInt(MaxPointerSize, 0);
554+
Decomposed.Offset = APInt(MaxIndexSize, 0);
555555
do {
556556
// See if this is a bitcast or GEP.
557557
const Operator *Op = dyn_cast<Operator>(V);
@@ -620,7 +620,7 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
620620
unsigned AS = GEPOp->getPointerAddressSpace();
621621
// Walk the indices of the GEP, accumulating them into BaseOff/VarIndices.
622622
gep_type_iterator GTI = gep_type_begin(GEPOp);
623-
unsigned PointerSize = DL.getPointerSizeInBits(AS);
623+
unsigned IndexSize = DL.getIndexSizeInBits(AS);
624624
// Assume all GEP operands are constants until proven otherwise.
625625
bool GepHasConstantOffset = true;
626626
for (User::const_op_iterator I = GEPOp->op_begin() + 1, E = GEPOp->op_end();
@@ -643,26 +643,26 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
643643
continue;
644644
Decomposed.Offset +=
645645
DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize() *
646-
CIdx->getValue().sextOrTrunc(MaxPointerSize);
646+
CIdx->getValue().sextOrTrunc(MaxIndexSize);
647647
continue;
648648
}
649649

650650
GepHasConstantOffset = false;
651651

652-
// If the integer type is smaller than the pointer size, it is implicitly
653-
// sign extended to pointer size.
652+
// If the integer type is smaller than the index size, it is implicitly
653+
// sign extended or truncated to index size.
654654
unsigned Width = Index->getType()->getIntegerBitWidth();
655-
unsigned SExtBits = PointerSize > Width ? PointerSize - Width : 0;
656-
unsigned TruncBits = PointerSize < Width ? Width - PointerSize : 0;
655+
unsigned SExtBits = IndexSize > Width ? IndexSize - Width : 0;
656+
unsigned TruncBits = IndexSize < Width ? Width - IndexSize : 0;
657657
LinearExpression LE = GetLinearExpression(
658658
CastedValue(Index, 0, SExtBits, TruncBits), DL, 0, AC, DT);
659659

660660
// Scale by the type size.
661661
unsigned TypeSize =
662662
DL.getTypeAllocSize(GTI.getIndexedType()).getFixedSize();
663-
LE = LE.mul(APInt(PointerSize, TypeSize), GEPOp->isInBounds());
664-
Decomposed.Offset += LE.Offset.sextOrSelf(MaxPointerSize);
665-
APInt Scale = LE.Scale.sextOrSelf(MaxPointerSize);
663+
LE = LE.mul(APInt(IndexSize, TypeSize), GEPOp->isInBounds());
664+
Decomposed.Offset += LE.Offset.sextOrSelf(MaxIndexSize);
665+
APInt Scale = LE.Scale.sextOrSelf(MaxIndexSize);
666666

667667
// If we already had an occurrence of this index variable, merge this
668668
// scale into it. For example, we want to handle:
@@ -678,8 +678,8 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
678678
}
679679

680680
// Make sure that we have a scale that makes sense for this target's
681-
// pointer size.
682-
Scale = adjustToPointerSize(Scale, PointerSize);
681+
// index size.
682+
Scale = adjustToIndexSize(Scale, IndexSize);
683683

684684
if (!!Scale) {
685685
VariableGEPIndex Entry = {LE.Val, Scale, CxtI, LE.IsNSW};
@@ -689,7 +689,7 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
689689

690690
// Take care of wrap-arounds
691691
if (GepHasConstantOffset)
692-
Decomposed.Offset = adjustToPointerSize(Decomposed.Offset, PointerSize);
692+
Decomposed.Offset = adjustToIndexSize(Decomposed.Offset, IndexSize);
693693

694694
// Analyze the base pointer next.
695695
V = GEPOp->getOperand(0);
@@ -1258,7 +1258,7 @@ AliasResult BasicAAResult::aliasGEP(
12581258
CR = Index.Val.evaluateWith(CR).sextOrTrunc(OffsetRange.getBitWidth());
12591259

12601260
assert(OffsetRange.getBitWidth() == Scale.getBitWidth() &&
1261-
"Bit widths are normalized to MaxPointerSize");
1261+
"Bit widths are normalized to MaxIndexSize");
12621262
if (Index.IsNSW)
12631263
OffsetRange = OffsetRange.add(CR.smul_sat(ConstantRange(Scale)));
12641264
else

llvm/lib/IR/DataLayout.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,12 +707,12 @@ unsigned DataLayout::getPointerSize(unsigned AS) const {
707707
return getPointerAlignElem(AS).TypeByteWidth;
708708
}
709709

710-
unsigned DataLayout::getMaxPointerSize() const {
711-
unsigned MaxPointerSize = 0;
710+
unsigned DataLayout::getMaxIndexSize() const {
711+
unsigned MaxIndexSize = 0;
712712
for (auto &P : Pointers)
713-
MaxPointerSize = std::max(MaxPointerSize, P.TypeByteWidth);
713+
MaxIndexSize = std::max(MaxIndexSize, P.IndexWidth);
714714

715-
return MaxPointerSize;
715+
return MaxIndexSize;
716716
}
717717

718718
unsigned DataLayout::getPointerTypeSizeInBits(Type *Ty) const {

llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) {
506506
// This is an "architecture", not a "feature", but we emit it as such for
507507
// the benefit of tools like Binaryen and consistency with other producers.
508508
// FIXME: Subtarget is null here, so can't Subtarget->hasAddr64() ?
509-
if (M.getDataLayout().getMaxPointerSize() == 8) {
509+
if (M.getDataLayout().getPointerSize() == 8) {
510510
// Can't use EmitFeature since "wasm-feature-memory64" is not a module
511511
// flag.
512512
EmittedFeatures.push_back({wasm::WASM_FEATURE_PREFIX_USED, "memory64"});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; RUN: opt -basic-aa -aa-eval -print-all-alias-modref-info -disable-output %s 2>&1 | FileCheck %s
2+
3+
target datalayout = "p:64:64:64:32"
4+
5+
; gep.1 and gep.2 must alias, because they are truncated to the index size
6+
; (32-bit), not the pointer size (64-bit).
7+
define void @mustalias_due_to_index_size(i8* %ptr) {
8+
; CHECK-LABEL: Function: mustalias_due_to_index_size
9+
; CHECK-NEXT: MustAlias: i8* %gep.1, i8* %ptr
10+
; CHECK-NEXT: MustAlias: i8* %gep.2, i8* %ptr
11+
; CHECK-NEXT: MustAlias: i8* %gep.1, i8* %gep.2
12+
;
13+
%gep.1 = getelementptr i8, i8* %ptr, i64 4294967296
14+
store i8 0, i8* %gep.1
15+
%gep.2 = getelementptr i8, i8* %ptr, i64 0
16+
store i8 1, i8* %gep.2
17+
ret void
18+
}

0 commit comments

Comments
 (0)