[PATCH] D146034: [TTI] Treat AND/OR with widenable conditions as free of cost

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 14 04:52:55 PDT 2023


mkazantsev created this revision.
mkazantsev added reviewers: anna, skatkov, reames, nikic, lebedev.ri.
Herald added subscribers: StephenFan, zzheng.
Herald added a project: All.
mkazantsev requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Because widenable conditions with eventually lower into a constant, such instructions
as `and`, `or` etc. will also be optimized away. Treat them as free.

This is an important thing to have if we want that guards represented as experimental.guard
calls and in their explicit form (branch by `and` with widenable condition) have the same cost
for unroller and other passes like this.


https://reviews.llvm.org/D146034

Files:
  llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
  llvm/test/Transforms/LoopUnroll/guard-cost-for-unrolling.ll


Index: llvm/test/Transforms/LoopUnroll/guard-cost-for-unrolling.ll
===================================================================
--- llvm/test/Transforms/LoopUnroll/guard-cost-for-unrolling.ll
+++ llvm/test/Transforms/LoopUnroll/guard-cost-for-unrolling.ll
@@ -2,7 +2,7 @@
 
 ; REQUIRES: asserts
 
-; FIXME: This test is needed to make sure that the guard cost remains the same,
+; This test is needed to make sure that the guard cost remains the same,
 ; independently on guard representation form (either intrinsic call or branch with
 ; widenable condition).
 
@@ -30,7 +30,7 @@
 
 define void @test_guard_as_branch(ptr %arr, i64 %n, i64 %bound) {
 ; CHECK-LABEL: Loop Unroll: F[test_guard_as_branch] Loop %loop
-; CHECK-NEXT:    Loop Size = 9
+; CHECK-NEXT:    Loop Size = 8
 ; CHECK-NEXT:    runtime unrolling with count: 2
 entry:
   br label %loop
Index: llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
===================================================================
--- llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -499,6 +499,14 @@
       TTI::OperandValueInfo Opd1Info, TTI::OperandValueInfo Opd2Info,
       ArrayRef<const Value *> Args,
       const Instruction *CxtI = nullptr) const {
+    // Widenable conditions will eventually lower into constants, so some
+    // operations with them will be trivially optimized away.
+    auto IsWidenableCondition = [](const Value *V) {
+      if (auto *II = dyn_cast<IntrinsicInst>(V))
+        if (II->getIntrinsicID() == Intrinsic::experimental_widenable_condition)
+          return true;
+      return false;
+    };
     // FIXME: A number of transformation tests seem to require these values
     // which seems a little odd for how arbitary there are.
     switch (Opcode) {
@@ -512,6 +520,11 @@
     case Instruction::URem:
       // FIXME: Unlikely to be true for CodeSize.
       return TTI::TCC_Expensive;
+    case Instruction::And:
+    case Instruction::Or:
+      if (any_of(Args, IsWidenableCondition))
+        return 0;
+      break;
     }
 
     // Assume a 3cy latency for fp arithmetic ops.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D146034.505035.patch
Type: text/x-patch
Size: 2151 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230314/8fd509c3/attachment.bin>


More information about the llvm-commits mailing list