Fix the: test/Regression/Other/2002-03-20-LevelRaiseCrash.ll bug. It turned

out that almost always the result of the add was the same as the GEP that
would be generated.  In this case, however, it wasn't, so bad things happened.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1929 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2002-03-21 06:22:23 +00:00
parent 35191769a5
commit 45ef5c26d4

View File

@ -62,6 +62,7 @@ static inline bool isReinterpretingCast(const CastInst *CI) {
static bool HandleCastToPointer(BasicBlock::iterator BI, static bool HandleCastToPointer(BasicBlock::iterator BI,
const PointerType *DestPTy) { const PointerType *DestPTy) {
CastInst *CI = cast<CastInst>(*BI); CastInst *CI = cast<CastInst>(*BI);
if (CI->use_empty()) return false;
// Scan all of the uses, looking for any uses that are not add // Scan all of the uses, looking for any uses that are not add
// instructions. If we have non-adds, do not make this transformation. // instructions. If we have non-adds, do not make this transformation.
@ -86,18 +87,36 @@ static bool HandleCastToPointer(BasicBlock::iterator BI,
// If we have a getelementptr capability... transform all of the // If we have a getelementptr capability... transform all of the
// add instruction uses into getelementptr's. // add instruction uses into getelementptr's.
while (!CI->use_empty()) { while (!CI->use_empty()) {
Instruction *I = cast<Instruction>(*CI->use_begin()); BinaryOperator *I = cast<BinaryOperator>(*CI->use_begin());
assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 && assert(I->getOpcode() == Instruction::Add && I->getNumOperands() == 2 &&
"Use is not a valid add instruction!"); "Use is not a valid add instruction!");
// Get the value added to the cast result pointer... // Get the value added to the cast result pointer...
Value *OtherPtr = I->getOperand((I->getOperand(0) == CI) ? 1 : 0); Value *OtherPtr = I->getOperand((I->getOperand(0) == CI) ? 1 : 0);
GetElementPtrInst *GEP = new GetElementPtrInst(OtherPtr, Indices); Instruction *GEP = new GetElementPtrInst(OtherPtr, Indices, I->getName());
PRINT_PEEPHOLE1("cast-add-to-gep:i", I); PRINT_PEEPHOLE1("cast-add-to-gep:i", I);
// Replace the old add instruction with the shiny new GEP inst if (GEP->getType() == I->getType()) {
ReplaceInstWithInst(I, GEP); // Replace the old add instruction with the shiny new GEP inst
ReplaceInstWithInst(I, GEP);
} else {
// If the type produced by the gep instruction differs from the original
// add instruction type, insert a cast now.
//
// Insert the GEP instruction before the old add instruction... and get an
// iterator to point at the add instruction...
BasicBlock::iterator GEPI = InsertInstBeforeInst(GEP, I)+1;
PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
CastInst *CI = new CastInst(GEP, I->getType());
GEP = CI;
// Replace the old add instruction with the shiny new GEP inst
ReplaceInstWithInst(I->getParent()->getInstList(), GEPI, GEP);
}
PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP); PRINT_PEEPHOLE1("cast-add-to-gep:o", GEP);
} }
return true; return true;