diff options
| author | Chris Lattner <sabre@nondot.org> | 2005-10-27 06:24:46 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2005-10-27 06:24:46 +0000 |
| commit | 0fe7551bc0d54032cb145818ac396bb8ba2dc7c0 (patch) | |
| tree | ec2d6bd7963484ba5461e76b0f1d20b351f0b33f /llvm/lib/Transforms/Scalar/InstructionCombining.cpp | |
| parent | b3ecf9690089ec6f98d939cbec60e4f4f76a4093 (diff) | |
| download | bcm5719-llvm-0fe7551bc0d54032cb145818ac396bb8ba2dc7c0.tar.gz bcm5719-llvm-0fe7551bc0d54032cb145818ac396bb8ba2dc7c0.zip | |
Teach instcombine to promote stuff like (cast (malloc sbyte, 8*X) to int*)
into: malloc int, (2*X)
llvm-svn: 24032
Diffstat (limited to 'llvm/lib/Transforms/Scalar/InstructionCombining.cpp')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/InstructionCombining.cpp | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp index 7ac1eccdf22..1a9b9d7424a 100644 --- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp @@ -3834,19 +3834,38 @@ Instruction *InstCombiner::PromoteCastOfAllocation(CastInst &CI, if (ConstantUInt *CI = dyn_cast<ConstantUInt>(NumElements)) { ArraySizeScale = CI->getValue(); NumElements = ConstantUInt::get(Type::UIntTy, 1); + } else if (ShiftInst *SI = dyn_cast<ShiftInst>(NumElements)) { + if (SI->getOpcode() == Instruction::Shl) + if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(SI->getOperand(1))) { + // This is a value scaled by '1 << the shift amt'. + NumElements = SI->getOperand(0); + ArraySizeScale = 1U << CUI->getValue(); + } + } else if (isa<Instruction>(NumElements) && + cast<Instruction>(NumElements)->getOpcode() == Instruction::Mul){ + BinaryOperator *BO = cast<BinaryOperator>(NumElements); + if (ConstantUInt *Scale = cast<ConstantUInt>(BO->getOperand(1))) { + // This value is scaled by 'Scale'. + NumElements = BO->getOperand(0); + ArraySizeScale = Scale->getValue(); + } } // 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); + unsigned Scale = (AllocElTySize*ArraySizeScale)/CastElTySize; + if (Scale == 1) { + Amt = NumElements; + } else { + Amt = ConstantUInt::get(Type::UIntTy, Scale); + if (ConstantUInt *CI = dyn_cast<ConstantUInt>(NumElements)) + Amt = ConstantExpr::getMul(CI, cast<ConstantUInt>(Amt)); + else if (cast<ConstantUInt>(Amt)->getValue() == 1) { + Instruction *Tmp = BinaryOperator::createMul(Amt, NumElements, "tmp"); + Amt = InsertNewInstBefore(Tmp, AI); + } } } |

