/* Standard Unary Operators */
LLVMFNeg = 66,
- LLVMFreeze = 68,
/* Standard Binary Operators */
LLVMAdd = 8,
LLVMShuffleVector = 52,
LLVMExtractValue = 53,
LLVMInsertValue = 54,
+ LLVMFreeze = 68,
/* Atomic operators */
LLVMFence = 55,
macro(ExtractValueInst) \
macro(LoadInst) \
macro(VAArgInst) \
+ macro(FreezeInst) \
macro(AtomicCmpXchgInst) \
macro(AtomicRMWInst) \
macro(FenceInst)
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);
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);
/// 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
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 {
}
Value *CreateFreeze(Value *V, const Twine &Name = "") {
- return Insert(UnaryOperator::CreateFreeze(V, Name));
+ return Insert(new FreezeInst(V), Name);
}
//===--------------------------------------------------------------------===//
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);}
// 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
return cast<StoreInst>(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<Instruction>(V) && classof(cast<Instruction>(V));
+ }
+};
+
} // end namespace llvm
#endif // LLVM_IR_INSTRUCTIONS_H
}
};
-class FreezeOperator : public ConcreteOperator<Operator, Instruction::Freeze>
-{};
-
} // end namespace llvm
#endif // LLVM_IR_OPERATOR_H
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) {
return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
}
+/// Matches FreezeInst.
+template <typename OpTy>
+inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) {
+ return OneOps_match<OpTy, Instruction::Freeze>(Op);
+}
+
/// Matches InsertElementInst.
template <typename Val_t, typename Elt_t, typename Idx_t>
inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
} while (false)
INSTKEYWORD(fneg, FNeg);
- INSTKEYWORD(freeze, Freeze);
INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
INSTKEYWORD(catchpad, CatchPad);
INSTKEYWORD(cleanuppad, CleanupPad);
+ INSTKEYWORD(freeze, Freeze);
+
#undef INSTKEYWORD
#define DWKEYWORD(TYPE, TOKEN) \
}
// Unary Operators.
- case lltok::kw_fneg:
- case lltok::kw_freeze: {
+ case lltok::kw_fneg: {
unsigned Opc = Lex.getUIntVal();
Constant *Val;
Lex.Lex();
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;
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:
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);
/// 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");
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
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);
// Instruction Opcodes (Opcode in UIntVal).
kw_fneg,
- kw_freeze,
kw_add,
kw_fadd,
kw_sub,
kw_insertvalue,
kw_blockaddress,
+ kw_freeze,
+
// Metadata types.
kw_distinct,
}
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;
}
}
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");
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
switch (Opcode) {
default: llvm_unreachable("Unknown binary instruction!");
case Instruction::FNeg: return bitc::UNOP_FNEG;
- case Instruction::Freeze: return bitc::UNOP_FREEZE;
}
}
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;
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<GEPOperator>(C);
AbbrevToUse = FUNCTION_INST_CAST_ABBREV;
Vals.push_back(VE.getTypeID(I.getType()));
Vals.push_back(getEncodedCastOpcode(I.getOpcode()));
- } else if (isa<UnaryOperator>(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<BinaryOperator>(I) && "Unknown instruction!");
Code = bitc::FUNC_CODE_INST_BINOP;
}
}
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;
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);
}
}
-void SelectionDAGBuilder::visitFreeze(const User &I) {
+void SelectionDAGBuilder::visitFreeze(const FreezeInst &I) {
SDValue N = getValue(I.getOperand(0));
setValue(&I, N);
}
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);
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);
Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) {
assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected");
- switch (static_cast<Instruction::UnaryOps>(Opcode)) {
- case Instruction::FNeg: {
- // Handle scalar UndefValue. Vectors are always evaluated per element.
- bool HasScalarUndef = !C->getType()->isVectorTy() && isa<UndefValue>(C);
+ // Handle scalar UndefValue. Vectors are always evaluated per element.
+ bool HasScalarUndef = !C->getType()->isVectorTy() && isa<UndefValue>(C);
- if (HasScalarUndef) {
+ if (HasScalarUndef) {
+ switch (static_cast<Instruction::UnaryOps>(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<ConstantInt>(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<ConstantInt>(C) && "Unexpected Integer UnaryOp");
- if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
- const APFloat &CV = CFP->getValueAPF();
+ if (ConstantFP *CFP = dyn_cast<ConstantFP>(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<VectorType>(C->getType())) {
- // Fold each element and create a vector constant from those constants.
- SmallVector<Constant*, 16> 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<ConstantFP>(C)) {
- return CFP;
- } else if (ConstantInt *CINT = dyn_cast<ConstantInt>(C)) {
- return CINT;
- } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
- // A global variable is neither undef nor poison.
- return GV;
+ } else if (VectorType *VTy = dyn_cast<VectorType>(C->getType())) {
+ // Fold each element and create a vector constant from those constants.
+ SmallVector<Constant*, 16> 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.
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));
}
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));
// Standard unary operators...
case FNeg: return "fneg";
- case Freeze: return "freeze";
// Standard binary operators...
case Add: return "add";
case InsertValue: return "insertvalue";
case LandingPad: return "landingpad";
case CleanupPad: return "cleanuppad";
+ case Freeze: return "freeze";
default: return "<Invalid operator> ";
}
"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
}
//===----------------------------------------------------------------------===//
+// 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
//===----------------------------------------------------------------------===//
LLVMContext &Context = getContext();
return new UnreachableInst(Context);
}
+
+FreezeInst *FreezeInst::cloneImpl() const {
+ return new FreezeInst(getOperand(0));
+}
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!");
}
* 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
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);
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;
%10 = freeze %struct.T %arg6
%11 = freeze [2 x i32] %arg7
%12 = freeze { i32, i32 } %arg8
+ %13 = freeze i8* null
ret i32 %1
}
}
; 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
}
}
; 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
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
}
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)
; 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()
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);