Skip to content

Commit

Permalink
[Transforms] Enhance CorrelatedValuePropagation to handle both values…
Browse files Browse the repository at this point in the history
… of select

The "Correlated Value Propagation" pass was missing a case when handling select instructions. It was only handling the "false" constant value, while in NVPTX the select may have the condition (and thus the branches) inverted, for example:
```
loop:
	%phi = phi i32* [ %sel, %loop ], [ %x, %entry ]
	%f = tail call i32* @f(i32* %phi)
	%cmp1 = icmp ne i32* %f, %y
	%sel = select i1 %cmp1, i32* %f, i32* null
	%cmp2 = icmp eq i32* %sel, null
	br i1 %cmp2, label %return, label %loop
```
But the select condition can be inverted:
```
	%cmp1 = icmp eq i32* %f, %y
	%sel = select i1 %cmp1, i32* null, i32* %f
```
The fix is to enhance "Correlated Value Propagation" to handle both branches of the select instruction.

Reviewed By: nikic, lebedev.ri

Differential Revision: https://reviews.llvm.org/D119643
  • Loading branch information
slydiman committed Feb 22, 2022
1 parent 0b302be commit 90a3b31
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 9 additions & 0 deletions llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
Expand Up @@ -245,11 +245,20 @@ static Value *getValueOnEdge(LazyValueInfo *LVI, Value *Incoming,
// value can never be that constant. In that case replace the incoming
// value with the other value of the select. This often allows us to
// remove the select later.

// The "false" case
if (auto *C = dyn_cast<Constant>(SI->getFalseValue()))
if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI) ==
LazyValueInfo::False)
return SI->getTrueValue();

// The "true" case,
// similar to the select "false" case, but try the select "true" value
if (auto *C = dyn_cast<Constant>(SI->getTrueValue()))
if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI) ==
LazyValueInfo::False)
return SI->getFalseValue();

return nullptr;
}

Expand Down
5 changes: 2 additions & 3 deletions llvm/test/Transforms/CorrelatedValuePropagation/basic.ll
Expand Up @@ -179,9 +179,8 @@ define void @loop2(i32* %x, i32* %y) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
;; CorrelatedValuePropagation should handle inverted select condition, but it does not yet.
;; CHECK-NEXT: [[PHI:%.*]] = phi i32* [ [[F:%.*]], [[LOOP]] ], [ [[X:%.*]], [[ENTRY:%.*]] ]
; CHECK-NEXT: [[F:%.*]] = tail call i32* @f(i32* [[PHI]])
; CHECK-NEXT: [[PHI:%.*]] = phi i32* [ [[F:%.*]], [[LOOP]] ], [ [[X:%.*]], [[ENTRY:%.*]] ]
; CHECK-NEXT: [[F]] = tail call i32* @f(i32* [[PHI]])
; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32* [[F]], [[Y:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], i32* null, i32* [[F]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32* [[SEL]], null
Expand Down

0 comments on commit 90a3b31

Please sign in to comment.