diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -69,7 +69,6 @@ /* Standard Unary Operators */ LLVMFNeg = 66, - LLVMFreeze = 68, /* Standard Binary Operators */ LLVMAdd = 8, @@ -128,6 +127,7 @@ LLVMShuffleVector = 52, LLVMExtractValue = 53, LLVMInsertValue = 54, + LLVMFreeze = 68, /* Atomic operators */ LLVMFence = 55, @@ -1601,6 +1601,7 @@ macro(ExtractValueInst) \ macro(LoadInst) \ macro(VAArgInst) \ + macro(FreezeInst) \ macro(AtomicCmpXchgInst) \ macro(AtomicRMWInst) \ macro(FenceInst) @@ -3748,7 +3749,6 @@ const char *Name); LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef, LLVMValueRef V, const char *Name); LLVMValueRef LLVMBuildNot(LLVMBuilderRef, LLVMValueRef V, const char *Name); -LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef, LLVMValueRef V, const char *Name); /* Memory */ LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name); @@ -3909,6 +3909,8 @@ LLVMValueRef LLVMBuildInsertValue(LLVMBuilderRef, LLVMValueRef AggVal, LLVMValueRef EltVal, unsigned Index, const char *Name); +LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef, LLVMValueRef Val, + const char *Name); LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef, LLVMValueRef Val, const char *Name); diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h --- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h +++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h @@ -391,8 +391,7 @@ /// have no fixed relation to the LLVM IR enum values. Changing these will /// break compatibility with old files. enum UnaryOpcodes { - UNOP_FNEG = 0, - UNOP_FREEZE = 1 + UNOP_FNEG = 0 }; /// BinaryOpcodes - These are values used in the bitcode files to encode which @@ -560,6 +559,7 @@ FUNC_CODE_INST_UNOP = 56, // UNOP: [opcode, ty, opval] FUNC_CODE_INST_CALLBR = 57, // CALLBR: [attr, cc, norm, transfs, // fnty, fnid, args...] + FUNC_CODE_INST_FREEZE = 58, // FREEZE: [opty, opval] }; enum UseListCodes { diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h --- a/llvm/include/llvm/IR/IRBuilder.h +++ b/llvm/include/llvm/IR/IRBuilder.h @@ -2393,7 +2393,7 @@ } Value *CreateFreeze(Value *V, const Twine &Name = "") { - return Insert(UnaryOperator::CreateFreeze(V, Name)); + return Insert(new FreezeInst(V), Name); } //===--------------------------------------------------------------------===// diff --git a/llvm/include/llvm/IR/InstVisitor.h b/llvm/include/llvm/IR/InstVisitor.h --- a/llvm/include/llvm/IR/InstVisitor.h +++ b/llvm/include/llvm/IR/InstVisitor.h @@ -199,6 +199,7 @@ RetTy visitFuncletPadInst(FuncletPadInst &I) { DELEGATE(Instruction); } RetTy visitCleanupPadInst(CleanupPadInst &I) { DELEGATE(FuncletPadInst); } RetTy visitCatchPadInst(CatchPadInst &I) { DELEGATE(FuncletPadInst); } + RetTy visitFreezeInst(FreezeInst &I) { DELEGATE(Instruction); } // Handle the special instrinsic instruction classes. RetTy visitDbgDeclareInst(DbgDeclareInst &I) { DELEGATE(DbgVariableIntrinsic);} diff --git a/llvm/include/llvm/IR/Instruction.def b/llvm/include/llvm/IR/Instruction.def --- a/llvm/include/llvm/IR/Instruction.def +++ b/llvm/include/llvm/IR/Instruction.def @@ -140,84 +140,84 @@ // Standard unary operators... FIRST_UNARY_INST(12) HANDLE_UNARY_INST(12, FNeg , UnaryOperator) -HANDLE_UNARY_INST(13, Freeze, UnaryOperator) - LAST_UNARY_INST(13) + LAST_UNARY_INST(12) // Standard binary operators... - FIRST_BINARY_INST(14) -HANDLE_BINARY_INST(14, Add , BinaryOperator) -HANDLE_BINARY_INST(15, FAdd , BinaryOperator) -HANDLE_BINARY_INST(16, Sub , BinaryOperator) -HANDLE_BINARY_INST(17, FSub , BinaryOperator) -HANDLE_BINARY_INST(18, Mul , BinaryOperator) -HANDLE_BINARY_INST(19, FMul , BinaryOperator) -HANDLE_BINARY_INST(20, UDiv , BinaryOperator) -HANDLE_BINARY_INST(21, SDiv , BinaryOperator) -HANDLE_BINARY_INST(22, FDiv , BinaryOperator) -HANDLE_BINARY_INST(23, URem , BinaryOperator) -HANDLE_BINARY_INST(24, SRem , BinaryOperator) -HANDLE_BINARY_INST(25, FRem , BinaryOperator) + FIRST_BINARY_INST(13) +HANDLE_BINARY_INST(13, Add , BinaryOperator) +HANDLE_BINARY_INST(14, FAdd , BinaryOperator) +HANDLE_BINARY_INST(15, Sub , BinaryOperator) +HANDLE_BINARY_INST(16, FSub , BinaryOperator) +HANDLE_BINARY_INST(17, Mul , BinaryOperator) +HANDLE_BINARY_INST(18, FMul , BinaryOperator) +HANDLE_BINARY_INST(19, UDiv , BinaryOperator) +HANDLE_BINARY_INST(20, SDiv , BinaryOperator) +HANDLE_BINARY_INST(21, FDiv , BinaryOperator) +HANDLE_BINARY_INST(22, URem , BinaryOperator) +HANDLE_BINARY_INST(23, SRem , BinaryOperator) +HANDLE_BINARY_INST(24, FRem , BinaryOperator) // Logical operators (integer operands) -HANDLE_BINARY_INST(26, Shl , BinaryOperator) // Shift left (logical) -HANDLE_BINARY_INST(27, LShr , BinaryOperator) // Shift right (logical) -HANDLE_BINARY_INST(28, AShr , BinaryOperator) // Shift right (arithmetic) -HANDLE_BINARY_INST(29, And , BinaryOperator) -HANDLE_BINARY_INST(30, Or , BinaryOperator) -HANDLE_BINARY_INST(31, Xor , BinaryOperator) - LAST_BINARY_INST(31) +HANDLE_BINARY_INST(25, Shl , BinaryOperator) // Shift left (logical) +HANDLE_BINARY_INST(26, LShr , BinaryOperator) // Shift right (logical) +HANDLE_BINARY_INST(27, AShr , BinaryOperator) // Shift right (arithmetic) +HANDLE_BINARY_INST(28, And , BinaryOperator) +HANDLE_BINARY_INST(29, Or , BinaryOperator) +HANDLE_BINARY_INST(30, Xor , BinaryOperator) + LAST_BINARY_INST(30) // Memory operators... - FIRST_MEMORY_INST(32) -HANDLE_MEMORY_INST(32, Alloca, AllocaInst) // Stack management -HANDLE_MEMORY_INST(33, Load , LoadInst ) // Memory manipulation instrs -HANDLE_MEMORY_INST(34, Store , StoreInst ) -HANDLE_MEMORY_INST(35, GetElementPtr, GetElementPtrInst) -HANDLE_MEMORY_INST(36, Fence , FenceInst ) -HANDLE_MEMORY_INST(37, AtomicCmpXchg , AtomicCmpXchgInst ) -HANDLE_MEMORY_INST(38, AtomicRMW , AtomicRMWInst ) - LAST_MEMORY_INST(38) + FIRST_MEMORY_INST(31) +HANDLE_MEMORY_INST(31, Alloca, AllocaInst) // Stack management +HANDLE_MEMORY_INST(32, Load , LoadInst ) // Memory manipulation instrs +HANDLE_MEMORY_INST(33, Store , StoreInst ) +HANDLE_MEMORY_INST(34, GetElementPtr, GetElementPtrInst) +HANDLE_MEMORY_INST(35, Fence , FenceInst ) +HANDLE_MEMORY_INST(36, AtomicCmpXchg , AtomicCmpXchgInst ) +HANDLE_MEMORY_INST(37, AtomicRMW , AtomicRMWInst ) + LAST_MEMORY_INST(37) // Cast operators ... // NOTE: The order matters here because CastInst::isEliminableCastPair // NOTE: (see Instructions.cpp) encodes a table based on this ordering. - FIRST_CAST_INST(39) -HANDLE_CAST_INST(39, Trunc , TruncInst ) // Truncate integers -HANDLE_CAST_INST(40, ZExt , ZExtInst ) // Zero extend integers -HANDLE_CAST_INST(41, SExt , SExtInst ) // Sign extend integers -HANDLE_CAST_INST(42, FPToUI , FPToUIInst ) // floating point -> UInt -HANDLE_CAST_INST(43, FPToSI , FPToSIInst ) // floating point -> SInt -HANDLE_CAST_INST(44, UIToFP , UIToFPInst ) // UInt -> floating point -HANDLE_CAST_INST(45, SIToFP , SIToFPInst ) // SInt -> floating point -HANDLE_CAST_INST(46, FPTrunc , FPTruncInst ) // Truncate floating point -HANDLE_CAST_INST(47, FPExt , FPExtInst ) // Extend floating point -HANDLE_CAST_INST(48, PtrToInt, PtrToIntInst) // Pointer -> Integer -HANDLE_CAST_INST(49, IntToPtr, IntToPtrInst) // Integer -> Pointer -HANDLE_CAST_INST(50, BitCast , BitCastInst ) // Type cast -HANDLE_CAST_INST(51, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast - LAST_CAST_INST(51) - - FIRST_FUNCLETPAD_INST(52) -HANDLE_FUNCLETPAD_INST(52, CleanupPad, CleanupPadInst) -HANDLE_FUNCLETPAD_INST(53, CatchPad , CatchPadInst) - LAST_FUNCLETPAD_INST(53) + FIRST_CAST_INST(38) +HANDLE_CAST_INST(38, Trunc , TruncInst ) // Truncate integers +HANDLE_CAST_INST(39, ZExt , ZExtInst ) // Zero extend integers +HANDLE_CAST_INST(40, SExt , SExtInst ) // Sign extend integers +HANDLE_CAST_INST(41, FPToUI , FPToUIInst ) // floating point -> UInt +HANDLE_CAST_INST(42, FPToSI , FPToSIInst ) // floating point -> SInt +HANDLE_CAST_INST(43, UIToFP , UIToFPInst ) // UInt -> floating point +HANDLE_CAST_INST(44, SIToFP , SIToFPInst ) // SInt -> floating point +HANDLE_CAST_INST(45, FPTrunc , FPTruncInst ) // Truncate floating point +HANDLE_CAST_INST(46, FPExt , FPExtInst ) // Extend floating point +HANDLE_CAST_INST(47, PtrToInt, PtrToIntInst) // Pointer -> Integer +HANDLE_CAST_INST(48, IntToPtr, IntToPtrInst) // Integer -> Pointer +HANDLE_CAST_INST(49, BitCast , BitCastInst ) // Type cast +HANDLE_CAST_INST(50, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast + LAST_CAST_INST(50) + + FIRST_FUNCLETPAD_INST(51) +HANDLE_FUNCLETPAD_INST(51, CleanupPad, CleanupPadInst) +HANDLE_FUNCLETPAD_INST(52, CatchPad , CatchPadInst) + LAST_FUNCLETPAD_INST(52) // Other operators... - FIRST_OTHER_INST(54) -HANDLE_OTHER_INST(54, ICmp , ICmpInst ) // Integer comparison instruction -HANDLE_OTHER_INST(55, FCmp , FCmpInst ) // Floating point comparison instr. -HANDLE_OTHER_INST(56, PHI , PHINode ) // PHI node instruction -HANDLE_OTHER_INST(57, Call , CallInst ) // Call a function -HANDLE_OTHER_INST(58, Select , SelectInst ) // select instruction -HANDLE_USER_INST (59, UserOp1, Instruction) // May be used internally in a pass -HANDLE_USER_INST (60, UserOp2, Instruction) // Internal to passes only -HANDLE_OTHER_INST(61, VAArg , VAArgInst ) // vaarg instruction -HANDLE_OTHER_INST(62, ExtractElement, ExtractElementInst)// extract from vector -HANDLE_OTHER_INST(63, InsertElement, InsertElementInst) // insert into vector -HANDLE_OTHER_INST(64, ShuffleVector, ShuffleVectorInst) // shuffle two vectors. -HANDLE_OTHER_INST(65, ExtractValue, ExtractValueInst)// extract from aggregate -HANDLE_OTHER_INST(66, InsertValue, InsertValueInst) // insert into aggregate -HANDLE_OTHER_INST(67, LandingPad, LandingPadInst) // Landing pad instruction. + FIRST_OTHER_INST(53) +HANDLE_OTHER_INST(53, ICmp , ICmpInst ) // Integer comparison instruction +HANDLE_OTHER_INST(54, FCmp , FCmpInst ) // Floating point comparison instr. +HANDLE_OTHER_INST(55, PHI , PHINode ) // PHI node instruction +HANDLE_OTHER_INST(56, Call , CallInst ) // Call a function +HANDLE_OTHER_INST(57, Select , SelectInst ) // select instruction +HANDLE_USER_INST (58, UserOp1, Instruction) // May be used internally in a pass +HANDLE_USER_INST (59, UserOp2, Instruction) // Internal to passes only +HANDLE_OTHER_INST(60, VAArg , VAArgInst ) // vaarg instruction +HANDLE_OTHER_INST(61, ExtractElement, ExtractElementInst)// extract from vector +HANDLE_OTHER_INST(62, InsertElement, InsertElementInst) // insert into vector +HANDLE_OTHER_INST(63, ShuffleVector, ShuffleVectorInst) // shuffle two vectors. +HANDLE_OTHER_INST(64, ExtractValue, ExtractValueInst)// extract from aggregate +HANDLE_OTHER_INST(65, InsertValue, InsertValueInst) // insert into aggregate +HANDLE_OTHER_INST(66, LandingPad, LandingPadInst) // Landing pad instruction. +HANDLE_OTHER_INST(67, Freeze, FreezeInst) // Freeze instruction. LAST_OTHER_INST(67) #undef FIRST_TERM_INST diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -5290,6 +5290,35 @@ return cast(I)->getPointerAddressSpace(); } +//===----------------------------------------------------------------------===// +// FreezeInst Class +//===----------------------------------------------------------------------===// + +/// This class represents a freeze function that returns random concrete +/// value if an operand is either a poison value or an undef value +class FreezeInst : public UnaryInstruction { +protected: + // Note: Instruction needs to be a friend here to call cloneImpl. + friend class Instruction; + + /// Clone an identical FreezeInst + FreezeInst *cloneImpl() const; + +public: + explicit FreezeInst(Value *S, + const Twine &NameStr = "", + Instruction *InsertBefore = nullptr); + FreezeInst(Value *S, const Twine &NameStr, BasicBlock *InsertAtEnd); + + // Methods for support type inquiry through isa, cast, and dyn_cast: + static inline bool classof(const Instruction *I) { + return I->getOpcode() == Freeze; + } + static inline bool classof(const Value *V) { + return isa(V) && classof(cast(V)); + } +}; + } // end namespace llvm #endif // LLVM_IR_INSTRUCTIONS_H diff --git a/llvm/include/llvm/IR/Operator.h b/llvm/include/llvm/IR/Operator.h --- a/llvm/include/llvm/IR/Operator.h +++ b/llvm/include/llvm/IR/Operator.h @@ -598,9 +598,6 @@ } }; -class FreezeOperator : public ConcreteOperator -{}; - } // end namespace llvm #endif // LLVM_IR_OPERATOR_H diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h --- a/llvm/include/llvm/IR/PatternMatch.h +++ b/llvm/include/llvm/IR/PatternMatch.h @@ -825,28 +825,6 @@ return m_FSub(m_AnyZeroFP(), X); } -template struct Freeze_match { - Op_t X; - - Freeze_match(const Op_t &Op) : X(Op) {} - template bool match(OpTy *V) { - auto *I = dyn_cast(V); - if (!I) return false; - - if (isa(I)) - return X.match(I->getOperand(0)); - - return false; - } -}; - -/// Matches freeze. -template -inline Freeze_match -m_Freeze(const OpTy &X) { - return Freeze_match(X); -} - template inline BinaryOp_match m_Mul(const LHS &L, const RHS &R) { @@ -1255,6 +1233,12 @@ return m_Select(C, m_ConstantInt(), m_ConstantInt()); } +/// Matches FreezeInst. +template +inline OneOps_match m_Freeze(const OpTy &Op) { + return OneOps_match(Op); +} + /// Matches InsertElementInst. template inline ThreeOps_match diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -839,7 +839,6 @@ } while (false) INSTKEYWORD(fneg, FNeg); - INSTKEYWORD(freeze, Freeze); INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd); INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub); @@ -896,6 +895,8 @@ INSTKEYWORD(catchpad, CatchPad); INSTKEYWORD(cleanuppad, CleanupPad); + INSTKEYWORD(freeze, Freeze); + #undef INSTKEYWORD #define DWKEYWORD(TYPE, TOKEN) \ diff --git a/llvm/lib/AsmParser/LLParser.h b/llvm/lib/AsmParser/LLParser.h --- a/llvm/lib/AsmParser/LLParser.h +++ b/llvm/lib/AsmParser/LLParser.h @@ -600,6 +600,7 @@ int ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS); int ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS); int ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS); + bool ParseFreeze(Instruction *&I, PerFunctionState &PFS); // Use-list order directives. bool ParseUseListOrder(PerFunctionState *PFS = nullptr); diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp --- a/llvm/lib/AsmParser/LLParser.cpp +++ b/llvm/lib/AsmParser/LLParser.cpp @@ -3418,8 +3418,7 @@ } // Unary Operators. - case lltok::kw_fneg: - case lltok::kw_freeze: { + case lltok::kw_fneg: { unsigned Opc = Lex.getUIntVal(); Constant *Val; Lex.Lex(); @@ -3434,8 +3433,6 @@ if (!Val->getType()->isFPOrFPVectorTy()) return Error(ID.Loc, "constexpr requires fp operands"); break; - case Instruction::Freeze: - break; default: llvm_unreachable("Unknown unary operator!"); } unsigned Flags = 0; @@ -5729,7 +5726,6 @@ Inst->setFastMathFlags(FMF); return false; } - case lltok::kw_freeze: return ParseUnaryOp(Inst, PFS, KeywordVal, false); // Binary Operators. case lltok::kw_add: case lltok::kw_sub: @@ -5833,6 +5829,7 @@ return 0; } case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS); + case lltok::kw_freeze: return ParseFreeze(Inst, PFS); // Call. case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None); case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail); @@ -6333,14 +6330,16 @@ /// ParseUnaryOp /// ::= UnaryOp TypeAndValue ',' Value /// -/// If IsFP is true, then fp operand is only allowed. +/// If IsFP is false, then any integer operand is allowed, if it is true, any fp +/// operand is allowed. bool LLParser::ParseUnaryOp(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc, bool IsFP) { LocTy Loc; Value *LHS; if (ParseTypeAndValue(LHS, Loc, PFS)) return true; - bool Valid = !IsFP || LHS->getType()->isFPOrFPVectorTy(); + bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy() + : LHS->getType()->isIntOrIntVectorTy(); if (!Valid) return Error(Loc, "invalid operand type for instruction"); @@ -6754,6 +6753,18 @@ return false; } +/// ParseFreeze +/// ::= 'freeze' Type Value +bool LLParser::ParseFreeze(Instruction *&Inst, PerFunctionState &PFS) { + LocTy Loc; + Value *Op; + if (ParseTypeAndValue(Op, Loc, PFS)) + return true; + + Inst = new FreezeInst(Op); + return false; +} + /// ParseCall /// ::= 'call' OptionalFastMathFlags OptionalCallingConv /// OptionalAttrs Type Value ParameterList OptionalAttrs diff --git a/llvm/lib/AsmParser/LLToken.h b/llvm/lib/AsmParser/LLToken.h --- a/llvm/lib/AsmParser/LLToken.h +++ b/llvm/lib/AsmParser/LLToken.h @@ -280,7 +280,6 @@ // Instruction Opcodes (Opcode in UIntVal). kw_fneg, - kw_freeze, kw_add, kw_fadd, kw_sub, @@ -355,6 +354,8 @@ kw_insertvalue, kw_blockaddress, + kw_freeze, + // Metadata types. kw_distinct, diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1056,13 +1056,16 @@ } static int getDecodedUnaryOpcode(unsigned Val, Type *Ty) { + bool IsFP = Ty->isFPOrFPVectorTy(); + // UnOps are only valid for int/fp or vector of int/fp types + if (!IsFP && !Ty->isIntOrIntVectorTy()) + return -1; + switch (Val) { default: return -1; case bitc::UNOP_FNEG: - return Ty->isFPOrFPVectorTy() ? Instruction::FNeg : -1; - case bitc::UNOP_FREEZE: - return Instruction::Freeze; + return IsFP ? Instruction::FNeg : -1; } } @@ -3863,7 +3866,7 @@ case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode] unsigned OpNum = 0; Value *LHS; - if (getValueTypePair(Record, OpNum, NextValueNo, LHS, &FullTy) || + if (getValueTypePair(Record, OpNum, NextValueNo, LHS) || OpNum+1 > Record.size()) return error("Invalid record"); @@ -5116,6 +5119,19 @@ OperandBundles.emplace_back(BundleTags[Record[0]], std::move(Inputs)); continue; } + + case bitc::FUNC_CODE_INST_FREEZE: { // FREEZE: [opty,opval] + unsigned OpNum = 0; + Value *Op = nullptr; + if (getValueTypePair(Record, OpNum, NextValueNo, Op, &FullTy)) + return error("Invalid record"); + if (OpNum != Record.size()) + return error("Invalid record"); + + I = new FreezeInst(Op); + InstructionList.push_back(I); + break; + } } // Add instruction to end of current BB. If there is no current BB, reject diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -521,7 +521,6 @@ switch (Opcode) { default: llvm_unreachable("Unknown binary instruction!"); case Instruction::FNeg: return bitc::UNOP_FNEG; - case Instruction::Freeze: return bitc::UNOP_FREEZE; } } @@ -2435,17 +2434,6 @@ Record.push_back(VE.getTypeID(C->getOperand(0)->getType())); Record.push_back(VE.getValueID(C->getOperand(0))); AbbrevToUse = CONSTANTS_CE_CAST_Abbrev; - } else if (Instruction::isUnaryOp(CE->getOpcode())) { - assert(CE->getNumOperands() == 1 && "Unknown constant expr!"); - Code = bitc::CST_CODE_CE_UNOP; - Record.push_back(getEncodedUnaryOpcode(CE->getOpcode())); - Record.push_back(VE.getValueID(C->getOperand(0))); - uint64_t Flags = getOptimizationFlags(CE); - if (Flags != 0) { - assert(CE->getOpcode() == Instruction::FNeg); - Record.push_back(Flags); - } - break; } else { assert(CE->getNumOperands() == 2 && "Unknown constant expr!"); Code = bitc::CST_CODE_CE_BINOP; @@ -2457,6 +2445,16 @@ Record.push_back(Flags); } break; + case Instruction::FNeg: { + assert(CE->getNumOperands() == 1 && "Unknown constant expr!"); + Code = bitc::CST_CODE_CE_UNOP; + Record.push_back(getEncodedUnaryOpcode(CE->getOpcode())); + Record.push_back(VE.getValueID(C->getOperand(0))); + uint64_t Flags = getOptimizationFlags(CE); + if (Flags != 0) + Record.push_back(Flags); + break; + } case Instruction::GetElementPtr: { Code = bitc::CST_CODE_CE_GEP; const auto *GO = cast(C); @@ -2614,17 +2612,6 @@ AbbrevToUse = FUNCTION_INST_CAST_ABBREV; Vals.push_back(VE.getTypeID(I.getType())); Vals.push_back(getEncodedCastOpcode(I.getOpcode())); - } else if (isa(I)) { - Code = bitc::FUNC_CODE_INST_UNOP; - if (!pushValueAndType(I.getOperand(0), InstID, Vals)) - AbbrevToUse = FUNCTION_INST_UNOP_ABBREV; - Vals.push_back(getEncodedUnaryOpcode(I.getOpcode())); - uint64_t Flags = getOptimizationFlags(&I); - if (Flags != 0) { - if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV) - AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV; - Vals.push_back(Flags); - } } else { assert(isa(I) && "Unknown instruction!"); Code = bitc::FUNC_CODE_INST_BINOP; @@ -2640,6 +2627,19 @@ } } break; + case Instruction::FNeg: { + Code = bitc::FUNC_CODE_INST_UNOP; + if (!pushValueAndType(I.getOperand(0), InstID, Vals)) + AbbrevToUse = FUNCTION_INST_UNOP_ABBREV; + Vals.push_back(getEncodedUnaryOpcode(I.getOpcode())); + uint64_t Flags = getOptimizationFlags(&I); + if (Flags != 0) { + if (AbbrevToUse == FUNCTION_INST_UNOP_ABBREV) + AbbrevToUse = FUNCTION_INST_UNOP_FLAGS_ABBREV; + Vals.push_back(Flags); + } + break; + } case Instruction::GetElementPtr: { Code = bitc::FUNC_CODE_INST_GEP; AbbrevToUse = FUNCTION_INST_GEP_ABBREV; @@ -3034,6 +3034,10 @@ pushValue(I.getOperand(0), InstID, Vals); // valist. Vals.push_back(VE.getTypeID(I.getType())); // restype. break; + case Instruction::Freeze: + Code = bitc::FUNC_CODE_INST_FREEZE; + pushValueAndType(I.getOperand(0), InstID, Vals); + break; } Stream.EmitRecord(Code, Vals, AbbrevToUse); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.h @@ -668,7 +668,6 @@ void visitUnary(const User &I, unsigned Opcode); void visitFNeg(const User &I) { visitUnary(I, ISD::FNEG); } - void visitFreeze(const User &I); void visitBinary(const User &I, unsigned Opcode); void visitShift(const User &I, unsigned Opcode); @@ -743,6 +742,7 @@ void visitAtomicStore(const StoreInst &I); void visitLoadFromSwiftError(const LoadInst &I); void visitStoreToSwiftError(const StoreInst &I); + void visitFreeze(const FreezeInst &I); void visitInlineAsm(ImmutableCallSite CS); void visitIntrinsicCall(const CallInst &I, unsigned Intrinsic); diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -10593,7 +10593,7 @@ } } -void SelectionDAGBuilder::visitFreeze(const User &I) { +void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) { SDValue N = getValue(I.getOperand(0)); setValue(&I, N); } diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -941,50 +941,43 @@ Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) { assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected"); - switch (static_cast(Opcode)) { - case Instruction::FNeg: { - // Handle scalar UndefValue. Vectors are always evaluated per element. - bool HasScalarUndef = !C->getType()->isVectorTy() && isa(C); + // Handle scalar UndefValue. Vectors are always evaluated per element. + bool HasScalarUndef = !C->getType()->isVectorTy() && isa(C); - if (HasScalarUndef) { + if (HasScalarUndef) { + switch (static_cast(Opcode)) { + case Instruction::FNeg: return C; // -undef -> undef + case Instruction::UnaryOpsEnd: + llvm_unreachable("Invalid UnaryOp"); } + } - // Constant should not be UndefValue, unless these are vector constants. - assert(!HasScalarUndef && "Unexpected UndefValue"); - assert(!isa(C) && "Unexpected Integer UnaryOp"); + // Constant should not be UndefValue, unless these are vector constants. + assert(!HasScalarUndef && "Unexpected UndefValue"); + // We only have FP UnaryOps right now. + assert(!isa(C) && "Unexpected Integer UnaryOp"); - if (ConstantFP *CFP = dyn_cast(C)) { - const APFloat &CV = CFP->getValueAPF(); + if (ConstantFP *CFP = dyn_cast(C)) { + const APFloat &CV = CFP->getValueAPF(); + switch (Opcode) { + default: + break; + case Instruction::FNeg: return ConstantFP::get(C->getContext(), neg(CV)); - } else if (VectorType *VTy = dyn_cast(C->getType())) { - // Fold each element and create a vector constant from those constants. - SmallVector Result; - Type *Ty = IntegerType::get(VTy->getContext(), 32); - for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { - Constant *ExtractIdx = ConstantInt::get(Ty, i); - Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx); - - Result.push_back(ConstantExpr::get(Opcode, Elt)); - } - - return ConstantVector::get(Result); } - break; - } - case Instruction::Freeze: { - if (ConstantFP *CFP = dyn_cast(C)) { - return CFP; - } else if (ConstantInt *CINT = dyn_cast(C)) { - return CINT; - } else if (GlobalVariable *GV = dyn_cast(C)) { - // A global variable is neither undef nor poison. - return GV; + } else if (VectorType *VTy = dyn_cast(C->getType())) { + // Fold each element and create a vector constant from those constants. + SmallVector Result; + Type *Ty = IntegerType::get(VTy->getContext(), 32); + for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { + Constant *ExtractIdx = ConstantInt::get(Ty, i); + Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx); + + Result.push_back(ConstantExpr::get(Opcode, Elt)); } - break; - } - case Instruction::UnaryOpsEnd: - llvm_unreachable("Invalid UnaryOp"); + + return ConstantVector::get(Result); } // We don't know how to fold this. diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -3410,11 +3410,6 @@ return wrap(unwrap(B)->CreateFNeg(unwrap(V), Name)); } -LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef V, - const char *Name) { - return wrap(unwrap(B)->CreateFreeze(unwrap(V), Name)); -} - LLVMValueRef LLVMBuildNot(LLVMBuilderRef B, LLVMValueRef V, const char *Name) { return wrap(unwrap(B)->CreateNot(unwrap(V), Name)); } @@ -3902,6 +3897,11 @@ Index, Name)); } +LLVMValueRef LLVMBuildFreeze(LLVMBuilderRef B, LLVMValueRef Val, + const char *Name) { + return wrap(unwrap(B)->CreateFreeze(unwrap(Val), Name)); +} + LLVMValueRef LLVMBuildIsNull(LLVMBuilderRef B, LLVMValueRef Val, const char *Name) { return wrap(unwrap(B)->CreateIsNull(unwrap(Val), Name)); diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp --- a/llvm/lib/IR/Instruction.cpp +++ b/llvm/lib/IR/Instruction.cpp @@ -307,7 +307,6 @@ // Standard unary operators... case FNeg: return "fneg"; - case Freeze: return "freeze"; // Standard binary operators... case Add: return "add"; @@ -369,6 +368,7 @@ case InsertValue: return "insertvalue"; case LandingPad: return "landingpad"; case CleanupPad: return "cleanuppad"; + case Freeze: return "freeze"; default: return " "; } diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -2232,9 +2232,6 @@ "Tried to create a floating-point operation on a " "non-floating-point type!"); break; - case Freeze: - // Freeze can take any type as an argument. - break; default: llvm_unreachable("Invalid opcode provided"); } #endif @@ -4095,6 +4092,22 @@ } //===----------------------------------------------------------------------===// +// FreezeInst Implementation +//===----------------------------------------------------------------------===// + +FreezeInst::FreezeInst(Value *S, + const Twine &Name, Instruction *InsertBefore) + : UnaryInstruction(S->getType(), Freeze, S, InsertBefore) { + setName(Name); +} + +FreezeInst::FreezeInst(Value *S, + const Twine &Name, BasicBlock *InsertAtEnd) + : UnaryInstruction(S->getType(), Freeze, S, InsertAtEnd) { + setName(Name); +} + +//===----------------------------------------------------------------------===// // cloneImpl() implementations //===----------------------------------------------------------------------===// @@ -4310,3 +4323,7 @@ LLVMContext &Context = getContext(); return new UnreachableInst(Context); } + +FreezeInst *FreezeInst::cloneImpl() const { + return new FreezeInst(getOperand(0)); +} diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp --- a/llvm/lib/IR/Verifier.cpp +++ b/llvm/lib/IR/Verifier.cpp @@ -3145,9 +3145,6 @@ Assert(U.getType()->isFPOrFPVectorTy(), "FNeg operator only works with float types!", &U); break; - case Instruction::Freeze: - // Freeze can take all kinds of types. - break; default: llvm_unreachable("Unknown UnaryOperator opcode!"); } diff --git a/llvm/test/Bindings/OCaml/core.ml b/llvm/test/Bindings/OCaml/core.ml --- a/llvm/test/Bindings/OCaml/core.ml +++ b/llvm/test/Bindings/OCaml/core.ml @@ -267,7 +267,6 @@ * CHECK: @const_nsw_neg = global i64 sub nsw * CHECK: @const_nuw_neg = global i64 sub nuw * CHECK: @const_fneg = global double fneg - * CHECK: @const_freeze = global i64 freeze * CHECK: @const_not = global i64 xor * CHECK: @const_add = global i64 add * CHECK: @const_nsw_add = global i64 add nsw @@ -304,7 +303,6 @@ ignore (define_global "const_nsw_neg" (const_nsw_neg foldbomb) m); ignore (define_global "const_nuw_neg" (const_nuw_neg foldbomb) m); ignore (define_global "const_fneg" (const_fneg ffoldbomb) m); - ignore (define_global "const_freeze" (const_freeze foldbomb) m); ignore (define_global "const_not" (const_not foldbomb) m); ignore (define_global "const_add" (const_add foldbomb five) m); ignore (define_global "const_nsw_add" (const_nsw_add foldbomb five) m); @@ -1402,8 +1400,8 @@ ignore (build_nsw_neg p1 "build_nsw_neg" b); ignore (build_nuw_neg p1 "build_nuw_neg" b); ignore (build_fneg f1 "build_fneg" b); - ignore (build_freeze f1 "build_freeze" b); ignore (build_not p1 "build_not" b); + ignore (build_freeze p1 "build_freeze" b); ignore (build_unreachable b) end; diff --git a/llvm/test/Bindings/llvm-c/freeze.ll b/llvm/test/Bindings/llvm-c/freeze.ll --- a/llvm/test/Bindings/llvm-c/freeze.ll +++ b/llvm/test/Bindings/llvm-c/freeze.ll @@ -18,5 +18,6 @@ %10 = freeze %struct.T %arg6 %11 = freeze [2 x i32] %arg7 %12 = freeze { i32, i32 } %arg8 + %13 = freeze i8* null ret i32 %1 } diff --git a/llvm/test/Bitcode/compatibility.ll b/llvm/test/Bitcode/compatibility.ll --- a/llvm/test/Bitcode/compatibility.ll +++ b/llvm/test/Bitcode/compatibility.ll @@ -1172,17 +1172,9 @@ } ; Instructions -- Unary Operations -define void @instructions.unops(double %op1, i32 %op2, <2 x i32> %op3, i8* %op4) { +define void @instructions.unops(double %op1) { fneg double %op1 ; CHECK: fneg double %op1 - freeze i32 %op2 - ; CHECK: freeze i32 %op2 - freeze double %op1 - ; CHECK: freeze double %op1 - freeze <2 x i32> %op3 - ; CHECK: freeze <2 x i32> %op3 - freeze i8* %op4 - ; CHECK: freeze i8* %op4 ret void } @@ -1377,7 +1369,7 @@ } ; Instructions -- Other Operations -define void @instructions.other(i32 %op1, i32 %op2, half %fop1, half %fop2) { +define void @instructions.other(i32 %op1, i32 %op2, half %fop1, half %fop2, <2 x i32> %vop, i8* %pop) { entry: icmp eq i32 %op1, %op2 ; CHECK: icmp eq i32 %op1, %op2 @@ -1457,6 +1449,16 @@ tail call ghccc nonnull i32* @f.nonnull() minsize ; CHECK: tail call ghccc nonnull i32* @f.nonnull() #7 + freeze i32 %op1 + ; CHECK: freeze i32 %op1 + freeze i32 10 + ; CHECK: freeze i32 10 + freeze half %fop1 + ; CHECK: freeze half %fop1 + freeze <2 x i32> %vop + ; CHECK: freeze <2 x i32> %vop + freeze i8* %pop + ; CHECK: freeze i8* %pop ret void } @@ -1834,10 +1836,6 @@ ret void } -define i64 @constexpr_freeze() { - ret i64 freeze (i64 32) -} - ; immarg attribute declare void @llvm.test.immarg.intrinsic(i32 immarg) ; CHECK: declare void @llvm.test.immarg.intrinsic(i32 immarg) diff --git a/llvm/test/Transforms/MergeFunc/inline-asm.ll b/llvm/test/Transforms/MergeFunc/inline-asm.ll --- a/llvm/test/Transforms/MergeFunc/inline-asm.ll +++ b/llvm/test/Transforms/MergeFunc/inline-asm.ll @@ -3,13 +3,13 @@ ; CHECK-LABEL: @int_ptr_arg_different ; CHECK-NEXT: call void asm +; CHECK-LABEL: @int_ptr_null +; CHECK-NEXT: tail call void @float_ptr_null() + ; CHECK-LABEL: @int_ptr_arg_same ; CHECK-NEXT: %2 = bitcast i32* %0 to float* ; CHECK-NEXT: tail call void @float_ptr_arg_same(float* %2) -; CHECK-LABEL: @int_ptr_null -; CHECK-NEXT: tail call void @float_ptr_null() - ; Used to satisfy minimum size limit declare void @stuff() diff --git a/llvm/unittests/IR/VerifierTest.cpp b/llvm/unittests/IR/VerifierTest.cpp --- a/llvm/unittests/IR/VerifierTest.cpp +++ b/llvm/unittests/IR/VerifierTest.cpp @@ -45,6 +45,54 @@ EXPECT_TRUE(verifyFunction(*F)); } +TEST(VerifierTest, Freeze) { + LLVMContext C; + Module M("M", C); + FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false); + Function *F = Function::Create(FTy, Function::ExternalLinkage, "foo", M); + BasicBlock *Entry = BasicBlock::Create(C, "entry", F); + ReturnInst *RI = ReturnInst::Create(C, Entry); + + IntegerType *ITy = IntegerType::get(C, 32); + ConstantInt *CI = ConstantInt::get(ITy, 0); + + // Valid type : freeze(<2 x i32>) + Constant *CV = ConstantVector::getSplat(2, CI); + FreezeInst *FI_vec = new FreezeInst(CV); + FI_vec->insertBefore(RI); + + EXPECT_FALSE(verifyFunction(*F)); + + FI_vec->eraseFromParent(); + + // Valid type : freeze(float) + Constant *CFP = ConstantFP::get(Type::getDoubleTy(C), 0.0); + FreezeInst *FI_dbl = new FreezeInst(CFP); + FI_dbl->insertBefore(RI); + + EXPECT_FALSE(verifyFunction(*F)); + + FI_dbl->eraseFromParent(); + + // Valid type : freeze(i32*) + PointerType *PT = PointerType::get(ITy, 0); + ConstantPointerNull *CPN = ConstantPointerNull::get(PT); + FreezeInst *FI_ptr = new FreezeInst(CPN); + FI_ptr->insertBefore(RI); + + EXPECT_FALSE(verifyFunction(*F)); + + FI_ptr->eraseFromParent(); + + // Valid type : freeze(int) + FreezeInst *FI = new FreezeInst(CI); + FI->insertBefore(RI); + + EXPECT_FALSE(verifyFunction(*F)); + + FI_ptr->eraseFromParent(); +} + TEST(VerifierTest, InvalidRetAttribute) { LLVMContext C; Module M("M", C);