Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 31fe4c9

Browse files
author
Kyle Butt
committed
Codegen: Factor tail duplication into a utility class. NFC
This is in preparation for tail duplication during block placement. See D18226. This needs to be a utility class for 2 reasons. No passes may run after block placement, and also, tail-duplication affects subsequent layout decisions, so it must be interleaved with placement, and can't be separated out into its own pass. The original pass is still useful, and now runs by delegating to the utility class. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265842 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d419844 commit 31fe4c9

File tree

4 files changed

+1011
-949
lines changed

4 files changed

+1011
-949
lines changed

include/llvm/CodeGen/TailDuplicator.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//===-- llvm/CodeGen/TailDuplicator.h ---------------------------*- C++ -*-===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file defines the TailDuplicator class. Used by the
11+
// TailDuplication pass, and MachineBlockPlacement.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_CODEGEN_TAILDUPLICATOR_H
16+
#define LLVM_CODEGEN_TAILDUPLICATOR_H
17+
18+
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
19+
#include "llvm/CodeGen/MachineModuleInfo.h"
20+
#include "llvm/CodeGen/MachineRegisterInfo.h"
21+
#include "llvm/CodeGen/MachineSSAUpdater.h"
22+
#include "llvm/CodeGen/RegisterScavenging.h"
23+
#include "llvm/Target/TargetInstrInfo.h"
24+
#include "llvm/Target/TargetRegisterInfo.h"
25+
#include "llvm/Target/TargetSubtargetInfo.h"
26+
27+
namespace llvm {
28+
29+
/// Utility class to perform tail duplication.
30+
class TailDuplicator {
31+
const TargetInstrInfo *TII;
32+
const TargetRegisterInfo *TRI;
33+
const MachineBranchProbabilityInfo *MBPI;
34+
const MachineModuleInfo *MMI;
35+
MachineRegisterInfo *MRI;
36+
std::unique_ptr<RegScavenger> RS;
37+
bool PreRegAlloc;
38+
39+
// A list of virtual registers for which to update SSA form.
40+
SmallVector<unsigned, 16> SSAUpdateVRs;
41+
42+
// For each virtual register in SSAUpdateVals keep a list of source virtual
43+
// registers.
44+
typedef std::vector<std::pair<MachineBasicBlock *, unsigned>> AvailableValsTy;
45+
46+
DenseMap<unsigned, AvailableValsTy> SSAUpdateVals;
47+
48+
public:
49+
void initMF(MachineFunction &MF, const MachineModuleInfo *MMI,
50+
const MachineBranchProbabilityInfo *MBPI);
51+
bool tailDuplicateBlocks(MachineFunction &MF);
52+
static bool isSimpleBB(MachineBasicBlock *TailBB);
53+
bool shouldTailDuplicate(const MachineFunction &MF, bool IsSimple,
54+
MachineBasicBlock &TailBB);
55+
bool tailDuplicateAndUpdate(MachineFunction &MF, bool IsSimple,
56+
MachineBasicBlock *MBB);
57+
58+
private:
59+
void addSSAUpdateEntry(unsigned OrigReg, unsigned NewReg,
60+
MachineBasicBlock *BB);
61+
void processPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
62+
MachineBasicBlock *PredBB,
63+
DenseMap<unsigned, unsigned> &LocalVRMap,
64+
SmallVectorImpl<std::pair<unsigned, unsigned>> &Copies,
65+
const DenseSet<unsigned> &UsedByPhi, bool Remove);
66+
void duplicateInstruction(MachineInstr *MI, MachineBasicBlock *TailBB,
67+
MachineBasicBlock *PredBB, MachineFunction &MF,
68+
DenseMap<unsigned, unsigned> &LocalVRMap,
69+
const DenseSet<unsigned> &UsedByPhi);
70+
void updateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
71+
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
72+
SmallSetVector<MachineBasicBlock *, 8> &Succs);
73+
bool canCompletelyDuplicateBB(MachineBasicBlock &BB);
74+
bool duplicateSimpleBB(MachineBasicBlock *TailBB,
75+
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
76+
const DenseSet<unsigned> &RegsUsedByPhi,
77+
SmallVectorImpl<MachineInstr *> &Copies);
78+
bool tailDuplicate(MachineFunction &MF, bool IsSimple,
79+
MachineBasicBlock *TailBB,
80+
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
81+
SmallVectorImpl<MachineInstr *> &Copies);
82+
83+
void removeDeadBlock(MachineBasicBlock *MBB);
84+
};
85+
86+
} // End llvm namespace
87+
88+
#endif

lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ add_llvm_library(LLVMCodeGen
114114
StackProtector.cpp
115115
StackSlotColoring.cpp
116116
TailDuplication.cpp
117+
TailDuplicator.cpp
117118
TargetFrameLoweringImpl.cpp
118119
TargetInstrInfo.cpp
119120
TargetLoweringBase.cpp

0 commit comments

Comments
 (0)