diff options
author | Chris Lattner <sabre@nondot.org> | 2009-11-27 00:29:05 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-11-27 00:29:05 +0000 |
commit | c514c1f5218b8fe7499a0b9a4737860344cf4c43 (patch) | |
tree | 63c2da331934d330f40edf4f9ae3e0dfb5b9f6a3 /lib/Analysis/InstructionSimplify.cpp | |
parent | 304076268a342b2b15e1af2e678d101af3165f4c (diff) |
factor some instcombine simplifications for getelementptr out to a new
SimplifyGEPInst method in InstructionSimplify.h. No functionality change.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@89980 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/InstructionSimplify.cpp')
-rw-r--r-- | lib/Analysis/InstructionSimplify.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index f9953e3c98..7a7eb6b682 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -264,6 +264,34 @@ Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, return 0; } +/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can +/// fold the result. If not, this returns null. +Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps, + const TargetData *TD) { + // getelementptr P -> P. + if (NumOps == 1) + return Ops[0]; + + // TODO. + //if (isa<UndefValue>(Ops[0])) + // return UndefValue::get(GEP.getType()); + + // getelementptr P, 0 -> P. + if (NumOps == 2) + if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1])) + if (C->isZero()) + return Ops[0]; + + // Check to see if this is constant foldable. + for (unsigned i = 0; i != NumOps; ++i) + if (!isa<Constant>(Ops[i])) + return 0; + + return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), + (Constant *const*)Ops+1, NumOps-1); +} + + //=== Helper functions for higher up the class hierarchy. /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can @@ -309,6 +337,10 @@ Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD) { case Instruction::FCmp: return SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), I->getOperand(0), I->getOperand(1), TD); + case Instruction::GetElementPtr: { + SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); + return SimplifyGEPInst(&Ops[0], Ops.size(), TD); + } } } |