LLVM API Documentation
00001 //===-- TargetInstrInfoImpl.cpp - Target Instruction Information ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the TargetInstrInfoImpl class, it just provides default 00011 // implementations of various methods. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Target/TargetInstrInfo.h" 00016 #include "llvm/ADT/SmallVector.h" 00017 #include "llvm/CodeGen/MachineFrameInfo.h" 00018 #include "llvm/CodeGen/MachineInstr.h" 00019 #include "llvm/CodeGen/MachineInstrBuilder.h" 00020 #include "llvm/CodeGen/PseudoSourceValue.h" 00021 #include "llvm/Support/ErrorHandling.h" 00022 #include "llvm/Support/raw_ostream.h" 00023 using namespace llvm; 00024 00025 // commuteInstruction - The default implementation of this method just exchanges 00026 // the two operands returned by findCommutedOpIndices. 00027 MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI, 00028 bool NewMI) const { 00029 const TargetInstrDesc &TID = MI->getDesc(); 00030 bool HasDef = TID.getNumDefs(); 00031 if (HasDef && !MI->getOperand(0).isReg()) 00032 // No idea how to commute this instruction. Target should implement its own. 00033 return 0; 00034 unsigned Idx1, Idx2; 00035 if (!findCommutedOpIndices(MI, Idx1, Idx2)) { 00036 std::string msg; 00037 raw_string_ostream Msg(msg); 00038 Msg << "Don't know how to commute: " << *MI; 00039 llvm_report_error(Msg.str()); 00040 } 00041 00042 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() && 00043 "This only knows how to commute register operands so far"); 00044 unsigned Reg1 = MI->getOperand(Idx1).getReg(); 00045 unsigned Reg2 = MI->getOperand(Idx2).getReg(); 00046 bool Reg1IsKill = MI->getOperand(Idx1).isKill(); 00047 bool Reg2IsKill = MI->getOperand(Idx2).isKill(); 00048 bool ChangeReg0 = false; 00049 if (HasDef && MI->getOperand(0).getReg() == Reg1) { 00050 // Must be two address instruction! 00051 assert(MI->getDesc().getOperandConstraint(0, TOI::TIED_TO) && 00052 "Expecting a two-address instruction!"); 00053 Reg2IsKill = false; 00054 ChangeReg0 = true; 00055 } 00056 00057 if (NewMI) { 00058 // Create a new instruction. 00059 unsigned Reg0 = HasDef 00060 ? (ChangeReg0 ? Reg2 : MI->getOperand(0).getReg()) : 0; 00061 bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false; 00062 MachineFunction &MF = *MI->getParent()->getParent(); 00063 if (HasDef) 00064 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) 00065 .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead)) 00066 .addReg(Reg2, getKillRegState(Reg2IsKill)) 00067 .addReg(Reg1, getKillRegState(Reg2IsKill)); 00068 else 00069 return BuildMI(MF, MI->getDebugLoc(), MI->getDesc()) 00070 .addReg(Reg2, getKillRegState(Reg2IsKill)) 00071 .addReg(Reg1, getKillRegState(Reg2IsKill)); 00072 } 00073 00074 if (ChangeReg0) 00075 MI->getOperand(0).setReg(Reg2); 00076 MI->getOperand(Idx2).setReg(Reg1); 00077 MI->getOperand(Idx1).setReg(Reg2); 00078 MI->getOperand(Idx2).setIsKill(Reg1IsKill); 00079 MI->getOperand(Idx1).setIsKill(Reg2IsKill); 00080 return MI; 00081 } 00082 00083 /// findCommutedOpIndices - If specified MI is commutable, return the two 00084 /// operand indices that would swap value. Return true if the instruction 00085 /// is not in a form which this routine understands. 00086 bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI, 00087 unsigned &SrcOpIdx1, 00088 unsigned &SrcOpIdx2) const { 00089 const TargetInstrDesc &TID = MI->getDesc(); 00090 if (!TID.isCommutable()) 00091 return false; 00092 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this 00093 // is not true, then the target must implement this. 00094 SrcOpIdx1 = TID.getNumDefs(); 00095 SrcOpIdx2 = SrcOpIdx1 + 1; 00096 if (!MI->getOperand(SrcOpIdx1).isReg() || 00097 !MI->getOperand(SrcOpIdx2).isReg()) 00098 // No idea. 00099 return false; 00100 return true; 00101 } 00102 00103 00104 bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI, 00105 const SmallVectorImpl<MachineOperand> &Pred) const { 00106 bool MadeChange = false; 00107 const TargetInstrDesc &TID = MI->getDesc(); 00108 if (!TID.isPredicable()) 00109 return false; 00110 00111 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) { 00112 if (TID.OpInfo[i].isPredicate()) { 00113 MachineOperand &MO = MI->getOperand(i); 00114 if (MO.isReg()) { 00115 MO.setReg(Pred[j].getReg()); 00116 MadeChange = true; 00117 } else if (MO.isImm()) { 00118 MO.setImm(Pred[j].getImm()); 00119 MadeChange = true; 00120 } else if (MO.isMBB()) { 00121 MO.setMBB(Pred[j].getMBB()); 00122 MadeChange = true; 00123 } 00124 ++j; 00125 } 00126 } 00127 return MadeChange; 00128 } 00129 00130 void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB, 00131 MachineBasicBlock::iterator I, 00132 unsigned DestReg, 00133 unsigned SubIdx, 00134 const MachineInstr *Orig) const { 00135 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig); 00136 MachineOperand &MO = MI->getOperand(0); 00137 MO.setReg(DestReg); 00138 MO.setSubReg(SubIdx); 00139 MBB.insert(I, MI); 00140 } 00141 00142 bool TargetInstrInfoImpl::isDeadInstruction(const MachineInstr *MI) const { 00143 const TargetInstrDesc &TID = MI->getDesc(); 00144 if (TID.mayLoad() || TID.mayStore() || TID.isCall() || TID.isTerminator() || 00145 TID.isCall() || TID.isBarrier() || TID.isReturn() || 00146 TID.hasUnmodeledSideEffects()) 00147 return false; 00148 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00149 const MachineOperand &MO = MI->getOperand(i); 00150 if (!MO.isReg() || !MO.getReg()) 00151 continue; 00152 if (MO.isDef() && !MO.isDead()) 00153 return false; 00154 if (MO.isUse() && MO.isKill()) 00155 // FIXME: We can't remove kill markers or else the scavenger will assert. 00156 // An alternative is to add a ADD pseudo instruction to replace kill 00157 // markers. 00158 return false; 00159 } 00160 return true; 00161 } 00162 00163 unsigned 00164 TargetInstrInfoImpl::GetFunctionSizeInBytes(const MachineFunction &MF) const { 00165 unsigned FnSize = 0; 00166 for (MachineFunction::const_iterator MBBI = MF.begin(), E = MF.end(); 00167 MBBI != E; ++MBBI) { 00168 const MachineBasicBlock &MBB = *MBBI; 00169 for (MachineBasicBlock::const_iterator I = MBB.begin(),E = MBB.end(); 00170 I != E; ++I) 00171 FnSize += GetInstSizeInBytes(I); 00172 } 00173 return FnSize; 00174 } 00175 00176 /// foldMemoryOperand - Attempt to fold a load or store of the specified stack 00177 /// slot into the specified machine instruction for the specified operand(s). 00178 /// If this is possible, a new instruction is returned with the specified 00179 /// operand folded, otherwise NULL is returned. The client is responsible for 00180 /// removing the old instruction and adding the new one in the instruction 00181 /// stream. 00182 MachineInstr* 00183 TargetInstrInfo::foldMemoryOperand(MachineFunction &MF, 00184 MachineInstr* MI, 00185 const SmallVectorImpl<unsigned> &Ops, 00186 int FrameIndex) const { 00187 unsigned Flags = 0; 00188 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 00189 if (MI->getOperand(Ops[i]).isDef()) 00190 Flags |= MachineMemOperand::MOStore; 00191 else 00192 Flags |= MachineMemOperand::MOLoad; 00193 00194 // Ask the target to do the actual folding. 00195 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FrameIndex); 00196 if (!NewMI) return 0; 00197 00198 assert((!(Flags & MachineMemOperand::MOStore) || 00199 NewMI->getDesc().mayStore()) && 00200 "Folded a def to a non-store!"); 00201 assert((!(Flags & MachineMemOperand::MOLoad) || 00202 NewMI->getDesc().mayLoad()) && 00203 "Folded a use to a non-load!"); 00204 const MachineFrameInfo &MFI = *MF.getFrameInfo(); 00205 assert(MFI.getObjectOffset(FrameIndex) != -1); 00206 MachineMemOperand MMO(PseudoSourceValue::getFixedStack(FrameIndex), 00207 Flags, 00208 MFI.getObjectOffset(FrameIndex), 00209 MFI.getObjectSize(FrameIndex), 00210 MFI.getObjectAlignment(FrameIndex)); 00211 NewMI->addMemOperand(MF, MMO); 00212 00213 return NewMI; 00214 } 00215 00216 /// foldMemoryOperand - Same as the previous version except it allows folding 00217 /// of any load and store from / to any address, not just from a specific 00218 /// stack slot. 00219 MachineInstr* 00220 TargetInstrInfo::foldMemoryOperand(MachineFunction &MF, 00221 MachineInstr* MI, 00222 const SmallVectorImpl<unsigned> &Ops, 00223 MachineInstr* LoadMI) const { 00224 assert(LoadMI->getDesc().canFoldAsLoad() && "LoadMI isn't foldable!"); 00225 #ifndef NDEBUG 00226 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 00227 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!"); 00228 #endif 00229 00230 // Ask the target to do the actual folding. 00231 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI); 00232 if (!NewMI) return 0; 00233 00234 // Copy the memoperands from the load to the folded instruction. 00235 for (std::list<MachineMemOperand>::iterator I = LoadMI->memoperands_begin(), 00236 E = LoadMI->memoperands_end(); I != E; ++I) 00237 NewMI->addMemOperand(MF, *I); 00238 00239 return NewMI; 00240 }