From 0425a87a625de7f107642242c21eafb58c2119da Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 21 Aug 2003 19:45:55 +0000 Subject: [PATCH] Implement Assembler/2003-08-21-ConstantExprCast-Fold.llx git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@8015 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/ConstantFold.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 85a89de145a..956397701ff 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -95,6 +95,17 @@ Constant *ConstantFoldCastInstruction(const Constant *V, const Type *DestTy) { if (S1 <= S2 && S2 >= S3 && S1 <= S3) return ConstantExpr::getCast(Op, DestTy); } + } else if (CE->getOpcode() == Instruction::GetElementPtr) { + // If all of the indexes in the GEP are null values, there is no pointer + // adjustment going on. We might as well cast the source pointer. + bool isAllNull = true; + for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) + if (!CE->getOperand(i)->isNullValue()) { + isAllNull = false; + break; + } + if (isAllNull) + return ConstantExpr::getCast(CE->getOperand(0), DestTy); } return ConstRules::get(*V, *V)->castTo(V, DestTy); @@ -137,9 +148,8 @@ Constant *ConstantFoldGetElementPtr(const Constant *C, (IdxList.size() == 1 && IdxList[0]->isNullValue())) return const_cast(C); - // If C is null and all idx's are null, return null of the right type. + // TODO If C is null and all idx's are null, return null of the right type. - // FIXME: Implement folding of GEP constant exprs the same as instcombine does if (const ConstantExpr *CE = dyn_cast(C)) { // Combine Indices - If the source pointer to this getelementptr instruction -- 2.34.1