From ed3f06b9b393cd51e78e5fbc7a46bce090c1817a Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 30 Oct 2023 09:04:04 +0100 Subject: [IR] Add zext nneg flag (#67982) Add an nneg flag to the zext instruction, which specifies that the argument is non-negative. Otherwise, the result is a poison value. The primary use-case for the flag is to preserve information when sext gets replaced with zext due to range-based canonicalization. The nneg flag allows us to convert the zext back into an sext later. This is useful for some optimizations (e.g. a signed icmp can fold with sext but not zext), as well as some targets (e.g. RISCV prefers sext over zext). Discourse thread: https://discourse.llvm.org/t/rfc-add-zext-nneg-flag/73914 This patch is based on https://reviews.llvm.org/D156444 by @Panagiotis156, with some implementation simplifications and additional tests. --------- Co-authored-by: Panagiotis K --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'llvm/lib/Bitcode/Reader/BitcodeReader.cpp') diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 28addb9..fcba736 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -4877,12 +4877,13 @@ Error BitcodeReader::parseFunctionBody(Function *F) { Value *Op; unsigned OpTypeID; if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB) || - OpNum+2 != Record.size()) + OpNum + 1 > Record.size()) return error("Invalid record"); - ResTypeID = Record[OpNum]; + ResTypeID = Record[OpNum++]; Type *ResTy = getTypeByID(ResTypeID); - int Opc = getDecodedCastOpcode(Record[OpNum + 1]); + int Opc = getDecodedCastOpcode(Record[OpNum++]); + if (Opc == -1 || !ResTy) return error("Invalid record"); Instruction *Temp = nullptr; @@ -4898,6 +4899,9 @@ Error BitcodeReader::parseFunctionBody(Function *F) { return error("Invalid cast"); I = CastInst::Create(CastOp, Op, ResTy); } + if (OpNum < Record.size() && isa(I) && + (Record[OpNum] & (1 << bitc::PNNI_NON_NEG))) + I->setNonNeg(true); InstructionList.push_back(I); break; } -- cgit v1.1