[llvm-commits] CVS: llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp IndVarSimplify.cpp Reassociate.cpp

Chris Lattner lattner at cs.uiuc.edu
Tue Sep 10 12:05:00 PDT 2002


Changes in directory llvm/lib/Transforms/Scalar:

DecomposeMultiDimRefs.cpp updated: 1.22 -> 1.23
IndVarSimplify.cpp updated: 1.29 -> 1.30
Reassociate.cpp updated: 1.9 -> 1.10

---
Log message:

Simplify code (somtimes dramatically), by using the new "auto-insert" feature
of instruction constructors.



---
Diffs of the changes:

Index: llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp
diff -u llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp:1.22 llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp:1.23
--- llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp:1.22	Thu Aug 22 18:36:48 2002
+++ llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp	Tue Sep 10 12:04:01 2002
@@ -18,14 +18,13 @@
 #include "llvm/Pass.h"
 #include "Support/StatisticReporter.h"
 
-static Statistic<> NumAdded("lowerrefs\t\t- New instructions added");
-
 namespace {
-  struct DecomposePass : public BasicBlockPass {
-    virtual bool runOnBasicBlock(BasicBlock &BB);
+  Statistic<> NumAdded("lowerrefs\t\t- # of getelementptr instructions added");
 
-  private:
-    static bool decomposeArrayRef(BasicBlock::iterator &BBI);
+  class DecomposePass : public BasicBlockPass {
+    static bool decomposeArrayRef(GetElementPtrInst &GEP);
+  public:
+    virtual bool runOnBasicBlock(BasicBlock &BB);
   };
 
   RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
@@ -47,23 +46,15 @@
 {
   bool Changed = false;
   for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) {
-    if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&*II))
-      if (GEP->getNumIndices() >= 2) {
-        Changed |= decomposeArrayRef(II); // always modifies II
-        continue;
-      }
+    Instruction *I = II;
     ++II;
+    if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
+      if (GEP->getNumIndices() >= 2)
+        Changed |= decomposeArrayRef(*GEP); // always modifies II
   }
   return Changed;
 }
 
-// Check for a constant (uint) 0.
-inline bool
-IsZero(Value* idx)
-{
-  return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
-}
-
 // For any GetElementPtrInst with 2 or more array and structure indices:
 // 
 //      opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
@@ -86,17 +77,11 @@
 // Return value: true if the instruction was replaced; false otherwise.
 // 
 bool
-DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI)
+DecomposePass::decomposeArrayRef(GetElementPtrInst &GEP)
 {
-  GetElementPtrInst &GEP = cast<GetElementPtrInst>(*BBI);
   BasicBlock *BB = GEP.getParent();
   Value *LastPtr = GEP.getPointerOperand();
-
-  // Remove the instruction from the stream
-  BB->getInstList().remove(BBI);
-
-  // The vector of new instructions to be created
-  std::vector<Instruction*> NewInsts;
+  Instruction *InsertPoint = GEP.getNext(); // Insert before the next insn
 
   // Process each index except the last one.
   User::const_op_iterator OI = GEP.idx_begin(), OE = GEP.idx_end();
@@ -105,16 +90,17 @@
     
     // If this is the first index and is 0, skip it and move on!
     if (OI == GEP.idx_begin()) {
-      if (IsZero(*OI)) continue;
-    } else
+      if (*OI == ConstantInt::getNullValue((*OI)->getType()))
+        continue;
+    } else {
       // Not the first index: include initial [0] to deref the last ptr
       Indices.push_back(Constant::getNullValue(Type::UIntTy));
+    }
 
     Indices.push_back(*OI);
 
     // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
-    LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
-    NewInsts.push_back(cast<Instruction>(LastPtr));
+    LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
     ++NumAdded;
   }
 
@@ -127,20 +113,13 @@
   Indices.push_back(Constant::getNullValue(Type::UIntTy));
   Indices.push_back(*OI);
 
-  Instruction *NewI = new GetElementPtrInst(LastPtr, Indices, GEP.getName());
-  NewInsts.push_back(NewI);
+  Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP.getName(),
+                                        InsertPoint);
 
   // Replace all uses of the old instruction with the new
-  GEP.replaceAllUsesWith(NewI);
-
-  // Now delete the old instruction...
-  delete &GEP;
-
-  // Insert all of the new instructions...
-  BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
+  GEP.replaceAllUsesWith(NewVal);
 
-  // Advance the iterator to the instruction following the one just inserted...
-  BBI = NewInsts.back();
-  ++BBI;
+  // Now remove and delete the old instruction...
+  BB->getInstList().erase(&GEP);
   return true;
 }


Index: llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
diff -u llvm/lib/Transforms/Scalar/IndVarSimplify.cpp:1.29 llvm/lib/Transforms/Scalar/IndVarSimplify.cpp:1.30
--- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp:1.29	Tue Sep 10 00:24:05 2002
+++ llvm/lib/Transforms/Scalar/IndVarSimplify.cpp	Tue Sep 10 12:04:01 2002
@@ -25,11 +25,8 @@
 // name...
 //
 static Instruction *InsertCast(Value *Val, const Type *Ty,
-                               BasicBlock::iterator It) {
-  Instruction *Cast = new CastInst(Val, Ty);
-  if (Val->hasName()) Cast->setName(Val->getName()+"-casted");
-  It->getParent()->getInstList().insert(It, Cast);
-  return Cast;
+                               Instruction *InsertBefore) {
+  return new CastInst(Val, Ty, Val->getName()+"-casted", InsertBefore);
 }
 
 static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
@@ -75,19 +72,14 @@
   // Okay, we want to convert other induction variables to use a cannonical
   // indvar.  If we don't have one, add one now...
   if (!Cannonical) {
-    // Create the PHI node for the new induction variable
-    PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar");
-
-    // Insert the phi node at the end of the other phi nodes...
-    AfterPHIIt = ++Header->getInstList().insert(AfterPHIIt, PN);
+    // Create the PHI node for the new induction variable, and insert the phi
+    // node at the end of the other phi nodes...
+    PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar", AfterPHIIt);
 
     // Create the increment instruction to add one to the counter...
     Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
                                               ConstantUInt::get(Type::UIntTy,1),
-                                              "add1-indvar");
-
-    // Insert the add instruction after all of the PHI nodes...
-    Header->getInstList().insert(AfterPHIIt, Add);
+                                              "add1-indvar", AfterPHIIt);
 
     // Figure out which block is incoming and which is the backedge for the loop
     BasicBlock *Incoming, *BackEdgeBlock;
@@ -147,9 +139,7 @@
           IV->Step = InsertCast(IV->Step, IVTy, AfterPHIIt);
 
         Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step,
-                                     IV->Phi->getName()+"-scale");
-        // Insert the phi node at the end of the other phi nodes...
-        Header->getInstList().insert(AfterPHIIt, Val);
+                                     IV->Phi->getName()+"-scale", AfterPHIIt);
       }
 
       // If the start != 0
@@ -160,11 +150,9 @@
         if (IV->Start->getType() != IVTy)
           IV->Start = InsertCast(IV->Start, IVTy, AfterPHIIt);
 
+        // Insert the instruction after the phi nodes...
         Val = BinaryOperator::create(Instruction::Add, Val, IV->Start,
-                                     IV->Phi->getName()+"-offset");
-
-        // Insert the phi node at the end of the other phi nodes...
-        Header->getInstList().insert(AfterPHIIt, Val);
+                                     IV->Phi->getName()+"-offset", AfterPHIIt);
       }
 
       // If the PHI node has a different type than val is, insert a cast now...


Index: llvm/lib/Transforms/Scalar/Reassociate.cpp
diff -u llvm/lib/Transforms/Scalar/Reassociate.cpp:1.9 llvm/lib/Transforms/Scalar/Reassociate.cpp:1.10
--- llvm/lib/Transforms/Scalar/Reassociate.cpp:1.9	Fri Jul 26 16:12:41 2002
+++ llvm/lib/Transforms/Scalar/Reassociate.cpp	Tue Sep 10 12:04:02 2002
@@ -180,12 +180,9 @@
       // adding it now, we are assured that the neg instructions we just
       // inserted dominate the instruction we are about to insert after them.
       //
-      BasicBlock::iterator NBI = cast<Instruction>(RHS);
-
-      Instruction *Add =
-        BinaryOperator::create(Instruction::Add, LHS, RHS, I->getName()+".neg");
-      BB->getInstList().insert(++NBI, Add);  // Add to the basic block...
-      return Add;
+      return BinaryOperator::create(Instruction::Add, LHS, RHS,
+                                    I->getName()+".neg",
+                                    cast<Instruction>(RHS)->getNext());
     }
 
   // Insert a 'neg' instruction that subtracts the value from zero to get the
@@ -194,8 +191,8 @@
   Instruction *Neg =
     BinaryOperator::create(Instruction::Sub,
                            Constant::getNullValue(V->getType()), V,
-                           V->getName()+".neg");
-  BI = BB->getInstList().insert(BI, Neg);  // Add to the basic block...
+                           V->getName()+".neg", BI);
+  --BI;
   return Neg;
 }
 
@@ -220,8 +217,9 @@
           // Insert a new temporary instruction... (A+B)+C
           BinaryOperator *Tmp = BinaryOperator::create(I->getOpcode(), LHSI,
                                                        RHSI->getOperand(0),
-                                                       RHSI->getName()+".ra");
-          BI = BB->getInstList().insert(BI, Tmp);  // Add to the basic block...
+                                                       RHSI->getName()+".ra",
+                                                       BI);
+          BI = Tmp;
           I->setOperand(0, Tmp);
           I->setOperand(1, RHSI->getOperand(1));
 





More information about the llvm-commits mailing list