diff options
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
| -rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 149 |
1 files changed, 0 insertions, 149 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 7b8b9a7b652d..693674ae0d06 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -120,66 +120,6 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) { return nullptr; } - -/// V is an integer constant which only has a subset of its bytes used. -/// The bytes used are indicated by ByteStart (which is the first byte used, -/// counting from the least significant byte) and ByteSize, which is the number -/// of bytes used. -/// -/// This function analyzes the specified constant to see if the specified byte -/// range can be returned as a simplified constant. If so, the constant is -/// returned, otherwise null is returned. -static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, - unsigned ByteSize) { - assert(C->getType()->isIntegerTy() && - (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 && - "Non-byte sized integer input"); - [[maybe_unused]] unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8; - assert(ByteSize && "Must be accessing some piece"); - assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input"); - assert(ByteSize != CSize && "Should not extract everything"); - - // Constant Integers are simple. - if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { - APInt V = CI->getValue(); - if (ByteStart) - V.lshrInPlace(ByteStart*8); - V = V.trunc(ByteSize*8); - return ConstantInt::get(CI->getContext(), V); - } - - // In the input is a constant expr, we might be able to recursively simplify. - // If not, we definitely can't do anything. - ConstantExpr *CE = dyn_cast<ConstantExpr>(C); - if (!CE) return nullptr; - - switch (CE->getOpcode()) { - default: return nullptr; - case Instruction::Shl: { - ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1)); - if (!Amt) - return nullptr; - APInt ShAmt = Amt->getValue(); - // Cannot analyze non-byte shifts. - if ((ShAmt & 7) != 0) - return nullptr; - ShAmt.lshrInPlace(3); - - // If the extract is known to be all zeros, return zero. - if (ShAmt.uge(ByteStart + ByteSize)) - return Constant::getNullValue( - IntegerType::get(CE->getContext(), ByteSize * 8)); - // If the extract is known to be fully in the input, extract it. - if (ShAmt.ule(ByteStart)) - return ExtractConstantBytes(CE->getOperand(0), - ByteStart - ShAmt.getZExtValue(), ByteSize); - - // TODO: Handle the 'partially zero' case. - return nullptr; - } - } -} - static Constant *foldMaybeUndesirableCast(unsigned opc, Constant *V, Type *DestTy) { return ConstantExpr::isDesirableCastOp(opc) @@ -313,14 +253,6 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, CI->getValue().trunc(DestBitWidth)); } - // The input must be a constantexpr. See if we can simplify this based on - // the bytes we are demanding. Only do this if the source and dest are an - // even multiple of a byte. - if ((DestBitWidth & 7) == 0 && - (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0) - if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8)) - return Res; - return nullptr; } case Instruction::BitCast: @@ -1404,83 +1336,7 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, return nullptr; } -// Combine Indices - If the source pointer to this getelementptr instruction -// is a getelementptr instruction, combine the indices of the two -// getelementptr instructions into a single instruction. -static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds, - ArrayRef<Value *> Idxs) { - if (PointeeTy != GEP->getResultElementType()) - return nullptr; - - // Leave inrange handling to DL-aware constant folding. - if (GEP->getInRange()) - return nullptr; - - Constant *Idx0 = cast<Constant>(Idxs[0]); - if (Idx0->isNullValue()) { - // Handle the simple case of a zero index. - SmallVector<Value*, 16> NewIndices; - NewIndices.reserve(Idxs.size() + GEP->getNumIndices()); - NewIndices.append(GEP->idx_begin(), GEP->idx_end()); - NewIndices.append(Idxs.begin() + 1, Idxs.end()); - return ConstantExpr::getGetElementPtr( - GEP->getSourceElementType(), cast<Constant>(GEP->getPointerOperand()), - NewIndices, InBounds && GEP->isInBounds()); - } - - gep_type_iterator LastI = gep_type_end(GEP); - for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP); - I != E; ++I) - LastI = I; - - // We can't combine GEPs if the last index is a struct type. - if (!LastI.isSequential()) - return nullptr; - // We could perform the transform with non-constant index, but prefer leaving - // it as GEP of GEP rather than GEP of add for now. - ConstantInt *CI = dyn_cast<ConstantInt>(Idx0); - if (!CI) - return nullptr; - - // TODO: This code may be extended to handle vectors as well. - auto *LastIdx = cast<Constant>(GEP->getOperand(GEP->getNumOperands()-1)); - Type *LastIdxTy = LastIdx->getType(); - if (LastIdxTy->isVectorTy()) - return nullptr; - - SmallVector<Value*, 16> NewIndices; - NewIndices.reserve(Idxs.size() + GEP->getNumIndices()); - NewIndices.append(GEP->idx_begin(), GEP->idx_end() - 1); - - // Add the last index of the source with the first index of the new GEP. - // Make sure to handle the case when they are actually different types. - if (LastIdxTy != Idx0->getType()) { - unsigned CommonExtendedWidth = - std::max(LastIdxTy->getIntegerBitWidth(), - Idx0->getType()->getIntegerBitWidth()); - CommonExtendedWidth = std::max(CommonExtendedWidth, 64U); - - Type *CommonTy = - Type::getIntNTy(LastIdxTy->getContext(), CommonExtendedWidth); - if (Idx0->getType() != CommonTy) - Idx0 = ConstantFoldCastInstruction(Instruction::SExt, Idx0, CommonTy); - if (LastIdx->getType() != CommonTy) - LastIdx = - ConstantFoldCastInstruction(Instruction::SExt, LastIdx, CommonTy); - if (!Idx0 || !LastIdx) - return nullptr; - } - - NewIndices.push_back(ConstantExpr::get(Instruction::Add, Idx0, LastIdx)); - NewIndices.append(Idxs.begin() + 1, Idxs.end()); - - return ConstantExpr::getGetElementPtr( - GEP->getSourceElementType(), cast<Constant>(GEP->getPointerOperand()), - NewIndices, InBounds && GEP->isInBounds()); -} - Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, - bool InBounds, std::optional<ConstantRange> InRange, ArrayRef<Value *> Idxs) { if (Idxs.empty()) return C; @@ -1510,10 +1366,5 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, cast<VectorType>(GEPTy)->getElementCount(), C) : C; - if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) - if (auto *GEP = dyn_cast<GEPOperator>(CE)) - if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs)) - return C; - return nullptr; } |
