mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-21 03:32:21 +00:00
* add ReplaceInstWithInst
* Add support for pointer indexing * Fix minor bugs that are majorly annoying git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1467 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
38ac81cbbc
commit
7991c281c5
@ -11,6 +11,7 @@
|
|||||||
#include "llvm/ConstantVals.h"
|
#include "llvm/ConstantVals.h"
|
||||||
#include "llvm/Analysis/Expressions.h"
|
#include "llvm/Analysis/Expressions.h"
|
||||||
#include "llvm/iOther.h"
|
#include "llvm/iOther.h"
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
// TargetData Hack: Eventually we will have annotations given to us by the
|
// TargetData Hack: Eventually we will have annotations given to us by the
|
||||||
// backend so that we know stuff about type size and alignments. For now
|
// backend so that we know stuff about type size and alignments. For now
|
||||||
@ -59,6 +60,15 @@ void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
|
|||||||
--BI;
|
--BI;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ReplaceInstWithInst(Instruction *From, Instruction *To) {
|
||||||
|
BasicBlock *BB = From->getParent();
|
||||||
|
BasicBlock::InstListType &BIL = BB->getInstList();
|
||||||
|
BasicBlock::iterator BI = find(BIL.begin(), BIL.end(), From);
|
||||||
|
assert(BI != BIL.end() && "Inst not in it's parents BB!");
|
||||||
|
ReplaceInstWithInst(BIL, BI, To);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// getStructOffsetType - Return a vector of offsets that are to be used to index
|
// getStructOffsetType - Return a vector of offsets that are to be used to index
|
||||||
// into the specified struct type to get as close as possible to index as we
|
// into the specified struct type to get as close as possible to index as we
|
||||||
@ -74,16 +84,13 @@ void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
|
|||||||
const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
|
const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
|
||||||
vector<Value*> &Offsets,
|
vector<Value*> &Offsets,
|
||||||
bool StopEarly = true) {
|
bool StopEarly = true) {
|
||||||
if (!isa<CompositeType>(Ty) ||
|
if (Offset == 0 && StopEarly && !Offsets.empty())
|
||||||
(Offset == 0 && StopEarly && !Offsets.empty())) {
|
|
||||||
Offset = 0; // Return the offset that we were able to acheive
|
|
||||||
return Ty; // Return the leaf type
|
return Ty; // Return the leaf type
|
||||||
}
|
|
||||||
|
|
||||||
unsigned ThisOffset;
|
unsigned ThisOffset;
|
||||||
const Type *NextType;
|
const Type *NextType;
|
||||||
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
|
if (const StructType *STy = dyn_cast<StructType>(Ty)) {
|
||||||
assert(Offset < TD.getTypeSize(Ty) && "Offset not in composite!");
|
assert(Offset < TD.getTypeSize(STy) && "Offset not in composite!");
|
||||||
const StructLayout *SL = TD.getStructLayout(STy);
|
const StructLayout *SL = TD.getStructLayout(STy);
|
||||||
|
|
||||||
// This loop terminates always on a 0 <= i < MemberOffsets.size()
|
// This loop terminates always on a 0 <= i < MemberOffsets.size()
|
||||||
@ -99,15 +106,16 @@ const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
|
|||||||
Offsets.push_back(ConstantUInt::get(Type::UByteTy, i));
|
Offsets.push_back(ConstantUInt::get(Type::UByteTy, i));
|
||||||
ThisOffset = SL->MemberOffsets[i];
|
ThisOffset = SL->MemberOffsets[i];
|
||||||
NextType = STy->getElementTypes()[i];
|
NextType = STy->getElementTypes()[i];
|
||||||
} else {
|
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
||||||
const ArrayType *ATy = cast<ArrayType>(Ty);
|
assert(Offset < TD.getTypeSize(ATy) && "Offset not in composite!");
|
||||||
assert(ATy->isUnsized() || Offset < TD.getTypeSize(Ty) &&
|
|
||||||
"Offset not in composite!");
|
|
||||||
|
|
||||||
NextType = ATy->getElementType();
|
NextType = ATy->getElementType();
|
||||||
unsigned ChildSize = TD.getTypeSize(NextType);
|
unsigned ChildSize = TD.getTypeSize(NextType);
|
||||||
Offsets.push_back(ConstantUInt::get(Type::UIntTy, Offset/ChildSize));
|
Offsets.push_back(ConstantUInt::get(Type::UIntTy, Offset/ChildSize));
|
||||||
ThisOffset = (Offset/ChildSize)*ChildSize;
|
ThisOffset = (Offset/ChildSize)*ChildSize;
|
||||||
|
} else {
|
||||||
|
Offset = 0; // Return the offset that we were able to acheive
|
||||||
|
return Ty; // Return the leaf type
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned SubOffs = Offset - ThisOffset;
|
unsigned SubOffs = Offset - ThisOffset;
|
||||||
@ -124,7 +132,7 @@ const Type *getStructOffsetType(const Type *Ty, unsigned &Offset,
|
|||||||
const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
|
const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
|
||||||
vector<Value*> &Indices,
|
vector<Value*> &Indices,
|
||||||
BasicBlock::iterator *BI = 0) {
|
BasicBlock::iterator *BI = 0) {
|
||||||
const CompositeType *CompTy = getPointedToComposite(Ty);
|
const CompositeType *CompTy = dyn_cast<CompositeType>(Ty);
|
||||||
if (CompTy == 0) return 0;
|
if (CompTy == 0) return 0;
|
||||||
|
|
||||||
// See if the cast is of an integer expression that is either a constant,
|
// See if the cast is of an integer expression that is either a constant,
|
||||||
@ -132,10 +140,6 @@ const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
|
|||||||
//
|
//
|
||||||
analysis::ExprType Expr = analysis::ClassifyExpression(OffsetVal);
|
analysis::ExprType Expr = analysis::ClassifyExpression(OffsetVal);
|
||||||
|
|
||||||
// The expression must either be a constant, or a scaled index to be useful
|
|
||||||
if (!Expr.Offset && !Expr.Scale)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// Get the offset and scale now...
|
// Get the offset and scale now...
|
||||||
unsigned Offset = 0, Scale = Expr.Var != 0;
|
unsigned Offset = 0, Scale = Expr.Var != 0;
|
||||||
|
|
||||||
@ -153,13 +157,6 @@ const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
|
|||||||
Scale = (unsigned)Val;
|
Scale = (unsigned)Val;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check to make sure the offset is not negative or really large, outside the
|
|
||||||
// scope of this structure...
|
|
||||||
//
|
|
||||||
if (!isa<ArrayType>(CompTy) || cast<ArrayType>(CompTy)->isSized())
|
|
||||||
if (Offset >= TD.getTypeSize(CompTy))
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// Loop over the Scale and Offset values, filling in the Indices vector for
|
// Loop over the Scale and Offset values, filling in the Indices vector for
|
||||||
// our final getelementptr instruction.
|
// our final getelementptr instruction.
|
||||||
//
|
//
|
||||||
@ -173,10 +170,11 @@ const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
|
|||||||
const StructLayout *SL = TD.getStructLayout(StructTy);
|
const StructLayout *SL = TD.getStructLayout(StructTy);
|
||||||
unsigned ActualOffset = Offset;
|
unsigned ActualOffset = Offset;
|
||||||
NextTy = getStructOffsetType(StructTy, ActualOffset, Indices);
|
NextTy = getStructOffsetType(StructTy, ActualOffset, Indices);
|
||||||
|
if (StructTy == NextTy && ActualOffset == 0) return 0; // No progress. :(
|
||||||
Offset -= ActualOffset;
|
Offset -= ActualOffset;
|
||||||
} else {
|
} else {
|
||||||
const ArrayType *AT = cast<ArrayType>(CompTy);
|
const Type *ElTy = cast<SequentialType>(CompTy)->getElementType();
|
||||||
const Type *ElTy = AT->getElementType();
|
if (!ElTy->isSized()) return 0; // Type is unreasonable... escape!
|
||||||
unsigned ElSize = TD.getTypeSize(ElTy);
|
unsigned ElSize = TD.getTypeSize(ElTy);
|
||||||
|
|
||||||
// See if the user is indexing into a different cell of this array...
|
// See if the user is indexing into a different cell of this array...
|
||||||
@ -188,7 +186,7 @@ const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
|
|||||||
unsigned ScaleAmt = Scale/ElSize;
|
unsigned ScaleAmt = Scale/ElSize;
|
||||||
if (Scale-ScaleAmt*ElSize)
|
if (Scale-ScaleAmt*ElSize)
|
||||||
return 0; // Didn't scale by a multiple of element size, bail out
|
return 0; // Didn't scale by a multiple of element size, bail out
|
||||||
Scale = ElSize;
|
Scale = 0; // Scale is consumed
|
||||||
|
|
||||||
unsigned Index = Offset/ElSize; // is zero unless Offset > ElSize
|
unsigned Index = Offset/ElSize; // is zero unless Offset > ElSize
|
||||||
Offset -= Index*ElSize; // Consume part of the offset
|
Offset -= Index*ElSize; // Consume part of the offset
|
||||||
@ -227,18 +225,19 @@ const Type *ConvertableToGEP(const Type *Ty, Value *OffsetVal,
|
|||||||
}
|
}
|
||||||
|
|
||||||
Indices.push_back(Expr.Var);
|
Indices.push_back(Expr.Var);
|
||||||
Scale = 0; // Consume scale factor!
|
|
||||||
} else if (Offset >= ElSize) {
|
} else if (Offset >= ElSize) {
|
||||||
// Calculate the index that we are entering into the array cell with
|
// Calculate the index that we are entering into the array cell with
|
||||||
unsigned Index = Offset/ElSize;
|
unsigned Index = Offset/ElSize;
|
||||||
Indices.push_back(ConstantUInt::get(Type::UIntTy, Index));
|
Indices.push_back(ConstantUInt::get(Type::UIntTy, Index));
|
||||||
Offset -= Index*ElSize; // Consume part of the offset
|
Offset -= Index*ElSize; // Consume part of the offset
|
||||||
|
|
||||||
} else {
|
} else if (!isa<PointerType>(CompTy) || CompTy == Ty) {
|
||||||
// Must be indexing a small amount into the first cell of the array
|
// Must be indexing a small amount into the first cell of the array
|
||||||
// Just index into element zero of the array here.
|
// Just index into element zero of the array here.
|
||||||
//
|
//
|
||||||
Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
|
Indices.push_back(ConstantUInt::get(Type::UIntTy, 0));
|
||||||
|
} else {
|
||||||
|
return 0; // Hrm. wierd, can't handle this case. Bail
|
||||||
}
|
}
|
||||||
NextTy = ElTy;
|
NextTy = ElTy;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user