Skip to content

Commit

Permalink
[ARM CGP] Fix ConvertTruncs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sparker-arm committed Feb 15, 2019
1 parent 73db5c1 commit 3c17cb7
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 103 deletions.
25 changes: 17 additions & 8 deletions llvm/lib/Target/ARM/ARMCodeGenPrepare.cpp
Expand Up @@ -686,6 +686,7 @@ void IRPromoter::TruncateSinks() {
}

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

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

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

auto *Trunc = cast<TruncInst>(V);
assert(LessThanTypeSize(Trunc) && "expected narrow trunc");

Builder.SetInsertPoint(Trunc);
unsigned NumBits =
cast<IntegerType>(Trunc->getType())->getScalarSizeInBits();
ConstantInt *Mask = ConstantInt::get(Ctx, APInt::getMaxValue(NumBits));
IntegerType *SrcTy = cast<IntegerType>(Trunc->getOperand(0)->getType());
IntegerType *DestTy = cast<IntegerType>(TruncTysMap[Trunc][0]);

unsigned NumBits = DestTy->getScalarSizeInBits();
ConstantInt *Mask =
ConstantInt::get(SrcTy, APInt::getMaxValue(NumBits).getZExtValue());
Value *Masked = Builder.CreateAnd(Trunc->getOperand(0), Mask);

if (auto *I = dyn_cast<Instruction>(Masked))
Expand Down Expand Up @@ -783,6 +786,12 @@ void IRPromoter::Mutate(Type *OrigTy,
TruncTysMap[I].push_back(I->getOperand(i)->getType());
}
}
for (auto *V : Visited) {
if (!isa<TruncInst>(V) || Sources.count(V))
continue;
auto *Trunc = cast<TruncInst>(V);
TruncTysMap[Trunc].push_back(Trunc->getDestTy());
}

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

// Convert any truncs, that aren't sources, into AND masks.
ConvertTruncs();

// Promote visited instructions, mutating their types in place. Also insert
// DSP intrinsics, if enabled, for adds and subs which would be unsafe to
// promote.
PromoteTree();

// Convert any truncs, that aren't sources, into AND masks.
ConvertTruncs();

// Insert trunc instructions for use by calls, stores etc...
TruncateSinks();

Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/ARM/CGP/arm-cgp-calls.ll
Expand Up @@ -201,7 +201,7 @@ exit:
}

; CHECK-LABEL: promote_arg_pass_to_call
; CHECK-NOT: uxt
; CHECK: uxtb
define i16 @promote_arg_pass_to_call(i16 zeroext %arg1, i16 zeroext %arg2) {
%conv = add nuw i16 %arg1, 15
%mul = mul nuw nsw i16 %conv, 3
Expand Down

0 comments on commit 3c17cb7

Please sign in to comment.