diff options
author | Chris Lattner <sabre@nondot.org> | 2002-06-25 16:13:24 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2002-06-25 16:13:24 +0000 |
commit | 7e70829632f82de15db187845666aaca6e04b792 (patch) | |
tree | 48dd2d804e7ebec9a3cbd8bf229cb2a2aa20dce5 /lib/Transforms/Utils | |
parent | 0b12b5f50ec77a8bd01b92d287c52d748619bb4b (diff) | |
download | external_llvm-7e70829632f82de15db187845666aaca6e04b792.tar.gz external_llvm-7e70829632f82de15db187845666aaca6e04b792.tar.bz2 external_llvm-7e70829632f82de15db187845666aaca6e04b792.zip |
MEGAPATCH checkin.
For details, See: docs/2002-06-25-MegaPatchInfo.txt
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2779 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils')
-rw-r--r-- | lib/Transforms/Utils/LowerAllocations.cpp | 44 | ||||
-rw-r--r-- | lib/Transforms/Utils/PromoteMemoryToRegister.cpp | 21 |
2 files changed, 32 insertions, 33 deletions
diff --git a/lib/Transforms/Utils/LowerAllocations.cpp b/lib/Transforms/Utils/LowerAllocations.cpp index 80eab61b75..003be33182 100644 --- a/lib/Transforms/Utils/LowerAllocations.cpp +++ b/lib/Transforms/Utils/LowerAllocations.cpp @@ -40,12 +40,12 @@ public: // doPassInitialization - For the lower allocations pass, this ensures that a // module contains a declaration for a malloc and a free function. // - bool doInitialization(Module *M); + bool doInitialization(Module &M); // runOnBasicBlock - This method does the actual work of converting // instructions over, assuming that the pass has already been initialized. // - bool runOnBasicBlock(BasicBlock *BB); + bool runOnBasicBlock(BasicBlock &BB); }; } @@ -61,7 +61,7 @@ Pass *createLowerAllocationsPass(const TargetData &TD) { // // This function is always successful. // -bool LowerAllocations::doInitialization(Module *M) { +bool LowerAllocations::doInitialization(Module &M) { const FunctionType *MallocType = FunctionType::get(PointerType::get(Type::SByteTy), vector<const Type*>(1, Type::UIntTy), false); @@ -70,8 +70,8 @@ bool LowerAllocations::doInitialization(Module *M) { vector<const Type*>(1, PointerType::get(Type::SByteTy)), false); - MallocFunc = M->getOrInsertFunction("malloc", MallocType); - FreeFunc = M->getOrInsertFunction("free" , FreeType); + MallocFunc = M.getOrInsertFunction("malloc", MallocType); + FreeFunc = M.getOrInsertFunction("free" , FreeType); return true; } @@ -79,17 +79,18 @@ bool LowerAllocations::doInitialization(Module *M) { // runOnBasicBlock - This method does the actual work of converting // instructions over, assuming that the pass has already been initialized. // -bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) { +bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) { bool Changed = false; - assert(MallocFunc && FreeFunc && BB && "Pass not initialized!"); + assert(MallocFunc && FreeFunc && "Pass not initialized!"); + + BasicBlock::InstListType &BBIL = BB.getInstList(); // Loop over all of the instructions, looking for malloc or free instructions - for (unsigned i = 0; i != BB->size(); ++i) { - BasicBlock::InstListType &BBIL = BB->getInstList(); - if (MallocInst *MI = dyn_cast<MallocInst>(*(BBIL.begin()+i))) { - BBIL.remove(BBIL.begin()+i); // remove the malloc instr... - - const Type *AllocTy = cast<PointerType>(MI->getType())->getElementType(); + for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) { + if (MallocInst *MI = dyn_cast<MallocInst>(&*I)) { + BBIL.remove(I); // remove the malloc instr... + + const Type *AllocTy = MI->getType()->getElementType(); // Get the number of bytes to be allocated for one element of the // requested type... @@ -103,35 +104,34 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock *BB) { // Multiply it by the array size if neccesary... MallocArg = BinaryOperator::create(Instruction::Mul,MI->getOperand(0), MallocArg); - BBIL.insert(BBIL.begin()+i++, cast<Instruction>(MallocArg)); + I = ++BBIL.insert(I, cast<Instruction>(MallocArg)); } // Create the call to Malloc... CallInst *MCall = new CallInst(MallocFunc, vector<Value*>(1, MallocArg)); - BBIL.insert(BBIL.begin()+i, MCall); + I = BBIL.insert(I, MCall); // Create a cast instruction to convert to the right type... CastInst *MCast = new CastInst(MCall, MI->getType()); - BBIL.insert(BBIL.begin()+i+1, MCast); + I = BBIL.insert(++I, MCast); // Replace all uses of the old malloc inst with the cast inst MI->replaceAllUsesWith(MCast); delete MI; // Delete the malloc inst Changed = true; ++NumLowered; - } else if (FreeInst *FI = dyn_cast<FreeInst>(*(BBIL.begin()+i))) { - BBIL.remove(BB->getInstList().begin()+i); + } else if (FreeInst *FI = dyn_cast<FreeInst>(&*I)) { + BBIL.remove(I); // Cast the argument to free into a ubyte*... CastInst *MCast = new CastInst(FI->getOperand(0), PointerType::get(Type::UByteTy)); - BBIL.insert(BBIL.begin()+i, MCast); + I = ++BBIL.insert(I, MCast); // Insert a call to the free function... - CallInst *FCall = new CallInst(FreeFunc, - vector<Value*>(1, MCast)); - BBIL.insert(BBIL.begin()+i+1, FCall); + CallInst *FCall = new CallInst(FreeFunc, vector<Value*>(1, MCast)); + I = BBIL.insert(I, FCall); // Delete the old free instruction delete FI; diff --git a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp index 1afb11a986..8a81ac7131 100644 --- a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp +++ b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp @@ -52,7 +52,7 @@ namespace { // runOnFunction - To run this pass, first we calculate the alloca // instructions that are safe for promotion, then we promote each one. // - virtual bool runOnFunction(Function *F); + virtual bool runOnFunction(Function &F); // getAnalysisUsage - We need dominance frontiers // @@ -65,7 +65,7 @@ namespace { void Traverse(BasicBlock *BB, BasicBlock *Pred, vector<Value*> &IncVals, set<BasicBlock*> &Visited); bool QueuePhiNode(BasicBlock *BB, unsigned AllocaIdx); - void FindSafeAllocas(Function *F); + void FindSafeAllocas(Function &F); }; } // end of anonymous namespace @@ -102,12 +102,12 @@ static inline bool isSafeAlloca(const AllocaInst *AI) { // FindSafeAllocas - Find allocas that are safe to promote // -void PromotePass::FindSafeAllocas(Function *F) { - BasicBlock *BB = F->getEntryNode(); // Get the entry node for the function +void PromotePass::FindSafeAllocas(Function &F) { + BasicBlock &BB = F.getEntryNode(); // Get the entry node for the function // Look at all instructions in the entry node - for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) - if (AllocaInst *AI = dyn_cast<AllocaInst>(*I)) // Is it an alloca? + for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) + if (AllocaInst *AI = dyn_cast<AllocaInst>(&*I)) // Is it an alloca? if (isSafeAlloca(AI)) { // If safe alloca, add alloca to safe list AllocaLookup[AI] = Allocas.size(); // Keep reverse mapping Allocas.push_back(AI); @@ -116,7 +116,7 @@ void PromotePass::FindSafeAllocas(Function *F) { -bool PromotePass::runOnFunction(Function *F) { +bool PromotePass::runOnFunction(Function &F) { // Calculate the set of safe allocas FindSafeAllocas(F); @@ -178,7 +178,7 @@ bool PromotePass::runOnFunction(Function *F) { // and inserting the phi nodes we marked as necessary // set<BasicBlock*> Visited; // The basic blocks we've already visited - Traverse(F->front(), 0, Values, Visited); + Traverse(F.begin(), 0, Values, Visited); // Remove all instructions marked by being placed in the KillList... // @@ -186,8 +186,7 @@ bool PromotePass::runOnFunction(Function *F) { Instruction *I = KillList.back(); KillList.pop_back(); - I->getParent()->getInstList().remove(I); - delete I; + I->getParent()->getInstList().erase(I); } NumPromoted += Allocas.size(); @@ -248,7 +247,7 @@ void PromotePass::Traverse(BasicBlock *BB, BasicBlock *Pred, // keep track of the value of each variable we're watching.. how? for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) { - Instruction *I = *II; //get the instruction + Instruction *I = II; // get the instruction if (LoadInst *LI = dyn_cast<LoadInst>(I)) { Value *Ptr = LI->getPointerOperand(); |