[PATCH] D126644: [llvm/CodeGen] Add ExpandLargeDivRem pass

Matthias Gehre via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 30 05:45:53 PDT 2022


mgehre-amd created this revision.
mgehre-amd added reviewers: efriedma, FreddyYe, MaskRay.
Herald added subscribers: StephenFan, pengfei, hiraditya, mgorny.
Herald added a project: All.
mgehre-amd requested review of this revision.
Herald added a project: LLVM.

Adds a pass ExpandLargeDivRem to expand div/rem instructions
with more than 128 bits into calls to auto-generated functions.

For example, urem i129 is expanded into a call to __llvm_urem129,
and the body of __llvm_urem129 is automatically generated to implement
a simple shift-subtract algorithm:

  ; Function Attrs: norecurse nounwind readnone speculatable willreturn
  define internal i129 @__llvm_urem129(i129 %0, i129 %1) #0 {
  entry:
    br label %loop
  
  loop:                                             ; preds = %if.end, %entry
    %i = phi i32 [ 128, %entry ], [ %new_i, %if.end ]
    %r = phi i129 [ 0, %entry ], [ %r3, %if.end ]
    %iext = zext i32 %i to i129
    %2 = lshr i129 %0, %iext
    %3 = trunc i129 %2 to i1
    %new_r = shl i129 %r, 1
    %4 = zext i1 %3 to i129
    %new_r1 = or i129 %new_r, %4
    %loop_exit_cond = icmp eq i32 %i, 0
    %new_i = add i32 %i, -1
    %5 = icmp uge i129 %new_r1, %1
    br i1 %5, label %then, label %if.end
  
  then:                                             ; preds = %loop
    %new_r2 = sub i129 %new_r1, %1
    br label %if.end
  
  if.end:                                           ; preds = %then, %loop
    %r3 = phi i129 [ %new_r2, %then ], [ %new_r1, %loop ]
    br i1 %loop_exit_cond, label %exit, label %loop
  
  exit:                                             ; preds = %if.end
    ret i129 %r3
  }

As discussed on https://reviews.llvm.org/D120327, this approach has the advantage
that it is independent of the runtime library. This also helps the clang driver,
which otherwise would need to understand enough about the runtime library
to know whether to allow _BitInts with more than 128 bits.

Targets are still free to disable this pass and instead provide a faster
implementation in a runtime library.

Fixes https://github.com/llvm/llvm-project/issues/44994


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126644

Files:
  llvm/include/llvm/CodeGen/ExpandLargeDivRem.h
  llvm/include/llvm/CodeGen/MachinePassRegistry.def
  llvm/include/llvm/CodeGen/Passes.h
  llvm/include/llvm/InitializePasses.h
  llvm/include/llvm/LinkAllPasses.h
  llvm/lib/CodeGen/CMakeLists.txt
  llvm/lib/CodeGen/CodeGen.cpp
  llvm/lib/CodeGen/ExpandLargeDivRem.cpp
  llvm/lib/CodeGen/TargetPassConfig.cpp
  llvm/test/CodeGen/AArch64/O0-pipeline.ll
  llvm/test/CodeGen/AArch64/O3-pipeline.ll
  llvm/test/CodeGen/AArch64/udivmodei5.ll
  llvm/test/CodeGen/ARM/O3-pipeline.ll
  llvm/test/CodeGen/X86/O0-pipeline.ll
  llvm/test/CodeGen/X86/opt-pipeline.ll
  llvm/test/CodeGen/X86/udivmodei5.ll
  llvm/test/CodeGen/X86/urem-seteq.ll
  llvm/test/Transforms/ExpandLargeDivRem/sdiv129.ll
  llvm/test/Transforms/ExpandLargeDivRem/srem129.ll
  llvm/test/Transforms/ExpandLargeDivRem/udiv129.ll
  llvm/test/Transforms/ExpandLargeDivRem/urem129.ll
  llvm/test/Transforms/ExpandLargeDivRem/values129.ll
  llvm/tools/opt/opt.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126644.432895.patch
Type: text/x-patch
Size: 69272 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220530/52120244/attachment.bin>


More information about the llvm-commits mailing list