blob: a7c3173dda11edfb7a6bf7bb12ccbf43619ce183 [file] [log] [blame]
Jyotsna Verma5eb59802013-05-07 19:53:00 +00001//=== HexagonSplitConst32AndConst64.cpp - split CONST32/Const64 into HI/LO ===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// When the compiler is invoked with no small data, for instance, with the -G0
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +000011// command line option, then all CONST* opcodes should be broken down into
Jyotsna Verma5eb59802013-05-07 19:53:00 +000012// appropriate LO and HI instructions. This splitting is done by this pass.
13// The only reason this is not done in the DAG lowering itself is that there
14// is no simple way of getting the register allocator to allot the same hard
15// register to the result of LO and HI instructions. This pass is always
16// scheduled after register allocation.
17//
18//===----------------------------------------------------------------------===//
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000019
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000020#include "HexagonMachineFunctionInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "HexagonSubtarget.h"
Eric Christopher0120db52014-05-21 22:42:07 +000022#include "HexagonTargetMachine.h"
23#include "HexagonTargetObjectFile.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000024#include "llvm/CodeGen/LatencyPriorityQueue.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000025#include "llvm/CodeGen/MachineDominators.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000027#include "llvm/CodeGen/MachineInstrBuilder.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000028#include "llvm/CodeGen/MachineLoopInfo.h"
29#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000030#include "llvm/CodeGen/Passes.h"
31#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000032#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000033#include "llvm/CodeGen/SchedulerRegistry.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000034#include "llvm/Support/Debug.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000035#include "llvm/Support/MathExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000036#include "llvm/Target/TargetInstrInfo.h"
37#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetRegisterInfo.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000039
40using namespace llvm;
41
Chandler Carruth84e68b22014-04-22 02:41:26 +000042#define DEBUG_TYPE "xfer"
43
Colin LeMahieu56efafc2015-06-15 19:05:35 +000044namespace llvm {
45 FunctionPass *createHexagonSplitConst32AndConst64();
46 void initializeHexagonSplitConst32AndConst64Pass(PassRegistry&);
47}
48
Jyotsna Verma5eb59802013-05-07 19:53:00 +000049namespace {
50
51class HexagonSplitConst32AndConst64 : public MachineFunctionPass {
Jyotsna Verma5eb59802013-05-07 19:53:00 +000052 public:
53 static char ID;
Eric Christopher01f875e2015-02-02 22:11:43 +000054 HexagonSplitConst32AndConst64() : MachineFunctionPass(ID) {}
Jyotsna Verma5eb59802013-05-07 19:53:00 +000055
Craig Topper906c2cd2014-04-29 07:58:16 +000056 const char *getPassName() const override {
Jyotsna Verma5eb59802013-05-07 19:53:00 +000057 return "Hexagon Split Const32s and Const64s";
58 }
Craig Topper906c2cd2014-04-29 07:58:16 +000059 bool runOnMachineFunction(MachineFunction &Fn) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000060 MachineFunctionProperties getRequiredProperties() const override {
61 return MachineFunctionProperties().set(
62 MachineFunctionProperties::Property::AllVRegsAllocated);
63 }
Jyotsna Verma5eb59802013-05-07 19:53:00 +000064};
65
66
67char HexagonSplitConst32AndConst64::ID = 0;
68
69
70bool HexagonSplitConst32AndConst64::runOnMachineFunction(MachineFunction &Fn) {
71
Eric Christopher0120db52014-05-21 22:42:07 +000072 const HexagonTargetObjectFile &TLOF =
Eric Christopher01f875e2015-02-02 22:11:43 +000073 *static_cast<const HexagonTargetObjectFile *>(
74 Fn.getTarget().getObjFileLowering());
Krzysztof Parzyszek5de59102016-04-21 18:56:45 +000075 if (TLOF.isSmallDataEnabled())
Eric Christopher0120db52014-05-21 22:42:07 +000076 return true;
77
Eric Christopher01f875e2015-02-02 22:11:43 +000078 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
Colin LeMahieu54251092015-03-09 20:11:02 +000079 const TargetRegisterInfo *TRI = Fn.getSubtarget().getRegisterInfo();
Jyotsna Verma5eb59802013-05-07 19:53:00 +000080
81 // Loop over all of the basic blocks
82 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
83 MBBb != MBBe; ++MBBb) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +000084 MachineBasicBlock *MBB = &*MBBb;
Jyotsna Verma5eb59802013-05-07 19:53:00 +000085 // Traverse the basic block
86 MachineBasicBlock::iterator MII = MBB->begin();
87 MachineBasicBlock::iterator MIE = MBB->end ();
88 while (MII != MIE) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000089 MachineInstr &MI = *MII;
90 int Opc = MI.getOpcode();
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +000091 if (Opc == Hexagon::CONST32 && MI.getOperand(1).isBlockAddress()) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000092 int DestReg = MI.getOperand(0).getReg();
93 MachineOperand &Symbol = MI.getOperand(1);
Jyotsna Verma5eb59802013-05-07 19:53:00 +000094
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +000095 BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::LO), DestReg)
96 .addOperand(Symbol);
97 BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::HI), DestReg)
98 .addOperand(Symbol);
Jyotsna Verma5eb59802013-05-07 19:53:00 +000099 // MBB->erase returns the iterator to the next instruction, which is the
100 // one we want to process next
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000101 MII = MBB->erase(&MI);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000102 continue;
103 }
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000104
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000105 else if (Opc == Hexagon::CONST32) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000106 int DestReg = MI.getOperand(0).getReg();
Colin LeMahieu54251092015-03-09 20:11:02 +0000107
108 // We have to convert an FP immediate into its corresponding integer
109 // representation
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000110 int64_t ImmValue = MI.getOperand(1).getImm();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000111 BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::A2_tfrsi),
112 DestReg)
113 .addImm(ImmValue);
114 MII = MBB->erase(&MI);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000115 continue;
116 }
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000117 else if (Opc == Hexagon::CONST64) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000118 int DestReg = MI.getOperand(0).getReg();
Krzysztof Parzyszeka3386502016-08-10 16:46:36 +0000119 int64_t ImmValue = MI.getOperand(1).getImm();
Colin LeMahieu54251092015-03-09 20:11:02 +0000120 unsigned DestLo = TRI->getSubReg(DestReg, Hexagon::subreg_loreg);
121 unsigned DestHi = TRI->getSubReg(DestReg, Hexagon::subreg_hireg);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000122
123 int32_t LowWord = (ImmValue & 0xFFFFFFFF);
124 int32_t HighWord = (ImmValue >> 32) & 0xFFFFFFFF;
125
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000126 BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::A2_tfrsi),
127 DestLo)
128 .addImm(LowWord);
129 BuildMI(*MBB, MII, MI.getDebugLoc(), TII->get(Hexagon::A2_tfrsi),
130 DestHi)
131 .addImm(HighWord);
132 MII = MBB->erase(&MI);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000133 continue;
Colin LeMahieu54251092015-03-09 20:11:02 +0000134 }
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000135 ++MII;
136 }
137 }
138
139 return true;
140}
141
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000142}
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000143
144//===----------------------------------------------------------------------===//
145// Public Constructor Functions
146//===----------------------------------------------------------------------===//
147
148FunctionPass *
Eric Christopher01f875e2015-02-02 22:11:43 +0000149llvm::createHexagonSplitConst32AndConst64() {
150 return new HexagonSplitConst32AndConst64();
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000151}