blob: f99a0645507c63d642bfb5477149e690b75a41d6 [file] [log] [blame]
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +00001//===--- HexagonOptAddrMode.cpp -------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// This implements a Hexagon-specific pass to optimize addressing mode for
10// load/store instructions.
11//===----------------------------------------------------------------------===//
12
13#define DEBUG_TYPE "opt-addr-mode"
14
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000015#include "HexagonInstrInfo.h"
16#include "HexagonSubtarget.h"
17#include "MCTargetDesc/HexagonBaseInfo.h"
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000018#include "RDFGraph.h"
19#include "RDFLiveness.h"
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000020#include "llvm/ADT/DenseSet.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000021#include "llvm/ADT/StringRef.h"
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000022#include "llvm/CodeGen/MachineBasicBlock.h"
23#include "llvm/CodeGen/MachineDominanceFrontier.h"
24#include "llvm/CodeGen/MachineDominators.h"
25#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstr.h"
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000029#include "llvm/CodeGen/MachineOperand.h"
30#include "llvm/MC/MCInstrDesc.h"
31#include "llvm/Pass.h"
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000034#include "llvm/Support/ErrorHandling.h"
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000035#include "llvm/Support/raw_ostream.h"
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000036#include <cassert>
37#include <cstdint>
38#include <map>
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000039
40static cl::opt<int> CodeGrowthLimit("hexagon-amode-growth-limit",
41 cl::Hidden, cl::init(0), cl::desc("Code growth limit for address mode "
42 "optimization"));
43
44using namespace llvm;
45using namespace rdf;
46
47namespace llvm {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000048
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000049 FunctionPass *createHexagonOptAddrMode();
50 void initializeHexagonOptAddrModePass(PassRegistry &);
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000051
52} // end namespace llvm
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000053
54namespace {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000055
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000056class HexagonOptAddrMode : public MachineFunctionPass {
57public:
58 static char ID;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000059
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000060 HexagonOptAddrMode()
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000061 : MachineFunctionPass(ID), HII(nullptr), MDT(nullptr), DFG(nullptr),
62 LV(nullptr) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000063 PassRegistry &R = *PassRegistry::getPassRegistry();
64 initializeHexagonOptAddrModePass(R);
65 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000066
Mehdi Amini117296c2016-10-01 02:56:57 +000067 StringRef getPassName() const override {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000068 return "Optimize addressing mode of load/store";
69 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000070
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000071 void getAnalysisUsage(AnalysisUsage &AU) const override {
72 MachineFunctionPass::getAnalysisUsage(AU);
73 AU.addRequired<MachineDominatorTree>();
74 AU.addRequired<MachineDominanceFrontier>();
75 AU.setPreservesAll();
76 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000077
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000078 bool runOnMachineFunction(MachineFunction &MF) override;
79
80private:
81 typedef DenseSet<MachineInstr *> MISetType;
82 typedef DenseMap<MachineInstr *, bool> InstrEvalMap;
83 const HexagonInstrInfo *HII;
84 MachineDominatorTree *MDT;
85 DataFlowGraph *DFG;
86 DataFlowGraph::DefStackMap DefM;
87 std::map<RegisterRef, std::map<NodeId, NodeId>> RDefMap;
88 Liveness *LV;
89 MISetType Deleted;
90
91 bool processBlock(NodeAddr<BlockNode *> BA);
92 bool xformUseMI(MachineInstr *TfrMI, MachineInstr *UseMI,
93 NodeAddr<UseNode *> UseN, unsigned UseMOnum);
94 bool analyzeUses(unsigned DefR, const NodeList &UNodeList,
95 InstrEvalMap &InstrEvalResult, short &SizeInc);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +000096 bool hasRepForm(MachineInstr &MI, unsigned TfrDefR);
97 bool canRemoveAddasl(NodeAddr<StmtNode *> AddAslSN, MachineInstr &MI,
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000098 const NodeList &UNodeList);
99 void getAllRealUses(NodeAddr<StmtNode *> SN, NodeList &UNodeList);
100 bool allValidCandidates(NodeAddr<StmtNode *> SA, NodeList &UNodeList);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000101 short getBaseWithLongOffset(const MachineInstr &MI) const;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000102 void updateMap(NodeAddr<InstrNode *> IA);
103 bool constructDefMap(MachineBasicBlock *B);
104 bool changeStore(MachineInstr *OldMI, MachineOperand ImmOp,
105 unsigned ImmOpNum);
106 bool changeLoad(MachineInstr *OldMI, MachineOperand ImmOp, unsigned ImmOpNum);
107 bool changeAddAsl(NodeAddr<UseNode *> AddAslUN, MachineInstr *AddAslMI,
108 const MachineOperand &ImmOp, unsigned ImmOpNum);
109};
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000110
111} // end anonymous namespace
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000112
113char HexagonOptAddrMode::ID = 0;
114
115INITIALIZE_PASS_BEGIN(HexagonOptAddrMode, "opt-amode",
116 "Optimize addressing mode", false, false)
117INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
118INITIALIZE_PASS_DEPENDENCY(MachineDominanceFrontier)
119INITIALIZE_PASS_END(HexagonOptAddrMode, "opt-amode", "Optimize addressing mode",
120 false, false)
121
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000122bool HexagonOptAddrMode::hasRepForm(MachineInstr &MI, unsigned TfrDefR) {
123 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000124
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000125 if ((!MID.mayStore() && !MID.mayLoad()) || HII->isPredicated(MI))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000126 return false;
127
128 if (MID.mayStore()) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000129 MachineOperand StOp = MI.getOperand(MI.getNumOperands() - 1);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000130 if (StOp.isReg() && StOp.getReg() == TfrDefR)
131 return false;
132 }
133
134 if (HII->getAddrMode(MI) == HexagonII::BaseRegOffset)
135 // Tranform to Absolute plus register offset.
136 return (HII->getBaseWithLongOffset(MI) >= 0);
137 else if (HII->getAddrMode(MI) == HexagonII::BaseImmOffset)
138 // Tranform to absolute addressing mode.
139 return (HII->getAbsoluteForm(MI) >= 0);
140
141 return false;
142}
143
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000144// Check if addasl instruction can be removed. This is possible only
145// if it's feeding to only load/store instructions with base + register
146// offset as these instruction can be tranformed to use 'absolute plus
147// shifted register offset'.
148// ex:
149// Rs = ##foo
150// Rx = addasl(Rs, Rt, #2)
151// Rd = memw(Rx + #28)
152// Above three instructions can be replaced with Rd = memw(Rt<<#2 + ##foo+28)
153
154bool HexagonOptAddrMode::canRemoveAddasl(NodeAddr<StmtNode *> AddAslSN,
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000155 MachineInstr &MI,
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000156 const NodeList &UNodeList) {
157 // check offset size in addasl. if 'offset > 3' return false
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000158 const MachineOperand &OffsetOp = MI.getOperand(3);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000159 if (!OffsetOp.isImm() || OffsetOp.getImm() > 3)
160 return false;
161
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000162 unsigned OffsetReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000163 RegisterRef OffsetRR;
164 NodeId OffsetRegRD = 0;
165 for (NodeAddr<UseNode *> UA : AddAslSN.Addr->members_if(DFG->IsUse, *DFG)) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000166 RegisterRef RR = UA.Addr->getRegRef(*DFG);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000167 if (OffsetReg == RR.Reg) {
168 OffsetRR = RR;
169 OffsetRegRD = UA.Addr->getReachingDef();
170 }
171 }
172
173 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
174 NodeAddr<UseNode *> UA = *I;
175 NodeAddr<InstrNode *> IA = UA.Addr->getOwner(*DFG);
176 if ((UA.Addr->getFlags() & NodeAttrs::PhiRef) ||
177 RDefMap[OffsetRR][IA.Id] != OffsetRegRD)
178 return false;
179
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000180 MachineInstr &UseMI = *NodeAddr<StmtNode *>(IA).Addr->getCode();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000181 NodeAddr<DefNode *> OffsetRegDN = DFG->addr<DefNode *>(OffsetRegRD);
182 // Reaching Def to an offset register can't be a phi.
183 if ((OffsetRegDN.Addr->getFlags() & NodeAttrs::PhiRef) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000184 MI.getParent() != UseMI.getParent())
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000185 return false;
186
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000187 const MCInstrDesc &UseMID = UseMI.getDesc();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000188 if ((!UseMID.mayLoad() && !UseMID.mayStore()) ||
189 HII->getAddrMode(UseMI) != HexagonII::BaseImmOffset ||
190 getBaseWithLongOffset(UseMI) < 0)
191 return false;
192
193 // Addasl output can't be a store value.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000194 if (UseMID.mayStore() && UseMI.getOperand(2).isReg() &&
195 UseMI.getOperand(2).getReg() == MI.getOperand(0).getReg())
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000196 return false;
197
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000198 for (auto &Mo : UseMI.operands())
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000199 if (Mo.isFI())
200 return false;
201 }
202 return true;
203}
204
205bool HexagonOptAddrMode::allValidCandidates(NodeAddr<StmtNode *> SA,
206 NodeList &UNodeList) {
207 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
208 NodeAddr<UseNode *> UN = *I;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000209 RegisterRef UR = UN.Addr->getRegRef(*DFG);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000210 NodeSet Visited, Defs;
211 const auto &ReachingDefs = LV->getAllReachingDefsRec(UR, UN, Visited, Defs);
212 if (ReachingDefs.size() > 1) {
Krzysztof Parzyszek4a3e2852016-05-23 17:31:30 +0000213 DEBUG({
214 dbgs() << "*** Multiple Reaching Defs found!!! ***\n";
215 for (auto DI : ReachingDefs) {
216 NodeAddr<UseNode *> DA = DFG->addr<UseNode *>(DI);
217 NodeAddr<StmtNode *> TempIA = DA.Addr->getOwner(*DFG);
218 dbgs() << "\t\t[Reaching Def]: "
219 << Print<NodeAddr<InstrNode *>>(TempIA, *DFG) << "\n";
220 }
221 });
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000222 return false;
223 }
224 }
225 return true;
226}
227
228void HexagonOptAddrMode::getAllRealUses(NodeAddr<StmtNode *> SA,
229 NodeList &UNodeList) {
230 for (NodeAddr<DefNode *> DA : SA.Addr->members_if(DFG->IsDef, *DFG)) {
231 DEBUG(dbgs() << "\t\t[DefNode]: " << Print<NodeAddr<DefNode *>>(DA, *DFG)
232 << "\n");
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000233 RegisterRef DR = DFG->normalizeRef(DA.Addr->getRegRef(*DFG));
234
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000235 auto UseSet = LV->getAllReachedUses(DR, DA);
236
237 for (auto UI : UseSet) {
238 NodeAddr<UseNode *> UA = DFG->addr<UseNode *>(UI);
Krzysztof Parzyszek4a3e2852016-05-23 17:31:30 +0000239 DEBUG({
240 NodeAddr<StmtNode *> TempIA = UA.Addr->getOwner(*DFG);
241 dbgs() << "\t\t\t[Reached Use]: "
242 << Print<NodeAddr<InstrNode *>>(TempIA, *DFG) << "\n";
243 });
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000244
245 if (UA.Addr->getFlags() & NodeAttrs::PhiRef) {
246 NodeAddr<PhiNode *> PA = UA.Addr->getOwner(*DFG);
247 NodeId id = PA.Id;
248 const Liveness::RefMap &phiUse = LV->getRealUses(id);
249 DEBUG(dbgs() << "\t\t\t\tphi real Uses"
250 << Print<Liveness::RefMap>(phiUse, *DFG) << "\n");
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000251 if (!phiUse.empty()) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000252 for (auto I : phiUse) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000253 if (DR.Reg != I.first)
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000254 continue;
255 auto phiUseSet = I.second;
256 for (auto phiUI : phiUseSet) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000257 NodeAddr<UseNode *> phiUA = DFG->addr<UseNode *>(phiUI.first);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000258 UNodeList.push_back(phiUA);
259 }
260 }
261 }
262 } else
263 UNodeList.push_back(UA);
264 }
265 }
266}
267
268bool HexagonOptAddrMode::analyzeUses(unsigned tfrDefR,
269 const NodeList &UNodeList,
270 InstrEvalMap &InstrEvalResult,
271 short &SizeInc) {
272 bool KeepTfr = false;
273 bool HasRepInstr = false;
274 InstrEvalResult.clear();
275
276 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
277 bool CanBeReplaced = false;
278 NodeAddr<UseNode *> UN = *I;
279 NodeAddr<StmtNode *> SN = UN.Addr->getOwner(*DFG);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000280 MachineInstr &MI = *SN.Addr->getCode();
281 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000282 if ((MID.mayLoad() || MID.mayStore())) {
283 if (!hasRepForm(MI, tfrDefR)) {
284 KeepTfr = true;
285 continue;
286 }
287 SizeInc++;
288 CanBeReplaced = true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000289 } else if (MI.getOpcode() == Hexagon::S2_addasl_rrri) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000290 NodeList AddaslUseList;
291
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000292 DEBUG(dbgs() << "\nGetting ReachedUses for === " << MI << "\n");
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000293 getAllRealUses(SN, AddaslUseList);
294 // Process phi nodes.
295 if (allValidCandidates(SN, AddaslUseList) &&
296 canRemoveAddasl(SN, MI, AddaslUseList)) {
297 SizeInc += AddaslUseList.size();
298 SizeInc -= 1; // Reduce size by 1 as addasl itself can be removed.
299 CanBeReplaced = true;
300 } else
301 SizeInc++;
302 } else
303 // Currently, only load/store and addasl are handled.
304 // Some other instructions to consider -
305 // A2_add -> A2_addi
306 // M4_mpyrr_addr -> M4_mpyrr_addi
307 KeepTfr = true;
308
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000309 InstrEvalResult[&MI] = CanBeReplaced;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000310 HasRepInstr |= CanBeReplaced;
311 }
312
313 // Reduce total size by 2 if original tfr can be deleted.
314 if (!KeepTfr)
315 SizeInc -= 2;
316
317 return HasRepInstr;
318}
319
320bool HexagonOptAddrMode::changeLoad(MachineInstr *OldMI, MachineOperand ImmOp,
321 unsigned ImmOpNum) {
322 bool Changed = false;
323 MachineBasicBlock *BB = OldMI->getParent();
324 auto UsePos = MachineBasicBlock::iterator(OldMI);
325 MachineBasicBlock::instr_iterator InsertPt = UsePos.getInstrIterator();
326 ++InsertPt;
327 unsigned OpStart;
328 unsigned OpEnd = OldMI->getNumOperands();
329 MachineInstrBuilder MIB;
330
331 if (ImmOpNum == 1) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000332 if (HII->getAddrMode(*OldMI) == HexagonII::BaseRegOffset) {
333 short NewOpCode = HII->getBaseWithLongOffset(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000334 assert(NewOpCode >= 0 && "Invalid New opcode\n");
335 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
Diana Picus116bbab2017-01-13 09:58:52 +0000336 MIB.add(OldMI->getOperand(0));
337 MIB.add(OldMI->getOperand(2));
338 MIB.add(OldMI->getOperand(3));
339 MIB.add(ImmOp);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000340 OpStart = 4;
341 Changed = true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000342 } else if (HII->getAddrMode(*OldMI) == HexagonII::BaseImmOffset) {
343 short NewOpCode = HII->getAbsoluteForm(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000344 assert(NewOpCode >= 0 && "Invalid New opcode\n");
345 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode))
Diana Picus116bbab2017-01-13 09:58:52 +0000346 .add(OldMI->getOperand(0));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000347 const GlobalValue *GV = ImmOp.getGlobal();
348 int64_t Offset = ImmOp.getOffset() + OldMI->getOperand(2).getImm();
349
350 MIB.addGlobalAddress(GV, Offset, ImmOp.getTargetFlags());
351 OpStart = 3;
352 Changed = true;
353 } else
354 Changed = false;
355
356 DEBUG(dbgs() << "[Changing]: " << *OldMI << "\n");
357 DEBUG(dbgs() << "[TO]: " << MIB << "\n");
358 } else if (ImmOpNum == 2 && OldMI->getOperand(3).getImm() == 0) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000359 short NewOpCode = HII->xformRegToImmOffset(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000360 assert(NewOpCode >= 0 && "Invalid New opcode\n");
361 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
Diana Picus116bbab2017-01-13 09:58:52 +0000362 MIB.add(OldMI->getOperand(0));
363 MIB.add(OldMI->getOperand(1));
364 MIB.add(ImmOp);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000365 OpStart = 4;
366 Changed = true;
367 DEBUG(dbgs() << "[Changing]: " << *OldMI << "\n");
368 DEBUG(dbgs() << "[TO]: " << MIB << "\n");
369 }
370
371 if (Changed)
372 for (unsigned i = OpStart; i < OpEnd; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000373 MIB.add(OldMI->getOperand(i));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000374
375 return Changed;
376}
377
378bool HexagonOptAddrMode::changeStore(MachineInstr *OldMI, MachineOperand ImmOp,
379 unsigned ImmOpNum) {
380 bool Changed = false;
381 unsigned OpStart;
382 unsigned OpEnd = OldMI->getNumOperands();
383 MachineBasicBlock *BB = OldMI->getParent();
384 auto UsePos = MachineBasicBlock::iterator(OldMI);
385 MachineBasicBlock::instr_iterator InsertPt = UsePos.getInstrIterator();
386 ++InsertPt;
387 MachineInstrBuilder MIB;
388 if (ImmOpNum == 0) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000389 if (HII->getAddrMode(*OldMI) == HexagonII::BaseRegOffset) {
390 short NewOpCode = HII->getBaseWithLongOffset(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000391 assert(NewOpCode >= 0 && "Invalid New opcode\n");
392 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
Diana Picus116bbab2017-01-13 09:58:52 +0000393 MIB.add(OldMI->getOperand(1));
394 MIB.add(OldMI->getOperand(2));
395 MIB.add(ImmOp);
396 MIB.add(OldMI->getOperand(3));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000397 OpStart = 4;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000398 } else if (HII->getAddrMode(*OldMI) == HexagonII::BaseImmOffset) {
399 short NewOpCode = HII->getAbsoluteForm(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000400 assert(NewOpCode >= 0 && "Invalid New opcode\n");
401 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
402 const GlobalValue *GV = ImmOp.getGlobal();
403 int64_t Offset = ImmOp.getOffset() + OldMI->getOperand(1).getImm();
404 MIB.addGlobalAddress(GV, Offset, ImmOp.getTargetFlags());
Diana Picus116bbab2017-01-13 09:58:52 +0000405 MIB.add(OldMI->getOperand(2));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000406 OpStart = 3;
407 }
408 Changed = true;
409 DEBUG(dbgs() << "[Changing]: " << *OldMI << "\n");
410 DEBUG(dbgs() << "[TO]: " << MIB << "\n");
411 } else if (ImmOpNum == 1 && OldMI->getOperand(2).getImm() == 0) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000412 short NewOpCode = HII->xformRegToImmOffset(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000413 assert(NewOpCode >= 0 && "Invalid New opcode\n");
414 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
Diana Picus116bbab2017-01-13 09:58:52 +0000415 MIB.add(OldMI->getOperand(0));
416 MIB.add(ImmOp);
417 MIB.add(OldMI->getOperand(1));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000418 OpStart = 2;
419 Changed = true;
420 DEBUG(dbgs() << "[Changing]: " << *OldMI << "\n");
421 DEBUG(dbgs() << "[TO]: " << MIB << "\n");
422 }
423 if (Changed)
424 for (unsigned i = OpStart; i < OpEnd; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000425 MIB.add(OldMI->getOperand(i));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000426
427 return Changed;
428}
429
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000430short HexagonOptAddrMode::getBaseWithLongOffset(const MachineInstr &MI) const {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000431 if (HII->getAddrMode(MI) == HexagonII::BaseImmOffset) {
432 short TempOpCode = HII->getBaseWithRegOffset(MI);
433 return HII->getBaseWithLongOffset(TempOpCode);
434 } else
435 return HII->getBaseWithLongOffset(MI);
436}
437
438bool HexagonOptAddrMode::changeAddAsl(NodeAddr<UseNode *> AddAslUN,
439 MachineInstr *AddAslMI,
440 const MachineOperand &ImmOp,
441 unsigned ImmOpNum) {
442 NodeAddr<StmtNode *> SA = AddAslUN.Addr->getOwner(*DFG);
443
444 DEBUG(dbgs() << "Processing addasl :" << *AddAslMI << "\n");
445
446 NodeList UNodeList;
447 getAllRealUses(SA, UNodeList);
448
449 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
450 NodeAddr<UseNode *> UseUN = *I;
451 assert(!(UseUN.Addr->getFlags() & NodeAttrs::PhiRef) &&
452 "Can't transform this 'AddAsl' instruction!");
453
454 NodeAddr<StmtNode *> UseIA = UseUN.Addr->getOwner(*DFG);
455 DEBUG(dbgs() << "[InstrNode]: " << Print<NodeAddr<InstrNode *>>(UseIA, *DFG)
456 << "\n");
457 MachineInstr *UseMI = UseIA.Addr->getCode();
458 DEBUG(dbgs() << "[MI <BB#" << UseMI->getParent()->getNumber()
459 << ">]: " << *UseMI << "\n");
460 const MCInstrDesc &UseMID = UseMI->getDesc();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000461 assert(HII->getAddrMode(*UseMI) == HexagonII::BaseImmOffset);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000462
463 auto UsePos = MachineBasicBlock::iterator(UseMI);
464 MachineBasicBlock::instr_iterator InsertPt = UsePos.getInstrIterator();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000465 short NewOpCode = getBaseWithLongOffset(*UseMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000466 assert(NewOpCode >= 0 && "Invalid New opcode\n");
467
468 unsigned OpStart;
469 unsigned OpEnd = UseMI->getNumOperands();
470
471 MachineBasicBlock *BB = UseMI->getParent();
472 MachineInstrBuilder MIB =
473 BuildMI(*BB, InsertPt, UseMI->getDebugLoc(), HII->get(NewOpCode));
474 // change mem(Rs + # ) -> mem(Rt << # + ##)
475 if (UseMID.mayLoad()) {
Diana Picus116bbab2017-01-13 09:58:52 +0000476 MIB.add(UseMI->getOperand(0));
477 MIB.add(AddAslMI->getOperand(2));
478 MIB.add(AddAslMI->getOperand(3));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000479 const GlobalValue *GV = ImmOp.getGlobal();
480 MIB.addGlobalAddress(GV, UseMI->getOperand(2).getImm(),
481 ImmOp.getTargetFlags());
482 OpStart = 3;
483 } else if (UseMID.mayStore()) {
Diana Picus116bbab2017-01-13 09:58:52 +0000484 MIB.add(AddAslMI->getOperand(2));
485 MIB.add(AddAslMI->getOperand(3));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000486 const GlobalValue *GV = ImmOp.getGlobal();
487 MIB.addGlobalAddress(GV, UseMI->getOperand(1).getImm(),
488 ImmOp.getTargetFlags());
Diana Picus116bbab2017-01-13 09:58:52 +0000489 MIB.add(UseMI->getOperand(2));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000490 OpStart = 3;
491 } else
492 llvm_unreachable("Unhandled instruction");
493
494 for (unsigned i = OpStart; i < OpEnd; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000495 MIB.add(UseMI->getOperand(i));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000496
497 Deleted.insert(UseMI);
498 }
499
500 return true;
501}
502
503bool HexagonOptAddrMode::xformUseMI(MachineInstr *TfrMI, MachineInstr *UseMI,
504 NodeAddr<UseNode *> UseN,
505 unsigned UseMOnum) {
506 const MachineOperand ImmOp = TfrMI->getOperand(1);
507 const MCInstrDesc &MID = UseMI->getDesc();
508 unsigned Changed = false;
509 if (MID.mayLoad())
510 Changed = changeLoad(UseMI, ImmOp, UseMOnum);
511 else if (MID.mayStore())
512 Changed = changeStore(UseMI, ImmOp, UseMOnum);
513 else if (UseMI->getOpcode() == Hexagon::S2_addasl_rrri)
514 Changed = changeAddAsl(UseN, UseMI, ImmOp, UseMOnum);
515
516 if (Changed)
517 Deleted.insert(UseMI);
518
519 return Changed;
520}
521
522bool HexagonOptAddrMode::processBlock(NodeAddr<BlockNode *> BA) {
523 bool Changed = false;
524
525 for (auto IA : BA.Addr->members(*DFG)) {
526 if (!DFG->IsCode<NodeAttrs::Stmt>(IA))
527 continue;
528
529 NodeAddr<StmtNode *> SA = IA;
530 MachineInstr *MI = SA.Addr->getCode();
531 if (MI->getOpcode() != Hexagon::A2_tfrsi ||
532 !MI->getOperand(1).isGlobal())
533 continue;
534
535 DEBUG(dbgs() << "[Analyzing A2_tfrsi]: " << *MI << "\n");
536 DEBUG(dbgs() << "\t[InstrNode]: " << Print<NodeAddr<InstrNode *>>(IA, *DFG)
537 << "\n");
538
539 NodeList UNodeList;
540 getAllRealUses(SA, UNodeList);
541
542 if (!allValidCandidates(SA, UNodeList))
543 continue;
544
545 short SizeInc = 0;
546 unsigned DefR = MI->getOperand(0).getReg();
547 InstrEvalMap InstrEvalResult;
548
549 // Analyze all uses and calculate increase in size. Perform the optimization
550 // only if there is no increase in size.
551 if (!analyzeUses(DefR, UNodeList, InstrEvalResult, SizeInc))
552 continue;
553 if (SizeInc > CodeGrowthLimit)
554 continue;
555
556 bool KeepTfr = false;
557
558 DEBUG(dbgs() << "\t[Total reached uses] : " << UNodeList.size() << "\n");
559 DEBUG(dbgs() << "\t[Processing Reached Uses] ===\n");
560 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
561 NodeAddr<UseNode *> UseN = *I;
562 assert(!(UseN.Addr->getFlags() & NodeAttrs::PhiRef) &&
563 "Found a PhiRef node as a real reached use!!");
564
565 NodeAddr<StmtNode *> OwnerN = UseN.Addr->getOwner(*DFG);
566 MachineInstr *UseMI = OwnerN.Addr->getCode();
Krzysztof Parzyszek4a3e2852016-05-23 17:31:30 +0000567 DEBUG(dbgs() << "\t\t[MI <BB#" << UseMI->getParent()->getNumber()
568 << ">]: " << *UseMI << "\n");
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000569
570 int UseMOnum = -1;
571 unsigned NumOperands = UseMI->getNumOperands();
572 for (unsigned j = 0; j < NumOperands - 1; ++j) {
573 const MachineOperand &op = UseMI->getOperand(j);
574 if (op.isReg() && op.isUse() && DefR == op.getReg())
575 UseMOnum = j;
576 }
577 assert(UseMOnum >= 0 && "Invalid reached use!");
578
579 if (InstrEvalResult[UseMI])
580 // Change UseMI if replacement is possible.
581 Changed |= xformUseMI(MI, UseMI, UseN, UseMOnum);
582 else
583 KeepTfr = true;
584 }
585 if (!KeepTfr)
586 Deleted.insert(MI);
587 }
588 return Changed;
589}
590
591void HexagonOptAddrMode::updateMap(NodeAddr<InstrNode *> IA) {
592 RegisterSet RRs;
593 for (NodeAddr<RefNode *> RA : IA.Addr->members(*DFG))
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000594 RRs.insert(RA.Addr->getRegRef(*DFG));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000595 bool Common = false;
596 for (auto &R : RDefMap) {
597 if (!RRs.count(R.first))
598 continue;
599 Common = true;
600 break;
601 }
602 if (!Common)
603 return;
604
605 for (auto &R : RDefMap) {
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000606 auto F = DefM.find(R.first.Reg);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000607 if (F == DefM.end() || F->second.empty())
608 continue;
609 R.second[IA.Id] = F->second.top()->Id;
610 }
611}
612
613bool HexagonOptAddrMode::constructDefMap(MachineBasicBlock *B) {
614 bool Changed = false;
615 auto BA = DFG->getFunc().Addr->findBlock(B, *DFG);
616 DFG->markBlock(BA.Id, DefM);
617
618 for (NodeAddr<InstrNode *> IA : BA.Addr->members(*DFG)) {
619 updateMap(IA);
620 DFG->pushDefs(IA, DefM);
621 }
622
623 MachineDomTreeNode *N = MDT->getNode(B);
624 for (auto I : *N)
625 Changed |= constructDefMap(I->getBlock());
626
627 DFG->releaseBlock(BA.Id, DefM);
628 return Changed;
629}
630
631bool HexagonOptAddrMode::runOnMachineFunction(MachineFunction &MF) {
632 bool Changed = false;
633 auto &HST = MF.getSubtarget<HexagonSubtarget>();
634 auto &MRI = MF.getRegInfo();
635 HII = HST.getInstrInfo();
636 const auto &MDF = getAnalysis<MachineDominanceFrontier>();
637 MDT = &getAnalysis<MachineDominatorTree>();
638 const auto &TRI = *MF.getSubtarget().getRegisterInfo();
639 const TargetOperandInfo TOI(*HII);
640
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000641 DataFlowGraph G(MF, *HII, TRI, *MDT, MDF, TOI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000642 G.build();
643 DFG = &G;
644
645 Liveness L(MRI, *DFG);
646 L.computePhiInfo();
647 LV = &L;
648
649 constructDefMap(&DFG->getMF().front());
650
651 Deleted.clear();
652 NodeAddr<FuncNode *> FA = DFG->getFunc();
653 DEBUG(dbgs() << "==== [RefMap#]=====:\n "
654 << Print<NodeAddr<FuncNode *>>(FA, *DFG) << "\n");
655
656 for (NodeAddr<BlockNode *> BA : FA.Addr->members(*DFG))
657 Changed |= processBlock(BA);
658
659 for (auto MI : Deleted)
660 MI->eraseFromParent();
661
662 if (Changed) {
663 G.build();
664 L.computeLiveIns();
665 L.resetLiveIns();
666 L.resetKills();
667 }
668
669 return Changed;
670}
671
672//===----------------------------------------------------------------------===//
673// Public Constructor Functions
674//===----------------------------------------------------------------------===//
675
676FunctionPass *llvm::createHexagonOptAddrMode() {
677 return new HexagonOptAddrMode();
678}