Skip to content

Commit 0650402

Browse files
Improve threading of comparisons over select instructions (spotted by my
auto-simplifier). This has a big impact on Ada code, but not much else. Unfortunately the impact is mostly negative! This is due to PR9004 (aka SCCP failing to resolve conditional branch conditions in the destination blocks of the branch), in which simple correlated expressions are not resolved but complicated ones are, so simplifying has a bad effect! llvm-svn: 124788
1 parent 22ec660 commit 0650402

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,17 +395,39 @@ static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
395395
assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
396396
SelectInst *SI = cast<SelectInst>(LHS);
397397

398-
// Now that we have "cmp select(cond, TV, FV), RHS", analyse it.
398+
// Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
399399
// Does "cmp TV, RHS" simplify?
400400
if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD, DT,
401-
MaxRecurse))
401+
MaxRecurse)) {
402402
// It does! Does "cmp FV, RHS" simplify?
403403
if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD, DT,
404-
MaxRecurse))
404+
MaxRecurse)) {
405405
// It does! If they simplified to the same value, then use it as the
406406
// result of the original comparison.
407407
if (TCmp == FCmp)
408408
return TCmp;
409+
Value *Cond = SI->getCondition();
410+
// If the false value simplified to false, then the result of the compare
411+
// is equal to "Cond && TCmp". This also catches the case when the false
412+
// value simplified to false and the true value to true, returning "Cond".
413+
if (match(FCmp, m_Zero()))
414+
if (Value *V = SimplifyAndInst(Cond, TCmp, TD, DT, MaxRecurse))
415+
return V;
416+
// If the true value simplified to true, then the result of the compare
417+
// is equal to "Cond || FCmp".
418+
if (match(TCmp, m_One()))
419+
if (Value *V = SimplifyOrInst(Cond, FCmp, TD, DT, MaxRecurse))
420+
return V;
421+
// Finally, if the false value simplified to true and the true value to
422+
// false, then the result of the compare is equal to "!Cond".
423+
if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
424+
if (Value *V =
425+
SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()),
426+
TD, DT, MaxRecurse))
427+
return V;
428+
}
429+
}
430+
409431
return 0;
410432
}
411433

llvm/test/Transforms/InstSimplify/2011-01-18-Compare.ll

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,38 @@ define i1 @ashr(i32 %x) {
132132
ret i1 %c
133133
; CHECK: ret i1 false
134134
}
135+
136+
define i1 @select1(i1 %cond) {
137+
; CHECK: @select1
138+
%s = select i1 %cond, i32 1, i32 0
139+
%c = icmp eq i32 %s, 1
140+
ret i1 %c
141+
; CHECK: ret i1 %cond
142+
}
143+
144+
define i1 @select2(i1 %cond) {
145+
; CHECK: @select2
146+
%x = zext i1 %cond to i32
147+
%s = select i1 %cond, i32 %x, i32 0
148+
%c = icmp ne i32 %s, 0
149+
ret i1 %c
150+
; CHECK: ret i1 %cond
151+
}
152+
153+
define i1 @select3(i1 %cond) {
154+
; CHECK: @select3
155+
%x = zext i1 %cond to i32
156+
%s = select i1 %cond, i32 1, i32 %x
157+
%c = icmp ne i32 %s, 0
158+
ret i1 %c
159+
; CHECK: ret i1 %cond
160+
}
161+
162+
define i1 @select4(i1 %cond) {
163+
; CHECK: @select4
164+
%invert = xor i1 %cond, 1
165+
%s = select i1 %invert, i32 0, i32 1
166+
%c = icmp ne i32 %s, 0
167+
ret i1 %c
168+
; CHECK: ret i1 %cond
169+
}

0 commit comments

Comments
 (0)