LLVM: lib/CodeGen/LiveRangeShrink.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

31#include

32#include

33

34using namespace llvm;

35

36#define DEBUG_TYPE "lrshrink"

37

38STATISTIC(NumInstrsHoistedToShrinkLiveRange,

39 "Number of insructions hoisted to shrink live range.");

40

41namespace {

42

44public:

45 static char ID;

46

49 }

50

51 void getAnalysisUsage(AnalysisUsage &AU) const override {

54 }

55

56 StringRef getPassName() const override { return "Live Range Shrink"; }

57

58 bool runOnMachineFunction(MachineFunction &MF) override;

59};

60

61}

62

63char LiveRangeShrink::ID = 0;

64

66

67INITIALIZE_PASS(LiveRangeShrink, "lrshrink", "Live Range Shrink Pass", false,

68 false)

69

71

72

73

74

75

76

79 const InstOrderMap &M) {

80 auto NewIter = M.find(&New);

81 if (NewIter == M.end())

82 return Old;

83 if (Old == nullptr)

84 return &New;

85 unsigned OrderOld = M.find(Old)->second;

86 unsigned OrderNew = NewIter->second;

87 if (OrderOld != OrderNew)

88 return OrderOld < OrderNew ? &New : Old;

89

90

91 for (MachineInstr *I = Old->getNextNode(); M.find(I)->second == OrderNew;

92 I = I->getNextNode())

93 if (I == &New)

94 return &New;

95 return Old;

96}

97

98

99

100

101

103 return MI.hasUnmodeledSideEffects() && MI.isPseudoProbe();

104}

105

106

107

109 InstOrderMap &M) {

110 M.clear();

111 unsigned i = 0;

114 break;

115 M[&I] = i++;

116 }

117}

118

119bool LiveRangeShrink::runOnMachineFunction(MachineFunction &MF) {

121 return false;

122

125

127

128 InstOrderMap IOM;

129

130

131

132

133 DenseMap<Register, std::pair<unsigned, MachineInstr *>> UseMap;

134

135 for (MachineBasicBlock &MBB : MF) {

137 continue;

138

141

142

145 continue;

146 }

147

151 bool SawStore = false;

152

154 MachineInstr &MI = *Next;

156

157 unsigned CurrentOrder = IOM[&MI];

159 MachineInstr *BarrierMI = nullptr;

160 for (const MachineOperand &MO : MI.operands()) {

161 if (!MO.isReg() || MO.isDebug())

162 continue;

163 if (MO.isUse())

164 UseMap[MO.getReg()] = std::make_pair(CurrentOrder, &MI);

165 else if (MO.isDead()) {

166

167

168 auto It = UseMap.find(MO.getReg());

169 if (It != UseMap.end() && Barrier < It->second.first)

170 std::tie(Barrier, BarrierMI) = It->second;

171 }

172 }

173

174 if (MI.isSafeToMove(SawStore)) {

175

176

177

180 SawStore = false;

181 }

182 continue;

183 }

184

185 const MachineOperand *DefMO = nullptr;

186 MachineInstr *Insert = nullptr;

187

188

189

190 unsigned NumEligibleUse = 0;

191

192 for (const MachineOperand &MO : MI.operands()) {

193 if (!MO.isReg() || MO.isDead() || MO.isDebug())

194 continue;

196

197

199 if (Reg || MRI.isConstantPhysReg(Reg))

200 continue;

202 break;

203 }

204 if (MO.isDef()) {

205

206 if (DefMO) {

208 break;

209 }

210 DefMO = &MO;

211 } else if (MRI.hasOneNonDBGUse(Reg) && MRI.hasOneDef(Reg) && DefMO &&

212 MRI.getRegClass(DefMO->getReg()) ==

213 MRI.getRegClass(MO.getReg())) {

214

215

216

217

218 MachineInstr &DefInstr = *MRI.def_instr_begin(Reg);

220 NumEligibleUse++;

222 } else {

224 break;

225 }

226 }

227

228

229

230 for (MachineInstr *I = Insert; I && IOM[I] == Barrier;

231 I = I->getNextNode())

232 if (I == BarrierMI) {

234 break;

235 }

236

237 if (DefMO && Insert && NumEligibleUse > 1 && Barrier <= IOM[Insert]) {

239

240 while (I != MBB.end() && (I->isPHI() || I->isDebugOrPseudoInstr()))

241 I = std::next(I);

242 if (I == MI.getIterator())

243 continue;

244

245

246

247

248 unsigned NewOrder = IOM[&*I];

249 IOM[&MI] = NewOrder;

250 NumInstrsHoistedToShrinkLiveRange++;

251

252

254 if (MI.getOperand(0).isReg())

255 for (; EndIter != MBB.end() && EndIter->isDebugValue() &&

256 EndIter->hasDebugOperandForReg(MI.getOperand(0).getReg());

257 ++EndIter)

258 IOM[&*EndIter] = NewOrder;

260 }

261 }

262 }

263 return false;

264}

unsigned const MachineRegisterInfo * MRI

const TargetInstrInfo & TII

This file defines the DenseMap class.

static bool isCodeMotionBarrier(MachineInstr &MI)

Returns whether this instruction is considered a code motion barrier by this pass.

Definition LiveRangeShrink.cpp:102

static MachineInstr * FindDominatedInstruction(MachineInstr &New, MachineInstr *Old, const InstOrderMap &M)

Returns New if it's dominated by Old, otherwise return Old.

Definition LiveRangeShrink.cpp:77

static void BuildInstOrderMap(MachineBasicBlock::iterator Start, InstOrderMap &M)

Builds Instruction to its dominating order number map M by traversing from instruction Start.

Definition LiveRangeShrink.cpp:108

Promote Memory to Register

#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)

This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...

#define STATISTIC(VARNAME, DESC)

LLVM_ABI void setPreservesCFG()

This function should be called by the pass, iff they do not:

iterator find(const_arg_type_t< KeyT > Val)

bool isEHPad() const

Returns true if the block is a landing pad.

LLVM_ABI iterator SkipPHIsLabelsAndDebug(iterator I, Register Reg=Register(), bool SkipPseudoOp=true)

Return the first instruction in MBB after I that is not a PHI, label or debug.

void splice(iterator Where, MachineBasicBlock *Other, iterator From)

Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...

MachineInstrBundleIterator< MachineInstr > iterator

MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...

void getAnalysisUsage(AnalysisUsage &AU) const override

getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.

const TargetSubtargetInfo & getSubtarget() const

getSubtarget - Return the subtarget for which this machine code is being compiled.

StringRef getName() const

getName - Return the name of the corresponding LLVM function.

MachineRegisterInfo & getRegInfo()

getRegInfo - Return information about the registers currently in use.

Function & getFunction()

Return the LLVM function that this machine code represents.

Representation of each machine instruction.

Register getReg() const

getReg - Returns the register number.

static LLVM_ABI PassRegistry * getPassRegistry()

getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...

constexpr bool isVirtual() const

Return true if the specified register number is in the virtual register namespace.

std::optional< DestSourcePair > isCopyInstr(const MachineInstr &MI) const

If the specific machine instruction is a instruction that moves/copies value from one register to ano...

virtual const TargetInstrInfo * getInstrInfo() const

This provides a very simple, boring adaptor for a begin and end iterator into a range type.

unsigned ID

LLVM IR allows to use arbitrary numbers as calling convention identifiers.

This is an optimization pass for GlobalISel generic memory operations.

iterator_range< T > make_range(T x, T y)

Convenience function for iterating over sub-ranges.

LLVM_ABI char & LiveRangeShrinkID

LiveRangeShrink pass.

Definition LiveRangeShrink.cpp:65

LLVM_ABI raw_ostream & dbgs()

dbgs() - This returns a reference to a raw_ostream for debugging messages.

LLVM_ABI void initializeLiveRangeShrinkPass(PassRegistry &)

FunctionAddr VTableAddr Next