Skip to content

Commit e6e17b9

Browse files
committed
Implement args refinement
Fixes #771
1 parent 8d9abb4 commit e6e17b9

13 files changed

+59
-41
lines changed

ir/attrs.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ ostream& operator<<(ostream &os, const FnAttrs &attr) {
7373
return os;
7474
}
7575

76+
bool ParamAttrs::refinedBy(const ParamAttrs &other) const {
77+
// check attributes that are properties of the caller
78+
unsigned attrs =
79+
NonNull |
80+
Dereferenceable |
81+
NoUndef |
82+
Align |
83+
NoAlias |
84+
DereferenceableOrNull
85+
;
86+
87+
auto other_params = (other.bits & attrs);
88+
if ((bits & other_params) != other_params)
89+
return false;
90+
91+
return derefBytes == other.derefBytes &&
92+
derefOrNullBytes == other.derefOrNullBytes &&
93+
blockSize == other.blockSize &&
94+
align == other.align;
95+
}
96+
7697
bool ParamAttrs::poisonImpliesUB() const {
7798
return has(Dereferenceable) || has(NoUndef) || has(ByVal) ||
7899
has(DereferenceableOrNull);

ir/attrs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ParamAttrs final {
3030

3131
bool has(Attribute a) const { return (bits & a) != 0; }
3232
void set(Attribute a) { bits |= (unsigned)a; }
33+
bool refinedBy(const ParamAttrs &other) const;
3334

3435
// Returns true if it's UB for the argument to be poison / have a poison elem.
3536
bool poisonImpliesUB() const;

ir/function.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,33 +212,6 @@ void Function::addInput(unique_ptr<Value> &&i) {
212212
inputs.emplace_back(move(i));
213213
}
214214

215-
bool Function::hasSameInputs(const Function &rhs) const {
216-
auto litr = inputs.begin(), lend = inputs.end();
217-
auto ritr = rhs.inputs.begin(), rend = rhs.inputs.end();
218-
219-
auto skip_constinputs = [&]() {
220-
while (litr != lend && dynamic_cast<ConstantInput *>((*litr).get()))
221-
litr++;
222-
while (ritr != rend && dynamic_cast<ConstantInput *>((*ritr).get()))
223-
ritr++;
224-
};
225-
226-
skip_constinputs();
227-
228-
while (litr != lend && ritr != rend) {
229-
auto *lv = dynamic_cast<Input *>((*litr).get());
230-
auto *rv = dynamic_cast<Input *>((*ritr).get());
231-
if (lv->getType().toString() != rv->getType().toString())
232-
return false;
233-
234-
++litr;
235-
++ritr;
236-
skip_constinputs();
237-
}
238-
239-
return litr == lend && ritr == rend;
240-
}
241-
242215
bool Function::hasReturn() const {
243216
for (auto &i : instrs()) {
244217
if (dynamic_cast<const Return *>(&i))

ir/function.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ class Function final {
133133
util::const_strip_unique_ptr<decltype(inputs)> getInputs() const {
134134
return inputs;
135135
}
136-
bool hasSameInputs(const Function &rhs) const;
137136
Value *getReturnedInput() const { return returned_input; }
138137
void setReturnedInput(Value *v) { returned_input = v; }
139138

tests/alive-tv/attrs/align-intro-withnoundef.srctgt.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
; Introduction of align AND noundef may introduce UB
2-
; ERROR: Source is more defined than target
1+
; ERROR: Parameter attributes not refined
32

43
define i8* @src(i8* %p) {
54
ret i8* %p

tests/alive-tv/attrs/align-intro.srctgt.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
; Introduction of align isn't immediate UB
2-
; ERROR: Target is more poisonous than source
1+
; ERROR: Parameter attributes not refined
32

43
define i8* @src(i8* %p) {
54
ret i8* %p

tests/alive-tv/attrs/dereferenceable_or_null.srctgt.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ define i32* @tgt(i32* dereferenceable(4) %p) {
66
ret i32* %p
77
}
88

9-
; ERROR: Source is more defined than target
9+
; ERROR: Parameter attributes not refined

tests/alive-tv/attrs/dereferenceable_or_null2.srctgt.ll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ define i32* @src(i32* dereferenceable(4) %p) {
55
define i32* @tgt(i32* dereferenceable_or_null(4) %p) {
66
ret i32* %p
77
}
8+
9+
; this one is technically ok, but it's not worth to support it
10+
; ERROR: Parameter attributes not refined

tests/alive-tv/attrs/nonnull-intro-withnoundef.srctgt.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
; Introduction of nonnull AND noundef may introduce UB
2-
; ERROR: Source is more defined than target
1+
; ERROR: Parameter attributes not refined
32

43
define i8* @src(i8* %p) {
54
ret i8* %p

tests/alive-tv/attrs/nonnull-intro.srctgt.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
; Introduction of nonnull isn't immediate UB
2-
; ERROR: Target is more poisonous than source
1+
; ERROR: Parameter attributes not refined
32

43
define i8* @src(i8* %p) {
54
ret i8* %p

0 commit comments

Comments
 (0)