Skip to content

Commit 7037d11

Browse files
committed
[InstCombine] propagate IR flags from binop through select
The tests with constant folding that produces poison could potentially remove the select entirely: https://alive2.llvm.org/ce/z/e-WUqF ...but this patch just removes the FMF-only limitation on propagation.
1 parent 69daa2f commit 7037d11

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,13 +1027,11 @@ static Value *foldOperationIntoSelectOperand(Instruction &I, Value *SO,
10271027
if (!ConstIsRHS)
10281028
std::swap(Op0, Op1);
10291029

1030-
auto *BO = cast<BinaryOperator>(&I);
1031-
Value *RI = Builder.CreateBinOp(BO->getOpcode(), Op0, Op1,
1032-
SO->getName() + ".op");
1033-
auto *FPInst = dyn_cast<Instruction>(RI);
1034-
if (FPInst && isa<FPMathOperator>(FPInst))
1035-
FPInst->copyFastMathFlags(BO);
1036-
return RI;
1030+
Value *NewBO = Builder.CreateBinOp(cast<BinaryOperator>(&I)->getOpcode(), Op0,
1031+
Op1, SO->getName() + ".op");
1032+
if (auto *NewBOI = dyn_cast<Instruction>(NewBO))
1033+
NewBOI->copyIRFlags(&I);
1034+
return NewBO;
10371035
}
10381036

10391037
Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op,

llvm/test/Transforms/InstCombine/select-2.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ define float @t3(float %x, float %y) {
4545

4646
define i8 @ashr_exact_poison_constant_fold(i1 %b, i8 %x) {
4747
; CHECK-LABEL: @ashr_exact_poison_constant_fold(
48-
; CHECK-NEXT: [[X_OP:%.*]] = ashr i8 [[X:%.*]], 3
48+
; CHECK-NEXT: [[X_OP:%.*]] = ashr exact i8 [[X:%.*]], 3
4949
; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], i8 [[X_OP]], i8 5
5050
; CHECK-NEXT: ret i8 [[R]]
5151
;
@@ -56,7 +56,7 @@ define i8 @ashr_exact_poison_constant_fold(i1 %b, i8 %x) {
5656

5757
define i8 @ashr_exact(i1 %b, i8 %x) {
5858
; CHECK-LABEL: @ashr_exact(
59-
; CHECK-NEXT: [[X_OP:%.*]] = ashr i8 [[X:%.*]], 3
59+
; CHECK-NEXT: [[X_OP:%.*]] = ashr exact i8 [[X:%.*]], 3
6060
; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], i8 [[X_OP]], i8 2
6161
; CHECK-NEXT: ret i8 [[R]]
6262
;
@@ -67,7 +67,7 @@ define i8 @ashr_exact(i1 %b, i8 %x) {
6767

6868
define i8 @shl_nsw_nuw_poison_constant_fold(i1 %b, i8 %x) {
6969
; CHECK-LABEL: @shl_nsw_nuw_poison_constant_fold(
70-
; CHECK-NEXT: [[X_OP:%.*]] = shl i8 16, [[X:%.*]]
70+
; CHECK-NEXT: [[X_OP:%.*]] = shl nuw nsw i8 16, [[X:%.*]]
7171
; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], i8 -128, i8 [[X_OP]]
7272
; CHECK-NEXT: ret i8 [[R]]
7373
;
@@ -78,7 +78,7 @@ define i8 @shl_nsw_nuw_poison_constant_fold(i1 %b, i8 %x) {
7878

7979
define i8 @shl_nsw_nuw(i1 %b, i8 %x) {
8080
; CHECK-LABEL: @shl_nsw_nuw(
81-
; CHECK-NEXT: [[X_OP:%.*]] = shl i8 7, [[X:%.*]]
81+
; CHECK-NEXT: [[X_OP:%.*]] = shl nuw nsw i8 7, [[X:%.*]]
8282
; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], i8 56, i8 [[X_OP]]
8383
; CHECK-NEXT: ret i8 [[R]]
8484
;
@@ -89,7 +89,7 @@ define i8 @shl_nsw_nuw(i1 %b, i8 %x) {
8989

9090
define i8 @add_nsw_poison_constant_fold(i1 %b, i8 %x) {
9191
; CHECK-LABEL: @add_nsw_poison_constant_fold(
92-
; CHECK-NEXT: [[X_OP:%.*]] = add i8 [[X:%.*]], 64
92+
; CHECK-NEXT: [[X_OP:%.*]] = add nsw i8 [[X:%.*]], 64
9393
; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], i8 [[X_OP]], i8 -127
9494
; CHECK-NEXT: ret i8 [[R]]
9595
;
@@ -100,7 +100,7 @@ define i8 @add_nsw_poison_constant_fold(i1 %b, i8 %x) {
100100

101101
define i8 @add_nsw(i1 %b, i8 %x) {
102102
; CHECK-LABEL: @add_nsw(
103-
; CHECK-NEXT: [[X_OP:%.*]] = add i8 [[X:%.*]], 64
103+
; CHECK-NEXT: [[X_OP:%.*]] = add nsw i8 [[X:%.*]], 64
104104
; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], i8 [[X_OP]], i8 71
105105
; CHECK-NEXT: ret i8 [[R]]
106106
;

0 commit comments

Comments
 (0)