Skip to content

Commit

Permalink
[ConstraintElimination] Generate true/false vectors for vector cmps.
Browse files Browse the repository at this point in the history
This fixes crashes when vector compares can be simplified to true/false.
  • Loading branch information
fhahn committed Nov 2, 2022
1 parent c050dd4 commit b478d8b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Expand Up @@ -746,6 +746,12 @@ void State::addInfoFor(BasicBlock &BB) {
WorkList.emplace_back(DT.getNode(Br->getSuccessor(1)), CmpI, true);
}

static Constant *getScalarConstOrSplat(ConstantInt *C, Type *Ty) {
if (auto *VTy = dyn_cast<FixedVectorType>(Ty))
return ConstantVector::getSplat(VTy->getElementCount(), C);
return C;
}

static bool checkAndReplaceCondition(CmpInst *Cmp, ConstraintInfo &Info) {
LLVM_DEBUG(dbgs() << "Checking " << *Cmp << "\n");
CmpInst::Predicate Pred = Cmp->getPredicate();
Expand Down Expand Up @@ -780,7 +786,9 @@ static bool checkAndReplaceCondition(CmpInst *Cmp, ConstraintInfo &Info) {
dbgs() << "Condition " << *Cmp << " implied by dominating constraints\n";
dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned));
});
Cmp->replaceUsesWithIf(ConstantInt::getTrue(Ctx), [](Use &U) {
Constant *TrueC =
getScalarConstOrSplat(ConstantInt::getTrue(Ctx), Cmp->getType());

This comment has been minimized.

Copy link
@nikic

nikic Nov 2, 2022

Contributor

ConstantInt::getTrue(CmpInst::makeCmpResultType(Cmp->getType())) would be the idiomatic way to write this, I think.

This comment has been minimized.

Copy link
@fhahn

fhahn Nov 3, 2022

Author Contributor

Thanks, that's better! Should be updated in 44d8f80:

Cmp->replaceUsesWithIf(TrueC, [](Use &U) {
// Conditions in an assume trivially simplify to true. Skip uses
// in assume calls to not destroy the available information.
auto *II = dyn_cast<IntrinsicInst>(U.getUser());
Expand All @@ -797,7 +805,9 @@ static bool checkAndReplaceCondition(CmpInst *Cmp, ConstraintInfo &Info) {
dbgs() << "Condition !" << *Cmp << " implied by dominating constraints\n";
dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned));
});
Cmp->replaceAllUsesWith(ConstantInt::getFalse(Ctx));
Constant *FalseC =
getScalarConstOrSplat(ConstantInt::getFalse(Ctx), Cmp->getType());
Cmp->replaceAllUsesWith(FalseC);
NumCondsRemoved++;
Changed = true;
}
Expand Down
22 changes: 22 additions & 0 deletions llvm/test/Transforms/ConstraintElimination/geps-ptrvector.ll
Expand Up @@ -12,3 +12,25 @@ define <2 x i1> @test.vectorgep(<2 x ptr> %vec) {
%cond = icmp ule <2 x ptr> %gep, zeroinitializer
ret <2 x i1> %cond
}

define <2 x i1> @test.vectorgep.ult.true(<2 x ptr> %vec) {
; CHECK-LABEL: @test.vectorgep.ult.true(
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr inbounds i32, <2 x ptr> [[VEC:%.*]], i64 1
; CHECK-NEXT: [[T_1:%.*]] = icmp ult <2 x ptr> [[VEC]], [[GEP_1]]
; CHECK-NEXT: ret <2 x i1> <i1 true, i1 true>
;
%gep.1 = getelementptr inbounds i32, <2 x ptr> %vec, i64 1
%t.1 = icmp ult <2 x ptr> %vec, %gep.1
ret <2 x i1> %t.1
}

define <2 x i1> @test.vectorgep.ult.false(<2 x ptr> %vec) {
; CHECK-LABEL: @test.vectorgep.ult.false(
; CHECK-NEXT: [[GEP_1:%.*]] = getelementptr inbounds i32, <2 x ptr> [[VEC:%.*]], i64 1
; CHECK-NEXT: [[T_1:%.*]] = icmp ult <2 x ptr> [[GEP_1]], [[VEC]]
; CHECK-NEXT: ret <2 x i1> zeroinitializer
;
%gep.1 = getelementptr inbounds i32, <2 x ptr> %vec, i64 1
%t.1 = icmp ult <2 x ptr> %gep.1, %vec
ret <2 x i1> %t.1
}

0 comments on commit b478d8b

Please sign in to comment.