-//===- llvm/Transforms/LowerAllocations.h - Remove Malloc & Free -*- C++ -*--=//
+//===- llvm/Transforms/ChangeAllocations.h -----------------------*- C++ -*--=//
//
-// This file defines the interface to a pass that lowers malloc and free
-// instructions to calls to %malloc & %free functions. This transformation is
-// a target dependant tranformation because we depend on the size of data types
-// and alignment constraints.
+// This file defines two passes that convert malloc and free instructions to
+// calls to and from %malloc & %free function calls. The LowerAllocations
+// transformation is a target dependant tranformation because it depends on the
+// size of data types and alignment constraints.
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_TRANSFORMS_LOWERALLOCATIONS_H
-#define LLVM_TRANSFORMS_LOWERALLOCATIONS_H
+#ifndef LLVM_TRANSFORMS_CHANGEALLOCATIONS_H
+#define LLVM_TRANSFORMS_CHANGEALLOCATIONS_H
#include "llvm/Pass.h"
class TargetData;
+// LowerAllocations - Turn malloc and free instructions into %malloc and %free
+// calls.
+//
class LowerAllocations : public BasicBlockPass {
Method *MallocMeth; // Methods in the module we are processing
- Method *FreeMeth; // Initialized by doPassInitializationVirt
+ Method *FreeMeth; // Initialized by doInitialization
const TargetData &DataLayout;
public:
bool runOnBasicBlock(BasicBlock *BB);
};
+// RaiseAllocations - Turn %malloc and %free calls into the appropriate
+// instruction.
+//
+class RaiseAllocations : public BasicBlockPass {
+ Method *MallocMeth; // Methods in the module we are processing
+ Method *FreeMeth; // Initialized by doPassInitializationVirt
+public:
+ inline RaiseAllocations() : MallocMeth(0), FreeMeth(0) {}
+
+ // doPassInitialization - For the raise allocations pass, this finds a
+ // declaration for malloc and free if they exist.
+ //
+ 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);
+};
+
#endif
#include "llvm/Analysis/FindUsedTypes.h"
class CleanupGCCOutput : public MethodPass {
- Method *Malloc, *Free; // Pointers to external declarations, or null if none
FindUsedTypes FUT; // Use FUT to eliminate type names that are never used
public:
- inline CleanupGCCOutput() : Malloc(0), Free(0) {}
-
// PatchUpMethodReferences - This is a part of the functionality exported by
// the CleanupGCCOutput pass. This causes functions with different signatures
// to be linked together if they have the same name.
// doPassFinalization - Strip out type names that are unused by the program
bool doFinalization(Module *M);
-private:
- bool doOneCleanupPass(Method *M);
};
#endif
//
// * Eliminate names for GCC types that we know can't be needed by the user.
// * Eliminate names for types that are unused in the entire translation unit
-// * Replace calls to 'sbyte *%malloc(uint)' and 'void %free(sbyte *)' with
-// malloc and free instructions.
//
// Note: This code produces dead declarations, it is a good idea to run DCE
// after this pass.
//
Changed |= PatchUpMethodReferences(M);
-
- // If the module has a symbol table, they might be referring to the malloc
- // and free functions. If this is the case, grab the method pointers that
- // the module is using.
- //
- // Lookup %malloc and %free in the symbol table, for later use. If they
- // don't exist, or are not external, we do not worry about converting calls
- // to that function into the appropriate instruction.
- //
- const PointerType *MallocType = // Get the type for malloc
- PointerType::get(MethodType::get(PointerType::get(Type::SByteTy),
- vector<const Type*>(1, Type::UIntTy), false));
- Malloc = cast_or_null<Method>(ST->lookup(MallocType, "malloc"));
- if (Malloc && !Malloc->isExternal())
- Malloc = 0; // Don't mess with locally defined versions of the fn
-
- const PointerType *FreeType = // Get the type for free
- PointerType::get(MethodType::get(Type::VoidTy,
- vector<const Type*>(1, PointerType::get(Type::SByteTy)), false));
- Free = cast_or_null<Method>(ST->lookup(FreeType, "free"));
- if (Free && !Free->isExternal())
- Free = 0; // Don't mess with locally defined versions of the fn
-
-
// Check the symbol table for superfluous type entries...
//
// Grab the 'type' plane of the module symbol...
}
-// doOneCleanupPass - Do one pass over the input method, fixing stuff up.
-//
-bool CleanupGCCOutput::doOneCleanupPass(Method *M) {
- bool Changed = false;
- for (Method::iterator MI = M->begin(), ME = M->end(); MI != ME; ++MI) {
- BasicBlock *BB = *MI;
- BasicBlock::InstListType &BIL = BB->getInstList();
-
- for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
- Instruction *I = *BI;
-
- if (CallInst *CI = dyn_cast<CallInst>(I)) {
- if (CI->getCalledValue() == Malloc) { // Replace call to malloc?
- MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
- CI->getName());
- CI->setName("");
- BI = BIL.insert(BI, MallocI)+1;
- ReplaceInstWithInst(BIL, BI, new CastInst(MallocI, PtrSByte));
- Changed = true;
- continue; // Skip the ++BI
- } else if (CI->getCalledValue() == Free) { // Replace call to free?
- ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
- Changed = true;
- continue; // Skip the ++BI
- }
- }
-
- ++BI;
- }
- }
-
- return Changed;
-}
-
-
// FixCastsAndPHIs - The LLVM GCC has a tendancy to intermix Cast instructions
// in with the PHI nodes. These cast instructions are potentially there for two
// different reasons:
//
bool CleanupGCCOutput::runOnMethod(Method *M) {
bool Changed = fixLocalProblems(M);
- while (doOneCleanupPass(M)) Changed = true;
FUT.runOnMethod(M);
return Changed;
-//===- LowerAllocations.cpp - Remove Malloc & Free Instructions -------------=//
+//===- ChangeAllocations.cpp - Modify %malloc & %free calls -----------------=//
//
-// This file implements a pass that lowers malloc and free instructions to
-// calls to %malloc & %free functions. This transformation is a target
-// dependant tranformation because we depend on the size of data types and
-// alignment constraints.
+// This file defines two passes that convert malloc and free instructions to
+// calls to and from %malloc & %free function calls. The LowerAllocations
+// transformation is a target dependant tranformation because it depends on the
+// size of data types and alignment constraints.
//
//===----------------------------------------------------------------------===//
#include "llvm/iOther.h"
#include "llvm/SymbolTable.h"
#include "llvm/ConstantVals.h"
+#include "TransformInternals.h"
using std::vector;
return Changed;
}
+bool RaiseAllocations::doInitialization(Module *M) {
+ SymbolTable *ST = M->getSymbolTable();
+ if (!ST) return false;
+
+ // If the module has a symbol table, they might be referring to the malloc
+ // and free functions. If this is the case, grab the method pointers that
+ // the module is using.
+ //
+ // Lookup %malloc and %free in the symbol table, for later use. If they
+ // don't exist, or are not external, we do not worry about converting calls
+ // to that function into the appropriate instruction.
+ //
+ const PointerType *MallocType = // Get the type for malloc
+ PointerType::get(MethodType::get(PointerType::get(Type::SByteTy),
+ vector<const Type*>(1, Type::UIntTy), false));
+ MallocMeth = cast_or_null<Method>(ST->lookup(MallocType, "malloc"));
+ if (MallocMeth && !MallocMeth->isExternal())
+ MallocMeth = 0; // Don't mess with locally defined versions of the fn
+
+ const PointerType *FreeType = // Get the type for free
+ PointerType::get(MethodType::get(Type::VoidTy,
+ vector<const Type*>(1, PointerType::get(Type::SByteTy)), false));
+ FreeMeth = cast_or_null<Method>(ST->lookup(FreeType, "free"));
+ if (FreeMeth && !FreeMeth->isExternal())
+ FreeMeth = 0; // Don't mess with locally defined versions of the fn
+
+ return false;
+}
+
+// doOneCleanupPass - Do one pass over the input method, fixing stuff up.
+//
+bool RaiseAllocations::runOnBasicBlock(BasicBlock *BB) {
+ bool Changed = false;
+ BasicBlock::InstListType &BIL = BB->getInstList();
+
+ for (BasicBlock::iterator BI = BB->begin(); BI != BB->end();) {
+ Instruction *I = *BI;
+
+ if (CallInst *CI = dyn_cast<CallInst>(I)) {
+ if (CI->getCalledValue() == MallocMeth) { // Replace call to malloc?
+ const Type *PtrSByte = PointerType::get(Type::SByteTy);
+ MallocInst *MallocI = new MallocInst(PtrSByte, CI->getOperand(1),
+ CI->getName());
+ CI->setName("");
+ BI = BIL.insert(BI, MallocI)+1;
+ ReplaceInstWithInst(BIL, BI, new CastInst(MallocI, PtrSByte));
+ Changed = true;
+ continue; // Skip the ++BI
+ } else if (CI->getCalledValue() == FreeMeth) { // Replace call to free?
+ ReplaceInstWithInst(BIL, BI, new FreeInst(CI->getOperand(1)));
+ Changed = true;
+ continue; // Skip the ++BI
+ }
+ }
+
+ ++BI;
+ }
+
+ return Changed;
+}
#include "llvm/Transforms/CleanupGCCOutput.h"
#include "llvm/Transforms/LevelChange.h"
#include "llvm/Transforms/ConstantMerge.h"
+#include "llvm/Transforms/LowerAllocations.h"
#include "llvm/Transforms/Scalar/DCE.h"
#include "llvm/Transforms/Scalar/IndVarSimplify.h"
#include "llvm/Transforms/Scalar/InstructionCombining.h"
//
PassManager Passes;
Passes.add(new DeadCodeElimination()); // Remove Dead code/vars
+ Passes.add(new RaiseAllocations()); // call %malloc -> malloc inst
Passes.add(new CleanupGCCOutput()); // Fix gccisms
Passes.add(new InductionVariableSimplify()); // Simplify indvars
Passes.add(new RaisePointerReferences()); // Eliminate casts
#include "llvm/Transforms/LevelChange.h"
#include "llvm/Transforms/MethodInlining.h"
#include "llvm/Transforms/SymbolStripping.h"
+#include "llvm/Transforms/LowerAllocations.h"
#include "llvm/Transforms/IPO/SimpleStructMutation.h"
#include "llvm/Transforms/IPO/GlobalDCE.h"
#include "llvm/Transforms/Scalar/DCE.h"
dce, constprop, inlining, constmerge, strip, mstrip,
// Miscellaneous Transformations
- trace, tracem, print, cleangcc,
+ trace, tracem, print, raiseallocs, cleangcc,
// More powerful optimizations
indvars, instcombine, sccp, adce, raise,
{ trace , new InsertTraceCode(true, true) },
{ tracem , new InsertTraceCode(false, true) },
{ print , new PrintMethodPass("Current Method: \n",&cerr) },
+ { raiseallocs, new RaiseAllocations() },
{ cleangcc , new CleanupGCCOutput() },
{ globaldce , new GlobalDCE() },
{ swapstructs, new SimpleStructMutation(SimpleStructMutation::SwapElements) },
clEnumVal(swapstructs, "Swap structure types around"),
clEnumVal(sortstructs, "Sort structure elements"),
+ clEnumVal(raiseallocs, "Raise allocations from calls to instructions"),
clEnumVal(cleangcc , "Cleanup GCC Output"),
clEnumVal(raise , "Raise to Higher Level"),
clEnumVal(trace , "Insert BB & Method trace code"),