diff --git a/llvm/include/llvm/Analysis/ScalarEvolutionExpander.h b/llvm/include/llvm/Analysis/ScalarEvolutionExpander.h index 8126a589a450..2f660c9c5b36 100644 --- a/llvm/include/llvm/Analysis/ScalarEvolutionExpander.h +++ b/llvm/include/llvm/Analysis/ScalarEvolutionExpander.h @@ -83,8 +83,8 @@ namespace llvm { /// InsertCastOfTo - Insert a cast of V to the specified type, doing what /// we can to share the casts. - static Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V, - const Type *Ty); + Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V, + const Type *Ty); /// InsertBinop - Insert the specified binary operator, doing a small amount /// of work to avoid inserting an obviously redundant operation. static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, diff --git a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp index d91061b2b3a7..0033fb4ae4ae 100644 --- a/llvm/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/llvm/lib/Analysis/ScalarEvolutionExpander.cpp @@ -26,6 +26,14 @@ Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, if (opcode == Instruction::BitCast && V->getType() == Ty) return V; + // Short-circuit unnecessary inttoptr<->ptrtoint casts. + if (opcode == Instruction::PtrToInt && Ty == TD.getIntPtrType()) + if (IntToPtrInst *ITP = dyn_cast(V)) + return ITP->getOperand(0); + if (opcode == Instruction::IntToPtr && V->getType() == TD.getIntPtrType()) + if (PtrToIntInst *PTI = dyn_cast(V)) + return PTI->getOperand(0); + // FIXME: keep track of the cast instruction. if (Constant *C = dyn_cast(V)) return ConstantExpr::getCast(opcode, C, Ty);