blob: d814e33de2c1a76ace1d48b6c8b37a222ecec6d8 [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 "HexagonTargetMachine.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000021#include "HexagonMachineFunctionInfo.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "HexagonSubtarget.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000023#include "llvm/ADT/Statistic.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"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000034#include "llvm/Support/CommandLine.h"
Jyotsna Verma5eb59802013-05-07 19:53:00 +000035#include "llvm/Support/Compiler.h"
36#include "llvm/Support/Debug.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000037#include "llvm/Support/MathExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000038#include "llvm/Target/TargetInstrInfo.h"
39#include "llvm/Target/TargetMachine.h"
40#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling0cb8c0b2013-08-21 20:36:42 +000041#include <map>
Jyotsna Verma5eb59802013-05-07 19:53:00 +000042
43using namespace llvm;
44
Chandler Carruth84e68b22014-04-22 02:41:26 +000045#define DEBUG_TYPE "xfer"
46
Jyotsna Verma5eb59802013-05-07 19:53:00 +000047namespace {
48
49class HexagonSplitConst32AndConst64 : public MachineFunctionPass {
Eric Christopher1e65e7c2014-05-21 22:42:02 +000050 const HexagonTargetMachine &QTM;
Jyotsna Verma5eb59802013-05-07 19:53:00 +000051
52 public:
53 static char ID;
Eric Christopher1e65e7c2014-05-21 22:42:02 +000054 HexagonSplitConst32AndConst64(const HexagonTargetMachine &TM)
55 : MachineFunctionPass(ID), QTM(TM) {}
Jyotsna Verma5eb59802013-05-07 19:53:00 +000056
Craig Topper906c2cd2014-04-29 07:58:16 +000057 const char *getPassName() const override {
Jyotsna Verma5eb59802013-05-07 19:53:00 +000058 return "Hexagon Split Const32s and Const64s";
59 }
Craig Topper906c2cd2014-04-29 07:58:16 +000060 bool runOnMachineFunction(MachineFunction &Fn) override;
Jyotsna Verma5eb59802013-05-07 19:53:00 +000061};
62
63
64char HexagonSplitConst32AndConst64::ID = 0;
65
66
67bool HexagonSplitConst32AndConst64::runOnMachineFunction(MachineFunction &Fn) {
68
69 const TargetInstrInfo *TII = QTM.getInstrInfo();
70
71 // Loop over all of the basic blocks
72 for (MachineFunction::iterator MBBb = Fn.begin(), MBBe = Fn.end();
73 MBBb != MBBe; ++MBBb) {
74 MachineBasicBlock* MBB = MBBb;
75 // Traverse the basic block
76 MachineBasicBlock::iterator MII = MBB->begin();
77 MachineBasicBlock::iterator MIE = MBB->end ();
78 while (MII != MIE) {
79 MachineInstr *MI = MII;
80 int Opc = MI->getOpcode();
81 if (Opc == Hexagon::CONST32_set) {
82 int DestReg = MI->getOperand(0).getReg();
83 MachineOperand &Symbol = MI->getOperand (1);
84
85 BuildMI (*MBB, MII, MI->getDebugLoc(),
86 TII->get(Hexagon::LO), DestReg).addOperand(Symbol);
87 BuildMI (*MBB, MII, MI->getDebugLoc(),
88 TII->get(Hexagon::HI), DestReg).addOperand(Symbol);
89 // MBB->erase returns the iterator to the next instruction, which is the
90 // one we want to process next
91 MII = MBB->erase (MI);
92 continue;
93 }
94 else if (Opc == Hexagon::CONST32_set_jt) {
95 int DestReg = MI->getOperand(0).getReg();
96 MachineOperand &Symbol = MI->getOperand (1);
97
98 BuildMI (*MBB, MII, MI->getDebugLoc(),
99 TII->get(Hexagon::LO_jt), DestReg).addOperand(Symbol);
100 BuildMI (*MBB, MII, MI->getDebugLoc(),
101 TII->get(Hexagon::HI_jt), DestReg).addOperand(Symbol);
102 // MBB->erase returns the iterator to the next instruction, which is the
103 // one we want to process next
104 MII = MBB->erase (MI);
105 continue;
106 }
107 else if (Opc == Hexagon::CONST32_Label) {
108 int DestReg = MI->getOperand(0).getReg();
109 MachineOperand &Symbol = MI->getOperand (1);
110
111 BuildMI (*MBB, MII, MI->getDebugLoc(),
112 TII->get(Hexagon::LO_label), DestReg).addOperand(Symbol);
113 BuildMI (*MBB, MII, MI->getDebugLoc(),
114 TII->get(Hexagon::HI_label), DestReg).addOperand(Symbol);
115 // MBB->erase returns the iterator to the next instruction, which is the
116 // one we want to process next
117 MII = MBB->erase (MI);
118 continue;
119 }
120 else if (Opc == Hexagon::CONST32_Int_Real) {
121 int DestReg = MI->getOperand(0).getReg();
122 int64_t ImmValue = MI->getOperand(1).getImm ();
123
124 BuildMI (*MBB, MII, MI->getDebugLoc(),
125 TII->get(Hexagon::LOi), DestReg).addImm(ImmValue);
126 BuildMI (*MBB, MII, MI->getDebugLoc(),
127 TII->get(Hexagon::HIi), DestReg).addImm(ImmValue);
128 MII = MBB->erase (MI);
129 continue;
130 }
131 else if (Opc == Hexagon::CONST64_Int_Real) {
132 int DestReg = MI->getOperand(0).getReg();
133 int64_t ImmValue = MI->getOperand(1).getImm ();
134 unsigned DestLo =
135 QTM.getRegisterInfo()->getSubReg (DestReg, Hexagon::subreg_loreg);
136 unsigned DestHi =
137 QTM.getRegisterInfo()->getSubReg (DestReg, Hexagon::subreg_hireg);
138
139 int32_t LowWord = (ImmValue & 0xFFFFFFFF);
140 int32_t HighWord = (ImmValue >> 32) & 0xFFFFFFFF;
141
142 // Lower Registers Lower Half
143 BuildMI (*MBB, MII, MI->getDebugLoc(),
144 TII->get(Hexagon::LOi), DestLo).addImm(LowWord);
145 // Lower Registers Higher Half
146 BuildMI (*MBB, MII, MI->getDebugLoc(),
147 TII->get(Hexagon::HIi), DestLo).addImm(LowWord);
148 // Higher Registers Lower Half
149 BuildMI (*MBB, MII, MI->getDebugLoc(),
150 TII->get(Hexagon::LOi), DestHi).addImm(HighWord);
151 // Higher Registers Higher Half.
152 BuildMI (*MBB, MII, MI->getDebugLoc(),
153 TII->get(Hexagon::HIi), DestHi).addImm(HighWord);
154 MII = MBB->erase (MI);
155 continue;
156 }
157 ++MII;
158 }
159 }
160
161 return true;
162}
163
164}
165
166//===----------------------------------------------------------------------===//
167// Public Constructor Functions
168//===----------------------------------------------------------------------===//
169
170FunctionPass *
171llvm::createHexagonSplitConst32AndConst64(const HexagonTargetMachine &TM) {
172 return new HexagonSplitConst32AndConst64(TM);
173}