Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more
Permalink
Browse files

[IR] Add Freeze instruction

Summary:
- Define Instruction::Freeze, let it be UnaryOperator
- Add support for freeze to LLLexer/LLParser/BitcodeReader/BitcodeWriter
  The format is `%x = freeze <ty> %v`
- Add support for freeze instruction to llvm-c interface.
- Add m_Freeze in PatternMatch.
- Erase freeze when lowering IR to SelDag.

Reviewers: deadalnix, hfinkel, efriedma, lebedev.ri, nlopes, jdoerfert, regehr, filcab, delcypher, whitequark

Reviewed By: lebedev.ri, jdoerfert

Subscribers: jfb, kristof.beyls, hiraditya, lebedev.ri, steven_wu, dexonsmith, xbolva00, delcypher, spatel, regehr, trentxintong, vsk, filcab, nlopes, mehdi_amini, deadalnix, llvm-commits

Differential Revision: https://reviews.llvm.org/D29011
  • Loading branch information
aqjune committed Nov 5, 2019
1 parent 9f34447 commit 58acbce3def63a207b8f5a69318a99666a4aac53
@@ -69,6 +69,7 @@ typedef enum {

/* Standard Unary Operators */
LLVMFNeg = 66,
LLVMFreeze = 68,

/* Standard Binary Operators */
LLVMAdd = 8,
@@ -3747,6 +3748,7 @@ LLVMValueRef LLVMBuildNUWNeg(LLVMBuilderRef B, LLVMValueRef V,
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);
@@ -391,7 +391,8 @@ enum CastOpcodes {
/// have no fixed relation to the LLVM IR enum values. Changing these will
/// break compatibility with old files.
enum UnaryOpcodes {
UNOP_FNEG = 0
UNOP_FNEG = 0,
UNOP_FREEZE = 1
};

/// BinaryOpcodes - These are values used in the bitcode files to encode which
@@ -483,6 +483,9 @@ class IRTranslator : public MachineFunctionPass {
bool translateUserOp2(const User &U, MachineIRBuilder &MIRBuilder) {
return false;
}
bool translateFreeze(const User &U, MachineIRBuilder &MIRBuilder) {
return false;
}

/// @}

@@ -2392,6 +2392,10 @@ class IRBuilder : public IRBuilderBase, public Inserter {
return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
}

Value *CreateFreeze(Value *V, const Twine &Name = "") {
return Insert(UnaryOperator::CreateFreeze(V, Name));
}

//===--------------------------------------------------------------------===//
// Utility creation methods
//===--------------------------------------------------------------------===//
@@ -140,84 +140,85 @@ HANDLE_TERM_INST (11, CallBr , CallBrInst) // A call-site terminator
// Standard unary operators...
FIRST_UNARY_INST(12)
HANDLE_UNARY_INST(12, FNeg , UnaryOperator)
LAST_UNARY_INST(12)
HANDLE_UNARY_INST(13, Freeze, UnaryOperator)
LAST_UNARY_INST(13)

// Standard binary operators...
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)
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)

// Logical operators (integer operands)
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)
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)

// Memory operators...
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)
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)

// Cast operators ...
// NOTE: The order matters here because CastInst::isEliminableCastPair
// NOTE: (see Instructions.cpp) encodes a table based on this ordering.
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)
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)

// Other operators...
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.
LAST_OTHER_INST(66)
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.
LAST_OTHER_INST(67)

#undef FIRST_TERM_INST
#undef HANDLE_TERM_INST
@@ -598,6 +598,9 @@ class BitCastOperator
}
};

class FreezeOperator : public ConcreteOperator<Operator, Instruction::Freeze>
{};

} // end namespace llvm

#endif // LLVM_IR_OPERATOR_H
@@ -825,6 +825,28 @@ m_FNegNSZ(const RHS &X) {
return m_FSub(m_AnyZeroFP(), X);
}

template <typename Op_t> struct Freeze_match {
Op_t X;

Freeze_match(const Op_t &Op) : X(Op) {}
template <typename OpTy> bool match(OpTy *V) {
auto *I = dyn_cast<UnaryOperator>(V);
if (!I) return false;

if (isa<FreezeOperator>(I))
return X.match(I->getOperand(0));

return false;
}
};

/// Matches freeze.
template <typename OpTy>
inline Freeze_match<OpTy>
m_Freeze(const OpTy &X) {
return Freeze_match<OpTy>(X);
}

template <typename LHS, typename RHS>
inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
const RHS &R) {
@@ -837,6 +837,7 @@ lltok::Kind LLLexer::LexIdentifier() {
} while (false)

INSTKEYWORD(fneg, FNeg);
INSTKEYWORD(freeze, Freeze);

INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
@@ -3414,7 +3414,8 @@ bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
}

// Unary Operators.
case lltok::kw_fneg: {
case lltok::kw_fneg:
case lltok::kw_freeze: {
unsigned Opc = Lex.getUIntVal();
Constant *Val;
Lex.Lex();
@@ -3429,6 +3430,8 @@ bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
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;
@@ -5722,6 +5725,7 @@ int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
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:
@@ -6325,16 +6329,14 @@ bool LLParser::ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
/// ParseUnaryOp
/// ::= UnaryOp TypeAndValue ',' Value
///
/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
/// operand is allowed.
/// If IsFP is true, then fp operand is only 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()
: LHS->getType()->isIntOrIntVectorTy();
bool Valid = !IsFP || LHS->getType()->isFPOrFPVectorTy();

if (!Valid)
return Error(Loc, "invalid operand type for instruction");
@@ -279,6 +279,7 @@ enum Kind {

// Instruction Opcodes (Opcode in UIntVal).
kw_fneg,
kw_freeze,
kw_add,
kw_fadd,
kw_sub,
@@ -1055,16 +1055,13 @@ static int getDecodedCastOpcode(unsigned Val) {
}

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 IsFP ? Instruction::FNeg : -1;
return Ty->isFPOrFPVectorTy() ? Instruction::FNeg : -1;
case bitc::UNOP_FREEZE:
return Instruction::Freeze;
}
}

@@ -3865,7 +3862,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
case bitc::FUNC_CODE_INST_UNOP: { // UNOP: [opval, ty, opcode]
unsigned OpNum = 0;
Value *LHS;
if (getValueTypePair(Record, OpNum, NextValueNo, LHS) ||
if (getValueTypePair(Record, OpNum, NextValueNo, LHS, &FullTy) ||
OpNum+1 > Record.size())
return error("Invalid record");

0 comments on commit 58acbce

Please sign in to comment.
You can’t perform that action at this time.