Teach SCEVExpander::InsertCastOfTo to avoid creating inttoptr-of-ptrtoint
authorDan Gohman <gohman@apple.com>
Thu, 16 Apr 2009 15:52:57 +0000 (15:52 +0000)
committerDan Gohman <gohman@apple.com>
Thu, 16 Apr 2009 15:52:57 +0000 (15:52 +0000)
and ptrtoint-of-inttoptr expressions. This fixes a regression in 300.twolf.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@69293 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Analysis/ScalarEvolutionExpander.h
lib/Analysis/ScalarEvolutionExpander.cpp

index 8126a589a45018a06636d4611407efb1346ce473..2f660c9c5b3689aa3e866b47391f00664f5b55e5 100644 (file)
@@ -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,
index d91061b2b3a7cec2de5f48abec35a3a37609d732..0033fb4ae4ae615a8868d96007341ced83a6c30e 100644 (file)
@@ -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<IntToPtrInst>(V))
+      return ITP->getOperand(0);
+  if (opcode == Instruction::IntToPtr && V->getType() == TD.getIntPtrType())
+    if (PtrToIntInst *PTI = dyn_cast<PtrToIntInst>(V))
+      return PTI->getOperand(0);
+
   // FIXME: keep track of the cast instruction.
   if (Constant *C = dyn_cast<Constant>(V))
     return ConstantExpr::getCast(opcode, C, Ty);