[PATCH] D41738: [GISel]: Don't create G_MUL with 1 during translation of GetElementPtr

Aditya Nandakumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 4 12:44:20 PST 2018


aditya_nandakumar created this revision.
aditya_nandakumar added reviewers: qcolombet, volkan, dsanders, aemerson, t.p.northover, ab.
Herald added a subscriber: javed.absar.

When element size is 1, it's just wasteful to create MUL with 1.


Repository:
  rL LLVM

https://reviews.llvm.org/D41738

Files:
  lib/CodeGen/GlobalISel/IRTranslator.cpp
  test/CodeGen/AArch64/GlobalISel/translate-gep.ll


Index: test/CodeGen/AArch64/GlobalISel/translate-gep.ll
===================================================================
--- test/CodeGen/AArch64/GlobalISel/translate-gep.ll
+++ test/CodeGen/AArch64/GlobalISel/translate-gep.ll
@@ -2,6 +2,15 @@
 
 %type = type [4 x {i8, i32}]
 
+define i8*  @translate_element_size1(i64 %arg) {
+; CHECK-LABEL: name: translate_element_size1
+; CHECK: [[OFFSET:%[0-9]+]]:_(s64) = COPY %x0
+; CHECK: [[BASE:%[0-9]+]]:_(p0) = G_CONSTANT i64 0
+; CHECK: [[GEP:%[0-9]+]]:_(p0) = G_GEP [[BASE]], [[OFFSET]]
+  %tmp = getelementptr i8, i8* null, i64 %arg
+  ret i8* %tmp
+}
+
 define %type* @first_offset_const(%type* %addr) {
 ; CHECK-LABEL: name: first_offset_const
 ; CHECK: [[BASE:%[0-9]+]]:_(p0) = COPY %x0
Index: lib/CodeGen/GlobalISel/IRTranslator.cpp
===================================================================
--- lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -516,22 +516,27 @@
         Offset = 0;
       }
 
-      // N = N + Idx * ElementSize;
-      unsigned ElementSizeReg =
-          getOrCreateVReg(*ConstantInt::get(OffsetIRTy, ElementSize));
-
       unsigned IdxReg = getOrCreateVReg(*Idx);
       if (MRI->getType(IdxReg) != OffsetTy) {
         unsigned NewIdxReg = MRI->createGenericVirtualRegister(OffsetTy);
         MIRBuilder.buildSExtOrTrunc(NewIdxReg, IdxReg);
         IdxReg = NewIdxReg;
       }
 
-      unsigned OffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
-      MIRBuilder.buildMul(OffsetReg, ElementSizeReg, IdxReg);
+      // N = N + Idx * ElementSize;
+      // Avoid doing it for ElementSize of 1.
+      unsigned GepOffsetReg;
+      if (ElementSize != 1) {
+        unsigned ElementSizeReg =
+            getOrCreateVReg(*ConstantInt::get(OffsetIRTy, ElementSize));
+
+        GepOffsetReg = MRI->createGenericVirtualRegister(OffsetTy);
+        MIRBuilder.buildMul(GepOffsetReg, ElementSizeReg, IdxReg);
+      } else
+        GepOffsetReg = IdxReg;
 
       unsigned NewBaseReg = MRI->createGenericVirtualRegister(PtrTy);
-      MIRBuilder.buildGEP(NewBaseReg, BaseReg, OffsetReg);
+      MIRBuilder.buildGEP(NewBaseReg, BaseReg, GepOffsetReg);
       BaseReg = NewBaseReg;
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41738.128638.patch
Type: text/x-patch
Size: 2221 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180104/26b19ecf/attachment.bin>


More information about the llvm-commits mailing list