Skip to content

Commit 3c17cb7

Browse files
committed
[ARM CGP] Fix ConvertTruncs
ConvertTruncs is used to replace a trunc for an AND mask, however this function wasn't working as expected. By performing the change later, we can create a wide type integer mask instead of a narrow -1 value, which could then be simply removed (incorrectly). Because we now perform this action later, it's necessary to cache the trunc type before we perform the promotion. Differential Revision: https://reviews.llvm.org/D57686 llvm-svn: 354108
1 parent 73db5c1 commit 3c17cb7

File tree

3 files changed

+123
-103
lines changed

3 files changed

+123
-103
lines changed

llvm/lib/Target/ARM/ARMCodeGenPrepare.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ void IRPromoter::TruncateSinks() {
686686
}
687687

688688
void IRPromoter::Cleanup() {
689+
LLVM_DEBUG(dbgs() << "ARM CGP: Cleanup..\n");
689690
// Some zexts will now have become redundant, along with their trunc
690691
// operands, so remove them
691692
for (auto V : *Visited) {
@@ -729,19 +730,21 @@ void IRPromoter::Cleanup() {
729730
}
730731

731732
void IRPromoter::ConvertTruncs() {
733+
LLVM_DEBUG(dbgs() << "ARM CGP: Converting truncs..\n");
732734
IRBuilder<> Builder{Ctx};
733735

734736
for (auto *V : *Visited) {
735737
if (!isa<TruncInst>(V) || Sources->count(V))
736738
continue;
737739

738740
auto *Trunc = cast<TruncInst>(V);
739-
assert(LessThanTypeSize(Trunc) && "expected narrow trunc");
740-
741741
Builder.SetInsertPoint(Trunc);
742-
unsigned NumBits =
743-
cast<IntegerType>(Trunc->getType())->getScalarSizeInBits();
744-
ConstantInt *Mask = ConstantInt::get(Ctx, APInt::getMaxValue(NumBits));
742+
IntegerType *SrcTy = cast<IntegerType>(Trunc->getOperand(0)->getType());
743+
IntegerType *DestTy = cast<IntegerType>(TruncTysMap[Trunc][0]);
744+
745+
unsigned NumBits = DestTy->getScalarSizeInBits();
746+
ConstantInt *Mask =
747+
ConstantInt::get(SrcTy, APInt::getMaxValue(NumBits).getZExtValue());
745748
Value *Masked = Builder.CreateAnd(Trunc->getOperand(0), Mask);
746749

747750
if (auto *I = dyn_cast<Instruction>(Masked))
@@ -783,6 +786,12 @@ void IRPromoter::Mutate(Type *OrigTy,
783786
TruncTysMap[I].push_back(I->getOperand(i)->getType());
784787
}
785788
}
789+
for (auto *V : Visited) {
790+
if (!isa<TruncInst>(V) || Sources.count(V))
791+
continue;
792+
auto *Trunc = cast<TruncInst>(V);
793+
TruncTysMap[Trunc].push_back(Trunc->getDestTy());
794+
}
786795

787796
// Convert adds and subs using negative immediates to equivalent instructions
788797
// that use positive constants.
@@ -791,14 +800,14 @@ void IRPromoter::Mutate(Type *OrigTy,
791800
// Insert zext instructions between sources and their users.
792801
ExtendSources();
793802

794-
// Convert any truncs, that aren't sources, into AND masks.
795-
ConvertTruncs();
796-
797803
// Promote visited instructions, mutating their types in place. Also insert
798804
// DSP intrinsics, if enabled, for adds and subs which would be unsafe to
799805
// promote.
800806
PromoteTree();
801807

808+
// Convert any truncs, that aren't sources, into AND masks.
809+
ConvertTruncs();
810+
802811
// Insert trunc instructions for use by calls, stores etc...
803812
TruncateSinks();
804813

llvm/test/CodeGen/ARM/CGP/arm-cgp-calls.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ exit:
201201
}
202202

203203
; CHECK-LABEL: promote_arg_pass_to_call
204-
; CHECK-NOT: uxt
204+
; CHECK: uxtb
205205
define i16 @promote_arg_pass_to_call(i16 zeroext %arg1, i16 zeroext %arg2) {
206206
%conv = add nuw i16 %arg1, 15
207207
%mul = mul nuw nsw i16 %conv, 3

0 commit comments

Comments
 (0)