diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2021-09-26 11:33:13 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2021-11-13 20:49:29 +0000 |
| commit | 28a41182c08e79534be77131840bcfdf73d31343 (patch) | |
| tree | 8a1fb5602dbadb061e195f566952ce7f59ce5682 /contrib/llvm-project/llvm/lib/IR/ConstantFold.cpp | |
| parent | 8c6f6c0c80f896f9c90b7833def615dcb0ce9244 (diff) | |
| parent | 3f25e997d96a3150a192777c3c389c258c5cf7ee (diff) | |
Diffstat (limited to 'contrib/llvm-project/llvm/lib/IR/ConstantFold.cpp')
| -rw-r--r-- | contrib/llvm-project/llvm/lib/IR/ConstantFold.cpp | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/contrib/llvm-project/llvm/lib/IR/ConstantFold.cpp b/contrib/llvm-project/llvm/lib/IR/ConstantFold.cpp index 513fe0fdd608..e1e28d1230b0 100644 --- a/contrib/llvm-project/llvm/lib/IR/ConstantFold.cpp +++ b/contrib/llvm-project/llvm/lib/IR/ConstantFold.cpp @@ -459,7 +459,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, V.convertToInteger(IntVal, APFloat::rmTowardZero, &ignored)) { // Undefined behavior invoked - the destination type can't represent // the input constant. - return UndefValue::get(DestTy); + return PoisonValue::get(DestTy); } return ConstantInt::get(FPC->getContext(), IntVal); } @@ -694,7 +694,7 @@ Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val, unsigned NumElts = ValTy->getNumElements(); if (CIdx->uge(NumElts)) - return UndefValue::get(Val->getType()); + return PoisonValue::get(Val->getType()); SmallVector<Constant*, 16> Result; Result.reserve(NumElts); @@ -922,23 +922,21 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, } case Instruction::SDiv: case Instruction::UDiv: - // X / undef -> undef - if (isa<UndefValue>(C2)) - return C2; - // undef / 0 -> undef + // X / undef -> poison + // X / 0 -> poison + if (match(C2, m_CombineOr(m_Undef(), m_Zero()))) + return PoisonValue::get(C2->getType()); // undef / 1 -> undef - if (match(C2, m_Zero()) || match(C2, m_One())) + if (match(C2, m_One())) return C1; // undef / X -> 0 otherwise return Constant::getNullValue(C1->getType()); case Instruction::URem: case Instruction::SRem: - // X % undef -> undef - if (match(C2, m_Undef())) - return C2; - // undef % 0 -> undef - if (match(C2, m_Zero())) - return C1; + // X % undef -> poison + // X % 0 -> poison + if (match(C2, m_CombineOr(m_Undef(), m_Zero()))) + return PoisonValue::get(C2->getType()); // undef % X -> 0 otherwise return Constant::getNullValue(C1->getType()); case Instruction::Or: // X | undef -> -1 @@ -946,28 +944,28 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, return C1; return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 case Instruction::LShr: - // X >>l undef -> undef + // X >>l undef -> poison if (isa<UndefValue>(C2)) - return C2; + return PoisonValue::get(C2->getType()); // undef >>l 0 -> undef if (match(C2, m_Zero())) return C1; // undef >>l X -> 0 return Constant::getNullValue(C1->getType()); case Instruction::AShr: - // X >>a undef -> undef + // X >>a undef -> poison if (isa<UndefValue>(C2)) - return C2; + return PoisonValue::get(C2->getType()); // undef >>a 0 -> undef if (match(C2, m_Zero())) return C1; - // TODO: undef >>a X -> undef if the shift is exact + // TODO: undef >>a X -> poison if the shift is exact // undef >>a X -> 0 return Constant::getNullValue(C1->getType()); case Instruction::Shl: // X << undef -> undef if (isa<UndefValue>(C2)) - return C2; + return PoisonValue::get(C2->getType()); // undef << 0 -> undef if (match(C2, m_Zero())) return C1; @@ -1020,14 +1018,14 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, if (CI2->isOne()) return C1; // X / 1 == X if (CI2->isZero()) - return UndefValue::get(CI2->getType()); // X / 0 == undef + return PoisonValue::get(CI2->getType()); // X / 0 == poison break; case Instruction::URem: case Instruction::SRem: if (CI2->isOne()) return Constant::getNullValue(CI2->getType()); // X % 1 == 0 if (CI2->isZero()) - return UndefValue::get(CI2->getType()); // X % 0 == undef + return PoisonValue::get(CI2->getType()); // X % 0 == poison break; case Instruction::And: if (CI2->isZero()) return C2; // X & 0 == 0 @@ -1141,7 +1139,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, case Instruction::SDiv: assert(!CI2->isZero() && "Div by zero handled above"); if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) - return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef + return PoisonValue::get(CI1->getType()); // MIN_INT / -1 -> poison return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V)); case Instruction::URem: assert(!CI2->isZero() && "Div by zero handled above"); @@ -1149,7 +1147,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, case Instruction::SRem: assert(!CI2->isZero() && "Div by zero handled above"); if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) - return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef + return PoisonValue::get(CI1->getType()); // MIN_INT % -1 -> poison return ConstantInt::get(CI1->getContext(), C1V.srem(C2V)); case Instruction::And: return ConstantInt::get(CI1->getContext(), C1V & C2V); @@ -1160,15 +1158,15 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, case Instruction::Shl: if (C2V.ult(C1V.getBitWidth())) return ConstantInt::get(CI1->getContext(), C1V.shl(C2V)); - return UndefValue::get(C1->getType()); // too big shift is undef + return PoisonValue::get(C1->getType()); // too big shift is poison case Instruction::LShr: if (C2V.ult(C1V.getBitWidth())) return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V)); - return UndefValue::get(C1->getType()); // too big shift is undef + return PoisonValue::get(C1->getType()); // too big shift is poison case Instruction::AShr: if (C2V.ult(C1V.getBitWidth())) return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V)); - return UndefValue::get(C1->getType()); // too big shift is undef + return PoisonValue::get(C1->getType()); // too big shift is poison } } @@ -1214,7 +1212,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, // Fast path for splatted constants. if (Constant *C2Splat = C2->getSplatValue()) { if (Instruction::isIntDivRem(Opcode) && C2Splat->isNullValue()) - return UndefValue::get(VTy); + return PoisonValue::get(VTy); if (Constant *C1Splat = C1->getSplatValue()) { return ConstantVector::getSplat( VTy->getElementCount(), @@ -1231,9 +1229,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1, Constant *LHS = ConstantExpr::getExtractElement(C1, ExtractIdx); Constant *RHS = ConstantExpr::getExtractElement(C2, ExtractIdx); - // If any element of a divisor vector is zero, the whole op is undef. + // If any element of a divisor vector is zero, the whole op is poison. if (Instruction::isIntDivRem(Opcode) && RHS->isNullValue()) - return UndefValue::get(VTy); + return PoisonValue::get(VTy); Result.push_back(ConstantExpr::get(Opcode, LHS, RHS)); } @@ -2209,7 +2207,8 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, return PoisonValue::get(GEPTy); if (isa<UndefValue>(C)) - return UndefValue::get(GEPTy); + // If inbounds, we can choose an out-of-bounds pointer as a base pointer. + return InBounds ? PoisonValue::get(GEPTy) : UndefValue::get(GEPTy); Constant *Idx0 = cast<Constant>(Idxs[0]); if (Idxs.size() == 1 && (Idx0->isNullValue() || isa<UndefValue>(Idx0))) |
