Implement a FIXME: if we're going to insert a cast, we might as well only
authorChris Lattner <sabre@nondot.org>
Wed, 14 Apr 2004 22:01:22 +0000 (22:01 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 14 Apr 2004 22:01:22 +0000 (22:01 +0000)
insert it once!

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

lib/Analysis/ScalarEvolution.cpp

index 62247e42ac499c5f381338707787609597dd68ed..f96cd5d82b81ea817c93d9a75a74c33f6bc5ed02 100644 (file)
@@ -2484,7 +2484,21 @@ Value *ScalarEvolutionRewriter::ExpandCodeFor(SCEVHandle SH,
   if (Constant *C = dyn_cast<Constant>(V))
     return ConstantExpr::getCast(C, Ty);
   else if (Instruction *I = dyn_cast<Instruction>(V)) {
-    // FIXME: check to see if there is already a cast!
+    // Check to see if there is already a cast.  If there is, use it.
+    for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); 
+         UI != E; ++UI) {
+      if ((*UI)->getType() == Ty)
+        if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) {
+          BasicBlock::iterator It = I; ++It;
+          if (It != BasicBlock::iterator(CI)) {
+            // Splice the cast immediately after the operand in question.
+            I->getParent()->getInstList().splice(It,
+                                                 CI->getParent()->getInstList(),
+                                                 CI);
+          }
+          return CI;
+        }
+    }
     BasicBlock::iterator IP = I; ++IP;
     if (InvokeInst *II = dyn_cast<InvokeInst>(I))
       IP = II->getNormalDest()->begin();