llvm.org GIT mirror llvm / 6d69ba8
Enable rematerialization of constants using AliasAnalysis::pointsToConstantMemory, and knowledge of PseudoSourceValues. This unfortunately isn't sufficient to allow constants to be rematerialized in PIC mode -- the extra indirection is a complication. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54000 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 11 years ago
6 changed file(s) with 144 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
3131
3232 namespace llvm {
3333
34 class AliasAnalysis;
3435 class LiveVariables;
3536 class MachineLoopInfo;
3637 class TargetRegisterInfo;
6061 const TargetMachine* tm_;
6162 const TargetRegisterInfo* tri_;
6263 const TargetInstrInfo* tii_;
64 AliasAnalysis *aa_;
6365 LiveVariables* lv_;
6466
6567 /// Special pool allocator for VNInfo's (LiveInterval val#).
205205 liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
206206 liveout_iterator liveout_end() const { return LiveOuts.end(); }
207207 bool liveout_empty() const { return LiveOuts.empty(); }
208
209 bool isLiveIn(unsigned Reg) const {
210 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
211 if (I->first == Reg || I->second == Reg)
212 return true;
213 return false;
214 }
215
208216 private:
209217 void HandleVRegListReallocation();
210218
1616 #include "llvm/Value.h"
1717
1818 namespace llvm {
19 class MachineFrameInfo;
20
1921 /// PseudoSourceValue - Special value supplied for machine level alias
2022 /// analysis. It indicates that the a memory access references the functions
2123 /// stack frame (e.g., a spill slot), below the stack frame (e.g., argument
2527 PseudoSourceValue();
2628
2729 virtual void print(std::ostream &OS) const;
30
31 /// isConstant - Test whether this PseudoSourceValue has a constant value.
32 ///
33 virtual bool isConstant(const MachineFrameInfo *) const;
2834
2935 /// classof - Methods for support type inquiry through isa, cast, and
3036 /// dyn_cast:
1818 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
1919 #include "VirtRegMap.h"
2020 #include "llvm/Value.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
2122 #include "llvm/CodeGen/LiveVariables.h"
2223 #include "llvm/CodeGen/MachineFrameInfo.h"
2324 #include "llvm/CodeGen/MachineInstr.h"
2425 #include "llvm/CodeGen/MachineLoopInfo.h"
2526 #include "llvm/CodeGen/MachineRegisterInfo.h"
2627 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/CodeGen/PseudoSourceValue.h"
2729 #include "llvm/Target/TargetRegisterInfo.h"
2830 #include "llvm/Target/TargetInstrInfo.h"
2931 #include "llvm/Target/TargetMachine.h"
5355 static RegisterPass X("liveintervals", "Live Interval Analysis");
5456
5557 void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.addRequired();
59 AU.addPreserved();
5660 AU.addPreserved();
5761 AU.addRequired();
5862 AU.addPreservedID(MachineLoopInfoID);
211215 tm_ = &fn.getTarget();
212216 tri_ = tm_->getRegisterInfo();
213217 tii_ = tm_->getInstrInfo();
218 aa_ = &getAnalysis();
214219 lv_ = &getAnalysis();
215220 allocatableRegs_ = tri_->getAllocatableSet(fn);
216221
749754 assert(!RegOp &&
750755 "Can't rematerialize instruction with multiple register operand!");
751756 RegOp = MO.getReg();
757 #ifndef NDEBUG
752758 break;
759 #endif
753760 }
754761 return RegOp;
755762 }
772779 if (DisableReMat)
773780 return false;
774781
775 isLoad = false;
776782 if (MI->getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
777783 return true;
778784
785791 // This is a load from fixed stack slot. It can be rematerialized.
786792 return true;
787793
788 if (tii_->isTriviallyReMaterializable(MI)) {
794 // If the target-specific rules don't identify an instruction as
795 // being trivially rematerializable, use some target-independent
796 // rules.
797 if (!MI->getDesc().isRematerializable() ||
798 !tii_->isTriviallyReMaterializable(MI)) {
799
800 // If the instruction access memory but the memoperands have been lost,
801 // we can't analyze it.
789802 const TargetInstrDesc &TID = MI->getDesc();
790 isLoad = TID.isSimpleLoad();
791
792 unsigned ImpUse = getReMatImplicitUse(li, MI);
793 if (ImpUse) {
794 const LiveInterval &ImpLi = getInterval(ImpUse);
795 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
796 re = mri_->use_end(); ri != re; ++ri) {
797 MachineInstr *UseMI = &*ri;
798 unsigned UseIdx = getInstructionIndex(UseMI);
799 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
803 if ((TID.mayLoad() || TID.mayStore()) && MI->memoperands_empty())
804 return false;
805
806 // Avoid instructions obviously unsafe for remat.
807 if (TID.hasUnmodeledSideEffects() || TID.isNotDuplicable())
808 return false;
809
810 // If the instruction accesses memory and the memory could be non-constant,
811 // assume the instruction is not rematerializable.
812 for (alist::const_iterator I = MI->memoperands_begin(),
813 E = MI->memoperands_end(); I != E; ++I) {
814 const MachineMemOperand &MMO = *I;
815 if (MMO.isVolatile() || MMO.isStore())
816 return false;
817 const Value *V = MMO.getValue();
818 if (!V)
819 return false;
820 if (const PseudoSourceValue *PSV = dyn_cast(V)) {
821 if (!PSV->isConstant(mf_->getFrameInfo()))
822 return false;
823 } else if (!aa_->pointsToConstantMemory(V))
824 return false;
825 }
826
827 // If any of the registers accessed are non-constant, conservatively assume
828 // the instruction is not rematerializable.
829 unsigned ImpUse = 0;
830 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
831 const MachineOperand &MO = MI->getOperand(i);
832 if (MO.isReg()) {
833 unsigned Reg = MO.getReg();
834 if (Reg == 0)
800835 continue;
801 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
836 if (TargetRegisterInfo::isPhysicalRegister(Reg))
802837 return false;
803 }
804 }
805 return true;
806 }
807
808 return false;
838
839 // Only allow one def, and that in the first operand.
840 if (MO.isDef() != (i == 0))
841 return false;
842
843 // Only allow constant-valued registers.
844 bool IsLiveIn = mri_->isLiveIn(Reg);
845 MachineRegisterInfo::def_iterator I = mri_->def_begin(Reg),
846 E = mri_->def_end();
847
848 // For the def, it should be the only def.
849 if (MO.isDef() && (next(I) != E || IsLiveIn))
850 return false;
851
852 if (MO.isUse()) {
853 // Only allow one use other register use, as that's all the
854 // remat mechanisms support currently.
855 if (Reg != li.reg) {
856 if (ImpUse == 0)
857 ImpUse = Reg;
858 else if (Reg != ImpUse)
859 return false;
860 }
861 // For uses, there should be only one associate def.
862 if (I != E && (next(I) != E || IsLiveIn))
863 return false;
864 }
865 }
866 }
867 }
868
869 unsigned ImpUse = getReMatImplicitUse(li, MI);
870 if (ImpUse) {
871 const LiveInterval &ImpLi = getInterval(ImpUse);
872 for (MachineRegisterInfo::use_iterator ri = mri_->use_begin(li.reg),
873 re = mri_->use_end(); ri != re; ++ri) {
874 MachineInstr *UseMI = &*ri;
875 unsigned UseIdx = getInstructionIndex(UseMI);
876 if (li.FindLiveRangeContaining(UseIdx)->valno != ValNo)
877 continue;
878 if (!isValNoAvailableAt(ImpLi, MI, UseIdx))
879 return false;
880 }
881 }
882 return true;
809883 }
810884
811885 /// isReMaterializable - Returns true if every definition of MI of every
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/CodeGen/MachineFrameInfo.h"
1314 #include "llvm/CodeGen/PseudoSourceValue.h"
1415 #include "llvm/DerivedTypes.h"
1516 #include "llvm/Support/Compiler.h"
5051 const int FI;
5152 public:
5253 explicit FixedStackPseudoSourceValue(int fi) : FI(fi) {}
54
55 virtual bool isConstant(const MachineFrameInfo *MFI) const;
56
5357 virtual void print(std::ostream &OS) const {
5458 OS << "FixedStack" << FI;
5559 }
6367 V = new FixedStackPseudoSourceValue(FI);
6468 return V;
6569 }
70
71 bool PseudoSourceValue::isConstant(const MachineFrameInfo *) const {
72 if (this == getStack())
73 return false;
74 if (this == getGOT() ||
75 this == getConstantPool() ||
76 this == getJumpTable())
77 return true;
78 assert(0 && "Unknown PseudoSourceValue!");
79 return false;
80 }
81
82 bool
83 FixedStackPseudoSourceValue::isConstant(const MachineFrameInfo *MFI) const {
84 return MFI && MFI->isImmutableObjectIndex(FI);
85 }
6686 }
0 ; RUN: llvm-as < %s | llc -march=x86-64 -relocation-model=static | grep xmm | count 2
1
2 declare void @bar() nounwind
3
4 @a = external constant float
5
6 declare void @qux(float %f) nounwind
7
8 define void @foo() nounwind {
9 %f = load float* @a
10 call void @bar()
11 call void @qux(float %f)
12 call void @qux(float %f)
13 ret void
14 }