| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#ifndef LLVM_CODEGEN_MODULOSCHEDULE_H |
| 61 |
#define LLVM_CODEGEN_MODULOSCHEDULE_H |
| 62 |
|
| 63 |
#include "llvm/CodeGen/MachineFunction.h" |
| 64 |
#include "llvm/CodeGen/MachineLoopInfo.h" |
| 65 |
#include "llvm/CodeGen/MachineLoopUtils.h" |
| 66 |
#include "llvm/CodeGen/TargetInstrInfo.h" |
| 67 |
#include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 68 |
#include <deque> |
| 69 |
#include <vector> |
| 70 |
|
| 71 |
namespace llvm { |
| 72 |
class MachineBasicBlock; |
| 73 |
class MachineInstr; |
| 74 |
class LiveIntervals; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
class ModuloSchedule { |
| 79 |
private: |
| 80 |
|
| 81 |
MachineLoop *Loop; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
std::vector<MachineInstr *> ScheduledInstrs; |
| 87 |
|
| 88 |
|
| 89 |
DenseMap<MachineInstr *, int> Cycle; |
| 90 |
|
| 91 |
|
| 92 |
DenseMap<MachineInstr *, int> Stage; |
| 93 |
|
| 94 |
|
| 95 |
int NumStages; |
| 96 |
|
| 97 |
public: |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
ModuloSchedule(MachineFunction &MF, MachineLoop *Loop, |
| 106 |
std::vector<MachineInstr *> ScheduledInstrs, |
| 107 |
DenseMap<MachineInstr *, int> Cycle, |
| 108 |
DenseMap<MachineInstr *, int> Stage) |
| 109 |
: Loop(Loop), ScheduledInstrs(ScheduledInstrs), Cycle(std::move(Cycle)), |
| 110 |
Stage(std::move(Stage)) { |
| 111 |
NumStages = 0; |
| 112 |
for (auto &KV : this->Stage) |
| 113 |
NumStages = std::max(NumStages, KV.second); |
| 114 |
++NumStages; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
MachineLoop *getLoop() const { return Loop; } |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
int getNumStages() const { return NumStages; } |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
int getFirstCycle() { return Cycle[ScheduledInstrs.front()]; } |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
int getFinalCycle() { return Cycle[ScheduledInstrs.back()]; } |
| 131 |
|
| 132 |
|
| 133 |
int getStage(MachineInstr *MI) { |
| 134 |
auto I = Stage.find(MI); |
| 135 |
return I == Stage.end() ? -1 : I->second; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
int getCycle(MachineInstr *MI) { |
| 140 |
auto I = Cycle.find(MI); |
| 141 |
return I == Cycle.end() ? -1 : I->second; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
void setStage(MachineInstr *MI, int MIStage) { |
| 146 |
assert(Stage.count(MI) == 0); |
| 147 |
Stage[MI] = MIStage; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
ArrayRef<MachineInstr *> getInstructions() { return ScheduledInstrs; } |
| 152 |
|
| 153 |
void dump() { print(dbgs()); } |
| 154 |
void print(raw_ostream &OS); |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
class ModuloScheduleExpander { |
| 160 |
public: |
| 161 |
using InstrChangesTy = DenseMap<MachineInstr *, std::pair<unsigned, int64_t>>; |
| 162 |
|
| 163 |
private: |
| 164 |
using ValueMapTy = DenseMap<unsigned, unsigned>; |
| 165 |
using MBBVectorTy = SmallVectorImpl<MachineBasicBlock *>; |
| 166 |
using InstrMapTy = DenseMap<MachineInstr *, MachineInstr *>; |
| 167 |
|
| 168 |
ModuloSchedule &Schedule; |
| 169 |
MachineFunction &MF; |
| 170 |
const TargetSubtargetInfo &ST; |
| 171 |
MachineRegisterInfo &MRI; |
| 172 |
const TargetInstrInfo *TII; |
| 173 |
LiveIntervals &LIS; |
| 174 |
|
| 175 |
MachineBasicBlock *BB; |
| 176 |
MachineBasicBlock *Preheader; |
| 177 |
MachineBasicBlock *NewKernel = nullptr; |
| 178 |
std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo> LoopInfo; |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
std::map<unsigned, std::pair<unsigned, bool>> RegToStageDiff; |
| 185 |
|
| 186 |
|
| 187 |
InstrChangesTy InstrChanges; |
| 188 |
|
| 189 |
void generatePipelinedLoop(); |
| 190 |
void generateProlog(unsigned LastStage, MachineBasicBlock *KernelBB, |
| 191 |
ValueMapTy *VRMap, MBBVectorTy &PrologBBs); |
| 192 |
void generateEpilog(unsigned LastStage, MachineBasicBlock *KernelBB, |
| 193 |
ValueMapTy *VRMap, MBBVectorTy &EpilogBBs, |
| 194 |
MBBVectorTy &PrologBBs); |
| 195 |
void generateExistingPhis(MachineBasicBlock *NewBB, MachineBasicBlock *BB1, |
| 196 |
MachineBasicBlock *BB2, MachineBasicBlock *KernelBB, |
| 197 |
ValueMapTy *VRMap, InstrMapTy &InstrMap, |
| 198 |
unsigned LastStageNum, unsigned CurStageNum, |
| 199 |
bool IsLast); |
| 200 |
void generatePhis(MachineBasicBlock *NewBB, MachineBasicBlock *BB1, |
| 201 |
MachineBasicBlock *BB2, MachineBasicBlock *KernelBB, |
| 202 |
ValueMapTy *VRMap, InstrMapTy &InstrMap, |
| 203 |
unsigned LastStageNum, unsigned CurStageNum, bool IsLast); |
| 204 |
void removeDeadInstructions(MachineBasicBlock *KernelBB, |
| 205 |
MBBVectorTy &EpilogBBs); |
| 206 |
void splitLifetimes(MachineBasicBlock *KernelBB, MBBVectorTy &EpilogBBs); |
| 207 |
void addBranches(MachineBasicBlock &PreheaderBB, MBBVectorTy &PrologBBs, |
| 208 |
MachineBasicBlock *KernelBB, MBBVectorTy &EpilogBBs, |
| 209 |
ValueMapTy *VRMap); |
| 210 |
bool computeDelta(MachineInstr &MI, unsigned &Delta); |
| 211 |
void updateMemOperands(MachineInstr &NewMI, MachineInstr &OldMI, |
| 212 |
unsigned Num); |
| 213 |
MachineInstr *cloneInstr(MachineInstr *OldMI, unsigned CurStageNum, |
| 214 |
unsigned InstStageNum); |
| 215 |
MachineInstr *cloneAndChangeInstr(MachineInstr *OldMI, unsigned CurStageNum, |
| 216 |
unsigned InstStageNum); |
| 217 |
void updateInstruction(MachineInstr *NewMI, bool LastDef, |
| 218 |
unsigned CurStageNum, unsigned InstrStageNum, |
| 219 |
ValueMapTy *VRMap); |
| 220 |
MachineInstr *findDefInLoop(unsigned Reg); |
| 221 |
unsigned getPrevMapVal(unsigned StageNum, unsigned PhiStage, unsigned LoopVal, |
| 222 |
unsigned LoopStage, ValueMapTy *VRMap, |
| 223 |
MachineBasicBlock *BB); |
| 224 |
void rewritePhiValues(MachineBasicBlock *NewBB, unsigned StageNum, |
| 225 |
ValueMapTy *VRMap, InstrMapTy &InstrMap); |
| 226 |
void rewriteScheduledInstr(MachineBasicBlock *BB, InstrMapTy &InstrMap, |
| 227 |
unsigned CurStageNum, unsigned PhiNum, |
| 228 |
MachineInstr *Phi, unsigned OldReg, |
| 229 |
unsigned NewReg, unsigned PrevReg = 0); |
| 230 |
bool isLoopCarried(MachineInstr &Phi); |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
unsigned getStagesForReg(int Reg, unsigned CurStage) { |
| 235 |
std::pair<unsigned, bool> Stages = RegToStageDiff[Reg]; |
| 236 |
if ((int)CurStage > Schedule.getNumStages() - 1 && Stages.first == 0 && |
| 237 |
Stages.second) |
| 238 |
return 1; |
| 239 |
return Stages.first; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
unsigned getStagesForPhi(int Reg) { |
| 249 |
std::pair<unsigned, bool> Stages = RegToStageDiff[Reg]; |
| 250 |
if (Stages.second) |
| 251 |
return Stages.first; |
| 252 |
return Stages.first - 1; |
| 253 |
} |
| 254 |
|
| 255 |
public: |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
ModuloScheduleExpander(MachineFunction &MF, ModuloSchedule &S, |
| 262 |
LiveIntervals &LIS, InstrChangesTy InstrChanges) |
| 263 |
: Schedule(S), MF(MF), ST(MF.getSubtarget()), MRI(MF.getRegInfo()), |
| 264 |
TII(ST.getInstrInfo()), LIS(LIS), |
| 265 |
InstrChanges(std::move(InstrChanges)) {} |
| 266 |
|
| 267 |
|
| 268 |
void expand(); |
| 269 |
|
| 270 |
void cleanup(); |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
MachineBasicBlock *getRewrittenKernel() { return NewKernel; } |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
class PeelingModuloScheduleExpander { |
| 280 |
public: |
| 281 |
PeelingModuloScheduleExpander(MachineFunction &MF, ModuloSchedule &S, |
| 282 |
LiveIntervals *LIS) |
| 283 |
: Schedule(S), MF(MF), ST(MF.getSubtarget()), MRI(MF.getRegInfo()), |
| 284 |
TII(ST.getInstrInfo()), LIS(LIS) {} |
| 285 |
|
| 286 |
void expand(); |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
void validateAgainstModuloScheduleExpander(); |
| 291 |
|
| 292 |
protected: |
| 293 |
ModuloSchedule &Schedule; |
| 294 |
MachineFunction &MF; |
| 295 |
const TargetSubtargetInfo &ST; |
| 296 |
MachineRegisterInfo &MRI; |
| 297 |
const TargetInstrInfo *TII; |
| 298 |
LiveIntervals *LIS; |
| 299 |
|
| 300 |
|
| 301 |
MachineBasicBlock *BB; |
| 302 |
|
| 303 |
MachineBasicBlock *Preheader; |
| 304 |
|
| 305 |
SmallVector<MachineBasicBlock *, 4> Prologs, Epilogs; |
| 306 |
|
| 307 |
DenseMap<MachineBasicBlock *, BitVector> LiveStages; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
DenseMap<MachineBasicBlock *, BitVector> AvailableStages; |
| 312 |
|
| 313 |
|
| 314 |
DenseMap<MachineInstr *, unsigned> PhiNodeLoopIteration; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
DenseMap<MachineInstr *, MachineInstr *> CanonicalMIs; |
| 319 |
DenseMap<std::pair<MachineBasicBlock *, MachineInstr *>, MachineInstr *> |
| 320 |
BlockMIs; |
| 321 |
|
| 322 |
|
| 323 |
std::deque<MachineBasicBlock *> PeeledFront, PeeledBack; |
| 324 |
|
| 325 |
SmallVector<MachineInstr *, 4> IllegalPhisToDelete; |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
void rewriteKernel(); |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
MachineBasicBlock *peelKernel(LoopPeelDirection LPD); |
| 334 |
|
| 335 |
|
| 336 |
void filterInstructions(MachineBasicBlock *MB, int MinStage); |
| 337 |
|
| 338 |
|
| 339 |
void moveStageBetweenBlocks(MachineBasicBlock *DestBB, |
| 340 |
MachineBasicBlock *SourceBB, unsigned Stage); |
| 341 |
|
| 342 |
|
| 343 |
void peelPrologAndEpilogs(); |
| 344 |
|
| 345 |
|
| 346 |
Register getEquivalentRegisterIn(Register Reg, MachineBasicBlock *BB); |
| 347 |
|
| 348 |
|
| 349 |
void rewriteUsesOf(MachineInstr *MI); |
| 350 |
|
| 351 |
void fixupBranches(); |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
MachineBasicBlock *CreateLCSSAExitingBlock(); |
| 356 |
|
| 357 |
unsigned getStage(MachineInstr *MI) { |
| 358 |
if (CanonicalMIs.count(MI)) |
| 359 |
MI = CanonicalMIs[MI]; |
| 360 |
return Schedule.getStage(MI); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
Register getPhiCanonicalReg(MachineInstr* CanonicalPhi, MachineInstr* Phi); |
| 365 |
|
| 366 |
std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo> LoopInfo; |
| 367 |
}; |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
class ModuloScheduleTestAnnotater { |
| 376 |
MachineFunction &MF; |
| 377 |
ModuloSchedule &S; |
| 378 |
|
| 379 |
public: |
| 380 |
ModuloScheduleTestAnnotater(MachineFunction &MF, ModuloSchedule &S) |
| 381 |
: MF(MF), S(S) {} |
| 382 |
|
| 383 |
|
| 384 |
void annotate(); |
| 385 |
}; |
| 386 |
|
| 387 |
} |
| 388 |
|
| 389 |
#endif |