From fc128e126bcd6ca2723e74282601ef76f705489f Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Mon, 13 Mar 2023 14:11:48 +0700 Subject: [PATCH] [LICM] Do not hoist min/max for pointer types umin and similar intrinsics are not defined for them. --- llvm/lib/Transforms/Scalar/LICM.cpp | 2 ++ llvm/test/Transforms/LICM/min_max.ll | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/llvm/lib/Transforms/Scalar/LICM.cpp b/llvm/lib/Transforms/Scalar/LICM.cpp index 6874950..d0f7dc2 100644 --- a/llvm/lib/Transforms/Scalar/LICM.cpp +++ b/llvm/lib/Transforms/Scalar/LICM.cpp @@ -2427,6 +2427,8 @@ static bool hoistMinMax(Instruction &I, Loop &L, ICFLoopSafetyInfo &SafetyInfo, Value *&LHS, Value *&RHS) { if (!match(C, m_OneUse(m_ICmp(P, m_Value(LHS), m_Value(RHS))))) return false; + if (!LHS->getType()->isIntegerTy()) + return false; if (!ICmpInst::isRelational(P)) return false; if (L.isLoopInvariant(LHS)) { diff --git a/llvm/test/Transforms/LICM/min_max.ll b/llvm/test/Transforms/LICM/min_max.ll index d9710da..9cd4409 100644 --- a/llvm/test/Transforms/LICM/min_max.ll +++ b/llvm/test/Transforms/LICM/min_max.ll @@ -31,6 +31,37 @@ exit: ret i32 %iv } +; Do not optimize: pointer types. +define ptr @test_ult_ptr_neg(ptr %start, ptr %inv_1, ptr %inv_2) { +; CHECK-LABEL: @test_ult_ptr_neg( +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[LOOP:%.*]] +; CHECK: loop: +; CHECK-NEXT: [[IV:%.*]] = phi ptr [ [[START:%.*]], [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ] +; CHECK-NEXT: [[CMP_1:%.*]] = icmp ult ptr [[IV]], [[INV_1:%.*]] +; CHECK-NEXT: [[CMP_2:%.*]] = icmp ult ptr [[IV]], [[INV_2:%.*]] +; CHECK-NEXT: [[LOOP_COND:%.*]] = and i1 [[CMP_1]], [[CMP_2]] +; CHECK-NEXT: [[IV_NEXT]] = getelementptr i32, ptr [[IV]], i64 4 +; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[EXIT:%.*]] +; CHECK: exit: +; CHECK-NEXT: [[IV_LCSSA:%.*]] = phi ptr [ [[IV]], [[LOOP]] ] +; CHECK-NEXT: ret ptr [[IV_LCSSA]] +; +entry: + br label %loop + +loop: + %iv = phi ptr [%start, %entry], [%iv.next, %loop] + %cmp_1 = icmp ult ptr %iv, %inv_1 + %cmp_2 = icmp ult ptr %iv, %inv_2 + %loop_cond = and i1 %cmp_1, %cmp_2 + %iv.next = getelementptr i32, ptr %iv, i64 4 + br i1 %loop_cond, label %loop, label %exit + +exit: + ret ptr %iv +} + ; turn to %iv <=u umin(inv_1, inv_2) and hoist it out of loop. define i32 @test_ule(i32 %start, i32 %inv_1, i32 %inv_2) { ; CHECK-LABEL: @test_ule( -- 2.7.4