[PATCH] D111237: [RFC] TypePromotion: Promote PHI-nodes

Andre Vieira via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 6 09:06:25 PDT 2021


avieira created this revision.
Herald added subscribers: jeroen.dobbelaere, kosarev, hiraditya, kristof.beyls.
avieira requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Hi all,

This is a RFC to teach TypePromotion to promote PHI-nodes with a single use by a ZExt or SExt, where the incoming values are either constants or loads where the target can extend the loaded value for free.

See below a motivating example:

#include <stdint.h>
int foo(uint8_t* p) {

  uint16_t index = *p;
  do {
      index = p[index];
  } while (index < 100);
  return index;

}

For aarch64 this would end up with the following IR:

define dso_local i32 @_Z3fooPh(i8* nocapture readonly %p) local_unnamed_addr #0 {
entry:

  %0 = load i8, i8* %p, align 1, !tbaa !8
  br label %do.body

do.body:                                          ; preds = %do.body, %entry

  %index.0.in = phi i8 [ %0, %entry ], [ %1, %do.body ]
  %idxprom = zext i8 %index.0.in to i64
  %arrayidx = getelementptr inbounds i8, i8* %p, i64 %idxprom
  %1 = load i8, i8* %arrayidx, align 1, !tbaa !8
  %cmp = icmp ult i8 %1, 100
  br i1 %cmp, label %do.body, label %do.end, !llvm.loop !11

do.end:                                           ; preds = %do.body

  %conv2 = zext i8 %1 to i32
  ret i32 %conv2

}

Which in turn leads to an unnecessary 'and' at the start of the loop. The goal here is to turn that PHI-node to an i64 and push the zext to the loads.

Let me know what you think of this approach and whether there might be more appropriate ways to ensure we get a better codegen for issues like this.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D111237

Files:
  llvm/lib/CodeGen/TypePromotion.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111237.377565.patch
Type: text/x-patch
Size: 8302 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211006/4e9ebf37/attachment.bin>


More information about the llvm-commits mailing list