[llvm] 0f1494b - AArch64: avoid UB shift of negative value

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 27 05:50:01 PDT 2020


Author: Tim Northover
Date: 2020-07-27T13:49:50+01:00
New Revision: 0f1494be43f0916516533fea9d99e9211bb4c581

URL: https://github.com/llvm/llvm-project/commit/0f1494be43f0916516533fea9d99e9211bb4c581
DIFF: https://github.com/llvm/llvm-project/commit/0f1494be43f0916516533fea9d99e9211bb4c581.diff

LOG: AArch64: avoid UB shift of negative value

Left shifting a negative value is undefined behaviour, so this just moves the
negation afterwards to avoid it.

Added: 
    

Modified: 
    llvm/lib/Target/AArch64/AArch64InstrInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index 08f80c9aa361..323ac76e903f 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -6847,10 +6847,9 @@ Optional<RegImmPair> AArch64InstrInfo::isAddImmediate(const MachineInstr &MI,
     if (!MI.getOperand(0).isReg() || !MI.getOperand(1).isReg() ||
         !MI.getOperand(2).isImm())
       return None;
-    Offset = MI.getOperand(2).getImm() * Sign;
     int Shift = MI.getOperand(3).getImm();
     assert((Shift == 0 || Shift == 12) && "Shift can be either 0 or 12");
-    Offset = Offset << Shift;
+    Offset = Sign * (MI.getOperand(2).getImm() << Shift);
   }
   }
   return RegImmPair{MI.getOperand(1).getReg(), Offset};


        


More information about the llvm-commits mailing list