Commit more code over to new cast style


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@697 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/IPO/InlineSimple.cpp b/lib/Transforms/IPO/InlineSimple.cpp
index 8bc0a77..c8afc27 100644
--- a/lib/Transforms/IPO/InlineSimple.cpp
+++ b/lib/Transforms/IPO/InlineSimple.cpp
@@ -63,12 +63,11 @@
 // method by one level.
 //
 bool opt::InlineMethod(BasicBlock::iterator CIIt) {
-  assert((*CIIt)->getOpcode() == Instruction::Call && 
-	 "InlineMethod only works on CallInst nodes!");
+  assert(isa<CallInst>(*CIIt) && "InlineMethod only works on CallInst nodes!");
   assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
   assert((*CIIt)->getParent()->getParent() && "Instruction not in method!");
 
-  CallInst *CI = (CallInst*)*CIIt;
+  CallInst *CI = cast<CallInst>(*CIIt);
   const Method *CalledMeth = CI->getCalledMethod();
   if (CalledMeth->isExternal()) return false;  // Can't inline external method!
   Method *CurrentMeth = CI->getParent()->getParent();
@@ -152,13 +151,13 @@
     // Copy over the terminator now...
     switch (TI->getOpcode()) {
     case Instruction::Ret: {
-      const ReturnInst *RI = (const ReturnInst*)TI;
+      const ReturnInst *RI = cast<const ReturnInst>(TI);
 
       if (PHI) {   // The PHI node should include this value!
 	assert(RI->getReturnValue() && "Ret should have value!");
 	assert(RI->getReturnValue()->getType() == PHI->getType() && 
 	       "Ret value not consistent in method!");
-	PHI->addIncoming((Value*)RI->getReturnValue(), (BasicBlock*)BB);
+	PHI->addIncoming((Value*)RI->getReturnValue(), cast<BasicBlock>(BB));
       }
 
       // Add a branch to the code that was after the original Call.
@@ -236,9 +235,8 @@
 
 static inline bool DoMethodInlining(BasicBlock *BB) {
   for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
-    if ((*I)->getOpcode() == Instruction::Call) {
+    if (CallInst *CI = dyn_cast<CallInst>(*I)) {
       // Check to see if we should inline this method
-      CallInst *CI = (CallInst*)*I;
       Method *M = CI->getCalledMethod();
       if (ShouldInlineMethod(CI, M))
 	return InlineMethod(I);
diff --git a/lib/Transforms/Scalar/ADCE.cpp b/lib/Transforms/Scalar/ADCE.cpp
index ea36745..18c851b 100644
--- a/lib/Transforms/Scalar/ADCE.cpp
+++ b/lib/Transforms/Scalar/ADCE.cpp
@@ -14,6 +14,7 @@
 #include "llvm/Support/DepthFirstIterator.h"
 #include "llvm/Analysis/Writer.h"
 #include "llvm/iTerminators.h"
+#include "llvm/iOther.h"
 #include <set>
 #include <algorithm>
 
@@ -171,15 +172,15 @@
   set<BasicBlock*> VisitedBlocks;
   BasicBlock *EntryBlock = fixupCFG(M->front(), VisitedBlocks, AliveBlocks);
   if (EntryBlock && EntryBlock != M->front()) {
-    if (EntryBlock->front()->isPHINode()) {
+    if (isa<PHINode>(EntryBlock->front())) {
       // Cannot make the first block be a block with a PHI node in it! Instead,
       // strip the first basic block of the method to contain no instructions,
       // then add a simple branch to the "real" entry node...
       //
       BasicBlock *E = M->front();
-      if (!E->front()->isTerminator() ||   // Check for an actual change...
-	  ((TerminatorInst*)E->front())->getNumSuccessors() != 1 ||
-	  ((TerminatorInst*)E->front())->getSuccessor(0) != EntryBlock) {
+      if (!isa<TerminatorInst>(E->front()) || // Check for an actual change...
+	  cast<TerminatorInst>(E->front())->getNumSuccessors() != 1 ||
+	  cast<TerminatorInst>(E->front())->getSuccessor(0) != EntryBlock) {
 	E->getInstList().delete_all();      // Delete all instructions in block
 	E->getInstList().push_back(new BranchInst(EntryBlock));
 	MadeChanges = true;
diff --git a/lib/Transforms/Scalar/ConstantProp.cpp b/lib/Transforms/Scalar/ConstantProp.cpp
index d43f693..61c026a 100644
--- a/lib/Transforms/Scalar/ConstantProp.cpp
+++ b/lib/Transforms/Scalar/ConstantProp.cpp
@@ -82,8 +82,7 @@
 //
 bool opt::ConstantFoldTerminator(TerminatorInst *T) {
   // Branch - See if we are conditional jumping on constant
-  if (T->getOpcode() == Instruction::Br) {
-    BranchInst *BI = (BranchInst*)T;
+  if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
     if (BI->isUnconditional()) return false;  // Can't optimize uncond branch
     BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0));
     BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1));
@@ -136,22 +135,22 @@
 inline static bool 
 ConstantFoldInstruction(Method *M, Method::inst_iterator &II) {
   Instruction *Inst = *II;
-  if (Inst->isBinaryOp()) {
+  if (BinaryOperator *BInst = dyn_cast<BinaryOperator>(Inst)) {
     ConstPoolVal *D1 = dyn_cast<ConstPoolVal>(Inst->getOperand(0));
     ConstPoolVal *D2 = dyn_cast<ConstPoolVal>(Inst->getOperand(1));
 
     if (D1 && D2)
-      return ConstantFoldBinaryInst(M, II, (BinaryOperator*)Inst, D1, D2);
+      return ConstantFoldBinaryInst(M, II, cast<BinaryOperator>(Inst), D1, D2);
 
-  } else if (Inst->isUnaryOp()) {
-    ConstPoolVal *D = dyn_cast<ConstPoolVal>(Inst->getOperand(0));
-    if (D) return ConstantFoldUnaryInst(M, II, (UnaryOperator*)Inst, D);
-  } else if (Inst->isTerminator()) {
-    return opt::ConstantFoldTerminator((TerminatorInst*)Inst);
+  } else if (UnaryOperator *UInst = dyn_cast<UnaryOperator>(Inst)) {
+    ConstPoolVal *D = dyn_cast<ConstPoolVal>(UInst->getOperand(0));
+    if (D) return ConstantFoldUnaryInst(M, II, UInst, D);
+  } else if (TerminatorInst *TInst = dyn_cast<TerminatorInst>(Inst)) {
+    return opt::ConstantFoldTerminator(TInst);
 
-  } else if (Inst->isPHINode()) {
-    PHINode *PN = (PHINode*)Inst; // If it's a PHI node and only has one operand
-                                  // Then replace it directly with that operand.
+  } else if (PHINode *PN = dyn_cast<PHINode>(Inst)) {
+    // If it's a PHI node and only has one operand
+    // Then replace it directly with that operand.
     assert(PN->getOperand(0) && "PHI Node must have at least one operand!");
     if (PN->getNumOperands() == 1) {    // If the PHI Node has exactly 1 operand
       Value *V = PN->getOperand(0);
diff --git a/lib/Transforms/Scalar/DCE.cpp b/lib/Transforms/Scalar/DCE.cpp
index ba3db99..10dcf1e 100644
--- a/lib/Transforms/Scalar/DCE.cpp
+++ b/lib/Transforms/Scalar/DCE.cpp
@@ -84,7 +84,7 @@
     return false;   // More than one predecessor...
 
   Instruction *I = BB->front();
-  if (!I->isPHINode()) return false;  // No PHI nodes
+  if (!isa<PHINode>(I)) return false;  // No PHI nodes
 
   //cerr << "Killing PHIs from " << BB;
   //cerr << "Pred #0 = " << *BB->pred_begin();
@@ -92,7 +92,7 @@
   //cerr << "Method == " << BB->getParent();
 
   do {
-    PHINode *PN = (PHINode*)I;
+    PHINode *PN = cast<PHINode>(I);
     assert(PN->getNumOperands() == 2 && "PHI node should only have one value!");
     Value *V = PN->getOperand(0);
 
@@ -100,7 +100,7 @@
     delete BB->getInstList().remove(BB->begin());
 
     I = BB->front();
-  } while (I->isPHINode());
+  } while (isa<PHINode>(I));
 	
   return true;  // Yes, we nuked at least one phi node
 }
@@ -120,7 +120,7 @@
 // Assumption: BB is the single predecessor of Succ.
 //
 static void PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
-  assert(Succ->front()->isPHINode() && "Only works on PHId BBs!");
+  assert(isa<PHINode>(Succ->front()) && "Only works on PHId BBs!");
 
   // If there is more than one predecessor, and there are PHI nodes in
   // the successor, then we need to add incoming edges for the PHI nodes
@@ -129,7 +129,7 @@
 
   BasicBlock::iterator I = Succ->begin();
   do {                     // Loop over all of the PHI nodes in the successor BB
-    PHINode *PN = (PHINode*)*I;
+    PHINode *PN = cast<PHINode>(*I);
     Value *OldVal = PN->removeIncomingValue(BB);
     assert(OldVal && "No entry in PHI for Pred BB!");
 
@@ -140,7 +140,7 @@
     }
 
     ++I;
-  } while ((*I)->isPHINode());
+  } while (isa<PHINode>(*I));
 }
 
 
@@ -198,7 +198,7 @@
       //cerr << "Killing Trivial BB: \n" << BB;
       
       if (Succ != BB) {   // Arg, don't hurt infinite loops!
-	if (Succ->front()->isPHINode()) {
+	if (isa<PHINode>(Succ->front())) {
 	  // If our successor has PHI nodes, then we need to update them to
 	  // include entries for BB's predecessors, not for BB itself.
 	  //
diff --git a/lib/Transforms/Scalar/InductionVars.cpp b/lib/Transforms/Scalar/InductionVars.cpp
index f2dcb45..9a8eb12 100644
--- a/lib/Transforms/Scalar/InductionVars.cpp
+++ b/lib/Transforms/Scalar/InductionVars.cpp
@@ -41,7 +41,7 @@
   if (!isa<Instruction>(V))
     return true;  // Constants and arguments are always loop invariant
 
-  BasicBlock *ValueBlock = ((Instruction*)V)->getParent();
+  BasicBlock *ValueBlock = cast<Instruction>(V)->getParent();
   assert(ValueBlock && "Instruction not embedded in basic block!");
 
   // For now, only consider values from outside of the interval, regardless of
@@ -80,8 +80,8 @@
   switch (I->getOpcode()) {       // Handle each instruction seperately
   case Instruction::Add:
   case Instruction::Sub: {
-    Value *SubV1 = ((BinaryOperator*)I)->getOperand(0);
-    Value *SubV2 = ((BinaryOperator*)I)->getOperand(1);
+    Value *SubV1 = cast<BinaryOperator>(I)->getOperand(0);
+    Value *SubV2 = cast<BinaryOperator>(I)->getOperand(1);
     LIVType SubLIVType1 = isLinearInductionVariableH(Int, SubV1, PN);
     if (SubLIVType1 == isOther) return isOther;  // Early bailout
     LIVType SubLIVType2 = isLinearInductionVariableH(Int, SubV2, PN);
@@ -144,12 +144,11 @@
 
   Value *StepExpr = PN->getIncomingValue(1);
   if (!isa<Instruction>(StepExpr) ||
-      ((Instruction*)StepExpr)->getOpcode() != Instruction::Add)
+      cast<Instruction>(StepExpr)->getOpcode() != Instruction::Add)
     return false;
 
-  BinaryOperator *I = (BinaryOperator*)StepExpr;
-  assert(isa<Instruction>(I->getOperand(0)) && 
-      ((Instruction*)I->getOperand(0))->isPHINode() &&
+  BinaryOperator *I = cast<BinaryOperator>(StepExpr);
+  assert(isa<PHINode>(I->getOperand(0)) && 
 	 "PHI node should be first operand of ADD instruction!");
 
   // Get the right hand side of the ADD node.  See if it is a constant 1.
@@ -225,7 +224,7 @@
   // Insert the Add instruction as the first (non-phi) instruction in the 
   // header node's basic block.
   BasicBlock::iterator I = IL.begin();
-  while ((*I)->isPHINode()) ++I;
+  while (isa<PHINode>(*I)) ++I;
   IL.insert(I, AddNode);
   return PN;
 }
@@ -256,8 +255,8 @@
   BasicBlock *Header = Int->getHeaderNode();
   // Loop over all of the PHI nodes in the interval header...
   for (BasicBlock::iterator I = Header->begin(), E = Header->end(); 
-       I != E && (*I)->isPHINode(); ++I) {
-    PHINode *PN = (PHINode*)*I;
+       I != E && isa<PHINode>(*I); ++I) {
+    PHINode *PN = cast<PHINode>(*I);
     if (PN->getNumIncomingValues() != 2) { // These should be eliminated by now.
       cerr << "Found interval header with more than 2 predecessors! Ignoring\n";
       return false;    // Todo, make an assertion.
diff --git a/lib/Transforms/Scalar/SCCP.cpp b/lib/Transforms/Scalar/SCCP.cpp
index 91e002d..b92b54f 100644
--- a/lib/Transforms/Scalar/SCCP.cpp
+++ b/lib/Transforms/Scalar/SCCP.cpp
@@ -270,7 +270,7 @@
       MadeChanges = true;
       continue;   // Skip the ++II at the end of the loop here...
     } else if (Inst->isTerminator()) {
-      MadeChanges |= opt::ConstantFoldTerminator((TerminatorInst*)Inst);
+      MadeChanges |= opt::ConstantFoldTerminator(cast<TerminatorInst>(Inst));
     }
 
     ++II;
@@ -312,7 +312,7 @@
     // Handle PHI nodes...
     //
   case Instruction::PHINode: {
-    PHINode *PN = (PHINode*)I;
+    PHINode *PN = cast<PHINode>(I);
     unsigned NumValues = PN->getNumIncomingValues(), i;
     InstVal *OperandIV = 0;
 
@@ -380,7 +380,7 @@
     //
   case Instruction::Ret: return;  // Method return doesn't affect anything
   case Instruction::Br: {        // Handle conditional branches...
-    BranchInst *BI = (BranchInst*)I;
+    BranchInst *BI = cast<BranchInst>(I);
     if (BI->isUnconditional()) 
       return; // Unconditional branches are already handled!
 
@@ -391,7 +391,7 @@
       markExecutable(BI->getSuccessor(1));
     } else if (BCValue.isConstant()) {
       // Constant condition variables mean the branch can only go a single way.
-      ConstPoolBool *CPB = (ConstPoolBool*)BCValue.getConstant();
+      ConstPoolBool *CPB = cast<ConstPoolBool>(BCValue.getConstant());
       if (CPB->getValue())       // If the branch condition is TRUE...
 	markExecutable(BI->getSuccessor(0));
       else                       // Else if the br cond is FALSE...
@@ -401,7 +401,7 @@
   }
 
   case Instruction::Switch: {
-    SwitchInst *SI = (SwitchInst*)I;
+    SwitchInst *SI = cast<SwitchInst>(I);
     InstVal &SCValue = getValueState(SI->getCondition());
     if (SCValue.isOverdefined()) {  // Overdefined condition?  All dests are exe
       for(unsigned i = 0; BasicBlock *Succ = SI->getSuccessor(i); ++i)
@@ -432,9 +432,9 @@
   //   Also treated as unary here, are cast instructions and getelementptr
   //   instructions on struct* operands.
   //
-  if (I->isUnaryOp() || I->getOpcode() == Instruction::Cast || 
-      (I->getOpcode() == Instruction::GetElementPtr &&
-       ((GetElementPtrInst*)I)->isStructSelector())) {
+  if (isa<UnaryOperator>(I) || isa<CastInst>(I) ||
+      (isa<GetElementPtrInst>(I) &&
+       cast<GetElementPtrInst>(I)->isStructSelector())) {
 
     Value *V = I->getOperand(0);
     InstVal &VState = getValueState(V);
@@ -458,8 +458,7 @@
   //===-----------------------------------------------------------------===//
   // Handle Binary instructions...
   //
-  if (I->isBinaryOp() || I->getOpcode() == Instruction::Shl || 
-      I->getOpcode() == Instruction::Shr) {
+  if (isa<BinaryOperator>(I) || isa<ShiftInst>(I)) {
     Value *V1 = I->getOperand(0);
     Value *V2 = I->getOperand(1);