diff options
author | aqjune <aqjune@gmail.com> | 2019-11-05 15:53:22 +0900 |
---|---|---|
committer | aqjune <aqjune@gmail.com> | 2019-11-05 15:54:56 +0900 |
commit | 58acbce3def63a207b8f5a69318a99666a4aac53 (patch) | |
tree | 876e5223495c373292d079b27cfe70c8d49693a1 /llvm/lib/IR/ConstantFold.cpp | |
parent | 9f34447f3ff525029ec889bf3a82b04678a9d7c0 (diff) | |
download | bcm5719-llvm-58acbce3def63a207b8f5a69318a99666a4aac53.tar.gz bcm5719-llvm-58acbce3def63a207b8f5a69318a99666a4aac53.zip |
[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
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 67 |
1 files changed, 38 insertions, 29 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index a6cd8331008..218a25fdfff 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -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. |