Skip to content

Commit 67fd0d2

Browse files
bcl5980tstellar
authored andcommitted
[TypePromotion] Add truncate in ConvertTruncs when the original truncate type is not extend type
If the src type is not extend type, after convert the truncate to and we need to truncate the and also to make sure the all user is legal. The old fix D137613 doesn't work when the truncate convert to and have the other users. So this time I try to add the truncate after and to avoid all these potential issues. Fix: #59554 Reviewed By: samparker Differential Revision: https://reviews.llvm.org/D140869 (cherry picked from commit a0b470c)
1 parent 74d3ba1 commit 67fd0d2

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

llvm/lib/CodeGen/TypePromotion.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@ void IRPromoter::Cleanup() {
570570
LLVM_DEBUG(dbgs() << "IR Promotion: Cleanup..\n");
571571
// Some zexts will now have become redundant, along with their trunc
572572
// operands, so remove them.
573-
// Some zexts need to be replaced with truncate if src bitwidth is larger.
574573
for (auto *V : Visited) {
575574
if (!isa<ZExtInst>(V))
576575
continue;
@@ -585,11 +584,6 @@ void IRPromoter::Cleanup() {
585584
<< "\n");
586585
ReplaceAllUsersOfWith(ZExt, Src);
587586
continue;
588-
} else if (ZExt->getSrcTy()->getScalarSizeInBits() > PromotedWidth) {
589-
IRBuilder<> Builder{ZExt};
590-
Value *Trunc = Builder.CreateTrunc(Src, ZExt->getDestTy());
591-
ReplaceAllUsersOfWith(ZExt, Trunc);
592-
continue;
593587
}
594588

595589
// We've inserted a trunc for a zext sink, but we already know that the
@@ -626,6 +620,8 @@ void IRPromoter::ConvertTruncs() {
626620
ConstantInt *Mask =
627621
ConstantInt::get(SrcTy, APInt::getMaxValue(NumBits).getZExtValue());
628622
Value *Masked = Builder.CreateAnd(Trunc->getOperand(0), Mask);
623+
if (SrcTy != ExtTy)
624+
Masked = Builder.CreateTrunc(Masked, ExtTy);
629625

630626
if (auto *I = dyn_cast<Instruction>(Masked))
631627
NewInsts.insert(I);

llvm/test/Transforms/TypePromotion/AArch64/pr58843.ll

-20
This file was deleted.

llvm/test/Transforms/TypePromotion/AArch64/trunc-zext-chain.ll

+36
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,39 @@ latch: ; preds = %bb14, %bb9
177177
exit:
178178
ret i64 %var30
179179
}
180+
181+
; Check the case don't crash due to zext source type bitwidth
182+
; larger than dest type bitwidth.
183+
define i1 @pr58843(i8 %arg) {
184+
; CHECK-LABEL: @pr58843(
185+
; CHECK-NEXT: [[EXT1:%.*]] = zext i8 [[ARG:%.*]] to i64
186+
; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[EXT1]], 7
187+
; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
188+
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[TMP2]], 0
189+
; CHECK-NEXT: ret i1 [[CMP]]
190+
;
191+
%ext1 = zext i8 %arg to i64
192+
%trunc = trunc i64 %ext1 to i3
193+
%ext2 = zext i3 %trunc to i8
194+
%cmp = icmp ne i8 %ext2, 0
195+
ret i1 %cmp
196+
}
197+
198+
; Check the case don't crash due to xor two op have different
199+
; types
200+
define i1 @pr59554(i8 %arg) {
201+
; CHECK-LABEL: @pr59554(
202+
; CHECK-NEXT: [[ARG_EXT:%.*]] = zext i8 [[ARG:%.*]] to i64
203+
; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[ARG_EXT]], 7
204+
; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32
205+
; CHECK-NEXT: [[SWITCH_TABLEIDX:%.*]] = xor i32 [[TMP2]], 1
206+
; CHECK-NEXT: [[SWITCH_LOBIT:%.*]] = icmp ne i32 [[TMP2]], 0
207+
; CHECK-NEXT: ret i1 [[SWITCH_LOBIT]]
208+
;
209+
%arg.ext = zext i8 %arg to i64
210+
%trunc = trunc i64 %arg.ext to i3
211+
%switch.tableidx = xor i3 %trunc, 1
212+
%switch.maskindex = zext i3 %trunc to i8
213+
%switch.lobit = icmp ne i8 %switch.maskindex, 0
214+
ret i1 %switch.lobit
215+
}

0 commit comments

Comments
 (0)