+#include "llvm/Instructions.h"
+#include "llvm/Constants.h"
+#include "llvm/Pass.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/Target/TargetData.h"
+#include "llvm/Support/Compiler.h"
+using namespace llvm;
+
+STATISTIC(NumLowered, "Number of allocations lowered");
+
+namespace {
+ /// LowerAllocations - Turn malloc and free instructions into %malloc and
+ /// %free calls.
+ ///
+ class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
+ Constant *MallocFunc; // Functions in the module we are processing
+ Constant *FreeFunc; // Initialized by doInitialization
+ bool LowerMallocArgToInteger;
+ public:
+ static char ID; // Pass ID, replacement for typeid
+ explicit LowerAllocations(bool LowerToInt = false)
+ : BasicBlockPass((intptr_t)&ID), MallocFunc(0), FreeFunc(0),
+ LowerMallocArgToInteger(LowerToInt) {}
+
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.addRequired<TargetData>();
+ AU.setPreservesCFG();
+
+ // This is a cluster of orthogonal Transforms:
+ AU.addPreserved<UnifyFunctionExitNodes>();
+ AU.addPreservedID(PromoteMemoryToRegisterID);
+ AU.addPreservedID(LowerSelectID);
+ AU.addPreservedID(LowerSwitchID);
+ AU.addPreservedID(LowerInvokePassID);
+ }
+
+ /// 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);
+
+ virtual bool doInitialization(Function &F) {
+ return BasicBlockPass::doInitialization(F);
+ }
+
+ /// runOnBasicBlock - This method does the actual work of converting
+ /// instructions over, assuming that the pass has already been initialized.
+ ///
+ bool runOnBasicBlock(BasicBlock &BB);
+ };
+
+ char LowerAllocations::ID = 0;
+ RegisterPass<LowerAllocations>
+ X("lowerallocs", "Lower allocations from instructions to calls");
+}
+
+// Publically exposed interface to pass...
+const PassInfo *llvm::LowerAllocationsID = X.getPassInfo();
+// createLowerAllocationsPass - Interface to this file...
+Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
+ return new LowerAllocations(LowerMallocArgToInteger);
+}