aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-07-15 01:08:08 +0000
committerChris Lattner <sabre@nondot.org>2004-07-15 01:08:08 +0000
commit8bfc2f11a481419a5b5d5e63dc432c09c54c8a3a (patch)
tree5028e20484a65197b91b052ecd5c76c14f65b070
parentd2995df5b7b94a42899dab266a7e83b316fa818f (diff)
Now that we codegen the portable "sizeof" efficiently, we can use it for
malloc lowering. This means that lowerallocations doesn't need targetdata anymore. yaay. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14835 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/Transforms/Utils/LowerAllocations.cpp40
1 files changed, 22 insertions, 18 deletions
diff --git a/lib/Transforms/Utils/LowerAllocations.cpp b/lib/Transforms/Utils/LowerAllocations.cpp
index 53b4c3a424..27e0f70315 100644
--- a/lib/Transforms/Utils/LowerAllocations.cpp
+++ b/lib/Transforms/Utils/LowerAllocations.cpp
@@ -19,7 +19,6 @@
#include "llvm/iOther.h"
#include "llvm/Constants.h"
#include "llvm/Pass.h"
-#include "llvm/Target/TargetData.h"
#include "Support/Statistic.h"
using namespace llvm;
@@ -35,10 +34,6 @@ namespace {
public:
LowerAllocations() : MallocFunc(0), FreeFunc(0) {}
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.addRequired<TargetData>();
- }
-
/// doPassInitialization - For the lower allocations pass, this ensures that
/// a module contains a declaration for a malloc and a free function.
///
@@ -78,6 +73,14 @@ bool LowerAllocations::doInitialization(Module &M) {
return true;
}
+static Constant *getSizeof(const Type *Ty) {
+ Constant *Ret = ConstantPointerNull::get(PointerType::get(Ty));
+ std::vector<Constant*> Idx;
+ Idx.push_back(ConstantUInt::get(Type::UIntTy, 1));
+ Ret = ConstantExpr::getGetElementPtr(Ret, Idx);
+ return ConstantExpr::getCast(Ret, Type::UIntTy);
+}
+
// runOnBasicBlock - This method does the actual work of converting
// instructions over, assuming that the pass has already been initialized.
//
@@ -86,25 +89,26 @@ bool LowerAllocations::runOnBasicBlock(BasicBlock &BB) {
assert(MallocFunc && FreeFunc && "Pass not initialized!");
BasicBlock::InstListType &BBIL = BB.getInstList();
- TargetData &DataLayout = getAnalysis<TargetData>();
// Loop over all of the instructions, looking for malloc or free instructions
for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
if (MallocInst *MI = dyn_cast<MallocInst>(I)) {
const Type *AllocTy = MI->getType()->getElementType();
- // Get the number of bytes to be allocated for one element of the
- // requested type...
- unsigned Size = DataLayout.getTypeSize(AllocTy);
-
- // malloc(type) becomes sbyte *malloc(constint)
- Value *MallocArg = ConstantUInt::get(Type::UIntTy, Size);
- if (MI->getNumOperands() && Size == 1) {
- MallocArg = MI->getOperand(0); // Operand * 1 = Operand
- } else if (MI->isArrayAllocation()) {
- // Multiply it by the array size if necessary...
- MallocArg = BinaryOperator::create(Instruction::Mul, MI->getOperand(0),
- MallocArg, "", I);
+ // malloc(type) becomes sbyte *malloc(size)
+ Value *MallocArg = getSizeof(AllocTy);
+ if (MI->isArrayAllocation()) {
+ if (isa<ConstantUInt>(MallocArg) &&
+ cast<ConstantUInt>(MallocArg)->getValue() == 1) {
+ MallocArg = MI->getOperand(0); // Operand * 1 = Operand
+ } else if (Constant *CO = dyn_cast<Constant>(MI->getOperand(0))) {
+ MallocArg = ConstantExpr::getMul(CO, cast<Constant>(MallocArg));
+ } else {
+ // Multiply it by the array size if necessary...
+ MallocArg = BinaryOperator::create(Instruction::Mul,
+ MI->getOperand(0),
+ MallocArg, "", I);
+ }
}
const FunctionType *MallocFTy = MallocFunc->getFunctionType();