annotate llvm/lib/Target/Hexagon/HexagonExpandCondsets.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents c4bab56944e8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- HexagonExpandCondsets.cpp ------------------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 // Replace mux instructions with the corresponding legal instructions.
anatofuz
parents:
diff changeset
10 // It is meant to work post-SSA, but still on virtual registers. It was
anatofuz
parents:
diff changeset
11 // originally placed between register coalescing and machine instruction
anatofuz
parents:
diff changeset
12 // scheduler.
anatofuz
parents:
diff changeset
13 // In this place in the optimization sequence, live interval analysis had
anatofuz
parents:
diff changeset
14 // been performed, and the live intervals should be preserved. A large part
anatofuz
parents:
diff changeset
15 // of the code deals with preserving the liveness information.
anatofuz
parents:
diff changeset
16 //
anatofuz
parents:
diff changeset
17 // Liveness tracking aside, the main functionality of this pass is divided
anatofuz
parents:
diff changeset
18 // into two steps. The first step is to replace an instruction
anatofuz
parents:
diff changeset
19 // %0 = C2_mux %1, %2, %3
anatofuz
parents:
diff changeset
20 // with a pair of conditional transfers
anatofuz
parents:
diff changeset
21 // %0 = A2_tfrt %1, %2
anatofuz
parents:
diff changeset
22 // %0 = A2_tfrf %1, %3
anatofuz
parents:
diff changeset
23 // It is the intention that the execution of this pass could be terminated
anatofuz
parents:
diff changeset
24 // after this step, and the code generated would be functionally correct.
anatofuz
parents:
diff changeset
25 //
anatofuz
parents:
diff changeset
26 // If the uses of the source values %1 and %2 are kills, and their
anatofuz
parents:
diff changeset
27 // definitions are predicable, then in the second step, the conditional
anatofuz
parents:
diff changeset
28 // transfers will then be rewritten as predicated instructions. E.g.
anatofuz
parents:
diff changeset
29 // %0 = A2_or %1, %2
anatofuz
parents:
diff changeset
30 // %3 = A2_tfrt %99, killed %0
anatofuz
parents:
diff changeset
31 // will be rewritten as
anatofuz
parents:
diff changeset
32 // %3 = A2_port %99, %1, %2
anatofuz
parents:
diff changeset
33 //
anatofuz
parents:
diff changeset
34 // This replacement has two variants: "up" and "down". Consider this case:
anatofuz
parents:
diff changeset
35 // %0 = A2_or %1, %2
anatofuz
parents:
diff changeset
36 // ... [intervening instructions] ...
anatofuz
parents:
diff changeset
37 // %3 = A2_tfrt %99, killed %0
anatofuz
parents:
diff changeset
38 // variant "up":
anatofuz
parents:
diff changeset
39 // %3 = A2_port %99, %1, %2
anatofuz
parents:
diff changeset
40 // ... [intervening instructions, %0->vreg3] ...
anatofuz
parents:
diff changeset
41 // [deleted]
anatofuz
parents:
diff changeset
42 // variant "down":
anatofuz
parents:
diff changeset
43 // [deleted]
anatofuz
parents:
diff changeset
44 // ... [intervening instructions] ...
anatofuz
parents:
diff changeset
45 // %3 = A2_port %99, %1, %2
anatofuz
parents:
diff changeset
46 //
anatofuz
parents:
diff changeset
47 // Both, one or none of these variants may be valid, and checks are made
anatofuz
parents:
diff changeset
48 // to rule out inapplicable variants.
anatofuz
parents:
diff changeset
49 //
anatofuz
parents:
diff changeset
50 // As an additional optimization, before either of the two steps above is
anatofuz
parents:
diff changeset
51 // executed, the pass attempts to coalesce the target register with one of
anatofuz
parents:
diff changeset
52 // the source registers, e.g. given an instruction
anatofuz
parents:
diff changeset
53 // %3 = C2_mux %0, %1, %2
anatofuz
parents:
diff changeset
54 // %3 will be coalesced with either %1 or %2. If this succeeds,
anatofuz
parents:
diff changeset
55 // the instruction would then be (for example)
anatofuz
parents:
diff changeset
56 // %3 = C2_mux %0, %3, %2
anatofuz
parents:
diff changeset
57 // and, under certain circumstances, this could result in only one predicated
anatofuz
parents:
diff changeset
58 // instruction:
anatofuz
parents:
diff changeset
59 // %3 = A2_tfrf %0, %2
anatofuz
parents:
diff changeset
60 //
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 // Splitting a definition of a register into two predicated transfers
anatofuz
parents:
diff changeset
63 // creates a complication in liveness tracking. Live interval computation
anatofuz
parents:
diff changeset
64 // will see both instructions as actual definitions, and will mark the
anatofuz
parents:
diff changeset
65 // first one as dead. The definition is not actually dead, and this
anatofuz
parents:
diff changeset
66 // situation will need to be fixed. For example:
anatofuz
parents:
diff changeset
67 // dead %1 = A2_tfrt ... ; marked as dead
anatofuz
parents:
diff changeset
68 // %1 = A2_tfrf ...
anatofuz
parents:
diff changeset
69 //
anatofuz
parents:
diff changeset
70 // Since any of the individual predicated transfers may end up getting
anatofuz
parents:
diff changeset
71 // removed (in case it is an identity copy), some pre-existing def may
anatofuz
parents:
diff changeset
72 // be marked as dead after live interval recomputation:
anatofuz
parents:
diff changeset
73 // dead %1 = ... ; marked as dead
anatofuz
parents:
diff changeset
74 // ...
anatofuz
parents:
diff changeset
75 // %1 = A2_tfrf ... ; if A2_tfrt is removed
anatofuz
parents:
diff changeset
76 // This case happens if %1 was used as a source in A2_tfrt, which means
anatofuz
parents:
diff changeset
77 // that is it actually live at the A2_tfrf, and so the now dead definition
anatofuz
parents:
diff changeset
78 // of %1 will need to be updated to non-dead at some point.
anatofuz
parents:
diff changeset
79 //
anatofuz
parents:
diff changeset
80 // This issue could be remedied by adding implicit uses to the predicated
anatofuz
parents:
diff changeset
81 // transfers, but this will create a problem with subsequent predication,
anatofuz
parents:
diff changeset
82 // since the transfers will no longer be possible to reorder. To avoid
anatofuz
parents:
diff changeset
83 // that, the initial splitting will not add any implicit uses. These
anatofuz
parents:
diff changeset
84 // implicit uses will be added later, after predication. The extra price,
anatofuz
parents:
diff changeset
85 // however, is that finding the locations where the implicit uses need
anatofuz
parents:
diff changeset
86 // to be added, and updating the live ranges will be more involved.
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 #include "HexagonInstrInfo.h"
anatofuz
parents:
diff changeset
89 #include "HexagonRegisterInfo.h"
anatofuz
parents:
diff changeset
90 #include "llvm/ADT/DenseMap.h"
anatofuz
parents:
diff changeset
91 #include "llvm/ADT/SetVector.h"
anatofuz
parents:
diff changeset
92 #include "llvm/ADT/SmallVector.h"
anatofuz
parents:
diff changeset
93 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
94 #include "llvm/CodeGen/LiveInterval.h"
anatofuz
parents:
diff changeset
95 #include "llvm/CodeGen/LiveIntervals.h"
anatofuz
parents:
diff changeset
96 #include "llvm/CodeGen/MachineBasicBlock.h"
anatofuz
parents:
diff changeset
97 #include "llvm/CodeGen/MachineDominators.h"
anatofuz
parents:
diff changeset
98 #include "llvm/CodeGen/MachineFunction.h"
anatofuz
parents:
diff changeset
99 #include "llvm/CodeGen/MachineFunctionPass.h"
anatofuz
parents:
diff changeset
100 #include "llvm/CodeGen/MachineInstr.h"
anatofuz
parents:
diff changeset
101 #include "llvm/CodeGen/MachineInstrBuilder.h"
anatofuz
parents:
diff changeset
102 #include "llvm/CodeGen/MachineOperand.h"
anatofuz
parents:
diff changeset
103 #include "llvm/CodeGen/MachineRegisterInfo.h"
anatofuz
parents:
diff changeset
104 #include "llvm/CodeGen/SlotIndexes.h"
anatofuz
parents:
diff changeset
105 #include "llvm/CodeGen/TargetRegisterInfo.h"
anatofuz
parents:
diff changeset
106 #include "llvm/CodeGen/TargetSubtargetInfo.h"
anatofuz
parents:
diff changeset
107 #include "llvm/IR/DebugLoc.h"
anatofuz
parents:
diff changeset
108 #include "llvm/IR/Function.h"
anatofuz
parents:
diff changeset
109 #include "llvm/InitializePasses.h"
anatofuz
parents:
diff changeset
110 #include "llvm/MC/LaneBitmask.h"
anatofuz
parents:
diff changeset
111 #include "llvm/Pass.h"
anatofuz
parents:
diff changeset
112 #include "llvm/Support/CommandLine.h"
anatofuz
parents:
diff changeset
113 #include "llvm/Support/Debug.h"
anatofuz
parents:
diff changeset
114 #include "llvm/Support/ErrorHandling.h"
anatofuz
parents:
diff changeset
115 #include "llvm/Support/raw_ostream.h"
anatofuz
parents:
diff changeset
116 #include <cassert>
anatofuz
parents:
diff changeset
117 #include <iterator>
anatofuz
parents:
diff changeset
118 #include <set>
anatofuz
parents:
diff changeset
119 #include <utility>
anatofuz
parents:
diff changeset
120
anatofuz
parents:
diff changeset
121 #define DEBUG_TYPE "expand-condsets"
anatofuz
parents:
diff changeset
122
anatofuz
parents:
diff changeset
123 using namespace llvm;
anatofuz
parents:
diff changeset
124
anatofuz
parents:
diff changeset
125 static cl::opt<unsigned> OptTfrLimit("expand-condsets-tfr-limit",
anatofuz
parents:
diff changeset
126 cl::init(~0U), cl::Hidden, cl::desc("Max number of mux expansions"));
anatofuz
parents:
diff changeset
127 static cl::opt<unsigned> OptCoaLimit("expand-condsets-coa-limit",
anatofuz
parents:
diff changeset
128 cl::init(~0U), cl::Hidden, cl::desc("Max number of segment coalescings"));
anatofuz
parents:
diff changeset
129
anatofuz
parents:
diff changeset
130 namespace llvm {
anatofuz
parents:
diff changeset
131
anatofuz
parents:
diff changeset
132 void initializeHexagonExpandCondsetsPass(PassRegistry&);
anatofuz
parents:
diff changeset
133 FunctionPass *createHexagonExpandCondsets();
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 } // end namespace llvm
anatofuz
parents:
diff changeset
136
anatofuz
parents:
diff changeset
137 namespace {
anatofuz
parents:
diff changeset
138
anatofuz
parents:
diff changeset
139 class HexagonExpandCondsets : public MachineFunctionPass {
anatofuz
parents:
diff changeset
140 public:
anatofuz
parents:
diff changeset
141 static char ID;
anatofuz
parents:
diff changeset
142
anatofuz
parents:
diff changeset
143 HexagonExpandCondsets() : MachineFunctionPass(ID) {
anatofuz
parents:
diff changeset
144 if (OptCoaLimit.getPosition())
anatofuz
parents:
diff changeset
145 CoaLimitActive = true, CoaLimit = OptCoaLimit;
anatofuz
parents:
diff changeset
146 if (OptTfrLimit.getPosition())
anatofuz
parents:
diff changeset
147 TfrLimitActive = true, TfrLimit = OptTfrLimit;
anatofuz
parents:
diff changeset
148 initializeHexagonExpandCondsetsPass(*PassRegistry::getPassRegistry());
anatofuz
parents:
diff changeset
149 }
anatofuz
parents:
diff changeset
150
anatofuz
parents:
diff changeset
151 StringRef getPassName() const override { return "Hexagon Expand Condsets"; }
anatofuz
parents:
diff changeset
152
anatofuz
parents:
diff changeset
153 void getAnalysisUsage(AnalysisUsage &AU) const override {
anatofuz
parents:
diff changeset
154 AU.addRequired<LiveIntervals>();
anatofuz
parents:
diff changeset
155 AU.addPreserved<LiveIntervals>();
anatofuz
parents:
diff changeset
156 AU.addPreserved<SlotIndexes>();
anatofuz
parents:
diff changeset
157 AU.addRequired<MachineDominatorTree>();
anatofuz
parents:
diff changeset
158 AU.addPreserved<MachineDominatorTree>();
anatofuz
parents:
diff changeset
159 MachineFunctionPass::getAnalysisUsage(AU);
anatofuz
parents:
diff changeset
160 }
anatofuz
parents:
diff changeset
161
anatofuz
parents:
diff changeset
162 bool runOnMachineFunction(MachineFunction &MF) override;
anatofuz
parents:
diff changeset
163
anatofuz
parents:
diff changeset
164 private:
anatofuz
parents:
diff changeset
165 const HexagonInstrInfo *HII = nullptr;
anatofuz
parents:
diff changeset
166 const TargetRegisterInfo *TRI = nullptr;
anatofuz
parents:
diff changeset
167 MachineDominatorTree *MDT;
anatofuz
parents:
diff changeset
168 MachineRegisterInfo *MRI = nullptr;
anatofuz
parents:
diff changeset
169 LiveIntervals *LIS = nullptr;
anatofuz
parents:
diff changeset
170 bool CoaLimitActive = false;
anatofuz
parents:
diff changeset
171 bool TfrLimitActive = false;
anatofuz
parents:
diff changeset
172 unsigned CoaLimit;
anatofuz
parents:
diff changeset
173 unsigned TfrLimit;
anatofuz
parents:
diff changeset
174 unsigned CoaCounter = 0;
anatofuz
parents:
diff changeset
175 unsigned TfrCounter = 0;
anatofuz
parents:
diff changeset
176
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
177 // FIXME: Consolidate duplicate definitions of RegisterRef
150
anatofuz
parents:
diff changeset
178 struct RegisterRef {
anatofuz
parents:
diff changeset
179 RegisterRef(const MachineOperand &Op) : Reg(Op.getReg()),
anatofuz
parents:
diff changeset
180 Sub(Op.getSubReg()) {}
anatofuz
parents:
diff changeset
181 RegisterRef(unsigned R = 0, unsigned S = 0) : Reg(R), Sub(S) {}
anatofuz
parents:
diff changeset
182
anatofuz
parents:
diff changeset
183 bool operator== (RegisterRef RR) const {
anatofuz
parents:
diff changeset
184 return Reg == RR.Reg && Sub == RR.Sub;
anatofuz
parents:
diff changeset
185 }
anatofuz
parents:
diff changeset
186 bool operator!= (RegisterRef RR) const { return !operator==(RR); }
anatofuz
parents:
diff changeset
187 bool operator< (RegisterRef RR) const {
anatofuz
parents:
diff changeset
188 return Reg < RR.Reg || (Reg == RR.Reg && Sub < RR.Sub);
anatofuz
parents:
diff changeset
189 }
anatofuz
parents:
diff changeset
190
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
191 Register Reg;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
192 unsigned Sub;
150
anatofuz
parents:
diff changeset
193 };
anatofuz
parents:
diff changeset
194
anatofuz
parents:
diff changeset
195 using ReferenceMap = DenseMap<unsigned, unsigned>;
anatofuz
parents:
diff changeset
196 enum { Sub_Low = 0x1, Sub_High = 0x2, Sub_None = (Sub_Low | Sub_High) };
anatofuz
parents:
diff changeset
197 enum { Exec_Then = 0x10, Exec_Else = 0x20 };
anatofuz
parents:
diff changeset
198
anatofuz
parents:
diff changeset
199 unsigned getMaskForSub(unsigned Sub);
anatofuz
parents:
diff changeset
200 bool isCondset(const MachineInstr &MI);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
201 LaneBitmask getLaneMask(Register Reg, unsigned Sub);
150
anatofuz
parents:
diff changeset
202
anatofuz
parents:
diff changeset
203 void addRefToMap(RegisterRef RR, ReferenceMap &Map, unsigned Exec);
anatofuz
parents:
diff changeset
204 bool isRefInMap(RegisterRef, ReferenceMap &Map, unsigned Exec);
anatofuz
parents:
diff changeset
205
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
206 void updateDeadsInRange(Register Reg, LaneBitmask LM, LiveRange &Range);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
207 void updateKillFlags(Register Reg);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
208 void updateDeadFlags(Register Reg);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
209 void recalculateLiveInterval(Register Reg);
150
anatofuz
parents:
diff changeset
210 void removeInstr(MachineInstr &MI);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
211 void updateLiveness(const std::set<Register> &RegSet, bool Recalc,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
212 bool UpdateKills, bool UpdateDeads);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
213 void distributeLiveIntervals(const std::set<Register> &Regs);
150
anatofuz
parents:
diff changeset
214
anatofuz
parents:
diff changeset
215 unsigned getCondTfrOpcode(const MachineOperand &SO, bool Cond);
anatofuz
parents:
diff changeset
216 MachineInstr *genCondTfrFor(MachineOperand &SrcOp,
anatofuz
parents:
diff changeset
217 MachineBasicBlock::iterator At, unsigned DstR,
anatofuz
parents:
diff changeset
218 unsigned DstSR, const MachineOperand &PredOp, bool PredSense,
anatofuz
parents:
diff changeset
219 bool ReadUndef, bool ImpUse);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
220 bool split(MachineInstr &MI, std::set<Register> &UpdRegs);
150
anatofuz
parents:
diff changeset
221
anatofuz
parents:
diff changeset
222 bool isPredicable(MachineInstr *MI);
anatofuz
parents:
diff changeset
223 MachineInstr *getReachingDefForPred(RegisterRef RD,
anatofuz
parents:
diff changeset
224 MachineBasicBlock::iterator UseIt, unsigned PredR, bool Cond);
anatofuz
parents:
diff changeset
225 bool canMoveOver(MachineInstr &MI, ReferenceMap &Defs, ReferenceMap &Uses);
anatofuz
parents:
diff changeset
226 bool canMoveMemTo(MachineInstr &MI, MachineInstr &ToI, bool IsDown);
anatofuz
parents:
diff changeset
227 void predicateAt(const MachineOperand &DefOp, MachineInstr &MI,
anatofuz
parents:
diff changeset
228 MachineBasicBlock::iterator Where,
anatofuz
parents:
diff changeset
229 const MachineOperand &PredOp, bool Cond,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
230 std::set<Register> &UpdRegs);
150
anatofuz
parents:
diff changeset
231 void renameInRange(RegisterRef RO, RegisterRef RN, unsigned PredR,
anatofuz
parents:
diff changeset
232 bool Cond, MachineBasicBlock::iterator First,
anatofuz
parents:
diff changeset
233 MachineBasicBlock::iterator Last);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
234 bool predicate(MachineInstr &TfrI, bool Cond, std::set<Register> &UpdRegs);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
235 bool predicateInBlock(MachineBasicBlock &B, std::set<Register> &UpdRegs);
150
anatofuz
parents:
diff changeset
236
anatofuz
parents:
diff changeset
237 bool isIntReg(RegisterRef RR, unsigned &BW);
anatofuz
parents:
diff changeset
238 bool isIntraBlocks(LiveInterval &LI);
anatofuz
parents:
diff changeset
239 bool coalesceRegisters(RegisterRef R1, RegisterRef R2);
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
240 bool coalesceSegments(const SmallVectorImpl<MachineInstr *> &Condsets,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
241 std::set<Register> &UpdRegs);
150
anatofuz
parents:
diff changeset
242 };
anatofuz
parents:
diff changeset
243
anatofuz
parents:
diff changeset
244 } // end anonymous namespace
anatofuz
parents:
diff changeset
245
anatofuz
parents:
diff changeset
246 char HexagonExpandCondsets::ID = 0;
anatofuz
parents:
diff changeset
247
anatofuz
parents:
diff changeset
248 namespace llvm {
anatofuz
parents:
diff changeset
249
anatofuz
parents:
diff changeset
250 char &HexagonExpandCondsetsID = HexagonExpandCondsets::ID;
anatofuz
parents:
diff changeset
251
anatofuz
parents:
diff changeset
252 } // end namespace llvm
anatofuz
parents:
diff changeset
253
anatofuz
parents:
diff changeset
254 INITIALIZE_PASS_BEGIN(HexagonExpandCondsets, "expand-condsets",
anatofuz
parents:
diff changeset
255 "Hexagon Expand Condsets", false, false)
anatofuz
parents:
diff changeset
256 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
anatofuz
parents:
diff changeset
257 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
anatofuz
parents:
diff changeset
258 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
anatofuz
parents:
diff changeset
259 INITIALIZE_PASS_END(HexagonExpandCondsets, "expand-condsets",
anatofuz
parents:
diff changeset
260 "Hexagon Expand Condsets", false, false)
anatofuz
parents:
diff changeset
261
anatofuz
parents:
diff changeset
262 unsigned HexagonExpandCondsets::getMaskForSub(unsigned Sub) {
anatofuz
parents:
diff changeset
263 switch (Sub) {
anatofuz
parents:
diff changeset
264 case Hexagon::isub_lo:
anatofuz
parents:
diff changeset
265 case Hexagon::vsub_lo:
anatofuz
parents:
diff changeset
266 return Sub_Low;
anatofuz
parents:
diff changeset
267 case Hexagon::isub_hi:
anatofuz
parents:
diff changeset
268 case Hexagon::vsub_hi:
anatofuz
parents:
diff changeset
269 return Sub_High;
anatofuz
parents:
diff changeset
270 case Hexagon::NoSubRegister:
anatofuz
parents:
diff changeset
271 return Sub_None;
anatofuz
parents:
diff changeset
272 }
anatofuz
parents:
diff changeset
273 llvm_unreachable("Invalid subregister");
anatofuz
parents:
diff changeset
274 }
anatofuz
parents:
diff changeset
275
anatofuz
parents:
diff changeset
276 bool HexagonExpandCondsets::isCondset(const MachineInstr &MI) {
anatofuz
parents:
diff changeset
277 unsigned Opc = MI.getOpcode();
anatofuz
parents:
diff changeset
278 switch (Opc) {
anatofuz
parents:
diff changeset
279 case Hexagon::C2_mux:
anatofuz
parents:
diff changeset
280 case Hexagon::C2_muxii:
anatofuz
parents:
diff changeset
281 case Hexagon::C2_muxir:
anatofuz
parents:
diff changeset
282 case Hexagon::C2_muxri:
anatofuz
parents:
diff changeset
283 case Hexagon::PS_pselect:
anatofuz
parents:
diff changeset
284 return true;
anatofuz
parents:
diff changeset
285 break;
anatofuz
parents:
diff changeset
286 }
anatofuz
parents:
diff changeset
287 return false;
anatofuz
parents:
diff changeset
288 }
anatofuz
parents:
diff changeset
289
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
290 LaneBitmask HexagonExpandCondsets::getLaneMask(Register Reg, unsigned Sub) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
291 assert(Reg.isVirtual());
150
anatofuz
parents:
diff changeset
292 return Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
anatofuz
parents:
diff changeset
293 : MRI->getMaxLaneMaskForVReg(Reg);
anatofuz
parents:
diff changeset
294 }
anatofuz
parents:
diff changeset
295
anatofuz
parents:
diff changeset
296 void HexagonExpandCondsets::addRefToMap(RegisterRef RR, ReferenceMap &Map,
anatofuz
parents:
diff changeset
297 unsigned Exec) {
anatofuz
parents:
diff changeset
298 unsigned Mask = getMaskForSub(RR.Sub) | Exec;
anatofuz
parents:
diff changeset
299 ReferenceMap::iterator F = Map.find(RR.Reg);
anatofuz
parents:
diff changeset
300 if (F == Map.end())
anatofuz
parents:
diff changeset
301 Map.insert(std::make_pair(RR.Reg, Mask));
anatofuz
parents:
diff changeset
302 else
anatofuz
parents:
diff changeset
303 F->second |= Mask;
anatofuz
parents:
diff changeset
304 }
anatofuz
parents:
diff changeset
305
anatofuz
parents:
diff changeset
306 bool HexagonExpandCondsets::isRefInMap(RegisterRef RR, ReferenceMap &Map,
anatofuz
parents:
diff changeset
307 unsigned Exec) {
anatofuz
parents:
diff changeset
308 ReferenceMap::iterator F = Map.find(RR.Reg);
anatofuz
parents:
diff changeset
309 if (F == Map.end())
anatofuz
parents:
diff changeset
310 return false;
anatofuz
parents:
diff changeset
311 unsigned Mask = getMaskForSub(RR.Sub) | Exec;
anatofuz
parents:
diff changeset
312 if (Mask & F->second)
anatofuz
parents:
diff changeset
313 return true;
anatofuz
parents:
diff changeset
314 return false;
anatofuz
parents:
diff changeset
315 }
anatofuz
parents:
diff changeset
316
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
317 void HexagonExpandCondsets::updateKillFlags(Register Reg) {
150
anatofuz
parents:
diff changeset
318 auto KillAt = [this,Reg] (SlotIndex K, LaneBitmask LM) -> void {
anatofuz
parents:
diff changeset
319 // Set the <kill> flag on a use of Reg whose lane mask is contained in LM.
anatofuz
parents:
diff changeset
320 MachineInstr *MI = LIS->getInstructionFromIndex(K);
anatofuz
parents:
diff changeset
321 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
anatofuz
parents:
diff changeset
322 MachineOperand &Op = MI->getOperand(i);
anatofuz
parents:
diff changeset
323 if (!Op.isReg() || !Op.isUse() || Op.getReg() != Reg ||
anatofuz
parents:
diff changeset
324 MI->isRegTiedToDefOperand(i))
anatofuz
parents:
diff changeset
325 continue;
anatofuz
parents:
diff changeset
326 LaneBitmask SLM = getLaneMask(Reg, Op.getSubReg());
anatofuz
parents:
diff changeset
327 if ((SLM & LM) == SLM) {
anatofuz
parents:
diff changeset
328 // Only set the kill flag on the first encountered use of Reg in this
anatofuz
parents:
diff changeset
329 // instruction.
anatofuz
parents:
diff changeset
330 Op.setIsKill(true);
anatofuz
parents:
diff changeset
331 break;
anatofuz
parents:
diff changeset
332 }
anatofuz
parents:
diff changeset
333 }
anatofuz
parents:
diff changeset
334 };
anatofuz
parents:
diff changeset
335
anatofuz
parents:
diff changeset
336 LiveInterval &LI = LIS->getInterval(Reg);
anatofuz
parents:
diff changeset
337 for (auto I = LI.begin(), E = LI.end(); I != E; ++I) {
anatofuz
parents:
diff changeset
338 if (!I->end.isRegister())
anatofuz
parents:
diff changeset
339 continue;
anatofuz
parents:
diff changeset
340 // Do not mark the end of the segment as <kill>, if the next segment
anatofuz
parents:
diff changeset
341 // starts with a predicated instruction.
anatofuz
parents:
diff changeset
342 auto NextI = std::next(I);
anatofuz
parents:
diff changeset
343 if (NextI != E && NextI->start.isRegister()) {
anatofuz
parents:
diff changeset
344 MachineInstr *DefI = LIS->getInstructionFromIndex(NextI->start);
anatofuz
parents:
diff changeset
345 if (HII->isPredicated(*DefI))
anatofuz
parents:
diff changeset
346 continue;
anatofuz
parents:
diff changeset
347 }
anatofuz
parents:
diff changeset
348 bool WholeReg = true;
anatofuz
parents:
diff changeset
349 if (LI.hasSubRanges()) {
anatofuz
parents:
diff changeset
350 auto EndsAtI = [I] (LiveInterval::SubRange &S) -> bool {
anatofuz
parents:
diff changeset
351 LiveRange::iterator F = S.find(I->end);
anatofuz
parents:
diff changeset
352 return F != S.end() && I->end == F->end;
anatofuz
parents:
diff changeset
353 };
anatofuz
parents:
diff changeset
354 // Check if all subranges end at I->end. If so, make sure to kill
anatofuz
parents:
diff changeset
355 // the whole register.
anatofuz
parents:
diff changeset
356 for (LiveInterval::SubRange &S : LI.subranges()) {
anatofuz
parents:
diff changeset
357 if (EndsAtI(S))
anatofuz
parents:
diff changeset
358 KillAt(I->end, S.LaneMask);
anatofuz
parents:
diff changeset
359 else
anatofuz
parents:
diff changeset
360 WholeReg = false;
anatofuz
parents:
diff changeset
361 }
anatofuz
parents:
diff changeset
362 }
anatofuz
parents:
diff changeset
363 if (WholeReg)
anatofuz
parents:
diff changeset
364 KillAt(I->end, MRI->getMaxLaneMaskForVReg(Reg));
anatofuz
parents:
diff changeset
365 }
anatofuz
parents:
diff changeset
366 }
anatofuz
parents:
diff changeset
367
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
368 void HexagonExpandCondsets::updateDeadsInRange(Register Reg, LaneBitmask LM,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
369 LiveRange &Range) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
370 assert(Reg.isVirtual());
150
anatofuz
parents:
diff changeset
371 if (Range.empty())
anatofuz
parents:
diff changeset
372 return;
anatofuz
parents:
diff changeset
373
anatofuz
parents:
diff changeset
374 // Return two booleans: { def-modifes-reg, def-covers-reg }.
anatofuz
parents:
diff changeset
375 auto IsRegDef = [this,Reg,LM] (MachineOperand &Op) -> std::pair<bool,bool> {
anatofuz
parents:
diff changeset
376 if (!Op.isReg() || !Op.isDef())
anatofuz
parents:
diff changeset
377 return { false, false };
anatofuz
parents:
diff changeset
378 Register DR = Op.getReg(), DSR = Op.getSubReg();
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
379 if (!DR.isVirtual() || DR != Reg)
150
anatofuz
parents:
diff changeset
380 return { false, false };
anatofuz
parents:
diff changeset
381 LaneBitmask SLM = getLaneMask(DR, DSR);
anatofuz
parents:
diff changeset
382 LaneBitmask A = SLM & LM;
anatofuz
parents:
diff changeset
383 return { A.any(), A == SLM };
anatofuz
parents:
diff changeset
384 };
anatofuz
parents:
diff changeset
385
anatofuz
parents:
diff changeset
386 // The splitting step will create pairs of predicated definitions without
anatofuz
parents:
diff changeset
387 // any implicit uses (since implicit uses would interfere with predication).
anatofuz
parents:
diff changeset
388 // This can cause the reaching defs to become dead after live range
anatofuz
parents:
diff changeset
389 // recomputation, even though they are not really dead.
anatofuz
parents:
diff changeset
390 // We need to identify predicated defs that need implicit uses, and
anatofuz
parents:
diff changeset
391 // dead defs that are not really dead, and correct both problems.
anatofuz
parents:
diff changeset
392
anatofuz
parents:
diff changeset
393 auto Dominate = [this] (SetVector<MachineBasicBlock*> &Defs,
anatofuz
parents:
diff changeset
394 MachineBasicBlock *Dest) -> bool {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
395 for (MachineBasicBlock *D : Defs) {
150
anatofuz
parents:
diff changeset
396 if (D != Dest && MDT->dominates(D, Dest))
anatofuz
parents:
diff changeset
397 return true;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
398 }
150
anatofuz
parents:
diff changeset
399 MachineBasicBlock *Entry = &Dest->getParent()->front();
anatofuz
parents:
diff changeset
400 SetVector<MachineBasicBlock*> Work(Dest->pred_begin(), Dest->pred_end());
anatofuz
parents:
diff changeset
401 for (unsigned i = 0; i < Work.size(); ++i) {
anatofuz
parents:
diff changeset
402 MachineBasicBlock *B = Work[i];
anatofuz
parents:
diff changeset
403 if (Defs.count(B))
anatofuz
parents:
diff changeset
404 continue;
anatofuz
parents:
diff changeset
405 if (B == Entry)
anatofuz
parents:
diff changeset
406 return false;
anatofuz
parents:
diff changeset
407 for (auto *P : B->predecessors())
anatofuz
parents:
diff changeset
408 Work.insert(P);
anatofuz
parents:
diff changeset
409 }
anatofuz
parents:
diff changeset
410 return true;
anatofuz
parents:
diff changeset
411 };
anatofuz
parents:
diff changeset
412
anatofuz
parents:
diff changeset
413 // First, try to extend live range within individual basic blocks. This
anatofuz
parents:
diff changeset
414 // will leave us only with dead defs that do not reach any predicated
anatofuz
parents:
diff changeset
415 // defs in the same block.
anatofuz
parents:
diff changeset
416 SetVector<MachineBasicBlock*> Defs;
anatofuz
parents:
diff changeset
417 SmallVector<SlotIndex,4> PredDefs;
anatofuz
parents:
diff changeset
418 for (auto &Seg : Range) {
anatofuz
parents:
diff changeset
419 if (!Seg.start.isRegister())
anatofuz
parents:
diff changeset
420 continue;
anatofuz
parents:
diff changeset
421 MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start);
anatofuz
parents:
diff changeset
422 Defs.insert(DefI->getParent());
anatofuz
parents:
diff changeset
423 if (HII->isPredicated(*DefI))
anatofuz
parents:
diff changeset
424 PredDefs.push_back(Seg.start);
anatofuz
parents:
diff changeset
425 }
anatofuz
parents:
diff changeset
426
anatofuz
parents:
diff changeset
427 SmallVector<SlotIndex,8> Undefs;
anatofuz
parents:
diff changeset
428 LiveInterval &LI = LIS->getInterval(Reg);
anatofuz
parents:
diff changeset
429 LI.computeSubRangeUndefs(Undefs, LM, *MRI, *LIS->getSlotIndexes());
anatofuz
parents:
diff changeset
430
anatofuz
parents:
diff changeset
431 for (auto &SI : PredDefs) {
anatofuz
parents:
diff changeset
432 MachineBasicBlock *BB = LIS->getMBBFromIndex(SI);
anatofuz
parents:
diff changeset
433 auto P = Range.extendInBlock(Undefs, LIS->getMBBStartIdx(BB), SI);
anatofuz
parents:
diff changeset
434 if (P.first != nullptr || P.second)
anatofuz
parents:
diff changeset
435 SI = SlotIndex();
anatofuz
parents:
diff changeset
436 }
anatofuz
parents:
diff changeset
437
anatofuz
parents:
diff changeset
438 // Calculate reachability for those predicated defs that were not handled
anatofuz
parents:
diff changeset
439 // by the in-block extension.
anatofuz
parents:
diff changeset
440 SmallVector<SlotIndex,4> ExtTo;
anatofuz
parents:
diff changeset
441 for (auto &SI : PredDefs) {
anatofuz
parents:
diff changeset
442 if (!SI.isValid())
anatofuz
parents:
diff changeset
443 continue;
anatofuz
parents:
diff changeset
444 MachineBasicBlock *BB = LIS->getMBBFromIndex(SI);
anatofuz
parents:
diff changeset
445 if (BB->pred_empty())
anatofuz
parents:
diff changeset
446 continue;
anatofuz
parents:
diff changeset
447 // If the defs from this range reach SI via all predecessors, it is live.
anatofuz
parents:
diff changeset
448 // It can happen that SI is reached by the defs through some paths, but
anatofuz
parents:
diff changeset
449 // not all. In the IR coming into this optimization, SI would not be
anatofuz
parents:
diff changeset
450 // considered live, since the defs would then not jointly dominate SI.
anatofuz
parents:
diff changeset
451 // That means that SI is an overwriting def, and no implicit use is
anatofuz
parents:
diff changeset
452 // needed at this point. Do not add SI to the extension points, since
anatofuz
parents:
diff changeset
453 // extendToIndices will abort if there is no joint dominance.
anatofuz
parents:
diff changeset
454 // If the abort was avoided by adding extra undefs added to Undefs,
anatofuz
parents:
diff changeset
455 // extendToIndices could actually indicate that SI is live, contrary
anatofuz
parents:
diff changeset
456 // to the original IR.
anatofuz
parents:
diff changeset
457 if (Dominate(Defs, BB))
anatofuz
parents:
diff changeset
458 ExtTo.push_back(SI);
anatofuz
parents:
diff changeset
459 }
anatofuz
parents:
diff changeset
460
anatofuz
parents:
diff changeset
461 if (!ExtTo.empty())
anatofuz
parents:
diff changeset
462 LIS->extendToIndices(Range, ExtTo, Undefs);
anatofuz
parents:
diff changeset
463
anatofuz
parents:
diff changeset
464 // Remove <dead> flags from all defs that are not dead after live range
anatofuz
parents:
diff changeset
465 // extension, and collect all def operands. They will be used to generate
anatofuz
parents:
diff changeset
466 // the necessary implicit uses.
anatofuz
parents:
diff changeset
467 // At the same time, add <dead> flag to all defs that are actually dead.
anatofuz
parents:
diff changeset
468 // This can happen, for example, when a mux with identical inputs is
anatofuz
parents:
diff changeset
469 // replaced with a COPY: the use of the predicate register disappears and
anatofuz
parents:
diff changeset
470 // the dead can become dead.
anatofuz
parents:
diff changeset
471 std::set<RegisterRef> DefRegs;
anatofuz
parents:
diff changeset
472 for (auto &Seg : Range) {
anatofuz
parents:
diff changeset
473 if (!Seg.start.isRegister())
anatofuz
parents:
diff changeset
474 continue;
anatofuz
parents:
diff changeset
475 MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start);
anatofuz
parents:
diff changeset
476 for (auto &Op : DefI->operands()) {
anatofuz
parents:
diff changeset
477 auto P = IsRegDef(Op);
anatofuz
parents:
diff changeset
478 if (P.second && Seg.end.isDead()) {
anatofuz
parents:
diff changeset
479 Op.setIsDead(true);
anatofuz
parents:
diff changeset
480 } else if (P.first) {
anatofuz
parents:
diff changeset
481 DefRegs.insert(Op);
anatofuz
parents:
diff changeset
482 Op.setIsDead(false);
anatofuz
parents:
diff changeset
483 }
anatofuz
parents:
diff changeset
484 }
anatofuz
parents:
diff changeset
485 }
anatofuz
parents:
diff changeset
486
anatofuz
parents:
diff changeset
487 // Now, add implicit uses to each predicated def that is reached
anatofuz
parents:
diff changeset
488 // by other defs.
anatofuz
parents:
diff changeset
489 for (auto &Seg : Range) {
anatofuz
parents:
diff changeset
490 if (!Seg.start.isRegister() || !Range.liveAt(Seg.start.getPrevSlot()))
anatofuz
parents:
diff changeset
491 continue;
anatofuz
parents:
diff changeset
492 MachineInstr *DefI = LIS->getInstructionFromIndex(Seg.start);
anatofuz
parents:
diff changeset
493 if (!HII->isPredicated(*DefI))
anatofuz
parents:
diff changeset
494 continue;
anatofuz
parents:
diff changeset
495 // Construct the set of all necessary implicit uses, based on the def
anatofuz
parents:
diff changeset
496 // operands in the instruction. We need to tie the implicit uses to
anatofuz
parents:
diff changeset
497 // the corresponding defs.
anatofuz
parents:
diff changeset
498 std::map<RegisterRef,unsigned> ImpUses;
anatofuz
parents:
diff changeset
499 for (unsigned i = 0, e = DefI->getNumOperands(); i != e; ++i) {
anatofuz
parents:
diff changeset
500 MachineOperand &Op = DefI->getOperand(i);
anatofuz
parents:
diff changeset
501 if (!Op.isReg() || !DefRegs.count(Op))
anatofuz
parents:
diff changeset
502 continue;
anatofuz
parents:
diff changeset
503 if (Op.isDef()) {
anatofuz
parents:
diff changeset
504 // Tied defs will always have corresponding uses, so no extra
anatofuz
parents:
diff changeset
505 // implicit uses are needed.
anatofuz
parents:
diff changeset
506 if (!Op.isTied())
anatofuz
parents:
diff changeset
507 ImpUses.insert({Op, i});
anatofuz
parents:
diff changeset
508 } else {
anatofuz
parents:
diff changeset
509 // This function can be called for the same register with different
anatofuz
parents:
diff changeset
510 // lane masks. If the def in this instruction was for the whole
anatofuz
parents:
diff changeset
511 // register, we can get here more than once. Avoid adding multiple
anatofuz
parents:
diff changeset
512 // implicit uses (or adding an implicit use when an explicit one is
anatofuz
parents:
diff changeset
513 // present).
anatofuz
parents:
diff changeset
514 if (Op.isTied())
anatofuz
parents:
diff changeset
515 ImpUses.erase(Op);
anatofuz
parents:
diff changeset
516 }
anatofuz
parents:
diff changeset
517 }
anatofuz
parents:
diff changeset
518 if (ImpUses.empty())
anatofuz
parents:
diff changeset
519 continue;
anatofuz
parents:
diff changeset
520 MachineFunction &MF = *DefI->getParent()->getParent();
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
521 for (auto [R, DefIdx] : ImpUses) {
150
anatofuz
parents:
diff changeset
522 MachineInstrBuilder(MF, DefI).addReg(R.Reg, RegState::Implicit, R.Sub);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
523 DefI->tieOperands(DefIdx, DefI->getNumOperands()-1);
150
anatofuz
parents:
diff changeset
524 }
anatofuz
parents:
diff changeset
525 }
anatofuz
parents:
diff changeset
526 }
anatofuz
parents:
diff changeset
527
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
528 void HexagonExpandCondsets::updateDeadFlags(Register Reg) {
150
anatofuz
parents:
diff changeset
529 LiveInterval &LI = LIS->getInterval(Reg);
anatofuz
parents:
diff changeset
530 if (LI.hasSubRanges()) {
anatofuz
parents:
diff changeset
531 for (LiveInterval::SubRange &S : LI.subranges()) {
anatofuz
parents:
diff changeset
532 updateDeadsInRange(Reg, S.LaneMask, S);
anatofuz
parents:
diff changeset
533 LIS->shrinkToUses(S, Reg);
anatofuz
parents:
diff changeset
534 }
anatofuz
parents:
diff changeset
535 LI.clear();
anatofuz
parents:
diff changeset
536 LIS->constructMainRangeFromSubranges(LI);
anatofuz
parents:
diff changeset
537 } else {
anatofuz
parents:
diff changeset
538 updateDeadsInRange(Reg, MRI->getMaxLaneMaskForVReg(Reg), LI);
anatofuz
parents:
diff changeset
539 }
anatofuz
parents:
diff changeset
540 }
anatofuz
parents:
diff changeset
541
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
542 void HexagonExpandCondsets::recalculateLiveInterval(Register Reg) {
150
anatofuz
parents:
diff changeset
543 LIS->removeInterval(Reg);
anatofuz
parents:
diff changeset
544 LIS->createAndComputeVirtRegInterval(Reg);
anatofuz
parents:
diff changeset
545 }
anatofuz
parents:
diff changeset
546
anatofuz
parents:
diff changeset
547 void HexagonExpandCondsets::removeInstr(MachineInstr &MI) {
anatofuz
parents:
diff changeset
548 LIS->RemoveMachineInstrFromMaps(MI);
anatofuz
parents:
diff changeset
549 MI.eraseFromParent();
anatofuz
parents:
diff changeset
550 }
anatofuz
parents:
diff changeset
551
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
552 void HexagonExpandCondsets::updateLiveness(const std::set<Register> &RegSet,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
553 bool Recalc, bool UpdateKills,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
554 bool UpdateDeads) {
150
anatofuz
parents:
diff changeset
555 UpdateKills |= UpdateDeads;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
556 for (Register R : RegSet) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
557 if (!R.isVirtual()) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
558 assert(R.isPhysical());
150
anatofuz
parents:
diff changeset
559 // There shouldn't be any physical registers as operands, except
anatofuz
parents:
diff changeset
560 // possibly reserved registers.
anatofuz
parents:
diff changeset
561 assert(MRI->isReserved(R));
anatofuz
parents:
diff changeset
562 continue;
anatofuz
parents:
diff changeset
563 }
anatofuz
parents:
diff changeset
564 if (Recalc)
anatofuz
parents:
diff changeset
565 recalculateLiveInterval(R);
anatofuz
parents:
diff changeset
566 if (UpdateKills)
anatofuz
parents:
diff changeset
567 MRI->clearKillFlags(R);
anatofuz
parents:
diff changeset
568 if (UpdateDeads)
anatofuz
parents:
diff changeset
569 updateDeadFlags(R);
anatofuz
parents:
diff changeset
570 // Fixing <dead> flags may extend live ranges, so reset <kill> flags
anatofuz
parents:
diff changeset
571 // after that.
anatofuz
parents:
diff changeset
572 if (UpdateKills)
anatofuz
parents:
diff changeset
573 updateKillFlags(R);
anatofuz
parents:
diff changeset
574 LIS->getInterval(R).verify();
anatofuz
parents:
diff changeset
575 }
anatofuz
parents:
diff changeset
576 }
anatofuz
parents:
diff changeset
577
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
578 void HexagonExpandCondsets::distributeLiveIntervals(
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
579 const std::set<Register> &Regs) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
580 ConnectedVNInfoEqClasses EQC(*LIS);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
581 for (Register R : Regs) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
582 if (!R.isVirtual())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
583 continue;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
584 LiveInterval &LI = LIS->getInterval(R);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
585 unsigned NumComp = EQC.Classify(LI);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
586 if (NumComp == 1)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
587 continue;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
588
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
589 SmallVector<LiveInterval*> NewLIs;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
590 const TargetRegisterClass *RC = MRI->getRegClass(LI.reg());
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
591 for (unsigned I = 1; I < NumComp; ++I) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
592 Register NewR = MRI->createVirtualRegister(RC);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
593 NewLIs.push_back(&LIS->createEmptyInterval(NewR));
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
594 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
595 EQC.Distribute(LI, NewLIs.begin(), *MRI);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
596 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
597 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
598
150
anatofuz
parents:
diff changeset
599 /// Get the opcode for a conditional transfer of the value in SO (source
anatofuz
parents:
diff changeset
600 /// operand). The condition (true/false) is given in Cond.
anatofuz
parents:
diff changeset
601 unsigned HexagonExpandCondsets::getCondTfrOpcode(const MachineOperand &SO,
anatofuz
parents:
diff changeset
602 bool IfTrue) {
anatofuz
parents:
diff changeset
603 if (SO.isReg()) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
604 MCRegister PhysR;
150
anatofuz
parents:
diff changeset
605 RegisterRef RS = SO;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
606 if (RS.Reg.isVirtual()) {
150
anatofuz
parents:
diff changeset
607 const TargetRegisterClass *VC = MRI->getRegClass(RS.Reg);
anatofuz
parents:
diff changeset
608 assert(VC->begin() != VC->end() && "Empty register class");
anatofuz
parents:
diff changeset
609 PhysR = *VC->begin();
anatofuz
parents:
diff changeset
610 } else {
anatofuz
parents:
diff changeset
611 PhysR = RS.Reg;
anatofuz
parents:
diff changeset
612 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
613 MCRegister PhysS = (RS.Sub == 0) ? PhysR : TRI->getSubReg(PhysR, RS.Sub);
150
anatofuz
parents:
diff changeset
614 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(PhysS);
anatofuz
parents:
diff changeset
615 switch (TRI->getRegSizeInBits(*RC)) {
anatofuz
parents:
diff changeset
616 case 32:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
617 return IfTrue ? Hexagon::A2_tfrt : Hexagon::A2_tfrf;
150
anatofuz
parents:
diff changeset
618 case 64:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
619 return IfTrue ? Hexagon::A2_tfrpt : Hexagon::A2_tfrpf;
150
anatofuz
parents:
diff changeset
620 }
anatofuz
parents:
diff changeset
621 llvm_unreachable("Invalid register operand");
anatofuz
parents:
diff changeset
622 }
anatofuz
parents:
diff changeset
623 switch (SO.getType()) {
anatofuz
parents:
diff changeset
624 case MachineOperand::MO_Immediate:
anatofuz
parents:
diff changeset
625 case MachineOperand::MO_FPImmediate:
anatofuz
parents:
diff changeset
626 case MachineOperand::MO_ConstantPoolIndex:
anatofuz
parents:
diff changeset
627 case MachineOperand::MO_TargetIndex:
anatofuz
parents:
diff changeset
628 case MachineOperand::MO_JumpTableIndex:
anatofuz
parents:
diff changeset
629 case MachineOperand::MO_ExternalSymbol:
anatofuz
parents:
diff changeset
630 case MachineOperand::MO_GlobalAddress:
anatofuz
parents:
diff changeset
631 case MachineOperand::MO_BlockAddress:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
632 return IfTrue ? Hexagon::C2_cmoveit : Hexagon::C2_cmoveif;
150
anatofuz
parents:
diff changeset
633 default:
anatofuz
parents:
diff changeset
634 break;
anatofuz
parents:
diff changeset
635 }
anatofuz
parents:
diff changeset
636 llvm_unreachable("Unexpected source operand");
anatofuz
parents:
diff changeset
637 }
anatofuz
parents:
diff changeset
638
anatofuz
parents:
diff changeset
639 /// Generate a conditional transfer, copying the value SrcOp to the
anatofuz
parents:
diff changeset
640 /// destination register DstR:DstSR, and using the predicate register from
anatofuz
parents:
diff changeset
641 /// PredOp. The Cond argument specifies whether the predicate is to be
anatofuz
parents:
diff changeset
642 /// if(PredOp), or if(!PredOp).
anatofuz
parents:
diff changeset
643 MachineInstr *HexagonExpandCondsets::genCondTfrFor(MachineOperand &SrcOp,
anatofuz
parents:
diff changeset
644 MachineBasicBlock::iterator At,
anatofuz
parents:
diff changeset
645 unsigned DstR, unsigned DstSR, const MachineOperand &PredOp,
anatofuz
parents:
diff changeset
646 bool PredSense, bool ReadUndef, bool ImpUse) {
anatofuz
parents:
diff changeset
647 MachineInstr *MI = SrcOp.getParent();
anatofuz
parents:
diff changeset
648 MachineBasicBlock &B = *At->getParent();
anatofuz
parents:
diff changeset
649 const DebugLoc &DL = MI->getDebugLoc();
anatofuz
parents:
diff changeset
650
anatofuz
parents:
diff changeset
651 // Don't avoid identity copies here (i.e. if the source and the destination
anatofuz
parents:
diff changeset
652 // are the same registers). It is actually better to generate them here,
anatofuz
parents:
diff changeset
653 // since this would cause the copy to potentially be predicated in the next
anatofuz
parents:
diff changeset
654 // step. The predication will remove such a copy if it is unable to
anatofuz
parents:
diff changeset
655 /// predicate.
anatofuz
parents:
diff changeset
656
anatofuz
parents:
diff changeset
657 unsigned Opc = getCondTfrOpcode(SrcOp, PredSense);
anatofuz
parents:
diff changeset
658 unsigned DstState = RegState::Define | (ReadUndef ? RegState::Undef : 0);
anatofuz
parents:
diff changeset
659 unsigned PredState = getRegState(PredOp) & ~RegState::Kill;
anatofuz
parents:
diff changeset
660 MachineInstrBuilder MIB;
anatofuz
parents:
diff changeset
661
anatofuz
parents:
diff changeset
662 if (SrcOp.isReg()) {
anatofuz
parents:
diff changeset
663 unsigned SrcState = getRegState(SrcOp);
anatofuz
parents:
diff changeset
664 if (RegisterRef(SrcOp) == RegisterRef(DstR, DstSR))
anatofuz
parents:
diff changeset
665 SrcState &= ~RegState::Kill;
anatofuz
parents:
diff changeset
666 MIB = BuildMI(B, At, DL, HII->get(Opc))
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
667 .addReg(DstR, DstState, DstSR)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
668 .addReg(PredOp.getReg(), PredState, PredOp.getSubReg())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
669 .addReg(SrcOp.getReg(), SrcState, SrcOp.getSubReg());
150
anatofuz
parents:
diff changeset
670 } else {
anatofuz
parents:
diff changeset
671 MIB = BuildMI(B, At, DL, HII->get(Opc))
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
672 .addReg(DstR, DstState, DstSR)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
673 .addReg(PredOp.getReg(), PredState, PredOp.getSubReg())
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
674 .add(SrcOp);
150
anatofuz
parents:
diff changeset
675 }
anatofuz
parents:
diff changeset
676
anatofuz
parents:
diff changeset
677 LLVM_DEBUG(dbgs() << "created an initial copy: " << *MIB);
anatofuz
parents:
diff changeset
678 return &*MIB;
anatofuz
parents:
diff changeset
679 }
anatofuz
parents:
diff changeset
680
anatofuz
parents:
diff changeset
681 /// Replace a MUX instruction MI with a pair A2_tfrt/A2_tfrf. This function
anatofuz
parents:
diff changeset
682 /// performs all necessary changes to complete the replacement.
anatofuz
parents:
diff changeset
683 bool HexagonExpandCondsets::split(MachineInstr &MI,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
684 std::set<Register> &UpdRegs) {
150
anatofuz
parents:
diff changeset
685 if (TfrLimitActive) {
anatofuz
parents:
diff changeset
686 if (TfrCounter >= TfrLimit)
anatofuz
parents:
diff changeset
687 return false;
anatofuz
parents:
diff changeset
688 TfrCounter++;
anatofuz
parents:
diff changeset
689 }
anatofuz
parents:
diff changeset
690 LLVM_DEBUG(dbgs() << "\nsplitting " << printMBBReference(*MI.getParent())
anatofuz
parents:
diff changeset
691 << ": " << MI);
anatofuz
parents:
diff changeset
692 MachineOperand &MD = MI.getOperand(0); // Definition
anatofuz
parents:
diff changeset
693 MachineOperand &MP = MI.getOperand(1); // Predicate register
anatofuz
parents:
diff changeset
694 assert(MD.isDef());
anatofuz
parents:
diff changeset
695 Register DR = MD.getReg(), DSR = MD.getSubReg();
anatofuz
parents:
diff changeset
696 bool ReadUndef = MD.isUndef();
anatofuz
parents:
diff changeset
697 MachineBasicBlock::iterator At = MI;
anatofuz
parents:
diff changeset
698
anatofuz
parents:
diff changeset
699 auto updateRegs = [&UpdRegs] (const MachineInstr &MI) -> void {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
700 for (auto &Op : MI.operands()) {
150
anatofuz
parents:
diff changeset
701 if (Op.isReg())
anatofuz
parents:
diff changeset
702 UpdRegs.insert(Op.getReg());
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
703 }
150
anatofuz
parents:
diff changeset
704 };
anatofuz
parents:
diff changeset
705
anatofuz
parents:
diff changeset
706 // If this is a mux of the same register, just replace it with COPY.
anatofuz
parents:
diff changeset
707 // Ideally, this would happen earlier, so that register coalescing would
anatofuz
parents:
diff changeset
708 // see it.
anatofuz
parents:
diff changeset
709 MachineOperand &ST = MI.getOperand(2);
anatofuz
parents:
diff changeset
710 MachineOperand &SF = MI.getOperand(3);
anatofuz
parents:
diff changeset
711 if (ST.isReg() && SF.isReg()) {
anatofuz
parents:
diff changeset
712 RegisterRef RT(ST);
anatofuz
parents:
diff changeset
713 if (RT == RegisterRef(SF)) {
anatofuz
parents:
diff changeset
714 // Copy regs to update first.
anatofuz
parents:
diff changeset
715 updateRegs(MI);
anatofuz
parents:
diff changeset
716 MI.setDesc(HII->get(TargetOpcode::COPY));
anatofuz
parents:
diff changeset
717 unsigned S = getRegState(ST);
anatofuz
parents:
diff changeset
718 while (MI.getNumOperands() > 1)
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
719 MI.removeOperand(MI.getNumOperands()-1);
150
anatofuz
parents:
diff changeset
720 MachineFunction &MF = *MI.getParent()->getParent();
anatofuz
parents:
diff changeset
721 MachineInstrBuilder(MF, MI).addReg(RT.Reg, S, RT.Sub);
anatofuz
parents:
diff changeset
722 return true;
anatofuz
parents:
diff changeset
723 }
anatofuz
parents:
diff changeset
724 }
anatofuz
parents:
diff changeset
725
anatofuz
parents:
diff changeset
726 // First, create the two invididual conditional transfers, and add each
anatofuz
parents:
diff changeset
727 // of them to the live intervals information. Do that first and then remove
anatofuz
parents:
diff changeset
728 // the old instruction from live intervals.
anatofuz
parents:
diff changeset
729 MachineInstr *TfrT =
anatofuz
parents:
diff changeset
730 genCondTfrFor(ST, At, DR, DSR, MP, true, ReadUndef, false);
anatofuz
parents:
diff changeset
731 MachineInstr *TfrF =
anatofuz
parents:
diff changeset
732 genCondTfrFor(SF, At, DR, DSR, MP, false, ReadUndef, true);
anatofuz
parents:
diff changeset
733 LIS->InsertMachineInstrInMaps(*TfrT);
anatofuz
parents:
diff changeset
734 LIS->InsertMachineInstrInMaps(*TfrF);
anatofuz
parents:
diff changeset
735
anatofuz
parents:
diff changeset
736 // Will need to recalculate live intervals for all registers in MI.
anatofuz
parents:
diff changeset
737 updateRegs(MI);
anatofuz
parents:
diff changeset
738
anatofuz
parents:
diff changeset
739 removeInstr(MI);
anatofuz
parents:
diff changeset
740 return true;
anatofuz
parents:
diff changeset
741 }
anatofuz
parents:
diff changeset
742
anatofuz
parents:
diff changeset
743 bool HexagonExpandCondsets::isPredicable(MachineInstr *MI) {
anatofuz
parents:
diff changeset
744 if (HII->isPredicated(*MI) || !HII->isPredicable(*MI))
anatofuz
parents:
diff changeset
745 return false;
anatofuz
parents:
diff changeset
746 if (MI->hasUnmodeledSideEffects() || MI->mayStore())
anatofuz
parents:
diff changeset
747 return false;
anatofuz
parents:
diff changeset
748 // Reject instructions with multiple defs (e.g. post-increment loads).
anatofuz
parents:
diff changeset
749 bool HasDef = false;
anatofuz
parents:
diff changeset
750 for (auto &Op : MI->operands()) {
anatofuz
parents:
diff changeset
751 if (!Op.isReg() || !Op.isDef())
anatofuz
parents:
diff changeset
752 continue;
anatofuz
parents:
diff changeset
753 if (HasDef)
anatofuz
parents:
diff changeset
754 return false;
anatofuz
parents:
diff changeset
755 HasDef = true;
anatofuz
parents:
diff changeset
756 }
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
757 for (auto &Mo : MI->memoperands()) {
150
anatofuz
parents:
diff changeset
758 if (Mo->isVolatile() || Mo->isAtomic())
anatofuz
parents:
diff changeset
759 return false;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
760 }
150
anatofuz
parents:
diff changeset
761 return true;
anatofuz
parents:
diff changeset
762 }
anatofuz
parents:
diff changeset
763
anatofuz
parents:
diff changeset
764 /// Find the reaching definition for a predicated use of RD. The RD is used
anatofuz
parents:
diff changeset
765 /// under the conditions given by PredR and Cond, and this function will ignore
anatofuz
parents:
diff changeset
766 /// definitions that set RD under the opposite conditions.
anatofuz
parents:
diff changeset
767 MachineInstr *HexagonExpandCondsets::getReachingDefForPred(RegisterRef RD,
anatofuz
parents:
diff changeset
768 MachineBasicBlock::iterator UseIt, unsigned PredR, bool Cond) {
anatofuz
parents:
diff changeset
769 MachineBasicBlock &B = *UseIt->getParent();
anatofuz
parents:
diff changeset
770 MachineBasicBlock::iterator I = UseIt, S = B.begin();
anatofuz
parents:
diff changeset
771 if (I == S)
anatofuz
parents:
diff changeset
772 return nullptr;
anatofuz
parents:
diff changeset
773
anatofuz
parents:
diff changeset
774 bool PredValid = true;
anatofuz
parents:
diff changeset
775 do {
anatofuz
parents:
diff changeset
776 --I;
anatofuz
parents:
diff changeset
777 MachineInstr *MI = &*I;
anatofuz
parents:
diff changeset
778 // Check if this instruction can be ignored, i.e. if it is predicated
anatofuz
parents:
diff changeset
779 // on the complementary condition.
anatofuz
parents:
diff changeset
780 if (PredValid && HII->isPredicated(*MI)) {
anatofuz
parents:
diff changeset
781 if (MI->readsRegister(PredR) && (Cond != HII->isPredicatedTrue(*MI)))
anatofuz
parents:
diff changeset
782 continue;
anatofuz
parents:
diff changeset
783 }
anatofuz
parents:
diff changeset
784
anatofuz
parents:
diff changeset
785 // Check the defs. If the PredR is defined, invalidate it. If RD is
anatofuz
parents:
diff changeset
786 // defined, return the instruction or 0, depending on the circumstances.
anatofuz
parents:
diff changeset
787 for (auto &Op : MI->operands()) {
anatofuz
parents:
diff changeset
788 if (!Op.isReg() || !Op.isDef())
anatofuz
parents:
diff changeset
789 continue;
anatofuz
parents:
diff changeset
790 RegisterRef RR = Op;
anatofuz
parents:
diff changeset
791 if (RR.Reg == PredR) {
anatofuz
parents:
diff changeset
792 PredValid = false;
anatofuz
parents:
diff changeset
793 continue;
anatofuz
parents:
diff changeset
794 }
anatofuz
parents:
diff changeset
795 if (RR.Reg != RD.Reg)
anatofuz
parents:
diff changeset
796 continue;
anatofuz
parents:
diff changeset
797 // If the "Reg" part agrees, there is still the subregister to check.
anatofuz
parents:
diff changeset
798 // If we are looking for %1:loreg, we can skip %1:hireg, but
anatofuz
parents:
diff changeset
799 // not %1 (w/o subregisters).
anatofuz
parents:
diff changeset
800 if (RR.Sub == RD.Sub)
anatofuz
parents:
diff changeset
801 return MI;
anatofuz
parents:
diff changeset
802 if (RR.Sub == 0 || RD.Sub == 0)
anatofuz
parents:
diff changeset
803 return nullptr;
anatofuz
parents:
diff changeset
804 // We have different subregisters, so we can continue looking.
anatofuz
parents:
diff changeset
805 }
anatofuz
parents:
diff changeset
806 } while (I != S);
anatofuz
parents:
diff changeset
807
anatofuz
parents:
diff changeset
808 return nullptr;
anatofuz
parents:
diff changeset
809 }
anatofuz
parents:
diff changeset
810
anatofuz
parents:
diff changeset
811 /// Check if the instruction MI can be safely moved over a set of instructions
anatofuz
parents:
diff changeset
812 /// whose side-effects (in terms of register defs and uses) are expressed in
anatofuz
parents:
diff changeset
813 /// the maps Defs and Uses. These maps reflect the conditional defs and uses
anatofuz
parents:
diff changeset
814 /// that depend on the same predicate register to allow moving instructions
anatofuz
parents:
diff changeset
815 /// over instructions predicated on the opposite condition.
anatofuz
parents:
diff changeset
816 bool HexagonExpandCondsets::canMoveOver(MachineInstr &MI, ReferenceMap &Defs,
anatofuz
parents:
diff changeset
817 ReferenceMap &Uses) {
anatofuz
parents:
diff changeset
818 // In order to be able to safely move MI over instructions that define
anatofuz
parents:
diff changeset
819 // "Defs" and use "Uses", no def operand from MI can be defined or used
anatofuz
parents:
diff changeset
820 // and no use operand can be defined.
anatofuz
parents:
diff changeset
821 for (auto &Op : MI.operands()) {
anatofuz
parents:
diff changeset
822 if (!Op.isReg())
anatofuz
parents:
diff changeset
823 continue;
anatofuz
parents:
diff changeset
824 RegisterRef RR = Op;
anatofuz
parents:
diff changeset
825 // For physical register we would need to check register aliases, etc.
anatofuz
parents:
diff changeset
826 // and we don't want to bother with that. It would be of little value
anatofuz
parents:
diff changeset
827 // before the actual register rewriting (from virtual to physical).
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
828 if (!RR.Reg.isVirtual())
150
anatofuz
parents:
diff changeset
829 return false;
anatofuz
parents:
diff changeset
830 // No redefs for any operand.
anatofuz
parents:
diff changeset
831 if (isRefInMap(RR, Defs, Exec_Then))
anatofuz
parents:
diff changeset
832 return false;
anatofuz
parents:
diff changeset
833 // For defs, there cannot be uses.
anatofuz
parents:
diff changeset
834 if (Op.isDef() && isRefInMap(RR, Uses, Exec_Then))
anatofuz
parents:
diff changeset
835 return false;
anatofuz
parents:
diff changeset
836 }
anatofuz
parents:
diff changeset
837 return true;
anatofuz
parents:
diff changeset
838 }
anatofuz
parents:
diff changeset
839
anatofuz
parents:
diff changeset
840 /// Check if the instruction accessing memory (TheI) can be moved to the
anatofuz
parents:
diff changeset
841 /// location ToI.
anatofuz
parents:
diff changeset
842 bool HexagonExpandCondsets::canMoveMemTo(MachineInstr &TheI, MachineInstr &ToI,
anatofuz
parents:
diff changeset
843 bool IsDown) {
anatofuz
parents:
diff changeset
844 bool IsLoad = TheI.mayLoad(), IsStore = TheI.mayStore();
anatofuz
parents:
diff changeset
845 if (!IsLoad && !IsStore)
anatofuz
parents:
diff changeset
846 return true;
anatofuz
parents:
diff changeset
847 if (HII->areMemAccessesTriviallyDisjoint(TheI, ToI))
anatofuz
parents:
diff changeset
848 return true;
anatofuz
parents:
diff changeset
849 if (TheI.hasUnmodeledSideEffects())
anatofuz
parents:
diff changeset
850 return false;
anatofuz
parents:
diff changeset
851
anatofuz
parents:
diff changeset
852 MachineBasicBlock::iterator StartI = IsDown ? TheI : ToI;
anatofuz
parents:
diff changeset
853 MachineBasicBlock::iterator EndI = IsDown ? ToI : TheI;
anatofuz
parents:
diff changeset
854 bool Ordered = TheI.hasOrderedMemoryRef();
anatofuz
parents:
diff changeset
855
anatofuz
parents:
diff changeset
856 // Search for aliased memory reference in (StartI, EndI).
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
857 for (MachineInstr &MI : llvm::make_range(std::next(StartI), EndI)) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
858 if (MI.hasUnmodeledSideEffects())
150
anatofuz
parents:
diff changeset
859 return false;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
860 bool L = MI.mayLoad(), S = MI.mayStore();
150
anatofuz
parents:
diff changeset
861 if (!L && !S)
anatofuz
parents:
diff changeset
862 continue;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
863 if (Ordered && MI.hasOrderedMemoryRef())
150
anatofuz
parents:
diff changeset
864 return false;
anatofuz
parents:
diff changeset
865
anatofuz
parents:
diff changeset
866 bool Conflict = (L && IsStore) || S;
anatofuz
parents:
diff changeset
867 if (Conflict)
anatofuz
parents:
diff changeset
868 return false;
anatofuz
parents:
diff changeset
869 }
anatofuz
parents:
diff changeset
870 return true;
anatofuz
parents:
diff changeset
871 }
anatofuz
parents:
diff changeset
872
anatofuz
parents:
diff changeset
873 /// Generate a predicated version of MI (where the condition is given via
anatofuz
parents:
diff changeset
874 /// PredR and Cond) at the point indicated by Where.
anatofuz
parents:
diff changeset
875 void HexagonExpandCondsets::predicateAt(const MachineOperand &DefOp,
anatofuz
parents:
diff changeset
876 MachineInstr &MI,
anatofuz
parents:
diff changeset
877 MachineBasicBlock::iterator Where,
anatofuz
parents:
diff changeset
878 const MachineOperand &PredOp, bool Cond,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
879 std::set<Register> &UpdRegs) {
150
anatofuz
parents:
diff changeset
880 // The problem with updating live intervals is that we can move one def
anatofuz
parents:
diff changeset
881 // past another def. In particular, this can happen when moving an A2_tfrt
anatofuz
parents:
diff changeset
882 // over an A2_tfrf defining the same register. From the point of view of
anatofuz
parents:
diff changeset
883 // live intervals, these two instructions are two separate definitions,
anatofuz
parents:
diff changeset
884 // and each one starts another live segment. LiveIntervals's "handleMove"
anatofuz
parents:
diff changeset
885 // does not allow such moves, so we need to handle it ourselves. To avoid
anatofuz
parents:
diff changeset
886 // invalidating liveness data while we are using it, the move will be
anatofuz
parents:
diff changeset
887 // implemented in 4 steps: (1) add a clone of the instruction MI at the
anatofuz
parents:
diff changeset
888 // target location, (2) update liveness, (3) delete the old instruction,
anatofuz
parents:
diff changeset
889 // and (4) update liveness again.
anatofuz
parents:
diff changeset
890
anatofuz
parents:
diff changeset
891 MachineBasicBlock &B = *MI.getParent();
anatofuz
parents:
diff changeset
892 DebugLoc DL = Where->getDebugLoc(); // "Where" points to an instruction.
anatofuz
parents:
diff changeset
893 unsigned Opc = MI.getOpcode();
anatofuz
parents:
diff changeset
894 unsigned PredOpc = HII->getCondOpcode(Opc, !Cond);
anatofuz
parents:
diff changeset
895 MachineInstrBuilder MB = BuildMI(B, Where, DL, HII->get(PredOpc));
anatofuz
parents:
diff changeset
896 unsigned Ox = 0, NP = MI.getNumOperands();
anatofuz
parents:
diff changeset
897 // Skip all defs from MI first.
anatofuz
parents:
diff changeset
898 while (Ox < NP) {
anatofuz
parents:
diff changeset
899 MachineOperand &MO = MI.getOperand(Ox);
anatofuz
parents:
diff changeset
900 if (!MO.isReg() || !MO.isDef())
anatofuz
parents:
diff changeset
901 break;
anatofuz
parents:
diff changeset
902 Ox++;
anatofuz
parents:
diff changeset
903 }
anatofuz
parents:
diff changeset
904 // Add the new def, then the predicate register, then the rest of the
anatofuz
parents:
diff changeset
905 // operands.
anatofuz
parents:
diff changeset
906 MB.addReg(DefOp.getReg(), getRegState(DefOp), DefOp.getSubReg());
anatofuz
parents:
diff changeset
907 MB.addReg(PredOp.getReg(), PredOp.isUndef() ? RegState::Undef : 0,
anatofuz
parents:
diff changeset
908 PredOp.getSubReg());
anatofuz
parents:
diff changeset
909 while (Ox < NP) {
anatofuz
parents:
diff changeset
910 MachineOperand &MO = MI.getOperand(Ox);
anatofuz
parents:
diff changeset
911 if (!MO.isReg() || !MO.isImplicit())
anatofuz
parents:
diff changeset
912 MB.add(MO);
anatofuz
parents:
diff changeset
913 Ox++;
anatofuz
parents:
diff changeset
914 }
anatofuz
parents:
diff changeset
915 MB.cloneMemRefs(MI);
anatofuz
parents:
diff changeset
916
anatofuz
parents:
diff changeset
917 MachineInstr *NewI = MB;
anatofuz
parents:
diff changeset
918 NewI->clearKillInfo();
anatofuz
parents:
diff changeset
919 LIS->InsertMachineInstrInMaps(*NewI);
anatofuz
parents:
diff changeset
920
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
921 for (auto &Op : NewI->operands()) {
150
anatofuz
parents:
diff changeset
922 if (Op.isReg())
anatofuz
parents:
diff changeset
923 UpdRegs.insert(Op.getReg());
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
924 }
150
anatofuz
parents:
diff changeset
925 }
anatofuz
parents:
diff changeset
926
anatofuz
parents:
diff changeset
927 /// In the range [First, Last], rename all references to the "old" register RO
anatofuz
parents:
diff changeset
928 /// to the "new" register RN, but only in instructions predicated on the given
anatofuz
parents:
diff changeset
929 /// condition.
anatofuz
parents:
diff changeset
930 void HexagonExpandCondsets::renameInRange(RegisterRef RO, RegisterRef RN,
anatofuz
parents:
diff changeset
931 unsigned PredR, bool Cond, MachineBasicBlock::iterator First,
anatofuz
parents:
diff changeset
932 MachineBasicBlock::iterator Last) {
anatofuz
parents:
diff changeset
933 MachineBasicBlock::iterator End = std::next(Last);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
934 for (MachineInstr &MI : llvm::make_range(First, End)) {
150
anatofuz
parents:
diff changeset
935 // Do not touch instructions that are not predicated, or are predicated
anatofuz
parents:
diff changeset
936 // on the opposite condition.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
937 if (!HII->isPredicated(MI))
150
anatofuz
parents:
diff changeset
938 continue;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
939 if (!MI.readsRegister(PredR) || (Cond != HII->isPredicatedTrue(MI)))
150
anatofuz
parents:
diff changeset
940 continue;
anatofuz
parents:
diff changeset
941
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
942 for (auto &Op : MI.operands()) {
150
anatofuz
parents:
diff changeset
943 if (!Op.isReg() || RO != RegisterRef(Op))
anatofuz
parents:
diff changeset
944 continue;
anatofuz
parents:
diff changeset
945 Op.setReg(RN.Reg);
anatofuz
parents:
diff changeset
946 Op.setSubReg(RN.Sub);
anatofuz
parents:
diff changeset
947 // In practice, this isn't supposed to see any defs.
anatofuz
parents:
diff changeset
948 assert(!Op.isDef() && "Not expecting a def");
anatofuz
parents:
diff changeset
949 }
anatofuz
parents:
diff changeset
950 }
anatofuz
parents:
diff changeset
951 }
anatofuz
parents:
diff changeset
952
anatofuz
parents:
diff changeset
953 /// For a given conditional copy, predicate the definition of the source of
anatofuz
parents:
diff changeset
954 /// the copy under the given condition (using the same predicate register as
anatofuz
parents:
diff changeset
955 /// the copy).
anatofuz
parents:
diff changeset
956 bool HexagonExpandCondsets::predicate(MachineInstr &TfrI, bool Cond,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
957 std::set<Register> &UpdRegs) {
150
anatofuz
parents:
diff changeset
958 // TfrI - A2_tfr[tf] Instruction (not A2_tfrsi).
anatofuz
parents:
diff changeset
959 unsigned Opc = TfrI.getOpcode();
anatofuz
parents:
diff changeset
960 (void)Opc;
anatofuz
parents:
diff changeset
961 assert(Opc == Hexagon::A2_tfrt || Opc == Hexagon::A2_tfrf);
anatofuz
parents:
diff changeset
962 LLVM_DEBUG(dbgs() << "\nattempt to predicate if-" << (Cond ? "true" : "false")
anatofuz
parents:
diff changeset
963 << ": " << TfrI);
anatofuz
parents:
diff changeset
964
anatofuz
parents:
diff changeset
965 MachineOperand &MD = TfrI.getOperand(0);
anatofuz
parents:
diff changeset
966 MachineOperand &MP = TfrI.getOperand(1);
anatofuz
parents:
diff changeset
967 MachineOperand &MS = TfrI.getOperand(2);
anatofuz
parents:
diff changeset
968 // The source operand should be a <kill>. This is not strictly necessary,
anatofuz
parents:
diff changeset
969 // but it makes things a lot simpler. Otherwise, we would need to rename
anatofuz
parents:
diff changeset
970 // some registers, which would complicate the transformation considerably.
anatofuz
parents:
diff changeset
971 if (!MS.isKill())
anatofuz
parents:
diff changeset
972 return false;
anatofuz
parents:
diff changeset
973 // Avoid predicating instructions that define a subregister if subregister
anatofuz
parents:
diff changeset
974 // liveness tracking is not enabled.
anatofuz
parents:
diff changeset
975 if (MD.getSubReg() && !MRI->shouldTrackSubRegLiveness(MD.getReg()))
anatofuz
parents:
diff changeset
976 return false;
anatofuz
parents:
diff changeset
977
anatofuz
parents:
diff changeset
978 RegisterRef RT(MS);
anatofuz
parents:
diff changeset
979 Register PredR = MP.getReg();
anatofuz
parents:
diff changeset
980 MachineInstr *DefI = getReachingDefForPred(RT, TfrI, PredR, Cond);
anatofuz
parents:
diff changeset
981 if (!DefI || !isPredicable(DefI))
anatofuz
parents:
diff changeset
982 return false;
anatofuz
parents:
diff changeset
983
anatofuz
parents:
diff changeset
984 LLVM_DEBUG(dbgs() << "Source def: " << *DefI);
anatofuz
parents:
diff changeset
985
anatofuz
parents:
diff changeset
986 // Collect the information about registers defined and used between the
anatofuz
parents:
diff changeset
987 // DefI and the TfrI.
anatofuz
parents:
diff changeset
988 // Map: reg -> bitmask of subregs
anatofuz
parents:
diff changeset
989 ReferenceMap Uses, Defs;
anatofuz
parents:
diff changeset
990 MachineBasicBlock::iterator DefIt = DefI, TfrIt = TfrI;
anatofuz
parents:
diff changeset
991
anatofuz
parents:
diff changeset
992 // Check if the predicate register is valid between DefI and TfrI.
anatofuz
parents:
diff changeset
993 // If it is, we can then ignore instructions predicated on the negated
anatofuz
parents:
diff changeset
994 // conditions when collecting def and use information.
anatofuz
parents:
diff changeset
995 bool PredValid = true;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
996 for (MachineInstr &MI : llvm::make_range(std::next(DefIt), TfrIt)) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
997 if (!MI.modifiesRegister(PredR, nullptr))
150
anatofuz
parents:
diff changeset
998 continue;
anatofuz
parents:
diff changeset
999 PredValid = false;
anatofuz
parents:
diff changeset
1000 break;
anatofuz
parents:
diff changeset
1001 }
anatofuz
parents:
diff changeset
1002
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1003 for (MachineInstr &MI : llvm::make_range(std::next(DefIt), TfrIt)) {
150
anatofuz
parents:
diff changeset
1004 // If this instruction is predicated on the same register, it could
anatofuz
parents:
diff changeset
1005 // potentially be ignored.
anatofuz
parents:
diff changeset
1006 // By default assume that the instruction executes on the same condition
anatofuz
parents:
diff changeset
1007 // as TfrI (Exec_Then), and also on the opposite one (Exec_Else).
anatofuz
parents:
diff changeset
1008 unsigned Exec = Exec_Then | Exec_Else;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1009 if (PredValid && HII->isPredicated(MI) && MI.readsRegister(PredR))
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1010 Exec = (Cond == HII->isPredicatedTrue(MI)) ? Exec_Then : Exec_Else;
150
anatofuz
parents:
diff changeset
1011
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1012 for (auto &Op : MI.operands()) {
150
anatofuz
parents:
diff changeset
1013 if (!Op.isReg())
anatofuz
parents:
diff changeset
1014 continue;
anatofuz
parents:
diff changeset
1015 // We don't want to deal with physical registers. The reason is that
anatofuz
parents:
diff changeset
1016 // they can be aliased with other physical registers. Aliased virtual
anatofuz
parents:
diff changeset
1017 // registers must share the same register number, and can only differ
anatofuz
parents:
diff changeset
1018 // in the subregisters, which we are keeping track of. Physical
anatofuz
parents:
diff changeset
1019 // registers ters no longer have subregisters---their super- and
anatofuz
parents:
diff changeset
1020 // subregisters are other physical registers, and we are not checking
anatofuz
parents:
diff changeset
1021 // that.
anatofuz
parents:
diff changeset
1022 RegisterRef RR = Op;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1023 if (!RR.Reg.isVirtual())
150
anatofuz
parents:
diff changeset
1024 return false;
anatofuz
parents:
diff changeset
1025
anatofuz
parents:
diff changeset
1026 ReferenceMap &Map = Op.isDef() ? Defs : Uses;
anatofuz
parents:
diff changeset
1027 if (Op.isDef() && Op.isUndef()) {
anatofuz
parents:
diff changeset
1028 assert(RR.Sub && "Expecting a subregister on <def,read-undef>");
anatofuz
parents:
diff changeset
1029 // If this is a <def,read-undef>, then it invalidates the non-written
anatofuz
parents:
diff changeset
1030 // part of the register. For the purpose of checking the validity of
anatofuz
parents:
diff changeset
1031 // the move, assume that it modifies the whole register.
anatofuz
parents:
diff changeset
1032 RR.Sub = 0;
anatofuz
parents:
diff changeset
1033 }
anatofuz
parents:
diff changeset
1034 addRefToMap(RR, Map, Exec);
anatofuz
parents:
diff changeset
1035 }
anatofuz
parents:
diff changeset
1036 }
anatofuz
parents:
diff changeset
1037
anatofuz
parents:
diff changeset
1038 // The situation:
anatofuz
parents:
diff changeset
1039 // RT = DefI
anatofuz
parents:
diff changeset
1040 // ...
anatofuz
parents:
diff changeset
1041 // RD = TfrI ..., RT
anatofuz
parents:
diff changeset
1042
anatofuz
parents:
diff changeset
1043 // If the register-in-the-middle (RT) is used or redefined between
anatofuz
parents:
diff changeset
1044 // DefI and TfrI, we may not be able proceed with this transformation.
anatofuz
parents:
diff changeset
1045 // We can ignore a def that will not execute together with TfrI, and a
anatofuz
parents:
diff changeset
1046 // use that will. If there is such a use (that does execute together with
anatofuz
parents:
diff changeset
1047 // TfrI), we will not be able to move DefI down. If there is a use that
anatofuz
parents:
diff changeset
1048 // executed if TfrI's condition is false, then RT must be available
anatofuz
parents:
diff changeset
1049 // unconditionally (cannot be predicated).
anatofuz
parents:
diff changeset
1050 // Essentially, we need to be able to rename RT to RD in this segment.
anatofuz
parents:
diff changeset
1051 if (isRefInMap(RT, Defs, Exec_Then) || isRefInMap(RT, Uses, Exec_Else))
anatofuz
parents:
diff changeset
1052 return false;
anatofuz
parents:
diff changeset
1053 RegisterRef RD = MD;
anatofuz
parents:
diff changeset
1054 // If the predicate register is defined between DefI and TfrI, the only
anatofuz
parents:
diff changeset
1055 // potential thing to do would be to move the DefI down to TfrI, and then
anatofuz
parents:
diff changeset
1056 // predicate. The reaching def (DefI) must be movable down to the location
anatofuz
parents:
diff changeset
1057 // of the TfrI.
anatofuz
parents:
diff changeset
1058 // If the target register of the TfrI (RD) is not used or defined between
anatofuz
parents:
diff changeset
1059 // DefI and TfrI, consider moving TfrI up to DefI.
anatofuz
parents:
diff changeset
1060 bool CanUp = canMoveOver(TfrI, Defs, Uses);
anatofuz
parents:
diff changeset
1061 bool CanDown = canMoveOver(*DefI, Defs, Uses);
anatofuz
parents:
diff changeset
1062 // The TfrI does not access memory, but DefI could. Check if it's safe
anatofuz
parents:
diff changeset
1063 // to move DefI down to TfrI.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1064 if (DefI->mayLoadOrStore()) {
150
anatofuz
parents:
diff changeset
1065 if (!canMoveMemTo(*DefI, TfrI, true))
anatofuz
parents:
diff changeset
1066 CanDown = false;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1067 }
150
anatofuz
parents:
diff changeset
1068
anatofuz
parents:
diff changeset
1069 LLVM_DEBUG(dbgs() << "Can move up: " << (CanUp ? "yes" : "no")
anatofuz
parents:
diff changeset
1070 << ", can move down: " << (CanDown ? "yes\n" : "no\n"));
anatofuz
parents:
diff changeset
1071 MachineBasicBlock::iterator PastDefIt = std::next(DefIt);
anatofuz
parents:
diff changeset
1072 if (CanUp)
anatofuz
parents:
diff changeset
1073 predicateAt(MD, *DefI, PastDefIt, MP, Cond, UpdRegs);
anatofuz
parents:
diff changeset
1074 else if (CanDown)
anatofuz
parents:
diff changeset
1075 predicateAt(MD, *DefI, TfrIt, MP, Cond, UpdRegs);
anatofuz
parents:
diff changeset
1076 else
anatofuz
parents:
diff changeset
1077 return false;
anatofuz
parents:
diff changeset
1078
anatofuz
parents:
diff changeset
1079 if (RT != RD) {
anatofuz
parents:
diff changeset
1080 renameInRange(RT, RD, PredR, Cond, PastDefIt, TfrIt);
anatofuz
parents:
diff changeset
1081 UpdRegs.insert(RT.Reg);
anatofuz
parents:
diff changeset
1082 }
anatofuz
parents:
diff changeset
1083
anatofuz
parents:
diff changeset
1084 removeInstr(TfrI);
anatofuz
parents:
diff changeset
1085 removeInstr(*DefI);
anatofuz
parents:
diff changeset
1086 return true;
anatofuz
parents:
diff changeset
1087 }
anatofuz
parents:
diff changeset
1088
anatofuz
parents:
diff changeset
1089 /// Predicate all cases of conditional copies in the specified block.
anatofuz
parents:
diff changeset
1090 bool HexagonExpandCondsets::predicateInBlock(MachineBasicBlock &B,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1091 std::set<Register> &UpdRegs) {
150
anatofuz
parents:
diff changeset
1092 bool Changed = false;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1093 for (MachineInstr &MI : llvm::make_early_inc_range(B)) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1094 unsigned Opc = MI.getOpcode();
150
anatofuz
parents:
diff changeset
1095 if (Opc == Hexagon::A2_tfrt || Opc == Hexagon::A2_tfrf) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1096 bool Done = predicate(MI, (Opc == Hexagon::A2_tfrt), UpdRegs);
150
anatofuz
parents:
diff changeset
1097 if (!Done) {
anatofuz
parents:
diff changeset
1098 // If we didn't predicate I, we may need to remove it in case it is
anatofuz
parents:
diff changeset
1099 // an "identity" copy, e.g. %1 = A2_tfrt %2, %1.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1100 if (RegisterRef(MI.getOperand(0)) == RegisterRef(MI.getOperand(2))) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1101 for (auto &Op : MI.operands()) {
150
anatofuz
parents:
diff changeset
1102 if (Op.isReg())
anatofuz
parents:
diff changeset
1103 UpdRegs.insert(Op.getReg());
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1104 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1105 removeInstr(MI);
150
anatofuz
parents:
diff changeset
1106 }
anatofuz
parents:
diff changeset
1107 }
anatofuz
parents:
diff changeset
1108 Changed |= Done;
anatofuz
parents:
diff changeset
1109 }
anatofuz
parents:
diff changeset
1110 }
anatofuz
parents:
diff changeset
1111 return Changed;
anatofuz
parents:
diff changeset
1112 }
anatofuz
parents:
diff changeset
1113
anatofuz
parents:
diff changeset
1114 bool HexagonExpandCondsets::isIntReg(RegisterRef RR, unsigned &BW) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1115 if (!RR.Reg.isVirtual())
150
anatofuz
parents:
diff changeset
1116 return false;
anatofuz
parents:
diff changeset
1117 const TargetRegisterClass *RC = MRI->getRegClass(RR.Reg);
anatofuz
parents:
diff changeset
1118 if (RC == &Hexagon::IntRegsRegClass) {
anatofuz
parents:
diff changeset
1119 BW = 32;
anatofuz
parents:
diff changeset
1120 return true;
anatofuz
parents:
diff changeset
1121 }
anatofuz
parents:
diff changeset
1122 if (RC == &Hexagon::DoubleRegsRegClass) {
anatofuz
parents:
diff changeset
1123 BW = (RR.Sub != 0) ? 32 : 64;
anatofuz
parents:
diff changeset
1124 return true;
anatofuz
parents:
diff changeset
1125 }
anatofuz
parents:
diff changeset
1126 return false;
anatofuz
parents:
diff changeset
1127 }
anatofuz
parents:
diff changeset
1128
anatofuz
parents:
diff changeset
1129 bool HexagonExpandCondsets::isIntraBlocks(LiveInterval &LI) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1130 for (LiveRange::Segment &LR : LI) {
150
anatofuz
parents:
diff changeset
1131 // Range must start at a register...
anatofuz
parents:
diff changeset
1132 if (!LR.start.isRegister())
anatofuz
parents:
diff changeset
1133 return false;
anatofuz
parents:
diff changeset
1134 // ...and end in a register or in a dead slot.
anatofuz
parents:
diff changeset
1135 if (!LR.end.isRegister() && !LR.end.isDead())
anatofuz
parents:
diff changeset
1136 return false;
anatofuz
parents:
diff changeset
1137 }
anatofuz
parents:
diff changeset
1138 return true;
anatofuz
parents:
diff changeset
1139 }
anatofuz
parents:
diff changeset
1140
anatofuz
parents:
diff changeset
1141 bool HexagonExpandCondsets::coalesceRegisters(RegisterRef R1, RegisterRef R2) {
anatofuz
parents:
diff changeset
1142 if (CoaLimitActive) {
anatofuz
parents:
diff changeset
1143 if (CoaCounter >= CoaLimit)
anatofuz
parents:
diff changeset
1144 return false;
anatofuz
parents:
diff changeset
1145 CoaCounter++;
anatofuz
parents:
diff changeset
1146 }
anatofuz
parents:
diff changeset
1147 unsigned BW1, BW2;
anatofuz
parents:
diff changeset
1148 if (!isIntReg(R1, BW1) || !isIntReg(R2, BW2) || BW1 != BW2)
anatofuz
parents:
diff changeset
1149 return false;
anatofuz
parents:
diff changeset
1150 if (MRI->isLiveIn(R1.Reg))
anatofuz
parents:
diff changeset
1151 return false;
anatofuz
parents:
diff changeset
1152 if (MRI->isLiveIn(R2.Reg))
anatofuz
parents:
diff changeset
1153 return false;
anatofuz
parents:
diff changeset
1154
anatofuz
parents:
diff changeset
1155 LiveInterval &L1 = LIS->getInterval(R1.Reg);
anatofuz
parents:
diff changeset
1156 LiveInterval &L2 = LIS->getInterval(R2.Reg);
anatofuz
parents:
diff changeset
1157 if (L2.empty())
anatofuz
parents:
diff changeset
1158 return false;
anatofuz
parents:
diff changeset
1159 if (L1.hasSubRanges() || L2.hasSubRanges())
anatofuz
parents:
diff changeset
1160 return false;
anatofuz
parents:
diff changeset
1161 bool Overlap = L1.overlaps(L2);
anatofuz
parents:
diff changeset
1162
anatofuz
parents:
diff changeset
1163 LLVM_DEBUG(dbgs() << "compatible registers: ("
anatofuz
parents:
diff changeset
1164 << (Overlap ? "overlap" : "disjoint") << ")\n "
anatofuz
parents:
diff changeset
1165 << printReg(R1.Reg, TRI, R1.Sub) << " " << L1 << "\n "
anatofuz
parents:
diff changeset
1166 << printReg(R2.Reg, TRI, R2.Sub) << " " << L2 << "\n");
anatofuz
parents:
diff changeset
1167 if (R1.Sub || R2.Sub)
anatofuz
parents:
diff changeset
1168 return false;
anatofuz
parents:
diff changeset
1169 if (Overlap)
anatofuz
parents:
diff changeset
1170 return false;
anatofuz
parents:
diff changeset
1171
anatofuz
parents:
diff changeset
1172 // Coalescing could have a negative impact on scheduling, so try to limit
anatofuz
parents:
diff changeset
1173 // to some reasonable extent. Only consider coalescing segments, when one
anatofuz
parents:
diff changeset
1174 // of them does not cross basic block boundaries.
anatofuz
parents:
diff changeset
1175 if (!isIntraBlocks(L1) && !isIntraBlocks(L2))
anatofuz
parents:
diff changeset
1176 return false;
anatofuz
parents:
diff changeset
1177
anatofuz
parents:
diff changeset
1178 MRI->replaceRegWith(R2.Reg, R1.Reg);
anatofuz
parents:
diff changeset
1179
anatofuz
parents:
diff changeset
1180 // Move all live segments from L2 to L1.
anatofuz
parents:
diff changeset
1181 using ValueInfoMap = DenseMap<VNInfo *, VNInfo *>;
anatofuz
parents:
diff changeset
1182 ValueInfoMap VM;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1183 for (LiveRange::Segment &I : L2) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1184 VNInfo *NewVN, *OldVN = I.valno;
150
anatofuz
parents:
diff changeset
1185 ValueInfoMap::iterator F = VM.find(OldVN);
anatofuz
parents:
diff changeset
1186 if (F == VM.end()) {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1187 NewVN = L1.getNextValue(I.valno->def, LIS->getVNInfoAllocator());
150
anatofuz
parents:
diff changeset
1188 VM.insert(std::make_pair(OldVN, NewVN));
anatofuz
parents:
diff changeset
1189 } else {
anatofuz
parents:
diff changeset
1190 NewVN = F->second;
anatofuz
parents:
diff changeset
1191 }
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1192 L1.addSegment(LiveRange::Segment(I.start, I.end, NewVN));
150
anatofuz
parents:
diff changeset
1193 }
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1194 while (!L2.empty())
150
anatofuz
parents:
diff changeset
1195 L2.removeSegment(*L2.begin());
anatofuz
parents:
diff changeset
1196 LIS->removeInterval(R2.Reg);
anatofuz
parents:
diff changeset
1197
anatofuz
parents:
diff changeset
1198 updateKillFlags(R1.Reg);
anatofuz
parents:
diff changeset
1199 LLVM_DEBUG(dbgs() << "coalesced: " << L1 << "\n");
anatofuz
parents:
diff changeset
1200 L1.verify();
anatofuz
parents:
diff changeset
1201
anatofuz
parents:
diff changeset
1202 return true;
anatofuz
parents:
diff changeset
1203 }
anatofuz
parents:
diff changeset
1204
anatofuz
parents:
diff changeset
1205 /// Attempt to coalesce one of the source registers to a MUX instruction with
anatofuz
parents:
diff changeset
1206 /// the destination register. This could lead to having only one predicated
anatofuz
parents:
diff changeset
1207 /// instruction in the end instead of two.
anatofuz
parents:
diff changeset
1208 bool HexagonExpandCondsets::coalesceSegments(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1209 const SmallVectorImpl<MachineInstr *> &Condsets,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1210 std::set<Register> &UpdRegs) {
150
anatofuz
parents:
diff changeset
1211 SmallVector<MachineInstr*,16> TwoRegs;
anatofuz
parents:
diff changeset
1212 for (MachineInstr *MI : Condsets) {
anatofuz
parents:
diff changeset
1213 MachineOperand &S1 = MI->getOperand(2), &S2 = MI->getOperand(3);
anatofuz
parents:
diff changeset
1214 if (!S1.isReg() && !S2.isReg())
anatofuz
parents:
diff changeset
1215 continue;
anatofuz
parents:
diff changeset
1216 TwoRegs.push_back(MI);
anatofuz
parents:
diff changeset
1217 }
anatofuz
parents:
diff changeset
1218
anatofuz
parents:
diff changeset
1219 bool Changed = false;
anatofuz
parents:
diff changeset
1220 for (MachineInstr *CI : TwoRegs) {
anatofuz
parents:
diff changeset
1221 RegisterRef RD = CI->getOperand(0);
anatofuz
parents:
diff changeset
1222 RegisterRef RP = CI->getOperand(1);
anatofuz
parents:
diff changeset
1223 MachineOperand &S1 = CI->getOperand(2), &S2 = CI->getOperand(3);
anatofuz
parents:
diff changeset
1224 bool Done = false;
anatofuz
parents:
diff changeset
1225 // Consider this case:
anatofuz
parents:
diff changeset
1226 // %1 = instr1 ...
anatofuz
parents:
diff changeset
1227 // %2 = instr2 ...
anatofuz
parents:
diff changeset
1228 // %0 = C2_mux ..., %1, %2
anatofuz
parents:
diff changeset
1229 // If %0 was coalesced with %1, we could end up with the following
anatofuz
parents:
diff changeset
1230 // code:
anatofuz
parents:
diff changeset
1231 // %0 = instr1 ...
anatofuz
parents:
diff changeset
1232 // %2 = instr2 ...
anatofuz
parents:
diff changeset
1233 // %0 = A2_tfrf ..., %2
anatofuz
parents:
diff changeset
1234 // which will later become:
anatofuz
parents:
diff changeset
1235 // %0 = instr1 ...
anatofuz
parents:
diff changeset
1236 // %0 = instr2_cNotPt ...
anatofuz
parents:
diff changeset
1237 // i.e. there will be an unconditional definition (instr1) of %0
anatofuz
parents:
diff changeset
1238 // followed by a conditional one. The output dependency was there before
anatofuz
parents:
diff changeset
1239 // and it unavoidable, but if instr1 is predicable, we will no longer be
anatofuz
parents:
diff changeset
1240 // able to predicate it here.
anatofuz
parents:
diff changeset
1241 // To avoid this scenario, don't coalesce the destination register with
anatofuz
parents:
diff changeset
1242 // a source register that is defined by a predicable instruction.
anatofuz
parents:
diff changeset
1243 if (S1.isReg()) {
anatofuz
parents:
diff changeset
1244 RegisterRef RS = S1;
anatofuz
parents:
diff changeset
1245 MachineInstr *RDef = getReachingDefForPred(RS, CI, RP.Reg, true);
anatofuz
parents:
diff changeset
1246 if (!RDef || !HII->isPredicable(*RDef)) {
anatofuz
parents:
diff changeset
1247 Done = coalesceRegisters(RD, RegisterRef(S1));
anatofuz
parents:
diff changeset
1248 if (Done) {
anatofuz
parents:
diff changeset
1249 UpdRegs.insert(RD.Reg);
anatofuz
parents:
diff changeset
1250 UpdRegs.insert(S1.getReg());
anatofuz
parents:
diff changeset
1251 }
anatofuz
parents:
diff changeset
1252 }
anatofuz
parents:
diff changeset
1253 }
anatofuz
parents:
diff changeset
1254 if (!Done && S2.isReg()) {
anatofuz
parents:
diff changeset
1255 RegisterRef RS = S2;
anatofuz
parents:
diff changeset
1256 MachineInstr *RDef = getReachingDefForPred(RS, CI, RP.Reg, false);
anatofuz
parents:
diff changeset
1257 if (!RDef || !HII->isPredicable(*RDef)) {
anatofuz
parents:
diff changeset
1258 Done = coalesceRegisters(RD, RegisterRef(S2));
anatofuz
parents:
diff changeset
1259 if (Done) {
anatofuz
parents:
diff changeset
1260 UpdRegs.insert(RD.Reg);
anatofuz
parents:
diff changeset
1261 UpdRegs.insert(S2.getReg());
anatofuz
parents:
diff changeset
1262 }
anatofuz
parents:
diff changeset
1263 }
anatofuz
parents:
diff changeset
1264 }
anatofuz
parents:
diff changeset
1265 Changed |= Done;
anatofuz
parents:
diff changeset
1266 }
anatofuz
parents:
diff changeset
1267 return Changed;
anatofuz
parents:
diff changeset
1268 }
anatofuz
parents:
diff changeset
1269
anatofuz
parents:
diff changeset
1270 bool HexagonExpandCondsets::runOnMachineFunction(MachineFunction &MF) {
anatofuz
parents:
diff changeset
1271 if (skipFunction(MF.getFunction()))
anatofuz
parents:
diff changeset
1272 return false;
anatofuz
parents:
diff changeset
1273
anatofuz
parents:
diff changeset
1274 HII = static_cast<const HexagonInstrInfo*>(MF.getSubtarget().getInstrInfo());
anatofuz
parents:
diff changeset
1275 TRI = MF.getSubtarget().getRegisterInfo();
anatofuz
parents:
diff changeset
1276 MDT = &getAnalysis<MachineDominatorTree>();
anatofuz
parents:
diff changeset
1277 LIS = &getAnalysis<LiveIntervals>();
anatofuz
parents:
diff changeset
1278 MRI = &MF.getRegInfo();
anatofuz
parents:
diff changeset
1279
anatofuz
parents:
diff changeset
1280 LLVM_DEBUG(LIS->print(dbgs() << "Before expand-condsets\n",
anatofuz
parents:
diff changeset
1281 MF.getFunction().getParent()));
anatofuz
parents:
diff changeset
1282
anatofuz
parents:
diff changeset
1283 bool Changed = false;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1284 std::set<Register> CoalUpd, PredUpd;
150
anatofuz
parents:
diff changeset
1285
anatofuz
parents:
diff changeset
1286 SmallVector<MachineInstr*,16> Condsets;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1287 for (auto &B : MF) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1288 for (auto &I : B) {
150
anatofuz
parents:
diff changeset
1289 if (isCondset(I))
anatofuz
parents:
diff changeset
1290 Condsets.push_back(&I);
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1291 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1292 }
150
anatofuz
parents:
diff changeset
1293
anatofuz
parents:
diff changeset
1294 // Try to coalesce the target of a mux with one of its sources.
anatofuz
parents:
diff changeset
1295 // This could eliminate a register copy in some circumstances.
anatofuz
parents:
diff changeset
1296 Changed |= coalesceSegments(Condsets, CoalUpd);
anatofuz
parents:
diff changeset
1297
anatofuz
parents:
diff changeset
1298 // Update kill flags on all source operands. This is done here because
anatofuz
parents:
diff changeset
1299 // at this moment (when expand-condsets runs), there are no kill flags
anatofuz
parents:
diff changeset
1300 // in the IR (they have been removed by live range analysis).
anatofuz
parents:
diff changeset
1301 // Updating them right before we split is the easiest, because splitting
anatofuz
parents:
diff changeset
1302 // adds definitions which would interfere with updating kills afterwards.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
1303 std::set<Register> KillUpd;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1304 for (MachineInstr *MI : Condsets) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1305 for (MachineOperand &Op : MI->operands()) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1306 if (Op.isReg() && Op.isUse()) {
150
anatofuz
parents:
diff changeset
1307 if (!CoalUpd.count(Op.getReg()))
anatofuz
parents:
diff changeset
1308 KillUpd.insert(Op.getReg());
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1309 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1310 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1311 }
150
anatofuz
parents:
diff changeset
1312 updateLiveness(KillUpd, false, true, false);
anatofuz
parents:
diff changeset
1313 LLVM_DEBUG(
anatofuz
parents:
diff changeset
1314 LIS->print(dbgs() << "After coalescing\n", MF.getFunction().getParent()));
anatofuz
parents:
diff changeset
1315
anatofuz
parents:
diff changeset
1316 // First, simply split all muxes into a pair of conditional transfers
anatofuz
parents:
diff changeset
1317 // and update the live intervals to reflect the new arrangement. The
anatofuz
parents:
diff changeset
1318 // goal is to update the kill flags, since predication will rely on
anatofuz
parents:
diff changeset
1319 // them.
anatofuz
parents:
diff changeset
1320 for (MachineInstr *MI : Condsets)
anatofuz
parents:
diff changeset
1321 Changed |= split(*MI, PredUpd);
anatofuz
parents:
diff changeset
1322 Condsets.clear(); // The contents of Condsets are invalid here anyway.
anatofuz
parents:
diff changeset
1323
anatofuz
parents:
diff changeset
1324 // Do not update live ranges after splitting. Recalculation of live
anatofuz
parents:
diff changeset
1325 // intervals removes kill flags, which were preserved by splitting on
anatofuz
parents:
diff changeset
1326 // the source operands of condsets. These kill flags are needed by
anatofuz
parents:
diff changeset
1327 // predication, and after splitting they are difficult to recalculate
anatofuz
parents:
diff changeset
1328 // (because of predicated defs), so make sure they are left untouched.
anatofuz
parents:
diff changeset
1329 // Predication does not use live intervals.
anatofuz
parents:
diff changeset
1330 LLVM_DEBUG(
anatofuz
parents:
diff changeset
1331 LIS->print(dbgs() << "After splitting\n", MF.getFunction().getParent()));
anatofuz
parents:
diff changeset
1332
anatofuz
parents:
diff changeset
1333 // Traverse all blocks and collapse predicable instructions feeding
anatofuz
parents:
diff changeset
1334 // conditional transfers into predicated instructions.
anatofuz
parents:
diff changeset
1335 // Walk over all the instructions again, so we may catch pre-existing
anatofuz
parents:
diff changeset
1336 // cases that were not created in the previous step.
anatofuz
parents:
diff changeset
1337 for (auto &B : MF)
anatofuz
parents:
diff changeset
1338 Changed |= predicateInBlock(B, PredUpd);
anatofuz
parents:
diff changeset
1339 LLVM_DEBUG(LIS->print(dbgs() << "After predicating\n",
anatofuz
parents:
diff changeset
1340 MF.getFunction().getParent()));
anatofuz
parents:
diff changeset
1341
anatofuz
parents:
diff changeset
1342 PredUpd.insert(CoalUpd.begin(), CoalUpd.end());
anatofuz
parents:
diff changeset
1343 updateLiveness(PredUpd, true, true, true);
anatofuz
parents:
diff changeset
1344
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1345 if (Changed)
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1346 distributeLiveIntervals(PredUpd);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
1347
150
anatofuz
parents:
diff changeset
1348 LLVM_DEBUG({
anatofuz
parents:
diff changeset
1349 if (Changed)
anatofuz
parents:
diff changeset
1350 LIS->print(dbgs() << "After expand-condsets\n",
anatofuz
parents:
diff changeset
1351 MF.getFunction().getParent());
anatofuz
parents:
diff changeset
1352 });
anatofuz
parents:
diff changeset
1353
anatofuz
parents:
diff changeset
1354 return Changed;
anatofuz
parents:
diff changeset
1355 }
anatofuz
parents:
diff changeset
1356
anatofuz
parents:
diff changeset
1357 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
1358 // Public Constructor Functions
anatofuz
parents:
diff changeset
1359 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
1360 FunctionPass *llvm::createHexagonExpandCondsets() {
anatofuz
parents:
diff changeset
1361 return new HexagonExpandCondsets();
anatofuz
parents:
diff changeset
1362 }