Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit 7c4d39c

Browse files
committed
[InstCombine] treat i1 as a special type in shouldChangeType()
This patch is based on the llvm-dev discussion here: http://lists.llvm.org/pipermail/llvm-dev/2017-January/109631.html Folding to i1 should always be desirable because that's better for value tracking and we have special folds for i1 types. I checked for other users of shouldChangeType() where this might have an effect, but we already handle the i1 case differently than other types in all of those cases. Side note: the default datalayout includes i1, so it seems we only find this gap in shouldChangeType + phi folding for the case when there is (1) an explicit datalayout without i1, (2) casting to i1 from a legal type, and (3) a phi with exactly 2 incoming casted operands (as Björn mentioned). Differential Revision: https://reviews.llvm.org/D29336 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@294066 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 21b9ce0 commit 7c4d39c

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ Value *InstCombiner::EmitGEPOffset(User *GEP) {
8989
/// Return true if it is desirable to convert an integer computation from a
9090
/// given bit width to a new bit width.
9191
/// We don't want to convert from a legal to an illegal type or from a smaller
92-
/// to a larger illegal type.
92+
/// to a larger illegal type. A width of '1' is always treated as a legal type
93+
/// because i1 is a fundamental type in IR, and there are many specialized
94+
/// optimizations for i1 types.
9395
bool InstCombiner::shouldChangeType(unsigned FromWidth,
9496
unsigned ToWidth) const {
95-
bool FromLegal = DL.isLegalInteger(FromWidth);
96-
bool ToLegal = DL.isLegalInteger(ToWidth);
97+
bool FromLegal = FromWidth == 1 || DL.isLegalInteger(FromWidth);
98+
bool ToLegal = ToWidth == 1 || DL.isLegalInteger(ToWidth);
9799

98100
// If this is a legal integer from type, and the result would be an illegal
99101
// type, don't do the transformation.
@@ -110,7 +112,9 @@ bool InstCombiner::shouldChangeType(unsigned FromWidth,
110112

111113
/// Return true if it is desirable to convert a computation from 'From' to 'To'.
112114
/// We don't want to convert from a legal to an illegal type or from a smaller
113-
/// to a larger illegal type.
115+
/// to a larger illegal type. i1 is always treated as a legal type because it is
116+
/// a fundamental type in IR, and there are many specialized optimizations for
117+
/// i1 types.
114118
bool InstCombiner::shouldChangeType(Type *From, Type *To) const {
115119
assert(From->isIntegerTy() && To->isIntegerTy());
116120

test/Transforms/InstCombine/zext-phi.ll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
target datalayout = "e-m:e-i64:64-n8:16:32:64"
44

5+
; Although i1 is not in the datalayout, we should treat it
6+
; as a legal type because it is a fundamental type in IR.
7+
; This means we should shrink the phi (sink the zexts).
8+
59
define i64 @sink_i1_casts(i1 %cond1, i1 %cond2) {
610
; CHECK-LABEL: @sink_i1_casts(
711
; CHECK-NEXT: entry:
8-
; CHECK-NEXT: [[Z1:%.*]] = zext i1 %cond1 to i64
912
; CHECK-NEXT: br i1 %cond1, label %if, label %end
1013
; CHECK: if:
11-
; CHECK-NEXT: [[Z2:%.*]] = zext i1 %cond2 to i64
1214
; CHECK-NEXT: br label %end
1315
; CHECK: end:
14-
; CHECK-NEXT: [[PHI:%.*]] = phi i64 [ [[Z1]], %entry ], [ [[Z2]], %if ]
16+
; CHECK-NEXT: [[PHI_IN:%.*]] = phi i1 [ %cond1, %entry ], [ %cond2, %if ]
17+
; CHECK-NEXT: [[PHI:%.*]] = zext i1 [[PHI_IN]] to i64
1518
; CHECK-NEXT: ret i64 [[PHI]]
1619
;
1720
entry:

0 commit comments

Comments
 (0)