[ConstraintElimination] Generate true/false vectors for vector cmps.
authorFlorian Hahn <flo@fhahn.com>
Wed, 2 Nov 2022 18:13:34 +0000 (18:13 +0000)
committerFlorian Hahn <flo@fhahn.com>
Wed, 2 Nov 2022 18:13:35 +0000 (18:13 +0000)
This fixes crashes when vector compares can be simplified to true/false.

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
llvm/test/Transforms/ConstraintElimination/geps-ptrvector.ll

index 8b9076a..375aa4e 100644 (file)
@@ -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();
@@ -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());
+    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());
@@ -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;
   }
index e30830f..df91565 100644 (file)
@@ -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
+}