Fix instcombine's handling of alloca to accept non-i32 types.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@104935 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Dan Gohman
2010-05-28 04:33:04 +00:00
parent e8f0349439
commit 28d2e0a39f

View File

@@ -22,19 +22,18 @@ using namespace PatternMatch;
/// X*Scale+Offset. /// X*Scale+Offset.
/// ///
static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale, static Value *DecomposeSimpleLinearExpr(Value *Val, unsigned &Scale,
int &Offset) { uint64_t &Offset) {
assert(Val->getType()->isIntegerTy(32) && "Unexpected allocation size type!");
if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) { if (ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Offset = CI->getZExtValue(); Offset = CI->getZExtValue();
Scale = 0; Scale = 0;
return ConstantInt::get(Type::getInt32Ty(Val->getContext()), 0); return ConstantInt::get(Val->getType(), 0);
} }
if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) { if (BinaryOperator *I = dyn_cast<BinaryOperator>(Val)) {
if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) { if (ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1))) {
if (I->getOpcode() == Instruction::Shl) { if (I->getOpcode() == Instruction::Shl) {
// This is a value scaled by '1 << the shift amt'. // This is a value scaled by '1 << the shift amt'.
Scale = 1U << RHS->getZExtValue(); Scale = UINT64_C(1) << RHS->getZExtValue();
Offset = 0; Offset = 0;
return I->getOperand(0); return I->getOperand(0);
} }
@@ -100,7 +99,7 @@ Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
// See if we can satisfy the modulus by pulling a scale out of the array // See if we can satisfy the modulus by pulling a scale out of the array
// size argument. // size argument.
unsigned ArraySizeScale; unsigned ArraySizeScale;
int ArrayOffset; uint64_t ArrayOffset;
Value *NumElements = // See if the array size is a decomposable linear expr. Value *NumElements = // See if the array size is a decomposable linear expr.
DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset); DecomposeSimpleLinearExpr(AI.getOperand(0), ArraySizeScale, ArrayOffset);
@@ -114,13 +113,13 @@ Instruction *InstCombiner::PromoteCastOfAllocation(BitCastInst &CI,
if (Scale == 1) { if (Scale == 1) {
Amt = NumElements; Amt = NumElements;
} else { } else {
Amt = ConstantInt::get(Type::getInt32Ty(CI.getContext()), Scale); Amt = ConstantInt::get(AI.getArraySize()->getType(), Scale);
// Insert before the alloca, not before the cast. // Insert before the alloca, not before the cast.
Amt = AllocaBuilder.CreateMul(Amt, NumElements, "tmp"); Amt = AllocaBuilder.CreateMul(Amt, NumElements, "tmp");
} }
if (int Offset = (AllocElTySize*ArrayOffset)/CastElTySize) { if (uint64_t Offset = (AllocElTySize*ArrayOffset)/CastElTySize) {
Value *Off = ConstantInt::get(Type::getInt32Ty(CI.getContext()), Value *Off = ConstantInt::get(AI.getArraySize()->getType(),
Offset, true); Offset, true);
Amt = AllocaBuilder.CreateAdd(Amt, Off, "tmp"); Amt = AllocaBuilder.CreateAdd(Amt, Off, "tmp");
} }