aboutsummaryrefslogtreecommitdiffstats
path: root/lib/VMCore/ConstantFold.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2003-06-26 05:22:45 +0000
committerChris Lattner <sabre@nondot.org>2003-06-26 05:22:45 +0000
commit6a2a60aaf36bd18577c5825afbbd9930600d515c (patch)
tree670035417b17483e2bd59c28d86c3e3c00566c13 /lib/VMCore/ConstantFold.cpp
parent833b8a4181f3a8a9f41fea373c0081aa97efd20b (diff)
downloadexternal_llvm-6a2a60aaf36bd18577c5825afbbd9930600d515c.zip
external_llvm-6a2a60aaf36bd18577c5825afbbd9930600d515c.tar.gz
external_llvm-6a2a60aaf36bd18577c5825afbbd9930600d515c.tar.bz2
Implement more aggressive folding of constant GEP instructions
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6913 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/ConstantFold.cpp')
-rw-r--r--lib/VMCore/ConstantFold.cpp31
1 files changed, 25 insertions, 6 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp
index 7644c1e..0edf6a7 100644
--- a/lib/VMCore/ConstantFold.cpp
+++ b/lib/VMCore/ConstantFold.cpp
@@ -141,12 +141,30 @@ Constant *ConstantFoldGetElementPtr(const Constant *C,
// FIXME: Implement folding of GEP constant exprs the same as instcombine does
- // Implement folding of:
- // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
- // long 0, long 0)
- // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
- //
- if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
+ if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
+ // Implement folding of:
+ // void ()** getelementptr (%struct..TorRec* getelementptr
+ // ([N x %struct..TorRec]* %llvm.global_dtors, long 0, long 0),
+ // long 0, ubyte 1)
+ // Into:
+ // %struct..TorRec* getelementptr ([N x %struct..TorRec]*
+ // %llvm.global_dtors, long 0, long 0, ubyte 1)
+ //
+ if (CE->getOpcode() == Instruction::GetElementPtr)
+ if (IdxList[0] == Constant::getNullValue(Type::LongTy)) {
+ std::vector<Constant*> NewIndices;
+ NewIndices.reserve(IdxList.size() + CE->getNumOperands());
+ for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
+ NewIndices.push_back(cast<Constant>(CE->getOperand(i)));
+ NewIndices.insert(NewIndices.end(), IdxList.begin()+1, IdxList.end());
+ return ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices);
+ }
+
+ // Implement folding of:
+ // int* getelementptr ([2 x int]* cast ([3 x int]* %X to [2 x int]*),
+ // long 0, long 0)
+ // To: int* getelementptr ([3 x int]* %X, long 0, long 0)
+ //
if (CE->getOpcode() == Instruction::Cast && IdxList.size() > 1 &&
IdxList[0]->isNullValue())
if (const PointerType *SPT =
@@ -157,6 +175,7 @@ Constant *ConstantFoldGetElementPtr(const Constant *C,
if (CAT->getElementType() == SAT->getElementType())
return ConstantExpr::getGetElementPtr(
(Constant*)CE->getOperand(0), IdxList);
+ }
return 0;
}