aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/TransformInternals.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2001-12-05 19:41:16 +0000
committerChris Lattner <sabre@nondot.org>2001-12-05 19:41:16 +0000
commit2dc48bd788baf3882c2e92407330f1b5d934ca8e (patch)
tree79b5d094594f1bf2eb9da4d6d4632a7d3fa180cf /lib/Transforms/TransformInternals.cpp
parent8da5d426d12bea312b24f54c08810835228b32b8 (diff)
Handle more complex array indexing expressions
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1424 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/TransformInternals.cpp')
-rw-r--r--lib/Transforms/TransformInternals.cpp61
1 files changed, 47 insertions, 14 deletions
diff --git a/lib/Transforms/TransformInternals.cpp b/lib/Transforms/TransformInternals.cpp
index 5fdffda908..c12fe6930f 100644
--- a/lib/Transforms/TransformInternals.cpp
+++ b/lib/Transforms/TransformInternals.cpp
@@ -180,26 +180,59 @@ const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
unsigned ElSize = TD.getTypeSize(ElTy);
// See if the user is indexing into a different cell of this array...
- if (Offset >= ElSize) {
- // Calculate the index that we are entering into the array cell with
- unsigned Index = Offset/ElSize;
- Indices.push_back(ConstantUInt::get(Type::UIntTy, Index));
- Offset -= Index*ElSize; // Consume part of the offset
+ if (Scale && Scale >= ElSize) {
+ // A scale n*ElSize might occur if we are not stepping through
+ // array by one. In this case, we will have to insert math to munge
+ // the index.
+ //
+ unsigned ScaleAmt = Scale/ElSize;
+ if (Scale-ScaleAmt*ElSize)
+ return 0; // Didn't scale by a multiple of element size, bail out
+ Scale = ElSize;
- } else if (Scale && Scale != 1) {
- // Must be indexing into this element with a variable...
- if (Scale != ElSize)
- return 0; // Type must not be finished yet...
+ unsigned Index = Offset/ElSize; // is zero unless Offset > ElSize
+ Offset -= Index*ElSize; // Consume part of the offset
- if (Expr.Var->getType() != Type::UIntTy && BI) {
+ if (BI) { // Generate code?
BasicBlock *BB = (**BI)->getParent();
- CastInst *IdxCast = new CastInst(Expr.Var, Type::UIntTy);
- *BI = BB->getInstList().insert(*BI, IdxCast)+1;
- Expr.Var = IdxCast;
- }
+ if (Expr.Var->getType() != Type::UIntTy) {
+ CastInst *IdxCast = new CastInst(Expr.Var, Type::UIntTy);
+ if (Expr.Var->hasName())
+ IdxCast->setName(Expr.Var->getName()+"-idxcast");
+ *BI = BB->getInstList().insert(*BI, IdxCast)+1;
+ Expr.Var = IdxCast;
+ }
+
+ if (Scale > ElSize) { // If we have to scale up our index, do so now
+ Value *ScaleAmtVal = ConstantUInt::get(Type::UIntTy, ScaleAmt);
+ Instruction *Scaler = BinaryOperator::create(Instruction::Mul,
+ Expr.Var,ScaleAmtVal);
+ if (Expr.Var->hasName())
+ Scaler->setName(Expr.Var->getName()+"-scale");
+
+ *BI = BB->getInstList().insert(*BI, Scaler)+1;
+ Expr.Var = Scaler;
+ }
+
+ if (Index) { // Add an offset to the index
+ Value *IndexAmt = ConstantUInt::get(Type::UIntTy, Index);
+ Instruction *Offseter = BinaryOperator::create(Instruction::Add,
+ Expr.Var, IndexAmt);
+ if (Expr.Var->hasName())
+ Offseter->setName(Expr.Var->getName()+"-offset");
+ *BI = BB->getInstList().insert(*BI, Offseter)+1;
+ Expr.Var = Offseter;
+ }
+ }
Indices.push_back(Expr.Var);
Scale = 0; // Consume scale factor!
+ } else if (Offset >= ElSize) {
+ // Calculate the index that we are entering into the array cell with
+ unsigned Index = Offset/ElSize;
+ Indices.push_back(ConstantUInt::get(Type::UIntTy, Index));
+ Offset -= Index*ElSize; // Consume part of the offset
+
} else {
// Must be indexing a small amount into the first cell of the array
// Just index into element zero of the array here.