Skip to content

Commit

Permalink
preprocessor: improve alignment inference
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoplopes committed Oct 8, 2023
1 parent 6ede728 commit 448b727
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
17 changes: 0 additions & 17 deletions ir/instr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3619,23 +3619,6 @@ uint64_t GEP::getMaxGEPOffset() const {
return off;
}

optional<uint64_t> GEP::getExactOffset() const {
uint64_t off = 0;
for (auto &[mul, v] : getIdxs()) {
if (mul == 0)
continue;
if (mul >= INT64_MAX)
return {};

if (auto n = getInt(*v)) {
off += (int64_t)mul * *n;
continue;
}
return {};
}
return off;
}

vector<Value*> GEP::operands() const {
vector<Value*> v = { ptr };
for (auto &[sz, idx] : idxs) {
Expand Down
1 change: 0 additions & 1 deletion ir/instr.h
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,6 @@ class GEP final : public MemInstr {
Value& getPtr() const { return *ptr; }
auto& getIdxs() const { return idxs; }
bool isInBounds() const { return inbounds; }
std::optional<uint64_t> getExactOffset() const;

std::pair<uint64_t, uint64_t> getMaxAllocSize() const override;
uint64_t getMaxAccessSize() const override;
Expand Down
19 changes: 15 additions & 4 deletions tools/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,10 +1689,21 @@ void Transform::preprocess() {
continue;
}
} else if (auto *gep = dynamic_cast<const GEP*>(i)) {
auto off = gep->getExactOffset();
if (!off || !is_power2(*off))
continue;
align = min(aligns[&gep->getPtr()], *off);
int mul_bits = sizeof(uint64_t) * 8 - 1;
uint64_t const_offset = 0;
for (auto &[mul, v] : gep->getIdxs()) {
if (mul == 0)
continue;
if (auto n = getInt(*v))
const_offset += mul * (uint64_t)*n;
else
mul_bits = min(mul_bits, countr_zero(mul));
}
align = min(aligns[&gep->getPtr()], ((uint64_t)1) << mul_bits);
if (const_offset) {
align = min(align, ((uint64_t)1) << countr_zero(const_offset));
}

} else if (auto *phi = dynamic_cast<const Phi*>(i)) {
// optimistic handling of phis: unreachable predecessors don't
// contribute to the result. This is revisited once they become reach
Expand Down

0 comments on commit 448b727

Please sign in to comment.