Skip to content

Commit

Permalink
LLVM semantics: alignment can be a non-power-of-2 if ptr == null
Browse files Browse the repository at this point in the history
  • Loading branch information
nunoplopes committed Nov 29, 2023
1 parent cdf792b commit ff9d8e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
9 changes: 3 additions & 6 deletions ir/pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,10 @@ expr Pointer::isBlockAligned(uint64_t align, bool exact) const {
}

expr Pointer::isAligned(uint64_t align) {
if (align == 0)
if (align == 0 || !is_power2(align))
return isNull();
if (align == 1)
return true;
if (!is_power2(align))
return false;

auto offset = getOffset();
if (isUndef(offset))
Expand Down Expand Up @@ -395,10 +393,9 @@ expr Pointer::isAligned(const expr &align) {
return isAligned(n);

return
expr::mkIf(align == 0,
expr::mkIf(align == 0 || !align.isPowerOf2(),
isNull(),
align.isPowerOf2() &&
getAddress().urem(align.zextOrTrunc(bits_ptr_address)) == 0);
getAddress().urem(align.zextOrTrunc(bits_ptr_address)) == 0);
}

static pair<expr, expr> is_dereferenceable(Pointer &p,
Expand Down
4 changes: 2 additions & 2 deletions smt/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ expr expr::udiv(const expr &rhs) const {
}

expr expr::srem(const expr &rhs) const {
if (eq(rhs) || (isSMin() && rhs.isAllOnes()))
if (eq(rhs) || isZero() || (isSMin() && rhs.isAllOnes()))
return mkUInt(0, sort());

if (rhs.isZero())
Expand All @@ -757,7 +757,7 @@ expr expr::srem(const expr &rhs) const {

expr expr::urem(const expr &rhs) const {
C();
if (eq(rhs))
if (eq(rhs) || isZero())
return mkUInt(0, sort());

uint64_t n, log;
Expand Down
13 changes: 13 additions & 0 deletions tests/alive-tv/assume/align-nonpow2.srctgt.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; ERROR: Source is more defined than target

define void @src(i32 %align) {
call void @llvm.assume(i1 true) ["align"(ptr null, i32 5)]
call void @llvm.assume(i1 true) ["align"(ptr null, i32 %align)]
ret void
}

define void @tgt(i32 %align) {
unreachable
}

declare void @llvm.assume(i1)

0 comments on commit ff9d8e8

Please sign in to comment.