@@ -521,6 +521,7 @@ static unsigned getEncodedUnaryOpcode(unsigned Opcode) {
switch (Opcode) {
default: llvm_unreachable("Unknown binary instruction!");
case Instruction::FNeg: return bitc::UNOP_FNEG;
case Instruction::Freeze: return bitc::UNOP_FREEZE;
}
}

@@ -2433,6 +2434,17 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
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;
@@ -2444,16 +2456,6 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
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);
@@ -2611,6 +2613,17 @@ void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
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;
@@ -2626,19 +2639,6 @@ void ModuleBitcodeWriter::writeInstruction(const Instruction &I,
}
}
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;
@@ -10592,3 +10592,8 @@ void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
lowerWorkItem(W, SI.getCondition(), SwitchMBB, DefaultMBB);
}
}

void SelectionDAGBuilder::visitFreeze(const User &I) {
SDValue N = getValue(I.getOperand(0));
setValue(&I, N);
}
@@ -668,6 +668,7 @@ class SelectionDAGBuilder {

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);
@@ -1663,6 +1663,7 @@ int TargetLoweringBase::InstructionOpcodeToISD(unsigned Opcode) const {
case ExtractValue: return ISD::MERGE_VALUES;
case InsertValue: return ISD::MERGE_VALUES;
case LandingPad: return 0;
case Freeze: return 0;
}

llvm_unreachable("Unknown instruction type encountered!");
@@ -941,43 +941,52 @@ Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) {
assert(Instruction::isUnaryOp(Opcode) && "Non-unary instruction detected");

// Handle scalar UndefValue. Vectors are always evaluated per element.
bool HasScalarUndef = !C->getType()->isVectorTy() && isa<UndefValue>(C);
switch (static_cast<Instruction::UnaryOps>(Opcode)) {
default:
break;
case Instruction::FNeg: {
// Handle scalar UndefValue. Vectors are always evaluated per element.
bool HasScalarUndef = !C->getType()->isVectorTy() && isa<UndefValue>(C);

if (HasScalarUndef) {
switch (static_cast<Instruction::UnaryOps>(Opcode)) {
case Instruction::FNeg:
if (HasScalarUndef) {
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");
// We only have FP UnaryOps right now.
assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp");
// Constant should not be UndefValue, unless these are vector constants.
assert(!HasScalarUndef && "Unexpected UndefValue");
assert(!isa<ConstantInt>(C) && "Unexpected Integer UnaryOp");

if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
const APFloat &CV = CFP->getValueAPF();
switch (Opcode) {
default:
break;
case Instruction::FNeg:
if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
const APFloat &CV = CFP->getValueAPF();
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);
} 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));
}

Result.push_back(ConstantExpr::get(Opcode, Elt));
return ConstantVector::get(Result);
}

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;
}
break;
}
case Instruction::UnaryOpsEnd:
llvm_unreachable("Invalid UnaryOp");
}

// We don't know how to fold this.
@@ -3410,6 +3410,11 @@ LLVMValueRef LLVMBuildFNeg(LLVMBuilderRef B, LLVMValueRef V, const char *Name) {
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));
}
@@ -307,6 +307,7 @@ const char *Instruction::getOpcodeName(unsigned OpCode) {

// Standard unary operators...
case FNeg: return "fneg";
case Freeze: return "freeze";

// Standard binary operators...
case Add: return "add";
@@ -2232,6 +2232,9 @@ void UnaryOperator::AssertOK() {
"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
@@ -3145,6 +3145,9 @@ void Verifier::visitUnaryOperator(UnaryOperator &U) {
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!");
}
@@ -267,6 +267,7 @@ let test_constants () =
* 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
@@ -303,6 +304,7 @@ let test_constants () =
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);
@@ -1400,6 +1402,7 @@ let test_builder () =
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_unreachable b)
end;
@@ -0,0 +1,22 @@
; RUN: llvm-as < %s | llvm-dis > %t.orig
; RUN: llvm-as < %s | llvm-c-test --echo > %t.echo
; RUN: diff -w %t.orig %t.echo

%struct.T = type { i32, i32 }

define i32 @f(i32 %arg, <2 x i32> %arg2, float %arg3, <2 x float> %arg4,
i8* %arg5, %struct.T %arg6, [2 x i32] %arg7, { i32, i32 } %arg8) {
%1 = freeze i32 %arg
%2 = freeze i32 10
%3 = freeze i32 %1
%4 = freeze i32 undef
%5 = freeze i666 11
%6 = freeze <2 x i32> %arg2
%7 = freeze float %arg3
%8 = freeze <2 x float> %arg4
%9 = freeze i8* %arg5
%10 = freeze %struct.T %arg6
%11 = freeze [2 x i32] %arg7
%12 = freeze { i32, i32 } %arg8
ret i32 %1
}
@@ -1172,9 +1172,17 @@ continue:
}

; Instructions -- Unary Operations
define void @instructions.unops(double %op1) {
define void @instructions.unops(double %op1, i32 %op2, <2 x i32> %op3, i8* %op4) {
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
}

@@ -1826,6 +1834,10 @@ define void @instructions.strictfp() strictfp {
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)
@@ -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()

@@ -755,6 +755,11 @@ struct FunCloner {
Dst = LLVMBuildInsertValue(Builder, Agg, V, I, Name);
break;
}
case LLVMFreeze: {
LLVMValueRef Arg = CloneValue(LLVMGetOperand(Src, 0));
Dst = LLVMBuildFreeze(Builder, Arg, Name);
break;
}
default:
break;
}