From 8863f1814ba2c26910edb30857d10a8587f2f7de Mon Sep 17 00:00:00 2001 From: Reid Spencer Date: Sun, 18 Jul 2004 00:38:32 +0000 Subject: [PATCH] bug 122: - Replace ConstantPointerRef usage with GlobalValue usage - Minimize redundant isa usage - Correct isa for GlobalValue subclass git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@14950 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../SparcV9/InstrSelection/InstrForest.cpp | 10 ++++--- .../InstrSelection/InstrSelectionSupport.cpp | 19 ++++++------ lib/Target/SparcV9/SparcV9InstrInfo.cpp | 26 +++++++---------- lib/Target/SparcV9/SparcV9PreSelection.cpp | 2 -- lib/Target/X86/InstSelectSimple.cpp | 17 ++--------- lib/Target/X86/Printer.cpp | 4 +-- lib/Target/X86/X86AsmPrinter.cpp | 4 +-- lib/Target/X86/X86ISelSimple.cpp | 17 ++--------- lib/Target/X86/X86SimpInstrSelector.cpp | 19 +++++------- .../Scalar/InstructionCombining.cpp | 29 +++++++++---------- 10 files changed, 56 insertions(+), 91 deletions(-) diff --git a/lib/Target/SparcV9/InstrSelection/InstrForest.cpp b/lib/Target/SparcV9/InstrSelection/InstrForest.cpp index cc22b3774a1..271857b80ad 100644 --- a/lib/Target/SparcV9/InstrSelection/InstrForest.cpp +++ b/lib/Target/SparcV9/InstrSelection/InstrForest.cpp @@ -244,8 +244,7 @@ InstructionNode* InstrForest::buildTreeForInstruction(Instruction *instr) { && !instr->isTerminator(); if (includeAddressOperand || isa(operand) || - isa(operand) || isa(operand) || - isa(operand)) + isa(operand) || isa(operand)) { // This operand is a data value @@ -273,8 +272,11 @@ InstructionNode* InstrForest::buildTreeForInstruction(Instruction *instr) { // Recursively create a treeNode for it. opTreeNode = buildTreeForInstruction((Instruction*)operand); } else if (Constant *CPV = dyn_cast(operand)) { - // Create a leaf node for a constant - opTreeNode = new ConstantNode(CPV); + if (isa(CPV)) + opTreeNode = new VRegNode(operand); + else + // Create a leaf node for a constant + opTreeNode = new ConstantNode(CPV); } else { // Create a leaf node for the virtual register opTreeNode = new VRegNode(operand); diff --git a/lib/Target/SparcV9/InstrSelection/InstrSelectionSupport.cpp b/lib/Target/SparcV9/InstrSelection/InstrSelectionSupport.cpp index e0f43b529bf..7e0f6c48c6d 100644 --- a/lib/Target/SparcV9/InstrSelection/InstrSelectionSupport.cpp +++ b/lib/Target/SparcV9/InstrSelection/InstrSelectionSupport.cpp @@ -22,6 +22,7 @@ #include "llvm/Constants.h" #include "llvm/BasicBlock.h" #include "llvm/DerivedTypes.h" +#include "llvm/GlobalValue.h" #include "../SparcV9InstrSelectionSupport.h" namespace llvm { @@ -159,13 +160,14 @@ FixConstantOperandsForInstr(Instruction* vmInstr, if (mop.getType() == MachineOperand::MO_VirtualRegister) { assert(mop.getVRegValue() != NULL); opValue = mop.getVRegValue(); - if (Constant *opConst = dyn_cast(opValue)) { - opType = ChooseRegOrImmed(opConst, opCode, target, - (immedPos == (int)op), machineRegNum, - immedValue); - if (opType == MachineOperand::MO_VirtualRegister) - constantThatMustBeLoaded = true; - } + if (Constant *opConst = dyn_cast(opValue)) + if (!isa(opConst)) { + opType = ChooseRegOrImmed(opConst, opCode, target, + (immedPos == (int)op), machineRegNum, + immedValue); + if (opType == MachineOperand::MO_VirtualRegister) + constantThatMustBeLoaded = true; + } } else { // // If the operand is from the constant pool, don't try to change it. @@ -242,8 +244,7 @@ FixConstantOperandsForInstr(Instruction* vmInstr, argDesc = CallArgsDescriptor::get(minstr); for (unsigned i=0, N=minstr->getNumImplicitRefs(); i < N; ++i) - if (isa(minstr->getImplicitRef(i)) || - isa(minstr->getImplicitRef(i))) + if (isa(minstr->getImplicitRef(i))) { Value* oldVal = minstr->getImplicitRef(i); TmpInstruction* tmpReg = diff --git a/lib/Target/SparcV9/SparcV9InstrInfo.cpp b/lib/Target/SparcV9/SparcV9InstrInfo.cpp index 986d4a88b3e..30d35546e6c 100644 --- a/lib/Target/SparcV9/SparcV9InstrInfo.cpp +++ b/lib/Target/SparcV9/SparcV9InstrInfo.cpp @@ -56,16 +56,14 @@ SparcV9InstrInfo::ConvertConstantToIntType(const TargetMachine &target, if (! destType->isIntegral() && ! isa(destType)) return C; - if (! isa(V)) + if (! isa(V) || isa(V)) return C; - // ConstantPointerRef: no conversions needed: get value and return it - if (const ConstantPointerRef* CPR = dyn_cast(V)) { - // A ConstantPointerRef is just a reference to GlobalValue. + // GlobalValue: no conversions needed: get value and return it + if (const GlobalValue* GV = dyn_cast(V)) { isValidConstant = true; // may be overwritten by recursive call - return (CPR->isNullValue()? 0 - : ConvertConstantToIntType(target, CPR->getValue(), destType, - isValidConstant)); + return GV->isNullValue() ? 0 : + ConvertConstantToIntType(target, GV, destType, isValidConstant); } // ConstantBool: no conversions needed: get value and return it @@ -284,7 +282,7 @@ CreateSETXLabel(const TargetMachine& target, Value* val, Instruction* tmpReg, Instruction* dest, std::vector& mvec) { - assert(isa(val) || isa(val) && + assert(isa(val) && "I only know about constant values and global addresses"); MachineInstr* MI; @@ -467,7 +465,7 @@ SparcV9InstrInfo::CreateCodeToLoadConst(const TargetMachine& target, std::vector& mvec, MachineCodeForInstruction& mcfi) const { - assert(isa(val) || isa(val) && + assert(isa(val) && "I only know about constant values and global addresses"); // Use a "set" instruction for known constants or symbolic constants (labels) @@ -477,10 +475,6 @@ SparcV9InstrInfo::CreateCodeToLoadConst(const TargetMachine& target, // const Type* valType = val->getType(); - // A ConstantPointerRef is just a reference to GlobalValue. - while (isa(val)) - val = cast(val)->getValue(); - if (isa(val)) { TmpInstruction* tmpReg = new TmpInstruction(mcfi, PointerType::get(val->getType()), val); @@ -688,7 +682,9 @@ SparcV9InstrInfo::CreateCopyInstructionsByType(const TargetMachine& target, // a global variable (i.e., a constant address), generate a load // instruction instead of an add // - if (isa(src)) { + if (isa(src)) + loadConstantToReg = true; + else if (isa(src)) { unsigned int machineRegNum; int64_t immedValue; MachineOperand::MachineOperandType opType = @@ -698,8 +694,6 @@ SparcV9InstrInfo::CreateCopyInstructionsByType(const TargetMachine& target, if (opType == MachineOperand::MO_VirtualRegister) loadConstantToReg = true; } - else if (isa(src)) - loadConstantToReg = true; if (loadConstantToReg) { // `src' is constant and cannot fit in immed field for the ADD diff --git a/lib/Target/SparcV9/SparcV9PreSelection.cpp b/lib/Target/SparcV9/SparcV9PreSelection.cpp index 00cfd1295e6..fc629a6a3fd 100644 --- a/lib/Target/SparcV9/SparcV9PreSelection.cpp +++ b/lib/Target/SparcV9/SparcV9PreSelection.cpp @@ -85,8 +85,6 @@ namespace { // getGlobalAddr(): Put address of a global into a v. register. static GetElementPtrInst* getGlobalAddr(Value* ptr, Instruction& insertBefore) { - if (isa(ptr)) - ptr = cast(ptr)->getValue(); return (isa(ptr)) ? new GetElementPtrInst(ptr, diff --git a/lib/Target/X86/InstSelectSimple.cpp b/lib/Target/X86/InstSelectSimple.cpp index 06165e9f6e1..3dced748bc1 100644 --- a/lib/Target/X86/InstSelectSimple.cpp +++ b/lib/Target/X86/InstSelectSimple.cpp @@ -394,16 +394,10 @@ unsigned ISel::getReg(Value *V, MachineBasicBlock *MBB, MachineBasicBlock::iterator IPt) { // If this operand is a constant, emit the code to copy the constant into // the register here... - // if (Constant *C = dyn_cast(V)) { unsigned Reg = makeAnotherReg(V->getType()); copyConstantToRegister(MBB, IPt, C, Reg); return Reg; - } else if (GlobalValue *GV = dyn_cast(V)) { - unsigned Reg = makeAnotherReg(V->getType()); - // Move the address of the global into the register - BuildMI(*MBB, IPt, X86::MOV32ri, 1, Reg).addGlobalAddress(GV); - return Reg; } else if (CastInst *CI = dyn_cast(V)) { // Do not emit noop casts at all, unless it's a double -> float cast. if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()) && @@ -554,8 +548,8 @@ void ISel::copyConstantToRegister(MachineBasicBlock *MBB, } else if (isa(C)) { // Copy zero (null pointer) to the register. BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0); - } else if (ConstantPointerRef *CPR = dyn_cast(C)) { - BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(CPR->getValue()); + } else if (GlobalValue *GV = dyn_cast(C)) { + BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(GV); } else { std::cerr << "Offending constant: " << *C << "\n"; assert(0 && "Type not handled yet!"); @@ -688,8 +682,7 @@ void ISel::SelectPHINodes() { // If this is a constant or GlobalValue, we may have to insert code // into the basic block to compute it into a virtual register. - if ((isa(Val) && !isa(Val)) || - isa(Val)) { + if ((isa(Val) && !isa(Val))) { // Simple constants get emitted at the end of the basic block, // before any terminator instructions. We "know" that the code to // move a constant into a register will never clobber any flags. @@ -3635,8 +3628,6 @@ bool ISel::isGEPFoldable(MachineBasicBlock *MBB, Value *Src, User::op_iterator IdxBegin, User::op_iterator IdxEnd, unsigned &BaseReg, unsigned &Scale, unsigned &IndexReg, unsigned &Disp) { - if (ConstantPointerRef *CPR = dyn_cast(Src)) - Src = CPR->getValue(); std::vector GEPOps; GEPOps.resize(IdxEnd-IdxBegin+1); @@ -3660,8 +3651,6 @@ void ISel::emitGEPOperation(MachineBasicBlock *MBB, Value *Src, User::op_iterator IdxBegin, User::op_iterator IdxEnd, unsigned TargetReg) { const TargetData &TD = TM.getTargetData(); - if (ConstantPointerRef *CPR = dyn_cast(Src)) - Src = CPR->getValue(); // If this is a getelementptr null, with all constant integer indices, just // replace it with TargetReg = 42. diff --git a/lib/Target/X86/Printer.cpp b/lib/Target/X86/Printer.cpp index 1181e0506be..9ac981f2b99 100644 --- a/lib/Target/X86/Printer.cpp +++ b/lib/Target/X86/Printer.cpp @@ -178,10 +178,10 @@ void Printer::emitConstantValueOnly(const Constant *CV) { O << (unsigned long long)CI->getValue(); else if (const ConstantUInt *CI = dyn_cast(CV)) O << CI->getValue(); - else if (const ConstantPointerRef *CPR = dyn_cast(CV)) + else if (const GlobalValue *GV = dyn_cast(CV)) // This is a constant address for a global variable or function. Use the // name of the variable or function as the address value. - O << Mang->getValueName(CPR->getValue()); + O << Mang->getValueName(GV); else if (const ConstantExpr *CE = dyn_cast(CV)) { const TargetData &TD = TM.getTargetData(); switch(CE->getOpcode()) { diff --git a/lib/Target/X86/X86AsmPrinter.cpp b/lib/Target/X86/X86AsmPrinter.cpp index 1181e0506be..9ac981f2b99 100644 --- a/lib/Target/X86/X86AsmPrinter.cpp +++ b/lib/Target/X86/X86AsmPrinter.cpp @@ -178,10 +178,10 @@ void Printer::emitConstantValueOnly(const Constant *CV) { O << (unsigned long long)CI->getValue(); else if (const ConstantUInt *CI = dyn_cast(CV)) O << CI->getValue(); - else if (const ConstantPointerRef *CPR = dyn_cast(CV)) + else if (const GlobalValue *GV = dyn_cast(CV)) // This is a constant address for a global variable or function. Use the // name of the variable or function as the address value. - O << Mang->getValueName(CPR->getValue()); + O << Mang->getValueName(GV); else if (const ConstantExpr *CE = dyn_cast(CV)) { const TargetData &TD = TM.getTargetData(); switch(CE->getOpcode()) { diff --git a/lib/Target/X86/X86ISelSimple.cpp b/lib/Target/X86/X86ISelSimple.cpp index 06165e9f6e1..3dced748bc1 100644 --- a/lib/Target/X86/X86ISelSimple.cpp +++ b/lib/Target/X86/X86ISelSimple.cpp @@ -394,16 +394,10 @@ unsigned ISel::getReg(Value *V, MachineBasicBlock *MBB, MachineBasicBlock::iterator IPt) { // If this operand is a constant, emit the code to copy the constant into // the register here... - // if (Constant *C = dyn_cast(V)) { unsigned Reg = makeAnotherReg(V->getType()); copyConstantToRegister(MBB, IPt, C, Reg); return Reg; - } else if (GlobalValue *GV = dyn_cast(V)) { - unsigned Reg = makeAnotherReg(V->getType()); - // Move the address of the global into the register - BuildMI(*MBB, IPt, X86::MOV32ri, 1, Reg).addGlobalAddress(GV); - return Reg; } else if (CastInst *CI = dyn_cast(V)) { // Do not emit noop casts at all, unless it's a double -> float cast. if (getClassB(CI->getType()) == getClassB(CI->getOperand(0)->getType()) && @@ -554,8 +548,8 @@ void ISel::copyConstantToRegister(MachineBasicBlock *MBB, } else if (isa(C)) { // Copy zero (null pointer) to the register. BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0); - } else if (ConstantPointerRef *CPR = dyn_cast(C)) { - BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(CPR->getValue()); + } else if (GlobalValue *GV = dyn_cast(C)) { + BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(GV); } else { std::cerr << "Offending constant: " << *C << "\n"; assert(0 && "Type not handled yet!"); @@ -688,8 +682,7 @@ void ISel::SelectPHINodes() { // If this is a constant or GlobalValue, we may have to insert code // into the basic block to compute it into a virtual register. - if ((isa(Val) && !isa(Val)) || - isa(Val)) { + if ((isa(Val) && !isa(Val))) { // Simple constants get emitted at the end of the basic block, // before any terminator instructions. We "know" that the code to // move a constant into a register will never clobber any flags. @@ -3635,8 +3628,6 @@ bool ISel::isGEPFoldable(MachineBasicBlock *MBB, Value *Src, User::op_iterator IdxBegin, User::op_iterator IdxEnd, unsigned &BaseReg, unsigned &Scale, unsigned &IndexReg, unsigned &Disp) { - if (ConstantPointerRef *CPR = dyn_cast(Src)) - Src = CPR->getValue(); std::vector GEPOps; GEPOps.resize(IdxEnd-IdxBegin+1); @@ -3660,8 +3651,6 @@ void ISel::emitGEPOperation(MachineBasicBlock *MBB, Value *Src, User::op_iterator IdxBegin, User::op_iterator IdxEnd, unsigned TargetReg) { const TargetData &TD = TM.getTargetData(); - if (ConstantPointerRef *CPR = dyn_cast(Src)) - Src = CPR->getValue(); // If this is a getelementptr null, with all constant integer indices, just // replace it with TargetReg = 42. diff --git a/lib/Target/X86/X86SimpInstrSelector.cpp b/lib/Target/X86/X86SimpInstrSelector.cpp index 9fd487374d8..2ada795718b 100644 --- a/lib/Target/X86/X86SimpInstrSelector.cpp +++ b/lib/Target/X86/X86SimpInstrSelector.cpp @@ -316,13 +316,13 @@ namespace { // If this operand is a constant, emit the code to copy the constant into // the register here... // - if (Constant *C = dyn_cast(V)) { - copyConstantToRegister(MBB, IPt, C, Reg); - RegMap.erase(V); // Assign a new name to this constant if ref'd again - } else if (GlobalValue *GV = dyn_cast(V)) { + if (GlobalValue *GV = dyn_cast(V)) { // Move the address of the global into the register BuildMI(*MBB, IPt, X86::MOV32ri, 1, Reg).addGlobalAddress(GV); RegMap.erase(V); // Assign a new name to this address if ref'd again + } else if (Constant *C = dyn_cast(V)) { + copyConstantToRegister(MBB, IPt, C, Reg); + RegMap.erase(V); // Assign a new name to this constant if ref'd again } return Reg; @@ -480,8 +480,8 @@ void ISel::copyConstantToRegister(MachineBasicBlock *MBB, } else if (isa(C)) { // Copy zero (null pointer) to the register. BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addImm(0); - } else if (ConstantPointerRef *CPR = dyn_cast(C)) { - BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(CPR->getValue()); + } else if (GlobalValue *GV = dyn_cast(C)) { + BuildMI(*MBB, IP, X86::MOV32ri, 1, R).addGlobalAddress(GV); } else { std::cerr << "Offending constant: " << *C << "\n"; assert(0 && "Type not handled yet!"); @@ -602,7 +602,7 @@ void ISel::SelectPHINodes() { // If this is a constant or GlobalValue, we may have to insert code // into the basic block to compute it into a virtual register. - if (isa(Val) || isa(Val)) { + if (isa(Val)) { // Because we don't want to clobber any values which might be in // physical registers with the computation of this constant (which // might be arbitrarily complex if it is a constant expression), @@ -2589,9 +2589,6 @@ bool ISel::isGEPFoldable(MachineBasicBlock *MBB, Value *Src, User::op_iterator IdxBegin, User::op_iterator IdxEnd, unsigned &BaseReg, unsigned &Scale, unsigned &IndexReg, unsigned &Disp) { - if (ConstantPointerRef *CPR = dyn_cast(Src)) - Src = CPR->getValue(); - std::vector GEPOps; GEPOps.resize(IdxEnd-IdxBegin+1); GEPOps[0] = Src; @@ -2614,8 +2611,6 @@ void ISel::emitGEPOperation(MachineBasicBlock *MBB, Value *Src, User::op_iterator IdxBegin, User::op_iterator IdxEnd, unsigned TargetReg) { const TargetData &TD = TM.getTargetData(); - if (ConstantPointerRef *CPR = dyn_cast(Src)) - Src = CPR->getValue(); std::vector GEPOps; GEPOps.resize(IdxEnd-IdxBegin+1); diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 8d353c8063a..33370c33977 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -2457,11 +2457,10 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { if (!isa(CS.getCalledValue())) return false; ConstantExpr *CE = cast(CS.getCalledValue()); if (CE->getOpcode() != Instruction::Cast || - !isa(CE->getOperand(0))) + !isa(CE->getOperand(0))) return false; - ConstantPointerRef *CPR = cast(CE->getOperand(0)); - if (!isa(CPR->getValue())) return false; - Function *Callee = cast(CPR->getValue()); + if (!isa(CE->getOperand(0))) return false; + Function *Callee = cast(CE->getOperand(0)); Instruction *Caller = CS.getInstruction(); // Okay, this is a cast from a function to a different type. Unless doing so @@ -2811,7 +2810,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { if (I == E) { // If they are all constants... Constant *CE = - ConstantExpr::getGetElementPtr(ConstantPointerRef::get(GV), Indices); + ConstantExpr::getGetElementPtr(GV, Indices); // Replace all uses of the GEP with the new constexpr... return ReplaceInstUsesWith(GEP, CE); @@ -2978,8 +2977,8 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { if (Constant *C = dyn_cast(Op)) if (C->isNullValue()) // load null -> 0 return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType())); - else if (ConstantPointerRef *CPR = dyn_cast(C)) - Op = CPR->getValue(); + else if (isa(C)) + Op = C; // Instcombine load (constant global) into the value loaded... if (GlobalVariable *GV = dyn_cast(Op)) @@ -2989,11 +2988,10 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded... if (ConstantExpr *CE = dyn_cast(Op)) if (CE->getOpcode() == Instruction::GetElementPtr) { - if (ConstantPointerRef *G=dyn_cast(CE->getOperand(0))) - if (GlobalVariable *GV = dyn_cast(G->getValue())) - if (GV->isConstant() && !GV->isExternal()) - if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE)) - return ReplaceInstUsesWith(LI, V); + if (GlobalVariable *GV = dyn_cast(CE->getOperand(0))) + if (GV->isConstant() && !GV->isExternal()) + if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE)) + return ReplaceInstUsesWith(LI, V); } else if (CE->getOpcode() == Instruction::Cast) { if (Instruction *Res = InstCombineLoadCast(*this, LI)) return Res; @@ -3106,12 +3104,11 @@ bool InstCombiner::runOnFunction(Function &F) { } // Check to see if any of the operands of this instruction are a - // ConstantPointerRef. Since they sneak in all over the place and inhibit + // GlobalValue. Since they sneak in all over the place and inhibit // optimization, we want to strip them out unconditionally! for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) - if (ConstantPointerRef *CPR = - dyn_cast(I->getOperand(i))) { - I->setOperand(i, CPR->getValue()); + if (isa(I->getOperand(i))) { + I->setOperand(i, I->getOperand(i)); Changed = true; }