Skip to content

Commit 0f671c9

Browse files
committed
[Transforms] Resolve FIXME: Pick the smallest legal type that fits
Pick the type based on the smallest bit-width possible.
1 parent 67c1c1d commit 0f671c9

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

llvm/lib/Transforms/Scalar/Float2Int.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,18 @@ bool Float2IntPass::validateAndTransform() {
382382
continue;
383383
}
384384

385-
// OK, R is known to be representable. Now pick a type for it.
386-
// FIXME: Pick the smallest legal type that will fit.
387-
Type *Ty = (MinBW > 32) ? Type::getInt64Ty(*Ctx) : Type::getInt32Ty(*Ctx);
385+
// OK, R is known to be representable.
386+
// Pick the smallest legal type that will fit.
387+
Type *Ty;
388+
if (MinBW <= 8) {
389+
Ty = Type::getInt8Ty(*Ctx);
390+
} else if (MinBW <= 16) {
391+
Ty = Type::getInt16Ty(*Ctx);
392+
} else if (MinBW <= 32) {
393+
Ty = Type::getInt32Ty(*Ctx);
394+
} else {
395+
Ty = Type::getInt64Ty(*Ctx);
396+
}
388397

389398
for (auto MI = ECs.member_begin(It), ME = ECs.member_end();
390399
MI != ME; ++MI)

llvm/test/Transforms/Float2Int/basic.ll

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
define i16 @simple1(i8 %a) {
99
; CHECK-LABEL: @simple1(
10-
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i32
11-
; CHECK-NEXT: [[T21:%.*]] = add i32 [[TMP1]], 1
12-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[T21]] to i16
13-
; CHECK-NEXT: ret i16 [[TMP2]]
10+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i16
11+
; CHECK-NEXT: [[T21:%.*]] = add i16 [[TMP1]], 1
12+
; CHECK-NEXT: ret i16 [[T21]]
1413
;
1514
%t1 = uitofp i8 %a to float
1615
%t2 = fadd float %t1, 1.0
@@ -20,9 +19,9 @@ define i16 @simple1(i8 %a) {
2019

2120
define i8 @simple2(i8 %a) {
2221
; CHECK-LABEL: @simple2(
23-
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i32
24-
; CHECK-NEXT: [[T21:%.*]] = sub i32 [[TMP1]], 1
25-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[T21]] to i8
22+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i16
23+
; CHECK-NEXT: [[T21:%.*]] = sub i16 [[TMP1]], 1
24+
; CHECK-NEXT: [[TMP2:%.*]] = trunc i16 [[T21]] to i8
2625
; CHECK-NEXT: ret i8 [[TMP2]]
2726
;
2827
%t1 = uitofp i8 %a to float
@@ -33,9 +32,10 @@ define i8 @simple2(i8 %a) {
3332

3433
define i32 @simple3(i8 %a) {
3534
; CHECK-LABEL: @simple3(
36-
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i32
37-
; CHECK-NEXT: [[T21:%.*]] = sub i32 [[TMP1]], 1
38-
; CHECK-NEXT: ret i32 [[T21]]
35+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i16
36+
; CHECK-NEXT: [[T21:%.*]] = sub i16 [[TMP1]], 1
37+
; CHECK-NEXT: [[TMP2:%.*]] = zext i16 [[T21]] to i32
38+
; CHECK-NEXT: ret i32 [[TMP2]]
3939
;
4040
%t1 = uitofp i8 %a to float
4141
%t2 = fsub float %t1, 1.0
@@ -45,9 +45,9 @@ define i32 @simple3(i8 %a) {
4545

4646
define i1 @cmp(i8 %a, i8 %b) {
4747
; CHECK-LABEL: @cmp(
48-
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i32
49-
; CHECK-NEXT: [[TMP2:%.*]] = zext i8 [[B:%.*]] to i32
50-
; CHECK-NEXT: [[T31:%.*]] = icmp slt i32 [[TMP1]], [[TMP2]]
48+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i16
49+
; CHECK-NEXT: [[TMP2:%.*]] = zext i8 [[B:%.*]] to i16
50+
; CHECK-NEXT: [[T31:%.*]] = icmp slt i16 [[TMP1]], [[TMP2]]
5151
; CHECK-NEXT: ret i1 [[T31]]
5252
;
5353
%t1 = uitofp i8 %a to float
@@ -106,13 +106,14 @@ define i32 @simple6(i8 %a, i8 %b) {
106106

107107
define i32 @multi1(i8 %a, i8 %b, i8 %c, float %d) {
108108
; CHECK-LABEL: @multi1(
109-
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i32
110-
; CHECK-NEXT: [[TMP2:%.*]] = zext i8 [[B:%.*]] to i32
109+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i16
110+
; CHECK-NEXT: [[TMP2:%.*]] = zext i8 [[B:%.*]] to i16
111111
; CHECK-NEXT: [[FC:%.*]] = uitofp i8 [[C:%.*]] to float
112-
; CHECK-NEXT: [[X1:%.*]] = add i32 [[TMP1]], [[TMP2]]
112+
; CHECK-NEXT: [[X1:%.*]] = add i16 [[TMP1]], [[TMP2]]
113+
; CHECK-NEXT: [[TMP3:%.*]] = zext i16 [[X1]] to i32
113114
; CHECK-NEXT: [[Z:%.*]] = fadd float [[FC]], [[D:%.*]]
114115
; CHECK-NEXT: [[W:%.*]] = fptoui float [[Z]] to i32
115-
; CHECK-NEXT: [[R:%.*]] = add i32 [[X1]], [[W]]
116+
; CHECK-NEXT: [[R:%.*]] = add i32 [[TMP3]], [[W]]
116117
; CHECK-NEXT: ret i32 [[R]]
117118
;
118119
%fa = uitofp i8 %a to float
@@ -128,10 +129,9 @@ define i32 @multi1(i8 %a, i8 %b, i8 %c, float %d) {
128129

129130
define i16 @simple_negzero(i8 %a) {
130131
; CHECK-LABEL: @simple_negzero(
131-
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i32
132-
; CHECK-NEXT: [[T21:%.*]] = add i32 [[TMP1]], 0
133-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[T21]] to i16
134-
; CHECK-NEXT: ret i16 [[TMP2]]
132+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i16
133+
; CHECK-NEXT: [[T21:%.*]] = add i16 [[TMP1]], 0
134+
; CHECK-NEXT: ret i16 [[T21]]
135135
;
136136
%t1 = uitofp i8 %a to float
137137
%t2 = fadd fast float %t1, -0.0
@@ -141,9 +141,9 @@ define i16 @simple_negzero(i8 %a) {
141141

142142
define i32 @simple_negative(i8 %call) {
143143
; CHECK-LABEL: @simple_negative(
144-
; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[CALL:%.*]] to i32
145-
; CHECK-NEXT: [[MUL1:%.*]] = mul i32 [[TMP1]], -3
146-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[MUL1]] to i8
144+
; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[CALL:%.*]] to i16
145+
; CHECK-NEXT: [[MUL1:%.*]] = mul i16 [[TMP1]], -3
146+
; CHECK-NEXT: [[TMP2:%.*]] = trunc i16 [[MUL1]] to i8
147147
; CHECK-NEXT: [[CONV3:%.*]] = sext i8 [[TMP2]] to i32
148148
; CHECK-NEXT: ret i32 [[CONV3]]
149149
;
@@ -156,10 +156,9 @@ define i32 @simple_negative(i8 %call) {
156156

157157
define i16 @simple_fneg(i8 %a) {
158158
; CHECK-LABEL: @simple_fneg(
159-
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i32
160-
; CHECK-NEXT: [[T21:%.*]] = sub i32 0, [[TMP1]]
161-
; CHECK-NEXT: [[TMP2:%.*]] = trunc i32 [[T21]] to i16
162-
; CHECK-NEXT: ret i16 [[TMP2]]
159+
; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[A:%.*]] to i16
160+
; CHECK-NEXT: [[T21:%.*]] = sub i16 0, [[TMP1]]
161+
; CHECK-NEXT: ret i16 [[T21]]
163162
;
164163
%t1 = uitofp i8 %a to float
165164
%t2 = fneg fast float %t1

0 commit comments

Comments
 (0)