Skip to content

Commit ba98d3c

Browse files
committed
[EarlyCSE] De-Duplicate callsites with differing attrs
We only do this if the attributes of the two callsites are compatible (intersectable) which is probably not in fact necessary.
1 parent 073552c commit ba98d3c

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

llvm/include/llvm/IR/Instruction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,8 @@ class Instruction : public User,
876876
/// Return true if the specified instruction is exactly identical to the
877877
/// current one. This means that all operands match and any extra information
878878
/// (e.g. load is volatile) agree.
879-
bool isIdenticalTo(const Instruction *I) const LLVM_READONLY;
879+
bool isIdenticalTo(const Instruction *I,
880+
bool IntersectAttrs = false) const LLVM_READONLY;
880881

881882
/// This is like isIdenticalTo, except that it ignores the
882883
/// SubclassOptionalData flags, which may specify conditions under which the

llvm/lib/IR/Instruction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,8 +862,9 @@ bool Instruction::hasSameSpecialState(const Instruction *I2,
862862
return true;
863863
}
864864

865-
bool Instruction::isIdenticalTo(const Instruction *I) const {
866-
return isIdenticalToWhenDefined(I) &&
865+
bool Instruction::isIdenticalTo(const Instruction *I,
866+
bool IntersectAttrs) const {
867+
return isIdenticalToWhenDefined(I, IntersectAttrs) &&
867868
SubclassOptionalData == I->SubclassOptionalData;
868869
}
869870

llvm/lib/Transforms/Scalar/EarlyCSE.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ static bool isEqualImpl(SimpleValue LHS, SimpleValue RHS) {
362362

363363
if (LHSI->getOpcode() != RHSI->getOpcode())
364364
return false;
365-
if (LHSI->isIdenticalToWhenDefined(RHSI)) {
365+
if (LHSI->isIdenticalToWhenDefined(RHSI, /*IntersectAttrs=*/true)) {
366366
// Convergent calls implicitly depend on the set of threads that is
367367
// currently executing, so conservatively return false if they are in
368368
// different basic blocks.
@@ -551,7 +551,7 @@ bool DenseMapInfo<CallValue>::isEqual(CallValue LHS, CallValue RHS) {
551551
if (LHSI->isConvergent() && LHSI->getParent() != RHSI->getParent())
552552
return false;
553553

554-
return LHSI->isIdenticalTo(RHSI);
554+
return LHSI->isIdenticalTo(RHSI, /*IntersectAttrs=*/true);
555555
}
556556

557557
//===----------------------------------------------------------------------===//
@@ -621,7 +621,7 @@ bool DenseMapInfo<GEPValue>::isEqual(const GEPValue &LHS, const GEPValue &RHS) {
621621
return false;
622622
if (LHS.ConstantOffset.has_value() && RHS.ConstantOffset.has_value())
623623
return LHS.ConstantOffset.value() == RHS.ConstantOffset.value();
624-
return LGEP->isIdenticalToWhenDefined(RGEP);
624+
return LGEP->isIdenticalToWhenDefined(RGEP, /*IntersectAttrs=*/true);
625625
}
626626

627627
//===----------------------------------------------------------------------===//
@@ -1632,6 +1632,9 @@ bool EarlyCSE::processNode(DomTreeNode *Node) {
16321632
LLVM_DEBUG(dbgs() << "Skipping due to debug counter\n");
16331633
continue;
16341634
}
1635+
1636+
// Potential TODO: We may be throwing away attribute information when
1637+
// we delete Inst that we could propagate too InVal.first.
16351638
if (!Inst.use_empty())
16361639
Inst.replaceAllUsesWith(InVal.first);
16371640
salvageKnowledge(&Inst, &AC);

llvm/test/Transforms/EarlyCSE/replace-calls-def-attrs.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ define i8 @same_parent_combine_diff_attrs(i8 %x, i8 %y) {
77
; CHECK-LABEL: define i8 @same_parent_combine_diff_attrs(
88
; CHECK-SAME: i8 [[X:%.*]], i8 [[Y:%.*]]) {
99
; CHECK-NEXT: [[C1:%.*]] = call i8 @baz(i8 [[X]], i8 noundef [[Y]])
10-
; CHECK-NEXT: [[C0:%.*]] = call i8 @baz(i8 noundef [[X]], i8 noundef [[Y]])
11-
; CHECK-NEXT: [[R:%.*]] = call i8 @buz(i8 [[C0]], i8 [[C1]])
10+
; CHECK-NEXT: [[R:%.*]] = call i8 @buz(i8 [[C1]], i8 [[C1]])
1211
; CHECK-NEXT: ret i8 [[R]]
1312
;
1413
%c1 = call i8 @baz(i8 %x, i8 noundef %y)
@@ -24,8 +23,7 @@ define i8 @diff_parent_combine_diff_attrs(i1 %c, i8 %x, i8 %y) {
2423
; CHECK-NEXT: [[C1:%.*]] = call i8 @baz(i8 [[X]], i8 noundef [[Y]])
2524
; CHECK-NEXT: br i1 [[C]], label %[[T:.*]], label %[[F:.*]]
2625
; CHECK: [[T]]:
27-
; CHECK-NEXT: [[C0:%.*]] = call i8 @baz(i8 noundef [[X]], i8 noundef [[Y]])
28-
; CHECK-NEXT: [[R:%.*]] = call i8 @buz(i8 [[C0]], i8 [[C1]])
26+
; CHECK-NEXT: [[R:%.*]] = call i8 @buz(i8 [[C1]], i8 [[C1]])
2927
; CHECK-NEXT: ret i8 [[R]]
3028
; CHECK: [[F]]:
3129
; CHECK-NEXT: [[R2:%.*]] = add i8 [[C1]], 4

0 commit comments

Comments
 (0)