blob: f12c9f3d20f327fef91a0b2bd13856d7b3ba66cd [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>
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000038
39static cl::opt<int> CodeGrowthLimit("hexagon-amode-growth-limit",
40 cl::Hidden, cl::init(0), cl::desc("Code growth limit for address mode "
41 "optimization"));
42
43using namespace llvm;
44using namespace rdf;
45
46namespace llvm {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000047
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000048 FunctionPass *createHexagonOptAddrMode();
49 void initializeHexagonOptAddrModePass(PassRegistry &);
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000050
51} // end namespace llvm
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000052
53namespace {
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000054
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000055class HexagonOptAddrMode : public MachineFunctionPass {
56public:
57 static char ID;
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000058
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000059 HexagonOptAddrMode()
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000060 : MachineFunctionPass(ID), HII(nullptr), MDT(nullptr), DFG(nullptr),
61 LV(nullptr) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000062 PassRegistry &R = *PassRegistry::getPassRegistry();
63 initializeHexagonOptAddrModePass(R);
64 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000065
Mehdi Amini117296c2016-10-01 02:56:57 +000066 StringRef getPassName() const override {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000067 return "Optimize addressing mode of load/store";
68 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000069
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000070 void getAnalysisUsage(AnalysisUsage &AU) const override {
71 MachineFunctionPass::getAnalysisUsage(AU);
72 AU.addRequired<MachineDominatorTree>();
73 AU.addRequired<MachineDominanceFrontier>();
74 AU.setPreservesAll();
75 }
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +000076
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000077 bool runOnMachineFunction(MachineFunction &MF) override;
78
79private:
80 typedef DenseSet<MachineInstr *> MISetType;
81 typedef DenseMap<MachineInstr *, bool> InstrEvalMap;
82 const HexagonInstrInfo *HII;
83 MachineDominatorTree *MDT;
84 DataFlowGraph *DFG;
85 DataFlowGraph::DefStackMap DefM;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000086 Liveness *LV;
87 MISetType Deleted;
88
89 bool processBlock(NodeAddr<BlockNode *> BA);
90 bool xformUseMI(MachineInstr *TfrMI, MachineInstr *UseMI,
91 NodeAddr<UseNode *> UseN, unsigned UseMOnum);
92 bool analyzeUses(unsigned DefR, const NodeList &UNodeList,
93 InstrEvalMap &InstrEvalResult, short &SizeInc);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +000094 bool hasRepForm(MachineInstr &MI, unsigned TfrDefR);
95 bool canRemoveAddasl(NodeAddr<StmtNode *> AddAslSN, MachineInstr &MI,
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +000096 const NodeList &UNodeList);
97 void getAllRealUses(NodeAddr<StmtNode *> SN, NodeList &UNodeList);
98 bool allValidCandidates(NodeAddr<StmtNode *> SA, NodeList &UNodeList);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +000099 short getBaseWithLongOffset(const MachineInstr &MI) const;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000100 bool changeStore(MachineInstr *OldMI, MachineOperand ImmOp,
101 unsigned ImmOpNum);
102 bool changeLoad(MachineInstr *OldMI, MachineOperand ImmOp, unsigned ImmOpNum);
103 bool changeAddAsl(NodeAddr<UseNode *> AddAslUN, MachineInstr *AddAslMI,
104 const MachineOperand &ImmOp, unsigned ImmOpNum);
105};
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000106
107} // end anonymous namespace
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000108
109char HexagonOptAddrMode::ID = 0;
110
111INITIALIZE_PASS_BEGIN(HexagonOptAddrMode, "opt-amode",
112 "Optimize addressing mode", false, false)
113INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
114INITIALIZE_PASS_DEPENDENCY(MachineDominanceFrontier)
115INITIALIZE_PASS_END(HexagonOptAddrMode, "opt-amode", "Optimize addressing mode",
116 false, false)
117
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000118bool HexagonOptAddrMode::hasRepForm(MachineInstr &MI, unsigned TfrDefR) {
119 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000120
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000121 if ((!MID.mayStore() && !MID.mayLoad()) || HII->isPredicated(MI))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000122 return false;
123
124 if (MID.mayStore()) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000125 MachineOperand StOp = MI.getOperand(MI.getNumOperands() - 1);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000126 if (StOp.isReg() && StOp.getReg() == TfrDefR)
127 return false;
128 }
129
130 if (HII->getAddrMode(MI) == HexagonII::BaseRegOffset)
131 // Tranform to Absolute plus register offset.
132 return (HII->getBaseWithLongOffset(MI) >= 0);
133 else if (HII->getAddrMode(MI) == HexagonII::BaseImmOffset)
134 // Tranform to absolute addressing mode.
135 return (HII->getAbsoluteForm(MI) >= 0);
136
137 return false;
138}
139
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000140// Check if addasl instruction can be removed. This is possible only
141// if it's feeding to only load/store instructions with base + register
142// offset as these instruction can be tranformed to use 'absolute plus
143// shifted register offset'.
144// ex:
145// Rs = ##foo
146// Rx = addasl(Rs, Rt, #2)
147// Rd = memw(Rx + #28)
148// Above three instructions can be replaced with Rd = memw(Rt<<#2 + ##foo+28)
149
150bool HexagonOptAddrMode::canRemoveAddasl(NodeAddr<StmtNode *> AddAslSN,
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000151 MachineInstr &MI,
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000152 const NodeList &UNodeList) {
153 // check offset size in addasl. if 'offset > 3' return false
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000154 const MachineOperand &OffsetOp = MI.getOperand(3);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000155 if (!OffsetOp.isImm() || OffsetOp.getImm() > 3)
156 return false;
157
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000158 unsigned OffsetReg = MI.getOperand(2).getReg();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000159 RegisterRef OffsetRR;
160 NodeId OffsetRegRD = 0;
161 for (NodeAddr<UseNode *> UA : AddAslSN.Addr->members_if(DFG->IsUse, *DFG)) {
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000162 RegisterRef RR = UA.Addr->getRegRef(*DFG);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000163 if (OffsetReg == RR.Reg) {
164 OffsetRR = RR;
165 OffsetRegRD = UA.Addr->getReachingDef();
166 }
167 }
168
169 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
170 NodeAddr<UseNode *> UA = *I;
171 NodeAddr<InstrNode *> IA = UA.Addr->getOwner(*DFG);
Krzysztof Parzyszek634f57e2017-04-19 15:14:30 +0000172 if (UA.Addr->getFlags() & NodeAttrs::PhiRef)
173 return false;
174 NodeAddr<RefNode*> AA = LV->getNearestAliasedRef(OffsetRR, IA);
175 if ((DFG->IsDef(AA) && AA.Id != OffsetRegRD) ||
176 AA.Addr->getReachingDef() != OffsetRegRD)
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000177 return false;
178
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000179 MachineInstr &UseMI = *NodeAddr<StmtNode *>(IA).Addr->getCode();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000180 NodeAddr<DefNode *> OffsetRegDN = DFG->addr<DefNode *>(OffsetRegRD);
181 // Reaching Def to an offset register can't be a phi.
182 if ((OffsetRegDN.Addr->getFlags() & NodeAttrs::PhiRef) &&
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000183 MI.getParent() != UseMI.getParent())
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000184 return false;
185
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000186 const MCInstrDesc &UseMID = UseMI.getDesc();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000187 if ((!UseMID.mayLoad() && !UseMID.mayStore()) ||
188 HII->getAddrMode(UseMI) != HexagonII::BaseImmOffset ||
189 getBaseWithLongOffset(UseMI) < 0)
190 return false;
191
192 // Addasl output can't be a store value.
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000193 if (UseMID.mayStore() && UseMI.getOperand(2).isReg() &&
194 UseMI.getOperand(2).getReg() == MI.getOperand(0).getReg())
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000195 return false;
196
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000197 for (auto &Mo : UseMI.operands())
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000198 if (Mo.isFI())
199 return false;
200 }
201 return true;
202}
203
204bool HexagonOptAddrMode::allValidCandidates(NodeAddr<StmtNode *> SA,
205 NodeList &UNodeList) {
206 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
207 NodeAddr<UseNode *> UN = *I;
Krzysztof Parzyszek445bd122016-10-14 17:57:55 +0000208 RegisterRef UR = UN.Addr->getRegRef(*DFG);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000209 NodeSet Visited, Defs;
Krzysztof Parzyszekebabd992017-03-01 19:30:42 +0000210 const auto &P = LV->getAllReachingDefsRec(UR, UN, Visited, Defs);
211 if (!P.second) {
212 DEBUG({
213 dbgs() << "*** Unable to collect all reaching defs for use ***\n"
214 << PrintNode<UseNode*>(UN, *DFG) << '\n'
215 << "The program's complexity may exceed the limits.\n";
216 });
217 return false;
218 }
219 const auto &ReachingDefs = P.first;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000220 if (ReachingDefs.size() > 1) {
Krzysztof Parzyszek4a3e2852016-05-23 17:31:30 +0000221 DEBUG({
222 dbgs() << "*** Multiple Reaching Defs found!!! ***\n";
223 for (auto DI : ReachingDefs) {
224 NodeAddr<UseNode *> DA = DFG->addr<UseNode *>(DI);
225 NodeAddr<StmtNode *> TempIA = DA.Addr->getOwner(*DFG);
226 dbgs() << "\t\t[Reaching Def]: "
227 << Print<NodeAddr<InstrNode *>>(TempIA, *DFG) << "\n";
228 }
229 });
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000230 return false;
231 }
232 }
233 return true;
234}
235
236void HexagonOptAddrMode::getAllRealUses(NodeAddr<StmtNode *> SA,
237 NodeList &UNodeList) {
238 for (NodeAddr<DefNode *> DA : SA.Addr->members_if(DFG->IsDef, *DFG)) {
239 DEBUG(dbgs() << "\t\t[DefNode]: " << Print<NodeAddr<DefNode *>>(DA, *DFG)
240 << "\n");
Krzysztof Parzyszek5226ba82017-02-16 18:45:23 +0000241 RegisterRef DR = DFG->getPRI().normalize(DA.Addr->getRegRef(*DFG));
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000242
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000243 auto UseSet = LV->getAllReachedUses(DR, DA);
244
245 for (auto UI : UseSet) {
246 NodeAddr<UseNode *> UA = DFG->addr<UseNode *>(UI);
Krzysztof Parzyszek4a3e2852016-05-23 17:31:30 +0000247 DEBUG({
248 NodeAddr<StmtNode *> TempIA = UA.Addr->getOwner(*DFG);
249 dbgs() << "\t\t\t[Reached Use]: "
250 << Print<NodeAddr<InstrNode *>>(TempIA, *DFG) << "\n";
251 });
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000252
253 if (UA.Addr->getFlags() & NodeAttrs::PhiRef) {
254 NodeAddr<PhiNode *> PA = UA.Addr->getOwner(*DFG);
255 NodeId id = PA.Id;
256 const Liveness::RefMap &phiUse = LV->getRealUses(id);
257 DEBUG(dbgs() << "\t\t\t\tphi real Uses"
258 << Print<Liveness::RefMap>(phiUse, *DFG) << "\n");
Eugene Zelenko26e8c7d2016-12-16 01:00:40 +0000259 if (!phiUse.empty()) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000260 for (auto I : phiUse) {
Krzysztof Parzyszek74b1f252017-04-14 17:25:13 +0000261 if (!DFG->getPRI().alias(RegisterRef(I.first), DR))
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000262 continue;
263 auto phiUseSet = I.second;
264 for (auto phiUI : phiUseSet) {
Krzysztof Parzyszek7bb63ac2016-10-19 16:30:56 +0000265 NodeAddr<UseNode *> phiUA = DFG->addr<UseNode *>(phiUI.first);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000266 UNodeList.push_back(phiUA);
267 }
268 }
269 }
270 } else
271 UNodeList.push_back(UA);
272 }
273 }
274}
275
276bool HexagonOptAddrMode::analyzeUses(unsigned tfrDefR,
277 const NodeList &UNodeList,
278 InstrEvalMap &InstrEvalResult,
279 short &SizeInc) {
280 bool KeepTfr = false;
281 bool HasRepInstr = false;
282 InstrEvalResult.clear();
283
284 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
285 bool CanBeReplaced = false;
286 NodeAddr<UseNode *> UN = *I;
287 NodeAddr<StmtNode *> SN = UN.Addr->getOwner(*DFG);
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000288 MachineInstr &MI = *SN.Addr->getCode();
289 const MCInstrDesc &MID = MI.getDesc();
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000290 if ((MID.mayLoad() || MID.mayStore())) {
291 if (!hasRepForm(MI, tfrDefR)) {
292 KeepTfr = true;
293 continue;
294 }
295 SizeInc++;
296 CanBeReplaced = true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000297 } else if (MI.getOpcode() == Hexagon::S2_addasl_rrri) {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000298 NodeList AddaslUseList;
299
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000300 DEBUG(dbgs() << "\nGetting ReachedUses for === " << MI << "\n");
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000301 getAllRealUses(SN, AddaslUseList);
302 // Process phi nodes.
303 if (allValidCandidates(SN, AddaslUseList) &&
304 canRemoveAddasl(SN, MI, AddaslUseList)) {
305 SizeInc += AddaslUseList.size();
306 SizeInc -= 1; // Reduce size by 1 as addasl itself can be removed.
307 CanBeReplaced = true;
308 } else
309 SizeInc++;
310 } else
311 // Currently, only load/store and addasl are handled.
312 // Some other instructions to consider -
313 // A2_add -> A2_addi
314 // M4_mpyrr_addr -> M4_mpyrr_addi
315 KeepTfr = true;
316
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000317 InstrEvalResult[&MI] = CanBeReplaced;
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000318 HasRepInstr |= CanBeReplaced;
319 }
320
321 // Reduce total size by 2 if original tfr can be deleted.
322 if (!KeepTfr)
323 SizeInc -= 2;
324
325 return HasRepInstr;
326}
327
328bool HexagonOptAddrMode::changeLoad(MachineInstr *OldMI, MachineOperand ImmOp,
329 unsigned ImmOpNum) {
330 bool Changed = false;
331 MachineBasicBlock *BB = OldMI->getParent();
332 auto UsePos = MachineBasicBlock::iterator(OldMI);
333 MachineBasicBlock::instr_iterator InsertPt = UsePos.getInstrIterator();
334 ++InsertPt;
335 unsigned OpStart;
336 unsigned OpEnd = OldMI->getNumOperands();
337 MachineInstrBuilder MIB;
338
339 if (ImmOpNum == 1) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000340 if (HII->getAddrMode(*OldMI) == HexagonII::BaseRegOffset) {
341 short NewOpCode = HII->getBaseWithLongOffset(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000342 assert(NewOpCode >= 0 && "Invalid New opcode\n");
343 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
Diana Picus116bbab2017-01-13 09:58:52 +0000344 MIB.add(OldMI->getOperand(0));
345 MIB.add(OldMI->getOperand(2));
346 MIB.add(OldMI->getOperand(3));
347 MIB.add(ImmOp);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000348 OpStart = 4;
349 Changed = true;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000350 } else if (HII->getAddrMode(*OldMI) == HexagonII::BaseImmOffset) {
351 short NewOpCode = HII->getAbsoluteForm(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000352 assert(NewOpCode >= 0 && "Invalid New opcode\n");
353 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode))
Diana Picus116bbab2017-01-13 09:58:52 +0000354 .add(OldMI->getOperand(0));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000355 const GlobalValue *GV = ImmOp.getGlobal();
356 int64_t Offset = ImmOp.getOffset() + OldMI->getOperand(2).getImm();
357
358 MIB.addGlobalAddress(GV, Offset, ImmOp.getTargetFlags());
359 OpStart = 3;
360 Changed = true;
361 } else
362 Changed = false;
363
364 DEBUG(dbgs() << "[Changing]: " << *OldMI << "\n");
365 DEBUG(dbgs() << "[TO]: " << MIB << "\n");
366 } else if (ImmOpNum == 2 && OldMI->getOperand(3).getImm() == 0) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000367 short NewOpCode = HII->xformRegToImmOffset(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000368 assert(NewOpCode >= 0 && "Invalid New opcode\n");
369 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
Diana Picus116bbab2017-01-13 09:58:52 +0000370 MIB.add(OldMI->getOperand(0));
371 MIB.add(OldMI->getOperand(1));
372 MIB.add(ImmOp);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000373 OpStart = 4;
374 Changed = true;
375 DEBUG(dbgs() << "[Changing]: " << *OldMI << "\n");
376 DEBUG(dbgs() << "[TO]: " << MIB << "\n");
377 }
378
379 if (Changed)
380 for (unsigned i = OpStart; i < OpEnd; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000381 MIB.add(OldMI->getOperand(i));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000382
383 return Changed;
384}
385
386bool HexagonOptAddrMode::changeStore(MachineInstr *OldMI, MachineOperand ImmOp,
387 unsigned ImmOpNum) {
388 bool Changed = false;
389 unsigned OpStart;
390 unsigned OpEnd = OldMI->getNumOperands();
391 MachineBasicBlock *BB = OldMI->getParent();
392 auto UsePos = MachineBasicBlock::iterator(OldMI);
393 MachineBasicBlock::instr_iterator InsertPt = UsePos.getInstrIterator();
394 ++InsertPt;
395 MachineInstrBuilder MIB;
396 if (ImmOpNum == 0) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000397 if (HII->getAddrMode(*OldMI) == HexagonII::BaseRegOffset) {
398 short NewOpCode = HII->getBaseWithLongOffset(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000399 assert(NewOpCode >= 0 && "Invalid New opcode\n");
400 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
Diana Picus116bbab2017-01-13 09:58:52 +0000401 MIB.add(OldMI->getOperand(1));
402 MIB.add(OldMI->getOperand(2));
403 MIB.add(ImmOp);
404 MIB.add(OldMI->getOperand(3));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000405 OpStart = 4;
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000406 } else if (HII->getAddrMode(*OldMI) == HexagonII::BaseImmOffset) {
407 short NewOpCode = HII->getAbsoluteForm(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000408 assert(NewOpCode >= 0 && "Invalid New opcode\n");
409 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
410 const GlobalValue *GV = ImmOp.getGlobal();
411 int64_t Offset = ImmOp.getOffset() + OldMI->getOperand(1).getImm();
412 MIB.addGlobalAddress(GV, Offset, ImmOp.getTargetFlags());
Diana Picus116bbab2017-01-13 09:58:52 +0000413 MIB.add(OldMI->getOperand(2));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000414 OpStart = 3;
415 }
416 Changed = true;
417 DEBUG(dbgs() << "[Changing]: " << *OldMI << "\n");
418 DEBUG(dbgs() << "[TO]: " << MIB << "\n");
419 } else if (ImmOpNum == 1 && OldMI->getOperand(2).getImm() == 0) {
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000420 short NewOpCode = HII->xformRegToImmOffset(*OldMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000421 assert(NewOpCode >= 0 && "Invalid New opcode\n");
422 MIB = BuildMI(*BB, InsertPt, OldMI->getDebugLoc(), HII->get(NewOpCode));
Diana Picus116bbab2017-01-13 09:58:52 +0000423 MIB.add(OldMI->getOperand(0));
424 MIB.add(ImmOp);
425 MIB.add(OldMI->getOperand(1));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000426 OpStart = 2;
427 Changed = true;
428 DEBUG(dbgs() << "[Changing]: " << *OldMI << "\n");
429 DEBUG(dbgs() << "[TO]: " << MIB << "\n");
430 }
431 if (Changed)
432 for (unsigned i = OpStart; i < OpEnd; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000433 MIB.add(OldMI->getOperand(i));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000434
435 return Changed;
436}
437
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000438short HexagonOptAddrMode::getBaseWithLongOffset(const MachineInstr &MI) const {
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000439 if (HII->getAddrMode(MI) == HexagonII::BaseImmOffset) {
440 short TempOpCode = HII->getBaseWithRegOffset(MI);
441 return HII->getBaseWithLongOffset(TempOpCode);
442 } else
443 return HII->getBaseWithLongOffset(MI);
444}
445
446bool HexagonOptAddrMode::changeAddAsl(NodeAddr<UseNode *> AddAslUN,
447 MachineInstr *AddAslMI,
448 const MachineOperand &ImmOp,
449 unsigned ImmOpNum) {
450 NodeAddr<StmtNode *> SA = AddAslUN.Addr->getOwner(*DFG);
451
452 DEBUG(dbgs() << "Processing addasl :" << *AddAslMI << "\n");
453
454 NodeList UNodeList;
455 getAllRealUses(SA, UNodeList);
456
457 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
458 NodeAddr<UseNode *> UseUN = *I;
459 assert(!(UseUN.Addr->getFlags() & NodeAttrs::PhiRef) &&
460 "Can't transform this 'AddAsl' instruction!");
461
462 NodeAddr<StmtNode *> UseIA = UseUN.Addr->getOwner(*DFG);
463 DEBUG(dbgs() << "[InstrNode]: " << Print<NodeAddr<InstrNode *>>(UseIA, *DFG)
464 << "\n");
465 MachineInstr *UseMI = UseIA.Addr->getCode();
466 DEBUG(dbgs() << "[MI <BB#" << UseMI->getParent()->getNumber()
467 << ">]: " << *UseMI << "\n");
468 const MCInstrDesc &UseMID = UseMI->getDesc();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000469 assert(HII->getAddrMode(*UseMI) == HexagonII::BaseImmOffset);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000470
471 auto UsePos = MachineBasicBlock::iterator(UseMI);
472 MachineBasicBlock::instr_iterator InsertPt = UsePos.getInstrIterator();
Krzysztof Parzyszekf0b34a52016-07-29 21:49:42 +0000473 short NewOpCode = getBaseWithLongOffset(*UseMI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000474 assert(NewOpCode >= 0 && "Invalid New opcode\n");
475
476 unsigned OpStart;
477 unsigned OpEnd = UseMI->getNumOperands();
478
479 MachineBasicBlock *BB = UseMI->getParent();
480 MachineInstrBuilder MIB =
481 BuildMI(*BB, InsertPt, UseMI->getDebugLoc(), HII->get(NewOpCode));
482 // change mem(Rs + # ) -> mem(Rt << # + ##)
483 if (UseMID.mayLoad()) {
Diana Picus116bbab2017-01-13 09:58:52 +0000484 MIB.add(UseMI->getOperand(0));
485 MIB.add(AddAslMI->getOperand(2));
486 MIB.add(AddAslMI->getOperand(3));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000487 const GlobalValue *GV = ImmOp.getGlobal();
488 MIB.addGlobalAddress(GV, UseMI->getOperand(2).getImm(),
489 ImmOp.getTargetFlags());
490 OpStart = 3;
491 } else if (UseMID.mayStore()) {
Diana Picus116bbab2017-01-13 09:58:52 +0000492 MIB.add(AddAslMI->getOperand(2));
493 MIB.add(AddAslMI->getOperand(3));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000494 const GlobalValue *GV = ImmOp.getGlobal();
495 MIB.addGlobalAddress(GV, UseMI->getOperand(1).getImm(),
496 ImmOp.getTargetFlags());
Diana Picus116bbab2017-01-13 09:58:52 +0000497 MIB.add(UseMI->getOperand(2));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000498 OpStart = 3;
499 } else
500 llvm_unreachable("Unhandled instruction");
501
502 for (unsigned i = OpStart; i < OpEnd; ++i)
Diana Picus116bbab2017-01-13 09:58:52 +0000503 MIB.add(UseMI->getOperand(i));
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000504
505 Deleted.insert(UseMI);
506 }
507
508 return true;
509}
510
511bool HexagonOptAddrMode::xformUseMI(MachineInstr *TfrMI, MachineInstr *UseMI,
512 NodeAddr<UseNode *> UseN,
513 unsigned UseMOnum) {
514 const MachineOperand ImmOp = TfrMI->getOperand(1);
515 const MCInstrDesc &MID = UseMI->getDesc();
516 unsigned Changed = false;
517 if (MID.mayLoad())
518 Changed = changeLoad(UseMI, ImmOp, UseMOnum);
519 else if (MID.mayStore())
520 Changed = changeStore(UseMI, ImmOp, UseMOnum);
521 else if (UseMI->getOpcode() == Hexagon::S2_addasl_rrri)
522 Changed = changeAddAsl(UseN, UseMI, ImmOp, UseMOnum);
523
524 if (Changed)
525 Deleted.insert(UseMI);
526
527 return Changed;
528}
529
530bool HexagonOptAddrMode::processBlock(NodeAddr<BlockNode *> BA) {
531 bool Changed = false;
532
533 for (auto IA : BA.Addr->members(*DFG)) {
534 if (!DFG->IsCode<NodeAttrs::Stmt>(IA))
535 continue;
536
537 NodeAddr<StmtNode *> SA = IA;
538 MachineInstr *MI = SA.Addr->getCode();
539 if (MI->getOpcode() != Hexagon::A2_tfrsi ||
540 !MI->getOperand(1).isGlobal())
541 continue;
542
543 DEBUG(dbgs() << "[Analyzing A2_tfrsi]: " << *MI << "\n");
544 DEBUG(dbgs() << "\t[InstrNode]: " << Print<NodeAddr<InstrNode *>>(IA, *DFG)
545 << "\n");
546
547 NodeList UNodeList;
548 getAllRealUses(SA, UNodeList);
549
550 if (!allValidCandidates(SA, UNodeList))
551 continue;
552
553 short SizeInc = 0;
554 unsigned DefR = MI->getOperand(0).getReg();
555 InstrEvalMap InstrEvalResult;
556
557 // Analyze all uses and calculate increase in size. Perform the optimization
558 // only if there is no increase in size.
559 if (!analyzeUses(DefR, UNodeList, InstrEvalResult, SizeInc))
560 continue;
561 if (SizeInc > CodeGrowthLimit)
562 continue;
563
564 bool KeepTfr = false;
565
566 DEBUG(dbgs() << "\t[Total reached uses] : " << UNodeList.size() << "\n");
567 DEBUG(dbgs() << "\t[Processing Reached Uses] ===\n");
568 for (auto I = UNodeList.rbegin(), E = UNodeList.rend(); I != E; ++I) {
569 NodeAddr<UseNode *> UseN = *I;
570 assert(!(UseN.Addr->getFlags() & NodeAttrs::PhiRef) &&
571 "Found a PhiRef node as a real reached use!!");
572
573 NodeAddr<StmtNode *> OwnerN = UseN.Addr->getOwner(*DFG);
574 MachineInstr *UseMI = OwnerN.Addr->getCode();
Krzysztof Parzyszek4a3e2852016-05-23 17:31:30 +0000575 DEBUG(dbgs() << "\t\t[MI <BB#" << UseMI->getParent()->getNumber()
576 << ">]: " << *UseMI << "\n");
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000577
578 int UseMOnum = -1;
579 unsigned NumOperands = UseMI->getNumOperands();
580 for (unsigned j = 0; j < NumOperands - 1; ++j) {
581 const MachineOperand &op = UseMI->getOperand(j);
582 if (op.isReg() && op.isUse() && DefR == op.getReg())
583 UseMOnum = j;
584 }
585 assert(UseMOnum >= 0 && "Invalid reached use!");
586
587 if (InstrEvalResult[UseMI])
588 // Change UseMI if replacement is possible.
589 Changed |= xformUseMI(MI, UseMI, UseN, UseMOnum);
590 else
591 KeepTfr = true;
592 }
593 if (!KeepTfr)
594 Deleted.insert(MI);
595 }
596 return Changed;
597}
598
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000599bool HexagonOptAddrMode::runOnMachineFunction(MachineFunction &MF) {
Krzysztof Parzyszek643aaea2017-04-14 15:26:34 +0000600 if (skipFunction(*MF.getFunction()))
601 return false;
602
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000603 bool Changed = false;
604 auto &HST = MF.getSubtarget<HexagonSubtarget>();
605 auto &MRI = MF.getRegInfo();
606 HII = HST.getInstrInfo();
607 const auto &MDF = getAnalysis<MachineDominanceFrontier>();
608 MDT = &getAnalysis<MachineDominatorTree>();
609 const auto &TRI = *MF.getSubtarget().getRegisterInfo();
610 const TargetOperandInfo TOI(*HII);
611
Krzysztof Parzyszeka77fe4e2016-10-03 17:14:48 +0000612 DataFlowGraph G(MF, *HII, TRI, *MDT, MDF, TOI);
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000613 G.build();
614 DFG = &G;
615
616 Liveness L(MRI, *DFG);
617 L.computePhiInfo();
618 LV = &L;
619
Krzysztof Parzyszekf5cbac92016-04-29 15:49:13 +0000620 Deleted.clear();
621 NodeAddr<FuncNode *> FA = DFG->getFunc();
622 DEBUG(dbgs() << "==== [RefMap#]=====:\n "
623 << Print<NodeAddr<FuncNode *>>(FA, *DFG) << "\n");
624
625 for (NodeAddr<BlockNode *> BA : FA.Addr->members(*DFG))
626 Changed |= processBlock(BA);
627
628 for (auto MI : Deleted)
629 MI->eraseFromParent();
630
631 if (Changed) {
632 G.build();
633 L.computeLiveIns();
634 L.resetLiveIns();
635 L.resetKills();
636 }
637
638 return Changed;
639}
640
641//===----------------------------------------------------------------------===//
642// Public Constructor Functions
643//===----------------------------------------------------------------------===//
644
645FunctionPass *llvm::createHexagonOptAddrMode() {
646 return new HexagonOptAddrMode();
647}