[PATCH] D24938: [x86] Rewrite getAddressFromInstr helper function

Alexander Ivchenko via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 26 13:50:32 PDT 2016


aivchenk created this revision.
aivchenk added a subscriber: llvm-commits.

        - So it does not modify the input instruction
        - Second operand of any address is always an Index Register,
        make sure we actually check for that, instead of a check for
        an immediate value
        - Add two asserts along the way

https://reviews.llvm.org/D24938

Files:
  lib/Target/X86/X86InstrBuilder.h

Index: lib/Target/X86/X86InstrBuilder.h
===================================================================
--- lib/Target/X86/X86InstrBuilder.h
+++ lib/Target/X86/X86InstrBuilder.h
@@ -90,28 +90,31 @@
 
 /// Compute the addressing mode from an machine instruction starting with the
 /// given operand.
-static inline X86AddressMode getAddressFromInstr(MachineInstr *MI,
+static inline X86AddressMode getAddressFromInstr(const MachineInstr *MI,
                                                  unsigned Operand) {
   X86AddressMode AM;
-  MachineOperand &Op = MI->getOperand(Operand);
-  if (Op.isReg()) {
+  const MachineOperand &Op0 = MI->getOperand(Operand);
+  if (Op0.isReg()) {
     AM.BaseType = X86AddressMode::RegBase;
-    AM.Base.Reg = Op.getReg();
+    AM.Base.Reg = Op0.getReg();
   } else {
     AM.BaseType = X86AddressMode::FrameIndexBase;
-    AM.Base.FrameIndex = Op.getIndex();
+    AM.Base.FrameIndex = Op0.getIndex();
   }
-  Op = MI->getOperand(Operand + 1);
-  if (Op.isImm())
-    AM.Scale = Op.getImm();
-  Op = MI->getOperand(Operand + 2);
-  if (Op.isImm())
-    AM.IndexReg = Op.getImm();
-  Op = MI->getOperand(Operand + 3);
-  if (Op.isGlobal()) {
-    AM.GV = Op.getGlobal();
+
+  const MachineOperand &Op1 = MI->getOperand(Operand + 1);
+  assert(Op1.isImm() && "Second address operand must be an immediate value");
+  AM.Scale = Op1.getImm();
+
+  const MachineOperand &Op2 = MI->getOperand(Operand + 2);
+  assert(Op2.isReg() && "Third address operand must be a register");
+  AM.IndexReg = Op2.getReg();
+
+  const MachineOperand &Op3 = MI->getOperand(Operand + 3);
+  if (Op3.isGlobal()) {
+    AM.GV = Op3.getGlobal();
   } else {
-    AM.Disp = Op.getImm();
+    AM.Disp = Op3.getImm();
   }
   return AM;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24938.72557.patch
Type: text/x-patch
Size: 1748 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160926/0f8888a5/attachment.bin>


More information about the llvm-commits mailing list