[PATCH] D118905: [TypePromotion] Avoid unnecessary trunc zext pairs

Sam Parker via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 3 07:07:41 PST 2022


samparker created this revision.
samparker added reviewers: dmgreen, avieira, adriantong1024, craig.topper.
Herald added a subscriber: hiraditya.
samparker requested review of this revision.
Herald added a project: LLVM.

Following on from D115451 <https://reviews.llvm.org/D115451>, this is the next in a series of improvements for this pass.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118905

Files:
  llvm/lib/CodeGen/TypePromotion.cpp
  llvm/test/Transforms/TypePromotion/ARM/casts.ll


Index: llvm/test/Transforms/TypePromotion/ARM/casts.ll
===================================================================
--- llvm/test/Transforms/TypePromotion/ARM/casts.ll
+++ llvm/test/Transforms/TypePromotion/ARM/casts.ll
@@ -1116,8 +1116,7 @@
 ; CHECK-NEXT:    [[TMP5:%.*]] = trunc i32 [[OR]] to i8
 ; CHECK-NEXT:    store i8 [[TMP5]], i8* [[B]], align 1
 ; CHECK-NEXT:    [[TMP6:%.*]] = trunc i32 [[OR]] to i8
-; CHECK-NEXT:    [[CONV5:%.*]] = zext i8 [[TMP6]] to i32
-; CHECK-NEXT:    ret i32 [[CONV5]]
+; CHECK-NEXT:    ret i32 [[OR]]
 ;
 entry:
   %0 = load i16, i16* %a, align 2
@@ -1143,10 +1142,9 @@
 ; CHECK-NEXT:    [[TMP3:%.*]] = and i32 [[TMP2]], 255
 ; CHECK-NEXT:    [[TMP4:%.*]] = udiv i32 [[TMP3]], 3
 ; CHECK-NEXT:    [[TMP5:%.*]] = trunc i32 [[TMP4]] to i8
-; CHECK-NEXT:    [[PHITMP:%.*]] = zext i8 [[TMP5]] to i32
 ; CHECK-NEXT:    br label [[COND_END]]
 ; CHECK:       cond.end:
-; CHECK-NEXT:    [[COND:%.*]] = phi i32 [ [[PHITMP]], [[COND_FALSE]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[COND:%.*]] = phi i32 [ [[TMP4]], [[COND_FALSE]] ], [ 0, [[ENTRY:%.*]] ]
 ; CHECK-NEXT:    ret i32 [[COND]]
 ;
 entry:
Index: llvm/lib/CodeGen/TypePromotion.cpp
===================================================================
--- llvm/lib/CodeGen/TypePromotion.cpp
+++ llvm/lib/CodeGen/TypePromotion.cpp
@@ -103,7 +103,6 @@
 namespace {
 class IRPromoter {
   LLVMContext &Ctx;
-  IntegerType *OrigTy = nullptr;
   unsigned PromotedWidth = 0;
   SetVector<Value*> &Visited;
   SetVector<Value*> &Sources;
@@ -123,16 +122,13 @@
   void Cleanup();
 
 public:
-  IRPromoter(LLVMContext &C, IntegerType *Ty, unsigned Width,
+  IRPromoter(LLVMContext &C, unsigned Width,
              SetVector<Value *> &visited, SetVector<Value *> &sources,
              SetVector<Instruction *> &sinks,
              SmallPtrSetImpl<Instruction *> &wrap)
-      : Ctx(C), OrigTy(Ty), PromotedWidth(Width), Visited(visited),
+      : Ctx(C), PromotedWidth(Width), Visited(visited),
         Sources(sources), Sinks(sinks), SafeWrap(wrap) {
     ExtTy = IntegerType::get(Ctx, PromotedWidth);
-    assert(OrigTy->getPrimitiveSizeInBits().getFixedSize() <
-               ExtTy->getPrimitiveSizeInBits().getFixedSize() &&
-           "Original type not smaller than extended type");
   }
 
   void Mutate();
@@ -586,11 +582,9 @@
       continue;
     }
 
-    // Unless they produce a value that is narrower than ExtTy, we can
-    // replace the result of the zext with the input of a newly inserted
-    // trunc.
-    if (NewInsts.count(Src) && isa<TruncInst>(Src) &&
-        Src->getType() == OrigTy) {
+    // We've inserted a trunc for a zext sink, but we already know that the
+    // input is in range, negating the need for the trunc.
+    if (NewInsts.count(Src) && isa<TruncInst>(Src)) {
       auto *Trunc = cast<TruncInst>(Src);
       assert(Trunc->getOperand(0)->getType() == ExtTy &&
              "expected inserted trunc to be operating on i32");
@@ -631,9 +625,6 @@
 }
 
 void IRPromoter::Mutate() {
-  LLVM_DEBUG(dbgs() << "IR Promotion: Promoting use-def chains from "
-             << OrigTy->getBitWidth() << " to " << PromotedWidth << "-bits\n");
-
   // Cache original types of the values that will likely need truncating
   for (auto *I : Sinks) {
     if (auto *Call = dyn_cast<CallInst>(I)) {
@@ -873,8 +864,11 @@
   if (ToPromote < 2 || (Blocks.size() == 1 && (NonFreeArgs > SafeWrap.size())))
     return false;
 
-  IRPromoter Promoter(*Ctx, cast<IntegerType>(OrigTy), PromotedWidth,
-                      CurrentVisited, Sources, Sinks, SafeWrap);
+  IRPromoter Promoter(*Ctx, PromotedWidth, CurrentVisited, Sources, Sinks,
+                      SafeWrap);
+  LLVM_DEBUG(dbgs() << "Promoting use-def chains from "
+             << cast<IntegerType>(OrigTy)->getBitWidth()
+             << " to " << PromotedWidth << "-bits\n");
   Promoter.Mutate();
   return true;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118905.405628.patch
Type: text/x-patch
Size: 3903 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220203/62868604/attachment.bin>


More information about the llvm-commits mailing list