blob: 2134d210e8c400510178e82ea405fb83304a6e31 [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
11// command line option, then all CONST32_* opcodes should be broken down into
12// 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"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000024#include "llvm/ADT/Statistic.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000025#include "llvm/CodeGen/LatencyPriorityQueue.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000026#include "llvm/CodeGen/MachineDominators.h"
27#include "llvm/CodeGen/MachineFunctionPass.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000028#include "llvm/CodeGen/MachineInstrBuilder.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000029#include "llvm/CodeGen/MachineLoopInfo.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000031#include "llvm/CodeGen/Passes.h"
32#include "llvm/CodeGen/ScheduleDAGInstrs.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000033#include "llvm/CodeGen/ScheduleHazardRecognizer.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000034#include "llvm/CodeGen/SchedulerRegistry.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000035#include "llvm/Support/CommandLine.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000036#include "llvm/Support/Compiler.h"
37#include "llvm/Support/Debug.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000038#include "llvm/Support/MathExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000039#include "llvm/Target/TargetInstrInfo.h"
40#include "llvm/Target/TargetMachine.h"
41#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000042#include <map>
Jyotsna Verma5eb59802013-05-07 19:53:00 +000043
44using namespace llvm;
45
Chandler Carruth84e68b22014-04-22 02:41:26 +000046#define DEBUG_TYPE "xfer"
47
Jyotsna Verma5eb59802013-05-07 19:53:00 +000048namespace {
49
50class HexagonSplitConst32AndConst64 : public MachineFunctionPass {
Jyotsna Verma5eb59802013-05-07 19:53:00 +000051 public:
52 static char ID;
Eric Christopher01f875e2015-02-02 22:11:43 +000053 HexagonSplitConst32AndConst64() : MachineFunctionPass(ID) {}
Jyotsna Verma5eb59802013-05-07 19:53:00 +000054
Craig Topper906c2cd2014-04-29 07:58:16 +000055 const char *getPassName() const override {
Jyotsna Verma5eb59802013-05-07 19:53:00 +000056 return "Hexagon Split Const32s and Const64s";
57 }
Craig Topper906c2cd2014-04-29 07:58:16 +000058 bool runOnMachineFunction(MachineFunction &Fn) override;
Jyotsna Verma5eb59802013-05-07 19:53:00 +000059};
60
61
62char HexagonSplitConst32AndConst64::ID = 0;
63
64
65bool HexagonSplitConst32AndConst64::runOnMachineFunction(MachineFunction &Fn) {
66
Eric Christopher0120db52014-05-21 22:42:07 +000067 const HexagonTargetObjectFile &TLOF =
Eric Christopher01f875e2015-02-02 22:11:43 +000068 *static_cast<const HexagonTargetObjectFile *>(
69 Fn.getTarget().getObjFileLowering());
Eric Christopher0120db52014-05-21 22:42:07 +000070 if (TLOF.IsSmallDataEnabled())
71 return true;
72
Eric Christopher01f875e2015-02-02 22:11:43 +000073 const TargetInstrInfo *TII = Fn.getSubtarget().getInstrInfo();
Jyotsna Verma5eb59802013-05-07 19:53:00 +000074
75 // Loop over all of the basic blocks
76 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
77 MBBb != MBBe; ++MBBb) {
78 MachineBasicBlock* MBB = MBBb;
79 // Traverse the basic block
80 MachineBasicBlock::iterator MII = MBB->begin();
81 MachineBasicBlock::iterator MIE = MBB->end ();
82 while (MII != MIE) {
83 MachineInstr *MI = MII;
84 int Opc = MI->getOpcode();
85 if (Opc == Hexagon::CONST32_set) {
86 int DestReg = MI->getOperand(0).getReg();
87 MachineOperand &Symbol = MI->getOperand (1);
88
89 BuildMI (*MBB, MII, MI->getDebugLoc(),
90 TII->get(Hexagon::LO), DestReg).addOperand(Symbol);
91 BuildMI (*MBB, MII, MI->getDebugLoc(),
92 TII->get(Hexagon::HI), DestReg).addOperand(Symbol);
93 // MBB->erase returns the iterator to the next instruction, which is the
94 // one we want to process next
95 MII = MBB->erase (MI);
96 continue;
97 }
98 else if (Opc == Hexagon::CONST32_set_jt) {
99 int DestReg = MI->getOperand(0).getReg();
100 MachineOperand &Symbol = MI->getOperand (1);
101
102 BuildMI (*MBB, MII, MI->getDebugLoc(),
103 TII->get(Hexagon::LO_jt), DestReg).addOperand(Symbol);
104 BuildMI (*MBB, MII, MI->getDebugLoc(),
105 TII->get(Hexagon::HI_jt), DestReg).addOperand(Symbol);
106 // MBB->erase returns the iterator to the next instruction, which is the
107 // one we want to process next
108 MII = MBB->erase (MI);
109 continue;
110 }
111 else if (Opc == Hexagon::CONST32_Label) {
112 int DestReg = MI->getOperand(0).getReg();
113 MachineOperand &Symbol = MI->getOperand (1);
114
115 BuildMI (*MBB, MII, MI->getDebugLoc(),
116 TII->get(Hexagon::LO_label), DestReg).addOperand(Symbol);
117 BuildMI (*MBB, MII, MI->getDebugLoc(),
118 TII->get(Hexagon::HI_label), DestReg).addOperand(Symbol);
119 // MBB->erase returns the iterator to the next instruction, which is the
120 // one we want to process next
121 MII = MBB->erase (MI);
122 continue;
123 }
124 else if (Opc == Hexagon::CONST32_Int_Real) {
125 int DestReg = MI->getOperand(0).getReg();
126 int64_t ImmValue = MI->getOperand(1).getImm ();
127
128 BuildMI (*MBB, MII, MI->getDebugLoc(),
129 TII->get(Hexagon::LOi), DestReg).addImm(ImmValue);
130 BuildMI (*MBB, MII, MI->getDebugLoc(),
131 TII->get(Hexagon::HIi), DestReg).addImm(ImmValue);
132 MII = MBB->erase (MI);
133 continue;
134 }
135 else if (Opc == Hexagon::CONST64_Int_Real) {
136 int DestReg = MI->getOperand(0).getReg();
137 int64_t ImmValue = MI->getOperand(1).getImm ();
Eric Christopher01f875e2015-02-02 22:11:43 +0000138 unsigned DestLo = Fn.getSubtarget().getRegisterInfo()->getSubReg(
Eric Christopherd9134482014-08-04 21:25:23 +0000139 DestReg, Hexagon::subreg_loreg);
Eric Christopher01f875e2015-02-02 22:11:43 +0000140 unsigned DestHi = Fn.getSubtarget().getRegisterInfo()->getSubReg(
Eric Christopherd9134482014-08-04 21:25:23 +0000141 DestReg, Hexagon::subreg_hireg);
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000142
143 int32_t LowWord = (ImmValue & 0xFFFFFFFF);
144 int32_t HighWord = (ImmValue >> 32) & 0xFFFFFFFF;
145
146 // Lower Registers Lower Half
147 BuildMI (*MBB, MII, MI->getDebugLoc(),
148 TII->get(Hexagon::LOi), DestLo).addImm(LowWord);
149 // Lower Registers Higher Half
150 BuildMI (*MBB, MII, MI->getDebugLoc(),
151 TII->get(Hexagon::HIi), DestLo).addImm(LowWord);
152 // Higher Registers Lower Half
153 BuildMI (*MBB, MII, MI->getDebugLoc(),
154 TII->get(Hexagon::LOi), DestHi).addImm(HighWord);
155 // Higher Registers Higher Half.
156 BuildMI (*MBB, MII, MI->getDebugLoc(),
157 TII->get(Hexagon::HIi), DestHi).addImm(HighWord);
158 MII = MBB->erase (MI);
159 continue;
160 }
161 ++MII;
162 }
163 }
164
165 return true;
166}
167
168}
169
170//===----------------------------------------------------------------------===//
171// Public Constructor Functions
172//===----------------------------------------------------------------------===//
173
174FunctionPass *
Eric Christopher01f875e2015-02-02 22:11:43 +0000175llvm::createHexagonSplitConst32AndConst64() {
176 return new HexagonSplitConst32AndConst64();
Jyotsna Verma5eb59802013-05-07 19:53:00 +0000177}