[llvm] f292d53 - [LoongArch] Fix undefined behavior: left shift of negative value

via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 11 05:18:50 PST 2023


Author: wanglei
Date: 2023-01-11T21:16:38+08:00
New Revision: f292d53c70f96ca2beee448fb9bf2debaf0134a0

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

LOG: [LoongArch] Fix undefined behavior: left shift of negative value

Fix undefined behavior in `decodeSImmOperand` where we were left shifting
a signed value.

Added: 
    

Modified: 
    llvm/lib/Target/LoongArch/Disassembler/LoongArchDisassembler.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/LoongArch/Disassembler/LoongArchDisassembler.cpp b/llvm/lib/Target/LoongArch/Disassembler/LoongArchDisassembler.cpp
index beb757c785969..2335152e5ab17 100644
--- a/llvm/lib/Target/LoongArch/Disassembler/LoongArchDisassembler.cpp
+++ b/llvm/lib/Target/LoongArch/Disassembler/LoongArchDisassembler.cpp
@@ -114,9 +114,9 @@ static DecodeStatus decodeSImmOperand(MCInst &Inst, uint64_t Imm,
                                       int64_t Address,
                                       const MCDisassembler *Decoder) {
   assert(isUInt<N>(Imm) && "Invalid immediate");
-  // Sign-extend the number in the bottom <N> bits of Imm, then shift left <S>
+  // Shift left Imm <S> bits, then sign-extend the number in the bottom <N+S>
   // bits.
-  Inst.addOperand(MCOperand::createImm(SignExtend64<N>(Imm) << S));
+  Inst.addOperand(MCOperand::createImm(SignExtend64<N + S>(Imm << S)));
   return MCDisassembler::Success;
 }
 


        


More information about the llvm-commits mailing list