| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef LLVM_CODEGEN_REACHINGDEFANALYSIS_H |
| 22 |
#define LLVM_CODEGEN_REACHINGDEFANALYSIS_H |
| 23 |
|
| 24 |
#include "llvm/ADT/DenseMap.h" |
| 25 |
#include "llvm/ADT/SmallVector.h" |
| 26 |
#include "llvm/ADT/TinyPtrVector.h" |
| 27 |
#include "llvm/CodeGen/LoopTraversal.h" |
| 28 |
#include "llvm/CodeGen/MachineFunctionPass.h" |
| 29 |
#include "llvm/InitializePasses.h" |
| 30 |
|
| 31 |
namespace llvm { |
| 32 |
|
| 33 |
class MachineBasicBlock; |
| 34 |
class MachineInstr; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class ReachingDef { |
| 41 |
uintptr_t Encoded; |
| 42 |
friend struct PointerLikeTypeTraits<ReachingDef>; |
| 43 |
explicit ReachingDef(uintptr_t Encoded) : Encoded(Encoded) {} |
| 44 |
|
| 45 |
public: |
| 46 |
ReachingDef(std::nullptr_t) : Encoded(0) {} |
| 47 |
ReachingDef(int Instr) : Encoded(((uintptr_t) Instr << 2) | 2) {} |
| 48 |
operator int() const { return ((int) Encoded) >> 2; } |
| 49 |
}; |
| 50 |
|
| 51 |
template<> |
| 52 |
struct PointerLikeTypeTraits<ReachingDef> { |
| 53 |
static constexpr int NumLowBitsAvailable = 1; |
| 54 |
|
| 55 |
static inline void *getAsVoidPointer(const ReachingDef &RD) { |
| 56 |
return reinterpret_cast<void *>(RD.Encoded); |
| 57 |
} |
| 58 |
|
| 59 |
static inline ReachingDef getFromVoidPointer(void *P) { |
| 60 |
return ReachingDef(reinterpret_cast<uintptr_t>(P)); |
| 61 |
} |
| 62 |
|
| 63 |
static inline ReachingDef getFromVoidPointer(const void *P) { |
| 64 |
return ReachingDef(reinterpret_cast<uintptr_t>(P)); |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
class ReachingDefAnalysis : public MachineFunctionPass { |
| 70 |
private: |
| 71 |
MachineFunction *MF; |
| 72 |
const TargetRegisterInfo *TRI; |
| 73 |
LoopTraversal::TraversalOrder TraversedMBBOrder; |
| 74 |
unsigned NumRegUnits; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
using LiveRegsDefInfo = std::vector<int>; |
| 80 |
LiveRegsDefInfo LiveRegs; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
using OutRegsInfoMap = SmallVector<LiveRegsDefInfo, 4>; |
| 86 |
OutRegsInfoMap MBBOutRegsInfos; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
int CurInstr; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
DenseMap<MachineInstr *, int> InstIds; |
| 95 |
|
| 96 |
|
| 97 |
using MBBRegUnitDefs = TinyPtrVector<ReachingDef>; |
| 98 |
|
| 99 |
using MBBDefsInfo = std::vector<MBBRegUnitDefs>; |
| 100 |
|
| 101 |
using MBBReachingDefsInfo = SmallVector<MBBDefsInfo, 4>; |
| 102 |
MBBReachingDefsInfo MBBReachingDefs; |
| 103 |
|
| 104 |
|
| 105 |
const int ReachingDefDefaultVal = -(1 << 20); |
| 106 |
|
| 107 |
using InstSet = SmallPtrSetImpl<MachineInstr*>; |
| 108 |
using BlockSet = SmallPtrSetImpl<MachineBasicBlock*>; |
| 109 |
|
| 110 |
public: |
| 111 |
static char ID; |
| 112 |
|
| 113 |
ReachingDefAnalysis() : MachineFunctionPass(ID) { |
| 114 |
initializeReachingDefAnalysisPass(*PassRegistry::getPassRegistry()); |
| 115 |
} |
| 116 |
void releaseMemory() override; |
| 117 |
|
| 118 |
void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 119 |
AU.setPreservesAll(); |
| 120 |
MachineFunctionPass::getAnalysisUsage(AU); |
| 121 |
} |
| 122 |
|
| 123 |
bool runOnMachineFunction(MachineFunction &MF) override; |
| 124 |
|
| 125 |
MachineFunctionProperties getRequiredProperties() const override { |
| 126 |
return MachineFunctionProperties().set( |
| 127 |
MachineFunctionProperties::Property::NoVRegs).set( |
| 128 |
MachineFunctionProperties::Property::TracksLiveness); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
void reset(); |
| 133 |
|
| 134 |
|
| 135 |
void init(); |
| 136 |
|
| 137 |
|
| 138 |
void traverse(); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
int getReachingDef(MachineInstr *MI, MCRegister PhysReg) const; |
| 143 |
|
| 144 |
|
| 145 |
bool hasSameReachingDef(MachineInstr *A, MachineInstr *B, |
| 146 |
MCRegister PhysReg) const; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
bool isReachingDefLiveOut(MachineInstr *MI, MCRegister PhysReg) const; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
MachineInstr *getLocalLiveOutMIDef(MachineBasicBlock *MBB, |
| 155 |
MCRegister PhysReg) const; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
MachineInstr *getUniqueReachingMIDef(MachineInstr *MI, |
| 160 |
MCRegister PhysReg) const; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
MachineInstr *getMIOperand(MachineInstr *MI, unsigned Idx) const; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
MachineInstr *getMIOperand(MachineInstr *MI, MachineOperand &MO) const; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
bool hasLocalDefBefore(MachineInstr *MI, MCRegister PhysReg) const; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
bool isRegUsedAfter(MachineInstr *MI, MCRegister PhysReg) const; |
| 177 |
|
| 178 |
|
| 179 |
bool isRegDefinedAfter(MachineInstr *MI, MCRegister PhysReg) const; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
int getClearance(MachineInstr *MI, MCRegister PhysReg) const; |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
void getReachingLocalUses(MachineInstr *MI, MCRegister PhysReg, |
| 188 |
InstSet &Uses) const; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
void getLiveOuts(MachineBasicBlock *MBB, MCRegister PhysReg, InstSet &Defs, |
| 193 |
BlockSet &VisitedBBs) const; |
| 194 |
void getLiveOuts(MachineBasicBlock *MBB, MCRegister PhysReg, |
| 195 |
InstSet &Defs) const; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
bool getLiveInUses(MachineBasicBlock *MBB, MCRegister PhysReg, |
| 201 |
InstSet &Uses) const; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
void getGlobalUses(MachineInstr *MI, MCRegister PhysReg, InstSet &Uses) const; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
void getGlobalReachingDefs(MachineInstr *MI, MCRegister PhysReg, |
| 210 |
InstSet &Defs) const; |
| 211 |
|
| 212 |
|
| 213 |
bool isSafeToMoveForwards(MachineInstr *From, MachineInstr *To) const; |
| 214 |
|
| 215 |
|
| 216 |
bool isSafeToMoveBackwards(MachineInstr *From, MachineInstr *To) const; |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
void collectKilledOperands(MachineInstr *MI, InstSet &Dead) const; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
bool isSafeToRemove(MachineInstr *MI, InstSet &ToRemove) const; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
bool isSafeToRemove(MachineInstr *MI, InstSet &ToRemove, |
| 230 |
InstSet &Ignore) const; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
bool isSafeToDefRegAt(MachineInstr *MI, MCRegister PhysReg) const; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
bool isSafeToDefRegAt(MachineInstr *MI, MCRegister PhysReg, |
| 240 |
InstSet &Ignore) const; |
| 241 |
|
| 242 |
private: |
| 243 |
|
| 244 |
void enterBasicBlock(MachineBasicBlock *MBB); |
| 245 |
|
| 246 |
|
| 247 |
void leaveBasicBlock(MachineBasicBlock *MBB); |
| 248 |
|
| 249 |
|
| 250 |
void processBasicBlock(const LoopTraversal::TraversedMBBInfo &TraversedMBB); |
| 251 |
|
| 252 |
|
| 253 |
void reprocessBasicBlock(MachineBasicBlock *MBB); |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
void processDefs(MachineInstr *); |
| 258 |
|
| 259 |
|
| 260 |
template<typename Iterator> |
| 261 |
bool isSafeToMove(MachineInstr *From, MachineInstr *To) const; |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
bool isSafeToRemove(MachineInstr *MI, InstSet &Visited, |
| 267 |
InstSet &ToRemove, InstSet &Ignore) const; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
MachineInstr *getInstFromId(MachineBasicBlock *MBB, int InstId) const; |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
MachineInstr *getReachingLocalMIDef(MachineInstr *MI, |
| 276 |
MCRegister PhysReg) const; |
| 277 |
}; |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
#endif |