Skip to content

Commit aef840a

Browse files
committed
[Transforms] Resolve FIXME: Pick the smallest legal type that fits
Pick the type based on the smallest bit-width possible, using DataLayout.
1 parent 463529f commit aef840a

File tree

3 files changed

+48
-36
lines changed

3 files changed

+48
-36
lines changed

llvm/include/llvm/Transforms/Scalar/Float2Int.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Float2IntPass : public PassInfoMixin<Float2IntPass> {
4444
std::optional<ConstantRange> calcRange(Instruction *I);
4545
void walkBackwards();
4646
void walkForwards();
47-
bool validateAndTransform();
47+
bool validateAndTransform(DataLayout DL);
4848
Value *convert(Instruction *I, Type *ToTy);
4949
void cleanup();
5050

llvm/lib/Transforms/Scalar/Float2Int.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ void Float2IntPass::walkForwards() {
311311
}
312312

313313
// If there is a valid transform to be done, do it.
314-
bool Float2IntPass::validateAndTransform() {
314+
bool Float2IntPass::validateAndTransform(DataLayout DL) {
315315
bool MadeChange = false;
316316

317317
// Iterate over every disjoint partition of the def-use graph.
@@ -382,12 +382,22 @@ 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 = nullptr;
388+
for (unsigned BW = 8; BW <= 64; BW *= 2) {
389+
if (MinBW <= BW && DL.isLegalInteger(BW)) {
390+
Ty = IntegerType::get(*Ctx, BW);
391+
break;
392+
}
393+
}
388394

389-
for (auto MI = ECs.member_begin(It), ME = ECs.member_end();
390-
MI != ME; ++MI)
395+
if (!Ty) {
396+
// Should not happen, but just in case:
397+
Ty = (MinBW > 32) ? Type::getInt64Ty(*Ctx) : Type::getInt32Ty(*Ctx);
398+
}
399+
400+
for (auto MI = ECs.member_begin(It), ME = ECs.member_end(); MI != ME; ++MI)
391401
convert(*MI, Ty);
392402
MadeChange = true;
393403
}
@@ -491,7 +501,10 @@ bool Float2IntPass::runImpl(Function &F, const DominatorTree &DT) {
491501
walkBackwards();
492502
walkForwards();
493503

494-
bool Modified = validateAndTransform();
504+
505+
const DataLayout &DL = F.getParent()->getDataLayout();
506+
507+
bool Modified = validateAndTransform(DL);
495508
if (Modified)
496509
cleanup();
497510
return Modified;

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)