Skip to content

Commit 2a58457

Browse files
committed
[InstCombine] Simplify multi use cast of Phi with constant inputs
1 parent 579fe67 commit 2a58457

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,13 @@ Instruction *InstCombinerImpl::commonCastTransforms(CastInst &CI) {
237237
// legal type.
238238
if (!Src->getType()->isIntegerTy() || !CI.getType()->isIntegerTy() ||
239239
shouldChangeType(CI.getSrcTy(), CI.getType()))
240-
if (Instruction *NV = foldOpIntoPhi(CI, PN))
240+
if (Instruction *NV =
241+
foldOpIntoPhi(CI, PN,
242+
/*AllowMultipleUses=*/
243+
is_contained({Instruction::Trunc, Instruction::ZExt,
244+
Instruction::SExt},
245+
CI.getOpcode()) &&
246+
all_of(PN->operands(), IsaPred<ConstantInt>)))
241247
return NV;
242248
}
243249

llvm/test/Transforms/InstCombine/cast_phi.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ define i32 @zext_constants_multi_use(i8 %x) {
393393
; CHECK: f:
394394
; CHECK-NEXT: br label [[EXIT]]
395395
; CHECK: exit:
396+
; CHECK-NEXT: [[R:%.*]] = phi i32 [ 5, [[T]] ], [ 255, [[F]] ]
396397
; CHECK-NEXT: [[P:%.*]] = phi i8 [ 5, [[T]] ], [ -1, [[F]] ]
397398
; CHECK-NEXT: call void @use8(i8 [[P]])
398-
; CHECK-NEXT: [[R:%.*]] = zext i8 [[P]] to i32
399399
; CHECK-NEXT: ret i32 [[R]]
400400
;
401401
entry:
@@ -425,9 +425,9 @@ define i32 @sext_constants_multi_use(i8 %x) {
425425
; CHECK: f:
426426
; CHECK-NEXT: br label [[EXIT]]
427427
; CHECK: exit:
428+
; CHECK-NEXT: [[R:%.*]] = phi i32 [ 5, [[T]] ], [ -1, [[F]] ]
428429
; CHECK-NEXT: [[P:%.*]] = phi i8 [ 5, [[T]] ], [ -1, [[F]] ]
429430
; CHECK-NEXT: call void @use8(i8 [[P]])
430-
; CHECK-NEXT: [[R:%.*]] = sext i8 [[P]] to i32
431431
; CHECK-NEXT: ret i32 [[R]]
432432
;
433433
entry:
@@ -457,9 +457,9 @@ define i8 @trunc_constants_multi_use(i8 %x) {
457457
; CHECK: f:
458458
; CHECK-NEXT: br label [[EXIT]]
459459
; CHECK: exit:
460+
; CHECK-NEXT: [[R:%.*]] = phi i8 [ 5, [[T]] ], [ -1, [[F]] ]
460461
; CHECK-NEXT: [[P:%.*]] = phi i32 [ 5, [[T]] ], [ -1, [[F]] ]
461462
; CHECK-NEXT: call void @use32(i32 [[P]])
462-
; CHECK-NEXT: [[R:%.*]] = trunc nsw i32 [[P]] to i8
463463
; CHECK-NEXT: ret i8 [[R]]
464464
;
465465
entry:

0 commit comments

Comments
 (0)