[llvm-commits] [PATCH] Add malloc call utility functions

Devang Patel devang.patel at gmail.com
Thu Aug 13 11:06:44 PDT 2009


On Tue, Aug 11, 2009 at 7:31 PM, Victor Hernandez<vhernandez at apple.com> wrote:
> I am working on fixing the MallocInst/i64 alloca bug:
> http://llvm.org/bugs/show_bug.cgi?id=715
>
> Here is the first of a series of patches that will result in tearing out
> MallocInst.  Before I can tear it out, I need to update all of the
> optimization passes and transforms that operate on MallocInst to operate on
> malloc CallInst instead.
>
> This patch consists of a set of utility functions that create IR for malloc
> calls and identify that IR.  It also rewrites LowerAllocations to use the
> new utility function, CallInst::CreateMalloc().
>

+/// ReplaceAndEraseMalloc - For all uses of the malloc call (which should all
+/// be BitCastInsts), replace their uses with the new value.  Then recursively
+/// delete all of the dead malloc instructions (all the bitcasts, the malloc
+/// call, and the instructions that computed the malloc's size argument).
+/// This function is defined in Local.cpp because it uses
+/// llvm::RecursivelyDeleteTriviallyDeadInstructions().
+void llvm::ReplaceAndEraseMalloc(CallInst* CI, Value* newVal) {
+  for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
UI != E; ) {
+    Instruction* I = cast<Instruction>(*UI++);
+    I->replaceAllUsesWith(newVal);
+    RecursivelyDeleteTriviallyDeadInstructions(I);
+  }
+}

This does not erase CI itself. There is not any check to make sure
that CI is malloc instruction. If you want to make it a general
purpose routine then you can drop Malloc from the name.

-
Devang




More information about the llvm-commits mailing list