diff options
author | Chris Lattner <sabre@nondot.org> | 2002-09-10 17:04:02 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2002-09-10 17:04:02 +0000 |
commit | 2a7c23ef9156a97f426a3fe8d1f5935b75d076d1 (patch) | |
tree | 55142cb1a54f1dfc3c68c196b1070bb94e73b51a /lib/Transforms/Scalar | |
parent | 3ea5cb0df1a992b900b6eefb9da2505792c2c819 (diff) |
Simplify code (somtimes dramatically), by using the new "auto-insert" feature
of instruction constructors.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@3656 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Scalar')
-rw-r--r-- | lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp | 63 | ||||
-rw-r--r-- | lib/Transforms/Scalar/IndVarSimplify.cpp | 30 | ||||
-rw-r--r-- | lib/Transforms/Scalar/Reassociate.cpp | 18 |
3 files changed, 38 insertions, 73 deletions
diff --git a/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp b/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp index b50c4fb5bf..a6d7e37cdb 100644 --- a/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp +++ b/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp @@ -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 @@ DecomposePass::runOnBasicBlock(BasicBlock &BB) { 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 @@ IsZero(Value* idx) // 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 @@ DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) // 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 @@ DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) 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; } diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index fbffb30836..004297ff8f 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -25,11 +25,8 @@ namespace { // 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 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) { // 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 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) { 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 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) { 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... diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp index 24d7dcebe8..7d76bfb681 100644 --- a/lib/Transforms/Scalar/Reassociate.cpp +++ b/lib/Transforms/Scalar/Reassociate.cpp @@ -180,12 +180,9 @@ static Value *NegateValue(Value *V, BasicBlock *BB, BasicBlock::iterator &BI) { // 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 @@ static Value *NegateValue(Value *V, BasicBlock *BB, BasicBlock::iterator &BI) { 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 @@ bool Reassociate::ReassociateBB(BasicBlock *BB) { // 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)); |