[PATCH] D12965: [InstCombine] shrink switch conditions better (PR24766)

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 21 11:40:27 PDT 2015


spatel updated this revision to Diff 35286.
spatel added a comment.

Patch updated:
Use new version (r248170) of "ShouldChangeType()".


http://reviews.llvm.org/D12965

Files:
  lib/Transforms/InstCombine/InstructionCombining.cpp
  test/Transforms/InstCombine/narrow-switch.ll
  test/Transforms/InstCombine/pr21651.ll

Index: test/Transforms/InstCombine/pr21651.ll
===================================================================
--- test/Transforms/InstCombine/pr21651.ll
+++ test/Transforms/InstCombine/pr21651.ll
@@ -15,9 +15,9 @@
 }
 
 ; CHECK-LABEL: define void @PR21651(
-; CHECK:   switch i2 0, label %out [
-; CHECK:     i2 0, label %out
-; CHECK:     i2 1, label %out
+; CHECK:   switch i1 false, label %out [
+; CHECK:     i1 false, label %out
+; CHECK:     i1 true, label %out
 ; CHECK:   ]
 ; CHECK: out:                                              ; preds = %0, %0, %0
 ; CHECK:   ret void
Index: test/Transforms/InstCombine/narrow-switch.ll
===================================================================
--- test/Transforms/InstCombine/narrow-switch.ll
+++ test/Transforms/InstCombine/narrow-switch.ll
@@ -1,13 +1,22 @@
-; RUN: opt < %s -instcombine -S | FileCheck %s
+; Vary legal integer types in data layout.
 
-target datalayout = "e-m:o-p:32:32-f64:32:64-v64:32:64-v128:32:128-a:0:32-n32-S32"
+; RUN: opt < %s -instcombine -S -default-data-layout=n32 | FileCheck %s
+; RUN: opt < %s -instcombine -S -default-data-layout=n32:64 | FileCheck %s --check-prefix=CHECK64
 
 ; CHECK-LABEL: define i32 @positive1
 ; CHECK: switch i32
 ; CHECK: i32 10, label
 ; CHECK: i32 100, label
 ; CHECK: i32 1001, label
 
+; It doesn't matter if 64-bit integers are legal; 32-bit integers are legal too, so shrink.
+;
+; CHECK64-LABEL: @positive1
+; CHECK64: switch i32
+; CHECK64: i32 10, label
+; CHECK64: i32 100, label
+; CHECK64: i32 1001, label
+
 define i32 @positive1(i64 %a) {
 entry:
   %and = and i64 %a, 4294967295
@@ -96,11 +105,20 @@
 ; truncation to generate the sub constant expressions that leads
 ; to the recomputed condition.
 ;
+; If both 64-bit and 59-bit integers are illegal, it's ok to shrink.
+;
 ; CHECK-LABEL: @trunc64to59
 ; CHECK: switch i59
 ; CHECK: i59 0, label
 ; CHECK: i59 18717182647723699, label
 
+; If 64-bit integers are legal, we should not shrink to an illegal type.
+;
+; CHECK64-LABEL: @trunc64to59
+; CHECK64: switch i64
+; CHECK64: i64 0, label
+; CHECK64: i64 18717182647723699, label
+
 define void @trunc64to59(i64 %a) {
 entry:
   %tmp0 = and i64 %a, 15
@@ -121,3 +139,4 @@
 sw.default:
   ret void
 }
+
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2155,17 +2155,15 @@
 
   unsigned NewWidth = BitWidth - std::max(LeadingKnownZeros, LeadingKnownOnes);
 
-  // Truncate the condition operand if the new type is equal to or larger than
-  // the largest legal integer type. We need to be conservative here since
-  // x86 generates redundant zero-extension instructions if the operand is
-  // truncated to i8 or i16.
+  // Truncate the condition operand if the new type is better (smaller/legal)
+  // than the old type.
   bool TruncCond = false;
-  if (NewWidth > 0 && BitWidth > NewWidth &&
-      NewWidth >= DL.getLargestLegalIntTypeSize()) {
+  if (NewWidth > 0 && NewWidth < BitWidth &&
+      ShouldChangeType(BitWidth, NewWidth)) {
     TruncCond = true;
     IntegerType *Ty = IntegerType::get(SI.getContext(), NewWidth);
     Builder->SetInsertPoint(&SI);
-    Value *NewCond = Builder->CreateTrunc(SI.getCondition(), Ty, "trunc");
+    Value *NewCond = Builder->CreateTrunc(Cond, Ty, "trunc");
     SI.setCondition(NewCond);
 
     for (auto &C : SI.cases())


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12965.35286.patch
Type: text/x-patch
Size: 3509 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150921/b21f9bfa/attachment.bin>


More information about the llvm-commits mailing list