diff options
author | Chris Lattner <sabre@nondot.org> | 2005-10-27 06:12:00 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2005-10-27 06:12:00 +0000 |
commit | 8142b0a54bd443f8813d914ba8b953964ae1e914 (patch) | |
tree | 558895e6fb01045012bcafeba85c7379fb9f557a /lib/Transforms | |
parent | 0ddac2a1c349f88fb0b6d1373ce9e66c2ffe77c3 (diff) | |
download | external_llvm-8142b0a54bd443f8813d914ba8b953964ae1e914.zip external_llvm-8142b0a54bd443f8813d914ba8b953964ae1e914.tar.gz external_llvm-8142b0a54bd443f8813d914ba8b953964ae1e914.tar.bz2 |
Promote cases like cast (malloc sbyte, 100) to int* into
(malloc [25 x int]) directly without having to convert to
(malloc [100 x sbyte]) first.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24031 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r-- | lib/Transforms/Scalar/InstructionCombining.cpp | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index a5a2597..7ac1ecc 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -3826,7 +3826,28 @@ Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, Amt = InsertNewInstBefore(Tmp, AI); } } else { - return 0; + // See if we can satisfy the modulus by pulling a scale out of the array + // size argument. + unsigned ArraySizeScale = 1; + Value *NumElements = AI.getOperand(0); + + if (ConstantUInt *CI = dyn_cast<ConstantUInt>(NumElements)) { + ArraySizeScale = CI->getValue(); + NumElements = ConstantUInt::get(Type::UIntTy, 1); + } + + // If we can now satisfy the modulus, by using a non-1 scale, we really can + // do the xform. + if ((AllocElTySize*ArraySizeScale) % CastElTySize != 0) return 0; + + Amt = ConstantUInt::get(Type::UIntTy, + (AllocElTySize*ArraySizeScale)/CastElTySize); + if (ConstantUInt *CI = dyn_cast<ConstantUInt>(NumElements)) + Amt = ConstantExpr::getMul(CI, cast<ConstantUInt>(Amt)); + else { + Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); + Amt = InsertNewInstBefore(Tmp, AI); + } } std::string Name = AI.getName(); AI.setName(""); |