[PATCH] D90577: [WebAssembly] Don't fold frame offset for global addresses

Heejin Ahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 1 17:35:55 PST 2020


aheejin created this revision.
aheejin added reviewers: sunfish, dschuff.
Herald added subscribers: llvm-commits, wingo, ecnelises, hiraditya, jgravelle-google, sbc100.
Herald added a project: LLVM.
aheejin requested review of this revision.

When machine instructions are in the form of

  %0 = CONST_I32 @str
  %1 = ADD_I32 %stack.0, %0
  %2 = LOAD 0, 0, %1

In the `ADD_I32` instruction, it is possible to fold it if %0 is a
`CONST_I32` from an immediate number. But in this case it is a global
address, so we shouldn't do that. But we haven't checked if the operand
of `ADD` is an immediate so far. This fixes the problem. (The case
applies the same for `ADD_I64` and `CONST_I64` instructions.)

Patch by Julien Jorge (jjorge at quarkslab.com)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90577

Files:
  llvm/lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp
  llvm/test/CodeGen/WebAssembly/userstack.ll


Index: llvm/test/CodeGen/WebAssembly/userstack.ll
===================================================================
--- llvm/test/CodeGen/WebAssembly/userstack.ll
+++ llvm/test/CodeGen/WebAssembly/userstack.ll
@@ -328,6 +328,22 @@
   ret void
 }
 
+; We optimize the format of "frame offset + operand" by folding it, but
+; this is only possible when that operand is an immediate. In this example it is
+; a global address, so we should not fold it.
+; CHECK-LABEL: frame_offset_with_global_address
+; CHECK: i[[PTR]].const ${{.*}}=, str
+ at str = local_unnamed_addr global [3 x i8] c"abc", align 16
+define i8 @frame_offset_with_global_address() {
+  %1 = alloca i8, align 4
+  %2 = ptrtoint i8* %1 to i32
+  ;; Here @str is a global address and not an immediate, so cannot be folded
+  %3 = getelementptr [3 x i8], [3 x i8]* @str, i32 0, i32 %2
+  %4 = load i8, i8* %3, align 8
+  %5 = and i8 %4, 67
+  ret i8 %5
+}
+
 ; CHECK: .globaltype	__stack_pointer, i[[PTR]]{{$}}
 
 ; TODO: test over-aligned alloca
Index: llvm/lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp
===================================================================
--- llvm/lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp
+++ llvm/lib/Target/WebAssembly/WebAssemblyRegisterInfo.cpp
@@ -101,10 +101,12 @@
               WebAssemblyFrameLowering::getOpcConst(MF) &&
             MRI.hasOneNonDBGUse(Def->getOperand(0).getReg())) {
           MachineOperand &ImmMO = Def->getOperand(1);
-          ImmMO.setImm(ImmMO.getImm() + uint32_t(FrameOffset));
-          MI.getOperand(FIOperandNum)
-              .ChangeToRegister(FrameRegister, /*isDef=*/false);
-          return;
+          if (ImmMO.isImm()) {
+            ImmMO.setImm(ImmMO.getImm() + uint32_t(FrameOffset));
+            MI.getOperand(FIOperandNum)
+                .ChangeToRegister(FrameRegister, /*isDef=*/false);
+            return;
+          }
         }
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D90577.302194.patch
Type: text/x-patch
Size: 1920 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201102/ade7515a/attachment.bin>


More information about the llvm-commits mailing list